From 6e5b7ab1bde29d471fff572aefdf7645805e58c6 Mon Sep 17 00:00:00 2001 From: awork Date: Sat, 17 Oct 2026 17:07:59 +0800 Subject: [PATCH 1/3] kernel: unknown-ref errors now distinguish sparse gaps from stale generations, name failing endpoints (#3302) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - resolveAnchorIndex: a byRef miss at/below the highest allocated ref is reported as an unallocated-or-pruned sparse-space hole, not '(typo or wrong session)'; above the highest it names the frontier - tombstoned dead refs (state.deadRefs) resolve by snapping to the nearest visible message instead of failing outright; no neighbor => honest consumed/DEAD classification - all-unknown gate names each failing endpoint (startId=/endId= per range) and stops inviting numeric probing; diagnostics state the ref space is sparse - danglingMessageRefs counts tombstoned endpoints as dangling so the #2362 DEAD path covers host-pruned refs kernel-change: #3302 — host-side pruning (session.ts pruneRefsToLiveIds) makes the ref space sparse, but kernel error semantics assumed absence means cross-session/typo; models then probed endIds one number at a time, each probe a full round. Host alone cannot fix the error semantics or the gate wording. Bumps acp-kernel to 0.0.128. --- kernel/package-lock.json | 4 +- kernel/package.json | 2 +- kernel/src/boundaries.ts | 47 ++++- kernel/src/compress.ts | 31 ++- kernel/tests/compress.test.ts | 48 +++-- .../issue3302-sparse-ref-unknown.test.ts | 185 ++++++++++++++++++ package-lock.json | 2 +- 7 files changed, 297 insertions(+), 22 deletions(-) create mode 100644 kernel/tests/issue3302-sparse-ref-unknown.test.ts diff --git a/kernel/package-lock.json b/kernel/package-lock.json index 6b1cfc297d..990a39927b 100644 --- a/kernel/package-lock.json +++ b/kernel/package-lock.json @@ -1,12 +1,12 @@ { "name": "acp-kernel", - "version": "0.0.129", + "version": "0.0.131", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "acp-kernel", - "version": "0.0.129", + "version": "0.0.131", "license": "MIT", "devDependencies": { "@types/node": "^22.0.0", diff --git a/kernel/package.json b/kernel/package.json index a318eb703b..56b69e7cff 100644 --- a/kernel/package.json +++ b/kernel/package.json @@ -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", diff --git a/kernel/src/boundaries.ts b/kernel/src/boundaries.ts index 5add019ecc..e1524f1395 100644 --- a/kernel/src/boundaries.ts +++ b/kernel/src/boundaries.ts @@ -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, @@ -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); @@ -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, diff --git a/kernel/src/compress.ts b/kernel/src/compress.ts index 453b0f4103..0ebd3f1765 100644 --- a/kernel/src/compress.ts +++ b/kernel/src/compress.ts @@ -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( @@ -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), ); @@ -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(); + 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}` diff --git a/kernel/tests/compress.test.ts b/kernel/tests/compress.test.ts index 7bf10601ee..7f65c0bae5 100644 --- a/kernel/tests/compress.test.ts +++ b/kernel/tests/compress.test.ts @@ -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/); @@ -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", () => { @@ -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/, ); }); @@ -774,7 +793,8 @@ 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/, ); @@ -782,10 +802,13 @@ test("first compress of a fresh session with foreign refs reports a new generati 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)", () => { @@ -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); }); @@ -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); @@ -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/); }); diff --git a/kernel/tests/issue3302-sparse-ref-unknown.test.ts b/kernel/tests/issue3302-sparse-ref-unknown.test.ts new file mode 100644 index 0000000000..9a67766915 --- /dev/null +++ b/kernel/tests/issue3302-sparse-ref-unknown.test.ts @@ -0,0 +1,185 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { createCore } from "../src/compress.js"; +import { createInitialState } from "../src/state.js"; +import { assignRefs } from "../src/refs.js"; +import type { Config, CompressionState, CoreMessage } from "../src/types.js"; + +function msg(id: string, text: string, role: CoreMessage["role"] = "user"): CoreMessage { + return { id, role, contentType: "text", text }; +} + +function config(overrides: Partial = {}): Config { + return { + tiers: { enabled: true, tier2Trigger: 5, tier3Trigger: 10 }, + nudge: { + maxContextLimitPct: 0.55, + minContextLimitPct: 0.45, + frequency: 5, + iterationThreshold: 15, + force: "soft", + growthRatio: 0.05, + growthFloor: 6000, + growthCap: 50000, + minGrowthFloor: 5000, + minGrowthRatio: 0.45, + emergencyThresholdPct: 0.98, + }, + promotionThreshold: 5, + truncate: { threshold: 1 }, + merge: { maxSummaryLength: 3000, minOldGenBlocks: 3 }, + compress: { minCompressRange: 0, maxSummaryLength: 0, minSummaryLength: 0 }, + protectedTools: [], + preserveRecentMessages: 0, + preserveRecentTokens: 0, + modelContextLimit: 100000, + ...overrides, + }; +} + +/** Session with `n` visible messages, refs m00001..m000N assigned. */ +function sessionWith(n: number) { + const core = createCore(); + const state = createInitialState(); + const messages = Array.from({ length: n }, (_, i) => msg(`u${i + 1}`, `content ${i + 1}`)); + state.messageRefs = assignRefs(messages, { existing: state.messageRefs, nextIndex: 1 }).map; + return { core, state, messages }; +} + +/** Simulate the host-side prune (session.ts pruneRefsToLiveIds): drop the ref + * pair for a message that left the resent view. Returns the raw id. */ +function hostPrune(state: CompressionState, ref: string): string { + const rawId = state.messageRefs.byRef[ref]; + assert.ok(rawId, `${ref} must be allocated`); + delete state.messageRefs.byRef[ref]; + delete state.messageRefs.byRaw[rawId]; + return rawId; +} + +test("in-band gap without tombstone reports sparse-space hole, not cross-session (billion-context#3302)", () => { + const { core, state, messages } = sessionWith(10); + const droppedRaw = hostPrune(state, "m00007"); + const view = messages.filter((m) => m.id !== droppedRaw); + + const result = core.applyCompression({ + ranges: [{ startRef: "m00005", endRef: "m00007", summary: "s" }], + messages: view, + state, + config: config(), + }); + + assert.equal(result.result.blocksCreated, 0); + assert.equal(result.result.errors.length, 1); + assert.match(result.result.errors[0]!, /^range m00005\.\.m00007: /); + // The reporter's case: endId below the highest allocated ref. The old text + // asserted "(typo or wrong session)" — false when the gap is a pruned hole. + assert.match(result.result.errors[0]!, /endId="m00007" was never allocated in this session or was pruned after allocation/); + assert.match(result.result.errors[0]!, /the ref space is sparse/); + assert.match(result.result.errors[0]!, /this is not a cross-session ref/); + assert.doesNotMatch(result.result.errors[0]!, /typo or wrong session/); +}); + +test("ref above the highest allocated reports stale-generation, names the highest (billion-context#3302)", () => { + const { core, state, messages } = sessionWith(10); + + const result = core.applyCompression({ + ranges: [{ startRef: "m00099", endRef: "m00100", summary: "s" }], + messages, + state, + config: config(), + }); + + assert.equal(result.result.blocksCreated, 0); + assert.match( + result.result.errors[0]!, + /startId="m00099" is above this session's highest allocated ref \(m00010\)/, + ); + assert.match(result.result.errors[0]!, /stale artifact from an earlier generation, or a typo/); + assert.doesNotMatch(result.result.errors[0]!, /ref space is sparse/); +}); + +test("tombstoned dead endId snaps to nearest visible message instead of failing (billion-context#3302 Ask 5)", () => { + const { core, state, messages } = sessionWith(10); + const droppedRaw = hostPrune(state, "m00007"); + const view = messages.filter((m) => m.id !== droppedRaw); + state.deadRefs = ["m00007"]; + + const result = core.applyCompression({ + ranges: [{ startRef: "m00005", endRef: "m00007", summary: "s" }], + messages: view, + state, + config: config(), + }); + + assert.equal(result.result.blocksCreated, 1, result.result.errors.join("; ")); + assert.ok( + result.result.warnings.some((w) => w.includes('endId="m00007" no longer backs any visible message')), + `snapped note missing: ${result.result.warnings.join("; ")}`, + ); + const block = result.state.blocks[0]!; + assert.deepEqual(block.effectiveMessageIds.sort(), [messages[4]!.id, messages[5]!.id].sort()); +}); + +test("tombstoned dead startId snaps forward to next visible message (billion-context#3302 Ask 5)", () => { + const { core, state, messages } = sessionWith(10); + const droppedRaw = hostPrune(state, "m00007"); + const view = messages.filter((m) => m.id !== droppedRaw); + state.deadRefs = ["m00007"]; + + const result = core.applyCompression({ + ranges: [{ startRef: "m00007", endRef: "m00009", summary: "s" }], + messages: view, + state, + config: config(), + }); + + assert.equal(result.result.blocksCreated, 1, result.result.errors.join("; ")); + assert.ok( + result.result.warnings.some((w) => w.includes('startId="m00007" no longer backs any visible message')), + `snapped note missing: ${result.result.warnings.join("; ")}`, + ); + const block = result.state.blocks[0]!; + assert.deepEqual(block.effectiveMessageIds.sort(), [messages[7]!.id, messages[8]!.id].sort()); +}); + +test("tombstoned dead ref with no visible neighbor classifies DEAD, not unknown (billion-context#3302 Ask 5)", () => { + const { core, state, messages } = sessionWith(5); + // m00006 was allocated in an earlier view and pruned; nothing visible beyond it. + state.deadRefs = ["m00006"]; + + const result = core.applyCompression({ + ranges: [{ startRef: "m00006", endRef: "m00009", summary: "s" }], + messages, + state, + config: config({ compress: { minCompressRange: 5000, maxSummaryLength: 0, minSummaryLength: 0 } }), + }); + + assert.equal(result.result.blocksCreated, 0); + assert.equal(result.result.errors.length, 1); + assert.match(result.result.errors[0]!, /cannot be anchored/); + assert.match(result.result.errors[0]!, /refs m00006 are known to this session but no longer back any visible or folded message/); + assert.match(result.result.errors[0]!, /recorded as DEAD/); + assert.doesNotMatch(result.result.errors[0]!, /unknown to this session/); + assert.deepEqual(result.state.deadRefs, ["m00006"]); +}); + +test("all-unknown gate names the failing endpoint per range (billion-context#3302 Ask 2)", () => { + const { core, state, messages } = sessionWith(10); + const droppedRaw = hostPrune(state, "m00007"); + const view = messages.filter((m) => m.id !== droppedRaw); + + const result = core.applyCompression({ + ranges: [{ startRef: "m00005", endRef: "m00007", summary: "s" }], + messages: view, + state, + config: config({ compress: { minCompressRange: 5000, maxSummaryLength: 0, minSummaryLength: 0 } }), + }); + + assert.equal(result.result.blocksCreated, 0); + assert.equal(result.result.errors.length, 2); + assert.match(result.result.errors[0]!, /None of the 1 requested range\(s\) resolved — every ref is unknown to this session/); + assert.match(result.result.errors[0]!, /failing endpoints: endId=m00007/); + assert.match(result.result.errors[0]!, /Do not probe for valid numbers/); + assert.doesNotMatch(result.result.errors[0]!, /cannot come from an earlier compress in this session/); + assert.match(result.result.errors[1]!, /endId="m00007" was never allocated in this session or was pruned after allocation/); +}); diff --git a/package-lock.json b/package-lock.json index 1bf7c35301..85d16c963b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -48,7 +48,7 @@ }, "kernel": { "name": "acp-kernel", - "version": "0.0.130", + "version": "0.0.131", "dev": true, "license": "MIT", "devDependencies": { From 5a6f07da487248c101756816e5aa88dcbbee3a55 Mon Sep 17 00:00:00 2001 From: awork Date: Sat, 17 Oct 2026 17:07:59 +0800 Subject: [PATCH 2/3] fix: tombstone host-pruned refs and make compress failure receipts actionable (#3302) - pruneRefsToLiveIds records pruned refs into state.deadRefs (deduped, sorted) so both producers (compaction archive, zombie reap) feed the kernel tombstone path - failure receipt falls back to the last nudge's compressible-range table when no live decision exists, instead of 'Call acp_status before retrying' - loop logs a one-shot warning when a fixed-endpoint/varying-endpoint compress probe family is detected (#370 breaker stays identical-args only) - stale-ref cause text and FOLD BASE GENERATION MISMATCH body no longer assert cross-session origin --- src/acp-status.ts | 5 +- src/loop/core.ts | 36 +++++ src/session.ts | 14 ++ src/stream.ts | 31 +++- tests/issue3302-sparse-ref-tombstones.test.ts | 137 ++++++++++++++++++ 5 files changed, 217 insertions(+), 6 deletions(-) create mode 100644 tests/issue3302-sparse-ref-tombstones.test.ts diff --git a/src/acp-status.ts b/src/acp-status.ts index 17ed8d0455..8b07c0b5ad 100644 --- a/src/acp-status.ts +++ b/src/acp-status.ts @@ -290,7 +290,10 @@ export function handleAcpStatus(args: Record, ctx: AcpStatusCtx if (staleRef || (substrateDestroyed && breaker === undefined)) { extra.push(""); if (staleRef) { - extra.push(`FOLD BASE GENERATION MISMATCH — last compress failure: ${lastCause}. The refs that failed belong to another session generation; the Compressible-ranges list above was re-derived from the current view — compress only ranges it lists. If refs keep failing, start a fresh conversation.`); + // #3302: "stale-ref" covers earlier-generation refs, typos AND + // sparse-space gaps — the per-failure cause above names the sub-case; + // don't over-assert generation here. + extra.push(`FOLD BASE GENERATION MISMATCH — last compress failure: ${lastCause}. The refs that failed do not exist in this session's current ref space; the Compressible-ranges list above was re-derived from the current view — compress only ranges it lists. If refs keep failing, start a fresh conversation.`); } else { extra.push(`FOLD SUBSTRATE INVALID — last compress failure was attributed to ${lastCause}. The Compressible-ranges list above was re-derived from the current view; if folding a listed range fails again, start a fresh conversation — the folded base no longer matches what the client resends.`); } diff --git a/src/loop/core.ts b/src/loop/core.ts index 703aa184ad..35b5aaf866 100644 --- a/src/loop/core.ts +++ b/src/loop/core.ts @@ -72,6 +72,25 @@ export function buildVisibilityMarker(toolName: string, result: string): string return `\n${icon} [ACP] ${inner}\n`; } +// #3302 (Ask 6): accumulate endpoint values from failed compress receipts and +// report the first endpoint that saw ≥2 distinct values — the probe-family +// signature. Pure so it is unit-testable without driving a full loop. +export function recordProbeFailures( + probes: Map>, + results: ReadonlyArray<{ name: string; result: string }>, +): { label: string; values: string[] } | null { + for (const pr of results) { + if (pr.name !== "compress") continue; + for (const m of pr.result.matchAll(/(startId|endId)="(m\d+)"/g)) { + const values = probes.get(m[1]) ?? new Set(); + values.add(m[2]); + probes.set(m[1], values); + } + } + const probing = [...probes.entries()].find(([, values]) => values.size >= 2); + return probing ? { label: probing[0], values: [...probing[1]] } : null; +} + // #413 follow-up: when the stream dies AFTER visible text has already been // forwarded (final-report shape — heavy reasoning, then the answer starts, // then the relay cuts), a blind re-fetch would make the client watch the @@ -520,6 +539,14 @@ export async function* runCompressLoop( // re-submission can never succeed — break early instead of at MAX_LOOP_ROUNDS. const failedSignatures = new Set(); + // #3302 (Ask 6): probe-family observability — a model failing compress with + // one endpoint fixed and the other varying is enumerating a sparse ref + // space; the identical-args breaker above never fires on that pattern. + // Log it once as an operator signal; the failure receipts carry the live + // range table, so the loop keeps running for the model to find it. + const probeFailures = new Map>(); + let probeWarned = false; + // #762,#1479: strict-echo origin for re-request normalization (the learned // flag rides on ctx.session.metadata; mirrors prepareOpenai's static gate). // EVERY body this loop sends upstream goes through it — main re-request AND @@ -1400,6 +1427,15 @@ export async function* runCompressLoop( } for (const pr of failedMutating) failedSignatures.add(`${pr.name}\u0000${canonicalArgs(pr.arguments)}`); + if (!probeWarned) { + const probing = recordProbeFailures(probeFailures, failedMutating); + if (probing) { + probeWarned = true; + ctx.log(`[acp-loop] round ${round}: model probed ${probing.values.length} distinct ${probing.label} values (${probing.values.join(", ")}) in failed compress calls — sparse ref-space enumeration (#3302); not breaking early, the receipts carry the live range table`); + loggerLog("warn", `[acp-loop] compress probe family at round ${round}: ${probing.values.length} distinct ${probing.label} values failed (${probing.values.join(", ")}) (#3302)`); + } + } + ctx.log(`[acp-loop] round ${round}: proxy tool executed; re-requesting so the model sees the result`); if (signal?.aborted) break; diff --git a/src/session.ts b/src/session.ts index cfdfb0bea8..24a48a749d 100644 --- a/src/session.ts +++ b/src/session.ts @@ -1457,11 +1457,25 @@ function pruneRefsToLiveIds( } if (highestBefore > highestUsedIndex({ byRaw: prunedByRaw, byRef: {} })) prunedByRaw[REF_FLOOR_RAW_ID] = indexToRef(highestBefore); const prunedByRef: Record = {}; + const droppedRefs: string[] = []; for (const [ref, rawId] of Object.entries(byRef)) { if (liveRawIds.has(rawId)) prunedByRef[ref] = rawId; + else droppedRefs.push(ref); } session.state.messageRefs.byRaw = prunedByRaw; session.state.messageRefs.byRef = prunedByRef; + if (droppedRefs.length > 0) { + // #3302: tombstone the freed numbers so the kernel classifies a later + // citation as dead (snap-to-neighbor / honest "content gone") instead + // of unknown ("typo or wrong session") — the mislabel that sent models + // probing upward for a valid number after every native-compaction + // prune. I6 invariant: deadRefs stays unique and numerically sorted + // ascending; empty ⇒ undefined. + const merged = new Set(session.state.deadRefs ?? []); + for (const ref of droppedRefs) merged.add(ref); + const sorted = [...merged].sort((a, b) => Number(a.replace(/^m/, "")) - Number(b.replace(/^m/, ""))); + session.state.deadRefs = sorted.length > 0 ? sorted : undefined; + } } /** #2695: remove fold blocks whose covered substrate the client demonstrably diff --git a/src/stream.ts b/src/stream.ts index a0d1e1681c..74a8c6bf21 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -108,6 +108,24 @@ function compressionRecovery(ctx: RewriteCtx): { hint: string; canRetry: boolean } return { hint: " No eligible message ranges are available now; continue the task without compressing. Recheck acp_status after the context changes.", canRetry: false }; } + // #3302: the live check only yields a verdict when this pass produced a + // nudge decision. When it didn't, the previous prepare's offer + // (ctx.lastNudge) still lists kernel-validated ranges — stale by at + // most one turn, and self-correcting if one was folded meanwhile (the + // kernel rejects it as already compressed). Sending the model to + // acp_status for another full round-trip after EVERY failed compress + // is exactly the cost this issue reports. + const last = ctx.lastNudge; + if (last?.shouldInject && last.compressibleRanges && last.compressibleRanges.length > 0) { + const minChars = ctx.config.compress.minCompressRange; + const submitGate = (r: CompressibleRangeItem): boolean => + minChars <= 0 || (r.chars ?? r.tokens * 4) >= minChars; + const ranges = excludeDeadRefRanges(viableRanges(last.compressibleRanges).filter(submitGate), ctx.session.state.deadRefs); + if (ranges.length > 0) { + const formatted = formatRanges(ranges, []).replace(/\s*\n\s*/g, " "); + return { hint: ` Current compressible ranges from the last compression check (use these refs exactly as listed; recheck acp_status if one is rejected): ${formatted} Do not repeat the rejected call unchanged.`, canRetry: true }; + } + } } catch { // Never replace a failed live check with a guessed ref span. } @@ -338,17 +356,20 @@ function compressFailureCause(errs: string, session: Session): string { ? "substrate-destruction — host-native compaction or bulk client-side history rewrite landed outside bili's knowledge (#1729/#2193); structural, report it" : "content-changed — the messages behind those refs changed and carry new refs now"; } - if (errs.includes("every ref is unknown to this session")) return "stale-ref — the refs belong to another session generation (or are typos)"; + // #3302: "unknown" now covers three sub-cases (earlier generation, typo, + // sparse-space gap) — name the class honestly; the prefix stays stable for + // downstream startsWith matching (acp_status verdict sections). + if (errs.includes("every ref is unknown to this session")) return "stale-ref — the refs do not exist in this session's ref space (another generation, a typo, or pruned gaps)"; if (errs.includes("covered by active block")) return "covered-by-block — nothing new to fold in that window"; return ""; } /** #2146: every requested endpoint sits strictly ABOVE the session's highest * mapped m-ref. Such refs cannot exist in this session (refs are never - * pre-allocated), so the kernel's generic "typo or wrong session" wording - * misleads models into hunting for the typo and re-anchoring on other stale - * artifacts. The deterministic cause is stale-generation refs baked into the - * client's persisted history — name it instead. */ + * pre-allocated); the kernel now classifies that case as above-frontier + * (#3302) and this note reinforces it with the model-visible consequence — + * NO superset of such refs can ever compress — so the model stops re-anchoring + * on other stale artifacts instead. */ export function beyondFrontierNote(state: Pick, ranges: Array<{ startRef: string; endRef: string }>): string { let highest = 0; for (const ref of Object.keys(state.messageRefs?.byRef ?? {})) { diff --git a/tests/issue3302-sparse-ref-tombstones.test.ts b/tests/issue3302-sparse-ref-tombstones.test.ts new file mode 100644 index 0000000000..7ab0090d73 --- /dev/null +++ b/tests/issue3302-sparse-ref-tombstones.test.ts @@ -0,0 +1,137 @@ +import assert from "node:assert/strict"; +import { randomUUID } from "node:crypto"; +import { test } from "node:test"; +import { assignRefs, createCore, defaultConfig, emptyRefMap, type CoreMessage } from "acp-kernel"; +import { parseCompressInput } from "../src/compress-tool.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { getSession, reapDestroyedSubstrate } from "../src/session.ts"; +import { applyRanges, type RewriteCtx } from "../src/stream.ts"; +import { recordProbeFailures } from "../src/loop/core.ts"; + +_setStoreForTest(new SessionStore({ enabled: false })); + +const SUMMARY = "Synthetic fold summary; keep the remaining recent messages."; + +function makeCtx(count: number, preserveRecentMessages = 5): RewriteCtx & { messages: CoreMessage[] } { + const messages: CoreMessage[] = Array.from({ length: count }, (_, i) => ({ + id: `raw-${i + 1}`, + role: i === count - 1 ? "user" : "assistant", + contentType: "text", + text: `Synthetic message ${i + 1}. ` + "x".repeat(6000), + })); + const session = getSession(`issue3302-${randomUUID()}`); + session.state.messageRefs = assignRefs(messages, { existing: emptyRefMap(), nextIndex: 0 }).map; + return { + core: createCore(), + config: defaultConfig(200000, { preserveRecentMessages, preserveRecentTokens: 0 }), + messages, + session, + log: () => {}, + }; +} + +function request(ctx: RewriteCtx, startId: string, endId: string, summary = SUMMARY) { + return applyRanges(parseCompressInput({ content: [{ startId, endId, summary }] }), ctx); +} + +/** Simulate the host-side prune dropping one message's ref pair. */ +function dropRef(ctx: ReturnType, ref: string): void { + const rawId = ctx.session.state.messageRefs.byRef[ref]; + assert.ok(rawId, `${ref} must be allocated`); + delete ctx.session.state.messageRefs.byRef[ref]; + delete ctx.session.state.messageRefs.byRaw[rawId]; +} + +test("reapDestroyedSubstrate tombstones pruned refs into state.deadRefs (billion-context#3302 Ask 5)", () => { + const ctx = makeCtx(6); + request(ctx, "m00001", "m00002"); + assert.equal(ctx.session.state.blocks.length, 1); + const blockId = ctx.session.state.blocks[0]!.blockId; + + const liveRawIds = new Set(ctx.messages.filter((m) => m.id !== "raw-5").map((m) => m.id)); + reapDestroyedSubstrate(ctx.session, new Set([blockId]), liveRawIds, () => {}); + + assert.equal(ctx.session.state.messageRefs.byRef["m00005"], undefined); + assert.equal(ctx.session.state.messageRefs.byRef["m00003"], "raw-3"); + assert.deepEqual(ctx.session.state.deadRefs, ["m00005"]); +}); + +test("tombstone merge keeps pre-existing dead refs, dedupes, sorts ascending (billion-context#3302 Ask 5)", () => { + const ctx = makeCtx(6); + request(ctx, "m00001", "m00002"); + const blockId = ctx.session.state.blocks[0]!.blockId; + ctx.session.state.deadRefs = ["m00008", "m00005"]; + + const liveRawIds = new Set(ctx.messages.filter((m) => m.id !== "raw-5" && m.id !== "raw-3").map((m) => m.id)); + reapDestroyedSubstrate(ctx.session, new Set([blockId]), liveRawIds, () => {}); + + assert.deepEqual(ctx.session.state.deadRefs, ["m00003", "m00005", "m00008"]); +}); + +test("pruned endpoint without tombstone reports sparse-space gap, not cross-session (billion-context#3302)", () => { + const ctx = makeCtx(6); + dropRef(ctx, "m00005"); + const result = request(ctx, "m00003", "m00005"); + assert.equal(result.outcome, "refused"); + assert.match(result.text, /endId="m00005" was never allocated in this session or was pruned after allocation/); + assert.doesNotMatch(result.text, /typo or wrong session/); +}); + +test("tombstoned pruned endpoint snaps instead of failing (billion-context#3302 Ask 5)", () => { + const ctx = makeCtx(12); + dropRef(ctx, "m00009"); + ctx.session.state.deadRefs = ["m00009"]; + const result = request(ctx, "m00004", "m00009"); + assert.equal(result.outcome, "applied", result.text); + assert.match(result.text, /endId="m00009" no longer backs any visible message/); +}); + +test("failure receipt falls back to the last nudge's compressible table when no live decision exists (billion-context#3302 Ask 3)", () => { + const ctx = makeCtx(10); + const baseCore = ctx.core; + const decision = baseCore.processTurn({ + messages: ctx.messages, + state: ctx.session.state, + config: ctx.config, + tokenCount: 180000, + }).nudge; + assert.ok(decision?.shouldInject, "fixture must produce an injectable nudge decision"); + assert.ok(decision.compressibleRanges.length > 0); + ctx.lastNudge = decision; + ctx.core = { + ...baseCore, + processTurn: (input: Parameters[0]) => ({ + ...baseCore.processTurn(input), + nudge: undefined, + }), + }; + + request(ctx, "m00001", "m00002"); + const rejected = request(ctx, "m00001", "m00002"); + assert.equal(rejected.outcome, "refused"); + assert.match(rejected.text, /Current compressible ranges from the last compression check/); + assert.doesNotMatch(rejected.text, /Call acp_status before retrying/); +}); + +test("recordProbeFailures flags a probe family only on >=2 distinct values at one endpoint (billion-context#3302 Ask 6)", () => { + const probes = new Map>(); + assert.equal(recordProbeFailures(probes, []), null); + assert.equal( + recordProbeFailures(probes, [ + { name: "compress", result: '[Compression FAILED: range m00003..m00007: endId="m00007" ...]' }, + ]), + null, + ); + assert.equal( + recordProbeFailures(probes, [ + { name: "decompress", result: '[Decompression FAILED: endId="m00007" unknown]' }, + { name: "bash", result: "FAILED exit 1" }, + ]), + null, + ); + const hit = recordProbeFailures(probes, [ + { name: "compress", result: '[Compression FAILED: range m00003..m00007: endId="m00007" ...]' }, + { name: "compress", result: '[Compression FAILED: range m00003..m00009: endId="m00009" ...]' }, + ]); + assert.deepEqual(hit, { label: "endId", values: ["m00007", "m00009"] }); +}); From 4870c296a17189e32b2360728b54207e5afb9788 Mon Sep 17 00:00:00 2001 From: awork Date: Sat, 17 Oct 2026 18:52:04 +0800 Subject: [PATCH 3/3] fix: restore npm-standard indentation in kernel/package-lock.json (PR #3533 review nit) The rebase hand-edit of the version field left packages."".version at 2-space indent instead of the npm lockfile's 6-space level. Whitespace-only. --- kernel/package-lock.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/package-lock.json b/kernel/package-lock.json index 990a39927b..44a1d11fe0 100644 --- a/kernel/package-lock.json +++ b/kernel/package-lock.json @@ -6,7 +6,7 @@ "packages": { "": { "name": "acp-kernel", - "version": "0.0.131", + "version": "0.0.131", "license": "MIT", "devDependencies": { "@types/node": "^22.0.0",