Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions src/degenerate-retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,29 @@ function releaseThrottleErrorReader(reader: ReadableStreamDefaultReader<Uint8Arr
// issue the pending action or state the final answer explicitly.
export const DEGENERATE_RETRY_NUDGE =
"[billion-context] Your previous response ended without a tool call. Continue now: take your next concrete action — issue the tool call you were about to make, or state your final answer explicitly.";
// #7440: the tag-echo shape of the degenerate turn — the model emitted ONLY
// internal render tags (conditioned on the tagged history) and the tag-echo
// filter discarded them, so the turn reached the host empty. The generic nudge
// above never mentions the tags, so the model has no signal to stop echoing
// them and the retry reproduces the same tag-only output → the #870 fatal
// error → the host re-requests the same prompt into a loop. This variant names
// the discarded tags and forbids them, which is the one signal the model needs
// to break the echo. Angles are hex-escaped per KDD #2.
export const DEGENERATE_RETRY_NUDGE_TAG_ECHO =
"[billion-context] Your previous response contained only internal \x3cacp\x3e metadata tags and was discarded. Do not output \x3cacp\x3e metadata tags. Continue now: take your next concrete action — issue the tool call you were about to make, or state your final answer explicitly in plain text.";

/** The pipe's one-shot degenerate retry can tell the refetch WHY the turn was
* empty; the refetch picks the matching nudge (the tag-echo shape needs the
* tag-forbidding variant, everything else the generic continuation push). */
export interface ContinuationRetryHint {
/** The attempt's only output was a render tag the filter stripped. */
sawStrippedEcho?: boolean;
}

/** The retry body: the forwarded body with the continuation nudge appended as a
* trailing user turn. Null when the body cannot carry one. */
export function injectContinuationNudge(protocol: WireProtocol, body: string | Buffer, model?: string): string | null {
return appendTrailingUserText(protocol, body, DEGENERATE_RETRY_NUDGE, model);
export function injectContinuationNudge(protocol: WireProtocol, body: string | Buffer, model?: string, nudge: string = DEGENERATE_RETRY_NUDGE): string | null {
return appendTrailingUserText(protocol, body, nudge, model);
}

interface ContinuationRetryOpts {
Expand Down Expand Up @@ -109,8 +127,8 @@ async function readSmallThrottleErrorBody(response: Response): Promise<Uint8Arra
* when the retry cannot be issued (unbuildable body, upstream error, client
* already gone) — the caller then passes the original empty turn through
* unchanged. */
export function makeContinuationRefetch(opts: ContinuationRetryOpts): () => Promise<ReadableStream<Uint8Array> | null> {
return async () => {
export function makeContinuationRefetch(opts: ContinuationRetryOpts): (hint?: ContinuationRetryHint) => Promise<ReadableStream<Uint8Array> | 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:
Expand All @@ -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;
Expand Down
19 changes: 15 additions & 4 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -2789,7 +2790,7 @@ export async function pipePluginChatWithStrip(
protocol: WireProtocol,
session?: Session,
log?: (msg: string) => void,
refetch?: () => Promise<ReadableStream<Uint8Array> | null>,
refetch?: (hint?: ContinuationRetryHint) => Promise<ReadableStream<Uint8Array> | 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
Expand Down Expand Up @@ -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<Uint8Array> | 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;
Expand Down Expand Up @@ -4378,7 +4385,7 @@ export async function pipePluginResponsesWithStrip(
res: ServerResponse,
session?: Session,
log?: (msg: string) => void,
refetch?: () => Promise<ReadableStream<Uint8Array> | null>,
refetch?: (hint?: ContinuationRetryHint) => Promise<ReadableStream<Uint8Array> | null>,
upstreamOrigin?: string,
// See pipePluginChatWithStrip: whole-field tool-call emission drop,
// gated on the request carrying the [ACP absorb] instruction (m00885).
Expand Down Expand Up @@ -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<Uint8Array> | 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;
Expand Down
10 changes: 6 additions & 4 deletions tests/cache-proof.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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");
Expand All @@ -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)}`);
}

Expand Down Expand Up @@ -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 {
Expand Down
139 changes: 138 additions & 1 deletion tests/plugin-empty-turn-retry.test.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<void> }> {
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<void>((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<void>((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();
}
});
Loading