@phren/cli
Version:
Knowledge layer for AI agents - CLI, MCP server, and data layer
299 lines (298 loc) • 18.8 kB
JavaScript
import { createHash } from "node:crypto";
import { lstat, readdir, realpath } from "node:fs/promises";
import { homedir } from "node:os";
import path from "node:path";
import { execProbe as exec } from "../probe-processes.js";
import { cacheRules, deletionDecision, housekeepingGit, inside, inspectCheckout, paneCloseBlock, parseWorktreeList, pathKey, plainReason, protectedRoots, tmpRoots, } from "./safety.js";
export const LABELS = {
"remove-worktree": "Remove worktree", "delete-path": "Delete", "close-pane": "Close pane", "end-job": "End job", "end-process": "End",
"shutdown-simulator": "Shut down simulator", "delete-simulator": "Delete simulator", "prune-docker-builder": "Prune build cache",
};
/** An action id: the row's kind, target and state, so a changed row gets a new id. */
export function actionId(kind, target, state) {
return createHash("sha256").update(JSON.stringify([kind, target, state])).digest("hex").slice(0, 16);
}
function action(kind, target, state, block, label = LABELS[kind], confirm) {
return { id: actionId(kind, target, state), kind, label, allowed: !block, ...(block ? { reason: block } : confirm ? { confirm } : {}) };
}
/** What the owner is told before a destructive approve. */
const CONFIRM = {
"derived-data": "Removes it. Xcode rebuilds it on the next build.",
"xctest-devices": "Removes the test clone. The next UI test run makes a new one.",
"device-support": "Removes it. Xcode copies it again when that device connects.",
cache: "Removes it. It is rebuilt when needed.",
tmp: "Removes the folder and everything in it.",
node_modules: "Removes it. pnpm install brings it back.",
};
export async function duBytes(target) {
if (process.platform === "win32") {
// Windows has no du. Measure logical file sizes without visiting junctions.
const root = await lstat(target).catch(() => undefined);
if (!root)
return undefined;
let bytes = 0;
const pending = [target];
while (pending.length) {
const entry = pending.pop();
const info = await lstat(entry).catch(() => undefined);
if (!info || info.isSymbolicLink() || info.dev !== root.dev)
continue;
if (info.isFile())
bytes += info.size;
if (info.isDirectory()) {
for (const name of await readdir(entry).catch(() => []))
pending.push(path.join(entry, name));
}
}
return bytes;
}
try {
// -x: one file system; nice: a background scan never competes with builds.
const { stdout } = await exec("nice", ["-n", "15", "du", "-sk", "-x", target], { timeout: 120_000, maxBuffer: 1024 * 1024 });
const kb = Number(stdout.trim().split(/\s+/)[0]);
return Number.isFinite(kb) ? kb * 1024 : undefined;
}
catch (error) {
// du exits 1 when some entries were unreadable; its total still counts the rest.
const stdout = error.stdout;
const kb = Number(String(stdout ?? "").trim().split(/\s+/)[0]);
return stdout && Number.isFinite(kb) ? kb * 1024 : undefined;
}
}
async function children(dir) {
return (await readdir(dir).catch(() => [])).filter(name => name !== ".DS_Store").map(name => path.join(dir, name));
}
const isDir = async (p) => (await lstat(p).catch(() => undefined))?.isDirectory() ?? false;
const has = async (p) => Boolean(await lstat(p).catch(() => undefined));
/** Checkouts under the roots (their direct children, plus each repo's linked worktrees and .claude/worktrees). */
export async function findCheckouts(roots, limit = 400) {
const found = new Map();
const add = async (dir) => {
if (found.size >= limit)
return;
const resolved = await realpath(dir).catch(() => dir);
found.set(pathKey(resolved), resolved);
};
for (const root of roots) {
const scanned = path.basename(root) === ".cache" ? (await children(root)).filter(p => path.basename(p).startsWith("fanout-")) : [root];
for (const base of scanned)
for (const dir of await children(base)) {
if (!await isDir(dir) || !await has(path.join(dir, ".git")))
continue;
await add(dir);
for (const inner of await children(path.join(dir, ".claude", "worktrees")))
if (await has(path.join(inner, ".git")))
await add(inner);
if (!await isDir(path.join(dir, ".git")))
continue; // a linked worktree: its main lists the rest
const listed = await housekeepingGit(dir, "worktree", "list", "--porcelain").catch(() => "");
for (const entry of parseWorktreeList(listed))
if (!entry.bare && !entry.prunable && await isDir(entry.path))
await add(entry.path);
}
}
return [...found.values()].sort();
}
export function defaultRoots(home = homedir(), platform = process.platform) {
return [path.join(home, "Projects"), path.join(home, "Sites"), path.join(home, ".cache"), ...tmpRoots(platform)];
}
export async function scanInventory(options) {
const started = Date.now();
const now = options.now ?? started;
const platform = options.platform ?? process.platform;
const home = options.home ?? homedir();
const errors = [];
const sizeOf = options.sizeOf ?? duBytes;
// One process and pane listing for the whole scan.
const processList = options.processes();
const paneList = options.panes();
const [processes] = await Promise.all([
processList.catch(error => { errors.push({ check: "processes", message: String(error) }); return []; }),
paneList.catch(error => { errors.push({ check: "panes", message: String(error) }); }),
]);
// Preserve inspection failures for the safety decisions while still showing inventory.
const sources = { processes: () => processList, panes: () => paneList };
const sessionRows = await (options.sessions?.() ?? Promise.resolve([])).catch(error => { errors.push({ check: "sessions", message: String(error) }); return []; });
const context = { ...sources, rules: options.rules ?? cacheRules(home, platform), tmpRoots: options.tmpRoots ?? tmpRoots(platform), now,
protected: protectedRoots(home) };
// Worktrees.
const worktrees = [];
const checkouts = await findCheckouts(options.roots ?? defaultRoots(home, platform)).catch(error => { errors.push({ check: "worktrees", message: String(error) }); return []; });
for (const dir of checkouts) {
const linked = !await isDir(path.join(dir, ".git"));
const { facts, blockers } = await inspectCheckout(dir, linked ? "worktree" : "repository", sources);
const inPanes = sessionRows.filter(row => row.cwd && inside(row.cwd, dir));
const procs = processes.filter(proc => inside(proc.cwd, dir) && proc.pid !== process.pid).slice(0, 10);
const main = facts?.main ?? !linked;
const state = { facts, blockers: blockers.map(b => b.code) };
const block = main ? "It is the repository's main checkout." : blockers.length ? plainReason(blockers) : undefined;
worktrees.push({
id: actionId("remove-worktree", dir, "row"), repo: facts?.repo ?? dir, ...(facts?.branch ? { branch: facts.branch } : {}), path: dir, main,
...(facts?.lastCommitAt ? { lastCommitAt: facts.lastCommitAt } : {}),
dirty: facts?.dirty ?? 0, untracked: facts?.untracked ?? 0, unpushed: facts?.unpushed ?? 0, stashes: facts?.stashes ?? 0,
...(facts?.inProgress.length ? { inProgress: facts.inProgress[0] } : {}), ...(facts?.locked ? { locked: true } : {}),
sessions: inPanes.map(row => ({ pane: row.pane, ...(row.agent ? { agent: row.agent } : {}), ...(row.title ? { title: row.title } : {}) })),
processes: procs.map(proc => ({ pid: proc.pid, ...(proc.command ? { command: proc.command } : {}) })),
action: action("remove-worktree", dir, state, block, LABELS["remove-worktree"], `Removes the folder.${facts?.branch ? ` The branch ${facts.branch} stays, and its commits are on a remote.` : " Its commits are on a remote."}`),
});
}
// Sizes last: the slow part, and a checkout nested in another is counted in both.
for (const row of worktrees) {
const size = await sizeOf(row.path);
if (size !== undefined)
row.sizeBytes = size;
}
// Space.
const space = [];
const pathRow = async (kind, area, target, title, withAction = true) => {
const info = await lstat(target).catch(() => undefined);
if (!info || info.isSymbolicLink())
return;
const size = await sizeOf(target);
if (size === undefined || size === 0)
return;
const decision = withAction ? await deletionDecision(target, context) : undefined;
const user = decision && !decision.allowed ? decision.blockers?.find(b => b.code === "live-process" || b.code === "bound-pane") : undefined;
space.push({ id: actionId("delete-path", target, "row"), area, kind, title, path: target, sizeBytes: size, lastUsedAt: new Date(info.mtimeMs).toISOString(),
...(user ? { inUse: user.detail.replace(/ runs in .*$/, "") } : {}), ...(!withAction ? { note: "Phren clears this cache by age on its own." } : {}),
...(decision ? { action: action("delete-path", target, [size, info.mtimeMs], decision.allowed ? undefined : decision.reason, kind === "node_modules" ? "Delete node_modules" : kind === "tmp" ? "Delete folder" : "Clear cache", CONFIRM[kind]) } : {}) });
};
const KINDS = { "derived-data": "derived-data", "xctest-devices": "xctest-devices", "ios-device-support": "device-support" };
for (const rule of context.rules) {
try {
if (["npm-cacache", "swiftpm-cache"].includes(rule.id)) {
await pathRow("cache", "disk", rule.root, `${rule.id === "npm-cacache" ? "npm" : "SwiftPM"} cache`, false);
continue;
}
for (const entry of await children(rule.root))
await pathRow(KINDS[rule.id] ?? "cache", rule.id === "xctest-devices" ? "simulators" : "disk", entry, `${path.basename(rule.root)}/${path.basename(entry)}`);
}
catch (error) {
errors.push({ check: `space:${rule.id}`, message: String(error) });
}
}
const checkoutPaths = new Set(checkouts.map(pathKey));
const uid = process.getuid?.();
for (const root of context.tmpRoots) {
for (const entry of await children(root)) {
if (checkoutPaths.has(pathKey(await realpath(entry).catch(() => entry))))
continue; // listed as a worktree
const info = await lstat(entry).catch(() => undefined);
if (!info?.isDirectory() || (uid !== undefined && info.uid !== uid))
continue;
await pathRow("tmp", "disk", entry, entry);
}
}
for (const dir of checkouts) {
const modules = path.join(dir, "node_modules");
if (await isDir(modules))
await pathRow("node_modules", "disk", modules, `${path.basename(dir)}/node_modules`);
}
if (platform === "darwin" && options.simulators) {
try {
const building = await (options.xcodebuildRunning?.() ?? Promise.resolve(false));
for (const sim of await options.simulators()) {
const size = await sizeOf(sim.dataPath);
if (!size)
continue;
const booted = sim.state === "Booted";
const kind = booted ? "shutdown-simulator" : "delete-simulator";
const block = building ? "An Xcode build or test is running." : undefined;
space.push({ id: actionId(kind, sim.udid, "row"), area: "simulators", kind: "simulator", title: `${sim.name} (${sim.state.toLowerCase()})`, udid: sim.udid,
sizeBytes: size, action: action(kind, sim.udid, [sim.state, building], block, LABELS[kind], booted ? "Shuts the simulator down. Its data stays." : "Deletes the simulator and its data.") });
}
}
catch (error) {
errors.push({ check: "space:simulators", message: String(error) });
}
}
if (options.docker) {
try {
for (const row of await options.docker()) {
if (!row.sizeBytes)
continue;
const build = /build cache/i.test(row.type);
const note = /volume/i.test(row.type) ? "Phren never deletes docker volumes: they hold data."
: /image/i.test(row.type) ? "Phren doesn't remove docker images." : "Phren doesn't remove docker containers.";
space.push({ id: actionId("prune-docker-builder", row.type, "row"), area: "docker", kind: "docker", title: `Docker ${row.type.toLowerCase()}`, sizeBytes: row.sizeBytes,
...(build ? { action: action("prune-docker-builder", "builder", [row.sizeBytes], row.reclaimableBytes ? undefined : "Nothing in it can be pruned.", LABELS["prune-docker-builder"], "Prunes build cache older than a week. Images, containers and volumes stay.") } : { note }) });
}
}
catch (error) {
errors.push({ check: "space:docker", message: String(error) });
}
}
space.sort((a, b) => b.sizeBytes - a.sizeBytes);
space.splice(options.spaceLimit ?? 500);
// Sessions.
const dispatches = await (options.dispatches?.() ?? Promise.resolve([])).catch(() => undefined);
const sessions = sessionRows.map(row => {
const block = dispatches ? paneCloseBlock({ server: row.pane.target.server, pane: row.pane.target.pane, status: row.status, role: row.role }, dispatches)
: "Phren couldn't check its dispatches, so it stays open.";
const target = `${row.pane.target.server}/${row.pane.target.pane}`;
return { id: actionId("close-pane", target, "row"), pane: row.pane, ...(row.agent ? { agent: row.agent } : {}), ...(row.title ? { title: row.title } : {}),
...(row.project ? { project: row.project } : {}), ...(row.status ? { status: row.status } : {}), ...(row.idleSeconds !== undefined ? { idleSeconds: row.idleSeconds } : {}),
...(row.cwd ? { cwd: row.cwd } : {}), ...(row.dispatch ? { dispatch: row.dispatch } : {}), ...(row.role ? { role: row.role } : {}),
...receiptOf(row, dispatches),
action: action("close-pane", target, [row.status, block], block, LABELS["close-pane"], "Closes the pane and stops its agent.") };
}).sort((a, b) => (b.idleSeconds ?? 0) - (a.idleSeconds ?? 0));
return { collectedAt: new Date(now).toISOString(), durationMs: Date.now() - started, worktrees, space, sessions, ...(errors.length ? { errors } : {}) };
}
/** The pane's open dispatch, newest first, as the phone shows it. */
function receiptOf(row, dispatches) {
const receipt = [...dispatches ?? []].reverse().find(d => d.id && !d.closedAt && d.server === row.pane.target.server && d.pane === row.pane.target.pane);
return receipt?.id ? { receipt: { id: receipt.id, returnRead: receipt.returned?.read ?? false } } : {};
}
/** Simulators with their data folders (macOS). */
export async function listSimulators(home = homedir()) {
const { stdout } = await exec("/usr/bin/xcrun", ["simctl", "list", "devices", "-j"], { timeout: 20_000, maxBuffer: 8 * 1024 * 1024 });
const devices = JSON.parse(stdout).devices ?? {};
const out = [];
for (const list of Object.values(devices))
for (const device of list) {
if (!device.udid || !/^[A-F0-9-]{36}$/i.test(device.udid))
continue;
const dataPath = path.join(home, "Library/Developer/CoreSimulator/Devices", device.udid);
out.push({ udid: device.udid.toUpperCase(), name: String(device.name ?? "Simulator").slice(0, 120), state: String(device.state ?? "Unknown"), dataPath });
}
return out;
}
const UNITS = { B: 1, KB: 1e3, MB: 1e6, GB: 1e9, TB: 1e12, KIB: 1024, MIB: 1024 ** 2, GIB: 1024 ** 3 };
export function parseDockerSize(text) {
const match = /^([\d.]+)\s*([KMGT]?i?B)/i.exec(text.trim());
return match ? Math.round(Number(match[1]) * (UNITS[match[2].toUpperCase()] ?? 1)) : 0;
}
/** `docker system df`, when docker answers. */
export async function dockerUsage() {
const { stdout } = await exec("docker", ["system", "df", "--format", "{{json .}}"], { timeout: 20_000 }).catch(() => ({ stdout: "" }));
return stdout.split("\n").filter(Boolean).map(line => {
const row = JSON.parse(line);
return { type: String(row.Type ?? "Unknown"), sizeBytes: parseDockerSize(String(row.Size ?? "0B")), reclaimableBytes: parseDockerSize(String(row.Reclaimable ?? "0B")) };
});
}
export async function xcodebuildRunning() {
return exec("pgrep", ["-x", "xcodebuild"], { timeout: 5_000 }).then(() => true, () => false);
}
/** Agent panes from one server's workspace overview (the Hook's `/v1/workspaces` shape). */
export function sessionRowsFromOverview(overview, now = Date.now()) {
const rows = [];
const list = (value) => Array.isArray(value) ? value.filter((v) => !!v && typeof v === "object") : [];
const text = (value) => typeof value === "string" && value ? value : undefined;
for (const group of list(overview.groups))
for (const tab of list(group.children)) {
const target = tab.target;
if (typeof tab.agent !== "string" || !target || typeof target.server !== "string" || typeof target.pane !== "string")
continue;
const changedAt = typeof tab.lastChangedAt === "string" ? Date.parse(tab.lastChangedAt) : NaN;
const label = text(tab.label) && !/^\d+$/.test(String(tab.label)) ? text(tab.label) : text(group.label);
rows.push({
pane: { target: { server: target.server, ...(target.workspace ? { workspace: target.workspace } : {}), ...(target.tab ? { tab: target.tab } : {}), pane: target.pane },
...(label ? { label } : {}) },
agent: tab.agent, ...(text(tab.title) ? { title: text(tab.title) } : {}), ...(text(tab.agentStatus) ? { status: text(tab.agentStatus) } : {}),
...(text(tab.role) ? { role: text(tab.role) } : {}), ...(text(tab.cwd) ? { cwd: text(tab.cwd) } : {}),
...(Number.isFinite(changedAt) ? { idleSeconds: Math.max(0, Math.floor((now - changedAt) / 1000)) } : {}),
...(text(tab.dispatch) ? { dispatch: text(tab.dispatch) } : {}),
});
}
return rows;
}