// A PIECE's lifecycle: active, parked or done (plans/umtool-redesign.md, // "Lifecycle"). // // REPORTS_ROOT/.umtool/lifecycle.json // { format: "umtool-lifecycle", version: 1, // pieces: { "": { state, at, by? } } } // // STATE, NOT CACHE: it is the operator's word on a piece, so it lives beside // the projects (the walk skips dot-dirs, so it is never a project) and not in // CACHE_DIR. A piece with no entry is `active`. // // "Looks done" signals (a song's every cut shipped, a final mp4 plus a // `share-*` batch, a published article with its video) only ever SUGGEST: they // become a `looks-done` decision (lib/inbox.ts) and never write here. Only // the operator's POST /api/pieces/lifecycle does. // // One writer, serialised in-process, and every write is a whole file to a temp // name and a rename. A file that does not parse is never overwritten: a write // refuses (LifecycleUnreadable) until somebody fixes it; a read treats it as // "nothing set" and says so in `error`. // // Plain ESM, so a CLI and a unit test read it the way the app does. import { mkdir, readFile, rename, rm, writeFile } from "node:fs/promises"; import path from "node:path"; import { isPieceId } from "./pieces/ids.mjs"; import { tmpPathFor } from "yt-dlp-transcript-common/lib/atomicFile.mjs"; export const LIFECYCLE_STATES = ["active", "parked", "done"]; export const LIFECYCLE_FORMAT = "umtool-lifecycle"; export const LIFECYCLE_VERSION = 1; export class LifecycleError extends Error { constructor(message, status = 400) { super(message); this.name = "LifecycleError"; this.status = status; } } /** The file under a reports root. */ export function lifecycleFile(reportsRoot) { return path.join(/* turbopackIgnore: true */ reportsRoot, ".umtool", "lifecycle.json"); } const isState = (s) => LIFECYCLE_STATES.includes(s); /** * Every entry: `{ pieces: { [id]: { state, at, by? } }, error? }`. A missing * file is empty; an unreadable one is empty with `error`. Entries that are not * a piece id or a state are skipped, never fatal. * * @param {string} reportsRoot */ export async function readLifecycle(reportsRoot) { let text; try { text = await readFile(/* turbopackIgnore: true */ lifecycleFile(reportsRoot), "utf8"); } catch (err) { if (err?.code === "ENOENT") return { pieces: {} }; return { pieces: {}, error: `lifecycle.json cannot be read: ${err?.message ?? err}` }; } let raw; try { raw = JSON.parse(text); } catch (err) { return { pieces: {}, error: `lifecycle.json is not JSON: ${err.message}` }; } if (!raw || typeof raw !== "object" || raw.format !== LIFECYCLE_FORMAT || typeof raw.pieces !== "object" || !raw.pieces) { return { pieces: {}, error: "lifecycle.json is not a umtool-lifecycle file" }; } const pieces = {}; for (const [id, e] of Object.entries(raw.pieces)) { if (!isPieceId(id) || !e || typeof e !== "object" || !isState(e.state)) continue; pieces[id] = { state: e.state, at: typeof e.at === "string" ? e.at : "", ...(typeof e.by === "string" ? { by: e.by } : {}) }; } return { pieces }; } /** A piece's state in a read: its entry's, else `active`. */ export function lifecycleOf(read, id) { return read?.pieces?.[id]?.state ?? "active"; } let chain = Promise.resolve(); /** * Set one piece's state. Answers the entry written. * * @param {string} reportsRoot * @param {string} id a piece id (lib/pieces/ids.mjs) * @param {string} state active | parked | done * @param {{ by?: string, now?: Date }} [opts] */ export function setLifecycle(reportsRoot, id, state, { by, now } = {}) { if (!isPieceId(id)) return Promise.reject(new LifecycleError(`not a piece id: ${JSON.stringify(id)}`)); if (!isState(state)) return Promise.reject(new LifecycleError(`state is ${LIFECYCLE_STATES.join(" | ")}, not ${JSON.stringify(state)}`)); const run = chain.then(() => write(reportsRoot, id, state, by, now ?? new Date())); // The next write waits for this one, whether it failed or not. chain = run.catch(() => {}); return run; } async function write(reportsRoot, id, state, by, now) { const file = lifecycleFile(reportsRoot); const read = await readLifecycle(reportsRoot); if (read.error) throw new LifecycleError(`${read.error}; fix or remove ${file} first`, 409); const entry = { state, at: now.toISOString(), ...(by ? { by } : {}) }; const doc = { format: LIFECYCLE_FORMAT, version: LIFECYCLE_VERSION, pieces: Object.fromEntries(Object.entries({ ...read.pieces, [id]: entry }).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))), }; await mkdir(/* turbopackIgnore: true */ path.dirname(file), { recursive: true }); const tmp = tmpPathFor(file); try { await writeFile(/* turbopackIgnore: true */ tmp, `${JSON.stringify(doc, null, 2)}\n`); await rename(/* turbopackIgnore: true */ tmp, file); } catch (err) { await rm(/* turbopackIgnore: true */ tmp, { force: true }).catch(() => {}); throw err; } return { id, ...entry }; }