// A LARGE REPORTS ROOT, AND HOW MANY TIMES IT IS WALKED (the regression S4 shipped with, 17 Nov 2026: the live server // pegged a core with hundreds of walks of the reports root in flight at once, and never answered `/`). A reports root // of 300 projects (30 folders of 10, and folders with no project beneath) and a queue store of 124 OLD take steps // (done and failed), no reads batch anywhere. Each surface's walks are counted (lib/projects/walk.mjs walkStats) and // must be few and bounded, run ONE at a time, and return. Run with: pnpm --filter umtool exec tsx --test // lib/inbox/walkLoad.test.ts import { after, before, test } from "node:test"; import assert from "node:assert/strict"; import { mkdtempSync } from "node:fs"; import { mkdir, rm, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; // The environment BEFORE any module that reads lib/paths.mjs (a static import would be hoisted and read ~/reports). const tmp = mkdtempSync(path.join(os.tmpdir(), "umtool-walk-load-")); const ROOT = path.join(tmp, "reports"); process.env.REPORTS_DIR = ROOT; process.env.CHANNELS_DIR = path.join(tmp, "channels"); process.env.SITES_DIR = path.join(tmp, "sites"); process.env.ARTICLES_DIR = path.join(tmp, "articles"); process.env.UMTOOL_QUEUE_DIR = path.join(ROOT, ".umtool-queue"); process.env.ARCHILYZER_EDITOR_URL = "http://127.0.0.1:9"; process.env.WORKER_TOKEN = ""; process.env.UMTOOL_URL = "http://127.0.0.1:9"; process.env.UMTOOL_XJAR_MEMO_MS = "0"; const QUEUE = path.join(ROOT, ".umtool-queue"); const { walkStats } = await import("../projects/walk.mjs"); const { PROJECT_KINDS } = await import("../projects/kinds.mjs"); const KIND = (PROJECT_KINDS.find((k: { makesTakes?: boolean }) => k.makesTakes) as { id: string }).id; const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms)); /** The walks `fn` starts, and the most that ran at once while it did. */ async function walksOf(fn: () => Promise): Promise<{ walks: number; atOnce: number; ms: number }> { const c0 = walkStats.calls; walkStats.maxInFlight = walkStats.inFlight; const t0 = Date.now(); await fn(); return { walks: walkStats.calls - c0, atOnce: walkStats.maxInFlight, ms: Date.now() - t0 }; } /** Every memo expired: what a server in its second second sees. */ const expired = () => sleep(1100); before(async () => { for (let g = 0; g < 30; g++) { for (let i = 0; i < 10; i++) { const d = path.join(ROOT, `group-${g}`, `proj-${i}`); await mkdir(path.join(d, "out"), { recursive: true }); await writeFile(path.join(d, "project.json"), JSON.stringify({ kind: KIND, title: `Load ${g}-${i}` })); await writeFile(path.join(d, "video.manifest.json"), JSON.stringify({ schemaVersion: 1, slug: `proj-${i}`, title: `Load ${g}-${i}`, render: { width: 1920, height: 1080, fps: 30 }, timeline: [] })); } await mkdir(path.join(ROOT, `group-${g}`, "scratch", "a", "b"), { recursive: true }); } await mkdir(path.join(QUEUE, "steps"), { recursive: true }); for (let n = 0; n < 124; n++) { const p = `group-${n % 30}/proj-${n % 10}`; await writeFile(path.join(QUEUE, "steps", `q${n + 1}.json`), JSON.stringify({ qid: `q${n + 1}`, seq: n + 1, label: `take ${p} --id rough-1 --preview`, argv: ["take", p, "--id", "rough-1", "--preview"], cwd: null, after: null, target: { project: p, take: "rough-1" }, state: n % 3 ? "done" : "failed", createdAt: "2026-11-01T00:00:00.000Z", by: "umtool queue add", startedAt: "2026-11-01T00:00:01.000Z", endedAt: "2026-11-01T00:10:00.000Z", exitCode: n % 3 ? 0 : 1, signal: null, pid: 1, pidStart: null, host: os.hostname(), runnerPid: 1, runnerStart: null, jobId: null, attempts: 1, error: n % 3 ? null : "the render failed", blockedBy: null, cancelRequested: null, timeoutMin: 720, })); } }); after(() => rm(tmp, { recursive: true, force: true })); test("THE REGRESSION: 300 inbox cards looked up at once with the memo expired -- one walk, not 300; 50 misses -- one", async () => { const { projectRefs } = await import("../projects"); const { playHrefOfPiece } = await import("../../components/projects/playable"); await projectRefs(); await expired(); let r = await walksOf(() => Promise.all(Array.from({ length: 300 }, (_, n) => playHrefOfPiece(`p:group-${n % 30}/proj-${n % 10}`)))); assert.deepEqual([r.walks, r.atOnce], [1, 1], JSON.stringify(r)); await expired(); r = await walksOf(() => Promise.all(Array.from({ length: 50 }, (_, n) => playHrefOfPiece(`p:gone-${n}`)))); assert.ok(r.walks <= 2 && r.atOnce === 1, JSON.stringify(r)); // The page's way: the projects walked once for every card, handed down. await expired(); r = await walksOf(async () => { const refs = await projectRefs(); await Promise.all(Array.from({ length: 300 }, (_, n) => playHrefOfPiece(`p:group-${n % 30}/proj-${n % 10}`, null, refs))); }); assert.deepEqual([r.walks, r.atOnce], [1, 1]); }); test("the home page's data (the inbox, the rail's counts, the cards' Play): a few walks, one at a time", async () => { const { readInbox } = await import("../inbox"); const { railCounts } = await import("../rail"); const { projectRefs } = await import("../projects"); const { playHrefOfPiece } = await import("../../components/projects/playable"); await expired(); const r = await walksOf(async () => { const inbox = await readInbox({}); await railCounts(); const refs = await projectRefs(); const cards = inbox.groups.flatMap((g: { cards: Array<{ piece: string | null }> }) => g.cards); await Promise.all(cards.map((c) => playHrefOfPiece(c.piece, null, refs))); }); console.log(`home: ${r.walks} walk(s), ${r.ms} ms`); assert.equal(r.atOnce, 1, "never two walks at once"); assert.ok(r.walks <= 8, `home walks ${r.walks}`); }); test("a project page's data (its path resolved, its readiness): at most 2 walks", async () => { const { resolveProjectPath } = await import("../projects"); const { readinessOf } = await import("./readiness"); await expired(); const r = await walksOf(async () => { const got = await resolveProjectPath(["group-7", "proj-3"]); assert.ok(got && "project" in got && got.project); await readinessOf(got.project); }); assert.ok(r.walks <= 2 && r.atOnce === 1, JSON.stringify(r)); }); test("the reads route's GET, one queue runner tick, the bench's 409 check: no walk; the report routes: one walk shared by 20 requests", async () => { const { projectRef } = await import("../projects"); const { readsGet } = await import("../report/readsApi.mjs"); const { queueTick } = await import("../queue/store.mjs"); const { rereadRunningFor } = await import("../report/readsQueue.mjs"); const { resolveReport } = await import("../report/serve.mjs"); const project = await projectRef("group-3/proj-4"); assert.ok(project); let r = await walksOf(() => readsGet(project, new URLSearchParams(), { reportsRoot: ROOT, queueDir: QUEUE, runner: null })); assert.equal(r.walks, 0, "reads GET"); // The runner's tick, its executor as lib/queue/runner.ts's (its describe resolves a failed step's project). r = await walksOf(() => queueTick(QUEUE, { busy: () => null, start: () => ({ pid: null, jobId: null, cancel: () => {} }), describe: async (s: { target?: { project?: string } }) => (await projectRef(String(s.target?.project ?? "")), null) } as never, { held: new Map() })); assert.equal(r.walks, 0, "a runner tick"); r = await walksOf(() => rereadRunningFor(project, "p-1", { queueDir: QUEUE })); assert.equal(r.walks, 0, "the 409 check"); await expired(); r = await walksOf(() => Promise.all(Array.from({ length: 20 }, () => resolveReport("group-3/proj-4")))); assert.deepEqual([r.walks, r.atOnce], [1, 1], "20 report requests at once: one walk"); r = await walksOf(() => Promise.all(Array.from({ length: 20 }, (_, n) => resolveReport(`group-3/gone-${n}`)))); assert.ok(r.walks <= 1 && r.atOnce <= 1, `20 misses: ${JSON.stringify(r)}`); });