Archilyzer · Source

archilyzer

Archilyzer
git clone https://archilyzer.pages.dev/source/archilyzer.git
Log | Files | Refs | README | LICENSE

commit 1b1290699e30d56a04ad8178906d316615a747f7
parent dc679890aee00a5c58ecf4f11f0c66622535c1a8
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date:   Tue, 13 Oct 2026 23:56:30 -0400

umtool: link an article's video by its iterations first, then the manifest, then the slug

An article of the store whose iteration names a projectDir links that project,
and claims it: no other article matches it by slug, so a generated article no
longer lists its own …/i1 as "possible". current.json's video picks the cut; a
projectDir outside REPORTS_ROOT is listed as unreachable, never dropped. Only
the slug step is ever ambiguous. clearLinksCache bumps an epoch a memo can
compare.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

Diffstat:
Mumtool/lib/articles/links.mjs | 171++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------
Aumtool/lib/articles/links.test.mjs | 110+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mumtool/lib/articles/sites.ts | 34++++++++++++++++++++++++++++++----
3 files changed, 296 insertions(+), 19 deletions(-)

diff --git a/umtool/lib/articles/links.mjs b/umtool/lib/articles/links.mjs @@ -1,23 +1,38 @@ // Which umtool report-video project is an article's video. // -// Two ways, in order: +// Three ways, in order (plans/umtool-redesign.md, "Linking a project to an +// article"): // -// 1. The manifest says so: a top-level `"article": "<site>/<report>"` in +// 1. The article's iterations made it: an article of the store +// (`articles/<id>`) whose `iterations/<n>/iteration.json` names a +// `projectDir`. That project is linked, and it is CLAIMED -- no other +// article can match it by slug. `current.json`'s `video` picks the one +// the article's Cut is. A `projectDir` outside REPORTS_ROOT is not +// dropped: it is listed as `unreachable` (not reachable here). +// 2. The manifest says so: a top-level `"article": "<site>/<report>"` in // video.manifest.json. build-video.mjs never reads top-level keys it does // not know (it reads `generatedBy` no more than this), so the key costs // the render nothing. A manifest that names an article is linked to it // and to nothing else. -// 2. The slug matches: the manifest's `slug` (else the project directory's +// 3. The slug matches: the manifest's `slug` (else the project directory's // name) is the report id, `polemic-<id>`, or the id without `polemic-` -- // and the project lives in the same workspace as the article's draft // (lib/articles/sources.mjs). A UNIQUE match is linked; two or more are -// only "possible", and the page says so rather than picking one. +// only "possible", and the page says so rather than picking one. Only +// this step is ever ambiguous. A project an iteration claimed (1) or +// whose manifest names an article (2) is never a slug match: before (1), +// every generated article listed its own `…/i1` as "possible". +// +// The iterations are read here as plain JSON (two keys: `projectDir`, and +// `current.json`'s `video`) rather than through common's reader, so `umtool +// notes` -- plain ESM -- links the same way the app does. Every write of +// those files is common's (lib/report/iterations.ts); this only reads. // // Plain ESM, so `umtool notes` can name an article's video too. -import { readFile } from "node:fs/promises"; +import { readdir, readFile, realpath } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; -import { REPORTS_ROOT } from "../paths.mjs"; +import { ARTICLES_DIR, ARTICLES_KEY, REPORTS_ROOT } from "../paths.mjs"; import { projectRefs } from "../projects/core.mjs"; import { kindLinksArticles } from "../projects/kinds.mjs"; import { idKeys } from "./sources.mjs"; @@ -25,6 +40,11 @@ import { idKeys } from "./sources.mjs"; const CACHE_MS = 30_000; /** @type {Map<string, { at: number, value: Promise<any[]> }>} */ const cache = new Map(); +/** @type {Map<string, { at: number, value: Promise<Map<string, any[]>> }>} */ +const iterCache = new Map(); +// Bumped by every clearLinksCache(), so a memo built from these reads (the +// pieces list, lib/pieces.ts) is dropped by the same writes that drop them. +let epoch = 0; /** `~/x` back to an absolute path. */ export function untildify(p) { @@ -70,31 +90,152 @@ export function videoProjects(reportsRoot = REPORTS_ROOT) { export function clearLinksCache() { cache.clear(); + iterCache.clear(); + epoch += 1; +} + +/** How many times the links cache has been cleared: a memo built on it compares this. */ +export const linksCacheEpoch = () => epoch; + +const NUMERIC = /^[1-9][0-9]*$/; + +async function readJson(file) { + try { + return JSON.parse(await readFile(/* turbopackIgnore: true */ file, "utf8")); + } catch { + return null; + } +} + +/** + * Every iteration of the article store that names a video project, by article: + * `Map<article, [{ n, dir, current, reachable, projectId }]>`, newest first. + * + * `reachable` is whether `dir` is under REPORTS_ROOT (realpaths, so a linked + * root still matches); `projectId` is its path relative to the root, which is + * the walk's project id when a project is there. + * + * @param {{ articlesDir?: string, reportsRoot?: string }} [opts] + */ +export function iterationProjects({ articlesDir = ARTICLES_DIR, reportsRoot = REPORTS_ROOT } = {}) { + const key = `${articlesDir}\0${reportsRoot}`; + const hit = iterCache.get(key); + if (hit && Date.now() - hit.at < CACHE_MS) return hit.value; + const value = readIterationProjects(articlesDir, reportsRoot); + iterCache.set(key, { at: Date.now(), value }); + value.catch(() => iterCache.delete(key)); + return value; +} + +/** `dir` relative to `root` as a POSIX path, or null when it is not strictly under it. */ +function under(root, dir) { + const rel = path.relative(/* turbopackIgnore: true */ root, dir); + if (!rel || rel.startsWith("..") || path.isAbsolute(rel)) return null; + return rel.split(path.sep).join("/"); +} + +async function readIterationProjects(articlesDir, reportsRoot) { + const out = new Map(); + const lexRoot = path.resolve(/* turbopackIgnore: true */ reportsRoot); + const root = await realpath(/* turbopackIgnore: true */ lexRoot).catch(() => lexRoot); + for (const article of await readdir(/* turbopackIgnore: true */ articlesDir).catch(() => [])) { + if (article.startsWith(".")) continue; + const itDir = path.join(/* turbopackIgnore: true */ articlesDir, article, "iterations"); + const names = (await readdir(/* turbopackIgnore: true */ itDir).catch(() => [])).filter((n) => NUMERIC.test(n)); + if (!names.length) continue; + const current = await readJson(path.join(/* turbopackIgnore: true */ itDir, "current.json")); + const video = Number.isInteger(current?.video) ? current.video : null; + const list = []; + for (const name of names) { + const it = await readJson(path.join(/* turbopackIgnore: true */ itDir, name, "iteration.json")); + const raw = it && typeof it.projectDir === "string" && it.projectDir.trim() ? it.projectDir.trim() : null; + if (!raw) continue; + const n = Number(name); + const dir = path.resolve(/* turbopackIgnore: true */ untildify(raw)); + // Lexically first (the walk's ids are lexical: a linked folder inside the + // root keeps its name), then by realpath (a root reached through a link). + let rel = under(lexRoot, dir); + if (rel === null) rel = under(root, await realpath(/* turbopackIgnore: true */ dir).catch(() => dir)); + list.push({ n, dir, current: video === n, reachable: rel !== null, projectId: rel }); + } + if (list.length) out.set(article, list.sort((a, b) => b.n - a.n)); + } + return out; } const inside = (root, p) => p === root || p.startsWith(root + path.sep); /** - * The projects linked to one article: `{ linked, possible, how }`. + * The projects linked to one article: + * `{ linked, possible, how, cut, unreachable }`. + * + * `linked` rows carry `via`: "iteration" (1), "manifest" (2) or "slug" (3). + * `cut` is the linked project the article's Cut tab opens: the iteration + * `current.video` names, else the first linked. `unreachable` lists the + * iterations whose project is outside REPORTS_ROOT (`{ n, dir }`) -- shown as + * "not reachable here", never dropped. * * @param {string} siteId * @param {string} reportId - * @param {{ reportsRoot?: string, workspace?: string | null, projects?: any[] }} [opts] + * @param {{ reportsRoot?: string, articlesDir?: string, workspace?: string | null, projects?: any[], iterations?: Map<string, any[]> }} [opts] * `workspace` is the article's workspace dir (sourceFor's, `~` allowed); without * one, slug matches are only ever "possible". */ -export async function linkedProjects(siteId, reportId, { reportsRoot = REPORTS_ROOT, workspace = null, projects } = {}) { +export async function linkedProjects( + siteId, + reportId, + { reportsRoot = REPORTS_ROOT, articlesDir = ARTICLES_DIR, workspace = null, projects, iterations } = {}, +) { const all = projects ?? (await videoProjects(reportsRoot)); + const its = iterations ?? (await iterationProjects({ articlesDir, reportsRoot })); const key = `${siteId}/${reportId}`; - const declared = all.filter((p) => p.article === key); - if (declared.length) return { linked: declared, possible: [], how: "manifest names the article" }; + const byId = new Map(all.map((p) => [p.id, p])); + + // (1) the article's own iterations. + const mine = siteId === ARTICLES_KEY ? (its.get(reportId) ?? []) : []; + const unreachable = mine.filter((x) => !x.reachable).map((x) => ({ n: x.n, dir: x.dir })); + const viaIteration = []; + for (const x of mine) { + const p = x.projectId ? byId.get(x.projectId) : null; + if (p && !viaIteration.includes(p)) viaIteration.push(p); + } + // (2) a manifest that names the article. + const declared = all.filter((p) => p.article === key && !viaIteration.includes(p)); + const linked = [...viaIteration.map((p) => ({ ...p, via: "iteration" })), ...declared.map((p) => ({ ...p, via: "manifest" }))]; + if (linked.length) { + const currentId = mine.find((x) => x.current)?.projectId ?? null; + const cut = linked.find((p) => p.id === currentId) ?? linked[0]; + const how = [ + viaIteration.length ? `${viaIteration.length} made by the article's iterations` : null, + declared.length ? "manifest names the article" : null, + ] + .filter(Boolean) + .join("; "); + return { linked, possible: [], how, cut: cut.id, unreachable }; + } + // (3) the slug. Never a project an iteration claimed or a manifest gave away. + const claimed = new Set(); + for (const list of its.values()) for (const x of list) if (x.projectId) claimed.add(x.projectId); const keys = idKeys(reportId); - // A manifest that names a DIFFERENT article is never a slug match. - const bySlug = all.filter((p) => !p.article && keys.has(p.slug)); + const bySlug = all.filter((p) => !p.article && !claimed.has(p.id) && keys.has(p.slug)); const ws = workspace ? untildify(workspace) : null; const inWs = ws ? bySlug.filter((p) => inside(ws, p.dir)) : []; - if (inWs.length === 1) return { linked: inWs, possible: [], how: `slug ${inWs[0].slug} in the article's workspace` }; + if (inWs.length === 1) { + return { + linked: [{ ...inWs[0], via: "slug" }], + possible: [], + how: `slug ${inWs[0].slug} in the article's workspace`, + cut: inWs[0].id, + unreachable, + }; + } const possible = inWs.length > 1 ? inWs : bySlug; - return { linked: [], possible, how: possible.length ? `${possible.length} project(s) share the slug` : "no project" }; + return { + linked: [], + possible, + how: possible.length ? `${possible.length} project(s) share the slug` : unreachable.length ? "not reachable here" : "no project", + cut: null, + unreachable, + }; } diff --git a/umtool/lib/articles/links.test.mjs b/umtool/lib/articles/links.test.mjs @@ -0,0 +1,110 @@ +// Linking a video project to an article, in the plan's order: the article's +// iterations, then the manifest's `article`, then the slug -- and only the +// slug is ever ambiguous. +import assert from "node:assert/strict"; +import { mkdir, mkdtemp, symlink, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import path from "node:path"; +import test from "node:test"; +import { clearLinksCache, iterationProjects, linkedProjects, linksCacheEpoch } from "./links.mjs"; + +async function put(file, value) { + await mkdir(path.dirname(file), { recursive: true }); + await writeFile(file, typeof value === "string" ? value : JSON.stringify(value)); +} + +const manifest = (slug, extra = {}) => ({ slug, title: slug, timeline: [], ...extra }); +const iteration = (article, n, projectDir) => ({ format: "archilyzer-iteration", version: 1, article, n, ...(projectDir ? { projectDir } : {}) }); + +async function fixture() { + const base = await mkdtemp(path.join(tmpdir(), "umtool-links-")); + const reports = path.join(base, "reports"); + const articles = path.join(base, "articles"); + // (1) zeta's iterations made two projects; a third is on another machine. + await put(path.join(reports, "articles/zeta/i1/video.manifest.json"), manifest("zeta")); + await put(path.join(reports, "articles/zeta/i3/video.manifest.json"), manifest("zeta")); + await put(path.join(articles, "zeta/iterations/1/iteration.json"), iteration("zeta", 1, path.join(reports, "articles/zeta/i1"))); + await put(path.join(articles, "zeta/iterations/2/iteration.json"), iteration("zeta", 2, null)); + await put(path.join(articles, "zeta/iterations/3/iteration.json"), iteration("zeta", 3, path.join(reports, "articles/zeta/i3"))); + await put(path.join(articles, "zeta/iterations/4/iteration.json"), iteration("zeta", 4, "/elsewhere/reports/articles/zeta/i4")); + await put(path.join(articles, "zeta/iterations/current.json"), { format: "archilyzer-iterations-current", version: 1, report: 1, reportFrom: 1, video: 3 }); + // (2) a hand-made project whose manifest names zeta. + await put(path.join(reports, "hand/zeta-cut/video.manifest.json"), manifest("zeta", { article: "articles/zeta" })); + // (3) slugs: one in a workspace, one outside it; two that only share a slug. + await put(path.join(reports, "ws/polemic-alpha/video.manifest.json"), manifest("polemic-alpha")); + await put(path.join(reports, "elsewhere/alpha/video.manifest.json"), manifest("alpha")); + await put(path.join(reports, "one/beta/video.manifest.json"), manifest("beta")); + await put(path.join(reports, "two/beta/video.manifest.json"), manifest("beta")); + clearLinksCache(); + return { base, reports, articles, o: { reportsRoot: reports, articlesDir: articles } }; +} + +test("(1) an article's iterations link their projects; current.video is the cut; a far one is unreachable", async () => { + const { o } = await fixture(); + const r = await linkedProjects("articles", "zeta", o); + assert.deepEqual( + r.linked.map((p) => [p.id, p.via]), + [ + ["articles/zeta/i3", "iteration"], + ["articles/zeta/i1", "iteration"], + ["hand/zeta-cut", "manifest"], + ], + ); + assert.equal(r.cut, "articles/zeta/i3"); + assert.deepEqual(r.possible, []); + assert.deepEqual(r.unreachable, [{ n: 4, dir: "/elsewhere/reports/articles/zeta/i4" }]); + assert.match(r.how, /2 made by the article's iterations; manifest names the article/); +}); + +test("(3) a claimed project is never another article's slug match", async () => { + const { o } = await fixture(); + // A site's own `zeta` shares the slug with zeta's i1 and i3 and with the + // hand-made cut. Before (1) claimed them, each was "possible" here -- and the + // store's zeta listed its own i1 as possible too. + const r = await linkedProjects("priv", "zeta", o); + assert.deepEqual(r.linked, []); + assert.deepEqual(r.possible, []); + assert.equal(r.cut, null); + assert.deepEqual(r.unreachable, []); +}); + +test("(3) a unique slug in the article's workspace links; without one it is only possible", async () => { + const { reports, o } = await fixture(); + const inWs = await linkedProjects("priv", "polemic-alpha", { ...o, workspace: path.join(reports, "ws") }); + assert.deepEqual(inWs.linked.map((p) => [p.id, p.via]), [["ws/polemic-alpha", "slug"]]); + assert.equal(inWs.cut, "ws/polemic-alpha"); + const noWs = await linkedProjects("priv", "polemic-alpha", o); + assert.deepEqual(noWs.linked, []); + assert.deepEqual(noWs.possible.map((p) => p.id).sort(), ["elsewhere/alpha", "ws/polemic-alpha"]); +}); + +test("(3) two projects sharing a slug are possible, never picked", async () => { + const { o } = await fixture(); + const r = await linkedProjects("priv", "beta", o); + assert.deepEqual(r.linked, []); + assert.deepEqual(r.possible.map((p) => p.id).sort(), ["one/beta", "two/beta"]); +}); + +test("iteration dirs: a root reached through a link still matches; far dirs are kept, not dropped", async () => { + const { base, reports, articles } = await fixture(); + const alias = path.join(base, "alias"); + await symlink(reports, alias); + await put(path.join(articles, "kappa/iterations/1/iteration.json"), iteration("kappa", 1, path.join(alias, "articles/zeta/i1"))); + clearLinksCache(); + const its = await iterationProjects({ articlesDir: articles, reportsRoot: reports }); + assert.deepEqual(its.get("kappa"), [{ n: 1, dir: path.join(alias, "articles/zeta/i1"), current: false, reachable: true, projectId: "articles/zeta/i1" }]); + assert.deepEqual( + its.get("zeta").map((x) => [x.n, x.reachable, x.projectId, x.current]), + [ + [4, false, null, false], + [3, true, "articles/zeta/i3", true], + [1, true, "articles/zeta/i1", false], + ], + ); +}); + +test("clearing the cache bumps the epoch a memo compares", () => { + const before = linksCacheEpoch(); + clearLinksCache(); + assert.equal(linksCacheEpoch(), before + 1); +}); diff --git a/umtool/lib/articles/sites.ts b/umtool/lib/articles/sites.ts @@ -47,7 +47,13 @@ export function reportDirFor(site: string, report: string): string { export type ArticleStatus = "published" | "draft"; -export type ProjectLinkRow = { id: string; title: string; slug: string }; +export type ProjectLinkRow = { + id: string; + title: string; + slug: string; + /** How a linked project is linked (lib/articles/links.mjs): its iterations, its manifest, or its slug. */ + via?: "iteration" | "manifest" | "slug"; +}; export type ArticleRow = { site: string; @@ -71,7 +77,16 @@ export type ArticleRow = { problem: string | null; problems: number; source: { draft?: string; generator?: string; how?: string; workspace?: string } | null; - projects: { linked: ProjectLinkRow[]; possible: ProjectLinkRow[] }; + projects: { + linked: ProjectLinkRow[]; + possible: ProjectLinkRow[]; + /** The linked project the Cut opens (current.video's), else null. */ + cut?: string | null; + /** In words: how the links were made. */ + how?: string; + /** Iterations whose project is outside REPORTS_ROOT: not reachable here. */ + unreachable?: { n: number; dir: string }[]; + }; }; export type SiteRow = { @@ -132,7 +147,12 @@ async function articleRow(siteId: string, id: string, published: Set<string>, pr const dir = reportDirFor(siteId, id); const r = read.report; const links = await linkedProjects(siteId, id, { workspace: source?.workspace ?? null, projects }); - const row = (p: { id: string; title: string; slug: string }) => ({ id: p.id, title: p.title, slug: p.slug }); + const row = (p: { id: string; title: string; slug: string; via?: ProjectLinkRow["via"] }): ProjectLinkRow => ({ + id: p.id, + title: p.title, + slug: p.slug, + ...(p.via ? { via: p.via } : {}), + }); return { site: siteId, id, @@ -151,7 +171,13 @@ async function articleRow(siteId: string, id: string, published: Set<string>, pr problem: read.problems[0]?.message ?? null, problems: read.problems.length, source, - projects: { linked: links.linked.map(row), possible: links.possible.map(row) }, + projects: { + linked: links.linked.map(row), + possible: links.possible.map(row), + cut: links.cut ?? null, + how: links.how, + unreachable: links.unreachable ?? [], + }, ...(siteId === ARTICLES_KEY ? { audience: articleAudience(r) } : {}), }; }