diff --git a/src/agent/dsh-native.ts b/src/agent/dsh-native.ts index 5d83069fb5..65373917ba 100644 --- a/src/agent/dsh-native.ts +++ b/src/agent/dsh-native.ts @@ -1589,7 +1589,12 @@ const bodyParseMemo = new WeakMap(); let bodyParseRealCount = 0; function inboundBytesOf(raw: unknown): number { - if (typeof raw === "string") return raw.length; + // #6908: report BYTES, not UTF-16 code units — the field feeds the + // host-json-parse `inbound=…B` line and operators compare it against the + // ingest gate's UTF-8 byteLength; raw.length under-reports CJK-heavy bodies + // by the byte/char ratio (field: 31.0M "B" vs a 33.5M-byte gate, an 8%+ + // gap that hid how close the body was to the wall). + if (typeof raw === "string") return Buffer.byteLength(raw, "utf8"); if (raw instanceof Uint8Array || raw instanceof ArrayBuffer) return raw.byteLength; return 0; } diff --git a/src/server/handle.ts b/src/server/handle.ts index 82b4dc21ce..0cb692f532 100644 --- a/src/server/handle.ts +++ b/src/server/handle.ts @@ -71,7 +71,7 @@ import { extractConversationId, isV1BackgroundSideRequest, isV1InternalRequest, import { anthropicBetaContextWindow, anthropicBetaMessageThreads, BILI_HOP_HEADER, capRegistryWindowByStandard, expandedContextSuffixWindow, launcherContextWindow, launcherMaxOutput, noteUnresolvedModelSession, UNRESOLVED_MODEL_SESSION_THRESHOLD, windowSourceLogged } from "./context-window.js"; import { NO_IDENTITY_MESSAGE, safeSessionId } from "./headers.js"; import { demoteGate, hasLeakedBiliToolsOnly, hasSingleShotBudget, isAudioOutputCall, isClaudeCodeTitleRequest, isDshAutoReviewRequest, isDshSingleShotSidecar, isDshTitleRequest, isMcodeOneShotAuxRequest, isMcodeTitleRequest, isOpencodeTitleRequest, isResponsesServerToolSingleShot, isServerToolUtilityCall, isSideRequest, isStructuredOutputCall, resolveSideLane, restoreOutputBudget, sideRequestGuard, stripLeakedBiliTools } from "./side-request.js"; -import { DSH_COMPACTION_SHAPE_MSGS, DSH_REFUSAL_STORM_LOG_EVERY, detectPassthroughDshCompactionCall, dshCompactionRefusal, dshPassthroughGuardStats, isDshCompactionCall } from "./dsh-compaction-guard.js"; +import { DSH_COMPACTION_MARKER_SNIFF, DSH_COMPACTION_SHAPE_MSGS, DSH_REFUSAL_STORM_LOG_EVERY, detectPassthroughDshCompactionCall, dshCompactionRefusal, dshPassthroughGuardStats, isDshCompactionCall } from "./dsh-compaction-guard.js"; import { buildDshCompactionForgeBody, renderDshAuthoredCheckpoint } from "./dsh-authored-compaction.js"; import { artifactSeedHit, detectAcpArtifacts } from "./chain-artifacts.js"; import { piSubagentChannelFallback } from "./pi-subagent-channel.js"; @@ -535,8 +535,50 @@ async function handleRequest( // pre-send byte budget. The probe is a cheap substring gate, // so the text-dump rejection (#5794's shape) stays parse-free. const ingestCap = protocol === "responses" ? 0 : knobLimitsIngestBytes(); + // #6908/#7426: two dimensionally-motivated exemptions. The + // band's premise ("any parseable 32 MiB+ text body is ≥ ~8M + // tokens, beyond every window") equates INBOUND BYTES with + // MODEL INPUT — true for the outbound / incremental lanes, + // false for the plugin lanes, which re-send the host's ENTIRE + // raw ledger on full-resend turns (dsh on the OpenAI wire, no + // incremental lane there, #3433; opencode V2 the same — field: + // 33.5 MiB inbound vs ~391K tokens of actual context, #7426): + // the body is the host's history, not the model input, and + // bili's job is to fold it (dsh field: 32.0 MiB inbound vs + // 446.4 KiB outbound, 70x). Refusing it 413s a healthy session + // forever, and the parse cost is the lane's structural cost — + // already paid every round below the cliff, so the gate only + // added a discontinuity at 32 MiB. + // (A) x-bili-plugin present (ANY plugin lane) → the body is + // fold substrate, exempt. #7426 generalized this from + // `=== "dsh"` to the whole cooperative plugin surface: + // the same full-resend structure (host ledger re-sent, + // bili folds it) is what the header announces, and the + // opencode lane was 413-locked by the dsh-only test. + // Incremental-lane turns carry small deltas and never + // reach the band, so the wider net is harmless there. + // (B) the dsh compaction marker present → the call is the ONLY + // action that shrinks the host's ledger via bili (#3733 + // answers it locally with the folded snapshot); rc.2 + // replays the full shadowed region (#2193), so the + // self-heal itself crosses the band and must pass the gate + // (self-lock otherwise). A 48-byte sniff decides; a false + // positive (a turn that merely quotes the marker) just pays + // the parse cost, identical to today's under-cliff behavior + // — the full marker-decisive check (isDshCompactionCall) + // still runs after parse. + // #7426: the exemption generalizes from "is it dsh" to "is it a + // plugin lane" — every cooperative plugin lane (dsh, opencode, + // omp, pi, …) re-sends the host's ledger on full-resend turns, + // so the body is fold substrate, not model input. The gate's + // premise ("inbound bytes ≈ model input") is false for all of + // them; incremental-lane turns carry small deltas and never hit + // the band, so the wider exemption is harmless there. + const pluginLane = req.headers["x-bili-plugin"] !== undefined; + const dshCompactionSniff = decoded.body.includes(DSH_COMPACTION_MARKER_SNIFF); if (ingestCap > 0 && decoded.body.byteLength > ingestCap - && countImagesInRawBody(protocol, decoded.body) === 0) { + && countImagesInRawBody(protocol, decoded.body) === 0 + && !pluginLane && !dshCompactionSniff) { throw new IngestBodyTooLargeError(ingestCap, decoded.body.byteLength); } bodyBuffer = decoded.body; @@ -561,7 +603,11 @@ async function handleRequest( return; } if (err instanceof BodyTooLargeError || err instanceof DecompressedTooLargeError || err instanceof IngestBodyTooLargeError) { - log("warn", `413: request body exceeds ${err.limit} bytes`); + // #6908: the ingest 413 names the ACTUAL decoded byte count next to + // the cap — operators were comparing the host-json-parse inbound + // figure against the cap and an 8%+ unit gap hid how close the body + // was to the wall. The other two caps only carry the limit. + log("warn", `413: request body exceeds ${err.limit} bytes${err instanceof IngestBodyTooLargeError ? ` (actual ${err.bytes.toLocaleString("en-US")} B after decode)` : ""}`); res.writeHead(413, { "content-type": "application/json" }); res.end(JSON.stringify({ error: { type: "request_too_large", stage: err instanceof IngestBodyTooLargeError ? "ingest" : err instanceof BodyTooLargeError ? "receive" : "decode", message: err.message } })); return; diff --git a/tests/issue5794-ingest-cap.test.ts b/tests/issue5794-ingest-cap.test.ts index e9a5c6c266..155c2d2bfd 100644 --- a/tests/issue5794-ingest-cap.test.ts +++ b/tests/issue5794-ingest-cap.test.ts @@ -204,3 +204,173 @@ test("e2e #5794 image doctrine: an over-band body carrying images is NOT refused rmrf(root); } }); + +// #6908: the band's premise ("any parseable 32 MiB+ text body is ≥ ~8M tokens, +// beyond every window") equates INBOUND BYTES with MODEL INPUT. On the dsh +// plugin lane that is dimensionally wrong: the host re-sends its ENTIRE raw +// ledger every round on the OpenAI wire (no incremental lane there, #3433), so +// the body is the host's history — fold substrate, not model input — and bili's +// job is to fold it (field: 32.0 MiB inbound vs 446.4 KiB outbound, 70x). Two +// exemptions keep a healthy session from being 413'd forever: (A) the dsh +// plugin lane, and (B) the dsh compaction call (the ONLY action that shrinks +// the host's ledger via bili — rc.2 replays the full shadowed region, #2193, so +// the self-heal itself crosses the band and must pass the gate or the session +// self-locks). Both directions are pinned below. + +test("e2e #6908 (A): the dsh plugin lane (x-bili-plugin: dsh) is exempt from the ingest band — an over-band full-resend body is folded and forwarded, not 413 at ingest", async () => { + const root = mkdtempSync(path.join(tmpdir(), "bili-6908a-")); + const disarm = armIngestBand(); + const { ctx, calls, close } = await setup(); + try { + // A realistic dsh full-resend body: the host re-sends its ENTIRE raw + // ledger every round on the OpenAI wire (no incremental lane there, + // #3433). The bulk is OLD foldable history (tool call/result pairs); + // the core is a small recent user message. Total body is over the + // 1 MiB test band, but the core is tiny — so bili folds the history + // and forwards. Without the lane exemption the gate 413s it at ingest + // before any folding happens (the field's self-lock). + const messages: Array> = []; + for (let i = 0; i < 1500; i++) { + messages.push({ role: "assistant", content: "", tool_calls: [{ id: `call_${i}`, type: "function", function: { name: "read_file", arguments: JSON.stringify({ path: `/tmp/file_${i}.txt` }) } }] }); + messages.push({ role: "tool", tool_call_id: `call_${i}`, content: "x".repeat(700) }); + } + messages.push({ role: "user", content: "Summarize what you found." }); + const r = await fetch(ctx.url, { + method: "POST", + headers: { ...ctx.headers, "x-bili-plugin": "dsh", "x-bili-plugin-conversation": "s-count-ingest" }, + body: JSON.stringify({ model: "gpt-test", stream: true, messages }), + }); + const body = await r.text(); + // The gate (ingest) must NOT refuse the dsh lane — the body is the + // host's fold substrate, not model input. A 200 SSE stream proves the + // body passed the ingest gate, was folded, and was forwarded upstream. + if (r.status !== 200) { + const parsed = JSON.parse(body) as { error?: { stage?: string } }; + assert.notEqual(parsed.error?.stage, "ingest", `the dsh lane body must pass the ingest gate (got stage=${parsed.error?.stage}, status=${r.status}): ${body.slice(0, 300)}`); + } + assert.equal(r.status, 200, `the dsh lane body is folded and forwarded (got ${r.status}): ${body.slice(0, 300)}`); + assert.equal(calls.length, 1, "the dsh lane body reached the upstream"); + } finally { + disarm(); + await close(); + rmrf(root); + } +}); + +test("e2e #6908 (B): a dsh compaction call (marker present) passes the ingest band — the self-heal reaches the compaction guard, not a 413", async () => { + const root = mkdtempSync(path.join(tmpdir(), "bili-6908b-")); + const disarm = armIngestBand(); + const { ctx, close } = await setup(); + try { + // The final user message STARTS with the compaction instruction prefix + // (marker-decisive, #2193) and the body is over the 1 MiB band. Without + // the sniff exemption the gate 413s it BEFORE parse and the self-heal is + // blocked (self-lock). With it, the body reaches isDshCompactionCall and + // is answered by the compaction guard (422 refusal on a fresh session + // with no active fold blocks) — proving it got PAST the ingest gate. + const marker = "You are now acting as a compaction engine for this AI coding assistant"; + const r = await fetch(ctx.url, { + method: "POST", + headers: { ...ctx.headers }, + body: JSON.stringify({ model: "gpt-test", stream: true, messages: [{ role: "user", content: marker + "x".repeat(3 * 1024 * 1024) }] }), + }); + const body = await r.text(); + assert.notEqual(r.status, 413, `compaction call must pass the ingest gate (got ${r.status}): ${body.slice(0, 300)}`); + assert.equal(r.status, 422, "the compaction guard answers it past the gate (proving it reached isDshCompactionCall)"); + assert.match(body, /dsh_compaction_refused/, "the refusal is the compaction guard's, not the ingest band's"); + } finally { + disarm(); + await close(); + rmrf(root); + } +}); + +test("e2e #6908 negative: an over-band body with NO plugin header and NO compaction marker is still refused 413 (the exemptions must not widen the band)", async () => { + const root = mkdtempSync(path.join(tmpdir(), "bili-6908c-")); + const disarm = armIngestBand(); + const { ctx, calls, close } = await setup(); + try { + // Over the 1 MiB band, plain openai lane, no x-bili-plugin, no marker — + // the band's original behavior is preserved (the #5794 shape). + const r = await postMessages(ctx, "x".repeat(3 * 1024 * 1024)); + const body = await r.text(); + const parsed = JSON.parse(body) as { error?: { type?: string; stage?: string } }; + assert.equal(r.status, 413, `non-plugin over-band body must still be refused (got ${r.status}): ${body.slice(0, 300)}`); + assert.equal(parsed.error?.stage, "ingest", "still the ingest stage"); + assert.equal(calls.length, 0, "zero upstream calls — the band still holds for non-plugin lanes"); + } finally { + disarm(); + await close(); + rmrf(root); + } +}); + +// #7426: the #6908 exemption was dsh-only (`x-bili-plugin === "dsh"`), so the +// opencode lane — which has the SAME full-resend structure (the host re-sends +// its ENTIRE raw ledger every round; bili's job is to fold it) — was still +// 413-locked at the band (field: 33.5 MiB inbound vs ~391K tokens of actual +// context, a healthy 390-block session). The criterion generalizes to ANY +// plugin lane: the `x-bili-plugin` header announces "my body is my ledger, +// fold it," which is dimensionally true for every cooperative plugin lane. +// Both directions are pinned below. + +test("e2e #7426: the opencode plugin lane (x-bili-plugin: opencode) is exempt from the ingest band — an over-band full-resend body is folded and forwarded, not 413 at ingest", async () => { + const root = mkdtempSync(path.join(tmpdir(), "bili-7426a-")); + const disarm = armIngestBand(); + const { ctx, calls, close } = await setup(); + try { + // A realistic opencode full-resend body: the host re-sends its ENTIRE + // raw ledger every round. The bulk is OLD foldable history (tool + // call/result pairs); the core is a small recent user message. Total + // body is over the 1 MiB test band, but the core is tiny — so bili + // folds the history and forwards. Without the lane exemption the gate + // 413s it at ingest before any folding happens (the field's self-lock). + const messages: Array> = []; + for (let i = 0; i < 1500; i++) { + messages.push({ role: "assistant", content: "", tool_calls: [{ id: `call_${i}`, type: "function", function: { name: "read_file", arguments: JSON.stringify({ path: `/tmp/file_${i}.txt` }) } }] }); + messages.push({ role: "tool", tool_call_id: `call_${i}`, content: "x".repeat(700) }); + } + messages.push({ role: "user", content: "Summarize what you found." }); + const r = await fetch(ctx.url, { + method: "POST", + headers: { ...ctx.headers, "x-bili-plugin": "opencode", "x-bili-plugin-conversation": "s-count-ingest" }, + body: JSON.stringify({ model: "gpt-test", stream: true, messages }), + }); + const body = await r.text(); + // The gate (ingest) must NOT refuse the opencode lane — the body is + // the host's fold substrate, not model input. A 200 SSE stream proves + // the body passed the ingest gate, was folded, and was forwarded + // upstream. + if (r.status !== 200) { + const parsed = JSON.parse(body) as { error?: { stage?: string } }; + assert.notEqual(parsed.error?.stage, "ingest", `the opencode lane body must pass the ingest gate (got stage=${parsed.error?.stage}, status=${r.status}): ${body.slice(0, 300)}`); + } + assert.equal(r.status, 200, `the opencode lane body is folded and forwarded (got ${r.status}): ${body.slice(0, 300)}`); + assert.equal(calls.length, 1, "the opencode lane body reached the upstream"); + } finally { + disarm(); + await close(); + rmrf(root); + } +}); + +test("e2e #7426 negative: an over-band body from a NON-plugin lane (no x-bili-plugin header) is still refused 413 — the generalized exemption must not leak to plain clients", async () => { + const root = mkdtempSync(path.join(tmpdir(), "bili-7426b-")); + const disarm = armIngestBand(); + const { ctx, calls, close } = await setup(); + try { + // Over the 1 MiB band, plain openai lane, NO x-bili-plugin header — + // the band's original behavior is preserved for non-plugin clients + // (the #5794 shape). The generalized exemption must not leak here. + const r = await postMessages(ctx, "x".repeat(3 * 1024 * 1024)); + const body = await r.text(); + const parsed = JSON.parse(body) as { error?: { type?: string; stage?: string } }; + assert.equal(r.status, 413, `non-plugin over-band body must still be refused (got ${r.status}): ${body.slice(0, 300)}`); + assert.equal(parsed.error?.stage, "ingest", "still the ingest stage"); + assert.equal(calls.length, 0, "zero upstream calls — the band still holds for plain clients"); + } finally { + disarm(); + await close(); + rmrf(root); + } +});