Skip to content
Closed
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.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",
Expand Down
17 changes: 16 additions & 1 deletion kernel/src/wire/anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" };
}
}
Expand Down
47 changes: 42 additions & 5 deletions kernel/tests/issue4292-orphan-system-guard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand All @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

99 changes: 83 additions & 16 deletions tests/issue4292-anthropic-fold-orphan-system.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<tools>…</tools>',
},
],
};

/** #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
Expand All @@ -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" },
];
Expand All @@ -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",
Expand All @@ -124,16 +169,38 @@ 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",
contentType: "text",
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",
Expand Down
Loading