Skip to content
Open
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.217",
"version": "0.0.218",
"description": "Framework-agnostic context-compression engine (model-driven, 3-tier LSM). Pure core: no host dependency.",
"license": "MIT",
"author": "ranxianglei",
Expand Down
18 changes: 16 additions & 2 deletions kernel/src/wire/anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -593,11 +593,25 @@ function healUnpairedServerToolBlocks(out: AnthropicWireMessage[]): number {
});
if (kept.length === blocks.length) continue;
dropped += blocks.length - kept.length;
const final = kept as unknown as AnthropicBlock[];
// #5864 second producer: dropping the orphaned server-tool half can leave a
// thinking-family block as the FINAL block — Anthropic 400s the whole
// request ("the final block in an assistant message cannot be thinking").
// Append the same placeholder the emptied case uses so the thinking block
// is never final. The thinking block itself is untouched (KDD #10: its
// bytes and signature round-trip verbatim); only a sibling text block is
// added after it.
const last = final[final.length - 1];
if (
last &&
(last.type === "thinking" || last.type === "redacted_thinking")
)
final.push({ type: "text", text: SERVER_TOOL_FOLDED_PLACEHOLDER });
out[mi] = {
role: "assistant",
content:
kept.length > 0
? (kept as unknown as AnthropicBlock[])
final.length > 0
? final
: [{ type: "text", text: SERVER_TOOL_FOLDED_PLACEHOLDER }],
};
}
Expand Down
112 changes: 112 additions & 0 deletions kernel/tests/server-tool-pair-heal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ const res = (id: string) => ({
tool_use_id: id,
content: { type: "advisor_result", text: "advice" },
});
const think = (signature?: string) => ({
type: "thinking",
thinking: "hmm",
...(signature ? { signature } : {}),
});
const redacted = () => ({
type: "redacted_thinking",
data: "REDACTED",
});
const wire = (messages: unknown[]) => messages as AnthropicWireMessage[];
const roundTrip = (messages: AnthropicWireMessage[]) => {
const { msgs, cacheControls } = anthropicToCore({
Expand Down Expand Up @@ -158,3 +167,106 @@ test("an assistant message emptied by the heal keeps role alternation via placeh
{ type: "text", text: "(server tool exchange folded)" },
]);
});

// #5864 second producer: dropping the orphaned server-tool half can leave a
// thinking-family block as the FINAL block — "the final block in an assistant
// message cannot be thinking" 400s the whole request. The heal must append the
// same placeholder the emptied case uses so the thinking block is never final.
// Positive rows (both fold directions + redacted_thinking) AND a negative row
// (thinking with a complete pair round-trips byte-identical, #3617).

test("orphaned result after thinking: placeholder keeps thinking from being the final block", () => {
const messages = wire([
{ role: "user", content: [{ type: "text", text: "q1" }] },
{
role: "assistant",
content: [think("sig"), use("srvtoolu_a"), res("srvtoolu_a")],
},
{ role: "user", content: [{ type: "text", text: "q2" }] },
]);
const { msgs } = roundTrip(messages);
// Simulate a fold that covered only the server_tool_use block.
const folded = msgs.filter(
(m) =>
!(m.rawAnthropicBlock as { type?: string } | undefined)?.type?.startsWith(
"server_tool_use",
),
);
const out = coreToAnthropic(folded);
assert.deepEqual(out[1], {
role: "assistant",
content: [
{ type: "thinking", thinking: "hmm", signature: "sig" },
{ type: "text", text: "(server tool exchange folded)" },
],
});
});

test("orphaned use after thinking (not last message): placeholder keeps thinking from being the final block", () => {
const messages = wire([
{ role: "user", content: [{ type: "text", text: "q1" }] },
{
role: "assistant",
content: [think("sig"), use("srvtoolu_a"), res("srvtoolu_a")],
},
{ role: "user", content: [{ type: "text", text: "q2" }] },
]);
const { msgs } = roundTrip(messages);
// Simulate a fold that covered only the advisor_tool_result block.
const folded = msgs.filter(
(m) =>
(m.rawAnthropicBlock as { type?: string } | undefined)?.type !==
"advisor_tool_result",
);
const out = coreToAnthropic(folded);
assert.deepEqual(out[1], {
role: "assistant",
content: [
{ type: "thinking", thinking: "hmm", signature: "sig" },
{ type: "text", text: "(server tool exchange folded)" },
],
});
});

test("orphaned result after redacted_thinking: placeholder keeps redacted_thinking from being the final block", () => {
const messages = wire([
{ role: "user", content: [{ type: "text", text: "q1" }] },
{
role: "assistant",
content: [redacted(), use("srvtoolu_a"), res("srvtoolu_a")],
},
{ role: "user", content: [{ type: "text", text: "q2" }] },
]);
const { msgs } = roundTrip(messages);
const folded = msgs.filter(
(m) =>
!(m.rawAnthropicBlock as { type?: string } | undefined)?.type?.startsWith(
"server_tool_use",
),
);
const out = coreToAnthropic(folded);
assert.deepEqual(out[1], {
role: "assistant",
content: [
{ type: "redacted_thinking", data: "REDACTED" },
{ type: "text", text: "(server tool exchange folded)" },
],
});
});

test("thinking with a complete server-tool pair round-trips byte-identical (negative row)", () => {
const messages = wire([
{ role: "user", content: [{ type: "text", text: "q1" }] },
{
role: "assistant",
content: [
think("sig"),
use("srvtoolu_a"),
res("srvtoolu_a"),
{ type: "text", text: "a1" },
],
},
{ role: "user", content: [{ type: "text", text: "q2" }] },
]);
assert.deepEqual(roundTrip(messages).out, messages);
});
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.

Loading