Skip to content

armgit / hypit

Public Imported from GitHub
0

Clone any viral video with AI agents. Not just a script, the whole workflow: swap the face, the words, the B-roll, ship 100 variants in one command, and get your 100M views.

refactor: separate runtime archive from artifacts

kashorin6564b3f3ddd0
diff --git a/packages/cli/src/archive.ts b/packages/cli/src/archive.ts
index f6661e3a..b3fc77e4 100644
--- a/packages/cli/src/archive.ts
+++ b/packages/cli/src/archive.ts
@@ -2,7 +2,7 @@ import { createHash, randomUUID } from "node:crypto";
import { mkdir, open, rename, rm, writeFile } from "node:fs/promises";
import { dirname } from "node:path";
-import type { CliRuntime } from "./runtime-port.js";
+import type { CliRuntimeArtifactAccess } from "./runtime-port.js";
import { isDigest } from "@narratage/protocol";
import type { BuildState, Digest, TypedRecord, TypeRef } from "@narratage/protocol";
import type { BuildCatalogEntry } from "@narratage/runtime";
@@ -41,7 +41,7 @@ function recordArtifact(record: TypedRecord): ArtifactIdentity | undefined {
}
export async function materializeRecord(
- runtime: Pick<CliRuntime, "openArtifact">,
+ runtime: Pick<CliRuntimeArtifactAccess, "openArtifact">,
record: TypedRecord,
destination: string,
): Promise<
@@ -60,7 +60,7 @@ export async function materializeRecord(
}
export async function materializeArtifact(
- runtime: Pick<CliRuntime, "openArtifact">,
+ runtime: Pick<CliRuntimeArtifactAccess, "openArtifact">,
artifact: ArtifactIdentity,
destination: string,
subject = "Build archive",
diff --git a/packages/cli/src/distribution.ts b/packages/cli/src/distribution.ts
index fc46818c..86cb9910 100644
--- a/packages/cli/src/distribution.ts
+++ b/packages/cli/src/distribution.ts
@@ -7,7 +7,9 @@ import type {
CliExternalServiceProgress,
CliExternalServiceReport,
CliRuntime,
- CliRuntimeControl,
+ CliRuntimeArchiveControl,
+ CliRuntimeArtifactAccess,
+ CliRuntimeMaintenance,
CliRuntimeDoctorResult,
} from "./runtime-port.js";
@@ -66,8 +68,12 @@ export type CliDistribution = {
/** Same-process package set already verified for compilation. */
readonly implementationPackages?: LoadedNodePackageSet;
}): Promise<CliRuntime>;
- /** Open only durable Stores for observation, cancellation, egress and maintenance. */
- createRuntimeControlFromConfig(path: string, options?: { readonly readOnly?: boolean }): Promise<CliRuntimeControl>;
+ /** Open only durable execution-state Stores for observation and cancellation. */
+ createRuntimeArchiveFromConfig(path: string, options?: { readonly readOnly?: boolean }): Promise<CliRuntimeArchiveControl>;
+ /** Open only the selected ArtifactStore for explicit byte ingress or egress. */
+ createRuntimeArtifactAccessFromConfig(path: string, options?: { readonly readOnly?: boolean }): Promise<CliRuntimeArtifactAccess>;
+ /** Open execution-state Stores plus the ArtifactStore for explicit retention maintenance. */
+ createRuntimeMaintenanceFromConfig(path: string, options?: { readonly readOnly?: boolean }): Promise<CliRuntimeMaintenance>;
/** Open only the selected Endpoint declaration and configured CredentialStores. */
createRuntimeCredentialsFromConfig(path: string, endpoint: string): Promise<CliCredentialControl>;
/** Re-enter this exact Distribution as the hidden durable Worker process. */
diff --git a/packages/cli/src/main.ts b/packages/cli/src/main.ts
index 01d486dc..77f326a8 100644
--- a/packages/cli/src/main.ts
+++ b/packages/cli/src/main.ts
@@ -2,7 +2,7 @@ import { dirname, resolve } from "node:path";
import { readFile } from "node:fs/promises";
import type { NodeCompiledSourceClosure } from "@narratage/compiler-node";
-import { plannedNeeds } from "@narratage/runtime";
+import { plannedNeeds, sameBuildCatalogDescriptor } from "@narratage/runtime";
import type { BuildCatalogDescriptor, CapacityReservation, OperationProgress } from "@narratage/runtime";
import { isDigest } from "@narratage/protocol";
import type { BuildState, CapabilityRef, TypeRef } from "@narratage/protocol";
@@ -34,7 +34,9 @@ import type {
CliExternalServiceProgress,
CliExternalServiceReport,
CliRuntime,
- CliRuntimeControl,
+ CliRuntimeArchiveControl,
+ CliRuntimeArtifactAccess,
+ CliRuntimeMaintenance,
} from "./runtime-port.js";
import { writeCliHelp, writeCliOutput } from "./output.js";
import type { CliColorMode, CliIo } from "./output.js";
@@ -596,12 +598,28 @@ async function loadRuntime(
});
}
-async function loadRuntimeControl(
+async function loadRuntimeArchive(
path: string,
distribution: CliDistribution,
readOnly = true,
-): Promise<CliRuntimeControl> {
- return await distribution.createRuntimeControlFromConfig(path, { readOnly });
+): Promise<CliRuntimeArchiveControl> {
+ return await distribution.createRuntimeArchiveFromConfig(path, { readOnly });
+}
+
+async function loadRuntimeArtifactAccess(
+ path: string,
+ distribution: CliDistribution,
+ readOnly = true,
+): Promise<CliRuntimeArtifactAccess> {
+ return await distribution.createRuntimeArtifactAccessFromConfig(path, { readOnly });
+}
+
+async function loadRuntimeMaintenance(
+ path: string,
+ distribution: CliDistribution,
+ readOnly = true,
+): Promise<CliRuntimeMaintenance> {
+ return await distribution.createRuntimeMaintenanceFromConfig(path, { readOnly });
}
function displayType(type: TypeRef): string {
@@ -780,7 +798,7 @@ async function observeBuild(
return current;
}
-type RuntimeArchiveView = Pick<CliRuntimeControl, "status" | "close">;
+type RuntimeArchiveView = Pick<CliRuntimeArchiveControl, "status" | "close">;
/**
* A Run Source needs the archive only when it names a historical Build Candidate.
@@ -791,9 +809,9 @@ function lazyRuntimeArchive(
path: string,
distribution: CliDistribution,
): RuntimeArchiveView {
- let loading: Promise<CliRuntimeControl> | undefined;
- const open = (): Promise<CliRuntimeControl> => {
- loading ??= loadRuntimeControl(path, distribution);
+ let loading: Promise<CliRuntimeArchiveControl> | undefined;
+ const open = (): Promise<CliRuntimeArchiveControl> => {
+ loading ??= loadRuntimeArchive(path, distribution);
return loading;
};
return {
@@ -1256,8 +1274,8 @@ export async function runCli(
}
// These are independent views over one Profile. Load them concurrently without inventing a
// second registry; each selected package remains the authority for its own report.
- const runtimeLoading = loadRuntimeControl(profile, distribution);
- let runtime: CliRuntimeControl | undefined;
+ const runtimeLoading = loadRuntimeArchive(profile, distribution);
+ let runtime: CliRuntimeArchiveControl | undefined;
try {
const loaded = await Promise.all([
distribution.runtimeProfileRevision(profile)
@@ -1308,7 +1326,7 @@ export async function runCli(
|| args.packages.length > 0) {
throw new Error("gc reads all deployment selection from the Runtime Profile itself");
}
- const runtime = await loadRuntimeControl(resolve(args.file!), distribution, !args.apply);
+ const runtime = await loadRuntimeMaintenance(resolve(args.file!), distribution, !args.apply);
try {
const report = await runtime.garbageCollectArtifacts({ apply: args.apply });
const machine = {
@@ -1403,7 +1421,7 @@ export async function runCli(
|| args.command === "history" || args.command === "get" || args.command === "cancel" || args.command === "queue"
|| args.command === "operations" || args.command === "operation") {
if (args.runtime === undefined) throw new Error(`${args.command} requires --runtime`);
- const runtime = await loadRuntimeControl(args.runtime, distribution, args.command !== "cancel");
+ const runtime = await loadRuntimeArchive(args.runtime, distribution, args.command !== "cancel");
try {
if (args.command === "queue") {
let previous: string | undefined;
@@ -1466,7 +1484,6 @@ export async function runCli(
["Active Builds", String(active.length)],
["Active Operations", String(activeOperations.length)],
["Worker", worker.pid === undefined ? worker.state : `${worker.state} · ${worker.pid}`],
- ["Queued Builds", String(queue.dispatches.length)],
["Operation tickets", String(queue.capacity.length)],
], [
...buildLines,
@@ -1600,7 +1617,8 @@ export async function runCli(
["Operations", String(status.operations.length)],
], notableOperations.map((operation) => operation.failure === undefined
? `${operation.endpoint}: ${operation.status}`
- : `${operation.endpoint}: ${operation.failure.code} — ${operation.failure.message}`));
+ : `${operation.endpoint}: ${operation.failure.code} — ${operation.failure.message}`)
+ .concat(status.dispatch?.reason === undefined ? [] : [`Reason ${status.dispatch.reason}`]));
if (status.build === undefined || status.dispatch?.terminal === "failed") io.setExitCode?.(1);
} else if (args.command === "inspect") {
const status = await runtime.status(args.file!);
@@ -1660,7 +1678,11 @@ export async function runCli(
["Targets", String(archive.targets.length)], ["Accepted records", String(archive.records.length)],
["Operations", String(status.operations.length)],
...(args.verbose ? [["Revision", String(status.build.revision)] as const] : []),
- ], [...targetLines, ...otherLines]);
+ ], [
+ ...(status.dispatch?.reason === undefined ? [] : [`Reason ${status.dispatch.reason}`]),
+ ...targetLines,
+ ...otherLines,
+ ]);
} else if (args.command === "get") {
const status = await runtime.status(args.file!);
if (status.build === undefined) throw new Error(`Build ${args.file} does not exist`);
@@ -1676,7 +1698,13 @@ export async function runCli(
} else {
const [first] = references;
if (first === undefined) throw new Error(`Build ${args.file} does not reference Artifact ${args.artifact}`);
- const materialized = await materializeArtifact(runtime, first, args.to, `Build ${args.file}`);
+ const artifacts = await loadRuntimeArtifactAccess(args.runtime!, distribution);
+ let materialized;
+ try {
+ materialized = await materializeArtifact(artifacts, first, args.to, `Build ${args.file}`);
+ } finally {
+ await artifacts.close();
+ }
const machine = {
build: status.build.build,
artifact: args.artifact,
@@ -1710,7 +1738,13 @@ export async function runCli(
["Artifacts", String(machine.artifacts.length)],
]);
} else {
- const materialized = await materializeRecord(runtime, record, args.to);
+ const artifacts = await loadRuntimeArtifactAccess(args.runtime!, distribution);
+ let materialized;
+ try {
+ materialized = await materializeRecord(artifacts, record, args.to);
+ } finally {
+ await artifacts.close();
+ }
const machine = {
build: status.build.build,
record: record.id,
@@ -1897,14 +1931,14 @@ export async function runCli(
await archive.close();
}
const result = loadedRun.compiler.planCompilation(loadedRun, loadedPackageSet?.lock.digest);
- const preflight = await preflightPlan(args.runtime, distribution, result.state, loadedPackageSet);
- // A managed program being down is repairable after Runtime validation;
- // every other deployment error fails before we construct execution or
- // start anything. With --no-services, readiness errors remain fatal.
- assertPreflight(preflight, args.noServices
- ? new Set()
- : new Set(["EXTERNAL_SERVICE_DOWN", "EXTERNAL_SERVICE_MISMATCH"]));
- const runtime = await loadRuntime(args.runtime, distribution, loadedPackageSet);
+ const runtimeArchive = await loadRuntimeArchive(args.runtime, distribution);
+ let archived;
+ try {
+ archived = await runtimeArchive.status(args.buildId ?? result.state.id);
+ } finally {
+ await runtimeArchive.close();
+ }
+ let runtime: CliRuntime | undefined;
try {
const catalog = createCatalogDescriptor({
core: result.state.id,
@@ -1920,23 +1954,57 @@ export async function runCli(
catalog,
attachments: result.compilation.attachments,
} as const;
- const services = args.noServices
- ? undefined
- : await startDeclaredServices(
- args.runtime,
- distribution,
- demandedCapabilities(result.state),
- reportServiceProgress,
- );
const workerProfileDigest = await distribution.runtimeProfileRevision(args.runtime);
- const worker = await ensureRuntimeProcess(
- args.runtime,
- distribution.runtimeWorkerLaunch(),
- workerProfileDigest,
- args.maxWaitMs ?? 10_000,
- );
- let built = await runtime.build(request);
- if (args.follow) {
+ const terminalArchive = archived.build !== undefined
+ && archived.dispatch?.phase === "terminal";
+ if (archived.build !== undefined && archived.build.state.id !== request.state.id) {
+ throw new Error(
+ `Build ${request.id} already names another Core Build. Choose a new --build-id for a new Run, `
+ + "or restore the original Author/Run Sources to resume this Build",
+ );
+ }
+ if (archived.catalog !== undefined && !sameBuildCatalogDescriptor(archived.catalog, request.catalog)) {
+ throw new Error(
+ `Build ${request.id} already has another source, Run Source or output naming. `
+ + "Choose a new --build-id instead of changing the presentation of an existing Build",
+ );
+ }
+ let services: Awaited<ReturnType<typeof startDeclaredServices>> | undefined;
+ let worker = await runtimeProcessStatus(args.runtime, workerProfileDigest);
+ let built: CliBuildSubmission;
+ if (terminalArchive) {
+ built = {
+ id: request.id,
+ state: archived.build!.state,
+ status: submissionStatus(archived.dispatch!),
+ dispatch: archived.dispatch!,
+ };
+ } else {
+ const preflight = await preflightPlan(args.runtime, distribution, result.state, loadedPackageSet);
+ // A managed program being down is repairable after Runtime validation;
+ // every other deployment error fails before we construct execution or
+ // start anything. With --no-services, readiness errors remain fatal.
+ assertPreflight(preflight, args.noServices
+ ? new Set()
+ : new Set(["EXTERNAL_SERVICE_DOWN", "EXTERNAL_SERVICE_MISMATCH"]));
+ services = args.noServices
+ ? undefined
+ : await startDeclaredServices(
+ args.runtime,
+ distribution,
+ demandedCapabilities(result.state),
+ reportServiceProgress,
+ );
+ runtime = await loadRuntime(args.runtime, distribution, loadedPackageSet);
+ worker = await ensureRuntimeProcess(
+ args.runtime,
+ distribution.runtimeWorkerLaunch(),
+ workerProfileDigest,
+ args.maxWaitMs ?? 10_000,
+ );
+ built = await runtime.build(request);
+ }
+ if (args.follow && runtime !== undefined) {
built = await observeBuild(runtime, built, {
...(args.maxWaitMs === undefined ? {} : { maxWaitMs: args.maxWaitMs }),
workerProfile: args.runtime,
@@ -1954,20 +2022,22 @@ export async function runCli(
});
}
const targetOutputs = new Set(built.state.request.targets.map((target) => target.output));
- const targetAliases = catalog.aliases.filter((alias) =>
+ const presentation = archived.catalog ?? catalog;
+ const targetAliases = presentation.aliases.filter((alias) =>
alias.ref.kind === "logical-output" && targetOutputs.has(alias.ref.id));
- const targetPresentations = targetAliases.map((alias) => {
+ const targetPresentations = targetAliases.flatMap((alias) => {
const selection = built.state.plan.selections.find((item) => item.output === alias.ref.id);
const record = selection === undefined
? undefined
: built.state.records.find((item) => item.id === selection.record);
- return {
+ if (record === undefined) return [];
+ return [{
alias,
record,
...(record?.value.kind === "inline"
? { inline: inlineValuePreview(record.value.value) }
: {}),
- };
+ }];
});
const machine = {
build: built.id,
@@ -1992,8 +2062,12 @@ export async function runCli(
},
};
const terminalLines = targetPresentations.length === 0
- ? [`Inspect narratage inspect ${built.id} --runtime ${args.runtime}`]
+ ? [
+ ...(built.dispatch.reason === undefined ? [] : [`Reason ${built.dispatch.reason}`]),
+ `Inspect narratage inspect ${built.id} --runtime ${args.runtime}`,
+ ]
: [
+ ...(built.dispatch.reason === undefined ? [] : [`Reason ${built.dispatch.reason}`]),
`Inspect narratage inspect ${built.id} --runtime ${args.runtime}`,
...targetPresentations
.filter((item) => item.inline !== undefined)
@@ -2013,7 +2087,7 @@ export async function runCli(
: built.status === "cancelled" || (args.follow && !terminal) ? "warning" : "success", [
["Build", built.id],
["Status", built.status],
- ["Worker", String(worker.pid)],
+ ["Worker", worker.state === "running" ? String(worker.pid) : worker.state],
["Goals", String(machine.goals.length)],
], terminal ? terminalLines : [
`Status narratage status ${built.id} --runtime ${args.runtime}`,
@@ -2022,7 +2096,7 @@ export async function runCli(
]);
if (built.status === "failed") io.setExitCode?.(1);
} finally {
- await runtime.close();
+ await runtime?.close();
}
return;
}
diff --git a/packages/cli/src/output.ts b/packages/cli/src/output.ts
index 5c705205..8c5055a5 100644
--- a/packages/cli/src/output.ts
+++ b/packages/cli/src/output.ts
@@ -371,7 +371,7 @@ function renderPlan(
}
}
const capabilityCount = view.machine.preflight.capabilities.length;
- lines.push(` ${colors.dim(`Only the ${capabilityCount} demanded ${capabilityCount === 1 ? "capability was" : "capabilities were"} checked.`)}`);
+ lines.push(` ${colors.dim(`Runtime base checked; ${capabilityCount} demanded Endpoint ${capabilityCount === 1 ? "capability" : "capabilities"} checked.`)}`);
}
const visibleSelections = verbose
? plan.selections
diff --git a/packages/cli/src/runtime-port.ts b/packages/cli/src/runtime-port.ts
index de86be6f..48aa92b9 100644
--- a/packages/cli/src/runtime-port.ts
+++ b/packages/cli/src/runtime-port.ts
@@ -41,7 +41,7 @@ export type CliArtifactGarbageCollection = {
readonly deleted: readonly Digest[];
};
-export type CliRuntimeControl = {
+export type CliRuntimeArchiveControl = {
status(build: string): Promise<CliRuntimeStatus>;
activity(build: string): Promise<{
readonly operations: readonly OperationSnapshot[];
@@ -56,12 +56,19 @@ export type CliRuntimeControl = {
builds(): Promise<readonly BuildCatalogEntry[]>;
cancel(build: string, reason?: string): Promise<BuildDispatchSnapshot | undefined>;
cancelOperation(id: Digest, reason?: string): Promise<OperationSnapshot | undefined>;
+ close(): void | Promise<void>;
+};
+
+export type CliRuntimeArtifactAccess = {
readArtifact(digest: Digest): Promise<Uint8Array | undefined>;
openArtifact(digest: Digest): Promise<AsyncIterable<Uint8Array> | undefined>;
- garbageCollectArtifacts(options?: { readonly apply?: boolean }): Promise<CliArtifactGarbageCollection>;
close(): void | Promise<void>;
};
+export type CliRuntimeMaintenance = CliRuntimeArchiveControl & CliRuntimeArtifactAccess & {
+ garbageCollectArtifacts(options?: { readonly apply?: boolean }): Promise<CliArtifactGarbageCollection>;
+};
+
export type CliCredentialControl = {
credentials(endpoint?: string): Promise<readonly CliCredentialStatus[]>;
putCredential(endpoint: string, slot: string, secret: string): Promise<CliCredentialStatus>;
@@ -72,7 +79,7 @@ export type CliCredentialControl = {
close(): void | Promise<void>;
};
-export type CliRuntime = CliRuntimeControl & CliCredentialControl & {
+export type CliRuntime = CliRuntimeMaintenance & CliCredentialControl & {
build(request: {
readonly id: string;
readonly state: BuildState;
diff --git a/packages/cli/test/control-commands.test.ts b/packages/cli/test/control-commands.test.ts
index c8c0bf68..11b676fc 100644
--- a/packages/cli/test/control-commands.test.ts
+++ b/packages/cli/test/control-commands.test.ts
@@ -3,7 +3,7 @@ import test from "node:test";
import type { CliDistribution } from "../src/distribution.js";
import { runCli } from "../src/main.js";
-import type { CliCredentialControl, CliRuntimeControl } from "../src/runtime-port.js";
+import type { CliCredentialControl, CliRuntimeArchiveControl } from "../src/runtime-port.js";
test("queue opens durable control without constructing execution Providers", async () => {
const calls: string[] = [];
@@ -15,10 +15,10 @@ test("queue opens durable control without constructing execution Providers", asy
async close() {
calls.push("control.close");
},
- } as unknown as CliRuntimeControl;
+ } as unknown as CliRuntimeArchiveControl;
const distribution = {
runtimeProfileRevision: async () => "test-revision",
- createRuntimeControlFromConfig: async () => {
+ createRuntimeArchiveFromConfig: async () => {
calls.push("control.create");
return control;
},
@@ -52,7 +52,7 @@ test("queue opens durable control without constructing execution Providers", asy
test("command options fail closed instead of being silently ignored", async () => {
const distribution = {
- createRuntimeControlFromConfig: async (path: string) => {
+ createRuntimeArchiveFromConfig: async (path: string) => {
throw new Error(`profile delegated: ${path}`);
},
} as unknown as CliDistribution;
diff --git a/packages/cli/test/runtime-build-process.test.ts b/packages/cli/test/runtime-build-process.test.ts
index b0ff3912..cdef9056 100644
--- a/packages/cli/test/runtime-build-process.test.ts
+++ b/packages/cli/test/runtime-build-process.test.ts
@@ -219,6 +219,19 @@ test("CLI exits after durable submission and a restarted detached Worker complet
]);
assert.equal((materialized.materialized as { readonly kind?: string }).kind, "json");
assert.notEqual(JSON.parse(await readFile(exported, "utf8")), undefined);
+
+ await stopRuntimeProcess(profile, 10_000);
+ const replayed = await cli(project, [
+ "build", run,
+ "--build-id", build,
+ "--runtime", profile,
+ "--root", project,
+ "--no-services",
+ "--follow",
+ ]);
+ assert.equal(replayed.status, "complete");
+ assert.equal((replayed.worker as { readonly state?: string }).state, "stopped",
+ "replaying a terminal Build reads its durable result without starting a Worker");
} finally {
await stopRuntimeProcess(profile, 10_000).catch(() => undefined);
await rm(project, { recursive: true, force: true });
diff --git a/packages/cli/test/services-command.test.ts b/packages/cli/test/services-command.test.ts
index 05cd37c6..45740f73 100644
--- a/packages/cli/test/services-command.test.ts
+++ b/packages/cli/test/services-command.test.ts
@@ -104,7 +104,7 @@ test("an idle stopped Runtime is status data, not a command failure", async () =
let exitCode: number | undefined;
const selected = {
...distribution([]),
- createRuntimeControlFromConfig: async () => ({
+ createRuntimeArchiveFromConfig: async () => ({
async queue() { return { dispatches: [], capacity: [], operations: [] }; },
async close() {},
}),
diff --git a/packages/local/src/config.ts b/packages/local/src/config.ts
index ad3b9eef..1e1b9e2d 100644
--- a/packages/local/src/config.ts
+++ b/packages/local/src/config.ts
@@ -31,7 +31,11 @@ import type { RuntimeServicePackage, RuntimeServiceSelection } from "@narratage/
import type { LocalRuntime } from "./types.js";
import type { LocalCredentialControl } from "./types.js";
-import type { LocalRuntimeControl } from "./types.js";
+import type {
+ LocalRuntimeArchiveControl,
+ LocalRuntimeArtifactAccess,
+ LocalRuntimeControl,
+} from "./types.js";
export type RuntimeConfigEntry = {
readonly use: string;
@@ -623,27 +627,88 @@ export async function createRuntimeFromConfig(
});
}
-/**
- * Open only the durable control Stores selected by a Runtime Profile. Endpoint
- * construction, credential resolution, author components and Worker instances
- * are unnecessary for archive inspection, queue visibility, egress and cancellation.
- */
-export async function createRuntimeControlFromConfig(
+async function createSelectedRuntimeServices(
+ document: RuntimeConfigDocument,
+ root: string,
+ packageRoot: string,
+ registry: RuntimeAdapterRegistry,
+ serviceIds: readonly string[],
+ readOnly: boolean,
+): Promise<readonly RuntimeServicePackage[]> {
+ const serviceEntries = runtimeEntriesForServices(document.runtimeServices, serviceIds);
+ await installLockedRuntimeAdapters(registry, document.runtimePackageLock, root, packageRoot, {
+ selected: [],
+ logical: serviceEntries.map((item) => ({ abi: runtimeServiceAdapterHostAbi, name: item.use })),
+ });
+ const packages: RuntimeServicePackage[] = [];
+ try {
+ for (const item of serviceEntries) {
+ packages.push(await registry.createService(item.use, {
+ root,
+ instance: item.instance,
+ config: item.config ?? {},
+ access: readOnly ? "read-only" : "read-write",
+ }));
+ }
+ return packages;
+ } catch (error) {
+ await closeRuntimeServicePackages(packages);
+ throw error;
+ }
+}
+
+/** Open Build, Operation and Dispatch state without constructing an ArtifactStore. */
+export async function createRuntimeArchiveFromConfig(
+ path: string,
+ options: LoadRuntimeConfigOptions = {},
+): Promise<LocalRuntimeArchiveControl> {
+ const { createProjectLocalRuntimeArchiveControl } = await import("./control.js");
+ const { document, root, packageRoot } = await openRuntimeConfig(path, options.packageRoot);
+ const registry = options.registry ?? new RuntimeAdapterRegistry();
+ const runtimeServices = await createSelectedRuntimeServices(document, root, packageRoot, registry, [
+ document.services.stores.build,
+ document.services.stores.operations,
+ document.services.stores.dispatch,
+ ], options.readOnly === true);
+ return await createProjectLocalRuntimeArchiveControl({
+ root,
+ runtimeServices,
+ runtimeSelection: document.services,
+ });
+}
+
+/** Open only the ArtifactStore selected by a Runtime Profile. */
+export async function createRuntimeArtifactAccessFromConfig(
+ path: string,
+ options: LoadRuntimeConfigOptions = {},
+): Promise<LocalRuntimeArtifactAccess> {
+ const { createProjectLocalRuntimeArtifactAccess } = await import("./control.js");
+ const { document, root, packageRoot } = await openRuntimeConfig(path, options.packageRoot);
+ const registry = options.registry ?? new RuntimeAdapterRegistry();
+ const runtimeServices = await createSelectedRuntimeServices(document, root, packageRoot, registry, [
+ document.services.stores.artifacts,
+ ], options.readOnly === true);
+ return await createProjectLocalRuntimeArtifactAccess({
+ root,
+ runtimeServices,
+ runtimeSelection: document.services,
+ });
+}
+
+/** Open state plus Artifacts only for explicit cross-store maintenance such as GC. */
+export async function createRuntimeMaintenanceFromConfig(
path: string,
options: LoadRuntimeConfigOptions = {},
): Promise<LocalRuntimeControl> {
const { createProjectLocalRuntimeControl } = await import("./control.js");
const { document, root, packageRoot } = await openRuntimeConfig(path, options.packageRoot);
const registry = options.registry ?? new RuntimeAdapterRegistry();
- await installLockedRuntimeAdapters(registry, document.runtimePackageLock, root, packageRoot, runtimePackageSelection(document));
- const runtimeServices = await Promise.all(document.runtimeServices.map(async (item) => {
- return await registry.createService(item.use, {
- root,
- instance: item.instance,
- config: item.config ?? {},
- access: options.readOnly ? "read-only" : "read-write",
- });
- }));
+ const runtimeServices = await createSelectedRuntimeServices(document, root, packageRoot, registry, [
+ document.services.stores.build,
+ document.services.stores.operations,
+ document.services.stores.dispatch,
+ document.services.stores.artifacts,
+ ], options.readOnly === true);
return await createProjectLocalRuntimeControl({
root,
runtimeServices,
@@ -691,11 +756,17 @@ export async function createRuntimeCredentialsFromConfig(
const endpointEntry = document.endpoints.find((item) => item.instance === endpointInstance);
if (endpointEntry === undefined) throw new Error(`Runtime Profile has no Endpoint instance ${endpointInstance}`);
const registry = options.registry ?? new RuntimeAdapterRegistry();
- await installLockedRuntimeAdapters(registry, document.runtimePackageLock, root, packageRoot, runtimePackageSelection(document));
const serviceEntries = runtimeEntriesForServices(
document.runtimeServices,
document.services.stores.credentials,
);
+ await installLockedRuntimeAdapters(registry, document.runtimePackageLock, root, packageRoot, {
+ selected: [],
+ logical: [
+ { abi: runtimeEndpointAdapterHostAbi, name: endpointEntry.use },
+ ...serviceEntries.map((item) => ({ abi: runtimeServiceAdapterHostAbi, name: item.use })),
+ ],
+ });
const packages = await Promise.all(serviceEntries.map(async (item) => await registry.createService(item.use, {
root,
instance: item.instance,
diff --git a/packages/local/src/control.ts b/packages/local/src/control.ts
index 5f228778..35dcc574 100644
--- a/packages/local/src/control.ts
+++ b/packages/local/src/control.ts
@@ -4,14 +4,22 @@ import {
isManagedArtifactStore,
isStreamingArtifactStore,
operationCancellationRequestId,
+ verifyRuntimeServicePackage,
} from "@narratage/runtime";
+import type { RuntimeService, RuntimeServicePackage } from "@narratage/runtime";
import type {
+ CreateLocalRuntimeArchiveControlOptions,
+ CreateLocalRuntimeArtifactAccessOptions,
CreateLocalRuntimeControlOptions,
+ LocalRuntimeArchiveControl,
+ LocalRuntimeArtifactAccess,
LocalRuntimeControl,
+ ProjectLocalRuntimeArchiveControlOptions,
+ ProjectLocalRuntimeArtifactAccessOptions,
ProjectLocalRuntimeControlOptions,
} from "./types.js";
-import { createProjectRuntimeServices } from "./project-services.js";
+import { selectedBuildCatalog } from "./project-services.js";
function assert(condition: unknown, message: string): asserts condition {
if (!condition) throw new Error(message);
@@ -36,10 +44,10 @@ function collectArtifactDigests(
}
}
-/** Store-only control surface shared by a full executor and read-only CLI commands. */
-export function createLocalRuntimeControl(
- options: CreateLocalRuntimeControlOptions,
-): LocalRuntimeControl {
+/** Durable execution-state control that never opens or depends on an ArtifactStore. */
+export function createLocalRuntimeArchiveControl(
+ options: CreateLocalRuntimeArchiveControlOptions,
+): LocalRuntimeArchiveControl {
const buildCatalog = options.buildCatalog;
return {
async activity(build) {
@@ -112,6 +120,17 @@ export function createLocalRuntimeControl(
await options.dispatchStore.wake(current.build);
return current;
},
+ close() {
+ return options.close?.();
+ },
+ };
+}
+
+/** Explicit Artifact byte access with no dependency on execution-state Stores. */
+export function createLocalRuntimeArtifactAccess(
+ options: CreateLocalRuntimeArtifactAccessOptions,
+): LocalRuntimeArtifactAccess {
+ return {
async readArtifact(digest) {
return await options.artifactStore.get(digest);
},
@@ -120,6 +139,21 @@ export function createLocalRuntimeControl(
const bytes = await options.artifactStore.get(digest);
return bytes === undefined ? undefined : (async function* () { yield bytes; })();
},
+ close() {
+ return options.close?.();
+ },
+ };
+}
+
+/** Full maintenance control used only by execution and explicit Artifact GC. */
+export function createLocalRuntimeControl(
+ options: CreateLocalRuntimeControlOptions,
+): LocalRuntimeControl {
+ const archive = createLocalRuntimeArchiveControl(options);
+ const artifacts = createLocalRuntimeArtifactAccess(options);
+ return {
+ ...archive,
+ ...artifacts,
async garbageCollectArtifacts(gc = {}) {
assert(isManagedArtifactStore(options.artifactStore),
"selected ArtifactStore does not expose explicit retention management");
@@ -148,19 +182,110 @@ export function createLocalRuntimeControl(
};
}
-/** Assemble selected service packages, then expose only their exact durable Store facets. */
+type OpenedProjectServices = {
+ readonly packages: readonly RuntimeServicePackage[];
+ readonly services: ReadonlyMap<string, { readonly package: RuntimeServicePackage; readonly service: RuntimeService }>;
+ close(): Promise<void>;
+};
+
+async function openProjectServices(
+ options: ProjectLocalRuntimeControlOptions,
+): Promise<OpenedProjectServices> {
+ const packages = [...options.runtimeServices];
+ const services = new Map<string, { readonly package: RuntimeServicePackage; readonly service: RuntimeService }>();
+ try {
+ for (const item of packages) {
+ verifyRuntimeServicePackage(item);
+ for (const service of item.services) {
+ assert(!services.has(service.instance.id), `Runtime service instance ${service.instance.id} is configured twice`);
+ services.set(service.instance.id, { package: item, service });
+ }
+ }
+ let closed = false;
+ return {
+ packages,
+ services,
+ close: async () => {
+ if (closed) return;
+ closed = true;
+ for (const item of [...packages].reverse()) await item.close?.();
+ },
+ };
+ } catch (error) {
+ for (const item of [...packages].reverse()) await item.close?.();
+ throw error;
+ }
+}
+
+function selectedService<Role extends RuntimeService["role"]>(
+ opened: OpenedProjectServices,
+ id: string,
+ role: Role,
+): Extract<RuntimeService, { readonly role: Role }> {
+ const item = opened.services.get(id);
+ assert(item !== undefined, `Runtime service selection refers to unknown instance ${id}`);
+ assert(item.service.role === role, `Runtime service ${id} is ${item.service.role}, not ${role}`);
+ return item.service as Extract<RuntimeService, { readonly role: Role }>;
+}
+
+/** Assemble only Build, Operation and Dispatch state selected by the Profile. */
+export async function createProjectLocalRuntimeArchiveControl(
+ options: ProjectLocalRuntimeArchiveControlOptions,
+): Promise<LocalRuntimeArchiveControl> {
+ const opened = await openProjectServices(options);
+ try {
+ const build = selectedService(opened, options.runtimeSelection.stores.build, "build-store");
+ const operations = selectedService(opened, options.runtimeSelection.stores.operations, "operation-store");
+ const dispatch = selectedService(opened, options.runtimeSelection.stores.dispatch, "dispatch-store");
+ const catalog = selectedBuildCatalog(opened.packages, options.runtimeSelection.stores.build);
+ return createLocalRuntimeArchiveControl({
+ buildStore: build.service,
+ ...(catalog === undefined ? {} : { buildCatalog: catalog }),
+ operationStore: operations.service,
+ dispatchStore: dispatch.service,
+ close: opened.close,
+ });
+ } catch (error) {
+ await opened.close();
+ throw error;
+ }
+}
+
+/** Assemble only the ArtifactStore selected by the Profile. */
+export async function createProjectLocalRuntimeArtifactAccess(
+ options: ProjectLocalRuntimeArtifactAccessOptions,
+): Promise<LocalRuntimeArtifactAccess> {
+ const opened = await openProjectServices(options);
+ try {
+ const artifacts = selectedService(opened, options.runtimeSelection.stores.artifacts, "artifact-store");
+ return createLocalRuntimeArtifactAccess({ artifactStore: artifacts.service, close: opened.close });
+ } catch (error) {
+ await opened.close();
+ throw error;
+ }
+}
+
+/** Assemble all durable Stores only for execution or explicit cross-store maintenance. */
export async function createProjectLocalRuntimeControl(
options: ProjectLocalRuntimeControlOptions,
): Promise<LocalRuntimeControl> {
- const root = options.root ?? process.cwd();
- const projectServices = await createProjectRuntimeServices(root, options);
- const services = projectServices.assembly;
- return createLocalRuntimeControl({
- buildStore: services.buildStore,
- ...(projectServices.catalog === undefined ? {} : { buildCatalog: projectServices.catalog }),
- operationStore: services.operationStore,
- dispatchStore: services.dispatchStore,
- artifactStore: services.artifactStore,
- close: projectServices.close,
- });
+ const opened = await openProjectServices(options);
+ try {
+ const build = selectedService(opened, options.runtimeSelection.stores.build, "build-store");
+ const operations = selectedService(opened, options.runtimeSelection.stores.operations, "operation-store");
+ const dispatch = selectedService(opened, options.runtimeSelection.stores.dispatch, "dispatch-store");
+ const artifacts = selectedService(opened, options.runtimeSelection.stores.artifacts, "artifact-store");
+ const catalog = selectedBuildCatalog(opened.packages, options.runtimeSelection.stores.build);
+ return createLocalRuntimeControl({
+ buildStore: build.service,
+ ...(catalog === undefined ? {} : { buildCatalog: catalog }),
+ operationStore: operations.service,
+ dispatchStore: dispatch.service,
+ artifactStore: artifacts.service,
+ close: opened.close,
+ });
+ } catch (error) {
+ await opened.close();
+ throw error;
+ }
}
diff --git a/packages/local/src/index.ts b/packages/local/src/index.ts
index f046ec25..76220028 100644
--- a/packages/local/src/index.ts
+++ b/packages/local/src/index.ts
@@ -2,7 +2,11 @@ export {
createLocalExecutionPackage,
} from "./scheduler-package.js";
export {
+ createLocalRuntimeArchiveControl,
+ createLocalRuntimeArtifactAccess,
createLocalRuntimeControl,
+ createProjectLocalRuntimeArchiveControl,
+ createProjectLocalRuntimeArtifactAccess,
createProjectLocalRuntimeControl,
} from "./control.js";
export { createLocalCredentialControl } from "./credentials.js";
@@ -12,7 +16,9 @@ export {
} from "./runtime.js";
export {
createRuntimeFromConfig,
- createRuntimeControlFromConfig,
+ createRuntimeArchiveFromConfig,
+ createRuntimeArtifactAccessFromConfig,
+ createRuntimeMaintenanceFromConfig,
createRuntimeCredentialsFromConfig,
declaredExternalServices,
doctorRuntimeConfig,
diff --git a/packages/local/src/project-services.ts b/packages/local/src/project-services.ts
index 0c6a56ff..173b2f1c 100644
--- a/packages/local/src/project-services.ts
+++ b/packages/local/src/project-services.ts
@@ -25,7 +25,7 @@ function assert(condition: unknown, message: string): asserts condition {
if (!condition) throw new Error(message);
}
-function selectedBuildCatalog(
+export function selectedBuildCatalog(
packages: readonly RuntimeServicePackage[],
buildStore: string,
): BuildCatalog | undefined {
diff --git a/packages/local/src/runtime.ts b/packages/local/src/runtime.ts
index addd444c..720c9cb7 100644
--- a/packages/local/src/runtime.ts
+++ b/packages/local/src/runtime.ts
@@ -20,6 +20,7 @@ import {
isStreamingArtifactStore,
nonTerminalDispatchPhases,
resolveRuntimeProfile,
+ sameBuildCatalogDescriptor,
sealRuntimeProfile,
} from "@narratage/runtime";
import { TypeValidatorRegistry } from "@narratage/validation";
@@ -172,6 +173,7 @@ export async function createLocalRuntime(
});
const stageAttachments = async (request: LocalBuildRequest): Promise<void> => {
for (const item of request.attachments ?? []) {
+ if (await options.artifactStore.has(item.artifact.digest)) continue;
const stream = await item.open();
const stored = isStreamingArtifactStore(options.artifactStore)
? await options.artifactStore.putStream(stream, item.artifact.mediaType)
@@ -212,6 +214,14 @@ export async function createLocalRuntime(
const submit = async (request: LocalBuildRequest): Promise<LocalBuildSubmission> => {
assert(request.id.trim().length > 0, "Build id must not be empty");
+ if (request.catalog !== undefined) {
+ assert(buildCatalog !== undefined, "Build supplied Host catalog metadata but no BuildCatalog was selected");
+ assert(request.catalog.core === request.state.id,
+ `Build Catalog Core ${request.catalog.core} differs from Build ${request.state.id}`);
+ const existingCatalog = await buildCatalog.read(request.id);
+ assert(existingCatalog === undefined || sameBuildCatalogDescriptor(existingCatalog, request.catalog),
+ `Build Catalog ${request.id} already has another source, Run Source or output naming`);
+ }
await stageAttachments(request);
let stored = await options.buildStore.read(request.id);
if (stored === undefined) {
@@ -231,10 +241,7 @@ export async function createLocalRuntime(
runtimeClosure,
}));
if (request.catalog !== undefined) {
- assert(buildCatalog !== undefined, "Build supplied Host catalog metadata but no BuildCatalog was selected");
- assert(request.catalog.core === request.state.id,
- `Build Catalog Core ${request.catalog.core} differs from Build ${request.state.id}`);
- await buildCatalog.record(request.id, request.catalog);
+ await buildCatalog!.record(request.id, request.catalog);
}
return await presentation(request.id);
};
diff --git a/packages/local/src/scheduler-package.ts b/packages/local/src/scheduler-package.ts
index 29134ff4..5a96a3bb 100644
--- a/packages/local/src/scheduler-package.ts
+++ b/packages/local/src/scheduler-package.ts
@@ -13,7 +13,7 @@ const localRuntimeModuleRef = {
} as const;
const localSchedulerImplementationDigest = digestOf("@narratage/local/scheduler@1");
-const localWorkerImplementationDigest = digestOf("@narratage/local/worker@1");
+const localWorkerImplementationDigest = digestOf("@narratage/local/worker@1/blocked-subject");
export function createLocalExecutionPackage(
instance: string,
diff --git a/packages/local/src/types.ts b/packages/local/src/types.ts
index 96243a80..74d4eac6 100644
--- a/packages/local/src/types.ts
+++ b/packages/local/src/types.ts
@@ -53,16 +53,24 @@ export type CreateLocalRuntimeOptions = {
readonly validators?: LocalTypeValidatorRegistry;
};
-export type CreateLocalRuntimeControlOptions = {
+export type CreateLocalRuntimeArchiveControlOptions = {
readonly buildStore: BuildStore;
readonly buildCatalog?: BuildCatalog;
readonly operationStore: OperationStore;
readonly dispatchStore: BuildDispatchStore;
+ /** Optional owner supplied by the project service assembly. */
+ readonly close?: () => Awaitable<void>;
+};
+
+export type CreateLocalRuntimeArtifactAccessOptions = {
readonly artifactStore: ArtifactStore;
/** Optional owner supplied by the project service assembly. */
readonly close?: () => Awaitable<void>;
};
+export type CreateLocalRuntimeControlOptions = CreateLocalRuntimeArchiveControlOptions
+ & CreateLocalRuntimeArtifactAccessOptions;
+
export type CreateLocalCredentialControlOptions = {
readonly credentialStore: CredentialStore;
readonly endpoints: readonly EndpointPackage[];
@@ -93,12 +101,15 @@ export type ProjectLocalRuntimeOptions = {
readonly validators?: LocalTypeValidatorRegistry;
};
-export type ProjectLocalRuntimeControlOptions = Pick<ProjectLocalRuntimeOptions,
+export type ProjectLocalRuntimeArchiveControlOptions = Pick<ProjectLocalRuntimeOptions,
| "root"
| "runtimeServices"
| "runtimeSelection"
>;
+export type ProjectLocalRuntimeArtifactAccessOptions = ProjectLocalRuntimeArchiveControlOptions;
+export type ProjectLocalRuntimeControlOptions = ProjectLocalRuntimeArchiveControlOptions;
+
export type LocalBuildRequest = {
/** Stable user/run identity. Reusing it resumes only the same Core Build identity. */
readonly id: string;
@@ -178,7 +189,26 @@ export type LocalRuntime = {
close(): Awaitable<void>;
};
-/** Durable project control that needs Stores but no Producer, Endpoint, Driver or Worker. */
+/** Durable execution-state archive that never opens the selected ArtifactStore. */
+export type LocalRuntimeArchiveControl = Pick<LocalRuntime,
+ | "status"
+ | "activity"
+ | "queue"
+ | "operation"
+ | "builds"
+ | "cancel"
+ | "cancelOperation"
+ | "close"
+>;
+
+/** Explicit Artifact byte access that never opens Build, Operation or Dispatch state. */
+export type LocalRuntimeArtifactAccess = Pick<LocalRuntime,
+ | "readArtifact"
+ | "openArtifact"
+ | "close"
+>;
+
+/** Durable project control used only when one operation truly spans state and Artifacts. */
export type LocalRuntimeControl = Pick<LocalRuntime,
| "status"
| "activity"
diff --git a/packages/local/src/worker.ts b/packages/local/src/worker.ts
index 8a6a0709..43bcdb75 100644
--- a/packages/local/src/worker.ts
+++ b/packages/local/src/worker.ts
@@ -360,7 +360,9 @@ class DurableLocalWorker implements RuntimeWorker {
? "waiting"
: result.blocked.length > 0 ? "blocked" : "queued",
availableAt: wakeAt,
- ...(result.blocked.length === 0 ? {} : { reason: result.blocked.map((item) => item.reason).join(", ") }),
+ ...(result.blocked.length === 0 ? {} : {
+ reason: result.blocked.map((item) => `${item.reason}: ${item.subject}`).join(", "),
+ }),
});
} catch (error) {
clearInterval(timer);
diff --git a/packages/local/test/config.test.ts b/packages/local/test/config.test.ts
index 1cc0de4b..615f8a35 100644
--- a/packages/local/test/config.test.ts
+++ b/packages/local/test/config.test.ts
@@ -13,9 +13,12 @@ import {
} from "@narratage/runtime-adapter";
import { digestOf } from "@narratage/protocol";
import { credentialRef, defineRuntimeServicePackage } from "@narratage/runtime";
+import { createSqliteRuntimeServicePackage } from "@narratage/store-sqlite";
import {
createRuntimeFromConfig,
+ createRuntimeArchiveFromConfig,
+ createRuntimeArtifactAccessFromConfig,
doctorRuntimeConfig,
parseRuntimeConfig,
runtimeConfigRevision,
@@ -36,6 +39,65 @@ const services = {
const required = { runtimeServices: [], services, scheduling: { maxConcurrency: 3 } } as const;
+test("archive observation does not construct the selected ArtifactStore adapter", async () => {
+ const root = await mkdtemp(join(tmpdir(), "svml-runtime-archive-slice-"));
+ const path = join(root, "svml.runtime.json");
+ await writeFile(path, JSON.stringify({
+ format: "svml.runtime-config@1",
+ runtimeServices: [
+ { use: "example.state", instance: "state", config: {} },
+ { use: "example.artifacts", instance: "artifacts", config: {} },
+ ],
+ services: {
+ scheduler: "execution.scheduler",
+ worker: "execution.worker",
+ stores: {
+ build: "state.builds",
+ operations: "state.operations",
+ dispatch: "state.dispatch",
+ artifacts: "artifacts.store",
+ credentials: [],
+ },
+ },
+ scheduling: { maxConcurrency: 1 },
+ endpoints: [],
+ }));
+ let artifactConstructions = 0;
+ const registry = new RuntimeAdapterRegistry();
+ registry.registerFacet(createRuntimeServiceAdapterFacet({
+ use: "example.state",
+ validate() {},
+ create(context) {
+ return createSqliteRuntimeServicePackage({
+ path: join(context.root, "runtime.sqlite"),
+ buildInstance: `${context.instance}.builds`,
+ operationInstance: `${context.instance}.operations`,
+ dispatchInstance: `${context.instance}.dispatch`,
+ readOnly: context.access === "read-only",
+ });
+ },
+ }));
+ registry.registerFacet(createRuntimeServiceAdapterFacet({
+ use: "example.artifacts",
+ validate() {},
+ create() {
+ artifactConstructions += 1;
+ throw new Error("artifact adapter constructed");
+ },
+ }));
+ try {
+ const archive = await createRuntimeArchiveFromConfig(path, { registry, readOnly: true });
+ assert.equal((await archive.status("missing")).build, undefined);
+ await archive.close();
+ assert.equal(artifactConstructions, 0);
+ await assert.rejects(createRuntimeArtifactAccessFromConfig(path, { registry, readOnly: true }),
+ /artifact adapter constructed/u);
+ assert.equal(artifactConstructions, 1);
+ } finally {
+ await rm(root, { recursive: true, force: true });
+ }
+});
+
test("Runtime revision follows content and referenced locks, not a filename suffix", async () => {
const root = await mkdtemp(join(tmpdir(), "svml-runtime-revision-"));
const path = join(root, "deployment.profile");
diff --git a/packages/local/test/local.test.ts b/packages/local/test/local.test.ts
index 42b481c3..497ea7e2 100644
--- a/packages/local/test/local.test.ts
+++ b/packages/local/test/local.test.ts
@@ -743,17 +743,34 @@ test("project local runtime accepts an explicitly selected replacement ArtifactS
assert.equal(blocked?.terminal, "failed");
assert.match(blocked?.reason ?? "", /does not bind demanded capability/u);
assert.deepEqual(await artifactStore.get(sourceArtifact.digest), bytes);
- await assert.rejects(
- runtime.build({
- id: "tampered-source-artifact",
- state: createGreetingBuild(),
- attachments: [{
- artifact: sourceArtifact,
- open: async () => (async function* () { yield new Uint8Array([0]); })(),
- }],
- }),
- /does not match its staged bytes/u,
- );
+ let reopened = false;
+ await runtime.build({
+ id: "existing-source-artifact",
+ state: createGreetingBuild(),
+ attachments: [{
+ artifact: sourceArtifact,
+ open: async () => {
+ reopened = true;
+ throw new Error("existing content-addressed bytes must not be reopened");
+ },
+ }],
+ });
+ assert.equal(reopened, false);
+ const absentBytes = new Uint8Array([10, 11, 12]);
+ const absentArtifact = {
+ kind: "blob" as const,
+ digest: `sha256:${createHash("sha256").update(absentBytes).digest("hex")}` as const,
+ size: absentBytes.byteLength,
+ mediaType: "application/octet-stream",
+ };
+ await assert.rejects(runtime.build({
+ id: "tampered-source-artifact",
+ state: createGreetingBuild(),
+ attachments: [{
+ artifact: absentArtifact,
+ open: async () => (async function* () { yield new Uint8Array([0]); })(),
+ }],
+ }), /does not match its staged bytes/u);
await runtime.close();
} finally {
await rm(directory, { recursive: true, force: true });
diff --git a/packages/runtime/src/catalog.ts b/packages/runtime/src/catalog.ts
index 8f3acb75..e6815aca 100644
--- a/packages/runtime/src/catalog.ts
+++ b/packages/runtime/src/catalog.ts
@@ -1,4 +1,4 @@
-import { isDigest } from "@narratage/protocol";
+import { canonicalStringify, isDigest } from "@narratage/protocol";
import type {
Digest,
LogicalOutputRef,
@@ -71,6 +71,24 @@ function copy<T>(value: T): T {
return structuredClone(value);
}
+function descriptorValue(descriptor: BuildCatalogDescriptor): BuildCatalogDescriptor {
+ return {
+ format: descriptor.format,
+ core: descriptor.core,
+ source: descriptor.source,
+ ...(descriptor.run === undefined ? {} : { run: descriptor.run }),
+ aliases: descriptor.aliases,
+ };
+}
+
+/** One Build id keeps the exact Host presentation under which it was first submitted. */
+export function sameBuildCatalogDescriptor(
+ left: BuildCatalogDescriptor,
+ right: BuildCatalogDescriptor,
+): boolean {
+ return canonicalStringify(descriptorValue(left)) === canonicalStringify(descriptorValue(right));
+}
+
export class MemoryBuildCatalog implements BuildCatalog {
readonly #entries = new Map<string, BuildCatalogEntry>();
readonly #now: () => number;
@@ -84,15 +102,17 @@ export class MemoryBuildCatalog implements BuildCatalog {
verifyBuildCatalogDescriptor(descriptor);
const existing = this.#entries.get(build);
if (existing !== undefined) {
- assert(existing.core === descriptor.core, `Build Catalog ${build} already names another Core Build`);
+ assert(sameBuildCatalogDescriptor(existing, descriptor),
+ `Build Catalog ${build} already has another source, Run Source or output naming`);
+ return copy(existing);
}
const now = this.#now();
assert(Number.isSafeInteger(now) && now >= 0, "Build Catalog clock returned an invalid time");
const entry: BuildCatalogEntry = {
...copy(descriptor),
build,
- createdAt: existing?.createdAt ?? now,
- updatedAt: Math.max(now, existing?.updatedAt ?? now),
+ createdAt: now,
+ updatedAt: now,
};
verifyBuildCatalogEntry(entry);
this.#entries.set(build, entry);
diff --git a/packages/runtime/src/index.ts b/packages/runtime/src/index.ts
index 9a263dae..dc345c3d 100644
--- a/packages/runtime/src/index.ts
+++ b/packages/runtime/src/index.ts
@@ -2,6 +2,7 @@ export { plannedNeeds } from "@narratage/core";
export { MemoryBuildStore } from "./memory.js";
export {
MemoryBuildCatalog,
+ sameBuildCatalogDescriptor,
verifyBuildCatalogDescriptor,
verifyBuildCatalogEntry,
} from "./catalog.js";
diff --git a/packages/runtime/test/catalog.test.ts b/packages/runtime/test/catalog.test.ts
index ac9f9eee..71ab9613 100644
--- a/packages/runtime/test/catalog.test.ts
+++ b/packages/runtime/test/catalog.test.ts
@@ -17,7 +17,7 @@ function descriptor(core = digestOf("core:one")) {
};
}
-test("Build Catalog indexes Host aliases without becoming Build truth", async () => {
+test("Build Catalog freezes the first Host presentation for a Build id", async () => {
let now = 100;
const catalog = new MemoryBuildCatalog(() => now);
const first = await catalog.record("delivery-01", descriptor());
@@ -25,19 +25,15 @@ test("Build Catalog indexes Host aliases without becoming Build truth", async ()
assert.equal(first.aliases[0]?.name, "final.video");
now = 200;
- const updated = await catalog.record("delivery-01", {
- ...descriptor(),
- aliases: [...descriptor().aliases, {
- name: "timing.map",
- ref: { kind: "logical-output" as const, id: "logical:timing" },
- }],
- });
- assert.equal(updated.createdAt, 100);
- assert.equal(updated.updatedAt, 200);
- assert.deepEqual(updated.aliases.map((item) => item.name), ["final.video", "timing.map"]);
+ const repeated = await catalog.record("delivery-01", descriptor());
+ assert.equal(repeated.createdAt, 100);
+ assert.equal(repeated.updatedAt, 100);
+ await assert.rejects(catalog.record("delivery-01", {
+ ...descriptor(), aliases: [{ ...descriptor().aliases[0]!, name: "renamed.video" }],
+ }), /another source, Run Source or output naming/u);
assert.deepEqual((await catalog.list()).map((item) => item.build), ["delivery-01"]);
- (updated as unknown as { aliases: { name: string }[] }).aliases[0]!.name = "tampered";
+ (repeated as unknown as { aliases: { name: string }[] }).aliases[0]!.name = "tampered";
assert.equal((await catalog.read("delivery-01"))?.aliases[0]?.name, "final.video");
});
@@ -46,6 +42,6 @@ test("Build Catalog cannot retarget one Build id to another Core Build", async (
await catalog.record("delivery-01", descriptor());
await assert.rejects(
catalog.record("delivery-01", descriptor(digestOf("core:two"))),
- /another Core Build/u,
+ /another source, Run Source or output naming/u,
);
});
diff --git a/packages/store-sqlite/src/store.ts b/packages/store-sqlite/src/store.ts
index f2d55447..952026dc 100644
--- a/packages/store-sqlite/src/store.ts
+++ b/packages/store-sqlite/src/store.ts
@@ -13,6 +13,7 @@ import type {
} from "@narratage/protocol";
import {
assertRuntimeClosureAdmission,
+ sameBuildCatalogDescriptor,
verifyBuildCatalogDescriptor,
verifyBuildCatalogEntry,
defineRuntimeServicePackage,
@@ -275,22 +276,22 @@ class SqliteBuildCatalog implements BuildCatalog {
async record(build: string, descriptor: BuildCatalogDescriptor): Promise<BuildCatalogEntry> {
assert(build.trim().length > 0, "Build Catalog build id must not be empty");
verifyBuildCatalogDescriptor(descriptor);
+ const existing = await this.read(build);
+ if (existing !== undefined) {
+ assert(sameBuildCatalogDescriptor(existing, descriptor),
+ `Build Catalog ${build} already has another source, Run Source or output naming`);
+ return existing;
+ }
const now = Date.now();
- const inserted = this.#database.prepare(`
+ this.#database.prepare(`
INSERT OR IGNORE INTO svml_build_catalog (
build_id, core_id, created_at, updated_at, descriptor_json
) VALUES (?, ?, ?, ?, ?)
`).run(build, descriptor.core, now, now, canonicalStringify(descriptor));
- if (inserted.changes !== 1) {
- const updated = this.#database.prepare(`
- UPDATE svml_build_catalog
- SET updated_at = MAX(updated_at, ?), descriptor_json = ?
- WHERE build_id = ? AND core_id = ?
- `).run(now, canonicalStringify(descriptor), build, descriptor.core);
- assert(updated.changes === 1, `Build Catalog ${build} already names another Core Build`);
- }
const stored = await this.read(build);
if (stored === undefined) throw new Error(`Build Catalog ${build} disappeared after record`);
+ assert(sameBuildCatalogDescriptor(stored, descriptor),
+ `Build Catalog ${build} already has another source, Run Source or output naming`);
return stored;
}
diff --git a/packages/store-sqlite/test/store.test.ts b/packages/store-sqlite/test/store.test.ts
index fa100ba6..c6439d2d 100644
--- a/packages/store-sqlite/test/store.test.ts
+++ b/packages/store-sqlite/test/store.test.ts
@@ -40,7 +40,7 @@ test("SQLite stores verified Build facts and Operation checkpoints across reopen
const initial = createGreetingBuild();
const created = await first.builds.create("video", initial);
assert.equal(created.revision, 0);
- await first.catalog.record("video", {
+ const catalog = {
format: "svml.build-catalog-descriptor@1",
core: initial.id,
source: { path: "/project/main.svml", closure: digestOf("source-closure") },
@@ -48,7 +48,13 @@ test("SQLite stores verified Build facts and Operation checkpoints across reopen
name: "final.video",
ref: { kind: "logical-output", id: initial.request.targets[0]!.output },
}],
- });
+ } as const;
+ await first.catalog.record("video", catalog);
+ assert.equal((await first.catalog.record("video", catalog)).aliases[0]?.name, "final.video");
+ await assert.rejects(first.catalog.record("video", {
+ ...catalog,
+ aliases: [{ ...catalog.aliases[0], name: "renamed.video" }],
+ }), /another source, Run Source or output naming/u);
const operation = sealOperationIdentity({
build: "video",
command: "command:generation",
diff --git a/packages/video-cli/src/distribution.ts b/packages/video-cli/src/distribution.ts
index ce0a0485..cba27ea5 100644
--- a/packages/video-cli/src/distribution.ts
+++ b/packages/video-cli/src/distribution.ts
@@ -38,9 +38,23 @@ export const videoCliDistribution: CliDistribution = {
const { createVideoRuntimeFromConfig } = await import("./runtime-config.js");
return await createVideoRuntimeFromConfig(path, packageRoot, options?.implementationPackages);
},
- createRuntimeControlFromConfig: async (path, options) => {
- const { createRuntimeControlFromConfig } = await import("@narratage/local/config");
- return await createRuntimeControlFromConfig(path, {
+ createRuntimeArchiveFromConfig: async (path, options) => {
+ const { createRuntimeArchiveFromConfig } = await import("@narratage/local/config");
+ return await createRuntimeArchiveFromConfig(path, {
+ packageRoot,
+ ...(options?.readOnly === undefined ? {} : { readOnly: options.readOnly }),
+ });
+ },
+ createRuntimeArtifactAccessFromConfig: async (path, options) => {
+ const { createRuntimeArtifactAccessFromConfig } = await import("@narratage/local/config");
+ return await createRuntimeArtifactAccessFromConfig(path, {
+ packageRoot,
+ ...(options?.readOnly === undefined ? {} : { readOnly: options.readOnly }),
+ });
+ },
+ createRuntimeMaintenanceFromConfig: async (path, options) => {
+ const { createRuntimeMaintenanceFromConfig } = await import("@narratage/local/config");
+ return await createRuntimeMaintenanceFromConfig(path, {
packageRoot,
...(options?.readOnly === undefined ? {} : { readOnly: options.readOnly }),
});
diff --git a/packages/video-cli/test/cli.test.ts b/packages/video-cli/test/cli.test.ts
index f9d6608f..7fe07259 100644
--- a/packages/video-cli/test/cli.test.ts
+++ b/packages/video-cli/test/cli.test.ts
@@ -773,7 +773,8 @@ test("CLI inspect and get read the durable Build archive independently of build
};
const selected = {
runtimeProfileRevision: async () => "test-revision",
- createRuntimeControlFromConfig: async () => control,
+ createRuntimeArchiveFromConfig: async () => control,
+ createRuntimeArtifactAccessFromConfig: async () => control,
} as unknown as CliDistribution;
const runArchiveCli = (
argv: readonly string[],