Skip to content

Commit 206657b

Browse files
committed
fix(pi): long tool calls no longer share a session approval with different calls
1 parent f31ce3e commit 206657b

3 files changed

Lines changed: 38 additions & 2 deletions

File tree

‎packages/provider-pi/src/server/mcpBridge.testkit.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// @effect-diagnostics nodeBuiltinImport:off -- Executes the shipped Pi extension at its native JavaScript boundary.
2+
import * as NodeCrypto from "node:crypto";
23
import * as NodeModule from "node:module";
34
import * as NodePath from "node:path";
45
import * as NodeVM from "node:vm";
@@ -95,6 +96,7 @@ export async function loadMcpBridge(
9596
},
9697
},
9798
AbortSignal,
99+
NodeCrypto,
98100
NodePath,
99101
Type: { Unsafe: (schema: unknown) => schema },
100102
fetch: async (

‎packages/provider-pi/src/server/mcpExtensionSource.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,32 @@ describe("Pi tool discovery permissions", () => {
400400
assert.equal(replaced?.block, true);
401401
assert.equal(confirmations.at(-1), "Allow tool_search?");
402402
});
403+
404+
it("gives long inputs that differ past the shown text different confirmation details", async () => {
405+
const bridge = await loadMcpBridge({ runtimeMode: "approval-required" });
406+
const hook = bridge.handlers.get("tool_call") as unknown as (
407+
event: { toolName: string; input: unknown },
408+
ctx: { ui: { confirm: (title: string, detail: string) => Promise<boolean> } },
409+
) => Promise<{ block: true; reason: string } | undefined>;
410+
const details: string[] = [];
411+
const ctx = {
412+
ui: {
413+
confirm: async (_title: string, detail: string) => {
414+
details.push(detail);
415+
return true;
416+
},
417+
},
418+
};
419+
const filler = "a".repeat(4_100);
420+
await hook({ toolName: "bash", input: { command: filler + "\necho one" } }, ctx);
421+
await hook({ toolName: "bash", input: { command: filler + "\necho two" } }, ctx);
422+
await hook({ toolName: "bash", input: { command: "ls" } }, ctx);
423+
const [first, second, short] = details;
424+
assert.equal(first!.slice(0, 4_000), second!.slice(0, 4_000));
425+
assert.notEqual(first, second);
426+
assert.match(first!, /\n\.\.\. \d+ more characters not shown \(sha256 [0-9a-f]{64}\)$/);
427+
assert.equal(short, JSON.stringify({ command: "ls" }, null, 2));
428+
});
403429
});
404430

405431
async function loadRequestHook() {

‎packages/provider-pi/src/server/mcpExtensionSource.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export const PI_FILE_CHANGE_TOOLS = ["edit", "write"] as const;
1414

1515
export const PI_T3_MCP_EXTENSION_SOURCE = `\
1616
import { stripFrontmatter, type ExtensionAPI, type ExtensionContext } from "@earendil-works/pi-coding-agent";
17+
import * as NodeCrypto from "node:crypto";
1718
import * as NodeFSP from "node:fs/promises";
1819
import * as NodePath from "node:path";
1920
import { Type } from "typebox";
@@ -61,12 +62,19 @@ function runtimeMode(): RuntimeMode {
6162
: "full-access";
6263
}
6364
65+
// T3 keys "Always allow this session" on this text, so a cut summary ends with the
66+
// full input's hash: calls that differ only past the cut get separate grants.
6467
function toolInputSummary(input: unknown): string {
68+
let text: string;
6569
try {
66-
return JSON.stringify(input, null, 2).slice(0, 4_000);
70+
text = JSON.stringify(input, null, 2) ?? String(input);
6771
} catch {
68-
return String(input).slice(0, 4_000);
72+
text = String(input);
6973
}
74+
if (text.length <= 4_000) return text;
75+
const hash = NodeCrypto.createHash("sha256").update(text).digest("hex");
76+
return text.slice(0, 4_000) + "\\n... " + (text.length - 4_000) +
77+
" more characters not shown (sha256 " + hash + ")";
7078
}
7179
7280
function parseSseOrJson(body: string, contentType: string): JsonRpcResponse {

0 commit comments

Comments
 (0)