UNPKG

@phren/cli

Version:

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

585 lines (584 loc) • 30.4 kB
import { execFile } from "node:child_process"; import { lstat, mkdir, readdir, readFile, statfs, unlink } from "node:fs/promises"; import { homedir } from "node:os"; import path from "node:path"; import { promisify } from "node:util"; import { tryFileLock } from "../../governance/locks.js"; import { atomicInPrivateDir, bridgeRoot } from "../protocol.js"; import { logger } from "../../logger.js"; import { badgeOf, buildReport, healthOf, viewOf, } from "./report.js"; import { actionId, scanInventory } from "./inventory.js"; import { endProcess, listProcesses } from "./processes.js"; import { cacheRules, paneCloseBlock, plainReason, protectedRoots, removeApprovedPath, removeCacheEntry, removeWorktreeSafely } from "./safety.js"; /** * The Hook's housekeeping pass: every TICK_MS, and at once when free disk * drops below the floor or the computer becomes stressed, run each check, * store the report for RETAIN_DAYS, and serve the phone's view of it. Checks * only observe and propose. The cache cleaner acts on allow-listed entries * daily and on fresh low-disk edges. Every other action requires approval. * Both paths go through safety.ts against fresh state. */ export const TICK_MS = 15 * 60_000; export const RETAIN_DAYS = 14; /** Below this the RC suites fail; the audit's floor for warning and cleaning. */ export const DISK_FLOOR_BYTES = 20 * 1024 ** 3; /** Approve answers within this; later work comes back `started` and lands in `cleaned`. */ export const APPROVE_BUDGET_MS = 45_000; /** The inventory scan (sizes are slow) runs hourly and on request. */ export const SCAN_MS = 60 * 60_000; /** The overview's badge may wait this long; a slower read falls back to the last good one. */ export const BADGE_WAIT_MS = 2_000; /** An inventory older than this is marked stale. */ const STALE_MS = 2 * SCAN_MS; const MAX_REPORTS = 2000; const MAX_FILE_BYTES = 8 * 1024 * 1024; const DAY = 86_400_000; const LOW_DISK_CLEAN_MIN_MS = 60 * 60_000; /** The overview badge, bounded: one read at a time, and a read slower than `waitMs` falls back to the last good badge. */ export function boundedBadge(read, waitMs = BADGE_WAIT_MS) { let last; let pending; let stalled = false; return async () => { if (!pending) { stalled = false; pending = read().then(badge => { last = badge ?? last; return badge; }).catch(() => undefined) .finally(() => { pending = undefined; }); } let timer; const timeout = new Promise(resolve => { timer = setTimeout(() => resolve("timeout"), waitMs); timer.unref(); }); try { const result = await Promise.race([pending, timeout]); if (result !== "timeout") return result; if (!stalled) { stalled = true; logger.warn("stream", `/v1/overview housekeeping badge read stalled past ${waitMs}ms`); } return last; } finally { clearTimeout(timer); } }; } /** Machine-health findings that are housekeeping's business, and their area. */ const HEALTH_AREAS = { swap: "memory", load: "memory", "memory-hog": "memory", "memory-leak": "memory", oom: "memory", disk: "disk", orphan: "sessions", "stale-lock": "sessions", "simulator-clones": "simulators", }; const SUSTAINED = new Set(["swap", "load", "memory-hog", "memory-leak"]); /** Warnings are machine-health findings, with their ids and wording, so the * phone and the conductor's `machine_health` name the same thing the same way. */ export const healthCheck = { name: "health", run: async ({ resources }) => (resources.health ?? []).flatMap(finding => { const area = HEALTH_AREAS[finding.check]; if (!area) return []; // Disk under the RC floor wakes the owner at once; pressure only once it lasts (see `pass`). const notify = finding.check === "disk" && (resources.disk?.freeBytes ?? Infinity) < DISK_FLOOR_BYTES; return [{ id: finding.id, section: "warning", area, kind: "health", severity: finding.severity, title: finding.title, reason: finding.detail, fix: finding.fix, target: { name: finding.check }, fingerprint: finding.severity, ...(notify ? { notify } : {}) }]; }), }; export const DEFAULT_CHECKS = [healthCheck]; async function readJson(file, fallback) { const info = await lstat(file).catch(() => undefined); if (!info?.isFile() || info.size > MAX_FILE_BYTES) return fallback; try { return JSON.parse(await readFile(file, "utf8")); } catch { return fallback; } } /** `<bridge>/housekeeping/`: reports/ (one file per pass), cleaned.json, dismissed.json. */ export class HousekeepingStore { dir; reports; constructor(dir = path.join(bridgeRoot(), "housekeeping")) { this.dir = dir; this.reports = path.join(dir, "reports"); } async names() { const entries = await readdir(this.reports).catch((error) => { if (error.code === "ENOENT") return []; throw error; }); return entries.filter(name => /^\d{8}T\d{6}Z-[a-f0-9]{12}\.json$/.test(name)).sort(); } async save(report, now) { await mkdir(this.reports, { recursive: true, mode: 0o700 }); const stamp = report.generatedAt.replace(/[-:]/g, "").replace(/\.\d+Z$/, "Z"); await atomicInPrivateDir(path.join(this.reports, `${stamp}-${report.id}.json`), report); const names = await this.names(); for (const [index, name] of names.entries()) { const at = Date.parse(name.replace(/^(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})Z.*/, "$1-$2-$3T$4:$5:$6Z")); if (at < now - RETAIN_DAYS * DAY || index < names.length - MAX_REPORTS) await unlink(path.join(this.reports, name)).catch(() => { }); } } async latest() { for (const name of (await this.names()).reverse()) { const report = await readJson(path.join(this.reports, name), undefined); if (report?.version === 1) return report; // a torn or foreign file: try the one before } return undefined; } async cleaned(now) { return (await readJson(path.join(this.dir, "cleaned.json"), [])).filter(entry => Date.parse(entry.at) >= now - RETAIN_DAYS * DAY); } async addCleaned(entry, now) { const kept = [...await this.cleaned(now), entry].slice(-5000); await atomicInPrivateDir(path.join(this.dir, "cleaned.json"), kept); } async inventory() { return readJson(path.join(this.dir, "inventory.json"), undefined); } async setInventory(inventory) { await atomicInPrivateDir(path.join(this.dir, "inventory.json"), inventory); } async dismissed() { return new Map(Object.entries(await readJson(path.join(this.dir, "dismissed.json"), {}))); } async setDismissed(marks, now) { // A mark for a state that has not come back in a month is dropped. const live = [...marks].filter(([, mark]) => Date.parse(mark.at) >= now - 30 * DAY); await atomicInPrivateDir(path.join(this.dir, "dismissed.json"), Object.fromEntries(live)); } } export class Housekeeper { options; current; loaded = false; running; queue = Promise.resolve(); cachedView; lastEdge = { lowDisk: false, stressed: false }; inventory; inventoryLoaded = false; scanning; store; now; constructor(options) { this.options = options; this.store = options.store ?? new HousekeepingStore(); this.now = options.now ?? Date.now; } async latest() { if (!this.loaded) { this.current ??= await this.store.latest(); this.loaded = true; } return this.current; } /** One pass. Concurrent callers share the pass in flight. */ run(trigger = "tick", persist = true) { const report = () => this.running ?? this.runReport(trigger, persist); if (!persist || (trigger !== "tick" && trigger !== "low-disk") || process.env.PHREN_HOUSEKEEPING_AUTOCLEAN === "off") return report(); return this.serial(() => this.cleanCaches(trigger)).catch(error => { logger.warn("housekeeping", "Automatic cache cleanup failed", { error: String(error) }); }).then(report); } runReport(trigger, persist) { const pass = this.pass(trigger, persist).finally(() => { this.running = undefined; }); this.running = pass; return pass; } async pass(trigger, persist) { const startedAt = this.now(); const resources = await this.options.resources(); const context = { resources, now: startedAt, processes: this.options.processes, panes: this.options.panes }; const drafts = [], errors = []; for (const check of this.options.checks ?? DEFAULT_CHECKS) { try { drafts.push(...await check.run(context)); } catch (error) { errors.push({ check: check.name, message: error instanceof Error ? error.message.slice(0, 300) : String(error) }); } } const previous = await this.latest(); // Pressure notifies once it was already there on the previous pass. const before = new Set(previous?.items.map(item => item.id)); for (const draft of drafts) if (draft.id && SUSTAINED.has(draft.target.name) && before.has(draft.id)) draft.notify = true; const report = buildReport({ computer: this.options.computer, trigger, startedAt, finishedAt: this.now(), drafts, previous, health: healthOf(resources), errors }); if (persist) { await this.store.save(report, this.now()); this.current = report; this.loaded = true; this.cachedView = undefined; // Warm the view: the inbox and the overview badge read it synchronously. await this.view(); } return report; } async latestInventory() { if (!this.inventoryLoaded) { this.inventory ??= await this.store.inventory(); this.inventoryLoaded = true; } return this.inventory; } /** Scan the inventory in the background; concurrent callers share the scan. */ scan() { if (this.scanning) return this.scanning; const run = (async () => { const inventory = await scanInventory({ ...this.options.inventory, processes: this.options.processes, panes: this.options.panes, now: this.now() }); await this.store.setInventory(inventory); this.inventory = inventory; this.inventoryLoaded = true; return inventory; })().finally(() => { this.scanning = undefined; this.cachedView = undefined; }); this.scanning = run; this.cachedView = undefined; return run; } /** The phone's view of the newest report; undefined before the first pass. */ async view(includeDismissed = false, includeInventory = false) { const plain = !includeDismissed && !includeInventory; if (plain && this.cachedView) return this.cachedView; const report = await this.latest(); if (!report) return undefined; const scanned = await this.latestInventory(); const inventory = scanned ? { collectedAt: scanned.collectedAt, scanning: Boolean(this.scanning), ids: [...scanned.worktrees, ...scanned.space, ...scanned.sessions].map(row => row.action?.id ?? row.id) } : undefined; const view = viewOf(report, { cleaned: await this.store.cleaned(this.now()), dismissed: await this.store.dismissed(), includeDismissed, nextCheckAt: new Date(Date.parse(report.generatedAt) + TICK_MS).toISOString(), ...(inventory ? { inventory } : {}), ...(includeInventory && scanned ? { include: inventoryOf(scanned, Boolean(this.scanning), this.now() - Date.parse(scanned.collectedAt) > STALE_MS, await this.liveProcesses()) } : {}) }); if (plain) this.cachedView = view; return view; } /** The Processes list, read now; omitted when the process table can't be read. */ async liveProcesses() { if (!this.options.processSources) return undefined; return listProcesses(this.options.processSources).catch(() => undefined); } /** The overview badge, from the cached view: no pass, no git. */ async badge() { const view = await this.view(); return view ? badgeOf(view, this.now()) : undefined; } /** Called with each resource read: a fresh drop below the disk floor or a * fresh stressed state runs a pass now rather than at the next tick. */ observe(resources) { const lowDisk = resources.disk ? resources.disk.freeBytes < DISK_FLOOR_BYTES : this.lastEdge.lowDisk; const stressed = resources.level === "stressed"; const edge = lowDisk && !this.lastEdge.lowDisk ? "low-disk" : stressed && !this.lastEdge.stressed ? "high-load" : undefined; this.lastEdge = { lowDisk, stressed }; return edge ? this.run(edge) : undefined; } /** Warnings worth waking the owner for, as owner-inbox sources keyed by item * id: a reworded finding stays one row, which resolves itself when it goes. */ inboxSources() { const view = this.cachedView; if (!view || !this.current) return []; return view.warnings.filter(item => item.notify).map(item => ({ source: `housekeeping:${item.id}`, kind: "needs-you", title: `Housekeeping on ${this.current.computer}: ${item.title}`.slice(0, 500), })); } serial(work) { const next = this.queue.then(work, work); this.queue = next.catch(() => { }); return next; } /** Hide items until their state changes. Unknown ids are ignored. */ dismiss(ids) { return this.serial(async () => { const report = await this.latest(); const marks = await this.store.dismissed(); const at = new Date(this.now()).toISOString(); for (const item of report?.items ?? []) if (ids.includes(item.id)) marks.set(item.id, { fingerprint: item.fingerprint, at }); await this.store.setDismissed(marks, this.now()); this.cachedView = undefined; return { revision: (await this.view())?.revision ?? "" }; }); } restore(ids) { return this.serial(async () => { const marks = await this.store.dismissed(); for (const id of ids) marks.delete(id); await this.store.setDismissed(marks, this.now()); this.cachedView = undefined; return { revision: (await this.view())?.revision ?? "" }; }); } /** * Apply the owner's approval. A fresh pass runs first and each id is judged * against it: an id that is no longer there (its state changed) is `gone`, * a kept item or a warning is `refused`, and an action re-checks safety * against the disk itself as it runs. Work past APPROVE_BUDGET_MS comes back * `started` and finishes in the background, landing in `cleaned`. */ approve(ids, by) { return this.serial(async () => { const started = this.now(); const fresh = await this.run("approve"); const items = new Map(fresh.items.map(item => [item.id, item])); const results = []; const later = []; let acted = false; for (const id of ids) { const item = items.get(id) ?? await this.inventoryItem(id); if (!item) { results.push({ id, status: "gone" }); continue; } if (item.section === "kept") { results.push({ id, status: "refused", reason: plainReason(item.blockers ?? []) || item.reason }); continue; } if (item.section === "warning") { results.push({ id, status: "refused", reason: "This is a warning; there is nothing to apply." }); continue; } if (!item.action || !EXECUTORS[item.action]) { results.push({ id, status: "refused", reason: "This computer's Hook can't do that yet." }); continue; } if (this.now() - started > APPROVE_BUDGET_MS) { later.push(item); results.push({ id, status: "started" }); continue; } const outcome = await this.apply(item, by); acted ||= outcome.status === "done"; results.push(outcome.status === "done" ? { id, status: "done", freedBytes: outcome.freedBytes } : { id, status: outcome.status, reason: outcome.reason }); } if (later.length) void this.serial(async () => { for (const item of later) await this.apply(item, by); await this.run("approve"); }).catch(() => { }); if (acted) await this.run("approve"); return { results, revision: (await this.view())?.revision ?? "" }; }); } async cleanCaches(trigger) { const inv = this.options.inventory; const rules = inv?.rules ?? cacheRules(inv?.home, inv?.platform); if (!rules.length) return; const state = path.join(this.store.dir, "cache-last-run.json"); await mkdir(this.store.dir, { recursive: true, mode: 0o700 }); const release = tryFileLock(state); if (!release) return; try { const started = this.now(); const last = await readJson(state, null); if (typeof last === "number" && started - last < (trigger === "tick" ? DAY : LOW_DISK_CLEAN_MIN_MS)) return; // Reserve the run before deleting so a restart after partial cleanup honors the same cooldown. await atomicInPrivateDir(state, started); const sources = { processes: this.options.processes, panes: this.options.panes, protected: protectedRoots(inv?.home) }; const counts = []; for (const rule of rules) { const count = { rule: rule.id, removed: 0, kept: 0, failed: 0 }; counts.push(count); const context = { ...sources, rules: [rule] }; try { const root = await lstat(rule.root); if (!root.isDirectory() || root.isSymbolicLink()) continue; const names = await readdir(rule.root); if (!names.length) continue; const before = await this.freeBytes(rule.root); for (const name of names) { try { const { removed } = await removeCacheEntry(path.join(rule.root, name), { ...context, now: this.now() }); if (removed) count.removed++; else count.kept++; } catch { count.failed++; } } if (!count.removed) continue; const after = await this.freeBytes(rule.root); count.freedBytes = Math.max(0, after - before); await this.store.addCleaned({ id: actionId("delete-path", rule.root, started), at: new Date(this.now()).toISOString(), area: rule.id === "xctest-devices" ? "simulators" : "disk", kind: "cache", title: `${rule.id} (${count.removed} removed)`, target: { path: rule.root, name: rule.id }, removedEntries: count.removed, freedBytes: count.freedBytes, by: "auto", }, this.now()); this.cachedView = undefined; } catch (error) { if (error.code !== "ENOENT") count.failed++; } } logger.info("housekeeping", "Automatic cache cleanup finished", { trigger, counts }); } finally { release(); } } async freeBytes(volume = homedir()) { if (this.options.freeBytes) return this.options.freeBytes(volume); const stats = await statfs(volume); return Number(stats.bavail) * Number(stats.bsize); } async apply(item, by) { const before = await this.freeBytes(); let outcome; // The same roots the scan used, so a row is judged at approval by the rules that listed it. const inv = this.options.inventory; const context = { processes: this.options.processes, panes: this.options.panes, rules: inv?.rules ?? cacheRules(inv?.home, inv?.platform), protected: protectedRoots(inv?.home), ...(inv?.tmpRoots ? { tmpRoots: inv.tmpRoots } : {}) }; try { outcome = await EXECUTORS[item.action](item, context, this.fresh()); } catch (error) { outcome = { status: "failed", reason: error instanceof Error ? error.message.slice(0, 300) : String(error) }; } if (outcome.status !== "done") return outcome; // What the volume really gained, not what the item estimated. const freedBytes = Math.max(0, (await this.freeBytes()) - before); await this.store.addCleaned({ id: item.id, at: new Date(this.now()).toISOString(), area: item.area, kind: item.kind, title: item.title, target: item.target, freedBytes, ...(item.sizeBytes !== undefined ? { reportedBytes: item.sizeBytes } : {}), by }, this.now()); // The row is gone from what the phone sees until the next scan confirms it. if (this.inventory) { const keep = (rows) => rows.filter(row => row.action?.id !== item.fingerprint); this.inventory = { ...this.inventory, worktrees: keep(this.inventory.worktrees), space: keep(this.inventory.space), sessions: keep(this.inventory.sessions) }; await this.store.setInventory(this.inventory); } this.cachedView = undefined; return { status: "done", freedBytes }; } /** An inventory row (prefixed id) as an approvable item; it is re-checked live by its executor. */ async inventoryItem(id) { const running = this.options.processSources ? (await this.liveProcesses())?.find(row => row.id === id) : undefined; if (running) return { section: "suggestion", since: new Date(this.now()).toISOString(), id, reason: "", fingerprint: id, area: "memory", kind: "process", title: running.name, target: { name: `${running.name} (pid ${running.pid})` }, action: "end-process", sizeBytes: running.memoryBytes }; const inventory = await this.latestInventory(); if (!inventory) return undefined; const tree = inventory.worktrees.find(row => row.action.id === id); const room = inventory.space.find(row => row.action?.id === id); const pane = inventory.sessions.find(row => row.action.id === id); const found = tree ? { section: "worktrees", row: tree } : room ? { section: "space", row: room } : pane ? { section: "sessions", row: pane } : undefined; if (!found) return undefined; // The fingerprint holds the row's own action id, which is how a handled row leaves the inventory. const base = { section: "suggestion", since: inventory.collectedAt, id, reason: "" }; if (found.section === "worktrees") { const tree = found.row; return { ...base, fingerprint: tree.action.id, area: "worktrees", kind: "worktree", title: path.basename(tree.path), target: { path: tree.path, repo: tree.repo, ...(tree.branch ? { branch: tree.branch } : {}) }, action: "remove-worktree", ...(tree.sizeBytes !== undefined ? { sizeBytes: tree.sizeBytes } : {}) }; } if (found.section === "space") { const room = found.row; if (!room.action) return undefined; return { ...base, fingerprint: room.action.id, area: room.area, kind: room.kind === "simulator" ? "simulator" : room.kind === "docker" ? "docker" : room.kind === "tmp" ? "tmp" : "cache", title: room.title, target: { ...(room.path ? { path: room.path } : {}), ...(room.udid ? { udid: room.udid } : {}), ...(room.kind === "docker" ? { name: "builder" } : {}) }, action: room.action.kind, sizeBytes: room.sizeBytes }; } const session = found.row; return { ...base, fingerprint: session.action.id, area: "sessions", kind: "pane", title: session.pane.label ?? session.title ?? session.pane.target.pane, target: { name: `${session.pane.target.server}/${session.pane.target.pane}` }, action: "close-pane", pane: session.pane }; } /** Live state for executors: fresh sessions and dispatches, never the scan's copy. */ fresh() { const inv = this.options.inventory; return { sessions: async () => (await inv?.sessions?.()) ?? [], dispatches: async () => { if (!inv?.dispatches) throw new Error("dispatches unavailable"); return inv.dispatches(); }, closePane: this.options.closePane, command: this.options.command ?? (async (file, args) => { await promisify(execFile)(file, args, { timeout: 120_000 }); }), xcodebuildRunning: inv?.xcodebuildRunning ?? (async () => false), simulators: inv?.simulators, processSources: this.options.processSources, }; } } async function simulatorAction(item, deps, verb) { const udid = item.target.udid; if (!udid || !/^[A-F0-9-]{36}$/i.test(udid)) return { status: "refused", reason: "The item names no simulator." }; if (await deps.xcodebuildRunning()) return { status: "refused", reason: "An Xcode build or test is running." }; const sim = (await deps.simulators?.() ?? []).find(row => row.udid === udid.toUpperCase()); if (!sim) return { status: "refused", reason: "The simulator is already gone." }; if (verb === "delete" && sim.state !== "Shutdown") return { status: "refused", reason: "It is running; shut it down first." }; if (verb === "shutdown" && sim.state !== "Booted") return { status: "refused", reason: "It isn't running." }; await deps.command("/usr/bin/xcrun", ["simctl", verb, udid]); return { status: "done", freedBytes: 0 }; } /** What each action does once approved; every one goes through safety.ts. */ const EXECUTORS = { "remove-worktree": async (item, sources) => { if (!item.target.path) return { status: "refused", reason: "The item names no worktree." }; const result = await removeWorktreeSafely(item.target.path, sources); return result.removed ? { status: "done", freedBytes: 0 } : { status: "refused", reason: plainReason(result.blockers) }; }, "delete-path": async (item, sources) => { if (!item.target.path) return { status: "refused", reason: "The item names no path." }; const result = await removeApprovedPath(item.target.path, sources); return result.removed ? { status: "done", freedBytes: 0 } : { status: "refused", reason: result.decision.allowed ? "" : result.decision.reason }; }, "close-pane": async (item, _sources, deps) => { const pane = item.pane?.target; if (!pane || !deps.closePane) return { status: "refused", reason: "This computer's Hook can't close panes." }; const live = (await deps.sessions()).find(row => row.pane.target.server === pane.server && row.pane.target.pane === pane.pane); if (!live) return { status: "refused", reason: "The pane is already closed." }; const dispatches = await deps.dispatches().catch(() => undefined); if (!dispatches) return { status: "refused", reason: "Phren couldn't check its dispatches, so it stays open." }; const block = paneCloseBlock({ server: pane.server, pane: pane.pane, status: live.status, role: live.role }, dispatches); if (block) return { status: "refused", reason: block }; await deps.closePane(pane.server, pane.pane); return { status: "done", freedBytes: 0 }; }, "end-process": async (item, _sources, deps) => { if (!deps.processSources) return { status: "refused", reason: "This computer's Hook can't end processes." }; const result = await endProcess(item.id, deps.processSources); // Memory, not disk: nothing on the volume was freed. return result.status === "done" ? { status: "done", freedBytes: 0 } : { status: "refused", reason: result.reason }; }, "shutdown-simulator": async (item, _sources, deps) => simulatorAction(item, deps, "shutdown"), "delete-simulator": async (item, _sources, deps) => simulatorAction(item, deps, "delete"), "prune-docker-builder": async (_item, _sources, deps) => { // Build cache only, older than a week; never images, containers or volumes. await deps.command("docker", ["builder", "prune", "--force", "--filter", "until=168h"]); return { status: "done", freedBytes: 0 }; }, }; /** The inventory as the view carries it: the scan, its state, and nothing internal. */ function inventoryOf(scan, scanning, stale, processes) { return { collectedAt: scan.collectedAt, durationMs: scan.durationMs, ...(scanning ? { scanning: true } : {}), ...(stale ? { stale: true } : {}), worktrees: scan.worktrees, space: scan.space, sessions: scan.sessions, ...(processes ? { processes } : {}), ...(scan.errors?.length ? { errors: scan.errors } : {}) }; }