/** * On-disk job registry. * * Jobs are OS processes, so their state must survive an extension reload or a * new session. A small JSON registry under the agent dir records every job; a * later session loads it, checks each running pid for liveness, and either * reattaches (detached) or reaps (default) the leftover process. Log files sit * beside the registry and are never rewritten. * * Each session is the sole writer of its own `registry-.json`, so a * concurrent session can never clobber a peer's read-modify-write. Loading * merges every session file, and a session adopts a dead peer's records (so a * detached server survives, and a crashed session's history is not lost). The * legacy shared `registry.json` is migrated on first load. * * The pure `planReconcile`/`mergeRecords`/`settleGone` helpers take liveness and * exit-status reads as callbacks, so their rules are unit-tested without * touching real processes. */ import { createHash } from "node:crypto"; import { existsSync, readdirSync, readFileSync, renameSync, unlinkSync, writeFileSync } from "node:fs"; import { join, resolve } from "node:path"; import type { ExtensionAPI } from "@earendil-works/pi-coding-agent"; import { getAgentDir } from "@earendil-works/pi-coding-agent"; import { resolveEffectiveCwd } from "../../lib/env.ts"; import type { RosterState } from "../../lib/session-roster.ts"; import { FILE_MODE, ensurePrivateDir } from "../../lib/json.ts"; import { registryPathFor } from "./paths.ts"; import { JOB_STATUSES, type JobRecord, type JobStatus, type RegistryFile } from "./types.ts"; export const REGISTRY_VERSION = 1; /** Stable per-directory key so each project gets its own registry. */ export function projectKey(cwd: string): string { const hash = createHash("sha1").update(resolve(cwd)).digest("hex").slice(0, 12); const base = resolve(cwd).split(/[\\/]/).filter(Boolean).pop() ?? "root"; return `${base.replace(/[^\w.-]+/g, "_")}-${hash}`; } /** The store's name inside a repository's directory: `/repos//jobs`. */ export const JOBS_DIR_NAME = "jobs"; /** * The flat store an older build wrote: `/jobs/-`. * * It is the mover's `legacy` argument now — the spelling a store is taken across from once — and * not a destination: every session files under `repos/`, so nothing writes this path. */ export function registryDirFor(cwd: string, override?: string): string { if (override) return resolve(override); return join(getAgentDir(), "jobs", projectKey(cwd)); } /** * Where this session's registry and logs live: `repos//jobs`, the sibling of the board (#771). * * One repository's state is one directory, so this is the last store to join it. With the host, the * shared mover asks git which repository the directory is and takes the flat store across once; * without one — a runtime built in a suite — the same layout is computed from the path alone, so a * hostless session still files under `repos/local-/jobs` and never at the flat spelling the * storage table no longer reserves. Both halves load in the call: `repo-key.ts` and `store-paths.ts` * each bring the git cluster with them, and this entry's registration graph may not grow. */ export async function jobsDir(cwd: string, override?: string, pi?: Pick): Promise { if (override !== undefined) return resolve(override); const agentDir = getAgentDir(); const base = resolveEffectiveCwd(cwd); if (pi === undefined) { const { repoKeyOf, repoStoreDir, storeIdOf } = await import("../../lib/repo-key.ts"); return repoStoreDir(agentDir, storeIdOf({}, repoKeyOf(base)), JOBS_DIR_NAME); } const { repoStoreDirFor } = await import("../../lib/store-paths.ts"); return (await repoStoreDirFor(pi, base, agentDir, JOBS_DIR_NAME, () => registryDirFor(cwd))).dir; } function isJobRecord(value: unknown): value is JobRecord { if (!value || typeof value !== "object") return false; const job = value as Partial; return ( typeof job.id === "string" && typeof job.command === "string" && typeof job.cwd === "string" && typeof job.startedAt === "number" && typeof job.logPath === "string" && typeof job.status === "string" && (JOB_STATUSES as readonly string[]).includes(job.status) && (job.pid === null || typeof job.pid === "number") && (job.exitCode === null || typeof job.exitCode === "number") && (job.signal === null || typeof job.signal === "string") && (job.finishedAt === null || typeof job.finishedAt === "number") && (job.statusPath === undefined || job.statusPath === null || typeof job.statusPath === "string") && (job.startToken === undefined || job.startToken === null || typeof job.startToken === "string") && (job.droppedBytes === undefined || typeof job.droppedBytes === "number") ); } /** Normalize field presence so downstream code only ever sees `null`, never `undefined`. */ function normalizeRecord(record: JobRecord): JobRecord { return { ...record, statusPath: record.statusPath ?? null, startToken: record.startToken ?? null, // A registry written before stdin support has no such flag; such a job was // started without a pipe, and its session is long gone anyway. stdin: record.stdin === true, // A record written before the drop counter was kept fell back on its handle, // which a settled job no longer has; nothing was recorded, so it reports none. droppedBytes: record.droppedBytes ?? 0, }; } /** Parse a registry file, skipping malformed entries rather than trusting them. */ export function parseRegistry(raw: unknown, fallbackSessionId = "unknown"): RegistryFile { if (!raw || typeof raw !== "object") { return { version: REGISTRY_VERSION, sessionId: fallbackSessionId, counter: 1, jobs: [] }; } const file = raw as Partial; const jobs = Array.isArray(file.jobs) ? file.jobs.filter(isJobRecord).map(normalizeRecord) : []; const counter = typeof file.counter === "number" && file.counter > 0 ? Math.floor(file.counter) : 1; const sessionId = typeof file.sessionId === "string" && file.sessionId ? file.sessionId : fallbackSessionId; return { version: REGISTRY_VERSION, sessionId, counter, jobs }; } export interface LoadedRegistries { /** Every well-formed registry file found in the directory. */ files: RegistryFile[]; /** Highest counter seen across files, at least 1. */ counter: number; } /** * Load every session's registry file in `dir`, newest id counter included. * * A leftover legacy `registry.json` (the pre-per-session format) is read as a * file owned by the sentinel `"legacy"` session. The caller adopts its records * and then calls {@link removeLegacyRegistry}. */ export function loadRegistries(dir: string): LoadedRegistries { const files: RegistryFile[] = []; let counter = 1; const consider = (file: RegistryFile): void => { files.push(file); counter = Math.max(counter, file.counter); }; try { for (const entry of readdirSync(dir)) { if (!entry.startsWith("registry-") || !entry.endsWith(".json")) continue; try { consider(parseRegistry(JSON.parse(readFileSync(join(dir, entry), "utf-8")))); } catch { // Skip a malformed registry file. } } } catch { // A missing directory has no registries. } const legacy = join(dir, "registry.json"); if (existsSync(legacy)) { try { consider(parseRegistry(JSON.parse(readFileSync(legacy, "utf-8")), "legacy")); } catch { // Leave an unreadable legacy file in place rather than lose it. } } return { files, counter }; } /** Delete the legacy shared registry once its records have been adopted. */ export function removeLegacyRegistry(dir: string): void { try { const path = join(dir, "registry.json"); if (existsSync(path)) unlinkSync(path); } catch { // Best-effort. } } /** Persist one session's registry atomically. Failures are non-fatal for the caller. */ export function saveRegistry(dir: string, file: RegistryFile): void { ensurePrivateDir(dir); const path = registryPathFor(dir, file.sessionId); const tmp = `${path}.tmp`; writeFileSync(tmp, JSON.stringify(file, null, 2), { encoding: "utf-8", mode: FILE_MODE }); renameSync(tmp, path); } /** Remove a session's registry file; missing files and failures are ignored. */ export function removeRegistry(dir: string, sessionId: string): void { try { const path = registryPathFor(dir, sessionId); if (existsSync(path)) unlinkSync(path); } catch { // Best-effort. } } /** * Combine two records for the same id from different files. * * A settled record beats a running one; a record owned by `preferSessionId` * (the reading session) beats a peer's; otherwise the more recently finished * (or, failing that, started) record wins. Exposed for tests. */ export function mergeRecords(a: JobRecord, b: JobRecord, preferSessionId?: string): JobRecord { const aRunning = a.status === "running"; const bRunning = b.status === "running"; if (aRunning !== bRunning) return aRunning ? b : a; if (preferSessionId) { if (a.sessionId === preferSessionId && b.sessionId !== preferSessionId) return a; if (b.sessionId === preferSessionId && a.sessionId !== preferSessionId) return b; } return (b.finishedAt ?? b.startedAt) > (a.finishedAt ?? a.startedAt) ? b : a; } export interface SettleGoneOptions { /** Whether a pid is still alive. */ isAlive: (pid: number) => boolean; /** Whether the record's start token still matches the live pid. */ tokenMatches: (record: JobRecord) => boolean; /** Read a recovered exit code for a gone process, or undefined. */ readExitCode?: (record: JobRecord) => number | undefined; /** Timestamp to record as `finishedAt`. */ now: number; } /** * Settle a record whose process may already be gone. * * Returns the record unchanged while the pid is live; marks it `unknown` when * the start token no longer matches or no exit code can be recovered; otherwise * records `exited`/`failed` with the recovered code. */ export function settleGone(record: JobRecord, options: SettleGoneOptions): JobRecord { if (record.status !== "running" || record.pid === null) return record; if (!options.tokenMatches(record)) return { ...record, status: "unknown", finishedAt: options.now }; if (options.isAlive(record.pid)) return record; const code = options.readExitCode?.(record); if (code === undefined) return { ...record, status: "unknown", finishedAt: options.now }; const status: JobStatus = code === 0 ? "exited" : "failed"; return { ...record, status, exitCode: code, finishedAt: options.now }; } /** What a load makes of every session's file, before the runtime touches a record. */ export interface LoadedRecords { /** Every record, merged across the session files and reconciled against liveness. */ records: JobRecord[]; /** Live, non-detached leftovers for the caller to kill. */ orphans: JobRecord[]; /** Sessions whose file may be dropped once their records are rewritten here. */ deadSessions: Set; } /** The liveness and ownership seams a load reconciles against. */ export interface LoadSeams { /** This session, which adopts a dead one's records. */ sessionId: string; /** What the session that wrote a file is now. */ ownerState: (sessionId: string) => RosterState; /** The process seams: whether a pid runs, whether its token still matches, its exit code. */ process: { alive: (pid: number) => boolean; owns: (record: JobRecord) => boolean; exitCode: (record: JobRecord) => number | undefined; }; now: number; } /** Merge every session's file, recover dead pids, and classify the survivors. */ export function loadRecords(files: readonly RegistryFile[], seams: LoadSeams): LoadedRecords { const merged = new Map(); const deadSessions = new Set(); for (const file of files) { if (file.sessionId !== seams.sessionId && seams.ownerState(file.sessionId) === "gone") deadSessions.add(file.sessionId); for (const record of file.jobs) { const existing = merged.get(record.id); merged.set(record.id, existing ? mergeRecords(existing, record, seams.sessionId) : record); } } // Resolve dead pids (recovering exit codes), then classify the survivors. const settled = [...merged.values()].map((record) => settleGone(record, { isAlive: seams.process.alive, tokenMatches: seams.process.owns, readExitCode: seams.process.exitCode, now: seams.now, }), ); const { jobs, orphans } = planReconcile(settled, seams.process.alive, seams.now, { ownerState: seams.ownerState, currentSessionId: seams.sessionId, }); return { records: jobs, orphans, deadSessions }; } export interface ReconcileResult { /** The records after liveness reconciliation. */ jobs: JobRecord[]; /** Live, non-detached leftovers that the caller should kill. */ orphans: JobRecord[]; } /** Owner-liveness context for {@link planReconcile}. */ export interface ReconcileOptions { /** * What the session that started a job is: `live`, `quiet`, or `gone`. * * Only `gone` frees the job to be reaped. A `quiet` owner is an idle one — its * heartbeat stops while it has nothing running — and its job is still its own. */ ownerState?: (sessionId: string) => RosterState; /** The reconciling session; its own leftovers are always reaped. */ currentSessionId?: string; } /** * Reconcile loaded records against current process liveness. * * Expects running records to have been through {@link settleGone} already, so a * dead pid here is a fallback. A live non-detached record is an orphan unless a * different, still-live session owns it. */ export function planReconcile( records: JobRecord[], isAlive: (pid: number) => boolean, now: number, options: ReconcileOptions = {}, ): ReconcileResult { const jobs: JobRecord[] = []; const orphans: JobRecord[] = []; for (const record of records) { if (record.status !== "running" || record.pid === null) { jobs.push(record); continue; } if (!isAlive(record.pid)) { jobs.push({ ...record, status: "unknown", finishedAt: now }); continue; } if (record.detached) { jobs.push(record); continue; } // Another session still owns this job: keep it running instead of reaping a // peer's work. Its owner is only `gone` when its process is. if ( options.ownerState && record.sessionId !== options.currentSessionId && options.ownerState(record.sessionId) !== "gone" ) { jobs.push(record); continue; } orphans.push(record); jobs.push(record); } return { jobs, orphans }; }