From 975f6b32f826cb24d1f8dba6a4a550efe34d79dc Mon Sep 17 00:00:00 2001 From: ci-check Date: Mon, 26 Oct 2026 06:03:11 +0800 Subject: [PATCH 1/2] fix(openai-lane): name the compress tool in the draft-tail retry nudge (#4764) The #2303 draft-tail guard fired on compression turns with the generic degenerate nudge ("take your next concrete action"), which OpenAI-lane models read as permission to do anything else - Edit/Bash instead of the pending compress call, so the compression never happened. - split the draft-tail predicate into a kind classifier (complete draft ending at the summary close tag vs truncated draft ending at the analysis close tag) and give each shape its own recovery nudge naming the compress tool and its content argument; proxy loop and both plugin pipes - add a vendor-agnostic tag-shape twin to the #2612 prose-summary escape hatch, proxy lane only; plugin pipes stay lexical-only because their test view is the whole shipped body carrying previously injected nudges verbatim - thread an optional nudge text through injectContinuationNudge / makeContinuationRefetch; omitted argument keeps the byte-identical generic wording on every non-draft-tail path (#732/#821) --- src/degenerate-retry.ts | 29 +- src/degenerate-turn.ts | 37 ++ src/loop/core.ts | 15 +- src/plugin.ts | 28 +- tests/issue4764-draft-tail-nudge.test.ts | 411 +++++++++++++++++++++++ 5 files changed, 502 insertions(+), 18 deletions(-) create mode 100644 tests/issue4764-draft-tail-nudge.test.ts diff --git a/src/degenerate-retry.ts b/src/degenerate-retry.ts index ba3ea55498..bf82362f40 100644 --- a/src/degenerate-retry.ts +++ b/src/degenerate-retry.ts @@ -23,10 +23,27 @@ import { appendTrailingUserText } from "./wire-body.js"; 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."; +// #4764: for the draft-tail shape the generic nudge above steered wrong — on a +// compression turn the model read "take your next concrete action" as +// permission to do anything else (observed: Edit/Bash/TaskOutput instead of +// the compress call; the draft sat in history as an ordinary assistant message +// and the compression never happened). These variants name the pending action; +// they are picked by draftTailKind (a complete draft already holds the finished +// summary written in the wrong channel, a truncated one stopped before the +// summary block). Like every injected nudge, neither may contain literal +// / tags — these texts ride later request views, where the +// tag shape is the #2612 suppression signal. +export const DRAFT_TAIL_RETRY_NUDGE_COMPLETE = + "[billion-context] Your previous response ended without a tool call. You already wrote the full compression summary as prose — do not extend or repeat it. Submit it now by calling the `compress` tool, with that summary as its `content` argument."; +export const DRAFT_TAIL_RETRY_NUDGE_TRUNCATED = + "[billion-context] Your previous response ended without a tool call: the analysis part of the compression draft finished, but the summary was never written. Stop writing prose now — issue the `compress` tool call immediately, with the summary as its `content` argument."; + /** 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): string | null { - return appendTrailingUserText(protocol, body, DEGENERATE_RETRY_NUDGE); + * trailing user turn. Null when the body cannot carry one. #4764: nudgeText + * lets the draft-tail callers steer the retry at the pending action instead of + * the generic wording; omitted callers keep the byte-identical default. */ +export function injectContinuationNudge(protocol: WireProtocol, body: string | Buffer, nudgeText: string = DEGENERATE_RETRY_NUDGE): string | null { + return appendTrailingUserText(protocol, body, nudgeText); } interface ContinuationRetryOpts { @@ -55,8 +72,8 @@ interface ContinuationRetryOpts { * 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 | null> { - return async () => { +export function makeContinuationRefetch(opts: ContinuationRetryOpts): (nudgeText?: string) => Promise | null> { + return async (nudgeText?: string) => { 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: @@ -74,7 +91,7 @@ export function makeContinuationRefetch(opts: ContinuationRetryOpts): () => Prom /* unparseable body: injectContinuationNudge below reports and skips */ } } - const retryBody = injectContinuationNudge(opts.protocol, opts.body); + const retryBody = injectContinuationNudge(opts.protocol, opts.body, nudgeText); 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/degenerate-turn.ts b/src/degenerate-turn.ts index 2092f58f32..1f1112be20 100644 --- a/src/degenerate-turn.ts +++ b/src/degenerate-turn.ts @@ -72,6 +72,22 @@ export function endsWithDraftClose(text: string): boolean { return text.length > 0 && DRAFT_CLOSE_TAIL.test(text); } +// #4764: WHICH closing tag sits at the tail decides what the model already has. +// A tail means the draft is COMPLETE — the full summary exists, just +// written in the wrong channel; a tail means the draft stopped +// before the summary block ever started. The retry nudge must name that +// difference (#2303's single generic nudge ignored both, and on OpenAI-lane +// compression turns the model read "take your next concrete action" as +// permission to do anything else — Edit/Bash instead of the compress call). +export type DraftTailKind = "complete" | "truncated"; +const DRAFT_CLOSE_COMPLETE_TAIL = /\x3c\/summary\x3e\s*$/i; +export function draftTailKind(text: string): DraftTailKind | null { + if (text.length === 0) return null; + if (DRAFT_CLOSE_COMPLETE_TAIL.test(text)) return "complete"; + if (DRAFT_CLOSE_TAIL.test(text)) return "truncated"; + return null; +} + // #2612: the #2303 draft-tail shape has a legitimate producer — the CLIENT's // own summarization request. Claude Code's compaction (/compact, auto-compact, // precomputed compression) instructs the model, in the final user message, to @@ -92,6 +108,27 @@ export function requestExpectsProseSummary(requestText: string | undefined): boo return typeof requestText === "string" && requestText.length > 0 && CLIENT_SUMMARY_INSTRUCTION.test(requestText); } +// #4764: CLIENT_SUMMARY_INSTRUCTION is vendor-brittle — it was written from +// Claude Code's two compaction wordings, so hosts whose compaction instruction +// names the tags without either phrase get no suppression and their compliant +// answer gets retried like a stalled draft (WorkBuddy/CodeBuddy kernel: same +// draft shape, consumed fine by its own envelope). The tag SHAPE in the final +// user message is the vendor-agnostic signal that a summary-shaped prose reply +// is expected. PROXY LANE ONLY: the loop's coreMessages view carries no bili- +// injected text (the I1 evidence in #4764 proves injections ride the outbound +// body, not this view), so a tag there can only come from the client. The +// plugin pipes test the WHOLE shipped body instead, which carries previously +// injected nudges verbatim — the anthropic lane note contains literal +// / tags — so a shape match there would suppress the guard +// forever; those call sites stay lexical-only on purpose. The [a-z_]* prefix +// catches vendor-suffixed markers (the WorkBuddy kernel's own structural test +// accepts , issue #4764 evidence); URLs and +// hyphenated tags deliberately do not match (':'/'.'/'-' break the run). +const PROSE_SUMMARY_TAG_SHAPE = /\x3c\/?[a-z_]*(?:summary|analysis)\b/i; +export function requestExpectsProseSummaryShape(requestText: string | undefined): boolean { + return typeof requestText === "string" && requestText.length > 0 && PROSE_SUMMARY_TAG_SHAPE.test(requestText); +} + /** #2612 proxy-lane twin: the last user message's text, where a client * compaction instruction lives (Claude Code puts it in the final user turn). * Undefined when there is no user message or it carries no text. */ diff --git a/src/loop/core.ts b/src/loop/core.ts index 39240e7cb0..b677475770 100644 --- a/src/loop/core.ts +++ b/src/loop/core.ts @@ -42,8 +42,8 @@ import { dumpsDir } from "../paths.js"; import { isStrictReasoningEcho, modelIdOf, normalizeStrictEchoBody } from "../strict-echo.js"; import { log as loggerLog } from "../logger.js"; import { promptInputTotal, type WireProtocol } from "../util.js"; -import { DEGENERATE_RETRY_NUDGE } from "../degenerate-retry.js"; -import { endsWithDraftClose, lastUserSummaryInstruction, requestExpectsProseSummary } from "../degenerate-turn.js"; +import { DEGENERATE_RETRY_NUDGE, DRAFT_TAIL_RETRY_NUDGE_COMPLETE, DRAFT_TAIL_RETRY_NUDGE_TRUNCATED } from "../degenerate-retry.js"; +import { draftTailKind, endsWithDraftClose, lastUserSummaryInstruction, requestExpectsProseSummary, requestExpectsProseSummaryShape } from "../degenerate-turn.js"; import { safePrefix, safeSuffix, scrubLoneSurrogatesOnWire } from "../text-safe.js"; export const MAX_LOOP_ROUNDS = 10; @@ -1081,16 +1081,21 @@ export async function* runCompressLoop( // design, not a stalled handoff; retrying it breaks the // compaction (the retry complies too → #870 error). !requestExpectsProseSummary(lastUserSummaryInstruction(coreMessages)) && + // #4764: vendor-agnostic twin for hosts whose compaction + // instruction names the tags without either Claude Code + // phrasing (proxy-lane view only — see the predicate). + !requestExpectsProseSummaryShape(lastUserSummaryInstruction(coreMessages)) && !signal?.aborted ) { degenerateRetried = true; - ctx.log(`[acp-loop] round ${round}: terminal turn ends with a compression-draft closing tag and no tool call; retrying once with continuation nudge (#2303)`); - loggerLog("warn", `[acp-loop] draft-tail auto-retry round ${round} (session ${ctx.session.id})`); + const tailKind = draftTailKind(assistantText) ?? "complete"; + ctx.log(`[acp-loop] round ${round}: terminal turn ends with a compression-draft closing tag (${tailKind === "truncated" ? "draft cut off before the summary" : "complete draft written as prose"}) and no tool call; retrying once with the compress-naming continuation nudge (#2303/#4764)`); + loggerLog("warn", `[acp-loop] draft-tail auto-retry round ${round} kind=${tailKind} (session ${ctx.session.id})`); const nudge: CoreMessage = { id: `acp_draft_tail_retry_r${round}`, role: "user", contentType: "text", - text: DEGENERATE_RETRY_NUDGE, + text: tailKind === "truncated" ? DRAFT_TAIL_RETRY_NUDGE_TRUNCATED : DRAFT_TAIL_RETRY_NUDGE_COMPLETE, }; try { const retryBody = await buildLoopRequestBody([nudge]); diff --git a/src/plugin.ts b/src/plugin.ts index 55024362d1..7b0fde69a4 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -56,7 +56,8 @@ function tailLooksTerminal(tail: string, protocol: WireProtocol | "responses"): return /event: response\.(?:completed|failed|incomplete)/.test(tail.slice(0, 64)) || /"type"\s*:\s*"response\.(?:completed|failed|incomplete)"/.test(tail); } -import { degenerateTurnWarning, endsWithDraftClose, requestExpectsProseSummary } from "./degenerate-turn.js"; +import { DRAFT_TAIL_RETRY_NUDGE_COMPLETE, DRAFT_TAIL_RETRY_NUDGE_TRUNCATED } from "./degenerate-retry.js"; +import { degenerateTurnWarning, draftTailKind, endsWithDraftClose, requestExpectsProseSummary } from "./degenerate-turn.js"; import { getChangelog, latestChangelog, warmChangelogOnce } from "./web/changelog.js"; import { PANEL_BOX_FOOTER } from "./acp-panel.js"; import { excludeDeadRefRanges } from "./acp-status.js"; @@ -2364,7 +2365,7 @@ export async function pipePluginChatWithStrip( protocol: WireProtocol, session?: Session, log?: (msg: string) => void, - refetch?: () => Promise | null>, + refetch?: (nudgeText?: string) => 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 @@ -2547,7 +2548,15 @@ export async function pipePluginChatWithStrip( // #2612: the request's OWN instruction can make this shape the correct // answer — a client compaction asking for a plain-text block // must not be retried (its compliant reply IS a draft tail). + // #4764: lexical escape hatch only here (NOT the shape twin) — this + // requestText is the WHOLE shipped body, which carries previously + // injected nudges verbatim; see requestExpectsProseSummaryShape. const draftTail = visibleTextChars > 0 && !sawToolUse && endsWithDraftClose(proseAcc) && !requestExpectsProseSummary(requestText); + // #4764: steer the retry at the pending compress call instead of the + // generic "take your next concrete action" (which on OpenAI-lane + // compression turns read as permission to do anything else). + const draftTailShape = draftTail ? draftTailKind(proseAcc) ?? "complete" : undefined; + const draftNudge = draftTailShape === "truncated" ? DRAFT_TAIL_RETRY_NUDGE_TRUNCATED : draftTailShape === "complete" ? DRAFT_TAIL_RETRY_NUDGE_COMPLETE : undefined; // #2756: the gate asks "did the host receive ANSWER text?" — prose that // is nothing but whitespace (a bare "\n\n" from a degenerate stop, the // B-accident shape) is not an answer; the empty-turn retry must fire for @@ -2584,11 +2593,11 @@ export async function pipePluginChatWithStrip( } degenerateRetried = true; log?.(draftTail - ? "[plugin] terminal turn ends with a compression-draft closing tag and no tool call; retrying once with a continuation nudge (#2303)" + ? `[plugin] terminal turn ends with a compression-draft closing tag (${draftTailShape === "truncated" ? "truncated before the summary" : "complete draft written as prose"}) and no tool call; retrying once with the compress-naming continuation nudge (#2303/#4764)` : "[plugin] degenerate terminal turn (no usable output); retrying once with a continuation nudge (#732/#821)"); let next: ReadableStream | null = null; try { - next = await refetch(); + next = await refetch(draftNudge); } catch (e) { log?.(`[plugin] degenerate-terminal retry failed (${e instanceof Error ? e.message : String(e)}); passing the empty turn through`); return false; @@ -3579,7 +3588,7 @@ export async function pipePluginResponsesWithStrip( res: ServerResponse, session?: Session, log?: (msg: string) => void, - refetch?: () => Promise | null>, + refetch?: (nudgeText?: string) => Promise | null>, upstreamOrigin?: string, // See pipePluginChatWithStrip: whole-field tool-call emission drop, // gated on the request carrying the [ACP absorb] instruction (m00885). @@ -3907,7 +3916,12 @@ export async function pipePluginResponsesWithStrip( // compression-draft closing tag with no function call is non-converged. // #2612: except when the request itself demands that shape (client // compaction summary instruction) — see the chat-pipe twin. + // #4764: lexical escape hatch only here (NOT the shape twin) — see the + // chat-pipe twin above. const draftTail = (visibleTextChars > 0 || heldVisibleChars > 0) && !sawFunctionCall && endsWithDraftClose(proseAcc) && !requestExpectsProseSummary(requestText); + // #4764: same steer as the chat-pipe twin. + const draftTailShape = draftTail ? draftTailKind(proseAcc) ?? "complete" : undefined; + const draftNudge = draftTailShape === "truncated" ? DRAFT_TAIL_RETRY_NUDGE_TRUNCATED : draftTailShape === "complete" ? DRAFT_TAIL_RETRY_NUDGE_COMPLETE : undefined; // #2756: same question as the chat-pipe twin — "did the host receive // ANSWER text?". Reasoning inlined into item.content[] (vLLM/ // llama.cpp reasoning_text) and whitespace-only output_text (a bare @@ -3932,11 +3946,11 @@ export async function pipePluginResponsesWithStrip( } degenerateRetried = true; log?.(draftTail - ? "[plugin] terminal turn ends with a compression-draft closing tag and no function call; retrying once with a continuation nudge (#2303)" + ? `[plugin] terminal turn ends with a compression-draft closing tag (${draftTailShape === "truncated" ? "truncated before the summary" : "complete draft written as prose"}) and no function call; retrying once with the compress-naming continuation nudge (#2303/#4764)` : "[plugin] degenerate terminal turn (no visible output); retrying once with a continuation nudge (#732/#821)"); let next: ReadableStream | null = null; try { - next = await refetch(); + next = await refetch(draftNudge); } 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/issue4764-draft-tail-nudge.test.ts b/tests/issue4764-draft-tail-nudge.test.ts new file mode 100644 index 0000000000..c024d70a0d --- /dev/null +++ b/tests/issue4764-draft-tail-nudge.test.ts @@ -0,0 +1,411 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import type { Config, CoreMessage } from "acp-kernel"; +import { createCore, createInitialState } from "acp-kernel"; +import { pipePluginChatWithStrip, pipePluginResponsesWithStrip } from "../src/plugin.ts"; +import { + DEGENERATE_RETRY_NUDGE, + DRAFT_TAIL_RETRY_NUDGE_COMPLETE, + DRAFT_TAIL_RETRY_NUDGE_TRUNCATED, + injectContinuationNudge, + makeContinuationRefetch, +} from "../src/degenerate-retry.ts"; +import { draftTailKind, requestExpectsProseSummaryShape } from "../src/degenerate-turn.ts"; +import type { Session } from "../src/session.ts"; +import { runCompressLoop, createOpenaiAdapter } from "../src/loop/index.ts"; +import { buildCompressSystemPrompt } from "../src/compress-tool.ts"; + +// #4764: the compression nudge never told OpenAI-lane models to use the +// `compress` tool, so they wrote the / draft as prose; the +// #2303 draft-tail guard then fired with the GENERIC degenerate nudge ("take +// your next concrete action"), which the model read as permission to do +// anything else — Edit/Bash instead of the compress call, and the compression +// never happened. Three fixes land here: +// 1. the draft-tail retry names the pending action, split by draft shape +// (complete draft: submit what was written; truncated: finish via the +// tool) — proxy loop AND both plugin pipes; +// 2. the #2612 escape hatch gains a vendor-agnostic tag-SHAPE twin for the +// PROXY lane only (hosts whose compaction instruction names the tags +// without either Claude Code phrasing were being retried like stalled +// drafts); plugin pipes stay lexical-only because their test view is the +// WHOLE shipped body, which carries previously injected nudges verbatim; +// 3. the generic wording is preserved byte-for-byte everywhere it is not a +// draft-tail retry (#732/#821 empty-turn shape unchanged). + +// ---------- unit: draftTailKind ---------- + +test("draftTailKind: classifies the closing tag at the tail", () => { + assert.equal(draftTailKind("\nx\n"), "truncated"); + assert.equal(draftTailKind("\nx\n\n\n"), "truncated"); + assert.equal(draftTailKind("\nx\n"), "complete"); + assert.equal(draftTailKind("prose ... "), "complete", "case-insensitive (#1731 drift)"); + assert.equal(draftTailKind(""), "truncated"); + // negatives: no draft close at the tail + assert.equal(draftTailKind(""), null); + assert.equal(draftTailKind("plain answer text"), null); + assert.equal(draftTailKind("open only"), null, "opening tag alone is not a close"); + assert.equal(draftTailKind("... "), null, "near-miss typo does not match"); + assert.equal(draftTailKind("... "), null, "longer word does not match"); +}); + +// ---------- unit: requestExpectsProseSummaryShape ---------- + +const WORKBUDDY_STYLE_INSTRUCTION = + "Please summarize the conversation so far. Put your final summary inside tags and do not call any tools."; + +test("requestExpectsProseSummaryShape: tag shape in the instruction is detected without the Claude Code phrases", () => { + assert.equal(requestExpectsProseSummaryShape(WORKBUDDY_STYLE_INSTRUCTION), true); + assert.equal(requestExpectsProseSummaryShape("end with an block"), true); + assert.equal(requestExpectsProseSummaryShape(""), true, "bare closing token counts"); + assert.equal(requestExpectsProseSummaryShape("x"), true, "legacy vendor marker substring"); + assert.equal(requestExpectsProseSummaryShape(" tags please"), true, "case-insensitive"); + // negatives + assert.equal(requestExpectsProseSummaryShape(undefined), false); + assert.equal(requestExpectsProseSummaryShape(""), false); + assert.equal(requestExpectsProseSummaryShape("write a summary of the session"), false, "the word alone is not a tag"); + assert.equal(requestExpectsProseSummaryShape("typo"), false, "near-miss typo does not match"); +}); + +// ---------- unit: nudge injection plumbing ---------- + +const OPENAI_MIN_BODY = JSON.stringify({ model: "qwen", messages: [{ role: "user", content: "hi" }] }); + +function trailingUserContent(body: string | null): string | null { + if (body === null) return null; + const parsed = JSON.parse(body) as { messages: Array<{ content: string }> }; + return parsed.messages[parsed.messages.length - 1]?.content ?? null; +} + +test("injectContinuationNudge: default keeps the generic wording byte-for-byte", () => { + // The openai wire appends the nudge to the EXISTING trailing user message + // (never a second consecutive user message). + const injected = injectContinuationNudge("openai", OPENAI_MIN_BODY); + assert.equal(trailingUserContent(injected), "hi\n\n" + DEGENERATE_RETRY_NUDGE); +}); + +test("injectContinuationNudge: explicit text replaces the generic wording", () => { + const injected = injectContinuationNudge("openai", OPENAI_MIN_BODY, DRAFT_TAIL_RETRY_NUDGE_TRUNCATED); + assert.equal(trailingUserContent(injected), "hi\n\n" + DRAFT_TAIL_RETRY_NUDGE_TRUNCATED); + assert.ok(!trailingUserContent(injected)!.includes("take your next concrete action")); +}); + +test("makeContinuationRefetch: threads the caller's nudge into the retry body", async () => { + const orig = globalThis.fetch; + const bodies: string[] = []; + globalThis.fetch = (async (_url: unknown, init?: { body?: unknown }) => { + if (typeof init?.body === "string") bodies.push(init.body); + return new Response(JSON.stringify({ choices: [] }), { status: 200 }); + }) as typeof fetch; + try { + const refetch = makeContinuationRefetch({ + protocol: "openai", + body: OPENAI_MIN_BODY, + upstreamUrl: "http://127.0.0.1:1/v1/chat/completions", + reqHeaders: {}, + signal: new AbortController().signal, + log: () => {}, + label: "testrefetch", + }); + await refetch(DRAFT_TAIL_RETRY_NUDGE_COMPLETE); + await refetch(); + assert.equal(bodies.length, 2); + assert.ok(bodies[0]!.includes(DRAFT_TAIL_RETRY_NUDGE_COMPLETE), "explicit nudge rides the retry body"); + assert.ok(!bodies[0]!.includes("take your next concrete action")); + assert.ok(bodies[1]!.includes(DEGENERATE_RETRY_NUDGE), "omitted argument falls back to the generic wording"); + } finally { + globalThis.fetch = orig; + } +}); + +// ---------- shared fixtures (mirrors issue2612-compaction-draft-tail.test.ts) ---------- + +function makeSession(): Session { + return { + id: "testsess", + protocol: "openai", + upstreamOrigin: "http://127.0.0.1:9/v1", + label: "test", + createdAt: 0, + lastUsedAt: 0, + requests: 0, + lastInputTokens: 0, + stats: {}, + dirty: false, + } as unknown as Session; +} + +function makeRes(chunks: string[]) { + return { + writes: chunks, + write(b: Buffer | string) { + chunks.push(typeof b === "string" ? b : b.toString("utf8")); + return true; + }, + end(b?: Buffer | string) { + if (b !== undefined) chunks.push(typeof b === "string" ? b : b.toString("utf8")); + }, + once() {}, + destroyed: false, + writableEnded: false, + } as unknown as import("node:http").ServerResponse; +} + +function streamOf(events: string[]): ReadableStream { + const enc = new TextEncoder(); + let i = 0; + return new ReadableStream({ + pull(controller) { + if (i < events.length) { + controller.enqueue(enc.encode(events[i])); + i += 1; + } else { + controller.close(); + } + }, + }); +} + +function chatChunk(delta: Record): string { + return `data: ${JSON.stringify({ id: "chatcmpl-1", object: "chat.completion.chunk", created: 1, model: "qwen", choices: [{ index: 0, delta, finish_reason: null }] })}\n\n`; +} + +const DONE = "data: [DONE]\n\n"; + +function chatStop(reason = "stop"): string { + return `data: ${JSON.stringify({ id: "chatcmpl-1", object: "chat.completion.chunk", created: 1, model: "qwen", choices: [{ index: 0, delta: {}, finish_reason: reason }] })}\n\n`; +} + +const sse = (event: string, data: unknown): string => `event: ${event}\ndata: ${JSON.stringify(data)}\n\n`; + +const TAG_OPEN = "\x3cacp tokens=\"247\" type=\"text\"\x3e"; +const TAG_CLOSE = "\x3c/acp\x3e"; + +/** The production shape from #4764: a COMPLETE compression draft written as + * prose — analysis block, summary block, ends with , no tool call. */ +const COMPLETE_DRAFT = "\x3canalysis\x3e\nThe session consumed its exploration outputs; the build and test results are spent.\n\x3c/analysis\x3e\n\x3csummary\x3e\nBuilt the retry lane; targeted suites green; PR pending.\n\x3c/summary\x3e"; +/** A TRUNCATED draft: the analysis finished, the summary block never started. */ +const TRUNCATED_DRAFT = "\x3canalysis\x3e\nThe session consumed its exploration outputs.\n\x3c/analysis\x3e"; + +function proseTurn(text: string): string[] { + return [chatChunk({ role: "assistant" }), chatChunk({ content: text }), chatStop(), DONE]; +} + +/** The #732/#821 empty-turn shape: the only text is a render-tag echo the + * filter empties, leaving a completed turn with nothing visible. */ +function echoOnlyTurn(): string[] { + return [chatChunk({ role: "assistant" }), chatChunk({ content: `${TAG_OPEN}m00155${TAG_CLOSE}` }), chatStop(), DONE]; +} + +function responsesDraftTurn(text: string): string[] { + return [ + sse("response.created", { type: "response.created", response: { id: "resp_1", status: "in_progress" } }), + sse("response.output_item.added", { type: "response.output_item.added", output_index: 0, item: { id: "item_1", type: "message", content: [] } }), + sse("response.content_part.added", { type: "response.content_part.added", item_id: "item_1", output_index: 0, part: { type: "output_text", text: "" } }), + sse("response.output_text.delta", { type: "response.output_text.delta", item_id: "item_1", output_index: 0, delta: text }), + sse("response.output_text.done", { type: "response.output_text.done", item_id: "item_1", output_index: 0, text }), + sse("response.completed", { type: "response.completed", response: { id: "resp_1", status: "completed", output: [{ id: "item_1", type: "message", content: [{ type: "output_text", text }] }] } }), + ]; +} + +// ---------- plugin chat pipe: the nudge the retry carries ---------- + +test("#4764 chat: complete draft tail retries with the compress-naming nudge (complete variant)", async () => { + const out: string[] = []; + const nudges: Array = []; + const refetch = (n?: string) => { + nudges.push(n); + return Promise.resolve(streamOf(proseTurn("compress issued"))); + }; + await pipePluginChatWithStrip( + streamOf(proseTurn(COMPLETE_DRAFT)), makeRes(out), "openai", makeSession(), undefined, refetch, + ); + assert.equal(nudges.length, 1, "the #2303 guard fires exactly once"); + assert.equal(nudges[0], DRAFT_TAIL_RETRY_NUDGE_COMPLETE, "steers at the pending compress call, not the generic action"); + assert.ok(!out.join("").includes("[ACP] stream error")); +}); + +test("#4764 chat: truncated draft tail retries with the truncated variant", async () => { + const out: string[] = []; + const nudges: Array = []; + const refetch = (n?: string) => { + nudges.push(n); + return Promise.resolve(streamOf(proseTurn("recover"))); + }; + await pipePluginChatWithStrip( + streamOf(proseTurn(TRUNCATED_DRAFT)), makeRes(out), "openai", makeSession(), undefined, refetch, + ); + assert.equal(nudges.length, 1); + assert.equal(nudges[0], DRAFT_TAIL_RETRY_NUDGE_TRUNCATED); +}); + +test("#4764 chat: the EMPTY-TURN shape (#732/#821) still gets the generic nudge, byte-for-byte", async () => { + const out: string[] = []; + const nudges: Array = []; + const refetch = (n?: string) => { + nudges.push(n); + return Promise.resolve(streamOf(proseTurn("recovered"))); + }; + await pipePluginChatWithStrip( + streamOf(echoOnlyTurn()), makeRes(out), "openai", makeSession(), undefined, refetch, + ); + assert.equal(nudges.length, 1, "empty-turn retry still fires"); + assert.equal(nudges[0], undefined, "no explicit text → the byte-identical generic default"); +}); + +test("#4764 chat: literal draft tags in the SHIPPED BODY do not suppress the retry (lexical-only asymmetry)", async () => { + const out: string[] = []; + const nudges: Array = []; + const refetch = (n?: string) => { + nudges.push(n); + return Promise.resolve(streamOf(proseTurn("recovered"))); + }; + // A previously injected anthropic-lane note rides the host's resent history + // verbatim — literal / tokens, neither Claude Code + // phrase. Extending the shape twin to this view would suppress the guard + // forever; the retry must fire. + const requestText = `earlier turns ... Never emit or tags in your reply text — they are not parsed ... current request`; + await pipePluginChatWithStrip( + streamOf(proseTurn(COMPLETE_DRAFT)), makeRes(out), "openai", makeSession(), undefined, refetch, + undefined, false, requestText, + ); + assert.equal(nudges.length, 1, "the guard is NOT suppressed by injected-note tags in history"); + assert.equal(nudges[0], DRAFT_TAIL_RETRY_NUDGE_COMPLETE); +}); + +// ---------- plugin responses pipe: same contract ---------- + +test("#4764 responses: complete draft tail retries with the compress-naming nudge", async () => { + const out: string[] = []; + const nudges: Array = []; + const refetch = (n?: string) => { + nudges.push(n); + return Promise.resolve(streamOf([DONE])); + }; + await pipePluginResponsesWithStrip( + streamOf(responsesDraftTurn(COMPLETE_DRAFT)), makeRes(out), makeSession(), undefined, refetch, + ); + assert.equal(nudges.length, 1); + assert.equal(nudges[0], DRAFT_TAIL_RETRY_NUDGE_COMPLETE); + assert.ok(!out.join("").includes("[ACP] stream error")); +}); + +test("#4764 responses: truncated draft tail retries with the truncated variant", async () => { + const out: string[] = []; + const nudges: Array = []; + const refetch = (n?: string) => { + nudges.push(n); + return Promise.resolve(streamOf([DONE])); + }; + await pipePluginResponsesWithStrip( + streamOf(responsesDraftTurn(TRUNCATED_DRAFT)), makeRes(out), makeSession(), undefined, refetch, + ); + assert.equal(nudges.length, 1); + assert.equal(nudges[0], DRAFT_TAIL_RETRY_NUDGE_TRUNCATED); +}); + +// ---------- compress loop (proxy mode): retry body + shape escape hatch ---------- + +function makeCtx(id: string, messages: CoreMessage[]) { + return { + core: createCore(), + config: { modelContextLimit: 200000 } as Config, + messages, + session: { + id, + meta: {}, + stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens: 0, contextTokens: 0 }, + metadata: {}, + state: createInitialState(), + createdAt: Date.now(), + lastSeen: Date.now(), + blockContents: new Map(), + inFlight: 0, + persisted: false, + } as unknown as Session, + log: () => {}, + protocol: "openai" as const, + }; +} + +function openaiSse(frames: Array>): string { + return frames.map((f) => `data: ${JSON.stringify(f)}\n\n`).join("") + "data: [DONE]\n\n"; +} + +function compliantReplySse(body: string, tail: string): string { + return openaiSse([ + { id: "chatcmpl_1", object: "chat.completion.chunk", choices: [{ index: 0, delta: { role: "assistant", content: "" }, finish_reason: null }] }, + { id: "chatcmpl_1", choices: [{ index: 0, delta: { content: body }, finish_reason: null }] }, + { id: "chatcmpl_1", choices: [{ index: 0, delta: { content: tail }, finish_reason: null }] }, + { id: "chatcmpl_1", choices: [{ index: 0, delta: {}, finish_reason: "stop" }] }, + ]); +} + +const OPENAI_GOOD = openaiSse([ + { id: "chatcmpl_2", object: "chat.completion.chunk", choices: [{ index: 0, delta: { role: "assistant", content: "" }, finish_reason: null }] }, + { id: "chatcmpl_2", choices: [{ index: 0, delta: { content: "continued after nudge" }, finish_reason: null }] }, + { id: "chatcmpl_2", choices: [{ index: 0, delta: {}, finish_reason: "stop" }] }, +]); + +async function drainCapturing(first: string, retries: string[], id: string, messages: CoreMessage[]): Promise<{ out: string; fetchCalls: number; bodies: string[] }> { + let fetchCalls = 0; + const bodies: string[] = []; + const orig = globalThis.fetch; + globalThis.fetch = (async (_url: unknown, init?: { body?: unknown }) => { + fetchCalls++; + if (typeof init?.body === "string") bodies.push(init.body); + return new Response(retries[fetchCalls - 1] ?? "", { status: 200 }); + }) as typeof fetch; + const chunks: Buffer[] = []; + try { + const ctx = makeCtx(id, messages); + for await (const chunk of runCompressLoop( + new Response(first, { status: 200 }).body!, + ctx, + { model: "deepseek-chat", stream: true }, + { url: "http://mock", headers: {} }, + createOpenaiAdapter({ model: "deepseek-chat", stream: true }), + buildCompressSystemPrompt(), + )) { + chunks.push(chunk); + } + } finally { + globalThis.fetch = orig; + } + return { out: Buffer.concat(chunks).toString("utf8"), fetchCalls, bodies }; +} + +test("#4764 loop: complete draft tail retries with the complete-variant nudge in the retry body", async () => { + const messages: CoreMessage[] = [{ id: "u1", role: "user", contentType: "text", text: "do the followup" }]; + const { fetchCalls, bodies } = await drainCapturing(compliantReplySse(COMPLETE_DRAFT, ""), [OPENAI_GOOD], "l4764-a", messages); + assert.equal(fetchCalls, 1, "the #2303 guard fires"); + assert.ok(bodies[0]!.includes(DRAFT_TAIL_RETRY_NUDGE_COMPLETE), "retry body steers at the compress call"); + assert.ok(!bodies[0]!.includes("take your next concrete action"), "the generic wording no longer rides this path"); +}); + +test("#4764 loop: truncated draft tail retries with the truncated-variant nudge", async () => { + const messages: CoreMessage[] = [{ id: "u1", role: "user", contentType: "text", text: "do the followup" }]; + const { fetchCalls, bodies } = await drainCapturing(compliantReplySse(TRUNCATED_DRAFT, ""), [OPENAI_GOOD], "l4764-b", messages); + assert.equal(fetchCalls, 1); + assert.ok(bodies[0]!.includes(DRAFT_TAIL_RETRY_NUDGE_TRUNCATED)); + assert.ok(!bodies[0]!.includes("take your next concrete action")); +}); + +test("#4764 loop: tag-shape compaction instruction in the LAST user message suppresses the retry (shape twin)", async () => { + const messages: CoreMessage[] = [ + { id: "u1", role: "user", contentType: "text", text: "long session elided" }, + { id: "u2", role: "user", contentType: "text", text: WORKBUDDY_STYLE_INSTRUCTION }, + ]; + const { out, fetchCalls } = await drainCapturing(compliantReplySse(COMPLETE_DRAFT, ""), [OPENAI_GOOD], "l4764-c", messages); + assert.equal(fetchCalls, 0, "no auto-retry: the reply answers the host's own summary request"); + assert.ok(out.includes("\x3c/summary\x3e"), "the compliant summary reaches the client"); +}); + +test("#4764 loop: tag shape in an EARLIER user message does not suppress the retry (last-user-message only)", async () => { + const messages: CoreMessage[] = [ + { id: "u1", role: "user", contentType: "text", text: WORKBUDDY_STYLE_INSTRUCTION }, + { id: "a1", role: "assistant", contentType: "text", text: "done" }, + { id: "u2", role: "user", contentType: "text", text: "now continue the task" }, + ]; + const { fetchCalls } = await drainCapturing(compliantReplySse(COMPLETE_DRAFT, ""), [OPENAI_GOOD], "l4764-d", messages); + assert.equal(fetchCalls, 1, "only the final user message carries a live compaction instruction"); +}); From 0da1013bcff5686793a78e21fb29284095b95adf Mon Sep 17 00:00:00 2001 From: ci-check Date: Thu, 12 Nov 2026 17:45:21 +0800 Subject: [PATCH 2/2] fix(ci): drop unused export on DraftTailKind (unused-exports gate, #4785) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The type is only used within src/degenerate-turn.ts as the return type of draftTailKind; no consumer imports the named type. INTERNAL_ONLY per the unused-exports gate — drop the leading export. --- src/degenerate-turn.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/degenerate-turn.ts b/src/degenerate-turn.ts index 1f1112be20..cfe0979e41 100644 --- a/src/degenerate-turn.ts +++ b/src/degenerate-turn.ts @@ -79,7 +79,7 @@ export function endsWithDraftClose(text: string): boolean { // difference (#2303's single generic nudge ignored both, and on OpenAI-lane // compression turns the model read "take your next concrete action" as // permission to do anything else — Edit/Bash instead of the compress call). -export type DraftTailKind = "complete" | "truncated"; +type DraftTailKind = "complete" | "truncated"; const DRAFT_CLOSE_COMPLETE_TAIL = /\x3c\/summary\x3e\s*$/i; export function draftTailKind(text: string): DraftTailKind | null { if (text.length === 0) return null;