diff --git a/src/degenerate-retry.ts b/src/degenerate-retry.ts index 305c37ec28..3fae26452b 100644 --- a/src/degenerate-retry.ts +++ b/src/degenerate-retry.ts @@ -42,11 +42,29 @@ function releaseThrottleErrorReader(reader: ReadableStreamDefaultReader Promise | null> { - return async () => { +export function makeContinuationRefetch(opts: ContinuationRetryOpts): (hint?: ContinuationRetryHint) => Promise | null> { + return async (hint) => { if (opts.signal.aborted) return null; // A body whose final input item is a compaction trigger must never be // re-issued: appending the nudge after it breaks the wire shape (#283: @@ -128,7 +146,10 @@ export function makeContinuationRefetch(opts: ContinuationRetryOpts): () => Prom /* unparseable body: injectContinuationNudge below reports and skips */ } } - const retryBody = injectContinuationNudge(opts.protocol, opts.body, opts.model); + // #7440: the tag-echo shape gets the tag-forbidding nudge; every other + // degenerate shape keeps the generic continuation push. + const nudge = hint?.sawStrippedEcho ? DEGENERATE_RETRY_NUDGE_TAG_ECHO : DEGENERATE_RETRY_NUDGE; + const retryBody = injectContinuationNudge(opts.protocol, opts.body, opts.model, nudge); if (retryBody === null) { opts.log("warn", `[${opts.label}] [plugin] degenerate-terminal retry skipped: request body carries no turn array`); return null; diff --git a/src/plugin.ts b/src/plugin.ts index 867038aef8..b639561311 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -63,6 +63,7 @@ function tailLooksTerminal(tail: string, protocol: WireProtocol | "responses"): /"type"\s*:\s*"response\.(?:completed|failed|incomplete)"/.test(tail); } import { degenerateTurnWarning, endsWithAnnouncement, endsWithDraftClose, requestExpectsProseSummary } from "./degenerate-turn.js"; +import type { ContinuationRetryHint } from "./degenerate-retry.js"; import { getChangelog, latestChangelog, warmChangelogOnce } from "./web/changelog.js"; import { warmBlogRefreshOnce } from "./web/blog-refresh.js"; import { PANEL_BOX_FOOTER } from "./acp-panel.js"; @@ -2789,7 +2790,7 @@ export async function pipePluginChatWithStrip( protocol: WireProtocol, session?: Session, log?: (msg: string) => void, - refetch?: () => Promise | null>, + refetch?: (hint?: ContinuationRetryHint) => Promise | null>, upstreamOrigin?: string, // True when the outbound request carried the [ACP absorb] instruction: // the model may then answer with a tool call written as prose, and the @@ -3059,9 +3060,15 @@ export async function pipePluginChatWithStrip( : draftTail ? "[plugin] terminal turn ends with a compression-draft closing tag and no tool call; retrying once with a continuation nudge (#2303)" : "[plugin] degenerate terminal turn (no usable output); retrying once with a continuation nudge (#732/#821)"); + // #7440: an empty turn whose only output was a stripped render-tag echo + // gets the tag-forbidding nudge — the generic continuation push never + // mentions the tags, so the model repeats the echo and the retry dies + // too (the #870 loop). A stalled-tail turn delivered visible prose, so + // it keeps the generic push. + const hint: ContinuationRetryHint | undefined = !stalledTail && sawStrippedEcho ? { sawStrippedEcho: true } : undefined; let next: ReadableStream | null = null; try { - next = await refetch(); + next = await refetch(hint); } catch (e) { log?.(`[plugin] degenerate-terminal retry failed (${e instanceof Error ? e.message : String(e)}); passing the empty turn through`); return false; @@ -4378,7 +4385,7 @@ export async function pipePluginResponsesWithStrip( res: ServerResponse, session?: Session, log?: (msg: string) => void, - refetch?: () => Promise | null>, + refetch?: (hint?: ContinuationRetryHint) => Promise | null>, upstreamOrigin?: string, // See pipePluginChatWithStrip: whole-field tool-call emission drop, // gated on the request carrying the [ACP absorb] instruction (m00885). @@ -4791,9 +4798,13 @@ export async function pipePluginResponsesWithStrip( : draftTail ? "[plugin] terminal turn ends with a compression-draft closing tag and no function call; retrying once with a continuation nudge (#2303)" : "[plugin] degenerate terminal turn (no visible output); retrying once with a continuation nudge (#732/#821)"); + // #7440: same as the chat-pipe twin — an empty turn whose only output + // was a stripped render-tag echo gets the tag-forbidding nudge; a + // stalled-tail turn delivered visible prose and keeps the generic push. + const hint: ContinuationRetryHint | undefined = !stalledTail && sawStrippedEcho ? { sawStrippedEcho: true } : undefined; let next: ReadableStream | null = null; try { - next = await refetch(); + next = await refetch(hint); } catch (e) { log?.(`[plugin] degenerate-terminal retry failed (${e instanceof Error ? e.message : String(e)}); passing the empty turn through`); return false; diff --git a/tests/cache-proof.test.ts b/tests/cache-proof.test.ts index 2ce716282f..cada3259c3 100644 --- a/tests/cache-proof.test.ts +++ b/tests/cache-proof.test.ts @@ -96,7 +96,7 @@ import { getSession } from "../src/session.ts"; import { conflictEventsOf } from "../src/conflict-watch.ts"; import type { ProxyOptions } from "../src/config.ts"; import { _setForTest as setRegistryForTest } from "../src/registry.ts"; -import { DEGENERATE_RETRY_NUDGE } from "../src/degenerate-retry.ts"; +import { DEGENERATE_RETRY_NUDGE, DEGENERATE_RETRY_NUDGE_TAG_ECHO } from "../src/degenerate-retry.ts"; import { TAG_OPEN } from "../src/chain-checkpoint.ts"; import { rmrf } from "./tmp-rm.ts"; @@ -522,7 +522,7 @@ function checkFoldNext(wire: Wire, bodies: string[], field: string, i: number, l * Contract proven here: element count unchanged, every element before the * last byte-identical, and the last user turn's text extended by exactly * "\n\n" + nudge — zero churn anywhere else. */ -function checkDegenerate(wire: Wire, bodies: string[], field: string, i: number, lines: string[]): void { +function checkDegenerate(wire: Wire, bodies: string[], field: string, i: number, lines: string[], expectedNudge: string = DEGENERATE_RETRY_NUDGE): void { const orig = bodies[i - 1]!; const retry = bodies[i]!; const OB = Buffer.from(orig, "utf8"); @@ -539,7 +539,7 @@ function checkDegenerate(wire: Wire, bodies: string[], field: string, i: number, assert.ok(tailEq(OB, RB, O, R), `${wire} pair#${i}->#${i + 1}: refetch mutated the post-array suffix`); const rLast = JSON.parse(elText(RB, R, R.elems.length - 1)) as Item; assert.equal(rLast.role, "user", `${wire} pair#${i}->#${i + 1}: refetch replaced the last user turn's role`); - assert.equal(String(rLast.content), `${oLast.content}\n\n${DEGENERATE_RETRY_NUDGE}`, `${wire} pair#${i}->#${i + 1}: nudge not merged verbatim ("\\n\n" + nudge) into the last user turn`); + assert.equal(String(rLast.content), `${oLast.content}\n\n${expectedNudge}`, `${wire} pair#${i}->#${i + 1}: nudge not merged verbatim ("\\n\n" + nudge) into the last user turn`); lines.push(`proof[${wire}] pair#${i}->#${i + 1} DEGEN ok nudge-merged-into-trailing-user-turn zero-other-churn sha=${sha16(retry)}`); } @@ -1105,7 +1105,9 @@ test("cache proof (degenerate refetch, chat wire): re-request is a pure tail app assert.equal(bodies.length, 3, `expected original + refetch (got ${bodies.length} bodies)`); const lines: string[] = []; checkGrowth("chat", bodies, "messages", 1, lines); - checkDegenerate("chat", bodies, "messages", 2, lines); + // #7440: the degenerate turn here is a tag-only echo, so the refetch + // carries the tag-forbidding nudge, not the generic continuation push. + checkDegenerate("chat", bodies, "messages", 2, lines, DEGENERATE_RETRY_NUDGE_TAG_ECHO); for (const l of lines) console.log(l); console.log(`proof[chat-degen] VERDICT pairs=2 growth=1 refetches=1 unexplainedDivergences=0`); } finally { diff --git a/tests/plugin-empty-turn-retry.test.ts b/tests/plugin-empty-turn-retry.test.ts index cf3625956a..b3a88e7b6a 100644 --- a/tests/plugin-empty-turn-retry.test.ts +++ b/tests/plugin-empty-turn-retry.test.ts @@ -1,8 +1,9 @@ import { test } from "node:test"; import assert from "node:assert/strict"; +import http from "node:http"; import { pipePluginChatWithStrip, pipePluginResponsesWithStrip } from "../src/plugin.ts"; import { setLogCapture } from "../src/logger.ts"; -import { DEGENERATE_RETRY_NUDGE, injectContinuationNudge } from "../src/degenerate-retry.ts"; +import { DEGENERATE_RETRY_NUDGE, DEGENERATE_RETRY_NUDGE_TAG_ECHO, injectContinuationNudge, makeContinuationRefetch, type ContinuationRetryHint } from "../src/degenerate-retry.ts"; import type { Session } from "../src/session.ts"; // The plugin pipe is what serves plugin-mode agents (this machine's omp @@ -573,3 +574,139 @@ test("plugin chat (anthropic) defaults to a native error event, not a synthetic assert.ok(text.includes('"code":"stream_error"'), "the error carries the stream_error code"); assert.ok(!text.includes("[ACP] stream error"), "no synthetic completed-turn error block by default"); }); + +// #7440: the tag-echo shape of the degenerate turn — the model emitted ONLY a +// render tag, the filter emptied it, and the retry must carry the tag-forbidding +// nudge (not the generic continuation push) or the model repeats the echo and +// the retry dies too. The pipe tells the refetch WHY the turn was empty via a +// hint; these tests pin both halves of that contract. + +test("plugin chat passes the tag-echo hint so the retry nudge forbids the tags (#7440)", async () => { + const out: string[] = []; + let hint: ContinuationRetryHint | undefined = {}; + let calls = 0; + const refetch = (h?: ContinuationRetryHint) => { + calls += 1; + hint = h; + return Promise.resolve(streamOf(proseTurn("real answer after the nudge"))); + }; + await pipePluginChatWithStrip(streamOf(echoOnlyTurn()), makeRes(out), "openai", makeSession(), undefined, refetch); + assert.equal(calls, 1, "the tag-echo turn is retried once"); + assert.deepEqual(hint, { sawStrippedEcho: true }, "the tag-echo shape passes the hint to the refetch"); + assert.ok(!out.join("").includes("m00155"), "the echoed tag never leaks"); +}); + +test("plugin chat does not pass the tag-echo hint for a silent-thinking turn (#7440)", async () => { + const out: string[] = []; + let hint: ContinuationRetryHint | undefined = {}; + let calls = 0; + const refetch = (h?: ContinuationRetryHint) => { + calls += 1; + hint = h; + return Promise.resolve(streamOf(proseTurn("the continuation"))); + }; + // A reasoning-only terminal (openai `reasoning_content`, zero visible text) + // is the silent-thought shape: retried, but with the GENERIC nudge — the + // model emitted no tag, so the tag-forbidding variant would be noise. + const events = [chatChunk({ role: "assistant" }), chatChunk({ reasoning_content: "a silent thought" }), chatStop(), DONE]; + await pipePluginChatWithStrip(streamOf(events), makeRes(out), "openai", makeSession(), undefined, refetch); + assert.equal(calls, 1, "the silent-thinking turn is retried"); + assert.equal(hint, undefined, "a thinking-only degenerate turn keeps the generic nudge (no hint)"); +}); + +test("plugin responses passes the tag-echo hint so the retry nudge forbids the tags (#7440)", async () => { + const out: string[] = []; + let hint: ContinuationRetryHint | undefined = {}; + let calls = 0; + const refetch = (h?: ContinuationRetryHint) => { + calls += 1; + hint = h; + return Promise.resolve(streamOf(responsesProseTurn("real answer after the nudge"))); + }; + await pipePluginResponsesWithStrip(streamOf(responsesEchoOnlyTurn()), makeRes(out), makeSession(), undefined, refetch); + assert.equal(calls, 1, "the tag-echo turn is retried once"); + assert.deepEqual(hint, { sawStrippedEcho: true }, "the responses pipe passes the hint to the refetch"); + assert.ok(!out.join("").includes("m00155"), "the echoed tag never leaks"); +}); + +test("the tag-echo retry nudge forbids the render tags and is injected as a trailing user turn (#7440)", () => { + const openaiBody = JSON.stringify({ model: "m", messages: [{ role: "user", content: "hi" }] }); + const merged = JSON.parse(injectContinuationNudge("openai", openaiBody, undefined, DEGENERATE_RETRY_NUDGE_TAG_ECHO)!) as { messages: { role: string; content: string }[] }; + assert.equal(merged.messages.length, 1, "merged into the existing user turn"); + assert.ok(merged.messages[0]!.content.startsWith("hi"), "the original text is preserved"); + assert.ok(merged.messages[0]!.content.includes("Do not output \x3cacp\x3e metadata tags"), `the tag-forbidding instruction is present, got: ${merged.messages[0]!.content}`); + assert.ok(merged.messages[0]!.content.includes("plain text"), "it tells the model to answer in plain text"); + // The default (no hint) keeps the generic nudge, tag-free. + const generic = JSON.parse(injectContinuationNudge("openai", openaiBody)!) as { messages: { content: string }[] }; + assert.ok(generic.messages[0]!.content.includes("ended without a tool call"), "the default nudge is unchanged"); + assert.ok(!generic.messages[0]!.content.includes("Do not output \x3cacp\x3e"), "the default nudge does not mention the tags"); +}); + +/** A server that records the request BODY of every hit, for the nudge-selection + * pin: the refetch must ship the tag-forbidding nudge on the wire, not just in + * the in-memory body. */ +async function bodyRecordingServer(): Promise<{ url: string; bodies: string[]; close: () => Promise }> { + const bodies: string[] = []; + const server = http.createServer((req, res) => { + let raw = ""; + req.on("data", (c) => { raw += c; }); + req.on("end", () => { + bodies.push(raw); + res.writeHead(200, { "content-type": "text/event-stream" }); + res.end("data: [DONE]\n\n"); + }); + }); + await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); + const addr = server.address(); + if (typeof addr !== "object" || addr === null) throw new Error("no addr"); + return { + url: `http://127.0.0.1:${addr.port}/v1/chat/completions`, + bodies, + close: () => new Promise((resolve) => server.close(() => resolve())), + }; +} + +test("makeContinuationRefetch ships the tag-forbidding nudge when the hint is set (#7440)", async () => { + const srv = await bodyRecordingServer(); + try { + const refetch = makeContinuationRefetch({ + protocol: "openai", + body: JSON.stringify({ model: "m", messages: [{ role: "user", content: "hi" }] }), + model: "m", + upstreamUrl: srv.url, + reqHeaders: {}, + signal: new AbortController().signal, + log: () => {}, + label: "test", + }); + await refetch({ sawStrippedEcho: true }); + assert.equal(srv.bodies.length, 1, "one upstream hit"); + const sent = JSON.parse(srv.bodies[0]!) as { messages: { content: string }[] }; + assert.ok(sent.messages[0]!.content.includes("Do not output \x3cacp\x3e metadata tags"), `the wire body carries the tag-forbidding nudge, got: ${sent.messages[0]!.content}`); + } finally { + await srv.close(); + } +}); + +test("makeContinuationRefetch ships the generic nudge when the hint is absent (#7440)", async () => { + const srv = await bodyRecordingServer(); + try { + const refetch = makeContinuationRefetch({ + protocol: "openai", + body: JSON.stringify({ model: "m", messages: [{ role: "user", content: "hi" }] }), + model: "m", + upstreamUrl: srv.url, + reqHeaders: {}, + signal: new AbortController().signal, + log: () => {}, + label: "test", + }); + await refetch(); + assert.equal(srv.bodies.length, 1, "one upstream hit"); + const sent = JSON.parse(srv.bodies[0]!) as { messages: { content: string }[] }; + assert.ok(sent.messages[0]!.content.includes("ended without a tool call"), "the wire body carries the generic nudge"); + assert.ok(!sent.messages[0]!.content.includes("Do not output \x3cacp\x3e"), "the wire body does not mention the tags"); + } finally { + await srv.close(); + } +});