import { revalidatePath } from "next/cache"; import { getWorkerPool } from "yt-dlp-transcript-common/jobs/workerPool"; import { isGateHeld, withGateHeld } from "yt-dlp-transcript-common/lib/pauseGates"; import { isLocalEditor, readRenderPriority, reapRenderMode, renderModeOff, renderModeOn, renderPriorityFile, type RenderModeLanes, type RenderPriorityState, } from "yt-dlp-transcript-common/lib/renderPriority.mjs"; import { getSettings } from "yt-dlp-transcript-common/lib/settings"; import { saveSettings } from "../settings/saveSettings"; // RENDER MODE, the editor's side (common/lib/renderPriority.mjs; AGENTS.md // "Render mode"). `pnpm ops render-mode` turns it on and off here, /jobs shows // it, and the heartbeat ends a hold that is no longer one — the render's // process gone, its window over — by releasing the lanes IT held. // // The lanes are held in-process, the way the lane buttons hold them // (operations/actions.ts setLaneHeld): the transcription pool first (its live // hold), then the stored gate. Not through that server action itself: the // heartbeat is no request, and its revalidatePath has none to revalidate. // // The state file is the one queue-lock reads: beside the heavy slot's lock in // this checkout's git common dir (the editor runs from editor/), or // RENDER_PRIORITY_FILE — the e2e servers set their own. type RenderLane = "transcription" | "download" | "backfill"; export function renderModeFile(): string { return renderPriorityFile(process.env, { root: process.cwd() }); } // WHICH EDITOR'S LANES. A hold records the editor its lanes were held through: // umtool its ARCHILYZER_EDITOR_URL, this editor its own loopback URL. Another // worktree's dev editor shares the state file (one common dir for every // worktree) but not the lanes, so it reaps only a hold that names its own // port — never the live editor's. A hold made through a URL that is not a // loopback one is reaped by `umtool render-mode status|on`, not by a heartbeat. const ownPort = () => process.env.EDITOR_PORT || process.env.PORT || "3001"; const selfUrl = () => `http://localhost:${ownPort()}`; const owns = (s: RenderPriorityState) => isLocalEditor(s.editor, ownPort()); async function setHeld(lane: RenderLane, held: boolean): Promise { if (lane === "transcription") { if (held) getWorkerPool().pauseAll(); else getWorkerPool().resumeAll(); } const cur = getSettings(); if (isGateHeld(cur, lane) !== held) { await saveSettings({ autoQueue: withGateHeld(cur, lane, held).autoQueue }); } try { revalidatePath("/jobs"); revalidatePath("/workers"); revalidatePath("/operations/[id]", "page"); } catch { /* outside a request (the heartbeat): nothing to revalidate */ } } /** The editor's own lanes, for renderModeOn / renderModeOff. */ export function editorRenderLanes(): RenderModeLanes { return { async isHeld(lane) { const l = lane as RenderLane; return isGateHeld(getSettings(), l) || (l === "transcription" && getWorkerPool().isPaused()); }, hold: (lane) => setHeld(lane as RenderLane, true), release: (lane) => setHeld(lane as RenderLane, false), }; } /** Render mode as it stands; a stale hold is ended first (its lanes released). */ export async function renderModeNow() { const file = renderModeFile(); const reaped = await reapRenderMode({ file, lanes: editorRenderLanes(), owns }).catch(() => null); return { ...readRenderPriority(file), reaped: reaped?.was ? reaped : null, file }; } /** On, as a manual window: `pnpm ops render-mode {"on": true, "for"?, "note"?}`. */ export async function renderModeOnHere(o: { forMs: number | null; note: string | null; by: string }) { const file = renderModeFile(); return renderModeOn({ file, holder: null, forMs: o.forMs, note: o.note, by: o.by, editor: selfUrl(), lanes: editorRenderLanes() }); } /** Off, whoever turned it on (the caller checked a running take's hold). */ export async function renderModeOffHere() { return renderModeOff({ file: renderModeFile(), lanes: editorRenderLanes() }); } /** * The heartbeat's look: a stale hold's lanes released, its file removed. Never * throws. A live hold is left alone. */ export async function renderModeReapTick(): Promise { try { await reapRenderMode({ file: renderModeFile(), lanes: editorRenderLanes(), owns }); } catch { /* the next beat looks again */ } }