// THE HEAVY SLOT, AS IT STANDS (UI notes 2026-10-30, #18: the render queue // panel). Read from the slot's own files beside its lock (scripts/queue-lock.mjs, // WORKTREES.md "The heavy slot") -- nothing here is guessed: // // heavy-queue.lock.jobs/.json one ticket per job RUNNING in the slot: // its estimate, whether it builds or runs // alone, its command, since when // heavy-queue.lock.waiting/.json one ticket per job WAITING: the same, // and its stage (render, line, slot, // memory, build, exclusive, serial) // heavy-queue.lock.admit.holder.json the job at the head of the line // heavy-queue.lock.build.holder.json the build slot's holder (one next build at a time) // heavy-queue.lock.render.json render mode (renderPriority.mjs) // -queue.lock.holder.json each e2e suite's own queue // // A ticket whose process is gone (or whose pid was reused) is not counted; // this module never removes one -- queue-lock does that. Plain ESM: umtool's // route and the CLI read it under bare node. import fs from "node:fs"; import path from "node:path"; import { describeRenderPriority, holderAlive, readRenderPriority, renderPriorityFile } from "./renderPriority.mjs"; /** What a waiting job waits for, in words. */ export const STAGE_WORDS = Object.freeze({ door: "arriving", render: "waiting for the render (render mode)", line: "in line behind the job at the head", slot: "at the head of the line, for the slot", memory: "at the head of the line, for memory", build: "for the build slot (one next build at a time)", exclusive: "for a job that runs alone", serial: "one job at a time (this machine is under the floor)", }); /** A ticket's process is alive and is the one that wrote it. */ const alive = (t, isAlive) => Number.isInteger(t?.pid) && isAlive({ pid: t.pid, pidStart: t.pidStart ?? null }); function readDir(dir, isAlive) { let names = []; try { names = fs.readdirSync(/* turbopackIgnore: true */ dir).filter((n) => n.endsWith(".json")); } catch { return []; } const out = []; for (const n of names) { try { const t = JSON.parse(fs.readFileSync(/* turbopackIgnore: true */ path.join(dir, n), "utf8")); if (alive(t, isAlive)) out.push(t); } catch { /* half-written by queue-lock: skipped this time */ } } return out; } function readHolder(file, isAlive) { try { const h = JSON.parse(fs.readFileSync(/* turbopackIgnore: true */ file, "utf8")); return alive(h, isAlive) ? h : null; } catch { return null; } } /** * What a command is: a render (build-video.mjs; its project directory and take * read from the manifest path it is given), an e2e suite, a next build, the * encode ladder, or the command itself. * @param {string} cmd * @returns {{ kind: "render" | "e2e" | "build" | "ladder" | "other", label: string, projectDir?: string | null, take?: string | null }} */ export function classifyCommand(cmd) { const c = String(cmd ?? ""); if (/build-video\.mjs/.test(c)) { // A take's render: `build-video.mjs /.take-.render.json --out /takes//out` // (umtool/lib/report/takeRender.mjs); a project build: `build-video.mjs /video.manifest.json`. const after = c.slice(c.indexOf("build-video.mjs") + "build-video.mjs".length).trim(); const man = after.split(/\s+/)[0] ?? ""; const take = /(?:^|\/)\.take-([a-z0-9][a-z0-9-]*)\.render\.json$/.exec(man)?.[1] ?? /\/takes\/([a-z0-9][a-z0-9-]*)\/out(?:\s|$)/.exec(after)?.[1] ?? null; const projectDir = man.includes("/") ? man.slice(0, man.lastIndexOf("/")) : null; const name = projectDir ? projectDir.split("/").slice(-2).join("/") : null; return { kind: "render", label: take ? `render of ${take}${name ? ` (${name})` : ""}` : `render${name ? ` of ${name}` : ""}`, projectDir, take }; } if (/variants|ladder/.test(c) && /umtool/.test(c)) return { kind: "ladder", label: "the encode ladder (variants)" }; if (/playwright|run-sharded-e2e|\be2e\b/.test(c)) { const suite = /--name (e2e-[a-z0-9-]+)/.exec(c)?.[1] ?? /--filter ([a-z0-9-]+)/.exec(c)?.[1] ?? null; return { kind: "e2e", label: suite ? `e2e suite ${suite}` : "an e2e suite" }; } if (/next build/.test(c)) { const pkg = /--filter ([a-z0-9-]+)/.exec(c)?.[1] ?? null; return { kind: "build", label: pkg ? `next build (${pkg})` : "a next build" }; } return { kind: "other", label: c.length > 90 ? `${c.slice(0, 90)}…` : c }; } const shape = (t, now) => { const what = classifyCommand(t.cmd); const started = Date.parse(t.since ?? t.startedAt ?? ""); return { pid: t.pid, what: what.label, kind: what.kind, ...(what.projectDir ? { projectDir: what.projectDir } : {}), ...(what.take ? { take: what.take } : {}), estimateMb: Number.isFinite(t.estimateMb) ? t.estimateMb : null, build: Boolean(t.build), alone: Boolean(t.exclusive), worktree: t.worktree ? path.basename(String(t.worktree)) : null, branch: t.branch ?? null, cmd: String(t.cmd ?? ""), since: Number.isFinite(started) ? new Date(started).toISOString() : null, forSeconds: Number.isFinite(started) ? Math.max(0, Math.round((now - started) / 1000)) : null, }; }; /** * The slot now. * @param {{ heavyLock: string, renderFile?: string, env?: Record, now?: number, isAlive?: (h: { pid: number, pidStart: string | null }) => boolean, meminfo?: string }} o */ export function readHeavyQueue({ heavyLock, renderFile, env = process.env, now = Date.now(), isAlive = holderAlive, meminfo = "/proc/meminfo" }) { const running = readDir(`${heavyLock}.jobs`, isAlive).map((t) => shape(t, now)).sort((a, b) => String(a.since).localeCompare(String(b.since))); const waiting = readDir(`${heavyLock}.waiting`, isAlive) .map((t) => ({ ...shape(t, now), stage: String(t.stage ?? "door"), stageWords: STAGE_WORDS[/** @type {keyof typeof STAGE_WORDS} */ (t.stage)] ?? String(t.stage ?? ""), })) // The head of the line first, then by arrival. .sort((a, b) => Number(b.stage !== "line" && b.stage !== "render" && b.stage !== "door") - Number(a.stage !== "line" && a.stage !== "render" && a.stage !== "door") || String(a.since).localeCompare(String(b.since))); const head = readHolder(`${heavyLock}.admit.holder.json`, isAlive); const build = readHolder(`${heavyLock}.build.holder.json`, isAlive); // An older copy of queue-lock writes no ticket, only the slot's holder file. const holder = running.length ? null : readHolder(`${heavyLock}.holder.json`, isAlive); const dir = path.dirname(heavyLock); const suites = []; try { for (const n of fs.readdirSync(/* turbopackIgnore: true */ dir)) { const m = /^(e2e(?:-[a-z0-9-]+)?)-queue\.lock\.holder\.json$/.exec(n); if (!m) continue; const h = readHolder(path.join(dir, n), isAlive); if (h) suites.push({ suite: m[1], ...shape(h, now) }); } } catch { /* no lock dir yet */ } // An e2e suite's slot ticket names only its command ("playwright test"): its // name is the suite queue its process holds. for (const r of [...running, ...waiting]) { if (r.kind !== "e2e") continue; const names = suites.filter((x) => x.pid === r.pid).map((x) => x.suite); if (names.length) r.what = `e2e suite ${names[0]}${names.length > 1 ? ` (and ${names.slice(1).join(", ")})` : ""}`; } const rp = readRenderPriority(renderFile ?? renderPriorityFile(env, { heavyLock })); let mem = null; try { const text = fs.readFileSync(/* turbopackIgnore: true */ meminfo, "utf8"); const kb = (k) => Number(new RegExp(`^${k}:\\s+(\\d+)`, "m").exec(text)?.[1] ?? NaN); const a = kb("MemAvailable"); const t = kb("MemTotal"); if (Number.isFinite(a)) mem = { availableMb: Math.round(a / 1024), totalMb: Number.isFinite(t) ? Math.round(t / 1024) : null }; } catch { mem = null; } const floor = Number(env.HEAVY_MIN_FREE_MB ?? 6000); const promised = running.reduce((n, r) => n + (r.estimateMb ?? 0), 0); return { heavyLock, running: holder && !running.length ? [shape(holder, now)] : running, waiting, head: head ? shape(head, now) : null, build: build ? shape(build, now) : null, suites: suites.sort((a, b) => a.suite.localeCompare(b.suite)), render: rp.held && rp.state ? { held: true, what: describeRenderPriority(rp.state), since: rp.state.since ?? null, until: rp.state.until ?? null } : { held: false }, memory: mem ? { ...mem, floorMb: Number.isFinite(floor) ? floor : 6000, promisedMb: promised } : null, heldBy: running.length ? running.map((r) => r.what).join("; ") : holder ? classifyCommand(holder.cmd).label : null, }; }