mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 17:47:39 +01:00
* fix(build): keep fs out of the browser bundle — the exit-contract matcher gets a Node-free home loki#900 made activity-status.ts import EXIT_CONTRACT_PATTERN from agent-config.ts. activity-status is shared with client components (the Activity feed strips the contract from a dispatch before showing it), and agent-config imports `fs` — so `fs` landed in the browser bundle and `next build` failed: "Module not found: Can't resolve 'fs'". Four deploys of main were refused; production kept serving the previous release, but nothing merged after #900 could ship either. `verify` could not see it: CI deliberately does not run `next build`, so a client/server boundary break only surfaces at deploy. The heading and the matcher move to src/lib/exit-contract.ts, which imports nothing. agent-config builds the full contract from that heading; the client and the lane prompt read the matcher from the Node-free module. Proved by the step that failed: `pnpm run build` against a schema-only Postgres, exactly as the deploy runs it. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016YJVAanfj8keVSXyZnTemh * test(build): catch a Node builtin in the browser bundle before the deploy does verify does not run `next build`, so a client/server boundary break only surfaces when the deploy builds — after the PR has merged. That is how #900 reached main and blocked every deploy. client-bundle-node-free.ts walks the client graph the way Turbopack does, without building: from every "use client" module through static value imports, stopping at "use server" modules (RPC stubs on the client), and fails on any builtin Next does not polyfill for the browser. Proved against the real break: with #900's activity-status.ts it reports exactly the two errors the deploy printed — `fs` in agent-config.ts and in session-paths.ts via activity-status.ts <- project-intent-panel.tsx — and on this branch it reports none. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016YJVAanfj8keVSXyZnTemh * style: format --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
146 lines
4.8 KiB
TypeScript
146 lines
4.8 KiB
TypeScript
/**
|
|
* Nothing in the browser bundle imports a Node builtin Next cannot polyfill.
|
|
*
|
|
* WHY THIS EXISTS
|
|
* ---------------
|
|
* `verify` does not run `next build` (it needs a database and minutes), so a
|
|
* client/server boundary break is invisible until the DEPLOY builds — after
|
|
* the PR has merged. On 2026-09-25 loki#900 made activity-status.ts (shared
|
|
* with client components) import from agent-config.ts, which imports `fs`.
|
|
* Turbopack failed with "Module not found: Can't resolve 'fs'"; four deploys
|
|
* of main were refused, and nothing merged after it could ship.
|
|
*
|
|
* HOW
|
|
* ---
|
|
* The same walk Turbopack does for the client graph, without building: start
|
|
* at every "use client" module, follow static imports (`import type` is
|
|
* erased, so skipped), and stop at "use server" modules — a client reaches
|
|
* those only as RPC stubs. Any module reached that imports a builtin Next does
|
|
* not polyfill for the browser is a build failure waiting for the deploy.
|
|
* `path`, `os`, `crypto`, `util` and friends ARE polyfilled and are allowed.
|
|
*
|
|
* Proved against the real break: with #900's activity-status.ts restored this
|
|
* reports exactly the two errors the deploy printed — `fs` in agent-config.ts
|
|
* and in session-paths.ts, via activity-status.ts ← project-intent-panel.tsx.
|
|
*
|
|
* Run: npx tsx scripts/test/client-bundle-node-free.ts
|
|
*/
|
|
import { readFileSync, existsSync, statSync, readdirSync } from "node:fs";
|
|
import { join, dirname, resolve } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), "../..");
|
|
const SRC = join(ROOT, "src");
|
|
|
|
/** Builtins Next.js does not polyfill for the browser. */
|
|
const UNPOLYFILLED = new Set([
|
|
"fs",
|
|
"fs/promises",
|
|
"child_process",
|
|
"net",
|
|
"tls",
|
|
"dns",
|
|
"worker_threads",
|
|
"module",
|
|
"readline",
|
|
"cluster",
|
|
"dgram",
|
|
"v8",
|
|
"perf_hooks",
|
|
"inspector",
|
|
"async_hooks",
|
|
"http2",
|
|
"repl",
|
|
]);
|
|
|
|
function sourceFiles(dir: string, out: string[] = []): string[] {
|
|
for (const name of readdirSync(dir)) {
|
|
const p = join(dir, name);
|
|
if (statSync(p).isDirectory()) sourceFiles(p, out);
|
|
else if (/\.(ts|tsx)$/.test(p)) out.push(p);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
function resolveImport(from: string, spec: string): string | null {
|
|
let base: string;
|
|
if (spec.startsWith("@/")) base = join(SRC, spec.slice(2));
|
|
else if (spec.startsWith(".")) base = resolve(dirname(from), spec);
|
|
else return null; // a package: not ours to walk
|
|
for (const candidate of [
|
|
base,
|
|
`${base}.ts`,
|
|
`${base}.tsx`,
|
|
join(base, "index.ts"),
|
|
join(base, "index.tsx"),
|
|
]) {
|
|
if (existsSync(candidate) && statSync(candidate).isFile()) return candidate;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
const codeCache = new Map<string, string>();
|
|
function code(file: string): string {
|
|
let c = codeCache.get(file);
|
|
if (c === undefined) {
|
|
c = readFileSync(file, "utf8")
|
|
.replace(/\/\*[\s\S]*?\*\//g, "")
|
|
.replace(/^\s*\/\/.*$/gm, "");
|
|
codeCache.set(file, c);
|
|
}
|
|
return c;
|
|
}
|
|
|
|
/** Static value imports and re-exports; `import type` / `export type` are erased. */
|
|
function importsOf(file: string): string[] {
|
|
const out: string[] = [];
|
|
const re = /^\s*(?:import|export)\s+(type\s+)?(?:[^'"]*?\sfrom\s+)?["']([^"']+)["']/gm;
|
|
let m: RegExpExecArray | null;
|
|
while ((m = re.exec(code(file)))) if (!m[1]) out.push(m[2]!);
|
|
return out;
|
|
}
|
|
|
|
const directive = (file: string, name: string) =>
|
|
new RegExp(`^\\s*["']${name}["']`).test(readFileSync(file, "utf8"));
|
|
|
|
const clientRoots = sourceFiles(SRC).filter((f) => directive(f, "use client"));
|
|
const parent = new Map<string, string | null>(clientRoots.map((f) => [f, null]));
|
|
const queue = [...clientRoots];
|
|
const violations: { file: string; builtin: string }[] = [];
|
|
|
|
while (queue.length) {
|
|
const file = queue.shift()!;
|
|
for (const spec of importsOf(file)) {
|
|
const bare = spec.replace(/^node:/, "");
|
|
if (UNPOLYFILLED.has(bare)) {
|
|
violations.push({ file, builtin: spec });
|
|
continue;
|
|
}
|
|
const next = resolveImport(file, spec);
|
|
if (!next || parent.has(next) || directive(next, "use server")) continue;
|
|
parent.set(next, file);
|
|
queue.push(next);
|
|
}
|
|
}
|
|
|
|
const rel = (p: string) => p.slice(ROOT.length + 1);
|
|
if (violations.length) {
|
|
console.error(
|
|
`✗ ${violations.length} Node builtin(s) reach the browser bundle — \`next build\` will fail at deploy:`,
|
|
);
|
|
for (const v of violations) {
|
|
const chain: string[] = [];
|
|
for (let f: string | null | undefined = v.file; f && chain.length < 8; f = parent.get(f)) {
|
|
chain.push(rel(f));
|
|
}
|
|
console.error(` '${v.builtin}' in ${chain.join(" ← ")}`);
|
|
}
|
|
console.error(
|
|
"\nFix: move what the client needs into a module with no Node imports (see src/lib/exit-contract.ts).",
|
|
);
|
|
process.exit(1);
|
|
}
|
|
console.log(
|
|
`✓ client bundle is Node-free: ${clientRoots.length} client modules, ${parent.size} reachable, 0 unpolyfilled builtins`,
|
|
);
|