// `umtool prune` (2026-10-27): the takes' work files -- what a build keeps to // make the next build faster -- measured, and removed where the policy says // no take will need them (lib/report/takePrune.mjs says what is work, what is // kept and why). A dry run unless --yes. The Takes page's "Free work files" // calls the same writer (pruneProject). // // Housekeeping (2026-11-06) added two passes, which `archilyzer housekeeping` // runs weekly over every project: // --versions whole SUPERSEDED takes deleted (lib/report/takeVersions.mjs: // the newest built take of each version line kept, and // anything picked, pinned, rendering, a base, noted on, or // refused by `umtool takes delete`), in projects idle --idle // (default 3d); // --idle-work D in a project idle D, the newest / picked / current takes' // work files go too (never a running build's or its base's, // never a takes/prune.json pin). // // umtool prune [--take X …] [--anyway] [--keep N] [--older-than 3d] [--yes] [--json] // umtool prune --all [--keep N] [--older-than 3d] [--yes] [--json] // umtool prune | --all --versions [--idle 3d] [--idle-work 3d] [--yes] [--json] // // Plain ESM: the CLI runs it with bare node. import { REPORTS_ROOT, resolveProject } from "../projects/core.mjs"; import { fmtBytes, freeBytesAt, planPrune, planView, policyLine, pruneAllProjects, pruneProject, } from "./takePrune.mjs"; export const PRUNE_USAGE = [ "usage: umtool prune [--take X[,Y]] [--anyway] [--keep N] [--older-than 3d] [--idle-work 3d] [--yes] [--json]", " umtool prune --all [--keep N] [--older-than 3d] [--idle-work 3d] [--yes] [--json]", " umtool prune | --all --versions [--idle 3d] [--grace 7d] [--idle-work 3d] [--yes] [--json]", "", "Each take's WORK files -- clip segments, chrome frames, crossfade batches, encode chunks, raw clip", "copies, work-from-*, the rail's .prerail*.mp4 base -- measured by inode (a file hard-linked", "into two takes is counted once, and freed only when both let it go), and which takes keep theirs", "and why. Kept: a running build and the take it builds from, the picked take, the take the project's", "manifest is, pins in takes/prune.json, the newest N of each group (--keep, default 2), anything", "modified within --older-than (default 3d), and the --from bases of those. The renders, preview.mp4,", "take.json and the manifests are never touched. A dry run unless --yes. --take X: only that take;", "--anyway also frees it when the policy keeps it (never a take being built, or built from).", "", "--idle-work D: in a project nothing has touched for D (every take's build, take.json and preview,", "the project's manifest, takes/verdicts.json), the newest, picked and current takes' work goes too", "-- never a running build's or its base's, never a pin in takes/prune.json.", "", "--versions: whole SUPERSEDED takes deleted, as `umtool takes delete` deletes one. A take's version", "line is its id with every digit run folded (draft-3 -> draft-#, final-1027-v6-blurred ->", "final-#-v#-blurred); a letter is never folded (v6c and v6d, release-6u and release-6 are lines of", "their own). Kept: the newest BUILT take of each line -- the highest version number (draft-10 over", "draft-9; a rebuilt draft-3 does not supersede draft-4), builtAt breaking a tie -- and every older", "take while that newest is younger than --grace (default 7d); any take picked, pinned or with any", "verdict of the operator's, rendering, a render's or build's base, being built, the project's", "manifest's, pinned in takes/prune.json, with operator notes on it, the --from base of a kept take,", "or refused by `umtool takes delete`. A project with anything modified within --idle (default 3d:", "its files, notes, takes, picks) is skipped whole, and so is one whose verdicts.json cannot be read.", "Picks, verdicts and notes are read again before each delete. With --idle-work too, the work pass", "runs after; without it, --versions is the only pass.", ].join("\n"); class CliError extends Error {} function parse(args) { const valued = new Set(["--take", "--keep", "--older-than", "--idle", "--idle-work", "--grace"]); /** @type {Record} */ const flags = {}; const positional = []; for (let i = 0; i < args.length; i += 1) { const a = args[i]; if (a.startsWith("--")) { const [name, inline] = a.includes("=") ? [a.slice(0, a.indexOf("=")), a.slice(a.indexOf("=") + 1)] : [a, null]; if (valued.has(name)) { const v = inline ?? args[++i]; if (v === undefined) throw new CliError(`${name} needs a value`); (flags[name] ??= []).push(v); } else (flags[name] ??= []).push(""); } else positional.push(a); } return { flags, positional, has: (n) => n in flags, one: (n) => flags[n]?.[flags[n].length - 1] }; } const pad = (s, n) => (s.length >= n ? s + " " : s + " ".repeat(n - s.length)); /** One project's plan as lines; `compact` lists only the takes that hold work. */ export function planLines(plan, { compact = false } = {}) { const lines = [`${plan.project.id} — ${policyLine(plan.policy)}`]; const width = Math.min(40, Math.max(12, ...plan.takes.map((t) => t.id.length + 2))); for (const t of plan.takes) { if (compact && t.status === "no-work") continue; const work = t.workBytes === null ? "" : fmtBytes(t.workBytes); if (t.status === "no-work") { lines.push(` ${pad(t.id, width)}no work files${t.pruned ? ` (pruned ${t.pruned.at.slice(0, 10)}, ${fmtBytes(t.pruned.freedBytes)} freed)` : ""}`); continue; } const shared = t.workBytes !== null && t.freedBytes !== null && t.workBytes > t.freedBytes ? `, ${fmtBytes(t.workBytes - t.freedBytes)} of it also linked elsewhere (another take, the project's out/)` : ""; if (t.status === "prunable") { const over = t.overridden.length ? ` — kept by policy (${t.overridden.map((r) => r.why).join("; ")}), freed anyway` : ""; lines.push(` ${pad(t.id, width)}prunable work ${work}, frees ${fmtBytes(t.freedBytes ?? 0)}${shared}${over}`); } else { lines.push(` ${pad(t.id, width)}kept work ${work || "(not measured)"}${t.freedBytes !== null ? `, would free ${fmtBytes(t.freedBytes)}` : ""} — ${t.keep.map((r) => r.why).join("; ")}`); } for (const n of t.notes) lines.push(` ${pad("", width)} note: ${n}`); } const p = plan.totals.prunable; const k = plan.totals.kept; const none = plan.takes.filter((t) => t.status === "no-work").length; lines.push( ` ${p.takes} prunable${p.takes ? `: frees ${fmtBytes(p.freedBytes)}${p.workBytes > p.freedBytes ? ` of ${fmtBytes(p.workBytes)} (the rest is also linked from a kept take or the project's out/)` : ""}` : ""}` + ` · ${k.takes} kept${k.workBytes !== null ? ` with ${fmtBytes(k.workBytes)} of work` : ""}` + `${none ? ` · ${none} with no work files` : ""}` + `${plan.totals.workBytes !== null ? ` · ${fmtBytes(plan.totals.workBytes)} of work in all` : ""}`, ); for (const n of plan.notes) lines.push(` note: ${n}`); return lines; } async function pickProject(arg, root) { const r = await resolveProject(arg, root); if (r.ambiguous) throw new CliError(`"${arg}" is the name of ${r.ambiguous.length} projects — pass its id`); if (!r.project) throw new CliError(`no project matches "${arg}"`); return { id: r.project.id, dir: r.project.dir }; } /** * `umtool prune …` * @param {string[]} args everything after `prune` * @param {{ reportsRoot?: string, log?: (s: string) => void, err?: (s: string) => void, deps?: Record }} [opts] * `deps`: passed to the planner/pruner (a test's `processes`, `alive`, `now`). * @returns {Promise} */ export async function pruneCommand(args, { reportsRoot = REPORTS_ROOT, log = console.log, err = console.error, deps = {} } = {}) { try { const p = parse(args); if (p.has("--help")) { log(PRUNE_USAGE); return 0; } const all = p.has("--all"); if (!all && !p.positional.length) { err(PRUNE_USAGE); return 2; } if (all && p.positional.length) throw new CliError("a project or --all, not both"); if (p.has("--yes") && p.has("--dry-run")) throw new CliError("--yes or --dry-run, not both"); const takes = (p.flags["--take"] ?? []).flatMap((v) => v.split(",")).map((s) => s.trim()).filter(Boolean); if (takes.length && all) throw new CliError("--take names a take of one project: pass the project, not --all"); if (p.has("--anyway") && !takes.length) throw new CliError("--anyway frees a take the policy keeps: name it with --take"); const versions = p.has("--versions"); if (versions && (takes.length || p.has("--anyway"))) throw new CliError("--versions judges every take of a project: no --take or --anyway"); if (p.has("--idle") && !versions) throw new CliError("--idle is --versions' (a project active within it is skipped); the work pass's is --idle-work"); if (p.has("--grace") && !versions) throw new CliError("--grace is --versions' (how old the superseding take must be)"); if (p.has("--idle-work") && takes.length) throw new CliError("--idle-work judges every take of a project: no --take"); const idleWork = p.has("--idle-work") ? p.one("--idle-work") : null; const { parseAge } = await import("./takePrune.mjs"); for (const [flag, v] of [["--idle", p.one("--idle")], ["--idle-work", idleWork], ["--grace", p.one("--grace")]]) { if (v === undefined || v === null) continue; try { parseAge(v); } catch (e) { throw new CliError(`${flag}: ${e instanceof Error ? e.message : e}`); } } const flags = { ...(p.has("--keep") ? { keep: p.one("--keep") } : {}), ...(p.has("--older-than") ? { olderThan: p.one("--older-than") } : {}) }; const apply = p.has("--yes"); const json = p.has("--json"); const common = { reportsRoot, flags, ...(idleWork !== null ? { idleWorkDays: idleWork } : {}), ...deps }; const project = all ? null : await pickProject(p.positional[0], reportsRoot); const freeBefore = await freeBytesAt(reportsRoot); // Pass 1, --versions: whole superseded takes. let vr = null; if (versions) { const { pruneVersionsAll } = await import("./takeVersions.mjs"); vr = await pruneVersionsAll({ reportsRoot, dryRun: !apply, ...(p.has("--idle") ? { idleDays: p.one("--idle") } : {}), ...(p.has("--grace") ? { graceDays: p.one("--grace") } : {}), ...(project ? { only: [project] } : {}), ...deps, }); } // Pass 2, the work files: always without --versions; with it, only when --idle-work asks. /** @type {{ project: { id: string, dir: string }, plan?: any, pruned?: any[], removed?: string[], failed?: any[], freedBytes?: number, error?: string }[] | null} */ let results = null; if (!versions || idleWork !== null) { if (all) { results = (await pruneAllProjects({ ...common, dryRun: !apply, by: "cli", measure: apply ? "prunable" : "all" })).results; } else { const one = /** @type {{ id: string, dir: string }} */ (project); const opts = { ...common, takes: takes.length ? takes : null, anyway: p.has("--anyway"), by: "cli" }; results = [apply ? { project: one, ...(await pruneProject(one, opts)) } : { project: one, plan: await planPrune(one, { ...opts, measure: "all" }) }]; } } const workFreed = (results ?? []).reduce((s, r) => s + (apply ? r.freedBytes ?? 0 : r.plan?.totals.prunable.freedBytes ?? 0), 0); const versionsFreed = vr ? (apply ? vr.totals.freedBytes : vr.totals.bytes) : 0; const freed = workFreed + versionsFreed; // A take a build started in since the plan is kept: an outcome, said, not a failure. const allFailed = (results ?? []).flatMap((r) => (r.failed ?? []).map((f) => ({ project: r.project.id, ...f }))); const failed = allFailed.filter((f) => !f.raced); const raced = [...allFailed.filter((f) => f.raced), ...(vr ? vr.results.flatMap((r) => (r.raced ?? []).map((x) => ({ project: r.project.id, path: `takes/${x.id}/`, error: x.why }))) : [])]; const errors = (results ?? []).filter((r) => r.error); const vFailed = vr ? vr.results.flatMap((r) => [...r.failed.map((f) => ({ project: r.project.id, path: `takes/${f.id}/`, error: f.error })), ...(r.error ? [{ project: r.project.id, path: "", error: r.error }] : [])]) : []; const free = await freeBytesAt(reportsRoot); if (json) { log( JSON.stringify( { dryRun: !apply, ...(vr ? { versions: { idleDays: vr.idleDays, graceDays: vr.graceDays, totals: vr.totals, projects: vr.results.map((r) => (r.error ? { project: r.project, error: r.error } : { ...r.plan, ...(apply ? { deleted: r.deleted, failed: r.failed, raced: r.raced, freedBytes: r.freedBytes } : {}) })), }, } : {}), ...(results ? { projects: results.map((r) => r.error ? { project: r.project, error: r.error } : { ...planView(r.plan), ...(apply ? { pruned: r.pruned, removed: r.removed, failed: r.failed, freedBytes: r.freedBytes } : {}) }, ), } : {}), workFreedBytes: workFreed, versionsFreedBytes: versionsFreed, freedBytes: freed, freeBytesBefore: freeBefore, freeBytes: free, }, null, 2, ), ); } else { if (vr) { const { versionLines } = await import("./takeVersions.mjs"); for (const r of vr.results) { if (r.error) { err(`${r.project.id}: ${r.error}`); continue; } if (all && !r.plan.skipped && r.plan.totals.delete === 0) continue; for (const line of versionLines(r.plan, { compact: all, applied: apply ? r : null })) log(line); log(""); } const t = vr.totals; log( apply ? `superseded takes: ${t.deleted} deleted (${fmtBytes(t.freedBytes)}), ${t.keep} kept${t.raced ? `, ${t.raced} kept since the plan` : ""}, ${t.skipped} project(s) skipped${t.failed ? `, ${t.failed} not deleted` : ""}` : `superseded takes: ${t.delete} to delete (${fmtBytes(t.bytes)}), ${t.keep} kept, ${t.skipped} project(s) skipped`, ); if (results) log(""); } for (const r of results ?? []) { if (r.error) { err(`${r.project.id}: ${r.error}`); continue; } if (all && !r.plan.takes.some((t) => t.status !== "no-work")) continue; for (const line of planLines(r.plan, { compact: all })) log(line); if (apply) for (const pr of r.pruned ?? []) log(` freed ${pr.id}: ${pr.paths.join(", ")}`); log(""); } for (const f of raced) log(`kept: ${f.project}/${f.path} — ${f.error}`); for (const f of [...vFailed, ...failed]) err(`not removed: ${f.project}/${f.path} — ${f.error}`); const freeNote = free !== null ? ` (${fmtBytes(free)} free under ${reportsRoot})` : ""; if (apply) log(`freed ${fmtBytes(freed)}${freeNote}`); else if (freed > 0) log(`dry run — nothing deleted. --yes frees ${fmtBytes(freed)}${freeNote}`); else log(`dry run — nothing to free${freeNote}`); } return failed.length || errors.length || vFailed.length ? 1 : 0; } catch (e) { if (e instanceof CliError) { err(e.message); return 2; } err(e instanceof Error ? e.message : String(e)); return 1; } }