diff --git a/kernel/package-lock.json b/kernel/package-lock.json index 00da442c39..df80849a7c 100644 --- a/kernel/package-lock.json +++ b/kernel/package-lock.json @@ -1,12 +1,12 @@ { "name": "acp-kernel", - "version": "0.0.160", + "version": "0.0.161", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "acp-kernel", - "version": "0.0.160", + "version": "0.0.161", "license": "MIT", "devDependencies": { "@types/node": "^22.0.0", diff --git a/kernel/package.json b/kernel/package.json index 976310a02e..e41b7fbd61 100644 --- a/kernel/package.json +++ b/kernel/package.json @@ -1,6 +1,6 @@ { "name": "acp-kernel", - "version": "0.0.160", + "version": "0.0.161", "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/wire/anthropic.ts b/kernel/src/wire/anthropic.ts index 9a97a4d4de..e73790335e 100644 --- a/kernel/src/wire/anthropic.ts +++ b/kernel/src/wire/anthropic.ts @@ -387,11 +387,26 @@ function isDirectiveOnlySystem(e: AnthropicWireMessage): boolean { ); } +// #4400: tool_addition/tool_removal blocks are ONLY valid inside role:system +// messages (WC-022), so re-voicing a system that carries them would trade the +// position rejection for a guaranteed tool-block rejection — the same +// permanent 400 this guard exists to prevent. Anthropic accepts tool-update +// systems at any mid-conversation position (that is their purpose), so the +// orphaned tool-carrying system stays verbatim. +function carriesToolUpdateBlocks(e: AnthropicWireMessage): boolean { + if (!Array.isArray(e.content)) return false; + return e.content.some( + (b) => + typeof b?.type === "string" && + (b.type.startsWith("tool_addition") || b.type.startsWith("tool_removal")), + ); +} + function revoiceFoldOrphanedSystems(out: AnthropicWireMessage[]): void { for (let j = out.length - 1; j >= 0; j--) { const e = out[j]; if (!e || e.role !== "system") break; - if (isDirectiveOnlySystem(e)) continue; + if (isDirectiveOnlySystem(e) || carriesToolUpdateBlocks(e)) continue; out[j] = { ...e, role: "user" }; } } diff --git a/kernel/tests/issue4292-orphan-system-guard.test.ts b/kernel/tests/issue4292-orphan-system-guard.test.ts index 46364e0736..9728c6605b 100644 --- a/kernel/tests/issue4292-orphan-system-guard.test.ts +++ b/kernel/tests/issue4292-orphan-system-guard.test.ts @@ -85,12 +85,9 @@ test("#4292 shape: sidecar system immediately before the anchor is re-voiced", ( }); }); -test("consecutive orphaned systems before the anchor re-voice as a whole run", () => { +test("consecutive plain orphaned systems before the anchor re-voice as a whole run", () => { const S1 = sys([{ type: "text", text: "first" }]); - const S2 = sys([ - { type: "text", text: "second" }, - TOOL_ADDITION, - ]); + const S2 = sys([{"type":"text","text":"second"}]); const rebuilt = coreToAnthropic([ { id: "u0", role: "user", contentType: "text", text: "q1" }, { id: "s1", role: "system", contentType: "text", text: "first", rawAnthropicMessage: S1 }, @@ -112,6 +109,46 @@ test("consecutive orphaned systems before the anchor re-voice as a whole run", ( }); }); +// #4400: a tool-carrying system may NOT be re-voiced — role:user with +// tool_addition blocks is a guaranteed Anthropic rejection (WC-022), the +// same permanent 400 the guard exists to prevent. Tool-update systems are +// accepted at any mid-conversation position, so they stay verbatim. +test("#4400 shape: tool-carrying orphan adjacent to the anchor stays system", () => { + const S = sys([{ type: "text", text: "tools updated" }, TOOL_ADDITION]); + const rebuilt = coreToAnthropic([ + { id: "u0", role: "user", contentType: "text", text: "q1" }, + { id: "s1", role: "system", contentType: "text", text: "tools updated", rawAnthropicMessage: S }, + anchor("a1", "[Compressed conversation section] folded"), + { id: "t1", role: "user", contentType: "text", text: "next" }, + ] as never); + assert.deepEqual(rebuilt[1], S); + assert.equal(rebuilt[2]!.role, "user"); +}); + +test("#4400 mixed run: plain system re-voices, tool-carrying system in the same run stays", () => { + const S1 = sys([{ type: "text", text: "plain" }]); + const S2 = sys([{ type: "text", text: "tools" }, TOOL_ADDITION]); + const rebuilt = coreToAnthropic([ + { id: "u0", role: "user", contentType: "text", text: "q1" }, + { id: "s1", role: "system", contentType: "text", text: "plain", rawAnthropicMessage: S1 }, + { id: "s2", role: "system", contentType: "text", text: "tools", rawAnthropicMessage: S2 }, + anchor("a1", "[Compressed conversation section] folded"), + ] as never); + assert.equal(rebuilt[1]!.role, "user"); + assert.deepEqual(rebuilt[1]!.content, S1.content); + assert.deepEqual(rebuilt[2], S2); +}); + +test("#4400: tool_removal-carrying orphan is exempt too", () => { + const S = sys([{ type: "tool_removal", tool_name: "t" } as never]); + const rebuilt = coreToAnthropic([ + { id: "u0", role: "user", contentType: "text", text: "q1" }, + { id: "s1", role: "system", contentType: "text", text: "[system]", rawAnthropicMessage: S }, + anchor("a1", "[Compressed conversation section] folded"), + ] as never); + assert.deepEqual(rebuilt[1], S); +}); + test("re-voicing substitutes fresh objects; the stored sidecar is never mutated", () => { const S = sys([{ type: "text", text: "delta" }]); const sidecarCore: BiliMessage = { diff --git a/package-lock.json b/package-lock.json index a252c0d3f4..4e3d5881b9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -50,7 +50,7 @@ }, "kernel": { "name": "acp-kernel", - "version": "0.0.160", + "version": "0.0.161", "dev": true, "license": "MIT", "devDependencies": { diff --git a/tests/issue4292-anthropic-fold-orphan-system.test.ts b/tests/issue4292-anthropic-fold-orphan-system.test.ts index 73da404d65..00826c0085 100644 --- a/tests/issue4292-anthropic-fold-orphan-system.test.ts +++ b/tests/issue4292-anthropic-fold-orphan-system.test.ts @@ -54,6 +54,40 @@ const CLIENT_SYSTEM: AnthropicSystemMessage = { ], }; +// The plain-text sibling of CLIENT_SYSTEM: no tool blocks, so the #4292 +// fold guard re-voices it (position repair only — nothing in the payload +// constrains its role). +const PLAIN_CLIENT_SYSTEM: AnthropicSystemMessage = { + role: "system", + content: [ + { + type: "text", + text: 'Updated system prompt section "tools":\n…', + }, + ], +}; + +/** #4400: tool-carrying systems are exempt from the plain-system position + * rule — tool_addition/tool_removal blocks are accepted mid-conversation, + * and re-voicing them to user is a guaranteed rejection (WC-022). */ +function positionIllegalSystems(messages: AnthropicWireMessage[]): number[] { + return orphanSystems(messages).filter( + (i) => !carriesToolUpdate(messages[i]!), + ); +} + +function carriesToolUpdate(m: AnthropicWireMessage): boolean { + return ( + Array.isArray(m.content) && + m.content.some( + (b) => + typeof b?.type === "string" && + (b.type.startsWith("tool_addition") || + b.type.startsWith("tool_removal")), + ) + ); +} + const ANCHOR_TEXT = "[Compressed conversation section] early turns folded"; // Pre-fold history: the client system was immediately followed by an @@ -69,11 +103,18 @@ const preFoldWire = [ /** Post-fold core list: the fold covered everything after the client system; * the kernel-rendered anchor (no sidecar) sits at the earliest covered * position — immediately after the orphaned system. */ -function postFoldCore(): BiliMessage[] { - const { msgs } = anthropicToCore({ messages: preFoldWire } as never); +function postFoldCore(clientSystem: AnthropicSystemMessage = CLIENT_SYSTEM): BiliMessage[] { + const preFold = [ + { role: "user", content: [{ type: "text", text: "enable subagents" }] }, + clientSystem, + { role: "assistant", content: [{ type: "text", text: "subagents enabled" }] }, + { role: "user", content: [{ type: "text", text: "run one" }] }, + { role: "assistant", content: [{ type: "text", text: "done" }] }, + ] as AnthropicWireMessage[]; + const { msgs } = anthropicToCore({ messages: preFold } as never); return [ msgs[0] as BiliMessage, // user: "enable subagents" - msgs[1] as BiliMessage, // sidecar system (CLIENT_SYSTEM) + msgs[1] as BiliMessage, // sidecar system (clientSystem) { id: "anchor", role: "system", contentType: "text", text: ANCHOR_TEXT }, { id: "tail", role: "user", contentType: "text", text: "next question" }, ]; @@ -86,29 +127,33 @@ test("pre-fold wire round-trips byte-identical (no fold, no re-voicing)", () => assert.deepEqual(orphanSystems(rebuilt), []); }); -test("without the anchor adjacent, the orphaned system would stay system (documents #4292)", () => { +// Pre-fold history fixture kept for the round-trip test above; postFoldCore +// takes the client system as a parameter (#4400 plain/tool-carrying split). + +test("without the anchor adjacent, the orphaned plain system stays system (documents #4292)", () => { // The fold covered a DIFFERENT region: the client system keeps its // verbatim system emission even though the assistants around it are gone. - // That emission is what Anthropic rejects — the kernel only re-voices the - // anchor-adjacency signature, so this shape is the host's to avoid. - const cores = postFoldCore().filter((m) => m.id !== "anchor"); + // For a PLAIN system that emission is positionally illegal — the kernel + // only re-voices the anchor-adjacency signature, so this shape is the + // host's to avoid. (Tool-carrying systems are legal anywhere, #4400.) + const cores = postFoldCore(PLAIN_CLIENT_SYSTEM).filter((m) => m.id !== "anchor"); const rebuilt = coreToAnthropic(cores as never); assert.equal(rebuilt[1]!.role, "system"); - assert.deepEqual(rebuilt[1]!.content, CLIENT_SYSTEM.content); + assert.deepEqual(rebuilt[1]!.content, PLAIN_CLIENT_SYSTEM.content); assert.deepEqual(orphanSystems(rebuilt), [1]); }); -test("post-fold: anchor adjacency re-voices the orphan, bytes preserved", () => { - const cores = postFoldCore(); +test("post-fold: plain orphan adjacent to the anchor re-voices, bytes preserved (#4292)", () => { + const cores = postFoldCore(PLAIN_CLIENT_SYSTEM); const rebuilt = coreToAnthropic(cores as never); assert.deepEqual( - orphanSystems(rebuilt), + positionIllegalSystems(rebuilt), [], "no illegal-position system remains after the fold guard", ); - // The orphaned system became a user message with byte-identical blocks. + // The orphaned plain system became a user message with byte-identical blocks. assert.equal(rebuilt[1]!.role, "user"); - assert.deepEqual(rebuilt[1]!.content, CLIENT_SYSTEM.content); + assert.deepEqual(rebuilt[1]!.content, PLAIN_CLIENT_SYSTEM.content); // Anchor + tail keep their user mapping with verbatim text. assert.deepEqual(rebuilt[2], { role: "user", @@ -124,8 +169,30 @@ test("post-fold: anchor adjacency re-voices the orphan, bytes preserved", () => ); }); -test("post-fold with a surviving assistant after the anchor stays fully legal", () => { - const cores = postFoldCore(); +test("post-fold: tool-carrying orphan adjacent to the anchor stays system verbatim (#4400)", () => { + // Re-voicing would ship tool_addition blocks in a user message — a + // guaranteed Anthropic rejection (WC-022), the exact death #4400 records. + const cores = postFoldCore(CLIENT_SYSTEM); + const rebuilt = coreToAnthropic(cores as never); + assert.deepEqual(rebuilt[1], CLIENT_SYSTEM); + assert.equal( + (cores[1]!.rawAnthropicMessage as AnthropicSystemMessage).role, + "system", + "sidecar never mutated", + ); + assert.deepEqual(positionIllegalSystems(rebuilt), []); + // Anchor + tail keep their user mapping with verbatim text. + assert.deepEqual(rebuilt[2], { + role: "user", + content: [ + { type: "text", text: ANCHOR_TEXT }, + { type: "text", text: "next question" }, + ], + }); +}); + +test("post-fold plain orphan with a surviving assistant after the anchor stays fully legal", () => { + const cores = postFoldCore(PLAIN_CLIENT_SYSTEM); cores.push({ id: "a2", role: "assistant", @@ -133,7 +200,7 @@ test("post-fold with a surviving assistant after the anchor stays fully legal", text: "final answer", }); const rebuilt = coreToAnthropic(cores as never); - assert.deepEqual(orphanSystems(rebuilt), []); + assert.deepEqual(positionIllegalSystems(rebuilt), []); assert.equal(rebuilt[1]!.role, "user"); assert.deepEqual(rebuilt[3], { role: "assistant",