diff --git a/CHANGELOG.md b/CHANGELOG.md index d02f52e71b..6d9e7ddf52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,13 @@ Versions follow the merge of a `*_release-v*` branch; CI publishes to npm on tag ### Fixes +- **Plugin passthrough re-issues a turn whose only visible output was a stripped tag echo (#870, PR #871)**: the plugin-mode direct pipe strips model-emitted render tags, and when that echo was a turn's ENTIRE visible content the emptied chunk was suppressed, so the host received a turn with no text and no tool call and aborted it. Releasing the echo instead is not an option (the #463 contract keeps a pure-echo turn swallowed, and #674/#673 forbid leaking the echo as content), so the pipe now re-issues: on a clean terminal (`stop` / `end_turn` / `stop_sequence`) with zero visible text, zero tool calls and no error finish reason, `src/plugin.ts` refetches the same request with a continuation nudge (`src/degenerate-retry.ts`: `DEGENERATE_RETRY_NUDGE`, `injectContinuationNudge`, `makeContinuationRefetch`) and relays the retry inside the original framing — Anthropic keeps a single `message_start`/`message_stop` with the retry's block indices offset, and the OpenAI terminal is the `choices[].finish_reason` chunk rather than `[DONE]`, so the first attempt's terminal never reaches the client twice. One retry per turn; if the retry is also empty the original turn passes through with the existing `[degenerate-turn]` warning, and an unissuable retry presents the original terminal with valid JSON. `appendTrailingUserText` moved to `src/wire-body.ts` so the fake-completion hint and the retry share one body builder. Tests: `tests/plugin-empty-turn-retry.test.ts` (8, driving the pipe with a fake response and a fake refetch). + The same gate now covers the unclosed case: when a render tag is dropped and its interior + survives as the turn's only visible text, the host receives orphan markup with no tool call and + stalls, so that interior is counted as markup rather than as visible output and the turn is re-issued. When the retry degenerates too, the client now receives an in-band stream error rather + than a silent empty turn: an empty turn is indistinguishable from a model that produced nothing, + and the host reports neither, so the session reads as idle while it is dead. + - **Degenerate-turn auto-retry reachable on OpenAI/Anthropic wires (#821, PR #822)**: the #732 invisible single-retry for a completed turn containing ONLY a thinking block (zero visible text, zero tool calls) was unreachable on the OpenAI/Anthropic wires: its gate required `!forwardedAny`, but on those wires reasoning deltas are forwarded verbatim to the client, so at `done` time both `forwardedAny` and `sawThinking` are true — mutually exclusive for exactly the shape this retry exists for. Only the Responses wire (round>1 framing suppression) could ever fire it. Wild-caught via WorkBuddy + deepseek-v4.1-flash through an OpenAI-chat gateway: two round-1 thinking-only turns (~6–8 min of thinking, zero visible output) logged the #673 `[degenerate-turn]` warning but passed through as empty turns, and the host aborted with an "empty stream" error. The loop now tracks model-VISIBLE output separately (`forwardedVisible`, set only on text forwards; reasoning/meta keep setting `forwardedAny`), and the retry gate uses it — a forwarded thinking prefix is invisible to host turn semantics and its chunks carry no `finish_reason` (adapters strip it), so appending the retry's content to the same stream is safe. Unchanged: one-shot bound, mandatory `sawThinking` (a genuinely empty no-reasoning turn still passes through untouched), and the #413 truncation retry's strict `!forwardedAny`. Regression tests pin the openai-wire round-1 shape (retry fires, nudge present, thinking prefix precedes retried content in the same stream) and the no-reasoning control (no retry). - **Cross-process startup-window race: concurrent launches no longer double-spawn proxies (#707)**: `ensureProxyRunning` only attached when it found a *healthy* instance at probe time — between the launcher's spawn and the child's first successful listen there was zero on-disk evidence that a bring-up was in flight, so two launches inside that window (~1s+) each spawned their own child: two live proxies over one sessions dir (double writers), last-writer-wins instance file, and the extra proxy living until ITS OWN spawner exited. The starter now claims `/proxy-starting` with O_EXCL before spawning (carrying `{token, pid, host, port, startedAt}`); a concurrent caller that sees an active marker (owner pid alive AND younger than SPAWN_WAIT_MS + 30s) waits for the starter's instance with the same probe as attach (pid alive + `/__bili/health` + instanceId match), bounded by SPAWN_WAIT_MS, and attaches on success; marker disappearance (starter gave up / crashed) breaks the wait early so the caller spawns immediately. The starter clears the marker on every terminal path via a token-checked clear; a hard crash leaves a stale marker that the dead-owner/TTL checks render inert. The O_EXCL claim itself is the cross-process arbiter: the pre-spawn read is only a fast path, so a loser of the claim (read→claim race, or a stale/garbage marker file left by a crash mid-write) re-checks and either waits for the live holder or removes-and-reclaims the slot — it never spawns blindly, and an unreadable marker file self-heals on the next launch instead of permanently degrading coordination. A final probe after the wait deadline catches a starter that succeeded in its last moments — its success path clears the marker, which otherwise looks identical to a failure bail. Incompatible-config fall-through is preserved (a waited-for instance that isn't compatible with our launch options still spawns, matching today's incompatible-healthy-instance behavior); explicit `bili start` bypasses the marker entirely (it's a user action, not auto-bootstrap). In-process dedup (`singleFlight`, #706) is untouched — this is the cross-process half. - **DeepSeek thinking mode 400 fixed: turns are atomic, reasoning-drop self-disables on strict-echo upstreams (#684)**: two paths could ship a rebuilt request whose assistant `tool_calls` turn lost its `reasoning_content` — (1) the proxy's recent-zone carve kept the last tool-call of a multi-call turn visible while its reasoning run folded into the block (kernel boundary logic only pulled the FIRST companion message after reasoning), and (2) #651's reasoning-drop stripped reasoning from a closed compress turn even on upstreams that document thinking-mode `reasoning_content` as mandatory round-trip. DeepSeek rejects both with 400 (`The \`reasoning_content\` in the thinking mode must be passed back to the API`) and the turn dies. Fixes: the kernel now computes atomic turn groups (reasoning run + its full assistant burst + every result paired by `toolCallId` — all fold together or none do; a range that would split a turn is withdrawn with a warning, or rejected with an explanatory error when nothing else remains), the forward boundary pull covers the whole burst, and the reasoning-drop never fires when the session is known strict-echo — statically for `deepseek` upstreams, or learned from the first 400 whose body mentions `reasoning_content` (persisted on the session, self-healing like #552). Three wire-level exit sentinels (`[reasoning-pair-violated]` on chat/completions and Responses, `[thinking-pair-violated]` on Anthropic) log loudly if any future path reintroduces a split turn. diff --git a/src/degenerate-retry.ts b/src/degenerate-retry.ts new file mode 100644 index 0000000000..1ec7dee40c --- /dev/null +++ b/src/degenerate-retry.ts @@ -0,0 +1,100 @@ +import { fetchWithTimeout } from "./fetch-util.js"; +import { hasCompactionTrigger } from "./codex-compact.js"; +import { proxyDispatcher } from "./upstream-proxy.js"; +import type { WireProtocol } from "./util.js"; +import { appendTrailingUserText } from "./wire-body.js"; + +// #732/#821 ran this retry for the compress loop, which owns the request body +// AND the response framing. The plugin pipe (plugin mode, and proxy-mode chat +// SSE that skipped compress injection) owns neither: it forwards the agent's +// own body and passes the upstream stream through byte-identically, so a turn +// that ends with nothing visible — the render-tag echo case, where the tag +// filter empties the only text block — reaches the host as an empty completed +// 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. +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."; + +/** 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); +} + +export interface ContinuationRetryOpts { + protocol: WireProtocol; + /** The body that was forwarded upstream (the agent's own, in plugin mode). */ + body: string | Buffer; + upstreamUrl: string; + reqHeaders: Record; + proxyUrl?: string; + dispatcher?: object; + signal: AbortSignal; + log: (level: string, msg: string) => void; + /** Log prefix, normally the session id. */ + label: string; +} + +/** Build the re-request used when a turn ends with no visible output. Returns a + * 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 () => { + 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: + // the trigger stays the last input item), and the trigger's terminal is + // decided by the compaction flow, not by an empty-turn retry. Parsed + // here, at retry time, so healthy turns pay nothing. + if (opts.protocol === "responses") { + try { + const parsed = JSON.parse(typeof opts.body === "string" ? opts.body : opts.body.toString("utf8")) as Record; + if (hasCompactionTrigger(parsed["input"])) { + opts.log("warn", `[${opts.label}] [plugin] degenerate-terminal retry skipped: request ends on a compaction trigger`); + return null; + } + } catch { + /* unparseable body: injectContinuationNudge below reports and skips */ + } + } + const retryBody = injectContinuationNudge(opts.protocol, opts.body); + if (retryBody === null) { + opts.log("warn", `[${opts.label}] [plugin] degenerate-terminal retry skipped: request body carries no turn array`); + return null; + } + try { + const r = await fetchWithTimeout( + opts.upstreamUrl, + { + method: "POST", + headers: opts.reqHeaders, + body: retryBody, + ...(opts.dispatcher ? { dispatcher: opts.dispatcher } : opts.proxyUrl ? { dispatcher: proxyDispatcher(opts.proxyUrl) } : {}), + }, + undefined, + opts.signal, + ); + if (!r.response.ok || !r.response.body) { + r.clearTimer(); + opts.log("warn", `[${opts.label}] [plugin] degenerate-terminal retry rejected (HTTP ${r.response.status}); passing the empty turn through`); + return null; + } + const body = r.response.body as ReadableStream; + // The retry stream is spliced into a response the caller already + // owns, so its own idle timer is dropped — a stalled retry then ends + // with the client abort instead of an upstream watchdog (the + // streamed re-request in reasoning-guard.ts does the same). + r.clearTimer(); + return body; + } catch (e) { + opts.log("warn", `[${opts.label}] [plugin] degenerate-terminal retry failed (${e instanceof Error ? e.message : String(e)}); passing the empty turn through`); + return null; + } + }; +} diff --git a/src/fake-completion.ts b/src/fake-completion.ts index db30b6aba5..00cb3e5194 100644 --- a/src/fake-completion.ts +++ b/src/fake-completion.ts @@ -1,5 +1,6 @@ import type { WireProtocol } from "./util.js"; import { containsToolCallXmlFragment } from "./loop/tag-echo-filter.js"; +import { appendTrailingUserText } from "./wire-body.js"; // #371 (root-cause follow-up to #361): a small model can WRITE a tool call as // plain text (echoing the tool-call XML template from the context) instead of @@ -82,43 +83,9 @@ export function isFakeCompletion(protocol: WireProtocol, rawText: string): boole return hasToolCallStructure(rawText) && !hasToolBlock(protocol, rawText); } -// Appends the hint as a trailing user message, merged into the last user -// message when present (avoids back-to-back user turns on strict providers). -// Returns the hinted JSON, or null if the body is unparseable (skip the retry). +/** Append the fake-completion hint to the request body as a trailing user turn + * (per-wire shapes live in `appendTrailingUserText`). Returns the hinted JSON, + * or null if the body is unparseable, in which case the retry is skipped. */ export function injectFakeCompletionHint(protocol: WireProtocol, body: string | Buffer): string | null { - const raw = typeof body === "string" ? body : body.toString("utf8"); - let obj: Record; - try { - obj = JSON.parse(raw) as Record; - } catch { - return null; - } - if (protocol === "responses") { - const input = obj.input; - if (!Array.isArray(input)) return null; - const arr = input as Record[]; - const last = arr[arr.length - 1]; - if (last && typeof last === "object" && last.role === "user") { - const c = last.content; - if (typeof c === "string") last.content = `${c}\n\n${FAKE_COMPLETION_HINT}`; - else if (Array.isArray(c)) last.content = [...c, { type: "input_text", text: FAKE_COMPLETION_HINT }]; - else last.content = FAKE_COMPLETION_HINT; - } else { - arr.push({ role: "user", content: [{ type: "input_text", text: FAKE_COMPLETION_HINT }] }); - } - return JSON.stringify(obj); - } - const messages = obj.messages; - if (!Array.isArray(messages)) return null; - const arr = messages as Record[]; - const last = arr[arr.length - 1]; - if (last && typeof last === "object" && last.role === "user") { - const c = last.content; - if (typeof c === "string") last.content = `${c}\n\n${FAKE_COMPLETION_HINT}`; - else if (Array.isArray(c)) last.content = [...c, { type: "text", text: FAKE_COMPLETION_HINT }]; - else last.content = FAKE_COMPLETION_HINT; - } else { - arr.push({ role: "user", content: FAKE_COMPLETION_HINT }); - } - return JSON.stringify(obj); + return appendTrailingUserText(protocol, body, FAKE_COMPLETION_HINT); } diff --git a/src/loop/core.ts b/src/loop/core.ts index 023ff6ddd0..326cdc24d2 100644 --- a/src/loop/core.ts +++ b/src/loop/core.ts @@ -24,15 +24,10 @@ import { dumpsDir } from "../paths.js"; import { isStrictReasoningEcho, 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"; export const MAX_LOOP_ROUNDS = 10; -// #732: ephemeral continuation prompt appended ONLY to the degenerate-turn -// retry body — never committed to coreMessages/session state, so it is neither -// persisted nor replayed on the client's next (client-authored) request. -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."; - function isLoopThinking(m: CoreMessage): boolean { return m.contentType === "reasoning" && typeof m.id === "string" && m.id.startsWith("acp_loop_"); } diff --git a/src/plugin.ts b/src/plugin.ts index ebf45d3b79..4bd7f1cd0a 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -1,6 +1,7 @@ import { type CompressionCore, type Config, type CoreMessage, type NudgeDecision, defaultCountTokens } from "acp-kernel"; import { buildStatusPanel } from "acp-kernel/panel"; import { fileURLToPath } from "node:url"; +import type { ServerResponse } from "node:http"; import fs from "node:fs"; import path from "node:path"; import { acquireInFlight, effectiveConfig, findSessionByCanonicalId, listSessions, markCompactionBoundary, markDirty, peekSession, releaseInFlight, withSessionLock, type Session } from "./session.js"; @@ -10,7 +11,7 @@ import { executeProxyTool } from "./loop/core.js"; import { normalizeSseLineEndings } from "./sse-util.js"; import { composeStreamFilters, containsMarkerLineText, containsRenderTagText, createMarkerLineFilter, createTagEchoFilter, mayStartMarkerLine, mayStartRenderTag, stripAnthropicText, stripOpenaiChatText, stripResponsesText, type TagEchoFilter } from "./loop/tag-echo-filter.js"; import { log as loggerLog } from "./logger.js"; -import { emitUpstreamTruncation } from "./stream-error.js"; +import { emitStreamError, emitUpstreamTruncation } from "./stream-error.js"; import { degenerateTurnWarning } from "./degenerate-turn.js"; import { noteWeakOverflow } from "./weak-overflow.js"; import { warnCacheCollapse } from "./cache-warn.js"; @@ -753,6 +754,14 @@ function mergeUsageSample(acc: UsageSample, sample: UsageSample): void { if (sample.outputTokens !== undefined) acc.outputTokens = sample.outputTokens; } +/** Terminal reasons after which the model genuinely finished its turn. A + * refusal or a safety block must never be re-prompted, and a token-capped turn + * would only truncate again — so the degenerate-turn retry (#732/#821 for the + * plugin pipe) engages on these alone. */ +const CLEAN_TURN_REASONS = new Set(["stop", "end_turn", "stop_sequence"]); + +const ANTHROPIC_BLOCK_EVENT = /^content_block_(start|delta|stop)$/; + /** Plugin-mode streaming passthrough for the OpenAI chat-completions and * Anthropic wires: forward upstream events byte-identical (the agent's * native tool loop must see the model's tool calls untouched) while (a) @@ -766,23 +775,35 @@ function mergeUsageSample(acc: UsageSample, sample: UsageSample): void { * title-gen exclusion / ACP_NO_INJECT_TOOL / classifier bypass). Pass no * session there — usage accounting must be skipped or a title-gen call's * tiny input_tokens would clobber lastInputTokens and break compression - * triggering for the main conversation. */ + * 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 + * into the client stream the first attempt already opened. Omit it for the + * plain pass-through. */ export async function pipePluginChatWithStrip( stream: ReadableStream, - res: import("node:http").ServerResponse, + res: ServerResponse, protocol: WireProtocol, session?: Session, log?: (msg: string) => void, + refetch?: () => Promise | null>, ): Promise { - const reader = stream.getReader(); - const decoder = new TextDecoder("utf-8"); + let reader = stream.getReader(); + let decoder = new TextDecoder("utf-8"); let buf = ""; const acc: UsageSample = {}; + let sawStrippedEcho = false; const onTagDrop = (snippet: string) => { + droppedTagInFrame = true; + sawStrippedEcho = true; loggerLog("warn", `[tag-echo] stripped model-emitted render tag (plugin passthrough): ${snippet.slice(0, 80).replace(/\n/g, " ")}`); log?.(`[tag-echo] stripped model-emitted render tag from plugin passthrough text`); }; const onMarkerDrop = (snippet: string) => { + sawStrippedEcho = true; loggerLog("warn", `[marker-echo] stripped model-emitted ACP confirmation marker (plugin passthrough): ${snippet.slice(0, 80).replace(/\n/g, " ")}`); log?.(`[marker-echo] stripped model-emitted ACP confirmation marker from plugin passthrough text`); }; @@ -814,14 +835,120 @@ export async function pipePluginChatWithStrip( let sawToolUse = false; let sawThinking = false; let visibleTextChars = 0; + /** Of that text, the chars released from a held markup span (the + * unclosed-tag case): markup the filter declined to swallow. A turn whose + * only visible output is this is as dead to the host as an empty one. */ + let releasedMarkupChars = 0; + /** Set while a frame is being rewritten because a render tag was dropped: + * the text that survives such a frame is the tag's own interior. */ + let droppedTagInFrame = false; let finalFinishReason: string | undefined; + // Degenerate-turn retry (#732/#821 for this pipe). The first attempt's + // 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 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. */ + const retryEmptyTurn = async (reason: string | undefined): Promise => { + if (refetch === undefined) return false; + // Markup released from a held span carries nothing the host can act on: + // an unclosed render tag stalls the turn exactly like an empty one. + 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)"); + emitStreamError(res, protocol, "the turn degenerated again after the continuation nudge"); + return true; + } + // A turn the model left genuinely bare — no thought, no stripped echo, + // no released markup — is the upstream's own empty answer, not a stall: + // 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)"); + let next: ReadableStream | null = null; + try { + next = await refetch(); + } catch (e) { + log?.(`[plugin] degenerate-terminal retry failed (${e instanceof Error ? e.message : String(e)}); passing the empty turn through`); + return false; + } + if (!next) return false; + // The turn is NOT over: the retry stream carries its own terminal, and a + // cut in it must still raise the truncation signal (#721). + sawTerminal = false; + finalFinishReason = undefined; + retryIndexOffset = blocksForwarded; + inRetry = true; + // The first attempt's held filter state belongs to text the client never + // saw (an emptying tag echo): the retry's content is filtered from + // scratch, so a partial tag there cannot swallow its opening characters. + streams.clear(); + // The first attempt is terminal and its body is drained; close the + // reader we are abandoning rather than leaving the socket held. + try { + await reader.cancel(); + } catch { + /* already closed */ + } + reader = next.getReader(); + decoder = new TextDecoder("utf-8"); + buf = ""; + return true; + }; + /** While the retry stream feeds the client, the message the FIRST attempt + * opened is still open: nothing may re-open it. Returns true when the event + * was consumed. */ + const retryFraming = (ev: Record): boolean => { + if (!inRetry) return false; + return ev["type"] === "message_start"; + }; + /** The retry's content blocks must land AFTER the ones the client already + * saw. The offset is applied to the serialized payload — a processor with + * nothing to strip forwards the original bytes, so rewriting only the parsed + * event would leave the client's own index untouched. */ + const offsetRetryIndices = (payload: string): string => { + if (!inRetry || retryIndexOffset === 0) return payload; + return payload + .split("\n") + .map((line) => { + if (!line.startsWith("data:")) return line; + const json = line.slice(5).trim(); + if (!json.startsWith("{")) return line; + let o: Record; + try { + o = JSON.parse(json) as Record; + } catch { + return line; + } + if (typeof o["index"] !== "number" || !ANTHROPIC_BLOCK_EVENT.test(String(o["type"]))) return line; + o["index"] = (o["index"] as number) + retryIndexOffset; + return `data: ${JSON.stringify(o)}`; + }) + .join("\n"); + }; const flushTails = (): string => { let out = ""; for (const s of streams.values()) { const tail = s.filter.flush(); if (tail.length > 0) { out += syntheticTail(s.field, s.index, tail); - if (s.field === "content" || s.field === "text") visibleTextChars += tail.length; + if (s.field === "content" || s.field === "text") { + visibleTextChars += tail.length; + releasedMarkupChars += tail.length; + } } } return out; @@ -913,7 +1040,15 @@ export async function pipePluginChatWithStrip( if (clean.length === 0) droppedText = true; else { keptText = true; - if (field === "content") visibleTextChars += clean.length; + if (field === "content") { + visibleTextChars += clean.length; + // What a dropped tag leaves behind is its own interior: the + // host finds no tool call in it and stalls the turn. + if (droppedTagInFrame) { + releasedMarkupChars += clean.length; + droppedTagInFrame = false; + } + } } if (changed) { if (!rebuilt) { @@ -964,7 +1099,13 @@ export async function pipePluginChatWithStrip( if (field === "text" && raw.length > 0) visibleTextChars += raw.length; return rawEvent + "\n\n"; } - if (field === "text" && clean.length > 0) visibleTextChars += clean.length; + if (field === "text" && clean.length > 0) { + visibleTextChars += clean.length; + if (droppedTagInFrame) { + releasedMarkupChars += clean.length; + droppedTagInFrame = false; + } + } if (clean.length === 0 && Object.keys(d ?? {}).length <= 2) return ""; return rebuildEvent(rawEvent, { ...ev, delta: { ...d, [field]: clean } }); }; @@ -994,11 +1135,31 @@ export async function pipePluginChatWithStrip( await write(rawEvent + "\n\n"); continue; } + if (retryFraming(ev)) continue; + if (protocol === "anthropic" && ev["type"] === "content_block_start") blocksForwarded++; if (ev["type"] === "message_stop") sawTerminal = true; if (ev["type"] === "message_delta") { const d = ev["delta"] as Record | undefined; if (d && typeof d["stop_reason"] === "string") finalFinishReason = d["stop_reason"] as string; } + // Each wire declares its terminal on its own event: anthropic on + // message_delta's stop_reason, openai on the finish_reason chunk + // ([DONE] only closes the stream). Read here so the retry + // decision can be taken before that event reaches the client. + let turnTerminal: string | undefined; + if (protocol === "anthropic" && ev["type"] === "message_delta") turnTerminal = finalFinishReason; + else if (protocol === "openai") { + const choices = ev["choices"]; + if (Array.isArray(choices)) { + for (const c of choices) { + const fr = c && typeof c === "object" ? (c as Record)["finish_reason"] : undefined; + if (typeof fr === "string" && fr.length > 0) { + finalFinishReason = fr; + turnTerminal = fr; + } + } + } + } const sample = usageFromSseEvent(ev); if (sample) mergeUsageSample(acc, sample); // #408: backfill the input-side usage so the host anchors on @@ -1006,7 +1167,12 @@ export async function pipePluginChatWithStrip( // processors run and only rebuild when they return the event // verbatim — otherwise their render-tag stripping is lost. const out = protocol === "anthropic" ? processAnthropic(ev, rawEvent) : processOpenai(ev, rawEvent); - if (out.length > 0) await write(out); + // The gate runs AFTER this event is processed: a coalesced chunk + // can carry content AND the finish reason, so its own text has to + // count before the turn may be called empty. The event's output is + // dropped only when the retry takes the turn over. + if (turnTerminal !== undefined && (await retryEmptyTurn(turnTerminal))) continue; + if (out.length > 0) await write(offsetRetryIndices(out)); } } if (res.destroyed || res.writableEnded) break; @@ -1021,9 +1187,9 @@ export async function pipePluginChatWithStrip( // blank-line-delimited block — corrupting the truncation signal. Drop // it when the signal follows: an unterminated event is unparseable by // the client anyway (same as the pre-#721 bare end). - if (!truncated && buf.length > 0 && !res.destroyed && !res.writableEnded) await write(buf); + if (!truncated && buf.length > 0 && !res.destroyed && !res.writableEnded) await write(offsetRetryIndices(buf)); const rest = flushTails(); - if (rest.length > 0 && !res.destroyed && !res.writableEnded) await write(rest); + if (rest.length > 0 && !res.destroyed && !res.writableEnded) await write(offsetRetryIndices(rest)); // Settle BEFORE res.end() in the finally below: the client can issue // its next request (e.g. /__bili/plugin/status, or the follow-up turn // that reads lastInputTokens for the nudge decision) the moment the @@ -1050,7 +1216,7 @@ export async function pipePluginChatWithStrip( // written raw it would fuse with the signal frame. try { const rest = flushTails(); - if (rest.length > 0) await write(rest); + if (rest.length > 0) await write(offsetRetryIndices(rest)); } catch { /* client half-gone; the emission below is best-effort too */ } @@ -1092,15 +1258,23 @@ function hadTextOtherThanTextFields(choices: unknown): boolean { * * Also serves proxy-mode Responses SSE that skipped compress injection * (#460, e.g. ACP_NO_INJECT_TOOL). Pass no session there — see - * pipePluginChatWithStrip for why usage accounting must be skipped. */ + * pipePluginChatWithStrip for why usage accounting must be skipped. + * + * #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 + * 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( stream: ReadableStream, - res: import("node:http").ServerResponse, + res: ServerResponse, session?: Session, log?: (msg: string) => void, + refetch?: () => Promise | null>, ): Promise { - const reader = stream.getReader(); - const decoder = new TextDecoder("utf-8"); + let reader = stream.getReader(); + let decoder = new TextDecoder("utf-8"); let buf = ""; const acc: UsageSample = {}; const tagFilter = composeStreamFilters( @@ -1116,7 +1290,27 @@ export async function pipePluginResponsesWithStrip( // #673: turn-level observability for degenerate terminal turns. let sawFunctionCall = false; let sawReasoning = false; + /** The model emitted markup the filter stripped (or would strip): proof the + * turn produced output, even when none survived to be visible. */ + let sawStrippedEcho = false; let responseStatus: string | undefined; + // Degenerate-turn retry (#732/#821 for this pipe). The first attempt's + // done-family events are HELD until its completion event decides the turn: + // released in place on a healthy turn, dropped whole when the retry takes + // over, so the client sees one turn carrying one set of ids. + let degenerateRetried = false; + let inRetry = false; + /** Text the client actually assembled from this attempt's deltas. */ + let visibleTextChars = 0; + /** Done-family events held for the attempt in flight. */ + let heldEvents: string[] = []; + /** Text those held events would hand the client, post-strip. */ + let heldVisibleChars = 0; + /** The ids the client already holds (first attempt), which the retry's own + * created/added events are dropped in favour of. */ + let heldItemId: unknown; + let heldOutputIndex: unknown; + let heldResponseId: unknown; const write = (s: string): Promise => { if (!res.write(Buffer.from(s, "utf8"))) { return new Promise((r) => res.once("drain", () => r())); @@ -1162,11 +1356,118 @@ export async function pipePluginResponsesWithStrip( const flushTail = (after: string) => { const tail = tagFilter.flush(); if (tail.length > 0) { - const meta = lastDeltaMeta ?? {}; + visibleTextChars += tail.length; + const meta = inRetry && heldItemId !== undefined ? { item_id: heldItemId, output_index: heldOutputIndex } : (lastDeltaMeta ?? {}); return `data: ${JSON.stringify({ type: "response.output_text.delta", ...meta, delta: tail })}\n\n` + after; } return after; }; + /** Visible (post-strip) text a done-family event carries — what the client + * would assemble from it. It decides the turn's degeneracy together with + * the deltas already forwarded. */ + const responsesEventTextLength = (ev: Record): number => { + let text = typeof ev["text"] === "string" ? (ev["text"] as string) : ""; + const part = ev["part"]; + if (part && typeof part === "object" && typeof (part as Record)["text"] === "string") { + text += (part as Record)["text"] as string; + } + const item = ev["item"]; + const content = item && typeof item === "object" ? (item as Record)["content"] : undefined; + if (Array.isArray(content)) { + for (const c of content) { + if (c && typeof c === "object" && typeof (c as Record)["text"] === "string") { + text += (c as Record)["text"] as string; + } + } + } + return text.length; + }; + /** Whether a serialized event must be rebuilt rather than forwarded: the + * retry's ids are rewritten in the parsed event, and a processor with + * nothing to strip forwards the original bytes, which would leave them + * untouched. */ + const retryRewritePending = (): boolean => + inRetry && (heldItemId !== undefined || heldOutputIndex !== undefined || heldResponseId !== undefined); + /** While the retry stream feeds the client, the framing the FIRST attempt + * opened is still open: the retry's own created/added events would hand the + * client a second set of ids, so they are dropped. Returns true when the + * event was consumed. */ + const retryFraming = (type: unknown): boolean => { + if (!inRetry) return false; + return type === "response.created" || type === "response.output_item.added" || type === "response.content_part.added"; + }; + /** Every id the retry carries is rewritten onto the first attempt's, so the + * client's assembled item stays the one it already holds. */ + const rewriteRetryIds = (ev: Record): void => { + if (!inRetry) return; + if (heldItemId !== undefined) ev["item_id"] = heldItemId; + if (heldOutputIndex !== undefined) ev["output_index"] = heldOutputIndex; + // `response.output_item.*` carries the item's identity nested as `item.id` + // rather than `item_id`. The done event is released to the client, so + // without this the client watches the item it holds be replaced. + const item = ev["item"]; + if (heldItemId !== undefined && item && typeof item === "object") { + (item as Record)["id"] = heldItemId; + } + const resp = ev["response"]; + if (!resp || typeof resp !== "object") return; + const r = resp as Record; + if (heldResponseId !== undefined) r["id"] = heldResponseId; + const output = r["output"]; + if (heldItemId !== undefined && Array.isArray(output)) { + for (const item of output) { + if (item && typeof item === "object") (item as Record)["id"] = heldItemId; + } + } + }; + /** One-shot re-issue when a Responses turn reaches its completion with + * nothing visible: the tag-echo case, where the filter empties the only + * text the model emitted and the host aborts an empty completed turn. + * 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 => { + if (degenerateRetried || refetch === undefined) return false; + if (visibleTextChars > 0 || heldVisibleChars > 0 || sawFunctionCall) return false; + if (status !== "completed") return false; + if (res.destroyed || res.writableEnded) return false; + // A turn the model left genuinely bare — no reasoning, no stripped + // echo — is the upstream's own empty answer, not a stall: re-issuing it + // 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)"); + let next: ReadableStream | null = null; + try { + next = await refetch(); + } catch (e) { + log?.(`[plugin] degenerate-terminal retry failed (${e instanceof Error ? e.message : String(e)}); passing the empty turn through`); + return false; + } + if (!next) return false; + // The turn is NOT over: the retry carries its own terminal, and a cut in + // it must still raise the truncation signal (#721). + sawTerminal = false; + responseStatus = undefined; + heldEvents = []; + heldVisibleChars = 0; + inRetry = true; + // The first attempt's held filter state belongs to text the client never + // saw (an emptying tag echo): the retry's content is filtered from + // scratch, so a partial tag there cannot swallow its opening characters. + tagFilter.flush(); + // The first attempt is terminal and its body is drained; close the + // reader we are abandoning rather than leaving the socket held. + try { + await reader.cancel(); + } catch { + /* already closed */ + } + reader = next.getReader(); + decoder = new TextDecoder("utf-8"); + buf = ""; + return true; + }; try { for (;;) { const { done, value } = await reader.read(); @@ -1204,22 +1505,53 @@ export async function pipePluginResponsesWithStrip( } const resp = ev["response"] as Record | undefined; if (resp && typeof resp["status"] === "string") responseStatus = resp["status"] as string; + if (!inRetry) { + // The ids the client holds are the first attempt's: the + // retry is reframed onto them (see rewriteRetryIds). + if (type === "response.created" && resp && resp["id"] !== undefined) heldResponseId = resp["id"]; + if (type === "response.output_item.added") { + const added = ev["item"] as Record | undefined; + if (added && added["id"] !== undefined) heldItemId = added["id"]; + if (ev["output_index"] !== undefined) heldOutputIndex = ev["output_index"]; + } + } + } + if (retryFraming(type)) continue; + if (type === "response.output_text.done" || type === "response.content_part.done" || type === "response.output_item.done") { + // HELD until the completion event decides the turn (see + // retryEmptyTurn): releasing it earlier would hand the + // client the echo's own text exactly when the retry is + // about to replace it. + const hadEchoText = containsRenderTagText(jsonStr) || containsMarkerLineText(jsonStr); + if (hadEchoText) sawStrippedEcho = true; + let evOut = ev; + let rebuild = hadEchoText || retryRewritePending(); + if (rebuild) evOut = stripResponsesText(ev); + rewriteRetryIds(evOut); + heldVisibleChars += responsesEventTextLength(evOut); + heldEvents.push(rebuild ? rebuildEvent(rawEvent, evOut) : rawEvent + "\n\n"); + continue; } - if ( - type === "response.output_text.done" || - type === "response.content_part.done" || - type === "response.output_item.done" || - type === "response.completed" || - type === "response.failed" || - type === "response.incomplete" - ) { - if (type === "response.completed" || type === "response.failed" || type === "response.incomplete") sawTerminal = true; + if (type === "response.completed" || type === "response.failed" || type === "response.incomplete") { + sawTerminal = true; + if (await retryEmptyTurn(type === "response.completed" ? "completed" : undefined)) { + // The retry took over: this attempt's held family and + // its own completion frame are dropped together. + heldEvents = []; + heldVisibleChars = 0; + continue; + } + const tailFrame = flushTail(""); + if (tailFrame.length > 0) await write(tailFrame); + for (const held of heldEvents) await write(held); + heldEvents = []; + heldVisibleChars = 0; // done-family events also carry full text payloads — strip those too. let evOut = ev; - let rebuild = containsRenderTagText(jsonStr) || containsMarkerLineText(jsonStr); + let rebuild = containsRenderTagText(jsonStr) || containsMarkerLineText(jsonStr) || retryRewritePending(); if (rebuild) evOut = stripResponsesText(ev); - const out = rebuild ? rebuildEvent(rawEvent, evOut) : rawEvent + "\n\n"; - await write(flushTail(out)); + rewriteRetryIds(evOut); + await write(rebuild ? rebuildEvent(rawEvent, evOut) : rawEvent + "\n\n"); continue; } if (type === "response.output_text.delta" && typeof ev["delta"] === "string") { @@ -1228,18 +1560,28 @@ export async function pipePluginResponsesWithStrip( await write(rawEvent + "\n\n"); continue; } - if (!mayStartRenderTag(delta) && !mayStartMarkerLine(delta) && !tagFilter.pending()) { + if (!retryRewritePending() && !mayStartRenderTag(delta) && !mayStartMarkerLine(delta) && !tagFilter.pending()) { await write(rawEvent + "\n\n"); continue; } const clean = tagFilter.push(delta); lastDeltaMeta = { item_id: ev["item_id"], output_index: ev["output_index"] }; - if (clean.length === 0) continue; - if (clean === delta) { + if (!inRetry && heldItemId === undefined && ev["item_id"] !== undefined) { + heldItemId = ev["item_id"]; + heldOutputIndex = ev["output_index"]; + } + if (clean.length === 0) { + sawStrippedEcho = true; + continue; + } + visibleTextChars += clean.length; + if (clean === delta && !retryRewritePending()) { await write(rawEvent + "\n\n"); continue; } - await write(rebuildEvent(rawEvent, { ...ev, delta: clean })); + const rebuilt = { ...ev, delta: clean }; + rewriteRetryIds(rebuilt); + await write(rebuildEvent(rawEvent, rebuilt)); continue; } await write(rawEvent + "\n\n"); diff --git a/src/server.ts b/src/server.ts index 6a2c18272b..5c6867ede6 100644 --- a/src/server.ts +++ b/src/server.ts @@ -63,6 +63,7 @@ import { containsToolCallXmlFragment } from "./loop/tag-echo-filter.js"; import { isStrictReasoningEcho, normalizeStrictEchoReasoning } from "./strict-echo.js"; export { isStrictReasoningEcho, normalizeStrictEchoReasoning }; import { isFakeCompletion, injectFakeCompletionHint, maxFakeCompletionRetries, fakeBufCap } from "./fake-completion.js"; +import { makeContinuationRefetch } 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"; @@ -3575,9 +3576,49 @@ async function forward( } if (prepared.stream) { if (prepared.protocol === "responses") { - await pipePluginResponsesWithStrip(pluginBody, res, prepared.session, (msg) => log("info", `[${prepared.session.id}] ${msg}`)); + // #732/#821 applies to this pipe too (#871): the agent's own + // body, held here with its URL and headers, is re-issued once + // when the turn completes with nothing visible. + await pipePluginResponsesWithStrip( + pluginBody, + res, + prepared.session, + (msg) => log("info", `[${prepared.session.id}] ${msg}`), + makeContinuationRefetch({ + protocol: "responses", + body, + upstreamUrl, + reqHeaders: buildForwardHeaders(headers), + proxyUrl, + dispatcher, + signal: clientAbort.signal, + log, + label: prepared.session.id, + }), + ); } else { - await pipePluginChatWithStrip(pluginBody, res, prepared.protocol, prepared.session, (msg) => log("info", `[${prepared.session.id}] ${msg}`)); + // #732/#821: the plugin pipe re-issues the agent's own body + // once when a turn ends with nothing visible (the render-tag + // echo case) — it holds the URL and headers, this is where + // they live. + await pipePluginChatWithStrip( + pluginBody, + res, + prepared.protocol, + prepared.session, + (msg) => log("info", `[${prepared.session.id}] ${msg}`), + makeContinuationRefetch({ + protocol: prepared.protocol, + body, + upstreamUrl, + reqHeaders: buildForwardHeaders(headers), + proxyUrl, + dispatcher, + signal: clientAbort.signal, + log, + label: prepared.session.id, + }), + ); } } else { await pipePluginJson(pluginBody, res, prepared.session, prepared.protocol); @@ -3658,7 +3699,23 @@ async function forward( // Responses stream. Same pipe as the non-injected branch below; // no session, so usage accounting stays off. if ((upstream.headers.get("content-type") ?? "").includes("text/event-stream")) { - await pipePluginResponsesWithStrip(toClient, res, undefined, tagLog); + await pipePluginResponsesWithStrip( + toClient, + res, + undefined, + tagLog, + makeContinuationRefetch({ + protocol: "responses", + body, + upstreamUrl, + reqHeaders: buildForwardHeaders(headers), + proxyUrl, + dispatcher, + signal: clientAbort.signal, + log, + label: prepared.session.id, + }), + ); } else { await pipeThrough(toClient, res); } @@ -3682,9 +3739,42 @@ async function forward( const p = prepared; const tagLog = (msg: string) => log("info", `[${p.session.id}] ${msg}`); if (p.protocol === "responses") { - await pipePluginResponsesWithStrip(responseBody, res, undefined, tagLog); + await pipePluginResponsesWithStrip( + responseBody, + res, + undefined, + tagLog, + makeContinuationRefetch({ + protocol: "responses", + body, + upstreamUrl, + reqHeaders: buildForwardHeaders(headers), + proxyUrl, + dispatcher, + signal: clientAbort.signal, + log, + label: p.session.id, + }), + ); } else { - await pipePluginChatWithStrip(responseBody, res, p.protocol, undefined, tagLog); + await pipePluginChatWithStrip( + responseBody, + res, + p.protocol, + undefined, + tagLog, + makeContinuationRefetch({ + protocol: p.protocol, + body, + upstreamUrl, + reqHeaders: buildForwardHeaders(headers), + proxyUrl, + dispatcher, + signal: clientAbort.signal, + log, + label: p.session.id, + }), + ); } } else if ( prepared && diff --git a/src/wire-body.ts b/src/wire-body.ts new file mode 100644 index 0000000000..27ad36180d --- /dev/null +++ b/src/wire-body.ts @@ -0,0 +1,50 @@ +import type { WireProtocol } from "./util.js"; + +/** Append `text` as a trailing user turn on the wire's own request body. + * + * Every post-hoc mutation of an ALREADY assembled body goes through here (the + * fake-completion hint and the degenerate-turn continuation nudge): each wire + * marks a user turn differently, and back-to-back user turns break the + * grouping strict providers apply to the replayed conversation, so the text is + * MERGED into the last user turn when there is one and a fresh user turn is + * started only when there is not. + * + * Returns null when the body is unparseable or carries no turn array — the + * caller skips the mutation rather than sending a body it has mangled. */ +export function appendTrailingUserText(protocol: WireProtocol, body: string | Buffer, text: string): string | null { + const raw = typeof body === "string" ? body : body.toString("utf8"); + let obj: Record; + try { + obj = JSON.parse(raw) as Record; + } catch { + return null; + } + if (protocol === "responses") { + const input = obj.input; + if (!Array.isArray(input)) return null; + const arr = input as Record[]; + const last = arr[arr.length - 1]; + if (last && typeof last === "object" && last.role === "user") { + const c = last.content; + if (typeof c === "string") last.content = `${c}\n\n${text}`; + else if (Array.isArray(c)) last.content = [...c, { type: "input_text", text }]; + else last.content = text; + } else { + arr.push({ role: "user", content: [{ type: "input_text", text }] }); + } + return JSON.stringify(obj); + } + const messages = obj.messages; + if (!Array.isArray(messages)) return null; + const arr = messages as Record[]; + const last = arr[arr.length - 1]; + if (last && typeof last === "object" && last.role === "user") { + const c = last.content; + if (typeof c === "string") last.content = `${c}\n\n${text}`; + else if (Array.isArray(c)) last.content = [...c, { type: "text", text }]; + else last.content = text; + } else { + arr.push({ role: "user", content: text }); + } + return JSON.stringify(obj); +} diff --git a/tests/plugin-empty-turn-retry.test.ts b/tests/plugin-empty-turn-retry.test.ts new file mode 100644 index 0000000000..75c5216b78 --- /dev/null +++ b/tests/plugin-empty-turn-retry.test.ts @@ -0,0 +1,480 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { pipePluginChatWithStrip, pipePluginResponsesWithStrip } from "../src/plugin.ts"; +import { setLogCapture } from "../src/logger.ts"; +import { DEGENERATE_RETRY_NUDGE, injectContinuationNudge } 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 +// sessions among them). A turn whose ONLY text is an echoed render tag is +// emptied by the tag filter, so the host receives a completed turn with no text +// and no tool call and aborts it — the #732/#821 retry, which the compress loop +// already ships for its own request path, is re-issued here on the agent's own +// body. These tests pin the pipe half of that contract. + +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(); + } + }, + }); +} + +const TAG_OPEN = "\x3cacp tokens=\"247\" type=\"text\"\x3e"; +const TAG_CLOSE = "\x3c/acp\x3e"; + +function chatChunk(delta: Record, extra: Record = {}): string { + return `data: ${JSON.stringify({ id: "chatcmpl-1", object: "chat.completion.chunk", created: 1, model: "qwen", choices: [{ index: 0, delta, finish_reason: null }], ...extra })}\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`; +} + +/** The failing turn in production: the model imitates the render tag it saw in + * its prompt and emits nothing else, so the filter empties the whole turn. */ +function echoOnlyTurn(): string[] { + return [chatChunk({ role: "assistant" }), chatChunk({ content: `${TAG_OPEN}m00155${TAG_CLOSE}` }), chatStop(), DONE]; +} + +function proseTurn(text: string): string[] { + return [chatChunk({ role: "assistant" }), chatChunk({ content: text }), chatStop(), DONE]; +} + +const sse = (event: string, data: unknown): string => `event: ${event}\ndata: ${JSON.stringify(data)}\n\n`; + +function anthropicEchoOnlyTurn(): string[] { + return [ + sse("message_start", { type: "message_start", message: { id: "msg_1", role: "assistant", usage: { input_tokens: 40 } } }), + sse("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } }), + sse("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: `${TAG_OPEN}m00155${TAG_CLOSE}` } }), + sse("content_block_stop", { type: "content_block_stop", index: 0 }), + sse("message_delta", { type: "message_delta", delta: { stop_reason: "end_turn", stop_sequence: null }, usage: { output_tokens: 3 } }), + sse("message_stop", { type: "message_stop" }), + ]; +} + +function anthropicProseTurn(text: string): string[] { + return [ + sse("message_start", { type: "message_start", message: { id: "msg_2", role: "assistant", usage: { input_tokens: 40 } } }), + sse("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } }), + sse("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "text_delta", text } }), + sse("content_block_stop", { type: "content_block_stop", index: 0 }), + sse("message_delta", { type: "message_delta", delta: { stop_reason: "end_turn", stop_sequence: null }, usage: { output_tokens: 9 } }), + sse("message_stop", { type: "message_stop" }), + ]; +} + +function textDeltas(raw: string, protocol: "openai" | "anthropic"): string { + const re = + protocol === "anthropic" ? /"type":"text_delta","text":"((?:[^"\\]|\\.)*)"/g : /"content":"((?:[^"\\]|\\.)*)"/g; + return [...raw.matchAll(re)].map((m) => JSON.parse(`"${m[1]}"`) as string).join(""); +} + +test("plugin chat retries once when the turn's only text was a stripped render-tag echo", async () => { + const out: string[] = []; + const res = makeRes(out); + let calls = 0; + const refetch = () => { + calls += 1; + return Promise.resolve(streamOf(proseTurn("real answer after the nudge"))); + }; + await pipePluginChatWithStrip(streamOf(echoOnlyTurn()), res, "openai", makeSession(), undefined, refetch); + const text = out.join(""); + assert.equal(calls, 1, "exactly one re-issue"); + assert.equal(textDeltas(text, "openai"), "real answer after the nudge", "the retry's content reaches the client"); + assert.equal((text.match(/\[DONE\]/g) ?? []).length, 1, "one turn, one terminal"); + assert.ok(!text.includes("m00155"), "the echoed tag never leaks"); + assert.equal((text.match(/"finish_reason":"stop"/g) ?? []).length, 1, "the first attempt's terminal is dropped, not doubled"); +}); + +test("plugin chat does not retry a turn that already delivered visible text", async () => { + const out: string[] = []; + let calls = 0; + const refetch = () => { + calls += 1; + return Promise.resolve(streamOf(proseTurn("unwanted"))); + }; + await pipePluginChatWithStrip(streamOf(proseTurn("the model answered")), makeRes(out), "openai", makeSession(), undefined, refetch); + assert.equal(calls, 0, "no re-issue when the turn was not empty"); + assert.equal(textDeltas(out.join(""), "openai"), "the model answered"); +}); + +test("plugin chat does not retry a turn that produced a tool call", async () => { + const out: string[] = []; + let calls = 0; + const refetch = () => { + calls += 1; + return Promise.resolve(streamOf(proseTurn("unwanted"))); + }; + const events = [ + chatChunk({ role: "assistant" }), + chatChunk({ content: `${TAG_OPEN}m00155${TAG_CLOSE}` }), + `data: ${JSON.stringify({ id: "chatcmpl-1", object: "chat.completion.chunk", created: 1, model: "qwen", choices: [{ index: 0, delta: { tool_calls: [{ index: 0, id: "call_1", type: "function", function: { name: "compress", arguments: "{}" } }] }, finish_reason: null }] })}\n\n`, + chatStop("tool_calls"), + DONE, + ]; + await pipePluginChatWithStrip(streamOf(events), makeRes(out), "openai", makeSession(), undefined, refetch); + assert.equal(calls, 0, "a tool call is not an empty turn"); + assert.ok(out.join("").includes("compress"), "the agent's own tool call still passes through"); +}); + +test("plugin chat does not retry a turn the provider ended on a non-clean reason", async () => { + const out: string[] = []; + let calls = 0; + const refetch = () => { + calls += 1; + return Promise.resolve(streamOf(proseTurn("unwanted"))); + }; + const events = [chatChunk({ role: "assistant" }), chatChunk({ content: `${TAG_OPEN}m00155${TAG_CLOSE}` }), chatStop("content_filter"), DONE]; + await pipePluginChatWithStrip(streamOf(events), makeRes(out), "openai", makeSession(), undefined, refetch); + assert.equal(calls, 0, "a filtered turn must not be re-prompted"); +}); + +test("plugin chat passes the empty turn through and warns when the retry is empty too", async () => { + const out: string[] = []; + const logs: string[] = []; + setLogCapture((_level, msg) => { + logs.push(msg); + }); + let calls = 0; + const refetch = () => { + calls += 1; + return Promise.resolve(streamOf([DONE])); + }; + try { + await pipePluginChatWithStrip(streamOf(echoOnlyTurn()), makeRes(out), "openai", makeSession(), undefined, refetch); + } finally { + setLogCapture(null); + } + const text = out.join(""); + assert.equal(calls, 1, "one attempt only"); + assert.equal((text.match(/\[DONE\]/g) ?? []).length, 1, "the retry's own terminal closes the turn"); + assert.ok( + logs.some((l) => l.includes("degenerate")), + `a still-empty turn stays visible to the operator, got: ${JSON.stringify(logs)}`, + ); +}); + +test("plugin chat passes the empty turn through when the retry cannot be issued", async () => { + const out: string[] = []; + const logs: string[] = []; + setLogCapture((_level, msg) => { + logs.push(msg); + }); + try { + await pipePluginChatWithStrip(streamOf(echoOnlyTurn()), makeRes(out), "openai", makeSession(), undefined, () => Promise.resolve(null)); + } finally { + setLogCapture(null); + } + const text = out.join(""); + assert.equal((text.match(/\[DONE\]/g) ?? []).length, 1, "the original terminal is presented unchanged"); + const dataLines = text.split("\n").filter((l) => l.startsWith("data:")).filter((l) => !l.includes("[DONE]")); + for (const l of dataLines) { + assert.doesNotThrow(() => JSON.parse(l.slice(5).trim()), "every data line stays valid JSON"); + } +}); + +test("plugin chat (anthropic) lands the retry's content in a block after the client's own", async () => { + const out: string[] = []; + let calls = 0; + const refetch = () => { + calls += 1; + return Promise.resolve(streamOf(anthropicProseTurn("the continuation"))); + }; + await pipePluginChatWithStrip(streamOf(anthropicEchoOnlyTurn()), makeRes(out), "anthropic", makeSession(), undefined, refetch); + const text = out.join(""); + assert.equal(calls, 1); + assert.equal((text.match(/"type":"message_start"/g) ?? []).length, 1, "the retry must not re-open the message"); + assert.equal((text.match(/"type":"message_stop"/g) ?? []).length, 1, "one turn, one terminal"); + assert.equal(textDeltas(text, "anthropic"), "the continuation"); + assert.ok(text.includes('"index":1'), `the retry's block follows the client's closed block 0, got: ${text}`); +}); + +test("the continuation retry body carries the nudge as a trailing user turn", () => { + const openaiBody = JSON.stringify({ model: "m", messages: [{ role: "user", content: "hi" }] }); + const merged = JSON.parse(injectContinuationNudge("openai", openaiBody)!) as { messages: { role: string; content: string }[] }; + assert.equal(merged.messages.length, 1, "merged into the existing user turn instead of adding a second one"); + assert.equal(merged.messages[0]!.role, "user"); + assert.ok(merged.messages[0]!.content.startsWith("hi"), "the original text is preserved"); + assert.ok( + merged.messages[0]!.content.includes("ended with no visible text and no tool call"), + `the nudge text is appended, got: ${merged.messages[0]!.content}`, + ); + + const anthropicBody = JSON.stringify({ messages: [{ role: "user", content: [{ type: "text", text: "hi" }] }] }); + const asBlock = JSON.parse(injectContinuationNudge("anthropic", anthropicBody)!) as { + messages: { content: { type: string; text: string }[] }[]; + }; + assert.equal(asBlock.messages[0]!.content.length, 2, "anthropic content is a block array"); + assert.equal(asBlock.messages[0]!.content[1]!.text, DEGENERATE_RETRY_NUDGE); + + const assistantLast = JSON.stringify({ messages: [{ role: "assistant", content: "done" }] }); + const appended = JSON.parse(injectContinuationNudge("openai", assistantLast)!) as { messages: { role: string }[] }; + assert.equal(appended.messages.length, 2, "a body ending on the assistant side gets a fresh user turn"); + + assert.equal(injectContinuationNudge("openai", JSON.stringify({ foo: 1 })), null, "unusable body is reported, not mangled"); + assert.equal(injectContinuationNudge("openai", "{not json"), null); +}); + +// The Responses pipe carries the same defect for omp's codex subagent turns, +// but its terminal is a FAMILY (output_text.done, content_part.done, +// output_item.done) decided by response.completed, and every event carries ids +// the client already holds. So the retry holds that family until the completion +// decides, and reframes the retry onto those ids: the client sees one turn. +function responsesEchoOnlyTurn(): 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: `${TAG_OPEN}m00155${TAG_CLOSE}` }), + sse("response.output_text.done", { type: "response.output_text.done", item_id: "item_1", output_index: 0, text: `${TAG_OPEN}m00155${TAG_CLOSE}` }), + sse("response.completed", { + type: "response.completed", + response: { + id: "resp_1", + status: "completed", + output: [{ id: "item_1", type: "message", content: [{ type: "output_text", text: `${TAG_OPEN}m00155${TAG_CLOSE}` }] }], + }, + }), + ]; +} + +function responsesProseTurn(text: string, responseId = "resp_2", itemId = "item_2", status = "completed"): string[] { + return [ + sse("response.created", { type: "response.created", response: { id: responseId, status: "in_progress" } }), + sse("response.output_item.added", { type: "response.output_item.added", output_index: 0, item: { id: itemId, type: "message", content: [] } }), + sse("response.content_part.added", { type: "response.content_part.added", item_id: itemId, output_index: 0, part: { type: "output_text", text: "" } }), + sse("response.output_text.delta", { type: "response.output_text.delta", item_id: itemId, output_index: 0, delta: text }), + sse("response.output_text.done", { type: "response.output_text.done", item_id: itemId, output_index: 0, text }), + // The frame that carries the item's identity NESTED rather than as + // `item_id`: it reaches the client, so it must be rewritten too. + sse("response.output_item.done", { type: "response.output_item.done", output_index: 0, item: { id: itemId, type: "message", content: [{ type: "output_text", text }] } }), + sse(status === "completed" ? "response.completed" : "response.failed", { + type: status === "completed" ? "response.completed" : "response.failed", + response: { id: responseId, status, output: [{ id: itemId, type: "message", content: [{ type: "output_text", text }] }] }, + }), + ]; +} + +function responsesDeltas(raw: string): string { + return [...raw.matchAll(/"delta":"((?:[^"\\]|\\.)*)"/g)].map((m) => JSON.parse(`"${m[1]}"`) as string).join(""); +} + +test("plugin responses retries once when the turn's only text was a stripped echo", async () => { + const out: string[] = []; + let calls = 0; + const refetch = () => { + calls += 1; + return Promise.resolve(streamOf(responsesProseTurn("recovered after the nudge"))); + }; + await pipePluginResponsesWithStrip(streamOf(responsesEchoOnlyTurn()), makeRes(out), makeSession(), undefined, refetch); + const text = out.join(""); + assert.equal(calls, 1, "exactly one re-issue"); + assert.equal(responsesDeltas(text), "recovered after the nudge", "the retry's prose is what the client assembles"); + assert.equal((text.match(/"type":"response\.created"/g) ?? []).length, 1, "the retry does not open a second response"); + assert.equal((text.match(/"type":"response\.completed"/g) ?? []).length, 1, "one turn, one terminal"); + assert.equal((text.match(/"type":"response\.output_item\.added"/g) ?? []).length, 1, "the retry's own added events are dropped"); + assert.ok(!text.includes("m00155"), "the echoed tag never leaks"); + assert.ok(!text.includes("item_2") && !text.includes("resp_2"), `the retry's ids are rewritten onto the client's, got: ${text}`); + assert.ok(text.includes('"item_id":"item_1"'), "the retry's deltas carry the item id the client already holds"); +}); + +test("plugin responses releases the held done-family events on a healthy turn", async () => { + const out: string[] = []; + let calls = 0; + const refetch = () => { + calls += 1; + return Promise.resolve(streamOf(responsesProseTurn("unwanted"))); + }; + await pipePluginResponsesWithStrip(streamOf(responsesProseTurn("the model answered")), makeRes(out), makeSession(), undefined, refetch); + const text = out.join(""); + assert.equal(calls, 0, "no re-issue when the turn was not empty"); + assert.equal(responsesDeltas(text), "the model answered"); + const done = text.indexOf('"type":"response.output_text.done"'); + const completed = text.indexOf('"type":"response.completed"'); + assert.ok(done >= 0 && completed > done, `holding must not reorder the turn, got: ${text}`); +}); + +test("plugin responses does not retry a completed turn whose text arrives only in the done event", async () => { + const out: string[] = []; + let calls = 0; + const refetch = () => { + calls += 1; + return Promise.resolve(streamOf(responsesProseTurn("unwanted"))); + }; + const events = responsesProseTurn("the whole answer off-stream", "resp_3", "item_3").filter((e) => !e.includes("response.output_text.delta")); + await pipePluginResponsesWithStrip(streamOf(events), makeRes(out), makeSession(), undefined, refetch); + assert.equal(calls, 0, "text the client would receive from the done event is not an empty turn"); +}); + +test("plugin responses does not retry a turn that produced a function call", async () => { + const out: string[] = []; + let calls = 0; + const refetch = () => { + calls += 1; + return Promise.resolve(streamOf(responsesProseTurn("unwanted"))); + }; + const events = [ + 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: "function_call", name: "read" } }), + sse("response.output_text.delta", { type: "response.output_text.delta", item_id: "item_1", output_index: 0, delta: `${TAG_OPEN}m00155${TAG_CLOSE}` }), + sse("response.completed", { type: "response.completed", response: { id: "resp_1", status: "completed", output: [{ id: "item_1", type: "function_call" }] } }), + ]; + await pipePluginResponsesWithStrip(streamOf(events), makeRes(out), makeSession(), undefined, refetch); + assert.equal(calls, 0, "a tool call is not an empty turn"); + assert.ok(out.join("").includes('"function_call"'), "the agent's own tool surface still passes through"); +}); + +test("plugin responses does not retry a turn the provider failed or cut", async () => { + const out: string[] = []; + let calls = 0; + const refetch = () => { + calls += 1; + return Promise.resolve(streamOf(responsesProseTurn("unwanted"))); + }; + await pipePluginResponsesWithStrip( + streamOf(responsesProseTurn(`${TAG_OPEN}m00155${TAG_CLOSE}`, "resp_4", "item_4", "failed")), + makeRes(out), + makeSession(), + undefined, + refetch, + ); + assert.equal(calls, 0, "a failed turn is a real terminal, not an empty one"); +}); + +// #721: takeover resets sawTerminal, so a retry that is itself cut must still +// warn the client instead of closing bare. +const TRUNC_MARKER = "upstream_stream_truncated"; + +/** A single chunk carrying content AND the finish reason — the coalescing some + * upstreams do, which the gate must count before it can call a turn empty. */ +function coalescedChunk(content: string, reason: string): string { + return `data: ${JSON.stringify({ id: "chatcmpl-1", object: "chat.completion.chunk", created: 1, model: "qwen", choices: [{ index: 0, delta: { content }, finish_reason: reason }] })}\n\n`; +} + +test("plugin chat: a coalesced content+finish_reason chunk counts its own text", async () => { + const out: string[] = []; + let calls = 0; + const refetch = () => { + calls += 1; + return Promise.resolve(streamOf(proseTurn("unwanted"))); + }; + await pipePluginChatWithStrip(streamOf([coalescedChunk("the whole answer", "stop"), DONE]), makeRes(out), "openai", makeSession(), undefined, refetch); + assert.equal(calls, 0, "content delivered in the same chunk as the finish reason is not an empty turn"); + assert.equal(textDeltas(out.join(""), "openai"), "the whole answer"); +}); + +test("plugin chat: a coalesced echo+finish_reason chunk still retries once", async () => { + const out: string[] = []; + let calls = 0; + const refetch = () => { + calls += 1; + return Promise.resolve(streamOf(proseTurn("recovered after the coalesced echo"))); + }; + await pipePluginChatWithStrip(streamOf([coalescedChunk(`${TAG_OPEN}m00155${TAG_CLOSE}`, "stop"), DONE]), makeRes(out), "openai", makeSession(), undefined, refetch); + const text = out.join(""); + assert.equal(calls, 1, "the gate runs after the chunk is processed, so the emptied echo is still seen"); + assert.equal(textDeltas(text, "openai"), "recovered after the coalesced echo"); + assert.ok(!text.includes("m00155"), "the echoed tag never leaks"); +}); + +test("plugin chat: a retry stream that is cut still raises the truncation signal", async () => { + const out: string[] = []; + let calls = 0; + const refetch = () => { + calls += 1; + return Promise.resolve(streamOf([])); + }; + await pipePluginChatWithStrip(streamOf(echoOnlyTurn()), makeRes(out), "openai", makeSession(), undefined, refetch); + const text = out.join(""); + assert.equal(calls, 1, "the retry was attempted"); + assert.ok(text.includes(TRUNC_MARKER), `a cut retry must still warn the client, got: ${text}`); + assert.ok(text.includes("data: [DONE]"), "the stream is still terminated for the client"); +}); + +/** The production shape of the unclosed-echo turn: the model opens a render tag, + * writes its tool call inside it, and never closes the tag, so the span stays + * pending and is released at the terminal. Reconstructed from the client-visible + * residue of session 01a0ab88-a02e-7459-b278-56d7dbfe9746 at + * 2026-09-16T19:43:34.613Z; the wire bytes were not kept anywhere. */ +function unclosedEchoTurn(): string[] { + const pad = " ".repeat(120); + const residue = ` m00693${pad}\n${pad}\n Checking pocket CLI surface`; + return [chatChunk({ role: "assistant" }), chatChunk({ content: `${TAG_OPEN}${residue}` }), chatStop(), DONE]; +} + +test("plugin chat retries a turn whose only visible output was an unclosed render tag", async () => { + const out: string[] = []; + let calls = 0; + const refetch = () => { + calls += 1; + return Promise.resolve(streamOf(proseTurn("real answer after the nudge"))); + }; + await pipePluginChatWithStrip(streamOf(unclosedEchoTurn()), makeRes(out), "openai", makeSession(), undefined, refetch); + const text = out.join(""); + // The dropped tag's interior has already reached the client by the time the + // terminal arrives, so the retry cannot erase it: what the fix guarantees is + // that the turn carries content the host can act on, which is what the stall + // was missing. + assert.equal(calls, 1, "a tag interior is not visible output, so the turn is retried once"); + assert.ok( + textDeltas(text, "openai").endsWith("real answer after the nudge"), + "the retry's content reaches the client after the residue", + ); +}); + +test("plugin chat emits an in-band error when the retry degenerates too", async () => { + const out: string[] = []; + const refetch = () => Promise.resolve(streamOf([chatChunk({ role: "assistant" }), chatStop(), DONE])); + await pipePluginChatWithStrip(streamOf(unclosedEchoTurn()), makeRes(out), "openai", makeSession(), undefined, refetch); + const text = out.join(""); + assert.ok( + text.includes("[ACP] stream error"), + "the client is told, rather than left with an empty turn the host never reports", + ); + // The error block closes the turn for the client; the retry stream's own + // terminator still follows it, which the client ignores because it stopped at + // the error's. + assert.equal((text.match(/\[DONE\]/g) ?? []).length, 2, "the error's terminal, then the retry's trailing terminator"); +});