The best way to control Claude Code, Codex and more from any computer or phone
| 1 | // @effect-diagnostics nodeBuiltinImport:off -- Pi extensions run outside Effect; these tests exercise their native filesystem boundary. |
| 2 | import * as NodeFSP from "node:fs/promises"; |
| 3 | import * as NodePath from "node:path"; |
| 4 | import * as NodeOS from "node:os"; |
| 5 | import * as NodeModule from "node:module"; |
| 6 | import * as NodeVM from "node:vm"; |
| 7 | import { assert, describe, it } from "@effect/vitest"; |
| 8 | import { PULL_REQUEST_LINKING_INSTRUCTIONS } from "@t3tools/provider-core/server/runtimeInstructions"; |
| 9 | |
| 10 | import { PI_T3_MCP_EXTENSION_SOURCE, PI_T3_SERVICE_TIER_COMMAND } from "./mcpExtensionSource.ts"; |
| 11 | import { loadMcpBridge } from "./mcpBridge.testkit.ts"; |
| 12 | |
| 13 | type RequestHook = ( |
| 14 | event: { payload: unknown }, |
| 15 | ctx: { model: { provider: string; serviceTiers?: ReadonlyArray<{ id: string }> } }, |
| 16 | ) => Record<string, unknown> | undefined; |
| 17 | |
| 18 | type CommandHandler = (args: string) => Promise<void>; |
| 19 | |
| 20 | interface SkillCommand { |
| 21 | readonly name: string; |
| 22 | readonly source: string; |
| 23 | readonly sourceInfo: { readonly path: string }; |
| 24 | } |
| 25 | |
| 26 | type InputHook = ( |
| 27 | event: { text: string; images?: ReadonlyArray<unknown> }, |
| 28 | ctx: { ui: { notify: (message: string, level: string) => void } }, |
| 29 | ) => Promise<{ action: string; text: string; images?: ReadonlyArray<unknown> } | undefined>; |
| 30 | |
| 31 | async function loadHooks( |
| 32 | commands: ReadonlyArray<SkillCommand> = [], |
| 33 | argv: ReadonlyArray<string> = [], |
| 34 | ) { |
| 35 | const handlers = new Map<string, unknown>(); |
| 36 | const source = NodeModule.stripTypeScriptTypes( |
| 37 | PI_T3_MCP_EXTENSION_SOURCE.replace(/^import .*;$/gm, "").replace( |
| 38 | "export default async function", |
| 39 | "async function", |
| 40 | ), |
| 41 | ); |
| 42 | await NodeVM.runInNewContext(`${source}\nt3McpExtension(pi)`, { |
| 43 | process: { env: {}, argv }, |
| 44 | NodeFSP, |
| 45 | NodePath, |
| 46 | stripFrontmatter: (content: string) => content, |
| 47 | pi: { |
| 48 | on: (name: string, handler: unknown) => handlers.set(name, handler), |
| 49 | registerCommand: () => {}, |
| 50 | getCommands: () => commands, |
| 51 | }, |
| 52 | }); |
| 53 | return handlers; |
| 54 | } |
| 55 | |
| 56 | describe("Pi orchestration prompt section", () => { |
| 57 | it.each([ |
| 58 | { |
| 59 | name: "on Pi builds without prompt sections", |
| 60 | events: [ |
| 61 | { systemPrompt: "base" }, |
| 62 | { systemPrompt: "base", systemPromptOptions: {} }, |
| 63 | { systemPrompt: "base", systemPromptOptions: { sections: null } }, |
| 64 | ], |
| 65 | }, |
| 66 | { |
| 67 | name: "when an earlier extension forces the system prompt", |
| 68 | events: [ |
| 69 | { |
| 70 | systemPrompt: "forced", |
| 71 | systemPromptOptions: { sections: {}, forceSystemPrompt: "forced" }, |
| 72 | }, |
| 73 | ], |
| 74 | }, |
| 75 | ])("appends guidance $name", async ({ events }) => { |
| 76 | const bridge = await loadMcpBridge(); |
| 77 | const hook = bridge.handlers.get("before_agent_start")!; |
| 78 | for (const event of events) { |
| 79 | const result = await hook(event, { ui: { notify: assert.fail } }); |
| 80 | assert.isDefined(result); |
| 81 | assert.isTrue(result!.systemPrompt.startsWith(`${event.systemPrompt}\n\n`)); |
| 82 | assert.include(result!.systemPrompt, "orchestrator_capabilities"); |
| 83 | assert.include(result!.systemPrompt, PULL_REQUEST_LINKING_INSTRUCTIONS); |
| 84 | } |
| 85 | }); |
| 86 | |
| 87 | it("sets the guidance section on Pi builds with prompt sections", async () => { |
| 88 | const bridge = await loadMcpBridge(); |
| 89 | const sections: Record<string, string> = {}; |
| 90 | const result = await bridge.handlers.get("before_agent_start")!( |
| 91 | { systemPrompt: "base", systemPromptOptions: { sections } }, |
| 92 | { ui: { notify: assert.fail } }, |
| 93 | ); |
| 94 | assert.isUndefined(result); |
| 95 | assert.include(sections.t3_code_orchestration, "orchestrator_capabilities"); |
| 96 | assert.include(sections.t3_code_orchestration, PULL_REQUEST_LINKING_INSTRUCTIONS); |
| 97 | }); |
| 98 | }); |
| 99 | |
| 100 | describe("Pi project trust", () => { |
| 101 | it.each([ |
| 102 | { name: "a trusted folder", trusted: true, argv: [], notices: 0 }, |
| 103 | { name: "an untrusted folder", trusted: false, argv: [], notices: 1 }, |
| 104 | { name: "--no-approve", trusted: false, argv: ["--approve", "-na"], notices: 0 }, |
| 105 | ])("sends $notices trust notice per session for $name", async ({ trusted, argv, notices }) => { |
| 106 | const sent: string[] = []; |
| 107 | const ctx = { |
| 108 | cwd: "/repo", |
| 109 | isProjectTrusted: () => trusted, |
| 110 | ui: { notify: (message: string) => sent.push(message) }, |
| 111 | }; |
| 112 | // Pi reruns extension factories on session replacement without asking about trust again. |
| 113 | for (const _session of [1, 2]) { |
| 114 | const handlers = (await loadHooks([], ["node", "pi", ...argv])) as Map< |
| 115 | string, |
| 116 | (event: unknown, ctx: unknown) => unknown |
| 117 | >; |
| 118 | for (const _turn of [1, 2]) await handlers.get("before_agent_start")!({}, ctx); |
| 119 | } |
| 120 | assert.equal(sent.length, notices * 2); |
| 121 | for (const message of sent) |
| 122 | assert.include(message, "for /repo because the folder is not trusted"); |
| 123 | }); |
| 124 | }); |
| 125 | |
| 126 | describe("Pi MCP tool results", () => { |
| 127 | it("returns mixed screenshot blocks and typed script output without repeating structured text", async () => { |
| 128 | const content = [ |
| 129 | { type: "text", text: "Browser screenshot" }, |
| 130 | { type: "image", data: "AAAA", mimeType: "image/png", _meta: { private: true } }, |
| 131 | { type: "text", text: "Screenshot captured" }, |
| 132 | ] as const; |
| 133 | const result = { content, structuredContent: { count: 2 }, _meta: { private: true } }; |
| 134 | const bridge = await loadMcpBridge({ modern: true, result }); |
| 135 | const tool = bridge.tools.find((tool) => tool.name === "mcp__t3-code__preview_snapshot")!; |
| 136 | const output = await tool.execute("snapshot", {}); |
| 137 | assert.deepEqual(output.content, [ |
| 138 | content[0], |
| 139 | { type: "image", data: "AAAA", mimeType: "image/png" }, |
| 140 | content[2], |
| 141 | ]); |
| 142 | assert.deepEqual(output.structuredContent, { content, structuredContent: { count: 2 } }); |
| 143 | assert.deepEqual(tool.outputSchema, { |
| 144 | type: "object", |
| 145 | properties: { |
| 146 | content: { type: "array", items: { type: "object" } }, |
| 147 | structuredContent: { type: "object", properties: { count: { type: "number" } } }, |
| 148 | isError: { type: "boolean" }, |
| 149 | _meta: { type: "object" }, |
| 150 | }, |
| 151 | required: ["content"], |
| 152 | }); |
| 153 | }); |
| 154 | |
| 155 | it.each([false, true])( |
| 156 | "preserves image-only results and error status, isError=%s", |
| 157 | async (isError) => { |
| 158 | const image = { type: "image", data: "AAAA", mimeType: "image/png" }; |
| 159 | const result = { content: [image], isError }; |
| 160 | const bridge = await loadMcpBridge({ result }); |
| 161 | const output = await bridge.tools[0]!.execute("image", {}); |
| 162 | assert.deepEqual(output.content[0], image); |
| 163 | assert.equal(output.content.length, isError ? 2 : 1); |
| 164 | if (isError) assert.include(output.content[1]?.text ?? "", "returned an error"); |
| 165 | assert.equal(output.isError, isError ? true : undefined); |
| 166 | assert.deepEqual(output.structuredContent, result); |
| 167 | }, |
| 168 | ); |
| 169 | |
| 170 | it("uses structured output as model text when content is empty", async () => { |
| 171 | const result = { content: [], structuredContent: { count: 2 } }; |
| 172 | const bridge = await loadMcpBridge({ result }); |
| 173 | const output = await bridge.tools[0]!.execute("structured", {}); |
| 174 | assert.deepEqual(output.content, [{ type: "text", text: '{"count":2}' }]); |
| 175 | assert.deepEqual(output.structuredContent, result); |
| 176 | }); |
| 177 | }); |
| 178 | |
| 179 | describe("Pi MCP tool exposure", () => { |
| 180 | const discoveryContext = { |
| 181 | model: { api: "anthropic-messages" }, |
| 182 | ui: { notify: () => undefined }, |
| 183 | }; |
| 184 | |
| 185 | it("keeps orchestration direct and optional bridge tools discoverable on modern Pi", async () => { |
| 186 | const bridge = await loadMcpBridge({ |
| 187 | modern: true, |
| 188 | toolSearchAvailable: true, |
| 189 | activatesOnRegistration: true, |
| 190 | }); |
| 191 | await bridge.handlers.get("session_start")!({ systemPrompt: "" }, discoveryContext); |
| 192 | const start = bridge.handlers.get("before_agent_start"); |
| 193 | assert.isDefined(start); |
| 194 | const prompt = await start!({ systemPrompt: "Pi system prompt" }, discoveryContext); |
| 195 | assert.include(prompt?.systemPrompt ?? "", "orchestrator_capabilities"); |
| 196 | assert.equal(bridge.servers.length, 0); |
| 197 | assert.equal(bridge.tools.length, 8); |
| 198 | const aliases = [ |
| 199 | "mcp__t3_code__orchestrator_capabilities", |
| 200 | "mcp__t3_code__delegate_task", |
| 201 | "mcp__t3_code__task_status", |
| 202 | "mcp__t3_code__preview_snapshot", |
| 203 | ]; |
| 204 | const publicDirect = [ |
| 205 | "mcp__t3-code__orchestrator_capabilities", |
| 206 | "mcp__t3-code__delegate_task", |
| 207 | "mcp__t3-code__task_status", |
| 208 | ]; |
| 209 | assert.sameMembers(bridge.getActiveTools(), [ |
| 210 | "read", |
| 211 | "tool_search", |
| 212 | ...publicDirect, |
| 213 | ...aliases, |
| 214 | ]); |
| 215 | assert.deepEqual(bridge.declaredTools(), publicDirect); |
| 216 | assert.deepEqual( |
| 217 | bridge.tools |
| 218 | .filter((tool) => tool.name.startsWith("mcp__t3-code__")) |
| 219 | .map((tool) => [tool.name, tool.exposure]), |
| 220 | [ |
| 221 | ["mcp__t3-code__orchestrator_capabilities", "direct"], |
| 222 | ["mcp__t3-code__delegate_task", "direct"], |
| 223 | ["mcp__t3-code__task_status", "direct"], |
| 224 | ["mcp__t3-code__preview_snapshot", "deferred"], |
| 225 | ], |
| 226 | ); |
| 227 | assert.deepEqual( |
| 228 | bridge.tools |
| 229 | .filter((tool) => tool.name.startsWith("mcp__t3_code__")) |
| 230 | .map((tool) => [tool.name, tool.exposure]), |
| 231 | aliases.map((name) => [name, "model-only"]), |
| 232 | ); |
| 233 | const result = await bridge.tools |
| 234 | .find((tool) => tool.name === "mcp__t3-code__preview_snapshot")! |
| 235 | .execute("call-1", { depth: 2 }); |
| 236 | assert.equal(result.content[0]?.text, "browser snapshot"); |
| 237 | assert.equal(bridge.requests.at(-1)?.method, "tools/call"); |
| 238 | }); |
| 239 | |
| 240 | it("restores gateway declarations after model switches and older tree loadouts without excluded tools", async () => { |
| 241 | const bridge = await loadMcpBridge({ |
| 242 | modern: true, |
| 243 | toolSearchAvailable: true, |
| 244 | allowsTool: (name) => name !== "mcp__t3-code__delegate_task", |
| 245 | activatesOnRegistration: true, |
| 246 | }); |
| 247 | const event = { systemPrompt: "" }; |
| 248 | await bridge.handlers.get("session_start")!(event, discoveryContext); |
| 249 | assert.notInclude(bridge.getActiveTools(), "mcp__t3_code__delegate_task"); |
| 250 | const gatewayContext = { model: { api: "openai-completions" }, ui: { notify: assert.fail } }; |
| 251 | await bridge.handlers.get("model_select")!(event, gatewayContext); |
| 252 | bridge.restoreActiveTools(["read", "mcp__t3-code__task_status"]); |
| 253 | await bridge.handlers.get("session_tree")!(event, gatewayContext); |
| 254 | assert.deepEqual(bridge.getActiveTools(), [ |
| 255 | "read", |
| 256 | "mcp__t3-code__task_status", |
| 257 | "tool_search", |
| 258 | "mcp__t3-code__orchestrator_capabilities", |
| 259 | "mcp__t3-code__preview_snapshot", |
| 260 | "mcp__t3_code__orchestrator_capabilities", |
| 261 | "mcp__t3_code__task_status", |
| 262 | "mcp__t3_code__preview_snapshot", |
| 263 | ]); |
| 264 | await bridge.handlers.get("model_select")!(event, discoveryContext); |
| 265 | assert.equal( |
| 266 | bridge.tools.find((tool) => tool.name === "mcp__t3-code__preview_snapshot")?.exposure, |
| 267 | "deferred", |
| 268 | ); |
| 269 | assert.include(bridge.getActiveTools(), "mcp__t3-code__preview_snapshot"); |
| 270 | }); |
| 271 | |
| 272 | it("keeps Pi's canonical MCP tool names callable by the model without declaring them twice", async () => { |
| 273 | const bridge = await loadMcpBridge({ |
| 274 | modern: true, |
| 275 | toolSearchAvailable: true, |
| 276 | activatesOnRegistration: true, |
| 277 | }); |
| 278 | const canonicalNames = [ |
| 279 | "mcp__t3_code__orchestrator_capabilities", |
| 280 | "mcp__t3_code__delegate_task", |
| 281 | "mcp__t3_code__task_status", |
| 282 | "mcp__t3_code__preview_snapshot", |
| 283 | ]; |
| 284 | const unreachableCanonicalNames = async () => { |
| 285 | const unreachable: string[] = []; |
| 286 | for (const name of canonicalNames) { |
| 287 | const result = await bridge.callModelTool(name, {}); |
| 288 | if (result.isError === true) unreachable.push(name); |
| 289 | } |
| 290 | return unreachable; |
| 291 | }; |
| 292 | const declaredCanonicalNames = () => |
| 293 | bridge.declaredTools().filter((name) => name.startsWith("mcp__t3_code__")); |
| 294 | const event = { systemPrompt: "" }; |
| 295 | |
| 296 | await bridge.handlers.get("session_start")!(event, discoveryContext); |
| 297 | assert.deepEqual( |
| 298 | await unreachableCanonicalNames(), |
| 299 | [], |
| 300 | "model calls to these names fail with Tool not found after session start", |
| 301 | ); |
| 302 | assert.deepEqual(declaredCanonicalNames(), [], "declared twice after session start"); |
| 303 | assert.equal(bridge.requests.at(-1)?.method, "tools/call"); |
| 304 | |
| 305 | bridge.restoreActiveTools([ |
| 306 | "read", |
| 307 | "mcp__t3-code__orchestrator_capabilities", |
| 308 | "mcp__t3-code__delegate_task", |
| 309 | "mcp__t3-code__task_status", |
| 310 | ]); |
| 311 | await bridge.handlers.get("session_tree")!(event, discoveryContext); |
| 312 | assert.deepEqual( |
| 313 | await unreachableCanonicalNames(), |
| 314 | [], |
| 315 | "model calls to these names fail with Tool not found after an older tree loadout", |
| 316 | ); |
| 317 | assert.deepEqual(declaredCanonicalNames(), [], "declared twice after an older tree loadout"); |
| 318 | assert.isFalse( |
| 319 | bridge.callableTools().some((name) => name.startsWith("mcp__t3_code__")), |
| 320 | "aliases duplicate the codemode catalog", |
| 321 | ); |
| 322 | |
| 323 | const requestCount = bridge.requests.length; |
| 324 | bridge.restoreActiveTools( |
| 325 | bridge.getActiveTools().filter((name) => name !== "mcp__t3-code__delegate_task"), |
| 326 | ); |
| 327 | const late = await bridge.callModelTool("mcp__t3_code__delegate_task", {}); |
| 328 | assert.isTrue(late.isError, "an alias runs after its public tool was switched off"); |
| 329 | assert.equal(bridge.requests.length, requestCount); |
| 330 | |
| 331 | bridge.restoreActiveTools(["read", "mcp__t3-code__task_status"]); |
| 332 | await bridge.handlers.get("session_tree")!(event, discoveryContext); |
| 333 | assert.deepEqual( |
| 334 | await unreachableCanonicalNames(), |
| 335 | ["mcp__t3_code__orchestrator_capabilities", "mcp__t3_code__delegate_task"], |
| 336 | "an alias stays callable while its direct public tool is off", |
| 337 | ); |
| 338 | }); |
| 339 | |
| 340 | it.each(["legacy Pi", "disabled tool search"])( |
| 341 | "keeps tool execution available with %s", |
| 342 | async (mode) => { |
| 343 | const bridge = await loadMcpBridge({ |
| 344 | modern: mode !== "legacy Pi", |
| 345 | toolSearchAvailable: mode === "disabled tool search", |
| 346 | toolSearchDisabled: mode === "disabled tool search", |
| 347 | }); |
| 348 | if (mode !== "legacy Pi") { |
| 349 | const start = bridge.handlers.get("session_start"); |
| 350 | await start!({ systemPrompt: "Pi system prompt" }, discoveryContext); |
| 351 | } |
| 352 | const publicTools = bridge.tools.filter((tool) => tool.name.startsWith("mcp__t3-code__")); |
| 353 | assert.equal(publicTools.length, 4); |
| 354 | assert.isTrue( |
| 355 | publicTools.every((tool) => tool.exposure === undefined || tool.exposure === "direct"), |
| 356 | ); |
| 357 | assert.deepEqual( |
| 358 | bridge.tools |
| 359 | .filter((tool) => tool.name.startsWith("mcp__t3_code__")) |
| 360 | .map((tool) => tool.exposure), |
| 361 | mode === "legacy Pi" ? [] : ["model-only", "model-only", "model-only", "model-only"], |
| 362 | ); |
| 363 | const tool = bridge.tools.find((tool) => tool.name === "mcp__t3-code__preview_snapshot"); |
| 364 | assert.isDefined(tool); |
| 365 | const controller = new AbortController(); |
| 366 | const result = await tool!.execute("call-1", { depth: 2 }, controller.signal); |
| 367 | assert.equal(result.content[0]?.text, "browser snapshot"); |
| 368 | assert.strictEqual(bridge.transports.at(-1)?.signal, controller.signal); |
| 369 | assert.equal(bridge.transports.at(-1)?.url, "http://fixture.invalid/mcp"); |
| 370 | assert.equal(bridge.transports.at(-1)?.authorization, "Bearer fixture-token"); |
| 371 | assert.deepEqual(JSON.parse(JSON.stringify(bridge.requests.at(-1))), { |
| 372 | jsonrpc: "2.0", |
| 373 | id: 3, |
| 374 | method: "tools/call", |
| 375 | params: { name: "preview_snapshot", arguments: { depth: 2 } }, |
| 376 | }); |
| 377 | assert.isUndefined(tool?.promptSnippet); |
| 378 | assert.isUndefined(tool?.promptGuidelines); |
| 379 | }, |
| 380 | ); |
| 381 | }); |
| 382 | |
| 383 | describe("Pi MCP tool schemas", () => { |
| 384 | it.each([{ type: "object" }, {}])( |
| 385 | "declares empty properties on object schemas that omit them: %j", |
| 386 | async (inputSchema) => { |
| 387 | const bridge = await loadMcpBridge({ inputSchema }); |
| 388 | assert.isAbove(bridge.tools.length, 0); |
| 389 | for (const tool of bridge.tools) { |
| 390 | assert.deepEqual(tool.parameters, { type: "object", properties: {} }); |
| 391 | } |
| 392 | }, |
| 393 | ); |
| 394 | |
| 395 | it("keeps declared properties and other schema keywords", async () => { |
| 396 | const inputSchema = { |
| 397 | type: "object", |
| 398 | properties: { depth: { type: "number" } }, |
| 399 | required: ["depth"], |
| 400 | additionalProperties: false, |
| 401 | }; |
| 402 | const bridge = await loadMcpBridge({ inputSchema }); |
| 403 | for (const tool of bridge.tools) { |
| 404 | assert.deepEqual(tool.parameters, inputSchema); |
| 405 | } |
| 406 | }); |
| 407 | }); |
| 408 | |
| 409 | describe("Pi tool discovery permissions", () => { |
| 410 | it.each(["approval-required", "auto-accept-edits", "auto"])( |
| 411 | "allows annotated T3 reads in %s while gating mutations and replacements", |
| 412 | async (runtimeMode) => { |
| 413 | const bridge = await loadMcpBridge({ modern: true, runtimeMode }); |
| 414 | const hook = bridge.handlers.get("tool_call") as unknown as ( |
| 415 | event: { toolName: string; input: unknown }, |
| 416 | ctx: { ui: { confirm: (title: string) => Promise<boolean> } }, |
| 417 | ) => Promise<{ block: true; reason: string } | undefined>; |
| 418 | const confirmations: string[] = []; |
| 419 | const ctx = { |
| 420 | ui: { |
| 421 | confirm: async (title: string) => { |
| 422 | confirmations.push(title); |
| 423 | return false; |
| 424 | }, |
| 425 | }, |
| 426 | }; |
| 427 | for (const prefix of ["mcp__t3-code__", "mcp__t3_code__"]) { |
| 428 | assert.isUndefined( |
| 429 | await hook({ toolName: prefix + "orchestrator_capabilities", input: {} }, ctx), |
| 430 | ); |
| 431 | assert.equal( |
| 432 | (await hook({ toolName: prefix + "delegate_task", input: {} }, ctx))?.block, |
| 433 | true, |
| 434 | ); |
| 435 | assert.equal( |
| 436 | (await hook({ toolName: prefix + "task_status", input: {} }, ctx))?.block, |
| 437 | true, |
| 438 | ); |
| 439 | } |
| 440 | assert.equal(confirmations.length, 4); |
| 441 | bridge.setBridgeSourcePath("/user/extensions/replacement.ts"); |
| 442 | assert.equal( |
| 443 | (await hook({ toolName: "mcp__t3-code__orchestrator_capabilities", input: {} }, ctx)) |
| 444 | ?.block, |
| 445 | true, |
| 446 | ); |
| 447 | assert.equal( |
| 448 | (await hook({ toolName: "mcp__other__orchestrator_capabilities", input: {} }, ctx))?.block, |
| 449 | true, |
| 450 | ); |
| 451 | assert.equal(confirmations.length, 6); |
| 452 | }, |
| 453 | ); |
| 454 | |
| 455 | it("allows discovery without confirmation and still gates the discovered tool", async () => { |
| 456 | type ToolCallHook = ( |
| 457 | event: { toolName: string; input: unknown }, |
| 458 | ctx: { |
| 459 | signal: AbortSignal; |
| 460 | ui: { |
| 461 | confirm: ( |
| 462 | title: string, |
| 463 | detail: string, |
| 464 | options: { signal: AbortSignal }, |
| 465 | ) => Promise<boolean>; |
| 466 | }; |
| 467 | }, |
| 468 | ) => Promise<{ block: true; reason: string } | undefined>; |
| 469 | let toolCall: ToolCallHook | undefined; |
| 470 | let searchPath = "builtin:tool-search"; |
| 471 | const source = NodeModule.stripTypeScriptTypes( |
| 472 | PI_T3_MCP_EXTENSION_SOURCE.replace(/^import .*;$/gm, "").replace( |
| 473 | "export default async function", |
| 474 | "async function", |
| 475 | ), |
| 476 | ); |
| 477 | await NodeVM.runInNewContext(`${source}\nt3McpExtension(pi)`, { |
| 478 | process: { env: { T3_PI_RUNTIME_MODE: "approval-required" } }, |
| 479 | pi: { |
| 480 | on: (name: string, handler: ToolCallHook) => { |
| 481 | if (name === "tool_call") toolCall = handler; |
| 482 | }, |
| 483 | registerCommand: () => {}, |
| 484 | getAllTools: () => [{ name: "tool_search", sourceInfo: { path: searchPath } }], |
| 485 | }, |
| 486 | }); |
| 487 | assert.isDefined(toolCall); |
| 488 | const confirmations: string[] = []; |
| 489 | const controller = new AbortController(); |
| 490 | const ctx = { |
| 491 | signal: controller.signal, |
| 492 | ui: { |
| 493 | confirm: async (title: string, _detail: string, options: { signal: AbortSignal }) => { |
| 494 | assert.strictEqual(options.signal, controller.signal); |
| 495 | confirmations.push(title); |
| 496 | return false; |
| 497 | }, |
| 498 | }, |
| 499 | }; |
| 500 | for (const builtinPath of ["builtin:tool-search", "<builtin:tool-search>"]) { |
| 501 | searchPath = builtinPath; |
| 502 | assert.isUndefined( |
| 503 | await toolCall!({ toolName: "tool_search", input: { query: "preview_snapshot" } }, ctx), |
| 504 | ); |
| 505 | } |
| 506 | assert.equal(confirmations.length, 0); |
| 507 | const result = await toolCall!({ toolName: "mcp__t3-code__preview_snapshot", input: {} }, ctx); |
| 508 | assert.equal(result?.block, true); |
| 509 | assert.deepEqual(confirmations, ["Allow mcp__t3-code__preview_snapshot?"]); |
| 510 | |
| 511 | searchPath = "/extensions/custom-search.ts"; |
| 512 | const replaced = await toolCall!({ toolName: "tool_search", input: {} }, ctx); |
| 513 | assert.equal(replaced?.block, true); |
| 514 | assert.equal(confirmations.at(-1), "Allow tool_search?"); |
| 515 | }); |
| 516 | |
| 517 | it("gives long inputs that differ past the shown text different confirmation details", async () => { |
| 518 | const bridge = await loadMcpBridge({ runtimeMode: "approval-required" }); |
| 519 | const hook = bridge.handlers.get("tool_call") as unknown as ( |
| 520 | event: { toolName: string; input: unknown }, |
| 521 | ctx: { ui: { confirm: (title: string, detail: string) => Promise<boolean> } }, |
| 522 | ) => Promise<{ block: true; reason: string } | undefined>; |
| 523 | const details: string[] = []; |
| 524 | const ctx = { |
| 525 | ui: { |
| 526 | confirm: async (_title: string, detail: string) => { |
| 527 | details.push(detail); |
| 528 | return true; |
| 529 | }, |
| 530 | }, |
| 531 | }; |
| 532 | const filler = "a".repeat(4_100); |
| 533 | await hook({ toolName: "bash", input: { command: filler + "\necho one" } }, ctx); |
| 534 | await hook({ toolName: "bash", input: { command: filler + "\necho two" } }, ctx); |
| 535 | await hook({ toolName: "bash", input: { command: "ls" } }, ctx); |
| 536 | const [first, second, short] = details; |
| 537 | assert.equal(first!.slice(0, 4_000), second!.slice(0, 4_000)); |
| 538 | assert.notEqual(first, second); |
| 539 | assert.match(first!, /\n\.\.\. \d+ more characters not shown \(sha256 [0-9a-f]{64}\)$/); |
| 540 | assert.equal(short, JSON.stringify({ command: "ls" }, null, 2)); |
| 541 | }); |
| 542 | }); |
| 543 | |
| 544 | async function loadRequestHook() { |
| 545 | const handlers = new Map<string, RequestHook>(); |
| 546 | const commands = new Map<string, CommandHandler>(); |
| 547 | const source = NodeModule.stripTypeScriptTypes( |
| 548 | PI_T3_MCP_EXTENSION_SOURCE.replace(/^import .*;$/gm, "").replace( |
| 549 | "export default async function", |
| 550 | "async function", |
| 551 | ), |
| 552 | ); |
| 553 | await NodeVM.runInNewContext(`${source}\nt3McpExtension(pi)`, { |
| 554 | process: { env: {} }, |
| 555 | pi: { |
| 556 | on: (name: string, handler: RequestHook) => handlers.set(name, handler), |
| 557 | registerCommand: (name: string, options: { handler: CommandHandler }) => |
| 558 | commands.set(name, options.handler), |
| 559 | }, |
| 560 | }); |
| 561 | const hook = handlers.get("before_provider_request"); |
| 562 | const setServiceTier = commands.get(PI_T3_SERVICE_TIER_COMMAND); |
| 563 | assert.isDefined(hook); |
| 564 | assert.isDefined(setServiceTier); |
| 565 | return Object.assign(hook!, { setServiceTier: setServiceTier! }); |
| 566 | } |
| 567 | |
| 568 | describe("Pi service tier", () => { |
| 569 | it("sends only a tier the active model advertises, until Standard clears it", async () => { |
| 570 | const hook = await loadRequestHook(); |
| 571 | const sol = { |
| 572 | provider: "columbia-codex", |
| 573 | serviceTiers: [{ id: "priority" }, { id: "ultrafast" }], |
| 574 | }; |
| 575 | const payload = { model: "gpt-6.1-sol", reasoning: { effort: "high" } }; |
| 576 | assert.isUndefined(hook({ payload }, { model: sol })); |
| 577 | |
| 578 | await hook.setServiceTier("priority"); |
| 579 | assert.deepEqual(hook({ payload }, { model: sol }), { ...payload, service_tier: "priority" }); |
| 580 | |
| 581 | await hook.setServiceTier("ultrafast"); |
| 582 | assert.equal(hook({ payload }, { model: sol })?.service_tier, "ultrafast"); |
| 583 | const fastOnly = { provider: "columbia-codex", serviceTiers: [{ id: "priority" }] }; |
| 584 | assert.isUndefined(hook({ payload }, { model: fastOnly })); |
| 585 | assert.isUndefined(hook({ payload }, { model: { provider: "anthropic" } })); |
| 586 | |
| 587 | await hook.setServiceTier(""); |
| 588 | assert.isUndefined(hook({ payload }, { model: sol })); |
| 589 | }); |
| 590 | }); |
| 591 | |
| 592 | describe("Pi upstream output-budget workaround", () => { |
| 593 | it.each(["max_tokens", "max_completion_tokens"])( |
| 594 | "caps %s without changing the conversation or tools", |
| 595 | async (key) => { |
| 596 | const hook = await loadRequestHook(); |
| 597 | const payload = { |
| 598 | model: "moonshotai/kimi-k2.6", |
| 599 | messages: [{ role: "user", content: "hello" }], |
| 600 | tools: [{ type: "function", function: { name: "read" } }], |
| 601 | [key]: 231_969, |
| 602 | }; |
| 603 | const result = hook({ payload }, { model: { provider: "openrouter" } }); |
| 604 | assert.equal(result?.[key], 32_768); |
| 605 | assert.strictEqual(result?.messages, payload.messages); |
| 606 | assert.strictEqual(result?.tools, payload.tools); |
| 607 | assert.equal(result?.model, payload.model); |
| 608 | assert.equal(payload[key], 231_969); |
| 609 | }, |
| 610 | ); |
| 611 | |
| 612 | it("preserves smaller budgets and other providers' payloads", async () => { |
| 613 | const hook = await loadRequestHook(); |
| 614 | for (const payload of [{ max_tokens: 8192 }, { max_completion_tokens: 32_768 }, {}, null]) { |
| 615 | assert.isUndefined(hook({ payload }, { model: { provider: "openrouter" } })); |
| 616 | } |
| 617 | assert.isUndefined( |
| 618 | hook({ payload: { max_tokens: 231_969 } }, { model: { provider: "anthropic" } }), |
| 619 | ); |
| 620 | }); |
| 621 | }); |
| 622 | |
| 623 | async function loadInputHook(commands: ReadonlyArray<SkillCommand>) { |
| 624 | const handlers = await loadHooks(commands); |
| 625 | const hook = handlers.get("input"); |
| 626 | assert.isDefined(hook); |
| 627 | return hook as InputHook; |
| 628 | } |
| 629 | |
| 630 | describe("Pi skill references", () => { |
| 631 | it("loads every selected skill once while preserving inline prose, whitespace, and images", async () => { |
| 632 | const directory = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "pi-skill-mentions-")); |
| 633 | try { |
| 634 | const alpha = NodePath.join(directory, "alpha.md"); |
| 635 | const beta = NodePath.join(directory, "beta.md"); |
| 636 | await NodeFSP.writeFile(alpha, "ALPHA_INSTRUCTIONS"); |
| 637 | await NodeFSP.writeFile(beta, "BETA_INSTRUCTIONS"); |
| 638 | const hook = await loadInputHook([ |
| 639 | { name: "skill:alpha", source: "skill", sourceInfo: { path: alpha } }, |
| 640 | { name: "skill:beta", source: "skill", sourceInfo: { path: beta } }, |
| 641 | ]); |
| 642 | const text = |
| 643 | "Please use the $alpha philosophy, then $beta and $alpha\n```ts\n const x = 1;\n```"; |
| 644 | const images = [{ type: "image", data: "fixture" }]; |
| 645 | const result = await hook({ text, images }, { ui: { notify: assert.fail } }); |
| 646 | assert.equal(result?.action, "transform"); |
| 647 | assert.isTrue(result?.text.startsWith(text + "\n\n")); |
| 648 | assert.equal(result?.text.split("ALPHA_INSTRUCTIONS").length, 2); |
| 649 | assert.equal(result?.text.split("BETA_INSTRUCTIONS").length, 2); |
| 650 | assert.include(result?.text ?? "", `References are relative to ${directory}.`); |
| 651 | assert.strictEqual(result?.images, images); |
| 652 | } finally { |
| 653 | await NodeFSP.rm(directory, { recursive: true }); |
| 654 | } |
| 655 | }); |
| 656 | |
| 657 | it("leaves unknown references and native skill commands for Pi", async () => { |
| 658 | const hook = await loadInputHook([ |
| 659 | { name: "skill:alpha", source: "skill", sourceInfo: { path: "/unused" } }, |
| 660 | ]); |
| 661 | for (const text of [ |
| 662 | "Explain $HOME", |
| 663 | "/skill:alpha use $alpha", |
| 664 | "Hello", |
| 665 | "$missing", |
| 666 | "Use $alpha, then continue", |
| 667 | "Use ($alpha)", |
| 668 | ]) { |
| 669 | assert.isUndefined(await hook({ text }, { ui: { notify: assert.fail } })); |
| 670 | } |
| 671 | }); |
| 672 | |
| 673 | it("loads additional chips without duplicating a leading native skill", async () => { |
| 674 | const directory = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "pi-skill-mentions-")); |
| 675 | try { |
| 676 | const path = NodePath.join(directory, "beta.md"); |
| 677 | await NodeFSP.writeFile(path, "BETA_INSTRUCTIONS"); |
| 678 | const hook = await loadInputHook([ |
| 679 | { name: "skill:alpha", source: "skill", sourceInfo: { path: "/unused" } }, |
| 680 | { name: "skill:beta", source: "skill", sourceInfo: { path } }, |
| 681 | ]); |
| 682 | const text = "/skill:alpha use $alpha and $beta"; |
| 683 | const result = await hook({ text }, { ui: { notify: assert.fail } }); |
| 684 | assert.isTrue(result?.text.startsWith(text + "\n\n")); |
| 685 | assert.notInclude(result?.text ?? "", '<skill name="alpha"'); |
| 686 | assert.include(result?.text ?? "", "BETA_INSTRUCTIONS"); |
| 687 | } finally { |
| 688 | await NodeFSP.rm(directory, { recursive: true }); |
| 689 | } |
| 690 | }); |
| 691 | |
| 692 | it("reports an unreadable skill without deleting its reference or other selected instructions", async () => { |
| 693 | const directory = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "pi-skill-mentions-")); |
| 694 | try { |
| 695 | const path = NodePath.join(directory, "readable.md"); |
| 696 | await NodeFSP.writeFile(path, "READABLE_INSTRUCTIONS"); |
| 697 | const hook = await loadInputHook([ |
| 698 | { |
| 699 | name: "skill:missing", |
| 700 | source: "skill", |
| 701 | sourceInfo: { path: NodePath.join(directory, "missing.md") }, |
| 702 | }, |
| 703 | { name: "skill:readable", source: "skill", sourceInfo: { path } }, |
| 704 | ]); |
| 705 | const notices: string[] = []; |
| 706 | const text = "Use $missing and $readable"; |
| 707 | const result = await hook( |
| 708 | { text }, |
| 709 | { |
| 710 | ui: { |
| 711 | notify: (message) => { |
| 712 | notices.push(message); |
| 713 | }, |
| 714 | }, |
| 715 | }, |
| 716 | ); |
| 717 | assert.isTrue(result?.text.startsWith(text + "\n\n")); |
| 718 | assert.include(result?.text ?? "", "READABLE_INSTRUCTIONS"); |
| 719 | assert.equal(notices.length, 1); |
| 720 | assert.include(notices[0] ?? "", "Could not load skill missing"); |
| 721 | } finally { |
| 722 | await NodeFSP.rm(directory, { recursive: true }); |
| 723 | } |
| 724 | }); |
| 725 | }); |