commit b97ab2055360f1bb37a3102bc916170a947ab372
parent 3dae2118baf51497aeeec6227f166168464c7b2b
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date: Wed, 14 Oct 2026 00:47:04 -0400
umtool: one project cache for every bundle, cleared by a scaffold — /browse/<new> no longer 404s for a second
Next bundles each route handler and page separately, so the module-level walk
memo was one copy per bundle: invalidateProjects() from an API route never
reached the page rendered next, and /api/projects/new did not call it at all.
The caches now live on globalThis and the scaffold route clears them. Fixes the
browse.spec "a new song gets the shape" flake (recorded since release 24).
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Diffstat:
2 files changed, 24 insertions(+), 6 deletions(-)
diff --git a/umtool/app/api/projects/new/route.ts b/umtool/app/api/projects/new/route.ts
@@ -1,3 +1,4 @@
+import { invalidateProjects } from "@/lib/projects";
import { ScaffoldError, scaffoldProject } from "@/lib/projects/scaffold";
export const dynamic = "force-dynamic";
@@ -14,6 +15,8 @@ export async function POST(request: Request) {
}
try {
const r = await scaffoldProject(body);
+ // The walk is memoised; the page this href opens must see the new project.
+ invalidateProjects();
return Response.json({ ok: true, ...r }, { headers: { "cache-control": "no-store" } });
} catch (e) {
const status = e instanceof ScaffoldError ? e.status : 500;
diff --git a/umtool/lib/projects.ts b/umtool/lib/projects.ts
@@ -57,10 +57,24 @@ export const ALL_DECISION_KINDS: string[] = [
// not changed and is thrown away the moment it has. That is the same rule the
// export build's incremental signatures follow, and it is the reason no index
// is needed yet at twelve projects.
+//
+// The caches live on globalThis, not in module scope: Next bundles each route
+// handler and page separately, so a module-level cache is one copy PER BUNDLE,
+// and invalidateProjects() called from an API route never reached the page that
+// renders next -- a project scaffolded a moment ago 404'd for up to a second.
// ---------------------------------------------------------------------------
-let walkCache: { at: number; refs: ProjectRef[] } | null = null;
-const summaryCache = new Map<string, { sig: string; value: ProjectSummary }>();
-const decisionCache = new Map<string, { sig: string; value: Decision[] }>();
+type ProjectCaches = {
+ walk: { at: number; refs: ProjectRef[] } | null;
+ summary: Map<string, { sig: string; value: ProjectSummary }>;
+ decision: Map<string, { sig: string; value: Decision[] }>;
+};
+const CACHES = ((globalThis as { __umtoolProjectCaches?: ProjectCaches }).__umtoolProjectCaches ??= {
+ walk: null,
+ summary: new Map(),
+ decision: new Map(),
+});
+const summaryCache = CACHES.summary;
+const decisionCache = CACHES.decision;
// ---------------------------------------------------------------------------
// The persistent index, opened once and held.
@@ -84,15 +98,16 @@ export const indexHealth = () => ({ ...lastIndexHits });
/** Drop every cache. The CLI's `scan` and the e2e suite want this. */
export function invalidateProjects(): void {
- walkCache = null;
+ CACHES.walk = null;
summaryCache.clear();
decisionCache.clear();
}
export async function projectRefs(): Promise<ProjectRef[]> {
- if (walkCache && Date.now() - walkCache.at < 1000) return walkCache.refs;
+ const hit = CACHES.walk;
+ if (hit && Date.now() - hit.at < 1000) return hit.refs;
const refs = (await walkProjects(REPORTS_ROOT)) as ProjectRef[];
- walkCache = { at: Date.now(), refs };
+ CACHES.walk = { at: Date.now(), refs };
return refs;
}