2026-09-14 10:26:11 -07:00
|
|
|
import fs from "node:fs/promises";
|
2026-09-15 15:23:24 -07:00
|
|
|
import { appendFileSync } from "node:fs";
|
2026-09-14 10:26:11 -07:00
|
|
|
import path from "node:path";
|
|
|
|
|
import { createHash, randomUUID } from "node:crypto";
|
|
|
|
|
import { authoringContext } from "./authoring-context.js";
|
|
|
|
|
import type { convergeAuthoring } from "./authoring-converge.js";
|
|
|
|
|
import { execFile as callback } from "node:child_process";
|
|
|
|
|
import { promisify } from "node:util";
|
|
|
|
|
import { buildImmutableCandidate, checkerIdentity } from "./checked-build.js";
|
|
|
|
|
import { planEvolution, type WorkspaceRevision, type EvolutionReview } from "../capability-model/index.js";
|
|
|
|
|
|
|
|
|
|
export const checkRecordName = (directory: string) => createHash("sha256").update(directory).digest("hex") + ".json";
|
2026-09-15 15:23:24 -07:00
|
|
|
export async function checkAuthoring(
|
|
|
|
|
start: string,
|
|
|
|
|
output: string,
|
|
|
|
|
options: { baseline?: string; reviews?: string; contractOnly?: boolean } = {},
|
|
|
|
|
) {
|
2026-09-14 20:35:53 -07:00
|
|
|
const started = performance.now();
|
|
|
|
|
const timings: Record<string, number> = {};
|
2026-09-14 10:26:11 -07:00
|
|
|
const context = await authoringContext(start);
|
|
|
|
|
const location = await fs.realpath(start);
|
|
|
|
|
const directory = location === context.workbench ? "root" : path.relative(context.workbench, location);
|
2026-09-15 15:23:24 -07:00
|
|
|
const resource = context.resources.find((entry) => entry.directory === directory);
|
2026-09-14 10:26:11 -07:00
|
|
|
if (!resource) throw new Error("Run check from a registered repository root or the workbench");
|
|
|
|
|
await fs.mkdir(output, { mode: 0o700 });
|
2026-09-15 15:23:24 -07:00
|
|
|
const report: {
|
|
|
|
|
directory: string;
|
|
|
|
|
checker: string;
|
|
|
|
|
candidateOnly: true;
|
|
|
|
|
activationEvidence: false;
|
|
|
|
|
commit?: string;
|
|
|
|
|
artifactPath?: string;
|
|
|
|
|
blockers: string[];
|
|
|
|
|
phase: string;
|
|
|
|
|
output: string;
|
|
|
|
|
compilation?: "passed";
|
2026-09-21 00:57:50 -07:00
|
|
|
runtime?: "execution";
|
2026-09-15 15:23:24 -07:00
|
|
|
activationReadiness?: "preserve" | "migration-required" | "blocked";
|
|
|
|
|
migrationRequired?: string[];
|
|
|
|
|
} = {
|
|
|
|
|
directory,
|
|
|
|
|
checker: checkerIdentity(),
|
|
|
|
|
candidateOnly: true,
|
|
|
|
|
activationEvidence: false,
|
|
|
|
|
blockers: [],
|
|
|
|
|
phase: "convergence",
|
|
|
|
|
output,
|
2026-09-14 10:26:11 -07:00
|
|
|
};
|
2026-09-15 13:45:42 -07:00
|
|
|
const progress = async (running = true) => {
|
2026-09-15 15:23:24 -07:00
|
|
|
const file = path.join(output, "report.json"),
|
|
|
|
|
temp = `${file}.tmp`;
|
|
|
|
|
await fs.writeFile(temp, JSON.stringify({ ...report, timings, running }, null, 2));
|
2026-09-15 13:45:42 -07:00
|
|
|
await fs.rename(temp, file);
|
|
|
|
|
};
|
|
|
|
|
await progress();
|
2026-09-14 10:26:11 -07:00
|
|
|
try {
|
2026-09-14 23:38:07 -07:00
|
|
|
console.error(`[${new Date().toISOString()}] Check: capture source and converge dependencies`);
|
2026-09-14 10:26:11 -07:00
|
|
|
// Serialize only source capture, not the potentially slow Nix build.
|
|
|
|
|
// Repository-scoped agents can check separate immutable candidates in parallel.
|
2026-09-15 13:45:42 -07:00
|
|
|
const capture = promisify(callback)("quixos-qx", ["converge", context.workbench, directory], {
|
2026-09-15 15:23:24 -07:00
|
|
|
maxBuffer: 4 * 1024 * 1024,
|
|
|
|
|
env: { ...process.env, QUIXOS_JJ_NO_CHECKPOINT: "1", QUIXOS_TRACE_CAPTURE: "1" },
|
2026-09-15 13:45:42 -07:00
|
|
|
});
|
|
|
|
|
console.error(`Capture details: ${path.join(output, "capture.log")}`);
|
2026-09-15 15:23:24 -07:00
|
|
|
capture.child.stderr?.on("data", (chunk) => appendFileSync(path.join(output, "capture.log"), chunk));
|
|
|
|
|
const captured = await capture.catch((error) => {
|
|
|
|
|
if (typeof error.stdout === "string" && error.stdout.trim().startsWith("{")) return { stdout: error.stdout };
|
2026-09-14 10:26:11 -07:00
|
|
|
throw error;
|
|
|
|
|
});
|
|
|
|
|
const converged = JSON.parse(captured.stdout) as Awaited<ReturnType<typeof convergeAuthoring>>;
|
2026-09-14 20:35:53 -07:00
|
|
|
timings.captureMs = Math.round(performance.now() - started);
|
2026-09-14 23:38:07 -07:00
|
|
|
console.error(`[${new Date().toISOString()}] Check: source captured in ${(timings.captureMs / 1000).toFixed(1)}s`);
|
2026-09-14 10:26:11 -07:00
|
|
|
if (!converged.candidate) {
|
2026-09-15 15:23:24 -07:00
|
|
|
report.phase = converged.worklist.find((entry) => entry.phase !== "dependency")?.phase ?? "convergence";
|
|
|
|
|
throw new Error(
|
|
|
|
|
converged.worklist.map((entry) => `${entry.directory} [${entry.phase}]: ${entry.message}`).join("\n"),
|
|
|
|
|
);
|
2026-09-14 10:26:11 -07:00
|
|
|
}
|
|
|
|
|
report.commit = converged.candidate.commit;
|
|
|
|
|
report.phase = "verification";
|
2026-09-15 13:45:42 -07:00
|
|
|
await progress();
|
2026-09-14 20:35:53 -07:00
|
|
|
const buildStarted = performance.now();
|
2026-09-15 15:23:24 -07:00
|
|
|
console.error(
|
|
|
|
|
`[${new Date().toISOString()}] Check: immutable Nix ${options.contractOnly ? "contract" : "verification"}; build output: ${path.join(output, "nix.log")}`,
|
|
|
|
|
);
|
2026-09-14 23:38:07 -07:00
|
|
|
try {
|
2026-09-15 15:23:24 -07:00
|
|
|
report.artifactPath = await buildImmutableCandidate(
|
|
|
|
|
converged.candidate,
|
|
|
|
|
resource.kind,
|
|
|
|
|
path.join(output, "nix.log"),
|
|
|
|
|
options.contractOnly,
|
|
|
|
|
);
|
2026-09-14 23:38:07 -07:00
|
|
|
} finally {
|
|
|
|
|
timings.immutableCheckMs = Math.round(performance.now() - buildStarted);
|
2026-09-15 15:23:24 -07:00
|
|
|
console.error(
|
|
|
|
|
`[${new Date().toISOString()}] Check: immutable phase ended after ${(timings.immutableCheckMs / 1000).toFixed(1)}s`,
|
|
|
|
|
);
|
2026-09-14 23:38:07 -07:00
|
|
|
}
|
2026-09-14 10:26:11 -07:00
|
|
|
const candidateText = await fs.readFile(path.join(report.artifactPath, "candidate.json"), "utf8");
|
|
|
|
|
await fs.writeFile(path.join(output, "candidate.json"), candidateText);
|
2026-09-14 15:24:37 -07:00
|
|
|
report.compilation = "passed";
|
2026-09-21 00:57:50 -07:00
|
|
|
try {
|
|
|
|
|
await fs.access(path.join(report.artifactPath, "execution-plan.json"));
|
|
|
|
|
report.runtime = "execution";
|
|
|
|
|
} catch (error) {
|
|
|
|
|
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
|
|
|
|
|
}
|
|
|
|
|
if (resource.kind === "workspace" && !options.contractOnly && report.runtime !== "execution") {
|
2026-09-14 10:26:11 -07:00
|
|
|
report.phase = "evolution";
|
2026-09-15 13:45:42 -07:00
|
|
|
await progress();
|
2026-09-14 10:26:11 -07:00
|
|
|
let baseline = options.baseline;
|
|
|
|
|
if (!baseline) {
|
|
|
|
|
try {
|
|
|
|
|
const host = JSON.parse(await fs.readFile("/etc/quixos/workspace-source.json", "utf8"));
|
2026-09-15 15:23:24 -07:00
|
|
|
if ((await fs.realpath(host.workbenchRoot)) === context.workbench)
|
|
|
|
|
baseline = JSON.parse(
|
|
|
|
|
await fs.readFile(path.join(host.runtimeClosureRoot, "manifest.json"), "utf8"),
|
|
|
|
|
).workspacePlanPath;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
|
|
|
|
|
}
|
2026-09-14 10:26:11 -07:00
|
|
|
}
|
2026-09-15 15:23:24 -07:00
|
|
|
const before = baseline ? (JSON.parse(await fs.readFile(baseline, "utf8")) as WorkspaceRevision) : null;
|
|
|
|
|
const reviews = options.reviews
|
|
|
|
|
? (JSON.parse(await fs.readFile(options.reviews, "utf8")) as EvolutionReview[])
|
|
|
|
|
: [];
|
2026-09-14 10:26:11 -07:00
|
|
|
const evolution = planEvolution(before, JSON.parse(candidateText), { reviews });
|
|
|
|
|
await fs.writeFile(path.join(output, "evolution.json"), JSON.stringify(evolution, null, 2));
|
|
|
|
|
report.blockers.push(...evolution.blockers);
|
2026-09-14 15:24:37 -07:00
|
|
|
report.migrationRequired = evolution.migrationRequired;
|
2026-09-15 15:23:24 -07:00
|
|
|
report.activationReadiness = evolution.blockers.length
|
|
|
|
|
? "blocked"
|
|
|
|
|
: evolution.migrationRequired.length
|
|
|
|
|
? "migration-required"
|
|
|
|
|
: "preserve";
|
|
|
|
|
if (evolution.migrationRequired.length)
|
|
|
|
|
report.blockers.push(
|
|
|
|
|
`Explicit migration required for: ${evolution.migrationRequired.join(", ")}. Compilation passed; supply a migration path before cutover.`,
|
|
|
|
|
);
|
2026-09-14 10:26:11 -07:00
|
|
|
}
|
|
|
|
|
if (!report.blockers.length) report.phase = options.contractOnly ? "contract-only" : "checked";
|
2026-09-15 15:23:24 -07:00
|
|
|
} catch (error) {
|
|
|
|
|
report.blockers.push(String(error instanceof Error ? error.message : error));
|
|
|
|
|
}
|
2026-09-14 20:35:53 -07:00
|
|
|
timings.totalMs = Math.round(performance.now() - started);
|
2026-09-15 15:23:24 -07:00
|
|
|
Object.assign(report, { timings });
|
2026-09-15 13:45:42 -07:00
|
|
|
await progress(false);
|
2026-09-14 10:26:11 -07:00
|
|
|
if (options.contractOnly) return report;
|
|
|
|
|
const records = path.join(context.workbench, ".quixos/checks");
|
|
|
|
|
await fs.mkdir(records, { recursive: true });
|
|
|
|
|
const remember = async (value: typeof report) => {
|
2026-09-15 15:23:24 -07:00
|
|
|
const filename = path.join(records, checkRecordName(value.directory)),
|
|
|
|
|
temporary = `${filename}.${randomUUID()}.tmp`;
|
2026-09-14 10:26:11 -07:00
|
|
|
await fs.writeFile(temporary, JSON.stringify(value, null, 2), { flag: "wx", mode: 0o600 });
|
|
|
|
|
await fs.rename(temporary, filename);
|
|
|
|
|
};
|
|
|
|
|
if (report.artifactPath) {
|
|
|
|
|
const graph = JSON.parse(await fs.readFile(path.join(report.artifactPath, "graph.json"), "utf8"));
|
|
|
|
|
for (const checked of graph.resources) {
|
2026-09-15 15:23:24 -07:00
|
|
|
const managed = context.resources.find(
|
|
|
|
|
(entry) => entry.kind === checked.kind && entry.source?.repository === checked.source.repository,
|
|
|
|
|
);
|
|
|
|
|
if (managed && managed.directory !== directory)
|
|
|
|
|
await remember({
|
|
|
|
|
...report,
|
|
|
|
|
directory: managed.directory,
|
|
|
|
|
commit: checked.source.commit,
|
|
|
|
|
blockers: [],
|
|
|
|
|
phase: "checked",
|
|
|
|
|
});
|
2026-09-14 10:26:11 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
await remember(report);
|
|
|
|
|
return report;
|
|
|
|
|
}
|