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.123",
"version": "0.0.124",
"description": "Framework-agnostic context-compression engine (model-driven, 3-tier LSM). Pure core: no host dependency.",
"license": "MIT",
"author": "ranxianglei",
Expand Down
58 changes: 40 additions & 18 deletions kernel/src/prune.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,35 +389,57 @@ function renderSummary(anchor: SummaryAnchor): CoreMessage {
};
}

/**
* Defense-in-depth for tool-result pairing. Pairing is by ORDER and
* MULTIPLICITY, never by id-set membership: host-minted tool-call ids are not
* guaranteed unique (a host that numbers calls `name:index` re-issues the same
* id on later turns), and a set cannot tell one instance from another — a
* result whose own call was folded into a summary then survives because a
* DIFFERENT call with the same id is still visible, and the wire carries a
* `tool` message before any assistant declares its `tool_call_id`. Strict
* upstreams reject that whole request with HTTP 400.
*/
function stripOrphanedToolResults(messages: CoreMessage[]): CoreMessage[] {
const knownCallIds = new Set<string>();
const openCalls = new Map<string, number>();
const kept: CoreMessage[] = [];
for (const m of messages) {
if (m.contentType === "tool-call" && m.toolCallId) {
knownCallIds.add(m.toolCallId);
openCalls.set(m.toolCallId, (openCalls.get(m.toolCallId) ?? 0) + 1);
} else if (m.contentType === "tool-result" && m.toolCallId) {
const open = openCalls.get(m.toolCallId) ?? 0;
if (open <= 0) continue;
openCalls.set(m.toolCallId, open - 1);
}
kept.push(m);
}
return messages.filter(
(m) =>
m.contentType !== "tool-result" ||
!m.toolCallId ||
knownCallIds.has(m.toolCallId),
);
return kept;
}

/**
* Mirror of {@link stripOrphanedToolResults} for the call side: drop tool-calls
* that no visible tool-result answers. Scanned from the tail so a call only
* claims a result that actually FOLLOWS it. `compress` calls carry their own
* receipt and stay regardless.
*/
function stripOrphanedToolCalls(messages: CoreMessage[]): CoreMessage[] {
const knownResultIds = new Set<string>();
for (const m of messages) {
const openResults = new Map<string, number>();
const kept: CoreMessage[] = [];
for (let i = messages.length - 1; i >= 0; i--) {
const m = messages[i]!;
if (m.contentType === "tool-result" && m.toolCallId) {
knownResultIds.add(m.toolCallId);
openResults.set(m.toolCallId, (openResults.get(m.toolCallId) ?? 0) + 1);
} else if (
m.contentType === "tool-call" &&
m.toolCallId &&
m.toolName !== "compress"
) {
const open = openResults.get(m.toolCallId) ?? 0;
if (open <= 0) continue;
openResults.set(m.toolCallId, open - 1);
}
kept.push(m);
}
return messages.filter(
(m) =>
m.contentType !== "tool-call" ||
!m.toolCallId ||
m.toolName === "compress" ||
knownResultIds.has(m.toolCallId),
);
return kept.reverse();
}

/**
Expand Down
164 changes: 164 additions & 0 deletions kernel/tests/issue3046-duplicate-tool-call-ids.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
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";

/**
* Regression: duplicate tool-call ids defeat id-set pairing in the two
* defense-in-depth strip guards. A host may re-issue the same tool-call id on a
* later turn (`name:index` style), so "a call with this id is visible" does not
* mean "THIS result's call is visible" — the fold only has to cover one of the
* two instances for a half whose partner is gone to survive, and the outgoing
* request then violates the upstream pairing contract
* ("an assistant message with 'tool_calls' must be followed by tool messages
* responding to each 'tool_call_id'") and the whole turn fails with HTTP 400.
*/

function makeBlock(
overrides: Partial<CompressionBlock> = {},
): CompressionBlock {
return {
blockId: "b0",
runId: "r0",
tier: 1,
summary: "Test summary",
directMessageIds: [],
effectiveMessageIds: [],
directBlockIds: [],
createdAt: 0,
survivedCount: 0,
generation: "young",
active: true,
...overrides,
};
}

function stateWith(block: CompressionBlock): CompressionState {
return { ...createInitialState(), blocks: [block] };
}

function call(id: string, toolCallId: string): CoreMessage {
return {
id,
role: "assistant",
contentType: "tool-call",
toolName: "send_message",
toolCallId,
text: "{}",
};
}

function result(id: string, toolCallId: string): CoreMessage {
return {
id,
role: "user",
contentType: "tool-result",
toolName: "send_message",
toolCallId,
text: "delivered",
};
}

function text(id: string, role: CoreMessage["role"]): CoreMessage {
return { id, role, contentType: "text", text: id };
}

/** Every surviving tool-result must be preceded by a call declaring its id, and
* every declared id must be answered exactly once. */
function assertPairsOrdered(messages: CoreMessage[]): void {
const declared = new Map<string, number>();
for (const m of messages) {
if (m.contentType === "tool-call" && m.toolCallId) {
declared.set(m.toolCallId, (declared.get(m.toolCallId) ?? 0) + 1);
}
if (m.contentType === "tool-result" && m.toolCallId) {
const open = declared.get(m.toolCallId) ?? 0;
assert.ok(
open > 0,
`tool-result ${m.id} (${m.toolCallId}) has no earlier unanswered call`,
);
declared.set(m.toolCallId, open - 1);
}
}
for (const [id, open] of declared) {
assert.equal(open, 0, `call id ${id} left unanswered`);
}
}

test("duplicate tool-call ids: a result whose own call was folded is stripped even though a same-id call survives", () => {
const messages: CoreMessage[] = [
text("u0", "user"),
call("a1", "send_message:0"),
result("a1r", "send_message:0"),
text("u1", "user"),
call("a2", "send_message:0"),
result("a2r", "send_message:0"),
];
// The fold covers the FIRST call only: its result stays visible while a
// second call carrying the same id is still in the request.
const state = stateWith(
makeBlock({ effectiveMessageIds: ["a1"], directMessageIds: ["a1"] }),
);

const out = prune(messages, state);
const ids = out.map((m) => m.id);

assert.ok(!ids.includes("a1"), "covered call is pruned");
assert.ok(
!ids.includes("a1r"),
"result of the covered call is stripped (id-set pairing kept it)",
);
assert.ok(ids.includes("a2") && ids.includes("a2r"), "intact pair survives");
assertPairsOrdered(out);
});

test("duplicate tool-call ids: a call whose own result was folded is stripped even though a same-id result survives", () => {
const messages: CoreMessage[] = [
text("u0", "user"),
result("a0r", "send_message:1"),
text("u1", "user"),
call("a1", "send_message:1"),
result("a1r", "send_message:1"),
];
// The fold covers the LAST result only: the call above it is left without an
// answer while an earlier result carrying the same id stays visible.
const state = stateWith(
makeBlock({ effectiveMessageIds: ["a1r"], directMessageIds: ["a1r"] }),
);

const out = prune(messages, state);
const ids = out.map((m) => m.id);

assert.ok(!ids.includes("a1r"), "covered result is pruned");
assert.ok(
!ids.includes("a0r"),
"result with no visible call is stripped (id-set pairing kept it)",
);
assertPairsOrdered(out);
});

test("duplicate tool-call ids: complete pairs are left untouched", () => {
const messages: CoreMessage[] = [
text("u0", "user"),
call("a1", "send_message:0"),
result("a1r", "send_message:0"),
text("u1", "user"),
call("a2", "send_message:0"),
result("a2r", "send_message:0"),
];
const state: CompressionState = { ...createInitialState() };

const out = prune(messages, state);

assert.deepEqual(
out.map((m) => m.id),
messages.map((m) => m.id),
"no message is dropped or reordered",
);
assertPairsOrdered(out);
});
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.

37 changes: 36 additions & 1 deletion tests/wire-contract-fakes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ export const WIRE_RULES: readonly WireRule[] = [
provenance:
"bili #3063 production silent failure (codex 0.162.0-alpha.17.2 through /bili/<chatgpt.com> prefix routing on bili 0.1.183+: upstream counted input tokens, produced zero output, turn ended completed; identical via --passthrough). Cross-checked against openai/codex source: codex-rs/codex-api/src/common.rs ResponseCreateWsRequest declares stream: bool as a required serialized field (no skip_serializing_if) and serializes stream_options when present; codex-rs/core/tests/suite/client_websockets.rs asserts body[\"stream\"] == true on the wire. Repair: src/responses-ws.ts re-attaches the client's transport fields at egress (egressTransportFields); supersedes WC-013's over-generalized scope for the codex lane.",
},
{
id: "WC-018",
wire: "openai-chat",
summary:
'openai-chat pairs assistant tool_calls with tool results BY ORDER, not by id-set membership: every role:"tool" message must be preceded by an assistant message declaring its tool_call_id, and every declared tool_calls[].id must be answered by exactly one later role:"tool" message for that id — a premature/undeclared tool result and an unanswered or double-answered declaration are both 400s',
provenance:
'bili 0.1.195/0.1.196 production traffic (upstream Kimi via the OpenAI-chat wire), two mirror 400 envelopes: \'Invalid request: tool_call_id is not found\' — the offending body carried a role:"tool" message BEFORE the assistant message declaring that tool_call_id — and \'Invalid request: an assistant message with \\\'tool_calls\\\' must be followed by tool messages responding to each \\\'tool_call_id\\\'. The following tool_call_ids did not have response messages: send_message:1\' — an assistant tool_calls entry no later tool message answered. Trigger: DSH Agent Teams re-issues a per-tool index as the id (send_message:0 / send_message:1, list_agents:0), so one id names several distinct calls. Why half a pair reaches the wire: kernel/src/prune.ts stripOrphanedToolResults / stripOrphanedToolCalls pair by id-SET membership, not by order or multiplicity — each first collects every tool-call id (resp. every tool-result id) into one Set with position ignored, then filters — so a role:"tool" result survives whenever ANY assistant call carries its id, even one declared later or a same-id duplicate from another turn, and an assistant tool_calls survives whenever ANY earlier same-id result exists; both halves of a pair can therefore be split across turns and only one side is dropped.',
},
{
id: "WC-016",
wire: "anthropic",
Expand Down Expand Up @@ -285,7 +293,34 @@ function validateAnthropicBody(body: unknown): string[] {
/** WC-004 on an OpenAI chat/completions body (only function-typed entries). */
export function validateOpenAiChatBody(body: unknown): string[] {
const out: string[] = [];
if (!isPlainObject(body) || !Array.isArray(body.tools)) return out;
if (!isPlainObject(body)) return out;
// WC-018: runs before the tools early-return — tool/assistant pairing binds
// on any messages array. Order + multiplicity, never id-set membership:
// host-minted ids repeat (a name:index id names several distinct calls).
if (Array.isArray(body.messages)) {
const declared = new Map<string, number>();
const answered = new Map<string, number>();
body.messages.forEach((m, i) => {
if (!isPlainObject(m)) return;
const calls = m.role === "assistant" ? m.tool_calls : undefined;
if (Array.isArray(calls)) {
for (const tc of calls) {
if (!isPlainObject(tc) || typeof tc.id !== "string") continue;
declared.set(tc.id, (declared.get(tc.id) ?? 0) + 1);
}
} else if (m.role === "tool") {
const id = typeof m.tool_call_id === "string" ? m.tool_call_id : "";
const pending = (declared.get(id) ?? 0) - (answered.get(id) ?? 0);
if (pending <= 0)
out.push(`WC-018 messages[${i}]: tool_call_id "${id}" is not declared by a preceding assistant tool_calls (upstream: "tool_call_id ${id} is not found")`);
else answered.set(id, (answered.get(id) ?? 0) + 1);
}
});
const unanswered = [...declared].filter(([id, n]) => (answered.get(id) ?? 0) < n).map(([id]) => id);
if (unanswered.length > 0)
out.push(`WC-018 messages: assistant tool_calls ids not answered by a later tool message: ${unanswered.join(", ")} (upstream: an assistant message with 'tool_calls' must be followed by tool messages responding to each 'tool_call_id')`);
}
if (!Array.isArray(body.tools)) return out;
body.tools.forEach((t, i) => {
if (!isPlainObject(t)) return;
const fn = t.function;
Expand Down
2 changes: 2 additions & 0 deletions tests/wire-contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ test("wire-contract ledger: every rule has a live enforcement clause", () => {
{ model: "gemini-synthetic", tools: [{ type: "function", function: { name: "compress", parameters: { type: "object", properties: { content: { type: ["array", "string"] } } } } }] },
{ tools: [{ type: "function", function: { name: "x".repeat(65), parameters: { type: "object", properties: {} } } }] },
{ tools: [{ type: "function", function: { name: "ok", parameters: { anyOf: [] } } }] },
// WC-018: the production shape — a tool result positioned before the assistant declaring its id.
{ messages: [{ role: "tool", tool_call_id: "send_message:0", content: "r" }, { role: "assistant", tool_calls: [{ id: "send_message:0", type: "function", function: { name: "send_message", arguments: "{}" } }] }] },
],
responses: [
{ type: "response.create", input: [], stream: true },
Expand Down
Loading