// THE RAIL'S RUNNING LIST, AS DATA (plans/umtool-redesign.md, P2b): which of // the editor's live jobs become rows, in what order, what folds into the foot // line, and how a job's progress and spend read. Pure -- no disk, no clock, no // fetch -- so `node --test` pins the rules; lib/activity.ts does the reading. // // The editor is asked for EVERY live job (`GET /api/ops/jobs?active=1`, no // `kind`): a research run's acquisitions are downloads, imports and post // walks, kinds no filter could name in advance. The choosing happens here: // // lanes the auto-queue runners (auto-download, auto-transcribe, …) run // for as long as the editor does. They are counted on the foot // line, never a row: a row that never ends says nothing. // rank 0 generation (a report draft, the verdict bench, a voice) // rank 1 ours: an acquisition a research run asked for, a clip window // umtool asked for // rank 2 anything else the editor runs (a sync, a transcription) // // Rows: every running job (umtool's own first, then by rank, oldest first), // then the QUEUED jobs of rank 0 and 1 -- at most MAX_ROWS. The rest folds: // "+ N running · M queued · K lanes". A queued rank-2 job is never a row; the // editor's queue can hold hundreds of them. /** The editor's lane runners: long-lived loops that enqueue the real work. */ export const LANE_KINDS = new Set(["auto-download", "auto-transcribe", "auto-digest", "auto-backfill", "auto-publish"]); /** Generation: model calls, the voices a narration is spoken with (and its derived takes: voice-enhance, voice-convert), and a recurring report's tick (R5). */ export const GENERATION_KINDS = new Set(["report-draft", "bench-verdicts", "tts-file", "voice-enhance", "voice-convert", "report-tick"]); /** Clip windows: umtool asks for them (`requestedBy: "umtool"`, the job's detail says so). */ const WINDOW_KINDS = new Set(["fetch-window", "fetch-windows"]); export const MAX_ROWS = 5; /** @param {string | undefined} kind */ export const isLane = (kind) => typeof kind === "string" && LANE_KINDS.has(kind); /** * A job's rank (lower shows first). `research` is the set of job ids a research * run's proposals name. * * @param {{ id: string, kind?: string, detail?: string }} job * @param {Set} [research] */ export function rankOf(job, research = new Set()) { if (job.kind && GENERATION_KINDS.has(job.kind)) return 0; if (research.has(job.id)) return 1; if (job.kind && WINDOW_KINDS.has(job.kind) && /^umtool\b/.test(job.detail ?? "")) return 1; return 2; } /** * The spend a generation job's log reports so far: the last `~$0.41` in it * (the agent tasks write one per turn, `turn 3: … — ~$0.41`), or null. * * @param {string[]} lines */ export function spendFromLog(lines) { let out = null; for (const line of lines ?? []) { for (const m of String(line).matchAll(/~\$(\d+(?:\.\d+)?)/g)) out = Number(m[1]); } return out !== null && Number.isFinite(out) ? out : null; } /** * The schedule a report-tick job's FIRST log line names (`series : …` or * `living : …`, controller/reportTick.ts), or null (R5). * * @param {string[]} lines */ export function tickTargetFromLog(lines) { for (const line of lines ?? []) { const m = /^(living|series) ([a-z0-9][a-z0-9-]*): /.exec(String(line)); if (m) return { shape: m[1], id: m[2] }; } return null; } /** * The article, iteration and task a report-draft job's FIRST log line names * (`sweep of
i3 on (…)`), or null. The fallback when no * iteration on disk carries the job's id yet. * * @param {string[]} lines */ export function draftTargetFromLog(lines) { for (const line of lines ?? []) { const m = /^([a-z-]+) of ([a-z0-9][a-z0-9-]*) i(\d+) on /.exec(String(line)); if (m) return { task: m[1], article: m[2], n: Number(m[3]) }; } return null; } /** * The editor's progress snapshot as one fraction and a few words, or null. * `current` is the runner's own count when it keeps one; else the snapshot's * `initial` (the editor re-counts the rest from disk for its own page only). * * @param {{ metric?: string, initial?: number, current?: number, target?: number } | null | undefined} p */ export function editorProgress(p) { if (!p || typeof p.target !== "number" || !(p.target > 0)) return null; const done = typeof p.current === "number" ? p.current : typeof p.initial === "number" ? p.initial : 0; const fraction = Math.max(0, Math.min(1, done / p.target)); return { fraction, text: `${done}/${p.target}${p.metric ? ` ${p.metric}` : ""}` }; } /** * Where a queued job waits, in words. * * @param {{ position?: number, queued?: number } | null | undefined} q */ export function queueText(q) { if (!q || typeof q.position !== "number" || q.position <= 0) return "queued"; return q.position === 1 ? "queued · next" : `queued · ${q.position - 1} ahead`; } /** * What one of umtool's own jobs is, from its kind string: `chain
i2` * is "Make the video" of that iteration, `draft-all …` "Draft everything", and * any other (`build (final)`, `narrate `, …) its * first word, the rest as the target. `project` is the job's own, when stamped. * * @param {string} kind * @param {string | null} [project] */ export function ownJobLabel(kind, project = null) { const chain = /^(chain|draft-all) (\S+) i(\d+)$/.exec(kind); if (chain) { return { label: chain[1] === "draft-all" ? "Draft everything" : "Make the video", target: `${chain[2]} i${chain[3]}`, article: chain[2], n: Number(chain[3]), generation: true, }; } const [head, ...rest] = kind.split(" "); const word = head ? head[0].toUpperCase() + head.slice(1).replace(/-/g, " ") : kind; return { label: word, target: project ?? (rest.join(" ") || null), article: null, n: null, generation: false }; } /** * Choose the rows and fold the rest. * * @template {{ state: string, own?: boolean, lane?: boolean, rank?: number, startedAt: number }} I * @param {I[]} items * @param {number} [max] * @returns {{ rows: I[], more: { running: number, queued: number, lanes: number }, running: number, queued: number }} */ export function foldRunning(items, max = MAX_ROWS) { const lanes = items.filter((i) => i.lane && i.state === "running").length; const live = items.filter((i) => !i.lane && (i.state === "running" || i.state === "queued")); const order = (a, b) => Number(!a.own) - Number(!b.own) || (a.rank ?? 2) - (b.rank ?? 2) || a.startedAt - b.startedAt; const running = live.filter((i) => i.state === "running").sort(order); const queued = live.filter((i) => i.state === "queued").sort(order); const candidates = [...running, ...queued.filter((i) => (i.rank ?? 2) <= 1)]; const rows = candidates.slice(0, max); const shown = new Set(rows); return { rows, more: { running: running.filter((i) => !shown.has(i)).length, queued: queued.filter((i) => !shown.has(i)).length, lanes, }, running: running.length, queued: queued.length, }; } /** * The foot line under the rows, or null: "+ 2 running · 140 queued · 3 lanes". * * @param {{ running: number, queued: number, lanes: number }} more */ export function moreText(more) { const parts = []; if (more.running) parts.push(`+ ${more.running} running`); if (more.queued) parts.push(`${parts.length ? "" : "+ "}${more.queued} queued`); if (more.lanes) parts.push(`${parts.length ? "" : "+ "}${more.lanes} lane${more.lanes === 1 ? "" : "s"}`); return parts.length ? parts.join(" · ") : null; } /** * The line that says why the editor's jobs are not listed, by reason * (lib/articles/generation.ts EditorErrorReason), or null when they are. * * @param {string | null} reason */ export function editorStateText(reason) { switch (reason) { case null: case undefined: return null; case "unreachable": return "Editor unreachable"; case "editor-token": return "Editor's WORKER_TOKEN not set"; case "no-token": return "WORKER_TOKEN not set here"; case "denied": return "WORKER_TOKEN does not match the editor's"; default: return "Editor's jobs could not be read"; } }