Skip to content
Merged
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.130",
"version": "0.0.131",
"description": "Framework-agnostic context-compression engine (model-driven, 3-tier LSM). Pure core: no host dependency.",
"license": "MIT",
"author": "ranxianglei",
Expand Down
47 changes: 43 additions & 4 deletions kernel/src/boundaries.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { activeBlocks, blockById } from "./state.js";
import { isRenderedSummaryMessage, summaryMessageId } from "./prune.js";
import { isRetrievedMessage } from "./ccr.js";
import { BLOCKED_REF, refToIndex } from "./refs.js";
import { BLOCKED_REF, highestUsedIndex, refToIndex } from "./refs.js";
import type {
CompressionBlock,
CompressionState,
Expand Down Expand Up @@ -178,14 +178,54 @@ function resolveAnchorIndex(
): AnchorResolution {
const label = endpoint === "start" ? "startId" : "endId";
if (boundary.kind === "message") {
const paddedRef = formatPaddedRef(boundary.numericId);
const rawId =
state.messageRefs.byRef[boundary.raw] ??
state.messageRefs.byRef[formatPaddedRef(boundary.numericId)];
state.messageRefs.byRef[paddedRef];
if (!rawId) {
// #3302: a dead-ref tombstone means this number WAS allocated here and its
// message was later pruned or removed (host-side tombstones, e.g. the
// native-compaction archive prune) — anchor to the nearest visible
// neighbor instead of mislabeling the gap "typo or wrong session" and
// sending the model probing upward for a valid number. Kernel-minted dead
// refs (#2362) keep their byRef entry and resolve through the branches
// below, so only host-pruned refs reach this path.
if (state.deadRefs?.includes(paddedRef)) {
const neighbor = snapToNeighborVisible(
state,
indexByMessageId,
boundary.numericId,
endpoint,
);
if (neighbor !== null) {
return {
index: neighbor,
snapped: `${label}="${boundary.raw}" no longer backs any visible message (its content was pruned or removed by the client) — anchored to the nearest visible message instead, so the range folds slightly less than requested.`,
};
}
throw new BoundaryNotFoundError(
"consumed",
endpoint,
`${label}="${boundary.raw}" is a dead ref (its message was pruned or removed by the client) and no adjacent visible message exists to anchor to — nothing left to fold here. run acp_status for live refs.`,
);
}
// #3302: split the unknown case into two honest ones. Refs are minted in
// order and never pre-allocated, so a number strictly ABOVE the session's
// highest allocated ref cannot exist here (stale artifact from an earlier
// generation or a typo); a gap at or below the highest is a sparse-space
// hole (pruned or skipped) and must not be read as cross-session.
const highest = highestUsedIndex(state.messageRefs);
if (boundary.numericId > highest) {
throw new BoundaryNotFoundError(
"unknown",
endpoint,
`${label}="${boundary.raw}" is above this session's highest allocated ref (${highest > 0 ? formatPaddedRef(highest) : "none"}) — refs are minted in order and never pre-allocated, so it cannot exist in this session (stale artifact from an earlier generation, or a typo). run acp_status for current refs.`,
);
}
throw new BoundaryNotFoundError(
"unknown",
endpoint,
`${label}="${boundary.raw}" does not exist in this session (typo or wrong session) — run acp_status for current refs.`,
`${label}="${boundary.raw}" was never allocated in this session or was pruned after allocation — the ref space is sparse, so a number at or below the highest is not necessarily present; this is not a cross-session ref. run acp_status for current refs.`,
);
}
const index = indexByMessageId.get(rawId);
Expand All @@ -199,7 +239,6 @@ function resolveAnchorIndex(
snapped: `${label}="${boundary.raw}" refers to a message already compressed into an active block — anchored to the active block covering it instead.`,
};
}
const paddedRef = formatPaddedRef(boundary.numericId);
if (state.hiddenOrphanRefs?.includes(paddedRef)) {
const neighbor = snapToNeighborVisible(
state,
Expand Down
31 changes: 28 additions & 3 deletions kernel/src/compress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,10 @@ function refGateDiagnostics(
): string {
const highest = highestUsedIndex(state.messageRefs);
const highestRef = highest > 0 ? indexToRef(highest) : "none";
return `[diagnostics: session highest ref=${highestRef}, unknown ranges in request=${unknownCount}/${requestedRanges}, session history=${state.stats.compressionCount} compression(s), ${state.blocks.length} block(s)]`;
// #3302: the bare "highest ref" read as "valid numbers live below here" and
// sent models probing downward/upward one number at a time (each probe a
// full round). State the sparse-space fact instead.
return `[diagnostics: session highest allocated ref=${highestRef} (the ref space is sparse — numbers at or below it are NOT necessarily allocated, so probing numbers will not find a valid one; use only refs acp_status lists), unknown ranges in request=${unknownCount}/${requestedRanges}, session history=${state.stats.compressionCount} compression(s), ${state.blocks.length} block(s)]`;
}

function danglingMessageRefs(
Expand All @@ -180,7 +183,18 @@ function danglingMessageRefs(
const rawId =
state.messageRefs.byRef[parsed.raw] ??
state.messageRefs.byRef[indexToRef(parsed.numericId)];
if (!rawId || visible.has(rawId)) continue;
if (!rawId) {
// #3302: a host-pruned byRef entry leaves no rawId to inspect, but a
// dead-ref tombstone proves the number WAS allocated here and its
// message was later pruned — the same "no longer backs any message"
// fact, so it counts as dangling (DEAD) instead of vanishing.
const padded = indexToRef(parsed.numericId);
if (state.deadRefs?.includes(parsed.raw) || state.deadRefs?.includes(padded)) {
dangling.push(padded);
}
continue;
}
if (visible.has(rawId)) continue;
const covered = state.blocks.some(
(block) => block.active && block.effectiveMessageIds.includes(rawId),
);
Expand Down Expand Up @@ -522,11 +536,22 @@ export function createCore(ports: Ports = {}): CompressionCore {
const danglingRefs = consumedRanges.flatMap(
(spec) => danglingBySpec.get(spec) ?? [],
);
// #3302: name the failing endpoints — "unknown ranges in request=1/1"
// said nothing about WHICH side failed, so the model kept re-probing
// the same doomed endId against fresh startIds.
const unknownEndpoints = new Set<string>();
for (const [spec, resolution] of classifications) {
if (resolution.status !== "unknown") continue;
const endpoint = resolution.error.endpoint;
unknownEndpoints.add(
`${endpoint}Id=${endpoint === "start" ? spec.startRef : spec.endRef}`,
);
}
let gateMessage =
resolvableCount === 0 &&
consumedRanges.length === 0 &&
unknownCount > 0
? `None of the ${input.ranges.length} requested range(s) resolved — every ref is unknown to this session. Refs are per-session snapshots, assigned once when a message is first rendered; no compress reassigns them, so unknown refs cannot come from an earlier compress in this session. They come from a different generation: a previous session instance (switching model or upstream mid-conversation starts a fresh session whose refs restart at m00001), the generation before a native-compaction rebase (which also resets refs to m00001), or a typo. ${diagnostics} Run acp_status, then call the compress tool again using only the refs it reports.`
? `None of the ${input.ranges.length} requested range(s) resolved — every ref is unknown to this session${unknownEndpoints.size > 0 ? `, failing endpoints: ${[...unknownEndpoints].join(", ")}` : ""}. Refs are per-session snapshots, assigned once when a message is first rendered; no compress reassigns them. An unknown ref is usually a stale artifact from an earlier generation (switching model or upstream mid-conversation starts a fresh session whose refs restart at m00001, and a native-compaction rebase resets them the same way) or a typo — but a number at or below the highest allocated ref may also simply be an unallocated gap: the ref space is sparse. ${diagnostics} Do not probe for valid numbers. Run acp_status, then call the compress tool again using only the refs it reports.`
: consumedRanges.length > 0
? danglingRefs.length > 0
? `Requested range(s) cannot be anchored (e.g. ${firstConsumed!.startRef}..${firstConsumed!.endRef}): refs ${danglingRefs.join(", ")} are known to this session but no longer back any visible or folded message — the client rewrote or dropped those messages (an edit reissues a new ref; host-native compaction or a bulk history rewrite drops them outright), and no active block covers them. These refs are now recorded as DEAD: no range that includes them can compress now or later, whatever the neighbors. Do not retry this range in any form — run acp_status and target only the live refs it reports. ${diagnostics}`
Expand Down
48 changes: 37 additions & 11 deletions kernel/tests/compress.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ test("consumed plus fresh-but-small range is not misreported as too small", () =
);
assert.match(
retry.result.errors[0]!,
/\[diagnostics: session highest ref=m00005, unknown ranges in request=0\/2, session history=1 compression\(s\), 1 block\(s\)\]/,
/\[diagnostics: session highest allocated ref=m00005 \(the ref space is sparse.*?\), unknown ranges in request=0\/2, session history=1 compression\(s\), 1 block\(s\)\]/,
);
assert.doesNotMatch(retry.result.errors[0]!, /renumber/i);
assert.match(retry.result.errors[0]!, /[Rr]un acp_status/);
Expand Down Expand Up @@ -680,19 +680,38 @@ test("all-unknown batch reports stale refs instead of too-small (billion-context
/None of the 2 requested range\(s\) resolved/,
);
assert.match(result.result.errors[0]!, /no compress reassigns them/);
// #3302: the old text asserted unknown refs "cannot come from an earlier
// compress" — false for sparse spaces where pruned numbers sit below the
// highest. The new text names the failing endpoints and drops the claim.
assert.match(
result.result.errors[0]!,
/failing endpoints: startId=m00050, startId=m00070/,
);
assert.doesNotMatch(
result.result.errors[0]!,
/cannot come from an earlier compress in this session/,
);
assert.match(
result.result.errors[0]!,
/\[diagnostics: session highest ref=m00005, unknown ranges in request=2\/2, session history=0 compression\(s\), 0 block\(s\)\]/,
/stale artifact from an earlier generation/,
);
assert.match(result.result.errors[0]!, /the ref space is sparse/);
assert.match(result.result.errors[0]!, /Do not probe for valid numbers/);
assert.match(
result.result.errors[0]!,
/\[diagnostics: session highest allocated ref=m00005 \(the ref space is sparse.*?\), unknown ranges in request=2\/2, session history=0 compression\(s\), 0 block\(s\)\]/,
);
assert.doesNotMatch(result.result.errors[0]!, /renumber/i);
assert.match(result.result.errors[0]!, /Run acp_status/);
assert.doesNotMatch(result.result.errors[0]!, /too small/);
assert.match(result.result.errors[1]!, /does not exist in this session/);
assert.match(result.result.errors[2]!, /does not exist in this session/);
assert.match(
result.result.errors[1]!,
/startId="m00050" is above this session's highest allocated ref \(m00005\)/,
);
assert.match(
result.result.errors[2]!,
/startId="m00070" is above this session's highest allocated ref \(m00005\)/,
);
});

test("consumed plus unknown ranges keep the already-compressed message", () => {
Expand Down Expand Up @@ -742,7 +761,7 @@ test("consumed plus unknown ranges keep the already-compressed message", () => {
);
assert.match(
retry.result.errors.find((e) => e.startsWith("range m00050..m00060")) ?? "",
/does not exist in this session/,
/startId="m00050" is above this session's highest allocated ref/,
);
});

Expand Down Expand Up @@ -774,18 +793,22 @@ test("first compress of a fresh session with foreign refs reports a new generati
result.result.errors[0]!,
/None of the 1 requested range\(s\) resolved/,
);
assert.match(
assert.match(result.result.errors[0]!, /failing endpoints: startId=m00050/);
assert.doesNotMatch(
result.result.errors[0]!,
/cannot come from an earlier compress in this session/,
);
assert.match(result.result.errors[0]!, /switching model or upstream/);
assert.match(result.result.errors[0]!, /native-compaction rebase/);
assert.match(
result.result.errors[0]!,
/\[diagnostics: session highest ref=m00003, unknown ranges in request=1\/1, session history=0 compression\(s\), 0 block\(s\)\]/,
/\[diagnostics: session highest allocated ref=m00003 \(the ref space is sparse.*?\), unknown ranges in request=1\/1, session history=0 compression\(s\), 0 block\(s\)\]/,
);
assert.doesNotMatch(result.result.errors[0]!, /renumber/i);
assert.match(result.result.errors[1]!, /does not exist in this session/);
assert.match(
result.result.errors[1]!,
/startId="m00050" is above this session's highest allocated ref \(m00003\)/,
);
});

test("refs from before a native-compaction rebase report a new generation with zeroed history (billion-context#387)", () => {
Expand Down Expand Up @@ -845,7 +868,7 @@ test("refs from before a native-compaction rebase report a new generation with z
assert.match(result.result.errors[0]!, /refs restart at m00001/);
assert.match(
result.result.errors[0]!,
/\[diagnostics: session highest ref=m00002, unknown ranges in request=1\/1, session history=0 compression\(s\), 0 block\(s\)\]/,
/\[diagnostics: session highest allocated ref=m00002 \(the ref space is sparse.*?\), unknown ranges in request=1\/1, session history=0 compression\(s\), 0 block\(s\)\]/,
);
assert.doesNotMatch(result.result.errors[0]!, /renumber/i);
});
Expand Down Expand Up @@ -898,7 +921,7 @@ test("drifted message content dangles the old ref — reported as unanchorable,
assert.deepEqual(result.state.deadRefs, ["m00005"]);
assert.match(
result.result.errors[0]!,
/\[diagnostics: session highest ref=m00006, unknown ranges in request=0\/1, session history=0 compression\(s\), 0 block\(s\)\]/,
/\[diagnostics: session highest allocated ref=m00006 \(the ref space is sparse.*?\), unknown ranges in request=0\/1, session history=0 compression\(s\), 0 block\(s\)\]/,
);
assert.doesNotMatch(result.result.errors[0]!, /already compressed/);
assert.doesNotMatch(result.result.errors[0]!, /renumber/i);
Expand Down Expand Up @@ -1054,7 +1077,10 @@ test("unknown ref (valid format, never allocated) names the ref and suggests acp

assert.equal(result.result.blocksCreated, 0);
assert.equal(result.result.errors.length, 1);
assert.match(result.result.errors[0]!, /does not exist in this session/);
assert.match(
result.result.errors[0]!,
/startId="m00099" is above this session's highest allocated ref \(m00001\)/,
);
assert.match(result.result.errors[0]!, /run acp_status/);
});

Expand Down
Loading
Loading