PEP-263: доверие Claude для worktree — и на основную копию репозитория #252

Merged
peppa-bot merged 1 commits from task/pep-263 into main 2026-11-24 17:34:19 +00:00
3 changed files with 76 additions and 11 deletions

View File

@ -1,6 +1,7 @@
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");
@ -10,13 +11,28 @@ const CONFIG = process.env.PEPPA_CLAUDE_CONFIG ?? join(homedir(), ".claude.json"
*/
export function trustWorkspace(dir: string): void {
const cfg = existsSync(CONFIG) ? (JSON.parse(readFileSync(CONFIG, "utf8")) as Record<string, any>) : {};
cfg.projects ??= {};
const p = (cfg.projects[dir] ??= {});
if (p.hasTrustDialogAccepted && p.hasCompletedProjectOnboarding) return;
p.hasTrustDialogAccepted = true;
p.hasCompletedProjectOnboarding = true;
p.enabledMcpjsonServers = Array.from(new Set([...(p.enabledMcpjsonServers ?? []), "peppa"]));
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<string, any>, 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;
}

View File

@ -7,6 +7,7 @@ import { join } from "node:path";
import { agentRelayToken } from "@peppa/core";
import { loadEnv, peppaHome } from "@peppa/shared";
import { runOn, shq, type Target } from "./exec.ts";
import { markClaudeTrusted } from "./claude-config.ts";
/** Inside the container: launch dir (settings, rules, opencode.json) and the agent's HOME. */
export const RUN_MOUNT = "/peppa/run";
@ -32,11 +33,8 @@ export function claudeConfigFor(existing: string | null, cwd: string): string {
cfg.hasCompletedOnboarding = true;
// PEP-230: a fresh HOME shows the "Bypass Permissions mode" notice and waits for a key — nobody is there to press it.
cfg.bypassPermissionsModeAccepted = true;
cfg.projects ??= {};
const p = (cfg.projects[cwd] ??= {});
p.hasTrustDialogAccepted = true;
p.hasCompletedProjectOnboarding = true;
p.enabledMcpjsonServers = Array.from(new Set([...(p.enabledMcpjsonServers ?? []), "peppa"]));
// PEP-263: the worktree and its main repository's root (Claude looks a worktree's trust up there).
markClaudeTrusted(cfg, cwd);
return JSON.stringify(cfg, null, 2);
}

View File

@ -0,0 +1,51 @@
// PEP-263: Claude Code 2.1.33x looks a git worktree's trust up by the MAIN repository's root. Trust
// written only for the worktree folder left agents on «Is this a project you trust?» (QLFW-80).
import { strict as assert } from "node:assert";
import { execFileSync } from "node:child_process";
import { mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { describe, test } from "node:test";
import { claudeConfigFor, markClaudeTrusted } from "@peppa/runner";
const git = (cwd: string, ...a: string[]) => execFileSync("git", a, { cwd, stdio: "pipe" });
describe("Доверие Claude к папке worktree (PEP-263)", () => {
test("Доверие ставится и на worktree, и на основную копию репозитория — не на HOME", () => {
const base = realpathSync(mkdtempSync(join(tmpdir(), "claude-trust-")));
try {
const repo = join(base, "workspaces", "proj");
git(base, "init", "-q", repo);
git(repo, "-c", "user.name=t", "-c", "user.email=t@t", "commit", "-q", "--allow-empty", "-m", "init");
const wt = join(base, "worktrees", "proj", "s1");
git(repo, "worktree", "add", "-q", "-b", "task/x", wt);
const cfg = JSON.parse(claudeConfigFor(null, wt)) as { projects: Record<string, { hasTrustDialogAccepted?: boolean; enabledMcpjsonServers?: string[] }> };
assert.deepEqual(Object.keys(cfg.projects).sort(), [repo, wt].sort());
for (const p of Object.values(cfg.projects)) {
assert.equal(p.hasTrustDialogAccepted, true);
assert.deepEqual(p.enabledMcpjsonServers, ["peppa"]);
}
// Без этого ключа Claude 2.1.330 показывает вопрос о доверии (до исправления был только wt).
assert.equal(cfg.projects[repo]?.hasTrustDialogAccepted, true);
// Обычная папка (не worktree) — только она сама.
const plain = JSON.parse(claudeConfigFor(null, repo)) as { projects: Record<string, unknown> };
assert.deepEqual(Object.keys(plain.projects), [repo]);
// Повтор ничего не меняет; уже доверенное сохраняется.
const again: Record<string, any> = cfg;
assert.equal(markClaudeTrusted(again, wt), false);
// trustWorkspace (агент без контейнера) пишет тот же набор в ~/.claude.json.
const file = join(base, "claude.json");
writeFileSync(file, JSON.stringify({ projects: { "/other": { hasTrustDialogAccepted: true } } }));
const out = execFileSync(process.execPath, ["-e", `import("@peppa/runner").then((r) => r.trustWorkspace(${JSON.stringify(wt)}))`], { env: { ...process.env, PEPPA_CLAUDE_CONFIG: file }, stdio: "pipe" });
void out;
const saved = JSON.parse(readFileSync(file, "utf8")) as { projects: Record<string, unknown> };
assert.deepEqual(Object.keys(saved.projects).sort(), ["/other", repo, wt].sort());
} finally {
rmSync(base, { recursive: true, force: true });
}
});
});