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: 25 additions & 6 deletions src/degenerate-retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,29 @@ function releaseThrottleErrorReader(reader: ReadableStreamDefaultReader<Uint8Arr
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
// <analysis>/<summary> 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, model?: string): string | null {
return appendTrailingUserText(protocol, body, DEGENERATE_RETRY_NUDGE, model);
* 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.
* model threads through to appendTrailingUserText for tool_result-first part
* order (#5390). */
export function injectContinuationNudge(protocol: WireProtocol, body: string | Buffer, nudgeText: string = DEGENERATE_RETRY_NUDGE, model?: string): string | null {
return appendTrailingUserText(protocol, body, nudgeText, model);
}

interface ContinuationRetryOpts {
Expand Down Expand Up @@ -109,8 +128,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): (nudgeText?: string) => Promise<ReadableStream<Uint8Array> | 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:
Expand All @@ -128,7 +147,7 @@ export function makeContinuationRefetch(opts: ContinuationRetryOpts): () => Prom
/* unparseable body: injectContinuationNudge below reports and skips */
}
}
const retryBody = injectContinuationNudge(opts.protocol, opts.body, opts.model);
const retryBody = injectContinuationNudge(opts.protocol, opts.body, nudgeText, opts.model);
if (retryBody === null) {
opts.log("warn", `[${opts.label}] [plugin] degenerate-terminal retry skipped: request body carries no turn array`);
return null;
Expand Down
37 changes: 37 additions & 0 deletions src/degenerate-turn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 </summary> tail means the draft is COMPLETE — the full summary exists, just
// written in the wrong channel; a </analysis> 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).
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;
}

// #3905: the announcement-tail shape. Unlike #2303 there is no draft tag: the
// ENTIRE visible output of the turn is a short promise of the next action
// ("Running verify after formatting:") that ends on a colon or ellipsis, with
Expand Down Expand Up @@ -127,6 +143,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
// <analysis>/<summary> 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 <conversation_history_summary>, 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. */
Expand Down
17 changes: 12 additions & 5 deletions src/loop/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,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 { endsWithAnnouncement, 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, endsWithAnnouncement, endsWithDraftClose, lastUserSummaryInstruction, requestExpectsProseSummary, requestExpectsProseSummaryShape } from "../degenerate-turn.js";
import { safePrefix, safeSuffix, scrubLoneSurrogateEscapesOnJson } from "../text-safe.js";

export const MAX_LOOP_ROUNDS = 10;
Expand Down Expand Up @@ -1319,21 +1319,28 @@ 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;
const tailKind = draftTailKind(assistantText) ?? "complete";
if (stalledTail === "announce") {
ctx.log(`[acp-loop] round ${round}: terminal turn is a bare announcement (ends mid-promise, no tool call); retrying once with continuation nudge (#3905)`);
loggerLog("warn", `[acp-loop] announcement-tail auto-retry round ${round} (session ${ctx.session.id})`);
} else {
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})`);
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: stalledTail === "announce"
? DEGENERATE_RETRY_NUDGE
: (tailKind === "truncated" ? DRAFT_TAIL_RETRY_NUDGE_TRUNCATED : DRAFT_TAIL_RETRY_NUDGE_COMPLETE),
};
try {
const retryBody = await buildLoopRequestBody([nudge]);
Expand Down
28 changes: 21 additions & 7 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,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, endsWithAnnouncement, endsWithDraftClose, requestExpectsProseSummary } from "./degenerate-turn.js";
import { DRAFT_TAIL_RETRY_NUDGE_COMPLETE, DRAFT_TAIL_RETRY_NUDGE_TRUNCATED } from "./degenerate-retry.js";
import { degenerateTurnWarning, draftTailKind, endsWithAnnouncement, endsWithDraftClose, requestExpectsProseSummary } from "./degenerate-turn.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 @@ -2760,7 +2761,7 @@ export async function pipePluginChatWithStrip(
protocol: WireProtocol,
session?: Session,
log?: (msg: string) => void,
refetch?: () => Promise<ReadableStream<Uint8Array> | null>,
refetch?: (nudgeText?: string) => 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 @@ -2972,7 +2973,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 <summary> 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;
// #3905: the same non-convergence without a draft tag — the whole visible
// output is a one-line promise of the next action ("Running verify after
// formatting:"). Without this branch the answerChars gate below would read
Expand Down Expand Up @@ -3027,11 +3036,11 @@ export async function pipePluginChatWithStrip(
log?.(announceTail
? "[plugin] terminal turn is a bare announcement (ends mid-promise, no tool call); retrying once with a continuation nudge (#3905)"
: 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<Uint8Array> | 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;
Expand Down Expand Up @@ -4339,7 +4348,7 @@ export async function pipePluginResponsesWithStrip(
res: ServerResponse,
session?: Session,
log?: (msg: string) => void,
refetch?: () => Promise<ReadableStream<Uint8Array> | null>,
refetch?: (nudgeText?: string) => 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 @@ -4715,7 +4724,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;
// #3905: bare-announcement twin — see the chat-pipe twin above
// (including why the detector reads answerProse, not proseAcc).
const announceTail = !draftTail && (visibleTextChars > 0 || heldVisibleChars > 0) && !sawFunctionCall && endsWithAnnouncement(answerProse) && !requestExpectsProseSummary(requestText);
Expand Down Expand Up @@ -4746,11 +4760,11 @@ export async function pipePluginResponsesWithStrip(
log?.(announceTail
? "[plugin] terminal turn is a bare announcement (ends mid-promise, no function call); retrying once with a continuation nudge (#3905)"
: 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<Uint8Array> | 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;
Expand Down
Loading
Loading