Skip to content
Open
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
14 changes: 13 additions & 1 deletion src/preflight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -500,12 +500,24 @@ export interface RawBodyBreakdown {
other: number;
}

// #7175: the foldable message array and the system prompt sit under WIRE-SPECIFIC
// top-level keys — `messages` (OpenAI chat / Anthropic), `input` (Responses),
// `contents` (Google) for messages; `system` (Anthropic), `instructions`
// (Responses), `systemInstruction` (Google) for the system prompt. Bucketing on
// `messages`/`system` alone dropped the Responses/Google message array into
// "other", which the #7167 irreducible gate then read as "dimensions folding
// cannot touch" and 413'd an entirely-foldable body (the #3686/#488-B
// Responses regressions). `total` is the sum of the buckets either way, so the
// doom verdicts that price `total` are untouched by this re-split.
const MESSAGE_ARRAY_KEYS = new Set(["messages", "input", "contents"]);
const SYSTEM_PROMPT_KEYS = new Set(["system", "instructions", "systemInstruction"]);

export function estimateRawBodyBreakdown(parsed: unknown): RawBodyBreakdown {
const buckets: [number, number, number, number] = [0, 0, 0, 0]; // messages, tools, system, other
const pending: [unknown, string | undefined, number][] = [];
if (parsed && typeof parsed === "object") {
for (const [k, v] of Object.entries(parsed as Record<string, unknown>)) {
const bucket = k === "messages" ? 0 : k === "tools" ? 1 : k === "system" ? 2 : 3;
const bucket = MESSAGE_ARRAY_KEYS.has(k) ? 0 : k === "tools" ? 1 : SYSTEM_PROMPT_KEYS.has(k) ? 2 : 3;
pending.push([v, k, bucket]);
}
} else {
Expand Down
31 changes: 31 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4030,6 +4030,37 @@ export async function preflightCompressIfNeeded(
}
}
if (limit <= 0 || !model || !triggerFires) return prepared;
// #7167: the irreducible-dimension gate — tools[], system and other are
// dimensions folding CANNOT touch: the moment they alone clear the doom
// bar, no amount of message compaction can fit the body, so the fold
// below is guaranteed-doomed from its first summary call — it would
// irreversibly rewrite the session's history and bill real tokens for a
// 413 the post-fold measured dooms (#1839/#3146 arms) would answer
// anyway. Measure the UNMODIFIED prepared body with the #554 guard meter
// and fail fast before probeForEvidence / preflightCompress. Same bar as
// those post-fold dooms (the native window when bili knows it, the
// headroom limit as fallback): the gate can only refuse a body the
// post-fold flow would have 413'd with the same measured meter — no new
// failure mode. Growth-armed payloads keep the autoFold fail-open rule
// (a growth trigger is operator intent and the payload fits the real
// window by definition — the #3444 floor mechanism owns that regime's
// unreachable-target case). Unparseable bodies fall through
// (measuredOutbound undefined) — current behavior holds.
if (!growthArmed) {
const preFoldOutbound = measuredOutbound(prepared);
if (preFoldOutbound !== undefined) {
const pf = preFoldOutbound.breakdown;
const irreducible = pf.tools + pf.system + pf.other;
const doomBar = typeof resolvedNativeWindow === "number" && resolvedNativeWindow > 0 ? resolvedNativeWindow : limit;
if (irreducible >= doomBar) {
const nativeKnown = typeof resolvedNativeWindow === "number" && resolvedNativeWindow > 0;
const irreducibleMessage = `tools[] + system + other alone measure ~${irreducible.toLocaleString("en-US")} tokens (${measuredBreakdownClause(pf)}; CJK-aware count, the #554 guard meter; images billed separately and never doom here — #496/#1800/#857) vs ${nativeKnown ? `the model's window ${doomBar.toLocaleString("en-US")}` : `bili's effective window ${doomBar.toLocaleString("en-US")}`} (model=${model}) — dimensions compression cannot touch, so no amount of message folding can fit this body. Compaction was NOT attempted and the body was NOT forwarded. Trim the tool surface (fewer or smaller tools, lazy tool discovery), shorten the system prompt${nativeKnown ? "" : ", or raise the effective window (compress.modelContextLimit)"} and retry.`;
log("error", `[${session.id}] preflight fail-fast 413 (retryable=false, irreducible-dimension gate): ${irreducibleMessage}`);
noteLocalRefusal(session, currentOrigin, model, limit);
return { failFast: true, status: 413, message: irreducibleMessage, retryable: false, respond: !res.writableEnded };
}
}
}
// #3846: an earlier local 413 for THIS origin+model at an equal or wider
// window is independent evidence the payload cannot fit here — the
// forward-once escapes below must not re-bet the estimator guess. Fitting
Expand Down
14 changes: 9 additions & 5 deletions tests/issue4079-egress-window-fuse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,19 @@ test("e2e #4079: a body that provably cannot fit any window is stopped at the eg
}
});

test("e2e #5000: a body whose mass rides tools[] — core text clearing the byte floor — is still stopped by the measured tier, not forwarded", async () => {
test("e2e #5000: a body whose mass rides tools[] — core text clearing the byte floor — is stopped by the #7167 irreducible gate before the fold, not forwarded", async () => {
const root = mkdtempSync(path.join(tmpdir(), "bili-4079t-"));
const { ctx, calls, close } = await setup();
try {
// Incident #5000's shape: the wire body's mass sits in tool definitions,
// which the core-text byte floor never sees (it prices m.text only).
// 120 tools × 40KB descriptions ≈ 4.8MB of tool text ≈ ~1.2M measured
// tokens — over the floored 1M bar — while the core text is ~1.5KB,
// ~5000× under the 7.5MB byte floor. With `model` present the preflight
// measured gate fires before the relay fuse; same verdict either way.
// ~5000× under the 7.5MB byte floor. With `model` present the #7167
// irreducible-dimension gate fires BEFORE the fold (tools[] alone clear
// the doom bar, so no amount of message folding can fit the body) —
// no history rewrite, no summary billing — and answers the same 413
// the post-fold measured doom would have, with the irreducible message.
const tools = Array.from({ length: 120 }, (_, i) => ({
type: "function",
function: { name: `tool_${i}`, description: "d".repeat(40_000) },
Expand All @@ -199,9 +202,10 @@ test("e2e #5000: a body whose mass rides tools[] — core text clearing the byte
const parsed = JSON.parse(body) as { error?: { type?: string; code?: string; retryable?: boolean; message?: string } };

assert.equal(r.status, 413, `tools-mass body must fail fast (got ${r.status}): ${body.slice(0, 300)}`);
assert.equal(parsed.error?.code, "preflight_compress_failed", "the preflight fail-fast house shape (the measured gate answers before the relay)");
assert.equal(parsed.error?.code, "preflight_compress_failed", "the preflight fail-fast house shape (the irreducible gate answers before the fold)");
assert.equal(parsed.error?.retryable, false, "4xx non-retryable class — retrying re-sends the same oversized history (#2847 semantics)");
assert.match(parsed.error?.message ?? "", /outbound body measures ~[\d,]+ TEXT tokens/, "the measured tier names itself (text-only; image payloads are #496 arbitration, never doom)");
assert.match(parsed.error?.message ?? "", /tools\[\] \+ system \+ other alone measure ~[\d,]+ tokens/, "the #7167 irreducible gate names the dimensions folding cannot touch (text-only; image payloads are #496 arbitration, never doom)");
assert.match(parsed.error?.message ?? "", /Compaction was NOT attempted/, "no history rewrite, no summary billing — the fold never ran");
assert.match(parsed.error?.message ?? "", /NOT forwarded/, "the remedy wording is present");
// THE behavior under test: the doomed payload NEVER reaches the upstream
// (before the measured tier it cleared the byte floor and rode forward
Expand Down
Loading
Loading