Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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.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",
Expand Down
60 changes: 42 additions & 18 deletions kernel/src/compress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 : [];
}),
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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") {
Expand Down
44 changes: 29 additions & 15 deletions kernel/tests/protect-recent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
Expand Down
Loading
Loading