import { statfs } from "node:fs/promises"; import { toolsCache } from "./doctor"; import { REPORTS_ROOT } from "./paths"; import { xJarDecisions } from "./inbox/xJar"; // The machine's own calls for the Inbox: a tool a report build needs that the // last doctor check found absent, and a reports root nearly out of space. Read // from the doctor's CACHE and one statfs -- never a probe (the probe is the // button on /settings/system). And X's login when X work waits on it (umtool // redesign L4, lib/inbox/xJar.ts: the editor's services, memoised, never // waited on long) -- its card names the editor's settings. SERVER ONLY. export type MachineCall = { kind: "machine-tool" | "machine-disk" | "x-jar-stale"; why: string; count: number; /** The card's title and its one action, when not umtool's own (X's login: the editor's settings). */ title?: string; action?: { label: string; href: string }; }; /** Below this a render or a fetch would fail part-way. */ export const LOW_DISK_BYTES = 5e9; export async function freeBytesUnder(dir: string): Promise { try { const s = await statfs(dir); return Number(s.bavail) * Number(s.bsize); } catch { return null; } } export async function machineCalls(): Promise { const out: MachineCall[] = []; const missing = toolsCache()?.tools.filter((t) => t.required && !t.present) ?? []; if (missing.length) { out.push({ kind: "machine-tool", why: `${missing.map((t) => t.id).join(", ")} absent at the last check — a report build cannot run`, count: missing.length, }); } const free = await freeBytesUnder(REPORTS_ROOT); if (free !== null && free < LOW_DISK_BYTES) { out.push({ kind: "machine-disk", why: `${(free / 1e9).toFixed(1)} GB free under the reports root`, count: 1 }); } for (const x of await xJarDecisions().catch(() => [])) { out.push({ kind: "x-jar-stale", why: x.why, count: 1, title: x.projectTitle ?? "X login", action: { label: "Refresh X's cookies", href: x.href } }); } return out; }