import { stat, readdir } from "node:fs/promises"; import path from "node:path"; import type { Decision } from "@/lib/decisions"; import { clipBenchHref, playHref, projectHref } from "@/lib/hrefs"; import { projectRefs } from "@/lib/projects"; import { kindMakesTakes } from "@/lib/projects/kinds.mjs"; import { readManifest, reviewOf, walkOrder } from "@/lib/projects/report.mjs"; import type { ProjectRef } from "@/lib/project-types"; import { QUOTE_CHECK_FILE, readQuoteCheck } from "@/lib/report/quoteCheck.mjs"; import { QUICK_CLIP_FOLDER, QUICK_CLIP_TAKE } from "@/lib/report/quickClip.mjs"; import { listTakes, readVerdicts, takesDirOf, verdictsFileOf } from "@/lib/report/takes.mjs"; import { quoteCheckDecisions } from "./checks.mjs"; import { versionsDecisions } from "./versions.mjs"; import { walkDecisions } from "./walk.mjs"; // --------------------------------------------------------------------------- // A REPORT VIDEO'S CALLS THE REDUCER DOES NOT MAKE (umtool redesign L4, "What // needs you"), read from its disk for the Inbox: // // version-unjudged a rendered version (or legacy take) with no verdict // (lib/inbox/versions.mjs) -- Play and judge // clips-unwalked the draft's clips with no walk verdict, on a draft that // is going somewhere (lib/inbox/walk.mjs) -- Walk // quote-check-stale clips the quote check no longer vouches for // (lib/inbox/checks.mjs) -- Run quote-check // // One project that cannot be read adds nothing; nothing here throws. Each // project's rows are kept against a signature of the files they come from // (the manifest, quote-check.json, takes/ and each take's directory, the // verdicts), on globalThis: one cache per process, not per Next bundle. // SERVER ONLY. // --------------------------------------------------------------------------- type Memo = Map; const g = globalThis as typeof globalThis & { __umtoolProjectCallsMemo?: Memo }; const memo: Memo = (g.__umtoolProjectCallsMemo ??= new Map()); const stamp = async (file: string) => { const s = await stat(/* turbopackIgnore: true */ file).catch(() => null); return s ? `${Math.round(s.mtimeMs)}.${s.size}` : "-"; }; async function signatureOf(dir: string): Promise { const takes = takesDirOf(dir); const entries = await readdir(/* turbopackIgnore: true */ takes).catch(() => [] as string[]); const per = await Promise.all(entries.filter((n) => !n.startsWith(".")).sort().map(async (n) => `${n}@${await stamp(path.join(takes, n))}`)); const [man, qc, verdicts] = await Promise.all([stamp(path.join(dir, "video.manifest.json")), stamp(path.join(dir, QUOTE_CHECK_FILE)), stamp(verdictsFileOf(dir))]); // A coarse clock too: versions.mjs and walk.mjs age rows into Quiet at RECENT_DAYS, which no file says. const bucket = Math.floor(Date.now() / (6 * 3600_000)); return [man, qc, verdicts, per.join(","), bucket].join("|"); } /** A quick clip's project (lib/report/quickClip.mjs: `/quick-clips/`): a share, not a piece in the making. */ const isQuickClip = (ref: ProjectRef) => ref.id.startsWith(`${QUICK_CLIP_FOLDER}/`); /** A take a quick clip made: a share, not a version to judge. */ const isQuickClipTake = (ref: ProjectRef, take: { id: string }) => take.id === QUICK_CLIP_TAKE && isQuickClip(ref); async function callsOf(ref: ProjectRef): Promise { // A quick clip is shared, not judged or walked: no calls. if (isQuickClip(ref)) return []; const manifest = (await readManifest(ref.dir)) as Record | null; const title = typeof manifest?.title === "string" && manifest.title ? manifest.title : ref.name; const base = { project: ref.id, projectTitle: title, projectKind: ref.kind }; const [{ takes }, verdicts] = await Promise.all([listTakes(ref.dir), readVerdicts(ref.dir)]); const out: Decision[] = []; out.push( ...(versionsDecisions({ ...base, takes, verdicts, playHref: (take: string) => playHref(ref.id, { take }), skip: (t: Record) => isQuickClipTake(ref, { id: String(t.id) }), }) as Decision[]), ); if (manifest) { const timeline = (manifest.timeline ?? []) as Record[]; const review = reviewOf(manifest); const verdictOf = new Map(review.clips.map((c: { id: string; verdict: string }) => [c.id, c.verdict])); const clips = walkOrder(timeline).map((e: { id: string }) => ({ id: String(e.id), verdict: verdictOf.get(e.id) ?? "unreviewed" })); const rendered = takes.some((t: { previewSize: number | null; version: boolean }) => t.previewSize != null || t.version); const at = await stat(/* turbopackIgnore: true */ path.join(ref.dir, "video.manifest.json")).then((s) => s.mtimeMs, () => 0); out.push(...(walkDecisions({ ...base, clips, rendered, at, benchHref: (clip: string) => clipBenchHref(ref.id, clip) }) as Decision[])); const doc = await readQuoteCheck(ref.dir); const checkedAt = Date.parse(String((doc as { checkedAt?: string }).checkedAt ?? "")) || at; out.push(...(quoteCheckDecisions({ ...base, timeline, doc, href: projectHref(ref.id), at: checkedAt }) as Decision[])); } return out; } /** One report video's calls (the readiness bar's, umtool redesign L7b): the same memo the Inbox reads. Never throws. */ export async function projectCallsOf(ref: ProjectRef): Promise { return projectCalls(ref).catch(() => [] as Decision[]); } async function projectCalls(ref: ProjectRef): Promise { const sig = await signatureOf(ref.dir); const hit = memo.get(ref.id); if (hit && hit.sig === sig) return hit.value; const value = await callsOf(ref).catch(() => [] as Decision[]); memo.set(ref.id, { sig, value }); return value; } // One read at a time: the page and the rail's count ask together, and share it. const gi = globalThis as typeof globalThis & { __umtoolProjectCallsInflight?: Promise | null }; /** Every report video's versions to judge, clips not walked and stale quote checks. Never throws. */ export function projectCallsAll(): Promise { if (gi.__umtoolProjectCallsInflight) return gi.__umtoolProjectCallsInflight; const p = readAll().finally(() => { if (gi.__umtoolProjectCallsInflight === p) gi.__umtoolProjectCallsInflight = null; }); gi.__umtoolProjectCallsInflight = p; return p; } async function readAll(): Promise { try { const refs = (await projectRefs()).filter((r) => kindMakesTakes(r.kind)); const live = new Set(refs.map((r) => r.id)); for (const id of memo.keys()) if (!live.has(id)) memo.delete(id); const per = await Promise.all(refs.map((r) => projectCalls(r).catch(() => [] as Decision[]))); return per.flat(); } catch { return []; } }