Skip to content
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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
4 changes: 2 additions & 2 deletions kernel/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion kernel/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "acp-kernel",
"version": "0.0.199",
"version": "0.0.200",
"description": "Framework-agnostic context-compression engine (model-driven, 3-tier LSM). Pure core: no host dependency.",
"license": "MIT",
"author": "ranxianglei",
Expand Down
2 changes: 2 additions & 0 deletions kernel/src/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,8 @@ function renderUncompressedRanges(
return "\u26a0 not foldable: protected zone only";
if (proj.fatal?.kind === "split")
return `\u26a0 not foldable: would split ${splitDescOf(proj)}`;
if (proj.fatal?.kind === "window")
return "\u26a0 not foldable: would merge block(s) beyond the requested ref window";
if (proj.filteredIds.length === 0 && proj.consumedBlockIds.length === 0)
return "\u26a0 not foldable: nothing new to fold";
return null;
Expand Down
93 changes: 92 additions & 1 deletion kernel/src/unit-projection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import {
buildIndexByMessageId,
resolveBoundaries,
blockVisibleInRange,
parseBoundary,
} from "./boundaries.js";
import { indexToRef, refToIndex } from "./refs.js";
import { isSummaryMessageId } from "./prune.js";
import {
collectFailedCompressCallIds,
Expand Down Expand Up @@ -91,7 +93,7 @@ export interface UnitProjectionInput {
}

export interface UnitProjectionFatal {
kind: "zone" | "split";
kind: "zone" | "split" | "window";
error: Error;
}

Expand Down Expand Up @@ -265,6 +267,95 @@ export function projectUnit(input: UnitProjectionInput): UnitProjection {
}
}

// #5830: the supplied summary describes exactly the requested ref window
// [startRef..endRef]. A plain message-ref range that would CONSUME an active
// block whose coverage extends outside that window replaces that block's
// wider-span summary with one that never saw part of it — silent
// active-summary coverage loss (incident: one tail-range summary left
// standing over a merged span back to the conversation start). Block-
// boundary merges (bN..bM) are exempt by contract — they exist to absorb
// the boundary blocks' coverage and the caller named the blocks. Pure
// resubmits (no new direct messages) are exempt too: they create no block,
// so there is no replacement to guard against (#4521 no-op class).
//
// #5531: pair/reasoning boundary widening is the fold's OWN defined scope —
// the widened cells ride under the same summary by design, so when both
// endpoints resolved IN PLACE (the requested message itself still occupies
// its slot), containment is judged against the WIDENED span, not the
// literal window. What must stay fatal is the #5830 producer itself: an
// endpoint that RESOLVED ELSEWHERE (stale inherited ref snapping to a
// distant inheriting-block anchor) sweeps territory the summary never
// described regardless of widening — hold the line at the requested
// window then. Coverage ids carrying NO ref entry (synthetic outbound-only
// markers, e.g. #2627 turn separators that legacy polluted views registered
// into block coverage) describe no client-side real-history content, so a
// replaced summary cannot lose anything they stand for — they ride along.
// A block whose ENTIRE coverage is unmappable still fails closed
// (containment unprovable).
if (!isBlockBoundary && consumedBlockIds.length > 0 && filteredIds.length > 0) {
const startBoundary = parseBoundary(input.startRef);
const endBoundary = parseBoundary(input.endRef);
if (startBoundary?.kind === "message" && endBoundary?.kind === "message") {
const windowLo = Math.min(startBoundary.numericId, endBoundary.numericId);
const windowHi = Math.max(startBoundary.numericId, endBoundary.numericId);
const startRaw = input.state.messageRefs.byRef[startBoundary.raw];
const endRaw = input.state.messageRefs.byRef[endBoundary.raw];
const resolvedEndpointIds = [
input.messages[resolved.startIndex]?.id,
input.messages[resolved.endIndex]?.id,
];
const resolutionInPlace =
startRaw !== undefined &&
endRaw !== undefined &&
resolvedEndpointIds.includes(startRaw) &&
resolvedEndpointIds.includes(endRaw);
let spanLo = windowLo;
let spanHi = windowHi;
if (resolutionInPlace) {
for (const id of rangeMessageIds) {
const ref = input.state.messageRefs.byRaw[id];
const num = ref === undefined ? null : refToIndex(ref);
if (num === null) continue;
if (num < spanLo) spanLo = num;
if (num > spanHi) spanHi = num;
}
}
const offenders: string[] = [];
for (const consumedId of consumedBlockIds) {
const block = blockById(input.state, consumedId);
if (!block || block.effectiveMessageIds.length === 0) continue;
let minNum: number | null = null;
let maxNum: number | null = null;
let outOfSpan = false;
for (const mid of block.effectiveMessageIds) {
const ref = input.state.messageRefs.byRaw[mid];
const num = ref === undefined ? null : refToIndex(ref);
if (num === null) continue;
if (num < spanLo || num > spanHi) outOfSpan = true;
if (minNum === null || num < minNum) minNum = num;
if (maxNum === null || num > maxNum) maxNum = num;
}
if (minNum === null || maxNum === null) {
offenders.push(`${consumedId} [unmapped coverage]`);
} else if (outOfSpan) {
offenders.push(`${consumedId} [${indexToRef(minNum)}..${indexToRef(maxNum)}]`);
}
}
if (offenders.length > 0) {
return finish({
fatal: {
kind: "window",
error: new Error(
resolutionInPlace
? `Range ${input.startRef}..${input.endRef} would merge ${offenders.join(", ")}, whose existing coverage lies outside the fold's span (${indexToRef(spanLo)}..${indexToRef(spanHi)}) — folding them would replace their summaries with one that does not cover their span. To merge them deliberately, reference the block ids directly (e.g. compress bN..bM); otherwise narrow the range.`
: `Range ${input.startRef}..${input.endRef} would merge ${offenders.join(", ")}, whose existing coverage extends beyond the requested ref window (${indexToRef(windowLo)}..${indexToRef(windowHi)}) — folding them would replace their summaries with one that does not cover their span. To merge them deliberately, reference the block ids directly (e.g. compress bN..bM); otherwise narrow the range so its endpoints bound the blocks' coverage.`,
),
},
});
}
}
}

const mediaExcluded = directMessageIds.filter((id) => {
const msg = msgById(id);
return !!msg && hasUnrecoverableMediaPayload(msg);
Expand Down
174 changes: 174 additions & 0 deletions kernel/tests/issue5830-merge-window-guard.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
import { describe, it } from "node:test";
import assert from "node:assert/strict";
import { createCore } from "../src/compress.js";
import {
blockById,
coveredMessageIds,
createInitialState,
} from "../src/state.js";
import { defaultConfig } from "../src/config.js";
import { summaryMessageId } from "../src/prune.js";
import type { CompressionState, CoreMessage } from "../src/types.js";

// #5830: a plain message-ref range must never merge an active block whose
// coverage extends beyond the requested ref window [startRef..endRef] — the
// supplied summary describes exactly that window, so such a merge replaces
// the block's wider-span summary with one that never saw part of it (the
// incident left b27 covering 1566 messages with only the last range's
// summary; prior blocks silently deactivated).
//
// Fixture = a reachable production sequence (full-resend host; the plugin
// lane judges on raw history per #3236's applyView): stage 1 builds two
// active blocks with OVERLAPPING coverage (a full-raw fold starting inside
// an existing T2's territory); stage 2 fires a range whose stale inherited
// startRef snaps to the T2's head anchor while its end is fresh, so the
// resolved span sweeps the overlapping T1 below the requested window.

const LONG =
"Synthetic historical content, safe for public reproduction. ".repeat(150);
const sum = (label: string) =>
`${label}: synthetic stage summary capturing the folded range. `.padEnd(72, ".");
const config = defaultConfig(255000, {
preserveRecentMessages: 0,
preserveRecentTokens: 0,
} as never);

function mkMsg(id: string): CoreMessage {
return { id, role: "user", contentType: "text", text: LONG };
}

type Ranges = Parameters<ReturnType<typeof createCore>["applyCompression"]>[0]["ranges"];

function buildStage1() {
const core = createCore();
const messages: CoreMessage[] = [];
const initial = createInitialState();
for (let i = 1; i <= 14; i++) {
const id = `u${String(i).padStart(2, "0")}`;
messages.push(mkMsg(id));
const ref = `m${String(i).padStart(5, "0")}`;
initial.messageRefs.byRaw[id] = ref;
initial.messageRefs.byRef[ref] = id;
}
let state: CompressionState = initial;
const fold = (ranges: Ranges) => {
const res = core.applyCompression({ messages, state, config, ranges });
if (res.result.errors.length !== 0) {
throw new Error(`stage-1 fold failed: ${JSON.stringify(res.result.errors)}`);
}
state = res.state;
return res;
};
fold([{ startRef: "m00001", endRef: "m00002", summary: sum("b1") }]);
fold([{ startRef: "m00003", endRef: "m00004", summary: sum("b2") }]);
fold([{ startRef: "b1", endRef: "b2", summary: sum("b3-distillation") }]);
const b3 = blockById(state, "b3");
assert.equal(b3?.tier, 2, "distillation must produce a T2");
assert.ok(b3?.active === true, "b3 must stay active");
// Full-raw fold over the T2's territory: b3's anchor sits inside the span
// but b3 is T2 (target tier of a plain range is 1), so it survives — b4
// ends up covering [u01..u12] and OVERLAPS b3's [u01..u04]. Both stay
// active: the stage-1 overlap product.
fold([{ startRef: "m00001", endRef: "m00012", summary: sum("b4-fullraw") }]);
const b4 = blockById(state, "b4");
assert.equal(b4?.tier, 1, "plain fold must produce a T1");
assert.ok(
b4?.active === true && b3.active === true,
"stage 1 must leave both blocks active with overlapping coverage",
);
return { core, messages, state };
}

/** The pruned view a normal host sends after stage 1: the two rendered
* summaries at their head anchors, then the fresh uncovered tail. */
function prunedView(messages: CoreMessage[]): CoreMessage[] {
return [
{
id: summaryMessageId("b3"),
role: "system",
contentType: "text",
text: "[Compressed conversation section] S-b3",
},
{
id: summaryMessageId("b4"),
role: "system",
contentType: "text",
text: "[Compressed conversation section] S-b4",
},
messages[12]!,
messages[13]!,
];
}

function lostCoverage(before: Set<string>, state: CompressionState): string[] {
const after = coveredMessageIds(state);
return [...before].filter((id) => !after.has(id));
}

describe("#5830 plain-range merge window guard", () => {
it("stale-inherited start + fresh end: a sweep that swallows an out-of-window block is rejected atomically with zero commits", () => {
const { core, messages, state } = buildStage1();
const before = coveredMessageIds(state);
const res = core.applyCompression({
messages: prunedView(messages),
state,
config,
ranges: [{ startRef: "m00002", endRef: "m00014", summary: sum("tail-only") }],
});
assert.equal(res.result.blocksCreated, 0, JSON.stringify(res.result));
assert.equal(res.result.errors.length, 1, JSON.stringify(res.result.errors));
assert.match(
res.result.errors[0]!,
/extends beyond the requested ref window/i,
);
assert.match(res.result.errors[0]!, /b4/, "the offender block must be named");
assert.deepEqual(lostCoverage(before, res.state), [], "no coverage may be lost");
assert.ok(blockById(res.state, "b4")!.active, "b4 must stay active");
assert.ok(blockById(res.state, "b3")!.active, "b3 must stay active");
});

it("control: a plain-range merge whose window CONTAINS the consumed block's coverage still lands", () => {
const { core, messages, state } = buildStage1();
const res = core.applyCompression({
messages: prunedView(messages),
state,
config,
ranges: [{ startRef: "m00001", endRef: "m00014", summary: sum("legit-wide") }],
});
assert.equal(res.result.errors.length, 0, JSON.stringify(res.result.errors));
assert.equal(res.result.blocksCreated, 1, JSON.stringify(res.result));
assert.equal(
blockById(res.state, "b4")!.active,
false,
"b4 [u01..u12] lies inside window [1..14] — legitimate absorption",
);
});

it("control: a fully-covered resubmit changes nothing — no block, no coverage loss (pre-fix: refold-guidance error, NOT a silent merge)", () => {
const { core, messages, state } = buildStage1();
const before = coveredMessageIds(state);
const res = core.applyCompression({
messages: prunedView(messages),
state,
config,
ranges: [{ startRef: "m00002", endRef: "m00004", summary: sum("resubmit") }],
});
assert.equal(res.result.blocksCreated, 0, JSON.stringify(res.result));
assert.deepEqual(lostCoverage(before, res.state), []);
assert.ok(
blockById(res.state, "b4")!.active && blockById(res.state, "b3")!.active,
);
});

it("control: block-boundary distillation across the same blocks is untouched by the gate", () => {
const { core, messages, state } = buildStage1();
const res = core.applyCompression({
messages: prunedView(messages),
state,
config,
ranges: [{ startRef: "b3", endRef: "b4", summary: sum("distill-mixed") }],
});
assert.equal(res.result.errors.length, 0, JSON.stringify(res.result.errors));
assert.equal(res.result.blocksCreated, 1, JSON.stringify(res.result));
});
});
Loading
Loading