diff --git a/packages/agy-cli/index.mjs b/packages/agy-cli/index.mjs index 48b8457..a1f8766 100644 --- a/packages/agy-cli/index.mjs +++ b/packages/agy-cli/index.mjs @@ -3449,7 +3449,9 @@ function inspectToolTurn(results, body) { } const rawText = blockText(block) if (!seenResult && message.role === "user" && rawText.trim()) { - throw new UnsupportedContent("text beside a live tool_result; send it after the tool response completes") + // A user can steer a pending tool turn from either side of a tool_result. + // Replay the instruction as a user prompt, never as an MCP tool response. + userSteered = true } const text = stripMagpiePlaceholderNotice(rawText) if (text !== rawText && !notices.includes(MAGPIE_PLACEHOLDER_NOTICE)) { @@ -3520,7 +3522,8 @@ function inspectToolTurn(results, body) { } else if (!isNoise(rest)) { if ((hookOutput.test(rest) || claudeMetaFeedback.test(rest)) && !userInterruption.test(rest)) { notices.push(rest) - } else if (userSteered || stoppedByHookOrUser || notices.some((item) => hookStoppedContinuation.test(item))) { + } else if (message.role === "user" || userSteered || stoppedByHookOrUser || + notices.some((item) => hookStoppedContinuation.test(item))) { userSteered = true } else { throw new UnsupportedContent("text beside a live tool_result; send it after the tool response completes") diff --git a/packages/agy-cli/test/compat.mjs b/packages/agy-cli/test/compat.mjs index 7d0010c..d58671a 100644 --- a/packages/agy-cli/test/compat.mjs +++ b/packages/agy-cli/test/compat.mjs @@ -46,6 +46,22 @@ test("a companion cannot attach to a different preceding tool", () => { } }) +test("ordinary user text beside live tool results steers a replay without becoming a tool response", () => { + const call = result("call_read", "Read", "file contents") + const instruction = { type: "text", text: "Continue reviewing the image pipeline" } + for (const blocks of [[call.block, instruction], [instruction, call.block]]) { + const body = bodyFor(blocks) + const inspected = inspectToolTurn([call], body) + assert.equal(inspected.userSteered, true, "mixed user text requires a fresh user prompt") + assert.deepEqual(inspected.results[0].result.content, call.result.content, + "user instructions must not be attached to an MCP function response") + const replay = renderInitialPromptParts(body, null, true).map(part => part.text || "").join("") + assert.ok(replay.includes("Continue reviewing the image pipeline"), + "the replay must carry the original user instruction") + assert.ok(replay.includes("file contents"), "the replay must retain the real tool result") + } +}) + test("a partial ID rename maps by full history without discarding the live ID anchor", () => { const original = [ { type: "tool_use", id: "original_a", name: "Read", input: { path: "a" } }, diff --git a/packages/agy-cli/test/smoke.mjs b/packages/agy-cli/test/smoke.mjs index 0b54c79..c4f8f29 100644 --- a/packages/agy-cli/test/smoke.mjs +++ b/packages/agy-cli/test/smoke.mjs @@ -1286,23 +1286,15 @@ for (const change of ["tools", "model"]) { { const firstBody = { model, messages: [{ role: "user", content: "TOOL extra text" }], tools: [echoTool] } const firstEvents = (await readSSE(await callFetch(firstBody))).events - const invalidBody = continuation(firstBody, firstEvents, "result", { extraText: true }) - invalidBody.messages.at(-1).content.at(-1).text += "\nbackground completed" - const beforeResult = continuation(firstBody, firstEvents, "result") - beforeResult.messages.at(-1).content.unshift({ type: "text", text: "too early" }) - const hookAndUser = continuation(firstBody, firstEvents, "result") - hookAndUser.messages.at(-1).content.push({ type: "text", text: "PreToolUse:Read hook additional context: hook output\n\nThe user sent a new message while you were working:\nstop now" }) - const systemBeforeResult = continuation(firstBody, firstEvents, "result") - systemBeforeResult.messages.at(-1).content.unshift({ type: "text", text: "too early" }) - const systemAndUser = continuation(firstBody, firstEvents, "result") - systemAndUser.messages.at(-1).content.push({ type: "text", text: "catalog changed\n\nordinary user instruction" }) const unclosedSystem = continuation(firstBody, firstEvents, "result") unclosedSystem.messages.at(-1).content.push({ type: "text", text: "not a complete protocol notice" }) - const rejected = await Promise.all([invalidBody, beforeResult, hookAndUser, systemBeforeResult, systemAndUser, unclosedSystem].map(body => callFetch(body))) - check("unsupported user text and malformed or pre-result notices return 400", rejected.every(response => response.status === 400), rejected.map(response => response.status).join(",")) + const rejected = await callFetch(unclosedSystem) + check("unclosed protocol notices beside tool results still return 400", rejected.status === 400, `got ${rejected.status}`) + if (rejected.status !== 200) await rejected.text() const validBody = continuation(firstBody, firstEvents, "result") - const validEvents = (await readSSE(await callFetch(validBody))).events - check("valid result can continue after rejected extra text", stopOf(validEvents) === "end_turn") + const validRes = await callFetch(validBody) + const validEvents = validRes.status === 200 ? (await readSSE(validRes)).events : [] + check("valid result can continue after rejected malformed notice", validRes.status === 200 && stopOf(validEvents) === "end_turn") } for (const location of ["inline", "system", "developer", "user"]) { @@ -2037,6 +2029,32 @@ for (const location of ["inline", "system", "developer", "user"]) { check("turn after client-normalized tool_use.input reuses the same live session", hThirdRes.status === 200 && inits().length === beforeHookInits && lastOf("user").convId === hConv) + // Some harnesses (including Claude Science) put a new user instruction in + // the same message as a live tool_result, before or after the result block. + for (const placement of ["before", "after"]) { + const mixedFirst = { + model, + messages: [{ role: "user", content: `ESCCALL mixed tool text ${placement}` }], + tools: [echoTool], + } + const mixedEvents = (await readSSE(await callFetch(mixedFirst))).events + const oldSession = inits().at(-1).convId + const mixedSecond = continuation(mixedFirst, mixedEvents, "MIXED_RESULT") + const resultBlock = mixedSecond.messages.at(-1).content[0] + const newInstruction = { type: "text", text: `MIXED_USER_INSTRUCTION_${placement}` } + mixedSecond.messages.at(-1).content = placement === "before" + ? [newInstruction, resultBlock] : [resultBlock, newInstruction] + const mixedResponse = await callFetch(mixedSecond) + const mixedFinal = mixedResponse.status === 200 ? (await readSSE(mixedResponse)).events : [] + if (mixedResponse.status !== 200) await mixedResponse.text() + const replay = lastOf("user") + check(`ordinary user text ${placement} tool_result is replayed as a user instruction`, + mixedResponse.status === 200 && stopOf(mixedFinal) === "end_turn" && + replay.convId !== oldSession && replay.text.includes(newInstruction.text) && + replay.text.includes("MIXED_RESULT"), + `status=${mixedResponse.status}`) + } + // User rejects tool call in TUI and enters a new prompt merged into the same user message by Claude Code aLe(). const rFirst = { model, messages: [{ role: "user", content: "ESCCALL user rejected tool" }], tools: [echoTool] } const rEvents = (await readSSE(await callFetch(rFirst))).events @@ -2129,14 +2147,25 @@ for (const location of ["inline", "system", "developer", "user"]) { const skillTool = { name: 'Skill', description: 'Skill', input_schema: { type: 'object' } } const first = { model, messages: [{ role: 'user', content: 'SKILLCALL ordinary user steering regression' }], tools: [skillTool] } const events = (await readSSE(await callFetch(first))).events + const oldSession = inits().at(-1).convId const second = continuation(first, events, 'Launching skill: archive') second.messages.at(-1).content.push({ type: 'text', text: 'Stop this task and explain something else' }) const response = await callFetch(second) - check('plain user instructions are not treated as a Skill prompt body', response.status === 400, `got ${response.status}`) - if (response.status === 400) { + if (response.status === 200) { + const final = (await readSSE(response)).events + const replay = lastOf('user') + const skillResult = logs().filter(event => event.type === 'mcpResult' && event.convId === oldSession).at(-1) + const skillOutput = skillResult?.result?.content?.map(part => part.text || '').join('') || '' + check('plain user instructions beside Skill results replay separately from the MCP result', + replay.convId !== oldSession && + replay.text.includes('Stop this task and explain something else') && + !skillOutput.includes('Stop this task and explain something else')) + if (stopOf(final) === 'tool_use') await drive(second, final, 'finish replayed skill') + } else { + check('plain user instructions beside Skill results replay separately from the MCP result', false, `got ${response.status}`) await response.text() await drive(first, events, 'cleanup ordinary Skill text') - } else await readSSE(response) + } } {