Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
245 changes: 227 additions & 18 deletions src/runaway-guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}

Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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<string> = 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<string> = 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;
Expand All @@ -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<RunawayVerdict["detail"]> => ({
acpTags,
toolXmlFragments: toolXml,
toolXmlGapBytes: toolGapBytes,
maxMonotonicRefRun: maxMonoRun,
zeroTokenFraction: acpTags > 0 ? zeroTokens / acpTags : 0,
});
const detail = (): NonNullable<RunawayVerdict["detail"]> => {
const d: NonNullable<RunawayVerdict["detail"]> = {
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() };
Expand Down Expand Up @@ -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;
Expand All @@ -163,6 +251,119 @@ export function wrapStreamWithRunawayGuard<T extends Uint8Array>(
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<string, unknown>)) {
collectText(v, k, ex);
if (walkTripped) return;
}
}
};

const processParsed = (obj: unknown): void => {
if (typeof obj !== "object" || obj === null) return;
const t = (obj as Record<string, unknown>)["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<T>): void => {
settled = true;
onTrip(tripVerdict ?? { tripped: true });
controller.close();
reader.cancel().catch(() => {});
};

return new ReadableStream<T>({
async pull(controller) {
if (settled) return;
Expand All @@ -179,17 +380,25 @@ export function wrapStreamWithRunawayGuard<T extends Uint8Array>(
// 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);
},
Expand Down
4 changes: 2 additions & 2 deletions src/server/relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down Expand Up @@ -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
Expand Down
Loading
Loading