commit 5e10071f72436987ba11296acf3d4fa61ad6fbe1
parent 553bb861f255b9340514da63bdf72650d85a2e94
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date: Tue, 27 Oct 2026 17:58:20 -0400
umtool prune: the takes' work files from a terminal -- a dry run unless --yes
umtool prune <project> [--take X] [--anyway] | --all, with --keep N and
--older-than 3d: per take its work and what freeing it gives back, or why
it is kept; the totals counted over the union (a du of them together).
--yes frees through the same writer the Takes page calls (pruneProject);
--take X --anyway frees a take the policy keeps, never one being built or
built from. --json for an agent.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Diffstat:
3 files changed, 230 insertions(+), 0 deletions(-)
diff --git a/umtool/bin/umtool.mjs b/umtool/bin/umtool.mjs
@@ -65,6 +65,8 @@
// the editor as one fetch-windows batch (release 35)
// umtool prep <project> [--clips a,b] [--wait | --status] [--json] the bench's excerpts and waveforms,
// made before the walk (one clip at a time, in the heavy slot)
+// umtool prune [<project> [--take X] [--anyway] | --all] [--keep N] [--older-than 3d] [--yes] [--json]
+// takes' work files (segments, chrome frames, batches) measured by inode, freed by policy
// umtool fallback <project> [--dry-run | --clip <id> … | --restore <clipId>] stand-ins for refused windows
// umtool phrases "<phrase>" ["<phrase>" …] --channels a,b [--prefer c] [--exclude RE] [--words|--regex]
// [--samples N] [--no-cache] [--json|--full] how often each was said, by year (one pass)
@@ -787,6 +789,10 @@ function usage() {
" what the clip bench makes on a clip's first open (a long recording's excerpt, the waveform),",
" made before the walk: queued and run in the background, one clip at a time, in the heavy slot",
" (a small declared estimate), under nice/ionice; --wait runs it here; a write or a fetch queues it too",
+ " umtool prune [<project> [--take X] [--anyway] | --all] [--keep N] [--older-than 3d] [--yes] [--json]",
+ " takes' work files (segments, chrome frames, crossfade batches, encode chunks): what each holds",
+ " (hard links counted by inode), which takes keep theirs and why, what freeing the rest gives back;",
+ " a dry run unless --yes. A take build prunes its project after; a render prunes when disk is low",
" umtool fallback <project> [--dry-run | --clip <id> … | --restore <clipId>] [--json]",
" stand-ins for refused montage windows: the instance's public alternate (YouTube first), on the clip",
" umtool phrases \"<phrase>\" [\"<phrase>\" …] --channels a,b [--prefer c] [--exclude RE] [--words|--regex] [--json]",
@@ -869,6 +875,8 @@ const COMMANDS = {
fetch: async () => process.exit(await settled(await (await import("../lib/report/fetchCli.mjs")).fetchCommand(argv.slice(argv.indexOf("fetch") + 1)))),
// The clip bench's first-open work, ahead of the walk: lib/report/prepCli.mjs.
prep: async () => process.exit(await (await import("../lib/report/prepCli.mjs")).prepCommand(argv.slice(argv.indexOf("prep") + 1))),
+ // Takes' work files, by policy (2026-10-27): lib/report/pruneCli.mjs.
+ prune: async () => process.exit(await (await import("../lib/report/pruneCli.mjs")).pruneCommand(argv.slice(argv.indexOf("prune") + 1))),
fallback: async () => process.exit(await (await import("../lib/report/standInsCli.mjs")).fallbackCommand(argv.slice(argv.indexOf("fallback") + 1))),
// The phrase finder (release 34): lib/phrase-finder/cli.mjs.
phrases: async () => process.exit(await (await import("../lib/phrase-finder/cli.mjs")).phrasesCommand(argv.slice(argv.indexOf("phrases") + 1))),
diff --git a/umtool/lib/report/pruneCli.mjs b/umtool/lib/report/pruneCli.mjs
@@ -0,0 +1,185 @@
+// `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).
+//
+// umtool prune <project> [--take X …] [--anyway] [--keep N] [--older-than 3d] [--yes] [--json]
+// umtool prune --all [--keep N] [--older-than 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 <project> [--take X[,Y]] [--anyway] [--keep N] [--older-than 3d] [--yes] [--json]",
+ " umtool prune --all [--keep N] [--older-than 3d] [--yes] [--json]",
+ "",
+ "Each take's WORK files -- clip segments, chrome frames, crossfade batches, encode chunks, raw clip",
+ "copies, work-from-* -- 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).",
+].join("\n");
+
+class CliError extends Error {}
+
+function parse(args) {
+ const valued = new Set(["--take", "--keep", "--older-than"]);
+ /** @type {Record<string, string[]>} */
+ 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<string, any> }} [opts]
+ * `deps`: passed to the planner/pruner (a test's `processes`, `alive`, `now`).
+ * @returns {Promise<number>}
+ */
+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 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, ...deps };
+
+ /** @type {{ project: { id: string, dir: string }, plan?: any, pruned?: any[], removed?: string[], failed?: any[], freedBytes?: number, error?: string }[]} */
+ let results;
+ if (all) {
+ results = (await pruneAllProjects({ ...common, dryRun: !apply, by: "cli", measure: apply ? "prunable" : "all" })).results;
+ } else {
+ const project = await pickProject(p.positional[0], reportsRoot);
+ const opts = { ...common, takes: takes.length ? takes : null, anyway: p.has("--anyway"), by: "cli" };
+ results = [apply ? { project, ...(await pruneProject(project, opts)) } : { project, plan: await planPrune(project, { ...opts, measure: "all" }) }];
+ }
+
+ const freed = results.reduce((s, r) => s + (apply ? r.freedBytes ?? 0 : r.plan?.totals.prunable.freedBytes ?? 0), 0);
+ const failed = results.flatMap((r) => (r.failed ?? []).map((f) => ({ project: r.project.id, ...f })));
+ const errors = results.filter((r) => r.error);
+ const free = await freeBytesAt(reportsRoot);
+ if (json) {
+ log(
+ JSON.stringify(
+ {
+ dryRun: !apply,
+ 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 } : {}) },
+ ),
+ freedBytes: freed,
+ freeBytes: free,
+ },
+ null,
+ 2,
+ ),
+ );
+ } else {
+ 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 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 ? 1 : 0;
+ } catch (e) {
+ if (e instanceof CliError) {
+ err(e.message);
+ return 2;
+ }
+ err(e instanceof Error ? e.message : String(e));
+ return 1;
+ }
+}
diff --git a/umtool/lib/report/takePrune.test.mjs b/umtool/lib/report/takePrune.test.mjs
@@ -29,6 +29,7 @@ import {
workPathsOf,
writeBuildLock,
} from "./takePrune.mjs";
+import { pruneCommand } from "./pruneCli.mjs";
import { framesOf, frameKeyOf } from "umtool-report-to-video/prune-frames";
const DAY = 24 * 3600 * 1000;
@@ -383,6 +384,42 @@ test("the disk guard: above the threshold nothing happens; under it the policy r
await assert.rejects(diskGuard({ dirs: ["/x"], env: { UMTOOL_DISK_GUARD_GB: "lots" }, free: async () => 1, pruneAll }), /not a number/);
});
+test("the CLI: a dry run by default, --yes frees, --json, and its refusals", async () => {
+ const { dir, reports } = await root();
+ await mkTake(dir, "a", { builtAt: "2026-09-01T00:00:00Z" });
+ await mkTake(dir, "b", { builtAt: "2026-09-02T00:00:00Z" });
+ const run = async (...args) => {
+ const out = [];
+ const err = [];
+ const code = await pruneCommand(args, { reportsRoot: reports, log: (l) => out.push(l), err: (l) => err.push(l), deps: { ...quiet, now: later } });
+ return { code, out: out.join("\n"), err: err.join("\n") };
+ };
+ let r = await run("proj", "--keep", "1");
+ assert.equal(r.code, 0, r.err);
+ assert.match(r.out, /a\s+prunable\s+work .* frees/);
+ assert.match(r.out, /b\s+kept .* the newest of group "takes" \(keeps 1\)/);
+ assert.match(r.out, /dry run — nothing deleted\. --yes frees/);
+ assert.ok(await exists(path.join(dir, "takes", "a", "out", "sourced", "segments")), "a dry run deletes nothing");
+ r = await run("proj", "--keep", "1", "--json");
+ const j = JSON.parse(r.out);
+ assert.equal(j.dryRun, true);
+ assert.equal(j.projects[0].takes.find((t) => t.id === "a").status, "prunable");
+ assert.equal(j.projects[0].measured, undefined);
+ r = await run("--all", "--keep", "1", "--yes");
+ assert.equal(r.code, 0, r.err);
+ assert.match(r.out, /freed a: /);
+ assert.equal(await exists(path.join(dir, "takes", "a", "out", "sourced", "segments")), false);
+ assert.ok(await exists(path.join(dir, "takes", "b", "out", "sourced", "segments")));
+ r = await run("proj", "--take", "b", "--anyway", "--yes");
+ assert.equal(r.code, 0, r.err);
+ assert.equal(await exists(path.join(dir, "takes", "b", "out", "sourced", "segments")), false);
+ assert.equal((await run()).code, 2);
+ assert.match((await run("--all", "--take", "a")).err, /names a take of one project/);
+ assert.match((await run("proj", "--anyway")).err, /name it with --take/);
+ assert.match((await run("proj", "--yes", "--dry-run")).err, /not both/);
+ assert.match((await run("nope")).err, /no project matches/);
+});
+
test("the names it prunes by are its writers' names", async () => {
const bv = await import("umtool-report-to-video/build-video");
const ce = await import("umtool-report-to-video/chunked-encode");