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.
TypeScript agentic-aiaiai-agentsai-videocompilerdslffmpeggenerative-aillmmarkup-languagemonorepoplugin-systemprogramming-languagetext-to-videotypescriptvideovideo-automationvideo-clonevideo-editingvideo-generation
refactor: simplify runtime state and scheduling
kashorin4256db2fca40
diff --git a/.agents/skills/narratage/references/environment.md b/.agents/skills/narratage/references/environment.mdindex 46f0dc11..28bbfe90 100644--- a/.agents/skills/narratage/references/environment.md+++ b/.agents/skills/narratage/references/environment.md@@ -44,16 +44,5 @@ Use `runtime up` for normal Build preparation: it owns the detached durable Workprograms declared by the Runtime Profile. `programs up/status/down` is only the narrow external-program view and does not manage the Worker. See `references/runtime.md` for the Build lifecycle.-After intentionally installing, removing or changing selected packages, refresh both explicit-project locks in one reviewed action:--```text-narratage runtime use narratage.runtime.json-```--This command derives Author roots from that Run/Author Source closure and Runtime roots from the-Profile's explicit `use` entries, then records the exact installed closures. It does not install-packages, discover a default set or mutate SVML/Run intent.-The repository owns skill discovery: `.agents/skills` is canonical, with repository-root`.codex/skills` and `.claude/skills` links. This is checkout layout, not a skill setup operation.diff --git a/.agents/skills/narratage/scripts/check-environment.mjs b/.agents/skills/narratage/scripts/check-environment.mjsindex c72c5f4d..014ef458 100755--- a/.agents/skills/narratage/scripts/check-environment.mjs+++ b/.agents/skills/narratage/scripts/check-environment.mjs@@ -1,9 +1,17 @@#!/usr/bin/env nodeimport { execFileSync } from "node:child_process";import process from "node:process";-for (const command of ["node", "pnpm", "corepack", "ffmpeg", "ffprobe", "uv"]) {+const commands = [+ ["node", "--version"],+ ["pnpm", "--version"],+ ["corepack", "--version"],+ ["ffmpeg", "-version"],+ ["ffprobe", "-version"],+ ["uv", "--version"],+];+for (const [command, versionFlag] of commands) {try {- const version = execFileSync(command, ["--version"], { encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] }).trim().split(/\r?\n/, 1)[0];+ const version = execFileSync(command, [versionFlag], { encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] }).trim().split(/\r?\n/, 1)[0];console.log(`${command}\tok\t${version}`);} catch { console.log(`${command}\tmissing`); }}diff --git a/README.md b/README.mdindex f6469fce..39693e4f 100644--- a/README.md+++ b/README.md@@ -142,7 +142,7 @@ Narratage separates authorship from execution:| `.svml` | What video to make, including the script, generated media and tracks || `.svs` | Reusable creative choices such as prompts, styles and layout || `.svrun` | What to build this time, including targets and prior results to reuse |-| Runtime profile | How and where to run, including providers, storage and concurrency |+| Runtime profile | How and where to run, including providers, credentials and storage |A project can have any number of these files, and the same `.svml` can be built by many `.svrun` files.diff --git a/bin/narratage.mjs b/bin/narratage.mjsindex cbf05c10..c0a76dc3 100755--- a/bin/narratage.mjs+++ b/bin/narratage.mjs@@ -2,6 +2,13 @@import { register } from "tsx/esm/api";+const emitWarning = process.emitWarning;+process.emitWarning = function narratageWarning(warning, ...args) {+ const message = warning instanceof Error ? warning.message : String(warning);+ if (message === "SQLite is an experimental feature and might change at any time") return;+ return emitWarning.call(process, warning, ...args);+};+process.env.NARRATAGE_CLI_LAUNCHER ??= import.meta.filename;register();await import("../packages/video-cli/src/cli.ts");diff --git a/docs/guide/runtime.md b/docs/guide/runtime.mdindex eec9b30b..ddac2d97 100644--- a/docs/guide/runtime.md+++ b/docs/guide/runtime.md@@ -40,8 +40,7 @@ The Profile contains only environmental choices that genuinely vary:},"endpoints": {"media": { "use": "@narratage/provider-media-local" }- },- "concurrency": 4+ }}}}@@ -50,7 +49,6 @@ The Profile contains only environmental choices that genuinely vary:* `artifacts` selects where generated bytes are stored.* `credentials` selects stores for explicit credential references.* `endpoints` selects exact Provider implementations and their configuration.-* `concurrency` bounds commands running in this local Worker process.Installing a package only makes an implementation available. A Profile must select it. The Profilecontains no Workspace, author imports or creative routing.@@ -78,9 +76,9 @@ Runtime pointer, or the entry source directory. Runtime configuration cannot wid## Queue and concurrency-`build` admits a fresh Build and returns. The Worker may advance multiple Builds concurrently. All of-them share the Profile's process-wide command limit. Endpoint packages additionally declare Provider-and model limits, so work from separate Builds still respects one shared external quota.+`build` admits a fresh Build and returns. The Worker advances every runnable Build while Core+preserves each Build's graph dependencies. Endpoint packages declare Provider and capability limits,+so work from separate Builds shares the exact external capacity that performs it.Cancellation is best effort. Queued work is withdrawn before it starts; running work stops receivingnew commands and an Endpoint may try to cancel an already submitted external operation. Completeddiff --git a/docs/zh/guide/runtime.md b/docs/zh/guide/runtime.mdindex 355424a0..2c00ebb9 100644--- a/docs/zh/guide/runtime.md+++ b/docs/zh/guide/runtime.md@@ -39,15 +39,14 @@ Profile 只保留真正会随环境变化的选择:},"endpoints": {"media": { "use": "@narratage/provider-media-local" }- },- "concurrency": 4+ }}}}````artifacts` 决定产物字节放在哪里;`credentials` 选择凭证存储;`endpoints` 选择明确的-Provider 实现;`concurrency` 限制这个本地 Worker 同时执行的 Command 数量。+Provider 实现。安装包只增加一种可选实现,不会自动激活。Profile 不包含 Workspace、作者 import 或隐藏的创作路由。@@ -75,9 +74,9 @@ Workspace 独立由显式 `--workspace`、Runtime 指针所在项目或入口 So## 队列与并发-`build` 保存一个全新 Build 后立即返回。Worker 可以并行推进多个 Build,它们共同受 Profile 的-进程级并发限制。Endpoint 包还会声明 Provider 与模型自身的限制,因此不同 Build 的外部调用仍-共享同一额度。+`build` 保存一个全新 Build 后立即返回。Worker 推进所有可运行的 Build,Core 保留每个+Build 的图依赖。Endpoint 包声明 Provider 与 capability 上限,因此不同 Build 的工作共享真正+执行它们的外部容量。取消是尽力而为:未开始的工作直接撤回;运行中的 Build 不再接收新 Command,Endpoint 可以尝试取消已经提交的外部 Operation。已经完成的产物永远保留,不做回滚。diff --git a/examples/all-components-preview/narratage.runtime.json b/examples/all-components-preview/narratage.runtime.jsonindex 0f147a6e..7d8a7c2d 100644--- a/examples/all-components-preview/narratage.runtime.json+++ b/examples/all-components-preview/narratage.runtime.json@@ -42,8 +42,7 @@"defaultConcurrency": 1}}- },- "concurrency": 4+ }}}}diff --git a/examples/talking-film-live/README.md b/examples/talking-film-live/README.mdindex 565c9ebc..ba0d1bfc 100644--- a/examples/talking-film-live/README.md+++ b/examples/talking-film-live/README.md@@ -49,7 +49,7 @@ narratage get <build-id> \observes the detached Worker; closing this terminal does not cancel the Build.`build.svrun` owns the Targets and any explicit Candidate selections. `narratage.runtime.json` owns Provider instances,-credentials and concurrency. The Build archives all accepted intermediate Records and referenced+credentials and their capacity. The Build archives all accepted intermediate Records and referencedArtifacts even when no destination path is requested. `get` only makes an optional copy of thealready archived named target. The CLI accepts the declarative JSON Runtime Profile shown here;applications that embed Narratage assemble Runtime roles directly through `@narratage/runtime-local`.diff --git a/examples/talking-film-live/narratage.runtime.json b/examples/talking-film-live/narratage.runtime.jsonindex 6aecf71d..71679b1a 100644--- a/examples/talking-film-live/narratage.runtime.json+++ b/examples/talking-film-live/narratage.runtime.json@@ -36,8 +36,7 @@"use": "@narratage/provider-hyperframes-local","config": { "workers": 2, "quality": "standard", "defaultConcurrency": 1 }}- },- "concurrency": 4+ }}}}diff --git a/examples/talking-head-aroll/narratage.runtime.json b/examples/talking-head-aroll/narratage.runtime.jsonindex 471304af..9aaf9004 100644--- a/examples/talking-head-aroll/narratage.runtime.json+++ b/examples/talking-head-aroll/narratage.runtime.json@@ -37,8 +37,7 @@"use": "@narratage/provider-hyperframes-local","config": { "workers": 2, "quality": "standard", "defaultConcurrency": 1 }}- },- "concurrency": 4+ }}}}diff --git a/packages/artifact-store-fs/src/store.ts b/packages/artifact-store-fs/src/store.tsindex cfacbc92..e34a14c9 100644--- a/packages/artifact-store-fs/src/store.ts+++ b/packages/artifact-store-fs/src/store.ts@@ -3,10 +3,8 @@ import {link,mkdir,open as openFile,- readdir,rm,stat,- unlink,} from "node:fs/promises";import { dirname, join, resolve } from "node:path";@@ -136,34 +134,4 @@ export class FileArtifactStore implements ArtifactStore {}}- async list(): Promise<readonly Digest[]> {- const algorithm = join(this.root, "sha256");- let prefixes;- try {- prefixes = await readdir(algorithm, { withFileTypes: true });- } catch (error) {- if (isNodeError(error, "ENOENT")) return [];- throw error;- }- const digests: Digest[] = [];- for (const prefix of prefixes) {- if (!prefix.isDirectory() || !/^[0-9a-f]{2}$/u.test(prefix.name)) continue;- for (const entry of await readdir(join(algorithm, prefix.name), { withFileTypes: true })) {- if (entry.isFile() && /^[0-9a-f]{64}$/u.test(entry.name) && entry.name.startsWith(prefix.name)) {- digests.push(`sha256:${entry.name}` as Digest);- }- }- }- return digests.sort();- }-- async delete(digest: Digest): Promise<boolean> {- try {- await unlink(digestPath(this.root, digest));- return true;- } catch (error) {- if (isNodeError(error, "ENOENT")) return false;- throw error;- }- }}diff --git a/packages/artifact-store-fs/test/store.test.ts b/packages/artifact-store-fs/test/store.test.tsindex 8be4427c..1b4eed1e 100644--- a/packages/artifact-store-fs/test/store.test.ts+++ b/packages/artifact-store-fs/test/store.test.ts@@ -25,7 +25,7 @@ test("filesystem artifacts are content-addressed and survive adapter restart", a}});-test("filesystem artifacts expose optional streaming and explicit retention capabilities", async () => {+test("filesystem artifacts stream writes and reads", async () => {const directory = await mkdtemp(join(tmpdir(), "narratage-artifacts-stream-"));try {const store = new FileArtifactStore(directory);@@ -33,15 +33,11 @@ test("filesystem artifacts expose optional streaming and explicit retention capayield new TextEncoder().encode("streamed ");yield new TextEncoder().encode("artifact");})(), "application/octet-stream");- assert.deepEqual(await store.list(), [artifact.digest]);const opened = await store.open(artifact.digest);assert.notEqual(opened, undefined);const values: number[] = [];for await (const chunk of opened!) values.push(...chunk);assert.equal(new TextDecoder().decode(Uint8Array.from(values)), "streamed artifact");- assert.equal(await store.delete(artifact.digest), true);- assert.equal(await store.delete(artifact.digest), false);- assert.deepEqual(await store.list(), []);} finally {await rm(directory, { recursive: true, force: true });}diff --git a/packages/artifact-store-s3/src/client.ts b/packages/artifact-store-s3/src/client.tsindex 3db52cfe..0de355a8 100644--- a/packages/artifact-store-s3/src/client.ts+++ b/packages/artifact-store-s3/src/client.ts@@ -6,7 +6,6 @@ import {DeleteObjectCommand,GetObjectCommand,HeadObjectCommand,- ListObjectsV2Command,PutObjectCommand,S3Client,UploadPartCommand,@@ -18,21 +17,15 @@ import type {CreateMultipartUploadCommandInput,DeleteObjectCommandInput,GetObjectCommandInput,- ListObjectsV2CommandInput,PutObjectCommandInput,S3ClientConfig,UploadPartCommandInput,} from "@aws-sdk/client-s3";-export type S3ListPage = {- readonly keys: readonly string[];- readonly continuationToken?: string;-};-/*** `put` and `get` are the whole port. The rest are optional: a client that* omits them leaves the store implementing only the three-method ArtifactStore,- * which is exactly what the optional Streaming and Managed facets mean.+ * which is exactly what the optional Streaming facet means.*/export type S3ObjectClient = {put(input: PutObjectCommandInput): Promise<void>;@@ -51,7 +44,6 @@ export type S3ObjectClient = {abortMultipart?(input: AbortMultipartUploadCommandInput): Promise<void>;copy?(input: CopyObjectCommandInput): Promise<void>;delete?(input: DeleteObjectCommandInput): Promise<void>;- list?(input: ListObjectsV2CommandInput): Promise<S3ListPage>;};function statusCode(error: unknown): number | undefined {@@ -149,16 +141,4 @@ export class AwsS3ObjectClient implements S3ObjectClient {await this.#client.send(new DeleteObjectCommand(input));}- async list(input: ListObjectsV2CommandInput): Promise<S3ListPage> {- const response = await this.#client.send(new ListObjectsV2Command(input));- const keys = (response.Contents ?? [])- .map((item) => item.Key)- .filter((key): key is string => key !== undefined);- return {- keys,- ...(response.IsTruncated === true && response.NextContinuationToken !== undefined- ? { continuationToken: response.NextContinuationToken }- : {}),- };- }}diff --git a/packages/artifact-store-s3/src/index.ts b/packages/artifact-store-s3/src/index.tsindex 320f35ae..fff882f2 100644--- a/packages/artifact-store-s3/src/index.ts+++ b/packages/artifact-store-s3/src/index.ts@@ -1,4 +1,4 @@export { AwsS3ObjectClient } from "./client.js";-export type { S3ListPage, S3ObjectClient } from "./client.js";+export type { S3ObjectClient } from "./client.js";export { S3ArtifactStore, createS3ArtifactStore, s3ArtifactKey, s3ArtifactStoreModuleRef } from "./store.js";export type * from "./store.js";diff --git a/packages/artifact-store-s3/src/store.ts b/packages/artifact-store-s3/src/store.tsindex a8c55116..4da770b9 100644--- a/packages/artifact-store-s3/src/store.ts+++ b/packages/artifact-store-s3/src/store.ts@@ -5,7 +5,7 @@ import type { BlobRef, Digest } from "@narratage/protocol";import type { ArtifactStore } from "@narratage/runtime";import { AwsS3ObjectClient } from "./client.js";-import type { S3ListPage, S3ObjectClient } from "./client.js";+import type { S3ObjectClient } from "./client.js";export const s3ArtifactStoreModuleRef = {name: "@narratage/artifact-store-s3",@@ -71,8 +71,8 @@ function positiveInteger(value: number, subject: string): number {/*** Content-addressed S3 ArtifactStore.*- * The streaming and retention facets are detected by method presence, so this- * store attaches them only when the injected client can back them. Claiming a+ * The streaming facet is detected by method presence, so this+ * store attaches it only when the injected client can back it. Claiming a* facet and then throwing would be worse than not claiming it: a deployment* would discover the gap during a Build rather than when it chose the client.*/@@ -85,9 +85,6 @@ export class S3ArtifactStore implements ArtifactStore {/** Present only when the client can stream and upload in parts. */readonly open?: (digest: Digest) => Promise<AsyncIterable<Uint8Array> | undefined>;readonly putStream?: (chunks: AsyncIterable<Uint8Array>, mediaType: string) => Promise<BlobRef>;- /** Present only when the client can enumerate and remove objects. */- readonly list?: () => Promise<readonly Digest[]>;- readonly delete?: (digest: Digest) => Promise<boolean>;constructor(options: S3ArtifactStoreOptions) {assert(options.bucket.trim().length > 0, "S3 Artifact bucket must not be empty");@@ -106,10 +103,6 @@ export class S3ArtifactStore implements ArtifactStore {this.open = (digest) => this.#openStream(digest);this.putStream = (chunks, mediaType) => this.#putStream(chunks, mediaType);}- if (client.list !== undefined && client.delete !== undefined && client.head !== undefined) {- this.list = () => this.#list();- this.delete = (digest) => this.#delete(digest);- }}key(digest: Digest): string {@@ -266,43 +259,6 @@ export class S3ArtifactStore implements ArtifactStore {return { kind: "blob", digest, size, mediaType };}- /** Every Artifact this store holds. Staging keys are not Artifacts and are not listed. */- async #list(): Promise<readonly Digest[]> {- const root = this.#prefix.length === 0 ? "sha256/" : `${this.#prefix}/sha256/`;- const digests: Digest[] = [];- let continuationToken: string | undefined;- do {- const page: S3ListPage = await this.#client.list!({- Bucket: this.#bucket,- Prefix: root,- ...(continuationToken === undefined ? {} : { ContinuationToken: continuationToken }),- ...(this.#expectedBucketOwner === undefined- ? {}- : { ExpectedBucketOwner: this.#expectedBucketOwner }),- });- for (const key of page.keys) {- const hex = key.slice(root.length).split("/").at(-1);- if (hex === undefined) continue;- const digest = `sha256:${hex}`;- // A key this store did not write is not reported as an Artifact.- if (isDigest(digest) && this.key(digest as Digest) === key) digests.push(digest as Digest);- }- continuationToken = page.continuationToken;- } while (continuationToken !== undefined);- return digests;- }-- /** True when the Artifact was there to remove. */- async #delete(digest: Digest): Promise<boolean> {- const key = this.key(digest);- const owner = this.#expectedBucketOwner === undefined- ? {}- : { ExpectedBucketOwner: this.#expectedBucketOwner };- const existing = await this.#client.head!({ Bucket: this.#bucket, Key: key, ...owner });- if (existing === undefined) return false;- await this.#client.delete!({ Bucket: this.#bucket, Key: key, ...owner });- return true;- }}export function createS3ArtifactStore(diff --git a/packages/artifact-store-s3/test/store.test.ts b/packages/artifact-store-s3/test/store.test.tsindex a0c8b692..0c1059a2 100644--- a/packages/artifact-store-s3/test/store.test.ts+++ b/packages/artifact-store-s3/test/store.test.ts@@ -3,7 +3,7 @@ import test from "node:test";import { S3ArtifactStore } from "@narratage/artifact-store-s3";import type { S3ObjectClient } from "@narratage/artifact-store-s3";-import { isManagedArtifactStore, isStreamingArtifactStore } from "@narratage/runtime";+import { isStreamingArtifactStore } from "@narratage/runtime";class FakeS3 implements S3ObjectClient {readonly values = new Map<string, Uint8Array>();@@ -92,19 +92,13 @@ class FullFakeS3 extends FakeS3 {this.values.delete(input.Key!);}- async list(input: Parameters<NonNullable<S3ObjectClient["list"]>>[0]) {- const keys = [...this.values.keys()].filter((key) => key.startsWith(input.Prefix ?? "")).sort();- return { keys };- }}-test("a client that cannot stream or enumerate makes the store admit it, rather than throw later", () => {+test("streaming is exposed only when the client supports it", () => {const store = new S3ArtifactStore({ client: new FakeS3(), bucket: "fixture" });assert.equal(isStreamingArtifactStore(store), false);- assert.equal(isManagedArtifactStore(store), false);const full = new S3ArtifactStore({ client: new FullFakeS3(), bucket: "fixture" });assert.equal(isStreamingArtifactStore(full), true);- assert.equal(isManagedArtifactStore(full), true);});test("a streamed Artifact reaches the content-addressed key it earned by being hashed", async () => {@@ -148,17 +142,3 @@ test("presence uses object metadata without downloading bytes", async () => {assert.equal(client.heads, 1);assert.equal(client.gets, gets, "has did not download the object");});--test("retention reports the Artifacts it stored, and whether a delete found one", async () => {- const client = new FullFakeS3();- const store = new S3ArtifactStore({ client, bucket: "fixture", prefix: "svml" });- const one = await store.put(new TextEncoder().encode("one"), "text/plain");- const two = await store.put(new TextEncoder().encode("two"), "text/plain");- // A key under the same prefix that this store did not write is not an Artifact.- client.values.set("svml/sha256/zz/not-a-digest", new Uint8Array(1));-- assert.deepEqual([...await store.list!()].sort(), [one.digest, two.digest].sort());- assert.equal(await store.delete!(one.digest), true);- assert.equal(await store.delete!(one.digest), false, "deleting what is gone is not an error");- assert.deepEqual(await store.list!(), [two.digest]);-});diff --git a/packages/cli/src/main.ts b/packages/cli/src/main.tsindex 9582f579..25786dc3 100644--- a/packages/cli/src/main.ts+++ b/packages/cli/src/main.ts@@ -29,7 +29,6 @@ import type {CliRuntimeArchiveControl,CliRuntimeArtifactAccess,CliRuntimeController,- CliRuntimeMaintenance,} from "./runtime-port.js";import { writeCliHelp, writeCliOutput } from "./output.js";import type { CliColorMode, CliIo } from "./output.js";@@ -59,7 +58,6 @@ type ParsedArgs = {readonly name: string | undefined;readonly artifact: string | undefined;readonly to: string | undefined;- readonly apply: boolean;/** Leave the declared external programs alone; build against what is running. */readonly noPrograms: boolean;readonly json: boolean;@@ -119,7 +117,6 @@ function parseArgs(argv: readonly string[]): ParsedArgs {let name: string | undefined;let artifact: string | undefined;let to: string | undefined;- let apply = false;let noPrograms = false;let json = false;let color: CliColorMode = "auto";@@ -137,7 +134,7 @@ function parseArgs(argv: readonly string[]): ParsedArgs {if (item.startsWith("--")) {const repeatable = ["--json", "--jsonl", "--watch", "--verbose", "--debug",- "--no-color", "--follow", "--apply", "--no-programs", "--asset-root",+ "--no-color", "--follow", "--no-programs", "--asset-root",].includes(item);if (!repeatable && seenOptions.has(item)) throw new Error(`${item} cannot be repeated`);seenOptions.add(item);@@ -244,10 +241,6 @@ function parseArgs(argv: readonly string[]): ParsedArgs {follow = true;continue;}- if (item === "--apply") {- apply = true;- continue;- }if (item === "--no-programs") {noPrograms = true;continue;@@ -314,7 +307,6 @@ function parseArgs(argv: readonly string[]): ParsedArgs {name,artifact,to,- apply,noPrograms,json,color,@@ -338,15 +330,12 @@ function assertCommandOptions(args: ParsedArgs): void {case "programs":// This command has older, more specific diagnostics for deployment-selection// flags and waiting on status/down; let its handler render those repairs.- add("--max-wait-ms", "--runtime", "--apply");+ add("--max-wait-ms", "--runtime");break;case "runtime":add("--runtime");if (args.action === "up" || args.action === "down") add("--max-wait-ms");break;- case "gc":- add("--apply");- break;case "auth":add("--runtime", "--slot");if (args.action === "login") add("--from");@@ -404,7 +393,6 @@ function usage(): string {" narratage runtime unset"," narratage runtime up|status|logs|down [<runtime-profile>]"," narratage queue [--runtime profile.json] [--watch]",- " narratage gc [<runtime-profile>] [--apply]"," narratage check <self-described-source> [--runtime profile.json] [--workspace workspace] [--asset-root directory]"," narratage plan <run-source> [--runtime profile.json] [--workspace workspace] [--asset-root directory]"," narratage build <run-source> [--runtime profile.json] [--workspace workspace] [--asset-root directory] [--follow] [--no-programs]",@@ -529,16 +517,8 @@ async function loadRuntimeArchive(async function loadRuntimeArtifactAccess(host: NodeRuntimeHost,- readOnly = true,): Promise<CliRuntimeArtifactAccess> {- return await host.openArtifacts({ readOnly });-}--async function loadRuntimeMaintenance(- host: NodeRuntimeHost,- readOnly = true,-): Promise<CliRuntimeMaintenance> {- return await host.openMaintenance({ readOnly });+ return await host.openArtifacts();}function displayType(type: TypeRef): string {@@ -746,8 +726,8 @@ export async function runCli(let selectedRuntimeProjectRoot: string | undefined;const commandProjectRoot = (): string => args.workspaceRoot?? selectedRuntimeProjectRoot- ?? ((args.command === "check" || args.command === "plan" || args.command === "build"- || args.command === "packages") && args.file !== undefined+ ?? ((args.command === "check" || args.command === "plan" || args.command === "build")+ && args.file !== undefined? dirname(resolve(args.file)): process.cwd());const packageRootForProject = async (projectRoot = commandProjectRoot()): Promise<string> =>@@ -832,7 +812,7 @@ export async function runCli(}const positionalRuntime = (args.command === "runtime" || args.command === "programs"- || args.command === "doctor" || args.command === "gc") && args.file !== undefined;+ || args.command === "doctor") && args.file !== undefined;const runtimeWasExplicit = args.runtime !== undefined || positionalRuntime;let runtimeNeedsHint = runtimeWasExplicit;if (args.runtime === undefined && !positionalRuntime) {@@ -851,13 +831,12 @@ export async function runCli(|| args.command === "build" || args.command === "status" || args.command === "builds"|| args.command === "history"|| args.command === "inspect" || args.command === "get" || args.command === "cancel"- || args.command === "doctor" || args.command === "gc" || args.command === "programs"+ || args.command === "doctor" || args.command === "programs"|| args.command === "runtime" || args.command === "queue" || args.command === "paths";const operational = known || args.command === "auth";const fileOptional = args.command === "builds" || args.command === "history" || args.command === "queue"|| args.command === "paths"- || args.command === "programs" || args.command === "runtime" || args.command === "doctor"- || args.command === "gc";+ || args.command === "programs" || args.command === "runtime" || args.command === "doctor";if (!operational || (!fileOptional && args.file === undefined)) {throw new Error(usage());}@@ -909,7 +888,7 @@ export async function runCli(return;}if (args.command === "doctor") {- if (args.packageRoot !== undefined || args.apply) {+ if (args.packageRoot !== undefined) {throw new Error("doctor reads all deployment selection from the Runtime Profile itself");}const profileInput = args.runtime ?? args.file;@@ -932,7 +911,7 @@ export async function runCli(if (args.file !== undefined && args.runtime !== undefined) {throw new Error("programs reads all deployment selection from the Runtime Profile itself; provide that Profile only once");}- if (args.packageRoot !== undefined || args.apply) {+ if (args.packageRoot !== undefined) {throw new Error("programs reads all deployment selection from the Runtime Profile itself");}if (args.action !== "up" && args.action !== "down" && args.action !== "status") {@@ -1079,34 +1058,6 @@ export async function runCli(}return;}- if (args.command === "gc") {- if (args.packageRoot !== undefined) {- throw new Error("gc reads all deployment selection from the Runtime Profile itself");- }- const profileInput = args.runtime ?? args.file;- if (profileInput === undefined) {- throw new Error("gc requires a Runtime Profile; run narratage runtime use <profile> or provide it positionally");- }- const runtime = await loadRuntimeMaintenance(await runtimeHost(profileInput), !args.apply);- try {- const report = await runtime.garbageCollectArtifacts({ apply: args.apply });- const machine = {- applied: args.apply,- reachable: report.reachable,- unreachable: report.unreachable,- deleted: report.deleted,- };- writeOperational(machine, args.apply ? "Artifact garbage collection applied" : "Artifact garbage collection preview",- report.unreachable.length === 0 ? "success" : "warning", [- ["Reachable", String(report.reachable.length)],- ["Unreachable", String(report.unreachable.length)],- ["Deleted", String(report.deleted.length)],- ]);- } finally {- await runtime.close();- }- return;- }if (args.command === "auth") {if (args.action !== "status" && args.action !== "login" && args.action !== "logout") {throw new Error("auth takes status, login or logout");@@ -1167,18 +1118,6 @@ export async function runCli(}return;}- if (args.apply) throw new Error("--apply is only valid for gc");- if (args.noPrograms && args.command !== "build") {- throw new Error("--no-programs is only valid for build; no other command starts an external program");- }- if ((args.record !== undefined || args.output !== undefined || args.name !== undefined- || args.artifact !== undefined || args.to !== undefined)- && args.command !== "get") {- throw new Error("--name, --record, --output, --artifact and --to are only valid for get");- }- if (args.source !== undefined && args.command !== "history") {- throw new Error("--source is only valid for history");- }if (args.command === "status" || args.command === "builds" || args.command === "inspect"|| args.command === "history" || args.command === "get" || args.command === "cancel" || args.command === "queue") {@@ -1378,7 +1317,7 @@ export async function runCli(: args.watch && !terminal ? "warning" : "info", [["Build", args.file!],["Status", effectiveStatus ?? "missing"],- ...(status.build === undefined || effectiveStatus === status.build.state.status+ ...(status.build === undefined || terminal || effectiveStatus === status.build.state.status? []: [["Core", status.build.state.status] as const]),["Dispatch", phase],@@ -1441,7 +1380,8 @@ export async function runCli(: [`Other accepted outputs ${otherAccepted.length} · use --verbose to list them`];writeOperational(machine, "Build archive detail", "info", [["Build", status.build.build], ["Status", effectiveStatus],- ...(effectiveStatus === archive.status ? [] : [["Core", archive.status] as const]),+ ...(status.dispatch?.phase === "terminal" || effectiveStatus === archive.status+ ? [] : [["Core", archive.status] as const]),["Targets", String(archive.targets.length)], ["Accepted records", String(archive.records.length)],["Operations", String(status.operations.length)],], [@@ -1815,7 +1755,7 @@ export async function runCli(machine: {format: "narratage.cli-plan@1",ok: true,- plan: result.plan,+ plan: result.definition.plan,...(preflight === undefined ? {} : { preflight }),},run: loaded.path,diff --git a/packages/cli/src/output.ts b/packages/cli/src/output.tsindex 2a062697..f19c1cc6 100644--- a/packages/cli/src/output.ts+++ b/packages/cli/src/output.ts@@ -512,12 +512,6 @@ function commandHelp(topic: string, colors: Palette): readonly string[] | undefi" narratage auth login <endpoint-instance> [--runtime <profile>] [--slot <name>] [--from <secret-file>]"," narratage auth logout <endpoint-instance> [--runtime <profile>] [--slot <name>]",],- gc: [- colors.accent(colors.strong("narratage gc")),- colors.dim("Report unreachable archived Artifacts; delete them only with --apply."),- "",- " narratage gc [<profile>] [--apply]",- ],};const selected = topics[topic];return selected === undefined ? undefined : [...selected, ...common];@@ -566,7 +560,6 @@ export function writeCliHelp(io: CliIo, topic?: string): void {row("queue [--watch]", "inspect durable dispatch and shared capacity"),row("paths", "show every effective state location"),row("auth status|login|logout", "manage Endpoint-declared credential references"),- row("gc [profile]", "report unreachable Artifacts; --apply deletes"),"",colors.strong("Output"),row("--json", "complete machine-readable result"),diff --git a/packages/cli/src/runtime-port.ts b/packages/cli/src/runtime-port.tsindex d4f6277a..0a3abb19 100644--- a/packages/cli/src/runtime-port.ts+++ b/packages/cli/src/runtime-port.ts@@ -4,13 +4,11 @@ export type {RuntimeController as CliRuntimeController,RuntimeHostArchive as CliRuntimeArchiveControl,RuntimeHostArtifactAccess as CliRuntimeArtifactAccess,- RuntimeHostArtifactGarbageCollection as CliArtifactGarbageCollection,RuntimeHostBuildSubmission as CliBuildSubmission,RuntimeHostCredentialControl as CliCredentialControl,RuntimeHostCredentialStatus as CliCredentialStatus,RuntimeHostDoctorResult as CliRuntimeDoctorResult,RuntimeHostExecution as CliRuntime,- RuntimeHostMaintenance as CliRuntimeMaintenance,RuntimeHostStatus as CliRuntimeStatus,RuntimeWorkerState as CliRuntimeWorkerState,} from "@narratage/runtime-host-node";diff --git a/packages/cli/src/source-discovery.ts b/packages/cli/src/source-discovery.tsindex 10d84cf2..35c9dbb3 100644--- a/packages/cli/src/source-discovery.ts+++ b/packages/cli/src/source-discovery.ts@@ -64,20 +64,24 @@ export async function discoverSourcePackages(const selected = new Set<string>();const logical = new Map<string, LogicalPackageAddress>();const visited = new Set<string>();+ const moduleOwners = new Map<string, string>();+ const fragmentOwners = new Map<string, string>();+ for (const item of packages) {+ for (const module of item.contribution.modules ?? []) {+ for (const request of [`${module.manifest.name}@${module.manifest.version}`, ...(module.specifiers ?? [])]) {+ moduleOwners.set(request, item.specifier);+ }+ }+ for (const facet of item.contribution.hostFacets ?? []) {+ if (facet.abi !== runFragmentHostAbi) continue;+ for (const request of facet.offers ?? []) fragmentOwners.set(request, item.specifier);+ }+ }const requireLogical = (address: LogicalPackageAddress, physical: string | undefined): void => {logical.set(addressKey(address), address);selected.add(physical ?? selectedPackage(address.name));};- const moduleOwner = (request: string): string | undefined => packages.find((item) =>- (item.contribution.modules ?? []).some((module) => [- `${module.manifest.name}@${module.manifest.version}`,- ...(module.specifiers ?? []),- ].includes(request)))?.specifier;- const fragmentOwner = (request: string): string | undefined => packages.find((item) =>- (item.contribution.hostFacets ?? []).some((facet) => facet.abi === runFragmentHostAbi- && (facet.offers ?? []).includes(request)))?.specifier;-const discover = async (path: string): Promise<void> => {const canonical = await realpath(path);if (!isWithin(root, canonical)) throw new Error(`Source ${canonical} is outside workspace root ${root}`);@@ -100,7 +104,7 @@ export async function discoverSourcePackages(}const discovery = await owner.frontend.discover(prepareAuthorSource({ id: canonical, name, text }));for (const request of discovery.modules) {- requireLogical({ abi: modulePackageAbi, name: request }, moduleOwner(request));+ requireLogical({ abi: modulePackageAbi, name: request }, moduleOwners.get(request));}for (const child of discovery.sources) await discover(relativeSource(canonical, child.from));return;@@ -111,7 +115,7 @@ export async function discoverSourcePackages(}const discovery = await owner.frontend.discover(prepareRunSource({ id: canonical, name, text }));for (const item of discovery.imports) {- requireLogical({ abi: runFragmentHostAbi, name: item.from }, fragmentOwner(item.from));+ requireLogical({ abi: runFragmentHostAbi, name: item.from }, fragmentOwners.get(item.from));}await discover(relativeSource(canonical, discovery.author.source));};diff --git a/packages/cli/src/source-packages.ts b/packages/cli/src/source-packages.tsindex 16667986..1158cd8f 100644--- a/packages/cli/src/source-packages.ts+++ b/packages/cli/src/source-packages.ts@@ -35,15 +35,22 @@ async function discover(return selection;}-function offers(packages: readonly LoadedPackage[], address: LogicalPackageAddress): number {- return packages.filter((item) => [- ...(item.contribution.modules ?? []).flatMap((module) => [- `${module.manifest.name}@${module.manifest.version}`,- ...(module.specifiers ?? []),- ].map((name) => ({ abi: modulePackageAbi, name }))),- ...(item.contribution.hostFacets ?? []).flatMap((facet) =>- (facet.offers ?? []).map((name) => ({ abi: facet.abi, name }))),- ].some((offer) => offer.abi === address.abi && offer.name === address.name)).length;+function offerCounts(packages: readonly LoadedPackage[]): ReadonlyMap<string, number> {+ const counts = new Map<string, number>();+ for (const item of packages) {+ const offers = [+ ...(item.contribution.modules ?? []).flatMap((module) => [+ `${module.manifest.name}@${module.manifest.version}`,+ ...(module.specifiers ?? []),+ ].map((name) => ({ abi: modulePackageAbi, name }))),+ ...(item.contribution.hostFacets ?? []).flatMap((facet) =>+ (facet.offers ?? []).map((name) => ({ abi: facet.abi, name }))),+ ];+ for (const address of new Set(offers.map((offer) => `${offer.abi}\u0000${offer.name}`))) {+ counts.set(address, (counts.get(address) ?? 0) + 1);+ }+ }+ return counts;}function selectionSatisfied(@@ -51,8 +58,9 @@ function selectionSatisfied(packages: readonly LoadedPackage[],): boolean {const installed = new Set(packages.map((item) => item.specifier));+ const counts = offerCounts(packages);return selection.selected.every((item) => installed.has(item))- && (selection.logical ?? []).every((address) => offers(packages, address) === 1);+ && (selection.logical ?? []).every((address) => counts.get(`${address.abi}\u0000${address.name}`) === 1);}/** Load only the installed packages reached by recursive Frontend discovery. */diff --git a/packages/compiler-node/src/compiler.ts b/packages/compiler-node/src/compiler.tsindex 31cace17..12b1c6ba 100644--- a/packages/compiler-node/src/compiler.ts+++ b/packages/compiler-node/src/compiler.ts@@ -95,8 +95,6 @@ export type NodeCompilerOptions = {readonly workspace: Workspace;/** Trusted Type-owner validators used to admit authored Records before linking. */readonly validators?: TypeValidatorRegistryLike;- /** Low-level Host hook for a sandboxed or remote admission implementation. */- readonly admitRecord?: AuthorRecordAdmitter;};export type NodeCompiledSourceClosure = CompiledSourceClosure & {@@ -110,15 +108,8 @@ export class NodeCompiler {readonly #admitRecord: AuthorRecordAdmitter;constructor(options: NodeCompilerOptions) {- if (options.validators !== undefined && options.admitRecord !== undefined) {- throw new NodeCompilerError(- "AMBIGUOUS_RECORD_ADMISSION",- "NodeCompiler accepts validators or a custom Record admitter, not both",- );- }this.#options = options;- this.#admitRecord = options.admitRecord- ?? createRecordAdmitter(options.validators ?? new TypeValidatorRegistry());+ this.#admitRecord = createRecordAdmitter(options.validators ?? new TypeValidatorRegistry());}supportsFrontend(id: string): boolean {diff --git a/packages/compiler-node/src/run.ts b/packages/compiler-node/src/run.tsindex 6bb8a71a..f8432ede 100644--- a/packages/compiler-node/src/run.ts+++ b/packages/compiler-node/src/run.ts@@ -11,9 +11,7 @@ import type {ArtifactAttachment,} from "@narratage/workspace";import type {- BuildPlan,BuildDefinition,- BuildRequest,BuildState,StoredValue,TypeRef,@@ -74,10 +72,6 @@ export type PlannedBuild = {readonly compilation: NodeCompiledRun;/** Immutable authority persisted once for every fresh Runtime Build. */readonly definition: BuildDefinition;- /** Convenience view of `definition.request`. */- readonly request: BuildRequest;- /** Convenience view of `definition.plan`. */- readonly plan: BuildPlan;/** Materialized plan/read view; durable Stores persist Definition + Facts instead. */readonly state: BuildState;};@@ -287,12 +281,9 @@ export class NodeRunCompiler {targets: compilation.run.graph.targets,});const sliced = sliceExecution(compilation.program, fullGraph, fullRequest);- const request = sealBuildRequest({- targets: compilation.run.graph.targets,- });- const definition = defineBuild(sliced.program, sliced.graph, request);+ const definition = defineBuild(sliced.program, sliced.graph, fullRequest);const state = materializeBuild(definition, []);- return { compilation, definition, request: definition.request, plan: definition.plan, state };+ return { compilation, definition, state };}async planFile(file: string): Promise<PlannedBuild> {diff --git a/packages/compiler-node/test/compiler.test.ts b/packages/compiler-node/test/compiler.test.tsindex a92b8b91..88c64db9 100644--- a/packages/compiler-node/test/compiler.test.ts+++ b/packages/compiler-node/test/compiler.test.ts@@ -404,10 +404,10 @@ test("Run-only Fragment modules extend the execution closure without polluting t[laboratory.name, previewModule.name].sort(),"the durable Build keeps only modules needed by its selected execution slice",);- assert.equal(planned.plan, planned.state.plan);- assert.equal(planned.request, planned.state.request);- assert.equal(planned.plan.steps.length, 1);- assert.equal(planned.plan.steps[0]?.producer.name, previewProducer.name);+ assert.equal(planned.definition.plan, planned.state.plan);+ assert.equal(planned.definition.request, planned.state.request);+ assert.equal(planned.definition.plan.steps.length, 1);+ assert.equal(planned.definition.plan.steps[0]?.producer.name, previewProducer.name);});test("static Run checking accepts a future BuildRecord without opening a BuildArchive", async () => {diff --git a/packages/core/src/canonical.ts b/packages/core/src/canonical.tsdeleted file mode 100644index 2043fec2..00000000--- a/packages/core/src/canonical.ts+++ /dev/null@@ -1,5 +0,0 @@-export {- canonicalStringify,- canonicalize,- isDigest,-} from "@narratage/protocol";diff --git a/packages/core/src/error.ts b/packages/core/src/error.tsindex 853a9e6a..fc9476e3 100644--- a/packages/core/src/error.ts+++ b/packages/core/src/error.ts@@ -1,7 +1,5 @@import { SvmlError } from "@narratage/protocol";-export { SvmlError as CoreError };-export function invariant(condition: unknown,code: string,diff --git a/packages/core/src/graph.ts b/packages/core/src/graph.tsindex e51098dd..c0277e4c 100644--- a/packages/core/src/graph.ts+++ b/packages/core/src/graph.ts@@ -10,11 +10,12 @@ import type {OperationResult,StoredValue,TypeRef,+ TypedRecord,} from "@narratage/protocol";-import { canonicalize } from "./canonical.js";+import { canonicalize } from "@narratage/protocol";import { invariant } from "./error.js";-import { resolveProducer, sealRecord, verifyRecordStructure } from "./link.js";+import { resolveProducer, verifyRecordStructure } from "./link.js";import { producerKey, sameType, typeKey } from "./reference.js";type GraphIndex = {@@ -248,11 +249,11 @@ function verifyCandidateValue(program: LinkedProgram, graph: CompiledGraph, cand);return;}- const provisional = sealRecord({+ const provisional: TypedRecord = {id: candidate.root.value.id,type: candidate.type,value: candidate.root.value.value,- });+ };verifyRecordStructure(program.closure, provisional);}diff --git a/packages/core/src/index.ts b/packages/core/src/index.tsindex 5a442e5c..cf33e271 100644--- a/packages/core/src/index.ts+++ b/packages/core/src/index.ts@@ -1,5 +1,3 @@-export { canonicalStringify, canonicalize, isDigest } from "./canonical.js";-export { CoreError } from "./error.js";export {createResolvedClosure,link,@@ -26,4 +24,3 @@ export {start,} from "./reducer.js";export { BuildMachine } from "./machine.js";-export type { TypedRecordDraft } from "./link.js";diff --git a/packages/core/src/link.ts b/packages/core/src/link.tsindex 68d350a9..603ca7b4 100644--- a/packages/core/src/link.ts+++ b/packages/core/src/link.ts@@ -4,7 +4,6 @@ import type {ModuleRef,ProducerRef,ResolvedModule,- ResolvedModuleManifest,ResolvedModuleClosure,ResolvedCapabilityDeclaration,ResolvedProducerDeclaration,@@ -16,8 +15,6 @@ import type {import { invariant } from "./error.js";import { capabilityKey, moduleKey, producerKey, typeKey } from "./reference.js";-export type TypedRecordDraft = TypedRecord;-type ClosureIndex = {readonly types: ReadonlyMap<string, ResolvedTypeDeclaration>;readonly producers: ReadonlyMap<string, ResolvedProducerDeclaration>;@@ -50,33 +47,10 @@ function closureIndex(closure: ResolvedModuleClosure): ClosureIndex {return created;}-function resolvedManifest(manifest: ModuleManifest): ResolvedModuleManifest {- return {- format: manifest.format,- name: manifest.name,- version: manifest.version,- dependencies: manifest.dependencies.map((dependency) => ({ module: { ...dependency.module } })),- types: manifest.types.map((type) => ({ name: type.name })),- capabilities: manifest.capabilities.map((capability) => ({- name: capability.name,- returns: structuredClone(capability.returns),- })),- producers: manifest.producers.map((producer) => ({- name: producer.name,- inputs: structuredClone(producer.inputs),- outputs: structuredClone(producer.outputs),- needs: structuredClone(producer.needs),- })),- };-}-export function createResolvedClosure(manifests: readonly ModuleManifest[],): ResolvedModuleClosure {- const modules = manifests.map((definition) => {- const manifest = resolvedManifest(definition);- return { manifest };- });+ const modules = manifests.map((manifest) => ({ manifest: structuredClone(manifest) }));return {format: "narratage.closure@1",modules,@@ -230,7 +204,7 @@ export function resolveProducer(return declaration;}-export function sealRecord(record: TypedRecordDraft): TypedRecord {+export function sealRecord(record: TypedRecord): TypedRecord {return { ...record };}diff --git a/packages/core/src/plan.ts b/packages/core/src/plan.tsindex 5ebc398c..204a50e5 100644--- a/packages/core/src/plan.ts+++ b/packages/core/src/plan.ts@@ -13,7 +13,7 @@ import type {TypedRecord,} from "@narratage/protocol";-import { canonicalStringify } from "./canonical.js";+import { canonicalStringify } from "@narratage/protocol";import { invariant } from "./error.js";import {operationResultRecord,@@ -23,7 +23,7 @@ import {satisfiedCandidate,verifyBuildRequest,} from "./graph.js";-import { resolveProducer, sealRecord } from "./link.js";+import { resolveProducer } from "./link.js";import { sameType, typeKey } from "./reference.js";function exactKeys(@@ -152,11 +152,11 @@ export function compileBuild(const candidate = satisfiedCandidate(graph, ref.id);let resolved: ResolvedSource;if (candidate.root.kind === "value") {- const record = sealRecord({+ const record: TypedRecord = {id: candidate.root.value.id,type: candidate.type,value: candidate.root.value.value,- });+ };invariant(!authored.has(record.id), "PROVIDED_RECORD_CONFLICT", `${record.id} conflicts with authored input`);resolved = { record: record.id, type: record.type };} else {@@ -314,11 +314,11 @@ export function selectedProvidedRecords(for (const selection of plan.selections) {const candidate = resolveCandidate(graph, selection.candidate);if (candidate.root.kind !== "value") continue;- const record = sealRecord({+ const record: TypedRecord = {id: candidate.root.value.id,type: candidate.type,value: candidate.root.value.value,- });+ };invariant(selection.record === record.id, "SELECTION_RECORD_MISMATCH", `${selection.output} does not select ${record.id}`);invariant(!authored.has(record.id), "PROVIDED_RECORD_CONFLICT", `${record.id} conflicts with authored input`, record.id);const previous = records.get(record.id);diff --git a/packages/core/src/provenance.ts b/packages/core/src/provenance.tsdeleted file mode 100644index 196fa772..00000000--- a/packages/core/src/provenance.ts+++ /dev/null@@ -1,3 +0,0 @@-export function commandId(kind: "producer" | "need", subject: string): string {- return `${kind}:${subject}`;-}diff --git a/packages/core/src/reducer.ts b/packages/core/src/reducer.tsindex 0162b78e..df281c13 100644--- a/packages/core/src/reducer.ts+++ b/packages/core/src/reducer.ts@@ -17,11 +17,14 @@ import type {TypedRecord,} from "@narratage/protocol";-import { canonicalize } from "./canonical.js";-import { CoreError, invariant } from "./error.js";+import { canonicalize, SvmlError } from "@narratage/protocol";+import { invariant } from "./error.js";import { resolveProducer, verifyRecordStructure } from "./link.js";import { compileBuild, producerStep, selectedProvidedRecords } from "./plan.js";-import { commandId } from "./provenance.js";++function commandId(kind: "producer" | "need", subject: string): string {+ return `${kind}:${subject}`;+}function withoutCommand(state: BuildState, id: string): readonly CoreCommand[] {return state.outstanding.filter((command) => command.id !== id);@@ -163,7 +166,7 @@ function applyEvent(state: BuildState, event: CommandResult): BuildState {if (command.kind === "fulfill-need" && event.kind === "need-fulfilled") {return acceptNeedEvent(state, command, event);}- throw new CoreError(+ throw new SvmlError("EVENT_COMMAND_MISMATCH",`${event.kind} cannot complete ${command.kind}`,command.id,diff --git a/packages/driver-node/src/driver.ts b/packages/driver-node/src/driver.tsindex 921cbd89..bdcd642e 100644--- a/packages/driver-node/src/driver.ts+++ b/packages/driver-node/src/driver.ts@@ -17,6 +17,7 @@ import {} from "@narratage/validation";import type { TypeValidatorRegistryLike } from "@narratage/validation";import type {+ ArtifactStore,CredentialStore,CredentialValue,OperationSnapshot,@@ -37,7 +38,6 @@ import {endpointReturnKey,} from "./registry.js";import type {- ArtifactStore,BlockedCommand,DriverExecutionOutcome,DriverRunResult,diff --git a/packages/driver-node/src/registry.ts b/packages/driver-node/src/registry.tsindex e85214b7..0dad7954 100644--- a/packages/driver-node/src/registry.ts+++ b/packages/driver-node/src/registry.ts@@ -1,6 +1,7 @@import type {EndpointRegistrar,EndpointRegistrationOptions,+ EndpointScheduling,ImmediateEndpointHandler,AsyncEndpoint,} from "@narratage/endpoint-kit";@@ -20,7 +21,6 @@ import type {ProducerRegistration,EndpointRegistration,EndpointResolution,- SchedulingHint,} from "./types.js";function moduleKey(ref: { readonly name: string; readonly version: string }): string {@@ -36,7 +36,7 @@ function sameRef(&& left.name === right.name;}-function verifyScheduling(scheduling: SchedulingHint | undefined): void {+function verifyScheduling(scheduling: EndpointScheduling | undefined): void {if (scheduling === undefined) return;if (scheduling.resources.length === 0) throw new Error("scheduling resources must not be empty");const ids = scheduling.resources.map((resource) => {@@ -73,7 +73,7 @@ export class ProducerRegistry implements ProducerRegistrar {registerProducer(producer: ProducerRef,handler: ProducerHandler,- options: { readonly scheduling?: SchedulingHint } = {},+ options: { readonly scheduling?: EndpointScheduling } = {},): void {const key = producerRegistryKey(producer);if (this.#producers.has(key)) throw new Error(`producer ${key} is already registered`);diff --git a/packages/driver-node/src/types.ts b/packages/driver-node/src/types.tsindex bb5e92e3..071db8b9 100644--- a/packages/driver-node/src/types.ts+++ b/packages/driver-node/src/types.ts@@ -9,8 +9,6 @@ import type {} from "@narratage/component-kit";import type { EndpointRegistrationOptions, EndpointScheduling, ImmediateEndpointHandler, AsyncEndpoint } from "@narratage/endpoint-kit";-export type { ArtifactStore } from "@narratage/runtime";-export type {ProducerHandler,ProducerHandlerContext,@@ -44,12 +42,9 @@ export type DriverRunResult = {readonly blocked: readonly BlockedCommand[];};-/** Endpoint scheduling metadata; it never changes Core demand or command identity. */-export type SchedulingHint = EndpointScheduling;-export type ProducerRegistration = {readonly handler: ProducerHandler;- readonly scheduling?: SchedulingHint;+ readonly scheduling?: EndpointScheduling;};type EndpointRegistrationBase = {diff --git a/packages/driver-node/test/scheduler.test.ts b/packages/driver-node/test/scheduler.test.tsindex aa4204a6..0117d449 100644--- a/packages/driver-node/test/scheduler.test.ts+++ b/packages/driver-node/test/scheduler.test.ts@@ -214,7 +214,7 @@ test("one local Scheduler shares an Endpoint resource across multiple Builds", amaximumActive = Math.max(maximumActive, active);},});- const scheduler = new LocalBuildScheduler(executor, { maxConcurrency: 8 });+ const scheduler = new LocalBuildScheduler(executor);const results = await scheduler.run([{ id: "video-a", state: createGreetingBuild() },{ id: "video-b", state: createGreetingBuild() },@@ -227,30 +227,6 @@ test("one local Scheduler shares an Endpoint resource across multiple Builds", aresult.outcomes.some((entry) => entry.resources.includes("pool:fixture.account"))), true);});-test("a Resolved Runtime resource override changes parallelism", async () => {- let maximumActive = 0;- const resource = "pool:fixture.account";- const { executor } = configuredExecutor({- resource,- defaultConcurrency: 1,- observe(active) {- maximumActive = Math.max(maximumActive, active);- },- });- const initial = createGreetingBuild();- const scheduler = new LocalBuildScheduler(executor, {- maxConcurrency: 8,- resourceLimits: { [resource]: 2 },- });- const results = await scheduler.run([- { id: "video-a", state: initial },- { id: "video-b", state: initial },- ]);-- assert.deepEqual(results.map((result) => result.status), ["complete", "complete"]);- assert.equal(maximumActive, 2);-});-test("independent paid commands inside one Build may fill the same resource without duplicating their shared upstream", async () => {let maximumActive = 0;const { executor, getCalls } = configuredExecutor({@@ -260,7 +236,7 @@ test("independent paid commands inside one Build may fill the same resource withmaximumActive = Math.max(maximumActive, active);},});- const [result] = await new LocalBuildScheduler(executor, { maxConcurrency: 8 }).run([{+ const [result] = await new LocalBuildScheduler(executor).run([{id: "two-shots",state: createParallelGreetingBuild(),}]);@@ -299,7 +275,7 @@ test("an asynchronous Endpoint starts once and is polled until complete", async};const firstExecutor = asyncExecutor(endpoint, operations);- const [first] = await new LocalBuildScheduler(firstExecutor, { maxConcurrency: 8 })+ const [first] = await new LocalBuildScheduler(firstExecutor).run([{ id: "video", state: createGreetingBuild() }]);assert.equal(first?.status, "paused");assert.equal(starts, 1);@@ -310,7 +286,7 @@ test("an asynchronous Endpoint starts once and is polled until complete", asyncassert.equal((await operations.read(pending.operation))?.status, "pending");const secondExecutor = asyncExecutor(endpoint, operations);- const [second] = await new LocalBuildScheduler(secondExecutor, { maxConcurrency: 8 })+ const [second] = await new LocalBuildScheduler(secondExecutor).run([{ id: "video", state: createGreetingBuild() }]);assert.equal(second?.status, "complete");@@ -339,7 +315,7 @@ test("wakeAt prevents early polling and Runtime cancellation becomes a terminal},};const executor = asyncExecutor(endpoint, operations);- const scheduler = new LocalBuildScheduler(executor, { maxConcurrency: 8 });+ const scheduler = new LocalBuildScheduler(executor);const [first] = await scheduler.run([{ id: "cancel-video", state: createGreetingBuild() }]);const operationId = first?.outcomes.find((item) => item.status === "pending")?.operation;assert.ok(operationId);diff --git a/packages/elaborator/src/source.ts b/packages/elaborator/src/source.tsindex 1c5a0d8b..ae68f08d 100644--- a/packages/elaborator/src/source.ts+++ b/packages/elaborator/src/source.ts@@ -1,4 +1,5 @@-import { canonicalStringify, isDigest, link } from "@narratage/core";+import { link } from "@narratage/core";+import { canonicalStringify, isDigest } from "@narratage/protocol";import type {CompiledGraph,GraphValueRef,diff --git a/packages/endpoint-kit/src/index.ts b/packages/endpoint-kit/src/index.tsindex a95804cd..763e0198 100644--- a/packages/endpoint-kit/src/index.ts+++ b/packages/endpoint-kit/src/index.ts@@ -192,9 +192,6 @@ export function defineEndpointPackage(options: DefineEndpointPackageOptions): Enassert(options.instance.trim().length > 0, "Endpoint instance is empty");assert(options.pool.trim().length > 0, "Endpoint Provider Pool is empty");assert(options.capabilities.length > 0, "Endpoint package declares no capability");- const lifecycle = options.capabilities[0]!.lifecycle;- assert(options.capabilities.every((item) => item.lifecycle === lifecycle),- "one Endpoint facet cannot mix immediate and asynchronous lifecycles");const keys = options.capabilities.map((item) => refKey(item.capability));assert(new Set(keys).size === keys.length, "Endpoint package repeats a capability");const lanes = options.capabilities.map((item) => item.lane ?? item.capability.name);diff --git a/packages/markup/src/frontend.ts b/packages/markup/src/frontend.tsindex 2d29239b..4e90c5d4 100644--- a/packages/markup/src/frontend.ts+++ b/packages/markup/src/frontend.ts@@ -1,8 +1,4 @@-import {- canonicalize,- canonicalStringify,- sealRecord,-} from "@narratage/core";+import { canonicalStringify } from "@narratage/protocol";import type {ModuleRef,ResolvedModule,@@ -229,7 +225,7 @@ export async function decodeMarkup(source: MarkupSource, context: MarkupDecodeCoif (imported !== undefined) {return imported.record === undefined? imported- : { ...imported, record: canonicalize(imported.record) as unknown as TypedRecord };+ : { ...imported, record: structuredClone(imported.record) };}const record = recordsById.get(path);if (record !== undefined) {@@ -237,7 +233,7 @@ export async function decodeMarkup(source: MarkupSource, context: MarkupDecodeCopath,ref: { kind: "record", id: record.id },type: record.type,- record: canonicalize(record) as unknown as TypedRecord,+ record: structuredClone(record),};}return componentReferences.get(path);@@ -304,11 +300,11 @@ export async function decodeMarkup(source: MarkupSource, context: MarkupDecodeCo);}recordIds.add(draft.id);- const record = sealRecord({+ const record: TypedRecord = {id: draft.id,type: draft.type,value: draft.value,- });+ };records.push(record);recordsById.set(record.id, record);}diff --git a/packages/package-loader-node/src/loader.ts b/packages/package-loader-node/src/loader.tsindex 9c56009a..0e99b79f 100644--- a/packages/package-loader-node/src/loader.ts+++ b/packages/package-loader-node/src/loader.ts@@ -14,7 +14,6 @@ import type {type PackageJson = {readonly name: string;- readonly version: string;readonly narratage?: { readonly activation?: string };};@@ -39,7 +38,6 @@ function parsePackageJson(value: unknown, subject: string): PackageJson {const narratage = item.narratage === undefined ? undefined : object(item.narratage, `${subject}.narratage`);return {name: text(item.name, `${subject}.name`),- version: text(item.version, `${subject}.version`),...(narratage?.activation === undefined ? {} : { narratage: { activation: text(narratage.activation, `${subject}.narratage.activation`) } }),};}diff --git a/packages/protocol/src/module.ts b/packages/protocol/src/module.tsindex 4cde9a38..e8537808 100644--- a/packages/protocol/src/module.ts+++ b/packages/protocol/src/module.ts@@ -44,13 +44,8 @@ export type ModuleManifest = {readonly producers: readonly ProducerDeclaration[];};-/** Domain-neutral interface resolved into Core; package-owned value schemas stay outside it. */-export type ResolvedModuleManifest = Omit<ModuleManifest, "types"> & {- readonly types: readonly { readonly name: string }[];-};-export type ResolvedModule = {- readonly manifest: ResolvedModuleManifest;+ readonly manifest: ModuleManifest;};export type ResolvedModuleClosure = {diff --git a/packages/provider-kie/test/live-smoke.ts b/packages/provider-kie/test/live-smoke.tsindex 37051380..fc042769 100644--- a/packages/provider-kie/test/live-smoke.ts+++ b/packages/provider-kie/test/live-smoke.ts@@ -402,7 +402,6 @@ async function main(): Promise<void> {...[...new Map(selected.map((item) => [item.manifest.name, item.component])).values()],],endpoints: [provider],- scheduling: { maxConcurrency: 1 },close: () => state.close(),});const failures: string[] = [];diff --git a/packages/runtime-host-node/src/index.ts b/packages/runtime-host-node/src/index.tsindex 7b962a4d..2974e51b 100644--- a/packages/runtime-host-node/src/index.ts+++ b/packages/runtime-host-node/src/index.ts@@ -39,12 +39,6 @@ export type RuntimeHostCredentialStatus = {readonly writable: boolean;};-export type RuntimeHostArtifactGarbageCollection = {- readonly reachable: readonly Digest[];- readonly unreachable: readonly Digest[];- readonly deleted: readonly Digest[];-};-export type RuntimeHostArchive = {status(build: string): Promise<RuntimeHostStatus>;activity(build: string): Promise<{@@ -67,10 +61,6 @@ export type RuntimeHostArtifactAccess = {close(): void | Promise<void>;};-export type RuntimeHostMaintenance = RuntimeHostArchive & RuntimeHostArtifactAccess & {- garbageCollectArtifacts(options?: { readonly apply?: boolean }): Promise<RuntimeHostArtifactGarbageCollection>;-};-export type RuntimeHostCredentialControl = {credentials(endpoint?: string): Promise<readonly RuntimeHostCredentialStatus[]>;putCredential(endpoint: string, slot: string, secret: string): Promise<RuntimeHostCredentialStatus>;@@ -81,7 +71,7 @@ export type RuntimeHostCredentialControl = {close(): void | Promise<void>;};-export type RuntimeHostExecution = RuntimeHostMaintenance & RuntimeHostCredentialControl & {+export type RuntimeHostExecution = RuntimeHostArchive & RuntimeHostArtifactAccess & RuntimeHostCredentialControl & {build(request: {readonly id: string;readonly definition: BuildDefinition;@@ -164,8 +154,7 @@ export type NodeRuntimeHost = {}): Promise<RuntimeController>;createRuntime(): Promise<RuntimeHostExecution>;openArchive(options?: { readonly readOnly?: boolean }): Promise<RuntimeHostArchive>;- openArtifacts(options?: { readonly readOnly?: boolean }): Promise<RuntimeHostArtifactAccess>;- openMaintenance(options?: { readonly readOnly?: boolean }): Promise<RuntimeHostMaintenance>;+ openArtifacts(): Promise<RuntimeHostArtifactAccess>;openCredentials(endpoint: string): Promise<RuntimeHostCredentialControl>;doctor(options?: {readonly capabilities?: readonly CapabilityRef[];diff --git a/packages/runtime-kit/src/index.ts b/packages/runtime-kit/src/index.tsindex 525e8633..96156f8d 100644--- a/packages/runtime-kit/src/index.ts+++ b/packages/runtime-kit/src/index.ts@@ -20,7 +20,6 @@ export type RuntimeAdapterFactoryContext = {readonly instance: string;readonly pool?: string;readonly config: CanonicalValue;- readonly access?: "read-write" | "read-only";};export type RuntimeDoctorDiagnostic = {diff --git a/packages/runtime-local/README.md b/packages/runtime-local/README.mdindex 761fa784..72959bc6 100644--- a/packages/runtime-local/README.md+++ b/packages/runtime-local/README.md@@ -19,8 +19,7 @@ A Runtime Profile selects only the environmental parts that genuinely vary:"credentials": {"environment": { "use": "@narratage/credential-store-env" }},- "endpoints": {},- "concurrency": 4+ "endpoints": {}}}}diff --git a/packages/runtime-local/src/config.ts b/packages/runtime-local/src/config.tsindex 207ffb36..d3662002 100644--- a/packages/runtime-local/src/config.ts+++ b/packages/runtime-local/src/config.ts@@ -1,7 +1,6 @@import { readFile, stat } from "node:fs/promises";import { dirname, resolve } from "node:path";-import type { ComponentPackage } from "@narratage/component-kit";import { collectNodePackageComponents, loadNodePackageSelection } from "@narratage/package-loader-node";import type { NodePackageSelectionRequest } from "@narratage/package-loader-node";import { canonicalize } from "@narratage/protocol";@@ -30,7 +29,6 @@ import { createLocalRuntime } from "./runtime.js";import {createLocalRuntimeArchiveControl,createLocalRuntimeArtifactAccess,- createLocalRuntimeControl,} from "./control.js";import { createLocalCredentialControl } from "./credentials.js";import type {@@ -38,7 +36,6 @@ import type {LocalRuntime,LocalRuntimeArchiveControl,LocalRuntimeArtifactAccess,- LocalRuntimeControl,} from "./types.js";export type RuntimeConfigEntry = {@@ -54,14 +51,11 @@ export type RuntimeConfigDocument = {readonly artifacts: RuntimeConfigEntry;readonly credentials: readonly RuntimeConfigEntry[];readonly endpoints: readonly RuntimeConfigEntry[];- readonly concurrency: number;};export type LoadRuntimeConfigOptions = {readonly registry?: RuntimeAdapterRegistry;- readonly components?: readonly ComponentPackage[];readonly packageRoot?: string;- readonly readOnly?: boolean;};export type RuntimeConfigDoctorResult = {@@ -101,11 +95,6 @@ function requiredString(value: unknown, subject: string): string {return value;}-function positiveInteger(value: unknown, subject: string): number {- if (!Number.isSafeInteger(value) || (value as number) < 1) throw new Error(`${subject} must be a positive integer`);- return value as number;-}-function entry(value: unknown, instance: string, subject: string, poolAllowed = false): RuntimeConfigEntry {const item = object(value, subject);exactKeys(item, poolAllowed ? ["use", "pool", "config"] : ["use", "config"], subject);@@ -138,7 +127,7 @@ export function parseRuntimeConfig(value: unknown): RuntimeConfigDocument {throw new Error(`Local Runtime loader cannot activate ${String(runtime.use)}`);}const config = object(runtime.config, "$runtime.runtime.config");- exactKeys(config, ["dataRoot", "artifacts", "credentials", "endpoints", "concurrency"], "$runtime.runtime.config");+ exactKeys(config, ["dataRoot", "artifacts", "credentials", "endpoints"], "$runtime.runtime.config");const artifacts = entry(config.artifacts, "artifacts", "$runtime.runtime.config.artifacts");const credentials = entries(config.credentials, "$runtime.runtime.config.credentials");const endpoints = entries(config.endpoints, "$runtime.runtime.config.endpoints", true);@@ -150,7 +139,6 @@ export function parseRuntimeConfig(value: unknown): RuntimeConfigDocument {artifacts,credentials,endpoints,- concurrency: positiveInteger(config.concurrency, "$runtime.runtime.config.concurrency"),};}@@ -264,7 +252,6 @@ async function openCredentialStores(document: RuntimeConfigDocument,root: string,registry: RuntimeAdapterRegistry,- readOnly: boolean,): Promise<{ readonly store: CredentialStore; close(): Promise<void> }> {const opened: RuntimeOpened<CredentialStore>[] = [];try {@@ -273,7 +260,6 @@ async function openCredentialStores(dataRoot: root,instance: item.instance,config: item.config ?? {},- access: readOnly ? "read-only" : "read-write",}));}return {@@ -292,14 +278,12 @@ async function openArtifactStore(document: RuntimeConfigDocument,root: string,registry: RuntimeAdapterRegistry,- readOnly: boolean,): Promise<RuntimeOpened<ArtifactStore>> {const item = document.artifacts;return await registry.openArtifactStore(item.use, {dataRoot: root,instance: item.instance,config: item.config ?? {},- access: readOnly ? "read-only" : "read-write",});}@@ -394,7 +378,7 @@ export async function doctorRuntimeConfig(if (credentialEndpoints.some((item) => item.endpoint.credentials.length > 0)) {let stores: Awaited<ReturnType<typeof openCredentialStores>> | undefined;try {- stores = await openCredentialStores(document, root, registry, true);+ stores = await openCredentialStores(document, root, registry);for (const activation of credentialEndpoints) {for (const slot of activation.endpoint.credentials) {if (await stores.store.resolve(slot.ref) !== undefined) continue;@@ -429,8 +413,8 @@ export async function createRuntimeFromConfig(let artifacts: RuntimeOpened<ArtifactStore> | undefined;let credentials: Awaited<ReturnType<typeof openCredentialStores>> | undefined;try {- artifacts = await openArtifactStore(document, root, registry, false);- credentials = await openCredentialStores(document, root, registry, false);+ artifacts = await openArtifactStore(document, root, registry);+ credentials = await openCredentialStores(document, root, registry);const endpoints = await Promise.all(document.endpoints.map(async (item) => await registry.createEndpoint(item.use, {dataRoot: root,instance: item.instance,@@ -444,13 +428,11 @@ export async function createRuntimeFromConfig(dispatchStore: state.dispatch,artifactStore: artifacts.value,credentialStore: credentials.store,- components: options.components ?? [],loadComponentPackages: async (specifiers) => {const loaded = await loadNodePackageSelection(specifiers, packageRoot);return collectNodePackageComponents(loaded.map((item) => item.contribution));},endpoints,- scheduling: { maxConcurrency: document.concurrency },close: async () => {await credentials?.close();await artifacts?.close?.();@@ -467,7 +449,7 @@ export async function createRuntimeFromConfig(export async function createRuntimeArchiveFromConfig(path: string,- options: LoadRuntimeConfigOptions = {},+ options: LoadRuntimeConfigOptions & { readonly readOnly?: boolean } = {},): Promise<LocalRuntimeArchiveControl> {const { root } = await openRuntimeConfig(path, options.packageRoot);const state = new SqliteRuntimeState(statePath(root), { readOnly: options.readOnly === true });@@ -489,42 +471,13 @@ export async function createRuntimeArtifactAccessFromConfig(await installRuntimeAdapters(registry, packageRoot, {selected: [], logical: [{ abi: runtimeArtifactStoreAdapterHostAbi, name: document.artifacts.use }],});- const opened = await openArtifactStore(document, root, registry, options.readOnly === true);+ const opened = await openArtifactStore(document, root, registry);return createLocalRuntimeArtifactAccess({artifactStore: opened.value,...(opened.close === undefined ? {} : { close: opened.close }),});}-export async function createRuntimeMaintenanceFromConfig(- path: string,- options: LoadRuntimeConfigOptions = {},-): Promise<LocalRuntimeControl> {- const { document, root, packageRoot } = await openRuntimeConfig(path, options.packageRoot);- const registry = options.registry ?? new RuntimeAdapterRegistry();- await installRuntimeAdapters(registry, packageRoot, {- selected: [], logical: [{ abi: runtimeArtifactStoreAdapterHostAbi, name: document.artifacts.use }],- });- const state = new SqliteRuntimeState(statePath(root), { readOnly: options.readOnly === true });- try {- const artifacts = await openArtifactStore(document, root, registry, options.readOnly === true);- return createLocalRuntimeControl({- buildStore: state.builds,- buildCatalog: state.catalog,- operationStore: state.operations,- dispatchStore: state.dispatch,- artifactStore: artifacts.value,- close: async () => {- await artifacts.close?.();- state.close();- },- });- } catch (error) {- state.close();- throw error;- }-}-export async function createRuntimeCredentialsFromConfig(path: string,endpointInstance: string,@@ -535,7 +488,7 @@ export async function createRuntimeCredentialsFromConfig(if (endpoint === undefined) throw new Error(`Runtime Profile has no Endpoint instance ${endpointInstance}`);const registry = options.registry ?? new RuntimeAdapterRegistry();await installRuntimeAdapters(registry, packageRoot, runtimePackageSelection(document));- const credentials = await openCredentialStores(document, root, registry, false);+ const credentials = await openCredentialStores(document, root, registry);try {const endpointPackage = await registry.createEndpoint(endpoint.use, {dataRoot: root,diff --git a/packages/runtime-local/src/control.ts b/packages/runtime-local/src/control.tsindex a239fb30..9b0e7ac7 100644--- a/packages/runtime-local/src/control.ts+++ b/packages/runtime-local/src/control.ts@@ -1,42 +1,14 @@-import { isDigest } from "@narratage/protocol";import {- isEnumerableBuildStore,- isManagedArtifactStore,isStreamingArtifactStore,} from "@narratage/runtime";import type {CreateLocalRuntimeArchiveControlOptions,CreateLocalRuntimeArtifactAccessOptions,- CreateLocalRuntimeControlOptions,LocalRuntimeArchiveControl,LocalRuntimeArtifactAccess,- LocalRuntimeControl,} from "./types.js";-function assert(condition: unknown, message: string): asserts condition {- if (!condition) throw new Error(message);-}--function collectArtifactDigests(- value: unknown,- digests: Set<import("@narratage/protocol").Digest>,- seen = new WeakSet<object>(),-): void {- if (value === null || typeof value !== "object") return;- if (seen.has(value)) return;- seen.add(value);- if (!Array.isArray(value)) {- const item = value as Record<string, unknown>;- if (item.kind === "blob" && typeof item.digest === "string" && isDigest(item.digest)) {- digests.add(item.digest);- }- }- for (const nested of Array.isArray(value) ? value : Object.values(value)) {- collectArtifactDigests(nested, digests, seen);- }-}-/** Durable execution-state control that never opens or depends on an ArtifactStore. */export function createLocalRuntimeArchiveControl(options: CreateLocalRuntimeArchiveControlOptions,@@ -51,19 +23,17 @@ export function createLocalRuntimeArchiveControl(return { operations, dispatch };},async status(build) {- const [snapshot, catalog, activity] = await Promise.all([+ const [snapshot, catalog, operations, dispatch] = await Promise.all([options.buildStore.read(build),buildCatalog?.read(build),- Promise.all([- options.operationStore.list({ build }),- options.dispatchStore.read(build),- ]),+ options.operationStore.list({ build }),+ options.dispatchStore.read(build),]);return {build: snapshot,catalog,- operations: activity[0],- dispatch: activity[1],+ operations,+ dispatch,};},async queue() {@@ -110,45 +80,3 @@ export function createLocalRuntimeArtifactAccess(},};}--/** 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");- assert(isEnumerableBuildStore(options.buildStore),- "selected BuildStore does not expose the maintenance index required for Artifact GC");- if (gc.apply === true) {- const active = await options.dispatchStore.list({ phases: ["queued", "running", "waiting"] });- assert(active.length === 0,- `Artifact GC cannot delete while ${active.length} Build${active.length === 1 ? " is" : "s are"} active`);- }- const reachable = new Set<import("@narratage/protocol").Digest>();- for (const snapshot of await options.buildStore.list()) collectArtifactDigests(snapshot.state, reachable);- for (const operation of await options.operationStore.list({})) collectArtifactDigests(operation, reachable);- const stored = await options.artifactStore.list();- const unreachable = stored.filter((digest) => !reachable.has(digest)).sort();- const deleted: import("@narratage/protocol").Digest[] = [];- if (gc.apply === true) {- for (const digest of unreachable) {- if (await options.artifactStore.delete(digest)) deleted.push(digest);- }- }- return {- reachable: [...reachable].sort(),- unreachable,- deleted,- };- },- close() {- return options.close?.();- },- };-}diff --git a/packages/runtime-local/src/host.ts b/packages/runtime-local/src/host.tsindex 85aa666c..ea86147a 100644--- a/packages/runtime-local/src/host.ts+++ b/packages/runtime-local/src/host.ts@@ -10,7 +10,6 @@ import {createRuntimeArtifactAccessFromConfig,createRuntimeCredentialsFromConfig,createRuntimeFromConfig,- createRuntimeMaintenanceFromConfig,doctorRuntimeConfig,resolveRuntimeConfigPaths,} from "./config.js";@@ -85,13 +84,8 @@ export async function openLocalRuntimeHost(packageRoot: basePackageRoot,...(options?.readOnly === undefined ? {} : { readOnly: options.readOnly }),}),- openArtifacts: async (options) => await createRuntimeArtifactAccessFromConfig(profile, {+ openArtifacts: async () => await createRuntimeArtifactAccessFromConfig(profile, {packageRoot: basePackageRoot,- ...(options?.readOnly === undefined ? {} : { readOnly: options.readOnly }),- }),- openMaintenance: async (options) => await createRuntimeMaintenanceFromConfig(profile, {- packageRoot: basePackageRoot,- ...(options?.readOnly === undefined ? {} : { readOnly: options.readOnly }),}),openCredentials: async (endpoint) => await createRuntimeCredentialsFromConfig(profile,diff --git a/packages/runtime-local/src/index.ts b/packages/runtime-local/src/index.tsindex cefc79f4..87290914 100644--- a/packages/runtime-local/src/index.ts+++ b/packages/runtime-local/src/index.ts@@ -1,7 +1,6 @@export {createLocalRuntimeArchiveControl,createLocalRuntimeArtifactAccess,- createLocalRuntimeControl,} from "./control.js";export { createLocalCredentialControl } from "./credentials.js";export {@@ -12,7 +11,6 @@ export {createRuntimeFromConfig,createRuntimeArchiveFromConfig,createRuntimeArtifactAccessFromConfig,- createRuntimeMaintenanceFromConfig,createRuntimeCredentialsFromConfig,declaredManagedPrograms,doctorRuntimeConfig,@@ -24,9 +22,6 @@ export {reportManagedPrograms,takeManagedProgramsDown,} from "./programs.js";-export {- RuntimeAdapterRegistry,-} from "@narratage/runtime-kit";export type * from "./types.js";export type * from "./config.js";export type * from "./programs.js";diff --git a/packages/runtime-local/src/programs.ts b/packages/runtime-local/src/programs.tsindex aa7871c2..8bd3af74 100644--- a/packages/runtime-local/src/programs.ts+++ b/packages/runtime-local/src/programs.ts@@ -130,10 +130,10 @@ function run(root: string, command: ManagedProgramCommand): Promise<{ ok: boolea* weights; `mismatch` is not, and stops the wait — a program that is answering* with another identity will not become the right one by waiting.*/-async function waitForReady(program: ManagedProgram, maxWaitMs: number): Promise<ManagedProgramState> {+async function waitForReady(program: ManagedProgram, pid: number, maxWaitMs: number): Promise<ManagedProgramState> {const deadline = Date.now() + maxWaitMs;let state = await program.probe();- while (state.state === "down" && Date.now() < deadline) {+ while (state.state === "down" && alive(pid) && Date.now() < deadline) {await sleep(1000);state = await program.probe();}@@ -192,15 +192,19 @@ async function bringUp(}await writeFile(join(directory(root), `${program.id}.pid`), `${child.pid}\n`);onProgress?.({ id: program.id, phase: "waiting" });- const state = await waitForReady(program, maxWaitMs);+ const state = await waitForReady(program, child.pid, maxWaitMs);if (state.state === "ready") onProgress?.({ id: program.id, phase: "ready" });+ const exited = state.state === "down" && !alive(child.pid);+ if (exited) await rm(join(directory(root), `${program.id}.pid`), { force: true });return {...base,action: state.state === "ready" ? "started" : "unchanged",state,pid: child.pid,logPath,- ...(state.state === "ready" ? {} : { detail: `see ${logPath}` }),+ ...(state.state === "ready" ? {} : {+ detail: exited ? `process exited; see ${logPath}` : `see ${logPath}`,+ }),};} finally {await log.close();@@ -213,6 +217,9 @@ export async function bringManagedProgramsUp(options: ManagedProgramOptions = {},): Promise<{ readonly dataRoot: string; readonly programs: readonly ManagedProgramReport[] }> {const { dataRoot, programs } = await declaredManagedPrograms(path, options);+ // A fresh Runtime has no data directory yet. Managed commands use it as+ // their working directory, so create it before the first prepare/start.+ await mkdir(dataRoot, { recursive: true });const reports = await Promise.all(distinct(programs).map(async ({ program, instances }) =>await bringUp(dataRoot, program, instances, options.maxWaitMs ?? 300_000, options.onProgress)));return { dataRoot, programs: reports };diff --git a/packages/runtime-local/src/runtime.ts b/packages/runtime-local/src/runtime.tsindex b458a014..844edcf3 100644--- a/packages/runtime-local/src/runtime.ts+++ b/packages/runtime-local/src/runtime.ts@@ -12,7 +12,7 @@ import {} from "@narratage/runtime";import { TypeValidatorRegistry } from "@narratage/validation";-import { createLocalRuntimeControl } from "./control.js";+import { createLocalRuntimeArchiveControl, createLocalRuntimeArtifactAccess } from "./control.js";import { createLocalCredentialControl } from "./credentials.js";import { createDurableLocalWorker } from "./worker.js";import type {@@ -55,7 +55,7 @@ export async function createLocalRuntime(const buildCatalog = options.buildCatalog;const producers = new ProducerRegistry();const endpoints = new EndpointRegistry();- const validators = options.validators ?? new TypeValidatorRegistry();+ const validators = new TypeValidatorRegistry();for (const component of options.components ?? []) {registerTypeValidatorFacets(validators, component.validators ?? []);registerProducerFacets(producers, component.producers ?? []);@@ -87,27 +87,26 @@ export async function createLocalRuntime(operations: options.operationStore,validators,});- const scheduling = options.scheduling;const worker = createDurableLocalWorker(driver, {stores: {builds: options.buildStore,operations: options.operationStore,dispatch: options.dispatchStore,},- scheduling,installComponentPackages,});const credentialControl = createLocalCredentialControl({credentialStore: options.credentialStore,endpoints: options.endpoints ?? [],});- const control = createLocalRuntimeControl({+ const archive = createLocalRuntimeArchiveControl({buildStore: options.buildStore,...(buildCatalog === undefined ? {} : { buildCatalog }),operationStore: options.operationStore,dispatchStore: options.dispatchStore,+ });+ const artifacts = createLocalRuntimeArtifactAccess({artifactStore: options.artifactStore,- ...(options.close === undefined ? {} : { close: options.close }),});const stageAttachments = async (request: LocalBuildRequest): Promise<void> => {for (const item of request.attachments ?? []) {@@ -185,7 +184,8 @@ export async function createLocalRuntime(return result;};return {- ...control,+ ...archive,+ ...artifacts,...credentialControl,build: runBuild,async workOnce() {@@ -194,6 +194,8 @@ export async function createLocalRuntime(async work(workOptions) {await worker.run(workOptions);},- close: control.close,+ close() {+ return options.close?.();+ },};}diff --git a/packages/runtime-local/src/types.ts b/packages/runtime-local/src/types.tsindex 8c328cbc..6af4b67c 100644--- a/packages/runtime-local/src/types.ts+++ b/packages/runtime-local/src/types.ts@@ -3,7 +3,6 @@ import type { EndpointPackage } from "@narratage/endpoint-kit";import type {ArtifactStore,BuildCatalog,- BuildSchedulerOptions,BuildStore,BuildDispatchStore,BuildDispatchSnapshot,@@ -13,21 +12,10 @@ import type {import type {RuntimeHostArchive,RuntimeHostArtifactAccess,- RuntimeHostArtifactGarbageCollection,RuntimeHostBuildSubmission,RuntimeHostCredentialControl,- RuntimeHostCredentialStatus,RuntimeHostExecution,- RuntimeHostMaintenance,- RuntimeHostStatus,} from "@narratage/runtime-host-node";-import type { TypeValidatorRegistrar, TypeValidatorRegistryLike } from "@narratage/validation";--export type { ComponentPackage } from "@narratage/component-kit";--export type LocalTypeValidatorRegistry = TypeValidatorRegistryLike & TypeValidatorRegistrar;--export type { EndpointPackage } from "@narratage/endpoint-kit";export type CreateLocalRuntimeOptions = {readonly buildStore: BuildStore;@@ -41,8 +29,6 @@ export type CreateLocalRuntimeOptions = {/** Load the component packages named by a claimed Build. */readonly loadComponentPackages?: (specifiers: readonly string[]) => Awaitable<readonly ComponentPackage[]>;readonly endpoints?: readonly EndpointPackage[];- readonly scheduling: Omit<BuildSchedulerOptions, "buildStore">;- readonly validators?: LocalTypeValidatorRegistry;readonly close?: () => Awaitable<void>;};@@ -61,9 +47,6 @@ export type CreateLocalRuntimeArtifactAccessOptions = {readonly close?: () => Awaitable<void>;};-export type CreateLocalRuntimeControlOptions = CreateLocalRuntimeArchiveControlOptions- & CreateLocalRuntimeArtifactAccessOptions;-export type CreateLocalCredentialControlOptions = {readonly credentialStore: CredentialStore;readonly endpoints: readonly EndpointPackage[];@@ -73,11 +56,6 @@ export type CreateLocalCredentialControlOptions = {export type LocalBuildRequest = Parameters<RuntimeHostExecution["build"]>[0];export type LocalBuildOptions = Parameters<RuntimeHostExecution["build"]>[1];export type LocalBuildSubmission = RuntimeHostBuildSubmission;-export type LocalRuntimeStatus = RuntimeHostStatus;-export type LocalRuntimeActivity = Awaited<ReturnType<RuntimeHostArchive["activity"]>>;-export type LocalRuntimeQueue = Awaited<ReturnType<RuntimeHostArchive["queue"]>>;-export type LocalCredentialStatus = RuntimeHostCredentialStatus;-export type ArtifactGarbageCollection = RuntimeHostArtifactGarbageCollection;export type LocalRuntime = RuntimeHostExecution & {workOnce(): Promise<BuildDispatchSnapshot | undefined>;@@ -89,8 +67,5 @@ export type LocalRuntimeArchiveControl = RuntimeHostArchive;/** Explicit Artifact byte access that never opens Build, Operation or Dispatch state. */export type LocalRuntimeArtifactAccess = RuntimeHostArtifactAccess;-/** Durable project control used only when one operation truly spans state and Artifacts. */-export type LocalRuntimeControl = RuntimeHostMaintenance;-/** Credential control for one or more exact Endpoint declarations; no execution state is opened. */export type LocalCredentialControl = RuntimeHostCredentialControl;diff --git a/packages/runtime-local/src/worker-process.ts b/packages/runtime-local/src/worker-process.tsindex d99be7f6..720d9457 100644--- a/packages/runtime-local/src/worker-process.ts+++ b/packages/runtime-local/src/worker-process.ts@@ -138,12 +138,8 @@ export async function ensureRuntimeProcess(launch: RuntimeWorkerLaunch,timeoutMs = 10_000,): Promise<RuntimeProcessState> {- const existing = await record(profile, dataRoot);const current = await runtimeProcessStatus(profile, dataRoot);if (current.state === "running") return current;- if (existing !== undefined && alive(existing.pid)) {- await stopRuntimeProcessUnlocked(profile, dataRoot, timeoutMs);- }const absolute = resolve(profile);const location = paths(dataRoot);await mkdir(location.root, { recursive: true });diff --git a/packages/runtime-local/src/worker.ts b/packages/runtime-local/src/worker.tsindex 09b87da4..60ad78bb 100644--- a/packages/runtime-local/src/worker.ts+++ b/packages/runtime-local/src/worker.ts@@ -1,12 +1,11 @@import type { BuildState } from "@narratage/protocol";import type {BuildDispatchSnapshot,- BuildSchedulerOptions,- CapacityResourceClaim,RuntimeCommandExecutor,RuntimeExecutionResult,RuntimePreparation,RuntimeRunnableCommand,+ ScheduledBuildResult,RuntimeWorkerRunOptions,} from "@narratage/runtime";import { LocalBuildScheduler } from "@narratage/runtime";@@ -17,7 +16,6 @@ type LocalWorkerOptions = {readonly operations: import("@narratage/runtime").OperationStore;readonly dispatch: import("@narratage/runtime").BuildDispatchStore;};- readonly scheduling: BuildSchedulerOptions;readonly installComponentPackages: (specifiers: readonly string[]) => Promise<void>;};@@ -33,88 +31,28 @@ function positive(value: number, subject: string): number {async function pause(ms: number, signal?: AbortSignal): Promise<void> {if (signal?.aborted === true) throw signal.reason ?? new Error("Worker stopped");await new Promise<void>((resolve, reject) => {- const timer = setTimeout(resolve, ms);- signal?.addEventListener("abort", () => {+ const done = (): void => {+ signal?.removeEventListener("abort", abort);+ resolve();+ };+ const timer = setTimeout(done, ms);+ const abort = (): void => {clearTimeout(timer);- reject(signal.reason ?? new Error("Worker stopped"));- }, { once: true });- });-}--type GateWaiter = {- readonly resources: readonly CapacityResourceClaim[];- readonly resolve: (release: () => void) => void;-};--/** One process-wide command limit shared by every Build owned by this Worker. */-class CommandGate {- readonly #limit: number;- readonly #overrides: Readonly<Record<string, number>>;- readonly #activeByResource = new Map<string, number>();- readonly #waiters: GateWaiter[] = [];- #active = 0;-- constructor(limit: number, overrides: Readonly<Record<string, number>> = {}) {- this.#limit = positive(limit, "Worker command concurrency");- this.#overrides = overrides;- }-- #canRun(resources: readonly CapacityResourceClaim[]): boolean {- if (this.#active >= this.#limit) return false;- return resources.every((resource) =>- (this.#activeByResource.get(resource.id) ?? 0)- < (this.#overrides[resource.id] ?? resource.maxActive));- }-- #start(resources: readonly CapacityResourceClaim[]): () => void {- this.#active += 1;- for (const resource of resources) {- this.#activeByResource.set(resource.id, (this.#activeByResource.get(resource.id) ?? 0) + 1);- }- let released = false;- return () => {- if (released) return;- released = true;- this.#active -= 1;- for (const resource of resources) {- const remaining = (this.#activeByResource.get(resource.id) ?? 1) - 1;- if (remaining === 0) this.#activeByResource.delete(resource.id);- else this.#activeByResource.set(resource.id, remaining);- }- this.#drain();+ signal?.removeEventListener("abort", abort);+ reject(signal?.reason ?? new Error("Worker stopped"));};- }-- #drain(): void {- for (let index = 0; index < this.#waiters.length;) {- const waiter = this.#waiters[index]!;- if (!this.#canRun(waiter.resources)) {- index += 1;- continue;- }- this.#waiters.splice(index, 1);- waiter.resolve(this.#start(waiter.resources));- }- }-- async acquire(resources: readonly CapacityResourceClaim[]): Promise<() => void> {- if (this.#canRun(resources)) return this.#start(resources);- return await new Promise<() => void>((resolve) => {- this.#waiters.push({ resources, resolve });- });- }+ signal?.addEventListener("abort", abort, { once: true });+ });}-/** Adds persistent in-flight limits only to asynchronous external Operations. */+/** Shares declared capacity across independently advancing Builds. */class CapacityExecutor implements RuntimeCommandExecutor {readonly #delegate: RuntimeCommandExecutor;readonly #options: LocalWorkerOptions;- readonly #gate: CommandGate;- constructor(delegate: RuntimeCommandExecutor, options: LocalWorkerOptions, gate: CommandGate) {+ constructor(delegate: RuntimeCommandExecutor, options: LocalWorkerOptions) {this.#delegate = delegate;this.#options = options;- this.#gate = gate;}prepare(state: BuildState): RuntimePreparation {@@ -132,45 +70,40 @@ class CapacityExecutor implements RuntimeCommandExecutor {descriptor: RuntimeRunnableCommand,context: { readonly build: string },): Promise<RuntimeExecutionResult> {- const resources = descriptor.resources.map((resource) => {- const override = this.#options.scheduling.resourceLimits?.[resource.id];- return override === undefined- ? resource- : { ...resource, maxActive: override, maxInFlight: override };- });+ const resources = descriptor.resources;await this.#assertRunning(context.build);- const releaseCommand = await this.#gate.acquire(resources);+ if (resources.length === 0) {+ return await this.#delegate.executeCommand(state, descriptor, context);+ }+ const acquired = await this.#options.stores.dispatch.acquireCapacity({+ build: context.build,+ command: descriptor.command.id,+ resources,+ ...(descriptor.queue === undefined ? {} : { queue: descriptor.queue }),+ now: Date.now(),+ });+ if (acquired.status === "blocked") {+ return {+ status: "deferred",+ wakeAt: acquired.availableAt,+ reason: `${acquired.reason}:${acquired.resource}`,+ };+ }try {- await this.#assertRunning(context.build);- if (descriptor.capacityMode !== "asynchronous") {- return await this.#delegate.executeCommand(state, descriptor, context);- }- const acquired = await this.#options.stores.dispatch.acquireCapacity({- build: context.build,- command: descriptor.command.id,- resources,- ...(descriptor.queue === undefined ? {} : { queue: descriptor.queue }),- now: Date.now(),- });- if (acquired.status === "blocked") {- return {- status: "deferred",- wakeAt: acquired.availableAt,- reason: `${acquired.reason}:${acquired.resource}`,- };- }- try {- const result = await this.#delegate.executeCommand(state, descriptor, context);- if (result.status !== "pending") {- await this.#options.stores.dispatch.releaseCapacity(acquired.reservation.id);- }- return result;- } catch (error) {- await this.#options.stores.dispatch.releaseCapacity(acquired.reservation.id).catch(() => undefined);- throw error;+ const result = await this.#delegate.executeCommand(state, descriptor, context);+ if (result.status !== "pending") {+ await this.#options.stores.dispatch.releaseCapacity(+ acquired.reservation.build,+ acquired.reservation.command,+ );}- } finally {- releaseCommand();+ return result;+ } catch (error) {+ await this.#options.stores.dispatch.releaseCapacity(+ acquired.reservation.build,+ acquired.reservation.command,+ ).catch(() => undefined);+ throw error;}}@@ -191,10 +124,7 @@ class DurableLocalWorker {constructor(executor: RuntimeCommandExecutor, options: LocalWorkerOptions) {this.#executor = executor;this.#options = options;- this.#executorWithCapacity = new CapacityExecutor(executor, options, new CommandGate(- options.scheduling.maxConcurrency ?? 1,- options.scheduling.resourceLimits,- ));+ this.#executorWithCapacity = new CapacityExecutor(executor, options);}async #cancel(dispatch: BuildDispatchSnapshot): Promise<BuildDispatchSnapshot> {@@ -217,91 +147,115 @@ class DurableLocalWorker {);}- async #runClaimed(dispatch: BuildDispatchSnapshot): Promise<BuildDispatchSnapshot> {- if (dispatch.cancellation !== undefined) return await this.#cancel(dispatch);- try {- await this.#options.installComponentPackages(dispatch.componentPackages);- const stored = await this.#options.stores.builds.read(dispatch.build);- assert(stored !== undefined, `Dispatch ${dispatch.build} has no Build Definition`);- const scheduler = new LocalBuildScheduler(this.#executorWithCapacity, {- ...this.#options.scheduling,- buildStore: this.#options.stores.builds,- });- const [result] = await scheduler.run([{ id: dispatch.build, state: stored.state, snapshot: stored }]);- assert(result !== undefined, `Scheduler returned no result for ${dispatch.build}`);- const current = await this.#options.stores.dispatch.read(dispatch.build);- assert(current?.phase === "running", `Dispatch ${dispatch.build} is no longer running`);- if (current.cancellation !== undefined) return await this.#cancel(current);- if (result.status === "complete") {- await this.#options.stores.dispatch.releaseBuildCapacity(dispatch.build);- return await this.#options.stores.dispatch.finish(dispatch.build, "complete");+ async #finish(dispatch: BuildDispatchSnapshot, result: ScheduledBuildResult): Promise<BuildDispatchSnapshot> {+ const current = await this.#options.stores.dispatch.read(dispatch.build);+ assert(current?.phase === "running", `Dispatch ${dispatch.build} is no longer running`);+ if (current.cancellation !== undefined) return await this.#cancel(current);+ if (result.status === "complete") {+ await this.#options.stores.dispatch.releaseBuildCapacity(dispatch.build);+ return await this.#options.stores.dispatch.finish(dispatch.build, "complete");+ }+ if (result.status === "failed" || result.outcomes.some((item) => item.status === "error")) {+ const reason = result.outcomes.find((item) => item.status === "error")?.message ?? "Core Build failed";+ await this.#options.stores.dispatch.releaseBuildCapacity(dispatch.build);+ return await this.#options.stores.dispatch.finish(dispatch.build, "failed", reason);+ }+ const pending = result.outcomes.filter((item) => item.status === "pending");+ const deferred = result.outcomes.filter((item) => item.status === "deferred");+ if (pending.length === 0 && deferred.length === 0 && result.blocked.length > 0) {+ const reason = result.blocked.map((item) => `${item.reason}: ${item.subject}`).join(", ");+ await this.#options.stores.dispatch.releaseBuildCapacity(dispatch.build);+ return await this.#options.stores.dispatch.finish(dispatch.build, "failed", reason);+ }+ const now = Date.now();+ const wakeAt = Math.min(...[+ ...pending.map((item) => item.wakeAt ?? now + 1_000),+ ...deferred.map((item) => item.wakeAt ?? now + 1_000),+ ]);+ const released = await this.#options.stores.dispatch.release(dispatch.build, {+ phase: pending.length > 0 ? "waiting" : "queued",+ availableAt: Number.isFinite(wakeAt) ? wakeAt : now,+ });+ return released.cancellation === undefined ? released : await this.#cancel(released);+ }++ async #fail(dispatch: BuildDispatchSnapshot, error: unknown): Promise<BuildDispatchSnapshot> {+ const current = await this.#options.stores.dispatch.read(dispatch.build);+ if (current?.cancellation !== undefined) return await this.#cancel(current);+ const reason = error instanceof Error ? error.message : String(error);+ await this.#options.stores.dispatch.releaseBuildCapacity(dispatch.build);+ return await this.#options.stores.dispatch.finish(dispatch.build, "failed", reason);+ }++ async #runClaimed(dispatches: readonly BuildDispatchSnapshot[]): Promise<readonly BuildDispatchSnapshot[]> {+ const finished: BuildDispatchSnapshot[] = [];+ const runnable: { readonly dispatch: BuildDispatchSnapshot; readonly snapshot: import("@narratage/runtime").BuildSnapshot }[] = [];+ for (const dispatch of dispatches) {+ if (dispatch.cancellation !== undefined) {+ finished.push(await this.#cancel(dispatch));+ continue;}- if (result.status === "failed" || result.outcomes.some((item) => item.status === "error")) {- const reason = result.outcomes.find((item) => item.status === "error")?.message ?? "Core Build failed";- await this.#options.stores.dispatch.releaseBuildCapacity(dispatch.build);- return await this.#options.stores.dispatch.finish(dispatch.build, "failed", reason);+ try {+ await this.#options.installComponentPackages(dispatch.componentPackages);+ const snapshot = await this.#options.stores.builds.read(dispatch.build);+ assert(snapshot !== undefined, `Dispatch ${dispatch.build} has no Build Definition`);+ runnable.push({ dispatch, snapshot });+ } catch (error) {+ finished.push(await this.#fail(dispatch, error));}- const pending = result.outcomes.filter((item) => item.status === "pending");- const deferred = result.outcomes.filter((item) => item.status === "deferred");- if (pending.length === 0 && deferred.length === 0 && result.blocked.length > 0) {- const reason = result.blocked.map((item) => `${item.reason}: ${item.subject}`).join(", ");- await this.#options.stores.dispatch.releaseBuildCapacity(dispatch.build);- return await this.#options.stores.dispatch.finish(dispatch.build, "failed", reason);+ }+ if (runnable.length === 0) return finished;+ const scheduler = new LocalBuildScheduler(this.#executorWithCapacity, {+ buildStore: this.#options.stores.builds,+ });+ try {+ const results = await scheduler.run(runnable.map(({ dispatch, snapshot }) => ({+ id: dispatch.build,+ snapshot,+ })));+ const byBuild = new Map(runnable.map((item) => [item.dispatch.build, item]));+ for (const result of results) {+ const item = byBuild.get(result.id);+ assert(item !== undefined, `Scheduler returned unknown Build ${result.id}`);+ finished.push(await this.#finish(item.dispatch, result));}- const now = Date.now();- const wakeTimes = [- ...pending.map((item) => item.wakeAt ?? now + 1_000),- ...deferred.map((item) => item.wakeAt ?? now + 1_000),- ];- const wakeAt = wakeTimes.length === 0 ? now : Math.min(...wakeTimes);- const released = await this.#options.stores.dispatch.release(dispatch.build, {- phase: pending.length > 0 ? "waiting" : "queued",- availableAt: wakeAt,- });- return released.cancellation === undefined ? released : await this.#cancel(released);} catch (error) {- const current = await this.#options.stores.dispatch.read(dispatch.build);- if (current?.cancellation !== undefined) return await this.#cancel(current);- const reason = error instanceof Error ? error.message : String(error);- await this.#options.stores.dispatch.releaseBuildCapacity(dispatch.build);- return await this.#options.stores.dispatch.finish(dispatch.build, "failed", reason);+ for (const item of runnable) finished.push(await this.#fail(item.dispatch, error));}+ return finished;}async runOnce(): Promise<BuildDispatchSnapshot | undefined> {const dispatch = await this.#options.stores.dispatch.claim(Date.now());- return dispatch === undefined ? undefined : await this.#runClaimed(dispatch);+ if (dispatch === undefined) return undefined;+ const [result] = await this.#runClaimed([dispatch]);+ return result;}async run(options: RuntimeWorkerRunOptions): Promise<void> {positive(options.idlePollMs, "Worker idlePollMs");- const active = new Set<Promise<BuildDispatchSnapshot>>();- const maxBuilds = positive(this.#options.scheduling.maxConcurrency ?? 1, "Worker active Builds");- const launch = (dispatch: BuildDispatchSnapshot): void => {- const task = this.#runClaimed(dispatch);- active.add(task);- void task.finally(() => active.delete(task)).catch(() => undefined);- };- while (options.signal?.aborted !== true) {- while (active.size < maxBuilds) {- const dispatch = await this.#options.stores.dispatch.claim(Date.now());- if (dispatch === undefined) break;- launch(dispatch);- }- if (active.size === 0) {- await pause(options.idlePollMs, options.signal).catch((error: unknown) => {+ const active = new Set<Promise<void>>();+ try {+ while (options.signal?.aborted !== true) {+ while (true) {+ const dispatch = await this.#options.stores.dispatch.claim(Date.now());+ if (dispatch === undefined) break;+ let task: Promise<void>;+ task = this.#runClaimed([dispatch])+ .then(() => undefined)+ .finally(() => active.delete(task));+ active.add(task);+ }+ await Promise.race([+ ...active,+ pause(options.idlePollMs, options.signal),+ ]).catch((error: unknown) => {if (options.signal?.aborted !== true) throw error;});- continue;}- await Promise.race([- ...active,- pause(options.idlePollMs, options.signal).then(() => undefined),- ]).catch((error: unknown) => {- if (options.signal?.aborted !== true) throw error;- });+ } finally {+ await Promise.allSettled(active);}- await Promise.all(active);}}diff --git a/packages/runtime-local/test/config.test.ts b/packages/runtime-local/test/config.test.tsindex 258a6744..fd047ad5 100644--- a/packages/runtime-local/test/config.test.ts+++ b/packages/runtime-local/test/config.test.ts@@ -7,6 +7,7 @@ import test from "node:test";import {createRuntimeArtifactStoreAdapterFacet,createRuntimeEndpointAdapterFacet,+ RuntimeAdapterRegistry,} from "@narratage/runtime-kit";import { MemoryArtifactStore } from "@narratage/driver-node";import { SqliteRuntimeState } from "@narratage/store-sqlite";@@ -15,7 +16,6 @@ import {createRuntimeArtifactAccessFromConfig,doctorRuntimeConfig,parseRuntimeConfig,- RuntimeAdapterRegistry,} from "@narratage/runtime-local";function profile(config: {@@ -33,7 +33,6 @@ function profile(config: {artifacts: config.artifacts ?? { use: "example.artifacts" },credentials: config.credentials ?? {},endpoints: config.endpoints ?? {},- concurrency: 3,},},};@@ -48,7 +47,6 @@ test("Runtime Profile names the stores and Endpoints used by one local Runtime",assert.deepEqual(parsed.artifacts, { use: "example.artifacts", instance: "artifacts" });assert.deepEqual(parsed.credentials, [{ use: "example.credentials", instance: "secrets" }]);assert.deepEqual(parsed.endpoints, [{ use: "example.provider", instance: "generation", pool: "shared" }]);- assert.equal(parsed.concurrency, 3);});test("Runtime Profile rejects source ownership fields", () => {@@ -76,7 +74,7 @@ test("archive inspection opens SQLite only; Artifact access opens the selected Sassert.equal((await archive.status("missing")).build, undefined);await archive.close();assert.equal(artifactConstructions, 0);- const artifacts = await createRuntimeArtifactAccessFromConfig(path, { registry, readOnly: true });+ const artifacts = await createRuntimeArtifactAccessFromConfig(path, { registry });await artifacts.close();assert.equal(artifactConstructions, 1);} finally {diff --git a/packages/runtime-local/test/local.test.ts b/packages/runtime-local/test/local.test.tsindex 94678c75..c0010e39 100644--- a/packages/runtime-local/test/local.test.ts+++ b/packages/runtime-local/test/local.test.ts@@ -14,11 +14,8 @@ import { FileArtifactStore } from "@narratage/artifact-store-fs";import { EnvironmentCredentialStore } from "@narratage/credential-store-env";import { MemoryArtifactStore } from "@narratage/driver-node";import { defineEndpointPackage } from "@narratage/endpoint-kit";-import type { AsyncEndpoint } from "@narratage/endpoint-kit";-import type {- ComponentPackage,- EndpointPackage,-} from "@narratage/runtime-local";+import type { AsyncEndpoint, EndpointPackage } from "@narratage/endpoint-kit";+import type { ComponentPackage } from "@narratage/component-kit";import { createLocalRuntime } from "@narratage/runtime-local";import { defineBuild } from "@narratage/core";import {@@ -52,35 +49,10 @@ function projectRuntimeFixture(directory: string) {dispatchStore: state.dispatch,artifactStore: new FileArtifactStore(join(directory, ".narratage", "artifacts")),credentialStore: new EnvironmentCredentialStore(),- scheduling: { maxConcurrency: 4 },close: () => state.close(),} as const;}-test("Artifact GC is explicit, dry-run by default, and only removes unreachable managed bytes", async () => {- const directory = await mkdtemp(join(tmpdir(), "narratage-local-artifact-gc-"));- try {- const artifacts = new FileArtifactStore(join(directory, ".narratage", "artifacts"));- const orphan = await artifacts.put(new TextEncoder().encode("orphan"), "application/octet-stream");- const runtime = await createLocalRuntime(projectRuntimeFixture(directory));- const preview = await runtime.garbageCollectArtifacts();- assert.deepEqual(preview.unreachable, [orphan.digest]);- assert.deepEqual(preview.deleted, []);- assert.equal(await artifacts.has(orphan.digest), true);- await runtime.build({ id: "active-build", definition: definition(createGreetingBuild()) });- await assert.rejects(- async () => await runtime.garbageCollectArtifacts({ apply: true }),- /Artifact GC cannot delete while 1 Build is active/u,- );- await runtime.cancel("active-build");- const applied = await runtime.garbageCollectArtifacts({ apply: true });- assert.deepEqual(applied.deleted, [orphan.digest]);- assert.equal(await artifacts.has(orphan.digest), false);- } finally {- await rm(directory, { recursive: true, force: true });- }-});-test("Endpoint-declared credentials use the selected writable Store without a Provider switch", async () => {const directory = await mkdtemp(join(tmpdir(), "narratage-local-auth-"));const values = new Map<string, CredentialValue>();@@ -265,17 +237,27 @@ test("project local runtime queues, polls and cancels work with replaceable pack}});-test("one local Worker advances independent Builds concurrently under one command limit", async () => {+test("one local Worker admits later Builds while preserving shared Endpoint capacity", async () => {const directory = await mkdtemp(join(tmpdir(), "narratage-local-parallel-builds-"));- let active = 0;- let mostActive = 0;+ let unrestrictedActive = 0;+ let mostUnrestricted = 0;+ let limitedActive = 0;+ let mostLimited = 0;+ let markFirstStarted: (() => void) | undefined;+ const firstStarted = new Promise<void>((resolve) => { markFirstStarted = resolve; });const components: ComponentPackage = {producers: [{producer: producers.makePrompt,- handler: ({ inputs }) => {+ handler: async ({ inputs }) => {if (inputs.intent?.value.kind !== "inline") throw new Error("missing greeting intent");const intent = inputs.intent.value.value as { readonly name: string };+ unrestrictedActive += 1;+ mostUnrestricted = Math.max(mostUnrestricted, unrestrictedActive);+ markFirstStarted?.();+ markFirstStarted = undefined;+ await new Promise((resolve) => setTimeout(resolve, 80));+ unrestrictedActive -= 1;return {outputs: { prompt: { kind: "inline", value: `Greet ${intent.name}` } },needs: {},@@ -283,17 +265,10 @@ test("one local Worker advances independent Builds concurrently under one comman},},{- producer: producers.placeholderText,- handler: async ({ inputs }) => {+ producer: producers.requestText,+ handler: ({ inputs }) => {if (inputs.prompt?.value.kind !== "inline") throw new Error("missing greeting prompt");- active += 1;- mostActive = Math.max(mostActive, active);- await new Promise((resolve) => setTimeout(resolve, 80));- active -= 1;- return {- outputs: { generated: { kind: "inline", value: `Preview: ${inputs.prompt.value.value}` } },- needs: {},- };+ return { outputs: {}, needs: { generation: { prompt: inputs.prompt.value.value } } };},},{@@ -308,17 +283,42 @@ test("one local Worker advances independent Builds concurrently under one comman},],};+ const endpoint = defineEndpointPackage({+ module: providerModule,+ facet: "generation",+ instance: "generation.serial",+ pool: "generation.serial",+ defaultConcurrency: 1,+ capabilities: [{+ lifecycle: "immediate",+ capability: capabilities.generation,+ returns: types.generated,+ handler: async () => {+ limitedActive += 1;+ mostLimited = Math.max(mostLimited, limitedActive);+ await new Promise((resolve) => setTimeout(resolve, 80));+ limitedActive -= 1;+ return { value: { kind: "inline", value: "Hello" } };+ },+ }],+ });try {const runtime = await createLocalRuntime({...projectRuntimeFixture(directory),components: [components],+ endpoints: [endpoint],+ });+ await runtime.build({+ id: "parallel-a",+ definition: definition(createGreetingBuild()),});- await Promise.all(["parallel-a", "parallel-b"].map(async (id) => await runtime.build({- id,- definition: definition(createGreetingBuild({ generationRealization: "placeholder" })),- })));const controller = new AbortController();const work = runtime.work({ idlePollMs: 5, signal: controller.signal });+ await firstStarted;+ await runtime.build({+ id: "parallel-b",+ definition: definition(createGreetingBuild()),+ });while (true) {const states = await Promise.all(["parallel-a", "parallel-b"].map(async (id) =>(await runtime.status(id)).dispatch?.terminal));@@ -327,7 +327,8 @@ test("one local Worker advances independent Builds concurrently under one comman}controller.abort();await work;- assert.equal(mostActive, 2);+ assert.equal(mostUnrestricted, 2, "a later Build must join work already in progress");+ assert.equal(mostLimited, 1, "declared capacity must span independent Builds");await runtime.close();} finally {await rm(directory, { recursive: true, force: true });diff --git a/packages/runtime-local/test/programs.test.ts b/packages/runtime-local/test/programs.test.tsindex ca24d72a..6cd5c2d9 100644--- a/packages/runtime-local/test/programs.test.ts+++ b/packages/runtime-local/test/programs.test.ts@@ -5,7 +5,7 @@ import { join } from "node:path";import test from "node:test";import { setTimeout as sleep } from "node:timers/promises";-import { createRuntimeEndpointAdapterFacet } from "@narratage/runtime-kit";+import { createRuntimeEndpointAdapterFacet, RuntimeAdapterRegistry } from "@narratage/runtime-kit";import type { ManagedProgram } from "@narratage/runtime-kit";import type { CapabilityRef } from "@narratage/protocol";@@ -13,7 +13,6 @@ import {bringManagedProgramsUp,declaredManagedPrograms,reportManagedPrograms,- RuntimeAdapterRegistry,takeManagedProgramsDown,} from "@narratage/runtime-local";@@ -42,7 +41,6 @@ async function project(program: (root: string) => ManagedProgram) {one: { use: "example.program", pool: "example.local", config: {} },two: { use: "example.program", pool: "example.local", config: {} },},- concurrency: 1,},},}));@@ -201,6 +199,59 @@ test("a program with nothing to start is prepared, and preparing is the whole joawait rm(directory, { recursive: true, force: true });});+test("up creates a fresh Runtime data directory before running commands", async () => {+ const projectRoot = await mkdtemp(join(tmpdir(), "narratage-fresh-runtime-"));+ const dataRoot = join(projectRoot, "never-created");+ const path = join(projectRoot, "narratage.runtime.json");+ await writeFile(path, JSON.stringify({+ format: "narratage.runtime-profile@1",+ runtime: {+ use: "@narratage/runtime-local",+ config: {+ dataRoot: "./never-created",+ artifacts: { use: "example.artifacts" },+ credentials: {},+ endpoints: { one: { use: "example.program", config: {} } },+ },+ },+ }));+ const registry = new RuntimeAdapterRegistry();+ registry.registerFacet(createRuntimeEndpointAdapterFacet({+ use: "example.program",+ activate: () => ({+ endpoint: {+ name: "one",+ manifest: { facets: [] },+ instance: { id: "one" },+ offers: [{+ capability: requiredCapability,+ returns: { module: { name: "example.values", version: "1" }, name: "Value" },+ endpoint: "one",+ }],+ credentials: [],+ install() {},+ } as never,+ program: {+ id: "example",+ prepare: { command: "sh", args: ["-c", "printf ready > prepared"] },+ async probe() {+ try {+ await readFile(join(dataRoot, "prepared"), "utf8");+ return { state: "ready" as const };+ } catch {+ return { state: "down" as const, detail: "not prepared" };+ }+ },+ },+ }),+ }));++ const result = await bringManagedProgramsUp(path, { registry });+ assert.equal(result.programs[0]!.action, "prepared");+ assert.equal(await readFile(join(dataRoot, "prepared"), "utf8"), "ready");+ await rm(projectRoot, { recursive: true, force: true });+});+test("a failing prepare stops before starting anything, and says which command failed", async () => {const { path, options } = await project(() => ({id: "example",@@ -214,6 +265,20 @@ test("a failing prepare stops before starting anything, and says which command fassert.equal(result.programs[0]!.logPath, undefined, "nothing was started, so nothing logged");});+test("up stops waiting when a started program exits", async () => {+ const { root, path, options } = await project(() => ({+ id: "example",+ start: { command: "sh", args: ["-c", "exit 1"] },+ probe: async () => ({ state: "down", detail: "nothing is answering" }),+ }));+ const startedAt = Date.now();+ const result = await bringManagedProgramsUp(path, { ...options, maxWaitMs: 20_000 });+ assert.ok(Date.now() - startedAt < 5_000, "a dead program must not consume the readiness timeout");+ assert.equal(result.programs[0]!.action, "unchanged");+ assert.match(result.programs[0]!.detail ?? "", /process exited; see/u);+ await assert.rejects(async () => await readFile(join(root, "programs", "example.pid"), "utf8"));+});+test("status probes and changes nothing, so it claims no action", async () => {const { path, options } = await project(() => ({id: "example",diff --git a/packages/runtime/src/dispatch.ts b/packages/runtime/src/dispatch.tsindex 32476f3b..12f3c20b 100644--- a/packages/runtime/src/dispatch.ts+++ b/packages/runtime/src/dispatch.ts@@ -36,7 +36,6 @@ export type CapacityResourceClaim = {/** One asynchronous external Operation that currently occupies Provider capacity. */export type CapacityReservation = {- readonly id: string;readonly build: string;readonly command: string;readonly resources: readonly CapacityResourceClaim[];@@ -76,16 +75,7 @@ export type BuildDispatchStore = {finish(build: string, terminal: DispatchTerminal, reason?: string): Promise<BuildDispatchSnapshot>;requestCancellation(build: string, reason?: string): Promise<BuildDispatchSnapshot>;acquireCapacity(request: CapacityAcquireRequest): Promise<CapacityAcquire>;- releaseCapacity(id: string): Promise<void>;+ releaseCapacity(build: string, command: string): Promise<void>;releaseBuildCapacity(build: string): Promise<void>;listCapacity(): Promise<readonly CapacityReservation[]>;};--function assert(condition: unknown, message: string): asserts condition {- if (!condition) throw new Error(message);-}--export function capacityReservationId(build: string, command: string): string {- assert(build.trim().length > 0 && command.trim().length > 0, "Capacity reservation identity is empty");- return `capacity:${encodeURIComponent(build)}:${encodeURIComponent(command)}`;-}diff --git a/packages/runtime/src/index.ts b/packages/runtime/src/index.tsindex eae56651..eb8455f8 100644--- a/packages/runtime/src/index.ts+++ b/packages/runtime/src/index.ts@@ -8,15 +8,10 @@ export {verifyCredentialRef,} from "./credentials.js";export type * from "./credentials.js";-export {- capacityReservationId,-} from "./dispatch.js";export type * from "./dispatch.js";export type * from "./operations.js";export { LocalBuildScheduler } from "./scheduler.js";export {- isEnumerableBuildStore,- isManagedArtifactStore,isStreamingArtifactStore,} from "./types.js";export type * from "./types.js";diff --git a/packages/runtime/src/scheduler.ts b/packages/runtime/src/scheduler.tsindex 4d74ab25..361ae8a9 100644--- a/packages/runtime/src/scheduler.ts+++ b/packages/runtime/src/scheduler.ts@@ -50,19 +50,11 @@ function buildCommandKey(build: string, command: string): string {*/export class LocalBuildScheduler {readonly #executor: RuntimeCommandExecutor;- readonly #maxConcurrency: number;- readonly #resourceLimits: Readonly<Record<string, number>>;readonly #buildStore: BuildSchedulerOptions["buildStore"];constructor(executor: RuntimeCommandExecutor, options: BuildSchedulerOptions = {}) {this.#executor = executor;- this.#maxConcurrency = positiveInteger(options.maxConcurrency ?? 4, "maxConcurrency");this.#buildStore = options.buildStore;- for (const [resource, limit] of Object.entries(options.resourceLimits ?? {})) {- if (resource.trim().length === 0) throw new Error("resource override name must not be empty");- positiveInteger(limit, `resource ${resource}`);- }- this.#resourceLimits = { ...(options.resourceLimits ?? {}) };}async run(requests: readonly ScheduledBuild[]): Promise<readonly ScheduledBuildResult[]> {@@ -72,9 +64,12 @@ export class LocalBuildScheduler {if (request.id.trim().length === 0) throw new Error("scheduled build id must not be empty");if (ids.has(request.id)) throw new Error(`scheduled build ${request.id} is duplicated`);ids.add(request.id);- let state = structuredClone(request.state);+ let state: BuildState;let machine: BuildMachine | undefined;- if (this.#buildStore !== undefined) {+ if (this.#buildStore === undefined) {+ if (request.state === undefined) throw new Error(`scheduled Build ${request.id} has no in-memory state`);+ state = structuredClone(request.state);+ } else {const snapshot = request.snapshot;if (snapshot === undefined) {throw new Error(`scheduled Build ${request.id} has no durable snapshot`);@@ -99,8 +94,6 @@ export class LocalBuildScheduler {let cursor = 0;const resourceLimit = (resource: RuntimeRunnableCommand["resources"][number]): number => {- const override = this.#resourceLimits[resource.id];- if (override !== undefined) return override;const proposed = positiveInteger(resource.maxActive, `resource ${resource.id} default`);const previous = resourceDefaults.get(resource.id);if (previous !== undefined && previous !== proposed) {@@ -164,9 +157,8 @@ export class LocalBuildScheduler {while (true) {const ready = await preparations();const counts = resourceCounts();- let slots = this.#maxConcurrency - active.size;- while (slots > 0 && builds.length > 0) {+ while (builds.length > 0) {let selected = false;for (let offset = 0; offset < builds.length; offset += 1) {const index = (cursor + offset) % builds.length;@@ -182,7 +174,6 @@ export class LocalBuildScheduler {counts.set(resource.id, (counts.get(resource.id) ?? 0) + 1);}cursor = (index + 1) % builds.length;- slots -= 1;selected = true;break;}diff --git a/packages/runtime/src/types.ts b/packages/runtime/src/types.tsindex bedc9fae..779a5a47 100644--- a/packages/runtime/src/types.ts+++ b/packages/runtime/src/types.ts@@ -87,22 +87,11 @@ export type StreamingArtifactStore = ArtifactStore & {open(digest: Digest): Promise<AsyncIterable<Uint8Array> | undefined>;};-/** Optional retention capability used only by explicit deployment maintenance. */-export type ManagedArtifactStore = ArtifactStore & {- list(): Promise<readonly Digest[]>;- delete(digest: Digest): Promise<boolean>;-};-export function isStreamingArtifactStore(value: ArtifactStore): value is StreamingArtifactStore {return "putStream" in value && typeof value.putStream === "function"&& "open" in value && typeof value.open === "function";}-export function isManagedArtifactStore(value: ArtifactStore): value is ManagedArtifactStore {- return "list" in value && typeof value.list === "function"- && "delete" in value && typeof value.delete === "function";-}-export type BuildSnapshot = {readonly build: string;readonly definition: BuildDefinition;@@ -118,21 +107,9 @@ export type BuildStore = {append(build: string, fact: BuildFact): Promise<void>;};-/** Optional maintenance index; execution still depends only on BuildStore create/read/append. */-export type EnumerableBuildStore = BuildStore & {- list(): Promise<readonly BuildSnapshot[]>;-};--export function isEnumerableBuildStore(value: BuildStore): value is EnumerableBuildStore {- return "list" in value && typeof value.list === "function";-}--export type ScheduledBuild = {- readonly id: string;- readonly state: BuildState;- /** Optional Store read already performed by the Worker; avoids reconstructing the same Build twice. */- readonly snapshot?: BuildSnapshot;-};+export type ScheduledBuild =+ | { readonly id: string; readonly state: BuildState; readonly snapshot?: never }+ | { readonly id: string; readonly snapshot: BuildSnapshot; readonly state?: never };export type SchedulerExecutionOutcome = {readonly command: string;@@ -153,8 +130,6 @@ export type ScheduledBuildResult = {};export type BuildSchedulerOptions = {- readonly maxConcurrency?: number;- readonly resourceLimits?: Readonly<Record<string, number>>;/** Optional durable authority. When present, every admitted Core Fact is appended. */readonly buildStore?: BuildStore;};diff --git a/packages/store-sqlite/src/store.ts b/packages/store-sqlite/src/store.tsindex 3e9c05a5..ab4d57ff 100644--- a/packages/store-sqlite/src/store.ts+++ b/packages/store-sqlite/src/store.ts@@ -2,17 +2,11 @@ import { existsSync, mkdirSync } from "node:fs";import { dirname, resolve } from "node:path";import { DatabaseSync } from "node:sqlite";-import {- canonicalStringify,- materializeBuild,-} from "@narratage/core";+import { materializeBuild } from "@narratage/core";import type {BuildDefinition,BuildFact,} from "@narratage/protocol";-import {- capacityReservationId,-} from "@narratage/runtime";import type {BuildDispatchRequest,BuildDispatchRelease,@@ -34,8 +28,6 @@ import type {OperationUpdate,} from "@narratage/runtime";-const databaseSchemaVersion = 13;-export type SqliteRuntimeStateOptions = {readonly busyTimeoutMs?: number;/** Open an existing archive without creating files or schema. */@@ -98,13 +90,12 @@ function parseOperationSnapshot(row: Row): OperationSnapshot {assert(typeof row[field] === "string", `SQLite Operation row has no ${field}`);}assert(typeof row.status === "string", "SQLite Operation row has no status");- const pending = row.status === "pending"- ? JSON.parse(String(row.handle_json)) as {+ const payload = typeof row.payload_json === "string" ? JSON.parse(row.payload_json) as unknown : undefined;+ const pending = row.status === "pending" ? payload as {readonly handle: unknown;readonly wakeAt?: number;readonly progress?: OperationSnapshot["progress"];- }- : undefined;+ } : undefined;const mutable = row.status === "pending"? {handle: pending!.handle,@@ -112,9 +103,9 @@ function parseOperationSnapshot(row: Row): OperationSnapshot {...(pending!.progress === undefined ? {} : { progress: pending!.progress }),}: row.status === "completed"- ? { completion: JSON.parse(String(row.completion_json)) }+ ? { completion: payload }: row.status === "failed"- ? { failure: JSON.parse(String(row.failure_json)) }+ ? { failure: payload }: {};return {id: row.operation_id,@@ -153,7 +144,7 @@ class SqliteBuildStore implements BuildStore {const result = this.#database.prepare(`INSERT OR IGNORE INTO narratage_builds (build_id, definition_json)VALUES (?, ?)- `).run(build, canonicalStringify(definition));+ `).run(build, JSON.stringify(definition));if (result.changes !== 1) throw new Error(`build ${build} already exists`);const storedDefinition = copy(definition);const facts: readonly BuildFact[] = [];@@ -170,21 +161,12 @@ class SqliteBuildStore implements BuildStore {return row === undefined ? undefined : parseBuildSnapshot(row, this.#facts(build));}- async list(): Promise<readonly BuildSnapshot[]> {- const rows = this.#database.prepare(`- SELECT build_id, definition_json- FROM narratage_builds- ORDER BY build_id ASC- `).all() as Row[];- return rows.map((row) => parseBuildSnapshot(row, this.#facts(String(row.build_id))));- }-async append(build: string, fact: BuildFact): Promise<void> {const result = this.#database.prepare(`INSERT INTO narratage_build_facts (build_id, command_id, fact_json) VALUES (?, ?, ?)- `).run(build, fact.command, canonicalStringify(fact));+ `).run(build, fact.command, JSON.stringify(fact));if (result.changes !== 1) throw new Error(`Build ${build} Fact was not stored`);}}@@ -215,7 +197,7 @@ class SqliteBuildCatalog implements BuildCatalog {INSERT INTO narratage_build_catalog (build_id, created_at, descriptor_json) VALUES (?, ?, ?)- `).run(build, now, canonicalStringify(descriptor));+ `).run(build, now, JSON.stringify(descriptor));return { ...copy(descriptor), build, createdAt: now };}@@ -247,20 +229,22 @@ class SqliteOperationStore implements OperationStore {async create(operation: OperationSnapshot): Promise<OperationSnapshot> {const identity = operationIdentityFrom(operation);- const handle = operation.status === "pending" ? canonicalStringify({- handle: operation.handle,- ...(operation.wakeAt === undefined ? {} : { wakeAt: operation.wakeAt }),- ...(operation.progress === undefined ? {} : { progress: operation.progress }),- }) : null;- const completion = operation.status === "completed" ? canonicalStringify(operation.completion) : null;- const failure = operation.status === "failed" ? canonicalStringify(operation.failure) : null;+ const payload = operation.status === "pending"+ ? JSON.stringify({+ handle: operation.handle,+ ...(operation.wakeAt === undefined ? {} : { wakeAt: operation.wakeAt }),+ ...(operation.progress === undefined ? {} : { progress: operation.progress }),+ })+ : operation.status === "completed"+ ? JSON.stringify(operation.completion)+ : operation.status === "failed" ? JSON.stringify(operation.failure) : null;const result = this.#database.prepare(`INSERT INTO narratage_operations (operation_id, build_id, command_id, endpoint_id, pool_id, lane_id, status,- handle_json, completion_json, failure_json- ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)+ payload_json+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`).run(identity.id, identity.build, identity.command, identity.endpoint, identity.pool, identity.lane,- operation.status, handle, completion, failure);+ operation.status, payload);if (result.changes !== 1) throw new Error(`Operation ${identity.id} was not created`);return copy(operation);}@@ -268,7 +252,7 @@ class SqliteOperationStore implements OperationStore {async read(id: string): Promise<OperationSnapshot | undefined> {const row = this.#database.prepare(`SELECT operation_id, build_id, command_id, endpoint_id, pool_id, lane_id,- status, handle_json, completion_json, failure_json+ status, payload_jsonFROM narratage_operationsWHERE operation_id = ?`).get(id) as Row | undefined;@@ -289,7 +273,7 @@ class SqliteOperationStore implements OperationStore {}const rows = this.#database.prepare(`SELECT operation_id, build_id, command_id, endpoint_id, pool_id, lane_id,- status, handle_json, completion_json, failure_json+ status, payload_jsonFROM narratage_operations${predicates.length === 0 ? "" : `WHERE ${predicates.join(" AND ")}`}ORDER BY operation_id ASC@@ -302,20 +286,20 @@ class SqliteOperationStore implements OperationStore {const current = await this.read(id);if (current === undefined) throw new Error(`Operation ${id} does not exist`);if (current.status === "completed" || current.status === "failed" || current.status === "cancelled") return current;- const handle = update.status === "pending" ? canonicalStringify({- handle: update.handle,- ...(update.wakeAt === undefined ? {} : { wakeAt: update.wakeAt }),- ...(update.progress === undefined ? {} : { progress: update.progress }),- }) : null;- const completion = update.status === "completed"- ? canonicalStringify(update.completion)- : null;- const failure = update.status === "failed" ? canonicalStringify(update.failure) : null;+ const payload = update.status === "pending"+ ? JSON.stringify({+ handle: update.handle,+ ...(update.wakeAt === undefined ? {} : { wakeAt: update.wakeAt }),+ ...(update.progress === undefined ? {} : { progress: update.progress }),+ })+ : update.status === "completed"+ ? JSON.stringify(update.completion)+ : update.status === "failed" ? JSON.stringify(update.failure) : null;const result = this.#database.prepare(`UPDATE narratage_operations- SET status = ?, handle_json = ?, completion_json = ?, failure_json = ?+ SET status = ?, payload_json = ?WHERE operation_id = ?- `).run(update.status, handle, completion, failure, id);+ `).run(update.status, payload, id);if (result.changes !== 1) throw new Error(`Operation ${id} disappeared during update`);const mutable = update.status === "pending"? {@@ -344,7 +328,7 @@ function parseDispatchSnapshot(row: Row): BuildDispatchSnapshot {const cancellation = row.cancel_requested === 1? {cancellation: {- ...(typeof row.cancel_reason === "string" ? { reason: row.cancel_reason } : {}),+ ...(typeof row.reason === "string" ? { reason: row.reason } : {}),},}: {};@@ -377,9 +361,9 @@ class SqliteBuildDispatchStore implements BuildDispatchStore {this.#database.prepare(`INSERT INTO narratage_dispatches (build_id, component_packages_json, created_at, available_at,- phase, reason, cancel_requested, cancel_reason, terminal- ) VALUES (?, ?, ?, ?, 'queued', NULL, 0, NULL, NULL)- `).run(request.build, canonicalStringify(request.componentPackages), now, now);+ phase, reason, cancel_requested, terminal+ ) VALUES (?, ?, ?, ?, 'queued', NULL, 0, NULL)+ `).run(request.build, JSON.stringify(request.componentPackages), now, now);return {...copy(request),createdAt: now,@@ -474,22 +458,22 @@ class SqliteBuildDispatchStore implements BuildDispatchStore {if (row === undefined) throw new Error(`Dispatch ${build} does not exist`);const current = parseDispatchSnapshot(row);if (current.phase === "terminal") return current;+ if (current.cancellation !== undefined) return current;if (current.phase === "queued") {this.#database.prepare(`UPDATE narratage_dispatchesSET phase = 'terminal', terminal = 'cancelled',- reason = ?, cancel_requested = 1, cancel_reason = ?+ reason = ?, cancel_requested = 1WHERE build_id = ?- `).run(reason ?? "cancelled before execution", reason ?? null, build);+ `).run(reason ?? "cancelled before execution", build);} else {this.#database.prepare(`UPDATE narratage_dispatchesSET phase = CASE WHEN phase = 'running' THEN phase ELSE 'queued' END,available_at = CASE WHEN phase = 'running' THEN available_at ELSE ? END,- reason = ?, cancel_requested = 1,- cancel_reason = COALESCE(cancel_reason, ?)+ reason = ?, cancel_requested = 1WHERE build_id = ?- `).run(now, reason ?? "cancellation requested", reason ?? null, build);+ `).run(now, reason ?? "cancellation requested", build);}return parseDispatchSnapshot(this.#database.prepare("SELECT * FROM narratage_dispatches WHERE build_id = ?").get(build) as Row,@@ -502,7 +486,6 @@ class SqliteBuildDispatchStore implements BuildDispatchStore {assert(request.resources.length > 0, "Capacity resources are empty");const resources = [...request.resources].sort((left, right) => left.id.localeCompare(right.id));const reservation: CapacityReservation = {- id: capacityReservationId(request.build, request.command),build: request.build,command: request.command,resources,@@ -511,14 +494,14 @@ class SqliteBuildDispatchStore implements BuildDispatchStore {};return transaction(this.#database, () => {const existing = this.#database.prepare(- "SELECT * FROM narratage_capacity WHERE reservation_id = ?",- ).get(reservation.id) as Row | undefined;+ "SELECT * FROM narratage_capacity WHERE build_id = ? AND command_id = ?",+ ).get(reservation.build, reservation.command) as Row | undefined;if (existing !== undefined) {return { status: "acquired", reservation: parseCapacityReservation(existing) };}for (const resource of resources) {const count = this.#database.prepare(`- SELECT COUNT(DISTINCT capacity.reservation_id) AS count+ SELECT COUNT(DISTINCT capacity.rowid) AS countFROM narratage_capacity AS capacity, json_each(capacity.resources_json) AS claimWHERE json_extract(claim.value, '$.id') = ?`).get(resource.id) as Row;@@ -534,22 +517,23 @@ class SqliteBuildDispatchStore implements BuildDispatchStore {}this.#database.prepare(`INSERT INTO narratage_capacity (- reservation_id, build_id, command_id, resources_json, queue_json, created_at- ) VALUES (?, ?, ?, ?, ?, ?)+ build_id, command_id, resources_json, queue_json, created_at+ ) VALUES (?, ?, ?, ?, ?)`).run(- reservation.id,reservation.build,reservation.command,- canonicalStringify(reservation.resources),- reservation.queue === undefined ? null : canonicalStringify(reservation.queue),+ JSON.stringify(reservation.resources),+ reservation.queue === undefined ? null : JSON.stringify(reservation.queue),reservation.createdAt,);return { status: "acquired", reservation };});}- async releaseCapacity(id: string): Promise<void> {- this.#database.prepare("DELETE FROM narratage_capacity WHERE reservation_id = ?").run(id);+ async releaseCapacity(build: string, command: string): Promise<void> {+ this.#database.prepare(+ "DELETE FROM narratage_capacity WHERE build_id = ? AND command_id = ?",+ ).run(build, command);}async releaseBuildCapacity(build: string): Promise<void> {@@ -558,17 +542,16 @@ class SqliteBuildDispatchStore implements BuildDispatchStore {async listCapacity(): Promise<readonly CapacityReservation[]> {return (this.#database.prepare(- "SELECT * FROM narratage_capacity ORDER BY created_at ASC, reservation_id ASC",+ "SELECT * FROM narratage_capacity ORDER BY created_at ASC, build_id ASC, command_id ASC",).all() as Row[]).map(parseCapacityReservation);}}function parseCapacityReservation(row: Row): CapacityReservation {- assert(typeof row.reservation_id === "string" && typeof row.build_id === "string"- && typeof row.command_id === "string" && typeof row.resources_json === "string"+ assert(typeof row.build_id === "string" && typeof row.command_id === "string"+ && typeof row.resources_json === "string"&& typeof row.created_at === "number", "SQLite Capacity row is invalid");const value = {- id: row.reservation_id,build: row.build_id,command: row.command_id,resources: JSON.parse(row.resources_json) as CapacityReservation["resources"],@@ -605,29 +588,7 @@ export class SqliteRuntimeState {this.#database.exec("PRAGMA journal_mode = WAL");this.#database.exec("PRAGMA synchronous = NORMAL");}- const alreadyInitialized = this.#database.prepare(`- SELECT 1 AS present FROM sqlite_master- WHERE type = 'table' AND name = 'narratage_store_meta'- `).get() as Row | undefined;- if (alreadyInitialized !== undefined) {- const version = this.#database.prepare(- "SELECT schema_version FROM narratage_store_meta WHERE singleton = 1",- ).get() as Row | undefined;- if (version === undefined || version.schema_version !== databaseSchemaVersion) {- const found = version === undefined ? "an incomplete schema" : `schema ${String(version.schema_version)}`;- this.#database.close();- throw new Error(- `@narratage/store-sqlite found ${found} at ${absolute}; expected schema ${databaseSchemaVersion}. `- + "Pre-release Runtime state is not migrated automatically. Archive that database (including its -wal and -shm files) "- + "or select a new SQLite path in the Runtime Profile; Artifact files are separate and are not deleted.",- );- }- }if (!options.readOnly || emptyReadOnly) this.#database.exec(`- CREATE TABLE IF NOT EXISTS narratage_store_meta (- singleton INTEGER PRIMARY KEY CHECK (singleton = 1),- schema_version INTEGER NOT NULL- ) STRICT;CREATE TABLE IF NOT EXISTS narratage_builds (build_id TEXT PRIMARY KEY,definition_json TEXT NOT NULL@@ -647,9 +608,7 @@ export class SqliteRuntimeState {pool_id TEXT NOT NULL,lane_id TEXT NOT NULL,status TEXT NOT NULL CHECK (status IN ('pending', 'completed', 'failed', 'cancelled')),- handle_json TEXT,- completion_json TEXT,- failure_json TEXT+ payload_json TEXT) STRICT;CREATE TABLE IF NOT EXISTS narratage_build_catalog (build_id TEXT PRIMARY KEY,@@ -664,24 +623,19 @@ export class SqliteRuntimeState {phase TEXT NOT NULL CHECK (phase IN ('queued', 'running', 'waiting', 'terminal')),reason TEXT,cancel_requested INTEGER NOT NULL DEFAULT 0 CHECK (cancel_requested IN (0, 1)),- cancel_reason TEXT,terminal TEXT CHECK (terminal IS NULL OR terminal IN ('complete', 'failed', 'cancelled'))) STRICT;CREATE INDEX IF NOT EXISTS narratage_dispatch_readyON narratage_dispatches (phase, available_at);CREATE TABLE IF NOT EXISTS narratage_capacity (- reservation_id TEXT PRIMARY KEY,build_id TEXT NOT NULL,command_id TEXT NOT NULL,resources_json TEXT NOT NULL,queue_json TEXT,created_at INTEGER NOT NULL,- UNIQUE (build_id, command_id)+ PRIMARY KEY (build_id, command_id)) STRICT;`);- if (alreadyInitialized === undefined) {- this.#database.prepare("INSERT INTO narratage_store_meta (singleton, schema_version) VALUES (1, ?)").run(databaseSchemaVersion);- }this.builds = new SqliteBuildStore(database);this.operations = new SqliteOperationStore(database);this.dispatch = new SqliteBuildDispatchStore(database);diff --git a/packages/store-sqlite/test/store.test.ts b/packages/store-sqlite/test/store.test.tsindex 929e6252..3e8deec0 100644--- a/packages/store-sqlite/test/store.test.ts+++ b/packages/store-sqlite/test/store.test.ts@@ -169,7 +169,9 @@ test("Pool and Lane limits count only asynchronous Operations still in flight",});assert.equal(otherRoute.status, "acquired");- if (first.status === "acquired") await state.dispatch.releaseCapacity(first.reservation.id);+ if (first.status === "acquired") {+ await state.dispatch.releaseCapacity(first.reservation.build, first.reservation.command);+ }const retried = await state.dispatch.acquireCapacity({build: "seedance-b",command: "generate:b",diff --git a/packages/validation/src/index.ts b/packages/validation/src/index.tsindex 8bcb138f..10d02a73 100644--- a/packages/validation/src/index.ts+++ b/packages/validation/src/index.ts@@ -1,6 +1,5 @@import {resolveType,- sealRecord,verifyRecordStructure,} from "@narratage/core";import type {@@ -87,10 +86,9 @@ export async function admitRecord(record: TypedRecord,registry: TypeValidatorRegistryLike,): Promise<TypedRecord> {- const admitted = sealRecord(record);- verifyRecordStructure(closure, admitted);+ verifyRecordStructure(closure, record);await refineValue(record.type, record.value, registry);- return admitted;+ return record;}export function createRecordAdmitter(