Files
loki/scripts/test/parallel-run-prompt.ts
CatoandClaude Opus 5.5 c88dc22cdf fix(build): keep fs out of the browser bundle, and catch that class in verify (main cannot deploy since #900) (#904)
* 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>
2026-09-25 03:01:32 -07:00

121 lines
4.8 KiB
TypeScript

/**
* A parallel lane's prompt names exactly ONE handoff file — its own.
*
* THE TRAP THIS LOCKS DOWN
* ------------------------
* Every dispatch ends with an exit contract telling the agent which session
* file to write its handoff to, and the run closes when that file reports
* ready — so the path in the contract IS the run's identity to the close path.
*
* A prompt assembled by inject-core (Implement, /loki, Control) already ends
* with a contract for the BASE tab (`~/.loki/sessions/loki.md`). The old
* parallel code, written for the one route whose prompt had no contract yet,
* APPENDED a second one. Fed an inject-core prompt it would have told the
* agent to write both files — and a ready handoff on the base file closes
* whichever run owns the base tab, not this one.
*
* Also a ratchet: the contract is built in ONE place (exitContractFor). Three
* call sites used to type the heading out by hand, which is how a replacement
* could never have been exact.
*
* Run: npx tsx scripts/test/parallel-run-prompt.ts
*/
import { readFileSync, readdirSync, statSync } from "node:fs";
import { join, dirname, resolve as resolvePath } from "node:path";
import { fileURLToPath } from "node:url";
import { promptForLane } from "@/lib/orchestration/lane-prompt";
import { exitContractFor } from "@/lib/agent-config";
import { EXIT_CONTRACT_PATTERN } from "@/lib/exit-contract";
import { deriveRunTab } from "@/lib/run-tab";
function assert(condition: unknown, message: string): asserts condition {
if (!condition) throw new Error(message);
}
let passed = 0;
function check(name: string, fn: () => void) {
fn();
passed++;
console.log(` ✓ ${name}`);
}
const BASE_FILE = "~/.loki/sessions/loki.md";
const lane = deriveRunTab("loki", "db093fd4-3d68-4740-9b2d-3ee8b6d55c1b");
const LANE_FILE = `~/.loki/sessions/${lane}.md`;
const contracts = (s: string) => (s.match(/^##[ \t]*Exit contract\b/gim) ?? []).length;
// The shape inject-core produces: preamble, context, the task, then the
// contract for the BASE tab, last.
const injectCorePrompt = [
"# Loki operator dispatch",
"Everything in this message is assembled by Loki's dispatch pipeline.",
"",
"## The operator's goals & deadlines",
"- ship the feedback inbox clamp",
"",
"Work on the project at /home/g/dev/loki.",
"Clamp a report body to three lines with a more toggle.",
"",
exitContractFor(BASE_FILE),
].join("\n");
check("an inject-core prompt keeps one contract, pointed at the lane's own file", () => {
const out = promptForLane(injectCorePrompt, lane);
assert(contracts(out) === 1, `expected exactly one exit contract, found ${contracts(out)}`);
assert(out.includes(LANE_FILE), `the contract must name the lane's file ${LANE_FILE}`);
assert(
!out.includes(BASE_FILE),
"the base tab's file must be gone — a ready handoff there closes the wrong run",
);
});
check("the task and its context survive the rewrite", () => {
const out = promptForLane(injectCorePrompt, lane);
assert(out.includes("Clamp a report body to three lines"), "the task was lost");
assert(out.includes("## The operator's goals & deadlines"), "the context was lost");
assert(
out.trimEnd().endsWith(exitContractFor(LANE_FILE).trimEnd()),
"the contract must stay last",
);
});
check("a prompt with no contract yet gets exactly one", () => {
const out = promptForLane("Work on the project at /x.\nDo the thing.", lane);
assert(contracts(out) === 1, `expected one exit contract, found ${contracts(out)}`);
assert(out.includes(LANE_FILE), "it must name the lane's file");
});
check("rewriting twice is the same as rewriting once", () => {
const once = promptForLane(injectCorePrompt, lane);
assert(promptForLane(once, lane) === once, "promptForLane must be idempotent");
});
check("the shared matcher finds the contract every builder emits", () => {
assert(
EXIT_CONTRACT_PATTERN.test(`body\n\n${exitContractFor(BASE_FILE)}`),
"EXIT_CONTRACT_PATTERN must match exitContractFor's output",
);
});
check("the exit contract is built in one place", () => {
const root = resolvePath(dirname(fileURLToPath(import.meta.url)), "../../src");
const files: string[] = [];
(function walk(dir: string) {
for (const name of readdirSync(dir)) {
const p = join(dir, name);
if (statSync(p).isDirectory()) walk(p);
else if (/\.tsx?$/.test(p)) files.push(p);
}
})(root);
const code = (p: string) =>
readFileSync(p, "utf8")
.replace(/\/\*[\s\S]*?\*\//g, "")
.replace(/^\s*\/\/.*$/gm, "");
const typed = files.filter((p) => code(p).includes("## Exit contract (operator requirement)"));
assert(
typed.length === 1 && typed[0]!.endsWith("src/lib/exit-contract.ts"),
`the contract heading must be typed only in exit-contract.ts (EXIT_CONTRACT_HEADING); found in: ${typed.join(", ")}`,
);
});
console.log(`\nparallel-run-prompt: ${passed} passed`);