diff --git a/kernel/package-lock.json b/kernel/package-lock.json index 8a2c4491d7..7ecaea2d8c 100644 --- a/kernel/package-lock.json +++ b/kernel/package-lock.json @@ -1,12 +1,12 @@ { "name": "acp-kernel", - "version": "0.0.201", + "version": "0.0.202", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "acp-kernel", - "version": "0.0.201", + "version": "0.0.202", "license": "MIT", "devDependencies": { "@types/node": "^22.0.0", diff --git a/kernel/package.json b/kernel/package.json index 5ac694d897..80a65d212a 100644 --- a/kernel/package.json +++ b/kernel/package.json @@ -1,6 +1,6 @@ { "name": "acp-kernel", - "version": "0.0.201", + "version": "0.0.202", "description": "Framework-agnostic context-compression engine (model-driven, 3-tier LSM). Pure core: no host dependency.", "license": "MIT", "author": "ranxianglei", diff --git a/kernel/src/compress.ts b/kernel/src/compress.ts index 43a0238c6e..64065c0c57 100644 --- a/kernel/src/compress.ts +++ b/kernel/src/compress.ts @@ -390,8 +390,8 @@ export function createCore(ports: Ports = {}): CompressionCore { // no-overlapping-active-siblings invariant at the top of this file). let preExistingCoverage = collectCoverage(state); - // Classify every requested range ONCE. The result feeds overlap - // skipSpecs, the minCompressRange pre-check, and the per-range loop — + // Classify every requested range ONCE. The result feeds the overlap + // gate, the minCompressRange pre-check, and the per-range loop — // previously each re-resolved and silently swallowed failures, so // consumed/unknown ranges produced misleading "too small" errors. const classifications = new Map< @@ -526,27 +526,53 @@ export function createCore(ports: Ports = {}): CompressionCore { }); } const sortedRanges = [...rangeSpans].sort((a, b) => a.start - b.start); - // Overlapping ranges warn+skip (earliest wins) rather than aborting the - // whole batch — see ISSUE-42 / dog/billion-context-pi#21. - const skipSpecs = new Set<(typeof input.ranges)[number]>(); + // Overlapping ranges REJECT THE WHOLE BATCH atomically (#5541): a partial + // fold silently discarded the model's summary for the skipped range(s) and + // left cross-references inside the surviving block's summary dangling. The + // all-or-nothing contract #3003/#3939 established for every other failure + // class applies here too; the earlier ISSUE-42 warn+skip (earliest wins) + // predates that contract and is retired. Nesting counts as overlap. + type RangeSpec = (typeof input.ranges)[number]; + const overlapConflicts: { spec: RangeSpec; against: RangeSpec }[] = []; let acceptedMaxIndex = -1; + let acceptedAtMax: RangeSpec | null = null; for (const entry of sortedRanges) { - if (entry.start <= acceptedMaxIndex) { - skipSpecs.add(entry.spec); - warnings.push( - `Skipped range (${entry.spec.startRef}..${entry.spec.endRef}) — overlaps an earlier range in the batch; the earlier range takes precedence. Keep ranges disjoint.`, - ); + if (entry.start <= acceptedMaxIndex && acceptedAtMax !== null) { + overlapConflicts.push({ spec: entry.spec, against: acceptedAtMax }); continue; } - if (entry.end > acceptedMaxIndex) acceptedMaxIndex = entry.end; + if (entry.end > acceptedMaxIndex) { + acceptedMaxIndex = entry.end; + acceptedAtMax = entry.spec; + } + } + if (overlapConflicts.length > 0) { + const detail = overlapConflicts + .map( + (c) => + `(${c.spec.startRef}..${c.spec.endRef}) overlaps (${c.against.startRef}..${c.against.endRef})`, + ) + .join("; "); + return { + state, + result: { + blocksCreated: 0, + tokensCompressed: 0, + errors: [ + `Batch rejected before folding any range — ${overlapConflicts.length} of ${input.ranges.length} requested range(s) overlap another range in the same batch: ${detail}. Nothing was folded, so no prefix was rewritten. Re-issue the batch with disjoint ranges (no message may belong to two ranges; nesting counts as overlap).`, + ...classificationErrors, + ], + warnings: [], + }, + }; } if (input.config.compress.minCompressRange > 0 && input.ranges.length > 0) { let totalRangeChars = 0; let hasBlockBoundaryRange = false; let countedRanges = 0; - for (const [spec, resolution] of classifications) { - if (resolution.status !== "ok" || skipSpecs.has(spec)) continue; + for (const [, resolution] of classifications) { + if (resolution.status !== "ok") continue; if (resolution.resolved.boundaryKind === "block") { hasBlockBoundaryRange = true; continue; @@ -597,7 +623,7 @@ export function createCore(ports: Ports = {}): CompressionCore { const okBlockedReasons = [ ...new Set( [...classifications.entries()].flatMap(([spec, resolution]) => { - if (skipSpecs.has(spec) || resolution.status !== "ok") return []; + if (resolution.status !== "ok") return []; const decision = refoldDecisions.get(spec); return decision?.kind === "blocked" ? decision.reasons : []; }), @@ -645,8 +671,8 @@ export function createCore(ports: Ports = {}): CompressionCore { }; } const reversalNotes: string[] = []; - for (const [spec, resolution] of classifications) { - if (resolution.status === "ok" && !skipSpecs.has(spec)) { + for (const [, resolution] of classifications) { + if (resolution.status === "ok") { const note = resolution.resolved.reversedNote; if (note) reversalNotes.push(note); } @@ -718,7 +744,6 @@ export function createCore(ports: Ports = {}): CompressionCore { { const foldCandidates: ApplyCompressionInput["ranges"][number][] = []; for (const spec of input.ranges) { - if (skipSpecs.has(spec)) continue; const resolution = classifications.get(spec); if (resolution === undefined) continue; const foldsFresh = resolution.status === "ok"; @@ -815,7 +840,6 @@ export function createCore(ports: Ports = {}): CompressionCore { // the seam; the later one shrinks to its own new messages or becomes // the #4521 idempotent no-op. Cheap: active blocks × their id sets. preExistingCoverage = collectCoverage(state); - if (skipSpecs.has(spec)) continue; const resolution = classifications.get(spec); if (resolution === undefined) continue; if (resolution.status === "consumed") { diff --git a/kernel/tests/protect-recent.test.ts b/kernel/tests/protect-recent.test.ts index 91ee13e42e..42d3e08bb0 100644 --- a/kernel/tests/protect-recent.test.ts +++ b/kernel/tests/protect-recent.test.ts @@ -354,35 +354,49 @@ test("warnings accumulate across multiple ranges in one batch", () => { const state = seededState(messages); const cfg = config({ preserveRecentMessages: 3 }); - // Two ranges: the second now ends inside the recent zone, which under the - // #3939 all-or-nothing contract would reject the whole batch (a fully- - // protected range is a fatal apply error, pinned in issue3939-fold-boundary- - // pins.test.ts), so it instead OVERLAPS the first range's span — overlap - // warn+skip keeps the batch valid while still exercising multi-range - // warning accumulation. + // Pre-fold the head so one batch range is already consumed (skip-with- + // warning) while the other stays live and gets clipped by the recent zone + // (exclusion warning): two distinct per-range warnings in one valid batch. + // Overlapping ranges can no longer play this role — since #5541 an overlap + // rejects the whole batch atomically (pinned in regression-bugfixes.test.ts). + const setup = core.applyCompression({ + ranges: [ + { + startRef: "m00001", + endRef: "m00003", + summary: "head fold", + topic: "h", + }, + ], + messages, + state, + config: cfg, + }); + assert.equal(setup.result.blocksCreated, 1, "setup fold lands"); + const result = core.applyCompression({ ranges: [ + { + startRef: "m00001", + endRef: "m00003", + summary: "already folded head", + topic: "h2", + }, { startRef: "m00004", endRef: "m00009", - summary: "first partial range summary", + summary: "live range clipped by the recent zone", topic: "a", }, - { - startRef: "m00008", - endRef: "m00010", - summary: "second range overlaps the first", - topic: "b", - }, ], messages, - state, + state: setup.state, config: cfg, }); assert.ok( result.result.blocksCreated >= 1, - "at least the unprotected head compresses", + "the live range still compresses", ); assert.ok(result.result.warnings.length >= 2, "warnings surfaced"); }); diff --git a/kernel/tests/regression-bugfixes.test.ts b/kernel/tests/regression-bugfixes.test.ts index 7a6d011f3c..d02773c8ef 100644 --- a/kernel/tests/regression-bugfixes.test.ts +++ b/kernel/tests/regression-bugfixes.test.ts @@ -371,7 +371,7 @@ test("render-refs is idempotent across repeated renders", () => { ); }); -test("batch: overlapping ranges warn and skip (earliest wins) instead of failing", () => { +test("batch: overlapping ranges reject the whole batch atomically (#5541)", () => { const core = createCore(); const messages = [ msg("a", "x".repeat(2000)), @@ -394,20 +394,20 @@ test("batch: overlapping ranges warn and skip (earliest wins) instead of failing config: config(), }); - assert.equal( - result.result.errors.length, - 0, - "overlap is non-fatal — no errors", - ); - assert.equal( - result.result.blocksCreated, - 1, - "the earlier range is still compressed", + assert.equal(result.result.blocksCreated, 0, "nothing is folded"); + assert.equal(result.result.tokensCompressed, 0); + assert.equal(result.result.warnings.length, 0, "no partial-apply warning"); + assert.equal(result.result.errors.length, 1); + assert.match( + result.result.errors[0]!, + /Batch rejected before folding any range/, ); - assert.ok( - result.result.warnings.some((w) => /overlaps an earlier range/i.test(w)), - "a warning is recorded for the skipped range", + assert.match( + result.result.errors[0]!, + /\(m00002\.\.m00003\) overlaps \(m00001\.\.m00002\)/, + "the conflicting pair is named", ); + assert.equal(result.state.blocks.length, 0, "zero-commit purity"); }); test("batch: non-overlapping ranges all succeed", () => { @@ -438,7 +438,7 @@ test("batch: non-overlapping ranges all succeed", () => { assert.equal(result.result.errors.length, 0); }); -test("batch: disjoint range still compresses when two others overlap", () => { +test("batch: one overlapping pair rejects the whole batch, including the disjoint sibling (#5541)", () => { const core = createCore(); const messages = [ msg("a", "x".repeat(2000)), @@ -472,15 +472,30 @@ test("batch: disjoint range still compresses when two others overlap", () => { config: config(), }); - assert.equal(result.result.errors.length, 0); - assert.equal(result.result.blocksCreated, 2); - assert.ok( - result.result.warnings.some((w) => /overlaps an earlier range/i.test(w)), - "an overlap warning is recorded", + assert.equal( + result.result.blocksCreated, + 0, + "the disjoint sibling does not land either", + ); + assert.equal(result.result.warnings.length, 0); + assert.equal(result.result.errors.length, 1); + assert.match( + result.result.errors[0]!, + /1 of 3 requested range\(s\) overlap another range/, ); + assert.match( + result.result.errors[0]!, + /\(m00002\.\.m00003\) overlaps \(m00001\.\.m00002\)/, + ); + assert.doesNotMatch( + result.result.errors[0]!, + /m00004\.\.m00005/, + "the disjoint range is not blamed", + ); + assert.equal(result.state.blocks.length, 0, "zero-commit purity"); }); -test("batch: overlap resolved deterministically regardless of input order", () => { +test("batch: overlap rejection is deterministic regardless of input order (#5541)", () => { const core = createCore(); const messages = [ msg("a", "x".repeat(2000)), @@ -538,17 +553,107 @@ test("batch: overlap resolved deterministically regardless of input order", () = ["sorted", sorted], ["shuffled", shuffled], ] as const) { - assert.equal(result.result.errors.length, 0, `${label}: no errors`); + assert.equal(result.result.blocksCreated, 0, `${label}: nothing folded`); + assert.equal(result.result.warnings.length, 0, `${label}: no warnings`); assert.equal( - result.result.blocksCreated, - 2, - `${label}: earliest range + disjoint range compressed`, + result.result.errors.length, + 1, + `${label}: one atomic-rejection error`, ); - assert.ok( - result.result.warnings.some((w) => /overlaps an earlier range/i.test(w)), - `${label}: overlap warning present`, + assert.match( + result.result.errors[0]!, + /Batch rejected before folding any range/, ); } + assert.deepEqual( + shuffled.result.errors, + sorted.result.errors, + "the conflict list is identical regardless of input order", + ); +}); + +test("batch: a nested range rejects the whole batch as overlap (#5541)", () => { + const core = createCore(); + const messages = [ + msg("a", "x".repeat(2000)), + msg("b", "x".repeat(2000)), + msg("c", "x".repeat(2000)), + msg("d", "x".repeat(2000)), + msg("e", "x".repeat(2000)), + ]; + const state = createInitialState(); + state.messageRefs = assignRefs(messages, { + existing: state.messageRefs, + nextIndex: 1, + }).map; + + const result = core.applyCompression({ + ranges: [ + { startRef: "m00001", endRef: "m00005", summary: "outer summary" }, + { startRef: "m00003", endRef: "m00004", summary: "inner summary" }, + ], + messages, + state, + config: config(), + }); + + assert.equal(result.result.blocksCreated, 0); + assert.equal(result.result.errors.length, 1); + assert.match( + result.result.errors[0]!, + /\(m00003\.\.m00004\) overlaps \(m00001\.\.m00005\)/, + "the inner range is named against its containing outer range", + ); + assert.match( + result.result.errors[0]!, + /nesting counts as overlap/, + ); + assert.equal(result.state.blocks.length, 0, "zero-commit purity"); +}); + +test("batch: adjacent block-boundary distillation is not overlap (#5541 guard)", () => { + // Tier distillation over two sequential T1 blocks must still fold: block-ref + // spans are strictly sequential in message space and must not trip the gate. + const core = createCore(); + const messages = [ + msg("a", "x".repeat(2000)), + msg("b", "x".repeat(2000)), + msg("c", "x".repeat(2000)), + msg("d", "x".repeat(2000)), + ]; + const state = createInitialState(); + state.messageRefs = assignRefs(messages, { + existing: state.messageRefs, + nextIndex: 1, + }).map; + + const t1 = core.applyCompression({ + ranges: [ + { startRef: "m00001", endRef: "m00002", summary: "ab summary" }, + { startRef: "m00003", endRef: "m00004", summary: "cd summary" }, + ], + messages, + state, + config: config(), + }); + assert.equal(t1.result.blocksCreated, 2); + + const t2 = core.applyCompression({ + ranges: [ + { startRef: "b1", endRef: "b1", summary: "distilled ab" }, + { startRef: "b2", endRef: "b2", summary: "distilled cd" }, + ], + messages, + state: t1.state, + config: config(), + }); + + assert.equal( + t2.result.blocksCreated, + 2, + "adjacent block spans are not an overlap", + ); + assert.equal(t2.result.errors.length, 0); }); test("block-boundary distillation counts consumed block tokens", () => { diff --git a/package-lock.json b/package-lock.json index 72519f7f77..8f31611801 100644 --- a/package-lock.json +++ b/package-lock.json @@ -51,7 +51,7 @@ }, "kernel": { "name": "acp-kernel", - "version": "0.0.201", + "version": "0.0.202", "dev": true, "license": "MIT", "devDependencies": { diff --git a/src/external-summary-compress.ts b/src/external-summary-compress.ts index 004032b072..f1ffff58fe 100644 --- a/src/external-summary-compress.ts +++ b/src/external-summary-compress.ts @@ -106,7 +106,11 @@ export async function applyConfiguredCompression(input: unknown, ctx: RewriteCtx instructions: `${ctx.session.meta.summaryInstructions ?? buildCompressSystemPrompt(undefined, messageTimestampSections(undefined))}\nWrite a faithful summary of the selected range ${range.startRef}-${range.endRef}. Preserve task status, constraints, decisions, exact code identifiers, paths and errors, and unfinished objectives with their source message refs. Submitted summary hints are non-authoritative. Never summarize or change the read-only reference.` + summaryLengthBudgetClause(maxSummaryChars, ctx.config.compress.minSummaryLength) }); } + // #5541: when the whole batch was rejected atomically (overlapping ranges), + // surface the kernel's named conflict instead of the generic protection text. + const overlapError = preview.result.errors.find((error) => /overlap another range/i.test(error)); if (work.length === 0) { + if (overlapError) return compressResult(`[Compression FAILED: ${overlapError} Nothing compressed.]`, "refused", 0); // #5558: an unchanged preview can be an already-covered/protected no-op // or a kernel refusal; the normal path supplies that truthful receipt. // If a probe changed state but yielded no content, never fold a hint diff --git a/src/stream.ts b/src/stream.ts index b074b1d456..1df16fe1ce 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -1598,7 +1598,8 @@ export function applyRanges(parsed: ReturnType, ctx: const errs = r.errors.join("; ") || "no blocks created"; // #2362: machine-readable failure class for the [plugin] execution // line — outcome=refused alone never says why in the logs. - const gateReason = /cannot be anchored/.test(errs) ? "gate:cannot-anchor" + const gateReason = /overlap another range/i.test(errs) ? "gate:overlap" + : /cannot be anchored/.test(errs) ? "gate:cannot-anchor" : /already compressed/.test(errs) ? "gate:already-compressed" : /requested range\(s\) resolved/.test(errs) ? "gate:none-resolved" : /too small/.test(errs) ? "gate:too-small"