import { existsSync, readFileSync, renameSync, writeFileSync } from "node:fs"; import { homedir } from "node:os"; import { join } from "node:path"; import { codexTrustRoots } from "./codex-trust.ts"; const CONFIG = process.env.PEPPA_CLAUDE_CONFIG ?? join(homedir(), ".claude.json"); /** * Marks a workspace as trusted in ~/.claude.json. Without it Claude Code ignores the * project's .claude/settings.json (permissions, hooks) and .mcp.json servers. */ export function trustWorkspace(dir: string): void { const cfg = existsSync(CONFIG) ? (JSON.parse(readFileSync(CONFIG, "utf8")) as Record) : {}; if (!markClaudeTrusted(cfg, dir)) return; const tmp = `${CONFIG}.peppa-${process.pid}`; writeFileSync(tmp, JSON.stringify(cfg, null, 2)); renameSync(tmp, CONFIG); } /** * PEP-263: Claude Code 2.1.33x looks the trust of a git worktree up by the root of the MAIN repository * (`git rev-parse --git-common-dir`/..), not by the worktree folder — trust written only for the * worktree left agents waiting on «Is this a project you trust?». Marks the folder and, for a * worktree, the main repository's root (the project's workspace — never HOME). True if changed. */ export function markClaudeTrusted(cfg: Record, dir: string, roots = codexTrustRoots(dir)): boolean { cfg.projects ??= {}; let changed = false; for (const root of roots) { const p = (cfg.projects[root] ??= {}); if (p.hasTrustDialogAccepted && p.hasCompletedProjectOnboarding && (p.enabledMcpjsonServers ?? []).includes("peppa")) continue; p.hasTrustDialogAccepted = true; p.hasCompletedProjectOnboarding = true; p.enabledMcpjsonServers = Array.from(new Set([...(p.enabledMcpjsonServers ?? []), "peppa"])); changed = true; } return changed; }