From 353ba2586e72d122d4fa664e38c6374d326c6c60 Mon Sep 17 00:00:00 2001 From: awork Date: Wed, 7 Oct 2026 22:50:28 +0800 Subject: [PATCH 1/3] fix: dumpSse covers plugin lane and non-injected streaming turns (#2347) --- src/server.ts | 34 +++-- tests/issue2347-dump-sse-plugin-lane.test.ts | 139 +++++++++++++++++++ 2 files changed, 165 insertions(+), 8 deletions(-) create mode 100644 tests/issue2347-dump-sse-plugin-lane.test.ts diff --git a/src/server.ts b/src/server.ts index 9617a157b7..22f2798a59 100644 --- a/src/server.ts +++ b/src/server.ts @@ -6961,6 +6961,7 @@ async function forward( // endpoint; the outer upstreamOrigin is the configured route target). let targetOrigin: string | undefined; try { targetOrigin = new URL(upstreamUrl).origin; } catch { targetOrigin = undefined; } + let dumpRaw: Promise | undefined; // Plugin mode: the agent's native loop owns the tool surface — pass the // response through VERBATIM (a model-emitted compress call must reach the // plugin untouched) while sniffing usage so lastInputTokens (the input to @@ -7005,6 +7006,13 @@ async function forward( }); pluginBody = bufferToStream(resolvedBuf); } + // #2347: raw-dump twin of the tee below — this branch returns + // before reaching it, so plugin-mode turns need their own. + if (opts.dumpSse && prepared.stream && pluginBody) { + const [a, b] = pluginBody.tee(); + pluginBody = a; + dumpRaw = dumpStreamToFile(b, opts.dumpSse, `${Date.now()}-${safeSessionId(prepared.session.id)}-raw.sse`); + } if (prepared.stream) { if (prepared.protocol === "responses") { // #732/#821 applies to this pipe too (#871): the agent's own @@ -7062,6 +7070,9 @@ async function forward( } } finally { clearUpstreamTimer(); + // #2347: finish the dump before the turn closes — its value is the + // full upstream tail, which the client may have stopped reading early. + if (dumpRaw) await dumpRaw; } return; } @@ -7120,6 +7131,17 @@ async function forward( }); responseBody = bufferToStream(resolvedBuf); } + // #2347: dump the FINAL upstream body for every prepared streaming turn, + // regardless of rewriter branch — the previous tee sat INSIDE the useRewriter + // branch, so non-injected turns never dumped (the plugin lane above has its + // own twin tee before its early return). Must stay AFTER resolveFakeCompletion + // (it replaces responseBody wholesale; teeing earlier dumps the discarded + // stream) and BEFORE the branch split. + if (opts.dumpSse && prepared !== null && prepared.stream && responseBody) { + const [a, b] = responseBody.tee(); + responseBody = a; + dumpRaw = dumpStreamToFile(b, opts.dumpSse, `${Date.now()}-${safeSessionId(prepared.session.id)}-raw.sse`); + } // We only rewrite when THIS request actually had the compress tool // injected (per-request). Non-injected requests (OpenAI title-gen // exclusion, ACP_NO_INJECT_TOOL, auto-mode classifier bypass) must NOT @@ -7231,6 +7253,9 @@ async function forward( } else { await pipeThrough(responseBody, res); } + // #2347: finish the dump before the turn closes — its value is the full + // upstream tail, which the client may have stopped reading early. + if (dumpRaw) await dumpRaw; return; } const ctx: RewriteCtx = { @@ -7242,13 +7267,6 @@ async function forward( debug: opts.debug, }; if (prepared.stream) { - let streamToRead = responseBody; - let dumpRaw: Promise | undefined; - if (opts.dumpSse) { - const [a, b] = responseBody.tee(); - streamToRead = a; - dumpRaw = dumpStreamToFile(b, opts.dumpSse, `${Date.now()}-${safeSessionId(prepared.session.id)}-raw.sse`); - } // P1.1: wrap the rewriter loops in try/catch. If a rewriter throws // (decompress/search edge case, JSON.parse failure, fetch abort), // emitStreamError sends a protocol-appropriate error + finish so the @@ -7344,7 +7362,7 @@ async function forward( // upstream expression as outboundPayloadBreakdown. const loopBillingUpstream = route?.rewrittenUrl ?? (/^https?:\/\//i.test(req.url ?? "") ? req.url ?? undefined : opts.upstream); const loop = runCompressLoop( - streamToRead, + responseBody, { core, config, messages: prepared.processedMessages.length > 0 ? prepared.processedMessages : prepared.originalMessages, compressMessages: prepared.originalMessages, session: prepared.session, log: ctx.log, proxyUrl, upstreamOrigin: targetOrigin, protocol: prepared.protocol, textProtocol, debug: opts.debug, refreshFolded, visibilityMarkers, dumpSse: loopDumpDir ? (name, stream) => dumpStreamToFile(stream, loopDumpDir, name) : undefined, imageLearn: { host: upstreamHost(loopBillingUpstream), fp: `${imageBillingFor(opts, loopBillingUpstream)}:${imageTokenCapFor(opts, loopBillingUpstream)}` } }, parsedReq, { url: upstreamUrl, headers: reqHeaders, wireTransform, resign: applyResign }, diff --git a/tests/issue2347-dump-sse-plugin-lane.test.ts b/tests/issue2347-dump-sse-plugin-lane.test.ts new file mode 100644 index 0000000000..20cd2ab36f --- /dev/null +++ b/tests/issue2347-dump-sse-plugin-lane.test.ts @@ -0,0 +1,139 @@ +// #2347: dumpSse was dead on every non-injected lane — the tee sat INSIDE the +// useRewriter branch of src/server.ts, so plugin mode (compressInjected=false +// ⇒ useRewriter=false) and non-injected proxy turns produced zero -raw.sse +// files even with dumpSse configured. The fix hoists one tee above the branch +// split (after resolveFakeCompletion replaces responseBody). These tests pin: +// A plugin-mode streaming turn dumps the raw upstream bytes +// B non-injected proxy streaming turn dumps them too +// C injected proxy turn still dumps EXACTLY once (old tee removed cleanly) +// D dumpSse unset ⇒ no files (default-off unchanged) +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; +import { existsSync, mkdtempSync, readdirSync, readFileSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _resetPluginStateForTest } from "../src/plugin.ts"; +import { _resetSessionsForTest } from "../src/session.ts"; +import { _setForTest } from "../src/registry.ts"; +import { resetToolRingForTest } from "../src/tool-ring.ts"; + +process.env.NODE_ENV = "test"; +const root = mkdtempSync(join(tmpdir(), "bili-issue2347-")); +test.after(() => rmSync(root, { recursive: true, force: true })); +for (const key of ["XDG_STATE_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME", "XDG_CONFIG_HOME"]) process.env[key] = root; + +const SSE_BODY = + 'data: {"id":"chatcmpl_1","object":"chat.completion.chunk","created":1,"model":"gpt-test","choices":[{"index":0,"delta":{"role":"assistant","content":"hel"},"finish_reason":null}]}\n\n' + + 'data: {"id":"chatcmpl_1","object":"chat.completion.chunk","created":1,"model":"gpt-test","choices":[{"index":0,"delta":{"content":"lo"},"finish_reason":null}]}\n\n' + + 'data: {"id":"chatcmpl_1","object":"chat.completion.chunk","created":1,"model":"gpt-test","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}\n\n' + + "data: [DONE]\n\n"; + +interface Harness { origin: string; upstreamUrl: string; close: () => Promise; } + +async function harness(opts: { dumpDir?: string; injectTool: boolean }): Promise { + _resetSessionsForTest(); + _resetPluginStateForTest(); + resetToolRingForTest(); + const store = new SessionStore({ dir: join(root, "sessions"), enabled: false }); + _setStoreForTest(store); + _setForTest({}); + const upstream = http.createServer((req, res) => { + req.resume(); + req.on("end", () => { + res.writeHead(200, { "content-type": "text/event-stream" }); + res.write(SSE_BODY); + res.end(); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const addr = upstream.address(); + assert(addr && typeof addr === "object"); + const upstreamUrl = `http://127.0.0.1:${addr.port}`; + const proxy = await startServer({ port: 0, host: "127.0.0.1", upstream: upstreamUrl, routes: { [upstreamUrl]: { models: { "gpt-test": { context: 400000 } } } }, modelContextLimit: 400000, kernelConfig: defaultConfig(400000), compress: { injectTool: opts.injectTool, injectNudge: false }, promptCache: { routing: "auto" }, sessionHeader: "x-acp-session", log: false, debug: false, passthrough: false, autoUpdate: false, mitm: { enabled: false, domains: [] }, ...(opts.dumpDir ? { dumpSse: opts.dumpDir } : {}) } as ProxyOptions); + await once(proxy, "listening"); + const paddr = proxy.address(); + assert(paddr && typeof paddr === "object"); + const origin = `http://127.0.0.1:${paddr.port}`; + return { + origin, upstreamUrl, + close: async () => { + store.cancelAll(); + const closed = Promise.all([once(proxy, "close"), once(upstream, "close")]); + proxy.close(); upstream.close(); + await closed; + }, + }; +} + +async function sendTurn(h: Harness, plugin: boolean, conv: string): Promise { + const headers: Record = { "content-type": "application/json", "x-acp-session": conv }; + if (plugin) { + headers["x-bili-plugin"] = "test-agent"; + headers["x-bili-plugin-conversation"] = conv; + } + const r = await fetch(`${h.origin}/bili/${h.upstreamUrl}/chat/completions`, { method: "POST", headers, body: JSON.stringify({ model: "gpt-test", stream: true, max_tokens: 1024, messages: [{ role: "user", content: "say hello" }] }) }); + assert.equal(r.status, 200); + assert(r.body); + const wire = await r.text(); + assert(wire.includes('"finish_reason":"stop"'), "client saw the terminal frame"); +} + +async function waitForDump(dir: string, expected: string, conv: string): Promise { + const deadline = Date.now() + 8000; + for (;;) { + const files = existsSync(dir) ? readdirSync(dir).filter((f) => f.endsWith("-raw.sse")) : []; + assert.ok(files.length <= 1, `expected at most one raw dump, got ${files.join(", ")}`); + if (files.length === 1) { + const content = readFileSync(join(dir, files[0]), "utf8"); + if (content === expected) return files[0]; + } + if (Date.now() > deadline) throw new Error(`dump never matched the upstream bytes; dir=${JSON.stringify(existsSync(dir) ? readdirSync(dir) : null)}`); + await new Promise((r) => setTimeout(r, 50)); + } +} + +test("#2347 A: plugin-mode streaming turn dumps the raw upstream SSE (previously zero files)", { timeout: 20000 }, async () => { + const dir = join(root, "raw-a"); + const h = await harness({ dumpDir: dir, injectTool: true }); + try { + await sendTurn(h, true, "conv-a"); + const name = await waitForDump(dir, SSE_BODY, "conv-a"); + assert(name.includes("conv-a"), `filename carries the sanitized session id: ${name}`); + } finally { await h.close(); } +}); + +test("#2347 B: non-injected proxy streaming turn dumps the raw upstream SSE", { timeout: 20000 }, async () => { + const dir = join(root, "raw-b"); + const h = await harness({ dumpDir: dir, injectTool: false }); + try { + await sendTurn(h, false, "conv-b"); + const name = await waitForDump(dir, SSE_BODY, "conv-b"); + assert(name.includes("conv-b"), `filename carries the sanitized session id: ${name}`); + } finally { await h.close(); } +}); + +test("#2347 C: injected proxy turn still dumps exactly once (no double tee after removal)", { timeout: 20000 }, async () => { + const dir = join(root, "raw-c"); + const h = await harness({ dumpDir: dir, injectTool: true }); + try { + await sendTurn(h, false, "conv-c"); + const name = await waitForDump(dir, SSE_BODY, "conv-c"); + assert(name.includes("conv-c")); + } finally { await h.close(); } +}); + +test("#2347 D: dumpSse unset writes nothing (default-off unchanged)", { timeout: 20000 }, async () => { + const dir = join(root, "raw-d"); + const h = await harness({ injectTool: true }); + try { + await sendTurn(h, true, "conv-d"); + assert(!existsSync(dir), "no dump dir created when dumpSse is unset"); + } finally { await h.close(); } +}); From 286a857131c0eee161074d2e9dc773e61c6a0bd9 Mon Sep 17 00:00:00 2001 From: awork Date: Wed, 7 Oct 2026 23:11:30 +0800 Subject: [PATCH 2/3] =?UTF-8?q?fix:=20#2347=20review=20=E2=80=94=20await?= =?UTF-8?q?=20dumpRaw=20in=20the=20safety-net=20finally=20(non-rewriter=20?= =?UTF-8?q?throw=20paths)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The non-rewriter branch awaited dumpRaw inline, so a rejecting pipe would skip it and leave the raw-SSE dump half-written — while the plugin lane and the rewriter lane both await in their own finallys. Move the non-rewriter await into the shared safety-net finally (idempotent for the rewriter lane) so all three lanes honor the 'full upstream tail before turn close' contract on every exit path. Success-path behavior unchanged. --- src/server.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/server.ts b/src/server.ts index 22f2798a59..35a3ffe6c9 100644 --- a/src/server.ts +++ b/src/server.ts @@ -7253,9 +7253,8 @@ async function forward( } else { await pipeThrough(responseBody, res); } - // #2347: finish the dump before the turn closes — its value is the full - // upstream tail, which the client may have stopped reading early. - if (dumpRaw) await dumpRaw; + // #2347: the raw-SSE dump is finished in the safety-net finally below — + // an inline await here would be skipped when a pipe rejects. return; } const ctx: RewriteCtx = { @@ -7487,6 +7486,11 @@ async function forward( // this one must sit at the very end so clearing never happens while a // live stream still needs the external-abort listener. clearUpstreamTimer(); + // #2347: finish the raw-SSE dump before the turn closes — its value is + // the full upstream tail, which the client may have stopped reading + // early. Idempotent where the rewriter lane already awaited it in its + // own finally; this covers the non-rewriter branch's throw paths too. + if (dumpRaw) await dumpRaw; } } From 4f71fef8604bf8eafac5494fa76ba1d6c15c3516 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Thu, 8 Oct 2026 00:41:52 +0800 Subject: [PATCH 3/3] feat: termination diagnostics on the plugin-lane truncation signal (#2328 Q2/Q4) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The #721 in-band upstream_stream_truncated error frame said a stream was cut, but never WHY it could not be completed: the pipes know the event count, the last event types, the dangling EOF buffer, the retry budget state and the upstream HTTP identity — none of it reached the client or the log. #2347 forensics had to fall back to raw-SSE dumps to tell "upstream never sent a terminal" from "the terminal's bytes never completed". - stream-error.ts: emitUpstreamTruncation takes an optional TruncationDiag; it rides the error frame as `meta` (openai/anthropic/google nested in error.meta, responses top-level meta — google keeps its numeric-code object shape) and the log line as classification + cause + full JSON. - plugin.ts: both strip pipes (chat + responses) track events/bytes/ unparseable-forwards/last-5-event-types; at EOF and read-error they build the diag and classify the cut (#2328 Q4): - no-terminal-seen — EOF/read-error with no terminal bytes in flight; - terminal-bytes-unrecognized — the dangling partial event carries the opening bytes of the wire's own terminal ([DO(ne), event: message_stop, a quoted finish_reason/finishReason chunk, response. completed-family head). Factual byte evidence only. Responses adds responseStatus + held done-family count; both add the #2171/#732 retry-budget state and the upstream status/content-type (threaded as optional UpstreamMeta from server.ts, 5 call sites). - Wire-shape note: `meta` is a NEW field on error-only frames — flagged for human review per house rule; no non-error frame changes. Tests: tests/issue2328-terminal-diag.test.ts — 8 cases across openai/ anthropic/google/responses: both classifications, cause eof/read-error, counters, upstreamMeta threading, log-line shape; existing truncation suites (#2171/#1452/#732/#1061/#1039) unchanged and green. --- src/plugin.ts | 141 ++++++++++++++- src/server.ts | 22 +++ src/stream-error.ts | 63 ++++++- tests/issue2328-terminal-diag.test.ts | 245 ++++++++++++++++++++++++++ 4 files changed, 460 insertions(+), 11 deletions(-) create mode 100644 tests/issue2328-terminal-diag.test.ts diff --git a/src/plugin.ts b/src/plugin.ts index 00043ad931..76d47bf978 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -22,6 +22,37 @@ import { log as loggerLog } from "./logger.js"; import { ccrEnabled, ccrLoopConfig, contentStoreOf, retrieveToolName } from "./store.js"; import { imageUsageSuffix } from "./image-compress.js"; import { emitStreamError, emitUpstreamTruncation } from "./stream-error.js"; +import type { TruncationDiag } from "./stream-error.js"; + +/** #2328 Q2: what the pipes know about the upstream HTTP response whose body + * they are piping — threaded in by server.ts so the truncation diag can cite + * status/content-type without the pipes holding the Response object. */ +export interface UpstreamMeta { + status?: number; + contentType?: string; +} + +/** #2328 Q4: does the dangling partial event left at a cut carry the opening + * bytes of the wire's own terminal? Factual byte evidence only — this never + * guesses about frames that parsed but were classified otherwise. */ +function tailLooksTerminal(tail: string, protocol: WireProtocol | "responses"): boolean { + // [DONE] torn anywhere past `data: [DO` is still recognizable as the + // terminal byte arriving. + if (/^data: \[DO/.test(tail)) return true; + if (protocol === "anthropic") return /event: message_stop/.test(tail.slice(0, 64)) || /"type"\s*:\s*"message_stop"/.test(tail); + if (protocol === "openai") { + // `"finish_reason":null` rides EVERY chat chunk — only a quoted + // (non-null) value counts, which is the final chunk's signature. + return /"finish_reason"\s*:\s*"/.test(tail); + } + if (protocol === "google") { + // finishReason only ever appears on Gemini's terminal chunk. + return /"finishReason"\s*:\s*"/.test(tail); + } + // responses: the completion-family event is the terminal. + return /event: response\.(?:completed|failed|incomplete)/.test(tail.slice(0, 64)) || + /"type"\s*:\s*"response\.(?:completed|failed|incomplete)"/.test(tail); +} import { degenerateTurnWarning, endsWithDraftClose } from "./degenerate-turn.js"; import { PANEL_BOX_FOOTER } from "./acp-panel.js"; import { describeAdvisory, getAdvisoryState } from "./advisory.js"; @@ -1730,6 +1761,11 @@ export async function pipePluginChatWithStrip( // The shipped request text: an emission-shaped span the user asked to // output verbatim is echoed, not dropped (m00885). requestText?: string, + // #2328 Q2: upstream HTTP status/content-type of the response this stream + // came from, for the truncation diag. Optional so existing callers/tests + // stay source-compatible; the plugin lane's resolveFakeCompletion refetch + // swallows the original response object, hence optional. + upstreamMeta?: UpstreamMeta, ): Promise { let reader = stream.getReader(); let decoder = new TextDecoder("utf-8"); @@ -1826,6 +1862,16 @@ export async function pipePluginChatWithStrip( * the text that survives such a frame is the tag's own interior. */ let droppedTagInFrame = false; let finalFinishReason: string | undefined; + // #2328 Q2/Q4: termination diagnostics — every truncation emit below + // carries these on the in-band error frame (meta) and the log line. + let diagEvents = 0; + let diagBytes = 0; + let diagUnparseable = 0; + const diagLastTypes: string[] = []; + const diagPushType = (label: string) => { + diagLastTypes.push(label); + if (diagLastTypes.length > 5) diagLastTypes.shift(); + }; // #1501 option C: tool-call observations on this verbatim lane, keyed per // protocol (openai: choice:toolIndex, anthropic: block:N, google: // candidate/part). Bytes are forwarded untouched (#1039); the tracker only @@ -1968,6 +2014,27 @@ export async function pipePluginChatWithStrip( buf = ""; return true; }; + // #2328 Q2/Q4: the diag snapshot riding every truncation emit below. Built + // at emit time so the counters reflect the final (possibly retried) + // attempt; `buf` is whatever partial SSE event the cut left dangling. + const buildTruncationDiag = (cause: "eof" | "read-error"): TruncationDiag => ({ + cause, + classification: buf.length > 0 && tailLooksTerminal(buf, protocol) ? "terminal-bytes-unrecognized" : "no-terminal-seen", + protocol, + events: diagEvents, + unparseableForwarded: diagUnparseable, + bytes: diagBytes, + lastEventTypes: [...diagLastTypes], + eofBufferBytes: buf.length, + ...(buf.length > 0 ? { eofBufferHead: buf.slice(0, 80).replace(/[\r\n]+/g, "\\n") } : {}), + ...(finalFinishReason !== undefined ? { finishReason: finalFinishReason } : {}), + visibleChars: visibleTextChars, + sawStrippedEcho, + retryZeroByteCutSpent: truncationRetried, + retryDegenerateSpent: degenerateRetried, + ...(upstreamMeta?.status !== undefined ? { upstreamStatus: upstreamMeta.status } : {}), + ...(upstreamMeta?.contentType !== undefined ? { upstreamContentType: upstreamMeta.contentType } : {}), + }); /** 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. */ @@ -2486,6 +2553,7 @@ export async function pipePluginChatWithStrip( buf = ""; pendingFinal = resolved; } else { + if (value && value.length > 0) diagBytes += value.byteLength; pendingFinal = value && value.length > 0 ? decoder.decode(value, { stream: true }) : null; } if (pendingFinal !== null) { @@ -2499,6 +2567,8 @@ export async function pipePluginChatWithStrip( const jsonStr = dataLines.map((l) => l.slice(5).replace(/^ /, "")).join("\n").trim(); if (!jsonStr) continue; if (jsonStr === "[DONE]") { + diagEvents++; + diagPushType("[DONE]"); sawTerminal = true; await write(flushTails() + rawEvent + "\n\n"); continue; @@ -2507,11 +2577,28 @@ export async function pipePluginChatWithStrip( try { ev = JSON.parse(jsonStr) as Record; } catch { + diagEvents++; + diagUnparseable++; + diagPushType("unparseable"); // #2190: unparseable frames bypass every filter — audit them. auditRawForward(rawEvent); await write(rawEvent + "\n\n"); continue; } + diagEvents++; + // #2328: wire-native label where one exists; the chat wires + // without a `type` get a synthesized one so the last-events + // trail is readable in the diag. + if (protocol === "anthropic") diagPushType(typeof ev["type"] === "string" ? ev["type"] as string : "frame"); + else if (protocol === "openai") { + const c0 = Array.isArray(ev["choices"]) ? (ev["choices"] as unknown[])[0] : undefined; + const fr = c0 && typeof c0 === "object" ? (c0 as Record)["finish_reason"] : undefined; + diagPushType(typeof fr === "string" ? `chunk:finish=${fr}` : "chunk"); + } else if (protocol === "google") { + const c0 = Array.isArray(ev["candidates"]) ? (ev["candidates"] as unknown[])[0] : undefined; + const fr = c0 && typeof c0 === "object" ? (c0 as Record)["finishReason"] : undefined; + diagPushType(typeof fr === "string" ? `chunk:finish=${fr}` : "chunk"); + } else diagPushType("frame"); if (retryFraming(ev)) continue; if (protocol === "anthropic" && ev["type"] === "content_block_start") blocksForwarded++; if (ev["type"] === "message_stop") sawTerminal = true; @@ -2580,7 +2667,7 @@ export async function pipePluginChatWithStrip( maybeWarnNamelessToolCalls(); settleWitnesses(); if (truncated) { - emitUpstreamTruncation(res, protocol, finalFinishReason !== undefined, log); + emitUpstreamTruncation(res, protocol, finalFinishReason !== undefined, log, buildTruncationDiag("eof")); return; } } catch (e) { @@ -2604,7 +2691,7 @@ export async function pipePluginChatWithStrip( /* client half-gone; the emission below is best-effort too */ } loggerLog("warn", `[plugin] upstream stream read failed (${protocol}): ${String(e instanceof Error ? e.message : e)} — emitting in-band truncation signal`); - emitUpstreamTruncation(res, protocol, finalFinishReason !== undefined, log); + emitUpstreamTruncation(res, protocol, finalFinishReason !== undefined, log, buildTruncationDiag("read-error")); return; } finally { reader.releaseLock(); @@ -2725,11 +2812,22 @@ export async function pipePluginResponsesWithStrip( absorbInstructed?: boolean, // m00885: echoed (user-requested verbatim) emission spans survive. requestText?: string, + // #2328 Q2 — see pipePluginChatWithStrip. + upstreamMeta?: UpstreamMeta, ): Promise { let reader = stream.getReader(); let decoder = new TextDecoder("utf-8"); let buf = ""; const acc: UsageSample = {}; + // #2328 Q2/Q4 — see the chat-pipe twin. + let diagEvents = 0; + let diagBytes = 0; + let diagUnparseable = 0; + const diagLastTypes: string[] = []; + const diagPushType = (label: string) => { + diagLastTypes.push(label); + if (diagLastTypes.length > 5) diagLastTypes.shift(); + }; const onTagDrop = (snippet: string) => { 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`); @@ -2944,6 +3042,29 @@ export async function pipePluginResponsesWithStrip( if (typeof type === "string" && type.startsWith("response.reasoning_summary_")) return true; return type === "response.created" || type === "response.output_item.added" || type === "response.content_part.added"; }; + // #2328 Q2/Q4 — see the chat-pipe twin. Responses-specific extras: the + // last completion-family status observed (a failed/incomplete status on a + // "truncated" stream changes the diagnosis) and how many done-family + // frames were still held for the degenerate-turn retry when the cut hit. + const buildTruncationDiag = (cause: "eof" | "read-error"): TruncationDiag => ({ + cause, + classification: buf.length > 0 && tailLooksTerminal(buf, "responses") ? "terminal-bytes-unrecognized" : "no-terminal-seen", + protocol: "responses", + events: diagEvents, + unparseableForwarded: diagUnparseable, + bytes: diagBytes, + lastEventTypes: [...diagLastTypes], + eofBufferBytes: buf.length, + ...(buf.length > 0 ? { eofBufferHead: buf.slice(0, 80).replace(/[\r\n]+/g, "\\n") } : {}), + ...(responseStatus !== undefined ? { responseStatus } : {}), + visibleChars: visibleTextChars, + sawStrippedEcho, + retryZeroByteCutSpent: truncationRetried, + retryDegenerateSpent: degenerateRetried, + heldEvents: heldEvents.length, + ...(upstreamMeta?.status !== undefined ? { upstreamStatus: upstreamMeta.status } : {}), + ...(upstreamMeta?.contentType !== undefined ? { upstreamContentType: upstreamMeta.contentType } : {}), + }); /** 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 => { @@ -3072,6 +3193,7 @@ export async function pipePluginResponsesWithStrip( buf = ""; pendingFinal = resolved; } else { + if (value && value.length > 0) diagBytes += value.byteLength; pendingFinal = value && value.length > 0 ? decoder.decode(value, { stream: true }) : null; } if (pendingFinal !== null) { @@ -3084,7 +3206,11 @@ export async function pipePluginResponsesWithStrip( if (dataLines.length === 0) continue; const jsonStr = dataLines.map((l) => l.slice(5).replace(/^ /, "")).join("\n").trim(); if (!jsonStr || jsonStr === "[DONE]") { - if (jsonStr === "[DONE]") sawTerminal = true; + if (jsonStr === "[DONE]") { + diagEvents++; + diagPushType("[DONE]"); + sawTerminal = true; + } await write(rawEvent + "\n\n"); continue; } @@ -3092,11 +3218,16 @@ export async function pipePluginResponsesWithStrip( try { ev = JSON.parse(jsonStr) as Record; } catch { + diagEvents++; + diagUnparseable++; + diagPushType("unparseable"); // #2190: unparseable frames bypass every filter — audit them. auditRawForward(rawEvent); await write(rawEvent + "\n\n"); continue; } + diagEvents++; + if (typeof ev["type"] === "string") diagPushType(ev["type"] as string); const sample = usageFromSseEvent(ev); if (sample) mergeUsageSample(acc, sample); const type = ev["type"]; @@ -3305,7 +3436,7 @@ export async function pipePluginResponsesWithStrip( // done-family event. Responses has no separate finish-reason concept // (terminal events carry the status), so this is always the error shape. if (!sawTerminal && !res.destroyed && !res.writableEnded) { - emitUpstreamTruncation(res, "responses", false, log); + emitUpstreamTruncation(res, "responses", false, log, buildTruncationDiag("eof")); return; } } catch (e) { @@ -3326,7 +3457,7 @@ export async function pipePluginResponsesWithStrip( /* client half-gone; the emission below is best-effort too */ } loggerLog("warn", `[plugin] upstream stream read failed (responses): ${String(e instanceof Error ? e.message : e)} — emitting in-band truncation signal`); - emitUpstreamTruncation(res, "responses", false, log); + emitUpstreamTruncation(res, "responses", false, log, buildTruncationDiag("read-error")); return; } finally { reader.releaseLock(); diff --git a/src/server.ts b/src/server.ts index 35a3ffe6c9..b91062a77f 100644 --- a/src/server.ts +++ b/src/server.ts @@ -118,6 +118,7 @@ import { personaNamespace } from "./persona-anchor.js"; import { prefixAffinity, type AnonymousAffinity } from "./prefix-affinity.js"; import { maybeAdoptForkBlocks, maybeAdoptResume } from "./fork-adoption.js"; import { publicForkInputMatches } from "./plugin.js"; +import type { UpstreamMeta } from "./plugin.js"; import { flushPrefixAffinity, hydratePrefixAffinity, scheduleAffinityPersist } from "./affinity-persist.js"; import { setSimhashAdoptionEnabled } from "./prefix-affinity.js"; import { consumePluginRegisterFor, flushConversations, handlePluginCompact, handlePluginManifest, handlePluginFork, handlePluginSnapshot, handlePluginRegister, handlePluginRuntimeInfo, handlePluginStatus, handlePluginTool, isPluginFoldCallId, loadConversations, pipePluginChatWithStrip, pipePluginJson, pipePluginResponsesWithStrip, pluginAgentHeader, pluginConversationHeader, pluginHeadersMatchModel, pluginReportedContextWindow, pluginReportedMaxOutput, pluginRequestAgentHeader, pluginRuntimeInfoFor, pluginRuntimeInfoForConversation, recordChainVerdict, recordPluginSession, rememberPluginMessages, resolveConversation, runtimeConversationId, takePendingPluginRegister } from "./plugin.js"; @@ -7014,6 +7015,11 @@ async function forward( dumpRaw = dumpStreamToFile(b, opts.dumpSse, `${Date.now()}-${safeSessionId(prepared.session.id)}-raw.sse`); } if (prepared.stream) { + // #2328: what the pipes know about the upstream HTTP response — + // first attempt's status/content-type (a resolveFakeCompletion + // re-send is not reflected; the pipes' own diag says which + // retry budget was spent instead). + const upstreamMeta: UpstreamMeta = { status: upstream.status, contentType: upstream.headers.get("content-type") ?? undefined }; if (prepared.protocol === "responses") { // #732/#821 applies to this pipe too (#871): the agent's own // body, held here with its URL and headers, is re-issued once @@ -7037,6 +7043,7 @@ async function forward( targetOrigin, absorbInstructed, wireBodyText, + upstreamMeta, ); } else { // #732/#821: the plugin pipe re-issues the agent's own body @@ -7063,6 +7070,7 @@ async function forward( targetOrigin, absorbInstructed, wireBodyText, + upstreamMeta, ); } } else { @@ -7153,6 +7161,8 @@ async function forward( prepared.compressInjected && prepared.processedMessages.length > 0; if (!useRewriter || prepared === null) { + // #2328: pipes cite the first upstream response's HTTP identity. + const upstreamMeta: UpstreamMeta = { status: upstream.status, contentType: upstream.headers.get("content-type") ?? undefined }; if (prepared && prepared.resetAfterSuccess) { const [toClient, toObserve] = responseBody.tee(); const observed = observeResponsesTerminalState(toObserve, prepared.stream); @@ -7179,6 +7189,10 @@ async function forward( log, label: prepared.session.id, }), + undefined, + undefined, + undefined, + upstreamMeta, ); } else { await pipeThrough(toClient, res); @@ -7219,6 +7233,10 @@ async function forward( log, label: p.session.id, }), + undefined, + undefined, + undefined, + upstreamMeta, ); } else { await pipePluginChatWithStrip( @@ -7238,6 +7256,10 @@ async function forward( log, label: p.session.id, }), + undefined, + undefined, + undefined, + upstreamMeta, ); } } else if ( diff --git a/src/stream-error.ts b/src/stream-error.ts index 87f1a0bbb7..1136c804d6 100644 --- a/src/stream-error.ts +++ b/src/stream-error.ts @@ -131,32 +131,83 @@ export function emitStreamError(res: http.ServerResponse, protocol: Protocol, me * only reachable case from a pipe that treats that chunk as terminal). * Never throws. */ -export function emitUpstreamTruncation(res: http.ServerResponse, protocol: Protocol, finished: boolean, log?: (msg: string) => void): void { +/** #2328 Q2: termination diagnostics carried on the in-band truncation + * error frame (`meta`) and in the log line, so "the upstream never sent a + * terminal event" (#721's no-terminal-seen) and "a terminal was arriving but + * its bytes never completed / were unrecognizable" can be told apart from + * the client side (#2347 forensics). Every field is plain-JSON safe. */ +export interface TruncationDiag { + /** Which pipe path fired: clean EOF without a terminal, or a read error. */ + cause: "eof" | "read-error"; + /** Q4 classification — see the helpers in plugin.ts that build this. */ + classification: "no-terminal-seen" | "terminal-bytes-unrecognized"; + /** Wire protocol of the truncated stream. */ + protocol: string; + /** Parsed SSE events (incl. [DONE] frames; excl. unparseable raw forwards). */ + events: number; + /** Frames forwarded verbatim because JSON.parse failed (#2190 audit twin). */ + unparseableForwarded: number; + /** Sum of decoded upstream bytes across the whole stream. */ + bytes: number; + /** Last ≤5 parsed event labels, oldest first (wire-native `type`, or a + * synthesized `chunk`/`chunk:finish=` for wires without one). */ + lastEventTypes: string[]; + /** Dangling partial event left in the normalize buffer at EOF/cut. */ + eofBufferBytes: number; + /** First ≤80 chars of that dangling buffer, newlines flattened. */ + eofBufferHead?: string; + /** Finish reason already delivered before the cut (chat wires), if any. */ + finishReason?: string; + /** Responses wire: status of the last completion-family frame, if any. */ + responseStatus?: string; + /** Post-filter visible chars the client actually assembled. */ + visibleChars: number; + /** The tag filter stripped echo markup somewhere in this stream. */ + sawStrippedEcho: boolean; + /** The one-shot #2171 re-issue was already spent earlier in this request. */ + retryZeroByteCutSpent: boolean; + /** The one-shot #732/#821 degenerate-turn re-issue was already spent. */ + retryDegenerateSpent: boolean; + /** Held done-family frames still buffered at the cut (responses wire). */ + heldEvents?: number; + /** Upstream HTTP status / content-type of the original response, when the + * caller still holds them (plugin lane re-fetches lose them by design). */ + upstreamStatus?: number; + upstreamContentType?: string; +} + +export function emitUpstreamTruncation(res: http.ServerResponse, protocol: Protocol, finished: boolean, log?: (msg: string) => void, diag?: TruncationDiag): void { const message = "upstream stream ended before a completion event; this turn may be incomplete"; const action = finished ? protocol === "google" ? "finish reason already delivered, stream complete" : "finish reason seen, synthesizing missing terminal byte" : "emitting in-band error"; - log?.(`[acp-proxy: upstream stream truncated (${protocol}) — ${action}]`); + // #2328: the diag blob rides the log line too — one grep for + // `upstream stream truncated` pulls the full classification + counters + // without needing the client-side error frame. + log?.(`[acp-proxy: upstream stream truncated (${protocol}) — ${action}${diag ? `; classification=${diag.classification} cause=${diag.cause}; diag=${JSON.stringify(diag)}` : ""}]`); + // #2328: attach the diagnostics as `meta` on the in-band error object. + // Wire-shape change (new field on an error-only frame) — human-merge item. + const metaField = diag === undefined ? {} : { meta: diag }; try { if (protocol === "openai") { if (finished) { safeWrite(res, "data: [DONE]\n\n"); } else { - safeWrite(res, `data: ${JSON.stringify({ error: { type: "server_error", code: "upstream_stream_truncated", message } })}\n\ndata: [DONE]\n\n`); + safeWrite(res, `data: ${JSON.stringify({ error: { type: "server_error", code: "upstream_stream_truncated", message, ...metaField } })}\n\ndata: [DONE]\n\n`); } } else if (protocol === "responses") { - safeWrite(res, `event: error\ndata: ${JSON.stringify({ type: "error", code: "upstream_stream_truncated", message })}\n\n`); + safeWrite(res, `event: error\ndata: ${JSON.stringify({ type: "error", code: "upstream_stream_truncated", message, ...metaField })}\n\n`); } else if (protocol === "google") { // The finishReason chunk IS Gemini's stream terminator: when it was // delivered, the client is already done and an extra terminal // object would read as a second answer. Only the mid-flight cut // needs the error frame (503/UNAVAILABLE — an upstream-cut stream). - if (!finished) safeWrite(res, `data: ${JSON.stringify({ error: { code: 503, message, status: "UNAVAILABLE" } })}\n\n`); + if (!finished) safeWrite(res, `data: ${JSON.stringify({ error: { code: 503, message, status: "UNAVAILABLE", ...metaField } })}\n\n`); } else { if (finished) { safeWrite(res, `event: message_stop\ndata: ${JSON.stringify({ type: "message_stop" })}\n\n`); } else { - safeWrite(res, `event: error\ndata: ${JSON.stringify({ type: "error", error: { type: "server_error", code: "upstream_stream_truncated", message } })}\n\n`); + safeWrite(res, `event: error\ndata: ${JSON.stringify({ type: "error", error: { type: "server_error", code: "upstream_stream_truncated", message, ...metaField } })}\n\n`); } } } catch { diff --git a/tests/issue2328-terminal-diag.test.ts b/tests/issue2328-terminal-diag.test.ts new file mode 100644 index 0000000000..458caec0f6 --- /dev/null +++ b/tests/issue2328-terminal-diag.test.ts @@ -0,0 +1,245 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { pipePluginChatWithStrip, pipePluginResponsesWithStrip } from "../src/plugin.ts"; + +// #2328 Q2/Q4: the plugin-lane strip pipes now attach termination +// diagnostics (meta) to the in-band upstream_stream_truncated error frame and +// to the log line, and classify the cut: +// - no-terminal-seen: EOF/read-error with no terminal bytes in flight. +// - terminal-bytes-unrecognized: the dangling partial event at the cut +// carries the opening bytes of the wire's own terminal ([DONE], +// message_stop, finishReason chunk, response.completed family). +// This is what makes "the upstream never finished the turn" distinguishable +// from "the terminal arrived but its bytes never completed" (#2347 forensics) +// without needing a raw-SSE dump. + +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[], then?: "error"): 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 if (then === "error") { + controller.error(new Error("upstream socket reset mid-stream")); + } else { + controller.close(); + } + }, + }); +} + +/** Pull the first data-frame JSON that carries the truncation code. */ +function truncationMeta(writes: string[], at: "top" | "nested"): Record | undefined { + for (const w of writes) { + for (const frame of w.split("\n\n")) { + const m = frame.match(/^(?:event: [^\n]+\n)?data: (.*)$/s); + if (!m) continue; + try { + const ev = JSON.parse(m[1]!) as Record; + if (at === "top" && ev["code"] === "upstream_stream_truncated") return ev["meta"] as Record; + if (at === "nested") { + const err = ev["error"] as Record | undefined; + // google's error object carries a numeric code (503) + + // gRPC status instead of the string code. + if (err && (err["code"] === "upstream_stream_truncated" || err["status"] === "UNAVAILABLE")) return err["meta"] as Record; + } + } catch { + /* not a JSON frame */ + } + } + } + return undefined; +} + +const openaiChunk = (fr: string | null) => `data: ${JSON.stringify({ object: "chat.completion.chunk", choices: [{ index: 0, delta: fr === null ? { content: "hi" } : {}, finish_reason: fr }] })}\n\n`; + +test("chat pipe (openai): dangling `data: [DO` at EOF classifies as terminal-bytes-unrecognized", async () => { + const out: string[] = []; + const logs: string[] = []; + await pipePluginChatWithStrip( + streamOf([openaiChunk(null), "data: [DO"]), + makeRes(out), + "openai", + undefined, + (m) => logs.push(m), + ); + const meta = truncationMeta(out, "nested"); + assert.ok(meta, "error frame carries meta"); + assert.equal(meta!["classification"], "terminal-bytes-unrecognized"); + assert.equal(meta!["cause"], "eof"); + assert.equal(meta!["protocol"], "openai"); + assert.equal(meta!["eofBufferBytes"], "data: [DO".length); + assert.equal(meta!["eofBufferHead"], "data: [DO"); + assert.deepEqual(meta!["lastEventTypes"], ["chunk"]); + assert.equal(meta!["events"], 1); + assert.ok((meta!["bytes"] as number) > 0); + assert.equal(meta!["retryZeroByteCutSpent"], false); + assert.equal(meta!["retryDegenerateSpent"], false); + // wire shape: openai meta nests inside `error`, and the [DONE] tail still + // follows the error frame (#721 contract unchanged). + assert.ok(out.join("").includes("data: [DONE]")); + // log line: one grep-able line with the classification + full diag. + const line = logs.find((l) => l.includes("upstream stream truncated")); + assert.ok(line, "log line emitted"); + assert.ok(line!.includes("classification=terminal-bytes-unrecognized")); + assert.ok(line!.includes("cause=eof")); + assert.ok(line!.includes("diag=")); +}); + +test("chat pipe (anthropic): clean EOF with no terminal classifies as no-terminal-seen", async () => { + const out: string[] = []; + await pipePluginChatWithStrip( + streamOf([ + `event: message_start\ndata: ${JSON.stringify({ type: "message_start" })}\n\n`, + `event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "hi" } })}\n\n`, + ]), + makeRes(out), + "anthropic", + ); + const meta = truncationMeta(out, "nested"); + assert.ok(meta, "error frame carries meta"); + assert.equal(meta!["classification"], "no-terminal-seen"); + assert.equal(meta!["cause"], "eof"); + assert.equal(meta!["eofBufferBytes"], 0); + assert.equal(meta!["eofBufferHead"], undefined); + assert.deepEqual(meta!["lastEventTypes"], ["message_start", "content_block_delta"]); + assert.ok((meta!["visibleChars"] as number) >= 2); + assert.ok(out.join("").includes("event: error")); +}); + +test("chat pipe (google): meta rides the UNAVAILABLE error object and counts unparseable forwards", async () => { + const out: string[] = []; + await pipePluginChatWithStrip( + streamOf([ + // one parseable chunk, then one unparseable frame (forwarded raw), + // then EOF with nothing terminal in flight. + `data: ${JSON.stringify({ candidates: [{ index: 0, content: { role: "model", parts: [{ text: "hi" }] } }] })}\n\n`, + "data: {{{not-json\n\n", + ]), + makeRes(out), + "google", + ); + const meta = truncationMeta(out, "nested"); + assert.ok(meta, "error frame carries meta"); + assert.equal(meta!["classification"], "no-terminal-seen"); + assert.equal(meta!["unparseableForwarded"], 1); + assert.deepEqual(meta!["lastEventTypes"], ["chunk", "unparseable"]); + const frame = out.join(""); + // Gemini's error object carries a numeric code + gRPC status, no string + // code (see emitUpstreamTruncation) — UNAVAILABLE identifies the frame. + assert.ok(frame.includes('"status":"UNAVAILABLE"')); + assert.ok(frame.includes('"code":503')); +}); + +test("chat pipe: upstreamMeta threads HTTP status/content-type into the diag", async () => { + const out: string[] = []; + await pipePluginChatWithStrip( + streamOf([openaiChunk(null)]), + makeRes(out), + "openai", + undefined, + undefined, + undefined, + undefined, + undefined, + undefined, + { status: 200, contentType: "text/event-stream" }, + ); + const meta = truncationMeta(out, "nested"); + assert.ok(meta); + assert.equal(meta!["upstreamStatus"], 200); + assert.equal(meta!["upstreamContentType"], "text/event-stream"); +}); + +test("responses pipe: dangling response.completed head classifies as terminal-bytes-unrecognized", async () => { + const out: string[] = []; + const sse = (e: string, d: Record) => `event: ${e}\ndata: ${JSON.stringify({ type: e, ...d })}\n\n`; + await pipePluginResponsesWithStrip( + streamOf([ + sse("response.created", { response: { id: "resp_1" } }), + sse("response.output_item.added", { output_index: 0, item: { type: "message", id: "msg_1" } }), + sse("response.content_part.added", { item_id: "msg_1", output_index: 0 }), + sse("response.output_text.delta", { item_id: "msg_1", output_index: 0, delta: "he" }), + 'event: response.completed\ndata: {"type":"response.comp', + ]), + makeRes(out), + ); + const meta = truncationMeta(out, "top"); + assert.ok(meta, "error frame carries meta"); + assert.equal(meta!["classification"], "terminal-bytes-unrecognized"); + assert.equal(meta!["cause"], "eof"); + assert.equal(meta!["protocol"], "responses"); + assert.ok((meta!["eofBufferHead"] as string).includes("response.comp")); + assert.deepEqual(meta!["lastEventTypes"], [ + "response.created", + "response.output_item.added", + "response.content_part.added", + "response.output_text.delta", + ]); + assert.equal(meta!["heldEvents"], 0); + assert.ok((meta!["visibleChars"] as number) >= 2); +}); + +test("responses pipe: clean EOF after deltas classifies as no-terminal-seen", async () => { + const out: string[] = []; + const sse = (e: string, d: Record) => `event: ${e}\ndata: ${JSON.stringify({ type: e, ...d })}\n\n`; + await pipePluginResponsesWithStrip( + streamOf([ + sse("response.created", { response: { id: "resp_1" } }), + sse("response.output_item.added", { output_index: 0, item: { type: "message", id: "msg_1" } }), + sse("response.content_part.added", { item_id: "msg_1", output_index: 0 }), + sse("response.output_text.delta", { item_id: "msg_1", output_index: 0, delta: "hi" }), + ]), + makeRes(out), + ); + const meta = truncationMeta(out, "top"); + assert.ok(meta); + assert.equal(meta!["classification"], "no-terminal-seen"); + assert.equal(meta!["eofBufferBytes"], 0); +}); + +test("responses pipe: upstream read error records cause=read-error", async () => { + const out: string[] = []; + const sse = (e: string, d: Record) => `event: ${e}\ndata: ${JSON.stringify({ type: e, ...d })}\n\n`; + await pipePluginResponsesWithStrip( + streamOf([sse("response.output_text.delta", { item_id: "msg_1", output_index: 0, delta: "x" })], "error"), + makeRes(out), + ); + const meta = truncationMeta(out, "top"); + assert.ok(meta); + assert.equal(meta!["cause"], "read-error"); + assert.equal(meta!["classification"], "no-terminal-seen"); +}); + +test("chat pipe (anthropic): dangling message_stop head classifies as terminal-bytes-unrecognized", async () => { + const out: string[] = []; + await pipePluginChatWithStrip( + streamOf([ + `event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "hi" } })}\n\n`, + "event: message_stop\ndata: {\"type\":\"mess", + ]), + makeRes(out), + "anthropic", + ); + const meta = truncationMeta(out, "nested"); + assert.ok(meta); + assert.equal(meta!["classification"], "terminal-bytes-unrecognized"); +});