commit 5aee9c97619ea2755efcd00b48a68cb0d11b0d90
parent 3e6a53b3edd292bbe3c61fa1b8f66298d50a141f
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date: Mon, 26 Oct 2026 21:49:43 -0400
umtool take --from: this take's own newer segments and chrome frames are kept, not linked over
A take re-run --force --from <other> after a stop had its own deck and
feed (keyed, finished, newer) replaced by the other take's older ones, and
recaptured them (15:44 run of final-1026-best-blurred). A segment's files
now go as a group, decided by the newer side; a layer by its .key's mtime,
the loser linked beside as chrome/work-from-*-frames for frame reuse. The
final encode's chunks and the content-hash memo are linked in too.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Diffstat:
3 files changed, 135 insertions(+), 9 deletions(-)
diff --git a/umtool/lib/report/takeFrom.mjs b/umtool/lib/report/takeFrom.mjs
@@ -16,6 +16,23 @@
// when both are there, the one whose .key
// was written last
//
+// chunks/chunk-<key>.mp4, audio-<key>.m4a
+// each chunk of the final encode, reused
+// when its key (its inputs' CONTENTS) is
+// (chunked-encode.mjs), and the content-
+// hash memo, merged
+//
+// THIS take's own files win when they are newer (release fast-rerender): a
+// take re-run `--force --from <other>` after it was stopped mid-render has
+// segments and chrome frames of its OWN cut on disk, newer than the other
+// take's; linking over them threw away a finished deck and feed (the 15:44
+// run of final-1026-best-blurred captured both again). A segment's files go
+// as a group (the .mp4 and the .cut.json naming its key must come from the
+// same build), decided by the newest of each side's files; a chrome layer by
+// its `.key`'s mtime. The older layer is not lost: it is linked in as
+// `chrome/work-from-<layer>-frames`, where the chrome render finds frames it
+// can reuse (compose-chrome's frame reuse), and a prune removes it.
+//
// Links, never copies, and never a symlink: every one of these the build
// REPLACES rather than writes -- a segment through a rename
// (encodeSegmentCached), its records and texts through a rename (replaceFile),
@@ -26,9 +43,10 @@
//
// Plain node; the CLI runs it with bare node.
-import { link, lstat, mkdir, readdir, rename, rm, writeFile } from "node:fs/promises";
+import { link, lstat, mkdir, readFile, readdir, rename, rm, writeFile } from "node:fs/promises";
import path from "node:path";
import { DEFAULT_VARIANT, SEGMENT_FILE_SUFFIXES, XFADE_FROM_FILE } from "umtool-report-to-video/build-video";
+import { CHUNKS_DIR, HASH_MEMO_FILE } from "umtool-report-to-video/chunked-encode";
import { takesDirOf } from "./takes.mjs";
/** Where `--from` keeps a layer aside: the other take's `out/.keep-frames/<layer>-frames`. */
@@ -79,7 +97,7 @@ export async function fromTakeSourced(projectDir, id, from) {
/**
* Hard-link take `from`'s built files into `takeOut/sourced` (see the top of the file).
- * @returns {Promise<{ segments: number, frames: string[], batches: number }>}
+ * @returns {Promise<{ segments: number, frames: string[], batches: number, chunks: number, keptOwn: string[], keptOwnFrames: string[] }>}
*/
export async function linkFromTake(projectDir, id, from, takeOut) {
const src = await fromTakeSourced(projectDir, id, from);
@@ -90,10 +108,38 @@ export async function linkFromTake(projectDir, id, from, takeOut) {
const segs = path.join(src, "segments");
const segOut = path.join(dst, "segments");
await mkdir(segOut, { recursive: true });
+ // By segment: its files, theirs and ours (see the top of the file).
+ const idOf = (f) => {
+ const suffix = SEGMENT_FILE_SUFFIXES.filter((x) => f.endsWith(x)).sort((x, y) => y.length - x.length)[0];
+ return suffix ? f.slice(0, -suffix.length) : null;
+ };
+ const groups = new Map();
for (const f of await names(segs)) {
- if (f.startsWith(".") || !SEGMENT_FILE_SUFFIXES.some((s) => f.endsWith(s))) continue;
- if (!(await isFile(path.join(segs, f)))) continue;
- if (await linkFile(path.join(segs, f), path.join(segOut, f))) out.segments += 1;
+ if (f.startsWith(".")) continue;
+ const id = idOf(f);
+ if (!id || !(await isFile(path.join(segs, f)))) continue;
+ groups.set(id, [...(groups.get(id) ?? []), f]);
+ }
+ const newest = async (dir, files) => Math.max(0, ...(await Promise.all(files.map((f) => lstat(path.join(dir, f)).then((st) => st.mtimeMs, () => 0)))));
+ const ownNames = await names(segOut);
+ out.keptOwn = [];
+ for (const [id, theirs] of [...groups].sort((x, y) => x[0].localeCompare(y[0]))) {
+ const own = ownNames.filter((f) => !f.startsWith(".") && idOf(f) === id);
+ const ownFiles = [];
+ for (const f of own) if (await isFile(path.join(segOut, f))) ownFiles.push(f);
+ const linkedAlready = await Promise.all(ownFiles.map(async (f) => {
+ const [a, b] = await Promise.all([lstat(path.join(segOut, f)), lstat(path.join(segs, f)).catch(() => null)]);
+ return !!b && a.ino === b.ino && a.dev === b.dev;
+ }));
+ const ownBuilt = ownFiles.filter((_, k) => !linkedAlready[k]);
+ if (ownBuilt.length && (await newest(segOut, ownBuilt)) > (await newest(segs, theirs))) {
+ out.keptOwn.push(id);
+ continue;
+ }
+ for (const f of theirs) if (await linkFile(path.join(segs, f), path.join(segOut, f))) out.segments += 1;
+ // Ours that theirs does not have would sit beside their segment as if
+ // they were its own: gone.
+ for (const f of ownFiles) if (!theirs.includes(f)) await rm(path.join(segOut, f), { force: true });
}
// The keyed renders: chrome/<layer>-frames, with its .key, else the one kept aside.
@@ -111,8 +157,20 @@ export async function linkFromTake(projectDir, id, from, takeOut) {
}
}
if (layers.size) await mkdir(path.join(dst, "chrome"), { recursive: true });
- for (const [name, { dir }] of [...layers].sort((x, y) => x[0].localeCompare(y[0]))) {
- await linkFrames(dir, path.join(dst, "chrome", name));
+ out.keptOwnFrames = [];
+ for (const [name, { dir, at }] of [...layers].sort((x, y) => x[0].localeCompare(y[0]))) {
+ const mine = path.join(dst, "chrome", name);
+ const ownKey = await lstat(path.join(mine, ".key")).catch(() => null);
+ const theirKey = await lstat(path.join(dir, ".key")).catch(() => null);
+ const same = ownKey && theirKey && ownKey.ino === theirKey.ino && ownKey.dev === theirKey.dev;
+ if (ownKey?.isFile() && !same && ownKey.mtimeMs > at) {
+ // This take's own render is the newer: kept; theirs beside it for frame reuse.
+ await linkFrames(dir, path.join(dst, "chrome", `work-from-${name}`));
+ out.keptOwnFrames.push(name);
+ continue;
+ }
+ if (ownKey?.isFile() && !same) await linkFrames(mine, path.join(dst, "chrome", `work-from-own-${name}`));
+ await linkFrames(dir, mine);
out.frames.push(name);
}
@@ -123,6 +181,25 @@ export async function linkFromTake(projectDir, id, from, takeOut) {
await mkdir(batchOut, { recursive: true });
if (await linkFile(path.join(batches, f), path.join(batchOut, f))) out.batches += 1;
}
+ // The final encode's chunks and sound, by content key: theirs beside ours.
+ const chunks = path.join(src, CHUNKS_DIR);
+ const chunksOut = path.join(dst, CHUNKS_DIR);
+ out.chunks = 0;
+ for (const f of await names(chunks)) {
+ if (!/^(chunk|audio)-[0-9a-f]+\.(mp4|m4a)$/.test(f) || !(await isFile(path.join(chunks, f)))) continue;
+ await mkdir(chunksOut, { recursive: true });
+ if (!(await isFile(path.join(chunksOut, f))) && (await linkFile(path.join(chunks, f), path.join(chunksOut, f)))) out.chunks += 1;
+ }
+ // The content-hash memo: theirs merged into ours (both name files by inode).
+ const memoOf = (d) => readFile(path.join(d, HASH_MEMO_FILE), "utf8").then((t) => JSON.parse(t), () => null);
+ const [theirMemo, ownMemo] = await Promise.all([memoOf(src), memoOf(dst)]);
+ if (theirMemo?.v === 1 && theirMemo.h) {
+ const merged = { v: 1, h: { ...theirMemo.h, ...(ownMemo?.v === 1 ? ownMemo.h : {}) } };
+ const tmp = path.join(dst, `${HASH_MEMO_FILE}.${process.pid}.tmp`);
+ await writeFile(tmp, JSON.stringify(merged), "utf8");
+ await rename(tmp, path.join(dst, HASH_MEMO_FILE));
+ }
+
// A batch the other take keyed by its own segments' paths (before keys
// named files) is found through this: build-video adopts it when this
// take's segments are those very files (xfadeBatchKeyV1).
diff --git a/umtool/lib/report/takeFrom.test.mjs b/umtool/lib/report/takeFrom.test.mjs
@@ -60,8 +60,9 @@ test("linkFromTake: segments, keyed chrome frames (kept-aside ones too) and fini
try {
const out = path.join(p.dir, "takes", "twin", "out");
await mkdir(path.join(out, "sourced", "segments"), { recursive: true });
- // A file of the twin's own is replaced by the link (through a rename).
+ // An OLDER file of the twin's own is replaced by the link (through a rename).
await writeFile(path.join(out, "sourced", "segments", "s02.mp4"), "the twin's own");
+ await utimes(path.join(out, "sourced", "segments", "s02.mp4"), new Date(0), new Date(0));
const got = await linkFromTake(p.dir, "twin", "blurred", out);
const d = path.join(out, "sourced");
for (const f of ["s01.mp4", "s01.cut.json", "s01.attrib.txt", "s02.mp4", "s02.cut.json", "brand-intro.mp4", "brand-intro.intro.json"]) {
@@ -97,6 +98,44 @@ test("linkFromTake: segments, keyed chrome frames (kept-aside ones too) and fini
}
});
+test("linkFromTake: this take's own NEWER build (a re-run after a stop) is kept, theirs beside it", async () => {
+ const p = await project();
+ try {
+ const out = path.join(p.dir, "takes", "twin", "out");
+ const d = path.join(out, "sourced");
+ await mkdir(path.join(d, "segments"), { recursive: true });
+ await mkdir(path.join(d, "chrome", "deck-frames"), { recursive: true });
+ // The twin's own run, stopped mid-chrome: s01 and the deck rendered for
+ // ITS cut, after the other take's.
+ const later = new Date(Date.now() + 60_000);
+ for (const f of ["s01.mp4", "s01.cut.json"]) {
+ await writeFile(path.join(d, "segments", f), `twin ${f}`);
+ await utimes(path.join(d, "segments", f), later, later);
+ }
+ await writeFile(path.join(d, "chrome", "deck-frames", "frame_000001.png"), "twin png");
+ await writeFile(path.join(d, "chrome", "deck-frames", ".key"), "twin-key\n");
+ await utimes(path.join(d, "chrome", "deck-frames", ".key"), later, later);
+ const got = await linkFromTake(p.dir, "twin", "blurred", out);
+ assert.deepEqual(got.keptOwn, ["s01"]);
+ assert.equal(await readFile(path.join(d, "segments", "s01.mp4"), "utf8"), "twin s01.mp4");
+ assert.equal(await readFile(path.join(d, "segments", "s01.cut.json"), "utf8"), "twin s01.cut.json", "the record stays with its segment");
+ assert.ok(!existsSync(path.join(d, "segments", "s01.attrib.txt")), "none of theirs beside our s01");
+ assert.ok(sameFile(path.join(p.src, "segments", "s02.mp4"), path.join(d, "segments", "s02.mp4")), "a segment we lack is linked");
+ assert.deepEqual(got.keptOwnFrames, ["deck-frames"]);
+ assert.ok(!got.frames.includes("deck-frames"));
+ assert.equal(await readFile(path.join(d, "chrome", "deck-frames", ".key"), "utf8"), "twin-key\n", "our deck is not replaced");
+ assert.ok(sameFile(path.join(p.src, "chrome", "deck-frames", "frame_000001.png"), path.join(d, "chrome", "work-from-deck-frames", "frame_000001.png")), "theirs kept for frame reuse");
+ // An OLDER own layer is replaced, and kept beside as well.
+ await utimes(path.join(d, "chrome", "deck-frames", ".key"), new Date(0), new Date(0));
+ const again = await linkFromTake(p.dir, "twin", "blurred", out);
+ assert.ok(again.frames.includes("deck-frames"));
+ assert.equal(await readFile(path.join(d, "chrome", "deck-frames", ".key"), "utf8"), "k1\n");
+ assert.equal(await readFile(path.join(d, "chrome", "work-from-own-deck-frames", ".key"), "utf8"), "twin-key\n");
+ } finally {
+ await rm(p.root, { recursive: true, force: true });
+ }
+});
+
test("makeTake --from: linked before the render; --keep-frames reaches the build; a bad --from is refused before anything is written", async () => {
const p = await project();
try {
diff --git a/umtool/lib/report/takeMaker.mjs b/umtool/lib/report/takeMaker.mjs
@@ -551,8 +551,18 @@ export async function makeTake(projectDir, rawSpec, opts = {}) {
const got = await fromTake.linkFromTake(projectDir, spec.id, /** @type {string} */ (opts.from), takeOut);
log(
`${spec.id}: linked from ${opts.from}: ${got.segments} segment file(s), ` +
- `${got.frames.length ? got.frames.join(", ") : "no chrome frames"}, ${got.batches} crossfade batch(es) — reused where they still match`,
+ `${got.frames.length ? got.frames.join(", ") : "no chrome frames"}, ${got.batches} crossfade batch(es), ` +
+ `${got.chunks ?? 0} encode chunk(s) — reused where they still match`,
);
+ // This take's own newer build, kept (takeFrom.mjs): said, so a re-run says why it did not start over.
+ if (got.keptOwn?.length || got.keptOwnFrames?.length) {
+ log(
+ `${spec.id}: kept this take's own newer ` +
+ [got.keptOwn?.length ? `${got.keptOwn.length} segment(s)` : null, got.keptOwnFrames?.length ? got.keptOwnFrames.join(", ") : null]
+ .filter(Boolean).join(" and ") +
+ ` (${opts.from}'s older ones kept beside them for reuse)`,
+ );
+ }
}
// The take's manifest is written under a work name until the render is