From d4f979bad2a42e1b75a5bf58af75236ebc8e4064 Mon Sep 17 00:00:00 2001 From: awork Date: Thu, 22 Oct 2026 18:39:13 +0800 Subject: [PATCH] kernel: lift the permanent exemption for FAILED compress exchanges (#4289) ALWAYS_PROTECTED_TOOLS exempted every compress call+result by tool name alone, so total-failure retries (9-21KB summary-draft args, zero ranges applied) stayed BLOCKED forever: never advertised, never folded, pinning the fold floor while the session starved on its own compression residue (~75% of one measured 227K session). Exemption is now outcome-aware: a compress exchange whose paired result carries a total-failure receipt ('[Compression FAILED', zero blocks created) loses its hard protection at all three judgment sites (assignRefs, unit-projection filterProtectedToolMessages, recommend buildCompressibleRanges) and folds like an ordinary message; success/partial receipts ('[Compressed ...') and result-less in-flight calls stay exempt, acp_rule is untouched, and host hooks (isMessageProtected) or protectedTools patterns still outrank the carve-out. Recognition goes through config.isCompressTool so MCP-dialect lanes (mcp__bili__compress) get identical treatment through their isToolProtected hook path. Existing sessions self-heal without migration: stale BLOCKED refs migrate to fresh numeric refs on the next assignRefs pass via the existing #2607 path (renderDeferredRefs deferral applies), which also makes the exchanges eligible for the #3769 orphan sweep once a real fold moves the breakpoint. Tests: kernel/tests/issue4289-failed-compress-foldable.test.ts (12 cases incl. negative controls: partial receipts, in-flight, acp_rule, host-override wins, unnamed-result pairing, self-heal migration, MCP dialect). --- kernel/package.json | 2 +- kernel/src/compress.ts | 25 +- kernel/src/index.ts | 3 + kernel/src/protected.ts | 102 +++++- kernel/src/recommend.ts | 17 +- kernel/src/unit-projection.ts | 8 +- ...issue4289-failed-compress-foldable.test.ts | 317 ++++++++++++++++++ 7 files changed, 460 insertions(+), 14 deletions(-) create mode 100644 kernel/tests/issue4289-failed-compress-foldable.test.ts diff --git a/kernel/package.json b/kernel/package.json index e2c2fc639d..0bf842da94 100644 --- a/kernel/package.json +++ b/kernel/package.json @@ -1,6 +1,6 @@ { "name": "acp-kernel", - "version": "0.0.151", + "version": "0.0.152", "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 67ff2df893..e18ed93dc8 100644 --- a/kernel/src/compress.ts +++ b/kernel/src/compress.ts @@ -28,6 +28,7 @@ import { activeBlockSpans } from "./block-map.js"; import { createRenderRefsNode } from "./render-refs.js"; import type { RenderStrategy } from "./render-refs.js"; import { + collectFailedCompressCallIds, collectLatestProtected, collectProtectedToolCallIds, hasUnrecoverableMediaPayload, @@ -1000,6 +1001,15 @@ const assignRefsNode: PipelineNode = { const latest = hasProtection ? collectLatestProtected(io.messages, ctx.config) : undefined; + // #4289: total-failure compress exchanges (receipt "[Compression FAILED", + // zero ranges applied) carry no summary carrier — lift their hard-coded + // exemption so they fold and get reclaimed like ordinary messages. + // Success/partial receipts and in-flight calls stay exempt. Computed only + // when some protection machinery is active, mirroring `latest` (otherwise + // nothing is BLOCKED anyway and behavior stays byte-identical). + const failedCompressCallIds = hasProtection + ? collectFailedCompressCallIds(io.messages, ctx.config) + : undefined; // Refs keep the exact pre-#1947 layout for name-only patterns (a // protected call's unnamed result keeps a foldable ref; actual folding // paths pair via filterProtectedToolMessages). #1947 path patterns @@ -1011,7 +1021,11 @@ const assignRefsNode: PipelineNode = { ); const pathProtectedCallIds = hasProtection && hasPathPatterns - ? collectProtectedToolCallIds(io.messages, ctx.config) + ? collectProtectedToolCallIds( + io.messages, + ctx.config, + failedCompressCallIds, + ) : undefined; // Unrecoverable media (remote URLs / file refs with no stored bytes) ride // outside msg.text; folding one destroys it permanently with no restore @@ -1022,8 +1036,13 @@ const assignRefsNode: PipelineNode = { hasUnrecoverableMediaPayload(m) || (hasProtection ? (pathProtectedCallIds - ? isMessageProtectedWithPairing(m, ctx.config, pathProtectedCallIds) - : isMessageProtected(m, ctx.config)) || + ? isMessageProtectedWithPairing( + m, + ctx.config, + pathProtectedCallIds, + failedCompressCallIds, + ) + : isMessageProtected(m, ctx.config, failedCompressCallIds)) || (latest ? isMessageLatestProtected(m, latest) : false) : false); const refResult = assignRefs(io.messages, { diff --git a/kernel/src/index.ts b/kernel/src/index.ts index a781234af0..c4c2b796f0 100644 --- a/kernel/src/index.ts +++ b/kernel/src/index.ts @@ -326,10 +326,13 @@ export { listSearchAlgorithms, } from "./search.js"; export { + COMPRESS_FAILED_RECEIPT_PREFIX, + collectFailedCompressCallIds, collectLatestProtected, collectProtectedToolCallIds, hasMediaPayload, hasUnrecoverableMediaPayload, + isFailedCompressReceiptText, isMessageLatestProtected, isMessageProtected, isMessageProtectedWithPairing, diff --git a/kernel/src/protected.ts b/kernel/src/protected.ts index e1ff9e54c3..db4ab327d2 100644 --- a/kernel/src/protected.ts +++ b/kernel/src/protected.ts @@ -6,9 +6,78 @@ import type { Config, CoreMessage } from "./types.js"; * treats past compress calls as load-bearing metadata. acp_rule calls record * persistent user rules that are re-injected every turn — compressing them * away would silently lose what the rules state. Letting any of these be - * compressed away breaks decompress and the "summary is historical" contract. */ + * compressed away breaks decompress and the "summary is historical" contract. + * + * Exception (billion-context#4289): a compress exchange whose paired result + * is a TOTAL-FAILURE receipt (COMPRESS_FAILED_RECEIPT_PREFIX — zero ranges + * applied) carries no summary carrier: no block cites its arguments, so + * decompress/search lose nothing folding it. Keeping such exchanges exempt + * forever pins every failed retry into context and raises the fold floor + * (measured: ~75% of a 227K context was compression residue). isMessageProtected + * lifts the exemption for exactly those exchanges when the caller passes the + * matching collectFailedCompressCallIds set; success/partial receipts + * ("[Compressed …") and result-less in-flight calls keep it. */ export const ALWAYS_PROTECTED_TOOLS = ["compress", "acp_rule"] as const; +/** Receipt prefix emitted by a compress invocation that applied ZERO ranges — + * the total-failure form. Successful AND partial-success receipts start with + * "[Compressed …" instead, so this prefix identifies total failure without + * parsing the body (billion-context#4289). */ +export const COMPRESS_FAILED_RECEIPT_PREFIX = "[Compression FAILED"; + +/** True when a tool-result text is a total-failure compress receipt. */ +export function isFailedCompressReceiptText( + text: string | undefined, +): boolean { + return ( + typeof text === "string" && + text.trimStart().startsWith(COMPRESS_FAILED_RECEIPT_PREFIX) + ); +} + +/** True when `name` denotes a `compress` call: the host dialect recognizer + * (#2970 isCompressTool covers e.g. MCP's "mcp__bili__compress") wins when + * set; otherwise the canonical bare name only. */ +function isCompressToolName( + name: string | undefined, + config: Pick, +): boolean { + if (!name) return false; + if (config.isCompressTool) return config.isCompressTool(name); + return name === "compress"; +} + +/** Tool-call ids of compress exchanges whose paired result is a TOTAL-FAILURE + * receipt (#4289). Such exchanges carry no summary carrier — zero ranges were + * applied, so no block cites their arguments and decompress/search never read + * them — yet they enjoyed compress's permanent exemption, pinning every + * failed retry into the context forever. Callers feed the returned set into + * isMessageProtected / collectProtectedToolCallIds / + * isMessageProtectedWithPairing to lift the exemption for exactly those + * exchanges. Success/partial receipts and result-less (in-flight) calls do + * NOT enter the set: their summaries are load-bearing (conservative). A + * result lacking any recognizable tool name (own or paired) is not marked — + * it stays protected, as before. O(n) + one id→name map. */ +export function collectFailedCompressCallIds( + messages: CoreMessage[], + config: Pick, +): Set { + const ids = new Set(); + const callNameById = new Map(); + for (const m of messages) { + if (m.contentType === "tool-call" && m.toolCallId && m.toolName) { + callNameById.set(m.toolCallId, m.toolName); + } + } + for (const m of messages) { + if (m.contentType !== "tool-result" || !m.toolCallId) continue; + if (!isFailedCompressReceiptText(m.text)) continue; + const name = m.toolName ?? callNameById.get(m.toolCallId); + if (isCompressToolName(name, config)) ids.add(m.toolCallId); + } + return ids; +} + /** Tool results that must NEVER participate in the soft-protected recent zone * (preserveRecentMessages / preserveRecentTokens / last user message). * @@ -202,6 +271,10 @@ export function virtualToolPathOf( export function isMessageProtected( msg: CoreMessage, config: Pick, + /** #4289: total-failure compress exchanges (from collectFailedCompressCallIds + * over the SAME message array class) lose their hard-coded exemption — see + * the ALWAYS_PROTECTED_TOOLS note above. Unset = legacy behavior. */ + failedCompressCallIds?: ReadonlySet, ): boolean { // Host-declared message-level protection (#2419): applies to ANY content // type and outranks everything below — the host owns the decision. @@ -226,8 +299,21 @@ export function isMessageProtected( return false; } - // Hard-coded protection: ACP metadata tools are never compressible. - if ((ALWAYS_PROTECTED_TOOLS as readonly string[]).includes(msg.toolName)) { + // #4289: total-failure compress exchange (both halves identified by their + // shared toolCallId in the failed set). Lifts ONLY the hard-coded exemption + // below — the pattern hooks still run, so a host that explicitly pins these + // via protectedTools/isToolProtected wins (conservative). + const failedCompress = + failedCompressCallIds !== undefined && + !!msg.toolCallId && + failedCompressCallIds.has(msg.toolCallId); + + // Hard-coded protection: ACP metadata tools are never compressible (except + // failed compress exchanges above — they carry no summary carrier). + if ( + !failedCompress && + (ALWAYS_PROTECTED_TOOLS as readonly string[]).includes(msg.toolName) + ) { return true; } @@ -235,7 +321,9 @@ export function isMessageProtected( if (matchToolMessagePattern(msg, pattern)) return true; } - if (config.isToolProtected?.(msg.toolName, msg.text)) return true; + if (!failedCompress && config.isToolProtected?.(msg.toolName, msg.text)) { + return true; + } return false; } @@ -247,13 +335,14 @@ export function isMessageProtected( export function collectProtectedToolCallIds( messages: CoreMessage[], config: Pick, + failedCompressCallIds?: ReadonlySet, ): Set { const ids = new Set(); for (const m of messages) { if ( m.contentType === "tool-call" && m.toolCallId && - isMessageProtected(m, config) + isMessageProtected(m, config, failedCompressCallIds) ) { ids.add(m.toolCallId); } @@ -267,8 +356,9 @@ export function isMessageProtectedWithPairing( msg: CoreMessage, config: Pick, protectedCallIds: Set, + failedCompressCallIds?: ReadonlySet, ): boolean { - if (isMessageProtected(msg, config)) return true; + if (isMessageProtected(msg, config, failedCompressCallIds)) return true; if ( msg.contentType === "tool-result" && msg.toolCallId && diff --git a/kernel/src/recommend.ts b/kernel/src/recommend.ts index 3f4030eae6..e43ec89962 100644 --- a/kernel/src/recommend.ts +++ b/kernel/src/recommend.ts @@ -24,6 +24,7 @@ import { isToolMessage } from "./message-kind.js"; import { coveredMessageIds, isLiveCheckpointCarrier } from "./state.js"; import { SUMMARY_HEADER } from "./prune.js"; import { + collectFailedCompressCallIds, collectLatestProtected, collectProtectedToolCallIds, hasUnrecoverableMediaPayload, @@ -290,9 +291,16 @@ export function buildCompressibleRanges( const covered = coveredMessageIds(state); + // #4289: total-failure compress exchanges are foldable — same set the refs + // site computed, so advertised ranges agree with assigned refs. + const failedCompressCallIds = collectFailedCompressCallIds(messages, config); // Pairing: a tool-result may carry only toolCallId (no toolName). Collect the // callIds of protected tool-calls first, then protect matching results too. - const protectedCallIds = collectProtectedToolCallIds(messages, config); + const protectedCallIds = collectProtectedToolCallIds( + messages, + config, + failedCompressCallIds, + ); // Latest-only protected calls: their results are covered by the pairing // union; the calls themselves need the explicit check below (pairing only // matches tool-results). @@ -355,7 +363,12 @@ export function buildCompressibleRanges( } if ( - isMessageProtectedWithPairing(msg, config, protectedCallIds) || + isMessageProtectedWithPairing( + msg, + config, + protectedCallIds, + failedCompressCallIds, + ) || isMessageLatestProtected(msg, latest) ) { protectedMsgs.push({ diff --git a/kernel/src/unit-projection.ts b/kernel/src/unit-projection.ts index 9ea3554485..dbec4ca14f 100644 --- a/kernel/src/unit-projection.ts +++ b/kernel/src/unit-projection.ts @@ -2,6 +2,7 @@ import { activeBlocks, blockById, isLiveCheckpointCarrier } from "./state.js"; import { resolveBoundaries, blockVisibleInRange } from "./boundaries.js"; import { isSummaryMessageId } from "./prune.js"; import { + collectFailedCompressCallIds, collectLatestProtected, hasUnrecoverableMediaPayload, isMessageLatestProtected, @@ -461,9 +462,12 @@ function filterProtectedToolMessages( const protectedCallIds = new Set(); const removedIds = new Set(); const latest = collectLatestProtected(messages, config); + // #4289: same failed-compress exemption as the refs/recommend sites — the + // apply-side judgment must agree with what assignRefs made foldable. + const failedCompressCallIds = collectFailedCompressCallIds(messages, config); for (const id of latest.callIds) protectedCallIds.add(id); for (const msg of messages) { - if (isMessageProtected(msg, config) && msg.toolCallId) { + if (isMessageProtected(msg, config, failedCompressCallIds) && msg.toolCallId) { protectedCallIds.add(msg.toolCallId); } } @@ -473,7 +477,7 @@ function filterProtectedToolMessages( if (!msg) continue; if ( hasUnrecoverableMediaPayload(msg) || - isMessageProtected(msg, config) || + isMessageProtected(msg, config, failedCompressCallIds) || isMessageLatestProtected(msg, latest) ) { removedIds.add(id); diff --git a/kernel/tests/issue4289-failed-compress-foldable.test.ts b/kernel/tests/issue4289-failed-compress-foldable.test.ts new file mode 100644 index 0000000000..c03ac16c11 --- /dev/null +++ b/kernel/tests/issue4289-failed-compress-foldable.test.ts @@ -0,0 +1,317 @@ +// #4289: a compress invocation that applied ZERO ranges (total-failure +// receipt "[Compression FAILED…") carries no summary carrier — no block cites +// its arguments, decompress/search never read them — yet it enjoyed +// compress's permanent exemption (ALWAYS_PROTECTED_TOOLS matches the tool +// NAME, not the outcome). Every failed retry pinned ~10KB of summary draft +// into context forever and raised the fold floor (measured: ~75% of a 227K +// context was compression residue). The exemption now follows the OUTCOME: +// total-failure exchanges fold like ordinary messages; success/partial +// receipts and result-less in-flight calls stay exempt. Existing sessions +// self-heal through the #2607 BLOCKED→numeric migration on the next +// assignRefs pass — no ledger script. +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, BLOCKED_REF, indexToRef, refToIndex } from "../src/refs.js"; +import { buildCompressibleRanges } from "../src/recommend.js"; +import { collectFailedCompressCallIds } from "../src/protected.js"; +import { defaultConfig } from "../src/config.js"; +import type { Config, CoreMessage, CompressionState } from "../src/types.js"; + +const FAILED_RECEIPT = + "[Compression FAILED: requested range(s) resolved to no messages]"; +const PARTIAL_RECEIPT = + "[Compressed m00001-m00002 (partial: 1 of 3 ranges applied) → 1 block(s), ~50 tokens saved]"; +const SUCCESS_RECEIPT = + "[Compressed m00001-m00002 → 1 block(s), ~120 tokens saved]"; +const VALID_SUMMARY = + "A meaningful summary that captures the key information of the compressed range including file paths and decisions."; + +// Emulates the bili host wiring (src/config.ts): hasProtection=true via the +// MCP hard-protection hook, dialect-aware compress recognition (#2970). +function biliConfig(overrides: Partial = {}): Config { + return defaultConfig(200000, { + compress: { minCompressRange: 0, maxSummaryLength: 0, minSummaryLength: 0 }, + preserveRecentMessages: 0, + preserveRecentTokens: 0, + isToolProtected: (name?: string) => + name === "mcp__bili__compress" || name === "mcp__bili__acp_rule", + isCompressTool: (name: string) => + name === "compress" || name === "mcp__bili__compress", + ...overrides, + }); +} + +function mk( + id: string, + role: CoreMessage["role"], + ct: CoreMessage["contentType"], + toolName?: string, + toolCallId?: string, + text = "x".repeat(800), +): CoreMessage { + return { id, role, contentType: ct, toolName, toolCallId, text }; +} + +// Main geometry: an early head, a SUCCESSFUL compress exchange, then a +// REASONING message nailed behind the FAILED exchange (turn-integrity +// withdrawal keeps it unfolded while the nail sits inside any span), then +// tail content. +function mainGeometry(): CoreMessage[] { + return [ + mk("U1", "user", "text"), + mk("A1", "assistant", "text"), + mk("C_OK", "assistant", "tool-call", "compress", "ok", "y".repeat(9000)), + mk("R_OK", "tool", "tool-result", "compress", "ok", SUCCESS_RECEIPT), + mk("REASON", "assistant", "reasoning"), + mk("C_FAIL", "assistant", "tool-call", "compress", "fail", "z".repeat(9000)), + mk("R_FAIL", "tool", "tool-result", "compress", "fail", FAILED_RECEIPT), + mk("U2", "user", "text"), + mk("A2", "assistant", "text"), + ]; +} + +function runTurn(messages: CoreMessage[], config: Config, state?: CompressionState) { + const core = createCore(); + return core.processTurn({ + messages, + state: state ?? createInitialState(), + config, + tokenCount: 1000, + }); +} + +test("#4289 unit: collectFailedCompressCallIds marks only unrecognized-name-free total failures", () => { + const msgs = [ + mk("c1", "assistant", "tool-call", "compress", "t1"), + mk("r1", "tool", "tool-result", "compress", "t1", FAILED_RECEIPT), + mk("c2", "assistant", "tool-call", "bash", "t2"), + mk("r2", "tool", "tool-result", "bash", "t2", FAILED_RECEIPT), + mk("c3", "assistant", "tool-call", "compress", "t3"), + // Unnamed result: recognition falls back to the paired call's toolName. + mk("r3", "tool", "tool-result", undefined, "t3", FAILED_RECEIPT), + mk("r4", "tool", "tool-result", undefined, "t4", FAILED_RECEIPT), + mk("c5", "assistant", "tool-call", "compress", "t5"), + mk("r5", "tool", "tool-result", "compress", "t5", SUCCESS_RECEIPT), + ]; + const ids = collectFailedCompressCallIds(msgs, {}); + assert.deepEqual( + [...ids].sort(), + ["t1", "t3"], + "failed bare-name and unnamed-paired compress results marked; bash-shaped text, orphan result, success receipt are not", + ); +}); + +test("#4289 refs: failed compress exchange gets foldable numeric refs; successful stays BLOCKED", () => { + const messages = mainGeometry(); + const config = biliConfig(); + const turn = runTurn(messages, config); + const refs = turn.state.messageRefs.byRaw; + + assert.equal(refs["C_OK"], BLOCKED_REF, "successful call stays protected"); + assert.equal(refs["R_OK"], BLOCKED_REF, "successful result stays protected"); + assert.match(refs["C_FAIL"]!, /^m\d+$/, "failed call migrates to a numeric ref"); + assert.match(refs["R_FAIL"]!, /^m\d+$/, "failed result migrates to a numeric ref"); + assert.notEqual( + refs["C_FAIL"], + refs["R_FAIL"], + "each half keeps its own distinct ref (id-never-reused)", + ); +}); + +test("#4289 recommend: failed exchange is advertised, successful exchange is not", () => { + const messages = mainGeometry(); + const config = biliConfig(); + const turn = runTurn(messages, config); + const ctx = buildCompressibleRanges(messages, turn.state, config); + + const idx = new Map(messages.map((m, i) => [m.id, i])); + const failLo = idx.get("C_FAIL")!; + const failHi = idx.get("R_FAIL")!; + const okLo = idx.get("C_OK")!; + const okHi = idx.get("R_OK")!; + + assert.ok( + ctx.compressible.some( + (r) => + (r.startIndex ?? 0) <= failLo && + (r.endIndex ?? 0) >= failHi, + ), + "a compressible range spans the failed exchange", + ); + // Ref-level invariant with teeth: advertised ranges are ref-addressed, and + // the successful exchange holds no numeric refs at all — so it can be + // neither an endpoint nor an interior member of any range's ref span. + // (Positional spans MAY cross the still-BLOCKED pair; BLOCKED messages + // consume no slot and break no adjacency, and the apply-side carve-out + // keeps them out of the folded set — proven by the apply test below.) + const coveredIds = new Set(); + for (const r of ctx.compressible) { + const lo = refToIndex(r.startRef); + const hi = refToIndex(r.endRef); + if (lo === null || hi === null) continue; + for (let n = lo; n <= hi; n++) { + const id = turn.state.messageRefs.byRef[indexToRef(n)]; + if (id) coveredIds.add(id); + } + } + assert.ok(coveredIds.has("C_FAIL") && coveredIds.has("R_FAIL"), + "the failed exchange is addressable through advertised refs"); + assert.ok(!coveredIds.has("C_OK") && !coveredIds.has("R_OK"), + "the successful exchange remains unaddressable by any advertised range"); +}); + +test("#4289 apply: the full span folds the failed exchange AND the reasoning it had pinned; the successful exchange is carved out, not folded", () => { + const core = createCore(); + const messages = mainGeometry(); + const config = biliConfig(); + const turn = runTurn(messages, config); + const refs = turn.state.messageRefs.byRaw; + + const result = core.applyCompression({ + ranges: [{ startRef: refs["U1"]!, endRef: refs["A2"]!, summary: VALID_SUMMARY }], + messages, + state: structuredClone(turn.state), + config, + }); + + assert.equal(result.result.errors.length, 0, result.result.errors.join("; ")); + assert.equal(result.result.blocksCreated, 1); + const block = result.state.blocks[0]!; + for (const id of ["U1", "A1", "REASON", "C_FAIL", "R_FAIL", "U2", "A2"]) { + assert.ok( + block.effectiveMessageIds.includes(id), + `${id} folded (the reasoning nailed behind the failed call goes with it)`, + ); + } + assert.ok( + !block.effectiveMessageIds.includes("C_OK"), + "successful call stays visible (carved out, not folded)", + ); + assert.ok( + !block.effectiveMessageIds.includes("R_OK"), + "successful result stays visible (carved out, not folded)", + ); +}); + +test("#4289 pairing: an unnamed failed result (toolCallId only) is unpinned too", () => { + const messages = [ + ...mainGeometry(), + mk("C_F2", "assistant", "tool-call", "compress", "fail2", "z".repeat(9000)), + mk("R_F2", "tool", "tool-result", undefined, "fail2", FAILED_RECEIPT), + ]; + const config = biliConfig(); + const turn = runTurn(messages, config); + const refs = turn.state.messageRefs.byRaw; + assert.match(refs["C_F2"]!, /^m\d+$/); + assert.match(refs["R_F2"]!, /^m\d+$/, "unnamed result unpinned via the pairing set"); +}); + +test("#4289 conservative: in-flight compress call (no result yet) stays protected", () => { + const messages = [ + mk("U1", "user", "text"), + mk("C_INFLIGHT", "assistant", "tool-call", "compress", "inflight", "z".repeat(9000)), + mk("U2", "user", "text"), + ]; + const turn = runTurn(messages, biliConfig()); + assert.equal( + turn.state.messageRefs.byRaw["C_INFLIGHT"], + BLOCKED_REF, + "a result-less call keeps its exemption until its receipt arrives", + ); +}); + +test("#4289 conservative: partial-success receipt (starts '[Compressed') stays protected", () => { + const messages = [ + mk("U1", "user", "text"), + mk("C_PART", "assistant", "tool-call", "compress", "part", "z".repeat(9000)), + mk("R_PART", "tool", "tool-result", "compress", "part", PARTIAL_RECEIPT), + mk("U2", "user", "text"), + ]; + const turn = runTurn(messages, biliConfig()); + assert.equal(turn.state.messageRefs.byRaw["C_PART"], BLOCKED_REF); + assert.equal(turn.state.messageRefs.byRaw["R_PART"], BLOCKED_REF); +}); + +test("#4289 acp_rule: untouched by the carve-out, still BLOCKED", () => { + const messages = [ + mk("U1", "user", "text"), + mk("C_RULE", "assistant", "tool-call", "acp_rule", "rule"), + mk("R_RULE", "tool", "tool-result", "acp_rule", "rule", "rule text"), + mk("U2", "user", "text"), + ]; + const turn = runTurn(messages, biliConfig()); + assert.equal(turn.state.messageRefs.byRaw["C_RULE"], BLOCKED_REF); + assert.equal(turn.state.messageRefs.byRaw["R_RULE"], BLOCKED_REF); +}); + +test("#4289 host intent outranks the carve-out: isMessageProtected hook still pins a failed exchange", () => { + const messages = mainGeometry(); + const config = biliConfig({ + isMessageProtected: (m) => m.id === "C_FAIL" || m.id === "R_FAIL", + }); + const turn = runTurn(messages, config); + assert.equal(turn.state.messageRefs.byRaw["C_FAIL"], BLOCKED_REF); + assert.equal(turn.state.messageRefs.byRaw["R_FAIL"], BLOCKED_REF); +}); + +test("#4289 host intent outranks the carve-out: protectedTools pattern still pins a failed exchange", () => { + const messages = mainGeometry(); + const config = biliConfig({ protectedTools: ["compress"] }); + const turn = runTurn(messages, config); + assert.equal(turn.state.messageRefs.byRaw["C_FAIL"], BLOCKED_REF); + assert.equal(turn.state.messageRefs.byRaw["R_FAIL"], BLOCKED_REF); +}); + +test("#4289 self-heal: stale BLOCKED refs from a pre-fix session migrate to numeric refs on the next turn", () => { + const messages = mainGeometry(); + const config = biliConfig(); + // Simulate the pre-upgrade ledger: every compress half was BLOCKED back + // then (name-based exemption), everything else held a live ref. + const state = createInitialState(); + state.messageRefs = { + byRaw: { + U1: "m00001", + A1: "m00002", + C_OK: BLOCKED_REF, + R_OK: BLOCKED_REF, + REASON: "m00003", + C_FAIL: BLOCKED_REF, + R_FAIL: BLOCKED_REF, + U2: "m00004", + A2: "m00005", + }, + byRef: {}, + }; + const turn = runTurn(messages, config, state); + const refs = turn.state.messageRefs.byRaw; + + assert.match(refs["C_FAIL"]!, /^m\d+$/, "stale failed call migrated"); + assert.match(refs["R_FAIL"]!, /^m\d+$/, "stale failed result migrated"); + assert.equal(refs["C_OK"], BLOCKED_REF, "successful call stays BLOCKED through the pass"); + assert.equal(refs["R_OK"], BLOCKED_REF, "successful result stays BLOCKED through the pass"); + for (const id of ["C_FAIL", "R_FAIL"]) { + assert.ok( + (turn.state.renderDeferredRefs ?? []).includes(refs[id]!), + `${id}'s fresh ref defers its tag re-render to the next fold boundary (#3939②)`, + ); + } +}); + +test("#4289 MCP dialect: mcp__bili__compress failure folds, success stays BLOCKED (isToolProtected path)", () => { + const messages = [ + mk("U1", "user", "text"), + mk("C_MOK", "assistant", "tool-call", "mcp__bili__compress", "mok", "y".repeat(9000)), + mk("R_MOK", "tool", "tool-result", "mcp__bili__compress", "mok", SUCCESS_RECEIPT), + mk("C_MFAIL", "assistant", "tool-call", "mcp__bili__compress", "mfail", "z".repeat(9000)), + mk("R_MFAIL", "tool", "tool-result", "mcp__bili__compress", "mfail", FAILED_RECEIPT), + mk("U2", "user", "text"), + ]; + const turn = runTurn(messages, biliConfig()); + const refs = turn.state.messageRefs.byRaw; + assert.equal(refs["C_MOK"], BLOCKED_REF, "MCP success stays protected via the hook"); + assert.equal(refs["R_MOK"], BLOCKED_REF); + assert.match(refs["C_MFAIL"]!, /^m\d+$/, "MCP failure lifted via the same carve-out"); + assert.match(refs["R_MFAIL"]!, /^m\d+$/); +});