import { stat } from "node:fs/promises"; import path from "node:path"; import type { Decision } from "@/lib/decisions"; import { projectHref } from "@/lib/hrefs"; import { kindMakesTakes } from "@/lib/projects/kinds.mjs"; import { projectRefs } from "@/lib/projects"; import { SOURCE_WATCH_FILE, backlogWords, readSourceWatch } from "@/lib/report/sourceBacklog.mjs"; import { readBacklog } from "@/lib/report/sourceBacklog"; // --------------------------------------------------------------------------- // "RE-SWEEP WHEN COMPLETE" (UI notes item 16), the Inbox's half -- one kind: // // sources-complete a report video's source watch (/source-watch.json, // lib/report/sourceBacklog.mjs) has fired: every channel the // cut cites has nothing left to download or transcribe. Review, // action Re-sweep (the project's page, the backlog line); // gone when the operator clears or re-arms the watch. // // The check is the refresh check's pattern (lib/inbox/refresh.ts): the Inbox // only READS the watch files; an armed watch whose last read is over an hour // old is read again in the background (the editor's snapshot counts, one // project at a time, never twice at once), in the app's server only and not // where UMTOOL_SOURCE_WATCH=0. The project page's own read records too, so // opening the project fires it as well. SERVER-ONLY. // --------------------------------------------------------------------------- export const SOURCES_COMPLETE = "sources-complete"; export const SOURCE_WATCH_EVERY_MS = 60 * 60 * 1000; type Watched = { id: string; dir: string; title: string; watch: { slugs: string[]; armedAt: string; firedAt?: string; fired?: Record[] }; checkedAt: string | null }; async function watched(): Promise { const refs = (await projectRefs()).filter((p) => kindMakesTakes(p.kind)); const out: Watched[] = []; for (const p of refs) { const has = await stat(path.join(/* turbopackIgnore: true */ p.dir, SOURCE_WATCH_FILE)).then(() => true, () => false); if (!has) continue; const { doc } = await readSourceWatch(p.dir); if (!doc.watch) continue; out.push({ id: p.id, dir: p.dir, title: p.name, watch: doc.watch, checkedAt: doc.last?.checkedAt ?? null }); } return out; } const g = globalThis as typeof globalThis & { __umtoolSourceWatch?: Promise | null }; /** Read the stale armed watches' backlogs in the background (a page never waits on it). */ export function kickSourceWatches(list: Watched[], now = Date.now()): void { if (g.__umtoolSourceWatch) return; const stale = list.filter((w) => !w.watch.firedAt && (!w.checkedAt || now - Date.parse(w.checkedAt) >= SOURCE_WATCH_EVERY_MS)); if (!stale.length) return; g.__umtoolSourceWatch = (async () => { for (const w of stale) await readBacklog(w.dir).catch(() => null); })() .catch(() => null) .finally(() => { g.__umtoolSourceWatch = null; }); } /** The Inbox's `sources-complete` cards (and the stale watches read in the background). */ export async function sourceWatchDecisions(o: { background?: boolean } = {}): Promise { const list = await watched(); if (o.background !== false && process.env.NEXT_RUNTIME && process.env.UMTOOL_SOURCE_WATCH !== "0") kickSourceWatches(list); return list .filter((w) => w.watch.firedAt) .map( (w) => ({ project: w.id, projectTitle: w.title, piece: `p:${w.id}`, kind: SOURCES_COMPLETE, target: w.watch.slugs.join(", "), why: `the backlog is done — ${(w.watch.fired ?? []).map((r) => backlogWords(r)).join("; ") || w.watch.slugs.join(", ")}: re-sweep the cut's sources`, href: projectHref(w.id, { tab: "overview" }), severity: "open", at: Date.parse(w.watch.firedAt!) || 0, }) as Decision, ); }