diff --git a/src/stream.ts b/src/stream.ts index 8325a875b0..5ec012d764 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -767,6 +767,41 @@ export function applyRanges(parsed: ReturnType, ctx: const beforeIds = new Set(ctx.session.state.blocks.map((b) => b.blockId)); const beforeSummaries = new Map(ctx.session.state.blocks.map((b) => [b.blockId, b.summary] as const)); const r = res.result; + // #3322: coverage-monotonicity backstop. A committed fold must never + // leave a previously-covered message without ANY covering block: on + // full-resend hosts the kernel's turn-integrity gate once judged splits + // against the fold's own set instead of post-fold coverage, so ids + // carried by consumed blocks were withdrawn wholesale — the processed + // view doubled (253→519), the local estimate blew past the window and + // preflight fail-fasted forever while upstream had headroom. Measure + // active-block coverage before vs after on the clone; any loss means + // this fold GROWS the live view, refuse it before commit (state and + // prefix cache untouched). Healthy folds never lose coverage: fresh + // folds add it, refolds preserve it, integrity withdrawals only touch + // direct ids of the range being folded. + { + const coveredAfter = new Set(); + for (const b of res.state.blocks) { + if (!b.active) continue; + for (const id of b.effectiveMessageIds) coveredAfter.add(id); + } + const lostCoverage = new Set(); + for (const b of ctx.session.state.blocks) { + if (!b.active) continue; + for (const id of b.effectiveMessageIds) { + if (!coveredAfter.has(id)) lostCoverage.add(id); + } + } + if (lostCoverage.size > 0) { + const detail = ranges.map((rg) => `${rg.startRef}–${rg.endRef}`).join(", "); + const sample = [...lostCoverage].slice(0, 5).map((id) => saltedMsgIdForLog(id)).join(", "); + const specKey = normalizedSpecKey(ranges); + const repeatGuard = recordCompressFailure(ctx.session, specKey); + ctx.log(`[acp-proxy: compress REFUSED ${detail} — coverage regression: ${lostCoverage.size} previously-folded message(s) would lose ALL covering block(s) (${sample}); committing it grows the live view and can push the local estimate past the model window (#3322). State left uncommitted, prefix cache untouched.]`); + const receipt = `[Compression refused: coverage regression — folding these ranges would leave ${lostCoverage.size} already-folded message(s) without any covering block, so the live context view would GROW instead of shrink (and the size estimate can blow past the model window, bricking the session behind permanent preflight rejections). Nothing was folded: no block was created and the context prefix/cache were left untouched. Retry with ranges aligned to whole turns (do not cut through a reasoning/tool-call/result group), target fewer blocks, or continue your task without compressing.${repeatGuard !== "" ? " " + repeatGuard : ""}]`; + return compressResult(receipt, "refused", 0, "gate:coverage-regression"); + } + } // #2748: host-side image notes append a ref-carrying footer to a new // block's summary AFTER the kernel's length validation — count that // wrapped mass in the net-benefit decision too. Moved above the commit diff --git a/tests/issue3322-coverage-regression-guard.test.ts b/tests/issue3322-coverage-regression-guard.test.ts new file mode 100644 index 0000000000..2c25a13769 --- /dev/null +++ b/tests/issue3322-coverage-regression-guard.test.ts @@ -0,0 +1,120 @@ +import assert from "node:assert/strict"; +import { randomUUID } from "node:crypto"; +import { afterEach, test } from "node:test"; +import { createCore, defaultConfig, type Config, type CoreMessage } from "acp-kernel"; +import { applyRanges } from "../src/stream.ts"; +import { parseCompressInput } from "../src/compress-tool.ts"; +import { getSession } from "../src/session.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _resetForTest as resetRegistryForTest } from "../src/registry.ts"; + +process.env.NODE_ENV = "test"; +_setStoreForTest(new SessionStore({ enabled: false })); + +afterEach(() => { + resetRegistryForTest(); +}); + +// #3322: a committed fold must never leave a previously-covered message +// WITHOUT ANY covering block. On full-resend hosts (dsh plugin lane compresses +// against the raw history, #2627) the kernel's turn-integrity gate once judged +// splits against the fold's OWN set instead of post-fold coverage: turns whose +// pieces sat in non-consumed sibling blocks were misread as split, their +// inherited ids withdrew wholesale, the consumed blocks deactivated — ~305 +// messages lost ALL coverage, the processed view doubled (253→519), the local +// estimate blew past the model window and preflight fail-fasted forever while +// upstream had headroom. applyRanges now measures active-block coverage before +// vs after on the clone and refuses the commit when it shrank (state and prefix +// cache untouched). The kernel root fix lands separately (Kernel Boundary); +// this guard is the host-side backstop pinning the commit contract either way. + +function makeCtx(prefix: string) { + const core = createCore(); + const config = defaultConfig(200_000) as Config; + config.compress.maxSummaryLength = 0; + // Fold ANY message: the guard under test must not depend on where the + // token-based recent-zone boundary falls inside this small fixture. + config.preserveRecentMessages = 0; + config.preserveRecentTokens = 0; + const session = getSession(`issue3322-${prefix}-${randomUUID()}`); + const msgs: CoreMessage[] = []; + for (let i = 0; i < 12; i++) { + msgs.push({ + id: `h_${i}`, + role: i % 2 === 0 ? "user" : "assistant", + contentType: "text", + text: `\x3cacp tokens="2K" type="text"\x3em${String(i + 1).padStart(5, "0")}\x3c/acp\x3e\nHistorical detail ${i}. ${"x".repeat(3000)}`, + }); + } + const turn = core.processTurn({ messages: msgs, state: session.state, config, tokenCount: 9999, renderTags: "text-only" }); + session.state = turn.state; + const logs: string[] = []; + const ctx = { core, config, messages: turn.messages, session, log: (msg: string) => { logs.push(msg); } }; + return { core, session, logs, ctx }; +} + +function coveredIds(state: { blocks: Array<{ active: boolean; effectiveMessageIds: string[] }> }): Set { + const out = new Set(); + for (const b of state.blocks) { + if (!b.active) continue; + for (const id of b.effectiveMessageIds) out.add(id); + } + return out; +} + +test("#3322 a fold that regresses coverage is refused BEFORE commit (state + cache untouched)", () => { + const { core, session, logs, ctx } = makeCtx("guard"); + + // Land a real fold first: b1 covers h_0/h_1. + const first = applyRanges(parseCompressInput({ content: [{ startId: "m00001", endId: "m00002", summary: "First summary: messages 1-2 covered the initial phase in detail." }] }), ctx as never); + assert.equal(first.outcome, "applied", `base fold must land: ${first.text}`); + const source = session.state.blocks.find((b) => b.active); + assert.ok(source, "the base fold materialized a block"); + assert.deepEqual([...coveredIds(session.state)].sort(), ["h_0", "h_1"]); + + // Simulate the incident on the NEXT fold: the kernel hands back a state in + // which the consuming block deactivated while the NEW block lost every + // inherited id (the pre-fix integrity gate's withdrawal shape). The guard + // must catch ANY such regression regardless of producer. + const realApply = core.applyCompression.bind(core); + core.applyCompression = (args) => { + const out = realApply(args); + const beforeIds = new Set(session.state.blocks.map((b) => b.blockId)); + for (const b of out.state.blocks) { + if (!beforeIds.has(b.blockId)) b.effectiveMessageIds = []; + } + return out; + }; + + const stateRefBefore = session.state; + const blocksBefore = session.state.blocks.length; + const distill = applyRanges(parseCompressInput({ content: [{ startId: source!.blockId, endId: source!.blockId, summary: "Distilled: the initial phase details merged into the running context." }] }), ctx as never); + assert.equal(distill.outcome, "refused", `a coverage-regressing fold must be refused: ${distill.text}`); + assert.equal(distill.reason, "gate:coverage-regression", `machine-readable reason: ${distill.reason}`); + assert.equal(distill.blocksCreated, 0, "no block is reported as created"); + assert.match(distill.text, /Compression refused: coverage regression/, `refusal is explicit: ${distill.text}`); + assert.match(distill.text, /Nothing was folded/, "the receipt states nothing was folded"); + assert.ok(!distill.text.includes("[Compressed "), `no fake success line: ${distill.text.slice(0, 120)}`); + assert.equal(session.state.blocks.length, blocksBefore, "no new block materialized"); + assert.equal(session.state, stateRefBefore, "state object uncommitted — prefix cache left untouched"); + assert.deepEqual([...coveredIds(session.state)].sort(), ["h_0", "h_1"], "previously-folded messages stay covered"); + assert.ok(logs.some((line) => line.includes("compress REFUSED") && line.includes("coverage regression")), "the refusal reaches the server log copy"); +}); + +test("#3322 legitimate nested consumption still applies (no false rejection)", () => { + const { session, ctx } = makeCtx("healthy"); + + const first = applyRanges(parseCompressInput({ content: [{ startId: "m00001", endId: "m00002", summary: "First summary: messages 1-2 covered the initial phase in detail." }] }), ctx as never); + assert.equal(first.outcome, "applied", `base fold must land: ${first.text}`); + + // Production shape of the incident batch: a later distillation whose span + // crosses the existing block's anchor consumes it — the new block INHERITS + // the old coverage, so the guard must let it through. + const second = applyRanges(parseCompressInput({ content: [{ startId: "m00001", endId: "m00006", summary: "Distilled span 1-6: initial phase plus the following exchange, details merged." }] }), ctx as never); + assert.equal(second.outcome, "applied", `nested consumption must not be refused: ${second.text}`); + assert.ok((second.blocksCreated ?? 0) >= 1, `a block landed: ${second.text}`); + const covered = coveredIds(session.state); + for (const id of ["h_0", "h_1", "h_2", "h_3", "h_4", "h_5"]) { + assert.ok(covered.has(id), `${id} must stay covered after the nested fold`); + } +});