1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/**
 * Permission Gate Extension
 *
 * Requires explicit user confirmation before every tool call.
 * Auto-allows read-only operations (read tool, non-destructive bash commands).
 * Auto-allows reading from tracked files, skills directories, and pi docs.
 * Shows tool name and a summary of the arguments, then asks to allow or block.
 *
 * Write/edit prompts offer "Allow for session" — once accepted, subsequent
 * writes/edits to tracked files are auto-allowed for the remainder of the
 * session. Writes to untracked files still require confirmation. Resets on
 * session start.
 *
 * Allowed bash commands are configured declaratively in BASE_COMMANDS below.
 * Projects can add to the allowlist via `.pi/permissions.json`, which requires
 * a one-time user confirmation (re-prompted if the file changes).
 */

import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
import { execSync } from "node:child_process";
import { createHash } from "node:crypto";
import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";

// ---------------------------------------------------------------------------
// Allowed bash commands configuration
//
// Each entry maps a command/subcommand to its permission rule, forming a tree:
//
//   true                          — allow with any further arguments
//   string[]                      — shorthand: allow these subcommands (each as true)
//   Record<string, CommandRule>   — recurse into subcommands
//   (args: string) => boolean     — custom predicate on remaining args
//
// Examples:
//   jj: ["diff", "log", "show", "status"],
//   gh: {
//     pr:    ["list", "view", "checks"],
//     issue: ["list", "view"],
//   },
// ---------------------------------------------------------------------------
type CommandRule = true | string[] | ((args: string) => boolean) | { [subcommand: string]: CommandRule };

/** JSON-safe subset of CommandRule (no predicates) for .pi/permissions.json */
type JsonCommandRule = true | string[] | { [subcommand: string]: JsonCommandRule };

const BASE_COMMANDS: CommandRule = {
  gh: { "issue": ["list", "view"], "project": ["item-list", "list"], "repo": ["list"] },
  jj: { "bookmark": ["list"], "diff": true, "log": true, "show": true, "st": true, "status": true },
};

// ---------------------------------------------------------------------------
// Per-project permission overrides
//
// Projects can place a `.pi/permissions.json` in the project root:
//
//   { "allow": { "cargo": ["test", "check"], "make": ["test"] } }
//
// On first use (or when the file changes), the user is shown a summary
// and asked to confirm. Approvals are stored in ~/.pi/agent/ keyed by
// a hash of the file content.
// ---------------------------------------------------------------------------
const APPROVALS_PATH = path.join(os.homedir(), ".pi/agent/approved-permissions.json");

interface ApprovalRecord {
  hash: string;
}

function loadApprovals(): Record<string, ApprovalRecord> {
  try {
    return JSON.parse(fs.readFileSync(APPROVALS_PATH, "utf-8"));
  } catch {
    return {};
  }
}

function saveApprovals(approvals: Record<string, ApprovalRecord>): void {
  fs.mkdirSync(path.dirname(APPROVALS_PATH), { recursive: true });
  fs.writeFileSync(APPROVALS_PATH, JSON.stringify(approvals, null, 2) + "\n");
}

function hashContent(content: string): string {
  return createHash("sha256").update(content).digest("hex");
}

/**
 * Render a command rule tree as a human-readable list of allowed command paths.
 * e.g. { cargo: ["test", "check"] } → ["cargo test", "cargo check"]
 */
function summarizeRules(rule: JsonCommandRule, prefix = ""): string[] {
  if (rule === true) return [prefix || "(all commands)"];
  if (Array.isArray(rule)) return rule.map((s) => `${prefix} ${s}`.trim());
  const lines: string[] = [];
  for (const [key, sub] of Object.entries(rule)) {
    lines.push(...summarizeRules(sub, `${prefix} ${key}`.trim()));
  }
  return lines;
}

/**
 * Deep-merge a JSON rule tree into an existing CommandRule tree.
 * Project rules can only add — they cannot override `true` with something
 * more restrictive, and predicates in the base are preserved.
 */
function mergeRules(base: CommandRule, override: JsonCommandRule): CommandRule {
  // base is already fully permissive — nothing to add
  if (base === true) return true;
  // override grants blanket access at this level
  if (override === true) return true;

  // base is a predicate or array — wrap into object form to merge
  if (typeof base === "function" || Array.isArray(base)) {
    // Can't cleanly merge a predicate/array with an object override.
    // Convert base array to object, keep predicates as-is.
    if (Array.isArray(base) && !Array.isArray(override)) {
      const obj: { [k: string]: CommandRule } = {};
      for (const k of base) obj[k] = true;
      return mergeRules(obj, override);
    }
    if (Array.isArray(base) && Array.isArray(override)) {
      return [...new Set([...base, ...override])];
    }
    // predicate base + object override — keep predicate (it's more expressive)
    return base;
  }

  // Both are objects (or override is array)
  if (Array.isArray(override)) {
    // Convert override array to object and merge
    const obj: { [k: string]: JsonCommandRule } = {};
    for (const k of override) obj[k] = true;
    return mergeRules(base, obj);
  }

  // Both are objects — recurse
  const merged: { [k: string]: CommandRule } = { ...base };
  for (const [key, val] of Object.entries(override)) {
    if (key in merged) {
      merged[key] = mergeRules(merged[key], val);
    } else {
      merged[key] = val;
    }
  }
  return merged;
}

/** The effective merged command tree, updated after approval. */
let allowedCommands: CommandRule = BASE_COMMANDS;

/**
 * Pending project rules awaiting user approval. Set at session start if the
 * project has a .pi/permissions.json that hasn't been approved yet. Cleared
 * after the user accepts or rejects.
 */
let pendingProjectRules: { raw: string; rules: JsonCommandRule } | null = null;

/** Per-session opt-in: when true, write/edit to tracked files are auto-allowed. */
let sessionAllowEdits = false;

// ---------------------------------------------------------------------------
// Helpers for read-tool path checks
// ---------------------------------------------------------------------------
const PI_DOCS_PREFIX = path.join(
  os.homedir(),
  ".volta/tools/image/packages/@earendil-works/pi-coding-agent/"
);

let trackedFiles: Set<string> | null = null;
let skillsDirs: Set<string> | null = null;

function getTrackedFiles(cwd: string): Set<string> {
  if (trackedFiles) return trackedFiles;
  try {
    const output = execSync("jj file list", { cwd, encoding: "utf-8" });
    trackedFiles = new Set(output.trim().split("\n").map((f) => {
      const resolved = path.resolve(cwd, f);
      try { return fs.realpathSync(resolved); } catch { return resolved; }
    }));
  } catch {
    trackedFiles = new Set();
  }
  return trackedFiles;
}

/**
 * Derive skills directories from the system prompt's <available_skills> block.
 * This reflects the actual loaded skills (including any added by extensions via
 * resources_discover), rather than just what's configured in settings.json.
 */
function getSkillsDirectories(ctx: ExtensionContext): Set<string> {
  if (skillsDirs) return skillsDirs;
  const dirs = new Set<string>();
  const prompt = ctx.getSystemPrompt();
  const locationRe = /<location>(.*?)<\/location>/g;
  let match;
  while ((match = locationRe.exec(prompt)) !== null) {
    const dir = path.dirname(match[1]);
    dirs.add(dir);
    // Also resolve symlinks so that paths through symlinked directories
    // (e.g. ~/.claude -> ~/.dotfiles/ai/claude/) still match after
    // realpathSync is applied to the file being read.
    try { dirs.add(fs.realpathSync(dir)); } catch { /* ignore */ }
  }
  skillsDirs = dirs;
  return skillsDirs;
}

function isInSkillsDirectory(filePath: string, ctx: ExtensionContext): boolean {
  const dirs = getSkillsDirectories(ctx);
  for (const dir of dirs) {
    if (filePath.startsWith(dir + path.sep) || filePath === dir) return true;
  }
  return false;
}

// ---------------------------------------------------------------------------
// Bash command parsing
// ---------------------------------------------------------------------------

/**
 * Strip a leading `cd <cwd> && ` prefix from a command.
 * The agent often wraps commands this way, which is a no-op but obscures the
 * real command from permission checks.
 */
function stripCdToCwd(cmd: string, cwd: string): string {
  const prefix = `cd ${cwd} && `;
  if (cmd.startsWith(prefix)) return cmd.slice(prefix.length);
  return cmd;
}

/** Returns true if the command string contains shell chaining, piping, or injection operators. */
function hasChaining(cmd: string): boolean {
  // Intentionally conservative — if the agent needs any of these it can
  // ask for permission on the full command.
  return /[;|&`\n]/.test(cmd)
    || cmd.includes("$(")
    || cmd.includes("<(") || cmd.includes(">(")
    || cmd.includes("<<");
}

/**
 * Walk the allowedCommands tree to check if a command is allowed.
 * Arrays are shorthand for an object whose keys all map to `true`.
 */
function isCommandAllowed(cmd: string): boolean {
  const tokens = cmd.trimStart().split(/\s+/);
  let rule: CommandRule = allowedCommands;

  for (let i = 0; i < tokens.length; i++) {
    if (rule === true) return true;
    if (typeof rule === "function") return rule(tokens.slice(i).join(" "));
    if (Array.isArray(rule)) return rule.includes(tokens[i]);
    // rule is a subcommand tree — look up the next token
    const next = rule[tokens[i]];
    if (next === undefined) return false;
    rule = next;
  }

  // We consumed all tokens — allow if the final node is `true` or a
  // predicate that accepts empty input.  A tree node means we stopped at
  // a branch (e.g. bare `jj` with no subcommand) — don't auto-allow that.
  if (rule === true) return true;
  if (typeof rule === "function") return rule("");
  return false;
}

// ---------------------------------------------------------------------------
// Main permission check
// ---------------------------------------------------------------------------

function isTrackedFile(filePath: string, ctx: ExtensionContext): boolean {
  let realPath: string;
  try {
    realPath = fs.realpathSync(filePath);
  } catch {
    return false;
  }
  return getTrackedFiles(ctx.cwd).has(realPath);
}

function isAllowed(toolName: string, input: Record<string, unknown>, ctx: ExtensionContext): boolean {
  // Dialog-only, no side effects; prompting to ask a prompt would be absurd.
  if (toolName === "ask_user") return true;

  if (toolName === "read") {
    const filePath = path.resolve(ctx.cwd, String(input.path ?? ""));
    // Resolve symlinks so a tracked symlink pointing outside the repo
    // doesn't auto-allow reading its target.
    let realPath: string;
    try {
      realPath = fs.realpathSync(filePath);
    } catch {
      return false; // Can't resolve — require prompt
    }
    return (
      getTrackedFiles(ctx.cwd).has(realPath) ||
      isInSkillsDirectory(realPath, ctx) ||
      realPath.startsWith(PI_DOCS_PREFIX)
    );
  }

  if (toolName === "write" || toolName === "edit") {
    if (!sessionAllowEdits) return false;
    const filePath = path.resolve(ctx.cwd, String(input.path ?? ""));
    return isTrackedFile(filePath, ctx);
  }

  if (toolName === "bash") {
    const cmd = stripCdToCwd(String(input.command ?? "").trimStart(), ctx.cwd);
    if (hasChaining(cmd)) return false;
    return isCommandAllowed(cmd);
  }

  return false;
}

// ---------------------------------------------------------------------------
// Blocked reporting
// ---------------------------------------------------------------------------

// Tells agent-state.ts that a permission dialog has the session stopped on
// a human. Emitting is always safe: with no subscriber it is a no-op.
function reportBlocked(pi: ExtensionAPI, active: boolean, label?: string): void {
  pi.events.emit("agent-state:blocked", { active, label });
}

// ---------------------------------------------------------------------------
// Extension entry point
// ---------------------------------------------------------------------------

function isContainer(): boolean {
  try { fs.accessSync("/.dockerenv"); return true; } catch {}
  try { fs.accessSync("/run/.containerenv"); return true; } catch {}
  if (process.env.container || process.env.CONTAINER) return true;
  if (process.env.RAMEKIN) return true;
  return false;
}

export default function(pi: ExtensionAPI) {
  if (isContainer()) return;

  pi.on("session_start", async (_event, ctx) => {
    // Reset state each session
    allowedCommands = BASE_COMMANDS;
    pendingProjectRules = null;
    sessionAllowEdits = false;

    const permPath = path.join(ctx.cwd, ".pi/permissions.json");
    let raw: string;
    try {
      raw = fs.readFileSync(permPath, "utf-8");
    } catch {
      return; // No project permissions file
    }

    let projectRules: JsonCommandRule;
    try {
      const parsed = JSON.parse(raw);
      projectRules = parsed.allow;
      if (!projectRules) return;
    } catch {
      if (ctx.hasUI) ctx.ui.notify("Invalid .pi/permissions.json — ignoring", "warning");
      return;
    }

    const hash = hashContent(raw);
    const approvals = loadApprovals();

    if (approvals[ctx.cwd]?.hash === hash) {
      // Already approved this exact version
      allowedCommands = mergeRules(BASE_COMMANDS, projectRules);
    } else {
      // Defer approval prompt to first tool_call (UI isn't ready here)
      pendingProjectRules = { raw, rules: projectRules };
    }
  });

  pi.on("tool_result", async (event) => {
    if (event.toolName === "bash") {
      const cmd = String(event.input.command ?? "").trimStart();
      if (cmd.startsWith("jj ")) {
        trackedFiles = null;
      }
    }
  });

  pi.on("tool_call", async (event, ctx) => {
    if (!ctx.hasUI) return;

    // Prompt for pending project permissions on first tool_call
    if (pendingProjectRules) {
      const pending = pendingProjectRules;
      pendingProjectRules = null; // Clear so we only prompt once

      reportBlocked(pi, true, "project permissions");
      try {
        const summary = summarizeRules(pending.rules);
        const message = "Project wants to auto-allow:\n" + summary.map((s) => `  ${s}`).join("\n");
        const approved = await ctx.ui.confirm("Project permissions", message);

        if (approved) {
          const approvals = loadApprovals();
          approvals[ctx.cwd] = { hash: hashContent(pending.raw) };
          saveApprovals(approvals);
          allowedCommands = mergeRules(BASE_COMMANDS, pending.rules);
        }
      } finally {
        reportBlocked(pi, false);
      }
    }

    if (isAllowed(event.toolName, event.input, ctx)) return;

    const summary = formatArgs(event.toolName, event.input);

    // Every dialog below blocks the session on a human; the finally keeps
    // the paired un-block report happening on early returns too.
    reportBlocked(pi, true, event.toolName);
    try {
      // For write/edit, offer a "allow for session" option
      if (event.toolName === "write" || event.toolName === "edit") {
        const choice = await ctx.ui.select(`${event.toolName}: ${summary}`, [
          "Allow once",
          "Allow for session",
          "Block",
        ]);

        if (choice === "Allow for session") {
          sessionAllowEdits = true;
          ctx.ui.notify("Edits allowed for this session (tracked files only)", "info");
          return;
        }
        if (choice === "Allow once") return;
        return { block: true, reason: "Blocked by user" };
      }

      const allowed = await ctx.ui.confirm(event.toolName, summary);

      if (!allowed) {
        return { block: true, reason: "Blocked by user" };
      }
    } finally {
      reportBlocked(pi, false);
    }
  });
}

// ---------------------------------------------------------------------------
// Formatting
// ---------------------------------------------------------------------------

function formatArgs(toolName: string, input: Record<string, unknown>): string {
  switch (toolName) {
    case "bash":
      return `$ ${input.command}`;
    case "read":
      return `${input.path}` + (input.offset ? ` (offset ${input.offset})` : "");
    case "write":
      return `${input.path} (${String(input.content ?? "").length} chars)`;
    case "edit":
      return `${input.path}`;
    default: {
      const keys = Object.keys(input);
      if (keys.length === 0) return "(no arguments)";
      return keys.map((k) => `${k}: ${truncate(String(input[k]), 80)}`).join("\n");
    }
  }
}

function truncate(s: string, max: number): string {
  if (s.length <= max) return s;
  return s.slice(0, max) + "…";
}