kernel: exempt MCP-form compress calls from orphan stripping via isCompressTool

#3222 mandatory fix 2. stripOrphanedToolCalls hardcoded toolName === "compress", so a pending mcp__bili__compress call (no result yet) was deleted as an orphan under active coverage while its bare-name twin survived. prune() now takes an optional isCompressTool predicate (same family as Config.isCompressTool / hideConsumedCompressCalls); unset stays bare-name only, byte-identical to before. pruneNode passes ctx.config.isCompressTool through; renderHandoff keeps the default (no config on that path). Bumps acp-kernel to 0.0.125.
This commit is contained in:
awork
2026-10-15 10:53:29 +08:00
parent 0cd865b195
commit 164f5d9e4f
4 changed files with 97 additions and 5 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "acp-kernel",
"version": "0.0.124",
"version": "0.0.125",
"description": "Framework-agnostic context-compression engine (model-driven, 3-tier LSM). Pure core: no host dependency.",
"license": "MIT",
"author": "ranxianglei",
+4 -2
View File
@@ -966,10 +966,12 @@ const syncBlocksNode: PipelineNode = {
const pruneNode: PipelineNode = {
name: "prune",
run(io) {
run(io, ctx) {
return {
...io,
messages: prune(io.messages, io.state),
messages: prune(io.messages, io.state, {
isCompressTool: ctx.config.isCompressTool,
}),
// #2663: keep the pre-prune resent array reachable so recommend can
// judge foldability on the same input class applyCompression sees.
effects: { ...io.effects, originalMessages: io.messages },
+15 -2
View File
@@ -70,6 +70,13 @@ export interface PruneOptions {
* provider prefix-cache head (#2733).
*/
headSummaryAnchors?: boolean;
/**
* Names that count as a `compress` call for the pending-call exemption in
* orphan stripping (same predicate family as Config.isCompressTool). Unset =
* bare-name only, byte-identical to before; without it an MCP-form compress
* call still awaiting its result is deleted while its bare-name twin survives.
*/
isCompressTool?: (toolName: string) => boolean;
}
export function prune(
@@ -83,6 +90,8 @@ export function prune(
for (const id of covered) coveredBases.add(baseIdOf(id));
const inject = options.injectSummaries ?? true;
const isCompressTool =
options.isCompressTool ?? ((name: string) => name === "compress");
const firstUserIndex = messages.findIndex(
(message) => message.role === "user",
);
@@ -129,6 +138,7 @@ export function prune(
knownSummaryIds,
options.headSummaryAnchors === true,
),
isCompressTool,
),
),
);
@@ -404,7 +414,10 @@ function stripOrphanedToolResults(messages: CoreMessage[]): CoreMessage[] {
);
}
function stripOrphanedToolCalls(messages: CoreMessage[]): CoreMessage[] {
function stripOrphanedToolCalls(
messages: CoreMessage[],
isCompressTool: (toolName: string) => boolean,
): CoreMessage[] {
const knownResultIds = new Set<string>();
for (const m of messages) {
if (m.contentType === "tool-result" && m.toolCallId) {
@@ -415,7 +428,7 @@ function stripOrphanedToolCalls(messages: CoreMessage[]): CoreMessage[] {
(m) =>
m.contentType !== "tool-call" ||
!m.toolCallId ||
m.toolName === "compress" ||
(typeof m.toolName === "string" && isCompressTool(m.toolName)) ||
knownResultIds.has(m.toolCallId),
);
}
+77
View File
@@ -0,0 +1,77 @@
// #3222 必改 2: stripOrphanedToolCalls exempted only the bare "compress" name
// from orphan-call removal, so a pending mcp__bili__compress call (no result
// yet) was stripped while the bare-name twin survived under the same coverage.
// prune now takes isCompressTool (same predicate family as Config.isCompressTool
// / hideConsumedCompressCalls); compress.ts's pruneNode passes the host
// predicate through, and unset options stay byte-identical to before.
import { test } from "node:test";
import assert from "node:assert/strict";
import { prune } from "../src/prune.js";
import { createInitialState } from "../src/state.js";
import type { CompressionBlock, CompressionState, CoreMessage } from "../src/types.js";
// Host-injected predicate mirroring src/config.ts isMcpBiliCompress (#2970).
const isMcp = (name: string): boolean =>
name === "compress" || name === "mcp__bili__compress";
function block(overrides: Partial<CompressionBlock>): CompressionBlock {
return {
blockId: "b0",
runId: "r0",
tier: 1,
summary: "summary",
directMessageIds: [],
effectiveMessageIds: [],
directBlockIds: [],
createdAt: 1000,
survivedCount: 0,
generation: "young",
active: true,
...overrides,
};
}
// Active coverage over raw1/raw2 so prune takes the real coverage path
// (covered.size > 0 — without it prune early-returns and never reaches
// stripOrphanedToolCalls).
function stateWithActiveCoverage(): CompressionState {
const s = createInitialState();
s.blocks.push(block({ blockId: "b1", effectiveMessageIds: ["raw1", "raw2"] }));
return s;
}
function seq(callName: string): CoreMessage[] {
return [
{ id: "raw1", role: "user", contentType: "text", text: "a" },
{ id: "raw2", role: "assistant", contentType: "text", text: "b" },
{ id: "later", role: "user", contentType: "text", text: "c" },
{ id: "pend", role: "assistant", contentType: "tool-call", toolName: callName, toolCallId: "c1", text: "{}" },
];
}
test("bare compress pending call survives an active coverage pass (pre-existing behavior)", () => {
const out = prune(seq("compress"), stateWithActiveCoverage());
assert.ok(out.some((m) => m.id === "pend"), "pending bare compress call kept");
});
test("#3222: pending mcp__bili__compress call survives with the host predicate (parity with bare name)", () => {
const out = prune(seq("mcp__bili__compress"), stateWithActiveCoverage(), { isCompressTool: isMcp });
assert.ok(out.some((m) => m.id === "pend"), "pending MCP compress call kept like its bare-name twin");
});
test("normal orphan calls are still stripped when the predicate is set", () => {
const out = prune(seq("bash"), stateWithActiveCoverage(), { isCompressTool: isMcp });
assert.ok(!out.some((m) => m.id === "pend"), "bash orphan still stripped");
});
test("other MCP tools are not misidentified as compress", () => {
const out = prune(seq("mcp__other__thing"), stateWithActiveCoverage(), { isCompressTool: isMcp });
assert.ok(!out.some((m) => m.id === "pend"), "foreign MCP tool still orphan-stripped");
});
test("default options stay byte-identical to pre-fix behavior (bare-name only)", () => {
const mcp = prune(seq("mcp__bili__compress"), stateWithActiveCoverage());
assert.ok(!mcp.some((m) => m.id === "pend"), "without the predicate the MCP call is still stripped");
const bare = prune(seq("compress"), stateWithActiveCoverage());
assert.ok(bare.some((m) => m.id === "pend"), "bare-name exemption unchanged under defaults");
});