From 880d1ccc787392bdff63a55c3f603711e0ea66c6 Mon Sep 17 00:00:00 2001 From: awork Date: Thu, 15 Oct 2026 21:43:51 +0800 Subject: [PATCH] fix: add single-codepoint repetition lane to the output-side runaway guard (#3260) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Port pi's findDegenerateRuns minRun semantics (200) onto the existing intrinsic stream terminator: a model stuck in a symbol loop (#3946: one symbol string for ~22 minutes) now trips the stream mid-generation instead of running to max-output or manual abort. Detection runs on DECODED model text only — per-frame JSON walk over text-family keys (openai delta.content/reasoning_content, anthropic text_delta/thinking_delta, responses top-level delta, google parts[].text). Tool-call arguments are excluded (#1039 user intent), and Responses cumulative snapshot events (.done/.completed/ .incomplete) are skipped so runs are never double-counted. Every frame is parsed: token-granular streaming interleaves framing bytes between identical characters, so a raw run-length pre-gate would stay blind to the incident shape. NDJSON bodies (bare-newline events) are handled by a fallback splitter; byte-exact forwarding and the clean-EOF trip contract are unchanged. Always on, zero new config. Also neutralizes the trip log wording in relay.ts (two sites): it said 'runaway enumeration detected' even when the reason is now text-run. --- src/runaway-guard.ts | 245 +++++++++++++++++++++-- src/server/relay.ts | 4 +- tests/issue3260-text-run-guard.test.ts | 259 +++++++++++++++++++++++++ 3 files changed, 488 insertions(+), 20 deletions(-) create mode 100644 tests/issue3260-text-run-guard.test.ts diff --git a/src/runaway-guard.ts b/src/runaway-guard.ts index c53ee33d80..0b08e349c3 100644 --- a/src/runaway-guard.ts +++ b/src/runaway-guard.ts @@ -11,16 +11,31 @@ import { ACP_NAME_ALT } from "./loop/tag-echo-filter.js"; // (#1039): counts RAW bytes only, never rewrites/drops/reorders a forwarded byte, sends // no corrective re-request nudge; on trip the caller aborts the upstream, which also // sets the loop signal and so suppresses the one-shot retries (all gated on !aborted). +// +// Third lane (#3260): single-codepoint repetition in the decoded MODEL TEXT — port of +// pi's findDegenerateRuns minRun semantics (production-tuned in billion-context-pi): +// a model stuck in a symbol loop (#3946: one symbol string for ~22 minutes) runs ONE +// codepoint >= 200 times. Detection runs on DECODED text payloads only (per-frame JSON, +// text-family keys), never on raw wire bytes: a low-entropy base64 blob or a +// tool-argument payload of 300 '=' must not read as a text run (#1039). Scale, never +// shape: legit prose/code tops out far below 200 consecutive identical codepoints. +// Every frame is parsed — there is deliberately NO raw pre-gate: token-granular +// streaming interleaves framing bytes between identical characters, so a raw +// run-length arm would stay blind to the exact incident shape (#3946). A +// structure-only frame costs a parse plus a short walk; nothing it carries enters +// the run tracker. export interface RunawayVerdict { tripped: boolean; - reason?: "acp-enumeration" | "tool-xml-flood"; + reason?: "acp-enumeration" | "tool-xml-flood" | "text-run"; detail?: { acpTags: number; toolXmlFragments: number; toolXmlGapBytes: number; maxMonotonicRefRun: number; zeroTokenFraction: number; + maxTextRun: number; + textRunChar?: string; }; } @@ -32,6 +47,9 @@ interface RunawayGuard { * bias the corroborator toward false trips on legit turns whose prose and * fragments alternate across chunks (#2451 review). */ feedGapOnly(chunkText: string): void; + /** Feed decoded model TEXT (assistant text/thinking deltas ONLY — never tool-call + * arguments): stateful single-codepoint run tracking across calls (#3260). */ + feedText(text: string): RunawayVerdict; } // Scale thresholds sit far above any legitimate single-message maximum. @@ -47,6 +65,18 @@ export const TOOL_XML_THRESHOLD = 300; // legit multi-tool turns << this; runawa // empty or single-token); real content spaces fragments with the payload. // Trip requires count ≥ threshold AND average inter-fragment gap ≤ this floor. const TOOL_XML_GAP_AVG_BYTES = 64; +// #3260: single-codepoint repetition lane. Parity with pi's findDegenerateRuns minRun +// (200) — the value billion-context-pi runs in production; legit text never carries +// 200 identical codepoints in a row, a symbol-loop model does (#3946). Internal +// constant on purpose: no config surface for a detector threshold (#2030 discipline). +export const TEXT_RUN_THRESHOLD = 200; +// Size at which the NDJSON fallback engages: a real SSE body separates every frame +// with a blank line long before this, so a buffer this large holding no blank line +// is not SSE-shaped. +const NDJSON_FALLBACK_MIN = 64 * 1024; +// Absolute cap on an unprocessed stream tail: beyond it the body is neither SSE nor +// NDJSON-shaped; drop rather than accumulate forever. +const FRAME_BUF_CAP = 8 * 1024 * 1024; // Bounded carry-over so a marker split across two chunks is still seen whole. Longer // than any single runaway marker (short tags with short attrs); a pathological long @@ -63,6 +93,30 @@ const TOOL_XML_RE = new RegExp("\\x3c\\/?(?:antml:)?(invoke|tool_calls|tool_call const REF_RE = /m(\d{4,})/; const TOKENS_ATTR_RE = /tokens\s*=\s*"(\d+)"/i; +// --- #3260 text-run lane: decoded-frame parsing (see header) --- +// SSE event separator (blank line), CRLF-tolerant. +const FRAME_SEP_RE = /\r?\n\r?\n/g; +const HAS_FRAME_SEP_RE = /\r?\n\r?\n/; +// Responses-wire cumulative SNAPSHOT events: .done / .completed / .incomplete re-send +// the FULL payload of a finished part. Feeding them on top of the deltas would +// double-count the run (a 150-run delivered as deltas, then re-sent in .completed, +// would read as 300). Deltas are the live generation; snapshots are not. +const SNAPSHOT_TYPE_RE = /\.(?:completed|incomplete|done)$/; +// Text-family LEAF keys: model-visible output text on every wire bili speaks (openai +// chat delta.content / delta.reasoning_content, anthropic delta.text_delta.text / +// thinking_delta.thinking, responses top-level "delta", google parts[].text). +// Everything else (ids, urls, roles, usage…) is structure, never output. +const TEXT_LEAF_KEYS: ReadonlySet = new Set([ + "content", "text", "thinking", "reasoning", "reasoning_content", "delta", +]); +// Branches whose strings are NEVER model output. Tool-call arguments are user intent +// (#1039): a write-tool payload of 300 '=' must not abort the turn. +const EXCLUDED_BRANCHES: ReadonlySet = new Set([ + "arguments", "input", "partial_json", "tool_calls", "tools", "functions", "system", + "instructions", "metadata", "usage", "usageMetadata", "logprobs", "annotations", + "error", "id", "url", +]); + export function createRunawayGuard(): RunawayGuard { let pending = ""; let acpTags = 0; @@ -80,14 +134,24 @@ export function createRunawayGuard(): RunawayGuard { let monoRun = 0; let maxMonoRun = 0; let tripped = false; + // #3260 text-run lane state. + let lastCp: string | null = null; + let textRun = 0; + let maxTextRun = 0; + let textRunChar: string | undefined; - const detail = (): NonNullable => ({ - acpTags, - toolXmlFragments: toolXml, - toolXmlGapBytes: toolGapBytes, - maxMonotonicRefRun: maxMonoRun, - zeroTokenFraction: acpTags > 0 ? zeroTokens / acpTags : 0, - }); + const detail = (): NonNullable => { + const d: NonNullable = { + acpTags, + toolXmlFragments: toolXml, + toolXmlGapBytes: toolGapBytes, + maxMonotonicRefRun: maxMonoRun, + zeroTokenFraction: acpTags > 0 ? zeroTokens / acpTags : 0, + maxTextRun, + }; + if (textRunChar !== undefined) d.textRunChar = textRunChar; + return d; + }; const feed = (chunkText: string): RunawayVerdict => { if (tripped) return { tripped: true, reason: undefined, detail: detail() }; @@ -145,11 +209,35 @@ export function createRunawayGuard(): RunawayGuard { return { tripped: false, detail: detail() }; }; + // #3260: stateful single-codepoint run over decoded model text. `for..of` iterates by + // CODE POINT, so an astral character (emoji) counts as one unit, not two surrogates. + const feedText = (text: string): RunawayVerdict => { + if (tripped) return { tripped: true, reason: undefined, detail: detail() }; + if (text.length === 0) return { tripped: false }; + for (const cp of text) { + if (cp === lastCp) { + textRun += 1; + if (textRun > maxTextRun) { + maxTextRun = textRun; + textRunChar = cp; + } + } else { + lastCp = cp; + textRun = 1; + } + if (textRun >= TEXT_RUN_THRESHOLD) { + tripped = true; + return { tripped: true, reason: "text-run", detail: detail() }; + } + } + return { tripped: false, detail: detail() }; + }; + const feedGapOnly = (chunkText: string): void => { toolGapBytes += chunkText.length; }; - return { feed, feedGapOnly }; + return { feed, feedGapOnly, feedText }; } /** Tee a raw response stream through a runaway guard. Bytes are forwarded BYTE-EXACT; @@ -163,6 +251,119 @@ export function wrapStreamWithRunawayGuard( const reader = source.getReader(); const decoder = new TextDecoder(); let settled = false; + + // #3260 text-run lane state. + let frameBuf = ""; // unprocessed tail: complete frames + at most one partial frame + let walkTripped = false; + let tripVerdict: RunawayVerdict | null = null; + + // Recursive walker over a parsed frame: feeds ONLY text-family leaves that sit + // outside excluded branches into the guard's run tracker. + const collectText = (node: unknown, key: string, excluded: boolean): void => { + if (walkTripped) return; + if (typeof node === "string") { + if (!excluded && TEXT_LEAF_KEYS.has(key)) { + const v = guard.feedText(node); + if (v.tripped) { + walkTripped = true; + tripVerdict = v; + } + } + return; + } + if (Array.isArray(node)) { + for (const item of node) { + collectText(item, key, excluded); + if (walkTripped) return; + } + return; + } + if (typeof node === "object" && node !== null) { + const ex = excluded || EXCLUDED_BRANCHES.has(key); + for (const [k, v] of Object.entries(node as Record)) { + collectText(v, k, ex); + if (walkTripped) return; + } + } + }; + + const processParsed = (obj: unknown): void => { + if (typeof obj !== "object" || obj === null) return; + const t = (obj as Record)["type"]; + if (typeof t === "string") { + if (SNAPSHOT_TYPE_RE.test(t)) return; // cumulative snapshot: deltas already counted it + if (t.includes("function_call") || t.includes("custom_tool_call")) return; // argument bytes (#1039) + } + walkTripped = false; + tripVerdict = null; + collectText(obj, "", false); + }; + + const processSseFrame = (frame: string): void => { + let data: string | null = null; + for (const lineRaw of frame.split("\n")) { + const line = lineRaw.endsWith("\r") ? lineRaw.slice(0, -1) : lineRaw; + if (!line.startsWith("data:")) continue; + const v = line.charAt(5) === " " ? line.slice(6) : line.slice(5); + data = data === null ? v : `${data}\n${v}`; + } + if (data === null || data === "[DONE]") return; + let obj: unknown; + try { + obj = JSON.parse(data); + } catch { + return; // comment/event/id-only frame or non-JSON payload — nothing to count + } + processParsed(obj); + }; + + const pumpFrames = (): void => { + // NDJSON fallback: some OpenAI-compatible servers separate events with bare + // newlines and never emit a blank line. Engages only once the buffer is large + // enough that standard SSE framing is clearly absent (a real SSE body separates + // every frame long before NDJSON_FALLBACK_MIN). + if (frameBuf.length >= NDJSON_FALLBACK_MIN && frameBuf.includes("\n") && !HAS_FRAME_SEP_RE.test(frameBuf)) { + const lines = frameBuf.split("\n"); + frameBuf = ""; + for (const lineRaw of lines) { + const line = lineRaw.endsWith("\r") ? lineRaw.slice(0, -1) : lineRaw; + if (line.length === 0 || (line.charAt(0) !== "{" && line.charAt(0) !== "[")) continue; + let obj: unknown; + try { + obj = JSON.parse(line); + } catch { + continue; + } + processParsed(obj); + if (walkTripped) return; + } + return; + } + if (frameBuf.length > FRAME_BUF_CAP) { + frameBuf = ""; + return; + } + FRAME_SEP_RE.lastIndex = 0; + let m: RegExpExecArray | null; + while ((m = FRAME_SEP_RE.exec(frameBuf)) !== null) { + const frame = frameBuf.slice(0, m.index); + frameBuf = frameBuf.slice(m.index + m[0].length); + processSseFrame(frame); + if (walkTripped) return; + if (frameBuf.length > FRAME_BUF_CAP) { + frameBuf = ""; + return; + } + } + }; + + const trip = (controller: ReadableStreamDefaultController): void => { + settled = true; + onTrip(tripVerdict ?? { tripped: true }); + controller.close(); + reader.cancel().catch(() => {}); + }; + return new ReadableStream({ async pull(controller) { if (settled) return; @@ -179,17 +380,25 @@ export function wrapStreamWithRunawayGuard( // skipping them would under-count gaps on prose/fragment-alternating turns // and bias the corroborator toward false trips). const text = decoder.decode(value, { stream: true }); - if (text.includes("\x3c")) { - const v = guard.feed(text); - if (v.tripped) { - settled = true; - onTrip(v); - controller.close(); - reader.cancel().catch(() => {}); + if (text.length > 0) { + if (text.includes("\x3c")) { + const v = guard.feed(text); + if (v.tripped) { + tripVerdict = v; + trip(controller); + return; + } + } else { + guard.feedGapOnly(text); + } + // #3260 text-run lane: assemble frames and feed decoded text leaves to + // the run tracker (see header for why every frame is parsed). + frameBuf += text; + pumpFrames(); + if (walkTripped) { + trip(controller); return; } - } else { - guard.feedGapOnly(text); } controller.enqueue(value); }, diff --git a/src/server/relay.ts b/src/server/relay.ts index 05c532ae9d..9203288666 100644 --- a/src/server/relay.ts +++ b/src/server/relay.ts @@ -1097,7 +1097,7 @@ export async function forward( // Sits AFTER the #2347 plugin-lane tee on purpose: the dump twin must // observe the raw upstream bytes even when the guard aborts them. pluginBody = wrapStreamWithRunawayGuard(pluginBody, (v) => { - log("error", `[${prepared.session.id}] runaway enumeration detected (${v.reason}; ${JSON.stringify(v.detail)}) — aborting upstream stream`); + log("error", `[${prepared.session.id}] output-side runaway guard tripped (${v.reason}; ${JSON.stringify(v.detail)}) — aborting upstream stream`); clientAbort.abort(); }); if (prepared.protocol === "responses") { @@ -1385,7 +1385,7 @@ export async function forward( // responseBody), so the guard wraps the post-tee body: the dump twin still // observes raw upstream bytes even when the guard aborts them. responseBody = wrapStreamWithRunawayGuard(responseBody, (v) => { - log("error", `[${prepared.session.id}] runaway enumeration detected (${v.reason}; ${JSON.stringify(v.detail)}) — aborting upstream stream`); + log("error", `[${prepared.session.id}] output-side runaway guard tripped (${v.reason}; ${JSON.stringify(v.detail)}) — aborting upstream stream`); clientAbort.abort(); }); // P1.1: wrap the rewriter loops in try/catch. If a rewriter throws diff --git a/tests/issue3260-text-run-guard.test.ts b/tests/issue3260-text-run-guard.test.ts new file mode 100644 index 0000000000..b19845d05b --- /dev/null +++ b/tests/issue3260-text-run-guard.test.ts @@ -0,0 +1,259 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { + createRunawayGuard, + wrapStreamWithRunawayGuard, + TEXT_RUN_THRESHOLD, + type RunawayVerdict, +} from "../src/runaway-guard.ts"; + +// #3260 regression: output-side single-codepoint repetition breaker (form ② / #3946: +// one symbol string for ~22 minutes). Port of pi's findDegenerateRuns minRun semantics +// onto bili's existing intrinsic stream terminator. Trips on >= 200 consecutive +// identical codepoints in DECODED model text only; never on raw bytes (base64 / +// tool-argument blobs) nor on cumulative snapshot events; byte-exact forwarding and +// the clean-EOF trip contract are preserved. + +const enc = new TextEncoder(); + +function sseFrame(json: string): Uint8Array { + return enc.encode(`data: ${json}\n\n`); +} + +function openaiDelta(content: string): string { + return JSON.stringify({ choices: [{ index: 0, delta: { content } }] }); +} + +function wrapParts(parts: Uint8Array[], verdicts: RunawayVerdict[]): ReadableStream { + const src = new ReadableStream({ + start(c) { + for (const p of parts) c.enqueue(p); + c.close(); + }, + }); + return wrapStreamWithRunawayGuard(src, (v) => verdicts.push(v)); +} + +async function collect(stream: ReadableStream): Promise { + const r = stream.getReader(); + const chunks: Uint8Array[] = []; + for (;;) { + const { done, value } = await r.read(); + if (done) break; + chunks.push(value); + } + return Buffer.concat(chunks); +} + +test("core: 200 identical codepoints trips as text-run (threshold parity with pi minRun)", () => { + const g = createRunawayGuard(); + const v = g.feedText("=".repeat(TEXT_RUN_THRESHOLD)); + assert.equal(v.tripped, true); + assert.equal(v.reason, "text-run"); + assert.equal(v.detail?.maxTextRun, TEXT_RUN_THRESHOLD); + assert.equal(v.detail?.textRunChar, "="); +}); + +test("core: threshold-1 does not trip; the final codepoint across calls trips (stateful)", () => { + const g = createRunawayGuard(); + let v = g.feedText("=".repeat(TEXT_RUN_THRESHOLD - 1)); + assert.equal(v.tripped, false); + v = g.feedText("="); + assert.equal(v.tripped, true); + assert.equal(v.reason, "text-run"); +}); + +test("core: a different codepoint resets the run (199 + x + 199 stays under)", () => { + const g = createRunawayGuard(); + const v = g.feedText("=".repeat(TEXT_RUN_THRESHOLD - 1) + "x" + "=".repeat(TEXT_RUN_THRESHOLD - 1)); + assert.equal(v.tripped, false, JSON.stringify(v.detail)); +}); + +test("core: astral codepoints count as ONE unit (emoji x200 trips)", () => { + const g = createRunawayGuard(); + const v = g.feedText("\u{1F501}".repeat(TEXT_RUN_THRESHOLD)); + assert.equal(v.tripped, true); + assert.equal(v.reason, "text-run"); + assert.equal(v.detail?.textRunChar, "\u{1F501}"); +}); + +test("core: alternating characters never trip regardless of length", () => { + const g = createRunawayGuard(); + const v = g.feedText("ab".repeat(50_000)); + assert.equal(v.tripped, false); +}); + +test("core: sub-threshold runs expose maxTextRun in detail (near-miss observability)", () => { + const g = createRunawayGuard(); + const v = g.feedText("z".repeat(180)); + assert.equal(v.tripped, false); + assert.equal(v.detail?.maxTextRun, 180); + assert.equal(v.detail?.textRunChar, "z"); +}); + +test("wrap: openai SSE frames building the run across frames trip; byte-exact prefix, clean EOF", async () => { + // 37 '=' per frame: cumulative 185 after 5 frames (< 200), 222 reached inside frame + // 6, so the guard trips while inspecting frame 6 and exactly frames 1..5 forward. + const pieceLen = 37; + const parts: Uint8Array[] = []; + for (let i = 0; i < 7; i++) parts.push(sseFrame(openaiDelta("=".repeat(pieceLen)))); + const tail = enc.encode("TAIL-MUST-NOT-BE-FORWARDED"); + parts.push(tail); + const verdicts: RunawayVerdict[] = []; + const out = await collect(wrapParts(parts, verdicts)); + assert.equal(verdicts.length, 1); + assert.equal(verdicts[0].reason, "text-run"); + assert.ok(!out.toString("utf8").includes("TAIL-MUST-NOT-BE-FORWARDED")); + const forwardedFrames = Math.floor((TEXT_RUN_THRESHOLD - 1) / pieceLen); // 5 + assert.deepEqual(out, Buffer.concat(parts.slice(0, forwardedFrames))); +}); + +test("wrap: exactly threshold-1 across frames does NOT trip; all bytes forwarded", async () => { + const parts: Uint8Array[] = []; + let emitted = 0; + while (emitted < TEXT_RUN_THRESHOLD - 1) { + const n = Math.min(37, TEXT_RUN_THRESHOLD - 1 - emitted); + parts.push(sseFrame(openaiDelta("=".repeat(n)))); + emitted += n; + } + const verdicts: RunawayVerdict[] = []; + const out = await collect(wrapParts(parts, verdicts)); + assert.equal(verdicts.length, 0); + assert.deepEqual(out, Buffer.concat(parts)); +}); + +test("wrap: 300 '=' inside a tool-call ARGUMENT must NOT trip (#1039 user intent)", async () => { + const argPayload = JSON.stringify({ path: "/tmp/blob.bin", data: "=".repeat(300) }); + const frame = JSON.stringify({ + choices: [ + { + index: 0, + delta: { + tool_calls: [ + { index: 0, id: "call_1", type: "function", function: { name: "write", arguments: argPayload } }, + ], + }, + }, + ], + }); + const parts = [sseFrame(frame), sseFrame(openaiDelta(" done."))]; + const verdicts: RunawayVerdict[] = []; + const out = await collect(wrapParts(parts, verdicts)); + assert.equal(verdicts.length, 0, JSON.stringify(verdicts)); + assert.deepEqual(out, Buffer.concat(parts)); +}); + +test("wrap: anthropic content_block_delta frames (spaces x240) trip", async () => { + const frame = (t: string): string => + JSON.stringify({ type: "content_block_delta", index: 0, delta: { type: "text_delta", text: t } }); + const parts: Uint8Array[] = []; + for (let i = 0; i < 6; i++) parts.push(sseFrame(frame(" ".repeat(40)))); + const verdicts: RunawayVerdict[] = []; + const out = await collect(wrapParts(parts, verdicts)); + assert.equal(verdicts.length, 1); + assert.equal(verdicts[0].reason, "text-run"); + assert.equal(verdicts[0].detail?.textRunChar, " "); + assert.ok(out.length < Buffer.concat(parts).length, "stream cut before the tail"); +}); + +test("wrap: responses output_text.delta trips; a .completed snapshot ALONE does not (no double-count)", async () => { + const deltaFrame = (t: string): string => + JSON.stringify({ type: "response.output_text.delta", item_id: "msg_1", output_index: 0, content_index: 0, delta: t }); + const parts: Uint8Array[] = []; + for (let i = 0; i < 5; i++) parts.push(sseFrame(deltaFrame("-".repeat(45)))); + const verdicts: RunawayVerdict[] = []; + await collect(wrapParts(parts, verdicts)); + assert.equal(verdicts.length, 1); + assert.equal(verdicts[0].reason, "text-run"); + + const snap = JSON.stringify({ + type: "response.completed", + response: { + id: "resp_1", + output: [ + { + type: "message", + id: "msg_1", + role: "assistant", + status: "completed", + content: [{ type: "output_text", text: "-".repeat(300), annotations: [] }], + }, + ], + }, + }); + const snapVerdicts: RunawayVerdict[] = []; + await collect(wrapParts([sseFrame(snap)], snapVerdicts)); + assert.equal(snapVerdicts.length, 0, JSON.stringify(snapVerdicts)); +}); + +test("wrap: responses function_call_arguments.delta with 300 '=' does NOT trip (argument bytes)", async () => { + const f = (t: string): string => + JSON.stringify({ type: "response.function_call_arguments.delta", item_id: "fc_1", call_id: "call_1", delta: t }); + const parts: Uint8Array[] = []; + for (let i = 0; i < 3; i++) parts.push(sseFrame(f("=".repeat(120)))); + const verdicts: RunawayVerdict[] = []; + const out = await collect(wrapParts(parts, verdicts)); + assert.equal(verdicts.length, 0, JSON.stringify(verdicts)); + assert.deepEqual(out, Buffer.concat(parts)); +}); + +test("wrap: 3-byte-at-a-time feed crosses decoder/frame boundary splits and still trips", async () => { + const sse = Array.from({ length: 7 }, () => `data: ${openaiDelta("=".repeat(37))}\n\n`).join(""); + const full = enc.encode(sse); + const src = new ReadableStream({ + start(c) { + for (let i = 0; i < full.length; i += 3) c.enqueue(full.subarray(i, i + 3)); + c.close(); + }, + }); + const verdicts: RunawayVerdict[] = []; + await collect(wrapStreamWithRunawayGuard(src, (v) => verdicts.push(v))); + assert.equal(verdicts.length, 1); + assert.equal(verdicts[0].reason, "text-run"); +}); + +test("wrap: NDJSON server (bare-newline events, no blank line) trips via the fallback splitter", async () => { + const line = openaiDelta("=="); + const body = Array.from({ length: 2000 }, () => line).join("\n"); + assert.ok(Buffer.byteLength(body) >= 65536, "sanity: large enough to engage the NDJSON fallback"); + const verdicts: RunawayVerdict[] = []; + const src = new ReadableStream({ + start(c) { + c.enqueue(enc.encode(body)); + c.close(); + }, + }); + await collect(wrapStreamWithRunawayGuard(src, (v) => verdicts.push(v))); + assert.equal(verdicts.length, 1); + assert.equal(verdicts[0].reason, "text-run"); +}); + +test("wrap: 200KB of legit prose passes untouched (no trip, byte-exact)", async () => { + const sentence = "Compression keeps the prefix cache warm while the session grows by a few thousand tokens per turn. "; + const body = sentence.repeat(Math.ceil(200_000 / sentence.length)); + const full = enc.encode(body); + const chunks: Uint8Array[] = []; + for (let i = 0; i < full.length; i += 8192) chunks.push(full.subarray(i, i + 8192)); + const verdicts: RunawayVerdict[] = []; + const out = await collect(wrapParts(chunks, verdicts)); + assert.equal(verdicts.length, 0); + assert.deepEqual(out, Buffer.from(full)); +}); + +test("wrap: a 150-char '=' separator line (margin under threshold) does NOT trip", async () => { + const parts = [sseFrame(openaiDelta("=".repeat(150))), sseFrame(openaiDelta(" rest of the answer"))]; + const verdicts: RunawayVerdict[] = []; + const out = await collect(wrapParts(parts, verdicts)); + assert.equal(verdicts.length, 0); + assert.deepEqual(out, Buffer.concat(parts)); +}); + +test("wrap: acp-tag flood still trips as acp-enumeration with the text lane active", async () => { + const tag = `\x3cacp tokens="0" type="text">m02040\x3c/acp\x3c/acp>\n`; + const parts: Uint8Array[] = []; + for (let i = 0; i < 265; i++) parts.push(enc.encode(tag)); + const verdicts: RunawayVerdict[] = []; + await collect(wrapParts(parts, verdicts)); + assert.equal(verdicts.length, 1); + assert.equal(verdicts[0].reason, "acp-enumeration"); +});