UNPKG

@phren/cli

Version:

Knowledge layer for AI agents - CLI, MCP server, and data layer

426 lines (425 loc) • 23.2 kB
/** Files-only turn checkpoints. Private, parentless commits never move the user's refs/index. */ import { execFile } from "node:child_process"; import { execProbe } from "./probe-processes.js"; import { createHash, randomBytes, randomUUID } from "node:crypto"; import { lstatSync, mkdirSync, rmdirSync, unlinkSync, symlinkSync, writeFileSync, chmodSync } from "node:fs"; import { lstat, mkdtemp, open, readlink, realpath, rm } from "node:fs/promises"; import path from "node:path"; import { promisify } from "node:util"; import { z } from "zod"; import { checkoutGitEnv } from "./projects.js"; import { BridgeError } from "./protocol.js"; import { readTurn, turnPhase } from "./turn-records.js"; import { ownRecord, liveBackground } from "./session-activity.js"; import { readFinalTurn } from "./schedule-watch.js"; import { childAgentTree, runningChildAgents } from "./transcripts.js"; // A callback can arrive while restore awaits an old disk record. Invalidate synchronously, // before callback placement/status/record I/O; daemon callbacks already name their true session. const activityGenerations = new Map(); let activityClock = 0; const activityKeys = (target) => [`pane:${target.server}:${target.pane}`, `session:${target.source}:${target.session}`]; export function checkpointActivityArrived(target) { const generation = ++activityClock; // Pane identity also covers a new conversation taking over; session identity covers daemon placement. for (const key of activityKeys(target)) activityGenerations.set(key, generation); } const activityGeneration = (target) => Math.max(...activityKeys(target).map(key => activityGenerations.get(key) ?? 0)); const exec = promisify(execFile); // The two hash directories and ref UUID can push a ref's .lock over Windows' // MAX_PATH even in a short checkout. Apply per process, never to user config. const GIT_PATH_OPTIONS = ["-c", "core.longpaths=true"]; const LIMIT = 50; // Background capture has its own safety cap, independent of callback latency. const CAPTURE_BUDGET_MS = 60_000; const secret = randomBytes(32); const hash = (text) => createHash("sha256").update(text).digest("hex"); const rowSchema = z.object({ id: z.string().uuid(), turn: z.number().int().nonnegative(), boundary: z.enum(["prompt", "stop", "recovery"]), at: z.string().datetime(), prompt: z.string().max(240), filesChanged: z.number().int().nonnegative(), promptEntryId: z.string().min(1).max(256).optional() }); function conflict(code, message) { throw new BridgeError(409, message, { code }); } function safePath(file) { if (!file || file.length > 4096 || path.isAbsolute(file) || path.win32.isAbsolute(file) || file.includes("\\") || file.includes("\0") || file.split("/").some(part => !part || part === "." || part === ".." || part.toLowerCase() === ".git")) { conflict("checkpoint-unsafe-path", "The checkpoint contains an unsafe path."); } } async function context(cwd, target, budget = 15_000) { const deadline = performance.now() + budget; const runEnv = async (root, extra, ...args) => { const left = Math.floor(deadline - performance.now()); if (left <= 0) throw new Error("Checkpoint time budget exceeded"); const run = ["rev-parse", "for-each-ref", "ls-files", "diff-tree", "ls-tree", "check-ignore"].includes(args[0]) ? execProbe : exec; return (await run("git", [...GIT_PATH_OPTIONS, "-C", root, "--no-pager", ...args], { env: { ...checkoutGitEnv(), ...extra }, timeout: left, killSignal: "SIGKILL", maxBuffer: 8_388_608 })).stdout; }; const runAt = (root, ...args) => runEnv(root, {}, ...args); let root, dir, common; try { root = await realpath((await runAt(cwd, "rev-parse", "--show-toplevel")).trimEnd()); dir = await realpath((await runAt(root, "rev-parse", "--absolute-git-dir")).trimEnd()); common = await realpath(path.resolve(root, (await runAt(root, "rev-parse", "--git-common-dir")).trimEnd())); } catch (error) { if (String(error.stderr).includes("not a git repository")) conflict("checkpoint-not-isolated", "Checkpoints require a linked Git worktree."); throw error; } if (!(await lstat(path.join(root, ".git"))).isFile() || path.dirname(dir) !== path.join(common, "worktrees")) { conflict("checkpoint-not-isolated", "Checkpoints require a linked Git worktree."); } const check = () => { if (performance.now() >= deadline) throw new Error("Checkpoint time budget exceeded"); }; return { check, root, dir, prefix: `refs/phren/checkpoints/${hash(root)}/${hash(`${target.source}:${target.session}`)}/`, run: (...args) => runAt(root, ...args), input: (index, args, input) => new Promise((resolve, reject) => { const left = Math.floor(deadline - performance.now()); if (left <= 0) { reject(new Error("Checkpoint time budget exceeded")); return; } const child = execFile("git", [...GIT_PATH_OPTIONS, "-C", root, ...args], { env: { ...checkoutGitEnv(), ...(index ? { GIT_INDEX_FILE: index } : {}) }, timeout: left, killSignal: "SIGKILL", maxBuffer: 8_388_608 }, (error, stdout) => error ? reject(error) : resolve(stdout)); child.stdin.on("error", () => { }); child.stdin.end(input); }), bytes: async (oid) => { check(); return (await execProbe("git", [...GIT_PATH_OPTIONS, "-C", root, "cat-file", "blob", oid], { env: checkoutGitEnv(), encoding: "buffer", timeout: Math.max(1, Math.floor(deadline - performance.now())), maxBuffer: 8_388_608 })).stdout; }, private: (index, ...args) => runEnv(root, { GIT_INDEX_FILE: index }, "-c", "core.fsmonitor=false", ...args) }; } async function locked(ctx, action) { const file = path.join(ctx.dir, "phren-checkpoint.lock"); const lock = await open(file, "wx", 0o600).catch(() => conflict("checkpoint-busy", "Another checkpoint operation is running.")); try { return await action(); } finally { await lock.close(); await rm(file, { force: true }); } } async function rows(ctx) { const raw = await ctx.run("for-each-ref", "--format=%(contents:subject)", ctx.prefix); return raw.split("\n").filter(Boolean).map(line => rowSchema.parse(JSON.parse(line))).sort((a, b) => a.at.localeCompare(b.at) || a.turn - b.turn); } async function tree(ctx) { const temp = await mkdtemp(path.join(ctx.dir, "phren-checkpoint-")); try { const index = path.join(temp, "index"); // Read only index membership, never its cached blobs/flags, and hash raw bytes without clean filters or CRLF conversion. const names = [...new Set((await ctx.run("ls-files", "--cached", "--others", "--exclude-standard", "-z")).split("\0").filter(Boolean))]; const regular = [], records = []; for (const file of names) { ctx.check(); safePath(file); if (nonDirectoryParent(ctx.root, file)) continue; // A tracked directory was replaced by a file/symlink. validateParents(ctx.root, file, []); const info = await lstat(path.join(ctx.root, file)).catch(error => { if (error.code === "ENOENT") return undefined; throw error; }); if (!info) continue; if (info.isSymbolicLink()) { const oid = (await ctx.input(undefined, ["hash-object", "-w", "--stdin"], await readlink(path.join(ctx.root, file)))).trim(); records.push(`120000 ${oid}\t${file}\0`); } else if (info.isFile()) regular.push({ file, mode: info.mode & 0o111 ? "100755" : "100644" }); else conflict("checkpoint-unsupported-tree", "Checkpoints do not support submodules or nested Git repositories."); } if (regular.length) { // Git's stdin-paths accepts C-quoted UTF-8 byte strings, including tabs/newlines in filenames. const quoted = regular.map(({ file }) => '"' + [...Buffer.from(file)].map(byte => "\\" + byte.toString(8).padStart(3, "0")).join("") + '"').join("\n") + "\n"; const oids = (await ctx.input(undefined, ["hash-object", "-w", "--no-filters", "--stdin-paths"], quoted)).trim().split("\n"); regular.forEach(({ file, mode }, i) => records.push(`${mode} ${oids[i]}\t${file}\0`)); } await ctx.private(index, "read-tree", "--empty"); await ctx.input(index, ["update-index", "-z", "--index-info"], records.join("")); const result = (await ctx.private(index, "write-tree")).trim(); return result; } finally { await rm(temp, { recursive: true, force: true }); } } async function changes(ctx, from, to) { const tokens = (await ctx.run("diff-tree", "--no-commit-id", "--no-renames", "--name-status", "-r", "-z", from, to)).split("\0"); const files = []; for (let i = 0; i + 1 < tokens.length; i += 2) { const file = tokens[i + 1]; safePath(file); files.push({ path: file, status: tokens[i] === "A" ? "A" : tokens[i] === "D" ? "D" : "M" }); } return files; } async function save(ctx, current, meta, force = false) { const previous = await rows(ctx), last = previous.at(-1); if (!force && last && (await ctx.run("rev-parse", `${ctx.prefix}${last.id}^{tree}`)).trim() === current) return last; const base = last ? `${ctx.prefix}${last.id}` : "HEAD"; const row = { ...meta, id: randomUUID(), filesChanged: (await changes(ctx, base, current)).length }; // Parentless: pruning a ref also makes that snapshot collectable, even when later snapshots survive. const commit = (await ctx.run("-c", "user.name=Phren checkpoint", "-c", "user.email=checkpoint@phren.local", "commit-tree", current, "-m", JSON.stringify(row))).trim(); const prune = previous.slice(0, Math.max(0, previous.length + 1 - LIMIT)); await ctx.input(undefined, ["update-ref", "--stdin"], `start\ncreate ${ctx.prefix}${row.id} ${commit}\n${prune.map(old => `delete ${ctx.prefix}${old.id}\n`).join("")}prepare\ncommit\n`); return row; } // All operations on a worktree share a queue, including different conversations: // the private Git lock and the working files are shared by those conversations. const pending = new Map(); async function checkoutKey(cwd) { let directory; try { directory = await realpath(cwd); } catch { return path.resolve(cwd); } for (let current = directory;; current = path.dirname(current)) { try { await lstat(path.join(current, ".git")); return current; } catch (error) { if (error.code !== "ENOENT") return directory; } if (path.dirname(current) === current) return directory; } } function enqueue(key, work) { const result = (pending.get(key) ?? Promise.resolve()).then(work); const settled = result.then(() => { }, () => { }); pending.set(key, settled); void settled.then(() => { if (pending.get(key) === settled) pending.delete(key); }); return result; } // Resolve paths in admission order, without making a callback do filesystem // I/O. Only admission is shared globally: independent worktrees capture in // parallel. Wrapping the queued promise keeps admission off the capture path. let admission = Promise.resolve(); function schedule(cwd, work) { const admitted = admission.then(async () => { const root = await checkoutKey(cwd); return { result: enqueue(root, () => work(root)) }; }); admission = admitted.then(() => { }, () => { }); return admitted.then(({ result }) => result); } const skipped = new Map(); const prompts = new Map(); const conversationKey = (root, target) => JSON.stringify([root, target.source, target.session]); /** Record immutable boundary facts before returning; Git runs off the callback path. * The snapshot contains the files when the queued capture runs, not historical * bytes from the callback instant. A route joins this queue before reading them. */ export function checkpointBoundary(cwd, target, event, turn, prompt = "", promptEntryId, at = new Date().toISOString()) { if (event !== "UserPromptSubmit" && event !== "Stop") return; const owner = { ...target }; const boundary = { turn, boundary: event === "Stop" ? "stop" : "prompt", at, prompt: prompt.replace(/[\x00-\x1f\x7f]/g, " ").slice(0, 240), ...(event === "UserPromptSubmit" && promptEntryId ? { promptEntryId } : {}) }; void schedule(cwd, async (root) => { const key = conversationKey(root, owner); try { const ctx = await context(root, owner, CAPTURE_BUDGET_MS); if (boundary.boundary === "prompt") { prompts.delete(key); prompts.set(key, { turn: boundary.turn, text: boundary.prompt }); if (prompts.size > 256) prompts.delete(prompts.keys().next().value); } await locked(ctx, async () => { const last = (await rows(ctx)).at(-1); const remembered = prompts.get(key); const excerpt = boundary.boundary === "prompt" ? boundary.prompt : remembered?.turn === boundary.turn ? remembered.text : last?.turn === boundary.turn ? last.prompt : ""; await save(ctx, await tree(ctx), { ...boundary, prompt: excerpt }, Boolean(boundary.promptEntryId)); }); } catch (error) { if (error instanceof BridgeError && error.details?.code === "checkpoint-not-isolated") return; const reason = error instanceof BridgeError ? String(error.details?.code ?? "checkpoint-failed") : /time budget/.test(String(error)) || error.killed ? "checkpoint-timeout" : "checkpoint-failed"; const failures = skipped.get(key) ?? []; failures.push({ ...boundary, reason }); skipped.delete(key); skipped.set(key, failures.slice(-LIMIT)); if (skipped.size > 256) skipped.delete(skipped.keys().next().value); console.warn(`[phren-hook] Checkpoint capture skipped (${reason}); boundary recorded in skippedCaptures.`); } }); } async function idle(target, readPane) { const pane = await readPane(); const record = ownRecord(await readTurn(target.server, target.pane), pane, target.source, target.session); const phase = record && turnPhase(record); const [background, children] = await Promise.all([ liveBackground(record, readFinalTurn), childAgentTree(target.source, target.session).then(runningChildAgents), ]); if (!["idle", "done"].includes(String(pane.agent_status)) || phase?.phase === "working" || background || children.length) { conflict("checkpoint-working", "The agent is working. Wait for it to stop before restoring files."); } } function token(ctx, id, current) { return createHash("sha256").update(secret).update(`${ctx.prefix}\0${id}\0${current}`).digest("hex"); } async function selected(ctx, value) { const id = z.string().uuid().parse(value); const row = (await rows(ctx)).find(row => row.id === id); if (!row) throw new BridgeError(404, "That checkpoint is no longer available.", { code: "checkpoint-not-found" }); return row; } /** Only the pane's own checkout and conversation; child/worktree selectors are deliberately excluded. */ export async function checkpointRoute(cwd, target, action, data, readPane) { try { return await schedule(cwd, root => handleCheckpointRoute(root, target, action, data, readPane)); } catch (error) { if (error instanceof BridgeError || error instanceof z.ZodError) throw error; const failure = error; if (failure.killed || failure.signal || /time budget/.test(String(error))) throw new BridgeError(504, "Checkpoint preparation exceeded its time budget.", { code: "checkpoint-timeout" }); if (failure.code === "ERR_CHILD_PROCESS_STDIO_MAXBUFFER") throw new BridgeError(413, "Checkpoint output exceeded its size limit.", { code: "checkpoint-output-limit" }); throw new BridgeError(503, "Checkpoint preparation failed. Retry when the repository is idle.", { code: "checkpoint-failed" }); } } async function handleCheckpointRoute(cwd, target, action, data, readPane) { const generation = activityGeneration(target); let ctx; try { ctx = await context(cwd, target); } catch (error) { if (action === "list" && error instanceof BridgeError && error.details?.code === "checkpoint-not-isolated") return { state: "not-isolated", checkpoints: [], limit: LIMIT }; throw error; } return locked(ctx, async () => { if (action === "list") return { state: "ready", repository: ctx.root, checkpoints: (await rows(ctx)).reverse(), skippedCaptures: [...(skipped.get(conversationKey(ctx.root, target)) ?? [])], limit: LIMIT }; if (!["preview", "restore"].includes(action)) throw new BridgeError(404, "Unknown checkpoint route."); const row = await selected(ctx, data.checkpointId); const current = await tree(ctx), destination = (await ctx.run("rev-parse", `${ctx.prefix}${row.id}^{tree}`)).trim(); const files = await changes(ctx, current, destination); if (action === "preview") return { state: "ready", checkpointId: row.id, repository: ctx.root, files, token: token(ctx, row.id, current) }; await idle(target, readPane); if (typeof data.token !== "string" || data.token !== token(ctx, row.id, current)) conflict("checkpoint-stale", "Files changed since the preview. Preview the restore again."); const recovery = await save(ctx, current, { boundary: "recovery", turn: row.turn, prompt: "Before restore", at: new Date().toISOString() }, true); // Materialize all destination blobs before changing anything; no filters, hooks or user index. const entries = (await ctx.run("ls-tree", "-r", "-z", destination)).split("\0").filter(Boolean); const blobs = new Map(); let blobBytes = 0; for (const entry of entries) { const tab = entry.indexOf("\t"), file = entry.slice(tab + 1); safePath(file); if (!files.some(change => change.path === file && change.status !== "D")) continue; const [mode, , oid] = entry.slice(0, tab).split(" "); // cat-file's base64-free Buffer path preserves binary file contents. const bytes = await ctx.bytes(oid); blobBytes += bytes.length; if (blobBytes > 64 * 1024 * 1024) throw new BridgeError(413, "Restore exceeds the 64 MiB preparation limit.", { code: "checkpoint-output-limit" }); blobs.set(file, { mode, bytes }); } const ignored = (await ctx.run("ls-files", "--others", "--ignored", "--exclude-standard", "-z")).split("\0").filter(Boolean); for (const change of files) { safePath(change.path); // Check ignores even for paths tracked at the destination, using --no-index. validateParents(ctx.root, change.path, files); const excluded = nonDirectoryParent(ctx.root, change.path) ? "" : await ctx.run("check-ignore", "--no-index", "--", change.path).catch(error => { if (error.code === 1) return ""; throw error; }); if (excluded || ignored.some(file => file === change.path || file.startsWith(change.path + "/") || change.path.startsWith(file + "/"))) { conflict("checkpoint-obstructed", "An ignored file obstructs this restore."); } } // Catch edits during recovery/blob reads and activity arriving during preparation. if (await tree(ctx) !== current) conflict("checkpoint-stale", "Files changed since the preview. Preview the restore again."); await idle(target, readPane); // No await after this comparison: resumed work or an input request wins even if the final read saw an old Stop. if (activityGeneration(target) !== generation) conflict("checkpoint-working", "Agent activity arrived. Wait for the agent to stop before restoring files."); try { applyFiles(ctx.root, files, blobs); } catch { throw new BridgeError(503, "Restore could not finish. Use the recovery checkpoint to recover files.", { code: "checkpoint-restore-failed", recoveryId: recovery.id }); } return { ok: true, checkpointId: row.id, recoveryId: recovery.id, files }; }); } function nonDirectoryParent(root, file) { const parts = file.split("/"); for (let i = 1; i < parts.length; i++) { const info = stat(path.join(root, ...parts.slice(0, i))); if (info && !info.isDirectory()) return true; } return false; } function validateParents(root, file, changes) { const parts = file.split("/"); for (let i = 1; i < parts.length; i++) { const parent = parts.slice(0, i).join("/"); const info = stat(path.join(root, parent)); if (info && !info.isDirectory()) { if (changes.some(change => change.path === parent && change.status === "D")) return; conflict("checkpoint-obstructed", "A file or symlink obstructs this restore."); } } } function stat(file) { try { return lstatSync(file); } catch (error) { if (error.code === "ENOENT" || error.code === "ENOTDIR") return undefined; throw error; } } function applyFiles(root, files, blobs) { // No await between final guard and these writes: local lifecycle callbacks cannot interleave. for (const change of [...files].sort((a, b) => b.path.length - a.path.length)) { // An added descendant of a file/symlink being deleted does not yet exist inside the repository. if (nonDirectoryParent(root, change.path)) continue; const abs = path.join(root, change.path), info = stat(abs); if (info && !info.isDirectory()) unlinkSync(abs); if (info?.isDirectory()) rmdirSync(abs); // Never recursively delete ignored/unrepresented contents. let parent = path.dirname(abs); while (parent !== root) { try { rmdirSync(parent); } catch { break; } parent = path.dirname(parent); } } for (const [file, { mode, bytes }] of blobs) { const abs = path.join(root, file); mkdirSync(path.dirname(abs), { recursive: true }); if (mode === "120000") symlinkSync(bytes.toString("utf8"), abs); else { writeFileSync(abs, bytes); chmodSync(abs, mode === "100755" ? 0o755 : 0o644); } } }