From 5e92408d7614412ede5426038bdccb34de176432 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Tue, 6 Oct 2026 23:41:45 +0800 Subject: [PATCH 1/2] escalate the degenerate-turn retry: second anti-echo nudge, bounded at two, with /__bili/status counters (#2176) - DEGENERATE_RETRY_MAX_ATTEMPTS = 2: the first re-issue keeps the plain #732/#821 continuation nudge; a second degenerate attempt re-issues with DEGENERATE_RETRY_NUDGE_ESCALATED, which names the behavior to stop (message-reference tags are stripped) and the output to produce instead. - Exhaustion keeps each pipe's honest terminal: chat = #870 in-band error (message now says 'nudges'), responses = the retry's own completion. - session.stats.degenerateRetries / degenerateExhausted count every fire and every unrecoverable turn (persisted); GET /__bili/status exposes the aggregate as degenerate: {sessions, retries, exhausted}. - The compress-loop lane keeps its original one-shot bound: its rounds are tool-result rounds, not user turns (#2176 was the plugin-pipe shape). - TECHNICAL-NOTES.md documents the escalation and the counters. --- TECHNICAL-NOTES.md | 32 ++++++++++ src/degenerate-retry.ts | 55 ++++++++++++++--- src/plugin.ts | 88 ++++++++++++++++---------- src/server.ts | 4 +- src/session.ts | 9 +++ tests/plugin-empty-turn-retry.test.ts | 89 +++++++++++++++++++++++++++ 6 files changed, 233 insertions(+), 44 deletions(-) diff --git a/TECHNICAL-NOTES.md b/TECHNICAL-NOTES.md index 376ca421a4..52ee5c8bb5 100644 --- a/TECHNICAL-NOTES.md +++ b/TECHNICAL-NOTES.md @@ -375,3 +375,35 @@ parse failures) forward as-is and log `[tag-echo] raw exit` so new leak shapes become visible without changing the wire. Pinned by `tests/unified-acp-invariants.test.ts` (eight tests, both lanes). + +## Degenerate-turn retry escalation — two nudges, then an honest failure (#2176) + +The empty-turn re-issue (`src/degenerate-retry.ts`, fired by both plugin +pipes when a turn terminates with nothing client-visible — the chronic +tag-echo shape) is bounded at `DEGENERATE_RETRY_MAX_ATTEMPTS = 2`: + +1. **First re-issue — the plain continuation nudge** (#732/#821 wording, + unchanged): "continue, take your next concrete action". +2. **Second re-issue — the anti-echo escalation**: names the behavior to + stop (message-reference tags are stripped before anyone reads them) and + the output to produce instead (plain prose or a tool call). Mentioning + the tags is deliberate: the turn is already lost at that point, and the + instruction rides a one-shot trailing user message that is never + persisted nor replayed. + +After both re-issues degenerate, each pipe keeps its existing honest +terminal: the chat pipe emits the #870 in-band error (a COMPLETED turn +carrying the error text), the Responses pipe lets the retry's own +completion close the turn. The compress-loop lane keeps its original +one-shot bound — its degenerate rounds are tool-result rounds, not user +turns, and were not part of the #2176 report. + +**Observability** (#2176): every fire increments +`session.stats.degenerateRetries`, every unrecoverable turn increments +`session.stats.degenerateExhausted` (both persisted, restart-stable), and +`GET /__bili/status` exposes the aggregate as `degenerate: { sessions, +retries, exhausted }`. The exhausted counter is gated behind the same +visible-text predicates as the retry itself, so a recovered escalation is +never miscounted. Nonzero `exhausted` is the signal that the escalation +did not save the turn — the tail is first-cause territory (injection +format), not retry-count territory. diff --git a/src/degenerate-retry.ts b/src/degenerate-retry.ts index 0553c79a9e..491e35c443 100644 --- a/src/degenerate-retry.ts +++ b/src/degenerate-retry.ts @@ -13,16 +13,31 @@ import { appendTrailingUserText } from "./wire-body.js"; // turn and the host aborts it. The pipe can still re-issue the SAME body // (plus this nudge) because it holds the upstream URL and the forwarded // headers, and it can splice the retry's content into the open client stream. -// Bounded to one attempt per turn; the body mutation never touches session -// state, so the nudge is neither persisted nor replayed on the client's next -// request. +// Bounded: the first re-issue carries the plain continuation nudge, a second +// (final) re-issue carries the anti-echo escalation (#2176: chronic tag-echo +// where the model keeps answering with markup only). The body mutation never +// touches session state, so the nudge is neither persisted nor replayed on +// the client's next request. export const DEGENERATE_RETRY_NUDGE = "[billion-context] Your previous response ended with no visible text and no tool call. Continue now: take your next concrete action."; +/** #2176: the second (escalated) re-issue. The first nudge says "continue"; + * a model that answers with markup again is imitating the render tags it saw + * in its prompt, so this one names the behavior to stop and the output to + * produce instead. Mentioning the tags here is deliberate: at this point the + * turn is already lost, and the instruction rides a one-shot trailing user + * message that is never persisted. */ +export const DEGENERATE_RETRY_NUDGE_ESCALATED = + "[billion-context] Your last two responses contained only message-reference tags with no visible text and no tool call — those tags are stripped before anyone reads them. Do not output them. Answer now in plain prose, or make a tool call."; + +/** #2176: re-issues per degenerate turn (1 = plain nudge, 2 = escalated). */ +export const DEGENERATE_RETRY_MAX_ATTEMPTS = 2; + /** 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. `escalated` selects + * the #2176 anti-echo nudge (second and final attempt). */ +export function injectContinuationNudge(protocol: WireProtocol, body: string | Buffer, escalated = false): string | null { + return appendTrailingUserText(protocol, body, escalated ? DEGENERATE_RETRY_NUDGE_ESCALATED : DEGENERATE_RETRY_NUDGE); } export interface ContinuationRetryOpts { @@ -47,9 +62,10 @@ export interface ContinuationRetryOpts { * function for the stream pipe: it yields the retry's response body, or null * 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 () => { + * unchanged. Callers that escalate pass `true` for the #2176 anti-echo nudge + * (the caller tracks attempt count; the closure is stateless). */ +export function makeContinuationRefetch(opts: ContinuationRetryOpts): (escalated?: boolean) => Promise | null> { + return async (escalated = false) => { 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: @@ -67,7 +83,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, escalated); if (retryBody === null) { opts.log("warn", `[${opts.label}] [plugin] degenerate-terminal retry skipped: request body carries no turn array`); return null; @@ -103,3 +119,22 @@ export function makeContinuationRefetch(opts: ContinuationRetryOpts): () => Prom } }; } + +/** #2176: aggregate view of the degenerate-retry counters carried in + * session.stats, surfaced by /__bili/status so the chronic tag-echo tail is + * measurable in the field (fired re-issues and turns no re-issue could + * save). Sessions without counters are skipped from the session count. */ +export function degenerateRetrySummary(sessions: Array<{ stats?: { degenerateRetries?: number; degenerateExhausted?: number } }>): { sessions: number; retries: number; exhausted: number } { + let touched = 0; + let retries = 0; + let exhausted = 0; + for (const s of sessions) { + const r = s.stats?.degenerateRetries ?? 0; + const e = s.stats?.degenerateExhausted ?? 0; + if (r === 0 && e === 0) continue; + touched += 1; + retries += r; + exhausted += e; + } + return { sessions: touched, retries, exhausted }; +} diff --git a/src/plugin.ts b/src/plugin.ts index 07c46ff465..c55de57ce6 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -22,6 +22,7 @@ import { ccrEnabled, ccrLoopConfig, contentStoreOf, retrieveToolName } from "./s import { imageUsageSuffix } from "./image-compress.js"; import { emitStreamError, emitUpstreamTruncation } from "./stream-error.js"; import { degenerateTurnWarning } from "./degenerate-turn.js"; +import { DEGENERATE_RETRY_MAX_ATTEMPTS } from "./degenerate-retry.js"; import { PANEL_BOX_FOOTER } from "./acp-panel.js"; import { describeAdvisory, getAdvisoryState } from "./advisory.js"; import { describeUpdateReady, getUpdateVisibility } from "./update-notes.js"; @@ -1709,10 +1710,11 @@ const ANTHROPIC_BLOCK_EVENT = /^content_block_(start|delta|stop)$/; * tiny input_tokens would clobber lastInputTokens and break compression * triggering for the main conversation. * - * `refetch` supplies the one-shot degenerate-turn retry (#732/#821): when the - * turn reaches its terminal with nothing visible — the tag-echo case, where - * the filter empties the only text block so the host aborts an empty turn — - * the pipe re-issues the request through it and splices the retry's content + * `refetch` supplies the degenerate-turn retry (#732/#821, #2176 escalation): + * when the turn reaches its terminal with nothing visible — the tag-echo + * case, where the filter empties the only text block so the host aborts an + * empty turn — the pipe re-issues the request through it (up to twice: a + * plain nudge, then an anti-echo escalation) and splices the retry's content * into the client stream the first attempt already opened. Omit it for the * plain pass-through. */ export async function pipePluginChatWithStrip( @@ -1721,7 +1723,7 @@ export async function pipePluginChatWithStrip( protocol: WireProtocol, session?: Session, log?: (msg: string) => void, - refetch?: () => Promise | null>, + refetch?: (escalated?: boolean) => 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 @@ -1849,19 +1851,19 @@ export async function pipePluginChatWithStrip( // terminal event is dropped when the retry takes over, so the client sees // one turn: its framing stays open, and the retry's content blocks are // shifted past the ones already streamed. - let degenerateRetried = false; + let degenerateRetries = 0; let truncationRetried = false; let forwardedAny = false; let inRetry = false; let retryIndexOffset = 0; let blocksForwarded = 0; - /** One-shot re-issue when a turn reaches its terminal with nothing visible: - * the tag-echo case, where the filter empties the only text block and the - * host aborts an empty completed turn. Returns true when the retry stream - * took over, in which case the caller drops the terminal event of the - * attempt it came from. Consults truncationRetried: a request whose stream - * already spent its one re-issue on a zero-visible cut (#2171) cannot also - * re-send on a degenerate completion — one re-issue per request, total. */ + /** Degenerate-turn re-issues (#732/#821), #2176-escalated to at most + * DEGENERATE_RETRY_MAX_ATTEMPTS: the first carries the plain continuation + * nudge, a second carries the anti-echo escalation. Returns true when the + * retry stream took over, in which case the caller drops the terminal + * event of the attempt it came from. Consults truncationRetried: a request + * whose stream already spent its re-issue on a zero-visible cut (#2171) + * cannot also re-send on a degenerate completion. */ const retryEmptyTurn = async (reason: string | undefined): Promise => { if (refetch === undefined || truncationRetried) return false; // Markup released from a held span carries nothing the host can act on: @@ -1869,13 +1871,15 @@ export async function pipePluginChatWithStrip( if (visibleTextChars > releasedMarkupChars || sawToolUse) return false; if (reason === undefined || !CLEAN_TURN_REASONS.has(reason)) return false; if (res.destroyed || res.writableEnded) return false; - if (degenerateRetried) { - // The retry degenerated too. An empty turn is indistinguishable from a - // model that produced nothing and the session reads as idle while it is - // dead, so the client gets an error the host would never surface (#870). - log?.("[plugin] degenerate terminal turn again after the retry; emitting an in-band error (#870)"); + if (degenerateRetries >= DEGENERATE_RETRY_MAX_ATTEMPTS) { + // Every re-issue degenerated too. An empty turn is indistinguishable + // from a model that produced nothing and the session reads as idle + // while it is dead, so the client gets an error the host would never + // surface (#870). + log?.("[plugin] degenerate terminal turn again after the continuation nudges; emitting an in-band error (#870)"); + if (session) session.stats.degenerateExhausted = (session.stats.degenerateExhausted ?? 0) + 1; // #870 deliberately chose a COMPLETED turn carrying the error text (visible to the host); keep the legacy shape here regardless of the global streamErrorShape default. - emitStreamError(res, protocol, "the turn degenerated again after the continuation nudge", undefined, "completion"); + emitStreamError(res, protocol, "the turn degenerated again after the continuation nudges", undefined, "completion"); return true; } // A turn the model left genuinely bare — no thought, no stripped echo, @@ -1883,11 +1887,15 @@ export async function pipePluginChatWithStrip( // re-issuing it double-bills an empty completion (#732/#821 keep the // same boundary in the compress loop). if (!sawThinking && !sawStrippedEcho && releasedMarkupChars === 0) return false; - degenerateRetried = true; - log?.("[plugin] degenerate terminal turn (no usable output); retrying once with a continuation nudge (#732/#821)"); + const escalated = degenerateRetries > 0; + degenerateRetries += 1; + if (session) session.stats.degenerateRetries = (session.stats.degenerateRetries ?? 0) + 1; + log?.(escalated + ? "[plugin] degenerate terminal turn again; escalating with the anti-echo nudge (#2176)" + : "[plugin] degenerate terminal turn (no usable output); retrying with a continuation nudge (#732/#821)"); let next: ReadableStream | null = null; try { - next = await refetch(); + next = await refetch(escalated); } catch (e) { log?.(`[plugin] degenerate-terminal retry failed (${e instanceof Error ? e.message : String(e)}); passing the empty turn through`); return false; @@ -1929,7 +1937,7 @@ export async function pipePluginChatWithStrip( * the #721 in-band error (partial content must not be regenerated). */ const retryZeroByteCut = async (): Promise => { if (refetch === undefined) return false; - if (degenerateRetried || truncationRetried) return false; + if (degenerateRetries > 0 || truncationRetried) return false; // Zero-visible predicate: no framing opened, no content block started, // no prose accumulated or held by the tag filter, no unparseable frame // forwarded verbatim, no partial SSE event left in the buffer. @@ -2691,7 +2699,8 @@ function googleFrameHasNonText(ev: Record): boolean { * #732/#821 parity with pipePluginChatWithStrip: when the turn's completion * reports a clean status with nothing visible — the echoed render tag was the * only thing the model emitted, and the filter emptied it — the agent's own - * body is re-issued ONCE with a continuation nudge instead of leaving the host + * body is re-issued with a continuation nudge (up to twice: plain, then the + * #2176 anti-echo escalation) instead of leaving the host * with an empty completed turn. The retry is reframed onto the ids the client * already holds, so the client still sees one turn. */ export async function pipePluginResponsesWithStrip( @@ -2699,7 +2708,7 @@ export async function pipePluginResponsesWithStrip( res: ServerResponse, session?: Session, log?: (msg: string) => void, - refetch?: () => Promise | null>, + refetch?: (escalated?: boolean) => Promise | null>, upstreamOrigin?: string, // See pipePluginChatWithStrip: whole-field tool-call emission drop, // gated on the request carrying the [ACP absorb] instruction (m00885). @@ -2758,7 +2767,7 @@ export async function pipePluginResponsesWithStrip( // output_item.added releases them early instead — clients require // done(itemN) before added(itemN+1) (#1061) — so by the terminal only the // last item's family can still be held. - let degenerateRetried = false; + let degenerateRetries = 0; let truncationRetried = false; let forwardedAny = false; let inRetry = false; @@ -2794,7 +2803,7 @@ export async function pipePluginResponsesWithStrip( * keeps the #721 in-band error. */ const retryZeroByteCut = async (): Promise => { if (refetch === undefined) return false; - if (degenerateRetried || truncationRetried) return false; + if (degenerateRetries > 0 || truncationRetried) return false; // Zero-visible predicate: no item framing held, no status observed, no // prose accumulated or held by the filters, no verbatim frame // forwarded, no partial SSE event left in the buffer. @@ -2955,8 +2964,9 @@ export async function pipePluginResponsesWithStrip( * Returns true when the retry stream took over, in which case the caller * drops the held done-family events AND the completion it came from. */ const retryEmptyTurn = async (status: string | undefined): Promise => { - // truncationRetried: one re-issue per request, total — see the chat-pipe twin. - if (degenerateRetried || truncationRetried || refetch === undefined) return false; + // truncationRetried: see the chat-pipe twin — a zero-visible cut (#2171) + // spends the budget a degenerate completion cannot also spend. + if (refetch === undefined || truncationRetried) return false; if (visibleTextChars > 0 || heldVisibleChars > 0 || sawFunctionCall) return false; if (status !== "completed") return false; if (res.destroyed || res.writableEnded) return false; @@ -2965,11 +2975,25 @@ export async function pipePluginResponsesWithStrip( // double-bills an empty completion (#732/#821 keep the same boundary in // the compress loop). if (!sawReasoning && !sawStrippedEcho) return false; - degenerateRetried = true; - log?.("[plugin] degenerate terminal turn (no visible output); retrying once with a continuation nudge (#732/#821)"); + if (degenerateRetries >= DEGENERATE_RETRY_MAX_ATTEMPTS) { + // Both re-issues degenerated: unlike the chat pipe (#870 in-band + // error) this lane lets the retry's own completion close the turn; + // count the exhaustion so /__bili/status can see the tail (#2176). + // Checked AFTER the visible-text gates so a recovered escalation + // (this same guard runs at its terminal too) is not miscounted. + log?.("[plugin] degenerate terminal turn again after the continuation nudges (#2176)"); + if (session) session.stats.degenerateExhausted = (session.stats.degenerateExhausted ?? 0) + 1; + return false; + } + const escalated = degenerateRetries > 0; + degenerateRetries += 1; + if (session) session.stats.degenerateRetries = (session.stats.degenerateRetries ?? 0) + 1; + log?.(escalated + ? "[plugin] degenerate terminal turn again; escalating with the anti-echo nudge (#2176)" + : "[plugin] degenerate terminal turn (no visible output); retrying with a continuation nudge (#732/#821)"); let next: ReadableStream | null = null; try { - next = await refetch(); + next = await refetch(escalated); } 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/src/server.ts b/src/server.ts index 11c1cc0bb4..77b885260a 100644 --- a/src/server.ts +++ b/src/server.ts @@ -99,7 +99,7 @@ import { ABSORB_INSTRUCTION_MARKER, containsToolCallXmlFragment } from "./loop/t import { isStrictReasoningEcho, modelIdOf, normalizeStrictEchoReasoning, normalizeStrictEchoResponsesInput } from "./strict-echo.js"; export { isStrictReasoningEcho, normalizeStrictEchoReasoning, normalizeStrictEchoResponsesInput }; import { isFakeCompletion, injectFakeCompletionHint, maxFakeCompletionRetries, fakeBufCap } from "./fake-completion.js"; -import { makeContinuationRefetch } from "./degenerate-retry.js"; +import { makeContinuationRefetch, degenerateRetrySummary } from "./degenerate-retry.js"; import { reasoningGuardEngages, runReasoningGuard } from "./reasoning-guard.js"; import { sanitizeResponsesInputIds, dropWhitespaceResponsesMessages, normalizeResponsesMessageItems } from "./loop/adapter-responses.js"; import { CODEX_COMPACT_HEALTH_RATIO, codexCompactMode, isCodexClient, hasCompactionTrigger, stripBiliCompactionItems, replaceBiliCompactionItems, codexCompactGate, codexCompactGatePre, buildTriggerForgeBody, mergeForgedSummaries } from "./codex-compact.js"; @@ -7905,7 +7905,7 @@ async function sendStatus(res: http.ServerResponse, opts: ProxyOptions): Promise loggerLog("warn", `split-session canary (#2170): conversation ${w.base} has live traffic under multiple session keys (design persona forks are excluded): ${w.sessions.map((s) => `${s.id} (requests=${s.requests})`).join("; ")}. For a non-persona host this is the #2165 failure shape (stolen anchor / never-compressing split) — investigate if unexpected.`); } } - res.end(JSON.stringify({ version: VERSION, diskVersion, stale, autoRestartOnUpdate: opts.autoRestartOnUpdate, advisory: currentAdvisoryPayload(), inFlight: totalInFlight(), splitSessions: splitWarnings, conflicts: summarizeConflicts(listSessions()) }, null, 2)); + res.end(JSON.stringify({ version: VERSION, diskVersion, stale, autoRestartOnUpdate: opts.autoRestartOnUpdate, advisory: currentAdvisoryPayload(), inFlight: totalInFlight(), splitSessions: splitWarnings, conflicts: summarizeConflicts(listSessions()), degenerate: degenerateRetrySummary(listSessions()) }, null, 2)); } // #2090 plan A — read-only view backing the web UI's "Signed upstreams" card: diff --git a/src/session.ts b/src/session.ts index efb342131d..7916c5c3cd 100644 --- a/src/session.ts +++ b/src/session.ts @@ -225,6 +225,15 @@ export type Session = { * Persisted so a restart doesn't re-arm the warmup delay; reset at * native-compaction boundaries with the rest of the baseline stats. */ calibrationRing?: { origin: string; model?: string; values: number[] }; + /** #2176: degenerate-turn re-issues fired by the plugin pipes (the + * empty-turn continuation nudge, now up to two per turn). Counted per + * FIRE, not per turn, so the field is also the amplification gauge. */ + degenerateRetries?: number; + /** #2176: turns where every re-issue still degenerated — the #870 + * in-band error (chat pipe) / the passed-through empty completion + * (responses pipe). Nonzero means the escalation did not save the + * turn; the tail is first-cause territory (injection format). */ + degenerateExhausted?: number; /** #1933 F1: pending pairing input — local estimate of the LAST * prepared outbound in BILLED caliber (estimateCoreMessages + * system/tools overhead + image reserve, defaultCountTokens rate), diff --git a/tests/plugin-empty-turn-retry.test.ts b/tests/plugin-empty-turn-retry.test.ts index 75c5216b78..46f793d751 100644 --- a/tests/plugin-empty-turn-retry.test.ts +++ b/tests/plugin-empty-turn-retry.test.ts @@ -478,3 +478,92 @@ test("plugin chat emits an in-band error when the retry degenerates too", async // the error's. assert.equal((text.match(/\[DONE\]/g) ?? []).length, 2, "the error's terminal, then the retry's trailing terminator"); }); + +// #2176: chronic tag-echo escalation — a model that answers with markup only +// gets a second, anti-echo nudge before the turn is given up on. The pipes +// fire up to DEGENERATE_RETRY_MAX_ATTEMPTS re-issues; the second carries the +// escalated nudge and session.stats counts every fire (and every exhaustion) +// for /__bili/status. +import { DEGENERATE_RETRY_NUDGE_ESCALATED, DEGENERATE_RETRY_MAX_ATTEMPTS, degenerateRetrySummary } from "../src/degenerate-retry.ts"; + +test("#2176 chat: a second degenerate attempt escalates with the anti-echo nudge and can still recover", async () => { + const out: string[] = []; + const calls: boolean[] = []; + const refetch = (escalated?: boolean) => { + calls.push(escalated === true); + // First re-issue degenerates again; the escalated one delivers prose. + return Promise.resolve(calls.length === 1 ? streamOf([chatChunk({ role: "assistant" }), chatStop(), DONE]) : streamOf(proseTurn("recovered by the escalated nudge"))); + }; + const session = makeSession(); + await pipePluginChatWithStrip(streamOf(echoOnlyTurn()), makeRes(out), "openai", session, undefined, refetch); + const text = out.join(""); + assert.equal(calls.length, 2, "two re-issues fire before giving up"); + assert.deepEqual(calls, [false, true], "the second re-issue is the escalated one"); + assert.equal(textDeltas(text, "openai"), "recovered by the escalated nudge", "the escalated retry's prose reaches the client"); + assert.ok(!text.includes("[ACP] stream error"), "a recovered turn carries no error"); + assert.equal((text.match(/\[DONE\]/g) ?? []).length, 1, "one turn, one terminal"); + assert.equal(session.stats.degenerateRetries, 2, "every fire is counted"); + assert.equal(session.stats.degenerateExhausted, undefined, "no exhaustion when the escalation recovers"); +}); + +test("#2176 chat: exhaustion after two degenerate re-issues emits the #870 error and counts the tail", async () => { + const out: string[] = []; + const logs: string[] = []; + setLogCapture((_level, msg) => logs.push(msg)); + let calls = 0; + const refetch = () => { + calls += 1; + return Promise.resolve(streamOf([chatChunk({ role: "assistant" }), chatStop(), DONE])); + }; + const session = makeSession(); + try { + await pipePluginChatWithStrip(streamOf(echoOnlyTurn()), makeRes(out), "openai", session, (m) => logs.push(m), refetch); + } finally { + setLogCapture(null); + } + const text = out.join(""); + assert.equal(calls, DEGENERATE_RETRY_MAX_ATTEMPTS, "the escalation is bounded"); + assert.ok(text.includes("[ACP] stream error"), "the client is told instead of left with an empty turn"); + assert.equal(session.stats.degenerateRetries, 2); + assert.equal(session.stats.degenerateExhausted, 1, "the unrecoverable turn is visible in the counters"); + assert.ok(logs.some((l) => l.includes("continuation nudges")), "the exhaustion log names the escalation"); +}); + +test("#2176 the escalated nudge rides the retry body as a trailing user turn", () => { + const body = JSON.stringify({ model: "m", messages: [{ role: "user", content: "hi" }] }); + const merged = JSON.parse(injectContinuationNudge("openai", body, true)!) as { messages: { role: string; content: string }[] }; + assert.ok(merged.messages[0]!.content.includes("Do not output them"), "the escalated text names the behavior to stop"); + assert.ok(merged.messages[0]!.content.includes(DEGENERATE_RETRY_NUDGE_ESCALATED.slice(0, 40)), "the escalated nudge is the payload"); + const plain = JSON.parse(injectContinuationNudge("openai", body)!) as { messages: { content: string }[] }; + assert.ok(!plain.messages[0]!.content.includes("Do not output them"), "the default nudge is unchanged"); +}); + +test("#2176 responses: the escalated second re-issue recovers the turn too", async () => { + const out: string[] = []; + const calls: boolean[] = []; + const refetch = (escalated?: boolean) => { + calls.push(escalated === true); + return Promise.resolve(calls.length === 1 + ? streamOf(responsesProseTurn("", "resp_2", "item_2")) + : streamOf(responsesProseTurn("recovered on the escalated nudge", "resp_3", "item_3"))); + }; + const session = makeSession(); + await pipePluginResponsesWithStrip(streamOf(responsesEchoOnlyTurn()), makeRes(out), session, undefined, refetch); + const text = out.join(""); + assert.equal(calls.length, 2, "the responses pipe escalates too"); + assert.deepEqual(calls, [false, true]); + assert.equal(responsesDeltas(text), "recovered on the escalated nudge"); + assert.equal((text.match(/"type":"response\.completed"/g) ?? []).length, 1, "one turn, one terminal"); + assert.equal(session.stats.degenerateRetries, 2); + assert.equal(session.stats.degenerateExhausted, undefined); +}); + +test("#2176 degenerateRetrySummary aggregates the counters for /__bili/status", () => { + const a = makeSession(); + a.stats.degenerateRetries = 3; + a.stats.degenerateExhausted = 1; + const b = makeSession(); + b.stats.degenerateRetries = 1; + const summary = degenerateRetrySummary([a, b, makeSession(), {}]); + assert.deepEqual(summary, { sessions: 2, retries: 4, exhausted: 1 }, "untouched sessions are skipped, fires sum across sessions"); +}); From 8c87b0118eba0aac8bde471d5958c98de5721cab Mon Sep 17 00:00:00 2001 From: ework-agent Date: Wed, 7 Oct 2026 00:35:07 +0800 Subject: [PATCH 2/2] test(#2176): pin the responses-lane exhaustion counting branch --- tests/plugin-empty-turn-retry.test.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/plugin-empty-turn-retry.test.ts b/tests/plugin-empty-turn-retry.test.ts index 46f793d751..124188f5b2 100644 --- a/tests/plugin-empty-turn-retry.test.ts +++ b/tests/plugin-empty-turn-retry.test.ts @@ -567,3 +567,28 @@ test("#2176 degenerateRetrySummary aggregates the counters for /__bili/status", const summary = degenerateRetrySummary([a, b, makeSession(), {}]); assert.deepEqual(summary, { sessions: 2, retries: 4, exhausted: 1 }, "untouched sessions are skipped, fires sum across sessions"); }); + +test("#2176 responses: exhaustion after two degenerate re-issues counts the tail and closes on its own completion", async () => { + const out: string[] = []; + const logs: string[] = []; + setLogCapture((_level, msg) => logs.push(msg)); + const calls: boolean[] = []; + const refetch = (escalated?: boolean) => { + calls.push(escalated === true); + return Promise.resolve(streamOf(responsesEchoOnlyTurn())); + }; + const session = makeSession(); + try { + await pipePluginResponsesWithStrip(streamOf(responsesEchoOnlyTurn()), makeRes(out), session, (m) => logs.push(m), refetch); + } finally { + setLogCapture(null); + } + const text = out.join(""); + assert.equal(calls.length, DEGENERATE_RETRY_MAX_ATTEMPTS, "the escalation is bounded"); + assert.deepEqual(calls, [false, true], "the second re-issue is the escalated one"); + assert.equal(responsesDeltas(text), "", "no prose survives a fully degenerate turn"); + assert.equal((text.match(/"type":"response\.completed"/g) ?? []).length, 1, "one turn, one terminal — the retry's own completion"); + assert.equal(session.stats.degenerateRetries, 2); + assert.equal(session.stats.degenerateExhausted, 1, "the unrecoverable turn is visible in the counters"); + assert.ok(logs.some((l) => l.includes("continuation nudges")), "the exhaustion log names the escalation"); +});