Skip to content
Merged
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
85 changes: 59 additions & 26 deletions kernel/src/wire/anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,21 +363,11 @@ function rebuildSystemMessage(
};
}

// #4292: bili's prune inserts each fold summary at the earliest covered
// position (kernel/src/prune.ts insertAt), so a client mid-conversation
// system message (verbatim via rawAnthropicMessage) orphaned by that fold is
// ALWAYS immediately followed by the no-sidecar core system anchor —
// emitting [.., system, anchor-as-user, ..] makes Anthropic 400 the session
// permanently ("role 'system' must precede an 'assistant' message or end
// the array"). Re-voice exactly that adjacency to user; every other client
// layout stays verbatim — the kernel is transport, not a validator, so even
// positionally-illegal client shapes round-trip byte-exact. Fresh objects
// are substituted, never mutations, so session sidecars stay intact.
// A directive-only system (empty content + output_config) is accepted at ANY
// position by Anthropic's mid-conversation-system rule, so it is never part of
// the orphan signature. Re-voicing it would mint an empty-content user message
// that 400s on its own — leave those in place (mirrors the reporter's
// isAnthropicDirectiveOnlySystem; #4292 hardening).
// #4292 hardening, kept for #4457's backstop: a directive-only system
// (empty content + output_config) is accepted at ANY position by
// Anthropic's mid-conversation-system rule, so it is never healed; demoting
// it would mint an empty-content user message that 400s on its own
// (mirrors the reporter's isAnthropicDirectiveOnlySystem).
function isDirectiveOnlySystem(e: AnthropicWireMessage): boolean {
return (
e.role === "system" &&
Expand All @@ -387,12 +377,57 @@ function isDirectiveOnlySystem(e: AnthropicWireMessage): boolean {
);
}

function revoiceFoldOrphanedSystems(out: AnthropicWireMessage[]): void {
for (let j = out.length - 1; j >= 0; j--) {
// #4457: Anthropic accepts a mid-conversation system only immediately
// before an assistant message or at the end of the array — any other
// position 400s the whole request, permanently killing the session (a run
// of consecutive systems is judged by the run's first non-system follower:
// Pi's trailing tool-delta + effort-marker runs are legal at array end).
// The fold layer keeps sidecar systems OUT of folds (kernel/src/protected.ts
// never-fold carve): their tool deltas are FUNCTIONAL under Anthropic's
// mid-conversation-tool-changes contract — tools[] stays static for cache
// stability and the deltas control which tools are offered (defer_loading
// surfacing, by-value definitions, removals, MCP toolsets) — so folding them
// would silently alter the effective tool set. Orphans still arise when a
// fold covers the assistant that legitimized one; this position-based
// backstop heals whatever still arrives illegal: sessions folded by older
// builds and positionally-illegal client input. Demotion re-voices to user
// with tool_addition/tool_removal blocks stripped first (WC-022 forbids them
// outside system) and drops the entry when nothing remains; a
// directive-only system (empty content + output_config) is accepted at ANY
// position, so it is never healed. Fresh objects only — sidecars stay
// intact for future verbatim emission once re-legitimized.
function isToolUpdateBlock(b: { type?: unknown }): boolean {
return b.type === "tool_addition" || b.type === "tool_removal";
}

function healIllegalSystemPositions(out: AnthropicWireMessage[]): void {
for (let j = 0; j < out.length; j++) {
const e = out[j];
if (!e || e.role !== "system") break;
if (isDirectiveOnlySystem(e)) continue;
out[j] = { ...e, role: "user" };
if (!e || e.role !== "system" || isDirectiveOnlySystem(e)) continue;
let k = j + 1;
while (k < out.length && out[k]!.role === "system") k++;
const next = out[k];
if (!next || next.role === "assistant") continue;
if (typeof e.content === "string") {
// An empty string demoted to user would mint an empty-content user
// message, which 400s on its own — drop instead.
if (e.content === "") {
out.splice(j, 1);
j--;
} else {
out[j] = { role: "user", content: e.content };
}
continue;
}
const kept = e.content.filter(
(b) => !isToolUpdateBlock(b as { type?: unknown }),
);
if (kept.length === 0) {
out.splice(j, 1);
j--;
continue;
}
out[j] = { role: "user", content: kept };
}
}

Expand All @@ -419,12 +454,9 @@ export function coreToAnthropic(
out.push(rebuildSystemMessage(m, cacheControls?.get(m.id)));
continue;
}
// #4292: a no-sidecar core system is a kernel-rendered fold anchor; the
// sidecar systems directly before it (current === null ⇒ the previous
// core emitted one) are the fold-orphaned run — re-voice it to user.
if (m.role === "system" && current === null) {
revoiceFoldOrphanedSystems(out);
}
// #4292/#4457: fold-orphaned and client-illegal system positions are
// healed by the position-based backstop after assembly (see below) —
// no shape matching here.
const target: "user" | "assistant" =
m.role === "assistant" ? "assistant" : "user";
if (!current || current.role !== target) {
Expand Down Expand Up @@ -472,6 +504,7 @@ export function coreToAnthropic(
}
}
flush();
healIllegalSystemPositions(out);
return out;
}

Expand Down
19 changes: 17 additions & 2 deletions kernel/tests/anthropic-system-message-passthrough.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ test("one core per system wire message, role system, wireIndex kept", () => {
assert.match(systems[1]!.text as string, /\[tool_addition\]$/);
});

test("system message between two user turns does not merge them", () => {
test("system message between two user turns heals to user (#4457)", () => {
// [user, system, user] is positionally illegal on the anthropic wire —
// the backstop re-voices it (tool blocks stripped per WC-022) instead of
// forwarding a request Anthropic guarantees to 400.
const body = {
messages: [
{ role: "user", content: [{ type: "text", text: "a" }] },
Expand All @@ -114,7 +117,19 @@ test("system message between two user turns does not merge them", () => {
],
};
const rebuilt = coreToAnthropic(anthropicToCore(body as never).msgs as never);
assert.deepEqual(rebuilt, body.messages);
assert.deepEqual(rebuilt, [
{ role: "user", content: [{ type: "text", text: "a" }] },
{
role: "user",
content: [
{
type: "text",
text: 'Updated system prompt section "tools":\n<tools>…</tools>',
},
],
},
{ role: "user", content: [{ type: "text", text: "b" }] },
]);
});

test("string-content system message round-trips", () => {
Expand Down
106 changes: 87 additions & 19 deletions kernel/tests/issue4292-orphan-system-guard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ import type {
} from "../src/wire/anthropic.js";
import type { BiliMessage } from "../src/wire/bili-message.js";

// #4292: a fold covers the assistant messages that legitimized a client
// mid-conversation system message. Because prune inserts each summary at
// the EARLIEST covered position (kernel/src/prune.ts insertAt), the orphaned
// sidecar system is always IMMEDIATELY followed by the no-sidecar core
// system anchor (rendered summary), which emits as user — producing
// [.., system, user, ..] that Anthropic rejects ("role 'system' must precede
// an 'assistant' message or end the array"), 400ing the session forever.
// coreToAnthropic re-voices exactly that adjacency (the fold signature) to
// user. Everything else emits verbatim: the kernel is transport, not a
// validator, so positionally-illegal client layouts round-trip byte-exact.
// #4292/#4457: a fold that covers the assistant messages legitimizing a
// client mid-conversation system message leaves it in a wire position
// Anthropic rejects ("role 'system' must precede an 'assistant' message or
// end the array"), 400ing the session forever. The fold layer keeps these
// sidecar systems out of folds (their tool deltas are functional — see
// protected.ts), so orphans still arise; coreToAnthropic therefore ends
// with a POSITION-based backstop: any non-directive
// system whose first non-system follower is neither an assistant nor the
// array end is healed — re-voiced to user with tool_update blocks stripped
// (WC-022), dropped when nothing remains. Legal layouts (assistant next,
// array end, trailing runs, directive-only anywhere) round-trip verbatim.

function sys(content: AnthropicSystemMessage["content"]): AnthropicSystemMessage {
return { role: "system", content };
Expand Down Expand Up @@ -85,7 +86,7 @@ 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 orphaned systems before the anchor heal as a whole run", () => {
const S1 = sys([{ type: "text", text: "first" }]);
const S2 = sys([
{ type: "text", text: "second" },
Expand All @@ -102,7 +103,9 @@ test("consecutive orphaned systems before the anchor re-voice as a whole run", (
assert.equal(rebuilt[1]!.role, "user");
assert.deepEqual(rebuilt[1]!.content, S1.content);
assert.equal(rebuilt[2]!.role, "user");
assert.deepEqual(rebuilt[2]!.content, S2.content);
// #4457/WC-022: tool_update blocks never survive a demotion to user —
// the text block alone is re-voiced, byte-identical.
assert.deepEqual(rebuilt[2]!.content, [{ type: "text", text: "second" }]);
assert.deepEqual(rebuilt[3], {
role: "user",
content: [
Expand Down Expand Up @@ -141,13 +144,22 @@ test("Pi shape: system run at array end (no anchor after) round-trips verbatim",
assert.deepEqual(roundTrip({ messages }), messages);
});

test("sidecar system followed by a plain user message stays verbatim", () => {
test("sidecar system followed by a plain user message is healed (#4457 shape)", () => {
// [user, system, user] is exactly the #4457 wire: Anthropic 400s it
// ("system must precede an assistant message or end the array") — the
// backstop re-voices it to user instead of forwarding a dead request.
const messages = [
{ role: "user", content: [{ type: "text", text: "a" }] },
sys([{ type: "text", text: "between user turns" }]),
{ role: "user", content: [{ type: "text", text: "b" }] },
] as AnthropicWireMessage[];
assert.deepEqual(roundTrip({ messages }), messages);
const rebuilt = coreToAnthropic(anthropicToCore({ messages } as never).msgs as never);
assert.deepEqual(orphans(rebuilt), []);
assert.deepEqual(rebuilt, [
{ role: "user", content: [{ type: "text", text: "a" }] },
{ role: "user", content: [{ type: "text", text: "between user turns" }] },
{ role: "user", content: [{ type: "text", text: "b" }] },
]);
});

test("sidecar system immediately followed by an assistant stays verbatim", () => {
Expand Down Expand Up @@ -180,17 +192,18 @@ test("directive-only system adjacent to the anchor stays system (legal anywhere)
assert.equal(rebuilt[2]!.role, "user");
});

test("run separated from a later anchor by a user core stays verbatim", () => {
// The anchor adjacency must be immediate: a user core between the system
// and the anchor means the system was NOT orphaned by that fold.
test("system separated from a later anchor by a user core is healed too", () => {
// #4457/S2: the orphaning fold's anchor lands BEFORE the surviving
// system — no anchor adjacency exists, only the position rule catches it.
const rebuilt = coreToAnthropic([
{ id: "u0", role: "user", contentType: "text", text: "q1" },
{ id: "s1", role: "system", contentType: "text", text: "mid", rawAnthropicMessage: sys("mid") },
{ id: "u1", role: "user", contentType: "text", text: "gap" },
anchor("a1", "[Compressed conversation section] x"),
] as never);
assert.equal(rebuilt[1]!.role, "system");
assert.deepEqual(rebuilt[1], sys("mid"));
assert.deepEqual(orphans(rebuilt), []);
assert.equal(rebuilt[1]!.role, "user");
assert.equal(rebuilt[1]!.content, "mid");
});

test("sidecar system before an assistant and a later anchor stays verbatim", () => {
Expand Down Expand Up @@ -242,3 +255,58 @@ test("edge arrays are unaffected: empty, user-only, assistant-only", () => {
[{ role: "assistant", content: [{ type: "text", text: "hey" }] }],
);
});

// --- #4457 backstop edges ---------------------------------------------------

test("#4457: tool-block-only orphaned system is dropped, not re-voiced empty", () => {
const S = sys([TOOL_ADDITION]);
const rebuilt = coreToAnthropic([
{ id: "u0", role: "user", contentType: "text", text: "q1" },
{ id: "s1", role: "system", contentType: "text", text: "[tool_addition]", rawAnthropicMessage: S },
{ id: "u1", role: "user", contentType: "text", text: "after" },
] as never);
assert.deepEqual(orphans(rebuilt), []);
// The demotion drops the entry entirely (nothing re-voicable remains);
// the neighbouring user turns stay separate entries — the backstop
// never merges.
assert.deepEqual(rebuilt, [
{ role: "user", content: [{ type: "text", text: "q1" }] },
{ role: "user", content: [{ type: "text", text: "after" }] },
]);
});

test("#4457: string-content orphaned system re-voices with the string intact", () => {
const S = sys("policy delta");
const rebuilt = coreToAnthropic([
{ id: "u0", role: "user", contentType: "text", text: "q1" },
{ id: "s1", role: "system", contentType: "text", text: "policy delta", rawAnthropicMessage: S },
{ id: "u1", role: "user", contentType: "text", text: "after" },
] as never);
assert.deepEqual(rebuilt[1], { role: "user", content: "policy delta" });
});

test("#4457: consecutive system run is judged by its first non-system follower", () => {
// Pi's trailing tool-delta + effort-marker run at array end is LEGAL —
// byte-exact verbatim even though the first system's immediate next is
// another system, not an assistant.
const messages = [
{ role: "user", content: [{ type: "text", text: "a" }] },
sys([{ type: "text", text: "tools" }, TOOL_ADDITION]),
sys([{ type: "text", text: "trailing effort" }]),
] as AnthropicWireMessage[];
assert.deepEqual(roundTrip({ messages }), messages);
// The same run followed by a user turn is illegal as a whole — every
// member is healed (tool blocks stripped, text kept).
const illegal = [
{ role: "user", content: [{ type: "text", text: "a" }] },
sys([{ type: "text", text: "tools" }, TOOL_ADDITION]),
sys([{ type: "text", text: "trailing effort" }]),
{ role: "user", content: [{ type: "text", text: "b" }] },
] as AnthropicWireMessage[];
const rebuilt = coreToAnthropic(anthropicToCore({ messages: illegal } as never).msgs as never);
assert.deepEqual(orphans(rebuilt), []);
assert.equal(rebuilt[1]!.role, "user");
assert.deepEqual(rebuilt[1]!.content, [{ type: "text", text: "tools" }]);
assert.equal(rebuilt[2]!.role, "user");
assert.deepEqual(rebuilt[2]!.content, [{ type: "text", text: "trailing effort" }]);
});
Loading
Loading