Load checked installations independently of the running compiler (WI-16)

Keep compiler-dependent checks in Nix builds; load stored plans and programs with original provenance and runtime artifact validation. Allow an identical executable world to receive a new activation epoch after a toolchain rebuild.

Compiler-update/altered-program regression, coordinator and full sandboxed integration checks pass; lint and formatting pass. Default platform/source wiring remains the next task.
This commit is contained in:
Timothy J. Aveni
2026-09-21 22:01:47 -07:00
parent e5544dde21
commit 60b44a6637
5 changed files with 87 additions and 50 deletions
+24 -17
View File
@@ -13,25 +13,32 @@ for (const check of checks) {
if (Object.hasOwn(roots, check.packageRevisionId)) throw Error("Duplicate checked package revision"); if (Object.hasOwn(roots, check.packageRevisionId)) throw Error("Duplicate checked package revision");
roots[check.packageRevisionId] = check.artifactPath; roots[check.packageRevisionId] = check.artifactPath;
} }
const checked = await compiler.loadCheckedArtifactWorld( const generator = createHash("sha256")
plan, .update(await readFile(compilerPath))
roots, .digest("hex");
createHash("sha256") // These compiler-dependent checks belong to the Nix build, not runtime recovery.
.update(await readFile(compilerPath)) const candidate = JSON.parse(await readFile(join(output, "candidate.json"), "utf8"));
.digest("hex"), const { executionContracts: _legacyContracts, ...workspace } = candidate;
); if (compiler.hexDigest(compiler.compileExecutionPlan(workspace)) !== compiler.hexDigest(plan))
const installation = await compiler.loadExecutionInstallation( throw Error("CANDIDATE_PLAN_MISMATCH: build plan differs from source compilation");
plan, const checked = await compiler.loadCheckedArtifactWorld(plan, roots, generator);
roots, const installation = await compiler.loadExecutionInstallation(plan, roots);
createHash("sha256") const kernel = compiler.compileKernelProgram(plan),
.update(await readFile(compilerPath)) queries = compiler.compileQueryProgram(plan);
.digest("hex"),
);
for (const [file, value] of [ for (const [file, value] of [
["execution-world.json", checked.world], ["execution-world.json", checked.world],
["installation.json", installation], ["installation.json", installation],
["kernel.json", compiler.compileKernelProgram(plan)], ["kernel.json", kernel],
["queries.json", compiler.compileQueryProgram(plan)], ["queries.json", queries],
["execution-artifacts.json", { codec: "camino-checked-execution-v1", roots, manifests: checked.manifests }], [
"execution-artifacts.json",
{
codec: "camino-checked-execution-v2",
generator,
roots,
manifests: checked.manifests,
programs: { kernel: compiler.hexDigest(kernel), queries: compiler.hexDigest(queries) },
},
],
]) ])
await writeFile(join(output, file), JSON.stringify(value) + "\n"); await writeFile(join(output, file), JSON.stringify(value) + "\n");
+38 -26
View File
@@ -1,11 +1,8 @@
import { open, lstat } from "node:fs/promises"; import { open, lstat } from "node:fs/promises";
import { constants } from "node:fs"; import { constants } from "node:fs";
import { join } from "node:path"; import { join } from "node:path";
import type { WorkspaceRevision } from "../capability-model/types.js"; import { hexDigest, ExecutionPlanError, type ExecutionPlan } from "./plan.js";
import { compileExecutionPlan, hexDigest, ExecutionPlanError, type ExecutionPlan } from "./plan.js";
import { loadExecutionInstallation, type PackageInstallationSelection } from "./installation.js"; import { loadExecutionInstallation, type PackageInstallationSelection } from "./installation.js";
import { compileKernelProgram } from "./kernel.js";
import { compileQueryProgram } from "./queries.js";
const rootPattern = /^\/nix\/store\/[a-z0-9]{32}-[^/\s]+$/; const rootPattern = /^\/nix\/store\/[a-z0-9]{32}-[^/\s]+$/;
async function readJson(root: string, file: string): Promise<any> { async function readJson(root: string, file: string): Promise<any> {
const handle = await open(join(root, file), constants.O_RDONLY | constants.O_NOFOLLOW); const handle = await open(join(root, file), constants.O_RDONLY | constants.O_NOFOLLOW);
@@ -17,29 +14,35 @@ async function readJson(root: string, file: string): Promise<any> {
await handle.close(); await handle.close();
} }
} }
/** Revalidate the normal checked-candidate output. This is the shared artifact /** Load the checked Nix output, preserving its original compilation and provenance.
* loader, not a second managed-only executable manifest or an activation grant. */ * The selected toolchain validates generated code during the build. This loader
* checks stored identities and artifact/runtime contracts, never recompiles the
* historical candidate with whichever compiler happens to run the host. */
export async function loadCheckedInstallationCandidate( export async function loadCheckedInstallationCandidate(
root: string, root: string,
generator: string,
selections: Readonly<Record<string, PackageInstallationSelection>> = {}, selections: Readonly<Record<string, PackageInstallationSelection>> = {},
) { ) {
if (!rootPattern.test(root) || !(await lstat(root)).isDirectory()) if (!rootPattern.test(root) || !(await lstat(root)).isDirectory())
throw Error("Candidate must be an immutable Nix output directory"); throw Error("Candidate must be an immutable Nix output directory");
const [candidate, recordedPlan, artifacts, source, entryRequest] = await Promise.all([ const [candidate, plan, artifacts, source, entryRequest, kernel, queries] = await Promise.all([
readJson(root, "candidate.json"), readJson(root, "candidate.json"),
readJson(root, "execution-plan.json"), readJson(root, "execution-plan.json"),
readJson(root, "execution-artifacts.json"), readJson(root, "execution-artifacts.json"),
readJson(root, "source.json"), readJson(root, "source.json"),
readJson(root, "entry.json"), readJson(root, "entry.json"),
readJson(root, "kernel.json"),
readJson(root, "queries.json"),
]); ]);
const { executionContracts: _legacyContracts, ...workspace } = candidate; const { executionContracts: _legacyContracts, ...workspace } = candidate;
const plan = compileExecutionPlan(workspace as WorkspaceRevision); if (
if (hexDigest(plan) !== hexDigest(recordedPlan)) plan?.definition?.codec !== "camino-execution-plan-v1" ||
hexDigest(plan.definition) !== plan.digest ||
hexDigest(workspace) !== hexDigest(plan.definition.workspace)
)
throw new ExecutionPlanError( throw new ExecutionPlanError(
"CANDIDATE_PLAN_MISMATCH", "CANDIDATE_PLAN_MISMATCH",
root, root,
"Recompiled source contract differs from the stored plan", "Stored plan must match its digest and exact workspace source contract",
); );
if ( if (
source.kind !== "workspace" || source.kind !== "workspace" ||
@@ -57,7 +60,9 @@ export async function loadCheckedInstallationCandidate(
) )
throw new ExecutionPlanError("CANDIDATE_SOURCE_INVALID", root, "Expected a source URL without credentials"); throw new ExecutionPlanError("CANDIDATE_SOURCE_INVALID", root, "Expected a source URL without credentials");
if ( if (
artifacts.codec !== "camino-checked-execution-v1" || artifacts.codec !== "camino-checked-execution-v2" ||
typeof artifacts.generator !== "string" ||
!/^[0-9a-f]{64}$/.test(artifacts.generator) ||
!artifacts.roots || !artifacts.roots ||
typeof artifacts.roots !== "object" || typeof artifacts.roots !== "object" ||
Array.isArray(artifacts.roots) Array.isArray(artifacts.roots)
@@ -70,30 +75,37 @@ export async function loadCheckedInstallationCandidate(
root, root,
"Package artifacts must be immutable Nix roots", "Package artifacts must be immutable Nix roots",
); );
const installation = await loadExecutionInstallation(plan, artifacts.roots, generator, selections); const installation = await loadExecutionInstallation(plan, artifacts.roots, selections);
const entry = compileWorkspaceEntry(plan, entryRequest); const entry = compileWorkspaceEntry(plan, entryRequest);
const kernel = compileKernelProgram(plan), for (const [name, program, codec] of [
queries = compileQueryProgram(plan); ["kernel", kernel, "camino-kernel-program-v1"],
// The isolated runner and managed host consume the same compiled programs. ["queries", queries, "camino-query-program-v1"],
for (const [file, expected] of [
["kernel.json", kernel],
["queries.json", queries],
] as const) ] as const)
if (hexDigest(await readJson(root, file)) !== hexDigest(expected)) if (program?.codec !== codec || program.plan !== plan.digest || hexDigest(program) !== artifacts.programs?.[name])
throw new ExecutionPlanError( throw new ExecutionPlanError(
"CANDIDATE_PROGRAM_MISMATCH", "CANDIDATE_PROGRAM_MISMATCH",
file, name,
"Stored interpreter input differs from checked compilation", "Stored interpreter input must match the checked build and plan",
); );
const provenance = { const provenance = {
workspaceRevision: workspace.id as string, workspaceRevision: workspace.id as string,
source: { repository: source.repository as string, commit: source.commit as string }, source: { repository: source.repository as string, commit: source.commit as string },
generator, generator: artifacts.generator as string,
packages: plan.definition.packages.map((pkg) => ({ revision: pkg.revision, source: pkg.source })), packages: (plan as ExecutionPlan).definition.packages.map((pkg) => ({
revision: pkg.revision,
source: pkg.source,
})),
}; };
const reference = { const reference = {
root, root,
digest: hexDigest({ plan: plan.digest, world: installation.world, provenance, roots: artifacts.roots, entry }), digest: hexDigest({
plan: plan.digest,
world: installation.world,
provenance,
roots: artifacts.roots,
entry,
programs: artifacts.programs,
}),
}; };
return { return {
reference, reference,
@@ -105,7 +117,7 @@ export async function loadCheckedInstallationCandidate(
kernel, kernel,
queries, queries,
installation, installation,
executionPlan: plan, executionPlan: plan as ExecutionPlan,
}; };
} }
+21 -4
View File
@@ -1,6 +1,6 @@
import path from "node:path"; import path from "node:path";
import { loadCheckedArtifactWorld } from "./receipts.js"; import { loadCheckedArtifactWorld } from "./receipts.js";
import { contentArtifactUrl } from "./files.js"; import { contentArtifactUrl, loadArtifactWorld, type LoadedArtifactWorld } from "./files.js";
import { selectArtifactImplementation, type ArtifactImplementation, type ComponentArtifact } from "./artifacts.js"; import { selectArtifactImplementation, type ArtifactImplementation, type ComponentArtifact } from "./artifacts.js";
import { ExecutionPlanError, hexDigest, type ExecutionPlan } from "./plan.js"; import { ExecutionPlanError, hexDigest, type ExecutionPlan } from "./plan.js";
import { import {
@@ -108,15 +108,32 @@ export interface InstalledComponent {
readonly compatibility: ComponentArtifact["compatibility"]; readonly compatibility: ComponentArtifact["compatibility"];
readonly sharedModules: Readonly<Record<keyof ComponentArtifact["sharedModules"], string>>; readonly sharedModules: Readonly<Record<keyof ComponentArtifact["sharedModules"], string>>;
} }
/** Verify candidate receipts before assembling process entrypoints and browser assets. /** Build-time validation uses the compiler selected by the Nix derivation. */
* This grants no authority to activate a world or run optional modules in process. */ export async function checkExecutionInstallation(
export async function loadExecutionInstallation(
plan: ExecutionPlan, plan: ExecutionPlan,
roots: Readonly<Record<string, string>>, roots: Readonly<Record<string, string>>,
generator: string, generator: string,
selections: Readonly<Record<string, PackageInstallationSelection>> = {}, selections: Readonly<Record<string, PackageInstallationSelection>> = {},
): Promise<ExecutionInstallation> { ): Promise<ExecutionInstallation> {
const loaded = await loadCheckedArtifactWorld(plan, roots, generator); const loaded = await loadCheckedArtifactWorld(plan, roots, generator);
return assembleExecutionInstallation(plan, roots, loaded, selections);
}
/** Load immutable artifacts without regenerating bindings with the host compiler.
* Format, coverage, content and runtime ABI checks still apply. This grants no
* authority to activate a world or run optional modules in process. */
export async function loadExecutionInstallation(
plan: ExecutionPlan,
roots: Readonly<Record<string, string>>,
selections: Readonly<Record<string, PackageInstallationSelection>> = {},
): Promise<ExecutionInstallation> {
return assembleExecutionInstallation(plan, roots, await loadArtifactWorld(plan, roots), selections);
}
function assembleExecutionInstallation(
plan: ExecutionPlan,
roots: Readonly<Record<string, string>>,
loaded: LoadedArtifactWorld,
selections: Readonly<Record<string, PackageInstallationSelection>>,
): ExecutionInstallation {
const fail = (where: string, message: string): never => { const fail = (where: string, message: string): never => {
throw new ExecutionPlanError("INSTALLATION_INVALID", where, message); throw new ExecutionPlanError("INSTALLATION_INVALID", where, message);
}; };
+2 -1
View File
@@ -119,7 +119,8 @@ export async function checkArtifactReceipt(
throw new ExecutionPlanError("INVALID_RECEIPT", file.path, "Generated bindings were replaced or omitted"); throw new ExecutionPlanError("INVALID_RECEIPT", file.path, "Generated bindings were replaced or omitted");
} }
} }
/** Activation uses this entrypoint with its trusted generator identity, not the manifest-only loader. */ /** Checked Nix builds validate generated sources with their selected compiler.
* Runtime loading uses the stored outputs instead of repeating compilation. */
export async function loadCheckedArtifactWorld( export async function loadCheckedArtifactWorld(
plan: ExecutionPlan, plan: ExecutionPlan,
roots: Readonly<Record<string, string>>, roots: Readonly<Record<string, string>>,
+2 -2
View File
@@ -17,7 +17,7 @@ import {
componentArtifactDigest, componentArtifactDigest,
componentSharedModules, componentSharedModules,
expectedArtifactReceipt, expectedArtifactReceipt,
loadExecutionInstallation, checkExecutionInstallation,
clientContractCatalog, clientContractCatalog,
type ComponentArtifact, type ComponentArtifact,
type PackageArtifacts, type PackageArtifacts,
@@ -208,7 +208,7 @@ test("checked component-only artifacts install without a service and reject miss
await writeFile(path.join(root, file), content); await writeFile(path.join(root, file), content);
} }
await writeFile(path.join(root, "share/quixos/build-receipt.json"), JSON.stringify(receipt)); await writeFile(path.join(root, "share/quixos/build-receipt.json"), JSON.stringify(receipt));
const installation = await loadExecutionInstallation(plan, { [pkg.revision]: root }, generator); const installation = await checkExecutionInstallation(plan, { [pkg.revision]: root }, generator);
assert.equal(installation.packages[0]!.service, undefined); assert.equal(installation.packages[0]!.service, undefined);
assert.equal(installation.components[0]!.id, "component:card"); assert.equal(installation.components[0]!.id, "component:card");
assert.ok(installation.routes[installation.components[0]!.url]); assert.ok(installation.routes[installation.components[0]!.url]);