// The takes' work-file pruner (takePrune.mjs): what is work and what is final, // hard links counted by inode, the retention rules, the build lock, the disk // guard, the symlink-escape guard, the CLI and the take maker's prune after a // take is made. Every project is a temp directory. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { link, lstat, mkdir, mkdtemp, readFile, readdir, rm, stat, symlink, utimes, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import test, { after } from "node:test"; import { BUILD_LOCK, DiskGuardRefused, KEEP_FRAMES_DIR, LADDER_DIR, PRUNE_RECORD, XFADE_FROM_FILE, deleteViewOf, diskGuard, measurePaths, parseAge, planPrune, pruneAfterBuild, pruneProject, readBuildLock, removeWorkPath, resolvePolicy, unionFreed, workPathsOf, writeBuildLock, } from "./takePrune.mjs"; import { pruneCommand } from "./pruneCli.mjs"; import { makeTake } from "./takeMaker.mjs"; import { framesOf, frameKeyOf } from "umtool-report-to-video/prune-frames"; const DAY = 24 * 3600 * 1000; const KiB = 1024; // A clock ten days on: nothing on disk is "recent" to it. const later = () => new Date(Date.now() + 10 * DAY); // No pipeline processes, and only this process's pid alive. const quiet = { processes: [], alive: (pid) => pid === process.pid }; // Every temp project, removed at the end (/tmp may be memory). const made = []; after(() => Promise.all(made.map((d) => rm(d, { recursive: true, force: true })))); async function root() { const r = await mkdtemp(path.join(tmpdir(), "takeprune-")); made.push(r); const reports = path.join(r, "reports"); const dir = path.join(reports, "proj"); await mkdir(path.join(dir, "takes"), { recursive: true }); await writeFile(path.join(dir, "video.manifest.json"), JSON.stringify({ slug: "fx", timeline: [] }) + "\n"); return { r, reports, dir, project: { id: "proj", dir }, opts: { reportsRoot: reports, mediaRoot: reports, ...quiet } }; } const bytes = (n, ch = "x") => Buffer.alloc(n, ch); /** * A take with final files and work: segments, two chrome layers' frames, a * crossfade batch, encode chunks, the hash memo -- each `kib` KiB a file. */ async function mkTake(dir, id, { group = "takes", builtAt = "2026-10-01T00:00:00Z", from, manifest, kib = 16, frames = 3, work = true } = {}) { const t = path.join(dir, "takes", id); const v = path.join(t, "out", "sourced"); await mkdir(v, { recursive: true }); await writeFile(path.join(t, "take.json"), JSON.stringify({ id, group, order: 1, label: id, kind: "different", preview: "preview.mp4", builtAt, ...(from ? { from } : {}) }) + "\n"); await writeFile(path.join(t, "preview.mp4"), bytes(kib * KiB, "p")); await link(path.join(t, "preview.mp4"), path.join(t, "out", "fx.mp4")); await writeFile(path.join(t, "out", "fx.720p.mp4"), bytes(8 * KiB, "r")); await writeFile(path.join(t, "video.manifest.json"), manifest ?? JSON.stringify({ slug: "fx", take: id }) + "\n"); for (const f of ["schedule.json", "sync.json", "chapters.ffmeta"]) await writeFile(path.join(v, f), "{}\n"); await mkdir(path.join(v, "cards"), { recursive: true }); await writeFile(path.join(v, "cards", "c1.png"), bytes(4 * KiB)); await mkdir(path.join(v, "chrome", "deck"), { recursive: true }); await writeFile(path.join(v, "chrome", "deck", "index.html"), ""); if (!work) return t; await mkdir(path.join(v, "segments"), { recursive: true }); await writeFile(path.join(v, "segments", "s01.mp4"), bytes(kib * KiB, "1")); await writeFile(path.join(v, "segments", "s01.cut.json"), "{}"); for (const layer of ["deck", "feed"]) { const fd = path.join(v, "chrome", `${layer}-frames`); await mkdir(fd, { recursive: true }); await writeFile(path.join(fd, ".key"), `key-${layer}-${id}\n`); for (let i = 1; i <= frames; i++) await writeFile(path.join(fd, `frame_${String(i).padStart(6, "0")}.png`), bytes(kib * KiB, "f")); } await mkdir(path.join(v, "xfade-batches"), { recursive: true }); await writeFile(path.join(v, "xfade-batches", "batch-abc.mov"), bytes(kib * KiB, "b")); if (from) await writeFile(path.join(v, "xfade-batches", XFADE_FROM_FILE), JSON.stringify({ take: from }) + "\n"); await mkdir(path.join(v, "chunks"), { recursive: true }); await writeFile(path.join(v, "chunks", "chunk-0a.mp4"), bytes(kib * KiB, "c")); await writeFile(path.join(v, ".content-hashes.json"), "{}"); return t; } const exists = (p) => lstat(p).then(() => true, () => false); const byId = (plan, id) => plan.takes.find((t) => t.id === id); const codes = (plan, id) => byId(plan, id).keep.map((r) => r.code).sort(); test("work and final: the work is the named build files under out/; the renders, preview, records and cards are not", async () => { const { dir, opts } = await root(); const t = await mkTake(dir, "a"); // A shared link to the project's clips-raw is not work; a real one is. await mkdir(path.join(dir, "out", "clips-raw"), { recursive: true }); await symlink(path.join(dir, "out", "clips-raw"), path.join(t, "out", "clips-raw")); await writeFile(path.join(t, "out", "sourced", "fx.prerail-hardcut.mp4"), bytes(KiB)); const { roots } = await workPathsOf(t, opts); assert.deepEqual(roots.map((r) => r.rel), [ "out/sourced/.content-hashes.json", "out/sourced/chrome/deck-frames", "out/sourced/chrome/feed-frames", "out/sourced/chunks", "out/sourced/fx.prerail-hardcut.mp4", "out/sourced/segments/*.mp4", "out/sourced/xfade-batches/batch-*.mov", ]); const r = await pruneProject({ id: "proj", dir }, { ...opts, flags: { keep: 0 }, now: later }); assert.deepEqual(r.pruned.map((p) => p.id), ["a"]); for (const f of ["take.json", "preview.mp4", "out/fx.mp4", "out/fx.720p.mp4", "video.manifest.json", "out/sourced/schedule.json", "out/sourced/sync.json", "out/sourced/chapters.ffmeta", "out/sourced/cards/c1.png", "out/sourced/chrome/deck/index.html"]) { assert.ok(await exists(path.join(t, f)), `${f} is final and stays`); } for (const f of ["out/sourced/segments/s01.mp4", "out/sourced/chrome/deck-frames", "out/sourced/xfade-batches/batch-abc.mov", "out/sourced/chunks", "out/sourced/.content-hashes.json"]) { assert.equal(await exists(path.join(t, f)), false, `${f} is work and goes`); } // A segment's records stay (a trailer maps its beats through them). assert.ok(await exists(path.join(t, "out", "sourced", "segments", "s01.cut.json"))); assert.ok((await lstat(path.join(t, "out", "clips-raw"))).isSymbolicLink(), "the shared clips-raw link stays"); // A pruned chrome layer stays verifiable: its count and key were recorded. const deck = path.join(t, "out", "sourced", "chrome", "deck-frames"); assert.equal(await framesOf(deck), 3); assert.equal(await frameKeyOf(deck), "key-deck-a"); const rec = JSON.parse(await readFile(path.join(t, PRUNE_RECORD), "utf8")); assert.equal(rec.prunes.length, 1); assert.equal(rec.prunes[0].by, "cli"); assert.ok(rec.prunes[0].freedBytes > 0); // Pruned once, a take has no work: the plan says so, and when. const again = await planPrune({ id: "proj", dir }, { ...opts, flags: { keep: 0 }, now: later }); assert.equal(byId(again, "a").status, "no-work"); assert.ok(byId(again, "a").pruned); }); test("hard links: a take built --from another shares its inodes; pruning one frees only what is its own, and the other keeps its bytes", async () => { const { dir, project, opts } = await root(); const a = await mkTake(dir, "a", { builtAt: "2026-10-01T00:00:00Z" }); const b = await mkTake(dir, "b", { builtAt: "2026-10-02T00:00:00Z" }); // b's segment and deck frames ARE a's (takeFrom.mjs links them in). const va = path.join(a, "out", "sourced"); const vb = path.join(b, "out", "sourced"); for (const f of ["s01.mp4", "s01.cut.json"]) { await rm(path.join(vb, "segments", f)); await link(path.join(va, "segments", f), path.join(vb, "segments", f)); } await rm(path.join(vb, "chrome", "deck-frames"), { recursive: true }); await mkdir(path.join(vb, "chrome", "deck-frames")); for (const f of await readdir(path.join(va, "chrome", "deck-frames"))) await link(path.join(va, "chrome", "deck-frames", f), path.join(vb, "chrome", "deck-frames", f)); // Measured alone, each take's work counts the shared inodes; its "frees" does not. const ma = await measurePaths((await workPathsOf(a, opts)).roots); const mb = await measurePaths((await workPathsOf(b, opts)).roots); const sharedKeys = [...ma.multi.keys()].filter((k) => mb.multi.has(k)); assert.ok(sharedKeys.length >= 5, "the segment, its record, three frames and the key are shared"); const plan = await planPrune(project, { ...opts, policy: resolvePolicy({ env: {}, flags: { keep: 0, olderThan: "0" } }), now: later }); const pa = byId(plan, "a"); assert.ok(pa.workBytes > pa.freedBytes, "a's work includes what b also holds"); const shared = [...ma.multi.values()].reduce((s, v) => s + v.bytes, 0); assert.equal(pa.workBytes - pa.freedBytes, shared); // Both prunable: the union frees the shared inodes too -- more than the two alone. assert.equal(plan.totals.prunable.freedBytes, unionFreed([ma, mb])); assert.equal(plan.totals.prunable.freedBytes, pa.freedBytes + byId(plan, "b").freedBytes + shared); assert.equal(plan.totals.prunable.workBytes, plan.totals.prunable.freedBytes, "nothing outside the two holds a link"); // Prune a alone (b pinned): b's files are whole, and the freed count is a's own. await writeFile(path.join(dir, "takes", "prune.json"), JSON.stringify({ keep: 0, olderThanDays: 0, pin: ["b"] })); const seg = await readFile(path.join(vb, "segments", "s01.mp4")); const r = await pruneProject(project, { ...opts, now: later }); assert.deepEqual(r.pruned.map((p) => p.id), ["a"]); assert.equal(r.freedBytes, pa.freedBytes); assert.equal(await exists(path.join(va, "segments", "s01.mp4")), false); assert.deepEqual(await readFile(path.join(vb, "segments", "s01.mp4")), seg); assert.equal((await stat(path.join(vb, "segments", "s01.mp4"))).nlink, 1, "b holds the last link now"); assert.equal((await readdir(path.join(vb, "chrome", "deck-frames"))).length, 4); // b, measured again, now frees all of its work. const after = await planPrune(project, { ...opts, now: later }); const pb = byId(after, "b"); assert.equal(pb.status, "kept"); assert.equal(pb.workBytes, pb.freedBytes); }); test("the policy: picked, current, pinned, newest K per group, modified within D days, and the --from bases of kept takes", async () => { const { dir, project, opts } = await root(); const manifest = await readFile(path.join(dir, "video.manifest.json"), "utf8"); await mkTake(dir, "old-1", { builtAt: "2026-09-01T00:00:00Z" }); await mkTake(dir, "old-2", { builtAt: "2026-09-02T00:00:00Z" }); await mkTake(dir, "picked", { builtAt: "2026-09-03T00:00:00Z" }); await mkTake(dir, "current", { builtAt: "2026-09-04T00:00:00Z", manifest }); await mkTake(dir, "base-of-base", { builtAt: "2026-09-05T00:00:00Z" }); await mkTake(dir, "base", { builtAt: "2026-09-06T00:00:00Z", from: "base-of-base" }); await mkTake(dir, "n2", { builtAt: "2026-09-07T00:00:00Z" }); await mkTake(dir, "n1", { builtAt: "2026-09-08T00:00:00Z", from: "base" }); await mkTake(dir, "other", { group: "intro", builtAt: "2026-09-01T00:00:00Z" }); await mkTake(dir, "pinned", { group: "intro", builtAt: "2026-08-01T00:00:00Z" }); await mkTake(dir, "nothing", { work: false, builtAt: "2026-08-01T00:00:00Z" }); await writeFile(path.join(dir, "takes", "verdicts.json"), JSON.stringify({ picked: { verdict: null, note: "", at: "", picked: true } })); await writeFile(path.join(dir, "takes", "prune.json"), JSON.stringify({ pin: ["pinned"] })); const plan = await planPrune(project, { ...opts, now: later }); assert.deepEqual(plan.policy.pin, ["pinned"]); assert.equal(plan.policy.keep, 2); assert.deepEqual(codes(plan, "n1"), ["newest"]); assert.deepEqual(codes(plan, "n2"), ["newest"]); assert.deepEqual(codes(plan, "base"), ["base"], "n1 is built from it"); assert.deepEqual(codes(plan, "base-of-base"), ["base"], "transitively"); assert.deepEqual(codes(plan, "picked"), ["picked"]); assert.deepEqual(codes(plan, "current"), ["current"]); assert.deepEqual(codes(plan, "other"), ["newest"], "the newest of its own group"); assert.deepEqual(codes(plan, "pinned"), ["newest", "pinned"]); assert.equal(byId(plan, "old-1").status, "prunable"); assert.equal(byId(plan, "old-2").status, "prunable"); assert.equal(byId(plan, "nothing").status, "no-work"); assert.equal(byId(plan, "pinned").keep.find((r) => r.code === "newest").why, 'the 2nd newest of group "intro" (keeps 2)'); // Today's clock: everything was modified just now. const today = await planPrune(project, opts); assert.ok(codes(today, "old-1").includes("recent")); assert.equal(today.totals.prunable.takes, 0); // --keep and --older-than on the command line beat the file and the defaults. const keep0 = await planPrune(project, { ...opts, flags: { keep: 0, olderThan: "12h" }, now: later }); assert.equal(byId(keep0, "n1").status, "prunable"); assert.equal(byId(keep0, "base").status, "prunable", "its child is not kept now, so neither is it"); assert.equal(byId(keep0, "pinned").status, "kept"); assert.equal(keep0.policy.from.keep, "--keep"); // A named take with --anyway: the policy's reasons are overridden. const named = await planPrune(project, { ...opts, takes: ["picked"], anyway: true, now: later }); assert.deepEqual(named.takes.map((t) => t.id), ["picked"]); assert.equal(byId(named, "picked").status, "prunable"); assert.deepEqual(byId(named, "picked").overridden.map((r) => r.code), ["picked"]); // The route's view: without force, a forceable refusal; with it, the take goes. const view = deleteViewOf(await planPrune(project, { ...opts, takes: ["picked"], now: later }), { take: "picked" }); assert.deepEqual(view.refusals.map((r) => r.forceable), [true]); assert.equal(deleteViewOf(named, { take: "picked" }).refusals.length, 0); await assert.rejects(planPrune(project, { ...opts, takes: ["nope"] }), /has no take nope/); }); test("the settings: parseAge, the environment, and a prune.json that does not parse refuses", async () => { assert.equal(parseAge("3d"), 3); assert.equal(parseAge("12h"), 0.5); assert.equal(parseAge("2"), 2); assert.throws(() => parseAge("soon"), /not an age/); const p = resolvePolicy({ env: { UMTOOL_PRUNE_KEEP: "4", UMTOOL_PRUNE_DAYS: "1d", UMTOOL_PRUNE_AFTER_BUILD: "0" }, file: { keep: 3 } }); assert.equal(p.keep, 3); assert.equal(p.olderThanDays, 1); assert.equal(p.afterBuild, false); assert.deepEqual(p.from, { keep: "prune.json", olderThanDays: "UMTOOL_PRUNE_DAYS", afterBuild: "UMTOOL_PRUNE_AFTER_BUILD" }); assert.throws(() => resolvePolicy({ env: { UMTOOL_PRUNE_KEEP: "-1" } }), /whole number/); const { dir, project, opts } = await root(); await mkTake(dir, "a"); await writeFile(path.join(dir, "takes", "prune.json"), "{ nope"); await assert.rejects(planPrune(project, opts), /does not parse/); await writeFile(path.join(dir, "takes", "prune.json"), JSON.stringify({ pin: "a" })); await assert.rejects(planPrune(project, opts), /pin is a list/); }); test("a running build: its take and the take it builds from are never pruned, not even by name; a dead lock is ignored", async () => { const { dir, project, opts } = await root(); await mkTake(dir, "base", { builtAt: "2026-09-01T00:00:00Z" }); const twin = await mkTake(dir, "twin", { builtAt: "2026-09-02T00:00:00Z" }); const other = await mkTake(dir, "other", { builtAt: "2026-09-03T00:00:00Z" }); await mkTake(dir, "loose", { builtAt: "2026-09-04T00:00:00Z" }); const flags = { keep: 0, olderThan: "0" }; await writeBuildLock(twin, { take: "twin", from: "base" }); // A lock of a process that is gone. await writeFile(path.join(other, BUILD_LOCK), JSON.stringify({ pid: 999999999, host: (await import("node:os")).hostname(), startedAt: "x" })); // build-video writing into loose's out/ by hand. const processes = [{ pid: 4242, args: ["node", "/x/build-video.mjs", "m.json", "--out", path.join(dir, "takes", "loose", "out")], cwd: "/" }]; const plan = await planPrune(project, { ...opts, processes, flags, now: later }); assert.deepEqual(codes(plan, "twin"), ["building"]); assert.deepEqual(codes(plan, "base"), ["base-of-building"]); assert.deepEqual(codes(plan, "loose"), ["building"]); assert.match(byId(plan, "loose").keep[0].why, /pid 4242/); assert.equal(byId(plan, "other").status, "prunable"); assert.ok(plan.notes.some((n) => /pid 999999999\) is gone/.test(n))); const anyway = await planPrune(project, { ...opts, processes, flags, takes: ["twin", "base"], anyway: true, now: later }); assert.equal(byId(anyway, "twin").status, "kept"); assert.equal(byId(anyway, "base").status, "kept"); const view = deleteViewOf(anyway, { take: "twin" }); assert.deepEqual(view.refusals.map((r) => r.forceable), [false]); const r = await pruneProject(project, { ...opts, processes, flags, now: later }); assert.deepEqual(r.pruned.map((p) => p.id), ["other"]); assert.ok(await exists(path.join(twin, "out", "sourced", "segments", "s01.mp4"))); assert.ok(await exists(path.join(dir, "takes", "base", "out", "sourced", "segments", "s01.mp4"))); assert.ok(await exists(path.join(dir, "takes", "loose", "out", "sourced", "segments", "s01.mp4"))); // The lock is this process's: removed, and the twin is prunable again. const { removeBuildLock } = await import("./takePrune.mjs"); await removeBuildLock(path.join(twin, BUILD_LOCK)); assert.equal(await readBuildLock(path.join(twin, BUILD_LOCK)), null); }); test("the symlink guard: a link is never work and never followed; a path resolving outside the take is not removed", async () => { const { r, dir, project, opts } = await root(); const outside = path.join(r, "corpus"); await mkdir(outside, { recursive: true }); await writeFile(path.join(outside, "window.mp4"), bytes(2 * KiB, "w")); await writeFile(path.join(outside, "frame.png"), bytes(KiB, "o")); const t = await mkTake(dir, "a"); const v = path.join(t, "out", "sourced"); // segments/ is a link to the corpus: not work. await rm(path.join(v, "segments"), { recursive: true }); await symlink(outside, path.join(v, "segments")); // A real clips-raw holding a link into the corpus: the link goes, the corpus file stays. await mkdir(path.join(t, "out", "clips-raw")); await symlink(path.join(outside, "window.mp4"), path.join(t, "out", "clips-raw", "window.mp4")); // A frame that is a link out of the take. await symlink(path.join(outside, "frame.png"), path.join(v, "chrome", "deck-frames", "frame_000099.png")); const { roots } = await workPathsOf(t, opts); assert.equal(roots.some((x) => x.rel.endsWith("segments")), false); assert.ok(roots.some((x) => x.rel === "out/clips-raw")); const pr = await pruneProject(project, { ...opts, flags: { keep: 0 }, now: later }); assert.deepEqual(pr.pruned.map((p) => p.id), ["a"]); assert.ok(await exists(path.join(outside, "window.mp4")), "the corpus file is whole"); assert.ok(await exists(path.join(outside, "frame.png"))); assert.ok((await lstat(path.join(v, "segments"))).isSymbolicLink(), "the link itself was not touched"); assert.ok(await exists(path.join(outside, "window.mp4"))); assert.equal(await exists(path.join(t, "out", "clips-raw")), false); assert.equal(await exists(path.join(v, "chrome", "deck-frames")), false); // removeWorkPath refuses a link and a path outside the allowed roots. await symlink(outside, path.join(v, "work-link")); assert.match(await removeWorkPath(path.join(v, "work-link"), [t]), /a link/); assert.match(await removeWorkPath(path.join(outside, "window.mp4"), [t]), /outside the take/); assert.ok(await exists(path.join(outside, "window.mp4"))); // A take whose out/ is a link anywhere but the media root's mirror: nothing in it is work. const b = path.join(dir, "takes", "b"); await mkdir(b, { recursive: true }); await writeFile(path.join(b, "take.json"), JSON.stringify({ id: "b", group: "g", order: 1, label: "b", kind: "different", preview: "preview.mp4" })); const elsewhere = path.join(r, "elsewhere"); await mkdir(path.join(elsewhere, "sourced", "segments"), { recursive: true }); await writeFile(path.join(elsewhere, "sourced", "segments", "s.mp4"), "s"); await symlink(elsewhere, path.join(b, "out")); const wb = await workPathsOf(b, opts); assert.equal(wb.roots.length, 0); assert.match(wb.notes[0], /not this take's media-root mirror/); // ... while out/ linked to its media-root mirror is followed. const media = path.join(r, "media"); const c = path.join(dir, "takes", "c"); await mkdir(c, { recursive: true }); const mirror = path.join(media, "proj", "takes", "c", "out"); await mkdir(path.join(mirror, "sourced", "segments"), { recursive: true }); await writeFile(path.join(mirror, "sourced", "segments", "s.mp4"), "s"); await symlink(mirror, path.join(c, "out")); const wc = await workPathsOf(c, { ...opts, mediaRoot: media }); assert.deepEqual(wc.roots.map((x) => x.rel), ["out/sourced/segments/*.mp4"]); }); test("the disk guard: above the threshold nothing happens; under it the policy runs; still under the floor the render is refused", async () => { const GB = 1024 ** 3; let calls = 0; const said = []; const pruneAll = async () => { calls += 1; return { freedBytes: 5 * GB, results: [{ pruned: [{ id: "a" }, { id: "b" }] }] }; }; // Plenty of room. let free = 100 * GB; let r = await diskGuard({ dirs: ["/x"], env: {}, free: async () => free, pruneAll, say: (l) => said.push(l) }); assert.equal(calls, 0); assert.equal(r.freedBytes, 0); // Under 30 GB: pruned, said, and the render goes on above the floor. const seq = [20 * GB, 25 * GB]; r = await diskGuard({ dirs: ["/x", "/y"], env: {}, free: async () => seq[Math.min(calls, 1)], pruneAll, say: (l) => said.push(l) }); assert.equal(calls, 1); assert.equal(r.freedBytes, 5 * GB); assert.ok(said.some((l) => /freed 5\.0 GiB from 2 take\(s\) in 1 project/.test(l)), said.join("\n")); // Under the floor after the prune: refused, with a sentence. free = 6 * GB; await assert.rejects( diskGuard({ dirs: ["/x"], env: {}, free: async () => free, pruneAll, say: () => {} }), (err) => err instanceof DiskGuardRefused && /only 6\.0 GiB free under \/x, under the 10\.0 GiB a render needs — nothing was rendered/.test(err.message), ); // The settings: thresholds from the environment, and off. calls = 0; r = await diskGuard({ dirs: ["/x"], env: { UMTOOL_DISK_GUARD_GB: "5", UMTOOL_DISK_FLOOR_GB: "1" }, free: async () => 6 * GB, pruneAll }); assert.equal(calls, 0); r = await diskGuard({ dirs: ["/x"], env: { UMTOOL_DISK_GUARD: "0" }, free: async () => 1, pruneAll }); assert.equal(r.checked, false); assert.equal(calls, 0); await assert.rejects(diskGuard({ dirs: ["/x"], env: { UMTOOL_DISK_GUARD_GB: "lots" }, free: async () => 1, pruneAll }), /not a number/); }); test("after a take is made: its project pruned by the policy, the new take kept, said in its log; the lock held during the render", async () => { const { dir, reports } = await root(); await writeFile(path.join(dir, "video.manifest.json"), JSON.stringify({ schemaVersion: 1, slug: "fx", title: "Fx", render: { width: 64, height: 36, fps: 15 }, timeline: [{ type: "card", id: "k1", seconds: 2 }] }, null, 2) + "\n"); const old = await mkTake(dir, "old", { builtAt: "2026-09-01T00:00:00Z" }); const longAgo = new Date(Date.now() - 30 * DAY); for (const f of ["take.json", "preview.mp4"]) await utimes(path.join(old, f), longAgo, longAgo); await writeFile(path.join(dir, "takes", "prune.json"), JSON.stringify({ keep: 0 })); const logs = []; let lockDuring = null; const render = async (job) => { lockDuring = await readBuildLock(path.join(dir, "takes", "new", BUILD_LOCK)); await mkdir(path.join(job.outDir, "sourced"), { recursive: true }); const file = path.join(job.outDir, `${job.slug}.mp4`); await writeFile(file, "render"); return { file }; }; const r = await makeTake(dir, { id: "new", label: "New", window: "full" }, { render, probe: async () => 2, log: (l) => logs.push(l), from: null }); assert.equal(r.skipped, false); assert.equal(lockDuring?.pid, process.pid); assert.equal(lockDuring?.take, "new"); assert.equal(await readBuildLock(path.join(dir, "takes", "new", BUILD_LOCK)), null, "released after"); assert.equal(await exists(path.join(old, "out", "sourced", "segments", "s01.mp4")), false, "the old take's work went"); assert.ok(await exists(path.join(old, "preview.mp4"))); assert.ok(logs.some((l) => /^new: prune: freed .* from 1 take\(s\): old/.test(l)), logs.join("\n")); // Off in takes/prune.json: nothing pruned, nothing said. const old2 = await mkTake(dir, "old2", { builtAt: "2026-09-01T00:00:00Z" }); for (const f of ["take.json", "preview.mp4"]) await utimes(path.join(old2, f), longAgo, longAgo); await writeFile(path.join(dir, "takes", "prune.json"), JSON.stringify({ keep: 0, afterBuild: false })); logs.length = 0; await makeTake(dir, { id: "new2", label: "New 2", window: "full" }, { render, probe: async () => 2, log: (l) => logs.push(l) }); assert.ok(await exists(path.join(old2, "out", "sourced", "segments", "s01.mp4"))); assert.equal(logs.some((l) => /prune:/.test(l)), false); // pruneAfterBuild never throws: a bad policy file is said. await writeFile(path.join(dir, "takes", "prune.json"), "{"); const line = await pruneAfterBuild(dir, { built: "new", reportsRoot: reports }); assert.match(line, /^prune: not run — .*does not parse/); // A take made --from records it (what the pruner reads as its base). await writeFile(path.join(dir, "takes", "prune.json"), JSON.stringify({ afterBuild: false })); await makeTake(dir, { id: "twin", label: "Twin", window: "full" }, { render, probe: async () => 2, from: "new" }); assert.equal(JSON.parse(await readFile(path.join(dir, "takes", "twin", "take.json"), "utf8")).from, "new"); }); 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", "s01.mp4")), "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", "s01.mp4")), false); assert.ok(await exists(path.join(dir, "takes", "b", "out", "sourced", "segments", "s01.mp4"))); 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", "s01.mp4")), 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"); const tf = await import("./takeFrom.mjs"); const lc = await import("umtool-report-to-video/ladder-chunks"); assert.equal(XFADE_FROM_FILE, bv.XFADE_FROM_FILE); assert.equal(LADDER_DIR, lc.LADDER_DIR); assert.equal(lc.LADDER_CHUNKS_DIR, "chunks"); assert.equal(KEEP_FRAMES_DIR, tf.KEEP_FRAMES_DIR); assert.equal(bv.DEFAULT_VARIANT, "sourced"); assert.equal(ce.CHUNKS_DIR, "chunks"); assert.equal(ce.HASH_MEMO_FILE, ".content-hashes.json"); }); test("the disk guard: still under the threshold after the takes' prune, the render pool gives back what only it holds", async () => { const GB = 1024 ** 3; const said = []; let free = 8 * GB; let asked = null; const r = await diskGuard({ dirs: ["/x"], env: {}, free: async () => free, say: (l) => said.push(l), pruneAll: async () => ({ freedBytes: 0, results: [] }), prunePool: async ({ deficit }) => { asked = deficit; free += 15 * GB; return { evicted: 3, bytes: 15 * GB }; }, }); assert.equal(asked, 22 * GB, "the deficit to the 30 GB threshold"); assert.equal(r.freedBytes, 15 * GB); assert.ok(said.some((l) => /the render pool evicted 3 object\(s\), 15\.0 GiB/.test(l)), said.join("\n")); });