// Which corpus a generation reads, and which origin it cites on (release 27, // S5; the stopgap's local_sweep.pick_build and site_origin). // // The corpus is a composed site build (`//out`, read // with the MCP's `local:` source): the newest build holding every requested // channel, a public one before a `-private` one (a public build is fresher // and carries its site's origin); a named build must hold them all. // // The citation origin is the build's site's `siteUrl` (a `-private` build's // public twin's), else the fallback the caller gives (settings). import { readdir, readFile, stat } from "node:fs/promises"; import path from "node:path"; export type CorpusBuild = { build: string; dir: string; channels: string[]; mtimeMs: number }; export class NoCorpusBuildError extends Error {} async function readBuild(exportBuildsDir: string, build: string): Promise { const dir = path.join(exportBuildsDir, build, "out"); const file = path.join(dir, "corpus.json"); try { const [text, st] = await Promise.all([readFile(file, "utf8"), stat(file)]); const corpus = JSON.parse(text) as { channels?: { slug?: unknown }[] }; const channels = (corpus.channels ?? []) .map((c) => (c && typeof c === "object" ? c.slug : null)) .filter((s): s is string => typeof s === "string"); return { build, dir, channels, mtimeMs: st.mtimeMs }; } catch { return null; } } export async function listCorpusBuilds(exportBuildsDir: string): Promise { let names: string[]; try { names = (await readdir(exportBuildsDir, { withFileTypes: true })).filter((e) => e.isDirectory()).map((e) => e.name); } catch { return []; } const out: CorpusBuild[] = []; for (const n of names) { const b = await readBuild(exportBuildsDir, n); if (b) out.push(b); } return out; } export async function pickCorpusBuild( channels: readonly string[], o: { exportBuildsDir: string; want?: string }, ): Promise { const builds = await listCorpusBuilds(o.exportBuildsDir); if (o.want) { const b = builds.find((x) => x.build === o.want); if (!b) { throw new NoCorpusBuildError( `no composed build "${o.want}" (have: ${builds.map((x) => x.build).sort().join(", ") || "none"})`, ); } const missing = channels.filter((c) => !b.channels.includes(c)); if (missing.length) throw new NoCorpusBuildError(`build ${o.want} does not hold ${missing.join(", ")}`); return b; } const holding = builds.filter((b) => channels.every((c) => b.channels.includes(c))); if (!holding.length) { throw new NoCorpusBuildError( `no composed build holds all of ${channels.join(", ") || "(no channels)"} — publish build the site that carries ` + "them (a private site's build for private channels), or name a build", ); } holding.sort((a, b) => Number(a.build.endsWith("-private")) - Number(b.build.endsWith("-private")) || b.mtimeMs - a.mtimeMs); return holding[0]!; } // The origin a build's links cite: its site's siteUrl, or for `-private` // the public site's; else null. export async function siteOriginOf(build: string, sitesDir: string): Promise { for (const id of [build, build.replace(/-private$/, "")]) { try { const site = JSON.parse(await readFile(path.join(sitesDir, id, "site.json"), "utf8")) as { siteUrl?: unknown }; if (typeof site.siteUrl === "string" && /^https?:\/\//.test(site.siteUrl)) return site.siteUrl.replace(/\/+$/, ""); } catch { // no such site } } return null; }