import { withEditNotes } from "@/lib/report/guard"; import { StaleToken, manifestToken, updateLinkPolicy } from "@/lib/report/manifest.mjs"; import { resolveReport } from "@/lib/report/serve.mjs"; import { refuseCaller } from "@/lib/report/caller"; import { linkPolicyOf } from "@/lib/report/linkPolicy.mjs"; import { sitesByOrigin } from "@/lib/report/siteDeploys.mjs"; import { SITES_DIR } from "@/lib/paths.mjs"; export const dynamic = "force-dynamic"; // THE PROJECT'S LINK POLICY (UI notes 2026-10-30, #10; lib/report/linkPolicy.mjs): // "cite via site X; these channels direct", `provenance.linkPolicy`. // // GET ?project= the policy, the cut's channels (with their clip counts), // the sites a policy may cite via (SITES_DIR, each with a // siteUrl), provenance.siteOrigin, the token // PUT { project, token, policy: { via?, direct? } | null } set it (null clears) // // Setting it rewrites no clip: what breaks it is the check's clip-link-policy // rows, each with its one-press fix through the window writer; new clips are // written by it (the inserters). `umtool link-policy

…` is the same writer. const noStore = { "cache-control": "no-store" }; export async function GET(request: Request) { const url = new URL(request.url); const r = await resolveReport(url.searchParams.get("project") ?? ""); if ("error" in r) return Response.json({ error: r.error }, { status: r.status }); const m = r.manifest as { provenance?: Record; timeline?: { type?: string; channel?: string }[] }; const counts = new Map(); const fallback = typeof m.provenance?.channelSlug === "string" ? m.provenance.channelSlug : null; for (const e of m.timeline ?? []) { if (e?.type !== "clip") continue; const c = e.channel ?? fallback; if (c) counts.set(c, (counts.get(c) ?? 0) + 1); } return Response.json( { policy: linkPolicyOf(m), siteOrigin: typeof m.provenance?.siteOrigin === "string" ? m.provenance.siteOrigin : null, channels: [...counts].map(([slug, clips]) => ({ slug, clips })).sort((a, b) => b.clips - a.clips || a.slug.localeCompare(b.slug)), sites: [...sitesByOrigin(SITES_DIR)].map(([origin, s]) => ({ origin, siteId: s.siteId, title: s.title })), token: await manifestToken(r.project.dir), }, { headers: noStore }, ); } export async function PUT(request: Request) { const refused = refuseCaller(request, "/api/report/link-policy"); if (refused) return refused; let body: Record; try { body = await request.json(); } catch { return Response.json({ error: "expected JSON" }, { status: 400 }); } if (!("policy" in body)) return Response.json({ error: "policy is required — { via?, direct? }, or null to clear it" }, { status: 400 }); const r = await resolveReport(String(body.project ?? "")); if ("error" in r) return Response.json({ error: r.error }, { status: r.status }); try { const { result, editNotes } = await withEditNotes(r.project, () => updateLinkPolicy(r.project.dir, body.policy ?? null, { token: typeof body.token === "string" ? body.token : null }), ); return Response.json({ ok: true, ...result, editNotes }, { headers: noStore }); } catch (e) { if (e instanceof StaleToken) { return Response.json({ error: e.message, expected: e.expected, got: e.got, stale: true }, { status: 409 }); } return Response.json({ error: e instanceof Error ? e.message : String(e) }, { status: 400 }); } }