From 2de20d67975a0a7045c505323c2b03e87d44f697 Mon Sep 17 00:00:00 2001 From: OMSociety Date: Wed, 14 Oct 2026 21:38:12 +0800 Subject: [PATCH 1/4] kernel: pair orphaned tool halves by order and multiplicity, not id-set membership (#3046) stripOrphanedToolResults / stripOrphanedToolCalls collected ids into a Set, so a half survived whenever ANY message carried its id - including a duplicate id from another turn or a declaration on the other side of the fold. Count open calls/results in render order instead: results claim the nearest earlier unanswered call, calls claim the nearest later unanswered result. Verified: new kernel/tests/issue3046-duplicate-tool-call-ids.test.ts fails on master at both pairing assertions and passes here; orphan-fixes, tool-pairs, tool-pair-integrity, state-prune, reasoning-pairs stay green (63/63). --- kernel/package.json | 2 +- kernel/src/prune.ts | 58 +++++-- .../issue3046-duplicate-tool-call-ids.test.ts | 164 ++++++++++++++++++ 3 files changed, 205 insertions(+), 19 deletions(-) create mode 100644 kernel/tests/issue3046-duplicate-tool-call-ids.test.ts diff --git a/kernel/package.json b/kernel/package.json index 5f54201184..de57e579e5 100644 --- a/kernel/package.json +++ b/kernel/package.json @@ -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", diff --git a/kernel/src/prune.ts b/kernel/src/prune.ts index 7273bea319..1f84392351 100644 --- a/kernel/src/prune.ts +++ b/kernel/src/prune.ts @@ -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(); + const openCalls = new Map(); + 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(); - for (const m of messages) { + const openResults = new Map(); + 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(); } /** diff --git a/kernel/tests/issue3046-duplicate-tool-call-ids.test.ts b/kernel/tests/issue3046-duplicate-tool-call-ids.test.ts new file mode 100644 index 0000000000..2236534814 --- /dev/null +++ b/kernel/tests/issue3046-duplicate-tool-call-ids.test.ts @@ -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 { + 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(); + 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); +}); From 8c74412a2221880a6680c3f293b5596961f4e1eb Mon Sep 17 00:00:00 2001 From: OMSociety Date: Wed, 14 Oct 2026 21:38:12 +0800 Subject: [PATCH 2/4] fix(#3046): sync root package-lock with kernel 0.0.124 bump --- package-lock.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index 24672c3853..3797abaa8b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -48,7 +48,7 @@ }, "kernel": { "name": "acp-kernel", - "version": "0.0.123", + "version": "0.0.124", "dev": true, "license": "MIT", "devDependencies": { From 8cbbebf4380c8c162f62741bade17c82c2b02255 Mon Sep 17 00:00:00 2001 From: OMSociety Date: Wed, 14 Oct 2026 21:38:12 +0800 Subject: [PATCH 3/4] fix(#3046): sync kernel/package-lock with kernel 0.0.124 bump --- kernel/package-lock.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/package-lock.json b/kernel/package-lock.json index 290d9a75e9..8940fbd3ae 100644 --- a/kernel/package-lock.json +++ b/kernel/package-lock.json @@ -1,12 +1,12 @@ { "name": "acp-kernel", - "version": "0.0.121", + "version": "0.0.124", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "acp-kernel", - "version": "0.0.121", + "version": "0.0.124", "license": "MIT", "devDependencies": { "@types/node": "^22.0.0", From 1c221a7eb67e40f7e2548db7fa9d1f08aa9f2421 Mon Sep 17 00:00:00 2001 From: OMSociety Date: Wed, 14 Oct 2026 21:38:12 +0800 Subject: [PATCH 4/4] test: ledger WC-018 for openai-chat tool-call/result order pairing (#3046) Records the constraint learned from production: a role:tool message must be preceded by the assistant declaring its tool_call_id, and every declared id must be answered exactly once later. Validator clause is order+multiplicity aware and runs before the tools early-return. --- tests/wire-contract-fakes.ts | 37 +++++++++++++++++++++++++++++++++++- tests/wire-contract.test.ts | 2 ++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/tests/wire-contract-fakes.ts b/tests/wire-contract-fakes.ts index 711f0209da..ff790506e9 100644 --- a/tests/wire-contract-fakes.ts +++ b/tests/wire-contract-fakes.ts @@ -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/ 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", @@ -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(); + const answered = new Map(); + 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; diff --git a/tests/wire-contract.test.ts b/tests/wire-contract.test.ts index ecc68dce6a..2a0c2e138b 100644 --- a/tests/wire-contract.test.ts +++ b/tests/wire-contract.test.ts @@ -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 },