// `umtool prep`: the clip bench's first-open work for a project, made ahead // of the walk (lib/report/benchPrep.mjs says what and why). // // umtool prep [--clips a,b] [--json] queue it; a runner starts in the background // umtool prep [--clips a,b] --wait run it here and wait (the same runner, niced) // umtool prep --status [--json] what is ready, what is not, the runner if one is alive // … --take a TAKE's clips (its walk's manifest -- a draft's too), // its own status, queue and runner // // `--run` is the runner itself (what the queue starts): it takes the // project's run lock and drains the queue, one clip at a time, each in the // machine's heavy slot with PREP_ESTIMATE_MB declared. Plain ESM: the CLI runs // under bare node. Imported by bin/umtool.mjs only -- never by the app, which // reaches the heavy gate through the runner process. import { spawn } from "node:child_process"; import { REPORTS_ROOT, resolveProject } from "../projects/core.mjs"; import { PREP_ESTIMATE_MB, benchPrepState, drainPrepQueue, queueBenchPrep, runnerArgv, } from "./benchPrep.mjs"; import { UMTOOL_DIR } from "./driver.mjs"; export const PREP_USAGE = [ "usage: umtool prep [--clips a,b] [--json] queue the clips' excerpts and waveforms for the walk; returns at once", " umtool prep [--clips a,b] --wait the same, here, until it is done", " umtool prep --status [--json] ready / to prepare / not fetched, and the runner", " … --take a take's clips instead of the cut's: the take's manifest as its walk reads it (a draft's", " too, nothing rendered), its own status, queue and runner (out/bench-excerpts/takes//)", "", "One clip at a time, each in the heavy slot (estimate " + PREP_ESTIMATE_MB + " MB), under nice -n 19 and ionice -c3.", "A clip already prepared (its excerpt cut from its recording as it is now, its waveform cached) is skipped.", ].join("\n"); /** @param {Awaited>} s */ export function stateLine(s) { const c = s.counts; const fetched = c.clips - c.noWindow; const parts = [`${c.ready} of ${fetched} fetched clip${fetched === 1 ? "" : "s"} ready for the walk`]; if (c.todo) parts.push(`${c.todo} to prepare${c.excerpts ? ` (${c.excerpts} excerpt${c.excerpts === 1 ? "" : "s"} to cut)` : ""}`); if (c.failed) parts.push(`${c.failed} failed`); if (c.noWindow) parts.push(`${c.noWindow} not fetched`); if (s.runner) { const cur = s.runner.current; parts.push(`preparing ${Math.min(s.runner.done + 1, s.runner.total)} of ${s.runner.total}${cur ? ` — ${cur.id}, ${cur.step === "waiting" ? "waiting for the heavy slot" : cur.step}` : ""} (pid ${s.runner.pid})`); } return `${s.project}${s.take ? ` take ${s.take}` : ""}: ${parts.join(" · ")}`; } const stamp = () => new Date().toISOString().slice(11, 19); /** * The quote check's verdicts on the project's clips as they are now (lane * one-clock, quoteCheck.mjs): "quotes: 40 ok, 2 moved (c43, c45), 1 not found * (c19) — umtool window

--align"; null with no check on record. * @param {string} dir */ export async function quoteLine(dir) { const [{ readQuoteCheck, rowIsCurrent }, { readManifest }] = await Promise.all([import("./quoteCheck.mjs"), import("../projects/report.mjs")]); const doc = await readQuoteCheck(dir); if (!Object.keys(doc.clips).length) return null; const m = await readManifest(dir); /** @type {Record} */ const by = {}; for (const e of m?.timeline ?? []) { if (e?.type !== "clip") continue; const row = doc.clips[e.id]; const st = !row ? "unchecked" : !rowIsCurrent(row, e) ? "stale" : e.quoteRuled && (row.status === "moved" || row.status === "not-found") ? "ruled" : row.status; (by[st] ??= []).push(e.id); } const part = (k, label) => (by[k]?.length ? `${by[k].length} ${label}${k === "ok" || k === "unchecked" || k === "stale" ? "" : ` (${by[k].join(", ")})`}` : null); const parts = [part("ok", "ok"), part("moved", "moved"), part("not-found", "not found"), part("ruled", "ruled"), part("stale", "stale"), part("unchecked", "unchecked")].filter(Boolean); return { counts: Object.fromEntries(Object.entries(by).map(([k, v]) => [k, v.length])), line: ` quotes: ${parts.join(", ")}${by.moved?.length ? " — umtool window

--align" : ""}${by.stale?.length || by.unchecked?.length ? " — umtool quote-check

" : ""}`, }; } /** The runner's progress as lines (stderr, and prep.log when it runs detached). */ export function eventLine(project, ev) { if (ev.ev === "start" && ev.take) project = `${project} take ${ev.take}`; switch (ev.ev) { case "start": return `${stamp()} prep ${project}: ${ev.total} to prepare (${ev.ready} ready${ev.noWindow ? `, ${ev.noWindow} not fetched` : ""})`; case "clip": return ev.step === "waiting" ? `${stamp()} ${ev.id} (${ev.n}/${ev.total}): waiting for the heavy slot` : `${stamp()} ${ev.id}: ${ev.step}${ev.window ? ` ${ev.window}` : ""}`; case "clip-done": return `${stamp()} ${ev.id}: ready (${(Number(ev.ms) / 1000).toFixed(1)} s)`; case "clip-failed": return `${stamp()} ${ev.id}: FAILED — ${String(ev.error).split("\n").pop()}`; case "end": return `${stamp()} prep ${project}: ${ev.done} prepared, ${ev.failed} failed, ${ev.skipped} already ready`; default: return null; } } /** * @param {string[]} args the words after `prep` * @param {{ root?: string, out?: (s: string) => void, err?: (s: string) => void }} [opts] * @returns {Promise} */ export async function prepCommand(args, { root = REPORTS_ROOT, out = (s) => console.log(s), err = (s) => console.error(s) } = {}) { const json = args.includes("--json"); const words = []; let clips = null; let take = null; for (let i = 0; i < args.length; i += 1) { if (args[i] === "--clips") { clips = String(args[i + 1] ?? "").split(",").map((s) => s.trim()).filter(Boolean); i += 1; } else if (args[i] === "--take") { take = String(args[i + 1] ?? ""); i += 1; if (!take || take.startsWith("--")) { err(`--take needs a take id\n${PREP_USAGE}`); return 2; } } else if (!args[i].startsWith("--")) words.push(args[i]); } const [projectArg] = words; if (!projectArg || args.includes("--help")) { err(PREP_USAGE); return 2; } const r = await resolveProject(projectArg, root); if (r.ambiguous) { err(`"${projectArg}" is the name of ${r.ambiguous.length} projects — pass its id`); return 1; } if (!r.project) { err(`no project matches "${projectArg}"`); return 1; } const project = { id: r.project.id, dir: r.project.dir }; if (take) { // The take must resolve (recorded, or named by a recipe), before anything is queued. try { const { prepManifestOf } = await import("./benchPrep.mjs"); await prepManifestOf(project, take); } catch (e) { err(`${project.id}: ${e instanceof Error ? e.message : String(e)}`); return 1; } } // ---- the runner ---------------------------------------------------------- if (args.includes("--run")) { const ndjson = args.includes("--ndjson"); const { withRenderSlot } = await import("../../report-to-video/heavy-gate.mjs"); const res = await drainPrepQueue(project, { take, initial: args.includes("--wait") ? { clips, retry: args.includes("--retry") } : null, wait: args.includes("--wait"), slot: (cmd, fn) => withRenderSlot(cmd, fn, { estimateMb: PREP_ESTIMATE_MB }), onEvent: (ev) => { const line = ndjson ? JSON.stringify(ev) : eventLine(project.id, ev); if (line) process.stderr.write(`${line}\n`); }, }); return res.failed.length ? 1 : 0; } if (args.includes("--status")) { const s = await benchPrepState(project, { clips, take }); const quotes = await quoteLine(project.dir); if (json) out(JSON.stringify({ ...s, ...(quotes ? { quoteCheck: quotes.counts } : {}) }, null, 2)); else { out(stateLine(s)); for (const it of s.items) if (it.state === "failed") out(` ✗ ${it.id}: ${it.error ?? "failed"}`); if (quotes) out(quotes.line); } return 0; } // ---- --wait: the runner, here -------------------------------------------- if (args.includes("--wait")) { const argv = runnerArgv(project.id, { wait: true, clips, retry: true, take }); const code = await new Promise((resolve) => { const child = spawn(argv[0], argv.slice(1), { cwd: UMTOOL_DIR, stdio: ["ignore", "inherit", "inherit"], env: process.env }); child.on("error", (e) => { err(String(e)); resolve(1); }); child.on("exit", (c, sig) => resolve(sig ? 1 : (c ?? 1))); }); const s = await benchPrepState(project, { clips, take }); if (json) out(JSON.stringify(s, null, 2)); else out(stateLine(s)); return Number(code) === 0 ? 0 : 1; } // ---- queue it -------------------------------------------------------------- const q = await queueBenchPrep(project, clips, { retry: true, take }); const s = await benchPrepState(project, { clips, take }); if (json) out(JSON.stringify({ ...s, queued: q.queued, started: q.started, pid: q.pid, ...(q.off ? { off: true } : {}) }, null, 2)); else { out(stateLine(s)); if (q.off) out(" UMTOOL_BENCH_PREP=0: nothing queued"); else if (q.queued.length) out(` queued ${q.queued.length}: ${q.started ? `runner started (pid ${q.pid})` : `the runner already going takes them (pid ${q.pid})`}; its log is out/bench-excerpts/${take ? `takes/${take}/` : ""}prep.log`); else out(" nothing to prepare"); } return 0; }