Files
PaperClipAI/packages/paperclip-runner/src/drivers/runner-tool-bridge.test.ts
T
Nicky LeachandClaude Fable 5.2 7866e9ef05 fix(acpx): admit a Claude tool call only for a tool use its turn announced (#21278)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - Paperclip Runner runs Claude Code through the ACPX sidecar. Claude
calls Paperclip tools through a Runner-owned MCP endpoint, the tool
bridge.
> - The bridge identifies a call by its JSON-RPC request id. That id
does not say which turn sent the call. The sidecar binds a call to the
turn that is active when the call arrives.
> - Because of this, the Runner stops and starts the Claude process at
each turn boundary. A late call from an earlier turn then cannot run in
a later turn. This restart also stops all background work that Claude
started.
> - Claude Code already sends the missing identity. Each MCP
`tools/call` has `_meta["claudecode/toolUseId"]`. It is the id of the
ACP `tool_call` that the provider announces first, inside one turn.
> - This pull request uses that id. The sidecar admits a Claude tool
call only if the active turn announced its tool use.
> - The benefit is that a call from an earlier turn is refused by its
identity. This is the first step to keep the Claude process alive
between turns. This pull request does not change the restart.

## Linked Issues or Issue Description

No public issue exists. The issue is described here.

Refs #19912 (the design for turn-scoped ACPX MCP authority), #12419 (the
restart at turn start) and #20190 (the restart at turn end).

This pull request is the bottom of a stack. #21274 is merged, so the
base branch is `master`. It uses no code from #21274. The stack order
lets a later pull request remove the direct gateway path after both.

**What existing behavior does this improve?**

How the Runner decides which turn a Claude tool call belongs to.

**Subsystem affected**

Paperclip Runner: the tool bridge and the ACPX sidecar, for the Claude
provider only.

**Current behavior**

- The bridge keys a call by the JSON-RPC request id
(`runner-tool-bridge.ts`).
- The sidecar gives the call to the active turn (`waitForTool` in
`acpx-runtime-sidecar.ts`).
- After `session/cancel`, Claude Code does not stop an MCP request that
is in flight. A test run showed two calls from two turns live at the
same time on one MCP session. Request ids cannot tell them apart.

**Proposed behavior**

- The bridge can read a provider tool-use id from `params._meta` and
give it to the handler. It keys replays by that id.
- For Claude, the sidecar admits a call only when the turn event stream
of the active turn contains the ACP `tool_call` with the same id, and no
earlier call used that id.
- The announcement and the call use different channels. If the call
arrives first, the sidecar waits for the announcement for a maximum of 5
seconds. The wait stops when the turn ends or the call is cancelled.
- A call with no id, an id from an earlier turn, an id that was never
announced, or an id that was used before gets a tool error. There is no
fallback.
- The call id that the sidecar sends to runnerd is `tool-use:<id>` for
Claude.

**Reason and benefit**

- A late call from an earlier turn cannot run in a later turn. The
identity of the call prevents this, not only the process restart.
- A later change can stop the restart between turns.

## What Changed

- `packages/paperclip-runner/src/drivers/runner-tool-bridge.ts`
- New option `providerToolUseIdMetaKey`. Without it the bridge does not
change.
- With it, the bridge reads `params._meta[key]`. It refuses a value that
is not a bounded string. It gives a valid value to the handler as
`providerToolUseId`.
- The replay key is the tool-use id. A retry with a new request id
replays the first result. The same id with different arguments is
refused.
  - `notifications/cancelled` still finds the call by its request id.
-
`packages/paperclip-runner/src/drivers/acpx/claude-tool-use-admission.ts`
(new): `ClaudeToolUseAdmission`.
- `observe` records an id only from an event with `type === "tool_call"`
and `tag === "tool_call"`. A `tool_call_update` does not count, so a
late update about an old tool use cannot admit it.
- `admit` applies the rule above. An id is used when it is admitted.
Limits: 4,096 announcements for each turn and 8,192 admitted ids. At a
limit, new calls are refused.
- `packages/paperclip-runner/src/cli/acpx-runtime-sidecar.ts`
- The sidecar creates the admission object only for `agent === "claude"`
and sets the bridge option only for Claude.
- `pumpTurn` gives each turn event to `observe`. `waitForTool` calls
`admit` and then checks the turn and run again.
- The internal `get_task_context` call for a completion continuation
does not go through admission.
- The Claude call id does not use the per-process namespace, because a
tool-use id is unique.
- No change to Rust, the sidecar contract, the `claude-agent-acp` patch,
the server, or restart, park and turn lifecycle code. Other ACPX agents
and Cursor Cloud use the same code path as before.

Why 5 seconds: the measured time between the announcement and the call
is 1.1 ms to 14.7 ms. The limit must also include queue and event-loop
delay. 5 seconds is far above that, and a call that cannot be attributed
still gets a fast answer.

## Verification

Commands, from `packages/paperclip-runner`:

- `npx vitest run
src/drivers/acpx/acpx-runtime-adapter.claude-tool-use.test.ts
src/drivers/acpx/claude-tool-use-admission.test.ts
src/drivers/runner-tool-bridge.test.ts
src/drivers/runner-tool-bridge.claude-tool-use.test.ts
src/drivers/runner-tool-bridge.claude-schema.test.ts
src/cli/acpx-runtime-sidecar.test.ts
src/drivers/acpx/acpx-runtime-adapter.test.ts
src/drivers/cursor-cloud/cursor-cloud-driver.test.ts`: `8 passed (8)`
files, `384 passed` tests.
- `npx tsc --noEmit -p tsconfig.json`: no errors.

New tests:

- **Real ACP runtime** (`acpx-runtime-adapter.claude-tool-use.test.ts`).
It uses the real `openCodexAcpxRuntime`, ACPX 0.22, the pinned and
patched `claude-agent-acp`, the pinned Claude Code client and a real
tool bridge, against a local fake Anthropic API.
- Turn 1: two parallel tool uses are admitted as `tool-use:<id>`. The
model receives both results. The turn completes.
- Turn 2 in the same session: a replay of a turn 1 id returns the cached
result and does not dispatch again. The same id with different arguments
is refused. `admit` refuses the old id.
- In five runs each call arrived 6.9 ms to 13.2 ms after its
announcement. No call waited.
- **Qualification** (`runner-tool-bridge.claude-tool-use.test.ts`). The
pinned Claude Code client sends `_meta["claudecode/toolUseId"]` on each
`tools/call`, with the id that the API issued. This test must pass
before a Claude Code version change.
- **Admission unit tests** (19): announced then called, called then
announced, never announced, turn end during the wait, cancel, an id from
the previous turn, reuse, two concurrent calls with one id, two turns
with live calls, a subagent call, a bad id.
- **Sidecar process tests** (2), with a fake host, for Claude and for
Codex. The Codex run shows no change.
- **Bridge unit tests** (5).

Mutation checks:

- Change the announcement predicate so that it matches nothing: the real
ACP test and one unit test fail.
- Remove the bridge option in the real ACP test: the test fails.
- Count only `tool_call_update`: the unit test "counts only an opening
ACP tool_call event as an announcement" fails.

Live run with a real Claude account, not in CI: the real ACP runtime
(ACPX 0.22, the patched `claude-agent-acp`, the pinned Claude Code
client), a real tool bridge, and the real Anthropic API with model
`claude-sonnet-5-5`, on the code at the top of this stack. In three
sessions, 8 Paperclip tool calls were admitted and none was refused.
This includes calls in a prompt turn, in a cycle that Claude started
itself, and in a later turn of the same process. Each call arrived 0.8 s
to 1.7 s after its announcement, so no call waited for a late
announcement.

Not run: a turn through the real sidecar process with the real host.
That needs a qualified provider installation, the runtime sandbox and
the provider lifetime lease. Not run: the Linux builds of the pinned
client and a test under load. Not run: `pnpm verify` and the full suite.
CI supplies that coverage.

## Risks

- **This changes each Paperclip tool call that Claude makes.** If the
announcement does not reach the turn event stream in production, the
sidecar refuses the call after 5 seconds. The real ACP test covers the
stream of ACPX 0.22 with the pinned provider. It does not cover the real
sidecar host.
- **The 5 second limit is not tested under load.** A slow host can delay
the announcement. The call is then refused, and Claude sees a tool
error.
- **A Claude Code version that stops sending the id breaks all Claude
tool calls.** The qualification test finds this before the version
changes.
- **The admitted-id ledger is in sidecar memory.** Today the sidecar
restarts at each turn boundary, so the ledger is small. The limit of
8,192 ids refuses new calls when it is full.
- **Subagent calls and calls from a cycle that Claude starts itself**
are covered by unit tests and fake-host tests, not by the real ACP
stream.
- **An admitted call that is in flight when its turn ends** is refused
by the existing turn cleanup. Claude Code can leave the HTTP request
open. The bridge stops it only on its own cancel or close, as before.

> For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and
discuss it in `#dev` before opening the PR. Feature PRs that overlap
with planned core work may need to be redirected — check the roadmap
first. See `CONTRIBUTING.md`.

## Model Used

- Claude Fable 5.2 (`claude-fable-5-2`) in Claude Code planned the
change, reviewed the diff, asked for the real ACP runtime test, and ran
the verification and a mutation check again.
- Claude Opus 5.5 (`claude-opus-5-5`), as a Claude Code subagent with
tool use (shell, file edits, local test runs), wrote the change and the
tests.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [ ] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-13 11:56:42 -08:00

791 lines
36 KiB
TypeScript

import { readNativeSemanticReceipt, semanticInputSha256, type SemanticToolReceipt, type SemanticToolResult } from "./semantic-tool-receipt.js";
import { validatePrpStructuredRunResult } from "../protocol/replay-contract.js";
import { connect } from "node:net";
import { afterEach, describe, expect, it, vi } from "vitest";
import {
PRP_BLOCK_RESULT_PROVIDER_INPUT_SCHEMA,
PRP_BLOCK_TOOL_DESCRIPTION,
PRP_COMPLETION_RESULT_PROVIDER_INPUT_SCHEMA,
PRP_COMPLETION_TOOL_DESCRIPTION,
} from "../contracts/completion-result.js";
import {
RUNNER_TOOL_SCOPE_TOKEN_HEADER,
canonicalRunnerToolName,
startRunnerToolBridge,
type RunnerToolBridge,
type RunnerToolCall,
} from "./runner-tool-bridge.js";
const bridges: RunnerToolBridge[] = [];
afterEach(async () => {
await Promise.all(bridges.splice(0).map((bridge) => bridge.close()));
});
type ToolCallBody = { result: { content: unknown } };
async function rpc(
bridge: RunnerToolBridge,
body: Record<string, unknown>,
secret = bridge.secret,
): Promise<Response> {
return fetch(bridge.url, {
method: "POST",
headers: {
Authorization: `Bearer ${secret}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ jsonrpc: "2.0", ...body }),
});
}
describe("runner semantic MCP bridge", () => {
it.each([false, true])("preloads only essential public Claude task tools when opted in (%s)", async preloadClaudeTaskTools => {
const bridge = await startRunnerToolBridge({
preloadClaudeTaskTools,
tools: [tool("paperclip__set_task_title"), {
...tool("documents.read"), _meta: { "anthropic/alwaysLoad": true },
}],
privateTools: [tool("__paperclip_permission")],
handler: async () => ({ ok: true }),
});
bridges.push(bridge);
const listed = await (await rpc(bridge, { id: 1, method: "tools/list" })).json() as {
result: { tools: Array<{ name: string; _meta?: Record<string, unknown> }> };
};
expect(listed.result.tools.map(({ name }) => name)).toEqual([
"set_task_title", "documents.read", "paperclip_finish", "paperclip_block",
]);
expect(listed.result.tools.filter(tool => tool._meta?.["anthropic/alwaysLoad"] === true).map(({ name }) => name))
.toEqual(preloadClaudeTaskTools ? ["set_task_title", "paperclip_finish", "paperclip_block"] : []);
expect(listed.result.tools.find(tool => tool.name === "documents.read")?._meta).toBeUndefined();
});
it("binds to loopback, requires authentication, and exposes a closed catalog", async () => {
const bridge = await startRunnerToolBridge({
secret: "session-secret",
tools: [tool("documents.read")],
handler: async () => ({ ok: true }),
});
bridges.push(bridge);
expect(new URL(bridge.url).hostname).toBe("127.0.0.1");
expect(
(await rpc(bridge, { id: 1, method: "tools/list" }, "wrong")).status,
).toBe(401);
expect(
await (await rpc(bridge, { id: 2, method: "tools/list" })).json(),
).toMatchObject({
result: {
tools: [
{ name: "documents.read" },
{ name: "paperclip_finish", description: PRP_COMPLETION_TOOL_DESCRIPTION, inputSchema: PRP_COMPLETION_RESULT_PROVIDER_INPUT_SCHEMA },
{ name: "paperclip_block", description: PRP_BLOCK_TOOL_DESCRIPTION, inputSchema: PRP_BLOCK_RESULT_PROVIDER_INPUT_SCHEMA },
],
},
});
});
it("checks scoped credentials again after a delayed body before dispatch", async () => {
let scope = "first", token = "first-scope-token", reads = 0;
const handler = vi.fn(async () => ({ ok: true }));
const bridge = await startRunnerToolBridge({ tools: [tool("documents.read")], handler,
sessionScope: () => scope, scopeToken: () => { reads++; return token; } });
bridges.push(bridge);
const headers = (value: string) => ({ Authorization: `Bearer ${bridge.secret}`, "content-type": "application/json", [RUNNER_TOOL_SCOPE_TOKEN_HEADER]: `Bearer ${value}` });
const initialize = (value: string) => fetch(bridge.url, { method: "POST", headers: headers(value), body: JSON.stringify({ jsonrpc: "2.0", id: 0, method: "initialize" }) });
expect((await rpc(bridge, { id: 0, method: "initialize" })).status).toBe(401);
const first = await initialize(token); expect(first.status).toBe(200);
const firstSession = first.headers.get("mcp-session-id")!;
const previousReads = reads;
const body = JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name: "documents.read", arguments: { path: "note" } } });
let bodyController!: ReadableStreamDefaultController<Uint8Array>;
const pending = fetch(bridge.url, { method: "POST", headers: { ...headers(token), "mcp-session-id": firstSession },
body: new ReadableStream<Uint8Array>({ start(controller) { bodyController = controller; controller.enqueue(new TextEncoder().encode(body.slice(0, 20))); } }),
duplex: "half" } as RequestInit);
await vi.waitFor(() => expect(reads).toBeGreaterThan(previousReads));
scope = "second"; token = "second-scope-token";
const second = await initialize(token); expect(second.status).toBe(200);
bodyController.enqueue(new TextEncoder().encode(body.slice(20))); bodyController.close();
expect((await pending).status).toBe(401);
expect(handler).not.toHaveBeenCalled();
const accepted = await fetch(bridge.url, { method: "POST", headers: { ...headers(token), "mcp-session-id": second.headers.get("mcp-session-id")! }, body });
expect(accepted.status).toBe(200); expect(handler).toHaveBeenCalledTimes(1);
});
it("keeps private operations callable but undiscoverable", async () => {
const handler = vi.fn(async ({ tool: name }) => ({ name }));
const bridge = await startRunnerToolBridge({
tools: [tool("documents.read")],
privateTools: [tool("__paperclip_permission")],
handler,
});
bridges.push(bridge);
const listed = (await (
await rpc(bridge, { id: 1, method: "tools/list" })
).json()) as { result: { tools: Array<{ name: string }> } };
expect(listed.result.tools.map(({ name }) => name)).not.toContain(
"__paperclip_permission",
);
expect(
await (
await rpc(bridge, {
id: "private-1",
method: "tools/call",
params: { name: "__paperclip_permission", arguments: {} },
})
).json(),
).toMatchObject({
result: {
content: [{ text: expect.stringContaining("__paperclip_permission") }],
},
});
});
it("normalizes names and executes identical duplicate calls once", async () => {
const handler = vi.fn(async () => ({ value: 7 }));
const bridge = await startRunnerToolBridge({
tools: [tool("documents.read")],
handler,
});
bridges.push(bridge);
const request = {
id: "call-1",
method: "tools/call",
params: {
name: "paperclip_documents.read",
arguments: { id: "doc" },
},
};
expect(await (await rpc(bridge, request)).json()).toMatchObject({
result: { content: [{ text: '{"value":7}' }] },
});
expect(await (await rpc(bridge, request)).json()).toMatchObject({
result: { content: [{ text: '{"value":7}' }] },
});
expect(handler).toHaveBeenCalledTimes(1);
expect(handler).toHaveBeenCalledWith(
expect.objectContaining({
tool: "documents.read",
callId: "call-1",
arguments: { id: "doc" },
}),
);
});
it("rejects conflicting duplicate identities and validates before dispatch", async () => {
const handler = vi.fn(async () => ({ ok: true }));
const bridge = await startRunnerToolBridge({
tools: [
{
name: "documents.read",
inputSchema: {
type: "object",
required: ["id"],
properties: { id: { type: "string" } },
additionalProperties: false,
},
},
],
handler,
});
bridges.push(bridge);
expect(
await (
await rpc(bridge, {
id: "same",
method: "tools/call",
params: { name: "documents.read", arguments: { id: 9 } },
})
).json(),
).toMatchObject({ result: { isError: true } });
expect(handler).not.toHaveBeenCalled();
await rpc(bridge, {
id: "same",
method: "tools/call",
params: { name: "documents.read", arguments: { id: "a" } },
});
expect(
await (
await rpc(bridge, {
id: "same",
method: "tools/call",
params: { name: "documents.read", arguments: { id: "b" } },
})
).json(),
).toMatchObject({
result: {
isError: true,
content: [{ text: "Duplicate call identity conflict." }],
},
});
});
it("keeps numeric and string JSON-RPC identities distinct", async () => {
const handler = vi.fn(async ({ callId }) => ({ callId }));
const bridge = await startRunnerToolBridge({
tools: [tool("documents.read")],
handler,
});
bridges.push(bridge);
await rpc(bridge, {
id: 1,
method: "tools/call",
params: { name: "documents.read", arguments: { kind: "number" } },
});
await rpc(bridge, {
id: "1",
method: "tools/call",
params: { name: "documents.read", arguments: { kind: "string" } },
});
expect(handler).toHaveBeenCalledTimes(2);
});
it("times out calls", async () => {
const bridge = await startRunnerToolBridge({
tools: [tool("documents.read")],
timeoutMs: 20,
handler: ({ signal }) =>
new Promise((_resolve, reject) => {
signal.addEventListener(
"abort",
() => reject(new Error("handler aborted")),
{ once: true },
);
}),
});
bridges.push(bridge);
expect(
await (
await rpc(bridge, {
id: "slow",
method: "tools/call",
params: { name: "documents.read", arguments: {} },
})
).json(),
).toMatchObject({
result: {
isError: true,
content: [{ text: "Paperclip tool call timed out" }],
},
});
});
it.each([60_000, null])("honors MCP cancellation after the handler starts (timeout=%s)", async timeoutMs => {
let markStarted!: () => void;
const started = new Promise<void>((resolve) => {
markStarted = resolve;
});
const aborted = vi.fn();
const bridge = await startRunnerToolBridge({
tools: [tool("documents.read")],
timeoutMs,
handler: ({ signal }) => new Promise((_resolve, reject) => {
signal.addEventListener("abort", () => {
aborted();
reject(new Error("handler aborted"));
}, { once: true });
markStarted();
}),
});
bridges.push(bridge);
const pending = rpc(bridge, {
id: "cancel-me",
method: "tools/call",
params: { name: "documents.read", arguments: {} },
});
// Observe request arrival without racing the separate timeout behavior.
// Retain the rejection for the assertion while avoiding an unhandled fetch
// rejection if a failed test closes the bridge before the request arrives.
void pending.catch(() => undefined);
await started;
expect(
(
await rpc(bridge, {
method: "notifications/cancelled",
params: { requestId: "cancel-me" },
})
).status,
).toBe(202);
expect(await (await pending).json()).toMatchObject({
result: {
isError: true,
content: [{ text: "Paperclip tool call cancelled" }],
},
});
expect(aborted).toHaveBeenCalledOnce();
});
it("cancels a lease-owned native tool when its bridge closes", async () => {
let admitted!: () => void;
const started = new Promise<void>(resolve => { admitted = resolve; });
let signal: AbortSignal | undefined;
const bridge = await startRunnerToolBridge({ tools: [tool("documents.read")], timeoutMs: null,
handler: call => { signal = call.signal; admitted(); return new Promise(() => {}); },
});
bridges.push(bridge);
const pending = rpc(bridge, { id: "close-me", method: "tools/call", params: { name: "documents.read", arguments: {} } });
await started;
await bridge.close();
expect(signal?.aborted).toBe(true);
expect(await (await pending).json()).toMatchObject({ result: { isError: true,
content: [{ text: "Paperclip tool call cancelled" }],
} });
});
it("preserves successful mutation identity when the result is oversized", async () => {
const result = { text: `snowman-${"☃".repeat(65 * 1024)}` };
const handler = vi.fn(async () => result);
const bridge = await startRunnerToolBridge({
tools: [tool("documents.read")],
handler,
});
bridges.push(bridge);
const request = {
id: "large-result",
method: "tools/call",
params: { name: "documents.read", arguments: {} },
};
const bodies = [];
for (const response of [
await rpc(bridge, request),
await rpc(bridge, request),
]) {
const body = (await response.json()) as {
result: { content: Array<{ text: string }>; isError?: boolean };
};
expect(body.result).not.toHaveProperty("isError");
const manifest = JSON.parse(body.result.content[0]!.text) as {
schema: string;
encoding: string;
chunkCount: number;
byteLength: number;
sha256: string;
};
const serialized = body.result.content
.slice(1)
.map(({ text }) => text)
.join("");
expect(manifest).toMatchObject({
schema: "paperclip.semantic_tool_result_chunks.v1",
encoding: "json",
chunkCount: body.result.content.length - 1,
byteLength: Buffer.byteLength(serialized),
});
expect(
body.result.content
.slice(1)
.every(({ text }) => Buffer.byteLength(text) <= 64 * 1024),
).toBe(true);
expect(JSON.parse(serialized)).toEqual(result);
bodies.push(body);
}
expect(bodies[1]).toEqual(bodies[0]);
expect(handler).toHaveBeenCalledTimes(1);
});
it("returns a complete tagged receipt for cyclic results without re-executing", async () => {
const cyclic: Record<string, unknown> = {};
cyclic.self = cyclic;
cyclic.revision = 9_007_199_254_740_993n;
const handler = vi.fn(async () => cyclic);
const bridge = await startRunnerToolBridge({
tools: [tool("documents.read")],
handler,
});
bridges.push(bridge);
const request = {
id: "cyclic-result",
method: "tools/call",
params: { name: "documents.read", arguments: {} },
};
const first = (await (await rpc(bridge, request)).json()) as {
result: { content: Array<{ text: string }>; isError?: boolean };
};
const second = (await (await rpc(bridge, request)).json()) as typeof first;
expect(first.result).not.toHaveProperty("isError");
expect(JSON.parse(first.result.content[0]!.text)).toMatchObject({
schema: "paperclip.semantic_tool_result.v1",
status: "completed",
tool: "documents.read",
callIdentitySha256: expect.stringMatching(/^[0-9a-f]{64}$/),
encoding: "paperclip.tagged_graph.v1",
result: {
root: { $ref: 1 },
nodes: [
{
id: 1,
type: "Object",
properties: [
{ key: "self", value: { $ref: 1 } },
{
key: "revision",
value: { $type: "bigint", value: "9007199254740993" },
},
],
},
],
},
});
expect(second).toEqual(first);
expect(handler).toHaveBeenCalledTimes(1);
});
it("destroys oversized and stalled request bodies before dispatch", async () => {
const handler = vi.fn(async () => ({ ok: true }));
const bridge = await startRunnerToolBridge({
tools: [tool("documents.read")],
handler,
maxBodyBytes: 64,
requestBodyTimeoutMs: 20,
});
bridges.push(bridge);
await expect(
rpc(bridge, {
id: "oversized",
method: "tools/call",
params: {
name: "documents.read",
arguments: { value: "x".repeat(128) },
},
}),
).rejects.toThrow();
const endpoint = new URL(bridge.url);
const socket = connect({
host: endpoint.hostname,
port: Number(endpoint.port),
});
const closed = new Promise<void>((resolve) =>
socket.once("close", () => resolve()),
);
socket.write(
[
"POST /mcp HTTP/1.1",
`Host: ${endpoint.host}`,
`Authorization: Bearer ${bridge.secret}`,
"Content-Type: application/json",
"Transfer-Encoding: chunked",
"",
"5",
"{",
].join("\r\n"),
);
await expect(closed).resolves.toBeUndefined();
expect(handler).not.toHaveBeenCalled();
});
it("prevents catalog ambiguity and reserved schema replacement", async () => {
await expect(
startRunnerToolBridge({
tools: [tool("documents.read"), tool("paperclip_documents.read")],
handler: async () => null,
}),
).rejects.toThrow("duplicated");
await expect(
startRunnerToolBridge({
tools: [tool("paperclip_finish")],
handler: async () => null,
}),
).rejects.toThrow("reserved by the protocol");
await expect(
startRunnerToolBridge({
tools: [tool("documents.read")],
privateTools: [tool("documents.read")],
handler: async () => null,
}),
).rejects.toThrow("both public and private");
});
it("rejects unsupported HTTP shapes and closes idempotently", async () => {
const bridge = await startRunnerToolBridge({
tools: [],
handler: async () => null,
});
bridges.push(bridge);
expect(
(
await fetch(bridge.url, {
method: "POST",
headers: { Authorization: `Bearer ${bridge.secret}` },
body: "{}",
})
).status,
).toBe(415);
await bridge.close();
await bridge.close();
bridges.splice(bridges.indexOf(bridge), 1);
});
it("normalizes every supported provider prefix", () => {
expect(canonicalRunnerToolName("paperclip_documents.read")).toBe(
"documents.read",
);
expect(canonicalRunnerToolName("paperclip__documents.read")).toBe(
"documents.read",
);
expect(canonicalRunnerToolName("paperclip.documents.read")).toBe(
"documents.read",
);
});
});
function tool(name: string): Readonly<Record<string, unknown>> {
return { name, inputSchema: { type: "object" } };
}
describe("provider tool-use identity", () => {
const META_KEY = "claudecode/toolUseId";
const call = (id: string | number, args: Record<string, unknown>, meta?: Record<string, unknown>) => ({
id, method: "tools/call", params: { name: "documents.read", arguments: args, ...(meta === undefined ? {} : { _meta: meta }) },
});
it("carries the provider tool-use id to the handler only when the caller names its meta key", async () => {
const handler = vi.fn(async (_call: RunnerToolCall) => ({ ok: true }));
const opted = await startRunnerToolBridge({ tools: [tool("documents.read")], providerToolUseIdMetaKey: META_KEY, handler });
const plain = await startRunnerToolBridge({ tools: [tool("documents.read")], handler });
bridges.push(opted, plain);
await rpc(opted, call(2, { n: 1 }, { [META_KEY]: "toolu_01Abc", progressToken: 2 }));
await rpc(plain, call(2, { n: 1 }, { [META_KEY]: "toolu_01Abc" }));
expect(handler.mock.calls.map(([value]) => value.providerToolUseId))
.toEqual(["toolu_01Abc", undefined]);
expect(handler.mock.calls.map(([value]) => value.callId)).toEqual(["2", "2"]);
});
it("passes no id when the provider omits it and refuses a malformed one before dispatch", async () => {
const handler = vi.fn(async (_call: RunnerToolCall) => ({ ok: true }));
const bridge = await startRunnerToolBridge({ tools: [tool("documents.read")], providerToolUseIdMetaKey: META_KEY, handler });
bridges.push(bridge);
await rpc(bridge, call(1, { n: 1 }));
await rpc(bridge, call(2, { n: 2 }, { progressToken: 2 }));
expect(handler.mock.calls.map(([value]) => value.providerToolUseId))
.toEqual([undefined, undefined]);
for (const [index, value] of [7, "", "x".repeat(129), "toolu 01", "toolu_01\n", "_toolu", { id: "toolu_01" }].entries()) {
expect(await (await rpc(bridge, call(10 + index, { n: 3 }, { [META_KEY]: value }))).json()).toMatchObject({
result: { isError: true, content: [{ text: "Invalid provider tool-use identity." }] },
});
}
expect(handler).toHaveBeenCalledTimes(2);
});
it("replays a retried tool use under a new request id and refuses it with different arguments", async () => {
let finish!: (value: unknown) => void;
const handler = vi.fn(() => new Promise(resolve => { finish = resolve; }));
const bridge = await startRunnerToolBridge({ tools: [tool("documents.read")], providerToolUseIdMetaKey: META_KEY, handler });
bridges.push(bridge);
const first = rpc(bridge, call(2, { n: 1 }, { [META_KEY]: "toolu_A" }));
await vi.waitFor(() => expect(handler).toHaveBeenCalledOnce());
const retry = rpc(bridge, call(9, { n: 1 }, { [META_KEY]: "toolu_A" }));
expect(await (await rpc(bridge, call(10, { n: 2 }, { [META_KEY]: "toolu_A" }))).json()).toMatchObject({
result: { isError: true, content: [{ text: "Duplicate call identity conflict." }] },
});
finish({ value: 1 });
expect(await (await first).json()).toMatchObject({ id: 2, result: { content: [{ text: '{"value":1}' }] } });
expect(await (await retry).json()).toMatchObject({ id: 9, result: { content: [{ text: '{"value":1}' }] } });
expect(await (await rpc(bridge, call(2, { n: 1 }, { [META_KEY]: "toolu_A" }))).json())
.toMatchObject({ result: { content: [{ text: '{"value":1}' }] } });
expect(handler).toHaveBeenCalledOnce();
});
it("dispatches distinct tool uses that reuse a request id, as two turns' calls on one session do", async () => {
const handler = vi.fn(async ({ providerToolUseId }: { providerToolUseId?: string }) => ({ providerToolUseId }));
const bridge = await startRunnerToolBridge({ tools: [tool("documents.read")], providerToolUseIdMetaKey: META_KEY, handler });
bridges.push(bridge);
expect(await (await rpc(bridge, call(2, { n: 1 }, { [META_KEY]: "toolu_turn1" }))).json())
.toMatchObject({ result: { content: [{ text: '{"providerToolUseId":"toolu_turn1"}' }] } });
expect(await (await rpc(bridge, call(2, { n: 2 }, { [META_KEY]: "toolu_turn2" }))).json())
.toMatchObject({ result: { content: [{ text: '{"providerToolUseId":"toolu_turn2"}' }] } });
expect(handler).toHaveBeenCalledTimes(2);
});
it("cancels a tool-use keyed call through its request id", async () => {
let signal: AbortSignal | undefined;
const handler = vi.fn((value: { signal: AbortSignal }) => { signal = value.signal; return new Promise(() => {}); });
const bridge = await startRunnerToolBridge({ tools: [tool("documents.read")], providerToolUseIdMetaKey: META_KEY, handler });
bridges.push(bridge);
const pending = rpc(bridge, call(4, { n: 1 }, { [META_KEY]: "toolu_cancel" }));
await vi.waitFor(() => expect(handler).toHaveBeenCalledOnce());
expect((await rpc(bridge, { method: "notifications/cancelled", params: { requestId: 4 } })).status).toBe(202);
expect(signal?.aborted).toBe(true);
expect(await (await pending).json()).toMatchObject({ result: { isError: true, content: [{ text: "Paperclip tool call cancelled" }] } });
});
it("cancels only the call of the MCP session that sent the cancel when two sessions reuse a request id", async () => {
const signals = new Map<string, AbortSignal>();
const handler = vi.fn((value: { signal: AbortSignal; providerToolUseId?: string }) => {
signals.set(value.providerToolUseId!, value.signal);
return new Promise(() => {});
});
const bridge = await startRunnerToolBridge({ tools: [tool("documents.read")], providerToolUseIdMetaKey: META_KEY, handler });
bridges.push(bridge);
const send = (session: string, body: Record<string, unknown>) => fetch(bridge.url, { method: "POST",
headers: { Authorization: `Bearer ${bridge.secret}`, "Content-Type": "application/json", "Mcp-Session-Id": session },
body: JSON.stringify({ jsonrpc: "2.0", ...body }) });
const first = send("session-a", call(7, { n: 1 }, { [META_KEY]: "toolu_first" }));
await vi.waitFor(() => expect(handler).toHaveBeenCalledTimes(1));
const second = send("session-b", call(7, { n: 2 }, { [META_KEY]: "toolu_second" }));
await vi.waitFor(() => expect(handler).toHaveBeenCalledTimes(2));
expect((await send("session-a", { method: "notifications/cancelled", params: { requestId: 7 } })).status).toBe(202);
expect(signals.get("toolu_first")?.aborted).toBe(true);
expect(signals.get("toolu_second")?.aborted).toBe(false);
expect(await (await first).json()).toMatchObject({ result: { isError: true, content: [{ text: "Paperclip tool call cancelled" }] } });
expect((await send("session-b", { method: "notifications/cancelled", params: { requestId: 7 } })).status).toBe(202);
expect(signals.get("toolu_second")?.aborted).toBe(true);
await second;
});
});
describe("Copilot semantic receipt opt-in", () => {
const rawFinish = { reportedWorkDisposition: "done", summary: "The task is complete.", evidence: [], verification: [],
completionClaim: { contractRevision: "1", objectiveSatisfied: true, criteria: [], remainingWork: [] } };
it.each(["committed", "not-forwarded", "invalid", "duplicate-capture", "conflicting-capture", "duplicate-commit"])(
"captures only the invocation's exact forwarded normalized input: %s", async mode => {
const receipts: SemanticToolReceipt[] = [];
let capturedInput: unknown;
const bridge = await startRunnerToolBridge({ captureSemanticReceipt: () => receipt => receipts.push(receipt), handler: async call => {
const validation = validatePrpStructuredRunResult(call.arguments);
if (!validation.ok) throw new Error("Invalid fixture input");
capturedInput = structuredClone(validation.result);
const invalid: Record<string, unknown> = {}; invalid.self = invalid;
const commit = call.captureNormalizedInput!(mode === "invalid" ? invalid : validation.result);
if (mode === "duplicate-capture") call.captureNormalizedInput!(validation.result)();
if (mode === "conflicting-capture") call.captureNormalizedInput!({ ...validation.result, summary: "foreign" })();
if (mode !== "not-forwarded") commit();
if (mode === "duplicate-commit") commit();
// The digest is a snapshot of forwarded input, not a later mutable value.
validation.result.summary = "later mutation";
return { accepted: true };
} });
bridges.push(bridge);
const request = { id: 3, method: "tools/call", params: { name: "paperclip_finish", arguments: rawFinish } };
const body = await (await rpc(bridge, request)).json() as ToolCallBody;
expect(receipts).toHaveLength(1);
expect(readNativeSemanticReceipt({ contents: body.result.content })).toEqual(receipts[0]);
expect(receipts[0]).toMatchObject({ schema: "paperclip.semantic_tool_receipt.v2", inputSha256: semanticInputSha256(rawFinish),
normalizedInputSha256: mode === "committed" ? semanticInputSha256(capturedInput) : null });
expect(semanticInputSha256(rawFinish)).not.toBe(semanticInputSha256(capturedInput));
expect(await (await rpc(bridge, request)).json()).toEqual(body);
expect(receipts).toHaveLength(1);
},
);
it("keeps concurrent call captures separate and ignores captures after settlement", async () => {
const pending = new Map<string, { capture: NonNullable<import("./runner-tool-bridge.js").RunnerToolCall["captureNormalizedInput"]>; settle: () => void }>();
const receipts: SemanticToolReceipt[] = [];
const bridge = await startRunnerToolBridge({ captureSemanticReceipt: () => receipt => receipts.push(receipt), handler: call => new Promise(resolve => {
pending.set(call.callId, { capture: call.captureNormalizedInput!, settle: () => resolve({ accepted: true }) });
}) });
bridges.push(bridge);
const first = rpc(bridge, { id: "first", method: "tools/call", params: { name: "paperclip_finish", arguments: rawFinish } });
const secondInput = { ...rawFinish, summary: "Second call" };
const second = rpc(bridge, { id: "second", method: "tools/call", params: { name: "paperclip_finish", arguments: secondInput } });
await vi.waitFor(() => expect(pending.size).toBe(2));
const normalizedFirst = validatePrpStructuredRunResult(rawFinish); const normalizedSecond = validatePrpStructuredRunResult(secondInput);
if (!normalizedFirst.ok || !normalizedSecond.ok) throw new Error("Invalid fixture");
pending.get("second")!.capture(normalizedSecond.result)(); pending.get("second")!.settle();
const secondReceipt = readNativeSemanticReceipt({ contents: (await (await second).json() as ToolCallBody).result.content });
expect(secondReceipt).toMatchObject({ inputSha256: semanticInputSha256(secondInput), normalizedInputSha256: semanticInputSha256(normalizedSecond.result) });
pending.get("second")!.capture(normalizedFirst.result)();
pending.get("first")!.capture(normalizedFirst.result)(); pending.get("first")!.settle();
const firstReceipt = readNativeSemanticReceipt({ contents: (await (await first).json() as ToolCallBody).result.content });
expect(firstReceipt).toMatchObject({ inputSha256: semanticInputSha256(rawFinish), normalizedInputSha256: semanticInputSha256(normalizedFirst.result) });
expect(receipts).toEqual([secondReceipt, firstReceipt]);
});
it("does not accept model metadata as normalized authority or widen non-Copilot calls", async () => {
const modelMetadata = { normalizedInputSha256: "a".repeat(64), captureNormalizedInput: "forged" };
const withEvidence = await startRunnerToolBridge({ tools: [tool("documents.read")], captureSemanticReceipt: () => () => {}, handler: async call => {
expect(call.captureNormalizedInput).toBeUndefined(); return modelMetadata;
} }); bridges.push(withEvidence);
const body = await (await rpc(withEvidence, { id: 1, method: "tools/call", params: { name: "documents.read", arguments: modelMetadata } })).json() as ToolCallBody;
expect(readNativeSemanticReceipt({ contents: body.result.content })).toHaveProperty("normalizedInputSha256", null);
const plain = await startRunnerToolBridge({ handler: async call => { expect(call.captureNormalizedInput).toBeUndefined(); return { accepted: true }; } }); bridges.push(plain);
const plainBody = await (await rpc(plain, { id: 2, method: "tools/call", params: { name: "paperclip_finish", arguments: rawFinish } })).json() as ToolCallBody;
expect(readNativeSemanticReceipt({ contents: plainBody.result.content })).toBeNull();
});
it.each(["small", "chunked", "tagged", "error"])("preserves %s result encoding and captures scope before dispatch", async encoding => {
const receipts: SemanticToolReceipt[] = [];
let resolve!: (value: unknown) => void;
let reject!: (error: Error) => void;
const pending = new Promise((yes, no) => { resolve = yes; reject = no; });
const capture = vi.fn(() => (receipt: SemanticToolReceipt) => receipts.push(receipt));
const handler = vi.fn(() => pending);
const bridge = await startRunnerToolBridge({ tools: [tool("documents.read")], handler, captureSemanticReceipt: capture });
bridges.push(bridge);
const request = { id: 3, method: "tools/call", params: { name: "documents.read", arguments: {} } };
const response = rpc(bridge, request);
await vi.waitFor(() => expect(handler).toHaveBeenCalledTimes(1));
expect(capture).toHaveBeenCalledTimes(1);
if (encoding === "error") reject(new Error("bounded failure"));
else resolve(encoding === "chunked" ? "x".repeat(70000) : encoding === "tagged" ? { bigint: 1n } : { accepted: false });
const body = await (await response).json() as { result: SemanticToolResult };
expect(receipts).toHaveLength(1);
expect(readNativeSemanticReceipt({ contents: body.result.content })).toEqual(receipts[0]);
expect(body.result.isError === true).toBe(encoding === "error");
if (encoding === "small") expect(JSON.parse(body.result.content[0].text)).toEqual({ accepted: false });
if (encoding === "error") expect(body.result.content[0].text).toBe("bounded failure");
if (encoding === "chunked") expect(body.result.content.length).toBeGreaterThan(2);
const replay = await (await rpc(bridge, request)).json();
expect(replay).toEqual(body);
expect(receipts).toHaveLength(1);
expect(handler).toHaveBeenCalledTimes(1);
});
it("does not receipt unauthorized/unadmitted calls or let diagnostic errors change results", async () => {
const capture = vi.fn(() => () => { throw new Error("diagnostic only"); });
const bridge = await startRunnerToolBridge({ tools: [tool("documents.read")], handler: async () => ({ ok: true }), captureSemanticReceipt: capture });
bridges.push(bridge);
await rpc(bridge, { id: 1, method: "tools/call", params: { name: "documents.read" } }, "wrong");
await rpc(bridge, { id: 2, method: "tools/call", params: { name: "unknown" } });
expect(capture).not.toHaveBeenCalled();
const body = await (await rpc(bridge, { id: 3, method: "tools/call", params: { name: "documents.read" } })).json() as { result: SemanticToolResult };
expect(JSON.parse(body.result.content[0].text)).toEqual({ ok: true });
expect(readNativeSemanticReceipt({ contents: body.result.content })).not.toBeNull();
});
});
// The schema owner supplies scope policy; the transport only returns its guidance.
it("returns missing-field guidance before dispatch and accepts an explicit retry", async () => {
const guidance = "Use the requested task UUID. For the active task, use read_document with key only.";
const handler = vi.fn(async () => ({ accepted: true }));
const bridge = await startRunnerToolBridge({ tools: [{ name: "read_task_document", inputSchema: {
type: "object", required: ["taskId", "key"], additionalProperties: false,
properties: { taskId: { type: "string", description: guidance }, key: { type: "string" } },
} }], handler });
bridges.push(bridge);
const request = { id: "scope-retry", method: "tools/call", params: { name: "read_task_document", arguments: { key: "sentry-case" } } };
type ScopeResponse = { result: { isError?: boolean; content: Array<{ text: string }> } };
const rejected = await (await rpc(bridge, request)).json() as ScopeResponse;
expect(rejected).toMatchObject({ result: { isError: true } });
expect(handler).not.toHaveBeenCalled();
expect(rejected.result.content[0].text).toContain(guidance);
const taskId = "00000000-0000-4000-8000-000000000005";
const accepted = await (await rpc(bridge, { ...request, params: { ...request.params, arguments: { key: "sentry-case", taskId } } })).json() as ScopeResponse;
expect(accepted.result.isError).not.toBe(true);
expect(handler).toHaveBeenCalledExactlyOnceWith(expect.objectContaining({ arguments: { key: "sentry-case", taskId } }));
});
describe("runner bridge repeated identical input rejections", () => {
it("corrects the second identical rejection and stops the third", async () => {
const handler = vi.fn(async () => ({ ok: true }));
const bridge = await startRunnerToolBridge({
tools: [{ name: "update_case", inputSchema: { type: "object", additionalProperties: false, required: ["idempotencyKey"], properties: { idempotencyKey: { type: "string" }, title: { type: "string" } } } }],
handler,
});
bridges.push(bridge);
const text = async (id: number) => ((await (await rpc(bridge, { id, method: "tools/call", params: { name: "update_case", arguments: {} } })).json()) as {
result: { content: Array<{ text: string }> };
}).result.content[0]!.text;
expect(await text(1)).not.toContain("rejected");
expect(await text(2)).toContain("Expected properties: required idempotencyKey; optional title.");
expect(await text(3)).toMatch(/^This call was not accepted: update_case was already rejected 2 times/);
expect(handler).not.toHaveBeenCalled();
});
});