import { realpathSync } from "node:fs"; import { dirname, basename, isAbsolute, join, normalize } from "node:path"; // Equipo's gate for Pi (connectors spec 2026-11-12, ยง4.1). Pi has no // permission layer of its own; this extension is the only one it loads // besides its built-in MCP support. EQUIPO_PI_TOOLS lists the tools a // step may call without asking (the tools its role pins). EQUIPO_PI_CONFINE is the one // directory its native file tools may use. Anything else is asked of Equipo over RPC (an // extension_ui_request), and runs only on a yes. export default function (pi: any) { const allowed = new Set((process.env.EQUIPO_PI_TOOLS ?? "").split(",").filter((t) => t !== "")); const confine = process.env.EQUIPO_PI_CONFINE ?? ""; const fileTools = new Set(["read", "grep", "find", "ls", "write", "edit"]); const inside = (raw: unknown): boolean => { let path = typeof raw === "string" && raw !== "" ? raw : "."; path = normalize(isAbsolute(path) ? path : join(process.cwd(), path)); try { path = realpathSync(path); } catch { // Resolve the nearest existing parent for new files, too. const tail: string[] = []; let parent = path; while (parent !== "/") { tail.unshift(basename(parent)); parent = dirname(parent); try { path = join(realpathSync(parent), ...tail); break; } catch {} } } return path === confine || path.startsWith(confine + "/"); }; // Pi gates every call in a message before running them together, so a // shell command could swap a checked path for a link before a file tool // opens it. A file tool and a shell command never run at the same time. const running = new Map(); const kindOf = (name: string): string => (name === "bash" ? "shell" : fileTools.has(name) ? "file" : ""); const clash = (kind: string): boolean => kind !== "" && [...running.values()].some((k) => k !== kind); pi.on("tool_result", async (event: any) => { running.delete(event.toolCallId); return undefined; }); pi.on("agent_end", async () => { running.clear(); }); pi.on("tool_call", async (event: any, ctx: any) => { const kind = kindOf(event.toolName); if (clash(kind)) { return { block: true, reason: "Run file tools and shell commands in separate messages." }; } const allow = () => { if (kind !== "") running.set(event.toolCallId, kind); return undefined; }; let ok = allowed.has(event.toolName); if (ok && confine !== "" && fileTools.has(event.toolName)) { ok = inside(event.input?.path); } if (ok) { return allow(); } const yes = await ctx.ui.confirm("equipo:tool_call", JSON.stringify({ id: event.toolCallId, tool: event.toolName, args: event.input })); if (yes === true) { return clash(kind) ? { block: true, reason: "Run file tools and shell commands in separate messages." } : allow(); } return { block: true, reason: "Equipo did not allow this call." }; }); }