From 9a96799efb5a71882683b01bf05a803b2741caa1 Mon Sep 17 00:00:00 2001 From: DaWei Liu Date: Wed, 11 Nov 2026 04:33:33 +0800 Subject: [PATCH] agy-cli: user text beside a live tool result replays the round in a new prompt (#909, #951) A user message that carried ordinary text beside a live tool_result, before or after the result block, was refused with 400 "text beside a live tool_result; send it after the tool response completes" (maxleebeta's DeepSeek-harness agent, #909; Claude Science does the same). inspectToolTurn now marks such a turn userSteered, so the bridge takes the existing replay path: the old session is stopped, its pending calls are not resolved, and a new session/prompt carries the round, the real tool result included. The user's text is never spliced into an MCP function response. The same goes for plain text after a Skill result, and for a hook line that carries "The user sent a new message while you were working". Still refused: an unclosed protocol notice (... with no end tag) on either side of the result, a companion (ARGUMENTS:, [Image: ...], Tool loaded.) with no matching preceding tool, and an image before the first tool_result. A turn whose tools also grew (#942) takes the same single replay. Changed behaviour beyond #909: a protocol notice, token line or hook-success line placed before the tool_result (previously 400) now replays in a new session, where the same line after the result is forwarded in-session. Tests: compat.mjs checks both placements are userSteered, the MCP result is unchanged, and the replay carries the instruction and the result. smoke.mjs drives both placements and the Skill case through the fake agy and checks a new convId whose prompt carries both. Five smoke checks that asserted 400 for text the bridge now replays were removed; the unclosed-notice 400 check stays. Run at head 04692ce0a74d (merged onto origin/main d5f4214e) on mote, bun 1.4.2: - bun test packages/agy-cli: 8 pass, 0 fail, at the head and merged - bun scripts/check.mjs agy-cli: ok, at the head and merged - Broken by hand (index.mjs reverted, tests kept): compat 1 fail (the new test); smoke 3 FAILURES of 321 (before, after, Skill: status 400) - Probe of inspectToolTurn merged: the user text appears once in the replay and the result once; MCP content stays "file contents" Not run: the root bun test suite (mote's suite queue was long); the reporter's own request bytes (never obtained, #909 stays open for them). Co-authored-by: Guuuu233 <308638291+Guuuu233@users.noreply.github.com> --- packages/agy-cli/index.mjs | 7 +++- packages/agy-cli/test/compat.mjs | 16 ++++++++ packages/agy-cli/test/smoke.mjs | 63 +++++++++++++++++++++++--------- 3 files changed, 67 insertions(+), 19 deletions(-) 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) + } } {