peppa/packages/runner/src/run-settings.ts

93 lines
4.2 KiB
TypeScript

import { mkdirSync, writeFileSync } from "node:fs";
import { peppaHome, peppaRoot } from "@peppa/shared";
import { dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
const hookBin = () => `${peppaRoot(resolve(dirname(fileURLToPath(import.meta.url)), "../../.."))}/packages/runner/bin/peppa-hook`;
/** Blocking hooks wait for the PM this long before falling back to the terminal. */
export const HOOK_WAIT_SEC = Number(process.env.PEPPA_HOOK_WAIT_SEC ?? 540);
/** Stop-hook "keep going" nudges in a row before the PM is called. */
export const MAX_AUTO_NUDGES = 2;
export const RUNS_DIR = join(peppaHome(), "runs");
/**
* Claude settings for one session run, passed with --settings. Hooks live here rather
* than in each project repo, so they are versioned with Peppa and only fire for
* sessions Peppa launched.
*/
export function runDir(sessionId: number, generation: number): string {
const dir = join(RUNS_DIR, `s${sessionId}-g${generation}`);
mkdirSync(dir, { recursive: true });
return dir;
}
/** Writes the shared Peppa rules for --append-system-prompt-file. */
export function writeRunRules(sessionId: number, generation: number, text: string): string {
const path = join(runDir(sessionId, generation), "rules.md");
writeFileSync(path, text);
return path;
}
/**
* PEP-271: the tools an agent may use without asking — the only list. Claude merges allow rules of
* every settings source, so a repo's .claude/settings.json could widen it; in the container the
* managed settings below (`allowManagedPermissionRulesOnly`) make this list the only one.
*/
export const AGENT_ALLOW: readonly string[] = [
"mcp__peppa",
"Bash(openspec:*)",
"Bash(git status:*)",
"Bash(git diff:*)",
"Bash(git log:*)",
"Bash(git add:*)",
"Bash(git commit:*)",
"Bash(git checkout -b:*)",
"Bash(git switch:*)",
];
/** PEP-271: where Claude Code reads managed (policy) settings on Linux — the runner image. */
export const MANAGED_SETTINGS_PATH = "/etc/claude-code/managed-settings.json";
export const RUN_POLICY_FILE = "managed-settings.json";
export function runPolicy(): Record<string, unknown> {
return { allowManagedPermissionRulesOnly: true, permissions: { allow: [...AGENT_ALLOW] } };
}
/** PEP-271: managed settings of the run; the container mounts the file read-only at MANAGED_SETTINGS_PATH. */
export function writeRunPolicy(sessionId: number, generation: number): string {
const path = join(runDir(sessionId, generation), RUN_POLICY_FILE);
writeFileSync(path, JSON.stringify(runPolicy(), null, 2));
return path;
}
/** `bin` — the hook program as the agent sees it (PEP-17: `peppa-hook` from the runner image). */
export function writeRunSettings(sessionId: number, generation: number, bin: string = hookBin()): string {
const hook = (event: string, timeout = 30) => ({ type: "command", command: `${bin} ${event}`, timeout });
// PEP-33: steps of the agent; async — Claude doesn't wait for the hook process.
const step = { ...hook("tool-use", 15), async: true };
const settings = {
// PEP-5: Claude reports cost, context and the 5h/7d limit windows to the status line command.
statusLine: { type: "command", command: `${bin} statusline`, padding: 0 },
// PEP-3/271: Peppa's own MCP tools never need the PM (status guards live in the server).
// In the container the same list comes from the managed settings and this one is ignored.
permissions: { allow: [...AGENT_ALLOW] },
hooks: {
SessionStart: [{ hooks: [hook("session-start")] }],
UserPromptSubmit: [{ hooks: [hook("prompt")] }],
PermissionRequest: [{ hooks: [hook("permission-request", HOOK_WAIT_SEC + 30)] }],
PreToolUse: [{ matcher: "AskUserQuestion", hooks: [hook("ask-question", HOOK_WAIT_SEC + 30)] }],
PostToolUse: [{ hooks: [step] }],
PostToolUseFailure: [{ hooks: [step] }],
Notification: [{ hooks: [hook("notification")] }],
Stop: [{ hooks: [hook("stop")] }],
StopFailure: [{ hooks: [hook("stop-failure")] }],
SessionEnd: [{ hooks: [hook("session-end")] }],
},
};
const path = join(runDir(sessionId, generation), "settings.json");
writeFileSync(path, JSON.stringify(settings, null, 2));
return path;
}