/** * Debugger Mode Extension * * Strictly read-only root-cause-analysis mode. When enabled, pi analyzes code * for a given problem and never makes changes: * - `edit`/`write` are removed from the active tool set * - `bash` is restricted to an allowlist of read-only commands * - A read-only RCA instruction set is injected into the prompt * * Toggle: /debugger or Ctrl+Shift+. * State persists across session resume. */ import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent"; import { Key } from "@earendil-works/pi-tui"; // Built-in tools removed while debugger mode is on const DEBUGGER_DISABLED_TOOLS = new Set(["edit", "write"]); // Read-only built-ins kept available (plus any non-builtin tools already active) const DEBUGGER_READ_ONLY_TOOLS = ["read", "bash", "grep", "find", "ls"]; // --- bash read-only allowlist (adapted from examples/extensions/plan-mode/utils.ts) --- const DESTRUCTIVE_PATTERNS = [ /\brm\b/i, /\brmdir\b/i, /\bmv\b/i, /\bcp\b/i, /\bmkdir\b/i, /\btouch\b/i, /\bchmod\b/i, /\bchown\b/i, /\bchgrp\b/i, /\bln\b/i, /\btee\b/i, /\btruncate\b/i, /\bdd\b/i, /\bshred\b/i, /(^|[^<])>(?!>)/, />>/, /\bnpm\s+(install|uninstall|update|ci|link|publish)/i, /\byarn\s+(add|remove|install|publish)/i, /\bpnpm\s+(add|remove|install|publish)/i, /\bpip\s+(install|uninstall)/i, /\bapt(-get)?\s+(install|remove|purge|update|upgrade)/i, /\bbrew\s+(install|uninstall|upgrade)/i, /\bgit\s+(add|commit|push|pull|merge|rebase|reset|checkout|branch\s+-[dD]|stash|cherry-pick|revert|tag|init|clone)/i, /\bsudo\b/i, /\bsu\b/i, /\bkill\b/i, /\bpkill\b/i, /\bkillall\b/i, /\breboot\b/i, /\bshutdown\b/i, /\bsystemctl\s+(start|stop|restart|enable|disable)/i, /\bservice\s+\S+\s+(start|stop|restart)/i, /\b(vim?|nano|emacs|code|subl)\b/i, ]; const SAFE_PATTERNS = [ /^\s*cat\b/, /^\s*head\b/, /^\s*tail\b/, /^\s*less\b/, /^\s*more\b/, /^\s*grep\b/, /^\s*find\b/, /^\s*ls\b/, /^\s*pwd\b/, /^\s*echo\b/, /^\s*printf\b/, /^\s*wc\b/, /^\s*sort\b/, /^\s*uniq\b/, /^\s*diff\b/, /^\s*file\b/, /^\s*stat\b/, /^\s*du\b/, /^\s*df\b/, /^\s*tree\b/, /^\s*which\b/, /^\s*whereis\b/, /^\s*type\b/, /^\s*env\b/, /^\s*printenv\b/, /^\s*uname\b/, /^\s*whoami\b/, /^\s*id\b/, /^\s*date\b/, /^\s*cal\b/, /^\s*uptime\b/, /^\s*ps\b/, /^\s*top\b/, /^\s*htop\b/, /^\s*free\b/, /^\s*git\s+(status|log|diff|show|branch|remote|config\s+--get)/i, /^\s*git\s+ls-/i, /^\s*npm\s+(list|ls|view|info|search|outdated|audit)/i, /^\s*yarn\s+(list|info|why|audit)/i, /^\s*node\s+--version/i, /^\s*python\s+--version/i, /^\s*curl\s/i, /^\s*wget\s+-O\s*-/i, /^\s*jq\b/, /^\s*sed\s+-n/i, /^\s*awk\b/, /^\s*rg\b/, /^\s*fd\b/, /^\s*bat\b/, /^\s*eza\b/, ]; function isSafeCommand(command: string): boolean { const isDestructive = DESTRUCTIVE_PATTERNS.some((p) => p.test(command)); const isSafe = SAFE_PATTERNS.some((p) => p.test(command)); return !isDestructive && isSafe; } export default function debuggerModeExtension(pi: ExtensionAPI): void { let enabled = false; let toolsBefore: string[] | undefined; function updateStatus(ctx: ExtensionContext): void { ctx.ui.setStatus("debugger-mode", enabled ? ctx.ui.theme.fg("warning", "🔍 debug") : undefined); } function enableTools(): void { if (toolsBefore === undefined) { toolsBefore = pi.getActiveTools(); } const kept = toolsBefore.filter((name) => !DEBUGGER_DISABLED_TOOLS.has(name)); pi.setActiveTools([...new Set([...kept, ...DEBUGGER_READ_ONLY_TOOLS])]); } function restoreTools(): void { pi.setActiveTools(toolsBefore ?? pi.getActiveTools()); toolsBefore = undefined; } function toggle(ctx: ExtensionContext): void { enabled = !enabled; if (enabled) { enableTools(); ctx.ui.notify("Debugger mode enabled. Analysis only — no file changes."); } else { restoreTools(); ctx.ui.notify("Debugger mode disabled. Full access restored."); } updateStatus(ctx); pi.appendEntry("debugger-mode", { enabled, toolsBefore }); } pi.registerCommand("debugger", { description: "Toggle debugger mode (read-only RCA, no code changes)", handler: async (_args, ctx) => toggle(ctx), }); pi.registerShortcut(Key.ctrlShift("."), { description: "Toggle debugger mode", handler: async (ctx) => toggle(ctx), }); // Hard block: write tools and non-read-only bash while enabled pi.on("tool_call", async (event) => { if (!enabled) return; if (DEBUGGER_DISABLED_TOOLS.has(event.toolName)) { return { block: true, reason: `Debugger mode: ${event.toolName} is disabled. Use /debugger to turn debugger mode off first.`, }; } if (event.toolName === "bash") { const command = event.input.command as string; if (!isSafeCommand(command)) { return { block: true, reason: `Debugger mode: bash command blocked (not read-only). Use /debugger to turn debugger mode off first.\nCommand: ${command}`, }; } } }); // Drop stale debugger-mode context once the mode is off pi.on("context", async (event) => { if (enabled) return; return { messages: event.messages.filter( (m) => (m as { customType?: string }).customType !== "debugger-mode-context", ), }; }); // Inject read-only RCA instructions while enabled pi.on("before_agent_start", async () => { if (!enabled) return; return { message: { customType: "debugger-mode-context", content: `[DEBUGGER MODE ACTIVE] You are in debugger mode - a strictly read-only root-cause-analysis mode. Restrictions: - edit and write tools are disabled; you cannot and must not modify any file - bash is restricted to read-only commands (no writes, no git mutations, no installs) Your job: given the problem, analyze the code and find the root cause. - Inspect and reproduce: read files, search, run read-only commands (tests, git log/diff, observation-only scripts) - Trace the actual flow end to end before concluding - Answer with a free-form RCA: the symptom, the root cause, the evidence (files/lines), and what the fix would be - Never apply a fix and never write files. Describe the fix only.`, display: false, }, }; }); // Restore state on session start/resume pi.on("session_start", async (_event, ctx) => { const entries = ctx.sessionManager.getEntries(); const entry = entries .filter((e: { type: string; customType?: string }) => e.type === "custom" && e.customType === "debugger-mode") .pop() as { data?: { enabled?: boolean; toolsBefore?: string[] } } | undefined; if (entry?.data) { enabled = entry.data.enabled ?? false; toolsBefore = entry.data.toolsBefore; if (enabled) enableTools(); } updateStatus(ctx); }); }