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:
@@ -1,11 +1,8 @@
|
||||
import { open, lstat } from "node:fs/promises";
|
||||
import { constants } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import type { WorkspaceRevision } from "../capability-model/types.js";
|
||||
import { compileExecutionPlan, hexDigest, ExecutionPlanError, type ExecutionPlan } from "./plan.js";
|
||||
import { hexDigest, ExecutionPlanError, type ExecutionPlan } from "./plan.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]+$/;
|
||||
async function readJson(root: string, file: string): Promise<any> {
|
||||
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();
|
||||
}
|
||||
}
|
||||
/** Revalidate the normal checked-candidate output. This is the shared artifact
|
||||
* loader, not a second managed-only executable manifest or an activation grant. */
|
||||
/** Load the checked Nix output, preserving its original compilation and provenance.
|
||||
* 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(
|
||||
root: string,
|
||||
generator: string,
|
||||
selections: Readonly<Record<string, PackageInstallationSelection>> = {},
|
||||
) {
|
||||
if (!rootPattern.test(root) || !(await lstat(root)).isDirectory())
|
||||
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, "execution-plan.json"),
|
||||
readJson(root, "execution-artifacts.json"),
|
||||
readJson(root, "source.json"),
|
||||
readJson(root, "entry.json"),
|
||||
readJson(root, "kernel.json"),
|
||||
readJson(root, "queries.json"),
|
||||
]);
|
||||
const { executionContracts: _legacyContracts, ...workspace } = candidate;
|
||||
const plan = compileExecutionPlan(workspace as WorkspaceRevision);
|
||||
if (hexDigest(plan) !== hexDigest(recordedPlan))
|
||||
if (
|
||||
plan?.definition?.codec !== "camino-execution-plan-v1" ||
|
||||
hexDigest(plan.definition) !== plan.digest ||
|
||||
hexDigest(workspace) !== hexDigest(plan.definition.workspace)
|
||||
)
|
||||
throw new ExecutionPlanError(
|
||||
"CANDIDATE_PLAN_MISMATCH",
|
||||
root,
|
||||
"Recompiled source contract differs from the stored plan",
|
||||
"Stored plan must match its digest and exact workspace source contract",
|
||||
);
|
||||
if (
|
||||
source.kind !== "workspace" ||
|
||||
@@ -57,7 +60,9 @@ export async function loadCheckedInstallationCandidate(
|
||||
)
|
||||
throw new ExecutionPlanError("CANDIDATE_SOURCE_INVALID", root, "Expected a source URL without credentials");
|
||||
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 ||
|
||||
typeof artifacts.roots !== "object" ||
|
||||
Array.isArray(artifacts.roots)
|
||||
@@ -70,30 +75,37 @@ export async function loadCheckedInstallationCandidate(
|
||||
root,
|
||||
"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 kernel = compileKernelProgram(plan),
|
||||
queries = compileQueryProgram(plan);
|
||||
// The isolated runner and managed host consume the same compiled programs.
|
||||
for (const [file, expected] of [
|
||||
["kernel.json", kernel],
|
||||
["queries.json", queries],
|
||||
for (const [name, program, codec] of [
|
||||
["kernel", kernel, "camino-kernel-program-v1"],
|
||||
["queries", queries, "camino-query-program-v1"],
|
||||
] 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(
|
||||
"CANDIDATE_PROGRAM_MISMATCH",
|
||||
file,
|
||||
"Stored interpreter input differs from checked compilation",
|
||||
name,
|
||||
"Stored interpreter input must match the checked build and plan",
|
||||
);
|
||||
const provenance = {
|
||||
workspaceRevision: workspace.id as string,
|
||||
source: { repository: source.repository as string, commit: source.commit as string },
|
||||
generator,
|
||||
packages: plan.definition.packages.map((pkg) => ({ revision: pkg.revision, source: pkg.source })),
|
||||
generator: artifacts.generator as string,
|
||||
packages: (plan as ExecutionPlan).definition.packages.map((pkg) => ({
|
||||
revision: pkg.revision,
|
||||
source: pkg.source,
|
||||
})),
|
||||
};
|
||||
const reference = {
|
||||
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 {
|
||||
reference,
|
||||
@@ -105,7 +117,7 @@ export async function loadCheckedInstallationCandidate(
|
||||
kernel,
|
||||
queries,
|
||||
installation,
|
||||
executionPlan: plan,
|
||||
executionPlan: plan as ExecutionPlan,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import path from "node:path";
|
||||
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 { ExecutionPlanError, hexDigest, type ExecutionPlan } from "./plan.js";
|
||||
import {
|
||||
@@ -108,15 +108,32 @@ export interface InstalledComponent {
|
||||
readonly compatibility: ComponentArtifact["compatibility"];
|
||||
readonly sharedModules: Readonly<Record<keyof ComponentArtifact["sharedModules"], string>>;
|
||||
}
|
||||
/** Verify candidate receipts before assembling process entrypoints and browser assets.
|
||||
* This grants no authority to activate a world or run optional modules in process. */
|
||||
export async function loadExecutionInstallation(
|
||||
/** Build-time validation uses the compiler selected by the Nix derivation. */
|
||||
export async function checkExecutionInstallation(
|
||||
plan: ExecutionPlan,
|
||||
roots: Readonly<Record<string, string>>,
|
||||
generator: string,
|
||||
selections: Readonly<Record<string, PackageInstallationSelection>> = {},
|
||||
): Promise<ExecutionInstallation> {
|
||||
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 => {
|
||||
throw new ExecutionPlanError("INSTALLATION_INVALID", where, message);
|
||||
};
|
||||
|
||||
@@ -119,7 +119,8 @@ export async function checkArtifactReceipt(
|
||||
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(
|
||||
plan: ExecutionPlan,
|
||||
roots: Readonly<Record<string, string>>,
|
||||
|
||||
Reference in New Issue
Block a user