From 69530908f85079308e7278dbfc13f8c1bdaed4c7 Mon Sep 17 00:00:00 2001 From: ci-check Date: Sat, 31 Oct 2026 22:34:33 +0800 Subject: [PATCH 1/2] fix: dsh raw-ledger planning floor follows the authored snapshot (#5289) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The compaction guard stamped dshCompactionRefusedBytes from the envelope bytes BEFORE branching into authored/refused, so even the #3733 local answer — which genuinely shrinks dsh's raw store to our folded snapshot — ratcheted the planning floor to the pre-answer raw peak and kept it sticky for the session's life. On a small window the output clamp then planned against that stale peak forever: 128K window - 93K floor - margin ~= 33K max_tokens while the folded wire sat at ~49K, truncating long replies until the user inflated DSH's declared window to dodge the clamp (#5289). Authored branch now stamps the exact bytes of the snapshot sent (the ride-out scale from now on); the refusal branch keeps the #2490 sticky high-water byte-for-byte. Docs aligned at both sites. --- src/server/budget.ts | 38 +-- src/server/handle.ts | 38 +-- tests/dsh-authored-compaction.test.ts | 2 +- ...ue5289-ledger-floor-authored-decay.test.ts | 219 ++++++++++++++++++ 4 files changed, 264 insertions(+), 33 deletions(-) create mode 100644 tests/issue5289-ledger-floor-authored-decay.test.ts diff --git a/src/server/budget.ts b/src/server/budget.ts index 9defd4ee77..37b11f1589 100644 --- a/src/server/budget.ts +++ b/src/server/budget.ts @@ -306,23 +306,27 @@ export function clampOutputBudget(requested: number, inputEstimate: number, nati return clamped; } -/** #2490: translate the refused dsh compaction replay size (recorded on the - * session by the guard in handle.ts) into a token floor for the clamp's - * planning basis. bytes/4 mirrors the default estimator's chars/4 caliber - * (the replay is JSON text riding the same wires). 0 when nothing was - * recorded — legacy behavior. - * STICKY BY DESIGN: the guard records a high-water mark and nothing decays - * or clears it while the session lives. While compaction stays refused the - * raw ledger only grows (refused checkpoints never land), so the max bounds - * every future re-send; decaying the floor would re-open the #2490 kill - * chain. Lifetime ends with the session (metadata eviction). Known - * conservative residue: if a checkpoint lands anyway (direct-bypass path; - * the #2028 opt-out was removed, #3666) and shrinks the ledger, the floor - * stays at its old high until eviction — fail-closed over-tightening, - * accepted. - * Note (#3384): recording stays sticky; what changed is APPLICATION — the - * clamp gates where this floor may drive planning (see clampOutgoingOutput), - * so a stale high-water mark can no longer starve a healthy session. */ +/** #2490: translate the recorded dsh raw-ledger scale (stamped on the session + * by the guard in handle.ts) into a token floor for the clamp's planning + * basis. bytes/4 mirrors the default estimator's chars/4 caliber (the replay + * is JSON text riding the same wires). 0 when nothing was recorded — legacy + * behavior. + * Two stamp regimes (#5289): the REFUSAL path stamps a STICKY high-water over + * the envelope bytes — while compaction stays refused the raw ledger never + * shrinks (refused checkpoints never land), so the max bounds every future + * re-send, and decaying it would re-open the #2490 kill chain. The AUTHORED + * path (#3733) stamps the EXACT bytes of the folded snapshot just handed to + * dsh — that answer genuinely shrinks dsh's store to the snapshot, so the + * floor follows reality instead of ratcheting to the pre-answer peak (which + * starved small-window sessions permanently: 128K window − 93K stale floor + * ≈ 33K max output forever while the folded wire sat at ~49K). Lifetime ends + * with the session (metadata eviction). Known conservative residue: a + * checkpoint landing through a path invisible to bili (direct bypass; the + * #2028 opt-out was removed, #3666) leaves the old high until eviction — + * fail-closed over-tightening, accepted. + * Note (#3384): APPLICATION is gated by the clamp (see clampOutgoingOutput) — + * a stale high-water mark can no longer starve a healthy session outside the + * giant-request / enforced-target regimes. */ export function dshLedgerFloorTokens(metadata: Record | undefined | null): number { const bytes = metadata?.["dshCompactionRefusedBytes"]; if (typeof bytes !== "number" || !Number.isFinite(bytes) || bytes <= 0) return 0; diff --git a/src/server/handle.ts b/src/server/handle.ts index 6064a9f130..7a9e104182 100644 --- a/src/server/handle.ts +++ b/src/server/handle.ts @@ -2036,23 +2036,18 @@ async function handleRequest( const shapeDrift = inboundMsgs !== null && inboundMsgs > DSH_COMPACTION_SHAPE_MSGS ? `, shape drifted from the ≤${DSH_COMPACTION_SHAPE_MSGS}-msg rc.1 envelope — rc.2 replays the full shadowed region (#2193)` : ""; log("warn", `[${session.id}] dsh native compaction call identified (final user message = COMPACTION_INSTRUCTION, ${inboundMsgs} msgs${shapeDrift}) — REFUSED, not forwarded: bili owns compression on this lane; a landed dsh checkpoint would durably shadow the raw history (#1729, cf. #1206/#1772)`); } - // #2490: remember the replayed envelope SIZE. The host's raw ledger is - // invisible to bili's folded view (folds never shrink what dsh itself - // counts), so the next turn's output clamp must plan against the raw - // scale or a monster output rides a small folded input out the door - // (the 384K-token/7.5MB turn-37 single-message kill chain). High-water - // mark, not a sum: the replay is the whole ledger every time, so the - // max already bounds every future re-send; a sum would compound on each - // retry. Write-only by design: nothing decays or clears this value — - // dshLedgerFloorTokens() in budget.ts documents why the mark stays - // sticky and consumes it. - const prevBytes = typeof session.metadata["dshCompactionRefusedBytes"] === "number" ? session.metadata["dshCompactionRefusedBytes"] as number : 0; - session.metadata["dshCompactionRefusedBytes"] = Math.max(prevBytes, inboundBytes); if (authored !== null) { - // #3733 counters + the same raw-ledger high-water as a refusal - // (the envelope still proves the raw scale the output clamp must - // plan against); NOT counted in dshCompactionRefusals. + // #3733 counter; NOT counted in dshCompactionRefusals. + // #5289: the authored answer SHRINKS dsh's raw store to the snapshot we just + // sent (dsh records our tagged checkpoint), so that snapshot IS the ride-out + // scale from now on — the planning floor follows it instead of ratcheting to + // the pre-answer raw peak. Ratcheting is exactly what starved small-window + // sessions permanently: 128K window − 93K stale floor ≈ 33K max output + // forever while the folded wire sat at ~49K. The refusal path below keeps + // the sticky high-water: there dsh's ledger really never shrinks and the + // #2490 monster-output protection must stay intact. session.metadata["dshAuthoredCompactions"] = (typeof session.metadata["dshAuthoredCompactions"] === "number" ? session.metadata["dshAuthoredCompactions"] as number : 0) + 1; + session.metadata["dshCompactionRefusedBytes"] = Buffer.byteLength(authored.body); markDirty(session); if (!res.headersSent && !res.writableEnded && !res.destroyed) { res.writeHead(200, { "content-type": authored.contentType }); @@ -2061,6 +2056,19 @@ async function handleRequest( logRequestCost(log, session.id, inboundMsgs, inboundBytes, reqT0); return; } + // #2490: on the REFUSAL path, remember the replayed envelope SIZE. The host's + // raw ledger is invisible to bili's folded view (folds never shrink what dsh + // itself counts), so the next turn's output clamp must plan against the raw + // scale or a monster output rides a small folded input out the door (the + // 384K-token/7.5MB turn-37 single-message kill chain). High-water mark, not a + // sum: the replay is the whole ledger every time, so the max already bounds + // every future re-send; a sum would compound on each retry. Sticky here by + // design — while compaction stays refused the ledger only grows (refused + // checkpoints never land); the authored path above resets to the snapshot + // scale instead (#5289). dshLedgerFloorTokens() in budget.ts documents + // consumption. + const prevBytes = typeof session.metadata["dshCompactionRefusedBytes"] === "number" ? session.metadata["dshCompactionRefusedBytes"] as number : 0; + session.metadata["dshCompactionRefusedBytes"] = Math.max(prevBytes, inboundBytes); session.metadata["dshCompactionRefusals"] = (typeof session.metadata["dshCompactionRefusals"] === "number" ? session.metadata["dshCompactionRefusals"] as number : 0) + 1; // #3013/#3022 (A3): after the once-warn above, refusals were fully // silent — the field storm burned 5051 refusals with zero further diff --git a/tests/dsh-authored-compaction.test.ts b/tests/dsh-authored-compaction.test.ts index c46bc4bfc3..ba8cae5500 100644 --- a/tests/dsh-authored-compaction.test.ts +++ b/tests/dsh-authored-compaction.test.ts @@ -326,7 +326,7 @@ test("e2e: authored compaction answers locally — zero egress, tagged checkpoin assert.equal(h.bodies.length, before, "upstream never contacted (zero egress)"); const s = h.sessionOf("s-auth")!; assert.equal(s.metadata["dshAuthoredCompactions"], 1, "authored counter"); - assert.ok(typeof s.metadata["dshCompactionRefusedBytes"] === "number", "raw ledger high-water still marked"); + assert.equal(s.metadata["dshCompactionRefusedBytes"], Buffer.byteLength(r.body), "#5289: floor follows the snapshot we sent, not the ~600K pre-answer envelope peak"); assert.equal(s.metadata["dshCompactionRefusals"], undefined, "not counted as a refusal"); }); }); diff --git a/tests/issue5289-ledger-floor-authored-decay.test.ts b/tests/issue5289-ledger-floor-authored-decay.test.ts new file mode 100644 index 0000000000..5c402dac10 --- /dev/null +++ b/tests/issue5289-ledger-floor-authored-decay.test.ts @@ -0,0 +1,219 @@ +// #5289: the dsh raw-ledger planning floor must follow reality on BOTH guard +// branches (#3617 — both directions of the gate pinned here). +// +// Authored branch (#3733 local answer): dsh records our folded snapshot and its +// raw store shrinks to it — so the floor := the EXACT bytes of the snapshot we +// sent, decaying below any earlier high-water. Pinning the floor to the +// pre-answer envelope peak instead starves small windows permanently (incident +// shape: 128K window − 93K stale floor − margin ≈ 33K max output while the +// folded wire sat at ~49K → long replies truncated at stop_reason=length until +// the user set DSH's window to 256K to dodge the clamp). +// +// Refusal branch: the #2490 sticky high-water over the exact envelope bytes +// stays byte-for-byte (while compaction stays refused the ledger never +// shrinks), and composing authored→refusal re-ratchets ON TOP of the decayed +// value (Math.max — never a reset). +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; + +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 { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import { listSessions, _resetSessionsForTest } from "../src/session.ts"; +import { DSH_COMPACTION_INSTRUCTION_PREFIX } from "../src/server/dsh-compaction-guard.ts"; +import { BILI_SNAPSHOT_TAG_PREFIX } from "../src/server/dsh-authored-compaction.ts"; + +const WINDOW = 131_072; +const SUMMARY = "MAIN-SUMMARY-SETUP-CONTEXT-FOLDED-BY-COMPRESSION-LONG-ENOUGH-FOR-KERNEL-MIN-LENGTH-CHECK"; + +function sse(obj: unknown): string { + return `data: ${JSON.stringify(obj)}\n\n`; +} + +// 24 messages × ~1.5k chars: under the window (no preflight); the default +// protection zone leaves m00001..m00008 foldable on the first turn (same shape +// as tests/dsh-authored-compaction.test.ts#conversation()). +function conversation(): { role: string; content: string }[] { + const out: { role: string; content: string }[] = []; + for (let i = 0; i < 24; i++) { + out.push({ role: i % 2 === 0 ? "user" : "assistant", content: `Message ${i} of the working session. ` + `WORK_${i}_content_`.repeat(88) }); + } + return out; +} + +type Seen = { maxTokens: unknown }; + +type Harness = { + url: string; + seen: Seen[]; + sessionOf: (label: string) => { metadata: Record; state: { blocks: { active: boolean }[] } } | undefined; +}; + +async function withHarness(fn: (h: Harness) => Promise): Promise { + const seen: Seen[] = []; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + const parsed = JSON.parse(Buffer.concat(chunks).toString("utf8")) as { max_tokens?: unknown }; + seen.push({ maxTokens: parsed.max_tokens }); + res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache" }); + if (seen.length === 1) { + // First egress: the model compresses the oldest span → active block. + const args = JSON.stringify({ content: [{ startId: "m00001", endId: "m00008", topic: "setup", summary: SUMMARY }] }); + res.write(sse({ id: "c1", object: "chat.completion.chunk", choices: [{ index: 0, delta: { role: "assistant", content: null, tool_calls: [{ index: 0, id: "call_p", type: "function", function: { name: "compress", arguments: args } }] } }] })); + res.write(sse({ id: "c1", object: "chat.completion.chunk", choices: [{ index: 0, delta: {}, finish_reason: "tool_calls" }] })); + res.write(sse({ id: "c1", object: "chat.completion.chunk", choices: [{ index: 0, delta: { role: "assistant", content: "done compressing" } }] })); + } + res.write(sse({ id: "c1", object: "chat.completion.chunk", choices: [{ index: 0, delta: {}, finish_reason: "stop" }] })); + res.end(); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as { port: number }).port; + + _setStoreForTest(new SessionStore({ enabled: false })); + _resetSessionsForTest(); + setRegistryForTest({}); + const proxy = await startServer({ + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { "gpt-test": { context: WINDOW } } } }, + modelContextLimit: WINDOW, + kernelConfig: defaultConfig(WINDOW), + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + passthroughSource: null, + autoUpdate: false, + autoRestartOnUpdate: false, + updateTag: "latest", + advisoryCheck: true, + releaseNotesCheck: true, + compat: { roles: {} }, + streamErrorShape: "protocol", + mitm: { enabled: false, domains: [] }, + } as unknown as ProxyOptions); + await once(proxy, "listening"); + const port = (proxy.address() as { port: number }).port; + process.env.BILI_DSH_AUTHORED_COMPACTION = "1"; + const h: Harness = { + url: `http://127.0.0.1:${port}/bili/http://127.0.0.1:${upstreamPort}/v1/chat/completions`, + seen, + sessionOf: (label) => listSessions().find((x) => x.meta.label === label) as never, + }; + try { + await fn(h); + } finally { + delete process.env.BILI_DSH_AUTHORED_COMPACTION; + proxy.close(); + proxy.closeIdleConnections?.(); + await once(proxy, "close"); + upstream.close(); + upstream.closeIdleConnections?.(); + await once(upstream, "close"); + } +} + +async function turn(h: Harness, label: string, body: string): Promise<{ status: number; body: string }> { + const r = await fetch(h.url, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": label }, + body, + }); + return { status: r.status ?? 0, body: await r.text() }; +} + +/** One normal turn that leaves an active block behind. */ +async function armBlock(h: Harness, label: string): Promise { + const r = await turn(h, label, JSON.stringify({ model: "gpt-test", messages: conversation(), stream: true })); + assert.equal(r.status, 200, r.body); + const s = h.sessionOf(label); + assert.ok(s, "session exists"); + assert.ok(s!.state.blocks.some((b) => b.active), "setup created an active block"); +} + +function compactionEnvelope(fillerChars: number): string { + return JSON.stringify({ + model: "gpt-test", + messages: [ + { role: "system", content: "sys" }, + { role: "user", content: "x".repeat(fillerChars) }, + { role: "user", content: `${DSH_COMPACTION_INSTRUCTION_PREFIX} Condense the conversation above.` }, + ], + stream: true, + }); +} + +test("e2e: authored checkpoint decays the ledger floor to the snapshot sent (small window unstarves)", async () => { + await withHarness(async (h) => { + await armBlock(h, "s-decay"); + const before = h.seen.length; + // ~372KB raw-history replay — the #5289 incident shape on a 128K window. + const r = await turn(h, "s-decay", compactionEnvelope(372_000)); + assert.equal(r.status, 200, r.body); + assert.ok(r.body.includes(BILI_SNAPSHOT_TAG_PREFIX), "answered locally with the bili snapshot"); + assert.equal(h.seen.length, before, "zero egress on the authored answer"); + const s = h.sessionOf("s-decay")!; + assert.equal(s.metadata["dshCompactionRefusedBytes"], Buffer.byteLength(r.body), "floor := the exact snapshot bytes we sent, not the 372KB envelope peak"); + assert.ok(Buffer.byteLength(r.body) < 10_000, "sanity: the snapshot is orders of magnitude smaller than the envelope"); + + // Next main turn on the shrunken store: a giant budget request (≥ 25% of + // the window — the line that engages the ledger floor). Healthy planning + // yields a full cap (>100K); a stale 93K peak would leave only ~33K. + const r2 = await turn(h, "s-decay", JSON.stringify({ + model: "gpt-test", + messages: [{ role: "user", content: "please write a very long detailed report, take your time" }], + max_tokens: 384_000, + stream: true, + })); + assert.equal(r2.status, 200, r2.body); + const forwarded = h.seen[h.seen.length - 1].maxTokens; + assert.equal(typeof forwarded, "number", "max_tokens present on the outbound request"); + assert.ok((forwarded as number) > 100_000, `outbound max_tokens ${String(forwarded)} is healthy (pre-fix it was ~33K, truncating long replies)`); + }); +}); + +test("e2e: refusal keeps the sticky high-water; authored→refusal composition re-ratchets", async () => { + await withHarness(async (h) => { + // Phase 1 — refusal (knob explicitly off; the default is ON): the #2490 + // high-water, byte-for-byte. + process.env.BILI_DSH_AUTHORED_COMPACTION = "0"; + await armBlock(h, "s-compose"); + const env1 = compactionEnvelope(300_000); + const r1 = await turn(h, "s-compose", env1); + assert.equal(r1.status, 422, "refused, not forwarded"); + let s = h.sessionOf("s-compose")!; + assert.equal(s.metadata["dshCompactionRefusedBytes"], Buffer.byteLength(env1), "refusal stamps the envelope's exact wire bytes"); + + // Phase 2 — authored (knob back on): the floor DECAYS below the earlier + // high-water to the snapshot we sent. + process.env.BILI_DSH_AUTHORED_COMPACTION = "1"; + const r2 = await turn(h, "s-compose", compactionEnvelope(200_000)); + assert.equal(r2.status, 200, r2.body); + assert.ok(r2.body.includes(BILI_SNAPSHOT_TAG_PREFIX)); + s = h.sessionOf("s-compose")!; + assert.equal(s.metadata["dshCompactionRefusedBytes"], Buffer.byteLength(r2.body), "authored answer replaces the 300KB high-water with the snapshot scale"); + assert.ok(Buffer.byteLength(r2.body) < Buffer.byteLength(env1), "decayed below the prior high-water"); + + // Phase 3 — refusal again (knob off): the ratchet applies ON TOP of the + // decayed value (Math.max), never a reset. + process.env.BILI_DSH_AUTHORED_COMPACTION = "0"; + const env3 = compactionEnvelope(400_000); + const r3 = await turn(h, "s-compose", env3); + assert.equal(r3.status, 422); + s = h.sessionOf("s-compose")!; + assert.equal(s.metadata["dshCompactionRefusedBytes"], Buffer.byteLength(env3), "later larger refusal re-ratchets above the decayed floor"); + }); +}); From b7f40c2934d77d71cd878da7bae7caf25cbdae64 Mon Sep 17 00:00:00 2001 From: ci-check Date: Sat, 31 Oct 2026 22:34:33 +0800 Subject: [PATCH 2/2] docs: align BILI_DSH_AUTHORED_COMPACTION docstring with the ON default Drive-by found while working #5289: the docstring still said 'Default OFF' while the function has returned true by default since the #3723/#3901 flip (0.1.203). Doc/code mismatch corrected; no behavior change. --- src/knobs.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/knobs.ts b/src/knobs.ts index cd93f1a194..350469a99c 100644 --- a/src/knobs.ts +++ b/src/knobs.ts @@ -568,13 +568,13 @@ export function codexCompactMode(): CodexCompactMode { } /** BILI_DSH_AUTHORED_COMPACTION — #3733 bili-authored dsh client compaction. - * Default OFF ("0"/unset): the #1729 guard keeps refusing dsh's native - * compaction summarize calls, exactly as before. "1": when the session - * holds active fold blocks, the guard site answers the call LOCALLY with + * Default ON (the #3723/#3901 flip): when the session holds active fold + * blocks, the guard site answers dsh's native compaction call LOCALLY with * our folded snapshot (zero egress) instead of refusing — dsh records our - * checkpoint and its file shrinks (#3733/#3723). Read per-request so a - * running proxy flips without a restart. Env-only for the rollout window; - * the #2028-style file/web toggle follows once adopted. */ + * checkpoint and its file shrinks (#3733/#3723). "0"/false is the escape + * hatch back to the #1729 refusal path. Read per-request so a running proxy + * flips without a restart. Env-only for the rollout window; the #2028-style + * file/web toggle follows once adopted. */ export function dshAuthoredCompactionEnabled(): boolean { const envRaw = process.env.BILI_DSH_AUTHORED_COMPACTION; if (envRaw !== undefined) return envRaw.trim() !== "0";