commit eb97327e2e3314c6a1570e15db7534b0d83db85a
parent 60cf82fe3888455fae5b5eec2c912440fcd2d18a
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date: Sun, 1 Nov 2026 00:05:42 -0400
umtool: fetch a gap-joined clip per kept piece -- gaps.mjs fetchPieces pads each kept piece at its own edges and joins pieces under a minute apart; umtool fetch asks one item per piece (a clip over 5385-21812 asks for its two moments, not 4.5 hours); a refetch evicts every piece
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Diffstat:
4 files changed, 90 insertions(+), 15 deletions(-)
diff --git a/umtool/lib/report/fetchWindows.mjs b/umtool/lib/report/fetchWindows.mjs
@@ -8,7 +8,10 @@
//
// `from`/`to` are the build's own fetch span (build-video.mjs `fetchSpan`:
// start − render.fetchPad … end + render.fetchPad, two decimals) — for a phrase
-// seed (fetchPad 0) exactly the clip's start and end. `pad` is provenance (the
+// seed (fetchPad 0) exactly the clip's start and end. A clip with gaps is
+// asked for PER KEPT PIECE (gaps.mjs fetchPieces, the build's `fetchSpans`):
+// each piece padded at its own edges, pieces under a minute apart as one —
+// a gap-joined clip over 5385–21812 asks for its two moments, not 4½ hours. `pad` is provenance (the
// seconds each side that are margin): the fetchPad, else 3. `reason` is
// "<title> montage" for a montage (clips carrying `montage`), else the title.
// A montage clip the rulings leave out (cut / not-him / read-aloud) is not
@@ -35,6 +38,7 @@ import { createHash } from "node:crypto";
import { mkdir, readFile, rename, writeFile } from "node:fs/promises";
import path from "node:path";
import { LEFT_OUT_VERDICTS, clipMomentKey, readMontageRulings } from "umtool-report-to-video/montage-rulings";
+import { fetchPieces, renderGaps } from "umtool-report-to-video/gaps";
import { agentToken } from "../annotations/agentAuth.mjs";
export const FETCH_RUNS_FILE = "fetch-runs.json";
@@ -87,15 +91,19 @@ export function fetchWindowsBody(manifest, { manifestPath, project, rulings = nu
skipped.push({ clipId: String(e.id), why: `ruled ${ruling.verdict}` });
continue;
}
- items.push({
- slug,
- id: e.video,
- from: r2(Math.max(0, Number(e.start) - margin)),
- to: r2(Number(e.end) + margin),
- clipId: e.id,
- pad: padOf,
- reason: why.slice(0, REASON_LIMIT),
- });
+ // One item per kept piece (gaps.mjs fetchPieces, the build's fetchSpans):
+ // a gap-joined clip asks for its moments, never the hours between them.
+ for (const span of fetchPieces(Number(e.start), Number(e.end), renderGaps(e, manifest?.render ?? {}), { before: margin, after: margin })) {
+ items.push({
+ slug,
+ id: e.video,
+ from: r2(span.from),
+ to: r2(span.to),
+ clipId: e.id,
+ pad: padOf,
+ reason: why.slice(0, REASON_LIMIT),
+ });
+ }
}
return {
body: { requestedBy: `umtool:${project}`, manifest: path.resolve(manifestPath), storedUrl: "fallback", items },
@@ -122,14 +130,15 @@ export function refetchEvictions(body, skipped, { clipIds = [], videos = [] }) {
const add = (it) => {
const key = `${it.slug}/${it.id}`;
const req = byVideo.get(key) ?? { slug: it.slug, id: it.id, covering: [], clipIds: [] };
- if (req.clipIds.includes(it.clipId)) return;
+ // A clip fetched in pieces is one clip with several spans.
+ if (req.covering.some((c) => c.from === it.from && c.to === it.to)) return;
req.covering.push({ from: it.from, to: it.to });
- req.clipIds.push(it.clipId);
+ if (!req.clipIds.includes(it.clipId)) req.clipIds.push(it.clipId);
byVideo.set(key, req);
};
for (const clipId of clipIds) {
- const it = items.find((i) => i.clipId === clipId);
- if (!it) {
+ const its = items.filter((i) => i.clipId === clipId);
+ if (!its.length) {
const left = skipped.find((s) => s.clipId === clipId);
return {
ok: false,
@@ -138,7 +147,7 @@ export function refetchEvictions(body, skipped, { clipIds = [], videos = [] }) {
: `no clip "${clipId}" in the manifest's timeline`,
};
}
- add(it);
+ for (const it of its) add(it);
}
for (const v of videos) {
const at = String(v).indexOf("/");
diff --git a/umtool/lib/report/fetchWindows.test.mjs b/umtool/lib/report/fetchWindows.test.mjs
@@ -273,3 +273,29 @@ test("umtool fetch --refetch: evict-clips {slug, id, covering} first, waited for
await rm(root, { recursive: true, force: true });
}
});
+
+test("a gap-joined clip is fetched per kept piece, each padded at its own edges -- never the hours between", () => {
+ // Two moments of one stream joined by a gap: 5385–5400 and 21800–21812.
+ const joined = { type: "clip", id: "j01", channel: "synth-tw", video: "2468013579", start: 5385, end: 21812, gaps: [{ from: 5400, to: 21800 }] };
+ const short = { type: "clip", id: "j02", channel: "synth-tw", video: "2468013579", start: 100, end: 130, gaps: [{ from: 110, to: 112.5 }] };
+ const m = { title: "Pieces", render: { fetchPad: 3, transition: 0 }, timeline: [joined, short] };
+ const { body } = fetchWindowsBody(m, { manifestPath: "/r/p/video.manifest.json", project: "p" });
+ assert.deepEqual(
+ body.items.map((i) => [i.clipId, i.from, i.to]),
+ [
+ ["j01", 5382, 5403],
+ ["j01", 21797, 21815],
+ // A short gap: one span, the padded extent, as before.
+ ["j02", 97, 133],
+ ],
+ );
+ const asked = body.items.filter((i) => i.clipId === "j01").reduce((s, i) => s + (i.to - i.from), 0);
+ assert.ok(asked < 60, `the joined clip asks for ${asked}s, not ${21815 - 5382}s`);
+ // A refetch of the clip evicts both of its pieces.
+ const r = refetchEvictions(body, [], { clipIds: ["j01"] });
+ assert.ok(r.ok);
+ assert.deepEqual(r.requests, [{ slug: "synth-tw", id: "2468013579", covering: [{ from: 5382, to: 5403 }, { from: 21797, to: 21815 }], clipIds: ["j01"] }]);
+ // A montage cuts every clip to its phrase, so applies no gaps: the whole extent.
+ const montage = fetchWindowsBody({ ...m, render: { ...m.render, montage: {} }, timeline: [joined] }, { manifestPath: "/r/p/video.manifest.json", project: "p" });
+ assert.deepEqual(montage.body.items.map((i) => [i.from, i.to]), [[5382, 21815]]);
+});
diff --git a/umtool/report-to-video/gaps.mjs b/umtool/report-to-video/gaps.mjs
@@ -284,6 +284,34 @@ export function keptSpans(from, to, gaps) {
return out;
}
+/** Padded pieces closer than this are fetched as one span: a minute more of one window is cheaper than another paced fetch. */
+export const FETCH_JOIN_UNDER = 60;
+
+/**
+ * The spans a clip's source is FETCHED over: each kept piece of [from, to]
+ * (keptSpans) padded `before` / `after` at its own outer edges, and padded
+ * pieces less than `joinUnder` seconds apart joined into one. A clip without
+ * gaps -- or whose gaps are short -- is one span, its padded extent, as it
+ * always was; a gap-joined clip spanning hours (two moments of one stream)
+ * is one span per moment, never the hours between.
+ *
+ * @param {number} from
+ * @param {number} to
+ * @param {Array<{ from: number, to: number }> | null | undefined} gaps the gaps a render applies (renderGaps)
+ * @param {{ before?: number, after?: number, joinUnder?: number }} [pad]
+ * @returns {Array<{ from: number, to: number }>}
+ */
+export function fetchPieces(from, to, gaps, { before = 0, after = 0, joinUnder = FETCH_JOIN_UNDER } = {}) {
+ const out = [];
+ for (const p of keptSpans(from, to, gaps)) {
+ const span = { from: Math.max(0, p.from - before), to: p.to + after };
+ const last = out[out.length - 1];
+ if (last && span.from - last.to < joinUnder) last.to = Math.max(last.to, span.to);
+ else out.push(span);
+ }
+ return out;
+}
+
/**
* A segment's gaps on its OWN clock, for the encode: each gap's edges in
* seconds from the segment's first frame (`segStart`, source seconds), put on
diff --git a/umtool/report-to-video/gaps.test.mjs b/umtool/report-to-video/gaps.test.mjs
@@ -207,3 +207,15 @@ test("gapFilterParts on a real clip: the kept seconds, the gap's black and silen
rmSync(dir, { recursive: true, force: true });
}
});
+
+test("fetchPieces: one padded span per kept piece; pieces under a minute apart are one; no gaps is the padded extent", async () => {
+ const { fetchPieces } = await import("./gaps.mjs");
+ assert.deepEqual(fetchPieces(10, 20, [], { before: 3, after: 2 }), [{ from: 7, to: 22 }]);
+ assert.deepEqual(fetchPieces(1, 20, null, { before: 3, after: 3 }), [{ from: 0, to: 23 }], "never before 0");
+ assert.deepEqual(fetchPieces(5385, 21812, [{ from: 5400, to: 21800 }], { before: 3, after: 3 }), [{ from: 5382, to: 5403 }, { from: 21797, to: 21815 }]);
+ assert.deepEqual(fetchPieces(100, 300, [{ from: 120, to: 170 }], { before: 3, after: 3 }), [{ from: 97, to: 303 }], "47 s apart: one fetch");
+ assert.deepEqual(
+ fetchPieces(0, 1000, [{ from: 100, to: 400 }, { from: 410, to: 900 }], { before: 3, after: 3 }),
+ [{ from: 0, to: 103 }, { from: 397, to: 413 }, { from: 897, to: 1003 }],
+ );
+});