import assert from "node:assert/strict"; import { mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync, } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import { afterEach, describe, it } from "vitest"; import { applyInitialization } from "../init.ts"; import { CORE_BYTE_LIMIT, composeSystemPrompt, preflightFileMutation, preflightShellMutation, } from "../system.ts"; const roots: string[] = []; const root = () => { const value = mkdtempSync(path.join(tmpdir(), "the-system-runtime-")); roots.push(value); mkdirSync(path.join(value, ".git")); applyInitialization(value, "PX"); for (const name of ["SYSTEM.md", "MISSION.md", "RULES.md"]) write(value, `.system/${name}`, `# ${name}\n\nApproved.\n`); return value; }; const write = (root: string, relative: string, content: string) => { const target = path.join(root, relative); mkdirSync(path.dirname(target), { recursive: true }); writeFileSync(target, content, "utf8"); }; const documentSource = (metadata: Record) => `---\n${Object.entries(metadata) .map( ([key, value]) => `${key}: ${Array.isArray(value) ? `[${value.join(", ")}]` : value}`, ) .join("\n")}\n---\n\n# Contract\n\nApproved.\n`; const writeSpec = (project: string) => { const relative = ".system/specs/PX-SPEC-001-contract/index.md"; write(project, relative, documentSource({ id: "PX-SPEC-001", type: "spec" })); return relative; }; const approval = (hasUI = true) => ({ hasUI, select: async (_title: string, items: string[]) => items[0], }); afterEach(() => { for (const value of roots.splice(0)) rmSync(value, { recursive: true, force: true }); }); describe("runtime core injection", () => { it("injects cores and reports malformed core paths", () => { const project = root(); write(project, ".system/MISSION.md", "x".repeat(CORE_BYTE_LIMIT + 1)); assert.match(composeSystemPrompt(project, "BASE"), /MISSION\.md exceeds/); rmSync(path.join(project, ".system/SYSTEM.md")); mkdirSync(path.join(project, ".system/SYSTEM.md")); assert.match(composeSystemPrompt(project, "BASE"), /governance error/); }); }); describe("runtime bundle governance", () => { it("allows valid new index candidates and blocks invalid layouts and metadata before approval", async () => { const project = root(); const valid = await preflightFileMutation( project, "write", { path: ".system/specs/PX-SPEC-001-contract/index.md", content: documentSource({ id: "PX-SPEC-001", type: "spec" }), }, approval(), ); assert.equal(valid?.block, undefined); for (const input of [ { path: ".system/specs/PX-SPEC-002-broken/index.md", content: "# no frontmatter", }, { path: ".system/specs/PX-SPEC-002-broken.md", content: "# loose document", }, { path: ".system/specs/loose.md", content: "x" }, { path: ".system/specs/PX-SPEC-002-broken/other/index.md", content: "x" }, ]) { const result = await preflightFileMutation( project, "write", input, approval(), ); assert.equal(result?.block, true, input.path); assert.match(result?.reason ?? "", /bundle|candidate validation/i); } }); it("requires a valid index before assets and approves every existing spec file", async () => { const project = root(); const asset = ".system/specs/PX-SPEC-001-contract/evidence/data.csv"; assert.equal( ( await preflightFileMutation( project, "write", { path: asset, content: "a,b" }, approval(), ) )?.block, true, ); const index = writeSpec(project); assert.equal( ( await preflightFileMutation( project, "write", { path: asset, content: "a,b" }, approval(), ) )?.block, undefined, ); const nestedIndex = await preflightFileMutation( project, "write", { path: ".system/specs/PX-SPEC-001-contract/other/index.md", content: documentSource({ id: "PX-SPEC-001", type: "spec" }), }, approval(), ); assert.equal(nestedIndex?.block, true); assert.match(nestedIndex?.reason ?? "", /document bundles/); for (const relative of [ index, ".system/specs/PX-SPEC-001-contract/mockup-mobile.html", ".system/specs/PX-SPEC-001-contract/diagram.svg", asset, ]) { write( project, relative, relative === index ? documentSource({ id: "PX-SPEC-001", type: "spec" }) : "existing", ); let approvals = 0; const result = await preflightFileMutation( project, "write", { path: relative, content: relative === index ? documentSource({ id: "PX-SPEC-001", type: "spec" }) : "replacement", }, { hasUI: true, select: async (_title, items) => { approvals += 1; return items[0]; }, }, ); assert.equal(result?.block, undefined, relative); assert.equal(approvals, 1, relative); } const closed = await preflightFileMutation( project, "write", { path: asset, content: "replacement" }, approval(false), ); assert.equal(closed?.block, true); assert.match(closed?.reason ?? "", /interactive UI/); }); it("validates existing non-spec indexes without ownership approval and rejects symlinks", async () => { const project = root(); writeSpec(project); for (const [relative, metadata] of [ [ ".system/roadmaps/PX-ROADMAP-001-delivery/index.md", { id: "PX-ROADMAP-001", type: "roadmap", spec: "PX-SPEC-001" }, ], [ ".system/plans/PX-PLAN-001-delivery/index.md", { id: "PX-PLAN-001", type: "plan", spec: "PX-SPEC-001" }, ], [ ".system/issues/PX-ISSUE-001-delivery/index.md", { id: "PX-ISSUE-001", type: "issue", status: "open", specs: ["PX-SPEC-001"], }, ], [ ".system/research/PX-RESEARCH-001-delivery/index.md", { id: "PX-RESEARCH-001", type: "research", capturedAt: "2026-07-16" }, ], ] as const) { write(project, relative, documentSource(metadata)); const result = await preflightFileMutation( project, "edit", { path: relative, edits: [{ oldText: "Approved.", newText: "Reviewed." }], }, { hasUI: true, select: async () => { throw new Error("unexpected approval"); }, }, ); assert.equal(result?.block, undefined, relative); } symlinkSync( "missing.md", path.join(project, ".system/specs/PX-SPEC-002-link"), ); const linked = await preflightFileMutation( project, "write", { path: ".system/specs/PX-SPEC-002-link/index.md", content: documentSource({ id: "PX-SPEC-002", type: "spec" }), }, approval(), ); assert.equal(linked?.block, true); assert.match(linked?.reason ?? "", /symbolic link/i); }); it("validates computed edits before approval", async () => { const project = root(); const relative = writeSpec(project); const result = await preflightFileMutation( project, "edit", { path: relative, edits: [{ oldText: "id: PX-SPEC-001", newText: "id: PX-SPEC-002" }], }, approval(), ); assert.equal(result?.block, true); assert.match(result?.reason ?? "", /candidate validation/i); }); }); describe("runtime shell advisory guard", () => { it.each([ "printf replacement > .system/RULES.md", "sed -i 's/Approved/Changed/' .system/specs/PX-SPEC-001-contract/index.md", "rm .system/specs/PX-SPEC-001-contract/evidence/data.csv", "rm -rf .system/specs/PX-SPEC-001-contract", ])("blocks recognizable owned mutation: %s", (command) => { const result = preflightShellMutation(command); assert.equal(result?.block, true); assert.match(result?.reason ?? "", /not a sandbox/); }); it.each([ "cat .system/RULES.md", "printf report > build/report.txt", "git status --short", ])("allows safe shell command: %s", (command) => { assert.equal(preflightShellMutation(command), undefined); }); });