| 1 | import { realpathSync } from "node:fs"; |
| 2 | import { dirname, basename, isAbsolute, join, normalize } from "node:path"; |
| 3 | |
| 4 | // Equipo's gate for Pi (connectors spec 2026-11-12, §4.1). Pi has no |
| 5 | // permission layer of its own; this extension is the only one it loads |
| 6 | // besides its built-in MCP support. EQUIPO_PI_TOOLS lists the tools a |
| 7 | // step may call without asking (the tools its role pins). EQUIPO_PI_CONFINE is the one |
| 8 | // directory its native file tools may use. Anything else is asked of Equipo over RPC (an |
| 9 | // extension_ui_request), and runs only on a yes. |
| 10 | export default function (pi: any) { |
| 11 | const allowed = new Set((process.env.EQUIPO_PI_TOOLS ?? "").split(",").filter((t) => t !== "")); |
| 12 | const confine = process.env.EQUIPO_PI_CONFINE ?? ""; |
| 13 | const fileTools = new Set(["read", "grep", "find", "ls", "write", "edit"]); |
| 14 | const inside = (raw: unknown): boolean => { |
| 15 | let path = typeof raw === "string" && raw !== "" ? raw : "."; |
| 16 | path = normalize(isAbsolute(path) ? path : join(process.cwd(), path)); |
| 17 | try { |
| 18 | path = realpathSync(path); |
| 19 | } catch { |
| 20 | // Resolve the nearest existing parent for new files, too. |
| 21 | const tail: string[] = []; |
| 22 | let parent = path; |
| 23 | while (parent !== "/") { |
| 24 | tail.unshift(basename(parent)); |
| 25 | parent = dirname(parent); |
| 26 | try { path = join(realpathSync(parent), ...tail); break; } catch {} |
| 27 | } |
| 28 | } |
| 29 | return path === confine || path.startsWith(confine + "/"); |
| 30 | }; |
| 31 | // Pi gates every call in a message before running them together, so a |
| 32 | // shell command could swap a checked path for a link before a file tool |
| 33 | // opens it. A file tool and a shell command never run at the same time. |
| 34 | const running = new Map<string, string>(); |
| 35 | const kindOf = (name: string): string => (name === "bash" ? "shell" : fileTools.has(name) ? "file" : ""); |
| 36 | const clash = (kind: string): boolean => |
| 37 | kind !== "" && [...running.values()].some((k) => k !== kind); |
| 38 | pi.on("tool_result", async (event: any) => { |
| 39 | running.delete(event.toolCallId); |
| 40 | return undefined; |
| 41 | }); |
| 42 | pi.on("agent_end", async () => { |
| 43 | running.clear(); |
| 44 | }); |
| 45 | pi.on("tool_call", async (event: any, ctx: any) => { |
| 46 | const kind = kindOf(event.toolName); |
| 47 | if (clash(kind)) { |
| 48 | return { block: true, reason: "Run file tools and shell commands in separate messages." }; |
| 49 | } |
| 50 | const allow = () => { |
| 51 | if (kind !== "") running.set(event.toolCallId, kind); |
| 52 | return undefined; |
| 53 | }; |
| 54 | let ok = allowed.has(event.toolName); |
| 55 | if (ok && confine !== "" && fileTools.has(event.toolName)) { |
| 56 | ok = inside(event.input?.path); |
| 57 | } |
| 58 | if (ok) { |
| 59 | return allow(); |
| 60 | } |
| 61 | const yes = await ctx.ui.confirm("equipo:tool_call", JSON.stringify({ id: event.toolCallId, tool: event.toolName, args: event.input })); |
| 62 | if (yes === true) { |
| 63 | return clash(kind) ? { block: true, reason: "Run file tools and shell commands in separate messages." } : allow(); |
| 64 | } |
| 65 | return { block: true, reason: "Equipo did not allow this call." }; |
| 66 | }); |
| 67 | } |
| 68 | |