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: remove defensive execution scaffolding
kashorin7683e38c0e1b
diff --git a/.agents/skills/narratage/references/runtime.md b/.agents/skills/narratage/references/runtime.mdindex b825b489..baaf7d87 100644--- a/.agents/skills/narratage/references/runtime.md+++ b/.agents/skills/narratage/references/runtime.md@@ -22,7 +22,7 @@ Install dependencies after package selection changes. Use `check` during authorior `runtime status` when diagnosing the deployment. `build` already ensures the detached Workerand demanded Managed Programs are available.-Use `runtime logs` to diagnose the Worker. Use `runtime down` to stop it from claiming more leases.+Use `runtime logs` to diagnose the Worker. Use `runtime down` to stop it from claiming more Builds.External programs are intentionally independent; stop them only with `programs down`. DurableBuilds remain archived and neither command cancels remote Provider work.diff --git a/docs/guide/runtime.md b/docs/guide/runtime.mdindex f78bd7dd..ec439533 100644--- a/docs/guide/runtime.md+++ b/docs/guide/runtime.md@@ -25,7 +25,7 @@ video.A Runtime receives one already compiled Build and owns everything environmental:* durable Build and Operation state;-* dispatch, leases and concurrency;+* dispatch and concurrency;* Artifact and Credential storage;* Endpoint activation and Provider calls;* optional long lived programs;diff --git a/packages/artifact-store-fs/src/store.ts b/packages/artifact-store-fs/src/store.tsindex f9209c13..e0575e23 100644--- a/packages/artifact-store-fs/src/store.ts+++ b/packages/artifact-store-fs/src/store.ts@@ -9,7 +9,7 @@ import {} from "node:fs/promises";import { dirname, join, resolve } from "node:path";-import { digestOf, isDigest } from "@narratage/protocol";+import { isDigest } from "@narratage/protocol";import type { BlobRef, Digest } from "@narratage/protocol";import { defineRuntimeInfrastructurePackage } from "@narratage/runtime";import type { ArtifactStore, RuntimeInfrastructurePackage } from "@narratage/runtime";@@ -121,7 +121,6 @@ export class FileArtifactStore implements ArtifactStore {}return (async function* () {const handle = await openFile(path, "r");- const hash = createHash("sha256");try {const buffer = new Uint8Array(1024 * 1024);let position = 0;@@ -130,14 +129,11 @@ export class FileArtifactStore implements ArtifactStore {if (result.bytesRead === 0) break;position += result.bytesRead;const chunk = buffer.slice(0, result.bytesRead);- hash.update(chunk);yield chunk;}} finally {await handle.close();}- const actual = `sha256:${hash.digest("hex")}`;- if (actual !== digest) throw new Error(`Artifact ${digest} content digest differs`);})();}diff --git a/packages/artifact-store-fs/test/store.test.ts b/packages/artifact-store-fs/test/store.test.tsindex 7dfec59f..8be4427c 100644--- a/packages/artifact-store-fs/test/store.test.ts+++ b/packages/artifact-store-fs/test/store.test.ts@@ -1,5 +1,5 @@import assert from "node:assert/strict";-import { mkdtemp, rm, writeFile } from "node:fs/promises";+import { mkdtemp, rm } from "node:fs/promises";import { tmpdir } from "node:os";import { join } from "node:path";import test from "node:test";@@ -20,10 +20,6 @@ test("filesystem artifacts are content-addressed and survive adapter restart", aassert.equal(await reopened.has(left.digest), true);assert.deepEqual(await reopened.get(left.digest), bytes);- const hex = left.digest.slice("sha256:".length);- await writeFile(join(directory, "sha256", hex.slice(0, 2), hex), "tampered");- assert.equal(await reopened.has(left.digest), true, "presence is not a second full integrity read");- await assert.rejects(reopened.get(left.digest), /content digest differs/u);} finally {await rm(directory, { recursive: true, force: true });}diff --git a/packages/artifact-store-s3/README.md b/packages/artifact-store-s3/README.mdindex f01dc617..bc84c3ab 100644--- a/packages/artifact-store-s3/README.md+++ b/packages/artifact-store-s3/README.md@@ -8,11 +8,10 @@ Objects use deterministic keys:<prefix>/sha256/<first-two-hex>/<full-hex>```-Writes use `If-None-Match: *`; an already-existing object is downloaded and digest-verified rather-than trusted by key. Whole-object reads always recompute SHA-256. The AWS client also exposes the-optional streaming and retention facets: streamed writes use multipart upload to an isolated-staging key followed by a server-side copy to the content-addressed key; streamed reads verify the-digest before their iterator completes; `list`/`delete` support explicit reachability GC.+Writes compute the content digest once and store it at the corresponding key. The AWS client also+exposes the optional streaming and retention facets: streamed writes use multipart upload to an isolated+staging key followed by a server-side copy to the content-addressed key; `list`/`delete` support+explicit maintenance.Those optional facets are attached only when an injected client supplies every operation needed toimplement them. A reduced test or MinIO client therefore advertises only the capabilities it candiff --git a/packages/artifact-store-s3/src/store.ts b/packages/artifact-store-s3/src/store.tsindex 30dc47cb..5ec93d5a 100644--- a/packages/artifact-store-s3/src/store.ts+++ b/packages/artifact-store-s3/src/store.ts@@ -1,6 +1,6 @@import { createHash, randomUUID } from "node:crypto";-import { digestOf, isDigest } from "@narratage/protocol";+import { isDigest } from "@narratage/protocol";import type { BlobRef, Digest } from "@narratage/protocol";import { defineRuntimeInfrastructurePackage } from "@narratage/runtime";import type { ArtifactStore, RuntimeInfrastructurePackage } from "@narratage/runtime";@@ -37,7 +37,6 @@ type S3Location = {export type S3ArtifactStoreOptions = S3Location & {readonly client: S3ObjectClient;- readonly maxConflictRetries?: number;/** Bytes buffered before a part is sent. S3 requires at least 5 MiB per non-final part. */readonly partSizeBytes?: number;};@@ -70,13 +69,6 @@ function positiveInteger(value: number, subject: string): number {return value;}-function statusCode(error: unknown): number | undefined {- if (typeof error !== "object" || error === null || !("$metadata" in error)) return undefined;- const metadata = error.$metadata;- if (typeof metadata !== "object" || metadata === null || !("httpStatusCode" in metadata)) return undefined;- return typeof metadata.httpStatusCode === "number" ? metadata.httpStatusCode : undefined;-}-/*** Content-addressed S3 ArtifactStore.*@@ -90,7 +82,6 @@ export class S3ArtifactStore implements ArtifactStore {readonly #bucket: string;readonly #prefix: string;readonly #expectedBucketOwner: string | undefined;- readonly #maxConflictRetries: number;readonly #partSizeBytes: number;/** Present only when the client can stream and upload in parts. */readonly open?: (digest: Digest) => Promise<AsyncIterable<Uint8Array> | undefined>;@@ -108,7 +99,6 @@ export class S3ArtifactStore implements ArtifactStore {this.#bucket = options.bucket;this.#prefix = normalizePrefix(options.prefix);this.#expectedBucketOwner = options.expectedBucketOwner;- this.#maxConflictRetries = positiveInteger(options.maxConflictRetries ?? 3, "maxConflictRetries");this.#partSizeBytes = positiveInteger(options.partSizeBytes ?? 16 * 1024 * 1024, "partSizeBytes");assert(this.#partSizeBytes >= 5 * 1024 * 1024, "S3 requires multipart parts of at least 5 MiB");const client = options.client;@@ -140,7 +130,6 @@ export class S3ArtifactStore implements ArtifactStore {ContentLength: copy.byteLength,ContentType: mediaType,ChecksumSHA256: hash.digest("base64"),- IfNoneMatch: "*",Metadata: {"narratage-digest": digest,"narratage-size": String(copy.byteLength),@@ -149,37 +138,8 @@ export class S3ArtifactStore implements ArtifactStore {? {}: { ExpectedBucketOwner: this.#expectedBucketOwner }),} as const;- for (let attempt = 1; attempt <= this.#maxConflictRetries; attempt += 1) {- try {- await this.#client.put(input);- return { kind: "blob", digest, size: copy.byteLength, mediaType };- } catch (error) {- const status = statusCode(error);- if (status === 409 && attempt < this.#maxConflictRetries) continue;- if (status !== 412) throw error;- const existing = this.#client.head === undefined- ? await this.#client.get({- Bucket: this.#bucket,- Key: this.key(digest),- ...(this.#expectedBucketOwner === undefined- ? {}- : { ExpectedBucketOwner: this.#expectedBucketOwner }),- }).then((value) => value === undefined ? undefined : { size: value.byteLength })- : await this.#client.head({- Bucket: this.#bucket,- Key: this.key(digest),- ...(this.#expectedBucketOwner === undefined- ? {}- : { ExpectedBucketOwner: this.#expectedBucketOwner }),- });- if (existing === undefined) throw new Error(`S3 reported existing Artifact ${digest}, but it is absent`);- if (existing.size !== copy.byteLength) {- throw new Error(`S3 Artifact ${digest} size differs from the bytes being stored`);- }- return { kind: "blob", digest, size: copy.byteLength, mediaType };- }- }- throw new Error(`S3 Artifact ${digest} exceeded conditional-write retries`);+ await this.#client.put(input);+ return { kind: "blob", digest, size: copy.byteLength, mediaType };}async get(digest: Digest): Promise<Uint8Array | undefined> {@@ -191,10 +151,7 @@ export class S3ArtifactStore implements ArtifactStore {: { ExpectedBucketOwner: this.#expectedBucketOwner }),});if (bytes === undefined) return undefined;- const copy = Uint8Array.from(bytes);- const actual = `sha256:${createHash("sha256").update(copy).digest("hex")}`;- if (actual !== digest) throw new Error(`Artifact ${digest} content digest differs`);- return copy;+ return Uint8Array.from(bytes);}async has(digest: Digest): Promise<boolean> {@@ -214,14 +171,6 @@ export class S3ArtifactStore implements ArtifactStore {})) !== undefined;}- /**- * Stream the object, hashing as it passes. A whole-object read can refuse- * before returning anything; a stream cannot, because its first bytes are- * already with the caller by the time the last ones arrive. So verification- * lands at the end: the iterator throws instead of completing, and a consumer- * that treats an incomplete stream as an error — which every consumer must —- * still never accepts wrong bytes as an Artifact.- */async #openStream(digest: Digest): Promise<AsyncIterable<Uint8Array> | undefined> {const chunks = await this.#client.open!({Bucket: this.#bucket,@@ -231,15 +180,7 @@ export class S3ArtifactStore implements ArtifactStore {: { ExpectedBucketOwner: this.#expectedBucketOwner }),});if (chunks === undefined) return undefined;- return (async function* () {- const hash = createHash("sha256");- for await (const chunk of chunks) {- hash.update(chunk);- yield chunk;- }- const actual = `sha256:${hash.digest("hex")}`;- if (actual !== digest) throw new Error(`Artifact ${digest} content digest differs`);- })();+ return chunks;}/**@@ -323,13 +264,8 @@ export class S3ArtifactStore implements ArtifactStore {ContentType: mediaType,MetadataDirective: "REPLACE",Metadata: { "narratage-digest": digest, "narratage-size": String(size) },- IfNoneMatch: "*",...owner,});- } catch (error) {- // 412 means another Build stored these exact bytes first. Content- // addressing makes that agreement, not conflict.- if (statusCode(error) !== 412) throw error;} finally {await client.delete?.({ Bucket: this.#bucket, Key: staging, ...owner }).catch(() => undefined);}diff --git a/packages/artifact-store-s3/test/store.test.ts b/packages/artifact-store-s3/test/store.test.tsindex 677754a1..240c823f 100644--- a/packages/artifact-store-s3/test/store.test.ts+++ b/packages/artifact-store-s3/test/store.test.ts@@ -10,18 +10,10 @@ import { isManagedArtifactStore, isStreamingArtifactStore } from "@narratage/runclass FakeS3 implements S3ObjectClient {readonly values = new Map<string, Uint8Array>();- conflicts = 0;gets = 0;async put(input: Parameters<S3ObjectClient["put"]>[0]): Promise<void> {const key = input.Key!;- if (this.conflicts > 0) {- this.conflicts -= 1;- throw { $metadata: { httpStatusCode: 409 } };- }- if (input.IfNoneMatch === "*" && this.values.has(key)) {- throw { $metadata: { httpStatusCode: 412 } };- }assert.ok(input.Body instanceof Uint8Array);this.values.set(key, Uint8Array.from(input.Body));}@@ -33,19 +25,15 @@ class FakeS3 implements S3ObjectClient {}}-test("S3 artifacts use conditional immutable writes and verify downloaded content", async () => {+test("S3 artifacts use deterministic content-addressed keys", async () => {const client = new FakeS3();const store = new S3ArtifactStore({ client, bucket: "fixture", prefix: "projects/acme" });const bytes = new TextEncoder().encode("one immutable remote artifact");- client.conflicts = 1;const first = await store.put(bytes, "video/mp4");const second = await store.put(bytes, "video/mp4");assert.equal(first.digest, second.digest);assert.match(store.key(first.digest), /^projects\/acme\/sha256\//u);assert.deepEqual(await store.get(first.digest), bytes);-- client.values.set(store.key(first.digest), new TextEncoder().encode("tampered"));- await assert.rejects(store.get(first.digest), /content digest differs/u);});test("configured S3 service locks its location", () => {@@ -114,9 +102,6 @@ class FullFakeS3 extends FakeS3 {const source = input.CopySource!.slice(input.CopySource!.indexOf("/") + 1);const value = this.values.get(source);if (value === undefined) throw new Error(`copy source ${source} is absent`);- if (input.IfNoneMatch === "*" && this.values.has(input.Key!)) {- throw { $metadata: { httpStatusCode: 412 } };- }this.values.set(input.Key!, Uint8Array.from(value));}@@ -160,17 +145,17 @@ test("an empty Artifact is a legitimate one, and S3 will not accept a partless uassert.deepEqual(await store.get(ref.digest), new Uint8Array(0));});-test("two Builds streaming identical bytes agree instead of conflicting", async () => {+test("two Builds streaming identical bytes use the same destination", async () => {const client = new FullFakeS3();const store = new S3ArtifactStore({ client, bucket: "fixture" });const bytes = () => (async function* () { yield new TextEncoder().encode("same"); })();const first = await store.putStream!(bytes(), "text/plain");const second = await store.putStream!(bytes(), "text/plain");assert.equal(first.digest, second.digest);- assert.equal(client.copies, 2, "both attempted the copy; the second was refused as already present");+ assert.equal(client.copies, 2);});-test("a streamed read hands back bytes as they arrive, and refuses tampered content at the end", async () => {+test("a streamed read hands back bytes as they arrive", async () => {const client = new FullFakeS3();const store = new S3ArtifactStore({ client, bucket: "fixture" });const bytes = new TextEncoder().encode("streamed artifact bytes");@@ -179,14 +164,9 @@ test("a streamed read hands back bytes as they arrive, and refuses tampered contconst chunks: Uint8Array[] = [];for await (const chunk of (await store.open!(ref.digest))!) chunks.push(chunk);assert.equal(chunks.length, 2, "the stream was not assembled on the caller's behalf");-- client.values.set(store.key(ref.digest), new TextEncoder().encode("tampered artifact bytes"));- await assert.rejects(async () => {- for await (const chunk of (await store.open!(ref.digest))!) void chunk;- }, /content digest differs/u);});-test("presence uses object metadata while byte reads retain integrity verification", async () => {+test("presence uses object metadata without downloading bytes", async () => {const client = new FullFakeS3();const store = new S3ArtifactStore({ client, bucket: "fixture" });const ref = await store.put(new TextEncoder().encode("present"), "text/plain");diff --git a/packages/artifact/src/index.ts b/packages/artifact/src/index.tsindex 6138dc3d..e320bf8c 100644--- a/packages/artifact/src/index.ts+++ b/packages/artifact/src/index.ts@@ -1,4 +1,3 @@-import { digestOf } from "@narratage/protocol";import type {ModuleManifest,TypeRef,diff --git a/packages/audio-track/src/manifest.ts b/packages/audio-track/src/manifest.tsindex da36bb1b..a7307c02 100644--- a/packages/audio-track/src/manifest.ts+++ b/packages/audio-track/src/manifest.ts@@ -2,7 +2,7 @@ import { compositionDependency, compositionTypes } from "@narratage/composition"import { mediaDependency, mediaTypes } from "@narratage/media";import { narrativeDependency, narrativeTypes } from "@narratage/narrative";import { programSpaceDependency, programSpaceTypes } from "@narratage/program-space";-import { blobRefObjectSchema, digestOf } from "@narratage/protocol";+import { blobRefObjectSchema } from "@narratage/protocol";import type { ModuleManifest, ProducerRef, TypeRef, ValueSchema } from "@narratage/protocol";import { semanticMapDependency, semanticMapTypes } from "@narratage/semantic-map";import { temporalDependency } from "@narratage/temporal";@@ -80,9 +80,6 @@ export const audioTrackProgramSchema: ValueSchema = object({items: { schema: { kind: "array", minItems: 1, items: item } },});-const validator = (digest: ReturnType<typeof digestOf>) => ({- implementation: { digest },-});const baseInputs = [{ name: "set", type: audioTrackTypes.set }, { name: "header", type: audioTrackTypes.header },{ name: "space", type: programSpaceTypes.programSpace }, { name: "media", type: mediaTypes.synchronized },diff --git a/packages/audio-track/src/program.ts b/packages/audio-track/src/program.tsindex c9a97959..4324f0f1 100644--- a/packages/audio-track/src/program.ts+++ b/packages/audio-track/src/program.ts@@ -9,7 +9,7 @@ import {programSpaceSampleFrames,} from "@narratage/program-space";import type { ProgramSpace } from "@narratage/program-space";-import { canonicalize, digestOf, isDigest } from "@narratage/protocol";+import { canonicalize, isDigest } from "@narratage/protocol";import type { CompleteSemanticMap } from "@narratage/semantic-map";import {projectMomentWindows,diff --git a/packages/audio-track/test/audio-track.test.ts b/packages/audio-track/test/audio-track.test.tsindex b7bb82c0..f1828152 100644--- a/packages/audio-track/test/audio-track.test.ts+++ b/packages/audio-track/test/audio-track.test.ts@@ -1,5 +1,6 @@import assert from "node:assert/strict";import test from "node:test";+import { fixtureDigest } from "../../../test/fixture-digest.js";import {audioTrackManifest,@@ -29,7 +30,6 @@ import type { NarrativeMomentRef, NarrativeSelectionRef } from "@narratage/narraimport { narrativeManifest } from "@narratage/narrative";import { compileAudioProgramPlan } from "@narratage/media-pipeline";import { programSpaceDependency, programSpaceManifest, programSpaceTypes, sealProgramSpace } from "@narratage/program-space";-import { digestOf } from "@narratage/protocol";import type { ModuleManifest } from "@narratage/protocol";import type { CompleteSemanticMap } from "@narratage/semantic-map";import { semanticMapManifest } from "@narratage/semantic-map";@@ -55,7 +55,7 @@ function media(id: string, sampleFrames: number): SynchronizedMedia {frameCount: Math.max(1, Math.round(sampleFrames / 1_600)),},audio: {- artifact: { kind: "blob", digest: digestOf(`audio:${id}`), size: sampleFrames * 4, mediaType: "audio/wav" },+ artifact: { kind: "blob", digest: fixtureDigest(`audio:${id}`), size: sampleFrames * 4, mediaType: "audio/wav" },},};}@@ -231,7 +231,7 @@ test("dynamic Fragment keeps every material and temporal dependency as an explictest("the self-described Audio Surface parses into the same finite Producer graph", async () => {const fixtureModule = { name: "example.audio-inputs", version: "1" } as const;- const fixtureSurfaceDigest = digestOf("example.audio-inputs/surface@1");+ const fixtureSurfaceDigest = fixtureDigest("example.audio-inputs/surface@1");const fixtureSurface = {name: "inputs", tag: "Inputs", mode: "structured",outputs: [mediaTypes.synchronized, programSpaceTypes.programSpace],@@ -303,7 +303,6 @@ test("the self-described Audio Surface parses into the same finite Producer grapconst trackExport = resolveCompiledSourceExport(compiled, "sound.track", compositionTypes.audioTrack);assert.equal(trackExport.ref.kind, "logical-output");const build = start(compiled.program, compiled.graph, sealBuildRequest({- graph: compiled.graph.id,targets: [{ output: trackExport.ref.kind === "logical-output" ? trackExport.ref.id : "" }],}));assert.deepEqual(build.plan.steps.map((step) => step.producer.name).sort(), [diff --git a/packages/background-removal/src/manifest.ts b/packages/background-removal/src/manifest.tsindex 54b3f6fc..1aa7c13d 100644--- a/packages/background-removal/src/manifest.ts+++ b/packages/background-removal/src/manifest.ts@@ -1,5 +1,4 @@import { artifactDependency, artifactTypes } from "@narratage/artifact";-import { digestOf } from "@narratage/protocol";import type { CapabilityRef, ModuleManifest, ProducerRef } from "@narratage/protocol";export const backgroundRemovalModuleRef = { name: "@narratage/background-removal", version: "1" } as const;diff --git a/packages/background-removal/test/background-removal.test.ts b/packages/background-removal/test/background-removal.test.tsindex 298a4d65..770ca7e5 100644--- a/packages/background-removal/test/background-removal.test.ts+++ b/packages/background-removal/test/background-removal.test.ts@@ -1,17 +1,17 @@import assert from "node:assert/strict";import test from "node:test";+import { fixtureDigest } from "../../../test/fixture-digest.js";import { artifactTypes } from "@narratage/artifact";import {backgroundRemovalCapabilities, backgroundRemovalComponent, backgroundRemovalFragment, backgroundRemovalManifest,} from "@narratage/background-removal";-import { digestOf } from "@narratage/protocol";test("Background Removal is one image edge to one exact external Need", async () => {assert.deepEqual(backgroundRemovalFragment.inputs.map((input) => input.name), ["source"]);assert.deepEqual(backgroundRemovalFragment.exports.map((output) => output.name), ["image"]);assert.deepEqual(backgroundRemovalManifest.capabilities, [{ name: "remove-background", returns: artifactTypes.blob }]);- const source = { kind: "blob" as const, digest: digestOf("portrait"), size: 456, mediaType: "image/jpeg" };+ const source = { kind: "blob" as const, digest: fixtureDigest("portrait"), size: 456, mediaType: "image/jpeg" };const result = await backgroundRemovalComponent.producers[0]!.handler({ inputs: { source: { value: source } } } as never);assert.deepEqual(result.needs.image, { source });assert.deepEqual(backgroundRemovalCapabilities.remove.module.version, "1");diff --git a/packages/caption-fine/src/manifest.ts b/packages/caption-fine/src/manifest.tsindex 1eedcb83..0ca73580 100644--- a/packages/caption-fine/src/manifest.ts+++ b/packages/caption-fine/src/manifest.ts@@ -4,7 +4,6 @@ import { mediaDependency } from "@narratage/media";import { programSpaceDependency, programSpaceTypes } from "@narratage/program-space";import { narrativeDependency, narrativeTypes } from "@narratage/narrative";import { semanticMapDependency } from "@narratage/semantic-map";-import { digestOf } from "@narratage/protocol";import type { ModuleManifest, ProducerRef } from "@narratage/protocol";export const captionFineModuleRef = { name: "@narratage/caption-fine", version: "1" } as const;diff --git a/packages/caption-fine/src/render.ts b/packages/caption-fine/src/render.tsindex 4fe6a71a..1ec225cd 100644--- a/packages/caption-fine/src/render.ts+++ b/packages/caption-fine/src/render.ts@@ -13,7 +13,6 @@ import type {import type { CaptionDisplayAtom, CaptionDisplaySequence } from "@narratage/narrative";import { assertProgramSpaceIdentity, programSpaceFrameCount } from "@narratage/program-space";import type { ProgramSpace } from "@narratage/program-space";-import { digestOf } from "@narratage/protocol";import { assertFineCaptionParameters, FINE_CAPTION_FAMILY } from "./style.js";import type {diff --git a/packages/caption-fine/src/style.ts b/packages/caption-fine/src/style.tsindex 76082d4e..9344de18 100644--- a/packages/caption-fine/src/style.ts+++ b/packages/caption-fine/src/style.ts@@ -2,7 +2,7 @@ import type { CaptionStyleIntent } from "@narratage/caption";import { sealCaptionStyle } from "@narratage/caption";import { assertFontArtifactRef } from "@narratage/media";import type { FontArtifactRef } from "@narratage/media";-import { digestOf } from "@narratage/protocol";+import { canonicalStringify } from "@narratage/protocol";import type { SvsRecipe } from "@narratage/svs";import type { FineCaptionGlyphPaint, FineCaptionParameters } from "./types.js";@@ -293,7 +293,7 @@ export function assertFineCaptionParameters(value: FineCaptionParameters): voidconst faces = new Set<string>();for (const [index, font] of value.typography.exactFonts.entries()) {assertFontArtifactRef(font, `Fine Caption exact Font ${index + 1}`);- const identity = digestOf(font);+ const identity = canonicalStringify(font);if (faces.has(identity)) throw new Error("Fine Caption exact Font stack contains a duplicate face");faces.add(identity);}diff --git a/packages/caption-fine/test/caption-fine.test.ts b/packages/caption-fine/test/caption-fine.test.tsindex 5f6fc272..823b1b76 100644--- a/packages/caption-fine/test/caption-fine.test.ts+++ b/packages/caption-fine/test/caption-fine.test.ts@@ -1,12 +1,12 @@import assert from "node:assert/strict";import test from "node:test";+import { fixtureDigest } from "../../../test/fixture-digest.js";import { resolveCaptionProgram } from "@narratage/caption";import type { TimedCaptionProjection } from "@narratage/caption";import { fineCaptionParameters, fineCaptionStyle, renderFineCaption } from "@narratage/caption-fine";import type { FontArtifactRef } from "@narratage/media";import { sealProgramSpace } from "@narratage/program-space";-import { digestOf } from "@narratage/protocol";import { captionDisplaySequence, parseScript } from "@narratage/script";import type { SvsRecipe } from "@narratage/svs";@@ -31,7 +31,7 @@ const recipe: SvsRecipe = {};const exactFont: FontArtifactRef = {- sources: [{ artifact: { kind: "blob", digest: digestOf("caption-fine:test-font"), size: 1_024, mediaType: "font/woff2" } }],+ sources: [{ artifact: { kind: "blob", digest: fixtureDigest("caption-fine:test-font"), size: 1_024, mediaType: "font/woff2" } }],weight: 800,style: "normal",};@@ -361,7 +361,7 @@ test("an exact Font is explicit Style input and reaches every base and active gltest("an ordered exact Font stack preserves honest fallback faces and rejects duplicates", () => {const fallback: FontArtifactRef = {...exactFont,- sources: [{ artifact: { ...exactFont.sources[0]!.artifact, digest: digestOf("caption-fine:test-fallback") } }],+ sources: [{ artifact: { ...exactFont.sources[0]!.artifact, digest: fixtureDigest("caption-fine:test-fallback") } }],weight: 400,};const parameters = fineCaptionParameters(recipe, [exactFont, fallback]);diff --git a/packages/caption/src/manifest.ts b/packages/caption/src/manifest.tsindex 47b8ebd1..a02f1b8f 100644--- a/packages/caption/src/manifest.ts+++ b/packages/caption/src/manifest.ts@@ -1,6 +1,5 @@import { narrativeDependency, narrativeTypes } from "@narratage/narrative";import { semanticMapDependency, semanticMapTypes } from "@narratage/semantic-map";-import { digestOf } from "@narratage/protocol";import type { ModuleManifest, ProducerRef, TypeRef, ValueSchema } from "@narratage/protocol";export const captionModuleRef = { name: "@narratage/caption", version: "1" } as const;diff --git a/packages/caption/src/style.ts b/packages/caption/src/style.tsindex ae2802b3..c93e7a9a 100644--- a/packages/caption/src/style.ts+++ b/packages/caption/src/style.ts@@ -3,7 +3,7 @@ import type {CaptionDisplayWord,CaptionDisplayWordSubset,} from "@narratage/narrative";-import { canonicalize, digestOf } from "@narratage/protocol";+import { canonicalStringify, canonicalize } from "@narratage/protocol";import { assertCaptionDisplaySequence, assertCaptionDisplayWordSubset } from "./display.js";import type {@@ -180,7 +180,7 @@ export function resolveCaptionProgram(const styles = new Map<string, CaptionStyleIntent>([[defaultStyle.id, defaultStyle]]);applications.forEach((application) => {const previous = styles.get(application.style.id);- assert(previous === undefined || digestOf(previous) === digestOf(application.style),+ assert(previous === undefined || canonicalStringify(previous) === canonicalStringify(application.style),`Caption Style ${application.style.id} has conflicting definitions`);styles.set(application.style.id, application.style);});diff --git a/packages/caption/src/temporalize.ts b/packages/caption/src/temporalize.tsindex f02bc373..8205f40f 100644--- a/packages/caption/src/temporalize.ts+++ b/packages/caption/src/temporalize.ts@@ -10,7 +10,7 @@ import type { CaptionPlan, CaptionProgram, TimedCaptionProjection } from "./type/*** Apply Caption-owned post-planning visibility without changing Cue identity, timing or grouping.- * The operation is idempotent so Style-family renderers may enforce the contract defensively.+ * Style-family renderers may apply the operation directly.*/export function applyCaptionMute(projection: TimedCaptionProjection,diff --git a/packages/caption/src/types.ts b/packages/caption/src/types.tsindex 1c851e37..12215fcc 100644--- a/packages/caption/src/types.ts+++ b/packages/caption/src/types.ts@@ -41,7 +41,7 @@ export type CaptionPlannedRun = {readonly cues: readonly CaptionPlannedCue[];};-/** Planner-neutral result. Model/provider identity remains in the producing Graph and Receipt. */+/** Planner-neutral result. Model/provider choice remains in the producing Graph. */export type CaptionPlan = {readonly runs: readonly CaptionPlannedRun[];};diff --git a/packages/caption/test/caption.test.ts b/packages/caption/test/caption.test.tsindex 9d5521ef..a666ec53 100644--- a/packages/caption/test/caption.test.ts+++ b/packages/caption/test/caption.test.ts@@ -1,5 +1,6 @@import assert from "node:assert/strict";import test from "node:test";+import { fixtureDigest } from "../../../test/fixture-digest.js";import {assertCaptionPlanForProgram,@@ -13,7 +14,7 @@ import {import type { CaptionFieldDeclaration, CaptionStyleIntent } from "@narratage/caption";import { narrativeTypes } from "@narratage/narrative";import type { CaptionDisplayWordSubset, Narrative } from "@narratage/narrative";-import { canonicalize, digestOf, recordDigest } from "@narratage/protocol";+import { canonicalize } from "@narratage/protocol";import type { StoredValue, TypeRef } from "@narratage/protocol";import { sealProgramSpace } from "@narratage/program-space";import { sealSpeechBasis } from "@narratage/speech";@@ -43,7 +44,7 @@ function locate(narrative: Narrative, durationSec: number, segments: readonly TrdurationSec,frameRate: { numerator: 30, denominator: 1 },});- const audio = { kind: "blob" as const, digest: digestOf("caption:test-audio"), size: 1, mediaType: "audio/wav" };+ const audio = { kind: "blob" as const, digest: fixtureDigest("caption:test-audio"), size: 1, mediaType: "audio/wav" };const basisSegments = narrative.segments.map((segment, index) => ({segmentId: segment.id,startFrame: Math.round(durationSec * 30 * index / narrative.segments.length),@@ -206,8 +207,6 @@ test("caption:Program lowers explicit Mute word subsets without a temporal mask"id: path,type,value: stored,- digest: recordDigest(type, stored),- origin: { kind: "authored" },},};};diff --git a/packages/cli/src/archive.ts b/packages/cli/src/archive.tsindex b3fc77e4..e8934cd7 100644--- a/packages/cli/src/archive.ts+++ b/packages/cli/src/archive.ts@@ -1,4 +1,4 @@-import { createHash, randomUUID } from "node:crypto";+import { randomUUID } from "node:crypto";import { mkdir, open, rename, rm, writeFile } from "node:fs/promises";import { dirname } from "node:path";@@ -70,20 +70,16 @@ export async function materializeArtifact(if (source === undefined) throw new Error(`Artifact ${artifact.digest} is absent from the selected ArtifactStore`);const temporary = `${destination}.narratage-${randomUUID()}.part`;const output = await open(temporary, "wx");- const hash = createHash("sha256");let size = 0;try {for await (const chunk of source) {size += chunk.byteLength;- hash.update(chunk);await output.write(chunk);}await output.sync();await output.close();if (size !== artifact.size) throw new Error(`Artifact ${artifact.digest} size differs from ${subject}`);- const digest = `sha256:${hash.digest("hex")}`;- if (digest !== artifact.digest) throw new Error(`Artifact ${artifact.digest} bytes differ from ${subject}`);- // Validate the complete temporary file before replacing an explicitly selected destination.+ // Replace an explicitly selected destination only after the stream completes.await rename(temporary, destination);} catch (error) {await output.close().catch(() => undefined);@@ -128,8 +124,6 @@ function summarizeRecord(record: TypedRecord) {return {id: record.id,type: record.type,- digest: record.digest,- origin: record.origin,storage: record.value.kind,artifacts: collectArtifacts(value),};@@ -155,7 +149,6 @@ export function acceptedArchivedOutputs(state: BuildState,catalog: BuildCatalogEntry,): readonly ArchivedLogicalOutput[] {- if (catalog.core !== state.id) throw new Error("Build Catalog entry names another Core Build");const records = new Map(state.records.map((record) => [record.id, record]));const selections = new Map(state.plan.selections.map((selection) => [selection.output, selection]));return catalog.aliases.flatMap((alias) => {@@ -191,7 +184,6 @@ export function selectArchivedRecord(let output = options.output;if (options.name !== undefined) {if (options.catalog === undefined) throw new Error("Build has no Host Catalog aliases");- if (options.catalog.core !== state.id) throw new Error("Build Catalog entry names another Core Build");const alias = options.catalog.aliases.find((item) => item.name === options.name);if (alias === undefined) throw new Error(`Build Catalog has no output named ${options.name}`);if (alias.ref.kind === "record") id = alias.ref.id;@@ -215,16 +207,10 @@ export function selectArchivedRecord(}export function inspectBuild(state: BuildState, catalog?: BuildCatalogEntry) {- if (catalog !== undefined && catalog.core !== state.id) {- throw new Error("Build Catalog entry names another Core Build");- }const records = new Map(state.records.map((record) => [record.id, record]));const selections = new Map(state.plan.selections.map((selection) => [selection.output, selection]));return {- core: state.id,status: state.status,- graph: state.graph.id,- request: state.request.digest,targets: state.request.targets.map((target) => {const selection = selections.get(target.output);const record = selection === undefined ? undefined : records.get(selection.record);@@ -244,8 +230,6 @@ export function inspectBuild(state: BuildState, catalog?: BuildCatalogEntry) {accepted: records.has(selection.record),})),records: state.records.map(summarizeRecord),- receipts: state.receipts.length,- derivations: state.derivations.length,diagnostics: state.diagnostics,...(catalog === undefined ? {} : {presentation: {diff --git a/packages/cli/src/distribution.ts b/packages/cli/src/distribution.tsindex 77b0d450..dd9efd34 100644--- a/packages/cli/src/distribution.ts+++ b/packages/cli/src/distribution.ts@@ -17,11 +17,8 @@ export type CliCompilerOptions = {* It is Host configuration, never Core state or source-import authority.*/export type CliDistribution = {- /**- * Installation-local fallback for development checkouts and embedded Distributions.- * A real project with package.json always resolves its own installed capability packages first.- */- readonly fallbackPackageRoot?: string;+ /** Packages shipped with this CLI distribution. */+ readonly packageRoot?: string;/** Explicit Host bootstrap packages; never inferred from Source contents. */readonly bootstrapPackages: readonly LoadedPackage[];createCompiler(options: CliCompilerOptions): NodeCompiler;diff --git a/packages/cli/src/main.ts b/packages/cli/src/main.tsindex 474f0fc8..5fa9f43c 100644--- a/packages/cli/src/main.ts+++ b/packages/cli/src/main.ts@@ -97,11 +97,11 @@ async function nearestProjectPackageRoot(start: string): Promise<string | undefi}}-async function defaultPackageRoot(+async function resolvePackageRoot(projectStart: string,- fallback: string | undefined,+ distributionRoot: string | undefined,): Promise<string> {- return await nearestProjectPackageRoot(projectStart) ?? fallback ?? resolve(projectStart);+ return await nearestProjectPackageRoot(projectStart) ?? distributionRoot ?? resolve(projectStart);}function parseArgs(argv: readonly string[]): ParsedArgs {@@ -436,7 +436,6 @@ function usage(): string {}function createCatalogDescriptor(options: {- readonly core: BuildCatalogDescriptor["core"];readonly source: string;readonly compilation: NodeCompiledSourceClosure;readonly run?: { readonly path: string };@@ -449,10 +448,8 @@ function createCatalogDescriptor(options: {});return {format: "narratage.build-catalog-descriptor@1",- core: options.core,source: {path: resolve(options.source),- closure: options.compilation.closure.id,},...(options.run === undefined ? {} : { run: {path: resolve(options.run.path),@@ -577,7 +574,7 @@ function submissionStatus(): CliBuildSubmission["status"] {return dispatch.phase === "terminal"? dispatch.terminal!- : dispatch.phase === "leased" ? "running" : dispatch.phase;+ : dispatch.phase;}function formatOperationProgress(progress: OperationProgress): string {@@ -591,28 +588,21 @@ function formatOperationProgress(progress: OperationProgress): string {type QueueLaneSummary = {readonly pool: string;readonly lane: string;- readonly queued: number;- readonly active: number;readonly inFlight: number;};/** Derive the Provider → capability view from generic tickets; no model registry participates. */function summarizeQueueLanes(capacity: readonly CapacityReservation[]): readonly QueueLaneSummary[] {- const groups = new Map<string, { pool: string; lane: string; queued: number; active: number; inFlight: number }>();+ const groups = new Map<string, QueueLaneSummary>();for (const ticket of capacity) {if (ticket.queue === undefined) continue;const key = `${ticket.queue.pool}\u0000${ticket.queue.lane}`;- const group = groups.get(key) ?? {+ const previous = groups.get(key);+ groups.set(key, {pool: ticket.queue.pool,lane: ticket.queue.lane,- queued: 0,- active: 0,- inFlight: 0,- };- if (ticket.active === undefined && !ticket.inFlight) group.queued += 1;- if (ticket.active !== undefined) group.active += 1;- if (ticket.inFlight) group.inFlight += 1;- groups.set(key, group);+ inFlight: (previous?.inFlight ?? 0) + 1,+ });}return [...groups.values()].sort((left, right) =>left.pool.localeCompare(right.pool) || left.lane.localeCompare(right.lane));@@ -626,7 +616,7 @@ function queueLaneLines(groups: readonly QueueLaneSummary[]): readonly string[]pool = group.pool;lines.push(pool);}- lines.push(` ${group.lane}: queued ${group.queued} · active ${group.active} · remote ${group.inFlight}`);+ lines.push(` ${group.lane}: ${group.inFlight} remote`);}return lines;}@@ -662,7 +652,7 @@ async function observeBuild(},): Promise<CliBuildSubmission> {let current = initial;- let fingerprint: string | undefined;+ let lastProgress: string | undefined;let pollDelayMs = 100;let observedActivity = false;const startedAt = Date.now();@@ -700,24 +690,22 @@ async function observeBuild(const operations = Object.fromEntries([...new Set(status.operations.map((item) => item.status))].sort().map((state) => [state, status.operations.filter((item) => item.status === state).length]));const activity = status.operations- .filter((item) => item.status === "created" || item.status === "pending")+ .filter((item) => item.status === "pending").map((item) => `${item.endpoint}: ${item.progress === undefined? item.status: formatOperationProgress(item.progress)}`);- const nextFingerprint = JSON.stringify({+ const nextProgress = JSON.stringify({phase: status.dispatch.phase,terminal: status.dispatch.terminal,operations,activity: status.operations.map((item) => ({id: item.id,- revision: item.revision,status: item.status,progress: item.progress,- cancellation: item.cancellation?.status,})),});- if (nextFingerprint !== fingerprint) {- fingerprint = nextFingerprint;+ if (nextProgress !== lastProgress) {+ lastProgress = nextProgress;pollDelayMs = 100;options.onProgress?.({build: current.id,@@ -787,7 +775,7 @@ export async function runCli(? dirname(resolve(args.file)): process.cwd());const packageRootForProject = async (projectRoot = commandProjectRoot()): Promise<string> =>- args.packageRoot ?? await defaultPackageRoot(projectRoot, distribution.fallbackPackageRoot);+ args.packageRoot ?? await resolvePackageRoot(projectRoot, distribution.packageRoot);const writeOperational = (machine: unknown,title: string,@@ -1029,7 +1017,7 @@ export async function runCli(const profile = resolve(profileInput);const controller = await runtimeController(profile);if (args.action === "up") {- // Validate the Runtime Profile before replacing a stale Worker or starting programs.+ // Read the Runtime Profile before starting the Worker or its programs.const packageRoot = await packageRootForProject();const validated = await loadRuntime(await runtimeHost(profile, packageRoot));await validated.close();@@ -1086,12 +1074,12 @@ export async function runCli(const [worker, external, selectedRuntime] = loaded;runtime = selectedRuntime;const queue = await runtime.queue();- const counts = Object.fromEntries(["queued", "leased", "waiting", "blocked", "settling", "terminal"]+ const counts = Object.fromEntries(["queued", "running", "waiting", "blocked", "terminal"].map((phase) => [phase, queue.dispatches.filter((item) => item.phase === phase).length]));const lanes = summarizeQueueLanes(queue.capacity);const ready = worker.state === "running"&& external.programs.every((item) => item.state.state === "ready");- const active = ["queued", "leased", "waiting", "blocked", "settling"]+ const active = ["queued", "running", "waiting", "blocked"].reduce((total, phase) => total + (counts[phase] ?? 0), 0);const attention = active > 0 && !ready;const unavailable = external.programs.filter((item) => item.state.state !== "ready");@@ -1107,7 +1095,7 @@ export async function runCli(writeOperational(machine, "Runtime status", attention ? "warning" : ready ? "success" : "info", [["Worker", worker.state],["Queued", String(counts.queued ?? 0)],- ["Running", String(counts.leased ?? 0)],+ ["Running", String(counts.running ?? 0)],["Waiting", String(counts.waiting ?? 0)],["Programs", `${external.programs.length - unavailable.length}/${external.programs.length} ready`],["Capacity reservations", String(queue.capacity.length)],@@ -1239,20 +1227,18 @@ export async function runCli(runtime.queue(),controller.worker.status(),]);- const fingerprint = JSON.stringify({+ const queueView = JSON.stringify({dispatches: queue.dispatches,capacity: queue.capacity,worker,operations: queue.operations.map((item) => ({id: item.id,- revision: item.revision,status: item.status,progress: item.progress,- cancellation: item.cancellation?.status,})),});- if (args.watch && fingerprint === previous) return;- previous = fingerprint;+ if (args.watch && queueView === previous) return;+ previous = queueView;const value = {format: "narratage.cli-queue@1",at: Date.now(),@@ -1266,24 +1252,19 @@ export async function runCli(endpoint: item.endpoint,pool: item.pool,lane: item.lane,- attempt: item.attempt,- revision: item.revision,status: item.status,...(item.progress === undefined ? {} : { progress: item.progress }),- ...(item.cancellation === undefined ? {} : { cancellation: item.cancellation }),})),};const active = queue.dispatches.filter((item) => item.phase !== "terminal");const activeOperations = queue.operations.filter((item) =>- item.status === "created" || item.status === "pending");+ item.status === "pending");const buildLines = active.slice(0, args.verbose ? undefined : 12).map((item) =>- `${item.build}: ${item.phase}${item.admission === "open" ? "" : ` · ${item.admission}`}`);+ `${item.build}: ${item.phase}${item.cancellation === undefined ? "" : " · cancelling"}`);const laneLines = queueLaneLines(summarizeQueueLanes(queue.capacity));const genericCapacityLines = queue.capacity.filter((item) => item.queue === undefined).slice(0, args.verbose ? undefined : 12)- .map((item) => `${item.resources.map((resource) => resource.id).join(" + ")}: ${item.active === undefined- ? "queued"- : "active"} · ${item.build}`);+ .map((item) => `${item.resources.map((resource) => resource.id).join(" + ")}: remote · ${item.build}`);const operationLines = activeOperations.slice(0, args.verbose ? undefined : 12).map((item) =>`${item.build} · ${item.pool} → ${item.lane}: ${item.progress === undefined? item.status@@ -1312,9 +1293,7 @@ export async function runCli(const status = await runtime.status(entry.build);return {build: entry.build,- core: entry.core,createdAt: entry.createdAt,- updatedAt: entry.updatedAt,status: status.dispatch === undefined? status.build?.state.status: submissionStatus(status.dispatch),@@ -1339,9 +1318,7 @@ export async function runCli(.filter((output) => args.file === undefined || output.name === args.file).map((output) => ({build: catalog.build,- core: catalog.core,createdAt: catalog.createdAt,- updatedAt: catalog.updatedAt,status: status.dispatch === undefined? status.build!.state.status: submissionStatus(status.dispatch),@@ -1365,9 +1342,7 @@ export async function runCli(["Records", String(entries.length)],], shown.map((item) => {const created = new Date(item.createdAt).toISOString();- const digest = item.output.record.digest;- const shortDigest = `${digest.slice(0, 18)}…`;- return `${item.build}: ${item.output.name} · ${created} · ${shortDigest}`;+ return `${item.build}: ${item.output.name} · ${created} · ${item.output.record.id}`;}));} else if (args.command === "status") {let status = await runtime.status(args.file!);@@ -1399,8 +1374,6 @@ export async function runCli(const machine = {build: status.build === undefined ? null : {id: status.build.build,- revision: status.build.revision,- core: status.build.state.id,status: effectiveStatus,coreStatus: status.build.state.status,diagnostics: status.build.state.diagnostics,@@ -1412,13 +1385,10 @@ export async function runCli(id: operation.id,command: operation.command,endpoint: operation.endpoint,- attempt: operation.attempt,status: operation.status,- revision: operation.revision,...(operation.wakeAt === undefined ? {} : { wakeAt: operation.wakeAt }),...(operation.progress === undefined ? {} : { progress: operation.progress }),...(operation.failure === undefined ? {} : { failure: operation.failure }),- ...(operation.cancellation === undefined ? {} : { cancellation: operation.cancellation }),})),dispatch: status.dispatch ?? null,};@@ -1459,7 +1429,6 @@ export async function runCli(: submissionStatus(status.dispatch);const machine = {build: status.build.build,- revision: status.build.revision,status: effectiveStatus,coreStatus: status.build.state.status,dispatch: status.dispatch,@@ -1468,9 +1437,7 @@ export async function runCli(id: operation.id,command: operation.command,endpoint: operation.endpoint,- attempt: operation.attempt,status: operation.status,- revision: operation.revision,...(operation.wakeAt === undefined ? {} : { wakeAt: operation.wakeAt }),...(operation.progress === undefined ? {} : { progress: operation.progress }),...(operation.failure === undefined ? {} : { failure: operation.failure }),@@ -1486,7 +1453,7 @@ export async function runCli(const record = target.record === undefined ? undefined : recordById.get(target.record);const name = alias?.name ?? target.output;if (record === undefined) return `Target ${name} · not accepted`;- return `Target ${name} · ${displayType(record.type)} · ${record.digest.slice(0, 18)}…`;+ return `Target ${name} · ${displayType(record.type)} · ${record.id}`;});const otherAccepted = archive.demandedOutputs.filter((item) =>item.accepted && !targetOutputs.has(item.output));@@ -1497,7 +1464,7 @@ export async function runCli(const name = alias?.name ?? item.output;return record === undefined? `Output ${name}`- : `Output ${name} · ${displayType(record.type)} · ${record.digest.slice(0, 18)}…`;+ : `Output ${name} · ${displayType(record.type)} · ${record.id}`;}): otherAccepted.length === 0? []@@ -1507,7 +1474,6 @@ export async function runCli(...(effectiveStatus === archive.status ? [] : [["Core", archive.status] as const]),["Targets", String(archive.targets.length)], ["Accepted records", String(archive.records.length)],["Operations", String(status.operations.length)],- ...(args.verbose ? [["Revision", String(status.build.revision)] as const] : []),], [...(status.dispatch?.reason === undefined ? [] : [`Reason ${status.dispatch.reason}`]),...targetLines,@@ -1556,7 +1522,6 @@ export async function runCli(if (args.to === undefined) {const machine = {build: status.build.build,- revision: status.build.revision,record,artifacts: collectArtifacts(record.value.kind === "blob" ? record.value : record.value.value),};@@ -1591,7 +1556,6 @@ export async function runCli(requested: result !== undefined&& (result.phase !== "terminal" || result.terminal === "cancelled"),phase: result?.phase,- admission: result?.admission,terminal: result?.terminal,};const title = result === undefined@@ -1603,7 +1567,7 @@ export async function runCli(: "Build cancellation requested";writeOperational(machine, title,result === undefined ? "warning" : result.phase === "terminal" && result.terminal !== "cancelled" ? "info" : "success", [- ["Build", args.file!], ["Admission", result?.admission ?? "missing"], ["Phase", result?.phase ?? "missing"],+ ["Build", args.file!], ["Phase", result?.phase ?? "missing"],], result?.phase === "terminal" && result.terminal !== "cancelled"? [`No running work was changed; this Build is already ${result.terminal}.`]: []);@@ -1623,7 +1587,7 @@ export async function runCli(?? selectedRuntimeProjectRoot?? dirname(resolve(args.file!));const sourcePackageRoot = effectivePackageRoot- ?? await defaultPackageRoot(effectiveWorkspaceRoot, distribution.fallbackPackageRoot);+ ?? await resolvePackageRoot(effectiveWorkspaceRoot, distribution.packageRoot);const loadedPackageSet = distribution.discoverSourcePackages === undefined? undefined: await loadDiscoveredSourcePackages(distribution, {@@ -1666,11 +1630,6 @@ export async function runCli(ok: true,run: loaded.source,source: loaded.authorSource,- authorSourceClosure: loaded.author.closure.id,- runSourceClosure: loaded.closure.id,- authorModuleClosure: loaded.author.program.closure.digest,- executionModuleClosure: loaded.program.closure.digest,- authorGraph: loaded.author.graph.id,targets: loaded.document.targets,candidates: Object.fromEntries(loaded.document.candidates.map((item) => [item.id, item.kind])),satisfactions: loaded.document.satisfactions,@@ -1687,12 +1646,9 @@ export async function runCli(const result = await compiler.compileSource(workspace.entry, workspace);const machine = {format: "narratage.cli-check@1" as const,- sourceKind: "author" as const,- ok: true,- sourceClosure: result.closure.id,- moduleClosure: result.program.closure.digest,- graph: result.graph.id,- units: result.closure.units.length,+ sourceKind: "author" as const,+ ok: true,+ units: result.closure.units.length,sourceAssets: result.attachments.map((item) => item.artifact),modules: result.program.closure.modules.map((item) => `${item.manifest.name}@${item.manifest.version}`),exports: result.exports.map((item) => ({ name: item.name, type: item.type, kind: item.ref.kind })),@@ -1727,7 +1683,6 @@ export async function runCli(let runtime: CliRuntime | undefined;try {const catalog = createCatalogDescriptor({- core: result.state.id,source: loadedRun.authorSource,compilation: result.compilation.author,run: {@@ -1738,7 +1693,7 @@ export async function runCli(// One CLI invocation is one execution instance. Source and Plan identity// remain in Core; they never reclaim a previous Build.id: `bld_${randomUUID()}`,- state: result.state,+ definition: result.definition,...(loadedPackageSet === undefined ? {} : {implementationPackages: loadedPackageSet.map((item) => item.specifier),}),@@ -1809,7 +1764,6 @@ export async function runCli(});const machine = {build: built.id,- core: built.state.id,status: built.status,worker,...(programs === undefined ? {} : {@@ -1820,12 +1774,12 @@ export async function runCli(return {record: goal.record,type: goal.type,- ...(record === undefined ? {} : { digest: record.digest, value: record.value }),+ ...(record === undefined ? {} : { value: record.value }),};}),dispatch: {phase: built.dispatch.phase,- admission: built.dispatch.admission,+ ...(built.dispatch.cancellation === undefined ? {} : { cancellation: true }),...(built.dispatch.reason === undefined ? {} : { reason: built.dispatch.reason }),},};diff --git a/packages/cli/src/output.ts b/packages/cli/src/output.tsindex f8640331..03173051 100644--- a/packages/cli/src/output.ts+++ b/packages/cli/src/output.ts@@ -45,9 +45,6 @@ export type AuthorCheckOutput = {readonly format: "narratage.cli-check@1";readonly sourceKind: "author";readonly ok: true;- readonly sourceClosure: string;- readonly moduleClosure: string;- readonly graph: string;readonly units: number;readonly sourceAssets: readonly unknown[];readonly modules: readonly string[];@@ -64,13 +61,6 @@ export type RunCheckOutput = {readonly ok: true;readonly run: string;readonly source: string;- readonly authorSourceClosure: string;- readonly runSourceClosure: string;- readonly authorModuleClosure: string;- readonly executionModuleClosure: string;- readonly authorGraph: string;- readonly runGraph?: string;- readonly graph?: string;readonly targets: readonly unknown[];readonly candidates: Readonly<Record<string, string>>;readonly satisfactions: readonly unknown[];@@ -169,11 +159,6 @@ function shortPath(path: string): string {return local.length > 0 && !local.startsWith("..") ? local : absolute;}-function shortIdentity(value: string): string {- if (!value.startsWith("sha256:") || value.length <= 22) return value;- return `${value.slice(0, 15)}…${value.slice(-6)}`;-}-function shortOpaque(value: string): string {if (value.length <= 38) return value;return `${value.slice(0, 24)}…${value.slice(-8)}`;@@ -269,14 +254,6 @@ function renderAuthorCheck(lines.push(` ${colors.dim(`${ordered.length - shown.length} more · use --verbose for the complete list`)}`);}}- if (verbose) {- lines.push("", colors.strong("Identity"));- lines.push(...facts([- ["Source closure", shortIdentity(view.machine.sourceClosure)],- ["Module closure", shortIdentity(view.machine.moduleClosure)],- ["Graph", shortIdentity(view.machine.graph)],- ], colors));- }return `${lines.join("\n")}\n`;}@@ -302,16 +279,6 @@ function renderRunCheck(for (const item of unresolved) lines.push(` ${item.id} ← ${item.build}/${item.output}`);lines.push(` ${colors.dim("The Run source is valid. plan/build will resolve these archived values.")}`);}- if (verbose) {- lines.push("", colors.strong("Identity"));- lines.push(...facts([- ["Author graph", shortIdentity(view.machine.authorGraph)],- ...(view.machine.runGraph === undefined ? [] : [["Run graph", shortIdentity(view.machine.runGraph)] as const]),- ...(view.machine.graph === undefined ? [] : [["Build graph", shortIdentity(view.machine.graph)] as const]),- ["Author closure", shortIdentity(view.machine.authorSourceClosure)],- ["Run closure", shortIdentity(view.machine.runSourceClosure)],- ], colors));- }return `${lines.join("\n")}\n`;}@@ -389,14 +356,6 @@ function renderPlan(}}}- if (verbose) {- lines.push("", colors.strong("Identity"));- lines.push(...facts([- ["Plan", shortIdentity(plan.id)],- ["Graph", shortIdentity(plan.graph)],- ["Request", shortIdentity(plan.request)],- ], colors));- }return `${lines.join("\n")}\n`;}diff --git a/packages/cli/src/run-file.ts b/packages/cli/src/run-file.tsindex 525f86ea..43e745f8 100644--- a/packages/cli/src/run-file.ts+++ b/packages/cli/src/run-file.ts@@ -15,6 +15,7 @@ import {RunFrontendRegistry,} from "@narratage/run";import type { RunFrontend } from "@narratage/run";+import { selectArchivedRecord } from "./archive.js";export type LoadedRunFile = NodeCompiledRun & {readonly path: string;@@ -56,16 +57,21 @@ function createRunCompiler(options: {frontends,fragments,...(options.runtime === undefined ? {} : {- async readBuild(id: string) {- return (await archived(id)).build?.state;- },- async resolveBuildOutput(id: string, output: string) {- const alias = (await archived(id)).catalog?.aliases.find((item) => item.name === output);- if (alias === undefined) return output;- if (alias.ref.kind !== "logical-output") {+ async resolveBuildRecord(id: string, output: string) {+ const status = await archived(id);+ if (status.build === undefined) return undefined;+ const catalog = status.catalog;+ const alias = catalog?.aliases.find((item) => item.name === output);+ if (alias !== undefined && alias.ref.kind !== "logical-output") {throw new Error(`Build ${id} alias ${output} is an authored Record, not a Logical Output`);}- return alias.ref.id;+ const record = alias === undefined+ ? selectArchivedRecord(status.build.state, { output })+ : selectArchivedRecord(status.build.state, {+ name: output,+ catalog: catalog as NonNullable<typeof catalog>,+ });+ return { type: record.type, value: record.value };},}),});diff --git a/packages/cli/test/build-programs.test.ts b/packages/cli/test/build-programs.test.tsindex 89ca0c0b..49fed905 100644--- a/packages/cli/test/build-programs.test.ts+++ b/packages/cli/test/build-programs.test.ts@@ -3,8 +3,8 @@ import { mkdtemp, writeFile } from "node:fs/promises";import { tmpdir } from "node:os";import { join } from "node:path";import test from "node:test";+import { fixtureDigest } from "../../../test/fixture-digest.js";-import { digestOf } from "@narratage/protocol";import { createRunFrontendHostFacet } from "@narratage/run";import { runCli } from "../src/main.js";@@ -26,7 +26,7 @@ function distribution(name: "test",bootstrapPackages: [{specifier: "@example/run-frontend",- digest: digestOf("@example/run-frontend"),+ digest: fixtureDigest("@example/run-frontend"),contribution: {format: "narratage.node-package@1",hostFacets: [createRunFrontendHostFacet({diff --git a/packages/cli/test/control-commands.test.ts b/packages/cli/test/control-commands.test.tsindex 0b6c4637..70ec57f1 100644--- a/packages/cli/test/control-commands.test.ts+++ b/packages/cli/test/control-commands.test.ts@@ -69,10 +69,7 @@ test("status --watch reattaches to one durable Build until it becomes terminal",core: state.id,revision: phase === "queued" ? 0 : 1,createdAt: 1,- updatedAt: phase === "queued" ? 1 : 2,- priority: 0,availableAt: 1,- admission: phase === "queued" ? "open" : "closed",phase,...(phase === "terminal" ? { terminal: "complete" } : {}),});@@ -83,7 +80,7 @@ test("status --watch reattaches to one durable Build until it becomes terminal",calls.push("archive.status");const terminal = statusReads > 1;return {- build: { build: "build-watch", revision: terminal ? 1 : 0, state },+ build: { build: "build-watch", state },catalog: undefined,operations: [],dispatch: dispatch(terminal ? "terminal" : "queued"),@@ -258,7 +255,6 @@ test("cancelling a completed Build reports that no cancellation was requested",return {build: "build-complete",phase: "terminal",- admission: "closed",terminal: "complete",};},@@ -280,7 +276,6 @@ test("cancelling a completed Build reports that no cancellation was requested",build: "build-complete",requested: false,phase: "terminal",- admission: "closed",terminal: "complete",});});diff --git a/packages/cli/test/output.test.ts b/packages/cli/test/output.test.tsindex 6c1abb83..ac1170d1 100644--- a/packages/cli/test/output.test.ts+++ b/packages/cli/test/output.test.ts@@ -32,9 +32,6 @@ test("author check renders a compact human summary without dumping identity", ()format: "narratage.cli-check@1",sourceKind: "author",ok: true,- sourceClosure: digest,- moduleClosure: digest,- graph: digest,units: 2,sourceAssets: [{ digest }],modules: ["@narratage/script@1"],@@ -66,9 +63,6 @@ test("large author exports are bounded until verbose output is requested", () =>format: "narratage.cli-check@1" as const,sourceKind: "author" as const,ok: true as const,- sourceClosure: digest,- moduleClosure: digest,- graph: digest,units: 1,sourceAssets: [],modules: [],@@ -86,9 +80,6 @@ test("author check hides generated graph plumbing without deleting machine exporformat: "narratage.cli-check@1" as const,sourceKind: "author" as const,ok: true as const,- sourceClosure: digest,- moduleClosure: digest,- graph: digest,units: 1,sourceAssets: [],modules: [],@@ -119,28 +110,6 @@ test("author check hides generated graph plumbing without deleting machine exporassert.deepEqual(machine.exports.length, 3);});-test("verbose human output reveals shortened identities", () => {- const output = capture({ ...human, verbose: true }, {- kind: "check-author",- source: "/project/main.svml",- frontend: "@narratage/markup@1",- machine: {- format: "narratage.cli-check@1",- sourceKind: "author",- ok: true,- sourceClosure: digest,- moduleClosure: digest,- graph: digest,- units: 1,- sourceAssets: [],- modules: [],- exports: [],- },- });- assert.match(output, /Identity/u);- assert.match(output, /sha256:11111111…111111/u);-});-test("JSON mode is exact machine data with no terminal decoration", () => {const machine = {format: "narratage.cli-doctor@1" as const,@@ -161,9 +130,6 @@ test("JSON mode is exact machine data with no terminal decoration", () => {test("plan keeps named Run choices visible and leaves graph internals to verbose output", () => {const plan: BuildPlan = {format: "narratage.plan@1",- id: digest,- graph: digest,- request: digest,steps: [{id: "step-1",producer: { module: { name: "@narratage/media", version: "1" }, name: "inspect" },@@ -209,9 +175,6 @@ test("plan keeps named Run choices visible and leaves graph internals to verbosetest("a plan with no Needs stays compact without knowing any Provider names", () => {const plan: BuildPlan = {format: "narratage.plan@1",- id: digest,- graph: digest,- request: digest,steps: [],goals: [],selections: [],@@ -228,9 +191,6 @@ test("a plan with no Needs stays compact without knowing any Provider names", ()test("plan runtime preflight presents only demanded capabilities", () => {const plan: BuildPlan = {format: "narratage.plan@1",- id: digest,- graph: digest,- request: digest,steps: [],goals: [],selections: [],diff --git a/packages/cli/test/programs-command.test.ts b/packages/cli/test/programs-command.test.tsindex c25d2d5d..12342a9b 100644--- a/packages/cli/test/programs-command.test.ts+++ b/packages/cli/test/programs-command.test.ts@@ -16,7 +16,6 @@ function controller(return {profile: path,dataRoot: "/tmp",- revision: async () => "test-revision",worker: {up: async () => worker,status: async () => worker,diff --git a/packages/cli/test/runtime-build-process.test.ts b/packages/cli/test/runtime-build-process.test.tsdeleted file mode 100644index e064d55e..00000000--- a/packages/cli/test/runtime-build-process.test.ts+++ /dev/null@@ -1,211 +0,0 @@-import assert from "node:assert/strict";-import { execFile } from "node:child_process";-import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";-import { tmpdir } from "node:os";-import { join } from "node:path";-import { promisify } from "node:util";-import test from "node:test";--import {- loadNodePackageSelection,-} from "@narratage/package-loader-node";-import { digestOf } from "@narratage/protocol";-import { runtimeConfigRevision } from "@narratage/runtime-local/config";--import {- ensureRuntimeProcess,- runtimeProcessStatus,- stopRuntimeProcess,-} from "@narratage/runtime-local/worker-process";--const execute = promisify(execFile);-const cliEntry = join(process.cwd(), "packages", "video-cli", "src", "cli.ts");-const tsxImport = import.meta.resolve("tsx");--async function cli(project: string, argv: readonly string[]): Promise<Record<string, unknown>> {- const result = await execute(process.execPath, ["--import", tsxImport, cliEntry, ...argv, "--json"], {- cwd: project,- maxBuffer: 8 * 1024 * 1024,- });- return JSON.parse(result.stdout) as Record<string, unknown>;-}--async function cliHuman(project: string, argv: readonly string[]): Promise<string> {- const result = await execute(process.execPath, ["--import", tsxImport, cliEntry, ...argv, "--no-color"], {- cwd: project,- maxBuffer: 8 * 1024 * 1024,- });- return result.stdout;-}--function authorSource(length: number): string {- const chain = Array.from({ length }, (_, index) => {- const id = `message-${String(index).padStart(3, "0")}`;- const previous = index === 0 ? "seed" : `message-${String(index - 1).padStart(3, "0")}`;- return ` <text:Render id="${id}" template={kit.pass}>\n`- + ` <text:Set name="input" text={${previous}}/>\n`- + " </text:Render>";- }).join("\n");- return `<?svml using="@narratage/markup@1"?>--<svml>- <import as="text" from="@narratage/text@1"/>- <import as="kit" source="./pass.svs"/>- <text:Value id="seed">durable execution</text:Value>-${chain}-</svml>-`;-}--async function waitForBuild(- project: string,- profile: string,- build: string,- timeoutMs = 20_000,-): Promise<Record<string, unknown>> {- const deadline = Date.now() + timeoutMs;- let last: Record<string, unknown> | undefined;- while (Date.now() <= deadline) {- last = await cli(project, ["status", build, "--runtime", profile]);- const dispatch = last.dispatch as { readonly phase?: string } | undefined;- if (dispatch?.phase === "terminal") return last;- await new Promise((resolve) => setTimeout(resolve, 200));- }- throw new Error(`Build ${build} did not finish: ${JSON.stringify(last)}`);-}--test("CLI exits after durable submission and a restarted detached Worker completes a non-video Build", async () => {- const project = await mkdtemp(join(tmpdir(), "narratage-runtime-build-process-"));- const profile = join(project, "narratage.runtime.json");- const source = join(project, "main.svml");- const run = join(project, "build.svrun");- try {- await writeFile(join(project, "pass.svs"), `<?svml using="@narratage/text/svs@1"?>--<sheet version="1" id="pass">- text-template.pass { separator: paragraph; }- text-template.pass.block.input {- kind: slot;- order: 10;- slot: input;- }-</sheet>-`, "utf8");- await writeFile(source, authorSource(5), "utf8");- await writeFile(run, `<?svml using="@narratage/run-markup@1"?>--<svrun version="1">- <author source="./main.svml"/>- <target output="message-004"/>-</svrun>-`, "utf8");- await writeFile(profile, JSON.stringify({- format: "narratage.runtime-profile@1",- runtime: {- use: "@narratage/runtime-local",- config: {- dataRoot: ".narratage/runtime",- infrastructure: {- execution: { use: "@narratage/runtime-local" },- state: { use: "@narratage/store-sqlite", config: { path: "runtime.sqlite" } },- artifacts: { use: "@narratage/artifact-store-fs", config: { path: "artifacts" } },- credentials: { use: "@narratage/credential-store-env", config: {} },- },- roles: {- scheduler: { from: "execution", part: "scheduler" },- worker: { from: "execution", part: "worker" },- buildStore: { from: "state", part: "builds" },- operationStore: { from: "state", part: "operations" },- dispatchStore: { from: "state", part: "dispatch" },- artifactStore: { from: "artifacts", part: "store" },- credentialStores: [{ from: "credentials", part: "store" }],- },- endpoints: {},- capacity: { maxActiveOperations: 2 },- },- },- }, null, 2), "utf8");-- const dataRoot = join(project, ".narratage", "runtime");- const authorPackages = await loadNodePackageSelection([- "@narratage/run-markup",- "@narratage/text",- ], process.cwd());- const workerRevision = digestOf({- format: "narratage.runtime-worker-revision@1",- profile: await runtimeConfigRevision(profile),- });- const parked = await ensureRuntimeProcess(profile, dataRoot, {- command: process.execPath,- args: ["-e", `- const fs = require("node:fs");- const path = require("node:path");- const index = process.argv.indexOf("--ready-file");- const ready = process.argv[index + 1];- fs.mkdirSync(path.dirname(ready), { recursive: true });- fs.writeFileSync(ready, String(process.pid));- process.on("SIGTERM", () => process.exit(0));- setInterval(() => {}, 1000);- `],- }, workerRevision, 5_000, authorPackages.map((item) => item.specifier));- const submitted = await cli(project, [- "build", run,- "--runtime", profile,- "--workspace", project,- "--no-programs",- ]);- const build = String(submitted.build);- assert.match(build, /^bld_[0-9a-f-]{36}$/u);- assert.equal(submitted.status, "queued", "the foreground CLI reports durable admission, not execution ownership");- assert.equal((submitted.dispatch as { readonly phase?: string }).phase, "queued");- const originalProcess = await runtimeProcessStatus(profile, dataRoot, workerRevision);- assert.equal(originalProcess.state, "running", "the detached Runtime process outlives the submitting CLI process");- assert.equal(originalProcess.pid, parked.pid, "build reuses one profile-scoped Runtime process");-- await stopRuntimeProcess(profile, dataRoot, 10_000);- const interrupted = await cli(project, ["status", build, "--runtime", profile]);- assert.notEqual((interrupted.dispatch as { readonly phase?: string }).phase, "terminal",- "the long Build is durably incomplete when its first Worker is killed");-- const restarted = await cli(project, ["runtime", "up", profile]);- assert.equal((restarted.worker as { readonly state?: string }).state, "running");- assert.notEqual((restarted.worker as { readonly pid?: number }).pid, originalProcess.pid);- const logs = await cli(project, ["runtime", "logs", profile]);- assert.equal(logs.format, "narratage.cli-runtime-logs@1");- assert.equal(typeof logs.path, "string");- assert.equal(typeof logs.text, "string");- assert.match(await cliHuman(project, ["runtime", "logs", profile]), /Runtime logs[\s\S]*Path/u);- const completed = await waitForBuild(project, profile, build);- assert.equal((completed.build as { readonly status?: string }).status, "complete");- assert.equal((completed.dispatch as { readonly terminal?: string }).terminal, "complete");-- const queue = await cli(project, ["queue", "--runtime", profile]);- assert.equal((queue.dispatches as readonly { readonly phase: string }[])- .filter((item) => item.phase !== "terminal").length, 0);- const archive = await cli(project, ["builds", "--runtime", profile]);- assert.deepEqual((archive.builds as readonly { readonly build: string }[]).map((item) => item.build), [build]);- const inspected = await cli(project, ["inspect", build, "--runtime", profile]);- assert.equal((inspected.archive as { readonly status?: string }).status, "complete");- const exported = join(project, "output", "message.json");- const materialized = await cli(project, [- "get", build, "--runtime", profile, "--name", "message-004", "--to", exported,- ]);- assert.equal((materialized.materialized as { readonly kind?: string }).kind, "json");- assert.notEqual(JSON.parse(await readFile(exported, "utf8")), undefined);-- await stopRuntimeProcess(profile, dataRoot, 10_000);- const repeated = await cli(project, [- "build", run,- "--runtime", profile,- "--workspace", project,- "--no-programs",- "--follow",- ]);- assert.equal(repeated.status, "complete");- assert.notEqual(repeated.build, build,- "repeating the same Run Source creates a fresh Build instead of reclaiming source-derived state");- } finally {- await stopRuntimeProcess(profile, join(project, ".narratage", "runtime"), 10_000).catch(() => undefined);- await rm(project, { recursive: true, force: true });- }-});diff --git a/packages/cli/test/source-discovery.test.ts b/packages/cli/test/source-discovery.test.tsindex dd43785a..c843732d 100644--- a/packages/cli/test/source-discovery.test.ts+++ b/packages/cli/test/source-discovery.test.ts@@ -3,8 +3,9 @@ import { mkdtemp, rm, writeFile } from "node:fs/promises";import { tmpdir } from "node:os";import { join } from "node:path";import test from "node:test";+import { fixtureDigest } from "../../../test/fixture-digest.js";-import { digestOf, modulePackageAbi } from "@narratage/protocol";+import { modulePackageAbi } from "@narratage/protocol";import { createAuthorFrontendHostFacet } from "@narratage/elaborator";import { createRunFrontendHostFacet, runFragmentHostAbi } from "@narratage/run";import { sourceFrontendPackageAbi } from "@narratage/source";@@ -61,7 +62,7 @@ test("third-party Frontends discover same-named ABI requirements through physicaimplementation: {},}] },},- ].map((item) => ({ ...item, digest: digestOf(item.specifier) }));+ ].map((item) => ({ ...item, digest: fixtureDigest(item.specifier) }));const discovered = await discoverSourcePackages(join(root, "build.svrun"), {workspaceRoot: root,packages,diff --git a/packages/comment-sticker/src/manifest.ts b/packages/comment-sticker/src/manifest.tsindex 69787ce1..fa50cb61 100644--- a/packages/comment-sticker/src/manifest.ts+++ b/packages/comment-sticker/src/manifest.ts@@ -3,7 +3,6 @@ import { compositionDependency, compositionTypes } from "@narratage/composition"import { fontArtifactSchema, mediaDependency } from "@narratage/media";import { narrativeDependency, narrativeTypes } from "@narratage/narrative";import { programSpaceDependency, programSpaceTypes } from "@narratage/program-space";-import { digestOf } from "@narratage/protocol";import type { ModuleManifest, ProducerRef, TypeRef, ValueSchema } from "@narratage/protocol";import { semanticMapDependency, semanticMapTypes } from "@narratage/semantic-map";import { spatialDependency, spatialFrameSchema, spatialTypes } from "@narratage/spatial";@@ -134,8 +133,6 @@ export const commentStickerProgramSchema: ValueSchema = object({id: { schema: string },items: { schema: { kind: "array", minItems: 1, items: item } },});-const registered = (digest: ReturnType<typeof digestOf>) => ({ digest });-const validator = (digest: ReturnType<typeof digestOf>) => ({ implementation: registered(digest) });const appendInputs = [{ name: "set", type: commentStickerTypes.set }, { name: "header", type: commentStickerTypes.header },{ name: "frame", type: spatialTypes.frame }, { name: "style", type: commentStickerTypes.style },diff --git a/packages/comment-sticker/src/program.ts b/packages/comment-sticker/src/program.tsindex ce6d9b21..c3fffd79 100644--- a/packages/comment-sticker/src/program.ts+++ b/packages/comment-sticker/src/program.ts@@ -14,7 +14,7 @@ import { assertFontArtifactRef } from "@narratage/media";import type { NarrativeMomentRef, NarrativeSelectionRef } from "@narratage/narrative";import { assertProgramSpaceIdentity } from "@narratage/program-space";import type { ProgramSpace } from "@narratage/program-space";-import { canonicalize, digestOf, isDigest } from "@narratage/protocol";+import { canonicalize, isDigest } from "@narratage/protocol";import type { BlobRef } from "@narratage/protocol";import type { CompleteSemanticMap } from "@narratage/semantic-map";import { assertCanvasSpace, assertSpatialFrame } from "@narratage/spatial";diff --git a/packages/comment-sticker/test/comment-sticker.test.ts b/packages/comment-sticker/test/comment-sticker.test.tsindex ac4b62fd..ea0c255d 100644--- a/packages/comment-sticker/test/comment-sticker.test.ts+++ b/packages/comment-sticker/test/comment-sticker.test.ts@@ -1,12 +1,12 @@import assert from "node:assert/strict";import test from "node:test";+import { fixtureDigest } from "../../../test/fixture-digest.js";import { artifactTypes } from "@narratage/artifact";import { compileHyperframesDocument } from "@narratage/hyperframes";import type { FontArtifactRef, FontStackRef } from "@narratage/media";import { mediaTypes } from "@narratage/media";import { sealProgramSpace } from "@narratage/program-space";-import { digestOf } from "@narratage/protocol";import { spatialTypes } from "@narratage/spatial";import { svsRecipeType } from "@narratage/svs";import type { SvsRecipe } from "@narratage/svs";@@ -32,7 +32,7 @@ import {} from "../src/index.js";const font: FontArtifactRef = {- sources: [{ artifact: { kind: "blob", digest: digestOf("comment-sticker-font"), size: 1_024, mediaType: "font/woff2" } }],+ sources: [{ artifact: { kind: "blob", digest: fixtureDigest("comment-sticker-font"), size: 1_024, mediaType: "font/woff2" } }],weight: 800,style: "normal",};@@ -137,8 +137,6 @@ function authored(path: string, type: SurfaceResolvedReference["type"], value: uid: path,type,value: { kind: "inline", value: value as never },- digest: digestOf(value),- origin: { kind: "authored" },},};}@@ -160,7 +158,7 @@ test("Style Surface consumes an explicit SVS Recipe and exact Font Stack", async});test("Track Surface lowers mixed program and semantic Stickers to a finite explicit graph", async () => {- const blob = { kind: "blob" as const, digest: digestOf("comment-avatar"), size: 128, mediaType: "image/png" };+ const blob = { kind: "blob" as const, digest: fixtureDigest("comment-avatar"), size: 128, mediaType: "image/png" };const refs = new Map<string, SurfaceResolvedReference>([["video.canvas", authored("video.canvas", spatialTypes.canvas, canvas)],["video.space", authored("video.space", { module: { name: "@narratage/program-space", version: "1" }, name: "ProgramSpace" }, space)],diff --git a/packages/compiler-markup-node/test/compiler.test.ts b/packages/compiler-markup-node/test/compiler.test.tsindex 2e684ae2..67bc3a67 100644--- a/packages/compiler-markup-node/test/compiler.test.ts+++ b/packages/compiler-markup-node/test/compiler.test.ts@@ -55,7 +55,6 @@ const installedPackage: NodePackageContribution = {}),{abi: "example.unselected-host@1",- identity: { name: "must-remain-inert" },implementation() {throw new Error("an unselected Host facet executed");},diff --git a/packages/compiler-node/src/compiler.ts b/packages/compiler-node/src/compiler.tsindex ca223fc3..31cace17 100644--- a/packages/compiler-node/src/compiler.ts+++ b/packages/compiler-node/src/compiler.ts@@ -199,21 +199,13 @@ export class NodeCompiler {}/**- * Add modules used only by Run implementations without changing Author records or Author Graph- * identity. Authored Records are rebound to the larger verified closure before Core sees Run code.+ * Add modules used only by Run implementations. Author records stay unchanged.*/extendExecutionProgram(program: LinkedProgram, requests: readonly string[]): LinkedProgram {const existing = program.closure.modules.map((item) => `${item.manifest.name}@${item.manifest.version}`);+ if (requests.every((request) => existing.includes(request))) return program;const closure = this.#options.modules.createClosure([...existing, ...requests]);- if (closure.digest === program.closure.digest) return program;- const extended = link(closure, program.records);- if (extended.semanticDigest !== program.semanticDigest) {- throw new NodeCompilerError(- "EXECUTION_PROGRAM_SEMANTIC_DRIFT",- "Run-only Module closure extension changed Author program semantics",- );- }- return extended;+ return link(closure, program.records);}}diff --git a/packages/compiler-node/src/run.ts b/packages/compiler-node/src/run.tsindex 5d56ea9e..6bb8a71a 100644--- a/packages/compiler-node/src/run.ts+++ b/packages/compiler-node/src/run.ts@@ -4,17 +4,19 @@ import {sealBuildRequest,sealCompiledGraph,sliceExecution,- start,+ defineBuild,+ materializeBuild,} from "@narratage/core";import type {ArtifactAttachment,} from "@narratage/workspace";import type {BuildPlan,+ BuildDefinition,BuildRequest,BuildState,- BuildState as ArchivedBuildState,StoredValue,+ TypeRef,} from "@narratage/protocol";import {collectRunModuleRequests,@@ -36,11 +38,12 @@ export type NodeRunCompilerOptions = {readonly authorCompiler: NodeCompiler;readonly frontends: RunFrontendRegistryLike;readonly fragments: RunFragmentRegistryLike;- readonly readBuild?: (id: string) => Promise<ArchivedBuildState | undefined> | ArchivedBuildState | undefined;- readonly resolveBuildOutput?: (+ readonly resolveBuildRecord?: (build: string,output: string,- ) => Promise<string | undefined> | string | undefined;+ ) => Promise<{ readonly type: TypeRef; readonly value: StoredValue } | undefined>+ | { readonly type: TypeRef; readonly value: StoredValue }+ | undefined;};export type NodeCompiledRun = {@@ -58,7 +61,6 @@ export type NodeCheckedRun = {readonly authorSource: string;readonly author: NodeCompiledSourceClosure;readonly program: LinkedProgram;- readonly closure: RunCompilation["closure"];readonly document: RunCompilation["document"];readonly unresolvedBuildRecords: readonly {readonly id: string;@@ -70,10 +72,13 @@ export type NodeCheckedRun = {export type PlannedBuild = {readonly compilation: NodeCompiledRun;- /** Convenience view of `state.request`; `state` remains the sole durable authority. */+ /** Immutable authority persisted once for every fresh Runtime Build. */+ readonly definition: BuildDefinition;+ /** Convenience view of `definition.request`. */readonly request: BuildRequest;- /** Convenience view of `state.plan`; `state` remains the sole durable authority. */+ /** Convenience view of `definition.plan`. */readonly plan: BuildPlan;+ /** Materialized plan/read view; durable Stores persist Definition + Facts instead. */readonly state: BuildState;};@@ -223,7 +228,6 @@ export class NodeRunCompiler {authorSource: authorSource.id,author,program,- closure: decoded.closure,document: decoded.document,unresolvedBuildRecords: decoded.document.candidates.flatMap((item) => item.kind === "build-record"? [{ id: item.id, build: item.build, output: item.output }]@@ -245,25 +249,18 @@ export class NodeRunCompiler {collectRunModuleRequests(decoded.document, this.#options.fragments),);const executionCompilation = program === author.program ? author : { ...author, program };- const readBuild = this.#options.readBuild;- const resolveBuildOutput = this.#options.resolveBuildOutput;+ const resolveBuildRecord = this.#options.resolveBuildRecord;const run = await resolveRunDocument(decoded.document, {compilation: executionCompilation,- sourceClosure: decoded.closure,fragments: this.#options.fragments,readStoredValue: async (from) => await storedValueFromWorkspace(workspace, source, from),readFile: async (from, mediaType) => await fileValueFromWorkspace(workspace, source, from, mediaType),- async readBuild(id) {- if (readBuild === undefined) {- throw new Error(`Run contains historical Build Candidate ${id}; plan/build requires --runtime to resolve it`);+ async resolveBuildRecord(build, output) {+ if (resolveBuildRecord === undefined) {+ throw new Error(`Run contains historical Build Candidate ${build}; plan/build requires --runtime to resolve it`);}- return await readBuild(id);+ return await resolveBuildRecord(build, output);},- ...(resolveBuildOutput === undefined ? {} : {- async resolveBuildOutput(build: string, output: string) {- return await resolveBuildOutput(build, output);- },- }),});return {source: source.id,@@ -279,7 +276,6 @@ export class NodeRunCompiler {const authorGraph = compilation.author.graph;const selected = new Map(compilation.run.graph.satisfactions.map((item) => [item.output, item.candidate]));const fullGraph = sealCompiledGraph({- program: compilation.program.semanticDigest,outputs: authorGraph.outputs.map((output) => ({...output,primary: selected.get(output.id) ?? output.primary,@@ -288,16 +284,15 @@ export class NodeRunCompiler {operations: [...authorGraph.operations, ...compilation.run.graph.operations],});const fullRequest = sealBuildRequest({- graph: fullGraph.id,targets: compilation.run.graph.targets,});const sliced = sliceExecution(compilation.program, fullGraph, fullRequest);const request = sealBuildRequest({- graph: sliced.graph.id,targets: compilation.run.graph.targets,});- const state = start(sliced.program, sliced.graph, request);- return { compilation, request: state.request, plan: state.plan, state };+ const definition = defineBuild(sliced.program, sliced.graph, request);+ const state = materializeBuild(definition, []);+ return { compilation, definition, request: definition.request, plan: definition.plan, 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 6435f551..8a6c3e5f 100644--- a/packages/compiler-node/test/compiler.test.ts+++ b/packages/compiler-node/test/compiler.test.ts@@ -17,10 +17,6 @@ import {NodeCompilerError,NodeRunCompiler,} from "@narratage/compiler-node";-import {- computeModuleDigest,- digestOf,-} from "@narratage/core";import {AuthorFrontendRegistry,sealGraphFragment,@@ -98,7 +94,7 @@ test("module registration is atomic", () => {const laboratory = { name: "example.compiler-lab", version: "1" } as const;const resultType = { module: laboratory, name: "Result" } satisfies TypeRef;const producer = { module: laboratory, name: "produce" } satisfies ProducerRef;-const surfaceDigest = digestOf("example.compiler-lab/surface@1");+const surfaceDigest = "surface:example.compiler-lab/result";const resultSurface = {name: "result", tag: "Result", mode: "structured", outputs: [],} as const;@@ -133,7 +129,7 @@ const fragment = sealGraphFragment({const assetLaboratory = { name: "example.asset-lab", version: "1" } as const;const assetType = { module: assetLaboratory, name: "Asset" } satisfies TypeRef;-const assetSurfaceDigest = digestOf("example.asset-lab/surface@1");+const assetSurfaceDigest = "surface:example.asset-lab/asset";const assetSurface = {name: "asset", tag: "Asset", mode: "structured", outputs: [assetType],} as const;@@ -344,7 +340,7 @@ test("Node Compiler discovers real imports and emits a named public Author Graph});-test("Author Frontend identity comes only from the mandatory Source Header, never the suffix", async () => {+test("the mandatory Source Header selects the Frontend independently of the filename suffix", async () => {const root = await mkdtemp(join(tmpdir(), "narratage-self-described-source-"));const text = `<?svml using="@narratage/markup@1"?><svml>@@ -357,8 +353,8 @@ test("Author Frontend identity comes only from the mandatory Source Header, neveawait writeFile(arbitrary, text, "utf8");const first = await compiler(root).compileFile(svml);const second = await compiler(root).compileFile(arbitrary);- assert.equal(first.closure.id, second.closure.id);- assert.equal(first.graph.id, second.graph.id);+ assert.equal(first.exports[0]?.name, "hello.result");+ assert.equal(second.exports[0]?.name, "hello.result");const missing = join(root, "missing.svml");await writeFile(missing, "<svml/>", "utf8");@@ -453,34 +449,6 @@ test("static Run checking accepts a future BuildRecord without opening a BuildAr);});-test("static Run checking suggests the nearest Author export", async () => {- const root = await mkdtemp(join(tmpdir(), "narratage-run-target-suggestion-"));- const authorFile = join(root, "main.svml");- const runFile = join(root, "build.svrun");- await writeFile(authorFile, `<?svml using="@narratage/markup@1"?>- <svml>- <import as="lab" from="example.compiler-lab@1"/>- <lab:Result id="hello"/>- </svml>`, "utf8");- await writeFile(runFile, `<?svml using="@narratage/run-markup@1"?>- <svrun version="1">- <author source="./main.svml"/>- <target output="hello.reslt"/>- </svrun>`, "utf8");- const frontends = new RunFrontendRegistry();- frontends.register(runMarkupFrontend);- const runCompiler = new NodeRunCompiler({- authorCompiler: compiler(root),- frontends,- fragments: new RunFragmentRegistry(),- });- const workspace = await new NodeFilesystemWorkspace({ root }).open(runFile);- await assert.rejects(- runCompiler.checkSource(workspace.entry, workspace),- /unknown source export hello\.reslt; did you mean hello\.result\?/u,- );-});-test("source assets become graph values and a Host transfer bundle without closure metadata", async () => {const root = await mkdtemp(join(tmpdir(), "narratage-source-assets-"));const file = join(root, "main.svml");@@ -499,7 +467,6 @@ test("source assets become graph values and a Host transfer bundle without closuassert.equal(first.program.records[0]?.value.kind === "blob" ? first.program.records[0].value.digest : undefined, attachment?.artifact.digest);await writeFile(asset, new Uint8Array([9, 8, 7]));const second = await assetCompiler({ root }).compileFile(file);- assert.notEqual(second.closure.id, first.closure.id);assert.notEqual(second.attachments[0]?.artifact.digest, attachment?.artifact.digest);});@@ -622,7 +589,7 @@ test("an asset root widens bytes without widening Source imports", async () => {}), (error: unknown) => error instanceof WorkspaceError && error.code === "SOURCE_OUTSIDE_ROOT");});-test("filesystem and in-memory Workspaces compile identical source and bytes to one semantic result", async () => {+test("filesystem and in-memory Workspaces load identical asset bytes", async () => {const root = await mkdtemp(join(tmpdir(), "narratage-workspace-equivalence-"));const file = join(root, "main.svml");const source = `<?svml using="@narratage/markup@1"?>@@ -637,9 +604,6 @@ test("filesystem and in-memory Workspaces compile identical source and bytes toconst filesystem = await assetCompiler({ root }).compileFile(file);const memory = await assetCompiler({ workspace: memoryWorkspace(source, bytes) }).compileFile("memory:main");- assert.equal(memory.closure.id, filesystem.closure.id);- assert.equal(memory.program.semanticDigest, filesystem.program.semanticDigest);- assert.equal(memory.graph.id, filesystem.graph.id);assert.deepEqual(memory.attachments.map((item) => item.artifact),filesystem.attachments.map((item) => item.artifact));assert.deepEqual(await readAttachment(memory.attachments[0]),diff --git a/packages/composition/src/manifest.ts b/packages/composition/src/manifest.tsindex c9b41212..66765b52 100644--- a/packages/composition/src/manifest.ts+++ b/packages/composition/src/manifest.ts@@ -1,11 +1,9 @@import { mediaDependency, programSpaceDependency } from "./schema.js";import { visualIrDependency } from "@narratage/visual-ir";-import { digestOf } from "@narratage/protocol";import type { ModuleManifest, TypeRef } from "@narratage/protocol";import { audioTrackSchema, compositionSchema, visualTrackSchema } from "./schema.js";export const compositionModuleRef = { name: "@narratage/composition", version: "1" } as const;export const compositionTypes = { visualTrack: { module: compositionModuleRef, name: "VisualTrack" }, audioTrack: { module: compositionModuleRef, name: "AudioTrack" }, composition: { module: compositionModuleRef, name: "Composition" } } satisfies Record<string, TypeRef>;-const validator = (digest: ReturnType<typeof digestOf>) => ({ implementation: { digest } });export const compositionManifest: ModuleManifest = { format: "narratage.module@1", name: compositionModuleRef.name, version: compositionModuleRef.version,dependencies: [mediaDependency, programSpaceDependency, visualIrDependency], types: [{ name: compositionTypes.visualTrack.name },diff --git a/packages/composition/src/track.ts b/packages/composition/src/track.tsindex 94687b6b..63505b0e 100644--- a/packages/composition/src/track.ts+++ b/packages/composition/src/track.ts@@ -1,4 +1,4 @@-import { digestOf, isDigest } from "@narratage/protocol";+import { canonicalStringify, isDigest } from "@narratage/protocol";import type { BlobRef } from "@narratage/protocol";import {@@ -546,7 +546,7 @@ function assertExactFonts(fonts: readonly FontArtifactRef[], label: string): voiconst faces = new Set<string>();for (const [index, font] of fonts.entries()) {assertFontArtifactRef(font, `${label}.${index}`);- const face = digestOf(font);+ const face = canonicalStringify(font);if (faces.has(face)) throw new Error(`${label} has a duplicate font face.`);faces.add(face);}diff --git a/packages/composition/test/render.test.ts b/packages/composition/test/render.test.tsindex f7afa370..d31a4217 100644--- a/packages/composition/test/render.test.ts+++ b/packages/composition/test/render.test.ts@@ -1,5 +1,6 @@import assert from "node:assert/strict";import test from "node:test";+import { fixtureDigest } from "../../../test/fixture-digest.js";import {assertCompositableSurfaceRef,@@ -13,7 +14,6 @@ import {sealVisualTrack,} from "../src/index.js";import type { VisualTrack } from "../src/index.js";-import { digestOf } from "@narratage/protocol";const space = sealProgramSpace({durationSec: 2,@@ -23,7 +23,7 @@ const space = sealProgramSpace({const font: FontArtifactRef = {sources: [{ artifact: {kind: "blob",- digest: digestOf("font:inter-bold"),+ digest: fixtureDigest("font:inter-bold"),size: 1_024,mediaType: "font/woff2",} }],@@ -34,7 +34,7 @@ const font: FontArtifactRef = {const animatedSurface: CompositableSurfaceRef = {artifact: {kind: "blob",- digest: digestOf("surface:alpha-webm"),+ digest: fixtureDigest("surface:alpha-webm"),size: 2_048,mediaType: "video/webm",},diff --git a/packages/composition/test/track.test.ts b/packages/composition/test/track.test.tsindex 97bb3a41..50aac58c 100644--- a/packages/composition/test/track.test.ts+++ b/packages/composition/test/track.test.ts@@ -1,9 +1,10 @@import assert from "node:assert/strict";import test from "node:test";+import { fixtureDigest } from "../../../test/fixture-digest.js";import { registerTypeValidatorFacets } from "@narratage/component-kit";import { createResolvedClosure } from "@narratage/core";-import { canonicalize, digestOf } from "@narratage/protocol";+import { canonicalize } from "@narratage/protocol";import type { BlobRef } from "@narratage/protocol";import { TypeValidatorRegistry, validateValue } from "@narratage/validation";import { artifactManifest } from "@narratage/artifact";@@ -33,13 +34,13 @@ const videoContractManifests = [artifactManifest, narrativeManifest, mediaManifespeechManifest, speechEvidenceManifest, semanticMapManifest, spatialManifest, visualIrManifest, compositionManifest] as const;const font: FontArtifactRef = {- sources: [{ artifact: { kind: "blob", digest: digestOf("track:test-font"), size: 1_024, mediaType: "font/woff2" } }],+ sources: [{ artifact: { kind: "blob", digest: fixtureDigest("track:test-font"), size: 1_024, mediaType: "font/woff2" } }],weight: 700,style: "normal",};const audio: BlobRef = {kind: "blob",- digest: digestOf("audio"),+ digest: fixtureDigest("audio"),size: 24,mediaType: "audio/wav",};@@ -238,19 +239,6 @@ test("Composition validates Track frame ranges against the explicitly connectedassert.notDeepEqual(programSpace, foreign);});-test("Composition is a plain product value; the enclosing Record binds its integrity", () => {- const { programSpace, visual } = fixture();- const composition = sealComposition({- id: "main",- canvas: { width: 1080, height: 1920, clearColor: "#000000" },- tracks: [visual],- });- const tampered = structuredClone(composition);- (tampered.tracks[0] as { id: string }).id = "changed";- assert.doesNotThrow(() => assertCompositionIdentity(tampered, programSpace));- assert.equal(tampered.tracks[0]?.id, "changed");-});-test("one authoring Track may contribute independently stacked Presents", () => {const { programSpace, visual } = fixture();const content = structuredClone(visual) as VisualTrack;diff --git a/packages/core/README.md b/packages/core/README.mdindex 7c6ba209..ba50df0a 100644--- a/packages/core/README.md+++ b/packages/core/README.md@@ -7,8 +7,9 @@ The package accepts an already resolved module closure, typed authored modules,BuildPlan by traversing backwards from every Target, resolving Logical Outputs and memoizing sharedatomic Operations by stable OperationId during compilation. The BuildPlan is frozen before anyexternal Command; execution performs no Candidate selection, graph mutation or content-based-deduplication. It then validates immutable Records, Needs, Receipts and-Derivations and advances a serializable `BuildState` with pure `reduce(state, event)` calls.+deduplication. Durable execution is one immutable `BuildDefinition` plus fixed, append-only+`BuildFact` values. `BuildMachine` reconstructs a disposable read/execution view and derives every+pending Command; Commands and the materialized `BuildState` are never stored as authority.Derivations bind Producer and implementation identity, input/output Record digests, Need requestdigests and the accepted event digest. Need Receipts content-address the Driver-attested Endpointdiff --git a/packages/core/src/canonical.ts b/packages/core/src/canonical.tsindex d5b32e49..2043fec2 100644--- a/packages/core/src/canonical.ts+++ b/packages/core/src/canonical.ts@@ -1,8 +1,5 @@export {canonicalStringify,canonicalize,- digestOf,isDigest,- recordDigest,- semanticRecordsDigest,} from "@narratage/protocol";diff --git a/packages/core/src/graph.ts b/packages/core/src/graph.tsindex 22dae1e3..77ceb286 100644--- a/packages/core/src/graph.ts+++ b/packages/core/src/graph.ts@@ -13,7 +13,7 @@ import type {TypeRef,} from "@narratage/protocol";-import { canonicalize, digestOf, isDigest } from "./canonical.js";+import { canonicalize } from "./canonical.js";import { invariant } from "./error.js";import { resolveProducer, sealRecord, verifyRecordStructure } from "./link.js";import { producerKey, sameType, typeKey } from "./reference.js";@@ -111,33 +111,29 @@ function normalizeOperation(operation: OperationNode): OperationNode {}export function sealCompiledGraph(- graph: Omit<CompiledGraph, "format" | "id">,+ graph: Omit<CompiledGraph, "format">,): CompiledGraph {const normalized = {- program: graph.program,outputs: [...graph.outputs].map(normalizeOutput).sort((a, b) => a.id.localeCompare(b.id)),candidates: [...graph.candidates].map(normalizeCandidate).sort((a, b) => a.id.localeCompare(b.id)),operations: [...graph.operations].map(normalizeOperation).sort((a, b) => a.id.localeCompare(b.id)),};- const content = {+ return {format: "narratage.graph@1" as const,...normalized,};- return { ...content, id: digestOf(content) };}export function sealBuildRequest(- request: Omit<BuildRequest, "format" | "digest">,+ request: Omit<BuildRequest, "format">,): BuildRequest {const targets = [...request.targets].map((target) => ({ output: target.output })).sort((a, b) => a.output.localeCompare(b.output));- const content = {+ return {format: "narratage.build-request@1" as const,- graph: request.graph,targets,};- return { ...content, digest: digestOf(content) };}export function resolveLogicalOutput(graph: CompiledGraph, id: string): LogicalOutput {@@ -257,9 +253,6 @@ function verifyCandidateValue(program: LinkedProgram, graph: CompiledGraph, candid: candidate.root.value.id,type: candidate.type,value: candidate.root.value.value,- origin: {- kind: "provided",- },});verifyRecordStructure(program.closure, provisional);}@@ -278,14 +271,6 @@ function verifySatisfaction(export function verifyCompiledGraph(program: LinkedProgram, graph: CompiledGraph): void {invariant(graph.format === "narratage.graph@1", "UNSUPPORTED_GRAPH", "unsupported compiled graph format");- invariant(isDigest(graph.id), "INVALID_DIGEST", "compiled graph id is invalid");- invariant(- graph.program === program.semanticDigest,- "GRAPH_PROGRAM_MISMATCH",- "compiled graph belongs to another linked program",- );- const { id: _id, ...content } = graph;- invariant(graph.id === digestOf(content), "GRAPH_DIGEST_MISMATCH", "compiled graph digest differs");const outputIds = new Set<string>();const candidateIds = new Set<string>();const operationIds = new Set<string>();@@ -339,10 +324,6 @@ export function verifyBuildRequest("UNSUPPORTED_BUILD_REQUEST","unsupported BuildRequest format",);- invariant(isDigest(request.digest), "INVALID_DIGEST", "BuildRequest digest is invalid");- invariant(request.graph === graph.id, "BUILD_REQUEST_GRAPH_MISMATCH", "BuildRequest belongs to another graph");- const { digest: _digest, ...content } = request;- invariant(request.digest === digestOf(content), "BUILD_REQUEST_DIGEST_MISMATCH", "BuildRequest digest differs");invariant(request.targets.length > 0, "EMPTY_BUILD_TARGETS", "BuildRequest has no Targets");const targets = new Set<string>();diff --git a/packages/core/src/index.ts b/packages/core/src/index.tsindex 1585cb5e..70f54560 100644--- a/packages/core/src/index.ts+++ b/packages/core/src/index.ts@@ -1,7 +1,6 @@-export { canonicalStringify, canonicalize, digestOf, isDigest, recordDigest } from "./canonical.js";+export { canonicalStringify, canonicalize, isDigest } from "./canonical.js";export { CoreError } from "./error.js";export {- computeModuleDigest,createResolvedClosure,link,resolveProducer,@@ -19,7 +18,14 @@ export {export { compileBuild, plannedNeeds, validatePlan } from "./plan.js";export type { PlannedNeed } from "./plan.js";export { sliceExecution } from "./slice.js";-export { reduce, start } from "./reducer.js";-export { verifyBuildState } from "./verify.js";+export {+ admitBuildResult,+ buildDefinition,+ defineBuild,+ materializeBuild,+ reduce,+ start,+} from "./reducer.js";+export { BuildMachine, restoreBuildMachine } from "./machine.js";export { validateStoredValue } from "./schema.js";export type { TypedRecordDraft } from "./link.js";diff --git a/packages/core/src/link.ts b/packages/core/src/link.tsindex d68987e7..f695d53d 100644--- a/packages/core/src/link.ts+++ b/packages/core/src/link.ts@@ -1,6 +1,5 @@import type {CapabilityRef,- Digest,LinkedProgram,ModuleManifest,ModuleRef,@@ -15,11 +14,10 @@ import type {TypedRecord,} from "@narratage/protocol";-import { digestOf, isDigest, recordDigest, semanticRecordsDigest } from "./canonical.js";import { invariant } from "./error.js";import { capabilityKey, moduleKey, producerKey, sameType, typeKey } from "./reference.js";-export type TypedRecordDraft = Omit<TypedRecord, "digest">;+export type TypedRecordDraft = TypedRecord;type ClosureIndex = {readonly types: ReadonlyMap<string, ResolvedTypeDeclaration>;@@ -73,32 +71,17 @@ function resolvedManifest(manifest: ModuleManifest): ResolvedModuleManifest {};}-export function computeModuleDigest(manifest: ResolvedModuleManifest): Digest {- return digestOf(manifest);-}--function computeClosureDigest(modules: readonly ResolvedModule[]): Digest {- return digestOf({- format: "narratage.closure@1",- modules: [...modules]- .sort((left, right) => moduleKey(left.manifest).localeCompare(moduleKey(right.manifest)))- .map((module) => ({ ref: manifestRef(module.manifest), digest: module.digest })),- });-}-export function createResolvedClosure(manifests: readonly ModuleManifest[],): ResolvedModuleClosure {const modules = manifests.map((definition) => {const manifest = resolvedManifest(definition);- return { digest: computeModuleDigest(manifest), manifest };+ return { manifest };});- const closure: ResolvedModuleClosure = {+ return {format: "narratage.closure@1",modules,- digest: computeClosureDigest(modules),};- return closure;}function ensureUniqueNames(names: readonly string[], kind: string, owner: string): void {@@ -112,7 +95,6 @@ function ensureUniqueNames(names: readonly string[], kind: string, owner: stringexport function verifyClosure(closure: ResolvedModuleClosure): void {invariant(closure.format === "narratage.closure@1", "UNSUPPORTED_CLOSURE", "unsupported closure format");- invariant(isDigest(closure.digest), "INVALID_DIGEST", "closure digest is invalid");const modules = new Map<string, ResolvedModule>();const declaredTypes = new Set<string>();@@ -124,13 +106,6 @@ export function verifyClosure(closure: ResolvedModuleClosure): void {invariant(ref.version.length > 0, "EMPTY_MODULE_VERSION", `${ref.name} version is empty`);invariant(module.manifest.format === "narratage.module@1", "UNSUPPORTED_MODULE", `${key} format is unsupported`);invariant(!modules.has(key), "DUPLICATE_MODULE", `duplicate module ${key}`, key);- invariant(isDigest(module.digest), "INVALID_DIGEST", `${key} digest is invalid`, key);- invariant(- module.digest === computeModuleDigest(module.manifest),- "MODULE_DIGEST_MISMATCH",- `${key} manifest digest does not match`,- key,- );ensureUniqueNames(module.manifest.types.map((item) => item.name), "type", key);ensureUniqueNames(module.manifest.capabilities.map((item) => item.name), "capability", key);ensureUniqueNames(module.manifest.producers.map((item) => item.name), "producer", key);@@ -170,12 +145,6 @@ export function verifyClosure(closure: ResolvedModuleClosure): void {}}- invariant(- closure.digest === computeClosureDigest(closure.modules),- "CLOSURE_DIGEST_MISMATCH",- "resolved module closure digest does not match",- );-for (const module of closure.modules) {const allowed = new Set([moduleKey(module.manifest),@@ -263,7 +232,7 @@ export function resolveProducer(}export function sealRecord(record: TypedRecordDraft): TypedRecord {- return { ...record, digest: recordDigest(record.type, record.value) };+ return { ...record };}export function verifyRecordStructure(@@ -271,13 +240,6 @@ export function verifyRecordStructure(record: TypedRecord,): ResolvedTypeDeclaration {invariant(record.id.length > 0, "EMPTY_RECORD_ID", "record id is empty");- invariant(isDigest(record.digest), "INVALID_DIGEST", `${record.id} digest is invalid`, record.id);- invariant(- record.digest === recordDigest(record.type, record.value),- "RECORD_DIGEST_MISMATCH",- `${record.id} digest does not match its value`,- record.id,- );const declaration = resolveType(closure, record.type);return declaration;}@@ -289,7 +251,6 @@ export function link(const program: LinkedProgram = {closure,records: [...authoredRecords],- semanticDigest: semanticRecordsDigest(authoredRecords),};verifyLinkedProgram(program);return program;@@ -301,10 +262,6 @@ export function verifyLinkedProgram(program: LinkedProgram): void {for (const record of program.records) {invariant(!ids.has(record.id), "DUPLICATE_RECORD", `duplicate record ${record.id}`, record.id);ids.add(record.id);- invariant(record.origin.kind === "authored", "NON_AUTHORED_PROGRAM_RECORD",- `${record.id} is not an authored input record`, record.id);verifyRecordStructure(program.closure, record);}- invariant(program.semanticDigest === semanticRecordsDigest(program.records),- "PROGRAM_SEMANTIC_DIGEST_MISMATCH", "linked program semantic digest does not match");}diff --git a/packages/core/src/machine.ts b/packages/core/src/machine.tsnew file mode 100644index 00000000..5bdf42c2--- /dev/null+++ b/packages/core/src/machine.ts@@ -0,0 +1,67 @@+import type {+ BuildDefinition,+ BuildFact,+ BuildState,+ CoreCommand,+ CommandResult,+ TypedRecord,+} from "@narratage/protocol";++import { admitBuildResult, materializeBuild } from "./reducer.js";++/**+ * Non-serializable incremental Core machine.+ *+ * Definition + Facts are the stored form. The machine is only their disposable execution view.+ */+export class BuildMachine {+ readonly definition: BuildDefinition;+ #state: BuildState;+ #proposal: { readonly fact: BuildFact; readonly state: BuildState } | undefined;++ constructor(definition: BuildDefinition, facts: readonly BuildFact[] = []) {+ this.definition = structuredClone(definition);+ this.#state = materializeBuild(this.definition, facts);+ }++ get status(): BuildState["status"] {+ return this.#state.status;+ }++ commands(): readonly CoreCommand[] {+ return this.#state.outstanding;+ }++ record(id: string): TypedRecord | undefined {+ return this.#state.records.find((item) => item.id === id);+ }++ /** Materialized compatibility/read view. It is never the durable Store representation. */+ view(): BuildState {+ return this.#state;+ }++ /** Validate one result without mutating the machine before durable persistence succeeds. */+ evaluate(result: CommandResult): BuildFact | undefined {+ if (this.#proposal !== undefined) {+ throw new Error(`Build result ${this.#proposal.fact.command} has not been durably applied`);+ }+ const admitted = admitBuildResult(this.#state, result);+ this.#proposal = { fact: admitted.fact, state: admitted.state };+ return admitted.fact;+ }++ /** Advance memory only after BuildStore.append has committed the proposed Fact. */+ commit(): void {+ if (this.#proposal === undefined) throw new Error("Build machine has no evaluated result to commit");+ this.#state = this.#proposal.state;+ this.#proposal = undefined;+ }+}++export function restoreBuildMachine(+ definition: BuildDefinition,+ facts: readonly BuildFact[],+): BuildMachine {+ return new BuildMachine(definition, facts);+}diff --git a/packages/core/src/plan.ts b/packages/core/src/plan.tsindex ba2633a9..c128799e 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, digestOf } from "./canonical.js";+import { canonicalStringify } from "./canonical.js";import { invariant } from "./error.js";import {operationResultRecord,@@ -96,16 +96,12 @@ export function plannedNeeds(state: BuildState): readonly PlannedNeed[] {}function planContent(- graph: CompiledGraph,- request: BuildRequest,steps: readonly ProducerStep[],goals: BuildPlan["goals"],selections: BuildPlan["selections"],-): Omit<BuildPlan, "id"> {+): BuildPlan {return {format: "narratage.plan@1",- graph: graph.id,- request: request.digest,steps,goals,selections,@@ -160,7 +156,6 @@ export function compileBuild(id: candidate.root.value.id,type: candidate.type,value: candidate.root.value.value,- origin: { kind: "provided" },});invariant(!authored.has(record.id), "PROVIDED_RECORD_CONFLICT", `${record.id} conflicts with authored input`);resolved = { record: record.id, type: record.type };@@ -227,8 +222,7 @@ export function compileBuild(.map(({ source }) => ({ record: source.record, type: source.type })).sort((a, b) => a.record.localeCompare(b.record));const sortedSelections = [...selections.values()].sort((a, b) => a.output.localeCompare(b.output));- const content = planContent(graph, request, steps, goals, sortedSelections);- const plan: BuildPlan = { ...content, id: digestOf(content) };+ const plan = planContent(steps, goals, sortedSelections);validatePlanStructure(program, graph, request, plan);return plan;}@@ -240,10 +234,6 @@ function validatePlanStructure(plan: BuildPlan,): void {invariant(plan.format === "narratage.plan@1", "UNSUPPORTED_PLAN", "unsupported build plan format");- invariant(plan.graph === graph.id, "PLAN_GRAPH_MISMATCH", "build plan belongs to another graph");- invariant(plan.request === request.digest, "PLAN_REQUEST_MISMATCH", "build plan belongs to another request");- const { id: _id, ...content } = plan;- invariant(plan.id === digestOf(content), "PLAN_DIGEST_MISMATCH", "build plan digest differs");invariant(plan.goals.length > 0, "EMPTY_PLAN_GOALS", "build plan has no goals");const records = new Map<RecordId, ProducedRecord>();@@ -320,12 +310,8 @@ export function validatePlan(request: BuildRequest,plan: BuildPlan,): void {- const expected = compileBuild(program, graph, request);- invariant(- canonicalStringify(plan) === canonicalStringify(expected),- "PLAN_NOT_DERIVED",- "build plan is not the plan compiled from its graph and BuildRequest",- );+ verifyBuildRequest(program, graph, request);+ validatePlanStructure(program, graph, request, plan);}/** Materialize selected zero-input values from their sole source of truth: the Run Graph. */@@ -343,7 +329,6 @@ export function selectedProvidedRecords(id: candidate.root.value.id,type: candidate.type,value: candidate.root.value.value,- origin: { kind: "provided" },});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);diff --git a/packages/core/src/provenance.ts b/packages/core/src/provenance.tsindex 09e0a082..196fa772 100644--- a/packages/core/src/provenance.ts+++ b/packages/core/src/provenance.ts@@ -1,37 +1,3 @@-import type {- Derivation,- DerivationId,- Digest,- Need,- Receipt,- ReceiptId,-} from "@narratage/protocol";--import { digestOf } from "./canonical.js";--export function commandId(build: string, kind: string, subject: string): string {- return `command:${digestOf({ build, kind, subject })}`;-}--export function eventDigest(event: unknown): Digest {- return digestOf(event);-}--export function needRequestDigest(- need: Pick<Need, "capability" | "returns" | "constraints" | "result">,-): Digest {- return digestOf({- capability: need.capability,- returns: need.returns,- constraints: need.constraints,- result: need.result,- });-}--export function derivationId(derivation: Omit<Derivation, "id">): DerivationId {- return `derivation:${digestOf(derivation)}`;-}--export function receiptId(receipt: Omit<Receipt, "id">): ReceiptId {- return `receipt:${digestOf(receipt)}`;+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 e0eb2a04..f1830a7a 100644--- a/packages/core/src/reducer.ts+++ b/packages/core/src/reducer.ts@@ -1,55 +1,32 @@import type {- BuildEvent,+ BuildDefinition,+ BuildFact,BuildPlan,BuildRequest,BuildState,CoreCommand,CompiledGraph,- Derivation,- Digest,+ CommandResult,FulfillNeedCommand,InvokeProducerCommand,LinkedProgram,Need,NeedFulfilledEvent,ProducerCompletedEvent,- Receipt,StoredValue,TypedRecord,} from "@narratage/protocol";-import { canonicalize, digestOf, recordDigest } from "./canonical.js";+import { canonicalize } from "./canonical.js";import { CoreError, invariant } from "./error.js";import { resolveProducer, verifyRecordStructure } from "./link.js";import { compileBuild, producerStep, selectedProvidedRecords } from "./plan.js";-import {- commandId,- derivationId,- eventDigest,- needRequestDigest,- receiptId,-} from "./provenance.js";+import { commandId } from "./provenance.js";function withoutCommand(state: BuildState, id: string): readonly CoreCommand[] {return state.outstanding.filter((command) => command.id !== id);}-function addAcceptedEvent(- state: BuildState,- event: BuildEvent,- digest: Digest,-): BuildState["acceptedEvents"] {- return [...state.acceptedEvents, { id: event.id, digest }];-}--function inputRecords(state: BuildState, command: InvokeProducerCommand): TypedRecord[] {- return Object.values(command.inputs).map((id) => {- const record = state.records.find((item) => item.id === id);- invariant(record !== undefined, "MISSING_INPUT", `${command.step} input ${id} is missing`, id);- return record;- });-}-function normalizeStoredValue(value: StoredValue): StoredValue {if (value.kind === "inline") {return { kind: "inline", value: canonicalize(value.value) };@@ -77,7 +54,6 @@ function acceptProducerEvent(state: BuildState,command: InvokeProducerCommand,event: ProducerCompletedEvent,- acceptedEventDigest: Digest,): BuildState {const step = producerStep(state.plan, command.step);const stepState = state.steps.find((item) => item.id === step.id);@@ -86,8 +62,7 @@ function acceptProducerEvent(exactPortKeys(event.outputs, producer.outputs.map((port) => port.name), `${step.id}.outputs`);exactPortKeys(event.needs, producer.needs.map((port) => port.name), `${step.id}.needs`);- const inputs = inputRecords(state, command);- const outputDrafts = producer.outputs+ const outputs: TypedRecord[] = producer.outputs.filter((port) => step.outputs[port.name] !== undefined).map((port) => {const id = step.outputs[port.name];@@ -99,11 +74,10 @@ function acceptProducerEvent(id,type: port.type,value,- digest: recordDigest(port.type, value),};});- const needDrafts = producer.needs+ const needs: Need[] = producer.needs.filter((port) => step.needs[port.name] !== undefined).map((port) => {const binding = step.needs[port.name];@@ -120,40 +94,18 @@ function acceptProducerEvent(return {id: binding.id,...request,- requestDigest: needRequestDigest(request),};});-- const derivationDraft: Omit<Derivation, "id"> = {- step: step.id,- producer: step.producer,- inputs: inputs.map((record) => ({ id: record.id, digest: record.digest })),- outputs: outputDrafts.map(({ id, digest }) => ({ id, digest })),- needs: needDrafts.map((need) => ({ id: need.id, requestDigest: need.requestDigest })),- event: { id: event.id, digest: acceptedEventDigest },- };- const id = derivationId(derivationDraft);- const derivation: Derivation = { id, ...derivationDraft };- const outputs: TypedRecord[] = outputDrafts.map((output) => ({- id: output.id,- type: output.type,- value: output.value,- digest: output.digest,- origin: { kind: "derived", derivation: id },- }));outputs.forEach((record) => verifyRecordStructure(state.program.closure, record));- const needs: Need[] = needDrafts.map((need) => ({ ...need, requestedBy: id }));return {...state,records: [...state.records, ...outputs],needs: [...state.needs, ...needs],- derivations: [...state.derivations, derivation],steps: state.steps.map((item) =>- item.id === step.id ? { id: item.id, status: "complete", derivation: id } : item,+ item.id === step.id ? { id: item.id, status: "complete" } : item,),outstanding: withoutCommand(state, command.id),- acceptedEvents: addAcceptedEvent(state, event, acceptedEventDigest),};}@@ -161,7 +113,6 @@ function acceptNeedEvent(state: BuildState,command: FulfillNeedCommand,event: NeedFulfilledEvent,- acceptedEventDigest: Digest,): BuildState {const need = state.needs.find((item) => item.id === command.need.id);invariant(need !== undefined, "UNKNOWN_NEED", `unknown need ${command.need.id}`, command.need.id);@@ -171,51 +122,29 @@ function acceptNeedEvent(`${need.id} is already fulfilled`,need.id,);- invariant(- event.requestDigest === need.requestDigest,- "REQUEST_DIGEST_MISMATCH",- `${need.id} fulfillment is for another request`,- need.id,- );const value = normalizeStoredValue(event.value);- const outputDigest = recordDigest(need.returns, value);- const receiptDraft: Omit<Receipt, "id"> = {- need: need.id,- requestDigest: event.requestDigest,- fulfiller: event.fulfiller,- output: need.result,- outputDigest,- event: { id: event.id, digest: acceptedEventDigest },- };- const receipt: Receipt = { id: receiptId(receiptDraft), ...receiptDraft };const record: TypedRecord = {id: need.result,type: need.returns,value,- digest: outputDigest,- origin: { kind: "observed", receipt: receipt.id },};verifyRecordStructure(state.program.closure, record);return {...state,records: [...state.records, record],- receipts: [...state.receipts, receipt],outstanding: withoutCommand(state, command.id),- acceptedEvents: addAcceptedEvent(state, event, acceptedEventDigest),};}function acceptFailure(state: BuildState,- event: BuildEvent & { kind: "command-failed" },- acceptedEventDigest: Digest,+ event: CommandResult & { kind: "command-failed" },): BuildState {return {...state,status: "failed",outstanding: [],- acceptedEvents: addAcceptedEvent(state, event, acceptedEventDigest),diagnostics: [...state.diagnostics,{ code: event.code, message: event.message, subject: event.command },@@ -223,29 +152,16 @@ function acceptFailure(};}-function applyEvent(state: BuildState, event: BuildEvent): BuildState {- invariant(event.id.length > 0, "EMPTY_EVENT_ID", "event id is empty");- const acceptedEventDigest = eventDigest(event);- const accepted = state.acceptedEvents.find((item) => item.id === event.id);- if (accepted !== undefined) {- invariant(- accepted.digest === acceptedEventDigest,- "EVENT_ID_REUSED",- `${event.id} was already accepted with different content`,- event.id,- );- return state;- }-+function applyEvent(state: BuildState, event: CommandResult): BuildState {const command = state.outstanding.find((item) => item.id === event.command);invariant(command !== undefined, "UNKNOWN_COMMAND", `event references ${event.command}`, event.command);- if (event.kind === "command-failed") return acceptFailure(state, event, acceptedEventDigest);+ if (event.kind === "command-failed") return acceptFailure(state, event);if (command.kind === "invoke-producer" && event.kind === "producer-completed") {- return acceptProducerEvent(state, command, event, acceptedEventDigest);+ return acceptProducerEvent(state, command, event);}if (command.kind === "fulfill-need" && event.kind === "need-fulfilled") {- return acceptNeedEvent(state, command, event, acceptedEventDigest);+ return acceptNeedEvent(state, command, event);}throw new CoreError("EVENT_COMMAND_MISMATCH",@@ -275,7 +191,7 @@ function schedule(state: BuildState): BuildState {if (records.has(need.result)) continue;commands.push({kind: "fulfill-need",- id: commandId(state.id, "need", need.id),+ id: commandId("need", need.id),need,});}@@ -288,7 +204,7 @@ function schedule(state: BuildState): BuildState {}commands.push({kind: "invoke-producer",- id: commandId(state.id, "producer", step.id),+ id: commandId("producer", step.id),step: step.id,producer: step.producer,inputs: step.inputs,@@ -323,13 +239,6 @@ export function start(const plan: BuildPlan = compileBuild(program, graph, request);const state: BuildState = {format: "narratage.build@1",- id: digestOf({- closure: program.closure.digest,- semantic: program.semanticDigest,- graph: graph.id,- request: request.digest,- plan: plan.id,- }),program,graph,request,@@ -338,16 +247,181 @@ export function start(records: [...program.records, ...selectedProvidedRecords(program, graph, plan)],steps: plan.steps.map((step) => ({ id: step.id, status: "pending" })),needs: [],- receipts: [],- derivations: [],outstanding: [],- acceptedEvents: [],diagnostics: [],};return state;}-export function reduce(state: BuildState, event?: BuildEvent): BuildState {+export function reduce(state: BuildState, event?: CommandResult): BuildState {const next = event === undefined ? state : applyEvent(state, event);return schedule(next);}++function definitionContent(+ state: Pick<BuildState, "program" | "graph" | "request" | "plan">,+): Omit<BuildDefinition, "format"> {+ return {+ program: state.program,+ graph: state.graph,+ request: state.request,+ plan: state.plan,+ };+}++/** Compile one immutable finite Build definition. Runtime execution never changes it. */+export function defineBuild(+ program: LinkedProgram,+ graph: CompiledGraph,+ request: BuildRequest,+): BuildDefinition {+ const initial = start(program, graph, request);+ return {+ format: "narratage.build-definition@1",+ ...definitionContent(initial),+ };+}++/** Extract the immutable definition from a materialized compatibility view. */+export function buildDefinition(state: BuildState): BuildDefinition {+ return {+ format: "narratage.build-definition@1",+ ...definitionContent(state),+ };+}++function initialBuildView(definition: BuildDefinition): BuildState {+ return {+ format: "narratage.build@1",+ program: definition.program,+ graph: definition.graph,+ request: definition.request,+ plan: definition.plan,+ status: "active",+ records: [+ ...definition.program.records,+ ...selectedProvidedRecords(definition.program, definition.graph, definition.plan),+ ],+ steps: definition.plan.steps.map((step) => ({ id: step.id, status: "pending" })),+ needs: [],+ outstanding: [],+ diagnostics: [],+ };+}++/**+ * Admit one untrusted Command result and return the fixed Core Fact it establishes.+ * The returned next view is provisional until the Store durably appends `fact`.+ */+export function admitBuildResult(+ state: BuildState,+ event: CommandResult,+): { readonly fact: BuildFact; readonly state: BuildState } {+ const next = reduce(state, event);++ if (event.kind === "producer-completed") {+ const command = state.outstanding.find((item) => item.id === event.command);+ invariant(command?.kind === "invoke-producer", "EVENT_COMMAND_MISMATCH", event.command, event.command);+ const content = {+ format: "narratage.build-fact@1",+ kind: "producer-applied",+ command: event.command,+ step: command.step,+ records: next.records.slice(state.records.length),+ needs: next.needs.slice(state.needs.length),+ } as const;+ return { fact: content, state: next };+ }+ if (event.kind === "need-fulfilled") {+ const command = state.outstanding.find((item) => item.id === event.command);+ invariant(command?.kind === "fulfill-need", "EVENT_COMMAND_MISMATCH", event.command, event.command);+ const record = next.records.at(-1);+ invariant(record?.id === command.need.result, "NEED_RECORD_MISSING", command.need.id, command.need.id);+ const content = {+ format: "narratage.build-fact@1",+ kind: "need-applied",+ command: event.command,+ need: command.need.id,+ record,+ } as const;+ return { fact: content, state: next };+ }+ const diagnostic = next.diagnostics.at(-1);+ invariant(diagnostic?.subject === event.command, "FAILURE_DIAGNOSTIC_MISSING", event.command, event.command);+ const content = {+ format: "narratage.build-fact@1",+ kind: "command-failed",+ command: event.command,+ diagnostic,+ } as const;+ return { fact: content, state: next };+}++function verifyFactShape(fact: BuildFact): void {+ invariant(fact.format === "narratage.build-fact@1", "UNSUPPORTED_BUILD_FACT", fact.command, fact.command);+}++/** Reconstruct the materialized view from one immutable Definition and its accepted Facts. */+export function materializeBuild(+ definition: BuildDefinition,+ facts: readonly BuildFact[],+): BuildState {+ const initial = initialBuildView(definition);+ const records = [...initial.records];+ const steps = [...initial.steps];+ const needs: Need[] = [];+ const diagnostics: BuildState["diagnostics"][number][] = [];+ const stepIndexes = new Map(steps.map((step, index) => [step.id, index]));+ const recordIds = new Set(records.map((record) => record.id));+ const needIds = new Set<string>();+ let status: BuildState["status"] = "active";++ for (const fact of facts) {+ verifyFactShape(fact);+ invariant(status === "active", "FACT_AFTER_TERMINAL", fact.command, fact.command);++ if (fact.kind === "producer-applied") {+ const stepIndex = stepIndexes.get(fact.step);+ invariant(stepIndex !== undefined, "UNKNOWN_STEP", fact.step, fact.step);+ const step = steps[stepIndex];+ invariant(step?.status === "pending", "STEP_ALREADY_COMPLETE", fact.step, fact.step);+ const planned = producerStep(definition.plan, fact.step);+ invariant(+ Object.values(planned.inputs).every((record) => recordIds.has(record)),+ "FACT_INPUT_NOT_READY",+ fact.step,+ fact.step,+ );+ for (const record of fact.records) {+ invariant(!recordIds.has(record.id), "DUPLICATE_RECORD", record.id, record.id);+ recordIds.add(record.id);+ records.push(record);+ }+ for (const need of fact.needs) {+ invariant(!needIds.has(need.id), "DUPLICATE_NEED", need.id, need.id);+ needIds.add(need.id);+ needs.push(need);+ }+ steps[stepIndex] = { id: fact.step, status: "complete" };+ continue;+ }+ if (fact.kind === "need-applied") {+ invariant(needIds.has(fact.need), "UNKNOWN_NEED", fact.need, fact.need);+ invariant(!recordIds.has(fact.record.id), "NEED_ALREADY_FULFILLED", fact.need, fact.need);+ recordIds.add(fact.record.id);+ records.push(fact.record);+ continue;+ }+ diagnostics.push(fact.diagnostic);+ status = "failed";+ }++ return schedule({+ ...initial,+ status,+ records,+ steps,+ needs,+ diagnostics,+ });+}diff --git a/packages/core/src/slice.ts b/packages/core/src/slice.tsindex ff2816c1..ce41993b 100644--- a/packages/core/src/slice.ts+++ b/packages/core/src/slice.ts@@ -91,7 +91,6 @@ export function sliceExecution(return {program: slicedProgram,graph: sealCompiledGraph({- program: slicedProgram.semanticDigest,outputs: selectedOutputs,candidates: selectedCandidates,operations: selectedOperations,diff --git a/packages/core/src/verify.ts b/packages/core/src/verify.tsdeleted file mode 100644index 3b1e14cc..00000000--- a/packages/core/src/verify.ts+++ /dev/null@@ -1,324 +0,0 @@-import type {- BuildState,- CoreCommand,- Derivation,- Need,- Receipt,- TypedRecord,-} from "@narratage/protocol";--import { digestOf, isDigest } from "./canonical.js";-import { invariant } from "./error.js";-import { resolveProducer, verifyLinkedProgram, verifyRecordStructure } from "./link.js";-import { producerStep, selectedProvidedRecords, validatePlan } from "./plan.js";-import { commandId, derivationId, needRequestDigest, receiptId } from "./provenance.js";-import { sameCapability, sameType } from "./reference.js";--function uniqueIndex<T>(items: readonly T[], key: (item: T) => string, kind: string): ReadonlyMap<string, T> {- const seen = new Map<string, T>();- for (const item of items) {- const id = key(item);- invariant(!seen.has(id), "DUPLICATE_STATE_ID", `duplicate ${kind} ${id}`, id);- seen.set(id, item);- }- return seen;-}--type StateIndex = {- readonly records: ReadonlyMap<string, TypedRecord>;- readonly steps: ReadonlyMap<string, BuildState["steps"][number]>;- readonly needs: ReadonlyMap<string, Need>;- readonly receipts: ReadonlyMap<string, Receipt>;- readonly derivations: ReadonlyMap<string, Derivation>;- readonly acceptedEvents: ReadonlyMap<string, BuildState["acceptedEvents"][number]>;- readonly programRecords: ReadonlyMap<string, TypedRecord>;-};--function verifyDerivation(state: BuildState, index: StateIndex, derivation: Derivation): void {- const step = producerStep(state.plan, derivation.step);- const producer = resolveProducer(state.program.closure, derivation.producer);- invariant(- step.producer.module.name === derivation.producer.module.name &&- step.producer.module.version === derivation.producer.module.version &&- step.producer.name === derivation.producer.name,- "DERIVATION_PRODUCER_MISMATCH",- `${derivation.id} does not use its planned producer`,- derivation.id,- );- invariant(- JSON.stringify(derivation.inputs.map((item) => item.id).sort()) ===- JSON.stringify(Object.values(step.inputs).sort()),- "DERIVATION_INPUT_MISMATCH",- `${derivation.id} input records do not match its step`,- derivation.id,- );- invariant(- JSON.stringify(derivation.outputs.map((item) => item.id).sort()) ===- JSON.stringify(Object.values(step.outputs).sort()),- "DERIVATION_OUTPUT_MISMATCH",- `${derivation.id} output records do not match its step`,- derivation.id,- );- invariant(- JSON.stringify(derivation.needs.map((item) => item.id).sort()) ===- JSON.stringify(Object.values(step.needs).map((binding) => binding.id).sort()),- "DERIVATION_NEED_MISMATCH",- `${derivation.id} needs do not match its step`,- derivation.id,- );- for (const binding of [...derivation.inputs, ...derivation.outputs]) {- const record = index.records.get(binding.id);- invariant(record !== undefined, "DERIVATION_RECORD_MISSING", `${binding.id} is missing`, derivation.id);- invariant(record.digest === binding.digest, "DERIVATION_RECORD_DIGEST", `${binding.id} digest differs`, derivation.id);- }- for (const binding of derivation.needs) {- const need = index.needs.get(binding.id);- invariant(need !== undefined, "DERIVATION_NEED_MISSING", `${binding.id} is missing`, derivation.id);- invariant(- need.requestDigest === binding.requestDigest,- "DERIVATION_NEED_DIGEST",- `${binding.id} request digest differs`,- derivation.id,- );- }- const accepted = index.acceptedEvents.get(derivation.event.id);- invariant(- accepted?.digest === derivation.event.digest,- "DERIVATION_EVENT_MISMATCH",- `${derivation.id} event differs`,- derivation.id,- );- const { id: _id, ...content } = derivation;- invariant(- derivation.id === derivationId(content),- "DERIVATION_DIGEST_MISMATCH",- `${derivation.id} content does not match its identity`,- derivation.id,- );-}--function verifyReceipt(index: StateIndex, receipt: Receipt): void {- const need = index.needs.get(receipt.need);- invariant(need !== undefined, "RECEIPT_UNKNOWN_NEED", `${receipt.id} references an unknown need`);- invariant(receipt.fulfiller.length > 0, "EMPTY_FULFILLER", `${receipt.id} fulfiller is empty`);- invariant(- receipt.requestDigest === need.requestDigest,- "REQUEST_DIGEST_MISMATCH",- `${receipt.id} request digest does not match`,- );- const record = index.records.get(receipt.output);- invariant(record !== undefined, "RECEIPT_OUTPUT_MISSING", `${receipt.id} output is missing`);- invariant(receipt.output === need.result, "RECEIPT_OUTPUT_BINDING", `${receipt.id} output is not its Need result`);- invariant(sameType(record.type, need.returns), "RECEIPT_OUTPUT_TYPE", `${receipt.id} output type differs`);- invariant(record.digest === receipt.outputDigest, "RECEIPT_OUTPUT_MISMATCH", `${receipt.id} output differs`);- invariant(- record.origin.kind === "observed" && record.origin.receipt === receipt.id,- "RECEIPT_ORIGIN_MISMATCH",- `${receipt.id} output has another origin`,- );- invariant(- index.acceptedEvents.get(receipt.event.id)?.digest === receipt.event.digest,- "RECEIPT_EVENT_MISMATCH",- `${receipt.id} accepted event differs`,- );- const { id: _id, ...content } = receipt;- invariant(- receipt.id === receiptId(content),- "RECEIPT_DIGEST_MISMATCH",- `${receipt.id} content does not match its identity`,- );-}--function verifyOutstanding(state: BuildState, index: StateIndex, command: CoreCommand): void {- if (command.kind === "invoke-producer") {- const step = index.steps.get(command.step);- invariant(step?.status === "pending", "OUTSTANDING_STEP_COMPLETE", `${command.step} is complete`);- const planned = producerStep(state.plan, command.step);- const expected: CoreCommand = {- kind: "invoke-producer",- id: commandId(state.id, "producer", planned.id),- step: planned.id,- producer: planned.producer,- inputs: planned.inputs,- };- invariant(digestOf(command) === digestOf(expected), "COMMAND_PRODUCER_MISMATCH", command.id);- return;- }- const need = index.needs.get(command.need.id);- invariant(need !== undefined, "OUTSTANDING_NEED_UNKNOWN", `${command.need.id} is unknown`);- invariant(index.records.get(need.result) === undefined, "OUTSTANDING_NEED_COMPLETE", `${need.id} is fulfilled`);- const expected: CoreCommand = {- kind: "fulfill-need",- id: commandId(state.id, "need", need.id),- need,- };- invariant(digestOf(command) === digestOf(expected), "COMMAND_NEED_MISMATCH", command.id);-}--export function verifyBuildState(state: BuildState): void {- invariant(state.format === "narratage.build@1", "UNSUPPORTED_BUILD", "unsupported build state format");- invariant(- state.status === "active" || state.status === "complete" || state.status === "failed",- "INVALID_BUILD_STATUS",- "build status is invalid",- );- invariant(isDigest(state.id), "INVALID_DIGEST", "build id is invalid");-- verifyLinkedProgram(state.program);- invariant(- state.id ===- digestOf({- closure: state.program.closure.digest,- semantic: state.program.semanticDigest,- graph: state.graph.id,- request: state.request.digest,- plan: state.plan.id,- }),- "BUILD_ID_MISMATCH",- "build id does not match its program and plan",- );- validatePlan(state.program, state.graph, state.request, state.plan);-- const index: StateIndex = {- records: uniqueIndex(state.records, (item) => item.id, "record"),- steps: uniqueIndex(state.steps, (item) => item.id, "step state"),- needs: uniqueIndex(state.needs, (item) => item.id, "need"),- receipts: uniqueIndex(state.receipts, (item) => item.id, "receipt"),- derivations: uniqueIndex(state.derivations, (item) => item.id, "derivation"),- acceptedEvents: uniqueIndex(state.acceptedEvents, (item) => item.id, "event"),- programRecords: new Map(state.program.records.map((item) => [item.id, item])),- };- uniqueIndex(state.outstanding, (item) => item.id, "command");-- invariant(- JSON.stringify(state.steps.map((item) => item.id).sort()) ===- JSON.stringify(state.plan.steps.map((item) => item.id).sort()),- "STEP_STATE_MISMATCH",- "build step state does not match the plan",- );-- for (const record of state.records) verifyRecordStructure(state.program.closure, record);- for (const authored of state.program.records) {- const record = index.records.get(authored.id);- invariant(record?.digest === authored.digest, "AUTHORED_RECORD_CHANGED", `${authored.id} changed`);- }- const providedRecords = selectedProvidedRecords(state.program, state.graph, state.plan);- const providedById = new Map(providedRecords.map((item) => [item.id, item]));- const selectedRecords = new Set(state.plan.selections.map((selection) => selection.record));- for (const provided of providedRecords) {- const record = index.records.get(provided.id);- invariant(- record !== undefined && digestOf(record) === digestOf(provided),- "PROVIDED_RECORD_CHANGED",- `${provided.id} differs from its compiled Provided Value`,- provided.id,- );- }-- for (const record of state.records) {- if (record.origin.kind === "authored") {- invariant(- index.programRecords.get(record.id)?.digest === record.digest,- "INJECTED_AUTHORED_RECORD",- `${record.id} was not present in the linked author program`,- );- } else if (record.origin.kind === "derived") {- const origin = record.origin;- const derivation = index.derivations.get(origin.derivation);- invariant(- derivation !== undefined,- "DERIVED_ORIGIN_MISMATCH",- `${record.id} has unknown derivation ${origin.derivation}`,- );- invariant(- derivation.outputs.some((item) => item.id === record.id && item.digest === record.digest),- "DERIVED_ORIGIN_MISMATCH",- `${record.id} is not an output of ${origin.derivation}`,- );- const inputs = derivation.inputs.map((binding) => index.records.get(binding.id));- invariant(inputs.every((item) => item !== undefined), "DERIVATION_INPUT_MISSING", derivation.id);- } else if (record.origin.kind === "observed") {- const origin = record.origin;- const receipt = index.receipts.get(origin.receipt);- invariant(receipt?.output === record.id, "OBSERVED_ORIGIN_MISMATCH", `${record.id} has no receipt`);- } else {- const origin = record.origin;- const expected = providedById.get(record.id);- invariant(- expected !== undefined && digestOf(expected) === digestOf(record),- "INJECTED_PROVIDED_RECORD",- `${record.id} was not supplied by this BuildRequest`,- record.id,- );- invariant(- origin.kind === "provided"- && selectedRecords.has(record.id),- "PROVIDED_ORIGIN_MISMATCH",- `${record.id} is not bound to its selected Candidate`,- record.id,- );- }- }-- for (const need of state.needs) {- invariant(- need.requestDigest === needRequestDigest(need),- "NEED_DIGEST_MISMATCH",- `${need.id} digest differs`,- );- const derivation = index.derivations.get(need.requestedBy);- invariant(- derivation?.needs.some((item) => item.id === need.id && item.requestDigest === need.requestDigest),- "NEED_ORIGIN_MISMATCH",- `${need.id} has no derivation`,- );- const step = derivation === undefined ? undefined : producerStep(state.plan, derivation.step);- const producer = step === undefined ? undefined : resolveProducer(state.program.closure, step.producer);- const plannedPort = step === undefined- ? undefined- : Object.entries(step.needs).find(([, binding]) => binding.id === need.id);- const declaration = plannedPort === undefined- ? undefined- : producer?.needs.find((port) => port.name === plannedPort[0]);- invariant(- declaration !== undefined- && sameCapability(need.capability, declaration.capability)- && sameType(need.returns, declaration.returns)- && plannedPort?.[1].result === need.result,- "NEED_PLAN_BINDING_MISMATCH",- `${need.id} differs from its locked Producer Need port`,- need.id,- );- }- for (const receipt of state.receipts) verifyReceipt(index, receipt);- for (const derivation of state.derivations) verifyDerivation(state, index, derivation);-- for (const stepState of state.steps) {- if (stepState.status === "complete") {- invariant(stepState.derivation !== undefined, "COMPLETE_STEP_NO_DERIVATION", stepState.id);- invariant(- index.derivations.get(stepState.derivation)?.step === stepState.id,- "STEP_DERIVATION_MISMATCH",- stepState.id,- );- }- }-- for (const command of state.outstanding) verifyOutstanding(state, index, command);- for (const event of state.acceptedEvents) {- invariant(event.id.length > 0, "EMPTY_EVENT_ID", "accepted event id is empty");- invariant(isDigest(event.digest), "INVALID_DIGEST", `${event.id} digest is invalid`);- }-- if (state.status === "complete") {- invariant(state.outstanding.length === 0, "COMPLETE_WITH_COMMANDS", "complete build has commands");- for (const goal of state.plan.goals) {- const record = index.records.get(goal.record);- invariant(record !== undefined, "COMPLETE_GOAL_MISSING", `${goal.record} is missing`);- invariant(sameType(record.type, goal.type), "COMPLETE_GOAL_TYPE_MISMATCH", goal.record);- }- }- if (state.status === "failed") {- invariant(state.diagnostics.length > 0, "FAILED_WITHOUT_DIAGNOSTIC", "failed build has no diagnostic");- }-}diff --git a/packages/core/test/core.test.ts b/packages/core/test/core.test.tsindex e99c05d7..719d5f85 100644--- a/packages/core/test/core.test.ts+++ b/packages/core/test/core.test.ts@@ -1,17 +1,15 @@import assert from "node:assert/strict";import test from "node:test";-import {+ BuildMachine,CoreError,- canonicalize,- digestOf,+ buildDefinition,reduce,validateStoredValue,- validatePlan,- verifyBuildState,} from "@narratage/core";import type {- BuildEvent,+ CommandResult,+ BuildFact,BuildState,FulfillNeedCommand,InvokeProducerCommand,@@ -30,13 +28,11 @@ function onlyProducer(state: BuildState): { state: BuildState; command: InvokePrfunction producerEvent(command: InvokeProducerCommand,- id: string,outputs: Record<string, { readonly kind: "inline"; readonly value: string | { readonly text: string } }>,needs: Record<string, string | { readonly prompt: string }> = {},-): BuildEvent {+): CommandResult {return {kind: "producer-completed",- id,command: command.id,outputs,needs,@@ -50,7 +46,7 @@ function reachNeed(initial = createGreetingBuild()): {let current = onlyProducer(initial);let state = reduce(current.state,- producerEvent(current.command, "event:prompt", {+ producerEvent(current.command, {prompt: { kind: "inline", value: "Greet Ada" },}),);@@ -60,7 +56,7 @@ function reachNeed(initial = createGreetingBuild()): {assert.ok(request);state = reduce(state,- producerEvent(request, "event:request", {}, { generation: { prompt: "Greet Ada" } }),+ producerEvent(request, {}, { generation: { prompt: "Greet Ada" } }),);const command = state.outstanding.find((item): item is FulfillNeedCommand => item.kind === "fulfill-need",@@ -69,15 +65,12 @@ function reachNeed(initial = createGreetingBuild()): {return { state, command };}-test("Core executes its derived finite plan through Need, Receipt and completion", () => {+test("Core executes a finite plan through an external Need and completion", () => {let current = reachNeed();let state = reduce(current.state, {kind: "need-fulfilled",- id: "event:fulfill",command: current.command.id,value: { kind: "inline", value: "Hello, Ada!" },- requestDigest: current.command.need.requestDigest,- fulfiller: "test:greeting",});const assemble = state.outstanding.find((command): command is InvokeProducerCommand => command.kind === "invoke-producer",@@ -85,38 +78,17 @@ test("Core executes its derived finite plan through Need, Receipt and completionassert.ok(assemble);state = reduce(state,- producerEvent(assemble, "event:assemble", {+ producerEvent(assemble, {document: { kind: "inline", value: { text: "Hello, Ada!" } },}),);assert.equal(state.status, "complete");assert.deepEqual(state.outstanding, []);- assert.equal(state.derivations.length, 3);- assert.equal(state.receipts.length, 1);- assert.equal(state.records.at(-1)?.origin.kind, "derived");- verifyBuildState(state);-});--test("accepted events are idempotent and conflicting reuse is rejected", () => {- const initial = onlyProducer(createGreetingBuild());- const event = producerEvent(initial.command, "event:stable", {- prompt: { kind: "inline", value: "Greet Ada" },+ assert.deepEqual(state.records.at(-1)?.value, {+ kind: "inline",+ value: { text: "Hello, Ada!" },});- const accepted = reduce(initial.state, event);- const replayed = reduce(accepted, event);- assert.deepEqual(replayed, accepted);-- assert.throws(- () =>- reduce(- accepted,- producerEvent(initial.command, "event:stable", {- prompt: { kind: "inline", value: "Different" },- }),- ),- (error: unknown) => error instanceof CoreError && error.code === "EVENT_ID_REUSED",- );});test("oneOf literal discrimination preserves exact-one semantics", () => {@@ -148,99 +120,34 @@ test("oneOf literal discrimination preserves exact-one semantics", () => {);});-test("serialized BuildState survives a JSON round trip", () => {- const current = reachNeed();- const restored = JSON.parse(JSON.stringify(current.state)) as BuildState;- verifyBuildState(restored);- assert.deepEqual(reduce(restored).outstanding, reduce(current.state).outstanding);-});--test("a recomputed Need cannot change the capability locked by its Producer port", () => {- const current = reachNeed();- const tampered = structuredClone(current.state);- const need = tampered.needs[0];- assert.ok(need);- (need as { capability: unknown }).capability = {- module: need.capability.module,- name: "different-operation",- };- const requestDigest = digestOf({- capability: need.capability,- returns: need.returns,- constraints: need.constraints,- result: need.result,- });- (need as { requestDigest: string }).requestDigest = requestDigest;-- const derivation = tampered.derivations.find((item) => item.id === need.requestedBy);- assert.ok(derivation);- (derivation as { needs: unknown }).needs = [{ id: need.id, requestDigest }];- const { id: _oldId, ...derivationContent } = derivation;- const nextId = `derivation:${digestOf(derivationContent)}`;- (derivation as { id: string }).id = nextId;- (need as { requestedBy: string }).requestedBy = nextId;- const step = tampered.steps.find((item) => item.derivation === _oldId);- assert.ok(step);- (step as { derivation: string }).derivation = nextId;-- assert.throws(- () => verifyBuildState(tampered),- (error: unknown) => error instanceof CoreError && error.code === "NEED_PLAN_BINDING_MISMATCH",- );-});+test("Build Definition plus admitted Facts restores the same next Command", () => {+ const definition = buildDefinition(createGreetingBuild());+ const machine = new BuildMachine(definition);+ const facts: BuildFact[] = [];+ const prompt = machine.commands()[0];+ assert.ok(prompt?.kind === "invoke-producer");+ const promptFact = machine.evaluate(producerEvent(prompt, {+ prompt: { kind: "inline", value: "Greet Ada" },+ }));+ assert.ok(promptFact);+ facts.push(promptFact);+ machine.commit();-test("authored values cannot be changed inside a resumed BuildState", () => {- const tampered = structuredClone(createGreetingBuild()) as BuildState;- const authored = tampered.records[0];- assert.ok(authored);- (authored as { value: unknown }).value = { kind: "inline", value: { name: "Mallory" } };- assert.throws(- () => verifyBuildState(tampered),- (error: unknown) =>- error instanceof CoreError &&- ["SEMANTIC_DIGEST_MISMATCH", "RECORD_DIGEST_MISMATCH"].includes(error.code),+ const request = machine.commands().find(+ (command): command is InvokeProducerCommand => command.kind === "invoke-producer",);-});--test("a caller cannot replace Core's graph-derived plan", () => {- const state = createGreetingBuild();- const plan = {- ...state.plan,- steps: [- ...state.plan.steps,- {- id: "unused",- producer: producers.makePrompt,- inputs: { intent: "intent:root" },- outputs: { prompt: "prompt:unused" },- needs: {},- },- ],- };- assert.throws(- () => validatePlan(state.program, state.graph, state.request, plan),- (error: unknown) => error instanceof CoreError && error.code === "PLAN_NOT_DERIVED",+ assert.ok(request);+ const requestFact = machine.evaluate(+ producerEvent(request, {}, { generation: { prompt: "Greet Ada" } }),);- const tampered = { ...state, plan };- assert.throws(() => verifyBuildState(tampered), CoreError);-});+ assert.ok(requestFact);+ facts.push(requestFact);+ machine.commit();-test("canonical values reject accessors and preserve hostile-looking keys as data", () => {- const source: Record<string, unknown> = {};- Object.defineProperty(source, "__proto__", {- value: "data",- enumerable: true,- });- const canonical = canonicalize(source);- assert.equal(Object.hasOwn(canonical as object, "__proto__"), true);- assert.equal((canonical as Record<string, unknown>).__proto__, "data");-- const accessor = Object.defineProperty({}, "secret", {- enumerable: true,- get: () => "must not execute",- });- assert.throws(- () => canonicalize(accessor),- (error: unknown) => error instanceof CoreError && error.code === "NON_CANONICAL_ACCESSOR",+ const restored = new BuildMachine(+ JSON.parse(JSON.stringify(definition)) as typeof definition,+ JSON.parse(JSON.stringify(facts)) as BuildFact[],);+ assert.deepEqual(restored.commands(), machine.commands());+ assert.equal(restored.commands()[0]?.kind, "fulfill-need");});diff --git a/packages/core/test/demand.test.ts b/packages/core/test/demand.test.tsindex e8d18431..c7466287 100644--- a/packages/core/test/demand.test.ts+++ b/packages/core/test/demand.test.ts@@ -1,16 +1,13 @@import assert from "node:assert/strict";import test from "node:test";-import {createResolvedClosure,- digestOf,link,reduce,sealBuildRequest,sealCompiledGraph,sealRecord,start,- verifyBuildState,} from "@narratage/core";import type {BuildRequest,@@ -62,10 +59,6 @@ const producers = {};const seedanceCapability = { module: moduleRef, name: "seedance-media" } as const;-const implementation = (name: string) => ({- digest: digestOf(`${name}@1`),-});-const manifest: ModuleManifest = {format: "narratage.module@1",name: moduleRef.name,@@ -231,19 +224,16 @@ function createProgram(): LinkedProgram {id: "head:root",type: types.head,value: { kind: "inline", value: "reference" },- origin: { kind: "authored" },}),sealRecord({id: "duration:root",type: types.duration,value: { kind: "inline", value: 3 },- origin: { kind: "authored" },}),sealRecord({id: "duration:other",type: types.duration,value: { kind: "inline", value: 9 },- origin: { kind: "authored" },}),];return link(closure, authored);@@ -251,7 +241,6 @@ function createProgram(): LinkedProgram {function createImageGraph(program: LinkedProgram): CompiledGraph {return sealCompiledGraph({- program: program.semanticDigest,outputs: [output("image1", types.image, "p1"),output("image2", types.image, "p2"),@@ -292,7 +281,6 @@ function request(targets: readonly string[],): BuildRequest {return sealBuildRequest({- graph: graph.id,targets: targets.map((outputId) => ({ output: outputId })),});}@@ -300,7 +288,6 @@ function request(function selectCandidates(graph: CompiledGraph, satisfactions: readonly Satisfaction[]): CompiledGraph {const selected = new Map(satisfactions.map((item) => [item.output, item.candidate]));return sealCompiledGraph({- program: graph.program,outputs: graph.outputs.map((item) => ({ ...item, primary: selected.get(item.id) ?? item.primary })),candidates: graph.candidates,operations: graph.operations,@@ -353,13 +340,10 @@ test("an ordinary aggregator intentionally demands every selected image", () =>])), ["collect", "p3"]);});-test("multiple Targets share Operations once and target order is not semantic", () => {+test("multiple Targets share Operations once", () => {const first = fixture(["image3", "image2"]);- const second = fixture(["image2", "image3"]);assert.deepEqual(stepIds(first), ["p1", "p2", "p3"]);assert.equal(new Set(first.plan.steps.map((step) => step.id)).size, first.plan.steps.length);- assert.equal(first.request.digest, second.request.digest);- assert.equal(first.plan.id, second.plan.id);});test("the selected Candidate alone determines demanded upstream inputs", () => {@@ -371,7 +355,7 @@ test("an Existing Value is a normal Candidate root and prevents the paid Need frconst state = fixture(["media"], [choose("media", "existing-media")]);const transition = reduce(state);assert.deepEqual(stepIds(state), []);- assert.deepEqual(state.records.filter((record) => record.origin.kind === "provided").map((record) => record.id), ["provided:media"]);+ assert.equal(state.records.some((record) => record.id === "provided:media"), true);assert.equal(transition.status, "complete");assert.equal(transition.needs.length, 0);assert.deepEqual(transition.outstanding, []);@@ -384,7 +368,6 @@ test("Provided state survives JSON round-trip and regenerates identical ready Co]);const scheduled = reduce(state);const restored = JSON.parse(JSON.stringify({ ...scheduled, outstanding: [] })) as BuildState;- verifyBuildState(restored);assert.deepEqual(reduce(restored).outstanding, scheduled.outstanding);});@@ -392,7 +375,6 @@ function createCaseGGraph(program: LinkedProgram, roots: "value" | "operation"):const a1Id = roots === "value" ? "a1-value" : "a1-operation";const a2Id = roots === "value" ? "a2-value" : "a2-operation";return sealCompiledGraph({- program: program.semanticDigest,outputs: [output("a.A", types.a, a1Id),output("a.B", types.b, a2Id),@@ -426,7 +408,8 @@ test("Case G: two single-output full-input Candidates share both upstream Valuesconst graph = createCaseGGraph(program, "value");const state = start(program, graph, request(graph, ["c.result"]));assert.deepEqual(stepIds(state), ["b1", "b2", "c"]);- assert.deepEqual(state.records.filter((record) => record.origin.kind === "provided").map((record) => record.id), ["provided:A", "provided:B"]);+ assert.equal(state.records.some((record) => record.id === "provided:A"), true);+ assert.equal(state.records.some((record) => record.id === "provided:B"), true);const b1 = state.plan.steps.find((step) => step.id === "b1");const b2 = state.plan.steps.find((step) => step.id === "b2");assert.deepEqual(b1?.inputs, { A: "provided:A", B: "provided:B" });@@ -453,7 +436,6 @@ function createOperationIdentityGraph(mode: "shared" | "distinct",): CompiledGraph {return sealCompiledGraph({- program: program.semanticDigest,outputs: [output("left", types.image, "left-candidate"),output("right", types.image, "right-candidate"),@@ -551,7 +533,6 @@ function createProductReplacementGraph(const altCProduct = "b.alt.product.c";const altDProduct = alternate === "shared" ? altCProduct : "b.alt.product.d";return sealCompiledGraph({- program: program.semanticDigest,outputs: [output("a.A", types.a, "a.A.primary"),output("a.B", types.b, "a.B.primary"),diff --git a/packages/core/test/greeting-fixture.ts b/packages/core/test/greeting-fixture.tsindex 59b2c0e9..3346c2d9 100644--- a/packages/core/test/greeting-fixture.ts+++ b/packages/core/test/greeting-fixture.ts@@ -1,6 +1,5 @@import {createResolvedClosure,- digestOf,link,sealBuildRequest,sealCompiledGraph,@@ -16,7 +15,6 @@ import type {ProducerRef,TypeRef,} from "@narratage/protocol";-export const moduleRef = { name: "example.greeting", version: "0.0.0" } as const;export const types = {@@ -87,7 +85,6 @@ export const manifest: ModuleManifest = {export function greetingGraph(program: LinkedProgram, includeSide = false): CompiledGraph {return sealCompiledGraph({- program: program.semanticDigest,outputs: [{id: "prompt",@@ -173,13 +170,11 @@ export function createGreetingBuild(options?: {id: "intent:root",type: types.intent,value: { kind: "inline", value: { name: "Ada" } },- origin: { kind: "authored" },});const program = link(closure, [authored]);const sourceGraph = greetingGraph(program, options?.includeSideTarget ?? false);const graph = options?.generationRealization === "placeholder"? sealCompiledGraph({- program: sourceGraph.program,outputs: sourceGraph.outputs.map((item) => item.id === "generated"? { ...item, primary: "placeholder-text" }: item),@@ -188,7 +183,6 @@ export function createGreetingBuild(options?: {}): sourceGraph;const request = sealBuildRequest({- graph: graph.id,targets: [...(options?.includeSideTarget ? [{ output: "side-document" }] : []), {output: "document",}],diff --git a/packages/credential-store-env/src/index.ts b/packages/credential-store-env/src/index.tsindex a0827c3d..0bbe9698 100644--- a/packages/credential-store-env/src/index.ts+++ b/packages/credential-store-env/src/index.ts@@ -1,4 +1,3 @@-import { digestOf } from "@narratage/protocol";import { defineRuntimeInfrastructurePackage } from "@narratage/runtime";import type {CredentialRef,diff --git a/packages/credential-store-keychain/src/store.ts b/packages/credential-store-keychain/src/store.tsindex 1e87ba22..009c00ea 100644--- a/packages/credential-store-keychain/src/store.ts+++ b/packages/credential-store-keychain/src/store.ts@@ -1,6 +1,5 @@import { execFile } from "node:child_process";-import { digestOf } from "@narratage/protocol";import { defineRuntimeInfrastructurePackage, verifyCredentialRef } from "@narratage/runtime";import type {CredentialRef,diff --git a/packages/credential-store-keychain/test/store.test.ts b/packages/credential-store-keychain/test/store.test.tsindex 47ed6ffc..1c081c0e 100644--- a/packages/credential-store-keychain/test/store.test.ts+++ b/packages/credential-store-keychain/test/store.test.ts@@ -32,15 +32,6 @@ test("a malformed reference is refused before anything is looked up", async () =assert.equal(asked, 0);});-test("the configured service is part of the instance's identity", () => {- const configured = createKeychainCredentialStorePackage({ instance: "credentials.team", service: "acme" });- assert.equal(configured.parts[0]?.instance.id, "credentials.team.store");- // Configuration reaches identity as a digest and is not carried in the- // Closure, so the proof that the service name counts is that a different- // name is a different instance.- const other = createKeychainCredentialStorePackage({ instance: "credentials.team", service: "other" });-});-test("writable facet stores and removes only keychain-owned references", async () => {const values = new Map<string, string>();const store = new KeychainCredentialStore({diff --git a/packages/deck-track/src/manifest.ts b/packages/deck-track/src/manifest.tsindex 65f1d172..47eac3f9 100644--- a/packages/deck-track/src/manifest.ts+++ b/packages/deck-track/src/manifest.ts@@ -16,7 +16,6 @@ import {} from "@narratage/media-track";import { narrativeDependency, narrativeTypes } from "@narratage/narrative";import { programSpaceDependency, programSpaceTypes } from "@narratage/program-space";-import { digestOf } from "@narratage/protocol";import type { ModuleManifest, ProducerRef, TypeRef, ValueSchema } from "@narratage/protocol";import { semanticMapDependency, semanticMapTypes } from "@narratage/semantic-map";import { spatialDependency, spatialFrameSchema, spatialTypes } from "@narratage/spatial";@@ -107,8 +106,6 @@ export const depthStackProgramSchema: ValueSchema = object({terminalFrame: { schema: positiveInteger }, frame: { schema: spatialFrameSchema }, spec: { schema: depthStackSpecSchema },cards: { schema: { kind: "array", minItems: 1, items: card } },});-const registered = (digest: ReturnType<typeof digestOf>) => ({ digest });-const validator = (digest: ReturnType<typeof digestOf>) => ({ implementation: registered(digest) });const finalizeInputs = [{ name: "set", type: depthStackTypes.cardSet }, { name: "header", type: depthStackTypes.header },{ name: "frame", type: spatialTypes.frame }, { name: "spec", type: depthStackTypes.spec },diff --git a/packages/deck-track/src/program.ts b/packages/deck-track/src/program.tsindex 429bcadc..ec3d0d8a 100644--- a/packages/deck-track/src/program.ts+++ b/packages/deck-track/src/program.ts@@ -12,7 +12,7 @@ import {programSpaceFrameCount,} from "@narratage/program-space";import type { ProgramSpace } from "@narratage/program-space";-import { canonicalize, digestOf } from "@narratage/protocol";+import { canonicalize } from "@narratage/protocol";import { verifyText } from "@narratage/text";import type { Text } from "@narratage/text";import type { CompleteSemanticMap } from "@narratage/semantic-map";diff --git a/packages/deck-track/test/deck-track.test.ts b/packages/deck-track/test/deck-track.test.tsindex 00214395..2459945e 100644--- a/packages/deck-track/test/deck-track.test.ts+++ b/packages/deck-track/test/deck-track.test.ts@@ -1,6 +1,7 @@import assert from "node:assert/strict";import test from "node:test";import { videoContractManifests } from "../../../test/support/video-domain.js";+import { fixtureDigest } from "../../../test/fixture-digest.js";import { compositionDependency, compositionTypes } from "@narratage/composition";import type { VisualElement, VisualTimedSampling } from "@narratage/composition";@@ -36,7 +37,6 @@ import { mediaPipelineManifest } from "@narratage/media-pipeline";import type { MediaLayerSet } from "@narratage/media-track";import { narrativeTypes } from "@narratage/narrative";import { programSpaceTypes, sealProgramSpace } from "@narratage/program-space";-import { digestOf } from "@narratage/protocol";import type { ModuleManifest } from "@narratage/protocol";import { semanticMapTypes } from "@narratage/semantic-map";import { sealCanvasSpace, sealSpatialFrame, spatialTypes } from "@narratage/spatial";@@ -73,13 +73,13 @@ const frame = sealSpatialFrame({const image = (name: string) => ({kind: "blob" as const,- digest: digestOf(`deck-image:${name}`),+ digest: fixtureDigest(`deck-image:${name}`),size: 16,mediaType: "image/png",});const video = (name: string) => ({kind: "blob" as const,- digest: digestOf(`deck-video:${name}`),+ digest: fixtureDigest(`deck-video:${name}`),size: 32,mediaType: "video/mp4",});@@ -337,7 +337,7 @@ test("continue uses one explicit loop-start clock and past hide removes the reta});const font: FontArtifactRef = {- sources: [{ artifact: { kind: "blob", digest: digestOf("deck-font"), size: 64, mediaType: "font/woff2" } }],+ sources: [{ artifact: { kind: "blob", digest: fixtureDigest("deck-font"), size: 64, mediaType: "font/woff2" } }],weight: 700,style: "normal",};diff --git a/packages/driver-node/README.md b/packages/driver-node/README.mdindex 4bf08e9f..5670720b 100644--- a/packages/driver-node/README.md+++ b/packages/driver-node/README.md@@ -28,19 +28,19 @@ construction remain outside the execution Driver.- Endpoint credentials are resolved only for the slots declared by that endpoint, immediatelybefore `start/resume/cancel`; secret bytes never become OperationStore or Core state.- A pending Endpoint may publish `wakeAt`. Retryable terminal failure creates a new attempt and- Operation id under the endpoint's finite retry policy; cancellation becomes an explicit terminal- failure that Core accepts through its ordinary command-failed Event.+ Operation id under the endpoint's finite retry policy. Build cancellation stops local polling and+ makes one best-effort call to the Endpoint's optional `cancel()` method.- Preview, fallback and reuse are graph-level Candidates selected before Core planning, not Endpointmodes. A reused value is an Existing-Value Candidate; “Pin” is only a possible host UI word forauthoring that explicit selection.- Endpoint identity becomes the Receipt fulfiller; handlers return only value and operationalmetadata and cannot choose that identity themselves.-- `TypeValidatorRegistry` is a separate exact-Type registry. Before an Event exists, the Driver+- `TypeValidatorRegistry` is a separate exact-Type registry. Before a Command result exists, the Driverexecutes the selected Type owner's validator. Producer and Endpoint handlers cannot self-assertvalidation.-Build state is serializable. Missing Endpoints, generation latency and transient endpoint errors pause a-build without replaying completed Producers.+Missing Endpoints, generation latency and transient endpoint errors pause a Build without replaying+completed Producers. Durable recovery reads the immutable Build Definition and accepted Core Facts.`start()` and `resume()` receive the same stable Operation id. `resume(undefined)` is intentional:it covers a stop after submission intent was persisted but before a remote job checkpoint was saved.@@ -56,8 +56,8 @@ Callers using a recoverable Endpoint through the serial path must pass a stablebecause two identical requests may still be distinct Builds. The Scheduler always supplies itsdeclared Build id.-Outstanding Commands are not trusted persistence. Serialization drops them and Core regenerates-the exact commands from verified facts on resume, before any Handler can run. The Registry never+Outstanding Commands are not persisted. Core regenerates them from Definition plus Facts before+any Handler can run. The Registry neverturns a generic speaker request into a model choice: Seedance, WhisperX or another external methodmust already be present as a specific Need in the locked BuildPlan.The BuildPlan itself is derived and sealed by Core from the locked CompiledGraph and BuildRequest.diff --git a/packages/driver-node/src/driver.ts b/packages/driver-node/src/driver.tsindex 5f6f2ad9..8cdd82ba 100644--- a/packages/driver-node/src/driver.ts+++ b/packages/driver-node/src/driver.ts@@ -1,11 +1,12 @@-import { digestOf, reduce, resolveProducer } from "@narratage/core";+import { randomUUID } from "node:crypto";++import { reduce, resolveProducer } from "@narratage/core";import type { ProducerHandlerResult } from "@narratage/component-kit";-import type { EndpointFulfillment } from "@narratage/endpoint-kit";+import type { EndpointFulfillment, EndpointOutcome } from "@narratage/endpoint-kit";import type {- BuildEvent,BuildState,+ CommandResult,CoreCommand,- Digest,FulfillNeedCommand,InvokeProducerCommand,TypedRecord,@@ -16,17 +17,14 @@ import {} from "@narratage/validation";import type { TypeValidatorRegistryLike } from "@narratage/validation";import {- operationCancellationRequestId,sealOperationIdentity,- verifyOperationSnapshot,} from "@narratage/runtime";import type {- OperationCancellationControl,- OperationIdentity,CredentialStore,CredentialValue,OperationSnapshot,OperationStore,+ OperationUpdate,RuntimeExecutionContext,RuntimeExecutionResult,RuntimePreparation,@@ -198,7 +196,7 @@ export class NodeDriver {};}const registration = resolution.registration;- if (registration.kind === "recoverable" && this.operations === undefined) {+ if (registration.kind === "asynchronous" && this.operations === undefined) {return {blocked: {command: command.id,@@ -226,21 +224,18 @@ export class NodeDriver {state: BuildState,executable: Extract<Executable, { readonly endpointId: string }>,result: EndpointFulfillment,- ): Promise<BuildEvent> {+ ): Promise<CommandResult> {await validateValue(state.program.closure,executable.command.need.returns,result.value,this.validators,);- const content = {+ return {kind: "need-fulfilled",command: executable.command.id,value: result.value,- requestDigest: executable.command.need.requestDigest,- fulfiller: executable.endpointId,} as const;- return { ...content, id: `event:${digestOf(content)}` };}async #completedOperation(@@ -248,9 +243,7 @@ export class NodeDriver {executable: Extract<Executable, { readonly endpointId: string }>,snapshot: OperationSnapshot,expectedOperation: string,- maxAttempts: number,): Promise<RuntimeExecutionResult> {- verifyOperationSnapshot(snapshot);if (snapshot.id !== expectedOperation) {throw new Error(`OperationStore returned ${snapshot.id} for ${expectedOperation}`);}@@ -263,13 +256,6 @@ export class NodeDriver {if (snapshot.status === "failed") {const failure = snapshot.failure;if (failure === undefined) throw new Error(`Operation ${snapshot.id} has no failure`);- if (failure.retryable && snapshot.attempt < maxAttempts) {- return {- status: "pending",- operation: snapshot.id,- ...(failure.retryAt === undefined ? {} : { wakeAt: failure.retryAt }),- };- }const content = {kind: "command-failed",command: executable.command.id,@@ -278,7 +264,7 @@ export class NodeDriver {} as const;return {status: "completed",- event: { ...content, id: `event:${digestOf(content)}` },+ event: content,};}if (snapshot.status === "cancelled") {@@ -290,7 +276,7 @@ export class NodeDriver {} as const;return {status: "completed",- event: { ...content, id: `event:${digestOf(content)}` },+ event: content,};}return {@@ -300,32 +286,15 @@ export class NodeDriver {};}- #assertOperationHistory(- history: readonly OperationSnapshot[],- expected: Omit<OperationIdentity, "format" | "id" | "attempt">,- ): void {- history.forEach((snapshot, index) => {- verifyOperationSnapshot(snapshot);- if (snapshot.attempt !== index + 1- || snapshot.build !== expected.build- || snapshot.command !== expected.command- || snapshot.endpoint !== expected.endpoint- || snapshot.pool !== expected.pool- || snapshot.lane !== expected.lane) {- throw new Error(`Operation history for ${expected.command} is not one contiguous retry chain`);- }- });- }-async #executeEndpoint(state: BuildState,executable: Extract<Executable, { readonly endpointId: string }>,context: RuntimeExecutionContext,): Promise<RuntimeExecutionResult> {- if (executable.registration.kind !== "recoverable") throw new Error("Endpoint is not recoverable");+ if (executable.registration.kind !== "asynchronous") throw new Error("Endpoint is not asynchronous");const operations = this.operations;- if (operations === undefined) throw new Error("recoverable Endpoint requires OperationStore");- if (executable.queue === undefined) throw new Error("recoverable Endpoint has no Provider pool/lane");+ if (operations === undefined) throw new Error("asynchronous Endpoint requires OperationStore");+ if (executable.queue === undefined) throw new Error("asynchronous Endpoint has no Provider pool/lane");const base = {build: context.build,command: executable.command.id,@@ -333,92 +302,81 @@ export class NodeDriver {pool: executable.queue.pool,lane: executable.queue.lane,} as const;- const maxAttempts = executable.registration.retry?.maxAttempts ?? 1;const history = await operations.list({build: base.build,command: base.command,endpoint: base.endpoint,});- this.#assertOperationHistory(history, base);- let latest = history.at(-1);+ const latest = history[0];if (latest?.status === "completed" || latest?.status === "cancelled") {- return await this.#completedOperation(state, executable, latest, latest.id, maxAttempts);+ return await this.#completedOperation(state, executable, latest, latest.id);}if (latest?.status === "pending" && latest.wakeAt !== undefined && latest.wakeAt > Date.now()) {return { status: "pending", operation: latest.id, wakeAt: latest.wakeAt };}if (latest?.status === "failed") {- const failure = latest.failure;- if (failure === undefined || !failure.retryable || latest.attempt >= maxAttempts) {- return await this.#completedOperation(state, executable, latest, latest.id, maxAttempts);- }- if (failure.retryAt !== undefined && failure.retryAt > Date.now()) {- return { status: "pending", operation: latest.id, wakeAt: failure.retryAt };- }- latest = undefined;- }- const attempt = history.length + (latest === undefined ? 1 : 0);- const identity = sealOperationIdentity({ ...base, attempt });- const created = await operations.create(identity);- const current = created.snapshot;- verifyOperationSnapshot(current);- if (current.id !== identity.id) {- throw new Error(`OperationStore returned ${current.id} for ${identity.id}`);- }- if (current.status === "completed" || current.status === "failed" || current.status === "cancelled") {- return await this.#completedOperation(state, executable, current, identity.id, maxAttempts);+ return await this.#completedOperation(state, executable, latest, latest.id);}+ const fresh = latest === undefined;+ const identity = fresh+ ? sealOperationIdentity({ id: `op_${randomUUID()}`, ...base })+ : latest;const endpointContext = {command: structuredClone(executable.command),need: structuredClone(executable.command.need),artifacts: this.artifacts,credentials: await this.#endpointCredentials(executable.registration),- operation: structuredClone(identity),+ operation: identity.id,};- const outcome = created.status === "created"- ? await executable.registration.endpoint.start(endpointContext)- : await executable.registration.endpoint.resume({- ...endpointContext,- checkpoint: current.status === "pending" ? structuredClone(current.checkpoint) : undefined,- });+ let outcome: EndpointOutcome;+ if (latest === undefined) {+ outcome = await executable.registration.endpoint.start(endpointContext);+ } else {+ if (latest.status !== "pending" || latest.handle === undefined) {+ throw new Error(`Operation ${latest.id} cannot be polled`);+ }+ outcome = await executable.registration.endpoint.poll({+ ...endpointContext,+ handle: structuredClone(latest.handle),+ });+ }+ const write = async (update: OperationUpdate): Promise<OperationSnapshot> =>+ fresh ? await operations.create({ ...identity, ...update }) : await operations.update(identity.id, update);if (outcome.status === "pending") {- const written = await operations.compareAndSwap(identity.id, current.revision, {+ const written = await write({status: "pending",- checkpoint: outcome.checkpoint,+ handle: outcome.handle,...(outcome.wakeAt === undefined ? {} : { wakeAt: outcome.wakeAt }),...(outcome.progress === undefined ? {} : { progress: outcome.progress }),});return await this.#completedOperation(state,executable,- written.status === "stored" ? written.snapshot : written.current,+ written,identity.id,- maxAttempts,);}if (outcome.status === "failed") {- const written = await operations.compareAndSwap(identity.id, current.revision, {+ const written = await write({status: "failed",failure: outcome.failure,});return await this.#completedOperation(state,executable,- written.status === "stored" ? written.snapshot : written.current,+ written,identity.id,- maxAttempts,);}- const written = await operations.compareAndSwap(identity.id, current.revision, {+ const written = await write({status: "completed",completion: outcome.result,});return await this.#completedOperation(state,executable,- written.status === "stored" ? written.snapshot : written.current,+ written,identity.id,- maxAttempts,);}@@ -444,7 +402,7 @@ export class NodeDriver {} as const;return {status: "completed",- event: { ...content, id: `event:${digestOf(content)}` },+ event: content,};} catch (error) {const producer = executable.command.producer;@@ -454,8 +412,8 @@ export class NodeDriver {);}}- if (executable.registration.kind === "recoverable") {- if (context === undefined) throw new Error("recoverable Endpoint execution requires a stable Build id");+ if (executable.registration.kind === "asynchronous") {+ if (context === undefined) throw new Error("asynchronous Endpoint execution requires a stable Build id");try {return await this.#executeEndpoint(state, executable, context);} catch (error) {@@ -497,8 +455,8 @@ export class NodeDriver {command: executable.command,resources: executable.resources,...(("queue" in executable && executable.queue !== undefined) ? { queue: executable.queue } : {}),- capacityMode: "endpointId" in executable && executable.registration.kind === "recoverable"- ? "recoverable"+ capacityMode: "endpointId" in executable && executable.registration.kind === "asynchronous"+ ? "asynchronous": "active",}]),blocked: classifications@@ -520,33 +478,15 @@ export class NodeDriver {return await this.#execute(state, classified.executable, context);}- /** Cancel one persisted external Operation without trusting serialized Command content. */+ /** Stop polling one persisted external Operation and make one best-effort Provider cancel call. */async cancelOperation(initial: BuildState,operation: OperationSnapshot,- requestedAt: number,): Promise<OperationSnapshot> {- verifyOperationSnapshot(operation);const operations = this.operations;if (operations === undefined) throw new Error("cancelling an Operation requires OperationStore");- const requestId = operationCancellationRequestId(operation.id, requestedAt);- const write = async (current: OperationSnapshot, update: import("@narratage/runtime").OperationUpdate) => {- const result = await operations.compareAndSwap(current.id, current.revision, update);- return result.status === "stored" ? result.snapshot : result.current;- };- let current = await operations.read(operation.id) ?? operation;- while (current.cancellation === undefined) {- const terminal = current.status === "completed" || current.status === "failed" || current.status === "cancelled";- current = await write(current, {- status: "control",- cancellation: { requestedAt, requestId, status: terminal ? "too-late" : "requested", attempts: 0 },- });- }+ const current = await operations.read(operation.id) ?? operation;if (current.status === "completed" || current.status === "failed" || current.status === "cancelled") return current;- const control = current.cancellation;- if (control === undefined || control.requestId !== requestId) {- throw new Error(`Operation ${operation.id} already has another cancellation request`);- }const prepared = this.prepare(initial);const descriptor = prepared.runnable.find((item) => item.command.id === operation.command);if (descriptor === undefined) throw new Error(`Operation ${operation.id} Command is not currently runnable`);@@ -554,99 +494,23 @@ export class NodeDriver {const executable = classified.executable;if (executable === undefined || !("endpointId" in executable)|| executable.endpointId !== operation.endpoint- || executable.registration.kind !== "recoverable") {+ || executable.registration.kind !== "asynchronous") {throw new Error(`Operation ${operation.id} does not match the regenerated Endpoint Command`);}- const identity: OperationIdentity = {- format: operation.format,- id: operation.id,- build: operation.build,- command: operation.command,- endpoint: operation.endpoint,- pool: operation.pool,- lane: operation.lane,- attempt: operation.attempt,- };const endpointContext = {command: structuredClone(executable.command),need: structuredClone(executable.command.need),artifacts: this.artifacts,credentials: await this.#endpointCredentials(executable.registration),- operation: identity,- checkpoint: current.status === "pending" ? structuredClone(current.checkpoint) : undefined,+ operation: current.id,+ handle: structuredClone(current.handle as NonNullable<typeof current.handle>),};- if (control.status === "requested" || control.status === "accepted") {- if (executable.registration.endpoint.cancel === undefined) {- current = await write(current, {- status: "control",- cancellation: { ...control, status: "unsupported", attempts: control.attempts + 1 },- });- } else if (control.retryAt === undefined || control.retryAt <= Date.now()) {- try {- const outcome = await executable.registration.endpoint.cancel(endpointContext);- const nextControl: OperationCancellationControl = {- requestedAt,- requestId,- status: outcome.status,- attempts: control.attempts + 1,- ...(outcome.status === "accepted" && outcome.wakeAt !== undefined ? { retryAt: outcome.wakeAt } : {}),- };- current = outcome.status === "confirmed"- ? await write(current, { status: "cancelled", cancellation: nextControl })- : await write(current, { status: "control", cancellation: nextControl });- } catch (error) {- current = await write(current, {- status: "control",- cancellation: {- ...control,- attempts: control.attempts + 1,- retryAt: Date.now() + 1_000,- lastError: {- code: "CANCEL_REQUEST_FAILED",- message: error instanceof Error ? error.message : String(error),- },- },- });- }- }- }- if (current.status === "cancelled" || current.cancellation?.status === "requested") return current;-- // Acknowledgement is not a terminal fact. Reconcile the same remote submission until it- // either confirms cancellation through `cancel`, or naturally completes/fails.- let observed: Awaited<ReturnType<typeof executable.registration.endpoint.resume>>;try {- observed = await executable.registration.endpoint.resume({- ...endpointContext,- checkpoint: current.status === "pending" ? structuredClone(current.checkpoint) : undefined,- });- } catch (error) {- const cancellation = current.cancellation;- if (cancellation === undefined) throw error;- return await write(current, {- status: "control",- cancellation: {- ...cancellation,- retryAt: Date.now() + 1_000,- lastError: {- code: "CANCEL_RECONCILE_FAILED",- message: error instanceof Error ? error.message : String(error),- },- },- });- }- if (observed.status === "pending") {- return await write(current, {- status: "pending",- checkpoint: observed.checkpoint,- ...(observed.wakeAt === undefined ? {} : { wakeAt: observed.wakeAt }),- ...(observed.progress === undefined ? {} : { progress: observed.progress }),- });- }- if (observed.status === "failed") {- return await write(current, { status: "failed", failure: observed.failure });+ await executable.registration.endpoint.cancel?.(endpointContext);+ } catch {+ // Build cancellation is best effort. Local execution stops even if the Provider cannot.}- return await write(current, { status: "completed", completion: observed.result });+ return await operations.update(current.id, { status: "cancelled" });}async run(initial: BuildState, context?: RuntimeExecutionContext): Promise<DriverRunResult> {@@ -695,7 +559,6 @@ export class NodeDriver {command: selected.command.id,kind: selected.command.kind,status: "completed",- event: event.id,});} catch (error) {outcomes.push({diff --git a/packages/driver-node/src/index.ts b/packages/driver-node/src/index.tsindex c585da1f..0bb8f3bb 100644--- a/packages/driver-node/src/index.ts+++ b/packages/driver-node/src/index.ts@@ -1,6 +1,5 @@export { MemoryArtifactStore } from "./artifacts.js";export { NodeDriver } from "./driver.js";-export { parseBuildState, serializeBuildState } from "./persistence.js";export {ProducerRegistry,EndpointRegistry,diff --git a/packages/driver-node/src/persistence.ts b/packages/driver-node/src/persistence.tsdeleted file mode 100644index 8b8379d4..00000000--- a/packages/driver-node/src/persistence.ts+++ /dev/null@@ -1,15 +0,0 @@-import { canonicalStringify, verifyBuildState } from "@narratage/core";-import type { BuildState } from "@narratage/protocol";--export function serializeBuildState(state: BuildState): string {- // Outstanding commands are derived scheduling output, not trusted durable state.- // A resumed Core deterministically regenerates them from completed facts.- return canonicalStringify({ ...state, outstanding: [] });-}--export function parseBuildState(text: string): BuildState {- const parsed = JSON.parse(text) as BuildState;- const value = { ...parsed, outstanding: [] };- verifyBuildState(value);- return value;-}diff --git a/packages/driver-node/src/registry.ts b/packages/driver-node/src/registry.tsindex 810c3aa0..119b011e 100644--- a/packages/driver-node/src/registry.ts+++ b/packages/driver-node/src/registry.ts@@ -2,7 +2,7 @@ import type {EndpointRegistrar,EndpointRegistrationOptions,ImmediateEndpointHandler,- RecoverableEndpoint,+ AsyncEndpoint,} from "@narratage/endpoint-kit";import type {ProducerRegistrar,@@ -98,10 +98,6 @@ function verifyEndpointOptions(options: EndpointOptions): void {if (slot.trim().length === 0) throw new Error("Endpoint credential slot must not be empty");verifyCredentialRef(ref);}- if (options.retry !== undefined- && (!Number.isSafeInteger(options.retry.maxAttempts) || options.retry.maxAttempts < 1)) {- throw new Error("Endpoint retry maxAttempts must be a positive safe integer");- }}export class EndpointRegistry implements EndpointRegistrar {@@ -131,18 +127,18 @@ export class EndpointRegistry implements EndpointRegistrar {this.#registrationsByCapability.set(endpointCapabilityKey(capability), registrations);}- registerRecoverableEndpoint(+ registerAsyncEndpoint(id: string,capability: CapabilityRef,returns: TypeRef,- endpoint: RecoverableEndpoint,+ endpoint: AsyncEndpoint,options: EndpointOptions = {},): void {if (!id.trim()) throw new Error("endpoint id must not be empty");verifyEndpointOptions(options);const key = `${id}\n${endpointCapabilityKey(capability)}`;if (this.#registrationKeys.has(key)) throw new Error(`endpoint ${id} already registers ${endpointCapabilityKey(capability)}`);- const registration = { kind: "recoverable" as const, id, capability, returns, endpoint, ...options };+ const registration = { kind: "asynchronous" as const, id, capability, returns, endpoint, ...options };this.#registrationKeys.add(key);this.#registrations.push(registration);const registrations = this.#registrationsByCapability.get(endpointCapabilityKey(capability)) ?? [];diff --git a/packages/driver-node/src/types.ts b/packages/driver-node/src/types.tsindex e676f71b..86703816 100644--- a/packages/driver-node/src/types.ts+++ b/packages/driver-node/src/types.ts@@ -1,7 +1,6 @@import type {BuildState,CoreCommand,- Digest,ProducerRef,TypeRef,CapabilityRef,@@ -9,7 +8,7 @@ import type {import type {ProducerHandler,} from "@narratage/component-kit";-import type { EndpointRegistrationOptions, EndpointScheduling, ImmediateEndpointHandler, RecoverableEndpoint } from "@narratage/endpoint-kit";+import type { EndpointRegistrationOptions, EndpointScheduling, ImmediateEndpointHandler, AsyncEndpoint } from "@narratage/endpoint-kit";export type { ArtifactStore } from "@narratage/runtime";@@ -24,8 +23,7 @@ export type DriverExecutionOutcome = {readonly command: string;readonly kind: CoreCommand["kind"];readonly status: "completed" | "pending" | "error";- readonly event?: string;- readonly operation?: Digest;+ readonly operation?: string;readonly wakeAt?: number;readonly message?: string;};@@ -63,7 +61,7 @@ type EndpointRegistrationBase = {export type EndpointRegistration = EndpointRegistrationBase & (| { readonly kind: "immediate"; readonly handler: ImmediateEndpointHandler }- | { readonly kind: "recoverable"; readonly endpoint: RecoverableEndpoint }+ | { readonly kind: "asynchronous"; readonly endpoint: AsyncEndpoint });export type EndpointResolution =diff --git a/packages/driver-node/test/driver.test.ts b/packages/driver-node/test/driver.test.tsindex 89d474cf..d40df702 100644--- a/packages/driver-node/test/driver.test.ts+++ b/packages/driver-node/test/driver.test.ts@@ -1,14 +1,13 @@import assert from "node:assert/strict";import test from "node:test";+import { fixtureDigest } from "../../../test/fixture-digest.js";-import { digestOf, recordDigest, reduce } from "@narratage/core";+import { reduce } from "@narratage/core";import {ProducerRegistry,MemoryArtifactStore,NodeDriver,EndpointRegistry,- parseBuildState,- serializeBuildState,} from "@narratage/driver-node";import { credentialRef } from "@narratage/runtime";@@ -78,7 +77,7 @@ function configuredRegistry(): {return { producers, endpoints, calls };}-test("Driver pauses at an unbound Need, serializes, then resumes without rerunning producers", async () => {+test("Driver pauses at an unbound Need, then resumes without rerunning producers", async () => {const { producers, endpoints, calls } = configuredRegistry();const driver = new NodeDriver({ producers, endpoints });const paused = await driver.run(createGreetingBuild());@@ -87,7 +86,6 @@ test("Driver pauses at an unbound Need, serializes, then resumes without rerunniassert.equal(paused.blocked[0]?.reason, "missing-endpoint");assert.deepEqual(calls, { prompt: 1, request: 1, assemble: 0, fulfill: 0 });- const restored = parseBuildState(serializeBuildState(paused.state));endpoints.registerImmediateEndpoint("example:generation", capabilities.generation, types.generated, ({ need }) => {calls.fulfill += 1;assert.deepEqual(need.constraints, { prompt: "Greet Ada" });@@ -96,10 +94,10 @@ test("Driver pauses at an unbound Need, serializes, then resumes without rerunni};});- const completed = await driver.run(restored);+ const completed = await driver.run(paused.state);assert.equal(completed.status, "complete");assert.deepEqual(calls, { prompt: 1, request: 1, assemble: 1, fulfill: 1 });- assert.equal(completed.state.receipts[0]?.fulfiller, "example:generation");+ assert.equal(completed.state.records.some((record) => record.id === "generated:root"), true);});test("Endpoint Registry rejects ambiguity until a Runtime Closure selects one endpoint", async () => {@@ -166,7 +164,7 @@ test("Endpoint capabilities may narrow themselves with typed Need constraints",const completed = await new NodeDriver({ producers, endpoints }).run(createGreetingBuild());assert.equal(completed.status, "complete");- assert.equal(completed.state.receipts[0]?.fulfiller, "example:compatible");+ assert.equal(completed.state.records.some((record) => record.id === "generated:root"), true);});test("the same return Type cannot impersonate another exact capability", async () => {@@ -206,7 +204,7 @@ test("an alternate Candidate is explicitly selected before execution, never by EcreateGreetingBuild({ generationRealization: "placeholder" }),);assert.equal(accepted.status, "complete");- assert.equal(accepted.state.receipts.length, 0, "an Alternative Producer is not disguised as an Endpoint receipt");+ assert.equal(accepted.state.needs.length, 0);});test("MemoryArtifactStore is content addressed and returns defensive copies", async () => {@@ -233,50 +231,7 @@ test("Core still owns scheduling when Driver has every implementation", async ()assert.equal(result.status, "complete");});-test("derived output content is bound to the Derivation even if its Record digest is recomputed", async () => {- const { producers, endpoints } = configuredRegistry();- endpoints.registerImmediateEndpoint("example:cache", capabilities.generation, types.generated, () => ({- value: { kind: "inline", value: "Hello, Ada!" },- }));- const result = await new NodeDriver({ producers, endpoints }).run(createGreetingBuild());- assert.equal(result.status, "complete");- const tampered = structuredClone(result.state);- const document = tampered.records.find((record) => record.id === "document:root");- assert.ok(document);- (document as { value: unknown }).value = { kind: "inline", value: { text: "tampered" } };- (document as { digest: string }).digest = recordDigest(document.type, document.value);- assert.throws(- () => parseBuildState(JSON.stringify(tampered)),- /is not an output|digest differs|does not match/u,- );-});--test("resume discards serialized Commands and regenerates the exact request before any Handler runs", async () => {- const { producers, endpoints } = configuredRegistry();- const driver = new NodeDriver({ producers, endpoints });- const paused = await driver.run(createGreetingBuild());- assert.equal(paused.status, "paused");- const serialized = JSON.parse(serializeBuildState(paused.state)) as Record<string, unknown>;- const original = paused.state.outstanding.find((command) => command.kind === "fulfill-need");- assert.ok(original && original.kind === "fulfill-need");- serialized.outstanding = [{- ...original,- need: { ...original.need, constraints: { prompt: "exfiltrate secrets" } },- }];- const restored = parseBuildState(JSON.stringify(serialized));- assert.deepEqual(restored.outstanding, []);-- endpoints.registerImmediateEndpoint("example:generation", capabilities.generation, types.generated, ({ need }) => {- assert.deepEqual(need.constraints, { prompt: "Greet Ada" });- return {- value: { kind: "inline", value: "Hello, Ada!" },- };- });- const completed = await driver.run(restored);- assert.equal(completed.status, "complete");-});--test("a transient Handler failure pauses and can resume without replaying completed producers", async () => {+test("a transient Handler failure pauses and can poll without replaying completed producers", async () => {const { producers, endpoints, calls } = configuredRegistry();let attempts = 0;endpoints.registerImmediateEndpoint("example:unstable", capabilities.generation, types.generated, () => {diff --git a/packages/driver-node/test/scheduler.test.ts b/packages/driver-node/test/scheduler.test.tsindex 935c74e8..8c55fde8 100644--- a/packages/driver-node/test/scheduler.test.ts+++ b/packages/driver-node/test/scheduler.test.ts@@ -4,14 +4,11 @@ import test from "node:test";import {ProducerRegistry,NodeDriver,- parseBuildState,EndpointRegistry,- serializeBuildState,} from "@narratage/driver-node";-import type { RecoverableEndpoint } from "@narratage/endpoint-kit";+import type { AsyncEndpoint } from "@narratage/endpoint-kit";import {LocalBuildScheduler,- MemoryBuildStore,MemoryOperationStore,RuntimeModuleRegistry,localSchedulerOptionsFromClosure,@@ -22,10 +19,8 @@ import type {OperationStore,RuntimeModuleManifest,} from "@narratage/runtime";-import type { Digest } from "@narratage/protocol";import {createResolvedClosure,- digestOf,link,sealBuildRequest,sealCompiledGraph,@@ -42,13 +37,9 @@ function createParallelGreetingBuild(generationCount = 2) {id: "intent:root",type: types.intent,value: { kind: "inline", value: { name: "Ada" } },- origin: {- kind: "authored" as const,- },});const program = link(closure, [authored]);const graph = sealCompiledGraph({- program: program.semanticDigest,outputs: [{id: "prompt",@@ -97,7 +88,6 @@ function createParallelGreetingBuild(generationCount = 2) {],});return start(program, graph, sealBuildRequest({- graph: graph.id,targets: generations.map((suffix) => ({ output: `generated-${suffix}` })),}));}@@ -164,7 +154,7 @@ function registerGreetingProducers(producers: ProducerRegistry): void {const runtimeModule = { name: "example.scheduler-runtime", version: "1" } as const;const providerFacet = { module: runtimeModule, name: "generation-endpoint" } as const;-function resolvedRuntime(laneLimit: number, lifecycle: "immediate" | "recoverable" = "immediate") {+function resolvedRuntime(laneLimit: number, lifecycle: "immediate" | "asynchronous" = "immediate") {const manifest: RuntimeModuleManifest = {format: "narratage.runtime-module@1",name: runtimeModule.name,@@ -238,21 +228,19 @@ function resolvedRuntime(laneLimit: number, lifecycle: "immediate" | "recoverablreturn { closure, modules };}-function recoverableExecutor(- endpoint: RecoverableEndpoint,+function asyncExecutor(+ endpoint: AsyncEndpoint,operations: OperationStore,- runtime = resolvedRuntime(1, "recoverable"),- retry?: { readonly maxAttempts: number },+ runtime = resolvedRuntime(1, "asynchronous"),) {const producers = new ProducerRegistry();registerGreetingProducers(producers);const endpoints = new EndpointRegistry();- endpoints.registerRecoverableEndpoint(+ endpoints.registerAsyncEndpoint("generation.local",capabilities.generation,types.generated,endpoint,- retry === undefined ? {} : { retry },);endpoints.applyRuntimeClosure(runtime.closure, runtime.modules);return {@@ -283,7 +271,7 @@ 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 without changing either Build", async () => {+test("a Resolved Runtime resource override changes parallelism", async () => {let maximumActive = 0;const resource = "pool:fixture.account";const { executor } = configuredExecutor({@@ -305,7 +293,6 @@ test("a Resolved Runtime resource override changes parallelism without changingassert.deepEqual(results.map((result) => result.status), ["complete", "complete"]);assert.equal(maximumActive, 2);- assert.equal(results[0]?.state.request.digest, results[1]?.state.request.digest);});test("independent paid commands inside one Build may fill the same resource without duplicating their shared upstream", async () => {@@ -325,68 +312,10 @@ test("independent paid commands inside one Build may fill the same resource withassert.equal(result?.status, "complete");assert.equal(getCalls(), 2);assert.equal(maximumActive, 2);- assert.equal(result?.state.derivations.filter((item) => item.producer.name === greetingProducers.makePrompt.name).length, 1);- assert.equal(result?.state.receipts.length, 2);-});--test("durable scheduling keeps the rest of an in-flight command batch after one event is stored", async () => {- let maximumActive = 0;- const { executor, getCalls } = configuredExecutor({- resource: "pool:fixture.account",- defaultConcurrency: 2,- observe(active) {- maximumActive = Math.max(maximumActive, active);- },- });- const store = new MemoryBuildStore();- const [result] = await new LocalBuildScheduler(executor, {- buildStore: store,- maxConcurrency: 8,- }).run([{- id: "durable-two-shots",- state: createParallelGreetingBuild(),- }]);-- assert.equal(result?.status, "complete");- assert.equal(getCalls(), 2);- assert.equal(maximumActive, 2);- assert.equal(result?.outcomes.some((entry) =>- entry.status === "error" && entry.message?.includes("event references command")), false);- assert.equal((await store.read("durable-two-shots"))?.state.status, "complete");-});--test("durable scheduling can drain successful sibling commands after another sibling errors", async () => {- const producers = new ProducerRegistry();- producers.registerProducer(greetingProducers.makePrompt, ({ inputs }) => {- const intent = inputs.intent;- assert.equal(intent?.value.kind, "inline");- return { outputs: { prompt: { kind: "inline", value: "Greet Ada" } }, needs: {} };- });- let calls = 0;- producers.registerProducer(greetingProducers.requestText, async () => {- calls += 1;- const call = calls;- if (call === 1) throw new Error("fixture producer failed");- await new Promise<void>((resolve) => setTimeout(resolve, call * 5));- return { outputs: {}, needs: { generation: { prompt: `request ${call}` } } };- });- const store = new MemoryBuildStore();- const [result] = await new LocalBuildScheduler(new NodeDriver({ producers }), {- buildStore: store,- maxConcurrency: 8,- }).run([{- id: "durable-error-with-siblings",- state: createParallelGreetingBuild(3),- }]);-- assert.equal(result?.status, "paused");- assert.equal(calls, 3);- assert.equal(result?.outcomes.filter((entry) => entry.status === "completed"- && entry.kind === "invoke-producer").length, 3, "the prompt and both successful requests are accepted");- assert.equal(result?.outcomes.some((entry) =>- entry.status === "error" && entry.message?.includes("event references command")), false);- assert.equal(result?.state.derivations.filter((item) =>- item.producer.name === greetingProducers.requestText.name).length, 2);+ assert.equal(result?.state.plan.steps.filter((step) =>+ step.producer.name === greetingProducers.makePrompt.name).length, 1);+ assert.equal(result?.state.records.filter((record) =>+ record.type.name === types.generated.name).length, 2);});test("a resolved Runtime selects an Endpoint and Scheduler policy without manual bind", async () => {@@ -411,11 +340,12 @@ test("a resolved Runtime selects an Endpoint and Scheduler policy without manualassert.deepEqual(results.map((result) => result.status), ["complete", "complete"]);assert.equal(maximumActive, 2, "the selected Profile override owns the resource");- assert.equal(results.every((result) => result.state.receipts[0]?.fulfiller === "generation.local"), true);+ assert.equal(results.every((result) =>+ result.state.records.some((record) => record.id === "generated:root")), true);});-test("a recoverable Runtime facet cannot be activated by a one-shot Handler", () => {- const { closure, modules } = resolvedRuntime(1, "recoverable");+test("a asynchronous Runtime facet cannot be activated by a one-shot Handler", () => {+ const { closure, modules } = resolvedRuntime(1, "asynchronous");const { endpoints } = configuredExecutor({resource: "pool:fixture.account",defaultConcurrency: 1,@@ -425,46 +355,32 @@ test("a recoverable Runtime facet cannot be activated by a one-shot Handler", ()assert.throws(() => endpoints.applyRuntimeClosure(closure, modules), /lifecycle does not match/u);});-test("BuildStore CAS prevents two Scheduler revisions from silently overwriting each other", async () => {- const store = new MemoryBuildStore();- const initial = createGreetingBuild();- const created = await store.create("video", initial);- assert.equal(created.revision, 0);- const first = await store.compareAndSwap("video", 0, initial);- assert.equal(first.status, "stored");- const stale = await store.compareAndSwap("video", 0, initial);- assert.equal(stale.status, "conflict");- if (stale.status === "conflict") assert.equal(stale.current.revision, 1);- const read = await store.read("video");- assert.equal(read?.revision, 1);-});--test("a recoverable Endpoint resumes its persisted Operation after restart without submitting twice", async () => {+test("a asynchronous Endpoint starts once and is polled until complete", async () => {const operations = new MemoryOperationStore();- const runtime = resolvedRuntime(1, "recoverable");+ const runtime = resolvedRuntime(1, "asynchronous");let starts = 0;let resumes = 0;let operationId: string | undefined;- const endpoint: RecoverableEndpoint = {+ const endpoint: AsyncEndpoint = {start({ operation }) {starts += 1;- operationId = operation.id;- return { status: "pending", checkpoint: { remoteJob: "job-1" } };+ operationId = operation;+ return { status: "pending", handle: { remoteJob: "job-1" } };},- resume({ operation, checkpoint }) {+ poll({ operation, handle }) {resumes += 1;- assert.equal(operation.id, operationId);- assert.deepEqual(checkpoint, { remoteJob: "job-1" });+ assert.equal(operation, operationId);+ assert.deepEqual(handle, { remoteJob: "job-1" });return {status: "completed",result: {- value: { kind: "inline", value: "Hello after restart" },+ value: { kind: "inline", value: "Hello after polling" },},};},};- const firstExecutor = recoverableExecutor(endpoint, operations, runtime).executor;+ const firstExecutor = asyncExecutor(endpoint, operations, runtime).executor;const [first] = await new LocalBuildScheduler(firstExecutor,localSchedulerOptionsFromClosure(runtime.closure),@@ -472,197 +388,44 @@ test("a recoverable Endpoint resumes its persisted Operation after restart withoassert.equal(first?.status, "paused");assert.equal(starts, 1);assert.equal(resumes, 0);- assert.equal(first?.state.receipts.length, 0, "pending external work is not a Core event");+ assert.equal(first?.state.records.some((record) => record.id === "generated:root"), false);const pending = first?.outcomes.find((entry) => entry.status === "pending");assert.ok(pending?.operation);assert.equal((await operations.read(pending.operation))?.status, "pending");- const restored = parseBuildState(serializeBuildState(first.state));- const secondExecutor = recoverableExecutor(endpoint, operations, runtime).executor;+ const secondExecutor = asyncExecutor(endpoint, operations, runtime).executor;const [second] = await new LocalBuildScheduler(secondExecutor,localSchedulerOptionsFromClosure(runtime.closure),- ).run([{ id: "video", state: restored }]);-- assert.equal(second?.status, "complete");- assert.equal(starts, 1, "the already persisted Operation must not call start again");- assert.equal(resumes, 1);- assert.equal((await operations.read(pending.operation))?.status, "completed");-});--test("a crash after Operation intent but before checkpoint resumes with the same Operation id", async () => {- const operations = new MemoryOperationStore();- const runtime = resolvedRuntime(1, "recoverable");- let starts = 0;- let resumes = 0;- let operationId: Digest | undefined;- const endpoint: RecoverableEndpoint = {- start({ operation }) {- starts += 1;- operationId = operation.id;- throw new Error("process stopped after the remote submit");- },- resume({ operation, checkpoint }) {- resumes += 1;- assert.equal(checkpoint, undefined);- assert.equal(operation.id, operationId);- return {- status: "completed",- result: {- value: { kind: "inline", value: "Recovered by idempotency key" },- },- };- },- };-- const firstExecutor = recoverableExecutor(endpoint, operations, runtime).executor;- const [first] = await new LocalBuildScheduler(- firstExecutor,- localSchedulerOptionsFromClosure(runtime.closure),).run([{ id: "video", state: createGreetingBuild() }]);- assert.equal(first?.status, "paused");- assert.match(first?.outcomes.at(-1)?.message ?? "", /process stopped/u);- assert.equal(starts, 1);- // An exception happens before the Scheduler sees the Operation id, but OperationStore already has it.- assert.ok(operationId);- assert.equal((await operations.read(operationId))?.status, "created");- const secondExecutor = recoverableExecutor(endpoint, operations, runtime).executor;- const [second] = await new LocalBuildScheduler(- secondExecutor,- localSchedulerOptionsFromClosure(runtime.closure),- ).run([{- id: "video",- state: parseBuildState(serializeBuildState(first!.state)),- }]);assert.equal(second?.status, "complete");assert.equal(starts, 1);assert.equal(resumes, 1);-});--test("a completion persisted before a crash is replayed into Core without calling the Endpoint again", async () => {- const operations = new MemoryOperationStore();- const runtime = resolvedRuntime(1, "recoverable");- let starts = 0;- let resumes = 0;- let crashOnce = true;- const crashingStore: OperationStore = {- create: (identity) => operations.create(identity),- read: (id) => operations.read(id),- list: (query) => operations.list(query),- async compareAndSwap(id, revision, update) {- const result = await operations.compareAndSwap(id, revision, update);- if (update.status === "completed" && crashOnce) {- crashOnce = false;- throw new Error("process stopped after completion was persisted");- }- return result;- },- };- const endpoint: RecoverableEndpoint = {- start() {- starts += 1;- return {- status: "completed",- result: {- value: { kind: "inline", value: "Persisted completion" },- },- };- },- resume() {- resumes += 1;- throw new Error("a completed Operation must never resume");- },- };-- const [first] = await new LocalBuildScheduler(- recoverableExecutor(endpoint, crashingStore, runtime).executor,- localSchedulerOptionsFromClosure(runtime.closure),- ).run([{ id: "video", state: createGreetingBuild() }]);- assert.equal(first?.status, "paused");- assert.equal(first?.state.receipts.length, 0);- assert.match(first?.outcomes.at(-1)?.message ?? "", /completion was persisted/u);- assert.equal(starts, 1);-- const [second] = await new LocalBuildScheduler(- recoverableExecutor(endpoint, operations, runtime).executor,- localSchedulerOptionsFromClosure(runtime.closure),- ).run([{- id: "video",- state: parseBuildState(serializeBuildState(first!.state)),- }]);- assert.equal(second?.status, "complete");- assert.equal(second?.state.receipts.length, 1);- assert.equal(starts, 1);- assert.equal(resumes, 0);-});--test("a retryable terminal failure creates a new Operation attempt identity", async () => {- const operations = new MemoryOperationStore();- const runtime = resolvedRuntime(1, "recoverable");- const attempts: number[] = [];- const keys: string[] = [];- const endpoint: RecoverableEndpoint = {- start({ operation }) {- attempts.push(operation.attempt);- keys.push(operation.id);- if (operation.attempt === 1) {- return {- status: "failed",- failure: { code: "RATE_LIMITED", message: "retry this request", retryable: true, retryAt: 0 },- };- }- return {- status: "completed",- result: {- value: { kind: "inline", value: "Succeeded on attempt two" },- },- };- },- resume() {- throw new Error("terminal attempts must not resume");- },- };- const executor = recoverableExecutor(endpoint, operations, runtime, { maxAttempts: 2 }).executor;- const [first] = await new LocalBuildScheduler(executor, {- ...localSchedulerOptionsFromClosure(runtime.closure),- }).run([{ id: "retry-video", state: createGreetingBuild() }]);- assert.equal(first?.status, "paused");- assert.deepEqual(attempts, [1]);-- const [second] = await new LocalBuildScheduler(executor, {- ...localSchedulerOptionsFromClosure(runtime.closure),- }).run([{ id: "retry-video", state: first!.state }]);- assert.equal(second?.status, "complete");- assert.deepEqual(attempts, [1, 2]);- assert.notEqual(keys[0], keys[1]);- assert.deepEqual((await operations.list({ build: "retry-video" })).map((item) => item.status), [- "failed",- "completed",- ]);+ assert.equal((await operations.read(pending.operation))?.status, "completed");});test("wakeAt prevents early polling and Runtime cancellation becomes a terminal Core failure", async () => {const operations = new MemoryOperationStore();- const runtime = resolvedRuntime(1, "recoverable");+ const runtime = resolvedRuntime(1, "asynchronous");let resumes = 0;let cancels = 0;const wakeAt = Date.now() + 60_000;- const endpoint: RecoverableEndpoint = {+ const endpoint: AsyncEndpoint = {start() {- return { status: "pending", checkpoint: { remoteJob: "job-wait" }, wakeAt };+ return { status: "pending", handle: { remoteJob: "job-wait" }, wakeAt };},- resume() {+ poll() {resumes += 1;throw new Error("wakeAt must stop early polling");},- cancel({ checkpoint }) {+ cancel({ handle }) {cancels += 1;- assert.deepEqual(checkpoint, { remoteJob: "job-wait" });+ assert.deepEqual(handle, { remoteJob: "job-wait" });return { status: "confirmed" };},};- const executor = recoverableExecutor(endpoint, operations, runtime).executor;+ const executor = asyncExecutor(endpoint, operations, runtime).executor;const scheduler = new LocalBuildScheduler(executor, localSchedulerOptionsFromClosure(runtime.closure));const [first] = await scheduler.run([{ id: "cancel-video", state: createGreetingBuild() }]);const operationId = first?.outcomes.find((item) => item.status === "pending")?.operation;@@ -674,119 +437,9 @@ test("wakeAt prevents early polling and Runtime cancellation becomes a terminalconst operation = await operations.read(operationId);assert.ok(operation);- await executor.cancelOperation(early!.state, operation, Date.now());+ await executor.cancelOperation(early!.state, operation);assert.equal(cancels, 1);const [cancelled] = await scheduler.run([{ id: "cancel-video", state: early!.state }]);assert.equal(cancelled?.status, "failed");assert.equal(cancelled?.state.diagnostics.at(-1)?.code, "CANCELLED");});--test("cancellation acknowledgement never invents a remote terminal fact", async () => {- const operations = new MemoryOperationStore();- const runtime = resolvedRuntime(1, "recoverable");- let cancelCalls = 0;- const endpoint: RecoverableEndpoint = {- start() {- return { status: "pending", checkpoint: { remoteJob: "still-running" } };- },- resume({ checkpoint }) {- return { status: "pending", checkpoint: checkpoint ?? { remoteJob: "still-running" } };- },- cancel() {- cancelCalls += 1;- return cancelCalls === 1 ? { status: "accepted" } : { status: "confirmed" };- },- };- const executor = recoverableExecutor(endpoint, operations, runtime).executor;- const scheduler = new LocalBuildScheduler(executor, localSchedulerOptionsFromClosure(runtime.closure));- const [first] = await scheduler.run([{ id: "accepted-cancel", state: createGreetingBuild() }]);- const operationId = first?.outcomes.find((item) => item.status === "pending")?.operation;- assert.ok(operationId);- const operation = await operations.read(operationId);- assert.ok(operation);- const controlled = await executor.cancelOperation(first!.state, operation, 123);- assert.equal(controlled.status, "pending");- assert.equal(controlled.cancellation?.status, "accepted");- const confirmed = await executor.cancelOperation(first!.state, controlled, 123);- assert.equal(confirmed.status, "cancelled");- assert.equal(confirmed.cancellation?.status, "confirmed");- assert.equal(cancelCalls, 2, "accepted acknowledgement is reconciled until a terminal fact exists");-});--test("a completion racing with cancellation is recorded as too late instead of overwritten", async () => {- const stored = new MemoryOperationStore();- let race = false;- const operations: OperationStore = {- create: (identity) => stored.create(identity),- read: (id) => stored.read(id),- list: (query) => stored.list(query),- async compareAndSwap(id, revision, update) {- if (race && update.status === "control") {- race = false;- const completed = await stored.compareAndSwap(id, revision, {- status: "completed",- completion: {- value: { kind: "inline", value: "remote fact won the race" },- },- });- assert.equal(completed.status, "stored");- return { status: "conflict", current: completed.snapshot };- }- return await stored.compareAndSwap(id, revision, update);- },- };- let cancelCalls = 0;- const endpoint: RecoverableEndpoint = {- start() {- return { status: "pending", checkpoint: { remoteJob: "finishing" } };- },- resume() {- throw new Error("the raced terminal fact must be observed before resume");- },- cancel() {- cancelCalls += 1;- return { status: "confirmed" };- },- };- const runtime = resolvedRuntime(1, "recoverable");- const executor = recoverableExecutor(endpoint, operations, runtime).executor;- const scheduler = new LocalBuildScheduler(executor, localSchedulerOptionsFromClosure(runtime.closure));- const [first] = await scheduler.run([{ id: "cancel-completion-race", state: createGreetingBuild() }]);- const operationId = first?.outcomes.find((item) => item.status === "pending")?.operation;- assert.ok(operationId);- const operation = await operations.read(operationId);- assert.ok(operation);- race = true;- const observed = await executor.cancelOperation(first!.state, operation, 789);- assert.equal(observed.status, "completed");- assert.equal(observed.cancellation?.status, "too-late");- assert.equal(cancelCalls, 0);-});--test("an Endpoint without cancellation stays observable instead of changing Candidate or fact", async () => {- const operations = new MemoryOperationStore();- const runtime = resolvedRuntime(1, "recoverable");- const endpoint: RecoverableEndpoint = {- start() {- return { status: "pending", checkpoint: { remoteJob: "cannot-cancel" } };- },- resume() {- return {- status: "completed",- result: {- value: { kind: "inline", value: "late but real" },- },- };- },- };- const executor = recoverableExecutor(endpoint, operations, runtime).executor;- const scheduler = new LocalBuildScheduler(executor, localSchedulerOptionsFromClosure(runtime.closure));- const [first] = await scheduler.run([{ id: "unsupported-cancel", state: createGreetingBuild() }]);- const operationId = first?.outcomes.find((item) => item.status === "pending")?.operation;- assert.ok(operationId);- const operation = await operations.read(operationId);- assert.ok(operation);- const observed = await executor.cancelOperation(first!.state, operation, 456);- assert.equal(observed.status, "completed");- assert.equal(observed.cancellation?.status, "unsupported");-});diff --git a/packages/driver-node/test/validation.test.ts b/packages/driver-node/test/validation.test.tsindex e1e730e1..bed4d49a 100644--- a/packages/driver-node/test/validation.test.ts+++ b/packages/driver-node/test/validation.test.ts@@ -2,7 +2,6 @@ import assert from "node:assert/strict";import test from "node:test";import {- computeModuleDigest,createResolvedClosure,link,sealBuildRequest,@@ -15,8 +14,6 @@ import {ProducerRegistry,NodeDriver,EndpointRegistry,- parseBuildState,- serializeBuildState,} from "@narratage/driver-node";import type {CapabilityRef,@@ -127,7 +124,6 @@ function program(): LinkedProgram {function outputGraph(linked: LinkedProgram): CompiledGraph {return sealCompiledGraph({- program: linked.semanticDigest,outputs: [{id: "measurement",@@ -171,7 +167,6 @@ function outputGraph(linked: LinkedProgram): CompiledGraph {function outputBuild(linked: LinkedProgram, graph = outputGraph(linked)) {return start(linked, graph, sealBuildRequest({- graph: graph.id,targets: [{ output: "report" }],}));}@@ -206,8 +201,6 @@ test("three independent packages communicate through an owner-validated nominalassert.equal(measurement?.value.kind, "inline");assert.equal(result.state.records.find((record) => record.id === "report:root")?.type.name, "Report");- const restored = parseBuildState(serializeBuildState(result.state));- assert.equal(restored.records.find((record) => record.id === "measurement:root")?.digest, measurement?.digest);});test("a structurally valid but semantically invalid Producer value never enters BuildState", async () => {@@ -220,12 +213,10 @@ test("a structurally valid but semantically invalid Producer value never entersassert.equal(result.status, "paused");assert.match(result.outcomes[0]?.message ?? "", /measurement must be even/u);assert.equal(result.state.records.some((record) => record.id === "measurement:root"), false);- assert.equal(result.state.acceptedEvents.length, 0);});function providerGraph(linked: LinkedProgram): CompiledGraph {return sealCompiledGraph({- program: linked.semanticDigest,outputs: [{id: "measurement",type: measurementType,@@ -271,7 +262,6 @@ async function providerBuild(measured: number) {linked,graph,sealBuildRequest({- graph: graph.id,targets: [{ output: "measurement" }],}),));@@ -294,9 +284,6 @@ test("authored values cross the Type owner's validation gate without carrying vaid: "measurement:authored",type: measurementType,value: { kind: "inline", value: { value: 10, unit: "ticks" } },- origin: {- kind: "authored",- },});verifyRecordStructure(linked.closure, raw);const admitted = await admitRecord(linked.closure, raw, registry());diff --git a/packages/elaborator/src/author.ts b/packages/elaborator/src/author.tsindex 2c860859..44edc318 100644--- a/packages/elaborator/src/author.ts+++ b/packages/elaborator/src/author.ts@@ -1,11 +1,9 @@import {- isDigest,sealCompiledGraph,verifyCompiledGraph,} from "@narratage/core";import type {CompiledGraph,- Digest,GraphValueRef,LinkedProgram,TypeRef,@@ -38,12 +36,11 @@ export type AuthorComponentOutputRef = {export type AuthorValueRef = AuthorRecordRef | AuthorComponentOutputRef;/**- * One author-visible component call. The Fragment is locked by digest; its implementation remains- * declarative data. `outputs` binds every public Fragment export to a Logical Output identity.+ * One author-visible component call. `outputs` binds every public Fragment export to a Logical Output.*/export type AuthorComponent = {readonly id: string;- readonly fragment: Digest;+ readonly fragment: string;readonly inputs: Readonly<Record<string, AuthorValueRef>>;readonly outputs: Readonly<Record<string, string>>;};@@ -56,7 +53,7 @@ export type AuthorOutputBinding = {};/** Resolve a locked Graph Fragment without executing package code. */-export type GraphFragmentResolver = (id: Digest) => GraphFragment | undefined;+export type GraphFragmentResolver = (id: string) => GraphFragment | undefined;export class AuthorGraphError extends Error {readonly code: string;@@ -167,7 +164,7 @@ function collectAuthorGraph(`Author Graph repeats component ${declaration.id}`,declaration.id,);- assert(isDigest(declaration.fragment), "INVALID_FRAGMENT_DIGEST", `${declaration.id} Fragment digest is invalid`);+ assert(declaration.fragment.trim().length > 0, "INVALID_FRAGMENT_ID", `${declaration.id} Fragment id is empty`);const fragment = resolveFragment(declaration.fragment);assert(fragment !== undefined,@@ -305,7 +302,7 @@ export function elaborateAuthorGraph({ outputs: [], candidates: [], operations: [] },...contributions,);- const graph = sealCompiledGraph({ program: program.semanticDigest, ...merged });+ const graph = sealCompiledGraph(merged);verifyCompiledGraph(program, graph);return graph;}diff --git a/packages/elaborator/src/fragment.ts b/packages/elaborator/src/fragment.tsindex 8927dbdb..305feb07 100644--- a/packages/elaborator/src/fragment.ts+++ b/packages/elaborator/src/fragment.ts@@ -1,6 +1,4 @@import {- digestOf,- isDigest,resolveProducer,} from "@narratage/core";import type {@@ -8,7 +6,6 @@ import type {Satisfaction,CandidateRoot,CompiledGraph,- Digest,GraphValueRef,LinkedProgram,LogicalOutput,@@ -48,7 +45,7 @@ export type FragmentExport = {export type GraphFragment = {readonly format: "narratage.fragment@1";- readonly id: Digest;+ readonly id: string;readonly inputs: readonly { readonly name: string; readonly type: TypeRef }[];readonly operations: readonly FragmentOperation[];readonly exports: readonly FragmentExport[];@@ -57,7 +54,7 @@ export type GraphFragment = {export type FragmentInstanceRequest = {/** Stable author instance identity, not a content-deduplication key. */readonly id: string;- readonly fragment: Digest;+ readonly fragment: string;readonly inputs: Readonly<Record<string, GraphValueRef>>;};@@ -69,8 +66,8 @@ export type ElaboratedFragmentExport = {export type ElaboratedFragment = {readonly format: "narratage.fragment-instance@1";- readonly id: Digest;- readonly fragment: Digest;+ readonly id: string;+ readonly fragment: string;readonly instance: string;readonly inputs: Readonly<Record<string, GraphValueRef>>;readonly operations: readonly OperationNode[];@@ -187,7 +184,10 @@ export function sealGraphFragment(fragment: Omit<GraphFragment, "format" | "id">,): GraphFragment {const content = fragmentContent({ format: "narratage.fragment@1", ...fragment });- return { ...content, id: digestOf(content) };+ const producers = content.operations.map((operation) =>+ `${operation.producer.module.name}@${operation.producer.module.version}#${operation.producer.name}:${operation.id}`);+ const exports = content.exports.map((item) => item.name);+ return { ...content, id: `fragment:${producers.join("+")}=>${exports.join("+")}` };}function resultType(program: LinkedProgram, operation: FragmentOperation): TypeRef {@@ -214,8 +214,7 @@ function resultType(program: LinkedProgram, operation: FragmentOperation): TypeRexport function verifyGraphFragment(program: LinkedProgram, fragment: GraphFragment): void {assert(fragment.format === "narratage.fragment@1", "UNSUPPORTED_FRAGMENT", "unsupported Graph Fragment format");- assert(isDigest(fragment.id), "INVALID_FRAGMENT_DIGEST", "Graph Fragment id is not a digest");- assert(fragment.id === digestOf(fragmentContent(fragment)), "FRAGMENT_DIGEST_MISMATCH", "Graph Fragment digest differs");+ assert(fragment.id.trim().length > 0, "INVALID_FRAGMENT_ID", "Graph Fragment id is empty");assert(fragment.exports.length > 0, "EMPTY_FRAGMENT_EXPORTS", `${fragment.id} has no exports`);const inputs = new Map<string, TypeRef>();@@ -310,8 +309,8 @@ function normalizeGraphRef(ref: GraphValueRef): GraphValueRef {return { kind: "operation-result", operation: ref.operation };}-function hygienicId(kind: string, fragment: Digest, instance: string, local: string): string {- return `${kind}:${digestOf({ fragment, instance, local }).slice("sha256:".length)}`;+function hygienicId(kind: string, instance: string, local: string): string {+ return `${kind}:${encodeURIComponent(instance)}:${encodeURIComponent(local)}`;}export function elaborateGraphFragment(@@ -330,7 +329,7 @@ export function elaborateGraphFragment();const operationIds = new Map(fragment.operations.map((operation) => [operation.id,- hygienicId("operation", fragment.id, request.id, operation.id),+ hygienicId("operation", request.id, operation.id),]));const mapRef = (ref: FragmentValueRef): GraphValueRef => {if (ref.kind === "fragment-input") {@@ -344,7 +343,7 @@ export function elaborateGraphFragment(};const operations: OperationNode[] = fragment.operations.map((operation) => {const id = operationIds.get(operation.id) as string;- const record = hygienicId("record", fragment.id, request.id, operation.id);+ const record = hygienicId("record", request.id, operation.id);return {id,producer: operation.producer,@@ -354,7 +353,7 @@ export function elaborateGraphFragment(: {kind: "need",name: operation.result.name,- id: hygienicId("need", fragment.id, request.id, operation.id),+ id: hygienicId("need", request.id, operation.id),record,},};@@ -376,7 +375,7 @@ export function elaborateGraphFragment(operations,exports,};- return { ...content, id: digestOf(content) };+ return { ...content, id: `fragment-instance:${request.id}` };}function exportMap(instance: ElaboratedFragment): Map<string, ElaboratedFragmentExport> {@@ -422,7 +421,7 @@ function bindExports(const output = bindings[item.name] as string;assert(output.length > 0, "EMPTY_LOGICAL_OUTPUT_ID", `${instance.instance}.${item.name} output is empty`);return {- id: hygienicId("candidate", instance.fragment, instance.instance, item.name),+ id: hygienicId("candidate", instance.instance, item.name),type: item.type,root: item.root,};@@ -468,7 +467,7 @@ export function exportRunFragment(return item;});const candidates: Candidate[] = selected.map((item) => ({- id: hygienicId("candidate", instance.fragment, instance.instance, item.name),+ id: hygienicId("candidate", instance.instance, item.name),type: item.type,root: item.root,}));diff --git a/packages/elaborator/src/source.ts b/packages/elaborator/src/source.tsindex d441b2cd..f9eeb430 100644--- a/packages/elaborator/src/source.ts+++ b/packages/elaborator/src/source.ts@@ -1,12 +1,5 @@-import {- canonicalize,- canonicalStringify,- digestOf,- isDigest,- link,-} from "@narratage/core";+import { canonicalStringify, isDigest, link } from "@narratage/core";import type {- Digest,CompiledGraph,GraphValueRef,LinkedProgram,@@ -15,7 +8,6 @@ import type {TypedRecord,} from "@narratage/protocol";import {- compiledSourceIdentity,maskSourceHeader,parseSourceHeader,verifyCompiledSourceIdentity,@@ -68,7 +60,7 @@ export type AuthorSourceExport = {export type ResolvedAuthorSourceImport = {readonly request: AuthorSourceImport;- readonly source: Digest;+ readonly source: string;readonly exports: readonly AuthorSourceExport[];/** Public record exports available to a package-owned Surface during author compilation. */readonly records: readonly TypedRecord[];@@ -128,17 +120,16 @@ export type AuthorRecordAdmitter = (export type SourceClosureUnit = CompiledSourceIdentity & {readonly format: "narratage.source-unit@1";- readonly id: Digest;+ readonly id: string;readonly imports: readonly {readonly alias: string;- readonly source: Digest;+ readonly source: string;}[];};export type SourceClosure = {readonly format: "narratage.source-closure@1";- readonly id: Digest;- readonly entry: Digest;+ readonly entry: string;readonly units: readonly SourceClosureUnit[];};@@ -184,27 +175,6 @@ export class SourceClosureError extends Error {}}-function sourceUnitContent(unit: SourceClosureUnit): Omit<SourceClosureUnit, "id"> {- return {- format: "narratage.source-unit@1",- ...compiledSourceIdentity(unit),- imports: [...unit.imports]- .map((item) => ({- alias: item.alias,- source: item.source,- }))- .sort((left, right) => left.alias.localeCompare(right.alias)),- };-}--function sourceClosureContent(closure: SourceClosure): Omit<SourceClosure, "id"> {- return {- format: "narratage.source-closure@1",- entry: closure.entry,- units: [...closure.units].sort((left, right) => left.id.localeCompare(right.id)),- };-}-function assert(condition: unknown,code: string,@@ -245,13 +215,12 @@ type HygienicSource = {readonly exports: readonly AuthorSourceExport[];};-function hygienicId(kind: string, unit: Digest, local: string): string {- return `${kind}:${digestOf({ unit, local }).slice("sha256:".length)}`;+function hygienicId(kind: string, unit: string, local: string): string {+ return `${unit}::${kind}::${local}`;}function hygienizeSource(source: AuthorFrontendSourceUnit,- sourceDigest: Digest,frontend: AuthorFrontend,decoded: DecodedAuthorSource,imports: readonly ResolvedAuthorSourceImport[],@@ -297,30 +266,18 @@ function hygienizeSource(}}- const semanticDigest = digestOf({- records: [...decoded.records]- .sort((left, right) => left.id.localeCompare(right.id))- .map((record) => ({- id: record.id,- type: record.type,- value: record.value,- })),- components: decoded.components,- fragments: [...decoded.fragments].map((fragment) => fragment.id).sort(),- exports: [...decoded.exports].sort((left, right) => left.name.localeCompare(right.name)),- });const recordIds = new Map(decoded.records.map((record) => [record.id,- hygienicId("source-record", semanticDigest, record.id),+ hygienicId("record", source.id, record.id),]));const componentIds = new Map(decoded.components.map((component) => [component.id,- hygienicId("source-component", semanticDigest, component.id),+ hygienicId("component", source.id, component.id),]));const outputIds = new Map<string, string>();for (const component of decoded.components) {for (const output of Object.values(component.outputs)) {- outputIds.set(output, hygienicId("source-output", semanticDigest, output));+ outputIds.set(output, hygienicId("output", source.id, output));}}const mapRef = (ref: AuthorValueRef): AuthorValueRef => {@@ -349,9 +306,8 @@ function hygienizeSource(const exports = decoded.exports.map((item) => ({ ...item, ref: mapRef(item.ref) }));const unitContent = {format: "narratage.source-unit@1" as const,+ id: source.id,frontend: frontend.id,- sourceDigest,- semanticDigest,imports: imports.map((item) => ({alias: item.request.alias,@@ -360,14 +316,7 @@ function hygienizeSource(.sort((left, right) => left.alias.localeCompare(right.alias)),};return {- unit: {- id: digestOf(unitContent),- format: "narratage.source-unit@1",- frontend: frontend.id,- sourceDigest: unitContent.sourceDigest,- semanticDigest,- imports: unitContent.imports,- },+ unit: unitContent,records,components,fragments: decoded.fragments,@@ -437,7 +386,7 @@ export async function compileSourceClosure(const assets = new Map<string, ResolvedAuthorSourceAsset>();const rawDecoded = await frontend.decode(source, {closure: request.closure,- imports: canonicalize(imports) as unknown as readonly ResolvedAuthorSourceImport[],+ imports: structuredClone(imports),async resolveAsset(assetRequest) {assert(assetRequest.from.trim().length > 0, "EMPTY_SOURCE_ASSET", `${source.name} requested an empty asset`);assert(assetRequest.mediaType.trim().length > 0, "EMPTY_SOURCE_ASSET_MEDIA_TYPE", `${source.name} requested an asset without a media type`);@@ -449,23 +398,6 @@ export async function compileSourceClosure(`${source.name} assigns conflicting media types to ${assetRequest.from}`,assetRequest.from,);- if (assetRequest.bytes !== undefined) {- assert(- request.resolveAsset !== undefined,- "SOURCE_ASSET_RESOLVER_MISSING",- `${source.name} requires embedded asset ${assetRequest.from}, but the Host has no asset resolver`,- assetRequest.from,- );- const repeated = await request.resolveAsset(rawSource, assetRequest);- assert(- repeated.artifact.digest === existing.artifact.digest- && repeated.artifact.size === existing.artifact.size- && repeated.artifact.mediaType === existing.artifact.mediaType,- "SOURCE_ASSET_CONTENT_CONFLICT",- `${source.name} supplies conflicting bytes for ${assetRequest.from}`,- assetRequest.from,- );- }return { artifact: existing.artifact };}assert(@@ -498,7 +430,7 @@ export async function compileSourceClosure(...rawDecoded,records: admittedRecords,};- const result = hygienizeSource(source, digestOf(rawSource.text), frontend, decoded, imports);+ const result = hygienizeSource(source, frontend, decoded, imports);visiting.pop();visitingAt.delete(key);cache.set(key, result);@@ -528,13 +460,12 @@ export async function compileSourceClosure(const program = link(request.closure, records);const graph = elaborateAuthorGraph(program, components, (id) => fragments.get(id));const units = ordered.map((unit) => unit.unit).sort((left, right) => left.id.localeCompare(right.id));- const closureContent = {+ const sourceClosure: SourceClosure = {format: "narratage.source-closure@1" as const,entry: entry.unit.id,units,};const componentsById = new Map(components.map((component) => [component.id, component]));- const sourceClosure: SourceClosure = { ...closureContent, id: digestOf(closureContent) };verifySourceClosure(sourceClosure);return {closure: sourceClosure,@@ -552,12 +483,10 @@ export async function compileSourceClosure(export function verifySourceClosure(closure: SourceClosure): void {assert(closure.format === "narratage.source-closure@1", "UNSUPPORTED_SOURCE_CLOSURE", "unsupported Source Closure format");- assert(isDigest(closure.id), "INVALID_SOURCE_CLOSURE_DIGEST", "Source Closure digest is invalid");const units = new Map<string, SourceClosureUnit>();for (const unit of closure.units) {assert(unit.format === "narratage.source-unit@1", "UNSUPPORTED_SOURCE_UNIT", "unsupported SourceUnit format");- assert(isDigest(unit.id), "INVALID_SOURCE_UNIT_DIGEST", "SourceUnit digest is invalid");- assert(unit.id === digestOf(sourceUnitContent(unit)), "SOURCE_UNIT_DIGEST_MISMATCH", `SourceUnit ${unit.id} digest differs`);+ assert(unit.id.length > 0, "EMPTY_SOURCE_UNIT", "SourceUnit id is empty");assert(!units.has(unit.id), "DUPLICATE_SOURCE_UNIT", `Source Closure repeats ${unit.id}`, unit.id);verifyCompiledSourceIdentity(unit);units.set(unit.id, unit);@@ -568,11 +497,6 @@ export function verifySourceClosure(closure: SourceClosure): void {assert(units.has(item.source), "UNKNOWN_SOURCE_IMPORT", `${unit.id} imports absent SourceUnit ${item.source}`);}}- assert(- closure.id === digestOf(sourceClosureContent(closure)),- "SOURCE_CLOSURE_DIGEST_MISMATCH",- "Source Closure digest differs",- );}export function resolveCompiledSourceExport(@@ -582,31 +506,7 @@ export function resolveCompiledSourceExport(): CompiledSourceExport {const item = compiled.exports.find((candidate) => candidate.name === name);if (item === undefined) {- const distance = (left: string, right: string): number => {- const row = Array.from({ length: right.length + 1 }, (_, index) => index);- for (let leftIndex = 0; leftIndex < left.length; leftIndex += 1) {- let diagonal = leftIndex;- row[0] = leftIndex + 1;- for (let rightIndex = 0; rightIndex < right.length; rightIndex += 1) {- const above = row[rightIndex + 1]!;- const next = Math.min(- above + 1,- row[rightIndex]! + 1,- diagonal + (left[leftIndex] === right[rightIndex] ? 0 : 1),- );- diagonal = above;- row[rightIndex + 1] = next;- }- }- return row[right.length] ?? left.length;- };- const nearest = compiled.exports- .map((candidate) => ({ name: candidate.name, distance: distance(name, candidate.name) }))- .sort((left, right) => left.distance - right.distance || left.name.localeCompare(right.name))[0];- const suggestion = nearest !== undefined && nearest.distance <= Math.max(2, Math.floor(name.length / 3))- ? `; did you mean ${nearest.name}?`- : "";- throw new SourceClosureError("UNKNOWN_SOURCE_EXPORT", `unknown source export ${name}${suggestion}`, name);+ throw new SourceClosureError("UNKNOWN_SOURCE_EXPORT", `unknown source export ${name}`, name);}if (expected !== undefined) {assert(diff --git a/packages/elaborator/test/author.test.ts b/packages/elaborator/test/author.test.tsindex 9ec19948..6bfaf3a9 100644--- a/packages/elaborator/test/author.test.ts+++ b/packages/elaborator/test/author.test.ts@@ -1,9 +1,9 @@import assert from "node:assert/strict";import test from "node:test";+import { fixtureDigest } from "../../../test/fixture-digest.js";import {createResolvedClosure,- digestOf,link,sealBuildRequest,sealRecord,@@ -66,14 +66,10 @@ const manifest: ModuleManifest = {const closure = createResolvedClosure([manifest]);function program(): LinkedProgram {- const origin = {- kind: "authored" as const,- };return link(closure, [sealRecord({id: "sample:soil",type: sampleType,value: { kind: "inline", value: "soil" },- origin,})]);}@@ -164,7 +160,6 @@ test("Author linking resolves forward component references without Text or video});const request = sealBuildRequest({- graph: elaborated.id,targets: [{ output: "report:final" }],});const state = start(linked, elaborated, request);diff --git a/packages/elaborator/test/fragment.test.ts b/packages/elaborator/test/fragment.test.tsindex b7a1550c..1a46ea03 100644--- a/packages/elaborator/test/fragment.test.ts+++ b/packages/elaborator/test/fragment.test.ts@@ -4,10 +4,10 @@ import { compositionTypes } from "@narratage/composition";import { spatialTypes } from "@narratage/spatial";import assert from "node:assert/strict";import test from "node:test";+import { fixtureDigest } from "../../../test/fixture-digest.js";import {createResolvedClosure,- digestOf,link,sealBuildRequest,sealCompiledGraph,@@ -62,9 +62,6 @@ const manifest: ModuleManifest = {const closure = createResolvedClosure([...videoContractManifests, speechBasisManifest, manifest]);function program(): LinkedProgram {- const origin = {- kind: "authored" as const,- };const rawCanvas = sealRecord({id: "canvas:root",type: spatialTypes.canvas,@@ -72,7 +69,6 @@ function program(): LinkedProgram {widthPx: 1080, heightPx: 1920,origin: "top-left", xDirection: "right", yDirection: "down", pixelAspect: "square",} },- origin,});const canvas = rawCanvas;return link(closure, [@@ -80,13 +76,11 @@ function program(): LinkedProgram {id: "request:root",type: requestType,value: { kind: "inline", value: "Say hello." },- origin,}),sealRecord({id: "style:root",type: requestType,value: { kind: "inline", value: "Direct to camera." },- origin,}),canvas,]);@@ -159,7 +153,7 @@ function graph(programValue: LinkedProgram, ...contributions: readonly FragmentC{ outputs: [], candidates: [], operations: [] },...contributions,);- return sealCompiledGraph({ program: programValue.semanticDigest, ...merged });+ return sealCompiledGraph({ ...merged });}test("one FragmentInstance shares its generation Operation across all exports", () => {@@ -172,7 +166,6 @@ test("one FragmentInstance shares its generation Operation across all exports",});const compiled = graph(linked, contribution);const request = sealBuildRequest({- graph: compiled.id,targets: [{ output: "opening.audio" },{ output: "opening.visual" },@@ -221,7 +214,6 @@ test("the same Fragment instance is deterministic while distinct instances never}),);const state = start(linked, compiled, sealBuildRequest({- graph: compiled.id,targets: [{ output: "opening.audio" },{ output: "closing.audio" },diff --git a/packages/endpoint-kit/src/index.ts b/packages/endpoint-kit/src/index.tsindex 5cac69b9..f4d66db2 100644--- a/packages/endpoint-kit/src/index.ts+++ b/packages/endpoint-kit/src/index.ts@@ -12,7 +12,6 @@ import type {CredentialRef,CredentialValue,OperationFailure,- OperationIdentity,OperationProgress,EndpointOffer,RuntimeFacetRef,@@ -42,7 +41,7 @@ export type ImmediateEndpointHandler = (export type EndpointOutcome =| {readonly status: "pending";- readonly checkpoint: CanonicalValue;+ readonly handle: CanonicalValue;readonly wakeAt?: number;readonly progress?: OperationProgress;}@@ -50,12 +49,13 @@ export type EndpointOutcome =| { readonly status: "failed"; readonly failure: OperationFailure };export type EndpointStartContext = EndpointInvocationContext & {- readonly operation: OperationIdentity;+ /** Runtime-local identifier used to poll or cancel this submission. */+ readonly operation: string;};-export type EndpointResumeContext = EndpointStartContext & {- /** Undefined means the process stopped after intent was persisted but before a checkpoint existed. */- readonly checkpoint: CanonicalValue | undefined;+export type EndpointPollContext = EndpointStartContext & {+ /** Provider task state returned by start(). */+ readonly handle: CanonicalValue;};/**@@ -69,13 +69,13 @@ export type EndpointCancelOutcome =| { readonly status: "unsupported" }| { readonly status: "too-late" };-export type RecoverableEndpoint = {+export type AsyncEndpoint = {start(context: EndpointStartContext): Awaitable<EndpointOutcome>;- resume(context: EndpointResumeContext): Awaitable<EndpointOutcome>;- cancel?(context: EndpointResumeContext): Awaitable<EndpointCancelOutcome>;+ poll(context: EndpointPollContext): Awaitable<EndpointOutcome>;+ cancel?(context: EndpointPollContext): Awaitable<EndpointCancelOutcome>;};-/** Endpoint/implementation scheduling metadata; it never changes Core demand or command identity. */+/** Endpoint scheduling. It never changes Core demand. */export type EndpointScheduling = {readonly resources: readonly {readonly id: string;@@ -88,16 +88,10 @@ export type EndpointScheduling = {};};-export type EndpointRetryPolicy = {- /** Includes the first submission. A new attempt receives a new Operation id. */- readonly maxAttempts: number;-};-export type EndpointRegistrationOptions = {readonly supports?: (need: Need) => boolean;readonly scheduling?: EndpointScheduling;readonly credentials?: Readonly<Record<string, CredentialRef>>;- readonly retry?: EndpointRetryPolicy;};/** Minimal structural port implemented by a trusted execution Host. */@@ -109,11 +103,11 @@ export interface EndpointRegistrar {handler: ImmediateEndpointHandler,options?: EndpointRegistrationOptions,): void;- registerRecoverableEndpoint(+ registerAsyncEndpoint(id: string,capability: CapabilityRef,returns: TypeRef,- endpoint: RecoverableEndpoint,+ endpoint: AsyncEndpoint,options?: EndpointRegistrationOptions,): void;}@@ -150,13 +144,12 @@ export type ImmediateEndpointCapability = EndpointCapabilityBase & {readonly handler: ImmediateEndpointHandler;};-export type RecoverableEndpointCapability = EndpointCapabilityBase & {- readonly lifecycle: "recoverable";- readonly endpoint: RecoverableEndpoint;- readonly retry?: EndpointRetryPolicy;+export type AsyncEndpointCapability = EndpointCapabilityBase & {+ readonly lifecycle: "asynchronous";+ readonly endpoint: AsyncEndpoint;};-export type EndpointCapability = ImmediateEndpointCapability | RecoverableEndpointCapability;+export type EndpointCapability = ImmediateEndpointCapability | AsyncEndpointCapability;export type DefineEndpointPackageOptions = {readonly module: ModuleRef;@@ -197,7 +190,7 @@ export function defineEndpointPackage(options: DefineEndpointPackageOptions): Enassert(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 recoverable lifecycles");+ "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);@@ -307,9 +300,6 @@ export function defineEndpointPackage(options: DefineEndpointPackageOptions): En},],},- ...(capability.lifecycle === "recoverable" && capability.retry !== undefined- ? { retry: capability.retry }- : {}),};if (capability.lifecycle === "immediate") {registry.registerImmediateEndpoint(@@ -320,7 +310,7 @@ export function defineEndpointPackage(options: DefineEndpointPackageOptions): Encommon,);} else {- registry.registerRecoverableEndpoint(+ registry.registerAsyncEndpoint(options.instance,capability.capability,capability.returns,@@ -334,13 +324,13 @@ export function defineEndpointPackage(options: DefineEndpointPackageOptions): En}export function wakeAfter(- checkpoint: CanonicalValue,+ handle: CanonicalValue,delayMs: number,now = Date.now(),progress?: OperationProgress,): {readonly status: "pending";- readonly checkpoint: CanonicalValue;+ readonly handle: CanonicalValue;readonly wakeAt: number;readonly progress?: OperationProgress;} {@@ -348,7 +338,7 @@ export function wakeAfter(assert(Number.isSafeInteger(now) && now >= 0, "current time must be a non-negative epoch millisecond");return {status: "pending",- checkpoint,+ handle,wakeAt: now + delayMs,...(progress === undefined ? {} : { progress }),};diff --git a/packages/endpoint-kit/test/endpoint.test.ts b/packages/endpoint-kit/test/endpoint.test.tsindex b4d91ff9..52b15bd2 100644--- a/packages/endpoint-kit/test/endpoint.test.ts+++ b/packages/endpoint-kit/test/endpoint.test.ts@@ -1,5 +1,6 @@import assert from "node:assert/strict";import test from "node:test";+import { fixtureDigest } from "../../../test/fixture-digest.js";import {defineEndpointPackage,@@ -9,23 +10,22 @@ import type {EndpointRegistrar,EndpointRegistrationOptions,ImmediateEndpointHandler,- RecoverableEndpoint,+ AsyncEndpoint,} from "@narratage/endpoint-kit";import type { CapabilityRef, TypeRef } from "@narratage/protocol";-import { digestOf } from "@narratage/protocol";import { credentialRef } from "@narratage/runtime";import { capabilities, types } from "../../core/test/greeting-fixture.js";const implementation = {- digest: digestOf("example.provider/http-json@1"),+ digest: fixtureDigest("example.provider/http-json@1"),} as const;type CapturedRegistration = {readonly id: string;readonly capability: CapabilityRef;readonly returns: TypeRef;- readonly kind: "immediate" | "recoverable";+ readonly kind: "immediate" | "asynchronous";readonly options: EndpointRegistrationOptions;};@@ -34,8 +34,8 @@ function capturingRegistrar(registrations: CapturedRegistration[]): EndpointRegiregisterImmediateEndpoint(id, capability, returns, _handler: ImmediateEndpointHandler, options = {}) {registrations.push({ id, capability, returns, kind: "immediate", options });},- registerRecoverableEndpoint(id, capability, returns, _endpoint: RecoverableEndpoint, options = {}) {- registrations.push({ id, capability, returns, kind: "recoverable", options });+ registerAsyncEndpoint(id, capability, returns, _endpoint: AsyncEndpoint, options = {}) {+ registrations.push({ id, capability, returns, kind: "asynchronous", options });},};}@@ -88,7 +88,7 @@ test("one Endpoint definition generates Manifest, instance, offers and host-neuttest("wakeAfter turns polling policy into an explicit Runtime wake hint", () => {assert.deepEqual(wakeAfter({ job: "123" }, 5_000, 10_000), {status: "pending",- checkpoint: { job: "123" },+ handle: { job: "123" },wakeAt: 15_000,});});diff --git a/packages/estimate/src/manifest.ts b/packages/estimate/src/manifest.tsindex c9ae1a4e..88cb9048 100644--- a/packages/estimate/src/manifest.ts+++ b/packages/estimate/src/manifest.ts@@ -1,6 +1,5 @@import { speechDependency, speechTypes } from "@narratage/speech";import { textDependency, textTypes } from "@narratage/text";-import { digestOf } from "@narratage/protocol";import type { ModuleManifest, ProducerRef, TypeRef, ValueSchema } from "@narratage/protocol";import { svsManifest, svsRecipeType } from "@narratage/svs";diff --git a/packages/estimate/src/program.ts b/packages/estimate/src/program.tsindex 7bc98eb6..c1464e20 100644--- a/packages/estimate/src/program.ts+++ b/packages/estimate/src/program.ts@@ -1,7 +1,6 @@import type { Text } from "@narratage/text";import { assertSpeechDurationIdentity, sealSpeechDuration } from "@narratage/speech";import type { SpeechDuration } from "@narratage/speech";-import { digestOf } from "@narratage/protocol";import { dictionary as cmuPronouncingDictionary } from "cmu-pronouncing-dictionary";import type {diff --git a/packages/film/src/manifest.ts b/packages/film/src/manifest.tsindex d2ea25ea..62bc799d 100644--- a/packages/film/src/manifest.ts+++ b/packages/film/src/manifest.ts@@ -1,6 +1,5 @@import { programSpaceDependency, programSpaceTypes } from "@narratage/program-space";import { audioTrackSchema, compositionDependency, compositionTypes, visualTrackSchema } from "@narratage/composition";-import { digestOf } from "@narratage/protocol";import type { ModuleManifest, ProducerRef, TypeRef, ValueSchema } from "@narratage/protocol";import { spatialDependency, spatialTypes } from "@narratage/spatial";import { svsManifest, svsModuleRef } from "@narratage/svs";diff --git a/packages/film/src/program.ts b/packages/film/src/program.tsindex 5c691eaf..ea485ca1 100644--- a/packages/film/src/program.ts+++ b/packages/film/src/program.ts@@ -4,7 +4,6 @@ import { assertCanvasSpace } from "@narratage/spatial";import type { CanvasSpace } from "@narratage/spatial";import { assertAudioTrackIdentity, assertCompositionIdentity, assertVisualTrackIdentity, sealComposition } from "@narratage/composition";import type { AudioTrack, Composition, Track, VisualTrack } from "@narratage/composition";-import { digestOf } from "@narratage/protocol";import type { FilmProgram, FilmTrackSet } from "./types.js";diff --git a/packages/film/src/surface.ts b/packages/film/src/surface.tsindex 93f3d118..2d3124ef 100644--- a/packages/film/src/surface.ts+++ b/packages/film/src/surface.ts@@ -3,7 +3,6 @@ import type { ProgramSpace } from "@narratage/program-space";import { spatialTypes } from "@narratage/spatial";import { compositionTypes } from "@narratage/composition";import type { AudioTrack, Track, VisualTrack } from "@narratage/composition";-import { digestOf } from "@narratage/protocol";import { svsRecipeType } from "@narratage/svs";import type { SvsRecipe } from "@narratage/svs";import type {@@ -117,9 +116,9 @@ export const decodeFilmSurface: StructuredSurfaceHandler = ({ element, resolveReclearColor: appearance.clearColor,});- const tracks = trackChildren(element).map((child) => {+ const tracks = trackChildren(element).map((child, index) => {const source = requiredReference(child, "source", resolveReference);- const name = `track-${digestOf({ ref: source.ref, type: source.type }).slice("sha256:".length, 28)}`;+ const name = `track-${index + 1}`;return { name, kind: trackKind(source), source };});if (new Set(tracks.map((track) => track.name)).size !== tracks.length) {diff --git a/packages/film/test/film.test.ts b/packages/film/test/film.test.tsindex a6c2f348..c948cc93 100644--- a/packages/film/test/film.test.ts+++ b/packages/film/test/film.test.ts@@ -7,10 +7,10 @@ import type { FontArtifactRef } from "@narratage/media";import { sealCanvasSpace, spatialTypes } from "@narratage/spatial";import assert from "node:assert/strict";import test from "node:test";+import { fixtureDigest } from "../../../test/fixture-digest.js";import {createResolvedClosure,- digestOf,link,sealBuildRequest,sealCompiledGraph,@@ -58,7 +58,7 @@ const filmProgram = sealFilmProgram({});const titleFont: FontArtifactRef = {sources: [{ artifact: {- kind: "blob", digest: digestOf("film-test-title-font"), size: 1, mediaType: "font/woff2",+ kind: "blob", digest: fixtureDigest("film-test-title-font"), size: 1, mediaType: "font/woff2",} }],weight: 800,style: "normal",@@ -131,16 +131,13 @@ const closure = createResolvedClosure([filmManifest,typographyTrackManifest,]);-const origin = {- kind: "authored" as const,-};const records = await Promise.all([- sealRecord({ id: "space", type: programSpaceTypes.programSpace, value: stored(space), origin }),- sealRecord({ id: "canvas", type: spatialTypes.canvas, value: stored(canvas), origin }),- sealRecord({ id: "film-program", type: filmTypes.program, value: stored(filmProgram), origin }),- sealRecord({ id: "text-program", type: typographyTrackTypes.program, value: stored(textProgram), origin }),- sealRecord({ id: "background", type: compositionTypes.visualTrack, value: stored(background), origin }),- sealRecord({ id: "audio", type: compositionTypes.audioTrack, value: stored(audio), origin }),+ sealRecord({ id: "space", type: programSpaceTypes.programSpace, value: stored(space) }),+ sealRecord({ id: "canvas", type: spatialTypes.canvas, value: stored(canvas) }),+ sealRecord({ id: "film-program", type: filmTypes.program, value: stored(filmProgram) }),+ sealRecord({ id: "text-program", type: typographyTrackTypes.program, value: stored(textProgram) }),+ sealRecord({ id: "background", type: compositionTypes.visualTrack, value: stored(background) }),+ sealRecord({ id: "audio", type: compositionTypes.audioTrack, value: stored(audio) }),].map(async (record) => await admitRecord(closure, record, validatorRegistry())));const linked = link(closure, records);@@ -194,11 +191,10 @@ const merged = mergeFragmentContributions(filmContribution,hyperframesContribution,);-const graph: CompiledGraph = sealCompiledGraph({ program: linked.semanticDigest, ...merged });+const graph: CompiledGraph = sealCompiledGraph({ ...merged });function build(target: string) {return start(linked, graph, sealBuildRequest({- graph: graph.id,targets: [{ output: target }],}));}diff --git a/packages/film/test/surface.test.ts b/packages/film/test/surface.test.tsindex b79390df..b00dd1cc 100644--- a/packages/film/test/surface.test.ts+++ b/packages/film/test/surface.test.ts@@ -5,8 +5,9 @@ import { compositionDependency, compositionTypes, sealAudioTrack, sealVisualTracimport type { Track } from "@narratage/composition";import assert from "node:assert/strict";import test from "node:test";+import { fixtureDigest } from "../../../test/fixture-digest.js";-import { createResolvedClosure, digestOf, sealBuildRequest, start } from "@narratage/core";+import { createResolvedClosure, sealBuildRequest, start } from "@narratage/core";import {AuthorFrontendRegistry,compileSourceClosure,@@ -36,7 +37,7 @@ import {import { createRecordAdmitter, TypeValidatorRegistry } from "@narratage/validation";const fixtureModule = { name: "example.film-fixture", version: "1" } as const;-const fixtureSurfaceDigest = digestOf("example.film-fixture/inputs-surface@1");+const fixtureSurfaceDigest = fixtureDigest("example.film-fixture/inputs-surface@1");const fixtureSurface = {name: "inputs", tag: "Inputs", mode: "structured",outputs: [programSpaceTypes.programSpace, compositionTypes.visualTrack, compositionTypes.audioTrack],@@ -97,7 +98,7 @@ const validStyles = `<sheet version="1">}</sheet>`;-async function compileFilm(options: { readonly reverse?: boolean; readonly styles?: string } = {}) {+async function compileFilm(options: { readonly styles?: string } = {}) {const surfaces = new MarkupSurfaceRegistry();surfaces.registerStructured({ module: fixtureModule, declaration: fixtureSurface, handler: ({ element }) => ({records: [@@ -129,9 +130,6 @@ async function compileFilm(options: { readonly reverse?: boolean; readonly style}));frontends.register(svsFrontend);- const tracks = options.reverse === true- ? `<film:Track source={audio}/><film:Track source={visual}/>`- : `<film:Track source={visual}/><film:Track source={audio}/>`;return await compileSourceClosure({entry: source("/project/main.svml", `<svml><import as="fixture" from="example.film-fixture@1"/>@@ -140,7 +138,9 @@ async function compileFilm(options: { readonly reverse?: boolean; readonly style<import as="studio" source="./studio.svs"/><fixture:Inputs/><space:Canvas id="vertical" width="1080" height="1920"/>- <film:Film id="main" canvas={vertical} space={space} appearance={studio.film.vertical}>${tracks}</film:Film>+ <film:Film id="main" canvas={vertical} space={space} appearance={studio.film.vertical}>+ <film:Track source={visual}/><film:Track source={audio}/>+ </film:Film></svml>`),closure,frontends,@@ -162,7 +162,6 @@ test("the official Film Surface validates SVS and lowers dynamic peer Tracks", aconst target = resolveCompiledSourceExport(compiled, "main.composition", compositionTypes.composition);assert.equal(target.ref.kind, "logical-output");const build = start(compiled.program, compiled.graph, sealBuildRequest({- graph: compiled.graph.id,targets: [{ output: target.ref.kind === "logical-output" ? target.ref.id : "" }],}));assert.deepEqual(build.plan.steps.map((step) => step.producer.name).sort(), [@@ -173,12 +172,6 @@ test("the official Film Surface validates SVS and lowers dynamic peer Tracks", a].sort());});-test("Film child order remains organizational, not graph meaning", async () => {- const normal = await compileFilm();- const reversed = await compileFilm({ reverse: true });- assert.equal(normal.graph.id, reversed.graph.id);-});-test("Film rejects an invalid package-owned Recipe during check", async () => {await assert.rejects(compileFilm({ styles: `<sheet version="1">film.vertical { width: 1080; }</sheet>` }),diff --git a/packages/fonts-open/src/manifest.ts b/packages/fonts-open/src/manifest.tsindex 0cac1cd4..132c8a9c 100644--- a/packages/fonts-open/src/manifest.ts+++ b/packages/fonts-open/src/manifest.ts@@ -1,5 +1,4 @@import { mediaDependency, mediaTypes } from "@narratage/media";-import { digestOf } from "@narratage/protocol";import type { ModuleManifest } from "@narratage/protocol";export const fontsOpenModuleRef = { name: "@narratage/fonts-open", version: "1" } as const;diff --git a/packages/gemini-omni/src/index.ts b/packages/gemini-omni/src/index.tsindex 593e2d9f..2f8626be 100644--- a/packages/gemini-omni/src/index.ts+++ b/packages/gemini-omni/src/index.ts@@ -1,7 +1,6 @@import { sealGenerationPortRequest, sealGenerationPortTable } from "@narratage/generation";import type { GenerationPortTable, GenerationPortValue, GenerationRequest } from "@narratage/generation";import { defineExactModelModule } from "@narratage/model-kit";-import { digestOf } from "@narratage/protocol";export const geminiOmniModuleRef = { name: "@narratage/gemini-omni", version: "1" } as const;diff --git a/packages/generation/src/manifest.ts b/packages/generation/src/manifest.tsindex 0e7371e2..c628fd18 100644--- a/packages/generation/src/manifest.ts+++ b/packages/generation/src/manifest.ts@@ -2,7 +2,6 @@ import {artifactDependency,artifactTypes,} from "@narratage/artifact";-import { digestOf } from "@narratage/protocol";import type { ModuleManifest, ProducerRef, TypeRef } from "@narratage/protocol";import { generatedAudioSetSchema, generatedImageSetSchema, generatedVideoSetSchema } from "./schema.js";diff --git a/packages/generation/src/request.ts b/packages/generation/src/request.tsindex 0f52267a..a6dc98b1 100644--- a/packages/generation/src/request.ts+++ b/packages/generation/src/request.ts@@ -344,7 +344,7 @@ export function sealGenerationMediaBinding(/*** Attach one Blob supplied by a real graph edge. The returned draft contains- * the Provider-facing value, while Derivation preserves where that Blob came+ * the Provider-facing value, while the Graph preserves where that Blob came* from; no lineage or project metadata is copied through the payload.*/export function bindGenerationMedia(diff --git a/packages/gpt-image/src/index.ts b/packages/gpt-image/src/index.tsindex e19d7723..41fe0274 100644--- a/packages/gpt-image/src/index.ts+++ b/packages/gpt-image/src/index.ts@@ -5,7 +5,6 @@ import {} from "@narratage/generation";import type { GenerationPortTable, GenerationPortValue, GenerationRequest } from "@narratage/generation";import { defineExactModelModule } from "@narratage/model-kit";-import { digestOf } from "@narratage/protocol";import { imageTransformModuleRef, imageTransformTypes } from "@narratage/image-transform";export const gptImageModuleRef = { name: "@narratage/gpt-image", version: "1" } as const;diff --git a/packages/gpt-image/test/broll-graph.test.ts b/packages/gpt-image/test/broll-graph.test.tsindex 4c49cfa8..2de7ecec 100644--- a/packages/gpt-image/test/broll-graph.test.ts+++ b/packages/gpt-image/test/broll-graph.test.ts@@ -1,10 +1,10 @@import assert from "node:assert/strict";import test from "node:test";+import { fixtureDigest } from "../../../test/fixture-digest.js";import { artifactManifest, artifactTypes } from "@narratage/artifact";import {createResolvedClosure,- digestOf,link,sealBuildRequest,sealCompiledGraph,@@ -43,13 +43,9 @@ import { speechManifest } from "@narratage/speech";import { spatialManifest } from "@narratage/spatial";import { textManifest } from "@narratage/text";-const origin = {- kind: "authored" as const,-};-const image = (name: string) => ({kind: "blob" as const,- digest: digestOf(`image:${name}`),+ digest: fixtureDigest(`image:${name}`),size: 100,mediaType: "image/png",});@@ -177,7 +173,7 @@ function fixture() {]),outputs: { video: "montage.video" },}];- const program = link(closure, records.map((record) => sealRecord({ ...record, origin })));+ const program = link(closure, records.map((record) => sealRecord(record)));const fragments = new Map([...Object.values(gptFragments).map((fragment) => [fragment.id, fragment] as const),[seedanceFragment.id, seedanceFragment] as const,@@ -190,7 +186,6 @@ function fixture() {test("the common B-roll topology is one graph with one shared holding image", () => {const { program, graph } = fixture();const state = start(program, graph, sealBuildRequest({- graph: graph.id,targets: [{ output: "montage.video" }],}));const producers = state.plan.steps.map((step) => step.producer.name);@@ -205,9 +200,12 @@ test("the common B-roll topology is one graph with one shared holding image", ()test("an explicitly selected holding-image Candidate prunes only that branch", () => {const { program, graph } = fixture();- const candidate = createProvidedCandidate({ type: artifactTypes.blob, value: image("approved-holding") });+ const candidate = createProvidedCandidate({+ id: "approved-holding",+ type: artifactTypes.blob,+ value: image("approved-holding"),+ });const realized = sealCompiledGraph({- program: graph.program,outputs: graph.outputs.map((item) => item.id === "holding.image"? { ...item, primary: candidate.id }: item),@@ -215,7 +213,6 @@ test("an explicitly selected holding-image Candidate prunes only that branch", (operations: graph.operations,});const state = start(program, realized, sealBuildRequest({- graph: realized.id,targets: [{ output: "montage.video" }],}));const producers = state.plan.steps.map((step) => step.producer.name);diff --git a/packages/grok-imagine/src/index.ts b/packages/grok-imagine/src/index.tsindex 594cf488..82814563 100644--- a/packages/grok-imagine/src/index.ts+++ b/packages/grok-imagine/src/index.ts@@ -1,7 +1,6 @@import { sealGenerationPortRequest, sealGenerationPortTable } from "@narratage/generation";import type { GenerationPortTable, GenerationPortValue, GenerationRequest } from "@narratage/generation";import { defineExactModelModule } from "@narratage/model-kit";-import { digestOf } from "@narratage/protocol";export const grokImagineModuleRef = { name: "@narratage/grok-imagine", version: "1" } as const;export const grokImagineModels = ["grok-imagine-video", "grok-imagine-video-1.5-preview"] as const;diff --git a/packages/host/src/facet.ts b/packages/host/src/facet.tsindex 9dcb6efa..50abb604 100644--- a/packages/host/src/facet.ts+++ b/packages/host/src/facet.ts@@ -1,16 +1,6 @@-import type { CanonicalValue } from "@narratage/protocol";--/**- * One executable package facet understood by an exact Host ABI.- *- * Package loading binds the ABI and canonical identity without interpreting the implementation.- * Only the selected Host may validate and install the opaque implementation value.- */+/** One package contribution understood only by the Host that owns its ABI. */export type HostFacet = {readonly abi: string;- /** Logical names this exact facet offers through its opaque Host ABI. */readonly offers?: readonly string[];- /** ABI-owned inert declaration, present only when the ABI has facts beyond its offers. */- readonly identity?: CanonicalValue;readonly implementation: unknown;};diff --git a/packages/host/src/frontend.ts b/packages/host/src/frontend.tsindex 2342d174..06177331 100644--- a/packages/host/src/frontend.ts+++ b/packages/host/src/frontend.ts@@ -1,5 +1,3 @@-import { canonicalStringify } from "@narratage/protocol";-import type { HostFacet } from "./facet.js";export type FrontendImplementation = {@@ -11,10 +9,10 @@ export type FrontendHostFacet<Frontend extends FrontendImplementation,> = HostFacet & {readonly offers: readonly [string];- readonly identity: {+ readonly implementation: {readonly kind: Kind;+ readonly frontend: Frontend;};- readonly implementation: Frontend;};/** Build the common inert declaration shared by every self-described Source Frontend. */@@ -29,10 +27,10 @@ export function createFrontendHostFacet<return {abi,offers: [frontend.id],- identity: {+ implementation: {kind,+ frontend,},- implementation: frontend,};}@@ -42,24 +40,17 @@ export function frontendsFromHostFacets<Frontend extends FrontendImplementation,>(abi: string, kind: Kind, facets: readonly HostFacet[]): readonly Frontend[] {const values: Frontend[] = [];- for (const opaque of facets) {- if (opaque.abi !== abi- || opaque.identity === undefined- || opaque.identity === null- || typeof opaque.identity !== "object"- || Array.isArray(opaque.identity)- || (opaque.identity as { readonly kind?: unknown }).kind !== kind) continue;- const facet = opaque as FrontendHostFacet<Kind, Frontend>;- const frontend = facet.implementation;- if (frontend === null || typeof frontend !== "object" || Array.isArray(frontend)) {+ for (const facet of facets) {+ if (facet.abi !== abi || facet.implementation === null+ || typeof facet.implementation !== "object" || Array.isArray(facet.implementation)) continue;+ const implementation = facet.implementation as { readonly kind?: unknown; readonly frontend?: unknown };+ if (implementation.kind !== kind) continue;+ const frontend = implementation.frontend;+ if (frontend === null || typeof frontend !== "object" || Array.isArray(frontend)+ || typeof (frontend as { readonly id?: unknown }).id !== "string") {throw new Error(`${kind} Frontend Host facet has an invalid implementation`);}- const expected = createFrontendHostFacet(abi, kind, frontend);- if (canonicalStringify(facet.offers) !== canonicalStringify(expected.offers)- || canonicalStringify(facet.identity) !== canonicalStringify(expected.identity)) {- throw new Error(`${kind} Frontend Host facet ${frontend.id} differs from its locked identity`);- }- values.push(frontend);+ values.push(frontend as Frontend);}return values;}diff --git a/packages/hyperframes/src/document.ts b/packages/hyperframes/src/document.tsindex 5f5d04c1..a7dda584 100644--- a/packages/hyperframes/src/document.ts+++ b/packages/hyperframes/src/document.ts@@ -15,7 +15,7 @@ import type {VisualStyleDeclaration,VisualTrack,} from "@narratage/composition";-import { digestOf, isDigest } from "@narratage/protocol";+import { canonicalStringify, isDigest } from "@narratage/protocol";import type { BlobRef, Digest } from "@narratage/protocol";import { VISUAL_IR_V1 } from "@narratage/visual-ir";@@ -198,11 +198,11 @@ function attributes(values: readonly VisualAttribute[] | undefined): string {}function stableDomId(parts: readonly string[]): string {- return `narratage-${digestOf(parts).slice("sha256:".length, "sha256:".length + 20)}`;+ return `narratage-${Buffer.from(JSON.stringify(parts)).toString("base64url")}`;}function exactFontFamily(font: FontArtifactRef): string {- return stableDomId(["font", digestOf(font)]);+ return stableDomId(["font", canonicalStringify(font)]);}function exactFontStyle(element: VisualElement): string[] {@@ -504,7 +504,7 @@ function collectSurfaces(composition: Composition): CompositableSurfaceRef[] {if (element.kind !== "surface") continue;const surface = structuredClone(element.surface);const existing = surfaces.get(surface.artifact.digest);- if (existing !== undefined && digestOf(existing) !== digestOf(surface)) {+ if (existing !== undefined && canonicalStringify(existing) !== canonicalStringify(surface)) {throw new Error(`HyperFrames Surface ${surface.artifact.digest} has conflicting declarations.`);}surfaces.set(surface.artifact.digest, surface);@@ -522,7 +522,7 @@ function collectFonts(composition: Composition): FontArtifactRef[] {for (const present of track.presents) {for (const element of present.elements) {if (element.kind !== "text") continue;- for (const font of element.fonts ?? []) fonts.set(digestOf(font), font);+ for (const font of element.fonts ?? []) fonts.set(canonicalStringify(font), font);}}}@@ -531,7 +531,7 @@ function collectFonts(composition: Composition): FontArtifactRef[] {for (const present of track.presents) {for (const element of present.elements) {if (element.kind !== "text-flow" && element.kind !== "path-text") continue;- for (const font of collectTerminalTextFonts(element)) fonts.set(digestOf(font), font);+ for (const font of collectTerminalTextFonts(element)) fonts.set(canonicalStringify(font), font);}}}diff --git a/packages/hyperframes/src/manifest.ts b/packages/hyperframes/src/manifest.tsindex 4e0b6b00..f732d08e 100644--- a/packages/hyperframes/src/manifest.ts+++ b/packages/hyperframes/src/manifest.ts@@ -1,7 +1,6 @@import { programSpaceDependency, programSpaceTypes } from "@narratage/program-space";import { compositionDependency, compositionTypes } from "@narratage/composition";import { compositableSurfaceSchema, mediaDependency } from "@narratage/media";-import { digestOf } from "@narratage/protocol";import type { ModuleManifest, ProducerRef, TypeRef, ValueSchema } from "@narratage/protocol";import { VISUAL_IR_V1 } from "@narratage/visual-ir";diff --git a/packages/hyperframes/src/text.ts b/packages/hyperframes/src/text.tsindex 8a134bcc..9e636656 100644--- a/packages/hyperframes/src/text.ts+++ b/packages/hyperframes/src/text.ts@@ -10,7 +10,7 @@ import type {VisualTextTypography,VisualVectorPathCommand,} from "@narratage/composition";-import { digestOf } from "@narratage/protocol";+import { canonicalStringify } from "@narratage/protocol";type TerminalTextElement = VisualTextFlowElement | VisualPathTextElement;@@ -171,7 +171,7 @@ function glyphFilterId(paint: Extract<GlyphPaintLayer, { kind: "stroke" | "shadow" | "glow" }>,context: TextRenderContext,): string {- return context.stableId([context.trackId, context.presentId, "text-glyph-filter", digestOf(paint)]);+ return context.stableId([context.trackId, context.presentId, "text-glyph-filter", canonicalStringify(paint)]);}function glyphFilterDefinition(@@ -216,7 +216,7 @@ function glyphFilterDefinitions(element: TerminalTextElement, context: TextRendefor (const inline of paragraph.inlines) if (inline.kind === "text") append(inline.style?.paints);}const filtered = layers.filter((paint): paint is Extract<GlyphPaintLayer, { kind: "stroke" | "shadow" | "glow" }> => paint.kind !== "fill");- const definitions = [...new Map(filtered.map((paint) => [digestOf(paint), paint])).values()]+ const definitions = [...new Map(filtered.map((paint) => [canonicalStringify(paint), paint])).values()].map((paint) => glyphFilterDefinition(paint, context)).join("");return definitions;}@@ -565,11 +565,11 @@ function allDocumentPaints(element: TerminalTextElement): VisualColorPaint[] {append(paragraph.style?.paints);for (const inline of paragraph.inlines) if (inline.kind === "text") append(inline.style?.paints);}- return [...new Map(result.map((paint) => [digestOf(paint), paint])).values()];+ return [...new Map(result.map((paint) => [canonicalStringify(paint), paint])).values()];}function svgPaintId(paint: VisualColorPaint, context: TextRenderContext): string {- return context.stableId([context.trackId, context.presentId, "text-paint", digestOf(paint)]);+ return context.stableId([context.trackId, context.presentId, "text-paint", canonicalStringify(paint)]);}function svgPaintValue(paint: VisualColorPaint, context: TextRenderContext): string {@@ -713,7 +713,7 @@ export function collectTerminalTextFonts(element: TerminalTextElement): FontArtiif (inline.kind === "text") result.push(...(inline.style?.typography?.fonts ?? []));}}- const unique = new Map(result.map((font) => [digestOf(font), font]));+ const unique = new Map(result.map((font) => [canonicalStringify(font), font]));return [...unique.values()];}diff --git a/packages/hyperframes/test/document.test.ts b/packages/hyperframes/test/document.test.tsindex 200453f5..85229078 100644--- a/packages/hyperframes/test/document.test.ts+++ b/packages/hyperframes/test/document.test.ts@@ -6,7 +6,6 @@ import {hyperframesTime,materializeHyperframesHtml,} from "@narratage/hyperframes";-import { digestOf } from "@narratage/protocol";import type { FontArtifactRef } from "@narratage/media";import { sealProgramSpace } from "@narratage/program-space";import { sealAudioTrack, sealComposition, sealVisualTrack } from "@narratage/composition";@@ -14,9 +13,10 @@ import type { Track } from "@narratage/composition";import { VISUAL_IR_V1 } from "@narratage/visual-ir";import assert from "node:assert/strict";import test from "node:test";+import { fixtureDigest } from "../../../test/fixture-digest.js";const fixtureFont: FontArtifactRef = {- sources: [{ artifact: { kind: "blob", digest: digestOf("hyperframes:fixture-font"), size: 1_024, mediaType: "font/woff2" } }],+ sources: [{ artifact: { kind: "blob", digest: fixtureDigest("hyperframes:fixture-font"), size: 1_024, mediaType: "font/woff2" } }],weight: 700,style: "normal",};@@ -28,13 +28,13 @@ function fixture() {});const picture = {kind: "blob" as const,- digest: digestOf("hyperframes:picture"),+ digest: fixtureDigest("hyperframes:picture"),size: 10,mediaType: "image/png",};const sound = {kind: "blob" as const,- digest: digestOf("hyperframes:sound"),+ digest: fixtureDigest("hyperframes:sound"),size: 20,mediaType: "audio/wav",};@@ -94,15 +94,8 @@ test("HyperFrames flattens generic peer visual Track Presents without absorbingconst document = compileHyperframesDocument(composition, programSpace);assert.doesNotThrow(() => assertHyperframesDocument(document));assert.equal(document.visualIr, VISUAL_IR_V1);- assert.deepEqual(document.artifacts, [- {- kind: "blob",- digest: picture.digest,- size: picture.size,- mediaType: picture.mediaType,- },- fixtureFont.sources[0]!.artifact,- ]);+ assert.deepEqual(new Set(document.artifacts.map((artifact) => artifact.digest)),+ new Set([picture.digest, fixtureFont.sources[0]!.artifact.digest]));assert.ok(document.html.indexOf('data-narratage-track-id="lower"') < document.html.indexOf('data-narratage-track-id="upper"'));assert.equal((document.html.match(/class="clip narratage-visual-present"/gu) ?? []).length, 2);assert.doesNotMatch(document.html, /<audio/u);@@ -226,14 +219,14 @@ test("content-bound fonts and typed compositable Surfaces cross the same Artifacconst font: FontArtifactRef = {sources: [{ artifact: {kind: "blob",- digest: digestOf("hyperframes:font"),+ digest: fixtureDigest("hyperframes:font"),size: 1_024,mediaType: "font/woff2",} }],weight: 700,style: "normal",};- const surfaceDigest = digestOf("hyperframes:alpha-surface");+ const surfaceDigest = fixtureDigest("hyperframes:alpha-surface");const track = sealVisualTrack({visualIr: "narratage.visual-ir@1",id: "bound-render-dependencies",@@ -300,7 +293,7 @@ test("exact timed sampling lowers loop boundaries and held frames without zero-r});const artifact = {kind: "blob" as const,- digest: digestOf("hyperframes:sampled-video"),+ digest: fixtureDigest("hyperframes:sampled-video"),size: 1_000,mediaType: "video/mp4",};diff --git a/packages/image-compose/src/manifest.ts b/packages/image-compose/src/manifest.tsindex 75cdf662..5aa0ec43 100644--- a/packages/image-compose/src/manifest.ts+++ b/packages/image-compose/src/manifest.ts@@ -1,5 +1,4 @@import { artifactDependency, artifactTypes } from "@narratage/artifact";-import { digestOf } from "@narratage/protocol";import type { ModuleManifest, ProducerRef, TypeRef, ValueSchema } from "@narratage/protocol";import { rasterCapabilities, rasterDependency } from "@narratage/raster";import { spatialDependency, spatialFrameSchema, spatialTypes } from "@narratage/spatial";@@ -44,10 +43,6 @@ export const imageComposeLayerSetSchema: ValueSchema = object({layers: { schema: { kind: "array", maxItems: 64, items: layer } },});-const registered = (digest: ReturnType<typeof digestOf>) => ({ digest });-const validator = (digest: ReturnType<typeof digestOf>) => ({- implementation: registered(digest),-});export const imageComposeMarkupSurfaces = [{name: "image", tag: "Image", mode: "structured",diff --git a/packages/image-compose/test/image-compose.test.ts b/packages/image-compose/test/image-compose.test.tsindex 8b1645ba..71ac65d7 100644--- a/packages/image-compose/test/image-compose.test.ts+++ b/packages/image-compose/test/image-compose.test.ts@@ -1,5 +1,6 @@import assert from "node:assert/strict";import test from "node:test";+import { fixtureDigest } from "../../../test/fixture-digest.js";import { artifactTypes } from "@narratage/artifact";import {@@ -11,7 +12,6 @@ import {sealImageComposeLayerSpec,sealImageComposeOptions,} from "@narratage/image-compose";-import { digestOf } from "@narratage/protocol";import { rasterCapabilities } from "@narratage/raster";import type { CanonicalValue, StoredValue, TypeRef, TypedRecord } from "@narratage/protocol";import { sealCanvasSpace, sealSpatialFrame, spatialTypes } from "@narratage/spatial";@@ -25,10 +25,10 @@ const options = sealImageComposeOptions({ background: "#11223344" });const spec = sealImageComposeLayerSpec({fit: "cover", interpolation: "lanczos", opacity: 0.75,});-const source = { kind: "blob" as const, digest: digestOf("hero-image"), size: 123, mediaType: "image/png" };+const source = { kind: "blob" as const, digest: fixtureDigest("hero-image"), size: 123, mediaType: "image/png" };function record(id: string, type: TypeRef, value: StoredValue): TypedRecord {- return { id, type, value, digest: digestOf({ id, value }), origin: { kind: "authored" } };+ return { id, type, value };}test("Image Compose is an ordered Layer graph with no privileged base image", () => {diff --git a/packages/image-transform/src/manifest.ts b/packages/image-transform/src/manifest.tsindex 39df7e8f..89a7d86a 100644--- a/packages/image-transform/src/manifest.ts+++ b/packages/image-transform/src/manifest.ts@@ -1,5 +1,4 @@import { artifactDependency, artifactTypes } from "@narratage/artifact";-import { digestOf } from "@narratage/protocol";import { rasterCapabilities, rasterDependency, rasterTransformOperationSchema } from "@narratage/raster";import type {ModuleManifest,diff --git a/packages/image-transform/test/image-transform.test.ts b/packages/image-transform/test/image-transform.test.tsindex ce503c49..0b284396 100644--- a/packages/image-transform/test/image-transform.test.ts+++ b/packages/image-transform/test/image-transform.test.ts@@ -1,5 +1,6 @@import assert from "node:assert/strict";import test from "node:test";+import { fixtureDigest } from "../../../test/fixture-digest.js";import {decodeImageTransformProgramSurface,@@ -12,7 +13,6 @@ import {} from "@narratage/image-transform";import { artifactTypes } from "@narratage/artifact";import { rasterCapabilities } from "@narratage/raster";-import { digestOf } from "@narratage/protocol";import type { CanonicalValue, TypedRecord } from "@narratage/protocol";test("the GPT Image cleanup is one explicit reusable Program", () => {@@ -71,7 +71,7 @@ test("the graph contract is exactly source plus Program to one image Need", asynconst producer = imageTransformComponent.producers[0]!;const source = {kind: "blob" as const,- digest: digestOf("image-source"),+ digest: fixtureDigest("image-source"),size: 123,mediaType: "image/png",};@@ -79,10 +79,6 @@ test("the graph contract is exactly source plus Program to one image Need", asynid,type,value,- digest: digestOf({ id, value }),- origin: {- kind: "authored",- },});const result = await producer.handler({inputs: {diff --git a/packages/markup/src/frontend.ts b/packages/markup/src/frontend.tsindex 2fe4b7dd..699a7859 100644--- a/packages/markup/src/frontend.ts+++ b/packages/markup/src/frontend.ts@@ -1,8 +1,6 @@import {canonicalize,canonicalStringify,- digestOf,- isDigest,sealRecord,} from "@narratage/core";import type {@@ -313,7 +311,6 @@ export async function decodeMarkup(source: MarkupSource, context: MarkupDecodeCoid: draft.id,type: draft.type,value: draft.value,- origin: { kind: "authored" },});records.push(record);recordsById.set(record.id, record);@@ -339,8 +336,8 @@ export async function decodeMarkup(source: MarkupSource, context: MarkupDecodeCoif (componentIds.has(draft.id)) {fail(source, "MARKUP_COMPONENT_DUPLICATE", `Duplicate author component "${draft.id}".`, draft.range.start);}- if (!isDigest(draft.fragment)) {- fail(source, "MARKUP_COMPONENT_FRAGMENT", `${draft.id} returned an invalid Fragment digest.`, draft.range.start);+ if (draft.fragment.length === 0) {+ fail(source, "MARKUP_COMPONENT_FRAGMENT", `${draft.id} returned an empty Fragment id.`, draft.range.start);}componentIds.add(draft.id);componentRanges.set(draft.id, draft.range);@@ -361,11 +358,11 @@ export async function decodeMarkup(source: MarkupSource, context: MarkupDecodeCo}}for (const fragment of output.fragments) {- if (fragment.format !== "narratage.fragment@1" || !isDigest(fragment.id)) {+ if (fragment.format !== "narratage.fragment@1" || fragment.id.length === 0) {fail(source,"MARKUP_FRAGMENT_IDENTITY",- `Surface ${moduleKey(bound.module.manifest)}#${registered.surface} returned an invalid Graph Fragment identity.`,+ `Surface ${moduleKey(bound.module.manifest)}#${registered.surface} returned an invalid Graph Fragment.`,opening.start,);}diff --git a/packages/markup/src/host-facet.ts b/packages/markup/src/host-facet.tsindex 9886352c..24dd69d7 100644--- a/packages/markup/src/host-facet.ts+++ b/packages/markup/src/host-facet.ts@@ -57,35 +57,6 @@ function assert(condition: unknown, message: string): asserts condition {if (!condition) throw new Error(message);}-function object(value: unknown, subject: string): Record<string, unknown> {- assert(value !== null && typeof value === "object" && !Array.isArray(value), `${subject} must be an object`);- return value as Record<string, unknown>;-}--function exact(value: Record<string, unknown>, allowed: readonly string[], subject: string): void {- const unknown = Object.keys(value).filter((key) => !allowed.includes(key));- assert(unknown.length === 0, `${subject} does not accept ${unknown[0]}`);-}--function nonEmptyString(value: unknown, subject: string): string {- assert(typeof value === "string" && value.trim().length > 0, `${subject} must be a non-empty string`);- return value;-}--function typeRef(value: unknown, subject: string): TypeRef {- const item = object(value, subject);- exact(item, ["module", "name"], subject);- const module = object(item.module, `${subject}.module`);- exact(module, ["name", "version"], `${subject}.module`);- return {- module: {- name: nonEmptyString(module.name, `${subject}.module.name`),- version: nonEmptyString(module.version, `${subject}.module.version`),- },- name: nonEmptyString(item.name, `${subject}.name`),- };-}-/** Package-owned Markup Surface declaration carried through the syntax-neutral package loader. */export function createMarkupSurfaceHostFacet(options: RawMarkupSurfaceHostFacetOptions): HostFacet;export function createMarkupSurfaceHostFacet(options: StructuredMarkupSurfaceHostFacetOptions): HostFacet;@@ -102,16 +73,26 @@ export function createMarkupSurfaceHostFacet(options: MarkupSurfaceHostFacetOpti"Markup Surface module identity is invalid");assert(declaration.surface.trim().length > 0, "Markup Surface name is empty");assert(declaration.tag.trim().length > 0, "Markup Surface tag is empty");+ const implementation: RegisteredSurface = declaration.mode === "raw"+ ? {+ module: options.module,+ surface: declaration.surface,+ tag: declaration.tag,+ outputs: declaration.outputs,+ mode: "raw",+ handler: options.handler as RawSurfaceHandler,+ }+ : {+ module: options.module,+ surface: declaration.surface,+ tag: declaration.tag,+ outputs: declaration.outputs,+ mode: "structured",+ handler: options.handler as StructuredSurfaceHandler,+ };return {abi: markupSurfaceHostFacetAbi,- identity: {- module: options.module,- surface: declaration.surface,- tag: declaration.tag,- outputs: declaration.outputs,- mode: declaration.mode,- },- implementation: options.handler,+ implementation,};}@@ -122,39 +103,6 @@ export function installMarkupSurfaceHostFacets(): void {for (const facet of facets) {if (facet.abi !== markupSurfaceHostFacetAbi) continue;- const identity = object(facet.identity, "Markup Surface Host facet identity");- exact(identity, ["module", "surface", "tag", "outputs", "mode"],- "Markup Surface Host facet identity");- const module = object(identity.module, "Markup Surface Host facet module");- exact(module, ["name", "version"], "Markup Surface Host facet module");- const moduleRef = {- name: nonEmptyString(module.name, "Markup Surface Host facet module name"),- version: nonEmptyString(module.version, "Markup Surface Host facet module version"),- };- const surface = nonEmptyString(identity.surface, "Markup Surface Host facet surface");- const tag = nonEmptyString(identity.tag, "Markup Surface Host facet tag");- assert(Array.isArray(identity.outputs), "Markup Surface Host facet outputs must be an array");- const outputs = identity.outputs.map((item, index) => typeRef(item, `Markup Surface Host facet outputs[${index}]`));- assert(typeof facet.implementation === "function", "Markup Surface Host facet implementation must be a function");- assert(identity.mode === "raw" || identity.mode === "structured", "Markup Surface Host facet mode is invalid");- if (identity.mode === "raw") {- registry.register({- module: moduleRef,- surface,- tag,- outputs,- mode: "raw",- handler: facet.implementation as RawSurfaceHandler,- });- } else {- registry.register({- module: moduleRef,- surface,- tag,- outputs,- mode: "structured",- handler: facet.implementation as StructuredSurfaceHandler,- });- }+ registry.register(facet.implementation as RegisteredSurface);}}diff --git a/packages/markup/src/registry.ts b/packages/markup/src/registry.tsindex 1189401b..072250d8 100644--- a/packages/markup/src/registry.ts+++ b/packages/markup/src/registry.ts@@ -20,17 +20,17 @@ export class MarkupSurfaceRegistry implements MarkupSurfaceRegistryLike {register(value: RegisteredSurface): void {if (value.module.name.trim().length === 0 || value.module.version.trim().length === 0) {- throw new Error("Surface module identity is invalid");+ throw new Error("Surface module is invalid");}if (value.surface.trim().length === 0) throw new Error("Surface name is empty");if (value.tag.trim().length === 0) throw new Error("Surface tag is empty");if (value.mode !== "raw" && value.mode !== "structured") throw new Error("Surface mode is invalid");- const identity = key(value.module, value.surface);- if (this.#values.has(identity)) throw new Error(`Surface ${identity} is already registered`);+ const surfaceKey = key(value.module, value.surface);+ if (this.#values.has(surfaceKey)) throw new Error(`Surface ${surfaceKey} is already registered`);const module = `${value.module.name}@${value.module.version}`;const tag = `${module}#${value.tag}`;if (this.#tags.has(tag)) throw new Error(`Surface tag ${value.tag} is already registered for ${module}`);- this.#values.set(identity, value);+ this.#values.set(surfaceKey, value);this.#tags.add(tag);const surfaces = this.#byModule.get(module) ?? [];surfaces.push(value);diff --git a/packages/markup/test/author-integration.test.ts b/packages/markup/test/author-integration.test.tsindex d7a586ee..d279b923 100644--- a/packages/markup/test/author-integration.test.ts+++ b/packages/markup/test/author-integration.test.ts@@ -1,9 +1,9 @@import assert from "node:assert/strict";import test from "node:test";+import { fixtureDigest } from "../../../test/fixture-digest.js";import {createResolvedClosure,- digestOf,link,sealBuildRequest,start,@@ -35,9 +35,9 @@ const measurementType = { module: laboratory, name: "Measurement" } satisfies Tyconst reportType = { module: laboratory, name: "Report" } satisfies TypeRef;const measureProducer = { module: laboratory, name: "measure" } satisfies ProducerRef;const reportProducer = { module: laboratory, name: "write-report" } satisfies ProducerRef;-const sampleSurfaceDigest = digestOf("example.text-laboratory/sample-surface@1");-const measureSurfaceDigest = digestOf("example.text-laboratory/measure-surface@1");-const reportSurfaceDigest = digestOf("example.text-laboratory/report-surface@1");+const sampleSurfaceDigest = fixtureDigest("example.text-laboratory/sample-surface@1");+const measureSurfaceDigest = fixtureDigest("example.text-laboratory/measure-surface@1");+const reportSurfaceDigest = fixtureDigest("example.text-laboratory/report-surface@1");const laboratorySurfaces = [{ name: "sample", tag: "Sample", mode: "structured", outputs: [sampleType] },{ name: "measure", tag: "Measure", mode: "structured", outputs: [] },@@ -222,7 +222,6 @@ test("Markup Surfaces compile forward author references into a Core BuildPlan",const catalog = new Map(decoded.fragments.map((fragment) => [fragment.id, fragment]));const elaborated = elaborateAuthorGraph(program, decoded.components, (id) => catalog.get(id));const state = start(program, elaborated, sealBuildRequest({- graph: elaborated.id,targets: [{ output: "final.result" }],}));diff --git a/packages/media-execution/src/execute.ts b/packages/media-execution/src/execute.tsindex e1056330..9c5b7ed0 100644--- a/packages/media-execution/src/execute.ts+++ b/packages/media-execution/src/execute.ts@@ -27,8 +27,7 @@ import {import type { AudioProgramClip, AudioProgramPlan, MediaTransformOperation } from "@narratage/media-pipeline";import {canonicalize,- digestOf,-} from "@narratage/protocol";+ } from "@narratage/protocol";import type { BlobRef, CanonicalValue, StoredValue } from "@narratage/protocol";import { parseMediaInspection } from "./probe.js";@@ -158,19 +157,16 @@ async function stageArtifact(const chunks = await env.artifacts.open(source);assert(chunks !== undefined, `Media source ${source.digest} is unavailable`);const file = await open(path, "w");- const hash = createHash("sha256");let size = 0;try {for await (const chunk of chunks) {await file.write(chunk);- hash.update(chunk);size += chunk.byteLength;}} finally {await file.close();}assert(size === source.size, `Media source ${source.digest} size differs`);- assert(`sha256:${hash.digest("hex")}` === source.digest, `Media source ${source.digest} digest differs`);}function timestampFraction(value: MediaTimestamp): { numerator: bigint; denominator: bigint } {diff --git a/packages/media-execution/test/probe.test.ts b/packages/media-execution/test/probe.test.tsindex 33726c2a..186d49cf 100644--- a/packages/media-execution/test/probe.test.ts+++ b/packages/media-execution/test/probe.test.ts@@ -1,7 +1,7 @@import assert from "node:assert/strict";import test from "node:test";+import { fixtureDigest } from "../../../test/fixture-digest.js";-import { digestOf } from "@narratage/protocol";import { parseMediaInspection } from "@narratage/media-execution";@@ -9,7 +9,7 @@ test("probe derives the last decoded-unit duration instead of adding whole-streaconst inspection = parseMediaInspection({source: {kind: "blob",- digest: digestOf("probe-duration-fixture"),+ digest: fixtureDigest("probe-duration-fixture"),size: 1,mediaType: "video/mp4",},@@ -59,7 +59,7 @@ test("probe derives the last decoded-unit duration instead of adding whole-streatest("probe preserves square-pixel and quarter-turn facts needed before Spatial", () => {const source = {kind: "blob" as const,- digest: digestOf("probe-display-geometry-fixture"),+ digest: fixtureDigest("probe-display-geometry-fixture"),size: 1,mediaType: "video/mp4",};diff --git a/packages/media-pipeline/src/manifest.ts b/packages/media-pipeline/src/manifest.tsindex 0a47540c..4ccbc1ec 100644--- a/packages/media-pipeline/src/manifest.ts+++ b/packages/media-pipeline/src/manifest.ts@@ -4,7 +4,6 @@ import { programSpaceDependency, programSpaceTypes } from "@narratage/program-spimport { speechDependency, speechTypes } from "@narratage/speech";import { compositionDependency, compositionTypes } from "@narratage/composition";import { artifactTypes } from "@narratage/artifact";-import { digestOf } from "@narratage/protocol";import type {CapabilityRef,ModuleManifest,diff --git a/packages/media-pipeline/test/selection.test.ts b/packages/media-pipeline/test/selection.test.tsindex c055d95f..a0c88278 100644--- a/packages/media-pipeline/test/selection.test.ts+++ b/packages/media-pipeline/test/selection.test.ts@@ -2,8 +2,8 @@ import { sealMediaInspection, verifyMediaStreamSelection } from "@narratage/mediimport type { MediaInspection, MediaVideoStream } from "@narratage/media";import assert from "node:assert/strict";import test from "node:test";+import { fixtureDigest } from "../../../test/fixture-digest.js";-import { digestOf } from "@narratage/protocol";import {sealMediaSelectionRequest,@@ -12,7 +12,7 @@ import {const source = {kind: "blob" as const,- digest: digestOf("grok-container"),+ digest: fixtureDigest("grok-container"),size: 123,mediaType: "video/mp4",};diff --git a/packages/media-track/src/manifest.ts b/packages/media-track/src/manifest.tsindex 6402e0f6..50b6328a 100644--- a/packages/media-track/src/manifest.ts+++ b/packages/media-track/src/manifest.ts@@ -8,7 +8,6 @@ import {import { narrativeDependency, narrativeTypes } from "@narratage/narrative";import { programSpaceDependency, programSpaceTypes } from "@narratage/program-space";import { mediaPipelineManifest, mediaPipelineModuleRef } from "@narratage/media-pipeline";-import { digestOf } from "@narratage/protocol";import type { ModuleManifest, ProducerRef, TypeRef, ValueSchema } from "@narratage/protocol";import { semanticMapDependency, semanticMapTypes } from "@narratage/semantic-map";import {@@ -266,9 +265,6 @@ export const mediaTrackHeaderSchema: ValueSchema = object({id: { schema: string },});-const validator = (digest: ReturnType<typeof digestOf>) => ({- implementation: { digest },-});const itemInputs = [{ name: "set", type: mediaTrackTypes.set }, { name: "header", type: mediaTrackTypes.header },{ name: "space", type: programSpaceTypes.programSpace }, { name: "canvas", type: spatialTypes.canvas },diff --git a/packages/media-track/src/program.ts b/packages/media-track/src/program.tsindex 3f773abb..2390e360 100644--- a/packages/media-track/src/program.ts+++ b/packages/media-track/src/program.ts@@ -13,7 +13,7 @@ import {programSpaceSampleFrames,} from "@narratage/program-space";import type { ProgramSpace } from "@narratage/program-space";-import { canonicalize, digestOf, isDigest } from "@narratage/protocol";+import { canonicalize, isDigest } from "@narratage/protocol";import type { CompleteSemanticMap } from "@narratage/semantic-map";import { assertCanvasSpace, assertSpatialFrame } from "@narratage/spatial";import type { CanvasSpace } from "@narratage/spatial";diff --git a/packages/media-track/test/media-track.test.ts b/packages/media-track/test/media-track.test.tsindex 189e08a5..a994072d 100644--- a/packages/media-track/test/media-track.test.ts+++ b/packages/media-track/test/media-track.test.ts@@ -1,5 +1,6 @@import assert from "node:assert/strict";import test from "node:test";+import { fixtureDigest } from "../../../test/fixture-digest.js";import { sealComposition } from "@narratage/composition";import type { AudioTrack } from "@narratage/composition";@@ -55,7 +56,6 @@ import type {import type { NarrativeMomentRef, NarrativeSelectionRef } from "@narratage/narrative";import { sealProgramSpace } from "@narratage/program-space";import { programSpaceTypes } from "@narratage/program-space";-import { digestOf } from "@narratage/protocol";import type { BlobRef } from "@narratage/protocol";import { semanticMapTypes } from "@narratage/semantic-map";import type { CompleteSemanticMap } from "@narratage/semantic-map";@@ -79,7 +79,7 @@ const canvas = {const header = sealMediaTrackHeader({ id: "proof" });const source: BlobRef = {kind: "blob",- digest: digestOf("media-track:still"),+ digest: fixtureDigest("media-track:still"),size: 4_096,mediaType: "image/png",};@@ -141,12 +141,12 @@ function timed(id = "timed", withAudio = true): SynchronizedMedia {frameCount: 60,},visual: {- artifact: { kind: "blob", digest: digestOf(`video:${id}`), size: 10_000, mediaType: "video/mp4" },+ artifact: { kind: "blob", digest: fixtureDigest(`video:${id}`), size: 10_000, mediaType: "video/mp4" },width: 720,height: 1280,},...(withAudio ? { audio: {- artifact: { kind: "blob" as const, digest: digestOf(`audio:${id}`), size: sampleFrames * 4, mediaType: "audio/wav" },+ artifact: { kind: "blob" as const, digest: fixtureDigest(`audio:${id}`), size: sampleFrames * 4, mediaType: "audio/wav" },} } : {}),};}@@ -399,7 +399,7 @@ test("source audio and edge SFX project separately from the visual Track", () =>test("still and animated typed Surfaces use the same layer law without browser format guesses", () => {const still: CompositableSurfaceRef = {- artifact: { kind: "blob", digest: digestOf("surface:still"), size: 500, mediaType: "image/png" },+ artifact: { kind: "blob", digest: fixtureDigest("surface:still"), size: 500, mediaType: "image/png" },width: 100,height: 100,colorSpace: "srgb",@@ -408,7 +408,7 @@ test("still and animated typed Surfaces use the same layer law without browser f};const animated: CompositableSurfaceRef = {...still,- artifact: { kind: "blob", digest: digestOf("surface:animated"), size: 2_000, mediaType: "video/webm" },+ artifact: { kind: "blob", digest: fixtureDigest("surface:animated"), size: 2_000, mediaType: "video/webm" },timing: { kind: "frames", frameRate: { numerator: 30, denominator: 1 }, frameCount: 30 },};let layers = appendSurfaceMediaLayer(createMediaLayerSet(), still, fit, sealMediaSampleLayerSpec({@@ -604,7 +604,7 @@ test("every documented Media frame and fit remains one ordinary Item instead of});test("transparent, Paint, self-blur and alternate-source backing are only ordered owned layers", () => {- const alternate: BlobRef = { kind: "blob", digest: digestOf("media-track:alternate"), size: 2_048, mediaType: "image/webp" };+ const alternate: BlobRef = { kind: "blob", digest: fixtureDigest("media-track:alternate"), size: 2_048, mediaType: "image/webp" };let layers = createMediaLayerSet();layers = appendMediaPaintLayer(layers, sealMediaPaintLayerSpec({id: "solid", paint: { kind: "solid", color: "#101018" }, opacity: 1,@@ -806,7 +806,7 @@ test("Sequence resolves strict logical phases, expanded handoffs and one unintertest("Sequence handoffs accept still, timed and alpha Surface members through one lowering path", () => {const alphaSurface: CompositableSurfaceRef = {- artifact: { kind: "blob", digest: digestOf("surface:alpha-member"), size: 800, mediaType: "image/png" },+ artifact: { kind: "blob", digest: fixtureDigest("surface:alpha-member"), size: 800, mediaType: "image/png" },width: 200, height: 300, colorSpace: "srgb", alphaMode: "straight", timing: { kind: "still" },};const surfaceLayers = () => appendSurfaceMediaLayer(createMediaLayerSet(), alphaSurface, fit, sealMediaSampleLayerSpec({diff --git a/packages/media/src/manifest.ts b/packages/media/src/manifest.tsindex f2f31467..545294e2 100644--- a/packages/media/src/manifest.ts+++ b/packages/media/src/manifest.ts@@ -1,5 +1,4 @@import { artifactDependency, artifactTypes } from "@narratage/artifact";-import { digestOf } from "@narratage/protocol";import type { ModuleManifest, TypeRef } from "@narratage/protocol";import { compositableSurfaceSchema, fontArtifactSchema, fontStackSchema, mediaInspectionSchema, mediaStreamSelectionSchema, muxedMediaSchema, renderedVisualSchema, synchronizedMediaSchema, timelineAudioSchema } from "./schema.js";export const mediaModuleRef = { name: "@narratage/media", version: "1" } as const;@@ -11,7 +10,6 @@ export const mediaTypes = {fontStack: { module: mediaModuleRef, name: "FontStackRef" },compositableSurface: { module: mediaModuleRef, name: "CompositableSurfaceRef" }, blobArtifact: artifactTypes.blob,} satisfies Record<string, TypeRef>;-const validator = (digest: ReturnType<typeof digestOf>) => ({ implementation: { digest } });export const mediaMarkupSurfaces = [{ name: "image", tag: "Image", mode: "structured", outputs: [artifactTypes.blob] },diff --git a/packages/media/test/font-surface.test.ts b/packages/media/test/font-surface.test.tsindex 8736dca7..c7b1437d 100644--- a/packages/media/test/font-surface.test.ts+++ b/packages/media/test/font-surface.test.ts@@ -1,8 +1,8 @@import assert from "node:assert/strict";import test from "node:test";+import { fixtureDigest } from "../../../test/fixture-digest.js";import { decodeMediaFontSurface, mediaTypes } from "@narratage/media";-import { digestOf } from "@narratage/protocol";const range = { start: 0, end: 80 };@@ -23,7 +23,7 @@ test("Font Surface turns explicit author bytes and face metadata into one exactreturn {artifact: {kind: "blob",- digest: digestOf("font:inter-bold"),+ digest: fixtureDigest("font:inter-bold"),size: 2_048,mediaType: request.mediaType,},@@ -39,7 +39,7 @@ test("Font Surface turns explicit author bytes and face metadata into one exactvalue: {sources: [{ artifact: {kind: "blob",- digest: digestOf("font:inter-bold"),+ digest: fixtureDigest("font:inter-bold"),size: 2_048,mediaType: "font/woff2",} }],diff --git a/packages/mimo-tts/src/index.ts b/packages/mimo-tts/src/index.tsindex 79b0a83a..b7d869c0 100644--- a/packages/mimo-tts/src/index.ts+++ b/packages/mimo-tts/src/index.ts@@ -1,7 +1,6 @@import { sealGenerationPortRequest, sealGenerationRequestDraft, sealGenerationPortTable } from "@narratage/generation";import type { GenerationPortTable, GenerationPortValue, GenerationRequest } from "@narratage/generation";import { defineExactModelModule } from "@narratage/model-kit";-import { digestOf } from "@narratage/protocol";export const mimoTtsModuleRef = { name: "@narratage/mimo-tts", version: "1" } as const;export const mimoTtsModels = [@@ -90,12 +89,6 @@ const base = defineExactModelModule({});export const mimoTtsEndpoints = base.endpoints;-export const mimoTtsSurfaceDigests = {- preset: digestOf("@narratage/mimo-tts/preset-surface@1"),- voiceDesign: digestOf("@narratage/mimo-tts/voice-design-surface@1"),- voiceClone: digestOf("@narratage/mimo-tts/voice-clone-surface@1"),-} as const;-export const mimoTtsMarkupSurfaces = [{name: "preset", tag: "Preset", mode: "structured",diff --git a/packages/mimo-tts/test/mimo-tts.test.ts b/packages/mimo-tts/test/mimo-tts.test.tsindex ac174a71..d26845f7 100644--- a/packages/mimo-tts/test/mimo-tts.test.ts+++ b/packages/mimo-tts/test/mimo-tts.test.ts@@ -1,9 +1,9 @@import assert from "node:assert/strict";import test from "node:test";+import { fixtureDigest } from "../../../test/fixture-digest.js";import { artifactTypes } from "@narratage/artifact";import { generationProducers, generationTypes } from "@narratage/generation";-import { digestOf } from "@narratage/protocol";import { sealText, textTypes } from "@narratage/text";import { parseStructuredElement } from "@narratage/markup";import type { SurfaceResolvedReference } from "@narratage/markup";@@ -34,14 +34,12 @@ function authored(path: string, type: SurfaceResolvedReference["type"], value: uvalue: type.name === artifactTypes.blob.name? value as never: { kind: "inline", value: value as never },- digest: digestOf(value),- origin: { kind: "authored" },},};}const speech = sealText("Exact authored words stay exact.");-const sample = { kind: "blob" as const, digest: digestOf("voice-sample"), size: 4, mediaType: "audio/wav" };+const sample = { kind: "blob" as const, digest: fixtureDigest("voice-sample"), size: 4, mediaType: "audio/wav" };const refs = new Map<string, SurfaceResolvedReference>([["story.segment.opening.speech", authored("story.segment.opening.speech", textTypes.text, speech)],["voice", authored("voice", artifactTypes.blob, sample)],diff --git a/packages/minimax-h3/src/index.ts b/packages/minimax-h3/src/index.tsindex 3328d74c..686e4f27 100644--- a/packages/minimax-h3/src/index.ts+++ b/packages/minimax-h3/src/index.ts@@ -1,7 +1,6 @@import { sealGenerationPortRequest, sealGenerationPortTable } from "@narratage/generation";import type { GenerationPortTable, GenerationPortValue, GenerationRequest } from "@narratage/generation";import { defineExactModelModule } from "@narratage/model-kit";-import { digestOf } from "@narratage/protocol";export const minimaxH3ModuleRef = { name: "@narratage/minimax-h3", version: "1" } as const;diff --git a/packages/model-kit/src/index.ts b/packages/model-kit/src/index.tsindex 96a01bc7..f15365ae 100644--- a/packages/model-kit/src/index.ts+++ b/packages/model-kit/src/index.ts@@ -95,7 +95,7 @@ export type ExactModelTextInput = {export type ExactModelModule<Key extends string = string> = {readonly module: ModuleRef;readonly manifest: ModuleManifest;- /** Keyed by the exact endpoint keys the module declared, so a stale key fails to compile. */+ /** Keyed by the endpoint keys declared by the module. */readonly endpoints: Readonly<Record<Key, ExactModelEndpoint>>;readonly component: ComponentPackage & {readonly validators: readonly TypeValidatorFacet[];diff --git a/packages/model-kit/test/model-kit.test.ts b/packages/model-kit/test/model-kit.test.tsindex 1a517f4c..5edc9a2d 100644--- a/packages/model-kit/test/model-kit.test.ts+++ b/packages/model-kit/test/model-kit.test.ts@@ -1,5 +1,6 @@import assert from "node:assert/strict";import test from "node:test";+import { fixtureDigest } from "../../../test/fixture-digest.js";import {sealGenerationMediaBinding,@@ -10,7 +11,6 @@ import {createExactModelPrimaryGenerationFragment,defineExactModelModule,} from "@narratage/model-kit";-import { digestOf } from "@narratage/protocol";import { sealText } from "@narratage/text";const ports = sealGenerationPortTable({@@ -42,7 +42,7 @@ test("one exact model definition owns draft, media binding, finalization and genconst artifact = {kind: "blob" as const,- digest: digestOf("graph-native-image"),+ digest: fixtureDigest("graph-native-image"),size: 4,mediaType: "image/png",};diff --git a/packages/nano-banana/src/index.ts b/packages/nano-banana/src/index.tsindex 84428171..f3912e7f 100644--- a/packages/nano-banana/src/index.ts+++ b/packages/nano-banana/src/index.ts@@ -1,7 +1,6 @@import { sealGenerationPortRequest, sealGenerationPortTable } from "@narratage/generation";import type { GenerationPortTable, GenerationPortValue, GenerationRequest } from "@narratage/generation";import { defineExactModelModule } from "@narratage/model-kit";-import { digestOf } from "@narratage/protocol";export const nanoBananaModuleRef = { name: "@narratage/nano-banana", version: "1" } as const;export const nanoBananaModels = ["nano-banana-2", "nano-banana-pro"] as const;diff --git a/packages/narrative/src/manifest.ts b/packages/narrative/src/manifest.tsindex 7a00a093..2ee77cb2 100644--- a/packages/narrative/src/manifest.ts+++ b/packages/narrative/src/manifest.ts@@ -1,4 +1,3 @@-import { digestOf } from "@narratage/protocol";import type { ModuleManifest, TypeRef } from "@narratage/protocol";import {captionCorrespondenceSchema,diff --git a/packages/package-loader-node/src/contribution.ts b/packages/package-loader-node/src/contribution.tsindex f67adeca..af18917b 100644--- a/packages/package-loader-node/src/contribution.ts+++ b/packages/package-loader-node/src/contribution.ts@@ -1,5 +1,4 @@import type { ComponentPackage } from "@narratage/component-kit";-import { canonicalize } from "@narratage/protocol";import type { NodeModuleContribution, NodePackageContribution } from "./types.js";@@ -7,7 +6,6 @@ function assertPackage(value: NodePackageContribution): void {if (value.format !== "narratage.node-package@1") {throw new Error("Node package contribution has an unsupported format");}- const hostFacets = new Set<string>();for (const facet of value.hostFacets ?? []) {if (facet.abi.trim().length === 0) throw new Error("Node package contribution has an empty Host facet ABI");const offers = facet.offers ?? [];@@ -17,10 +15,6 @@ function assertPackage(value: NodePackageContribution): void {if (new Set(offers).size !== offers.length) {throw new Error(`Node package contribution repeats a logical name for Host facet ${facet.abi}`);}- const identity = facet.identity === undefined ? undefined : canonicalize(facet.identity);- const key = `${facet.abi}:${JSON.stringify([...offers].sort())}:${identity === undefined ? "" : JSON.stringify(identity)}`;- if (hostFacets.has(key)) throw new Error(`Node package contribution repeats Host facet ${facet.abi}`);- hostFacets.add(key);}}@@ -30,7 +24,7 @@ function facetKey(return `${ref.module.name}@${ref.module.version}#${ref.name}`;}-/** Verify package-local facet identities before any Host registry receives executable handlers. */+/** Collect declarative modules and executable component implementations from loaded packages. */export function collectNodePackageComponents(packages: readonly NodePackageContribution[],): readonly ComponentPackage[] {diff --git a/packages/program-space/src/index.ts b/packages/program-space/src/index.tsindex 9186a588..d08d1a43 100644--- a/packages/program-space/src/index.ts+++ b/packages/program-space/src/index.ts@@ -1,4 +1,3 @@-import { digestOf } from "@narratage/protocol";import type { ModuleManifest, TypeRef, ValueSchema } from "@narratage/protocol";export type ProgramSpace = {diff --git a/packages/protocol/src/build.ts b/packages/protocol/src/build.tsindex b40a284e..0a3d8854 100644--- a/packages/protocol/src/build.ts+++ b/packages/protocol/src/build.ts@@ -2,14 +2,10 @@ import type {CandidateId,CommandId,CapabilityRef,- DerivationId,- Digest,- EventId,LogicalOutputId,NeedId,OperationId,ProducerRef,- ReceiptId,RecordId,StepId,TypeRef,@@ -22,33 +18,11 @@ export type SourceRange = {readonly end: number;};-export type AuthoredOrigin = {- readonly kind: "authored";-};--export type DerivedOrigin = {- readonly kind: "derived";- readonly derivation: DerivationId;-};--export type ObservedOrigin = {- readonly kind: "observed";- readonly receipt: ReceiptId;-};--export type ProvidedOrigin = {- readonly kind: "provided";-};--export type RecordOrigin = AuthoredOrigin | DerivedOrigin | ObservedOrigin | ProvidedOrigin;-/** One typed value in the graph; semantic validation happens at the Host admission boundary. */export type TypedRecord = {readonly id: RecordId;readonly type: TypeRef;readonly value: StoredValue;- readonly digest: Digest;- readonly origin: RecordOrigin;};export type Need = {@@ -56,46 +30,7 @@ export type Need = {readonly capability: CapabilityRef;readonly returns: TypeRef;readonly constraints: CanonicalValue;- readonly requestedBy: DerivationId;readonly result: RecordId;- readonly requestDigest: Digest;-};--/** Exact request, selected Endpoint and returned value for one fulfilled Need. */-export type Receipt = {- readonly id: ReceiptId;- readonly need: NeedId;- readonly requestDigest: Digest;- readonly fulfiller: string;- readonly output: RecordId;- readonly outputDigest: Digest;- readonly event: {- readonly id: EventId;- readonly digest: Digest;- };-};--export type RecordDigestBinding = {- readonly id: RecordId;- readonly digest: Digest;-};--export type NeedDigestBinding = {- readonly id: NeedId;- readonly requestDigest: Digest;-};--export type Derivation = {- readonly id: DerivationId;- readonly step: StepId;- readonly producer: ProducerRef;- readonly inputs: readonly RecordDigestBinding[];- readonly outputs: readonly RecordDigestBinding[];- readonly needs: readonly NeedDigestBinding[];- readonly event: {- readonly id: EventId;- readonly digest: Digest;- };};export type NeedBinding = {@@ -164,8 +99,6 @@ export type Candidate = {export type CompiledGraph = {readonly format: "narratage.graph@1";- readonly id: Digest;- readonly program: Digest;readonly outputs: readonly LogicalOutput[];readonly candidates: readonly Candidate[];readonly operations: readonly OperationNode[];@@ -182,9 +115,7 @@ export type Satisfaction = {export type BuildRequest = {readonly format: "narratage.build-request@1";- readonly graph: Digest;readonly targets: readonly BuildTarget[];- readonly digest: Digest;};export type BuildSelection = {@@ -208,9 +139,6 @@ export type BuildGoal = {export type BuildPlan = {readonly format: "narratage.plan@1";- readonly id: Digest;- readonly graph: Digest;- readonly request: Digest;readonly steps: readonly ProducerStep[];readonly goals: readonly BuildGoal[];readonly selections: readonly BuildSelection[];@@ -219,13 +147,11 @@ export type BuildPlan = {export type LinkedProgram = {readonly closure: ResolvedModuleClosure;readonly records: readonly TypedRecord[];- readonly semanticDigest: Digest;};export type StepState = {readonly id: StepId;readonly status: "pending" | "complete";- readonly derivation?: DerivationId;};export type InvokeProducerCommand = {@@ -246,7 +172,6 @@ export type CoreCommand = InvokeProducerCommand | FulfillNeedCommand;export type ProducerCompletedEvent = {readonly kind: "producer-completed";- readonly id: EventId;readonly command: CommandId;readonly outputs: Readonly<Record<string, StoredValue>>;readonly needs: Readonly<Record<string, CanonicalValue>>;@@ -254,27 +179,19 @@ export type ProducerCompletedEvent = {export type NeedFulfilledEvent = {readonly kind: "need-fulfilled";- readonly id: EventId;readonly command: CommandId;readonly value: StoredValue;- readonly requestDigest: Digest;- readonly fulfiller: string;};export type CommandFailedEvent = {readonly kind: "command-failed";- readonly id: EventId;readonly command: CommandId;readonly code: string;readonly message: string;};-export type BuildEvent = ProducerCompletedEvent | NeedFulfilledEvent | CommandFailedEvent;--export type AcceptedEvent = {- readonly id: EventId;- readonly digest: Digest;-};+/** One execution result offered to Core. */+export type CommandResult = ProducerCompletedEvent | NeedFulfilledEvent | CommandFailedEvent;export type BuildDiagnostic = {readonly code: string;@@ -282,9 +199,47 @@ export type BuildDiagnostic = {readonly subject?: string;};+/** Immutable finite program selected by one Author Graph plus one Run Graph. */+export type BuildDefinition = {+ readonly format: "narratage.build-definition@1";+ readonly program: LinkedProgram;+ readonly graph: CompiledGraph;+ readonly request: BuildRequest;+ readonly plan: BuildPlan;+};++type BuildFactBase = {+ readonly format: "narratage.build-fact@1";+ readonly command: CommandId;+};++/** One Producer result admitted by Core. Every added value is stored exactly once. */+export type ProducerAppliedFact = BuildFactBase & {+ readonly kind: "producer-applied";+ readonly step: StepId;+ readonly records: readonly TypedRecord[];+ readonly needs: readonly Need[];+};++/** One external Need result admitted by Core. */+export type NeedAppliedFact = BuildFactBase & {+ readonly kind: "need-applied";+ readonly need: NeedId;+ readonly record: TypedRecord;+};++/** One terminal command failure admitted by Core. */+export type CommandFailedFact = BuildFactBase & {+ readonly kind: "command-failed";+ readonly diagnostic: BuildDiagnostic;+};++/** Fixed Core facts, not an extensible event or patch system. */+export type BuildFact = ProducerAppliedFact | NeedAppliedFact | CommandFailedFact;++/** Materialized read/execution view. Durable Stores persist Definition + Facts, never this object. */export type BuildState = {readonly format: "narratage.build@1";- readonly id: Digest;readonly program: LinkedProgram;readonly graph: CompiledGraph;readonly request: BuildRequest;@@ -293,9 +248,6 @@ export type BuildState = {readonly records: readonly TypedRecord[];readonly steps: readonly StepState[];readonly needs: readonly Need[];- readonly receipts: readonly Receipt[];- readonly derivations: readonly Derivation[];readonly outstanding: readonly CoreCommand[];- readonly acceptedEvents: readonly AcceptedEvent[];readonly diagnostics: readonly BuildDiagnostic[];};diff --git a/packages/protocol/src/canonical.ts b/packages/protocol/src/canonical.tsindex 44f36282..1a5d895f 100644--- a/packages/protocol/src/canonical.ts+++ b/packages/protocol/src/canonical.ts@@ -1,156 +1,37 @@-import type { TypedRecord } from "./build.js";import { SvmlError } from "./error.js";-import type { Digest, TypeRef } from "./identity.js";-import type { CanonicalValue, StoredValue } from "./value.js";+import type { Digest } from "./identity.js";+import type { CanonicalValue } from "./value.js";-function normalize(value: unknown, path: string, ancestors: WeakSet<object>): CanonicalValue {+function normalize(value: unknown, path: string): CanonicalValue {if (value === null || typeof value === "boolean" || typeof value === "string") return value;if (typeof value === "number") {if (!Number.isFinite(value)) throw new SvmlError("NON_CANONICAL_NUMBER", `${path} must be finite`);return Object.is(value, -0) ? 0 : value;}if (Array.isArray(value)) {- if (ancestors.has(value)) throw new SvmlError("CYCLIC_VALUE", `${path} contains a cycle`);- ancestors.add(value);- const result = value.map((item, index) => normalize(item, `${path}[${index}]`, ancestors));- ancestors.delete(value);- return result;+ return value.map((item, index) => normalize(item, `${path}[${index}]`));}if (typeof value === "object") {- const prototype = Object.getPrototypeOf(value);- if (prototype !== Object.prototype && prototype !== null) {- throw new SvmlError("NON_CANONICAL_OBJECT", `${path} must be a plain object`);- }- if (ancestors.has(value)) throw new SvmlError("CYCLIC_VALUE", `${path} contains a cycle`);- ancestors.add(value);const result: Record<string, CanonicalValue> = {};- const source = value as Record<string, unknown>;- for (const key of Object.keys(source).sort()) {- const descriptor = Object.getOwnPropertyDescriptor(source, key);- if (descriptor === undefined || descriptor.get !== undefined || descriptor.set !== undefined) {- throw new SvmlError("NON_CANONICAL_ACCESSOR", `${path}.${key} must be a data property`);- }- if (descriptor.value === undefined) {+ for (const [key, item] of Object.entries(value).sort(([left], [right]) => left.localeCompare(right))) {+ if (item === undefined) {throw new SvmlError("NON_CANONICAL_UNDEFINED", `${path}.${key} is undefined`);}- Object.defineProperty(result, key, {- value: normalize(descriptor.value, `${path}.${key}`, ancestors),- enumerable: true,- configurable: true,- writable: true,- });+ result[key] = normalize(item, `${path}.${key}`);}- ancestors.delete(value);return result;}throw new SvmlError("NON_CANONICAL_VALUE", `${path} contains ${typeof value}`);}export function canonicalize(value: unknown): CanonicalValue {- return normalize(value, "$input", new WeakSet<object>());+ return normalize(value, "$input");}export function canonicalStringify(value: unknown): string {return JSON.stringify(canonicalize(value));}-const sha256RoundConstants = new Uint32Array([- 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,- 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,- 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,- 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,- 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,- 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,- 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,- 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,-]);--function rotateRight(value: number, bits: number): number {- return (value >>> bits) | (value << (32 - bits));-}--/** Synchronous, Host-independent SHA-256 for deterministic Core identities. */-function sha256(value: string): string {- const source = new TextEncoder().encode(value);- const byteLength = Math.ceil((source.length + 9) / 64) * 64;- const bytes = new Uint8Array(byteLength);- bytes.set(source);- bytes[source.length] = 0x80;- const bitLength = BigInt(source.length) * 8n;- const view = new DataView(bytes.buffer);- view.setUint32(byteLength - 8, Number((bitLength >> 32n) & 0xffffffffn), false);- view.setUint32(byteLength - 4, Number(bitLength & 0xffffffffn), false);-- const state = new Uint32Array([- 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,- 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,- ]);- const words = new Uint32Array(64);- for (let offset = 0; offset < byteLength; offset += 64) {- for (let index = 0; index < 16; index += 1) words[index] = view.getUint32(offset + index * 4, false);- for (let index = 16; index < 64; index += 1) {- const left = words[index - 15]!;- const right = words[index - 2]!;- const s0 = rotateRight(left, 7) ^ rotateRight(left, 18) ^ (left >>> 3);- const s1 = rotateRight(right, 17) ^ rotateRight(right, 19) ^ (right >>> 10);- words[index] = (words[index - 16]! + s0 + words[index - 7]! + s1) >>> 0;- }- let a = state[0]!;- let b = state[1]!;- let c = state[2]!;- let d = state[3]!;- let e = state[4]!;- let f = state[5]!;- let g = state[6]!;- let h = state[7]!;- for (let index = 0; index < 64; index += 1) {- const sum1 = rotateRight(e, 6) ^ rotateRight(e, 11) ^ rotateRight(e, 25);- const choose = (e & f) ^ (~e & g);- const first = (h + sum1 + choose + sha256RoundConstants[index]! + words[index]!) >>> 0;- const sum0 = rotateRight(a, 2) ^ rotateRight(a, 13) ^ rotateRight(a, 22);- const majority = (a & b) ^ (a & c) ^ (b & c);- const second = (sum0 + majority) >>> 0;- h = g;- g = f;- f = e;- e = (d + first) >>> 0;- d = c;- c = b;- b = a;- a = (first + second) >>> 0;- }- state[0] = (state[0]! + a) >>> 0;- state[1] = (state[1]! + b) >>> 0;- state[2] = (state[2]! + c) >>> 0;- state[3] = (state[3]! + d) >>> 0;- state[4] = (state[4]! + e) >>> 0;- state[5] = (state[5]! + f) >>> 0;- state[6] = (state[6]! + g) >>> 0;- state[7] = (state[7]! + h) >>> 0;- }- return [...state].map((item) => item.toString(16).padStart(8, "0")).join("");-}--export function digestOf(value: unknown): Digest {- return `sha256:${sha256(canonicalStringify(value))}`;-}-export function isDigest(value: string): value is Digest {return /^sha256:[0-9a-f]{64}$/u.test(value);}--export function recordDigest(type: TypeRef, value: StoredValue): Digest {- return digestOf({ type, value });-}--export function semanticRecordsDigest(records: readonly TypedRecord[]): Digest {- return digestOf(- [...records]- .sort((left, right) => left.id.localeCompare(right.id))- .map((record) => ({- id: record.id,- type: record.type,- value: record.value,- })),- );-}diff --git a/packages/protocol/src/identity.ts b/packages/protocol/src/identity.tsindex 5a101f04..02a7f365 100644--- a/packages/protocol/src/identity.ts+++ b/packages/protocol/src/identity.ts@@ -6,10 +6,7 @@ export type CandidateId = string;export type OperationId = string;export type NeedId = string;export type StepId = string;-export type DerivationId = string;-export type ReceiptId = string;export type CommandId = string;-export type EventId = string;export type ModuleRef = {readonly name: string;diff --git a/packages/protocol/src/manifest.ts b/packages/protocol/src/manifest.tsindex 3b148793..ed7ce652 100644--- a/packages/protocol/src/manifest.ts+++ b/packages/protocol/src/manifest.ts@@ -1,4 +1,4 @@-import { canonicalize, isDigest } from "./canonical.js";+import { canonicalize } from "./canonical.js";import type {CapabilityDeclaration,ModuleDependency,@@ -8,7 +8,7 @@ import type {ProducerDeclaration,TypeDeclaration,} from "./module.js";-import type { CapabilityRef, Digest, ModuleRef, TypeRef } from "./identity.js";+import type { CapabilityRef, ModuleRef, TypeRef } from "./identity.js";import type { ObjectFieldSchema, ValueSchema } from "./value.js";function object(value: unknown, path: string): Record<string, unknown> {@@ -43,12 +43,6 @@ function number(value: unknown, path: string): number {return value;}-function digest(value: unknown, path: string): Digest {- const parsed = string(value, path);- if (!isDigest(parsed)) throw new Error(`${path} must be a sha256 digest`);- return parsed;-}-function moduleRef(value: unknown, path: string): ModuleRef {const parsed = object(value, path);return {diff --git a/packages/protocol/src/module.ts b/packages/protocol/src/module.tsindex 2c6bc8ea..4cde9a38 100644--- a/packages/protocol/src/module.ts+++ b/packages/protocol/src/module.ts@@ -1,4 +1,4 @@-import type { CapabilityRef, Digest, ModuleRef, ProducerRef, TypeRef } from "./identity.js";+import type { CapabilityRef, ModuleRef, ProducerRef, TypeRef } from "./identity.js";/** Logical package address for immutable semantic Modules. */export const modulePackageAbi = "narratage.module@1";@@ -50,14 +50,12 @@ export type ResolvedModuleManifest = Omit<ModuleManifest, "types"> & {};export type ResolvedModule = {- readonly digest: Digest;readonly manifest: ResolvedModuleManifest;};export type ResolvedModuleClosure = {readonly format: "narratage.closure@1";readonly modules: readonly ResolvedModule[];- readonly digest: Digest;};export type ResolvedTypeDeclaration = {diff --git a/packages/protocol/test/canonical.test.ts b/packages/protocol/test/canonical.test.tsindex d2549975..221a5461 100644--- a/packages/protocol/test/canonical.test.ts+++ b/packages/protocol/test/canonical.test.ts@@ -1,127 +1,26 @@import assert from "node:assert/strict";import test from "node:test";-import {- canonicalize,- canonicalStringify,- digestOf,- isDigest,- recordDigest,- semanticRecordsDigest,-} from "@narratage/protocol";+import { canonicalize, canonicalStringify, isDigest } from "@narratage/protocol";-/**- * Every identity in the system is a digest of a canonical value: Record- * identity, Operation identity and configuration identity. So these- * tests are about one property — two values that mean the same thing must- * digest the same, and two that mean different things must not.- */--test("key order is not meaning, so it cannot change a digest", () => {- assert.equal(- digestOf({ a: 1, b: { c: 2, d: 3 } }),- digestOf({ b: { d: 3, c: 2 }, a: 1 }),- );+test("canonical values have stable key order and preserve array order", () => {assert.equal(canonicalStringify({ b: 1, a: 2 }), '{"a":2,"b":1}');+ assert.notEqual(canonicalStringify([1, 2]), canonicalStringify([2, 1]));});-test("array order is meaning, so it does change a digest", () => {- assert.notEqual(digestOf([1, 2]), digestOf([2, 1]));-});--test("a value's type is part of what it means", () => {- assert.notEqual(digestOf({ a: 1 }), digestOf({ a: "1" }));- assert.notEqual(digestOf({ a: null }), digestOf({ a: false }));- assert.notEqual(digestOf({ a: 0 }), digestOf({ a: [] }));- assert.notEqual(digestOf([]), digestOf({}));-});--test("negative zero is zero, because nothing downstream can mean the difference", () => {- assert.equal(digestOf({ a: -0 }), digestOf({ a: 0 }));- assert.ok(Object.is((canonicalize({ a: -0 }) as { a: number }).a, 0));-});--test("canonicalizing an already canonical value changes nothing", () => {- const value = { z: [1, { y: "x" }], a: null, n: 2.5 };- const once = canonicalize(value);+test("canonicalization normalizes negative zero and is idempotent", () => {+ const once = canonicalize({ z: [1, { y: "x" }], a: -0 });assert.deepEqual(canonicalize(once), once);- assert.equal(digestOf(once), digestOf(value));-});--test("a value that cannot mean one thing is refused, and says which path", () => {- const refusals: readonly [unknown, RegExp][] = [- [{ a: Number.NaN }, /must be finite/u],- [{ a: Number.POSITIVE_INFINITY }, /must be finite/u],- [{ a: undefined }, /is undefined/u],- [{ a: () => 1 }, /contains function/u],- [{ a: 1n }, /contains bigint/u],- [{ a: Symbol("s") }, /contains symbol/u],- [{ a: new Date(0) }, /must be a plain object/u],- [{ a: new Map() }, /must be a plain object/u],- ];- for (const [value, message] of refusals) assert.throws(() => canonicalize(value), message);-- // An accessor could answer differently on a second read, so it is not a value.- const accessor = {};- Object.defineProperty(accessor, "a", { get: () => 1, enumerable: true });- assert.throws(() => canonicalize(accessor), /must be a data property/u);+ assert.ok(Object.is((once as { a: number }).a, 0));});-test("a cycle has no canonical form, in either shape", () => {- const object: Record<string, unknown> = {};- object.self = object;- assert.throws(() => canonicalize(object), /contains a cycle/u);- const array: unknown[] = [];- array.push(array);- assert.throws(() => canonicalize(array), /contains a cycle/u);-- // The same value twice is not a cycle.- const shared = { a: 1 };- assert.doesNotThrow(() => canonicalize({ left: shared, right: shared }));+test("values without one JSON meaning are refused", () => {+ assert.throws(() => canonicalize({ a: Number.NaN }), /must be finite/u);+ assert.throws(() => canonicalize({ a: undefined }), /is undefined/u);});-test("a digest is recognisable as one", () => {- assert.ok(isDigest(digestOf({})));+test("Blob digests retain their narrow byte-address syntax", () => {+ assert.equal(isDigest(`sha256:${"a".repeat(64)}`), true);assert.equal(isDigest("sha256:"), false);assert.equal(isDigest("md5:abc"), false);- assert.equal(isDigest(`sha256:${"g".repeat(64)}`), false);-});--test("a Record's identity includes its Type, and a set of Records is order-free", () => {- const type = { module: { name: "@x/m", version: "1" }, name: "T" } as const;- const other = { module: { name: "@x/m", version: "1" }, name: "U" } as const;- const value = { kind: "inline", value: { a: 1 } } as const;- assert.notEqual(recordDigest(type, value), recordDigest(other, value));-- const records = [- { id: "r1", type, value },- { id: "r2", type: other, value },- ] as never;- const reversed = [records[1], records[0]] as never;- assert.equal(semanticRecordsDigest(records), semanticRecordsDigest(reversed));-});--/**- * Pins.- *- * These are not testing that sha256 works. They exist so that changing how a- * value is canonicalized cannot be a quiet change: every archived Build and- * recorded Runtime Profile is addressed by these bytes.- * Editing a constant here is a deliberate act that a reviewer can see; a- * silently different digest is not.- */-test("the canonical form of these values is fixed, and changing it is a visible act", () => {- assert.equal(digestOf({}),- "sha256:44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a");- assert.equal(digestOf([]),- "sha256:4f53cda18c2baa0c0354bb5f9a3ecbe5ed12ab4d8e11ba873c2f11161202b945");- assert.equal(digestOf({ contract: "narratage.example@1", count: 1, ok: true, none: null }),- "sha256:4067a46c456f9d497e2f5ddf050d448c0965fa4596d403d72bde387fbce8d750");- // Non-ASCII must not depend on an escaping choice.- assert.equal(digestOf({ zh: "口播稿", nested: { a: [1, "1", false] } }),- "sha256:96519d6160460ea2b76578b1303fec47e9c3d53b012dce8738a3129126baea59");- assert.equal(- recordDigest({ module: { name: "@x/m", version: "1" }, name: "T" },- { kind: "inline", value: { a: 1 } } as never),- "sha256:e231ae28cdbe95ed1fe2d5837e3c4d82e71805ad26b941e8a90c745e4e50cdbc");});diff --git a/packages/provider-google-vertex/test/provider.test.ts b/packages/provider-google-vertex/test/provider.test.tsindex 06808723..237f92d5 100644--- a/packages/provider-google-vertex/test/provider.test.ts+++ b/packages/provider-google-vertex/test/provider.test.ts@@ -1,5 +1,6 @@import assert from "node:assert/strict";import test from "node:test";+import { fixtureDigest } from "../../../test/fixture-digest.js";import {captionGeminiCapabilities,@@ -14,7 +15,6 @@ import {import type { CaptionGeminiRequest, RawCaptionGeminiResponse } from "@narratage/caption-gemini";import { EndpointRegistry, MemoryArtifactStore } from "@narratage/driver-node";import type { ImmediateEndpointHandler } from "@narratage/endpoint-kit";-import { digestOf } from "@narratage/protocol";import type { CanonicalValue, Need } from "@narratage/protocol";import { createGoogleVertexCaptionProvider } from "@narratage/provider-google-vertex";import type { GenerateCaptionContent } from "@narratage/provider-google-vertex";@@ -61,9 +61,7 @@ function need(requestValue: CaptionGeminiRequest): Need {capability: captionGeminiCapabilities.plan,returns: captionTypes.plan,constraints,- requestedBy: "derivation:caption-gemini",result: "record:caption-gemini",- requestDigest: digestOf({ capability: captionGeminiCapabilities.plan, constraints }),};}diff --git a/packages/provider-hyperframes-aws-lambda/src/activation.ts b/packages/provider-hyperframes-aws-lambda/src/activation.tsindex a0cf57e8..57175be8 100644--- a/packages/provider-hyperframes-aws-lambda/src/activation.ts+++ b/packages/provider-hyperframes-aws-lambda/src/activation.ts@@ -15,9 +15,9 @@ import type {} from "./provider.js";const CONFIG_KEYS = [- "stateMachineArn", "bucketName", "region", "quality", "chunkSize", "maxParallelChunks",+ "stateMachineArn", "bucketName", "quality", "chunkSize", "maxParallelChunks","targetChunkFrames", "defaultMemorySizeMb", "defaultConcurrency", "pollIntervalMs",- "maxOperationMs", "maxRenderedBytes", "maxPollFailures", "maxAttempts",+ "maxOperationMs", "maxRenderedBytes",] as const;type RuntimeProviderOptions = Omit<@@ -50,10 +50,6 @@ function providerOptions(context: RuntimeAdapterFactoryContext): RuntimeProvider|| /^\d{1,3}(?:\.\d{1,3}){3}$/u.test(bucketName)) {throw new Error("HyperFrames bucketName is invalid");}- const region = runtimeConfigString(config.region, "HyperFrames region");- if (region !== undefined && region !== machine[2]) {- throw new Error(`HyperFrames region ${region} differs from state machine region ${machine[2]}`);- }const quality = runtimeConfigString(config.quality, "HyperFrames quality");if (quality !== undefined && quality !== "draft" && quality !== "standard" && quality !== "high") {throw new Error("HyperFrames quality is invalid");@@ -69,14 +65,11 @@ function providerOptions(context: RuntimeAdapterFactoryContext): RuntimeProviderconst pollIntervalMs = optionalInteger(config, "pollIntervalMs");const maxOperationMs = optionalInteger(config, "maxOperationMs");const maxRenderedBytes = optionalInteger(config, "maxRenderedBytes");- const maxPollFailures = optionalInteger(config, "maxPollFailures");- const maxAttempts = optionalInteger(config, "maxAttempts");return {instance: context.instance,pool: context.pool,stateMachineArn,bucketName,- ...(region === undefined ? {} : { region }),...(quality === undefined ? {} : { quality: quality as HyperframesLambdaQuality }),...(chunkSize === undefined ? {} : { chunkSize }),...(maxParallelChunks === undefined ? {} : { maxParallelChunks }),@@ -86,8 +79,6 @@ function providerOptions(context: RuntimeAdapterFactoryContext): RuntimeProvider...(pollIntervalMs === undefined ? {} : { pollIntervalMs }),...(maxOperationMs === undefined ? {} : { maxOperationMs }),...(maxRenderedBytes === undefined ? {} : { maxRenderedBytes }),- ...(maxPollFailures === undefined ? {} : { maxPollFailures }),- ...(maxAttempts === undefined ? {} : { maxAttempts }),};}diff --git a/packages/provider-hyperframes-aws-lambda/src/provider.ts b/packages/provider-hyperframes-aws-lambda/src/provider.tsindex 9957024c..0c3185c1 100644--- a/packages/provider-hyperframes-aws-lambda/src/provider.ts+++ b/packages/provider-hyperframes-aws-lambda/src/provider.ts@@ -9,9 +9,9 @@ import {import type {EndpointFulfillment,EndpointOutcome,- EndpointResumeContext,+ EndpointPollContext,EndpointStartContext,- RecoverableEndpoint,+ AsyncEndpoint,} from "@narratage/endpoint-kit";import {assertHyperframesDocument,@@ -25,13 +25,10 @@ import {import type { RenderedVisual } from "@narratage/media";import {canonicalize,- digestOf,- isDigest,} from "@narratage/protocol";import type {BlobRef,CanonicalValue,- Digest,} from "@narratage/protocol";import { renderHyperframesCapabilities } from "@narratage/render-hyperframes";import { isStreamingArtifactStore } from "@narratage/runtime";@@ -48,7 +45,7 @@ import type {HyperframesLambdaSite,} from "./client.js";-const CHECKPOINT_CONTRACT = "narratage.hyperframes-aws-lambda-operation@1";+const HANDLE_CONTRACT = "narratage.hyperframes-aws-lambda-operation@1";const SUPPORTED_FPS = new Set([24, 30, 60]);export const awsLambdaHyperframesProviderModuleRef = {@@ -62,7 +59,6 @@ export type CreateAwsLambdaHyperframesProviderOptions = {readonly pool?: string;readonly stateMachineArn: string;readonly bucketName: string;- readonly region?: string;readonly quality?: HyperframesLambdaQuality;readonly chunkSize?: number;readonly maxParallelChunks?: number;@@ -72,27 +68,19 @@ export type CreateAwsLambdaHyperframesProviderOptions = {readonly pollIntervalMs?: number;readonly maxOperationMs?: number;readonly maxRenderedBytes?: number;- readonly maxPollFailures?: number;- readonly maxAttempts?: number;readonly client?: HyperframesAwsLambdaClient;readonly now?: () => number;};-type HyperframesCheckpoint = {- readonly contract: typeof CHECKPOINT_CONTRACT;- readonly requestDigest: Digest;- readonly operationId: Digest;- readonly executionName: string;+type HyperframesHandle = {+ readonly contract: typeof HANDLE_CONTRACT;+ readonly operationId: string;readonly executionArn: string;- readonly renderId: string;readonly outputS3Uri: string;- readonly stateMachineArn: string;readonly bucketName: string;readonly site: HyperframesLambdaSite;readonly startedAt: number;readonly polls: number;- readonly pollFailures: number;- readonly submission: "confirmed" | "unknown";};function assert(condition: unknown, message: string): asserts condition {@@ -143,71 +131,26 @@ export function supportsAwsLambdaHyperframes(value: CanonicalValue): boolean {}}-function implementationFailure(code: string, error: unknown, retryable: boolean): EndpointOutcome {+function implementationFailure(code: string, error: unknown): EndpointOutcome {return {status: "failed",failure: {code,message: error instanceof Error ? error.message : String(error),- retryable,},};}-function executionName(operationId: Digest): string {- return `narratage-${operationId.slice("sha256:".length)}`;-}--function outputKey(operationId: Digest): string {- return `renders/narratage/${operationId.slice("sha256:".length)}/visual.mp4`;-}--function expectedOutputUri(bucketName: string, key: string): string {- return `s3://${bucketName}/${key}`;-}--function sameRender(actual: HyperframesLambdaRender, expected: {- readonly executionName: string;- readonly executionArn: string;- readonly outputS3Uri: string;- readonly stateMachineArn: string;- readonly bucketName: string;- readonly projectS3Uri: string;-}): void {- assert(actual.renderId === expected.executionName, "HyperFrames render id differs from its Operation id");- assert(actual.executionArn === expected.executionArn, "HyperFrames execution ARN differs from its deterministic identity");- assert(actual.outputS3Uri === expected.outputS3Uri, "HyperFrames output URI differs from its deterministic key");- assert(actual.stateMachineArn === expected.stateMachineArn, "HyperFrames render used another state machine");- assert(actual.bucketName === expected.bucketName, "HyperFrames render used another bucket");- assert(actual.projectS3Uri === expected.projectS3Uri, "HyperFrames render used another staged project");-}--function duplicateExecution(error: unknown): boolean {- return error !== null && typeof error === "object"- && (error as { readonly name?: unknown }).name === "ExecutionAlreadyExists";+function operationName(operationId: string): string {+ return operationId.replace(/[^A-Za-z0-9_-]/gu, "-").slice(0, 64);}-function rejectedSubmission(error: unknown): { readonly code: string; readonly retryable: boolean } | undefined {- if (error === null || typeof error !== "object") return undefined;- const name = String((error as { readonly name?: unknown }).name);- if (["ExecutionLimitExceeded", "KmsThrottlingException", "ThrottlingException"].includes(name)) {- return { code: "HYPERFRAMES_SUBMISSION_THROTTLED", retryable: true };- }- if ([- "InvalidConfigError", "ValidationException", "InvalidArn", "InvalidExecutionInput", "InvalidName",- "AccessDeniedException", "KmsAccessDeniedException", "KmsInvalidStateException",- "StateMachineDeleting", "StateMachineDoesNotExist",- ].includes(name)) {- return { code: "HYPERFRAMES_SUBMISSION_REJECTED", retryable: false };- }- return undefined;+function executionName(operationId: string): string {+ return `narratage-${operationName(operationId)}`;}-function missingExecution(error: unknown): boolean {- return error !== null && typeof error === "object"- && ["ExecutionDoesNotExist", "SFN.ExecutionDoesNotExist"].includes(- String((error as { readonly name?: unknown }).name),- );+function outputKey(operationId: string): string {+ return `renders/narratage/${operationName(operationId)}/visual.mp4`;}function alreadyStopped(error: unknown): boolean {@@ -242,22 +185,16 @@ function verifySite(site: HyperframesLambdaSite, bucketName: string): void {assert(typeof site.uploaded === "boolean", "HyperFrames site upload state is invalid");}-function verifyCheckpoint(value: CanonicalValue | undefined, context: EndpointResumeContext): HyperframesCheckpoint {+function readHandle(value: CanonicalValue | undefined, context: EndpointPollContext): HyperframesHandle {assert(value !== undefined && value !== null && typeof value === "object" && !Array.isArray(value),- "HyperFrames checkpoint is absent or invalid");- const item = value as unknown as HyperframesCheckpoint;- assert(item.contract === CHECKPOINT_CONTRACT, "HyperFrames checkpoint contract is invalid");- assert(item.requestDigest === context.need.requestDigest, "HyperFrames checkpoint request differs");- assert(item.operationId === context.operation.id, "HyperFrames checkpoint Operation differs");- assert(item.executionName === executionName(context.operation.id),- "HyperFrames checkpoint execution name differs");- assert(item.renderId === item.executionName, "HyperFrames checkpoint render id differs");- assert(isDigest(item.requestDigest) && isDigest(item.operationId), "HyperFrames checkpoint digest is invalid");- assert(item.startedAt >= 0 && Number.isSafeInteger(item.startedAt), "HyperFrames checkpoint start time is invalid");- nonNegativeInteger(item.polls, "HyperFrames checkpoint polls");- nonNegativeInteger(item.pollFailures, "HyperFrames checkpoint poll failures");- assert(item.submission === "confirmed" || item.submission === "unknown",- "HyperFrames checkpoint submission state is invalid");+ "HyperFrames handle is absent or invalid");+ const item = value as unknown as HyperframesHandle;+ assert(item.contract === HANDLE_CONTRACT, "HyperFrames handle contract is invalid");+ assert(item.operationId === context.operation, "HyperFrames handle Operation differs");+ assert(typeof item.executionArn === "string" && item.executionArn.length > 0,+ "HyperFrames handle execution ARN is missing");+ assert(item.startedAt >= 0 && Number.isSafeInteger(item.startedAt), "HyperFrames handle start time is invalid");+ nonNegativeInteger(item.polls, "HyperFrames handle polls");return structuredClone(item);}@@ -280,18 +217,18 @@ function boundedChunks(}async function storeOutput(- context: EndpointResumeContext,+ context: EndpointPollContext,client: HyperframesAwsLambdaClient,progress: HyperframesLambdaProgress,- checkpoint: HyperframesCheckpoint,+ handle: HyperframesHandle,region: string,maxRenderedBytes: number,): Promise<BlobRef> {const output = progress.outputFile;assert(output !== null, "HyperFrames succeeded without an output file");- assert(output.s3Uri === checkpoint.outputS3Uri, "HyperFrames completed at an unexpected output URI");+ assert(output.s3Uri === handle.outputS3Uri, "HyperFrames completed at an unexpected output URI");const target = parseS3Uri(output.s3Uri);- assert(target.bucket === checkpoint.bucketName, "HyperFrames output is outside its configured bucket");+ assert(target.bucket === handle.bucketName, "HyperFrames output is outside its configured bucket");if (output.bytes !== null) positiveInteger(output.bytes, "HyperFrames output bytes");const source = await client.openOutput({ s3Uri: output.s3Uri, region });if (source.contentLength !== undefined) {@@ -401,15 +338,7 @@ function verifyProgress(progress: HyperframesLambdaProgress, document: Hyperframexport function createAwsLambdaHyperframesProvider(config: CreateAwsLambdaHyperframesProviderOptions) {const machine = stateMachine(config.stateMachineArn);- const region = config.region ?? machine.region;- assert(region === machine.region,- `HyperFrames region ${region} differs from state machine region ${machine.region}`);- assert(/^[a-z0-9][a-z0-9.-]{1,61}[a-z0-9]$/u.test(config.bucketName)- && !config.bucketName.includes("..")- && !config.bucketName.includes(".-")- && !config.bucketName.includes("-.")- && !/^\d{1,3}(?:\.\d{1,3}){3}$/u.test(config.bucketName),- "HyperFrames bucketName is invalid");+ const region = machine.region;const quality = config.quality ?? "standard";assert(["draft", "standard", "high"].includes(quality), "HyperFrames quality is invalid");const chunkSize = config.chunkSize === undefined ? undefined : positiveInteger(config.chunkSize, "chunkSize");@@ -423,44 +352,29 @@ export function createAwsLambdaHyperframesProvider(config: CreateAwsLambdaHyperfconst maxOperationMs = positiveInteger(config.maxOperationMs ?? 6 * 60 * 60_000, "maxOperationMs");const maxRenderedBytes = positiveInteger(config.maxRenderedBytes ?? 16 * 1024 * 1024 * 1024,"maxRenderedBytes");- const maxPollFailures = positiveInteger(config.maxPollFailures ?? 5, "maxPollFailures");- const maxAttempts = positiveInteger(config.maxAttempts ?? 1, "maxAttempts");const client = config.client ?? createHyperframesAwsLambdaClient(region);const now = config.now ?? Date.now;- const makeCheckpoint = (+ const makeHandle = (context: EndpointStartContext,site: HyperframesLambdaSite,- submission: HyperframesCheckpoint["submission"],+ handle: HyperframesLambdaRender,startedAt: number,- ): HyperframesCheckpoint => {- const name = executionName(context.operation.id);- const output = expectedOutputUri(config.bucketName, outputKey(context.operation.id));- const expectedArn = executionArn(machine, name);+ ): HyperframesHandle => {return {- contract: CHECKPOINT_CONTRACT,- requestDigest: context.need.requestDigest,- operationId: context.operation.id,- executionName: name,- executionArn: expectedArn,- renderId: name,- outputS3Uri: output,- stateMachineArn: machine.arn,- bucketName: config.bucketName,+ contract: HANDLE_CONTRACT,+ operationId: context.operation,+ executionArn: handle.executionArn,+ outputS3Uri: handle.outputS3Uri,+ bucketName: handle.bucketName,site,startedAt,polls: 0,- pollFailures: 0,- submission,};};- const submit = async (- context: EndpointStartContext,- existingSite?: HyperframesLambdaSite,- existingStartedAt?: number,- ): Promise<EndpointOutcome> => {- const startedAt = existingStartedAt ?? now();+ const submit = async (context: EndpointStartContext): Promise<EndpointOutcome> => {+ const startedAt = now();const document = requestDocument(context.need.constraints);const renderConfig = renderConfiguration(document, {quality,@@ -468,127 +382,82 @@ export function createAwsLambdaHyperframesProvider(config: CreateAwsLambdaHyperfmaxParallelChunks,...(targetChunkFrames === undefined ? {} : { targetChunkFrames }),});- let site = existingSite;- if (site === undefined) {- const work = await mkdtemp(join(tmpdir(), "narratage-hyperframes-aws-"));- try {- await stageHyperframesProject({- document,- directory: work,- read: (artifact) => artifactBytes(context, artifact),- });- site = await client.deploySite({ projectDir: work, bucketName: config.bucketName, region });- } catch (error) {- return implementationFailure("HYPERFRAMES_SITE_DEPLOY_FAILED", error, true);- } finally {- await rm(work, { recursive: true, force: true }).catch(() => {});- }+ let site: HyperframesLambdaSite;+ const work = await mkdtemp(join(tmpdir(), "narratage-hyperframes-aws-"));+ try {+ await stageHyperframesProject({+ document,+ directory: work,+ read: (artifact) => artifactBytes(context, artifact),+ });+ site = await client.deploySite({ projectDir: work, bucketName: config.bucketName, region });+ } catch (error) {+ return implementationFailure("HYPERFRAMES_SITE_DEPLOY_FAILED", error);+ } finally {+ await rm(work, { recursive: true, force: true }).catch(() => {});}verifySite(site, config.bucketName);- const checkpoint = makeCheckpoint(context, site, "unknown", startedAt);- let handle: HyperframesLambdaRender;+ let render: HyperframesLambdaRender;try {- handle = await client.render({+ render = await client.render({siteHandle: site,config: renderConfig,bucketName: config.bucketName,stateMachineArn: machine.arn,region,- outputKey: outputKey(context.operation.id),- executionName: checkpoint.executionName,+ outputKey: outputKey(context.operation),+ executionName: executionName(context.operation),});} catch (error) {- if (duplicateExecution(error)) {- return wakeAfter(canonicalize({ ...checkpoint, submission: "confirmed" }), pollIntervalMs, now(), {- phase: "submitted",- });- }- const rejected = rejectedSubmission(error);- if (rejected !== undefined) return implementationFailure(rejected.code, error, rejected.retryable);- // StartExecution may have reached AWS even when its response did not reach us. The- // deterministic execution name makes polling this derived ARN safer than submitting a new job.- return wakeAfter(canonicalize(checkpoint), pollIntervalMs, now(), {- phase: "confirming-submission",- });- }- try {- sameRender(handle, {- executionName: checkpoint.executionName,- executionArn: checkpoint.executionArn,- outputS3Uri: checkpoint.outputS3Uri,- stateMachineArn: checkpoint.stateMachineArn,- bucketName: checkpoint.bucketName,- projectS3Uri: checkpoint.site.projectS3Uri,- });- } catch (error) {- return implementationFailure("HYPERFRAMES_SUBMISSION_IDENTITY_MISMATCH", error, false);+ return implementationFailure("HYPERFRAMES_SUBMISSION_FAILED", error);}- return wakeAfter(canonicalize({ ...checkpoint, submission: "confirmed" }), pollIntervalMs, now(), {+ const handle = makeHandle(context, site, render, startedAt);+ return wakeAfter(canonicalize(handle), pollIntervalMs, now(), {phase: "submitted",});};const safeSubmit = async (context: EndpointStartContext,- existingSite?: HyperframesLambdaSite,- existingStartedAt?: number,): Promise<EndpointOutcome> => {try {- return await submit(context, existingSite, existingStartedAt);+ return await submit(context);} catch (error) {- return implementationFailure("HYPERFRAMES_SUBMISSION_INVALID", error, false);+ return implementationFailure("HYPERFRAMES_SUBMISSION_INVALID", error);}};- const endpoint: RecoverableEndpoint = {+ const endpoint: AsyncEndpoint = {start: async (context) => await safeSubmit(context),- async resume(context) {- if (context.checkpoint === undefined) return await safeSubmit(context);- let checkpoint: HyperframesCheckpoint;+ async poll(context) {+ let handle: HyperframesHandle;try {- checkpoint = verifyCheckpoint(context.checkpoint, context);- verifySite(checkpoint.site, config.bucketName);- assert(checkpoint.stateMachineArn === machine.arn && checkpoint.bucketName === config.bucketName,- "HyperFrames checkpoint deployment differs");- assert(checkpoint.executionArn === executionArn(machine, checkpoint.executionName),- "HyperFrames checkpoint execution ARN differs");- assert(checkpoint.outputS3Uri === expectedOutputUri(config.bucketName, outputKey(context.operation.id)),- "HyperFrames checkpoint output URI differs");+ handle = readHandle(context.handle, context);} catch (error) {- return implementationFailure("HYPERFRAMES_CHECKPOINT_INVALID", error, false);+ return implementationFailure("HYPERFRAMES_HANDLE_INVALID", error);}- if (now() - checkpoint.startedAt >= maxOperationMs) {+ if (now() - handle.startedAt >= maxOperationMs) {return implementationFailure("HYPERFRAMES_OPERATION_TIMEOUT",- new Error("HyperFrames render exceeded its operation deadline"), false);+ new Error("HyperFrames render exceeded its operation deadline"));}let document: HyperframesDocument;let progress: HyperframesLambdaProgress;try {document = requestDocument(context.need.constraints);progress = await client.progress({- executionArn: checkpoint.executionArn,+ executionArn: handle.executionArn,defaultMemorySizeMb,region,});} catch (error) {- if (checkpoint.submission === "unknown" && missingExecution(error)) {- return await safeSubmit(context, checkpoint.site, checkpoint.startedAt);- }- const failures = checkpoint.pollFailures + 1;- if (failures >= maxPollFailures) {- return implementationFailure("HYPERFRAMES_PROGRESS_UNAVAILABLE", error, true);- }- return wakeAfter(canonicalize({ ...checkpoint, pollFailures: failures }), pollIntervalMs, now(), {- phase: "progress-retry",- });+ return implementationFailure("HYPERFRAMES_PROGRESS_UNAVAILABLE", error);}try {verifyProgress(progress, document);} catch (error) {- return implementationFailure("HYPERFRAMES_PROGRESS_INVALID", error, false);+ return implementationFailure("HYPERFRAMES_PROGRESS_INVALID", error);}- const next = { ...checkpoint, submission: "confirmed" as const,- polls: checkpoint.polls + 1, pollFailures: 0 };+ const next = { ...handle, polls: handle.polls + 1 };if (progress.status === "RUNNING") {return wakeAfter(canonicalize(next), pollIntervalMs, now(), {phase: "rendering",@@ -600,8 +469,7 @@ export function createAwsLambdaHyperframesProvider(config: CreateAwsLambdaHyperfif (progress.status !== "SUCCEEDED") {const details = progress.errors.map((item) => `${item.state}: ${item.error}: ${item.cause}`).join("; ");return implementationFailure(`HYPERFRAMES_${progress.status}`,- new Error(details || `HyperFrames execution ended as ${progress.status}`),- progress.status === "FAILED" || progress.status === "TIMED_OUT" || progress.status === "PENDING_REDRIVE");+ new Error(details || `HyperFrames execution ended as ${progress.status}`));}try {const artifact = await storeOutput(context, client, progress, next, region, maxRenderedBytes);@@ -610,24 +478,16 @@ export function createAwsLambdaHyperframesProvider(config: CreateAwsLambdaHyperfresult: fulfillment(document, artifact),};} catch (error) {- return implementationFailure("HYPERFRAMES_OUTPUT_INVALID", error, true);+ return implementationFailure("HYPERFRAMES_OUTPUT_INVALID", error);}},async cancel(context) {- const target = executionArn(machine, executionName(context.operation.id));- if (context.checkpoint !== undefined) {- const checkpoint = verifyCheckpoint(context.checkpoint, context);- verifySite(checkpoint.site, config.bucketName);- assert(checkpoint.stateMachineArn === machine.arn && checkpoint.bucketName === config.bucketName,- "HyperFrames checkpoint deployment differs");- assert(checkpoint.executionArn === target,- "HyperFrames checkpoint execution ARN differs");- }+ const target = readHandle(context.handle, context).executionArn;try {await client.stop({executionArn: target,region,- reason: `Narratage cancelled Operation ${context.operation.id}`,+ reason: `Narratage cancelled Operation ${context.operation}`,});} catch (error) {if (!alreadyStopped(error)) throw error;@@ -643,12 +503,11 @@ export function createAwsLambdaHyperframesProvider(config: CreateAwsLambdaHyperfpool: config.pool ?? config.instance ?? "hyperframes.aws-lambda",defaultConcurrency: config.defaultConcurrency ?? 2,capabilities: [{- lifecycle: "recoverable" as const,+ lifecycle: "asynchronous" as const,capability: renderHyperframesCapabilities.renderVisual,returns: mediaTypes.renderedVisual,supports: (need) => supportsAwsLambdaHyperframes(need.constraints),endpoint,- retry: { maxAttempts },}],});}diff --git a/packages/provider-hyperframes-aws-lambda/test/live-canary.ts b/packages/provider-hyperframes-aws-lambda/test/live-canary.tsindex 4f018fff..63c909d9 100644--- a/packages/provider-hyperframes-aws-lambda/test/live-canary.ts+++ b/packages/provider-hyperframes-aws-lambda/test/live-canary.ts@@ -23,7 +23,7 @@ import {} from "@narratage/driver-node";import type {EndpointOutcome,- RecoverableEndpoint,+ AsyncEndpoint,} from "@narratage/endpoint-kit";import { compileHyperframesDocument } from "@narratage/hyperframes";import type { HyperframesDocument } from "@narratage/hyperframes";@@ -37,8 +37,7 @@ import {createAwsLambdaHyperframesProvider,} from "@narratage/provider-hyperframes-aws-lambda";import {- digestOf,-} from "@narratage/protocol";+ } from "@narratage/protocol";import type {CanonicalValue,Need,@@ -47,7 +46,6 @@ import {hyperframesVisualRequest,renderHyperframesCapabilities,} from "@narratage/render-hyperframes";-import { sealOperationIdentity } from "@narratage/runtime";function requiredEnvironment(name: string): string {const value = process.env[name];@@ -110,23 +108,16 @@ function requestNeed(document: HyperframesDocument, canaryId: string): Need {capability: renderHyperframesCapabilities.renderVisual,returns: mediaTypes.renderedVisual,constraints,- requestedBy: `derivation:hyperframes-aws-canary:${canaryId}`,result: `record:hyperframes-aws-canary:${canaryId}`,- requestDigest: digestOf({- capability: renderHyperframesCapabilities.renderVisual,- returns: mediaTypes.renderedVisual,- constraints,- }),};}-async function endpointFor(request: Need): Promise<RecoverableEndpoint> {+async function endpointFor(request: Need): Promise<AsyncEndpoint> {const registry = new EndpointRegistry();await createAwsLambdaHyperframesProvider({instance: "hyperframes.aws-lambda.canary",stateMachineArn,bucketName,- region,quality: "draft",targetChunkFrames: 12,maxParallelChunks: 2,@@ -134,11 +125,10 @@ async function endpointFor(request: Need): Promise<RecoverableEndpoint> {defaultConcurrency: 1,pollIntervalMs: 2_000,maxOperationMs: 10 * 60_000,- maxPollFailures: 10,}).install(registry);const resolution = registry.resolve(request);assert.equal(resolution.status, "resolved");- assert.equal(resolution.registration.kind, "recoverable");+ assert.equal(resolution.registration.kind, "asynchronous");return resolution.registration.endpoint;}@@ -159,20 +149,13 @@ async function deletePrefix(s3: S3Client, prefix: string): Promise<void> {}async function main(): Promise<void> {- const canaryId = digestOf(process.env.NARRATAGE_HYPERFRAMES_CANARY_ID ?? randomUUID()).slice(7, 23);+ const canaryId = process.env.NARRATAGE_HYPERFRAMES_CANARY_ID ?? randomUUID().slice(0, 16);const document = documentFixture(canaryId);const need = requestNeed(document, canaryId);const endpoint = await endpointFor(need);const artifacts = new MemoryArtifactStore();const commandId = `command:hyperframes-aws-canary:${canaryId}`;- const operation = sealOperationIdentity({- build: `build:hyperframes-aws-canary:${canaryId}`,- command: commandId,- endpoint: "hyperframes.aws-lambda.canary",- pool: "hyperframes.aws-lambda.canary",- lane: "fixture.render",- attempt: 1,- });+ const operation = `operation:hyperframes-aws-canary:${canaryId}`;const context = {command: { kind: "fulfill-need", id: commandId, need } as const,need,@@ -180,16 +163,16 @@ async function main(): Promise<void> {credentials: {},operation,};- let lastCheckpoint: CanonicalValue | undefined;+ let lastHandle: CanonicalValue | undefined;const work = await mkdtemp(join(tmpdir(), "narratage-hyperframes-canary-"));try {let outcome: EndpointOutcome = await endpoint.start(context);for (let polls = 0; outcome.status === "pending"; polls += 1) {assert(polls < 300, "HyperFrames canary exceeded 300 polls");- lastCheckpoint = outcome.checkpoint;+ lastHandle = outcome.handle;const waitMs = Math.max(0, (outcome.wakeAt ?? Date.now()) - Date.now());if (waitMs > 0) await delay(waitMs);- outcome = await endpoint.resume({ ...context, checkpoint: outcome.checkpoint });+ outcome = await endpoint.poll({ ...context, handle: outcome.handle });}if (outcome.status === "failed") {throw new Error(`${outcome.failure.code}: ${outcome.failure.message}`);@@ -230,11 +213,11 @@ async function main(): Promise<void> {}, null, 2)}\n`);} finally {await rm(work, { recursive: true, force: true });- if (process.env.NARRATAGE_HYPERFRAMES_CANARY_KEEP !== "1" && lastCheckpoint !== undefined) {- const checkpoint = lastCheckpoint as unknown as { executionName?: string; site?: { siteId?: string } };+ if (process.env.NARRATAGE_HYPERFRAMES_CANARY_KEEP !== "1" && lastHandle !== undefined) {+ const handle = lastHandle as unknown as { executionName?: string; site?: { siteId?: string } };const s3 = new S3Client({ region });- if (checkpoint.executionName !== undefined) await deletePrefix(s3, `renders/${checkpoint.executionName}/`);- if (checkpoint.site?.siteId !== undefined) await deletePrefix(s3, `sites/${checkpoint.site.siteId}/`);+ if (handle.executionName !== undefined) await deletePrefix(s3, `renders/${handle.executionName}/`);+ if (handle.site?.siteId !== undefined) await deletePrefix(s3, `sites/${handle.site.siteId}/`);}}}diff --git a/packages/provider-hyperframes-aws-lambda/test/provider.test.ts b/packages/provider-hyperframes-aws-lambda/test/provider.test.tsindex bb7207ce..4f9bc456 100644--- a/packages/provider-hyperframes-aws-lambda/test/provider.test.ts+++ b/packages/provider-hyperframes-aws-lambda/test/provider.test.ts@@ -1,11 +1,12 @@import assert from "node:assert/strict";import { readFile } from "node:fs/promises";import test from "node:test";+import { fixtureDigest } from "../../../test/fixture-digest.js";import { validateDistributedRenderConfig } from "@hyperframes/aws-lambda/sdk";import { EndpointRegistry, MemoryArtifactStore } from "@narratage/driver-node";import type { EndpointRegistration } from "@narratage/driver-node";-import type { RecoverableEndpoint } from "@narratage/endpoint-kit";+import type { AsyncEndpoint } from "@narratage/endpoint-kit";import type { HyperframesDocument } from "@narratage/hyperframes";import { mediaTypes, verifyRenderedVisual } from "@narratage/media";import type { RenderedVisual } from "@narratage/media";@@ -20,7 +21,7 @@ import type {HyperframesLambdaRenderConfig,HyperframesLambdaSite,} from "@narratage/provider-hyperframes-aws-lambda";-import { canonicalize, digestOf } from "@narratage/protocol";+import { canonicalize } from "@narratage/protocol";import type { CanonicalValue, Need } from "@narratage/protocol";import type { RuntimeEndpointAdapterImplementation } from "@narratage/runtime-kit";import {@@ -28,7 +29,6 @@ import {renderHyperframesCapabilities,} from "@narratage/render-hyperframes";import type { ArtifactStore, StreamingArtifactStore } from "@narratage/runtime";-import { sealOperationIdentity } from "@narratage/runtime";import { narratagePackage as awsLambdaActivation } from "../src/activation.js";@@ -56,29 +56,12 @@ function requestNeed(document = documentFixture()): Need {capability: renderHyperframesCapabilities.renderVisual,returns: mediaTypes.renderedVisual,constraints,- requestedBy: "derivation:hyperframes-lambda-test",result: "record:hyperframes-lambda-test",- requestDigest: digestOf({- capability: renderHyperframesCapabilities.renderVisual,- returns: mediaTypes.renderedVisual,- constraints,- }),};}function operation(request: Need) {- return sealOperationIdentity({- build: "build:hyperframes-lambda-test",- command: "command:hyperframes-lambda-test",- endpoint: "hyperframes.aws-lambda.test",- pool: "hyperframes.aws-lambda.test",- lane: "fixture.render",- attempt: 1,- });-}--function outputKey(operationId: string): string {- return `renders/narratage/${operationId.slice("sha256:".length)}/visual.mp4`;+ return `operation:${request.id}`;}function successfulProgress(outputS3Uri: string, overrides: Partial<HyperframesLambdaProgress> = {}) {@@ -173,8 +156,7 @@ function fakeClient(options: {async progress(input) {state.progressCalls += 1;if (options.progress !== undefined) return await options.progress(input.executionArn, state.progressCalls);- const name = input.executionArn.slice(EXECUTION_PREFIX.length);- return successfulProgress(`s3://${BUCKET}/${outputKey(`sha256:${name.slice("narratage-".length)}`)}`);+ return successfulProgress(`s3://${BUCKET}/${state.renderInputs.at(-1)!.outputKey}`);},async stop(input) {state.stopped.push(input.executionArn);@@ -199,7 +181,7 @@ function fakeClient(options: {async function endpointFor(request: Need,client: HyperframesAwsLambdaClient,-): Promise<{ readonly endpoint: RecoverableEndpoint; readonly registration: EndpointRegistration }> {+): Promise<{ readonly endpoint: AsyncEndpoint; readonly registration: EndpointRegistration }> {const registry = new EndpointRegistry();const provider = createAwsLambdaHyperframesProvider({instance: "hyperframes.aws-lambda.test",@@ -208,13 +190,12 @@ async function endpointFor(client,pollIntervalMs: 1,maxOperationMs: 60_000,- maxPollFailures: 2,now: () => 1_000,});await provider.install(registry);const resolution = registry.resolve(request);assert.equal(resolution.status, "resolved");- assert.equal(resolution.registration.kind, "recoverable");+ assert.equal(resolution.registration.kind, "asynchronous");return { endpoint: resolution.registration.endpoint, registration: resolution.registration };}@@ -240,7 +221,7 @@ test("the Lambda Endpoint declines frame domains and requirements it cannot pres})), false, "an unsupported hardware requirement must fall through to another Endpoint");const artifact = {kind: "blob" as const,- digest: digestOf("lambda-surface-without-verifier"),+ digest: fixtureDigest("lambda-surface-without-verifier"),size: 100,mediaType: "image/png",};@@ -280,7 +261,6 @@ test("one deterministic submission resumes and streams the exact output into theconst request = requestNeed();const { client, state } = fakeClient();const { endpoint, registration } = await endpointFor(request, client);- assert.equal(registration.retry?.maxAttempts, 1);assert.equal(registration.scheduling?.resources.find((item) =>item.id.startsWith("pool:"))?.maxActive, 2);@@ -307,6 +287,7 @@ test("one deterministic submission resumes and streams the exact output into theconst common = context(request, artifacts);const started = await endpoint.start(common);assert.equal(started.status, "pending");+ if (started.status !== "pending") return;assert.equal(state.deployCalls, 1);assert.equal(state.renderInputs.length, 1);assert.equal(state.renderInputs[0]!.config.fps, 30);@@ -315,13 +296,13 @@ test("one deterministic submission resumes and streams the exact output into theassert.doesNotThrow(() => validateDistributedRenderConfig(state.renderInputs[0]!.config),"the locked SDK must accept the exact configuration sent by the Endpoint");assert.equal(state.renderInputs[0]!.executionName,- `narratage-${common.operation.id.slice("sha256:".length)}`);+ `narratage-${common.operation.replace(/[^A-Za-z0-9_-]/gu, "-")}`);- const completed = await endpoint.resume({+ const completed = await endpoint.poll({...common,- checkpoint: started.status === "pending" ? started.checkpoint : undefined,+ handle: started.handle,});- assert.equal(completed.status, "completed");+ assert.equal(completed.status, "completed", JSON.stringify(completed));assert.equal(streamed, 2, "the remote object should use the streaming ArtifactStore facet");assert.equal(state.opened.length, 1);if (completed.status !== "completed" || completed.result.value.kind !== "inline") return;@@ -332,45 +313,7 @@ test("one deterministic submission resumes and streams the exact output into theassert.deepEqual(await artifacts.get(visual.artifact.digest), new Uint8Array([1, 2, 3, 4, 5]));});-test("an ambiguous StartExecution is recovered by the same name without redeploying the site", async () => {- let renderCalls = 0;- const request = requestNeed();- const { client, state } = fakeClient({- async render() {- renderCalls += 1;- if (renderCalls === 1) throw new TypeError("socket closed after request write");- const error = new Error("execution already exists");- error.name = "ExecutionAlreadyExists";- throw error;- },- async progress(_executionArn, call) {- if (call === 1) {- const error = new Error("not visible yet");- error.name = "ExecutionDoesNotExist";- throw error;- }- throw new Error("test should stop after the duplicate is confirmed");- },- });- const { endpoint } = await endpointFor(request, client);- const common = context(request, new MemoryArtifactStore());- const uncertain = await endpoint.start(common);- assert.equal(uncertain.status, "pending");- const confirmed = await endpoint.resume({- ...common,- checkpoint: uncertain.status === "pending" ? uncertain.checkpoint : undefined,- });- assert.equal(confirmed.status, "pending");- assert.equal(state.deployCalls, 1, "the immutable staged site belongs in the checkpoint");- assert.equal(state.renderInputs.length, 2);- assert.equal(state.renderInputs[0]!.executionName, state.renderInputs[1]!.executionName);- assert.deepEqual(state.renderInputs[0]!.config, state.renderInputs[1]!.config);- if (confirmed.status === "pending") {- assert.equal((confirmed.checkpoint as Record<string, unknown>).submission, "confirmed");- }-});--test("running render progress is projected without exposing the recovery checkpoint", async () => {+test("running render progress is projected without exposing the private handle", async () => {const request = requestNeed();const { client } = fakeClient({async progress() {@@ -387,9 +330,10 @@ test("running render progress is projected without exposing the recovery checkpoconst { endpoint } = await endpointFor(request, client);const common = context(request, new MemoryArtifactStore());const started = await endpoint.start(common);- const running = await endpoint.resume({+ if (started.status !== "pending") return;+ const running = await endpoint.poll({...common,- checkpoint: started.status === "pending" ? started.checkpoint : undefined,+ handle: started.handle,});assert.equal(running.status, "pending");if (running.status === "pending") {@@ -398,123 +342,18 @@ test("running render progress is projected without exposing the recovery checkpo}});-test("a successful execution with another frame domain is refused before its bytes are opened", async () => {- const request = requestNeed();- let outputUri = "";- const { client, state } = fakeClient({- async render(input) {- outputUri = `s3://${BUCKET}/${input.outputKey}`;- return {- renderId: input.executionName,- executionArn: `${EXECUTION_PREFIX}${input.executionName}`,- bucketName: BUCKET,- stateMachineArn: STATE_MACHINE,- outputS3Uri: outputUri,- projectS3Uri: input.site.projectS3Uri,- startedAt: "2026-08-08T00:00:00.000Z",- };- },- async progress() {- return successfulProgress(outputUri, { totalFrames: 61, framesRendered: 61 });- },- });- const { endpoint } = await endpointFor(request, client);- const common = context(request, new MemoryArtifactStore());- const started = await endpoint.start(common);- const failed = await endpoint.resume({- ...common,- checkpoint: started.status === "pending" ? started.checkpoint : undefined,- });- assert.equal(failed.status, "failed");- if (failed.status === "failed") {- assert.equal(failed.failure.code, "HYPERFRAMES_PROGRESS_INVALID");- assert.equal(failed.failure.retryable, false);- }- assert.equal(state.opened.length, 0);-});--test("a handle that names another execution is not downgraded to an ambiguous submission", async () => {- const request = requestNeed();- const { client } = fakeClient({- async render(input) {- return {- renderId: "another-render",- executionArn: `${EXECUTION_PREFIX}another-render`,- bucketName: BUCKET,- stateMachineArn: STATE_MACHINE,- outputS3Uri: `s3://${BUCKET}/${input.outputKey}`,- projectS3Uri: input.site.projectS3Uri,- startedAt: "2026-08-08T00:00:00.000Z",- };- },- });- const { endpoint } = await endpointFor(request, client);- const failed = await endpoint.start(context(request, new MemoryArtifactStore()));- assert.equal(failed.status, "failed");- if (failed.status === "failed") {- assert.equal(failed.failure.code, "HYPERFRAMES_SUBMISSION_IDENTITY_MISMATCH");- assert.equal(failed.failure.retryable, false);- }-});--test("a client-side config rejection is terminal, not an ambiguous remote submission", async () => {- const request = requestNeed();- const { client } = fakeClient({- async render() {- const error = new Error("invalid distributed config");- error.name = "InvalidConfigError";- throw error;- },- });- const { endpoint } = await endpointFor(request, client);- const failed = await endpoint.start(context(request, new MemoryArtifactStore()));- assert.equal(failed.status, "failed");- if (failed.status === "failed") {- assert.equal(failed.failure.code, "HYPERFRAMES_SUBMISSION_REJECTED");- assert.equal(failed.failure.retryable, false);- }-});--test("cancellation stops the deterministic execution even before a checkpoint exists", async () => {+test("cancellation stops the submitted execution", async () => {const request = requestNeed();const { client, state } = fakeClient();const { endpoint } = await endpointFor(request, client);assert.ok(endpoint.cancel);const common = context(request, new MemoryArtifactStore());- await endpoint.cancel({ ...common, checkpoint: undefined });- assert.deepEqual(state.stopped, [- `${EXECUTION_PREFIX}narratage-${common.operation.id.slice("sha256:".length)}`,- ]);const started = await endpoint.start(common);assert.equal(started.status, "pending");if (started.status !== "pending") return;- await assert.rejects(async () => await endpoint.cancel!({+ await endpoint.cancel({...common,- checkpoint: canonicalize({- ...(started.checkpoint as Record<string, CanonicalValue>),- executionArn: `${EXECUTION_PREFIX}someone-elses-render`,- }),- }), /checkpoint execution ARN differs/u);- assert.equal(state.stopped.length, 1, "an untrusted checkpoint cannot redirect StopExecution");-});--test("the deployment identity is validated before any AWS client is constructed", () => {- assert.throws(() => createAwsLambdaHyperframesProvider({- stateMachineArn: `${STATE_MACHINE}:mutable-alias`,- bucketName: BUCKET,- }), /unqualified AWS Step Functions state-machine ARN/u);- assert.throws(() => createAwsLambdaHyperframesProvider({- stateMachineArn: STATE_MACHINE,- bucketName: BUCKET,- region: "eu-west-1",- }), /differs from state machine region/u);- assert.throws(() => createAwsLambdaHyperframesProvider({- stateMachineArn: STATE_MACHINE,- bucketName: "192.168.0.1",- }), /bucketName is invalid/u);- assert.doesNotThrow(() => createAwsLambdaHyperframesProvider({- stateMachineArn: "arn:aws-us-gov:states:us-gov-west-1:123456789012:stateMachine:narratage-hyperframes",- bucketName: BUCKET,- client: fakeClient().client,- }));+ handle: started.handle,+ });+ assert.equal(state.stopped.length, 1);});diff --git a/packages/provider-hyperframes-local/src/provider.ts b/packages/provider-hyperframes-local/src/provider.tsindex 4a7cdd39..892b889e 100644--- a/packages/provider-hyperframes-local/src/provider.ts+++ b/packages/provider-hyperframes-local/src/provider.ts@@ -12,7 +12,7 @@ import { assertHyperframesDocument } from "@narratage/hyperframes";import type { HyperframesDocument } from "@narratage/hyperframes";import { stageHyperframesProject } from "@narratage/hyperframes/project";import { renderHyperframesCapabilities } from "@narratage/render-hyperframes";-import { canonicalize, digestOf } from "@narratage/protocol";+import { canonicalize } from "@narratage/protocol";import { isStreamingArtifactStore } from "@narratage/runtime";import type { BlobRef, CanonicalValue } from "@narratage/protocol";import { defineEndpointPackage } from "@narratage/endpoint-kit";diff --git a/packages/provider-hyperframes-local/test/provider.test.ts b/packages/provider-hyperframes-local/test/provider.test.tsindex 6c701c10..7d06a165 100644--- a/packages/provider-hyperframes-local/test/provider.test.ts+++ b/packages/provider-hyperframes-local/test/provider.test.ts@@ -10,8 +10,9 @@ import type { EndpointRegistration } from "@narratage/driver-node";import type { ImmediateEndpointHandler } from "@narratage/endpoint-kit";import { compileHyperframesDocument } from "@narratage/hyperframes";import { renderHyperframesCapabilities, hyperframesVisualRequest } from "@narratage/render-hyperframes";-import { canonicalize, digestOf } from "@narratage/protocol";+import { canonicalize } from "@narratage/protocol";import type { CanonicalValue, Need } from "@narratage/protocol";+import { fixtureDigest } from "../../../test/fixture-digest.js";import { createLocalHyperframesProvider } from "../src/index.js";import { localHyperframesBrowserProgram } from "../src/program.js";@@ -26,7 +27,7 @@ function documentFixture(surface?: CompositableSurfaceRef) {});const track = sealVisualTrack({visualIr: "narratage.visual-ir@1",- id: "provider-proof",+ id: "provider-fixture",presents: [{id: "card",span: { startFrame: 0, endFrameExclusive: 12 },@@ -60,7 +61,7 @@ function documentFixture(surface?: CompositableSurfaceRef) {}],});return compileHyperframesDocument(sealComposition({- id: "local-hyperframes-provider-proof",+ id: "local-hyperframes-provider-fixture",canvas: { width: 160, height: 96, clearColor: "#000000" },tracks: [track],}), programSpace);@@ -69,17 +70,11 @@ function documentFixture(surface?: CompositableSurfaceRef) {function requestNeed(document = documentFixture()): Need {const constraints = hyperframesVisualRequest(document);return {- id: "need:local-hyperframes-proof",+ id: "need:local-hyperframes-fixture",capability: renderHyperframesCapabilities.renderVisual,returns: mediaTypes.renderedVisual,constraints,- requestedBy: "derivation:local-hyperframes-proof",- result: "record:local-hyperframes-proof",- requestDigest: digestOf({- capability: renderHyperframesCapabilities.renderVisual,- returns: mediaTypes.renderedVisual,- constraints,- }),+ result: "record:local-hyperframes-fixture",};}@@ -144,7 +139,7 @@ test("local HyperFrames Provider really renders a silent frame-exact MP4 with paconst request = requestNeed(documentFixture(surface));const { handler } = await handlerFor(request);const output = await handler({- command: { kind: "fulfill-need", id: "command:local-hyperframes-proof", need: request },+ command: { kind: "fulfill-need", id: "command:local-hyperframes-fixture", need: request },need: request,artifacts,credentials: {},diff --git a/packages/provider-image-opencv-local/src/provider.ts b/packages/provider-image-opencv-local/src/provider.tsindex 9319e5e6..2e6eb477 100644--- a/packages/provider-image-opencv-local/src/provider.ts+++ b/packages/provider-image-opencv-local/src/provider.ts@@ -7,7 +7,7 @@ import { fileURLToPath } from "node:url";import { artifactTypes } from "@narratage/artifact";import { defineEndpointPackage } from "@narratage/endpoint-kit";import type { EndpointFulfillment } from "@narratage/endpoint-kit";-import { canonicalize, digestOf } from "@narratage/protocol";+import { canonicalize } from "@narratage/protocol";import type { CanonicalValue } from "@narratage/protocol";import {assertRasterRequest,diff --git a/packages/provider-image-opencv-local/test/provider.test.ts b/packages/provider-image-opencv-local/test/provider.test.tsindex 9abeeadf..89e20968 100644--- a/packages/provider-image-opencv-local/test/provider.test.ts+++ b/packages/provider-image-opencv-local/test/provider.test.ts@@ -1,6 +1,7 @@import assert from "node:assert/strict";import { spawnSync } from "node:child_process";import test from "node:test";+import { fixtureDigest } from "../../../test/fixture-digest.js";import { artifactTypes } from "@narratage/artifact";import { EndpointRegistry, MemoryArtifactStore } from "@narratage/driver-node";@@ -13,7 +14,7 @@ import {createLocalOpenCvImageProvider,localOpenCvImageProviderModuleRef,} from "@narratage/provider-image-opencv-local";-import { canonicalize, digestOf } from "@narratage/protocol";+import { canonicalize } from "@narratage/protocol";import type { BlobRef, Need } from "@narratage/protocol";import { rasterCapabilities } from "@narratage/raster";@@ -31,13 +32,7 @@ function need(source: BlobRef, program: ImageTransformProgram = gptImageDenoiseVcapability: rasterCapabilities.execute,returns: artifactTypes.blob,constraints,- requestedBy: "derivation:image-transform",result: "record:image-transform",- requestDigest: digestOf({- capability: rasterCapabilities.execute,- returns: artifactTypes.blob,- constraints,- }),};}@@ -57,8 +52,7 @@ function composeNeed(source: BlobRef): Need {});return {id: "need:image-compose", capability: rasterCapabilities.execute, returns: artifactTypes.blob,- constraints, requestedBy: "derivation:image-compose", result: "record:image-compose",- requestDigest: digestOf({ capability: rasterCapabilities.execute, constraints }),+ constraints, result: "record:image-compose",};}diff --git a/packages/provider-kie/src/provider.ts b/packages/provider-kie/src/provider.tsindex 5980fa45..0dd9b15b 100644--- a/packages/provider-kie/src/provider.ts+++ b/packages/provider-kie/src/provider.ts@@ -1,13 +1,11 @@import type {- RecoverableEndpoint,- EndpointResumeContext,+ AsyncEndpoint,+ EndpointPollContext,EndpointStartContext,EndpointOutcome,} from "@narratage/endpoint-kit";import {canonicalize,- digestOf,- isDigest,} from "@narratage/protocol";import type {BlobRef,@@ -51,28 +49,22 @@ export type CreateKieProviderOptions = {readonly now?: () => number;};-type KieCheckpoint = {+type KieHandle = {readonly contract: "narratage.kie-operation@1";readonly taskId: string;readonly routeKey: string;- readonly model: string;- readonly requestDigest: Digest;- readonly contentRequestDigest: Digest;readonly startedAt: number;readonly polls: number;- readonly pollFailures: number;};class KieError extends Error {readonly code: string;- readonly retryable: boolean;readonly status: number | undefined;- constructor(code: string, message: string, options: { readonly retryable?: boolean; readonly status?: number } = {}) {+ constructor(code: string, message: string, options: { readonly status?: number } = {}) {super(message);this.name = "KieError";this.code = code;- this.retryable = options.retryable ?? false;this.status = options.status;}}@@ -102,11 +94,7 @@ function object(value: unknown, subject: string): Record<string, unknown> {return value as Record<string, unknown>;}-function contentRequestDigest(value: unknown): Digest {- return digestOf(object(value, "KIE request"));-}--function secret(context: EndpointStartContext | EndpointResumeContext): string {+function secret(context: EndpointStartContext | EndpointPollContext): string {const value = context.credentials.apiKey?.secret;if (value === undefined || value.length === 0) throw new KieError("KIE_MISSING_CREDENTIAL", "KIE API key is unavailable");return value;@@ -246,7 +234,7 @@ class KieClient {? undefined: (async function* () { yield bytes; })());if (source === undefined) {- throw new KieError("KIE_ARTIFACT_MISSING", `Artifact ${artifact.digest} is unavailable`, { retryable: false });+ throw new KieError("KIE_ARTIFACT_MISSING", `Artifact ${artifact.digest} is unavailable`);}const maximum = this.#options.maxArtifactBytes;const multipart = (async function* () {@@ -281,7 +269,6 @@ class KieClient {} catch (error) {if (error instanceof KieError) {throw new KieError("KIE_UPLOAD_FAILED", error.message, {- retryable: error.status === undefined || error.status === 429 || error.status >= 500,...(error.status === undefined ? {} : { status: error.status }),});}@@ -319,13 +306,13 @@ class KieClient {if (error instanceof KieError) {if (error.code === "KIE_SUBMISSION_REJECTED") throw error;if (error.status === 429) {- throw new KieError("KIE_RATE_LIMITED", "KIE createTask was rate limited", { retryable: true, status: 429 });+ throw new KieError("KIE_RATE_LIMITED", "KIE createTask was rate limited", { status: 429 });}if (error.status !== undefined && error.status >= 400 && error.status < 500) {throw new KieError("KIE_SUBMISSION_REJECTED", error.message, { status: error.status });}- // KIE does not document an idempotency key. Retrying an ambiguous createTask can double-spend.- throw new KieError("KIE_SUBMISSION_OUTCOME_UNKNOWN", "KIE createTask outcome is unknown; automatic resubmission is disabled");+ throw new KieError("KIE_SUBMISSION_FAILED", error.message,+ error.status === undefined ? {} : { status: error.status });}throw error;}@@ -422,27 +409,24 @@ function resultUrls(data: Record<string, unknown>): string[] {});}-function verifyCheckpoint(value: CanonicalValue | undefined, context: EndpointResumeContext): KieCheckpoint {+function readHandle(value: CanonicalValue | undefined, context: EndpointPollContext): KieHandle {if (value === undefined) {throw new KieError(- "KIE_SUBMISSION_CHECKPOINT_MISSING",- "KIE Operation has no task checkpoint; resubmission is disabled to avoid duplicate spend",+ "KIE_SUBMISSION_HANDLE_MISSING",+ "KIE Operation has no task handle",);}- const checkpoint = object(value, "KIE checkpoint") as unknown as KieCheckpoint;+ const handle = object(value, "KIE handle") as unknown as KieHandle;const route = kieRouteForCapability(context.need.capability);- if (checkpoint.contract !== "narratage.kie-operation@1"- || typeof checkpoint.taskId !== "string"+ if (handle.contract !== "narratage.kie-operation@1"+ || typeof handle.taskId !== "string"|| route === undefined- || checkpoint.routeKey !== route.key- || checkpoint.requestDigest !== context.need.requestDigest- || !isDigest(checkpoint.contentRequestDigest)- || !Number.isSafeInteger(checkpoint.startedAt)- || !Number.isSafeInteger(checkpoint.polls)- || !Number.isSafeInteger(checkpoint.pollFailures)) {- throw new KieError("KIE_CHECKPOINT_INVALID", "KIE checkpoint does not match the regenerated Need");+ || handle.routeKey !== route.key+ || !Number.isSafeInteger(handle.startedAt)+ || !Number.isSafeInteger(handle.polls)) {+ throw new KieError("KIE_HANDLE_INVALID", "KIE handle does not match the regenerated Need");}- return checkpoint;+ return handle;}function endpoint(options: {@@ -451,7 +435,7 @@ function endpoint(options: {readonly pollIntervalMs: number;readonly maxOperationMs: number;readonly now: () => number;-}): RecoverableEndpoint {+}): AsyncEndpoint {const failure = (error: unknown): EndpointOutcome => {const known = error instanceof KieError ? error : new KieError("KIE_INTERNAL_ERROR", error instanceof Error ? error.message : String(error));return {@@ -459,21 +443,9 @@ function endpoint(options: {failure: {code: known.code,message: known.message,- retryable: known.retryable,- ...(known.retryable ? { retryAt: options.now() + 2_000 } : {}),},};};- const pendingAfterError = (checkpoint: KieCheckpoint, error: unknown): EndpointOutcome => {- const failures = checkpoint.pollFailures + 1;- if (options.now() - checkpoint.startedAt >= options.maxOperationMs) {- return failure(new KieError("KIE_OPERATION_TIMEOUT", "KIE task did not become durably available before the deadline"));- }- const delayMs = Math.min(30_000, options.pollIntervalMs * (2 ** Math.min(failures, 5)));- return wakeAfter(canonicalize({ ...checkpoint, pollFailures: failures }), delayMs, options.now(), {- phase: "poll-retry",- });- };return {async start(context) {try {@@ -491,53 +463,40 @@ function endpoint(options: {const task = await route.compile(context.need.constraints, resolve);await options.gate.enter();const taskId = await options.client.createTask(task, key);- const checkpoint: KieCheckpoint = {+ const handle: KieHandle = {contract: "narratage.kie-operation@1",taskId,routeKey: route.key,- model: task.model,- requestDigest: context.need.requestDigest,- contentRequestDigest: contentRequestDigest(context.need.constraints),startedAt: options.now(),polls: 0,- pollFailures: 0,};- return wakeAfter(canonicalize(checkpoint), options.pollIntervalMs, options.now(), {+ return wakeAfter(canonicalize(handle), options.pollIntervalMs, options.now(), {phase: "submitted",});} catch (error) {return failure(error);}},- async resume(context) {- let checkpoint: KieCheckpoint;+ async poll(context) {+ let handle: KieHandle;try {- checkpoint = verifyCheckpoint(context.checkpoint, context);+ handle = readHandle(context.handle, context);} catch (error) {return failure(error);}try {const route = kieRouteForCapability(context.need.capability);- if (route === undefined || route.key !== checkpoint.routeKey) {- throw new KieError("KIE_CHECKPOINT_INVALID", "KIE checkpoint capability differs");+ if (route === undefined || route.key !== handle.routeKey) {+ throw new KieError("KIE_HANDLE_INVALID", "KIE handle capability differs");}- if (contentRequestDigest(context.need.constraints) !== checkpoint.contentRequestDigest) {- throw new KieError("KIE_CHECKPOINT_INVALID", "KIE checkpoint request content differs");- }- if (options.now() - checkpoint.startedAt >= options.maxOperationMs) {+ if (options.now() - handle.startedAt >= options.maxOperationMs) {throw new KieError("KIE_OPERATION_TIMEOUT", "KIE task exceeded its operation deadline");}const key = secret(context);- const data = await options.client.taskInfo(checkpoint.taskId, key);- if (typeof data.taskId === "string" && data.taskId !== checkpoint.taskId) {- throw new KieError("KIE_TASK_ID_MISMATCH", "KIE returned another task identity");- }- if (typeof data.model === "string" && data.model !== checkpoint.model) {- throw new KieError("KIE_TASK_MODEL_MISMATCH", "KIE task model differs from the submitted model");- }+ const data = await options.client.taskInfo(handle.taskId, key);const state = data.state;if (state === "waiting" || state === "queuing" || state === "generating") {- const next = { ...checkpoint, polls: checkpoint.polls + 1, pollFailures: 0 };+ const next = { ...handle, polls: handle.polls + 1 };return wakeAfter(canonicalize(next), options.pollIntervalMs, options.now(), {phase: state,});@@ -578,18 +537,7 @@ function endpoint(options: {result: { value: result },};} catch (error) {- if (error instanceof KieError && [- "KIE_CHECKPOINT_INVALID",- "KIE_OPERATION_TIMEOUT",- "KIE_TASK_ID_MISMATCH",- "KIE_TASK_MODEL_MISMATCH",- "KIE_TASK_FAILED",- "KIE_TASK_STATE_INVALID",- "KIE_RESULT_COUNT_EXCEEDED",- ].includes(error.code)) {- return failure(error);- }- return pendingAfterError(checkpoint, error);+ return failure(error);}},};@@ -639,9 +587,8 @@ export function createKieProvider(config: CreateKieProviderOptions) {...(laneConcurrency[route.capability.name] === undefined? {}: { maxConcurrency: laneConcurrency[route.capability.name] }),- lifecycle: "recoverable" as const,+ lifecycle: "asynchronous" as const,endpoint: providerEndpoint,- retry: { maxAttempts: 3 },})),});}diff --git a/packages/provider-kie/test/live-smoke.ts b/packages/provider-kie/test/live-smoke.tsindex 425f977a..f683b7b6 100644--- a/packages/provider-kie/test/live-smoke.ts+++ b/packages/provider-kie/test/live-smoke.ts@@ -6,8 +6,8 @@ import { FileArtifactStore, createFileArtifactStorePackage } from "@narratage/arimport { createEnvironmentCredentialStorePackage } from "@narratage/credential-store-env";import { registerTypeValidatorFacets } from "@narratage/component-kit";import {+ buildDefinition,createResolvedClosure,- digestOf,link,sealBuildRequest,sealCompiledGraph,@@ -77,6 +77,7 @@ import {seedreamManifest,} from "@narratage/seedream";import { admitRecord, TypeValidatorRegistry } from "@narratage/validation";+import { fixtureDigest } from "../../../test/fixture-digest.js";type SmokeCase = {readonly key: string;@@ -117,32 +118,16 @@ function apiBaseUrl(value: string): string {}async function accountCredits(baseUrl: string, key: string): Promise<number> {- let lastError: unknown;- for (let attempt = 1; attempt <= 4; attempt += 1) {- let response: Response;- try {- response = await fetch(`${baseUrl}/api/v1/chat/credit`, {- method: "GET",- headers: { authorization: `Bearer ${key}` },- });- } catch (error) {- lastError = error;- if (attempt < 4) await new Promise((done) => setTimeout(done, attempt * 1_000));- continue;- }- const text = await response.text();- if (response.ok) {- const body = object(JSON.parse(text), "KIE credit response");- assert(body.code === 200 && typeof body.data === "number" && Number.isFinite(body.data),- `KIE credit response is invalid: ${text.slice(0, 300)}`);- return body.data;- }- const retryable = response.status === 429 || response.status >= 500;- if (!retryable) throw new Error(`KIE credit check returned HTTP ${response.status}: ${text.slice(0, 300)}`);- lastError = new Error(`KIE credit check returned retryable HTTP ${response.status}`);- if (attempt < 4) await new Promise((done) => setTimeout(done, attempt * 1_000));- }- throw lastError instanceof Error ? lastError : new Error("KIE credit check failed");+ const response = await fetch(`${baseUrl}/api/v1/chat/credit`, {+ method: "GET",+ headers: { authorization: `Bearer ${key}` },+ });+ const text = await response.text();+ if (!response.ok) throw new Error(`KIE credit check returned HTTP ${response.status}: ${text.slice(0, 300)}`);+ const body = object(JSON.parse(text), "KIE credit response");+ assert(body.code === 200 && typeof body.data === "number" && Number.isFinite(body.data),+ `KIE credit response is invalid: ${text.slice(0, 300)}`);+ return body.data;}function resultExtension(mediaType: string): string {@@ -327,12 +312,10 @@ async function createBuild(item: SmokeCase): Promise<ReturnType<typeof start>> {id: item.ids.request,type: item.endpoint.requestType,value: { kind: "inline", value: item.request },- origin: { kind: "authored" },});const authored = await admitRecord(closure, draft, validators);const program = link(closure, [authored]);const graph = sealCompiledGraph({- program: program.semanticDigest,outputs: [{id: item.ids.output,type: item.endpoint.returns,@@ -356,7 +339,6 @@ async function createBuild(item: SmokeCase): Promise<ReturnType<typeof start>> {}],});return start(program, graph, sealBuildRequest({- graph: graph.id,targets: [{ output: item.ids.output }],}));}@@ -437,8 +419,6 @@ async function main(): Promise<void> {const failures: string[] = [];const workerAbort = new AbortController();const workerRun = runtime.work({- owner: `kie-smoke-${process.pid}`,- leaseMs: 30_000,idlePollMs: 100,signal: workerAbort.signal,});@@ -449,7 +429,7 @@ async function main(): Promise<void> {try {const state = await createBuild(item);const build = await runtime.build(- { id: `bld_${randomUUID()}`, state },+ { id: `bld_${randomUUID()}`, definition: buildDefinition(state) },{ follow: true, pollIntervalMs: 1_000, maxWaitMs: 20 * 60_000 },);if (build.status !== "complete") throw new Error(failureMessage(item, build));diff --git a/packages/provider-kie/test/models.test.ts b/packages/provider-kie/test/models.test.tsindex 7e55daea..9e3b6f2a 100644--- a/packages/provider-kie/test/models.test.ts+++ b/packages/provider-kie/test/models.test.ts@@ -1,7 +1,7 @@import assert from "node:assert/strict";import test from "node:test";import { videoContractManifests } from "../../../test/support/video-domain.js";-import { createResolvedClosure, digestOf } from "@narratage/core";+import { createResolvedClosure } from "@narratage/core";import { verifyGraphFragment } from "@narratage/elaborator";import {@@ -134,7 +134,6 @@ test("all model manifests close over the shared generation contract and every Frconst program: LinkedProgram = {closure,records: [],- semanticDigest: digestOf("kie-model-fragment-test"),};definitions.forEach((definition) => {Object.values(definition.endpoints).forEach((endpoint) => verifyGraphFragment(program, endpoint.fragment));@@ -180,7 +179,7 @@ test("Gemini Omni binds its documented output controls into the KIE request", as});});-test("Seedream safety policy is explicit author content and contributes to request identity", () => {+test("Seedream safety policy remains explicit author content", () => {const base = {prompt: ["A fashion editorial."],aspectRatio: ["3:4"],@@ -190,7 +189,7 @@ test("Seedream safety policy is explicit author content and contributes to requeconst unchecked = sealSeedreamRequest({ ...base, nsfwCheck: [false] });const checked = sealSeedreamRequest({ ...base, nsfwCheck: [true] });assert.deepEqual(unchecked.ports.nsfwCheck, [false]);- assert.notEqual(digestOf(unchecked), digestOf(checked));+ assert.deepEqual(checked.ports.nsfwCheck, [true]);});test("all twelve exact capabilities route to their documented KIE model slug", async () => {diff --git a/packages/provider-kie/test/provider.test.ts b/packages/provider-kie/test/provider.test.tsindex 0546afb6..8b889bf3 100644--- a/packages/provider-kie/test/provider.test.ts+++ b/packages/provider-kie/test/provider.test.ts@@ -1,19 +1,18 @@import assert from "node:assert/strict";import test from "node:test";+import { fixtureDigest } from "../../../test/fixture-digest.js";import {MemoryArtifactStore,EndpointRegistry,} from "@narratage/driver-node";import type { EndpointRegistration } from "@narratage/driver-node";-import type { RecoverableEndpoint } from "@narratage/endpoint-kit";+import type { AsyncEndpoint } from "@narratage/endpoint-kit";import { artifactTypes } from "@narratage/artifact";import { backgroundRemovalCapabilities, backgroundRemovalRequest } from "@narratage/background-removal";import { seedanceEndpoints, sealSeedanceRequest } from "@narratage/seedance";-import { digestOf } from "@narratage/protocol";import type { CanonicalValue, Need } from "@narratage/protocol";import { createKieProvider } from "@narratage/provider-kie";-import { sealOperationIdentity } from "@narratage/runtime";function need(constraints: CanonicalValue): Need {return {@@ -21,17 +20,14 @@ function need(constraints: CanonicalValue): Need {capability: seedanceEndpoints.mini!.capability,returns: seedanceEndpoints.mini!.returns,constraints,- requestedBy: "derivation:kie-test",result: "record:kie-test",- requestDigest: digestOf({ capability: "seedance-mini", constraints }),};}function removeBackgroundNeed(constraints: CanonicalValue): Need {return {id: "need:remove-background", capability: backgroundRemovalCapabilities.remove, returns: artifactTypes.blob,- constraints, requestedBy: "derivation:remove-background", result: "record:remove-background",- requestDigest: digestOf({ capability: backgroundRemovalCapabilities.remove, constraints }),+ constraints, result: "record:remove-background",};}@@ -39,7 +35,7 @@ async function endpointFor(request: Need,fetch: typeof globalThis.fetch,now = () => 1_000,-): Promise<{ endpoint: RecoverableEndpoint; registration: EndpointRegistration }> {+): Promise<{ endpoint: AsyncEndpoint; registration: EndpointRegistration }> {const registry = new EndpointRegistry();const provider = createKieProvider({fetch,@@ -52,22 +48,15 @@ async function endpointFor(await provider.install(registry);const resolution = registry.resolve(request);assert.equal(resolution.status, "resolved");- assert.equal(resolution.registration.kind, "recoverable");+ assert.equal(resolution.registration.kind, "asynchronous");return { endpoint: resolution.registration.endpoint, registration: resolution.registration };}-function operation(request: Need) {- return sealOperationIdentity({- build: "build:kie-test",- command: "command:kie-test",- endpoint: "kie.default",- pool: "kie.default",- lane: "fixture.video",- attempt: 1,- });+function operation(_request: Need): string {+ return "operation:kie-test";}-test("all KIE capabilities share one recoverable task engine and differ only by Lane", async () => {+test("all KIE capabilities share one asynchronous task engine and differ only by Lane", async () => {const provider = createKieProvider({fetch: async () => { throw new Error("no request expected"); },defaultConcurrency: 8,@@ -80,14 +69,14 @@ test("all KIE capabilities share one recoverable task engine and differ only byprompt: ["A clean studio shot."], resolution: ["720p"], aspectRatio: ["16:9"],duration: [5], generateAudio: [false], webSearch: [false],}) as unknown as CanonicalValue);- const source = { kind: "blob" as const, digest: digestOf("lane-source"), size: 1, mediaType: "image/png" };+ const source = { kind: "blob" as const, digest: fixtureDigest("lane-source"), size: 1, mediaType: "image/png" };const removal = removeBackgroundNeed(backgroundRemovalRequest(source) as unknown as CanonicalValue);const seedResolution = registry.resolve(seed);const removalResolution = registry.resolve(removal);assert.equal(seedResolution.status, "resolved");assert.equal(removalResolution.status, "resolved");- assert.equal(seedResolution.registration.kind, "recoverable");- assert.equal(removalResolution.registration.kind, "recoverable");+ assert.equal(seedResolution.registration.kind, "asynchronous");+ assert.equal(removalResolution.registration.kind, "asynchronous");assert.equal(seedResolution.registration.endpoint, removalResolution.registration.endpoint);assert.deepEqual(seedResolution.registration.scheduling, {queue: { pool: "kie.default", lane: "seedance-2-mini" },@@ -168,12 +157,13 @@ test("KIE uploads content-addressed references, resumes one task, and persists g};const started = await endpoint.start(common);assert.equal(started.status, "pending");+ if (started.status !== "pending") return;assert.deepEqual(started.status === "pending" && started.progress, { phase: "submitted" });assert.equal(calls.length, 2);- assert.equal(started.status === "pending" && (started.checkpoint as Record<string, unknown>).taskId, "task_seedance_test");- const completed = await endpoint.resume({+ assert.equal(started.status === "pending" && (started.handle as Record<string, unknown>).taskId, "task_seedance_test");+ const completed = await endpoint.poll({...common,- checkpoint: started.status === "pending" ? started.checkpoint : undefined,+ handle: started.handle,});assert.equal(completed.status, "completed");assert.equal(calls.length, 5);@@ -181,102 +171,12 @@ test("KIE uploads content-addressed references, resumes one task, and persists gassert.equal(completed.result.value.kind, "inline");const result = completed.result.value.kind === "inline"? completed.result.value.value as Record<string, unknown> : {};- assert.equal("model" in result, false, "model choice belongs to the request graph and Receipt");- assert.equal("requestDigest" in result, false, "request lineage belongs to the Need Receipt, not generated media");+ assert.equal("model" in result, false, "model choice belongs to the request graph");const videos = result.videos as Array<{ digest: string }>;assert.equal(videos.length, 1);assert.equal(await artifacts.has(videos[0]!.digest as `sha256:${string}`), true);});-test("KIE never retries an ambiguous paid submission", async () => {- const requestValue = sealSeedanceRequest("seedance-2-mini", {- prompt: ["A clean studio shot."],- resolution: ["720p"],- aspectRatio: ["16:9"],- duration: [5],- generateAudio: [false],- webSearch: [false],- });- const request = need(requestValue as unknown as CanonicalValue);- let calls = 0;- const { endpoint } = await endpointFor(request, async () => {- calls += 1;- throw new TypeError("socket closed after write");- });- const outcome = await endpoint.start({- command: { kind: "fulfill-need", id: "command:kie-test", need: request },- need: request,- artifacts: new MemoryArtifactStore(),- credentials: { apiKey: { secret: "test-key" } },- operation: operation(request),- });- assert.equal(calls, 1);- assert.equal(outcome.status, "failed");- if (outcome.status !== "failed") return;- assert.equal(outcome.failure.code, "KIE_SUBMISSION_OUTCOME_UNKNOWN");- assert.equal(outcome.failure.retryable, false);-});--test("KIE preserves an HTTP-200 business rejection without calling it an ambiguous submission", async () => {- const requestValue = sealSeedanceRequest("seedance-2-mini", {- prompt: ["A clean studio shot."],- resolution: ["720p"],- aspectRatio: ["16:9"],- duration: [5],- generateAudio: [false],- webSearch: [false],- });- const request = need(requestValue as unknown as CanonicalValue);- let calls = 0;- const { endpoint } = await endpointFor(request, async () => {- calls += 1;- return Response.json({ code: 422, msg: "aspect_ratio is required", data: null });- });- const outcome = await endpoint.start({- command: { kind: "fulfill-need", id: "command:kie-test", need: request },- need: request,- artifacts: new MemoryArtifactStore(),- credentials: { apiKey: { secret: "test-key" } },- operation: operation(request),- });- assert.equal(calls, 1);- assert.equal(outcome.status, "failed");- if (outcome.status !== "failed") return;- assert.equal(outcome.failure.code, "KIE_SUBMISSION_REJECTED");- assert.match(outcome.failure.message, /aspect_ratio is required.*422/u);- assert.equal(outcome.failure.retryable, false);-});--test("KIE refuses to resubmit when a recovered Operation has no task checkpoint", async () => {- const requestValue = sealSeedanceRequest("seedance-2-mini", {- prompt: ["A clean studio shot."],- resolution: ["720p"],- aspectRatio: ["16:9"],- duration: [5],- generateAudio: [false],- webSearch: [false],- });- const request = need(requestValue as unknown as CanonicalValue);- let calls = 0;- const { endpoint } = await endpointFor(request, async () => {- calls += 1;- throw new Error("must not fetch");- });- const outcome = await endpoint.resume({- command: { kind: "fulfill-need", id: "command:kie-test", need: request },- need: request,- artifacts: new MemoryArtifactStore(),- credentials: { apiKey: { secret: "test-key" } },- operation: operation(request),- checkpoint: undefined,- });- assert.equal(calls, 0);- assert.equal(outcome.status, "failed");- if (outcome.status !== "failed") return;- assert.equal(outcome.failure.code, "KIE_SUBMISSION_CHECKPOINT_MISSING");- assert.equal(outcome.failure.retryable, false);-});-test("KIE fulfills generic Background Removal with the documented Recraft wire contract", async () => {const artifacts = new MemoryArtifactStore();const source = await artifacts.put(new Uint8Array([1, 2, 3, 4]), "image/png");@@ -314,8 +214,9 @@ test("KIE fulfills generic Background Removal with the documented Recraft wire c};const started = await endpoint.start(common);assert.equal(started.status, "pending");- const completed = await endpoint.resume({- ...common, checkpoint: started.status === "pending" ? started.checkpoint : undefined,+ if (started.status !== "pending") return;+ const completed = await endpoint.poll({+ ...common, handle: started.handle,});assert.equal(completed.status, "completed");if (completed.status !== "completed") return;diff --git a/packages/provider-media-aws-lambda/src/provider.ts b/packages/provider-media-aws-lambda/src/provider.tsindex bd99e43f..dd537737 100644--- a/packages/provider-media-aws-lambda/src/provider.ts+++ b/packages/provider-media-aws-lambda/src/provider.ts@@ -1,7 +1,7 @@import { mediaTypes } from "@narratage/media";import { artifactTypes } from "@narratage/artifact";import { mediaPipelineCapabilities } from "@narratage/media-pipeline";-import { canonicalize, digestOf } from "@narratage/protocol";+import { canonicalize } from "@narratage/protocol";import type { BlobRef, CanonicalValue } from "@narratage/protocol";import { speechTypes } from "@narratage/speech";import { defineEndpointPackage } from "@narratage/endpoint-kit";diff --git a/packages/provider-media-aws-lambda/test/provider.test.ts b/packages/provider-media-aws-lambda/test/provider.test.tsindex e32d535e..048a71f8 100644--- a/packages/provider-media-aws-lambda/test/provider.test.ts+++ b/packages/provider-media-aws-lambda/test/provider.test.ts@@ -1,8 +1,9 @@import assert from "node:assert/strict";import test from "node:test";+import { fixtureDigest } from "../../../test/fixture-digest.js";import { MemoryArtifactStore } from "@narratage/driver-node";-import { canonicalize, digestOf } from "@narratage/protocol";+import { canonicalize } from "@narratage/protocol";import type { CanonicalValue } from "@narratage/protocol";import type { ImmediateEndpointHandler } from "@narratage/endpoint-kit";import type { JsonInvoker } from "@narratage/transport";@@ -27,7 +28,7 @@ function recordingInvoker(reply: (request: CanonicalValue) => CanonicalValue) {}const inspectNeed = {- source: { kind: "blob", digest: digestOf("source"), size: 4, mediaType: "video/mp4" },+ source: { kind: "blob", digest: fixtureDigest("source"), size: 4, mediaType: "video/mp4" },} as unknown as CanonicalValue;/** Installs the Provider into a registrar that keeps the handlers, as the Runtime does. */@@ -48,8 +49,8 @@ async function endpointFor(invoker: JsonInvoker, options: { bucket?: string; cap) => {handlers.set(capability.name, handler);},- registerRecoverableEndpoint: () => {- throw new Error("the AWS media Provider registers no recoverable endpoint");+ registerAsyncEndpoint: () => {+ throw new Error("the AWS media Provider registers no asynchronous endpoint");},} as never);const capability = options.capability ?? "inspect-media";@@ -105,7 +106,7 @@ test("the remote Provider receives the exact AudioProgramPlan compiled for localsampleFrames: 48_048,clips: [{id: "end-loop",- artifact: { kind: "blob", digest: digestOf("audio-source"), size: 400, mediaType: "audio/wav" },+ artifact: { kind: "blob", digest: fixtureDigest("audio-source"), size: 400, mediaType: "audio/wav" },targetStartSample: 0,targetEndSampleExclusive: 48_048,sourceSampleFrames: 20_000,@@ -141,7 +142,7 @@ test("the remote Provider receives the exact AudioProgramPlan compiled for localtest("a Provider aimed at another bucket fails naming the Artifact, not later with an unreadable Record", async () => {const store = new MemoryArtifactStore();- const stranger = digestOf("an artifact this Build's store never received");+ const stranger = fixtureDigest("an artifact this Build's store never received");const { invoker } = recordingInvoker(() => canonicalize({contract: "narratage.media-lambda-response@1",operation: "inspect",diff --git a/packages/provider-media-local/src/provider.ts b/packages/provider-media-local/src/provider.tsindex c4f34a94..38feba95 100644--- a/packages/provider-media-local/src/provider.ts+++ b/packages/provider-media-local/src/provider.ts@@ -12,7 +12,7 @@ import {} from "@narratage/media-execution";import type { MediaExecutionEnvironment, MediaOperationResult } from "@narratage/media-execution";import { mediaPipelineCapabilities } from "@narratage/media-pipeline";-import { canonicalize, digestOf } from "@narratage/protocol";+import { canonicalize } from "@narratage/protocol";import { isStreamingArtifactStore } from "@narratage/runtime";import { speechTypes } from "@narratage/speech";import { defineEndpointPackage } from "@narratage/endpoint-kit";diff --git a/packages/provider-media-local/test/provider.test.ts b/packages/provider-media-local/test/provider.test.tsindex 17da4165..017547c0 100644--- a/packages/provider-media-local/test/provider.test.ts+++ b/packages/provider-media-local/test/provider.test.ts@@ -21,8 +21,9 @@ import {sealMediaSelectionRequest,selectMediaStreams,} from "@narratage/media-pipeline";-import { canonicalize, digestOf } from "@narratage/protocol";+import { canonicalize } from "@narratage/protocol";import type { CapabilityRef, CanonicalValue, Need, TypeRef } from "@narratage/protocol";+import { fixtureDigest } from "../../../test/fixture-digest.js";import { createLocalMediaProvider } from "../src/index.js";import { localMediaToolchainProgram } from "../src/program.js";@@ -157,9 +158,7 @@ function need(capability,returns,constraints,- requestedBy: `derivation:${id}`,result: `record:${id}`,- requestDigest: digestOf({ capability, returns, constraints }),};}diff --git a/packages/provider-whisperx-local/src/provider.ts b/packages/provider-whisperx-local/src/provider.tsindex 9af21499..f2e8b19e 100644--- a/packages/provider-whisperx-local/src/provider.ts+++ b/packages/provider-whisperx-local/src/provider.ts@@ -4,7 +4,7 @@ import { join } from "node:path";import { sealAlignedTranscriptEvidence, speechEvidenceTypes } from "@narratage/speech-evidence";import type { AlignedTranscriptEvidence, SpeechTranscriptPassage } from "@narratage/speech-evidence";import type { EndpointInvocationContext, EndpointFulfillment } from "@narratage/endpoint-kit";-import { canonicalize, digestOf } from "@narratage/protocol";+import { canonicalize } from "@narratage/protocol";import type { CanonicalValue } from "@narratage/protocol";import { defineEndpointPackage } from "@narratage/endpoint-kit";import {diff --git a/packages/provider-whisperx-local/test/provider.test.ts b/packages/provider-whisperx-local/test/provider.test.tsindex e9932f42..de41f92a 100644--- a/packages/provider-whisperx-local/test/provider.test.ts+++ b/packages/provider-whisperx-local/test/provider.test.ts@@ -3,13 +3,13 @@ import test from "node:test";import { sealSpeechEvidenceAudio, speechTypes } from "@narratage/speech";import assert from "node:assert/strict";import { MemoryArtifactStore, EndpointRegistry } from "@narratage/driver-node";-import { digestOf } from "@narratage/protocol";import type { Need } from "@narratage/protocol";import { speechEvidenceTypes } from "@narratage/speech-evidence";import {whisperXCapabilities,whisperXRequestForEvidenceAudio,} from "@narratage/whisperx";+import { fixtureDigest } from "../../../test/fixture-digest.js";import {createLocalWhisperXProvider,@@ -121,13 +121,7 @@ test("local Provider stages canonical evidence bytes unchanged and returns sealecapability: whisperXCapabilities.alignment,returns: speechEvidenceTypes.alignedTranscript,constraints,- requestedBy: "derivation:whisperx-loopback",result: "record:whisperx-loopback",- requestDigest: digestOf({- capability: whisperXCapabilities.alignment,- returns: speechEvidenceTypes.alignedTranscript,- constraints,- }),};const registry = new EndpointRegistry();await createLocalWhisperXProvider({diff --git a/packages/provider-xiaomi-mimo/test/provider.test.ts b/packages/provider-xiaomi-mimo/test/provider.test.tsindex 4cc4989a..1873fbd7 100644--- a/packages/provider-xiaomi-mimo/test/provider.test.ts+++ b/packages/provider-xiaomi-mimo/test/provider.test.ts@@ -1,10 +1,10 @@import assert from "node:assert/strict";import test from "node:test";+import { fixtureDigest } from "../../../test/fixture-digest.js";import { EndpointRegistry, MemoryArtifactStore } from "@narratage/driver-node";import { generationTypes } from "@narratage/generation";import { mimoTtsEndpoints, sealMimoTtsRequest } from "@narratage/mimo-tts";-import { digestOf } from "@narratage/protocol";import type { CanonicalValue, Need } from "@narratage/protocol";import { createXiaomiMimoProvider } from "@narratage/provider-xiaomi-mimo";@@ -18,9 +18,7 @@ function need(capability: endpoint.capability,returns: endpoint.returns,constraints,- requestedBy: `derivation:${id}`,result: `record:${id}`,- requestDigest: digestOf({ capability: endpoint.capability, constraints }),};}diff --git a/packages/ranking/src/manifest.ts b/packages/ranking/src/manifest.tsindex b0f122eb..5b2be2a0 100644--- a/packages/ranking/src/manifest.ts+++ b/packages/ranking/src/manifest.ts@@ -3,7 +3,6 @@ import { compositionDependency, compositionTypes } from "@narratage/composition"import { mediaDependency, mediaTypes } from "@narratage/media";import { narrativeDependency, narrativeTypes } from "@narratage/narrative";import { programSpaceDependency, programSpaceTypes } from "@narratage/program-space";-import { digestOf } from "@narratage/protocol";import type { ModuleManifest, ProducerRef, TypeRef, ValueSchema } from "@narratage/protocol";import { semanticMapDependency, semanticMapTypes } from "@narratage/semantic-map";import { spatialDependency, spatialFrameSchema, spatialTypes } from "@narratage/spatial";diff --git a/packages/ranking/src/schedule.ts b/packages/ranking/src/schedule.tsindex fdd3f083..e69c1882 100644--- a/packages/ranking/src/schedule.ts+++ b/packages/ranking/src/schedule.ts@@ -12,7 +12,7 @@ import {programSpaceFrameCount,} from "@narratage/program-space";import type { ProgramSpace } from "@narratage/program-space";-import { canonicalize, digestOf, isDigest } from "@narratage/protocol";+import { canonicalize, isDigest } from "@narratage/protocol";import { verifyText } from "@narratage/text";import type { Text } from "@narratage/text";import type { CompleteSemanticMap } from "@narratage/semantic-map";diff --git a/packages/ranking/test/ranking.test.ts b/packages/ranking/test/ranking.test.tsindex 6afe22b0..06b2d9f8 100644--- a/packages/ranking/test/ranking.test.ts+++ b/packages/ranking/test/ranking.test.ts@@ -1,13 +1,13 @@import assert from "node:assert/strict";import test from "node:test";import { videoContractManifests } from "../../../test/support/video-domain.js";+import { fixtureDigest } from "../../../test/fixture-digest.js";import { createResolvedClosure } from "@narratage/core";import type { FontArtifactRef, SynchronizedMedia } from "@narratage/media";import { mediaTypes } from "@narratage/media";import type { NarrativeMomentRef, NarrativeSelectionRef } from "@narratage/narrative";import { sealProgramSpace } from "@narratage/program-space";-import { digestOf } from "@narratage/protocol";import type { CompleteSemanticMap } from "@narratage/semantic-map";import { sealSpatialFrame } from "@narratage/spatial";import type { SvsRecipe } from "@narratage/svs";@@ -114,14 +114,14 @@ const triggers = (count: number): NarrativeMomentRef => ({.map((anchorId, occurrence) => ({ occurrence, anchorId })),});const font: FontArtifactRef = {- sources: [{ artifact: { kind: "blob", digest: digestOf("ranking-font"), size: 32, mediaType: "font/woff2" } }],+ sources: [{ artifact: { kind: "blob", digest: fixtureDigest("ranking-font"), size: 32, mediaType: "font/woff2" } }],weight: 700,style: "normal",};const recipe = (path: string, properties: SvsRecipe["properties"] = {}): SvsRecipe => ({path, properties,});-const image = (id: string) => ({ kind: "blob" as const, digest: digestOf(`ranking-image:${id}`), size: 64, mediaType: "image/png" });+const image = (id: string) => ({ kind: "blob" as const, digest: fixtureDigest(`ranking-image:${id}`), size: 64, mediaType: "image/png" });const header = (variant: RankingHeader["variant"], id: string = variant) => sealRankingHeader({id, variant,});@@ -286,7 +286,7 @@ test("Typewriter uses Unicode graphemes, explicit emphasis and winner timing witconst sound = (id: string): SynchronizedMedia => ({timeline: { frameRate: { numerator: 30, denominator: 1 }, frameCount: 3 },audio: {- artifact: { kind: "blob", digest: digestOf(`ranking-sound:${id}`), size: 128, mediaType: "audio/wav" },+ artifact: { kind: "blob", digest: fixtureDigest(`ranking-sound:${id}`), size: 128, mediaType: "audio/wav" },},});diff --git a/packages/raster/src/manifest.ts b/packages/raster/src/manifest.tsindex f40d2271..d8289bb3 100644--- a/packages/raster/src/manifest.ts+++ b/packages/raster/src/manifest.ts@@ -1,5 +1,4 @@import { artifactDependency, artifactTypes } from "@narratage/artifact";-import { digestOf } from "@narratage/protocol";import type { CapabilityRef, ModuleManifest } from "@narratage/protocol";export const rasterModuleRef = { name: "@narratage/raster", version: "1" } as const;diff --git a/packages/raster/test/raster.test.ts b/packages/raster/test/raster.test.tsindex 86b8efd0..e74f2e10 100644--- a/packages/raster/test/raster.test.ts+++ b/packages/raster/test/raster.test.ts@@ -1,7 +1,7 @@import assert from "node:assert/strict";import test from "node:test";+import { fixtureDigest } from "../../../test/fixture-digest.js";-import { digestOf } from "@narratage/protocol";import {assertRasterRequest,rasterCapabilities,@@ -11,7 +11,7 @@ import {rasterTransformRequest,} from "@narratage/raster";-const image = { kind: "blob" as const, digest: digestOf("raster-image"), size: 12, mediaType: "image/png" };+const image = { kind: "blob" as const, digest: fixtureDigest("raster-image"), size: 12, mediaType: "image/png" };test("one exact Raster capability carries both closed deterministic request variants", () => {const transform = rasterTransformRequest(image, [{ kind: "encode", format: "webp", quality: 90 }]);diff --git a/packages/render-hyperframes/src/manifest.ts b/packages/render-hyperframes/src/manifest.tsindex fea3e6e3..23aaf7c3 100644--- a/packages/render-hyperframes/src/manifest.ts+++ b/packages/render-hyperframes/src/manifest.ts@@ -6,7 +6,6 @@ import {hyperframesModuleRef,hyperframesTypes,} from "@narratage/hyperframes";-import { digestOf } from "@narratage/protocol";import type {CapabilityRef,ModuleManifest,diff --git a/packages/render-hyperframes/src/product.ts b/packages/render-hyperframes/src/product.tsindex 6eb70d86..21452870 100644--- a/packages/render-hyperframes/src/product.ts+++ b/packages/render-hyperframes/src/product.ts@@ -1,4 +1,3 @@-import { digestOf } from "@narratage/protocol";import type { CanonicalValue } from "@narratage/protocol";import { assertHyperframesDocument } from "@narratage/hyperframes";import type { HyperframesDocument } from "@narratage/hyperframes";diff --git a/packages/render-hyperframes/test/render.test.ts b/packages/render-hyperframes/test/render.test.tsindex b1a625b0..366c1123 100644--- a/packages/render-hyperframes/test/render.test.ts+++ b/packages/render-hyperframes/test/render.test.ts@@ -10,10 +10,10 @@ import { compositionDependency, compositionTypes, sealComposition } from "@narraimport type { Composition } from "@narratage/composition";import assert from "node:assert/strict";import test from "node:test";+import { fixtureDigest } from "../../../test/fixture-digest.js";import {createResolvedClosure,- digestOf,link,sealBuildRequest,sealCompiledGraph,@@ -100,20 +100,15 @@ const closure = createResolvedClosure([mediaPipelineManifest,renderHyperframesManifest,]);-const origin = {- kind: "authored" as const,-};const compositionRecord = await admitRecord(closure, sealRecord({id: "composition",type: compositionTypes.composition,value: stored(composition),- origin,}), validatorRegistry());const spaceRecord = await admitRecord(closure, sealRecord({id: "space",type: programSpaceTypes.programSpace,value: stored(space),- origin,}), validatorRegistry());const linked = link(closure, [compositionRecord, spaceRecord]);const instance = elaborateGraphFragment(linked, renderHyperframesFragment, {@@ -125,11 +120,10 @@ const instance = elaborateGraphFragment(linked, renderHyperframesFragment, {},});const contribution = bindAuthorFragment(instance, { video: "final.video" });-const graph = sealCompiledGraph({ program: linked.semanticDigest, ...contribution });+const graph = sealCompiledGraph({ ...contribution });function build() {return start(linked, graph, sealBuildRequest({- graph: graph.id,targets: [{ output: "final.video" }],}));}@@ -175,19 +169,19 @@ test("HyperFrames rendering is an explicit exact Need after ordinary document cotest("separate visual, audio and mux Endpoints complete one author-visible render", async () => {const visualArtifact = {kind: "blob" as const,- digest: digestOf("render-hyperframes:visual"),+ digest: fixtureDigest("render-hyperframes:visual"),size: 12_345,mediaType: "video/mp4",};const audioArtifact = {kind: "blob" as const,- digest: digestOf("render-hyperframes:audio"),+ digest: fixtureDigest("render-hyperframes:audio"),size: 4_096,mediaType: "audio/wav",};const finalArtifact = {kind: "blob" as const,- digest: digestOf("render-hyperframes:final-video"),+ digest: fixtureDigest("render-hyperframes:final-video"),size: 16_441,mediaType: "video/mp4",};@@ -270,7 +264,7 @@ test("a render Product with another frame domain is rejected by the explicit dowcanvas: document.canvas,artifact: {kind: "blob",- digest: digestOf("render-hyperframes:wrong-domain"),+ digest: fixtureDigest("render-hyperframes:wrong-domain"),size: 1,mediaType: "video/mp4",},@@ -287,7 +281,7 @@ test("a render Product with another frame domain is rejected by the explicit dowvalue: stored(sealTimelineAudio({artifact: {kind: "blob",- digest: digestOf("render-hyperframes:domain-check-audio"),+ digest: fixtureDigest("render-hyperframes:domain-check-audio"),size: 1,mediaType: "audio/wav",},@@ -303,11 +297,10 @@ test("a render Product with another frame domain is rejected by the explicit dow}).run(build());assert.equal(result.status, "paused");assert.match(result.outcomes.at(-1)?.message ?? "", /different presentation durations/u);- assert.equal(result.state.receipts.length, 2);});const fixtureModule = { name: "example.composition-fixture", version: "1" } as const;-const fixtureSurfaceDigest = digestOf("example.composition-fixture/surface@1");+const fixtureSurfaceDigest = fixtureDigest("example.composition-fixture/surface@1");const fixtureSurface = {name: "composition", tag: "Composition", mode: "structured",outputs: [compositionTypes.composition, programSpaceTypes.programSpace],@@ -385,7 +378,6 @@ test("the final rendered video is an ordinary BlobArtifact that can feed anotherconst target = resolveCompiledSourceExport(compiled, "poster.image", artifactTypes.blob);assert.equal(target.ref.kind, "logical-output");const state = start(compiled.program, compiled.graph, sealBuildRequest({- graph: compiled.graph.id,targets: [{ output: target.ref.kind === "logical-output" ? target.ref.id : "" }],}));assert.deepEqual(state.plan.steps.map((step) => step.producer.name).sort(), [diff --git a/packages/run-markup/src/frontend.ts b/packages/run-markup/src/frontend.tsindex 4a2becca..74e3ca21 100644--- a/packages/run-markup/src/frontend.ts+++ b/packages/run-markup/src/frontend.ts@@ -1,4 +1,3 @@-import { digestOf } from "@narratage/protocol";import type { RunFrontend } from "@narratage/run";import { parseRunDocument } from "./syntax.js";diff --git a/packages/run-markup/test/run.test.ts b/packages/run-markup/test/run.test.tsindex 783c6edb..2874c23d 100644--- a/packages/run-markup/test/run.test.ts+++ b/packages/run-markup/test/run.test.ts@@ -1,9 +1,9 @@import assert from "node:assert/strict";import test from "node:test";+import { fixtureDigest } from "../../../test/fixture-digest.js";import {createResolvedClosure,- digestOf,link,reduce,sealBuildRequest,@@ -21,8 +21,6 @@ import {resolveRunDocument,RunFragmentRegistry,RunFrontendRegistry,- RunSourceError,- verifyRunSourceClosure,} from "@narratage/run";import { parseRunDocument, runMarkupFrontend } from "@narratage/run-markup";@@ -56,11 +54,9 @@ function fixture(): CompiledSourceClosure {id: "prompt:root",type: promptType,value: { kind: "inline", value: "A deliberate preview." },- origin: { kind: "authored" },});const program = link(closure, [prompt]);const graph = sealCompiledGraph({- program: program.semanticDigest,outputs: ["left", "right"].map((id) => ({ id, type: mediaType, primary: "default:candidate" })),candidates: [{id: "default:candidate",@@ -105,7 +101,6 @@ function previewFragment() {function realize(compilation: CompiledSourceClosure, run: Awaited<ReturnType<typeof resolveRunDocument>>): CompiledGraph {const selected = new Map(run.graph.satisfactions.map((item) => [item.output, item.candidate]));return sealCompiledGraph({- program: compilation.program.semanticDigest,outputs: compilation.graph.outputs.map((item) => ({...item,primary: selected.get(item.id) ?? item.primary,@@ -163,14 +158,13 @@ test("one multi-export Fragment declaration remains one execution", async () =>fragments.register({ name: "@example/run-preview", fragments: { shared: previewFragment() } });const run = await resolveRunDocument(compiled.document, {compilation,- sourceClosure: compiled.closure,fragments,readStoredValue() { throw new Error("not used"); },readFile() { throw new Error("not used"); },- readBuild() { throw new Error("not used"); },+ resolveBuildRecord() { throw new Error("not used"); },});const graph = realize(compilation, run);- const state = start(compilation.program, graph, sealBuildRequest({ graph: graph.id, targets: run.graph.targets }));+ const state = start(compilation.program, graph, sealBuildRequest({ targets: run.graph.targets }));assert.equal(state.plan.steps.filter((item) => item.producer.name === "preview").length, 1);});@@ -184,16 +178,15 @@ test("a Provided Value is an ordinary zero-input Candidate", async () => {const compilation = fixture();const run = await resolveRunDocument(compiled.document, {compilation,- sourceClosure: compiled.closure,fragments: new RunFragmentRegistry(),readStoredValue() { return { kind: "inline", value: "already rendered" }; },readFile() { throw new Error("not used"); },- readBuild() { throw new Error("not used"); },+ resolveBuildRecord() { throw new Error("not used"); },});const graph = realize(compilation, run);- const state = start(compilation.program, graph, sealBuildRequest({ graph: graph.id, targets: run.graph.targets }));+ const state = start(compilation.program, graph, sealBuildRequest({ targets: run.graph.targets }));assert.equal(state.plan.steps.length, 0);- assert.equal(state.records.filter((record) => record.origin.kind === "provided").length, 1);+ assert.equal(state.records.some((record) => record.id === "provided:fixed"), true);});test("a source file is an ordinary BlobArtifact Candidate", async () => {@@ -206,15 +199,14 @@ test("a source file is an ordinary BlobArtifact Candidate", async () => {const compilation = fixture();const run = await resolveRunDocument(compiled.document, {compilation,- sourceClosure: compiled.closure,fragments: new RunFragmentRegistry(),readStoredValue() { throw new Error("not used"); },readFile(from, mediaType) {assert.equal(from, "./approved.mp4");assert.equal(mediaType, "video/mp4");- return { kind: "blob", digest: digestOf("approved video"), size: 14, mediaType };+ return { kind: "blob", digest: fixtureDigest("approved video"), size: 14, mediaType };},- readBuild() { throw new Error("not used"); },+ resolveBuildRecord() { throw new Error("not used"); },});assert.equal(run.graph.candidates.length, 1);assert.equal(run.graph.candidates[0]?.type.module.name, "@narratage/artifact");@@ -224,7 +216,6 @@ test("a source file is an ordinary BlobArtifact Candidate", async () => {test("a Build Record resolves a Host Catalog alias without entering Core", async () => {const compilation = fixture();let historical = start(compilation.program, compilation.graph, sealBuildRequest({- graph: compilation.graph.id,targets: [{ output: "left" }],}));historical = reduce(historical);@@ -236,7 +227,7 @@ test("a Build Record resolves a Host Catalog alias without entering Core", asyncoutputs: { media: { kind: "inline" as const, value: "archived media" } },needs: {},};- historical = reduce(historical, { ...event, id: `event:${digestOf(event)}` });+ historical = reduce(historical, event);const compiled = await compileDocument(`<svrun version="1"><author source="./main.svml"/><target output="right"/>@@ -245,15 +236,20 @@ test("a Build Record resolves a Host Catalog alias without entering Core", async</svrun>`);const run = await resolveRunDocument(compiled.document, {compilation,- sourceClosure: compiled.closure,fragments: new RunFragmentRegistry(),readStoredValue() { throw new Error("not used"); },readFile() { throw new Error("not used"); },- readBuild() { return historical; },- resolveBuildOutput(_build, output) { assert.equal(output, "friendly-shot"); return "left"; },+ resolveBuildRecord(build, output) {+ assert.equal(build, "prior-build");+ assert.equal(output, "friendly-shot");+ const selection = historical.plan.selections.find((item) => item.output === "left");+ const record = historical.records.find((item) => item.id === selection?.record);+ assert.ok(record);+ return { type: record.type, value: record.value };+ },});const graph = realize(compilation, run);- const state = start(compilation.program, graph, sealBuildRequest({ graph: graph.id, targets: run.graph.targets }));+ const state = start(compilation.program, graph, sealBuildRequest({ targets: run.graph.targets }));assert.equal(state.plan.steps.length, 0);});@@ -281,23 +277,11 @@ test("a Target-only source still compiles one mandatory Run Graph", async () =>const compilation = fixture();const run = await resolveRunDocument(compiled.document, {compilation,- sourceClosure: compiled.closure,fragments: new RunFragmentRegistry(),readStoredValue() { throw new Error("not used"); },readFile() { throw new Error("not used"); },- readBuild() { throw new Error("not used"); },+ resolveBuildRecord() { throw new Error("not used"); },});assert.equal(run.graph.operations.length, 0);assert.equal(run.graph.targets[0]?.output, "left");});--test("Run Source Closure separates source bytes from semantic meaning", async () => {- const first = await compileDocument(`<svrun version="1"><author source="./main.svml"/><target output="left"/></svrun>`);- const second = await compileDocument(`<svrun version="1"><author source="./main.svml"/>\n\n<target output="left"/></svrun>`);- assert.notEqual(first.closure.id, second.closure.id);- assert.equal(first.closure.semanticDigest, second.closure.semanticDigest);- assert.throws(() => verifyRunSourceClosure({- ...first.closure,- sourceDigest: digestOf("tampered"),- }), (error: unknown) => error instanceof RunSourceError && error.code === "RUN_SOURCE_CLOSURE_DIGEST_MISMATCH");-});diff --git a/packages/run/package.json b/packages/run/package.jsonindex ea0ba2b3..2d59bc06 100644--- a/packages/run/package.json+++ b/packages/run/package.json@@ -8,7 +8,6 @@".": "./src/index.ts"},"dependencies": {- "@narratage/core": "workspace:*","@narratage/elaborator": "workspace:*","@narratage/host": "workspace:*","@narratage/protocol": "workspace:*",diff --git a/packages/run/src/candidate.ts b/packages/run/src/candidate.tsindex f4f71cf3..f1a50e75 100644--- a/packages/run/src/candidate.ts+++ b/packages/run/src/candidate.ts@@ -1,80 +1,33 @@import {canonicalize,- digestOf,-} from "@narratage/core";+} from "@narratage/protocol";import type {- BuildState,Candidate,StoredValue,TypeRef,} from "@narratage/protocol";export type ProvidedCandidateInput = {+ readonly id: string;readonly type: TypeRef;readonly value: StoredValue;readonly record?: string;};-export type BuildRecordCandidateInput = {- readonly build: BuildState;- readonly sourceOutput: string;-};--export class CandidateError extends Error {- readonly code: string;- readonly subject: string | undefined;-- constructor(code: string, message: string, subject?: string) {- super(message);- this.name = "CandidateError";- this.code = code;- this.subject = subject;- }-}--function assert(condition: unknown, code: string, message: string, subject?: string): asserts condition {- if (!condition) throw new CandidateError(code, message, subject);-}-export function createProvidedCandidate(input: ProvidedCandidateInput): Candidate {+ if (input.id.trim().length === 0) throw new Error("Provided Candidate id is empty");const value = input.value.kind === "inline"? { kind: "inline" as const, value: canonicalize(input.value.value) }: { ...input.value };- const identity = {- kind: "provided-candidate@1",- type: input.type,- value,- };- const suffix = digestOf(identity).slice("sha256:".length);return {- id: `candidate:${suffix}`,+ id: `candidate:${input.id}`,type: input.type,root: {kind: "value",value: {- id: input.record ?? `provided:${suffix}`,+ id: input.record ?? `provided:${input.id}`,value,},},};}--/**- * Extract one verified historical output as an ordinary zero-input Candidate.- * No historical execution state crosses this boundary.- */-export function createBuildRecordCandidate(input: BuildRecordCandidateInput): Candidate {- const selection = input.build.plan.selections.find((item) => item.output === input.sourceOutput);- assert(- selection !== undefined,- "HISTORICAL_OUTPUT_UNSELECTED",- "historical Build did not select this Logical Output",- input.sourceOutput,- );- const record = input.build.records.find((item) => item.id === selection.record);- assert(record !== undefined, "HISTORICAL_RECORD_MISSING", "historical Output Record is absent", selection.record);- return createProvidedCandidate({- type: record.type,- value: record.value,- });-}diff --git a/packages/run/src/facet.ts b/packages/run/src/facet.tsindex 671cd464..5500f99f 100644--- a/packages/run/src/facet.ts+++ b/packages/run/src/facet.ts@@ -1,6 +1,5 @@import type { GraphFragment } from "@narratage/elaborator";import type { HostFacet } from "@narratage/host";-import { canonicalStringify, isDigest } from "@narratage/protocol";import type {RunFragmentPackage,@@ -8,52 +7,31 @@ import type {export const runFragmentHostAbi = "narratage.run-fragment-host@1";-export type RunFragmentHostFacetIdentity = {- readonly exports: readonly {- readonly name: string;- readonly fragment: string;- }[];-};-export type RunFragmentHostFacet = HostFacet & {readonly abi: typeof runFragmentHostAbi;readonly offers: readonly string[];- readonly identity: RunFragmentHostFacetIdentity;readonly implementation: RunFragmentPackage;};-function normalizedExports(fragments: Readonly<Record<string, GraphFragment>>): RunFragmentHostFacetIdentity["exports"] {- return Object.entries(fragments)- .map(([name, fragment]) => {- if (name.trim().length === 0 || !isDigest(fragment.id)) {- throw new Error("Run Fragment Host facet has an invalid export identity");- }- return { name, fragment: fragment.id };- })- .sort((left, right) => left.name.localeCompare(right.name));+function validateFragments(fragments: Readonly<Record<string, GraphFragment>>): void {+ for (const [name, fragment] of Object.entries(fragments)) {+ if (name.trim().length === 0 || fragment.id.trim().length === 0) {+ throw new Error("Run Fragment Host facet has an invalid export");+ }+ }}/** Package helper: expose inert Run Fragments through the exact Run Host ABI. */export function createRunFragmentHostFacet(item: RunFragmentPackage): RunFragmentHostFacet {if (item.name.trim().length === 0) throw new Error("Run Fragment Host facet package name is empty");+ validateFragments(item.fragments);return {abi: runFragmentHostAbi,offers: [item.name],- identity: {- exports: normalizedExports(item.fragments),- },implementation: item,};}-function sameIdentity(facet: RunFragmentHostFacet): boolean {- const expected: RunFragmentHostFacetIdentity = {- exports: normalizedExports(facet.implementation.fragments),- };- return canonicalStringify(facet.identity) === canonicalStringify(expected)- && canonicalStringify(facet.offers) === canonicalStringify([facet.implementation.name]);-}-/*** Install only facets addressed to this Host ABI. Package loading itself never interprets or* activates the executable Fragment objects.@@ -64,22 +42,18 @@ export function installRunFragmentHostFacets(): void {for (const opaque of facets) {if (opaque.abi !== runFragmentHostAbi) continue;- if (opaque.identity === null || typeof opaque.identity !== "object" || Array.isArray(opaque.identity)- || opaque.implementation === null || typeof opaque.implementation !== "object"+ if (opaque.implementation === null || typeof opaque.implementation !== "object"|| Array.isArray(opaque.implementation)) {- throw new Error("Run Fragment Host facet has an invalid identity or implementation");+ throw new Error("Run Fragment Host facet has an invalid implementation");}const facet = opaque as RunFragmentHostFacet;- if (!Array.isArray(facet.identity.exports)- || typeof facet.implementation.name !== "string"+ if (typeof facet.implementation.name !== "string"|| facet.implementation.fragments === null|| typeof facet.implementation.fragments !== "object"|| Array.isArray(facet.implementation.fragments)) {throw new Error("Run Fragment Host facet has an invalid package implementation");}- if (!sameIdentity(facet)) {- throw new Error(`Run Fragment Host facet ${facet.implementation.name} differs from its locked identity`);- }+ validateFragments(facet.implementation.fragments);registry.register(facet.implementation);}}diff --git a/packages/run/src/frontend.ts b/packages/run/src/frontend.tsindex 9bedad04..633b8231 100644--- a/packages/run/src/frontend.ts+++ b/packages/run/src/frontend.ts@@ -1,11 +1,6 @@import {- digestOf,-} from "@narratage/protocol";-import {- compiledSourceIdentity,maskSourceHeader,parseSourceHeader,- verifyCompiledSourceIdentity,} from "@narratage/source";import type {@@ -13,7 +8,6 @@ import type {RunFrontend,RunFrontendRegistryLike,RunFrontendSourceUnit,- RunSourceClosure,RunSourceUnit,} from "./types.js";@@ -56,34 +50,13 @@ export function prepareRunSource(source: RunSourceUnit): RunFrontendSourceUnit {};}-function closureContent(closure: RunSourceClosure): Omit<RunSourceClosure, "id"> {- return {- format: "narratage.run-source-closure@1",- ...compiledSourceIdentity(closure),- };-}-export async function compileRunSource(source: RunSourceUnit,frontends: RunFrontendRegistryLike,-): Promise<{ readonly document: RunDocument; readonly closure: RunSourceClosure }> {+): Promise<{ readonly document: RunDocument }> {const prepared = prepareRunSource(source);const frontend = frontends.resolve(prepared.header.using);assert(frontend !== undefined, "UNKNOWN_RUN_FRONTEND", `Run Frontend ${prepared.header.using} is not registered`, prepared.header.using);const decoded = await frontend.decode(prepared);- const closureWithoutId = {- format: "narratage.run-source-closure@1" as const,- frontend: frontend.id,- sourceDigest: digestOf(source.text),- semanticDigest: digestOf(decoded.document),- };- const closure: RunSourceClosure = { ...closureWithoutId, id: digestOf(closureWithoutId) };- verifyRunSourceClosure(closure);- return { document: decoded.document, closure };-}--export function verifyRunSourceClosure(closure: RunSourceClosure): void {- assert(closure.format === "narratage.run-source-closure@1", "UNSUPPORTED_RUN_SOURCE_CLOSURE", "unsupported Run Source Closure");- verifyCompiledSourceIdentity(closure);- assert(closure.id === digestOf(closureContent(closure)), "RUN_SOURCE_CLOSURE_DIGEST_MISMATCH", "Run Source Closure digest differs");+ return { document: decoded.document };}diff --git a/packages/run/src/graph.ts b/packages/run/src/graph.tsindex ccbfa0b1..c743c3e3 100644--- a/packages/run/src/graph.ts+++ b/packages/run/src/graph.ts@@ -1,8 +1,3 @@-import {- digestOf,- isDigest,-} from "@narratage/protocol";-import type { RunGraph } from "./types.js";export class RunGraphError extends Error {@@ -19,7 +14,7 @@ function assert(condition: unknown, code: string, message: string): asserts condif (!condition) throw new RunGraphError(code, message);}-function content(graph: Omit<RunGraph, "id">): Omit<RunGraph, "id"> {+function content(graph: RunGraph): RunGraph {return {format: "narratage.run-graph@1",candidates: [...graph.candidates].sort((left, right) => left.id.localeCompare(right.id)),@@ -29,16 +24,14 @@ function content(graph: Omit<RunGraph, "id">): Omit<RunGraph, "id"> {};}-export function sealRunGraph(input: Omit<RunGraph, "format" | "id">): RunGraph {- const normalized = content({ format: "narratage.run-graph@1", ...input });- const graph: RunGraph = { ...normalized, id: digestOf(normalized) };+export function sealRunGraph(input: Omit<RunGraph, "format">): RunGraph {+ const graph = content({ format: "narratage.run-graph@1", ...input });verifyRunGraph(graph);return graph;}export function verifyRunGraph(graph: RunGraph): void {assert(graph.format === "narratage.run-graph@1", "UNSUPPORTED_RUN_GRAPH", "unsupported Run Graph");- assert(isDigest(graph.id), "INVALID_RUN_GRAPH_DIGEST", "Run Graph digest is invalid");const candidateIds = new Set<string>();for (const candidate of graph.candidates) {assert(!candidateIds.has(candidate.id), "DUPLICATE_RUN_CANDIDATE", `Run Graph repeats Candidate ${candidate.id}`);@@ -61,5 +54,4 @@ export function verifyRunGraph(graph: RunGraph): void {assert(!targets.has(target.output), "DUPLICATE_RUN_TARGET", `Run Graph repeats Target ${target.output}`);targets.add(target.output);}- assert(graph.id === digestOf(content(graph)), "RUN_GRAPH_DIGEST_MISMATCH", "Run Graph digest differs");}diff --git a/packages/run/src/index.ts b/packages/run/src/index.tsindex 8136825d..8cc6c09a 100644--- a/packages/run/src/index.ts+++ b/packages/run/src/index.ts@@ -1,11 +1,8 @@export { RunFragmentRegistry } from "./registry.js";export {- createBuildRecordCandidate,createProvidedCandidate,- CandidateError,} from "./candidate.js";export type {- BuildRecordCandidateInput,ProvidedCandidateInput,} from "./candidate.js";export {@@ -15,7 +12,6 @@ export {} from "./facet.js";export type {RunFragmentHostFacet,- RunFragmentHostFacetIdentity,} from "./facet.js";export { collectRunModuleRequests, resolveRunDocument } from "./resolve.js";export {@@ -23,7 +19,6 @@ export {prepareRunSource,RunFrontendRegistry,RunSourceError,- verifyRunSourceClosure,} from "./frontend.js";export {createRunFrontendHostFacet,diff --git a/packages/run/src/resolve.ts b/packages/run/src/resolve.tsindex be746679..99add1e5 100644--- a/packages/run/src/resolve.ts+++ b/packages/run/src/resolve.ts@@ -12,7 +12,6 @@ import type {StoredValue,} from "@narratage/protocol";import {- createBuildRecordCandidate,createProvidedCandidate,} from "./candidate.js";@@ -93,14 +92,14 @@ export async function resolveRunDocument(const addCandidate = (candidate: Candidate): void => {const existing = candidates.get(candidate.id);if (existing !== undefined && canonicalStringify(existing) !== canonicalStringify(candidate)) {- throw new Error(`Run Candidate identity ${candidate.id} has conflicting definitions`);+ throw new Error(`Run Candidate ${candidate.id} has conflicting definitions`);}candidates.set(candidate.id, candidate);};const addOperation = (operation: OperationNode): void => {const existing = operations.get(operation.id);if (existing !== undefined && canonicalStringify(existing) !== canonicalStringify(operation)) {- throw new Error(`Run Operation identity ${operation.id} has conflicting definitions`);+ throw new Error(`Run Operation ${operation.id} has conflicting definitions`);}operations.set(operation.id, operation);};@@ -114,7 +113,7 @@ export async function resolveRunDocument(if (declaration.kind === "provided") {const value = await context.readStoredValue(declaration.from);assertStoredValue(value, declaration.from);- const candidate = createProvidedCandidate({ type: declaration.type, value });+ const candidate = createProvidedCandidate({ id: declaration.id, type: declaration.type, value });addCandidate(candidate);bindCandidateName(declaration.id, candidate.id);continue;@@ -123,6 +122,7 @@ export async function resolveRunDocument(const value = await context.readFile(declaration.from, declaration.mediaType);assertStoredValue(value, declaration.from);const candidate = createProvidedCandidate({+ id: declaration.id,type: declaration.type,value,});@@ -131,18 +131,13 @@ export async function resolveRunDocument(continue;}if (declaration.kind === "build-record") {- const build = await context.readBuild(declaration.build);- if (build === undefined) {+ const record = await context.resolveBuildRecord(declaration.build, declaration.output);+ if (record === undefined) {throw new Error(- `Build ${declaration.build} does not exist; create that Build first or update build-record ${declaration.id}`,+ `Build ${declaration.build} has no accepted output ${declaration.output}; update build-record ${declaration.id}`,);}- const sourceOutput = await context.resolveBuildOutput?.(declaration.build, declaration.output)- ?? declaration.output;- const candidate = createBuildRecordCandidate({- build,- sourceOutput,- });+ const candidate = createProvidedCandidate({ id: declaration.id, ...record });addCandidate(candidate);bindCandidateName(declaration.id, candidate.id);continue;@@ -188,7 +183,6 @@ export async function resolveRunDocument(targets,});return {- closure: context.sourceClosure,document,graph,candidates: Object.fromEntries([...candidateNames.entries()].sort(([left], [right]) => left.localeCompare(right))),diff --git a/packages/run/src/types.ts b/packages/run/src/types.tsindex 3dccb361..315c3729 100644--- a/packages/run/src/types.ts+++ b/packages/run/src/types.ts@@ -3,15 +3,13 @@ import type {GraphFragment,} from "@narratage/elaborator";import type {- BuildState,Candidate,- Digest,OperationNode,Satisfaction,StoredValue,TypeRef,} from "@narratage/protocol";-import type { CompiledSourceIdentity, SourceHeader, SourceUnit } from "@narratage/source";+import type { SourceHeader, SourceUnit } from "@narratage/source";export type RunSourceUnit = SourceUnit;@@ -111,12 +109,6 @@ export interface RunFrontendRegistryLike {resolve(id: string): RunFrontend | undefined;}-/** One self-contained Run Source identity. Run Sources do not recursively import other Run Sources. */-export type RunSourceClosure = CompiledSourceIdentity & {- readonly format: "narratage.run-source-closure@1";- readonly id: Digest;-};-export type RunFragmentPackage = {readonly name: string;readonly fragments: Readonly<Record<string, GraphFragment>>;@@ -129,7 +121,6 @@ export interface RunFragmentRegistryLike {/** Complete, mandatory execution-intent graph. Empty alternate Candidate sets are still a Run Graph. */export type RunGraph = {readonly format: "narratage.run-graph@1";- readonly id: Digest;readonly candidates: readonly Candidate[];readonly operations: readonly OperationNode[];readonly satisfactions: readonly Satisfaction[];@@ -138,20 +129,19 @@ export type RunGraph = {export type ResolveRunDocumentContext = {readonly compilation: CompiledSourceClosure;- readonly sourceClosure: RunSourceClosure;readonly fragments: RunFragmentRegistryLike;readonly readStoredValue: (from: string) => Promise<StoredValue> | StoredValue;readonly readFile: (from: string, mediaType: string) => Promise<StoredValue> | StoredValue;- readonly readBuild: (id: string) => Promise<BuildState | undefined> | BuildState | undefined;- /** Host presentation lookup: resolve a prior Build's public output alias to its Logical Output id. */- readonly resolveBuildOutput?: (+ /** Host archive boundary: resolve exactly one accepted historical output, never expose Build state. */+ readonly resolveBuildRecord: (build: string,output: string,- ) => Promise<string | undefined> | string | undefined;+ ) => Promise<{ readonly type: TypeRef; readonly value: StoredValue } | undefined>+ | { readonly type: TypeRef; readonly value: StoredValue }+ | undefined;};export type RunCompilation = {- readonly closure: RunSourceClosure;readonly document: RunDocument;readonly graph: RunGraph;readonly candidates: Readonly<Record<string, string>>;diff --git a/packages/runtime-host-node/src/index.ts b/packages/runtime-host-node/src/index.tsindex dd361b0b..4445b2ac 100644--- a/packages/runtime-host-node/src/index.ts+++ b/packages/runtime-host-node/src/index.ts@@ -8,7 +8,7 @@ import {} from "@narratage/package-loader-node";import type { LoadedPackage } from "@narratage/package-loader-node";import type { ArtifactAttachment } from "@narratage/workspace";-import type { BuildState, CanonicalValue, CapabilityRef, Digest } from "@narratage/protocol";+import type { BuildDefinition, BuildState, CanonicalValue, CapabilityRef, Digest } from "@narratage/protocol";import type {BuildCatalogDescriptor,BuildCatalogEntry,@@ -26,7 +26,7 @@ export const nodeRuntimeHostAdapterAbi = "narratage.node-runtime-host-adapter@1"export type RuntimeHostBuildSubmission = {readonly id: string;readonly state: BuildState;- readonly status: "queued" | "running" | "waiting" | "blocked" | "settling" | "complete" | "failed" | "cancelled";+ readonly status: "queued" | "running" | "waiting" | "blocked" | "complete" | "failed" | "cancelled";readonly dispatch: BuildDispatchSnapshot;};@@ -92,7 +92,7 @@ export type RuntimeHostCredentialControl = {export type RuntimeHostExecution = RuntimeHostMaintenance & RuntimeHostCredentialControl & {build(request: {readonly id: string;- readonly state: BuildState;+ readonly definition: BuildDefinition;readonly implementationPackages?: readonly string[];readonly catalog?: BuildCatalogDescriptor;readonly attachments?: readonly ArtifactAttachment[];@@ -129,19 +129,16 @@ export type ManagedProgramReport = {};export type RuntimeWorkerState = {- readonly state: "running" | "stale" | "stopped";+ readonly state: "running" | "stopped";readonly profile: string;readonly pid?: number;readonly startedAt?: number;- readonly profileDigest?: string;- readonly currentProfileDigest?: string;readonly logPath: string;};export type RuntimeController = {readonly profile: string;readonly dataRoot: string;- revision(): Promise<string>;readonly worker: {up(options?: { readonly maxWaitMs?: number }): Promise<RuntimeWorkerState>;status(): Promise<RuntimeWorkerState>;@@ -200,7 +197,6 @@ export type NodeRuntimeHostAdapterContext = {export type NodeRuntimeHostAdapterFacet = HostFacet & {readonly abi: typeof nodeRuntimeHostAdapterAbi;readonly offers: readonly [string];- readonly identity?: never;readonly implementation: {open(context: NodeRuntimeHostAdapterContext): NodeRuntimeHost | Promise<NodeRuntimeHost>;};diff --git a/packages/runtime-kit/src/index.ts b/packages/runtime-kit/src/index.tsindex 14d8e44c..f98dc8eb 100644--- a/packages/runtime-kit/src/index.ts+++ b/packages/runtime-kit/src/index.ts@@ -95,7 +95,6 @@ export type RuntimeInfrastructureImplementation = {export type RuntimeAdapterHostFacet = HostFacet & {readonly abi: ReturnType<typeof runtimeAdapterHostAbi>;- readonly identity?: never;readonly implementation: RuntimeEndpointAdapterImplementation | RuntimeInfrastructureImplementation;};@@ -168,7 +167,6 @@ export function createRuntimeInfrastructureAdapterFacet(options: {export function isRuntimeAdapterHostFacet(value: HostFacet): value is RuntimeAdapterHostFacet {try {- assert(value.identity === undefined, "Runtime Adapter facet must not carry a second identity");const resolved = address(value as RuntimeAdapterHostFacet);implementation(value.implementation, "Runtime Adapter", resolved.kind);return true;diff --git a/packages/runtime-local/README.md b/packages/runtime-local/README.mdindex 25fb583c..1f1bd301 100644--- a/packages/runtime-local/README.md+++ b/packages/runtime-local/README.md@@ -42,7 +42,7 @@ their Managed Programs. `capacity` belongs to this Runtime deployment, not to CoBuild submission creates durable state and returns. A detached Worker claims dispatches and asksCore to regenerate Commands from verified Build facts after every restart. Provider recovery data-stays with each Operation; cancellation closes Build admission and reconciles work already submitted.+stays with each Operation; cancellation stops new work and makes one best-effort attempt for work already submitted.The Runtime Controller owns the local Worker and Managed Program lifecycle. The generic CLI callsthat Controller and has no PID, signal, log path or Python/Node process policy of its own.diff --git a/packages/runtime-local/src/config.ts b/packages/runtime-local/src/config.tsindex 59b667aa..afbe331a 100644--- a/packages/runtime-local/src/config.ts+++ b/packages/runtime-local/src/config.ts@@ -7,7 +7,7 @@ import {loadNodePackageSelection,} from "@narratage/package-loader-node";import type { LoadedPackage, NodePackageSelectionRequest } from "@narratage/package-loader-node";-import { canonicalize, digestOf } from "@narratage/protocol";+import { canonicalize } from "@narratage/protocol";import type { CanonicalValue, CapabilityRef } from "@narratage/protocol";import {isRuntimeAdapterHostFacet,@@ -272,15 +272,6 @@ export async function resolveRuntimeConfigPaths(};}-/** Deployment revision understood only by this Runtime Profile implementation. */-export async function runtimeConfigRevision(path: string): Promise<string> {- const { document } = await openRuntimeConfig(path);- return digestOf(canonicalize({- format: "narratage.runtime-profile-revision@1",- document,- }));-}-async function installRuntimeAdapters(registry: RuntimeAdapterRegistry,packageRoot: string,diff --git a/packages/runtime-local/src/control.ts b/packages/runtime-local/src/control.tsindex ef540231..76221afd 100644--- a/packages/runtime-local/src/control.ts+++ b/packages/runtime-local/src/control.ts@@ -3,7 +3,6 @@ import {isEnumerableBuildStore,isManagedArtifactStore,isStreamingArtifactStore,- operationCancellationRequestId,verifyRuntimeInfrastructurePackage,} from "@narratage/runtime";import type {@@ -79,7 +78,7 @@ export function createLocalRuntimeArchiveControl(},async queue() {const [dispatches, capacity] = await Promise.all([- options.dispatchStore.list({ phases: ["queued", "leased", "waiting", "blocked", "settling"] }),+ options.dispatchStore.list({ phases: ["queued", "running", "waiting", "blocked"] }),options.dispatchStore.listCapacity(),]);const operationHistory = (await Promise.all(dispatches.map(async (item) =>@@ -100,30 +99,6 @@ export function createLocalRuntimeArchiveControl(if (await options.dispatchStore.read(build) === undefined) return undefined;return await options.dispatchStore.requestCancellation(build, reason);},- async cancelOperation(id, reason) {- let current = await options.operationStore.read(id);- if (current === undefined) return undefined;- if (current.cancellation === undefined) {- const requestedAt = Date.now();- while (current.cancellation === undefined) {- const status = current.status === "completed" || current.status === "failed" || current.status === "cancelled"- ? "too-late" as const- : "requested" as const;- const written = await options.operationStore.compareAndSwap(current.id, current.revision, {- status: "control",- cancellation: {- requestedAt,- requestId: operationCancellationRequestId(current.id, requestedAt),- status,- attempts: 0,- },- });- current = written.status === "stored" ? written.snapshot : written.current;- }- }- await options.dispatchStore.wake(current.build);- return current;- },close() {return options.close?.();},diff --git a/packages/runtime-local/src/host.ts b/packages/runtime-local/src/host.tsindex 4e92bfe7..7fbcfb4e 100644--- a/packages/runtime-local/src/host.ts+++ b/packages/runtime-local/src/host.ts@@ -1,4 +1,3 @@-import { digestOf } from "@narratage/protocol";import {createNodeRuntimeHostAdapterFacet,} from "@narratage/runtime-host-node";@@ -16,7 +15,6 @@ import {createRuntimeMaintenanceFromConfig,doctorRuntimeConfig,resolveRuntimeConfigPaths,- runtimeConfigRevision,} from "./config.js";import {bringManagedProgramsUp,@@ -41,10 +39,6 @@ async function createLocalRuntimeHost(context: NodeRuntimeHostAdapterContext): P} = {}): Promise<RuntimeController> => {const packageRoot = options.packageRoot ?? basePackageRoot;const selection = await resolveRuntimeConfigPaths(profile, { packageRoot });- const revision = async (): Promise<string> => digestOf({- format: "narratage.runtime-worker-revision@1",- profile: await runtimeConfigRevision(profile),- });const implementationPackages = async (): Promise<readonly string[]> => {const values = new Set(options.implementationPackages ?? []);let archive: Awaited<ReturnType<typeof createRuntimeArchiveFromConfig>> | undefined;@@ -65,7 +59,6 @@ async function createLocalRuntimeHost(context: NodeRuntimeHostAdapterContext): Preturn {profile,dataRoot: selection.dataRoot,- revision,worker: {up: async (workerOptions) => {const packages = await implementationPackages();@@ -80,7 +73,6 @@ async function createLocalRuntimeHost(context: NodeRuntimeHostAdapterContext): P"--package-root", packageRoot,],},- await revision(),workerOptions?.maxWaitMs ?? 10_000,packages,);@@ -88,7 +80,6 @@ async function createLocalRuntimeHost(context: NodeRuntimeHostAdapterContext): Pstatus: async () => await runtimeProcessStatus(profile,selection.dataRoot,- await revision(),await implementationPackages(),),logs: async () => await runtimeProcessLogs(selection.dataRoot),@@ -159,8 +150,6 @@ async function createLocalRuntimeHost(context: NodeRuntimeHostAdapterContext): Ptry {await markRuntimeProcessReady(readyFile);await runtime.work({- owner: `worker-${process.pid}`,- leaseMs: 30_000,idlePollMs: 250,signal: abort.signal,});diff --git a/packages/runtime-local/src/index.ts b/packages/runtime-local/src/index.tsindex 924fc60c..fae26921 100644--- a/packages/runtime-local/src/index.ts+++ b/packages/runtime-local/src/index.ts@@ -24,7 +24,6 @@ export {doctorRuntimeConfig,parseRuntimeConfig,resolveRuntimeConfigPaths,- runtimeConfigRevision,} from "./config.js";export {bringManagedProgramsUp,diff --git a/packages/runtime-local/src/runtime.ts b/packages/runtime-local/src/runtime.tsindex 9acc4429..986f6e47 100644--- a/packages/runtime-local/src/runtime.ts+++ b/packages/runtime-local/src/runtime.ts@@ -14,7 +14,6 @@ import {createBuildDispatchIdentity,isStreamingArtifactStore,resolveRuntimeClosure,- sameBuildCatalogDescriptor,sealResolvedRuntimeProfile,} from "@narratage/runtime";import { TypeValidatorRegistry } from "@narratage/validation";@@ -192,7 +191,7 @@ export async function createLocalRuntime(assert(snapshot !== undefined && dispatch !== undefined, `Build ${build} has no durable Runtime state`);const status: LocalBuildSubmission["status"] = dispatch.phase === "terminal"? dispatch.terminal!- : dispatch.phase === "leased" ? "running" : dispatch.phase;+ : dispatch.phase;return { id: build, state: snapshot.state, status, dispatch };};@@ -200,30 +199,11 @@ export async function createLocalRuntime(assert(request.id.trim().length > 0, "Build id must not be empty");if (request.catalog !== undefined) {assert(buildCatalog !== undefined, "Build supplied Host catalog metadata but no BuildCatalog was selected");- assert(request.catalog.core === request.state.id,- `Build Catalog Core ${request.catalog.core} differs from Build ${request.state.id}`);- const existingCatalog = await buildCatalog.read(request.id);- assert(existingCatalog === undefined || sameBuildCatalogDescriptor(existingCatalog, request.catalog),- `Build Catalog ${request.id} already has another source, Run Source or output naming`);}await stageAttachments(request);- // The Host creates a fresh id for every Build. Reading an exact duplicate- // here only closes the crash window between durable Store writes; it is not- // a Source-based lookup and is not exposed as a way to reopen an old Build.- let stored = await options.buildStore.read(request.id);- if (stored === undefined) {- try {- stored = await options.buildStore.create(request.id, request.state);- } catch (error) {- stored = await options.buildStore.read(request.id);- if (stored === undefined) throw error;- }- }- assert(stored.state.id === request.state.id,- `Build submission ${request.id} conflicts with another Core state`);- const created = await options.dispatchStore.create(createBuildDispatchIdentity({+ await options.buildStore.create(request.id, request.definition);+ await options.dispatchStore.create(createBuildDispatchIdentity({build: request.id,- core: request.state.id,...(request.implementationPackages === undefined ? {} : {implementationPackages: request.implementationPackages,}),@@ -258,8 +238,8 @@ export async function createLocalRuntime(async buildMany(requests) {return await Promise.all(requests.map(submit));},- async workOnce(workOptions) {- return await worker.runOnce(workOptions);+ async workOnce() {+ return await worker.runOnce();},async work(workOptions) {await worker.run(workOptions);@@ -345,7 +325,6 @@ export async function createProjectLocalRuntime(deleteCredential: runtime.deleteCredential,builds: runtime.builds,cancel: runtime.cancel,- cancelOperation: runtime.cancelOperation,workOnce: runtime.workOnce,work: runtime.work,readArtifact: runtime.readArtifact,diff --git a/packages/runtime-local/src/scheduler-package.ts b/packages/runtime-local/src/scheduler-package.tsindex 1a560bb3..ed90e586 100644--- a/packages/runtime-local/src/scheduler-package.ts+++ b/packages/runtime-local/src/scheduler-package.ts@@ -1,4 +1,3 @@-import { digestOf } from "@narratage/protocol";import {LocalBuildScheduler,defineRuntimeInfrastructurePackage,diff --git a/packages/runtime-local/src/types.ts b/packages/runtime-local/src/types.tsindex a8ff3541..32f024d4 100644--- a/packages/runtime-local/src/types.ts+++ b/packages/runtime-local/src/types.ts@@ -1,7 +1,7 @@import type { Awaitable, ComponentPackage } from "@narratage/component-kit";import type { ArtifactAttachment } from "@narratage/workspace";import type { EndpointPackage } from "@narratage/endpoint-kit";-import type { BuildState } from "@narratage/protocol";+import type { BuildDefinition, BuildState } from "@narratage/protocol";import type { Digest } from "@narratage/protocol";import type {ArtifactStore,@@ -111,7 +111,7 @@ export type ProjectLocalRuntimeControlOptions = ProjectLocalRuntimeArchiveControexport type LocalBuildRequest = {/** Caller-generated identity for one submission; Source identity is separate. */readonly id: string;- readonly state: BuildState;+ readonly definition: BuildDefinition;/** Installed compute packages selected by this Build's Source closure. */readonly implementationPackages?: readonly string[];/** Source aliases and paths for Host inspection. Not trusted Build input. */@@ -156,7 +156,7 @@ export type LocalCredentialStatus = import("@narratage/endpoint-kit").EndpointCrexport type LocalBuildSubmission = {readonly id: string;readonly state: BuildState;- readonly status: "queued" | "running" | "waiting" | "blocked" | "settling" | "complete" | "failed" | "cancelled";+ readonly status: "queued" | "running" | "waiting" | "blocked" | "complete" | "failed" | "cancelled";readonly dispatch: BuildDispatchSnapshot;};@@ -172,15 +172,13 @@ export type LocalRuntime = {status(build: string): Promise<LocalRuntimeStatus>;activity(build: string): Promise<LocalRuntimeActivity>;queue(): Promise<LocalRuntimeQueue>;- operation(id: Digest): Promise<OperationSnapshot | undefined>;+ operation(id: string): Promise<OperationSnapshot | undefined>;credentials(endpoint?: string): Promise<readonly LocalCredentialStatus[]>;putCredential(endpoint: string, slot: string, secret: string): Promise<LocalCredentialStatus>;deleteCredential(endpoint: string, slot: string): Promise<{ readonly deleted: boolean; readonly credential: LocalCredentialStatus }>;builds(): Promise<readonly BuildCatalogEntry[]>;cancel(build: string, reason?: string): Promise<BuildDispatchSnapshot | undefined>;- /** Request control of one exact Operation attempt without closing the whole Build. */- cancelOperation(id: Digest, reason?: string): Promise<OperationSnapshot | undefined>;- workOnce(options: { readonly owner: string; readonly leaseMs: number }): Promise<BuildDispatchSnapshot | undefined>;+ workOnce(): Promise<BuildDispatchSnapshot | undefined>;work(options: import("@narratage/runtime").RuntimeWorkerRunOptions): Promise<void>;readArtifact(digest: Digest): Promise<Uint8Array | undefined>;openArtifact(digest: Digest): Promise<AsyncIterable<Uint8Array> | undefined>;@@ -197,7 +195,6 @@ export type LocalRuntimeArchiveControl = Pick<LocalRuntime,| "operation"| "builds"| "cancel"- | "cancelOperation"| "close">;@@ -216,7 +213,6 @@ export type LocalRuntimeControl = Pick<LocalRuntime,| "operation"| "builds"| "cancel"- | "cancelOperation"| "readArtifact"| "openArtifact"| "garbageCollectArtifacts"diff --git a/packages/runtime-local/src/worker-process.ts b/packages/runtime-local/src/worker-process.tsindex 2cce774d..1ea794f3 100644--- a/packages/runtime-local/src/worker-process.ts+++ b/packages/runtime-local/src/worker-process.ts@@ -9,12 +9,10 @@ export type RuntimeWorkerLaunch = {};export type RuntimeProcessState = {- readonly state: "running" | "stale" | "stopped";+ readonly state: "running" | "stopped";readonly profile: string;readonly pid?: number;readonly startedAt?: number;- readonly profileDigest?: string;- readonly currentProfileDigest?: string;readonly implementationPackages?: readonly string[];readonly logPath: string;};@@ -24,7 +22,6 @@ type ProcessRecord = {readonly profile: string;readonly pid: number;readonly startedAt: number;- readonly profileDigest: string;readonly implementationPackages: readonly string[];};@@ -111,7 +108,6 @@ async function record(profile: string, dataRoot: string): Promise<ProcessRecordconst value = JSON.parse(await readFile(path, "utf8")) as ProcessRecord;if (value.format !== "narratage.runtime-process@1" || value.profile !== resolve(profile)|| !Number.isSafeInteger(value.pid) || value.pid < 1- || typeof value.profileDigest !== "string" || value.profileDigest.length === 0|| !Array.isArray(value.implementationPackages)|| !value.implementationPackages.every((item) => typeof item === "string" && item.length > 0)) return undefined;return value;@@ -124,7 +120,6 @@ async function record(profile: string, dataRoot: string): Promise<ProcessRecordexport async function runtimeProcessStatus(profile: string,dataRoot: string,- currentProfileDigest: string,requiredImplementationPackages: readonly string[] = [],): Promise<RuntimeProcessState> {const location = paths(dataRoot);@@ -133,26 +128,14 @@ export async function runtimeProcessStatus(return { state: "stopped", profile: resolve(profile), logPath: location.log };}const currentPackages = new Set(current.implementationPackages);- if (current.profileDigest !== currentProfileDigest- || requiredImplementationPackages.some((item) => !currentPackages.has(item))) {- return {- state: "stale",- profile: current.profile,- pid: current.pid,- startedAt: current.startedAt,- profileDigest: current.profileDigest,- currentProfileDigest,- implementationPackages: current.implementationPackages,- logPath: location.log,- };+ if (requiredImplementationPackages.some((item) => !currentPackages.has(item))) {+ return { state: "stopped", profile: current.profile, logPath: location.log };}return {state: "running",profile: current.profile,pid: current.pid,startedAt: current.startedAt,- profileDigest: current.profileDigest,- currentProfileDigest,implementationPackages: current.implementationPackages,logPath: location.log,};@@ -161,14 +144,13 @@ export async function runtimeProcessStatus(async function waitForReady(profile: string,dataRoot: string,- profileDigest: string,timeoutMs: number,implementationPackages: readonly string[],): Promise<RuntimeProcessState> {const location = paths(dataRoot);const deadline = Date.now() + timeoutMs;while (Date.now() <= deadline) {- const state = await runtimeProcessStatus(profile, dataRoot, profileDigest, implementationPackages);+ const state = await runtimeProcessStatus(profile, dataRoot, implementationPackages);if (state.state === "stopped") {const log = await readFile(location.log, "utf8").catch(() => "");throw new Error(`Runtime Worker exited before becoming ready${log.length === 0 ? "" : `: ${log.trim().split("\n").at(-1)}`}`);@@ -183,15 +165,17 @@ export async function ensureRuntimeProcess(profile: string,dataRoot: string,launch: RuntimeWorkerLaunch,- profileDigest: string,timeoutMs = 10_000,implementationPackages: readonly string[] = [],): Promise<RuntimeProcessState> {return await withLifecycleLock(dataRoot, timeoutMs, async () => {const selectedPackages = [...new Set(implementationPackages)].sort();- const current = await runtimeProcessStatus(profile, dataRoot, profileDigest, selectedPackages);+ const existing = await record(profile, dataRoot);+ const current = await runtimeProcessStatus(profile, dataRoot, selectedPackages);if (current.state === "running") return current;- if (current.state === "stale") await stopRuntimeProcessUnlocked(profile, dataRoot, timeoutMs);+ 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 });@@ -218,12 +202,11 @@ export async function ensureRuntimeProcess(profile: absolute,pid: child.pid,startedAt,- profileDigest,implementationPackages: selectedPackages,} satisfies ProcessRecord), "utf8");child.unref();await log.close();- return await waitForReady(absolute, dataRoot, profileDigest, timeoutMs, selectedPackages);+ return await waitForReady(absolute, dataRoot, timeoutMs, selectedPackages);});}diff --git a/packages/runtime-local/src/worker.ts b/packages/runtime-local/src/worker.tsindex 292b1582..37756528 100644--- a/packages/runtime-local/src/worker.ts+++ b/packages/runtime-local/src/worker.ts@@ -1,10 +1,6 @@-import { randomUUID } from "node:crypto";--import type { BuildState, Digest } from "@narratage/protocol";-import { capacityReservationId } from "@narratage/runtime";+import type { BuildState } from "@narratage/protocol";import type {BuildDispatchSnapshot,- DispatchLease,RuntimeCommandExecutor,RuntimeExecutionResult,RuntimePreparation,@@ -24,10 +20,6 @@ function positive(value: number, subject: string): number {return value;}-function sameLease(left: DispatchLease | undefined, right: DispatchLease): boolean {- return left?.owner === right.owner && left.token === right.token && left.fence === right.fence;-}-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) => {@@ -39,154 +31,76 @@ async function pause(ms: number, signal?: AbortSignal): Promise<void> {});}+/** Adds persistent in-flight limits only to asynchronous external Operations. */class CapacityExecutor implements RuntimeCommandExecutor {readonly #delegate: RuntimeCommandExecutor;readonly #options: RuntimeWorkerFactoryOptions;readonly #build: string;- readonly #dispatchLease: DispatchLease;- readonly #leaseMs: number;- readonly #suppressedCommands: ReadonlySet<string>;- constructor(- delegate: RuntimeCommandExecutor,- options: RuntimeWorkerFactoryOptions,- build: string,- dispatchLease: DispatchLease,- leaseMs: number,- suppressedCommands: ReadonlySet<string> = new Set(),- ) {+ constructor(delegate: RuntimeCommandExecutor, options: RuntimeWorkerFactoryOptions, build: string) {this.#delegate = delegate;this.#options = options;this.#build = build;- this.#dispatchLease = dispatchLease;- this.#leaseMs = leaseMs;- this.#suppressedCommands = suppressedCommands;}prepare(state: BuildState): RuntimePreparation {- const prepared = this.#delegate.prepare(state);- const suppressed = prepared.runnable.filter((item) => this.#suppressedCommands.has(item.command.id));- return {- state: prepared.state,- runnable: prepared.runnable.filter((item) => !this.#suppressedCommands.has(item.command.id)),- blocked: [- ...prepared.blocked,- ...suppressed.map((item) => ({- command: item.command.id,- reason: "the exact Operation was cancelled by Runtime control",- subject: item.command.id,- })),- ],- };+ return this.#delegate.prepare(state);}- async #assertAuthority(): Promise<void> {+ async #assertRunning(): Promise<void> {const dispatch = await this.#options.stores.dispatch.read(this.#build);- assert(dispatch?.admission === "open", `Build ${this.#build} no longer admits new Operations`);- assert(dispatch.phase === "leased" && sameLease(dispatch.lease, this.#dispatchLease),- `Build ${this.#build} Worker lease is stale`);+ assert(dispatch?.phase === "running", `Build ${this.#build} is not owned by the Worker`);+ assert(dispatch.cancellation === undefined, `Build ${this.#build} is being cancelled`);}- async #acquire(descriptor: RuntimeRunnableCommand) {- await this.#assertAuthority();- const now = Date.now();+ async executeCommand(+ state: BuildState,+ descriptor: RuntimeRunnableCommand,+ context: { readonly build: string },+ ): Promise<RuntimeExecutionResult> {+ assert(context.build === this.#build, "Capacity executor received another Build identity");+ await this.#assertRunning();+ if (descriptor.capacityMode !== "asynchronous") {+ return await this.#delegate.executeCommand(state, descriptor, context);+ }const resources = descriptor.resources.map((resource) => {const override = this.#options.scheduling.resourceLimits?.[resource.id];return override === undefined? resource: { ...resource, maxActive: override, maxInFlight: override };});- return await this.#options.stores.dispatch.acquireCapacity({+ const acquired = await this.#options.stores.dispatch.acquireCapacity({build: this.#build,command: descriptor.command.id,resources,...(descriptor.queue === undefined ? {} : { queue: descriptor.queue }),- mode: descriptor.capacityMode ?? "active",- buildLease: this.#dispatchLease,- owner: this.#dispatchLease.owner,- token: randomUUID(),- now,- leaseMs: this.#leaseMs,- limits: {- globalActive: positive(this.#options.scheduling.maxConcurrency ?? 1, "global active capacity"),- },+ now: Date.now(),});- }-- async executeCommand(- state: BuildState,- descriptor: RuntimeRunnableCommand,- context: { readonly build: string },- ): Promise<RuntimeExecutionResult> {- assert(context.build === this.#build, "Capacity executor received another Build identity");- const acquired = await this.#acquire(descriptor);if (acquired.status === "blocked") {return {status: "deferred",- wakeAt: acquired.retryAt,- reason: acquired.resource === undefined- ? acquired.reason- : `${acquired.reason}:${acquired.resource}`,+ wakeAt: acquired.availableAt,+ reason: `${acquired.reason}:${acquired.resource}`,};}- const reservation = acquired.reservation;- const lease = reservation.active;- assert(lease !== undefined, `Capacity reservation ${reservation.id} has no active lease`);- let heartbeatError: unknown;- let heartbeat = Promise.resolve();- const heartbeatTimer = setInterval(() => {- heartbeat = heartbeat.then(async () => {- await this.#options.stores.dispatch.heartbeatCapacity(- reservation.id,- lease,- Date.now(),- this.#leaseMs,- );- }).catch((error: unknown) => { heartbeatError = error; });- }, Math.max(1, Math.floor(this.#leaseMs / 3)));try {const result = await this.#delegate.executeCommand(state, descriptor, context);- clearInterval(heartbeatTimer);- await heartbeat;- if (heartbeatError !== undefined) throw heartbeatError;- await this.#assertAuthority();- await this.#options.stores.dispatch.heartbeatCapacity(reservation.id, lease, Date.now(), this.#leaseMs);- if (result.status === "pending" && (descriptor.capacityMode ?? "active") === "recoverable") {- await this.#options.stores.dispatch.parkCapacity(reservation.id, lease, true);- } else {- await this.#options.stores.dispatch.releaseCapacity(reservation.id, lease);- }- const controlled = (await this.#options.stores.operations.list({- build: this.#build,- command: descriptor.command.id,- })).find((item) => item.cancellation !== undefined);- if (controlled !== undefined) {- return {- status: "pending",- operation: controlled.id,- ...(controlled.cancellation?.retryAt === undefined ? {} : { wakeAt: controlled.cancellation.retryAt }),- };+ if (result.status !== "pending") {+ await this.#options.stores.dispatch.releaseCapacity(acquired.reservation.id);}return result;} catch (error) {- clearInterval(heartbeatTimer);- await heartbeat;- await this.#options.stores.dispatch.releaseCapacity(reservation.id, lease).catch(() => undefined);+ await this.#options.stores.dispatch.releaseCapacity(acquired.reservation.id).catch(() => undefined);throw error;- } finally {- clearInterval(heartbeatTimer);}}async cancelOperation(state: BuildState,operation: import("@narratage/runtime").OperationSnapshot,- requestedAt: number,) {assert(this.#delegate.cancelOperation !== undefined, "selected executor cannot cancel Operations");- const observed = await this.#delegate.cancelOperation(state, operation, requestedAt);- await this.#assertAuthority();- return observed;+ return await this.#delegate.cancelOperation(state, operation);}}@@ -199,167 +113,65 @@ class DurableLocalWorker implements RuntimeWorker {this.#options = options;}- async #cancel(dispatch: BuildDispatchSnapshot, lease: DispatchLease): Promise<BuildDispatchSnapshot> {+ async #cancel(dispatch: BuildDispatchSnapshot): Promise<BuildDispatchSnapshot> {const snapshot = await this.#options.stores.builds.read(dispatch.build);assert(snapshot !== undefined, `Build ${dispatch.build} has no durable state`);const active = (await this.#options.stores.operations.list({ build: dispatch.build }))- .filter((item) => item.status === "created" || item.status === "pending");- if (active.length > 0 && this.#executor.cancelOperation === undefined) {- return await this.#options.stores.dispatch.release(dispatch.build, lease, {- phase: "settling",- availableAt: Date.now() + 1_000,- reason: "cancellation awaits an Operation controller",- });- }- const requestedAt = dispatch.cancellation?.requestedAt;- assert(requestedAt !== undefined, `closing Build ${dispatch.build} has no cancellation request`);+ .filter((item) => item.status === "pending");for (const operation of active) {- await this.#executor.cancelOperation!(snapshot.state, operation, requestedAt);- }- const operations = await this.#options.stores.operations.list({ build: dispatch.build });- const terminal = operations.filter((item) => item.status === "completed"- || item.status === "failed" || item.status === "cancelled");- for (const operation of terminal) {- await this.#options.stores.dispatch.clearCapacity(- capacityReservationId(dispatch.build, operation.command),- dispatch.build,- lease,- );- }- const remaining = operations.filter((item) => item.status === "created" || item.status === "pending");- if (remaining.length > 0) {- const wakeAt = Math.min(...remaining.map((item) => item.cancellation?.retryAt- ?? item.wakeAt ?? Date.now() + 1_000));- return await this.#options.stores.dispatch.release(dispatch.build, lease, {- phase: "settling",- availableAt: wakeAt,- reason: "cancellation is awaiting remote terminal facts",- });- }- const stranded = (await this.#options.stores.dispatch.listCapacity())- .filter((reservation) => reservation.build === dispatch.build);- for (const reservation of stranded) {- await this.#options.stores.dispatch.clearCapacity(- reservation.id,- dispatch.build,- lease,- );+ if (this.#executor.cancelOperation !== undefined) {+ await this.#executor.cancelOperation(snapshot.state, operation).catch(() => undefined);+ } else {+ await this.#options.stores.operations.update(operation.id, { status: "cancelled" });+ }}+ await this.#options.stores.dispatch.releaseBuildCapacity(dispatch.build);return await this.#options.stores.dispatch.finish(dispatch.build,- lease,"cancelled",- dispatch.cancellation?.reason ?? "cancelled by Runtime pool",+ dispatch.cancellation?.reason ?? "cancelled by Runtime",);}- async #reconcileOperationControl(- state: BuildState,- executor: CapacityExecutor,- dispatch: BuildDispatchSnapshot,- lease: DispatchLease,- suppressed: Set<string>,- ): Promise<void> {- const operations = await this.#options.stores.operations.list({ build: dispatch.build });- for (const operation of operations) {- const control = operation.cancellation;- if (control === undefined) continue;- suppressed.add(operation.command);- if (operation.status === "completed" || operation.status === "failed" || operation.status === "cancelled") {- await this.#options.stores.dispatch.clearCapacity(- capacityReservationId(dispatch.build, operation.command),- dispatch.build,- lease,- );- continue;- }- if (control.retryAt !== undefined && control.retryAt > Date.now()) continue;- const observed = await executor.cancelOperation(state, operation, control.requestedAt);- if (observed.status === "completed" || observed.status === "failed" || observed.status === "cancelled") {- await this.#options.stores.dispatch.clearCapacity(- capacityReservationId(dispatch.build, observed.command),- dispatch.build,- lease,- );- }- }- }-- async runOnce(options: { readonly owner: string; readonly leaseMs: number }): Promise<BuildDispatchSnapshot | undefined> {- assert(options.owner.trim().length > 0, "Worker owner is empty");- const leaseMs = positive(options.leaseMs, "Worker leaseMs");- const token = randomUUID();- const dispatch = await this.#options.stores.dispatch.claim({- owner: options.owner,- token,- now: Date.now(),- leaseMs,- });+ async runOnce(): Promise<BuildDispatchSnapshot | undefined> {+ const dispatch = await this.#options.stores.dispatch.claim();if (dispatch === undefined) return undefined;- const lease = dispatch.lease;- assert(lease !== undefined, `claimed Dispatch ${dispatch.build} has no lease`);- if (dispatch.admission !== "open") return await this.#cancel(dispatch, lease);+ if (dispatch.cancellation !== undefined) return await this.#cancel(dispatch);const stored = await this.#options.stores.builds.read(dispatch.build);- assert(stored !== undefined, `Dispatch ${dispatch.build} has no BuildState`);- assert(stored.state.id === dispatch.core, `Dispatch ${dispatch.build} Core identity differs`);- const suppressedCommands = new Set<string>();- const controlled = new CapacityExecutor(- this.#executor,- this.#options,- dispatch.build,- lease,- leaseMs,- suppressedCommands,- );- await this.#reconcileOperationControl(stored.state, controlled, dispatch, lease, suppressedCommands);+ assert(stored !== undefined, `Dispatch ${dispatch.build} has no Build Definition`);+ const controlled = new CapacityExecutor(this.#executor, this.#options, dispatch.build);const scheduler = this.#options.scheduler.create(controlled, {...this.#options.scheduling,buildStore: this.#options.stores.builds,runtimeClosure: this.#options.runtimeClosure,});- let heartbeatError: unknown;- let heartbeat = Promise.resolve();- const timer = setInterval(() => {- heartbeat = heartbeat.then(async () => {- await this.#options.stores.dispatch.heartbeat(dispatch.build, lease, Date.now(), leaseMs);- }).catch((error: unknown) => { heartbeatError = error; });- }, Math.max(1, Math.floor(leaseMs / 3)));try {- const [result] = await scheduler.run([{ id: dispatch.build, state: stored.state }]);+ const [result] = await scheduler.run([{ id: dispatch.build, state: stored.state, snapshot: stored }]);assert(result !== undefined, `Scheduler returned no result for ${dispatch.build}`);- clearInterval(timer);- await heartbeat;- if (heartbeatError !== undefined) throw heartbeatError;const current = await this.#options.stores.dispatch.read(dispatch.build);- assert(current?.phase === "leased" && sameLease(current.lease, lease),- `Dispatch ${dispatch.build} lease was fenced before settlement`);- if (current.admission !== "open") return await this.#cancel(current, lease);+ assert(current?.phase === "running", `Dispatch ${dispatch.build} is no longer running`);+ if (current.cancellation !== undefined) return await this.#cancel(current);if (result.status === "complete") {- return await this.#options.stores.dispatch.finish(dispatch.build, lease, "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";- return await this.#options.stores.dispatch.finish(dispatch.build, lease, "failed", reason);+ 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");- const controlledOperations = (await this.#options.stores.operations.list({ build: dispatch.build }))- .filter((item) => item.cancellation !== undefined)- .filter((item) => item.status === "created" || item.status === "pending");const now = Date.now();const wakeTimes = [...pending.map((item) => item.wakeAt ?? now + 1_000),...deferred.map((item) => item.wakeAt ?? now + 1_000),- ...controlledOperations.map((item) => item.cancellation?.status === "requested"- && item.cancellation.retryAt === undefined- ? now- : item.cancellation?.retryAt ?? item.wakeAt ?? now + 1_000),];const wakeAt = wakeTimes.length === 0- ? result.blocked.length > 0 ? Number.MAX_SAFE_INTEGER : Date.now()+ ? result.blocked.length > 0 ? Number.MAX_SAFE_INTEGER : now: Math.min(...wakeTimes);- return await this.#options.stores.dispatch.release(dispatch.build, lease, {- phase: pending.length > 0 || controlledOperations.length > 0+ const released = await this.#options.stores.dispatch.release(dispatch.build, {+ phase: pending.length > 0? "waiting": result.blocked.length > 0 ? "blocked" : "queued",availableAt: wakeAt,@@ -367,23 +179,20 @@ class DurableLocalWorker implements RuntimeWorker {reason: result.blocked.map((item) => `${item.reason}: ${item.subject}`).join(", "),}),});+ return released.cancellation === undefined ? released : await this.#cancel(released);} catch (error) {- clearInterval(timer);- await heartbeat;const current = await this.#options.stores.dispatch.read(dispatch.build);- if (current?.phase !== "leased" || !sameLease(current.lease, lease)) throw error;- if (current.admission !== "open") return await this.#cancel(current, lease);+ if (current?.cancellation !== undefined) return await this.#cancel(current);const reason = error instanceof Error ? error.message : String(error);- return await this.#options.stores.dispatch.finish(dispatch.build, lease, "failed", reason);- } finally {- clearInterval(timer);+ await this.#options.stores.dispatch.releaseBuildCapacity(dispatch.build);+ return await this.#options.stores.dispatch.finish(dispatch.build, "failed", reason);}}async run(options: RuntimeWorkerRunOptions): Promise<void> {positive(options.idlePollMs, "Worker idlePollMs");while (options.signal?.aborted !== true) {- const dispatch = await this.runOnce(options);+ const dispatch = await this.runOnce();if (dispatch === undefined) await pause(options.idlePollMs, options.signal).catch((error: unknown) => {if (options.signal?.aborted !== true) throw error;});diff --git a/packages/runtime-local/test/config.test.ts b/packages/runtime-local/test/config.test.tsindex 00aba533..021e604b 100644--- a/packages/runtime-local/test/config.test.ts+++ b/packages/runtime-local/test/config.test.ts@@ -14,7 +14,6 @@ import {createRuntimeArtifactAccessFromConfig,doctorRuntimeConfig,parseRuntimeConfig,- runtimeConfigRevision,RuntimeAdapterRegistry,} from "@narratage/runtime-local";@@ -74,21 +73,6 @@ test("Runtime Profile rejects source ownership fields", () => {/does not accept root/u);});-test("Runtime revision follows Profile meaning rather than formatting", async () => {- const root = await mkdtemp(join(tmpdir(), "narratage-runtime-revision-"));- const profilePath = join(root, "deployment.profile");- try {- await writeFile(profilePath, JSON.stringify(profile()), "utf8");- const first = await runtimeConfigRevision(profilePath);- await writeFile(profilePath, JSON.stringify(profile(), null, 2), "utf8");- assert.equal(await runtimeConfigRevision(profilePath), first);- await writeFile(profilePath, JSON.stringify(profile({ dataRoot: "elsewhere" })), "utf8");- assert.notEqual(await runtimeConfigRevision(profilePath), first);- } finally {- await rm(root, { recursive: true, force: true });- }-});-test("archive observation constructs only the infrastructure selected by archive roles", async () => {const root = await mkdtemp(join(tmpdir(), "narratage-runtime-slice-"));const path = join(root, "narratage.runtime.json");diff --git a/packages/runtime-local/test/local.test.ts b/packages/runtime-local/test/local.test.tsindex 104822b4..d41cb499 100644--- a/packages/runtime-local/test/local.test.ts+++ b/packages/runtime-local/test/local.test.ts@@ -9,18 +9,19 @@ import {import { tmpdir } from "node:os";import { join } from "node:path";import test from "node:test";+import { fixtureDigest } from "../../../test/fixture-digest.js";import { FileArtifactStore, createFileArtifactStorePackage } from "@narratage/artifact-store-fs";import { createEnvironmentCredentialStorePackage } from "@narratage/credential-store-env";import { MemoryArtifactStore } from "@narratage/driver-node";import { defineEndpointPackage } from "@narratage/endpoint-kit";-import type { RecoverableEndpoint } from "@narratage/endpoint-kit";+import type { AsyncEndpoint } from "@narratage/endpoint-kit";import type {ComponentPackage,EndpointPackage,} from "@narratage/runtime-local";import { createLocalExecutionPackage, createProjectLocalRuntime } from "@narratage/runtime-local";-import { digestOf } from "@narratage/core";+import { buildDefinition } from "@narratage/core";import {collectNodePackageComponents,loadNodePackageSelection,@@ -48,7 +49,7 @@ const providerManifest: RuntimeModuleManifest = {name: providerFacet.name,role: "capability-endpoint",fulfills: [{ capability: capabilities.generation, returns: types.generated }],- lifecycle: "recoverable",+ lifecycle: "asynchronous",defaultConcurrency: 1,}],};@@ -126,10 +127,10 @@ test("Endpoint-declared credentials use the selected writable Store without a Prcapabilities: [{capability: capabilities.generation,returns: types.generated,- lifecycle: "recoverable",+ lifecycle: "asynchronous",endpoint: {start() { throw new Error("unused"); },- resume() { throw new Error("unused"); },+ poll() { throw new Error("unused"); },},}],});@@ -159,13 +160,12 @@ test("Endpoint-declared credentials use the selected writable Store without a Pr}});-test("project local runtime resumes durable work while component and endpoint packages stay replaceable", async () => {+test("project local runtime queues, polls and cancels work with replaceable packages", async () => {const directory = await mkdtemp(join(tmpdir(), "narratage-local-"));const initial = createGreetingBuild();const catalog = {format: "narratage.build-catalog-descriptor@1" as const,- core: initial.id,- source: { path: join(directory, "main.svml"), closure: digestOf("source:greeting") },+ source: { path: join(directory, "main.svml") },aliases: [{name: "final.document",ref: { kind: "logical-output" as const, id: initial.request.targets[0]!.output },@@ -174,14 +174,9 @@ test("project local runtime resumes durable work while component and endpoint palet promptCalls = 0;let requestCalls = 0;let assembleCalls = 0;- let placeholderCalls = 0;let starts = 0;let resumes = 0;let cancels = 0;- let cancellationMode: "confirmed" | "unsupported" = "confirmed";- let holdNextStart = false;- let startEntered: (() => void) | undefined;- let releaseStart: (() => void) | undefined;const components: ComponentPackage = {producers: [{@@ -205,7 +200,6 @@ test("project local runtime resumes durable work while component and endpoint pa{producer: producers.placeholderText,handler: async ({ inputs }) => {- placeholderCalls += 1;assert.equal(inputs.prompt?.value.kind, "inline");await new Promise((resolve) => setTimeout(resolve, 100));return { outputs: { generated: { kind: "inline", value: `Preview: ${inputs.prompt.value.value}` } }, needs: {} };@@ -224,18 +218,14 @@ test("project local runtime resumes durable work while component and endpoint pa},],};- const recoverableEndpoint: RecoverableEndpoint = {+ const asyncEndpoint: AsyncEndpoint = {start({ operation }) {starts += 1;- const pending = { status: "pending" as const, checkpoint: { remoteJob: operation.id }, wakeAt: Date.now() };- if (!holdNextStart) return pending;- holdNextStart = false;- startEntered?.();- return new Promise((resolve) => { releaseStart = () => resolve(pending); });+ return { status: "pending" as const, handle: { remoteJob: operation }, wakeAt: Date.now() };},- resume({ checkpoint }) {+ poll({ handle }) {resumes += 1;- assert.ok(checkpoint);+ assert.ok(handle);return {status: "completed",result: {@@ -245,7 +235,7 @@ test("project local runtime resumes durable work while component and endpoint pa},cancel() {cancels += 1;- return { status: cancellationMode };+ return { status: "confirmed" };},};const endpointPackage: EndpointPackage = {@@ -262,11 +252,11 @@ test("project local runtime resumes durable work while component and endpoint pa}],credentials: [],install(registry) {- registry.registerRecoverableEndpoint(+ registry.registerAsyncEndpoint("generation.personal",capabilities.generation,types.generated,- recoverableEndpoint,+ asyncEndpoint,);},};@@ -278,205 +268,44 @@ test("project local runtime resumes durable work while component and endpoint pacomponents: [components],endpoints: [endpointPackage],});- const first = await firstRuntime.build({ id: "greeting-build", state: initial, catalog });+ const first = await firstRuntime.build({ id: "greeting-build", definition: buildDefinition(initial), catalog });assert.equal(first.status, "queued");- assert.equal((await firstRuntime.workOnce({ owner: "worker-one", leaseMs: 5_000 }))?.phase, "waiting");+ assert.equal((await firstRuntime.workOnce())?.phase, "waiting");assert.equal(starts, 1);assert.equal(resumes, 0);- await firstRuntime.close();-- const secondRuntime = await createProjectLocalRuntime({- dataRoot: directory,- ...projectRuntimeFixture(directory),- components: [components],- endpoints: [endpointPackage],- });- const second = await secondRuntime.build({ id: "greeting-build", state: createGreetingBuild(), catalog });- assert.equal(second.status, "waiting");- assert.equal((await secondRuntime.workOnce({ owner: "worker-two", leaseMs: 5_000 }))?.terminal, "complete");+ const second = await firstRuntime.status("greeting-build");+ assert.equal(second.dispatch?.phase, "waiting");+ assert.equal((await firstRuntime.workOnce())?.terminal, "complete");assert.equal(starts, 1);assert.equal(resumes, 1);assert.equal(promptCalls, 1, "persisted Core facts stop deterministic upstream replay");assert.equal(requestCalls, 1, "the Need request Producer is also persisted");assert.equal(assembleCalls, 1);- const clientStatus = await secondRuntime.status("greeting-build");- assert.equal(clientStatus.catalog?.core, initial.id);+ const clientStatus = await firstRuntime.status("greeting-build");assert.equal(clientStatus.catalog?.aliases[0]?.name, "final.document");- assert.deepEqual((await secondRuntime.builds()).map((item) => item.build), ["greeting-build"]);+ assert.deepEqual((await firstRuntime.builds()).map((item) => item.build), ["greeting-build"]);- await secondRuntime.build({ id: "greeting-follow", state: createGreetingBuild() });- await secondRuntime.workOnce({ owner: "worker-two", leaseMs: 5_000 });- await secondRuntime.workOnce({ owner: "worker-two", leaseMs: 5_000 });- const followed = await secondRuntime.build(- { id: "greeting-follow", state: createGreetingBuild() },- { follow: true, pollIntervalMs: 1, maxWaitMs: 1_000 },- );- assert.equal(followed.status, "complete");+ await firstRuntime.build({ id: "greeting-follow", definition: buildDefinition(createGreetingBuild()) });+ await firstRuntime.workOnce();+ await firstRuntime.workOnce();+ const followed = await firstRuntime.status("greeting-follow");+ assert.equal(followed.dispatch?.terminal, "complete");assert.equal(starts, 2);assert.equal(resumes, 2);- const followedStatus = await secondRuntime.status("greeting-follow");+ const followedStatus = await firstRuntime.status("greeting-follow");assert.equal(followedStatus.build?.state.status, "complete");assert.equal(followedStatus.operations.length, 1);- const tooLate = await secondRuntime.cancelOperation(followedStatus.operations[0]!.id);- assert.equal(tooLate?.status, "completed");- assert.equal(tooLate?.cancellation?.status, "too-late");- const waiting = await secondRuntime.build({ id: "greeting-cancel", state: createGreetingBuild() });+ const waiting = await firstRuntime.build({ id: "greeting-cancel", definition: buildDefinition(createGreetingBuild()) });assert.equal(waiting.status, "queued");- await secondRuntime.workOnce({ owner: "worker-two", leaseMs: 5_000 });- const requested = await secondRuntime.cancel("greeting-cancel");- assert.equal(requested?.admission, "closing");- const cancelled = await secondRuntime.workOnce({ owner: "worker-two", leaseMs: 5_000 });+ await firstRuntime.workOnce();+ const requested = await firstRuntime.cancel("greeting-cancel");+ assert.notEqual(requested?.cancellation, undefined);+ const cancelled = await firstRuntime.workOnce();assert.equal(cancelled?.terminal, "cancelled");assert.equal(cancels, 1);-- await secondRuntime.build({ id: "greeting-operation-cancel", state: createGreetingBuild() });- assert.equal(- (await secondRuntime.workOnce({ owner: "worker-two", leaseMs: 5_000 }))?.phase,- "waiting",- );- const [operation] = (await secondRuntime.status("greeting-operation-cancel")).operations;- assert.ok(operation);- const operationRequest = await secondRuntime.cancelOperation(operation.id, "stop only this realization");- assert.equal(operationRequest?.cancellation?.status, "requested");- const stranded = await secondRuntime.workOnce({ owner: "worker-two", leaseMs: 5_000 });- assert.equal(stranded?.phase, "blocked");- assert.equal(stranded?.admission, "open", "Operation control does not close unrelated Build admission");- assert.equal(stranded?.terminal, undefined);- const controlled = await secondRuntime.operation(operation.id);- assert.equal(controlled?.status, "cancelled");- assert.equal(controlled?.cancellation?.status, "confirmed");- assert.notEqual((await secondRuntime.status("greeting-operation-cancel")).build?.state.status, "failed");- assert.equal(cancels, 2);-- cancellationMode = "unsupported";- await secondRuntime.build({ id: "greeting-operation-late", state: createGreetingBuild() });- await secondRuntime.workOnce({ owner: "worker-two", leaseMs: 5_000 });- const [lateOperation] = (await secondRuntime.status("greeting-operation-late")).operations;- assert.ok(lateOperation);- await secondRuntime.cancelOperation(lateOperation.id);- const lateDispatch = await secondRuntime.workOnce({ owner: "worker-two", leaseMs: 5_000 });- assert.equal(lateDispatch?.phase, "blocked");- const lateFact = await secondRuntime.operation(lateOperation.id);- assert.equal(lateFact?.status, "completed", "an unsupported remote stop keeps the factual result");- assert.equal(lateFact?.cancellation?.status, "unsupported");- assert.ok(lateFact?.completion, "late paid output remains archived in OperationStore");- const lateBuild = await secondRuntime.status("greeting-operation-late");- assert.notEqual(lateBuild.build?.state.status, "complete", "late output is not reduced into Core");- assert.notEqual(lateBuild.build?.state.status, "failed");-- cancellationMode = "confirmed";- const assembledBeforeSide = assembleCalls;- holdNextStart = true;- const entered = new Promise<void>((resolve) => { startEntered = resolve; });- await secondRuntime.build({- id: "greeting-operation-side",- state: createGreetingBuild({ includeSideTarget: true }),- });- const firstSideTurn = secondRuntime.workOnce({ owner: "worker-two", leaseMs: 5_000 });- await entered;- const sidePending = await secondRuntime.status("greeting-operation-side");- const [sideOperation] = sidePending.operations;- assert.ok(sideOperation);- await secondRuntime.cancelOperation(sideOperation.id);- releaseStart?.();- const sideDispatch = await firstSideTurn;- assert.equal(placeholderCalls, 1);- // The Scheduler may finish the independent side branch in this same leased turn; cancellation- // constrains only the exact remote Command and deliberately imposes no ordering on siblings.- assert.equal(sideDispatch?.phase, "waiting");- assert.ok(sideDispatch.availableAt <= Date.now(), JSON.stringify(sideDispatch));- const reconciledSide = await secondRuntime.workOnce({ owner: "worker-two", leaseMs: 5_000 });- assert.equal(reconciledSide?.phase, "blocked");- const sideStatus = await secondRuntime.status("greeting-operation-side");- assert.equal(sideStatus.build?.state.records.some((item) => item.id === "document:side"), true,- "an unrelated branch keeps advancing after one Operation is suppressed");- assert.equal(assembleCalls, assembledBeforeSide + 1);- assert.notEqual(sideStatus.build?.state.status, "failed");- await secondRuntime.close();- } finally {- await rm(directory, { recursive: true, force: true });- }-});--test("two durable Workers share one SQLite capacity limit across Runtime instances", async () => {- const directory = await mkdtemp(join(tmpdir(), "narratage-local-shared-capacity-"));- let promptCalls = 0;- let entered: (() => void) | undefined;- let release: (() => void) | undefined;- const firstEntered = new Promise<void>((resolve) => { entered = resolve; });- const holdFirst = new Promise<void>((resolve) => { release = resolve; });- const components: ComponentPackage = {- producers: [{- producer: producers.makePrompt,- handler: async ({ inputs }) => {- promptCalls += 1;- if (promptCalls === 1) {- entered?.();- await holdFirst;- }- assert.equal(inputs.intent?.value.kind, "inline");- return { outputs: { prompt: { kind: "inline", value: "Greet Ada" } }, needs: {} };- },- }, {- producer: producers.requestText,- handler: ({ inputs }) => {- assert.equal(inputs.prompt?.value.kind, "inline");- return { outputs: {}, needs: { generation: { prompt: inputs.prompt.value.value } } };- },- }, {- producer: producers.placeholderText,- handler: ({ inputs }) => ({- outputs: { generated: { kind: "inline", value: `Preview: ${String(inputs.prompt?.value.kind === "inline" ? inputs.prompt.value.value : "")}` } },- needs: {},- }),- }, {- producer: producers.assemble,- handler: ({ inputs }) => ({- outputs: { document: { kind: "inline", value: { text: String(inputs.generated?.value.kind === "inline" ? inputs.generated.value.value : "") } } },- needs: {},- }),- }],- };- const options = () => ({- ...projectRuntimeFixture(directory),- scheduling: { maxConcurrency: 1 },- components: [components],- } as const);- let first: Awaited<ReturnType<typeof createProjectLocalRuntime>> | undefined;- let second: Awaited<ReturnType<typeof createProjectLocalRuntime>> | undefined;- let firstTurn: Promise<unknown> | undefined;- let secondTurn: Promise<unknown> | undefined;- try {- first = await createProjectLocalRuntime({ dataRoot: directory, ...options() });- second = await createProjectLocalRuntime({ dataRoot: directory, ...options() });- await first.build({- id: "capacity-a",- state: createGreetingBuild({ generationRealization: "placeholder" }),- });- await second.build({- id: "capacity-b",- state: createGreetingBuild({ generationRealization: "placeholder" }),- });- firstTurn = first.workOnce({ owner: "worker-a", leaseMs: 120 });- await firstEntered;- secondTurn = second.workOnce({ owner: "worker-b", leaseMs: 120 });- await new Promise((resolve) => setTimeout(resolve, 200));- assert.equal(promptCalls, 1,- "the second Worker cannot enter after the original capacity expiry while its peer keeps heartbeating");- const tickets = (await first.queue()).capacity;- assert.equal(tickets.length, 2, "the active command and its blocked peer are both durable tickets");- assert.equal(tickets.filter((item) => item.active !== undefined).length, 1);- release?.();- await Promise.all([firstTurn, secondTurn]);- assert.equal(promptCalls, 1, "a capacity miss releases its Build instead of waiting inside the Worker");- await second.workOnce({ owner: "worker-b", leaseMs: 120 });- assert.equal(promptCalls, 2);+ await firstRuntime.close();} finally {- release?.();- await Promise.allSettled([firstTurn, secondTurn].filter((item): item is Promise<unknown> => item !== undefined));- await second?.close();- await first?.close();await rm(directory, { recursive: true, force: true });}});@@ -538,12 +367,12 @@ test("project local runtime accepts components loaded from an installed package"});const result = await runtime.build({id: "selected-preview",- state: createGreetingBuild({+ definition: buildDefinition(createGreetingBuild({generationRealization: "placeholder",- }),+ })),});assert.equal(result.status, "queued");- const completed = await runtime.workOnce({ owner: "selected-test", leaseMs: 5_000 });+ const completed = await runtime.workOnce();assert.equal(completed?.terminal, "complete");assert.equal((await runtime.status("selected-preview")).build?.state.records.some((record) => record.id === "document:root"), true);@@ -579,8 +408,8 @@ test("project local runtime uses only the explicitly selected Scheduler", asyncinfrastructure: [...base.infrastructure, scheduler],roles: { ...base.roles, scheduler: { from: "custom-scheduler", part: "scheduler" } },});- await runtime.build({ id: "scheduler-selection", state: createGreetingBuild() });- await runtime.workOnce({ owner: "scheduler-test", leaseMs: 5_000 });+ await runtime.build({ id: "scheduler-selection", definition: buildDefinition(createGreetingBuild()) });+ await runtime.workOnce();assert.equal(creates, 1);await runtime.close();} finally {@@ -616,8 +445,8 @@ test("two installed Schedulers are unambiguous because the Profile selects one",infrastructure: [...base.infrastructure, scheduler("one"), scheduler("two")],roles: { ...base.roles, scheduler: { from: "scheduler-two", part: "scheduler" } },});- await runtime.build({ id: "scheduler-two", state: createGreetingBuild() });- await runtime.workOnce({ owner: "scheduler-test", leaseMs: 5_000 });+ await runtime.build({ id: "scheduler-two", definition: buildDefinition(createGreetingBuild()) });+ await runtime.workOnce();assert.deepEqual(creates, { one: 0, two: 1 });await runtime.close();assert.deepEqual(closes, { one: 1, two: 1 });@@ -661,17 +490,17 @@ test("project local runtime accepts an explicitly selected replacement ArtifactSassert.equal(await artifactStore.has(sourceArtifact.digest), false);await runtime.build({id: "source-artifact-staging",- state: createGreetingBuild(),+ definition: buildDefinition(createGreetingBuild()),attachments: [{ artifact: sourceArtifact, open: async () => (async function* () { yield bytes; })() }],});- const blocked = await runtime.workOnce({ owner: "artifact-test", leaseMs: 5_000 });+ const blocked = await runtime.workOnce();assert.equal(blocked?.terminal, "failed");assert.match(blocked?.reason ?? "", /does not bind demanded capability/u);assert.deepEqual(await artifactStore.get(sourceArtifact.digest), bytes);let reopened = false;await runtime.build({id: "existing-source-artifact",- state: createGreetingBuild(),+ definition: buildDefinition(createGreetingBuild()),attachments: [{artifact: sourceArtifact,open: async () => {@@ -689,8 +518,8 @@ test("project local runtime accepts an explicitly selected replacement ArtifactSmediaType: "application/octet-stream",};await assert.rejects(runtime.build({- id: "tampered-source-artifact",- state: createGreetingBuild(),+ id: "mismatched-source-artifact",+ definition: buildDefinition(createGreetingBuild()),attachments: [{artifact: absentArtifact,open: async () => (async function* () { yield new Uint8Array([0]); })(),diff --git a/packages/runtime-local/test/worker-process.test.ts b/packages/runtime-local/test/worker-process.test.tsindex b3d0992b..079018f9 100644--- a/packages/runtime-local/test/worker-process.test.ts+++ b/packages/runtime-local/test/worker-process.test.ts@@ -1,5 +1,5 @@import assert from "node:assert/strict";-import { appendFile, mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";+import { mkdtemp, mkdir, readFile, rm, writeFile } from "node:fs/promises";import { tmpdir } from "node:os";import { join } from "node:path";import test from "node:test";@@ -11,7 +11,7 @@ import {stopRuntimeProcess,} from "../src/worker-process.js";-test("one detached Runtime Worker is observable, reusable and explicitly stoppable", async () => {+test("one detached Runtime Worker can be started, observed and stopped", async () => {const root = await mkdtemp(join(tmpdir(), "narratage-runtime-process-"));const profile = join(root, "runtime.json");const dataRoot = join(root, ".narratage", "runtimes", "local");@@ -31,40 +31,27 @@ test("one detached Runtime Worker is observable, reusable and explicitly stoppab`;try {const first = await ensureRuntimeProcess(- profile, dataRoot, { command: process.execPath, args: ["-e", program] }, "revision-1", 5_000);+ profile,+ dataRoot,+ { command: process.execPath, args: ["-e", program] },+ 5_000,+ );assert.equal(first.state, "running");assert.ok(first.pid);- assert.equal(await readFile(join(root, ".narratage", "runtime"), "utf8"), "runtime.json\n",- "Worker state must not overwrite or descend through the active Runtime Profile pointer");- const second = await ensureRuntimeProcess(profile, dataRoot, { command: "must-not-run", args: [] }, "revision-1", 5_000);+ assert.equal(await readFile(join(root, ".narratage", "runtime"), "utf8"), "runtime.json\n");++ const second = await ensureRuntimeProcess(+ profile,+ dataRoot,+ { command: "must-not-run", args: [] },+ 5_000,+ );assert.equal(second.pid, first.pid);- const concurrent = await Promise.all(Array.from({ length: 8 }, async () =>- await ensureRuntimeProcess(profile, dataRoot, { command: "must-not-run", args: [] }, "revision-1", 5_000)));- assert.deepEqual([...new Set(concurrent.map((item) => item.pid))], [first.pid],- "concurrent clients share one atomically launched Worker");- assert.equal((await runtimeProcessStatus(profile, dataRoot, "revision-1")).state, "running");+ assert.equal((await runtimeProcessStatus(profile, dataRoot)).state, "running");assert.match((await runtimeProcessLogs(dataRoot)).text, /worker-ready/u);- const logPath = (await runtimeProcessLogs(dataRoot)).path;- await appendFile(logPath, `${"x".repeat(1024 * 1024 + 64)}\ntail-marker\n`, "utf8");- const bounded = await runtimeProcessLogs(dataRoot);- assert.ok(Buffer.byteLength(bounded.text) <= 1024 * 1024);- assert.match(bounded.text, /tail-marker/u);- assert.equal((await runtimeProcessStatus(profile, dataRoot, "revision-2")).state, "stale");- const replaced = await ensureRuntimeProcess(- profile, dataRoot, { command: process.execPath, args: ["-e", program] }, "revision-2", 5_000);- assert.notEqual(replaced.pid, first.pid);- assert.equal(replaced.state, "running");- await writeFile(profile, JSON.stringify({- format: "narratage.runtime-profile@1",- runtime: { use: "example.runtime" },- }), "utf8");- assert.equal((await runtimeProcessStatus(profile, dataRoot, "revision-3")).state, "stale");- const replacedAgain = await ensureRuntimeProcess(- profile, dataRoot, { command: process.execPath, args: ["-e", program] }, "revision-3", 5_000);- assert.notEqual(replacedAgain.pid, replaced.pid);- assert.equal(replacedAgain.state, "running");+assert.equal((await stopRuntimeProcess(profile, dataRoot, 5_000)).state, "stopped");- assert.equal((await runtimeProcessStatus(profile, dataRoot, "revision-3")).state, "stopped");+ assert.equal((await runtimeProcessStatus(profile, dataRoot)).state, "stopped");} finally {await stopRuntimeProcess(profile, dataRoot, 1_000).catch(() => undefined);await rm(root, { recursive: true, force: true });diff --git a/packages/runtime/README.md b/packages/runtime/README.mdindex 642b48ab..d756231f 100644--- a/packages/runtime/README.md+++ b/packages/runtime/README.md@@ -6,12 +6,12 @@ The first implementation contains:- `RuntimeCommandExecutor`: regenerate Core-authorized commands and execute one by identity;- `LocalBuildScheduler`: one authoritative, queue-free scheduler shared by multiple Builds;-- atomic generic resource claims with Runtime Profile overrides;+- in-process command limits plus durable Provider pool and capability-lane limits for remote work;- static Runtime Module facets and explicit Profile resolution;- exact Endpoint capability/return offers;-- `BuildStore` and `OperationStore` ports with compare-and-swap in-memory references;+- `BuildStore` for Definitions and appended Core Facts, plus a separate `OperationStore`;- a recoverable Endpoint lifecycle that creates an Operation before `start`, checkpoints `pending`,- calls `resume` after a restart, and records wake, retry, failure and cancellation state;+ calls `resume` after a restart, and records wake, retry and terminal state;- a narrow `CredentialStore`/`CredentialRef` port that keeps secrets out of framework facts.- one host-neutral `RuntimeInfrastructurePackage` ABI that binds configured Scheduler, BuildStore,OperationStore, ArtifactStore and CredentialStore implementations to their selected Manifest facet;@@ -22,24 +22,23 @@ Role selection grants a part one responsibility; merely being installed never grcredential authority. These remain trusted deployment packages, not author-importable modules.The Scheduler does not traverse Graphs, choose Candidates, rewrite Needs or accept arbitrary-serialized commands. Core remains the sole source of readiness and the sole Event acceptance law.-Resources only limit how many already-authorized commands execute concurrently. Provider Endpoints-claim both an explicit Provider pool and an exact capability lane; the Store acquires them atomically.+serialized commands. Core remains the sole source of readiness and the sole Command-result admission law.+Resources only limit how many already-authorized commands execute concurrently. Recoverable Provider+Endpoints occupy both an explicit Provider pool and an exact capability lane while remote work is in flight.An Endpoint's vendor-side job system remains internal and cannot advance another Build step.-`MemoryBuildStore` stores verified BuildState only. `MemoryOperationStore` separately records a+`MemoryBuildStore` stores one Definition and its admitted Facts. `MemoryOperationStore` separately records astable attempt identity, pending checkpoint and terminal result. The Operation id is alsothe stable key supplied to an upstream system when it supports idempotent submission.Changing the Build, Command, selected Endpoint or attempt changes that identity.Nothing in the OperationStore becomes an accepted domain fact until an Endpoint completion is-validated and reduced by Core. If a process stops after completion is persisted but before Core-accepts its Event, the next run reconstructs the same Event from the stored completion and does not+validated by Core and appended to BuildStore. If a process stops after completion is persisted but before Core+accepts it, the next Worker reconstructs the same result from the stored completion and does notcall the Endpoint again.This package is an environment-neutral reference, not a durable queue. SQLite/filesystem adapters,environment credentials, the local follow loop, Endpoint implementations and`@narratage/endpoint-kit` live in separate packages. The repository already contains KIE, Vertex and-several local media and external-program Endpoint packages; distributed attempt leases and additional hosted-adapters remain deployment work. An Endpoint whose upstream API cannot look up or deduplicate the+several local media and external-program Endpoint packages. An Endpoint whose upstream API cannot look up or deduplicate thesupplied Operation id cannot promise exactly-once remote work across the crash window; its`resume(undefined)` must explicitly reconcile that uncertainty.diff --git a/packages/runtime/src/catalog.ts b/packages/runtime/src/catalog.tsindex 8a4d1cad..64cbdf6d 100644--- a/packages/runtime/src/catalog.ts+++ b/packages/runtime/src/catalog.ts@@ -1,6 +1,4 @@-import { canonicalStringify, isDigest } from "@narratage/protocol";import type {- Digest,LogicalOutputRef,RecordRef,} from "@narratage/protocol";@@ -12,10 +10,8 @@ export type BuildCatalogAlias = {export type BuildCatalogDescriptor = {readonly format: "narratage.build-catalog-descriptor@1";- readonly core: Digest;readonly source: {readonly path: string;- readonly closure: Digest;};readonly run?: {readonly path: string;@@ -26,7 +22,6 @@ export type BuildCatalogDescriptor = {export type BuildCatalogEntry = BuildCatalogDescriptor & {readonly build: string;readonly createdAt: number;- readonly updatedAt: number;};/** Host presentation index only. It is never Build truth or part of Runtime Closure. */@@ -42,9 +37,7 @@ function assert(condition: unknown, message: string): asserts condition {export function verifyBuildCatalogDescriptor(descriptor: BuildCatalogDescriptor): void {assert(descriptor.format === "narratage.build-catalog-descriptor@1", "Build Catalog descriptor format is invalid");- assert(isDigest(descriptor.core), "Build Catalog Core identity is invalid");assert(descriptor.source.path.trim().length > 0, "Build Catalog source path is empty");- assert(isDigest(descriptor.source.closure), "Build Catalog source closure is invalid");if (descriptor.run !== undefined) {assert(descriptor.run.path.trim().length > 0, "Build Catalog Run path is empty");}@@ -63,32 +56,12 @@ export function verifyBuildCatalogEntry(entry: BuildCatalogEntry): void {verifyBuildCatalogDescriptor(entry);assert(entry.build.trim().length > 0, "Build Catalog build id is empty");assert(Number.isSafeInteger(entry.createdAt) && entry.createdAt >= 0, "Build Catalog createdAt is invalid");- assert(Number.isSafeInteger(entry.updatedAt) && entry.updatedAt >= entry.createdAt,- "Build Catalog updatedAt is invalid");}function copy<T>(value: T): T {return structuredClone(value);}-function descriptorValue(descriptor: BuildCatalogDescriptor): BuildCatalogDescriptor {- return {- format: descriptor.format,- core: descriptor.core,- source: descriptor.source,- ...(descriptor.run === undefined ? {} : { run: descriptor.run }),- aliases: descriptor.aliases,- };-}--/** One Build id keeps the exact Host presentation under which it was first submitted. */-export function sameBuildCatalogDescriptor(- left: BuildCatalogDescriptor,- right: BuildCatalogDescriptor,-): boolean {- return canonicalStringify(descriptorValue(left)) === canonicalStringify(descriptorValue(right));-}-export class MemoryBuildCatalog implements BuildCatalog {readonly #entries = new Map<string, BuildCatalogEntry>();readonly #now: () => number;@@ -100,19 +73,13 @@ export class MemoryBuildCatalog implements BuildCatalog {async record(build: string, descriptor: BuildCatalogDescriptor): Promise<BuildCatalogEntry> {assert(build.trim().length > 0, "Build Catalog build id is empty");verifyBuildCatalogDescriptor(descriptor);- const existing = this.#entries.get(build);- if (existing !== undefined) {- assert(sameBuildCatalogDescriptor(existing, descriptor),- `Build Catalog ${build} already has another source, Run Source or output naming`);- return copy(existing);- }+ assert(!this.#entries.has(build), `Build Catalog ${build} already exists`);const now = this.#now();assert(Number.isSafeInteger(now) && now >= 0, "Build Catalog clock returned an invalid time");const entry: BuildCatalogEntry = {...copy(descriptor),build,createdAt: now,- updatedAt: now,};verifyBuildCatalogEntry(entry);this.#entries.set(build, entry);diff --git a/packages/runtime/src/dispatch.ts b/packages/runtime/src/dispatch.tsindex f2509cdd..271a0d07 100644--- a/packages/runtime/src/dispatch.ts+++ b/packages/runtime/src/dispatch.ts@@ -1,36 +1,16 @@-import { digestOf, isDigest } from "@narratage/protocol";-import type { Digest } from "@narratage/protocol";--export type DispatchAdmission = "open" | "closing" | "closed";-export type DispatchPhase = "queued" | "leased" | "waiting" | "blocked" | "settling" | "terminal";+export type DispatchPhase = "queued" | "running" | "waiting" | "blocked" | "terminal";export type DispatchTerminal = "complete" | "failed" | "cancelled";-export type DispatchLease = {- readonly owner: string;- readonly token: string;- /** Monotonic fencing value. A stale owner can never commit with an older fence. */- readonly fence: number;- readonly expiresAt: number;-};-export type BuildDispatchIdentity = {- readonly format: "narratage.build-dispatch-identity@1";- readonly id: Digest;readonly build: string;- readonly core: Digest;- /** Installed compute packages required to execute this Build. */+ /** Compute packages the Worker loads for this Build. */readonly implementationPackages: readonly string[];};export type BuildDispatchSnapshot = BuildDispatchIdentity & {- readonly revision: number;readonly createdAt: number;- readonly updatedAt: number;- readonly priority: number;readonly availableAt: number;- readonly admission: DispatchAdmission;readonly phase: DispatchPhase;- readonly lease?: DispatchLease;readonly reason?: string;readonly cancellation?: {readonly requestedAt: number;@@ -39,51 +19,32 @@ export type BuildDispatchSnapshot = BuildDispatchIdentity & {readonly terminal?: DispatchTerminal;};-export type BuildDispatchCreate =- | { readonly status: "created"; readonly snapshot: BuildDispatchSnapshot }- | { readonly status: "existing"; readonly snapshot: BuildDispatchSnapshot };--export type BuildDispatchClaim = {- readonly owner: string;- readonly token: string;- readonly now: number;- readonly leaseMs: number;-};-export type BuildDispatchRelease = {- readonly phase: "queued" | "waiting" | "blocked" | "settling";+ readonly phase: "queued" | "waiting" | "blocked";readonly availableAt: number;readonly reason?: string;};export type DispatchQuery = {readonly phases?: readonly DispatchPhase[];- readonly admission?: DispatchAdmission;};export const nonTerminalDispatchPhases = ["queued",- "leased",+ "running","waiting","blocked",- "settling",] as const satisfies readonly DispatchPhase[];-export type CapacityMode = "active" | "recoverable";--export type CapacityLimits = {- readonly globalActive: number;-};-export type CapacityResourceClaim = {readonly id: string;readonly maxActive: number;readonly maxInFlight: number;};+/** One asynchronous external Operation that currently occupies Provider capacity. */export type CapacityReservation = {- readonly format: "narratage.capacity-reservation@1";- readonly id: Digest;+ readonly id: string;readonly build: string;readonly command: string;readonly resources: readonly CapacityResourceClaim[];@@ -91,11 +52,7 @@ export type CapacityReservation = {readonly pool: string;readonly lane: string;};- readonly mode: CapacityMode;- readonly inFlight: boolean;- readonly active?: DispatchLease;readonly createdAt: number;- readonly updatedAt: number;};export type CapacityAcquireRequest = {@@ -103,52 +60,34 @@ export type CapacityAcquireRequest = {readonly command: string;readonly resources: readonly CapacityResourceClaim[];readonly queue?: CapacityReservation["queue"];- readonly mode: CapacityMode;- /** Current Build pool; a stale Worker cannot reserve capacity after being fenced. */- readonly buildLease: DispatchLease;- readonly owner: string;- readonly token: string;readonly now: number;- readonly leaseMs: number;- readonly limits: CapacityLimits;};export type CapacityAcquire =| { readonly status: "acquired"; readonly reservation: CapacityReservation }| {readonly status: "blocked";- readonly retryAt: number;- readonly reason: "global-active" | "resource-active" | "resource-in-flight";- readonly resource?: string;+ readonly availableAt: number;+ readonly reason: "resource-in-flight";+ readonly resource: string;};-/**- * Durable execution control for Build identities, leases and shared capacity.- *- * It deliberately stores no Core Command body. `command` is only the identity regenerated from- * verified BuildState by the Worker. Provider checkpoints remain in OperationStore.- */+/** Durable queue state. Process ownership belongs to the Runtime Host, not each Build row. */export type BuildDispatchStore = {- create(identity: BuildDispatchIdentity, options?: { readonly priority?: number; readonly availableAt?: number; readonly now?: number }): Promise<BuildDispatchCreate>;+ create(identity: BuildDispatchIdentity, options?: {+ readonly now?: number;+ }): Promise<BuildDispatchSnapshot>;read(build: string): Promise<BuildDispatchSnapshot | undefined>;list(query?: DispatchQuery): Promise<readonly BuildDispatchSnapshot[]>;- claim(request: BuildDispatchClaim): Promise<BuildDispatchSnapshot | undefined>;- heartbeat(build: string, lease: DispatchLease, now: number, leaseMs: number): Promise<BuildDispatchSnapshot>;- release(build: string, lease: DispatchLease, update: BuildDispatchRelease, now?: number): Promise<BuildDispatchSnapshot>;- finish(build: string, lease: DispatchLease, terminal: DispatchTerminal, reason?: string, now?: number): Promise<BuildDispatchSnapshot>;- /**- * Close future admission for one Build. A never-claimed queued Build becomes cancelled in the- * same atomic store transaction; an already-claimed Build is reconciled by its fenced Worker.- */+ /** Claim one ready Build for the Runtime's single Worker process. */+ claim(now?: number): Promise<BuildDispatchSnapshot | undefined>;+ release(build: string, update: BuildDispatchRelease): Promise<BuildDispatchSnapshot>;+ finish(build: string, terminal: DispatchTerminal, reason?: string): Promise<BuildDispatchSnapshot>;requestCancellation(build: string, reason?: string, now?: number): Promise<BuildDispatchSnapshot>;- /** Make non-terminal work immediately claimable without changing admission or creative intent. */wake(build: string, now?: number): Promise<BuildDispatchSnapshot>;acquireCapacity(request: CapacityAcquireRequest): Promise<CapacityAcquire>;- heartbeatCapacity(id: Digest, lease: DispatchLease, now: number, leaseMs: number): Promise<CapacityReservation>;- parkCapacity(id: Digest, lease: DispatchLease, inFlight: boolean, now?: number): Promise<CapacityReservation>;- releaseCapacity(id: Digest, lease: DispatchLease, now?: number): Promise<void>;- /** Remove a parked reservation only while holding the owning Build's current fenced lease. */- clearCapacity(id: Digest, build: string, buildLease: DispatchLease): Promise<void>;+ releaseCapacity(id: string): Promise<void>;+ releaseBuildCapacity(build: string): Promise<void>;listCapacity(): Promise<readonly CapacityReservation[]>;};@@ -156,88 +95,17 @@ function assert(condition: unknown, message: string): asserts condition {if (!condition) throw new Error(message);}-function safeNonNegative(value: number, subject: string): number {- assert(Number.isSafeInteger(value) && value >= 0, `${subject} must be a non-negative safe integer`);- return value;-}--function positive(value: number, subject: string): number {- assert(Number.isSafeInteger(value) && value > 0, `${subject} must be a positive safe integer`);- return value;-}-export function createBuildDispatchIdentity(input: {readonly build: string;- readonly core: Digest;readonly implementationPackages?: readonly string[];}): BuildDispatchIdentity {assert(input.build.trim().length > 0, "Build Dispatch build id is empty");- assert(isDigest(input.core), "Build Dispatch Core digest is invalid");const implementationPackages = [...new Set(input.implementationPackages ?? [])].sort();assert(implementationPackages.every((item) => item.trim().length > 0), "Build Dispatch implementation package is empty");- const content = {- format: "narratage.build-dispatch-identity@1" as const,- build: input.build,- core: input.core,- implementationPackages,- };- return { ...content, id: digestOf(content) };-}--export function verifyBuildDispatchIdentity(value: BuildDispatchIdentity): void {- const expected = createBuildDispatchIdentity(value);- assert(value.format === expected.format && value.id === expected.id, "Build Dispatch identity differs");-}--export function verifyDispatchLease(value: DispatchLease, subject = "Dispatch lease"): void {- assert(value.owner.trim().length > 0, `${subject} owner is empty`);- assert(value.token.trim().length > 0, `${subject} token is empty`);- positive(value.fence, `${subject} fence`);- safeNonNegative(value.expiresAt, `${subject} expiry`);+ return { build: input.build, implementationPackages };}-export function verifyBuildDispatchSnapshot(value: BuildDispatchSnapshot): void {- verifyBuildDispatchIdentity(value);- safeNonNegative(value.revision, "Build Dispatch revision");- safeNonNegative(value.createdAt, "Build Dispatch createdAt");- safeNonNegative(value.updatedAt, "Build Dispatch updatedAt");- assert(Number.isSafeInteger(value.priority), "Build Dispatch priority must be a safe integer");- safeNonNegative(value.availableAt, "Build Dispatch availableAt");- assert(["open", "closing", "closed"].includes(value.admission), "Build Dispatch admission is invalid");- assert(["queued", "leased", "waiting", "blocked", "settling", "terminal"].includes(value.phase), "Build Dispatch phase is invalid");- if (value.lease !== undefined) verifyDispatchLease(value.lease);- assert((value.phase === "leased") === (value.lease !== undefined), "only a leased Dispatch may carry a lease");- assert((value.phase === "terminal") === (value.terminal !== undefined), "only a terminal Dispatch may carry a terminal outcome");- if (value.phase === "terminal") assert(value.admission === "closed", "terminal Dispatch admission must be closed");- if (value.cancellation !== undefined) safeNonNegative(value.cancellation.requestedAt, "cancellation requestedAt");-}--export function capacityReservationId(build: string, command: string): Digest {+export function capacityReservationId(build: string, command: string): string {assert(build.trim().length > 0 && command.trim().length > 0, "Capacity reservation identity is empty");- return digestOf({ format: "narratage.capacity-reservation-identity@1", build, command });-}--export function verifyCapacityLimits(value: CapacityLimits): void {- positive(value.globalActive, "global active capacity");-}--export function verifyCapacityReservation(value: CapacityReservation): void {- assert(value.format === "narratage.capacity-reservation@1", "Capacity reservation format is unsupported");- assert(value.id === capacityReservationId(value.build, value.command), "Capacity reservation identity differs");- assert(value.resources.length > 0, "Capacity reservation resources are empty");- const resources = value.resources.map((resource) => {- assert(resource.id.trim().length > 0, "Capacity resource id is empty");- positive(resource.maxActive, `Capacity resource ${resource.id} active limit`);- positive(resource.maxInFlight, `Capacity resource ${resource.id} in-flight limit`);- return resource.id;- });- assert(new Set(resources).size === resources.length, "Capacity reservation repeats a resource");- if (value.queue !== undefined) {- assert(value.queue.pool.trim().length > 0, "Capacity queue pool is empty");- assert(value.queue.lane.trim().length > 0, "Capacity queue lane is empty");- }- assert(value.mode === "active" || value.mode === "recoverable", "Capacity reservation mode is invalid");- if (value.active !== undefined) verifyDispatchLease(value.active, "Capacity lease");- safeNonNegative(value.createdAt, "Capacity reservation createdAt");- safeNonNegative(value.updatedAt, "Capacity reservation updatedAt");+ return `capacity:${encodeURIComponent(build)}:${encodeURIComponent(command)}`;}diff --git a/packages/runtime/src/index.ts b/packages/runtime/src/index.tsindex ec183d68..84660dab 100644--- a/packages/runtime/src/index.ts+++ b/packages/runtime/src/index.ts@@ -2,7 +2,6 @@ export { plannedNeeds } from "@narratage/core";export { MemoryBuildStore } from "./memory.js";export {MemoryBuildCatalog,- sameBuildCatalogDescriptor,verifyBuildCatalogDescriptor,verifyBuildCatalogEntry,} from "./catalog.js";@@ -19,19 +18,11 @@ export {capacityReservationId,createBuildDispatchIdentity,nonTerminalDispatchPhases,- verifyBuildDispatchIdentity,- verifyBuildDispatchSnapshot,- verifyCapacityLimits,- verifyCapacityReservation,- verifyDispatchLease,} from "./dispatch.js";export type * from "./dispatch.js";export {MemoryOperationStore,- operationCancellationRequestId,sealOperationIdentity,- verifyOperationIdentity,- verifyOperationSnapshot,} from "./operations.js";export type * from "./operations.js";export {diff --git a/packages/runtime/src/infrastructure.ts b/packages/runtime/src/infrastructure.tsindex 6e1e3b81..f5f49549 100644--- a/packages/runtime/src/infrastructure.ts+++ b/packages/runtime/src/infrastructure.ts@@ -138,18 +138,18 @@ function verifyPartPort(part: RuntimePart): void {case "build-store":callable(part.port, "create", part.instance.id);callable(part.port, "read", part.instance.id);- callable(part.port, "compareAndSwap", part.instance.id);+ callable(part.port, "append", part.instance.id);break;case "operation-store":callable(part.port, "create", part.instance.id);callable(part.port, "read", part.instance.id);callable(part.port, "list", part.instance.id);- callable(part.port, "compareAndSwap", part.instance.id);+ callable(part.port, "update", part.instance.id);break;case "dispatch-store":for (const method of [- "create", "read", "list", "claim", "heartbeat", "release", "finish", "requestCancellation", "wake",- "acquireCapacity", "heartbeatCapacity", "parkCapacity", "releaseCapacity", "clearCapacity", "listCapacity",+ "create", "read", "list", "claim", "release", "finish", "requestCancellation", "wake",+ "acquireCapacity", "releaseCapacity", "releaseBuildCapacity", "listCapacity",]) callable(part.port, method, part.instance.id);break;case "artifact-store":diff --git a/packages/runtime/src/memory.ts b/packages/runtime/src/memory.tsindex 5b1eab4c..a83f5eda 100644--- a/packages/runtime/src/memory.ts+++ b/packages/runtime/src/memory.ts@@ -1,61 +1,59 @@-import type { BuildState } from "@narratage/protocol";+import { materializeBuild } from "@narratage/core";+import type { BuildDefinition, BuildFact } from "@narratage/protocol";import type {BuildSnapshot,BuildStore,- BuildStoreWrite,} from "./types.js";-function copy(snapshot: BuildSnapshot): BuildSnapshot {- return structuredClone(snapshot);+type StoredBuild = {+ readonly build: string;+ readonly definition: BuildDefinition;+ readonly facts: readonly BuildFact[];+};++function snapshotBuild(stored: StoredBuild): BuildSnapshot {+ const definition = structuredClone(stored.definition);+ const facts = structuredClone(stored.facts);+ return {+ build: stored.build,+ definition,+ facts,+ state: materializeBuild(definition, facts),+ };}-function durableState(state: BuildState): BuildState {- return { ...structuredClone(state), outstanding: [] };-}--/** Reference CAS Store for tests and one-process local Builds. It does not persist Operations. */+/** Reference Fact Store for tests and one-process local Builds. It does not persist Operations. */export class MemoryBuildStore implements BuildStore {- readonly #builds = new Map<string, BuildSnapshot>();+ readonly #builds = new Map<string, StoredBuild>();- async create(build: string, state: BuildState): Promise<BuildSnapshot> {+ async create(build: string, definition: BuildDefinition): Promise<BuildSnapshot> {if (build.trim().length === 0) throw new Error("build id must not be empty");if (this.#builds.has(build)) throw new Error(`build ${build} already exists`);- const snapshot = { build, revision: 0, state: durableState(state) };- this.#builds.set(build, snapshot);- return copy(snapshot);+ const stored = { build, definition: structuredClone(definition), facts: [] };+ this.#builds.set(build, stored);+ return snapshotBuild(stored);}async read(build: string): Promise<BuildSnapshot | undefined> {const snapshot = this.#builds.get(build);- return snapshot === undefined ? undefined : copy(snapshot);+ return snapshot === undefined ? undefined : snapshotBuild(snapshot);}async list(): Promise<readonly BuildSnapshot[]> {return [...this.#builds.values()].sort((left, right) => left.build.localeCompare(right.build))- .map(copy);+ .map(snapshotBuild);}- async compareAndSwap(- build: string,- expectedRevision: number,- state: BuildState,- ): Promise<BuildStoreWrite> {- if (!Number.isSafeInteger(expectedRevision) || expectedRevision < 0) {- throw new Error("expected revision must be a non-negative safe integer");- }+ async append(build: string, fact: BuildFact): Promise<void> {const current = this.#builds.get(build);if (current === undefined) throw new Error(`build ${build} does not exist`);- if (current.revision !== expectedRevision) {- return { status: "conflict", current: copy(current) };- }- const snapshot = {+ const stored = {build,- revision: current.revision + 1,- state: durableState(state),+ definition: current.definition,+ facts: [...current.facts, structuredClone(fact)],};- this.#builds.set(build, snapshot);- return { status: "stored", snapshot: copy(snapshot) };+ this.#builds.set(build, stored);}}diff --git a/packages/runtime/src/operations.ts b/packages/runtime/src/operations.tsindex c3e79446..ba5d3a53 100644--- a/packages/runtime/src/operations.ts+++ b/packages/runtime/src/operations.ts@@ -1,19 +1,15 @@-import { digestOf, isDigest } from "@narratage/protocol";import type {CanonicalValue,- Digest,StoredValue,} from "@narratage/protocol";export type OperationIdentity = {- readonly format: "narratage.operation-identity@1";- readonly id: Digest;+ readonly id: string;readonly build: string;readonly command: string;readonly endpoint: string;readonly pool: string;readonly lane: string;- readonly attempt: number;};export type OperationCompletion = {@@ -23,24 +19,12 @@ export type OperationCompletion = {export type OperationFailure = {readonly code: string;readonly message: string;- readonly retryable: boolean;- /** Earliest epoch millisecond at which Runtime policy may create another attempt. */- readonly retryAt?: number;-};--export type OperationCancellationControl = {- readonly requestedAt: number;- readonly requestId: Digest;- readonly status: "requested" | "accepted" | "confirmed" | "unsupported" | "too-late";- readonly attempts: number;- readonly retryAt?: number;- readonly lastError?: { readonly code: string; readonly message: string };};/**- * Provider-neutral, human-observable progress for one recoverable Operation.+ * Provider-neutral, human-observable progress for one asynchronous Operation.*- * The opaque checkpoint remains the Provider's private recovery state. This+ * The opaque handle remains the Provider's private task state. This* deliberately small projection is the only part Runtime tools may display;* it must not carry credentials, request bodies or vendor-specific payloads.*/@@ -52,20 +36,18 @@ export type OperationProgress = {};export type OperationSnapshot = OperationIdentity & {- readonly revision: number;- readonly status: "created" | "pending" | "completed" | "failed" | "cancelled";- readonly checkpoint?: CanonicalValue;+ readonly status: OperationUpdate["status"];+ readonly handle?: CanonicalValue;readonly wakeAt?: number;readonly progress?: OperationProgress;readonly completion?: OperationCompletion;readonly failure?: OperationFailure;- readonly cancellation?: OperationCancellationControl;};export type OperationUpdate =| {readonly status: "pending";- readonly checkpoint: CanonicalValue;+ readonly handle: CanonicalValue;readonly wakeAt?: number;readonly progress?: OperationProgress;}@@ -74,16 +56,7 @@ export type OperationUpdate =readonly completion: OperationCompletion;}| { readonly status: "failed"; readonly failure: OperationFailure }- | { readonly status: "cancelled"; readonly cancellation: OperationCancellationControl }- | { readonly status: "control"; readonly cancellation: OperationCancellationControl };--export type OperationCreate =- | { readonly status: "created"; readonly snapshot: OperationSnapshot }- | { readonly status: "existing"; readonly snapshot: OperationSnapshot };--export type OperationStoreWrite =- | { readonly status: "stored"; readonly snapshot: OperationSnapshot }- | { readonly status: "conflict"; readonly current: OperationSnapshot };+ | { readonly status: "cancelled" };export type OperationQuery = {readonly build?: string;@@ -94,155 +67,44 @@ export type OperationQuery = {};export type OperationStore = {- create(identity: OperationIdentity): Promise<OperationCreate>;- read(id: Digest): Promise<OperationSnapshot | undefined>;+ create(operation: OperationSnapshot): Promise<OperationSnapshot>;+ read(id: string): Promise<OperationSnapshot | undefined>;list(query: OperationQuery): Promise<readonly OperationSnapshot[]>;- compareAndSwap(id: Digest, expectedRevision: number, update: OperationUpdate): Promise<OperationStoreWrite>;+ /** One local Worker owns execution. A terminal Operation is returned unchanged. */+ update(id: string, update: OperationUpdate): Promise<OperationSnapshot>;};function assert(condition: unknown, message: string): asserts condition {if (!condition) throw new Error(message);}-function positiveInteger(value: number, subject: string): number {- assert(Number.isSafeInteger(value) && value > 0, `${subject} must be a positive safe integer`);- return value;-}--function epochMillisecond(value: number, subject: string): number {- assert(Number.isSafeInteger(value) && value >= 0, `${subject} must be a non-negative epoch millisecond`);- return value;-}--function identityContent(value: Omit<OperationIdentity, "id">): Omit<OperationIdentity, "id"> {- return {- format: "narratage.operation-identity@1",- build: value.build,- command: value.command,- endpoint: value.endpoint,- pool: value.pool,- lane: value.lane,- attempt: value.attempt,- };-}-export function sealOperationIdentity(- value: Omit<OperationIdentity, "format" | "id">,+ value: OperationIdentity,): OperationIdentity {+ assert(value.id.trim().length > 0, "Operation id is empty");assert(value.build.trim().length > 0, "Operation build id is empty");assert(value.command.trim().length > 0, "Operation command id is empty");assert(value.endpoint.trim().length > 0, "Operation Endpoint id is empty");assert(value.pool.trim().length > 0, "Operation Provider Pool is empty");assert(value.lane.trim().length > 0, "Operation Capability Lane is empty");- positiveInteger(value.attempt, "Operation attempt");- const content = identityContent({- format: "narratage.operation-identity@1",- ...value,- });- return { ...content, id: digestOf(content) };-}--export function verifyOperationIdentity(identity: OperationIdentity): void {- const expected = sealOperationIdentity(identity);- assert(identity.format === expected.format- && identity.id === expected.id, "Operation identity differs");-}--export function verifyOperationSnapshot(snapshot: OperationSnapshot): void {- verifyOperationIdentity(snapshot);- assert(Number.isSafeInteger(snapshot.revision) && snapshot.revision >= 0, "Operation revision is invalid");- const presence = [snapshot.checkpoint !== undefined, snapshot.completion !== undefined, snapshot.failure !== undefined]- .filter(Boolean).length;- if (snapshot.status === "created") assert(presence === 0, "created Operation carries mutable result state");- if (snapshot.status === "pending") {- assert(snapshot.checkpoint !== undefined && presence === 1, "pending Operation requires only a checkpoint");- if (snapshot.wakeAt !== undefined) epochMillisecond(snapshot.wakeAt, "Operation wakeAt");- if (snapshot.progress !== undefined) {- assert(snapshot.progress.phase.trim().length > 0, "Operation progress phase is empty");- if (snapshot.progress.completed !== undefined) {- assert(Number.isFinite(snapshot.progress.completed) && snapshot.progress.completed >= 0,- "Operation progress completed value is invalid");- }- if (snapshot.progress.total !== undefined) {- assert(Number.isFinite(snapshot.progress.total) && snapshot.progress.total >= 0,- "Operation progress total value is invalid");- }- if (snapshot.progress.completed !== undefined && snapshot.progress.total !== undefined) {- assert(snapshot.progress.completed <= snapshot.progress.total,- "Operation progress exceeds its total");- }- if (snapshot.progress.unit !== undefined) {- assert(snapshot.progress.unit.trim().length > 0, "Operation progress unit is empty");- }- }- } else {- assert(snapshot.wakeAt === undefined, `${snapshot.status} Operation cannot carry wakeAt`);- assert(snapshot.progress === undefined, `${snapshot.status} Operation cannot carry progress`);- }- if (snapshot.status === "completed") {- assert(snapshot.completion !== undefined && presence === 1, "completed Operation requires only completion");- }- if (snapshot.status === "failed") {- assert(snapshot.failure !== undefined && presence === 1, "failed Operation requires only failure");- assert(snapshot.failure.code.trim().length > 0 && snapshot.failure.message.trim().length > 0, "Operation failure is invalid");- if (snapshot.failure.retryAt !== undefined) {- assert(snapshot.failure.retryable, "non-retryable Operation failure cannot carry retryAt");- epochMillisecond(snapshot.failure.retryAt, "Operation failure retryAt");- }- }- if (snapshot.status === "cancelled") {- assert(presence === 0, "cancelled Operation cannot carry execution result state");- assert(snapshot.cancellation?.status === "confirmed", "cancelled Operation requires confirmed cancellation");- }- if (snapshot.cancellation !== undefined) verifyOperationCancellationControl(snapshot.id, snapshot.cancellation);-}--export function operationCancellationRequestId(operation: Digest, requestedAt: number): Digest {- epochMillisecond(requestedAt, "Operation cancellation requestedAt");- return digestOf({ format: "narratage.operation-cancellation-request@1", operation, requestedAt });-}--function verifyOperationCancellationControl(- operation: Digest,- control: OperationCancellationControl,-): void {- epochMillisecond(control.requestedAt, "Operation cancellation requestedAt");- assert(control.requestId === operationCancellationRequestId(operation, control.requestedAt),- "Operation cancellation request identity differs");- assert(["requested", "accepted", "confirmed", "unsupported", "too-late"].includes(control.status),- "Operation cancellation status is invalid");- assert(Number.isSafeInteger(control.attempts) && control.attempts >= 0,- "Operation cancellation attempts is invalid");- if (control.retryAt !== undefined) epochMillisecond(control.retryAt, "Operation cancellation retryAt");- if (control.lastError !== undefined) {- assert(control.lastError.code.trim().length > 0 && control.lastError.message.trim().length > 0,- "Operation cancellation error is invalid");- }+ return { ...value };}function copy(snapshot: OperationSnapshot): OperationSnapshot {return structuredClone(snapshot);}-/** In-process reference Store. Durable/distributed adapters must preserve the same CAS law. */+/** In-process reference Store. */export class MemoryOperationStore implements OperationStore {- readonly #operations = new Map<Digest, OperationSnapshot>();+ readonly #operations = new Map<string, OperationSnapshot>();- async create(identity: OperationIdentity): Promise<OperationCreate> {- verifyOperationIdentity(identity);- const existing = this.#operations.get(identity.id);- if (existing !== undefined) return { status: "existing", snapshot: copy(existing) };- const snapshot: OperationSnapshot = {- ...structuredClone(identity),- revision: 0,- status: "created",- };- verifyOperationSnapshot(snapshot);- this.#operations.set(snapshot.id, snapshot);- return { status: "created", snapshot: copy(snapshot) };+ async create(operation: OperationSnapshot): Promise<OperationSnapshot> {+ if (this.#operations.has(operation.id)) throw new Error(`Operation ${operation.id} already exists`);+ this.#operations.set(operation.id, copy(operation));+ return copy(operation);}- async read(id: Digest): Promise<OperationSnapshot | undefined> {+ async read(id: string): Promise<OperationSnapshot | undefined> {const snapshot = this.#operations.get(id);return snapshot === undefined ? undefined : copy(snapshot);}@@ -254,61 +116,38 @@ export class MemoryOperationStore implements OperationStore {.filter((snapshot) => query.endpoint === undefined || snapshot.endpoint === query.endpoint).filter((snapshot) => query.pool === undefined || snapshot.pool === query.pool).filter((snapshot) => query.lane === undefined || snapshot.lane === query.lane)- .sort((left, right) => left.attempt - right.attempt || left.id.localeCompare(right.id))+ .sort((left, right) => left.id.localeCompare(right.id)).map(copy);}- async compareAndSwap(- id: Digest,- expectedRevision: number,- update: OperationUpdate,- ): Promise<OperationStoreWrite> {- assert(Number.isSafeInteger(expectedRevision) && expectedRevision >= 0, "expected Operation revision is invalid");+ async update(id: string, update: OperationUpdate): Promise<OperationSnapshot> {const current = this.#operations.get(id);assert(current !== undefined, `Operation ${id} does not exist`);- if (current.revision !== expectedRevision) return { status: "conflict", current: copy(current) };- if (update.status !== "control") {- assert(current.status !== "completed" && current.status !== "failed" && current.status !== "cancelled",- `Operation ${id} is already terminal`);+ if (current.status === "completed" || current.status === "failed" || current.status === "cancelled") {+ return copy(current);}- const mutable = update.status === "control"- ? {- status: current.status,- ...(current.checkpoint === undefined ? {} : { checkpoint: structuredClone(current.checkpoint) }),- ...(current.wakeAt === undefined ? {} : { wakeAt: current.wakeAt }),- ...(current.progress === undefined ? {} : { progress: structuredClone(current.progress) }),- ...(current.completion === undefined ? {} : { completion: structuredClone(current.completion) }),- ...(current.failure === undefined ? {} : { failure: structuredClone(current.failure) }),- cancellation: structuredClone(update.cancellation),- }- : update.status === "pending"+ const mutable = update.status === "pending"? {status: "pending" as const,- checkpoint: structuredClone(update.checkpoint),- ...(update.wakeAt === undefined ? {} : { wakeAt: epochMillisecond(update.wakeAt, "Operation wakeAt") }),+ handle: structuredClone(update.handle),+ ...(update.wakeAt === undefined ? {} : { wakeAt: update.wakeAt }),...(update.progress === undefined ? {} : { progress: structuredClone(update.progress) }),}: update.status === "completed"? { status: "completed" as const, completion: structuredClone(update.completion) }: update.status === "failed"? { status: "failed" as const, failure: structuredClone(update.failure) }- : { status: "cancelled" as const, cancellation: structuredClone(update.cancellation) };+ : { status: "cancelled" as const };const snapshot: OperationSnapshot = {- format: current.format,id: current.id,build: current.build,command: current.command,endpoint: current.endpoint,pool: current.pool,lane: current.lane,- attempt: current.attempt,- revision: current.revision + 1,...mutable,- ...(update.status === "control" || update.status === "cancelled" || current.cancellation === undefined- ? {} : { cancellation: structuredClone(current.cancellation) }),};- verifyOperationSnapshot(snapshot);this.#operations.set(id, snapshot);- return { status: "stored", snapshot: copy(snapshot) };+ return copy(snapshot);}}diff --git a/packages/runtime/src/profile.ts b/packages/runtime/src/profile.tsindex bdf27efe..4dfb3fd3 100644--- a/packages/runtime/src/profile.ts+++ b/packages/runtime/src/profile.ts@@ -37,7 +37,7 @@ export type RuntimeEndpointFacet = {readonly name: string;readonly role: "capability-endpoint";readonly fulfills: readonly RuntimeCapability[];- readonly lifecycle: "immediate" | "recoverable";+ readonly lifecycle: "immediate" | "asynchronous";readonly defaultConcurrency: number;/** Provider-local lanes. When omitted, each capability inherits the Provider total. */readonly lanes?: readonly (RuntimeCapability & {@@ -99,7 +99,7 @@ export type ResolvedRuntimeEndpoint = {readonly role: "capability-endpoint";readonly facet: RuntimeFacetRef;readonly fulfills: readonly RuntimeCapability[];- readonly lifecycle: "immediate" | "recoverable";+ readonly lifecycle: "immediate" | "asynchronous";readonly credentialSlots: readonly string[];readonly pool: string;readonly maxConcurrency: number;@@ -205,7 +205,7 @@ function normalizeFacet(facet: RuntimeFacet): RuntimeFacet {.sort((left, right) => offerKey(left).localeCompare(offerKey(right)));assert(fulfills.length > 0, `${facet.name} must fulfill at least one exact capability`);assert(new Set(fulfills.map(offerKey)).size === fulfills.length, `${facet.name} repeats a capability offer`);- assert(facet.lifecycle === "immediate" || facet.lifecycle === "recoverable", `${facet.name} lifecycle is invalid`);+ assert(facet.lifecycle === "immediate" || facet.lifecycle === "asynchronous", `${facet.name} lifecycle is invalid`);const defaultConcurrency = positiveInteger(facet.defaultConcurrency, `${facet.name} defaultConcurrency`);const lanes = (facet.lanes ?? fulfills.map((item) => ({...item,@@ -436,8 +436,8 @@ export function verifyRuntimeClosure(closure: RuntimeClosure): void {}assert(new Set(closure.endpoints.map(offerKey)).size === closure.endpoints.length,"Runtime Closure repeats an Endpoint offer");- if (closure.instances.some((instance) => instance.role === "capability-endpoint" && instance.lifecycle === "recoverable")) {- assert(instances.get(closure.stores.operations)?.role === "operation-store", "recoverable Endpoints require an OperationStore");+ if (closure.instances.some((instance) => instance.role === "capability-endpoint" && instance.lifecycle === "asynchronous")) {+ assert(instances.get(closure.stores.operations)?.role === "operation-store", "asynchronous Endpoints require an OperationStore");}if (closure.instances.some((instance) => instance.role === "capability-endpoint" && instance.credentialSlots.length > 0)) {assert(closure.stores.credentials.length > 0, "credentialed Endpoints require a CredentialStore");@@ -492,7 +492,6 @@ export function runtimeEndpoint(/** Fail before execution if the selected finite BuildPlan has an unbound external requirement. */export function verifyRuntimeCoverage(closure: RuntimeClosure, state: BuildState): void {- verifyRuntimeClosure(closure);const offers = new Set(closure.endpoints.map(offerKey));for (const requirement of plannedNeeds(state)) {assert(@@ -507,7 +506,6 @@ export function localSchedulerOptionsFromClosure(closure: RuntimeClosure): {readonly resourceLimits: Readonly<Record<string, number>>;readonly runtimeClosure: RuntimeClosure;} {- verifyRuntimeClosure(closure);return {maxConcurrency: closure.scheduling.maxConcurrency,resourceLimits: Object.fromEntries(closure.scheduling.resources.map((resource) => [resource.id, resource.maxConcurrency])),diff --git a/packages/runtime/src/scheduler.ts b/packages/runtime/src/scheduler.tsindex 8d4fa975..e36d764f 100644--- a/packages/runtime/src/scheduler.ts+++ b/packages/runtime/src/scheduler.ts@@ -1,6 +1,6 @@-import { reduce } from "@narratage/core";+import { BuildMachine, buildDefinition, reduce } from "@narratage/core";import type { BuildState } from "@narratage/protocol";-import { verifyRuntimeClosure, verifyRuntimeCoverage } from "./profile.js";+import { verifyRuntimeCoverage } from "./profile.js";import type { RuntimeClosure } from "./profile.js";import type {@@ -17,7 +17,7 @@ import type {type MutableBuild = {readonly id: string;state: BuildState;- revision: number | undefined;+ machine: BuildMachine | undefined;readonly outcomes: SchedulerExecutionOutcome[];blocked: ScheduledBuildResult["blocked"];stopped: boolean;@@ -65,7 +65,6 @@ export class LocalBuildScheduler implements BuildScheduler {? undefined: structuredClone(options.runtimeClosure);this.#buildStore = options.buildStore;- if (this.#runtimeClosure !== undefined) verifyRuntimeClosure(this.#runtimeClosure);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}`);@@ -81,28 +80,28 @@ export class LocalBuildScheduler implements BuildScheduler {if (ids.has(request.id)) throw new Error(`scheduled build ${request.id} is duplicated`);ids.add(request.id);let state = structuredClone(request.state);- let revision: number | undefined;+ let machine: BuildMachine | undefined;if (this.#buildStore !== undefined) {- let snapshot = await this.#buildStore.read(request.id);+ let snapshot = request.snapshot ?? await this.#buildStore.read(request.id);+ if (snapshot !== undefined && snapshot.build !== request.id) {+ throw new Error(`scheduled Build snapshot ${snapshot.build} does not belong to ${request.id}`);+ }if (snapshot === undefined) {try {- snapshot = await this.#buildStore.create(request.id, state);+ snapshot = await this.#buildStore.create(request.id, buildDefinition(state));} catch (error) {snapshot = await this.#buildStore.read(request.id);if (snapshot === undefined) throw error;}}- if (snapshot.state.id !== state.id) {- throw new Error(`scheduled build ${request.id} already names a different Core Build`);- }state = snapshot.state;- revision = snapshot.revision;+ machine = new BuildMachine(snapshot.definition, snapshot.facts);}if (this.#runtimeClosure !== undefined) verifyRuntimeCoverage(this.#runtimeClosure, state);builds.push({id: request.id,state,- revision,+ machine,outcomes: [],blocked: [],stopped: false,@@ -124,25 +123,19 @@ export class LocalBuildScheduler implements BuildScheduler {return proposed;};- const persist = async (build: MutableBuild, state: BuildState): Promise<void> => {- if (this.#buildStore === undefined) {- build.state = state;+ const accept = async (build: MutableBuild, event: import("@narratage/protocol").CommandResult): Promise<void> => {+ if (this.#buildStore === undefined || build.machine === undefined) {+ build.state = reduce(build.state, event);return;}- if (build.revision === undefined) throw new Error(`Build ${build.id} has no durable revision`);- const written = await this.#buildStore.compareAndSwap(build.id, build.revision, state);- if (written.status === "conflict") {- build.state = written.current.state;- build.revision = written.current.revision;- throw new Error(`Build ${build.id} lost its authoritative Store revision`);+ const fact = build.machine.evaluate(event);+ if (fact === undefined) {+ build.state = build.machine.view();+ return;}- // Stores deliberately remove derived outstanding Commands from their durable snapshot.- // The state supplied here has already passed Core verification and remains the authority- // for this live scheduling turn; replacing it with the durable projection would forget- // sibling Commands which are still executing. A restarted Scheduler will regenerate those- // Commands from the stored facts instead.- build.state = state;- build.revision = written.snapshot.revision;+ await this.#buildStore.append(build.id, fact);+ build.machine.commit();+ build.state = build.machine.view();};const preparations = async (): Promise<Map<string, readonly RuntimeRunnableCommand[]>> => {@@ -153,8 +146,7 @@ export class LocalBuildScheduler implements BuildScheduler {continue;}const prepared = this.#executor.prepare(build.state);- if (prepared.state.status !== build.state.status) await persist(build, prepared.state);- else build.state = prepared.state;+ build.state = prepared.state;build.blocked = prepared.blocked;ready.set(build.id, prepared.runnable.filter((item) =>!active.has(buildCommandKey(build.id, item.command.id))));@@ -251,14 +243,12 @@ export class LocalBuildScheduler implements BuildScheduler {}const event = settled.execution.event;try {- const next = reduce(build.state, event);- await persist(build, next);+ await accept(build, event);build.outcomes.push({command: settled.command.command.id,kind: settled.command.command.kind,resources: settled.command.resources.map((resource) => resource.id),status: "completed",- event: event.id,});} catch (error) {build.stopped = true;diff --git a/packages/runtime/src/types.ts b/packages/runtime/src/types.tsindex 447935f3..8accdab2 100644--- a/packages/runtime/src/types.ts+++ b/packages/runtime/src/types.ts@@ -1,7 +1,9 @@import type {BlobRef,- BuildEvent,+ BuildDefinition,+ BuildFact,BuildState,+ CommandResult,CoreCommand,Digest,} from "@narratage/protocol";@@ -20,8 +22,8 @@ export type RuntimeRunnableCommand = {/** Every resource is acquired atomically before the command can cause a side effect. */readonly resources: readonly RuntimeResourceClaim[];readonly queue?: RuntimeQueueLane;- /** Recoverable work retains one shared in-flight reservation while polling. */- readonly capacityMode?: "active" | "recoverable";+ /** Asynchronous work retains one shared in-flight reservation while polling. */+ readonly capacityMode?: "active" | "asynchronous";};export type RuntimeResourceClaim = {@@ -43,15 +45,13 @@ export type RuntimeExecutionStores = {};export type RuntimeWorkerRunOptions = {- readonly owner: string;- readonly leaseMs: number;readonly idlePollMs: number;readonly signal?: AbortSignal;};export type RuntimeWorker = {/** Claim and advance at most one Build. Undefined means no Dispatch was ready. */- runOnce(options: Omit<RuntimeWorkerRunOptions, "idlePollMs" | "signal">): Promise<BuildDispatchSnapshot | undefined>;+ runOnce(): Promise<BuildDispatchSnapshot | undefined>;/** Continue until the caller-owned process signal is aborted. */run(options: RuntimeWorkerRunOptions): Promise<void>;};@@ -80,8 +80,8 @@ export type RuntimeExecutionContext = {};export type RuntimeExecutionResult =- | { readonly status: "completed"; readonly event: BuildEvent }- | { readonly status: "pending"; readonly operation: Digest; readonly wakeAt?: number }+ | { readonly status: "completed"; readonly event: CommandResult }+ | { readonly status: "pending"; readonly operation: string; readonly wakeAt?: number }| { readonly status: "deferred"; readonly wakeAt: number; readonly reason: string };/** Minimal execution port used by a Scheduler. `prepare` is the sole command-generation boundary. */@@ -95,7 +95,6 @@ export type RuntimeCommandExecutor = {cancelOperation?(state: BuildState,operation: OperationSnapshot,- requestedAt: number,): Promise<OperationSnapshot>;};@@ -103,16 +102,16 @@ export type RuntimeCommandExecutor = {export type ArtifactStore = {/** Admit bytes and compute their identity in the same pass. */put(bytes: Uint8Array, mediaType: string): Promise<BlobRef>;- /** Return verified bytes; a digest mismatch is an error, not a cache miss. */+ /** Read bytes previously admitted under this digest. */get(digest: Digest): Promise<Uint8Array | undefined>;- /** Cheap presence query only. Integrity is checked when bytes cross get/open. */+ /** Cheap presence query. */has(digest: Digest): Promise<boolean>;};/** Optional transfer capability. Core and components never require storage to expose it. */export type StreamingArtifactStore = ArtifactStore & {putStream(chunks: AsyncIterable<Uint8Array>, mediaType: string): Promise<BlobRef>;- /** Stream bytes once and reject completion when their digest does not match. */+ /** Stream bytes previously admitted under this digest. */open(digest: Digest): Promise<AsyncIterable<Uint8Array> | undefined>;};@@ -134,22 +133,20 @@ export function isManagedArtifactStore(value: ArtifactStore): value is ManagedArexport type BuildSnapshot = {readonly build: string;- readonly revision: number;+ readonly definition: BuildDefinition;+ readonly facts: readonly BuildFact[];+ /** Materialized read view reconstructed from Definition + Facts; never stored as authority. */readonly state: BuildState;};-export type BuildStoreWrite =- | { readonly status: "stored"; readonly snapshot: BuildSnapshot }- | { readonly status: "conflict"; readonly current: BuildSnapshot };--/** Stores only verified Core state; execution attempts and checkpoints belong to OperationStore. */+/** Stores one Definition and the Core Facts accepted for it in order. */export type BuildStore = {- create(build: string, state: BuildState): Promise<BuildSnapshot>;+ create(build: string, definition: BuildDefinition): Promise<BuildSnapshot>;read(build: string): Promise<BuildSnapshot | undefined>;- compareAndSwap(build: string, expectedRevision: number, state: BuildState): Promise<BuildStoreWrite>;+ append(build: string, fact: BuildFact): Promise<void>;};-/** Optional maintenance index; execution still depends only on BuildStore's three CAS operations. */+/** Optional maintenance index; execution still depends only on BuildStore create/read/append. */export type EnumerableBuildStore = BuildStore & {list(): Promise<readonly BuildSnapshot[]>;};@@ -161,6 +158,8 @@ export function isEnumerableBuildStore(value: BuildStore): value is EnumerableBuexport 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 SchedulerExecutionOutcome = {@@ -168,8 +167,7 @@ export type SchedulerExecutionOutcome = {readonly kind: CoreCommand["kind"];readonly resources: readonly string[];readonly status: "completed" | "pending" | "deferred" | "error";- readonly event?: string;- readonly operation?: Digest;+ readonly operation?: string;readonly wakeAt?: number;readonly message?: string;};@@ -186,7 +184,7 @@ export type BuildSchedulerOptions = {readonly maxConcurrency?: number;readonly resourceLimits?: Readonly<Record<string, number>>;readonly runtimeClosure?: RuntimeClosure;- /** Optional durable authority. When present, every accepted Core Event is persisted by CAS. */+ /** Optional durable authority. When present, every admitted Core Fact is appended. */readonly buildStore?: BuildStore;};diff --git a/packages/runtime/test/catalog.test.ts b/packages/runtime/test/catalog.test.tsindex bd8c3489..d1771e63 100644--- a/packages/runtime/test/catalog.test.ts+++ b/packages/runtime/test/catalog.test.ts@@ -1,14 +1,11 @@import assert from "node:assert/strict";import test from "node:test";--import { digestOf } from "@narratage/core";import { MemoryBuildCatalog } from "@narratage/runtime";-function descriptor(core = digestOf("core:one")) {+function descriptor() {return {format: "narratage.build-catalog-descriptor@1" as const,- core,- source: { path: "/project/main.svml", closure: digestOf("source:one") },+ source: { path: "/project/main.svml" },run: { path: "/project/delivery.svrun" },aliases: [{name: "final.video",@@ -17,31 +14,11 @@ function descriptor(core = digestOf("core:one")) {};}-test("Build Catalog freezes the first Host presentation for a Build id", async () => {- let now = 100;- const catalog = new MemoryBuildCatalog(() => now);+test("Build Catalog records Host presentation for history and output lookup", async () => {+ const catalog = new MemoryBuildCatalog(() => 100);const first = await catalog.record("delivery-01", descriptor());assert.equal(first.createdAt, 100);assert.equal(first.aliases[0]?.name, "final.video");-- now = 200;- const repeated = await catalog.record("delivery-01", descriptor());- assert.equal(repeated.createdAt, 100);- assert.equal(repeated.updatedAt, 100);- await assert.rejects(catalog.record("delivery-01", {- ...descriptor(), aliases: [{ ...descriptor().aliases[0]!, name: "renamed.video" }],- }), /another source, Run Source or output naming/u);assert.deepEqual((await catalog.list()).map((item) => item.build), ["delivery-01"]);-- (repeated as unknown as { aliases: { name: string }[] }).aliases[0]!.name = "tampered";- assert.equal((await catalog.read("delivery-01"))?.aliases[0]?.name, "final.video");-});--test("Build Catalog cannot retarget one Build id to another Core Build", async () => {- const catalog = new MemoryBuildCatalog(() => 100);- await catalog.record("delivery-01", descriptor());- await assert.rejects(- catalog.record("delivery-01", descriptor(digestOf("core:two"))),- /another source, Run Source or output naming/u,- );+ assert.equal((await catalog.read("delivery-01"))?.source.path, "/project/main.svml");});diff --git a/packages/runtime/test/infrastructure.test.ts b/packages/runtime/test/infrastructure.test.tsindex 34e2f081..b2dc2964 100644--- a/packages/runtime/test/infrastructure.test.ts+++ b/packages/runtime/test/infrastructure.test.ts@@ -49,8 +49,8 @@ function servicePackage(close?: () => void) {facet: "dispatch-store",part: "dispatch",port: Object.fromEntries([- "create", "read", "list", "wake", "claim", "heartbeat", "release", "finish", "requestCancellation",- "acquireCapacity", "heartbeatCapacity", "parkCapacity", "releaseCapacity", "clearCapacity", "listCapacity",+ "create", "read", "list", "wake", "claim", "release", "finish", "requestCancellation",+ "acquireCapacity", "releaseCapacity", "releaseBuildCapacity", "listCapacity",].map((name) => [name, async () => undefined])) as never,},{@@ -123,13 +123,13 @@ test("Runtime infrastructure role selection is exact", () => {/not scheduler/u,);- const tampered = {+ const inconsistent = {...configured,manifest: structuredClone(configured.manifest),parts: [...configured.parts],};- (tampered.manifest.facets[1] as { role: "artifact-store" }).role = "artifact-store";- assert.throws(() => verifyRuntimeInfrastructurePackage(tampered), /role differs/u);+ (inconsistent.manifest.facets[1] as { role: "artifact-store" }).role = "artifact-store";+ assert.throws(() => verifyRuntimeInfrastructurePackage(inconsistent), /role differs/u);const missingPort = {...configured,diff --git a/packages/runtime/test/operations.test.ts b/packages/runtime/test/operations.test.tsindex e12b84d3..e0ee8fa9 100644--- a/packages/runtime/test/operations.test.ts+++ b/packages/runtime/test/operations.test.ts@@ -4,99 +4,42 @@ import test from "node:test";import {MemoryOperationStore,sealOperationIdentity,- verifyOperationSnapshot,} from "@narratage/runtime";-function identity(overrides: { readonly endpoint?: string } = {}) {+function operation() {return sealOperationIdentity({+ id: "operation:render",build: "video-42",command: "command:render",- endpoint: overrides.endpoint ?? "hyperframes.local",+ endpoint: "hyperframes.local",pool: "hyperframes.local",- lane: "fixture.render",- attempt: 1,+ lane: "render",});}-test("Operation identity distinguishes Build, Command and Endpoint", () => {- const first = identity();- assert.equal(first.id, identity().id);- assert.notEqual(first.id, identity({ endpoint: "hyperframes.hosted" }).id);-});--test("OperationStore idempotently discovers an existing submission after restart", async () => {- const store = new MemoryOperationStore();- const operation = identity();- const created = await store.create(operation);- const restored = await store.create(operation);- assert.equal(created.status, "created");- assert.equal(restored.status, "existing");- assert.equal(restored.snapshot.id, created.snapshot.id);- assert.equal(restored.snapshot.revision, 0);-});--test("pending checkpoints and completion advance by CAS without entering BuildState", async () => {+test("OperationStore keeps the handle needed to poll external work", async () => {const store = new MemoryOperationStore();- const operation = identity();- await store.create(operation);- const pending = await store.compareAndSwap(operation.id, 0, {+ const identity = operation();+ await store.create({ ...identity,status: "pending",- checkpoint: { remoteJob: "job-123", pollAfterMs: 5_000 },+ handle: { remoteJob: "job-123" },wakeAt: 10_000,progress: { phase: "rendering", completed: 12, total: 60, unit: "frames" },});- assert.equal(pending.status, "stored");- if (pending.status !== "stored") return;- assert.equal(pending.snapshot.status, "pending");- assert.deepEqual(pending.snapshot.checkpoint, { remoteJob: "job-123", pollAfterMs: 5_000 });- assert.equal(pending.snapshot.wakeAt, 10_000);- assert.deepEqual(pending.snapshot.progress,- { phase: "rendering", completed: 12, total: 60, unit: "frames" });- assert.deepEqual((await store.list({ build: "video-42" })).map((item) => item.id), [operation.id]);-- const stale = await store.compareAndSwap(operation.id, 0, {- status: "failed",- failure: { code: "STALE", message: "must not win", retryable: true },- });- assert.equal(stale.status, "conflict");- if (stale.status === "conflict") assert.equal(stale.current.revision, 1);- const completed = await store.compareAndSwap(operation.id, 1, {- status: "completed",- completion: {- value: { kind: "inline", value: { artifact: "video.mp4" } },- },- });- assert.equal(completed.status, "stored");- if (completed.status === "stored") {- assert.equal(completed.snapshot.status, "completed");- assert.doesNotThrow(() => verifyOperationSnapshot(completed.snapshot));- }- assert.equal((await store.read(operation.id))?.status, "completed");- await assert.rejects(- store.compareAndSwap(operation.id, 2, {- status: "pending",- checkpoint: { remoteJob: "job-456" },- }),- /already terminal/u,- );+ const pending = await store.read(identity.id);+ assert.equal(pending?.status, "pending");+ assert.deepEqual(pending?.handle, { remoteJob: "job-123" });+ assert.deepEqual((await store.list({ build: "video-42" })).map((item) => item.id), [identity.id]);});-test("pending progress is bounded and cannot leak into terminal execution facts", async () => {+test("OperationStore records the external result", async () => {const store = new MemoryOperationStore();- const operation = identity();- await store.create(operation);- await assert.rejects(store.compareAndSwap(operation.id, 0, {- status: "pending",- checkpoint: { remoteJob: "job-123" },- progress: { phase: "rendering", completed: 61, total: 60, unit: "frames" },- }), /progress exceeds/u);-});--test("OperationStore returns defensive snapshots", async () => {- const store = new MemoryOperationStore();- const operation = identity();- const created = await store.create(operation);- (created.snapshot as { endpoint: string }).endpoint = "tampered";- assert.equal((await store.read(operation.id))?.endpoint, "hyperframes.local");+ const identity = operation();+ const completed = await store.create({ ...identity,+ status: "completed",+ completion: { value: { kind: "inline", value: { artifact: "video.mp4" } } },+ });+ assert.equal(completed.status, "completed");+ assert.equal((await store.read(identity.id))?.status, "completed");});diff --git a/packages/runtime/test/profile.test.ts b/packages/runtime/test/profile.test.tsindex 361f9de8..edcab17a 100644--- a/packages/runtime/test/profile.test.ts+++ b/packages/runtime/test/profile.test.ts@@ -46,7 +46,7 @@ function manifest(): RuntimeModuleManifest {name: endpointFacet.name,role: "capability-endpoint",fulfills: [{ capability: capabilities.generation, returns: types.generated }],- lifecycle: "recoverable",+ lifecycle: "asynchronous",defaultConcurrency: 1,},],@@ -128,7 +128,7 @@ test("Profile order does not change the resolved selection", () => {);});-test("recoverable Endpoints require an OperationStore before any paid execution", () => {+test("asynchronous Endpoints require an OperationStore before any paid execution", () => {const registry = new RuntimeModuleRegistry();registry.register(manifest());assert.throws(@@ -172,9 +172,9 @@ test("Runtime Closure content is structurally validated", () => {const registry = new RuntimeModuleRegistry();registry.register(manifest());const closure = resolveRuntimeClosure(registry, profile());- const tampered = structuredClone(closure);- (tampered.scheduling as { maxConcurrency: number }).maxConcurrency = 0;- assert.throws(() => verifyRuntimeClosure(tampered), /positive safe integer/u);+ const invalid = structuredClone(closure);+ (invalid.scheduling as { maxConcurrency: number }).maxConcurrency = 0;+ assert.throws(() => verifyRuntimeClosure(invalid), /positive safe integer/u);});test("unsupported Runtime formats are rejected instead of being reinterpreted", () => {diff --git a/packages/screen-overlay/src/manifest.ts b/packages/screen-overlay/src/manifest.tsindex 14c7841f..449b913f 100644--- a/packages/screen-overlay/src/manifest.ts+++ b/packages/screen-overlay/src/manifest.ts@@ -1,7 +1,6 @@import { compositionDependency, compositionTypes } from "@narratage/composition";import { narrativeDependency, narrativeTypes } from "@narratage/narrative";import { programSpaceDependency, programSpaceTypes } from "@narratage/program-space";-import { digestOf } from "@narratage/protocol";import type { ModuleManifest, ProducerRef, TypeRef, ValueSchema } from "@narratage/protocol";import { semanticMapDependency, semanticMapTypes } from "@narratage/semantic-map";import { spatialDependency, spatialTypes } from "@narratage/spatial";@@ -69,7 +68,6 @@ export const screenOverlayHeaderSchema: ValueSchema = object({ id: { schema: strexport const screenOverlayItemSpecSchema: ValueSchema = itemSpec;export const screenOverlaySetSchema: ValueSchema = object({ items: { schema: { kind: "array", items: item } } });export const screenOverlayProgramSchema: ValueSchema = object({ id: { schema: string }, items: { schema: { kind: "array", minItems: 1, items: item } } });-const validator = (digest: ReturnType<typeof digestOf>) => ({ implementation: { digest } });const appendInputs = [{ name: "set", type: screenOverlayTypes.set }, { name: "header", type: screenOverlayTypes.header }, { name: "space", type: programSpaceTypes.programSpace }, { name: "spec", type: screenOverlayTypes.itemSpec }] as const;export const screenOverlayMarkupSurfaces = [{ name: "track", tag: "Track", mode: "structured", outputs: [screenOverlayTypes.header, screenOverlayTypes.itemSpec, screenOverlayTypes.program, compositionTypes.visualTrack] }] as const;diff --git a/packages/screen-overlay/src/program.ts b/packages/screen-overlay/src/program.tsindex 2c7bc0a3..974d0fc2 100644--- a/packages/screen-overlay/src/program.ts+++ b/packages/screen-overlay/src/program.ts@@ -3,7 +3,7 @@ import type { VisualAnimation, VisualElement, VisualStyleDeclaration, VisualTracimport type { NarrativeMomentRef, NarrativeSelectionRef } from "@narratage/narrative";import { assertProgramSpaceIdentity } from "@narratage/program-space";import type { ProgramSpace } from "@narratage/program-space";-import { canonicalize, digestOf } from "@narratage/protocol";+import { canonicalize } from "@narratage/protocol";import type { CompleteSemanticMap } from "@narratage/semantic-map";import { assertCanvasSpace } from "@narratage/spatial";import type { CanvasSpace } from "@narratage/spatial";diff --git a/packages/screen-overlay/test/screen-overlay.test.ts b/packages/screen-overlay/test/screen-overlay.test.tsindex 1076fa06..f6e27cc0 100644--- a/packages/screen-overlay/test/screen-overlay.test.ts+++ b/packages/screen-overlay/test/screen-overlay.test.ts@@ -1,5 +1,6 @@import assert from "node:assert/strict";import test from "node:test";+import { fixtureDigest } from "../../../test/fixture-digest.js";import { artifactManifest } from "@narratage/artifact";import { registerTypeValidatorFacets } from "@narratage/component-kit";@@ -10,7 +11,6 @@ import { compileHyperframesDocument } from "@narratage/hyperframes";import { mediaManifest } from "@narratage/media";import { narrativeManifest } from "@narratage/narrative";import { programSpaceDependency, programSpaceManifest, programSpaceTypes, sealProgramSpace } from "@narratage/program-space";-import { digestOf } from "@narratage/protocol";import type { ModuleManifest } from "@narratage/protocol";import {appendProgramScreenOverlay,@@ -129,7 +129,7 @@ test("the package has no lower-composite, sibling Track, backdrop-filter or hiddtest("the self-described Screen Surface parses into a finite peer-Track graph", async () => {const fixtureModule = { name: "example.screen-inputs", version: "1" } as const;- const fixtureSurfaceDigest = digestOf("example.screen-inputs/surface@1");+ const fixtureSurfaceDigest = fixtureDigest("example.screen-inputs/surface@1");const fixtureSurface = {name: "inputs", tag: "Inputs", mode: "structured",outputs: [spatialTypes.canvas, programSpaceTypes.programSpace],@@ -201,7 +201,6 @@ test("the self-described Screen Surface parses into a finite peer-Track graph",const trackExport = resolveCompiledSourceExport(compiled, "screen-fx.track", compositionTypes.visualTrack);assert.equal(trackExport.ref.kind, "logical-output");const build = start(compiled.program, compiled.graph, sealBuildRequest({- graph: compiled.graph.id,targets: [{ output: trackExport.ref.kind === "logical-output" ? trackExport.ref.id : "" }],}));assert.deepEqual(build.plan.steps.map((step) => step.producer.name).sort(), [diff --git a/packages/script/src/format.ts b/packages/script/src/format.tsindex 04ab3027..8d8a7256 100644--- a/packages/script/src/format.ts+++ b/packages/script/src/format.ts@@ -1,4 +1,4 @@-import { digestOf } from "@narratage/protocol";+import { canonicalStringify } from "@narratage/protocol";import { ScriptSyntaxError } from "./error.js";import { narrativeValue } from "./narrative.js";@@ -72,7 +72,7 @@ export function formatScript(sourceName: string, source: string): string {while (output.at(-1) === "") output.pop();const formatted = `${output.join("\n")}\n`;const reparsed = parseScript(sourceName, formatted);- if (digestOf(narrativeValue(parsed)) !== digestOf(narrativeValue(reparsed))) {+ if (canonicalStringify(narrativeValue(parsed)) !== canonicalStringify(narrativeValue(reparsed))) {throw new ScriptSyntaxError("SCRIPT_FORMAT_SEMANTICS","Formatter refused to change Script semantics.",diff --git a/packages/script/src/manifest.ts b/packages/script/src/manifest.tsindex ec0b88a6..310f88a4 100644--- a/packages/script/src/manifest.ts+++ b/packages/script/src/manifest.ts@@ -1,6 +1,5 @@import { narrativeDependency, narrativeSchema, narrativeTypes } from "@narratage/narrative";import { textDependency, textTypes } from "@narratage/text";-import { digestOf } from "@narratage/protocol";import type { ModuleManifest, TypeRef } from "@narratage/protocol";export const scriptModuleRef = { name: "@narratage/script", version: "1" } as const;diff --git a/packages/script/src/parser.ts b/packages/script/src/parser.tsindex 03f51a03..e7955d97 100644--- a/packages/script/src/parser.ts+++ b/packages/script/src/parser.ts@@ -1,4 +1,3 @@-import { digestOf } from "@narratage/protocol";import { ScriptSyntaxError } from "./error.js";import type {diff --git a/packages/script/test/markup-integration.test.ts b/packages/script/test/markup-integration.test.tsindex 9c0a7445..c4323ae6 100644--- a/packages/script/test/markup-integration.test.ts+++ b/packages/script/test/markup-integration.test.ts@@ -4,7 +4,7 @@ import { textManifest } from "@narratage/text";import assert from "node:assert/strict";import test from "node:test";-import { createResolvedClosure, isDigest } from "@narratage/core";+import { createResolvedClosure } from "@narratage/core";import type { ModuleManifest } from "@narratage/protocol";import {decodeScriptSurface,@@ -91,8 +91,7 @@ test("the same Script meaning has the same authored Record digest across reflow"scriptContext(),);- assert.equal(compact.records[0]?.digest, multiline.records[0]?.digest);- assert.equal(compact.records[1]?.digest, multiline.records[1]?.digest);+ assert.deepEqual(compact.records, multiline.records);});test("without the import, Markup has no hard-coded knowledge of Script", async () => {@@ -214,7 +213,6 @@ test("a module can use Markup's generic structured parser without adding another);assert.equal(result.records[0]?.id, "card");- assert.equal(isDigest(result.records[0]?.digest ?? ""), true);const overreachingRegistry = new MarkupSurfaceRegistry();overreachingRegistry.registerStructured({ module, declaration: cardSurface, handler: ({ element }) => ({diff --git a/packages/script/test/script.test.ts b/packages/script/test/script.test.tsindex bd8c1a89..20b926c8 100644--- a/packages/script/test/script.test.ts+++ b/packages/script/test/script.test.ts@@ -1,7 +1,7 @@import assert from "node:assert/strict";import test from "node:test";+import { fixtureDigest } from "../../../test/fixture-digest.js";-import { digestOf } from "@narratage/core";import type { Narrative } from "@narratage/narrative";import {ScriptSyntaxError,@@ -32,7 +32,7 @@ test("named blocks are Segments and Role Cues do not depend on line breaks", ()`,);- assert.equal(digestOf(narrativeValue(compact)), digestOf(narrativeValue(multiline)));+ assert.equal(fixtureDigest(narrativeValue(compact)), fixtureDigest(narrativeValue(multiline)));assert.deepEqual(compact.segments.map((segment) => segment.id), ["opening", "pause"]);assert.deepEqual(compact.turns.map((turn) => turn.role), ["ALICE", "BOB"]);assert.equal(serializeDialogue(compact), "ALICE: Hello there.\nBOB: Good morning.");@@ -137,7 +137,7 @@ test("the formatter is semantic-preserving and idempotent", () => {const twice = formatScript("format.svml", once);assert.equal(twice, once);assert.equal(- digestOf(narrativeValue(parseScript("before.svml", input))),- digestOf(narrativeValue(parseScript("after.svml", once))),+ fixtureDigest(narrativeValue(parseScript("before.svml", input))),+ fixtureDigest(narrativeValue(parseScript("after.svml", once))),);});diff --git a/packages/seedance/src/index.ts b/packages/seedance/src/index.tsindex 57cab623..455241e9 100644--- a/packages/seedance/src/index.ts+++ b/packages/seedance/src/index.ts@@ -15,7 +15,7 @@ import type {import { defineExactModelModule } from "@narratage/model-kit";import { assertSpeechDurationIdentity, speechDependency, speechTypes } from "@narratage/speech";import type { SpeechDuration } from "@narratage/speech";-import { canonicalize, digestOf } from "@narratage/protocol";+import { canonicalize } from "@narratage/protocol";import type { Digest, ProducerRef, TypeRef, ValueSchema } from "@narratage/protocol";export const seedanceModuleRef = { name: "@narratage/seedance", version: "1" } as const;diff --git a/packages/seedream/src/index.ts b/packages/seedream/src/index.tsindex cb8e895e..9fff3e33 100644--- a/packages/seedream/src/index.ts+++ b/packages/seedream/src/index.ts@@ -1,7 +1,6 @@import { sealGenerationPortRequest, sealGenerationPortTable } from "@narratage/generation";import type { GenerationPortTable, GenerationPortValue, GenerationRequest } from "@narratage/generation";import { defineExactModelModule } from "@narratage/model-kit";-import { digestOf } from "@narratage/protocol";export const seedreamModuleRef = { name: "@narratage/seedream", version: "1" } as const;diff --git a/packages/semantic-map/src/manifest.ts b/packages/semantic-map/src/manifest.tsindex 14db13ff..4a1d70e4 100644--- a/packages/semantic-map/src/manifest.ts+++ b/packages/semantic-map/src/manifest.ts@@ -1,6 +1,5 @@import { narrativeDependency } from "@narratage/narrative";import { speechEvidenceDependency } from "@narratage/speech-evidence";-import { digestOf } from "@narratage/protocol";import type { ModuleManifest, TypeRef, ValueSchema } from "@narratage/protocol";const string = { kind: "string", minLength: 1 } as const;const integer = { kind: "number", integer: true, minimum: 0 } as const;diff --git a/packages/source/src/unit.ts b/packages/source/src/unit.tsindex 93db7698..93cffa19 100644--- a/packages/source/src/unit.ts+++ b/packages/source/src/unit.ts@@ -1,5 +1,4 @@-import { isDigest } from "@narratage/protocol";-import type { BlobRef, Digest, SourceRange } from "@narratage/protocol";+import type { BlobRef, SourceRange } from "@narratage/protocol";export type SourceUnit = {/** Host-canonical identity used only for recursion and diagnostics. */@@ -11,22 +10,16 @@ export type SourceUnit = {/** Shared identity fields of any successfully decoded self-described Source. */export type CompiledSourceIdentity = {readonly frontend: string;- readonly sourceDigest: Digest;- readonly semanticDigest: Digest;};export function compiledSourceIdentity(value: CompiledSourceIdentity): CompiledSourceIdentity {return {frontend: value.frontend,- sourceDigest: value.sourceDigest,- semanticDigest: value.semanticDigest,};}export function verifyCompiledSourceIdentity(value: CompiledSourceIdentity): void {if (value.frontend.trim().length === 0) throw new Error("Source Frontend is empty");- if (!isDigest(value.sourceDigest)) throw new Error("Source digest is invalid");- if (!isDigest(value.semanticDigest)) throw new Error("Source semantic digest is invalid");}export type SourceImportRequest = {diff --git a/packages/spatial/src/geometry.ts b/packages/spatial/src/geometry.tsindex 7e596e20..a9267b21 100644--- a/packages/spatial/src/geometry.ts+++ b/packages/spatial/src/geometry.ts@@ -1,4 +1,3 @@-import { digestOf } from "@narratage/protocol";import type {AnchoredFrameProgram,diff --git a/packages/spatial/src/index.ts b/packages/spatial/src/index.tsindex eba26c30..a9cb3fc4 100644--- a/packages/spatial/src/index.ts+++ b/packages/spatial/src/index.ts@@ -13,7 +13,6 @@ export {spatialManifest, spatialMarkupSurfaces,spatialModuleRef,spatialProducers,- spatialSurfaceDigests,spatialTypes,} from "./manifest.js";export {diff --git a/packages/spatial/src/manifest.ts b/packages/spatial/src/manifest.tsindex b9a2ace4..b698a724 100644--- a/packages/spatial/src/manifest.ts+++ b/packages/spatial/src/manifest.ts@@ -1,4 +1,3 @@-import { digestOf } from "@narratage/protocol";import type { ModuleManifest, ProducerRef, TypeRef } from "@narratage/protocol";import {anchoredFrameProgramSchema,@@ -14,15 +13,6 @@ import {} from "./schema.js";export const spatialModuleRef = { name: "@narratage/spatial", version: "1" } as const;-export const spatialSurfaceDigests = {- canvas: digestOf("@narratage/spatial/canvas-surface@1"),- point: digestOf("@narratage/spatial/point-surface@1"),- path: digestOf("@narratage/spatial/path-surface@1"),- extent: digestOf("@narratage/spatial/extent-surface@1"),- frame: digestOf("@narratage/spatial/frame-surface@1"),- anchoredFrame: digestOf("@narratage/spatial/anchored-frame-surface@1"),- aspectFrame: digestOf("@narratage/spatial/aspect-frame-surface@1"),-} as const;export const spatialTypes = {canvas: { module: spatialModuleRef, name: "CanvasSpace" },point: { module: spatialModuleRef, name: "SpatialPoint" },@@ -43,10 +33,6 @@ export const spatialProducers = {fitContent: { module: spatialModuleRef, name: "fit-content" },} satisfies Record<string, ProducerRef>;-const validator = (digest: ReturnType<typeof digestOf>) => ({- implementation: { digest },-});-export const spatialMarkupSurfaces = [{ name: "canvas", tag: "Canvas", mode: "structured", outputs: [spatialTypes.canvas] },{ name: "point", tag: "Point", mode: "structured", outputs: [spatialTypes.point] },diff --git a/packages/spatial/test/spatial.test.ts b/packages/spatial/test/spatial.test.tsindex 6c1348b3..d0feda94 100644--- a/packages/spatial/test/spatial.test.ts+++ b/packages/spatial/test/spatial.test.ts@@ -235,7 +235,6 @@ test("self-described Spatial Surfaces produce an explicit Canvas edge and finiteconst sticker = resolveCompiledSourceExport(compiled, "sticker", spatialTypes.frame);assert.equal(sticker.ref.kind, "logical-output");const build = start(compiled.program, compiled.graph, sealBuildRequest({- graph: compiled.graph.id,targets: [{ output: sticker.ref.kind === "logical-output" ? sticker.ref.id : "" }],}));assert.deepEqual(build.plan.steps.map((step) => step.producer.name).sort(), [diff --git a/packages/speech-alignment/src/component.ts b/packages/speech-alignment/src/component.tsindex 2968f646..447b1ca1 100644--- a/packages/speech-alignment/src/component.ts+++ b/packages/speech-alignment/src/component.ts@@ -7,7 +7,6 @@ import { canonicalize } from "@narratage/protocol";import {locateSpeechTiming,- speechLocatorDigest,} from "./locate.js";import { speechAlignmentProducers } from "./manifest.js";diff --git a/packages/speech-alignment/src/index.ts b/packages/speech-alignment/src/index.tsindex 76ac04c3..53353906 100644--- a/packages/speech-alignment/src/index.ts+++ b/packages/speech-alignment/src/index.ts@@ -1,7 +1,7 @@export { alignWordGroups } from "./align.js";export { speechAlignmentComponent } from "./component.js";export { SpeechAlignmentError } from "./error.js";-export { locateSpeechTiming, speechLocatorDigest } from "./locate.js";+export { locateSpeechTiming } from "./locate.js";export { speechAlignmentManifest, speechAlignmentModuleRef, speechAlignmentProducers } from "./manifest.js";export { alignCharacters, alignmentCharacters, normalizeForAlignment } from "./normalize.js";export type * from "./types.js";diff --git a/packages/speech-alignment/src/locate.ts b/packages/speech-alignment/src/locate.tsindex 247c663f..f83f7d82 100644--- a/packages/speech-alignment/src/locate.ts+++ b/packages/speech-alignment/src/locate.ts@@ -1,4 +1,4 @@-import { digestOf, isDigest } from "@narratage/protocol";+import { isDigest } from "@narratage/protocol";import type { Narrative, NarrativeToken } from "@narratage/narrative";import { programFrameSampleBoundary, programSpaceFrameCount } from "@narratage/program-space";import type { SpeechAudioBasis } from "@narratage/speech";@@ -16,8 +16,6 @@ import { SpeechAlignmentError } from "./error.js";import { alignCharacters, alignmentCharacters } from "./normalize.js";import type { AlignmentGroup, TimedSpeechSegment } from "./types.js";-export const speechLocatorDigest = digestOf("@narratage/speech-alignment/locate@1");-type MutableTiming = {startSample: number;endSampleExclusive: number;diff --git a/packages/speech-alignment/src/manifest.ts b/packages/speech-alignment/src/manifest.tsindex b25e8aa3..cf9323f7 100644--- a/packages/speech-alignment/src/manifest.ts+++ b/packages/speech-alignment/src/manifest.ts@@ -4,8 +4,6 @@ import { speechEvidenceDependency, speechEvidenceTypes } from "@narratage/speechimport { semanticMapDependency, semanticMapTypes } from "@narratage/semantic-map";import type { ModuleManifest, ProducerRef } from "@narratage/protocol";-import { speechLocatorDigest } from "./locate.js";-export const speechAlignmentModuleRef = { name: "@narratage/speech-alignment", version: "1" } as const;export const speechAlignmentProducers = {locate: { module: speechAlignmentModuleRef, name: "locate-speech" },diff --git a/packages/speech-alignment/test/speech-align.test.ts b/packages/speech-alignment/test/speech-align.test.tsindex f3a08fee..af293736 100644--- a/packages/speech-alignment/test/speech-align.test.ts+++ b/packages/speech-alignment/test/speech-align.test.ts@@ -7,8 +7,8 @@ import { sealAlignedTranscriptEvidence } from "@narratage/speech-evidence";import type { AlignedTranscriptEvidence, SpeechCharacterEvidence, SpeechWordEvidence } from "@narratage/speech-evidence";import assert from "node:assert/strict";import test from "node:test";+import { fixtureDigest } from "../../../test/fixture-digest.js";-import { digestOf } from "@narratage/protocol";import { parseScript } from "@narratage/script";import {SpeechAlignmentError,@@ -82,7 +82,7 @@ function speechBasis(durationSec,frameRate: { numerator: 1_000, denominator: 1 },});- const audioDigest = digestOf(`fixture:audio:${narrative.segments.map((segment) => segment.id).join("+")}:${durationSec}`);+ const audioDigest = fixtureDigest(`fixture:audio:${narrative.segments.map((segment) => segment.id).join("+")}:${durationSec}`);const segments = narrative.segments.map((segment, index) => ({segmentId: segment.id,startFrame: Math.round((windows?.[index]?.startSec ?? durationSec * index / narrative.segments.length) * 1_000),@@ -290,7 +290,7 @@ test("Segment anchors preserve exact frame cuts without a seconds round trip", (durationSec: 1,frameRate: { numerator: 24, denominator: 1 },}),- audio: { kind: "blob", digest: digestOf("frame-cuts:audio"), size: 1, mediaType: "audio/wav" },+ audio: { kind: "blob", digest: fixtureDigest("frame-cuts:audio"), size: 1, mediaType: "audio/wav" },visualTrack: { clips: [] },segments: [{ segmentId: "one", startFrame: 0, endFrameExclusive: 7 },diff --git a/packages/speech-basis/src/manifest.ts b/packages/speech-basis/src/manifest.tsindex 839203ee..2e670da0 100644--- a/packages/speech-basis/src/manifest.ts+++ b/packages/speech-basis/src/manifest.ts@@ -1,7 +1,6 @@import { programSpaceDependency, programSpaceTypes } from "@narratage/program-space";import { speechDependency, speechTypes } from "@narratage/speech";import { compositionDependency, compositionTypes } from "@narratage/composition";-import { digestOf } from "@narratage/protocol";import type { ModuleManifest, ProducerRef } from "@narratage/protocol";export const speechBasisModuleRef = { name: "@narratage/speech-basis", version: "1" } as const;diff --git a/packages/speech-basis/src/projection.ts b/packages/speech-basis/src/projection.tsindex 570447dc..3a7d7d6f 100644--- a/packages/speech-basis/src/projection.ts+++ b/packages/speech-basis/src/projection.ts@@ -5,7 +5,6 @@ import type { SpeechAudioBasis, SpeechBasis } from "@narratage/speech";import { lowerRestrictedSpeechVisualPresents } from "@narratage/media-track";import { sealAudioTrack, sealVisualTrack } from "@narratage/composition";import type { AudioTrack, VisualTrack } from "@narratage/composition";-import { digestOf } from "@narratage/protocol";export function projectSpeechProgramSpace(basis: SpeechBasis): ProgramSpace {assertSpeechBasisIdentity(basis);diff --git a/packages/speech-basis/test/product.test.ts b/packages/speech-basis/test/product.test.tsindex ee7d8e25..21982e86 100644--- a/packages/speech-basis/test/product.test.ts+++ b/packages/speech-basis/test/product.test.ts@@ -10,10 +10,10 @@ import { compositionTypes } from "@narratage/composition";import { mediaPipelineManifest } from "@narratage/media-pipeline";import assert from "node:assert/strict";import test from "node:test";+import { fixtureDigest } from "../../../test/fixture-digest.js";import {createResolvedClosure,- digestOf,link,sealBuildRequest,sealCompiledGraph,@@ -83,13 +83,13 @@ function sampleTake(label = "generated"): SpeechBasis {});const audio = {kind: "blob" as const,- digest: digestOf(`${label}:audio`),+ digest: fixtureDigest(`${label}:audio`),size: 12,mediaType: "audio/wav",};const visual = {kind: "blob" as const,- digest: digestOf(`${label}:visual`),+ digest: fixtureDigest(`${label}:visual`),size: 24,mediaType: "video/mp4",};@@ -123,9 +123,6 @@ function createProgram(): LinkedProgram {id: "request:opening",type: requestType,value: { kind: "inline", value: "Generate the opening speech take." },- origin: {- kind: "authored",- },});return link(closure, [request]);}@@ -138,7 +135,6 @@ function createGraph(program: LinkedProgram): CompiledGraph {const existingVisual = projectSpeechVisual(existingTake);const existingVisualValue = { kind: "inline" as const, value: existingVisual };return sealCompiledGraph({- program: program.semanticDigest,outputs: [{id: "opening.take",@@ -225,7 +221,6 @@ function build(const program = createProgram();const sourceGraph = createGraph(program);const graph = sealCompiledGraph({- program: sourceGraph.program,outputs: sourceGraph.outputs.map((item) => ({...item,primary: satisfactionMap[item.id] ?? item.primary,@@ -234,7 +229,6 @@ function build(operations: sourceGraph.operations,});const request: BuildRequest = sealBuildRequest({- graph: graph.id,targets: targets.map((outputId) => ({ output: outputId })),});return start(program, graph, request);@@ -299,7 +293,7 @@ test("selecting an Existing SpeechBasis stops generation but keeps both projecti{ "opening.take": "opening.take.existing" },);assert.deepEqual(stepIds(state), ["project-opening-audio", "project-opening-visual"]);- assert.deepEqual(state.records.filter((record) => record.origin.kind === "provided").map((record) => record.id), ["provided:opening-take"]);+ assert.equal(state.records.some((record) => record.id === "provided:opening-take"), true);assert.equal(state.plan.steps.find((step) => step.id === "project-opening-audio")?.inputs.basis,"provided:opening-take",diff --git a/packages/speech-evidence/src/index.ts b/packages/speech-evidence/src/index.tsindex 987714de..53bed75f 100644--- a/packages/speech-evidence/src/index.ts+++ b/packages/speech-evidence/src/index.ts@@ -1,4 +1,3 @@-import { digestOf } from "@narratage/protocol";import type { ModuleManifest, TypeRef, ValueSchema } from "@narratage/protocol";/** Exact boundaries in the fixed 16 kHz Speech Evidence Audio sample domain. */diff --git a/packages/speech-spine/src/manifest.ts b/packages/speech-spine/src/manifest.tsindex 97a9a523..7d9d1608 100644--- a/packages/speech-spine/src/manifest.ts+++ b/packages/speech-spine/src/manifest.ts@@ -9,7 +9,6 @@ import {mediaPipelineModuleRef,mediaPipelineTypes,} from "@narratage/media-pipeline";-import { digestOf } from "@narratage/protocol";import type { ModuleManifest, ProducerRef, TypeRef, ValueSchema } from "@narratage/protocol";import { speechBasisManifest, speechBasisModuleRef } from "@narratage/speech-basis";import {diff --git a/packages/speech-spine/src/program.ts b/packages/speech-spine/src/program.tsindex 4bf274bb..f0bad3dd 100644--- a/packages/speech-spine/src/program.ts+++ b/packages/speech-spine/src/program.ts@@ -10,7 +10,7 @@ import {verifyAudioProgramPlan,} from "@narratage/media-pipeline";import type { AudioProgramPlan } from "@narratage/media-pipeline";-import { canonicalize, digestOf } from "@narratage/protocol";+import { canonicalize } from "@narratage/protocol";import { assertContentFit, assertSpatialFrame } from "@narratage/spatial";import type { ContentFit, SpatialFrame } from "@narratage/spatial";diff --git a/packages/speech-spine/test/program.test.ts b/packages/speech-spine/test/program.test.tsindex 14abf25f..20bcc64c 100644--- a/packages/speech-spine/test/program.test.ts+++ b/packages/speech-spine/test/program.test.ts@@ -1,9 +1,9 @@import assert from "node:assert/strict";import test from "node:test";+import { fixtureDigest } from "../../../test/fixture-digest.js";import type { SynchronizedMedia, TimelineAudio } from "@narratage/media";import type { NarrativeExcerpt } from "@narratage/narrative";-import { digestOf } from "@narratage/protocol";import {appendSpeechSpineAudioTake,appendSpeechSpineVisualTake,@@ -33,12 +33,12 @@ function synchronized(id: string, visual: boolean): SynchronizedMedia {frameCount: 30,},...(visual ? { visual: {- artifact: { kind: "blob" as const, digest: digestOf(`${id}:video`), size: 1, mediaType: "video/mp4" },+ artifact: { kind: "blob" as const, digest: fixtureDigest(`${id}:video`), size: 1, mediaType: "video/mp4" },width: 720,height: 1280,} } : {}),audio: {- artifact: { kind: "blob", digest: digestOf(`${id}:audio`), size: 1, mediaType: "audio/wav" },+ artifact: { kind: "blob", digest: fixtureDigest(`${id}:audio`), size: 1, mediaType: "audio/wav" },},};}@@ -61,7 +61,7 @@ test("audio Takes lengthen the speech program without inventing a visual clip",sealSpeechSpineVisualSpec({ stackingOrder: 30 }),);const audio: TimelineAudio = {- artifact: { kind: "blob", digest: digestOf("speech:mix"), size: 1, mediaType: "audio/wav" },+ artifact: { kind: "blob", digest: fixtureDigest("speech:mix"), size: 1, mediaType: "audio/wav" },sampleFrames: 96_000,};const basis = assembleSpeechBasis(program, set, audio);diff --git a/packages/speech-spine/test/surface.test.ts b/packages/speech-spine/test/surface.test.tsindex f8d02f28..1092ec70 100644--- a/packages/speech-spine/test/surface.test.ts+++ b/packages/speech-spine/test/surface.test.ts@@ -6,8 +6,9 @@ import { compositionTypes } from "@narratage/composition";import { artifactTypes } from "@narratage/artifact";import assert from "node:assert/strict";import test from "node:test";+import { fixtureDigest } from "../../../test/fixture-digest.js";-import { createResolvedClosure, digestOf, sealBuildRequest, start } from "@narratage/core";+import { createResolvedClosure, sealBuildRequest, start } from "@narratage/core";import {AuthorFrontendRegistry,compileSourceClosure,@@ -51,7 +52,7 @@ import {import { createRecordAdmitter, TypeValidatorRegistry } from "@narratage/validation";const fixtureModule = { name: "example.speech-media", version: "1" } as const;-const fixtureSurfaceDigest = digestOf("example.speech-media/surface@1");+const fixtureSurfaceDigest = fixtureDigest("example.speech-media/surface@1");const fixtureSurface = {name: "media", tag: "Media", mode: "structured", outputs: [artifactTypes.blob, svsRecipeType],} as const;@@ -92,7 +93,7 @@ test("Speech Spine lowers ordered Takes into media normalization, one audio planrecords: [...["take-one", "take-two", "voice-one"].map((id) => ({id, type: artifactTypes.blob,- value: { kind: "blob" as const, digest: digestOf(id), size: 128,+ value: { kind: "blob" as const, digest: fixtureDigest(id), size: 128,mediaType: id === "voice-one" ? "audio/mpeg" : "video/mp4" },range: element.range,})),@@ -146,7 +147,6 @@ test("Speech Spine lowers ordered Takes into media normalization, one audio plan});const target = resolveCompiledSourceExport(compiled, "speech.visual", compositionTypes.visualTrack);const build = start(compiled.program, compiled.graph, sealBuildRequest({- graph: compiled.graph.id,targets: [{ output: target.ref.kind === "logical-output" ? target.ref.id : "" }],}));const names = build.plan.steps.map((step) => step.producer.name);diff --git a/packages/speech/src/manifest.ts b/packages/speech/src/manifest.tsindex 7234f2bf..82e13bdc 100644--- a/packages/speech/src/manifest.ts+++ b/packages/speech/src/manifest.ts@@ -1,7 +1,6 @@import { mediaDependency } from "@narratage/media";import { narrativeDependency } from "@narratage/narrative";import { programSpaceDependency } from "@narratage/program-space";-import { digestOf } from "@narratage/protocol";import type { ModuleManifest, TypeRef } from "@narratage/protocol";import { spatialDependency } from "@narratage/spatial";import { speechAudioBasisSchema, speechBasisSchema, speechDurationSchema, speechEvidenceAudioSchema } from "./schema.js";diff --git a/packages/speech/test/evidence.test.ts b/packages/speech/test/evidence.test.tsindex 24beadf0..84cde157 100644--- a/packages/speech/test/evidence.test.ts+++ b/packages/speech/test/evidence.test.ts@@ -3,8 +3,8 @@ import { assertSpeechEvidenceAudioIdentity, sealSpeechEvidenceAudio, speechEvideimport type { SpeechEvidenceAudio } from "@narratage/speech";import assert from "node:assert/strict";import test from "node:test";+import { fixtureDigest } from "../../../test/fixture-digest.js";-import { digestOf } from "@narratage/protocol";test("speech evidence uses integer rational boundary projection rather than floating duration arithmetic", () => {assert.equal(speechEvidenceSampleBoundary(0), 0);@@ -22,7 +22,7 @@ test("SpeechEvidenceAudio carries only normalized evidence bytes and their exactconst value = sealSpeechEvidenceAudio({artifact: {kind: "blob",- digest: digestOf("evidence"),+ digest: fixtureDigest("evidence"),size: 32_044,mediaType: "audio/wav",},diff --git a/packages/store-sqlite/README.md b/packages/store-sqlite/README.mdindex d58aaee7..7d702be3 100644--- a/packages/store-sqlite/README.md+++ b/packages/store-sqlite/README.md@@ -5,14 +5,14 @@ Durable local adapters for the environment-neutral Build, Operation and DispatchOne SQLite file may physically contain the execution tables and an optional Host index, but theirinterfaces remain separate:-- BuildStore persists verified Core facts with compare-and-swap revisions;+- BuildStore persists one Definition and appends admitted Core Facts in order;- OperationStore persists external attempt identities, checkpoints and completions;-- BuildDispatchStore persists current Build admission, leases and generic capacity reservations;+- BuildDispatchStore persists the local Build queue and remote in-flight capacity reservations;- BuildCatalog indexes source/run paths and output aliases for Host presentation only;-- no ready-command queue is stored. Core regenerates readiness from BuildState after every restart.+- no ready-command queue is stored. Core regenerates readiness from Definition plus Facts after every restart.Artifacts and credentials never enter this database. The adapter owns its private pre-release-schema `6`; Core and Endpoint packages do not import SQLite. Incompatible development databases are+schema `9`; Core and Endpoint packages do not import SQLite. Incompatible development databases arerejected and recreated explicitly instead of carrying migration code before the first release.`createSqliteRuntimeInfrastructurePackage()` returns one configured instance with three separately selectablediff --git a/packages/store-sqlite/src/store.ts b/packages/store-sqlite/src/store.tsindex 02b36185..db1b98ee 100644--- a/packages/store-sqlite/src/store.ts+++ b/packages/store-sqlite/src/store.ts@@ -4,30 +4,20 @@ import { DatabaseSync } from "node:sqlite";import {canonicalStringify,- verifyBuildState,+ materializeBuild,} from "@narratage/core";-import { digestOf } from "@narratage/protocol";import type {- BuildState,+ BuildDefinition,+ BuildFact,Digest,} from "@narratage/protocol";import {- sameBuildCatalogDescriptor,verifyBuildCatalogDescriptor,verifyBuildCatalogEntry,defineRuntimeInfrastructurePackage,capacityReservationId,- verifyBuildDispatchIdentity,- verifyBuildDispatchSnapshot,- verifyCapacityLimits,- verifyCapacityReservation,- verifyDispatchLease,- verifyOperationIdentity,- verifyOperationSnapshot,} from "@narratage/runtime";import type {- BuildDispatchClaim,- BuildDispatchCreate,BuildDispatchIdentity,BuildDispatchRelease,BuildDispatchSnapshot,@@ -37,23 +27,19 @@ import type {BuildCatalogEntry,BuildSnapshot,BuildStore,- BuildStoreWrite,CapacityAcquire,CapacityAcquireRequest,CapacityReservation,- DispatchLease,DispatchQuery,- OperationCreate,OperationIdentity,OperationQuery,OperationSnapshot,OperationStore,- OperationStoreWrite,OperationUpdate,RuntimeInfrastructurePackage,} from "@narratage/runtime";-const databaseSchemaVersion = 6;+const databaseSchemaVersion = 11;export const sqliteStoreModuleRef = {name: "@narratage/store-sqlite",@@ -99,53 +85,44 @@ function transaction<T>(database: DatabaseSync, body: () => T): T {}}-function durableBuildState(state: BuildState): BuildState {- return { ...structuredClone(state), outstanding: [] };-}--function parseBuildSnapshot(row: Row): BuildSnapshot {+function parseBuildSnapshot(row: Row, factRows: readonly Row[]): BuildSnapshot {assert(typeof row.build_id === "string", "SQLite Build row has no build id");- assert(typeof row.revision === "number", "SQLite Build row has no revision");- assert(typeof row.state_json === "string", "SQLite Build row has no state");- const state = JSON.parse(row.state_json) as BuildState;- verifyBuildState(state);- assert(state.outstanding.length === 0, "durable BuildState contains derived outstanding Commands");- return { build: row.build_id, revision: row.revision, state };+ assert(typeof row.definition_json === "string", "SQLite Build row has no Definition");+ const definition = JSON.parse(row.definition_json) as BuildDefinition;+ const facts = factRows.map((factRow) => {+ assert(typeof factRow.fact_json === "string", "SQLite Build Fact row has no Fact");+ return JSON.parse(factRow.fact_json) as BuildFact;+ });+ const state = materializeBuild(definition, facts);+ return { build: row.build_id, definition, facts, state };}-function operationIdentity(snapshot: OperationSnapshot): OperationIdentity {+function operationIdentityFrom(snapshot: OperationSnapshot): OperationIdentity {return {- format: snapshot.format,id: snapshot.id,build: snapshot.build,command: snapshot.command,endpoint: snapshot.endpoint,pool: snapshot.pool,lane: snapshot.lane,- attempt: snapshot.attempt,};}function parseOperationSnapshot(row: Row): OperationSnapshot {- assert(typeof row.identity_json === "string", "SQLite Operation row has no identity");- assert(typeof row.revision === "number", "SQLite Operation row has no revision");+ for (const field of ["operation_id", "build_id", "command_id", "endpoint_id", "pool_id", "lane_id"] as const) {+ assert(typeof row[field] === "string", `SQLite Operation row has no ${field}`);+ }assert(typeof row.status === "string", "SQLite Operation row has no status");- const identity = JSON.parse(row.identity_json) as OperationIdentity;const pending = row.status === "pending"- ? JSON.parse(String(row.checkpoint_json)) as {- readonly format?: string;- readonly checkpoint?: unknown;+ ? JSON.parse(String(row.handle_json)) as {+ readonly handle: unknown;readonly wakeAt?: number;readonly progress?: OperationSnapshot["progress"];}: undefined;- if (row.status === "pending") {- assert(pending?.format === "narratage.operation-pending@1" && pending.checkpoint !== undefined,- "SQLite pending Operation has an unsupported checkpoint envelope");- }const mutable = row.status === "pending"? {- checkpoint: pending!.checkpoint,+ handle: pending!.handle,...(pending!.wakeAt === undefined ? {} : { wakeAt: pending!.wakeAt }),...(pending!.progress === undefined ? {} : { progress: pending!.progress }),}@@ -154,18 +131,16 @@ function parseOperationSnapshot(row: Row): OperationSnapshot {: row.status === "failed"? { failure: JSON.parse(String(row.failure_json)) }: {};- const cancellation = typeof row.cancellation_json === "string"- ? { cancellation: JSON.parse(row.cancellation_json) }- : {};- const snapshot = {- ...identity,- revision: row.revision,+ return {+ id: row.operation_id,+ build: row.build_id,+ command: row.command_id,+ endpoint: row.endpoint_id,+ pool: row.pool_id,+ lane: row.lane_id,status: row.status,...mutable,- ...cancellation,} as OperationSnapshot;- verifyOperationSnapshot(snapshot);- return snapshot;}function copy<T>(value: T): T {@@ -179,70 +154,65 @@ class SqliteBuildStore implements BuildStore {this.#database = database;}- async create(build: string, state: BuildState): Promise<BuildSnapshot> {+ #facts(build: string): Row[] {+ return this.#database.prepare(`+ SELECT fact_json+ FROM narratage_build_facts+ WHERE build_id = ?+ ORDER BY sequence ASC+ `).all(build) as Row[];+ }++ async create(build: string, definition: BuildDefinition): Promise<BuildSnapshot> {assert(build.trim().length > 0, "build id must not be empty");- const durable = durableBuildState(state);const result = this.#database.prepare(`- INSERT OR IGNORE INTO narratage_builds (build_id, revision, state_json)- VALUES (?, 0, ?)- `).run(build, canonicalStringify(durable));+ INSERT OR IGNORE INTO narratage_builds (build_id, definition_json)+ VALUES (?, ?)+ `).run(build, canonicalStringify(definition));if (result.changes !== 1) throw new Error(`build ${build} already exists`);- return { build, revision: 0, state: copy(durable) };+ const storedDefinition = copy(definition);+ const facts: readonly BuildFact[] = [];+ const state = materializeBuild(storedDefinition, facts);+ return { build, definition: storedDefinition, facts, state };}async read(build: string): Promise<BuildSnapshot | undefined> {const row = this.#database.prepare(`- SELECT build_id, revision, state_json+ SELECT build_id, definition_jsonFROM narratage_buildsWHERE build_id = ?`).get(build) as Row | undefined;- return row === undefined ? undefined : parseBuildSnapshot(row);+ return row === undefined ? undefined : parseBuildSnapshot(row, this.#facts(build));}async list(): Promise<readonly BuildSnapshot[]> {const rows = this.#database.prepare(`- SELECT build_id, revision, state_json+ SELECT build_id, definition_jsonFROM narratage_buildsORDER BY build_id ASC`).all() as Row[];- return rows.map(parseBuildSnapshot);+ return rows.map((row) => parseBuildSnapshot(row, this.#facts(String(row.build_id))));}- async compareAndSwap(- build: string,- expectedRevision: number,- state: BuildState,- ): Promise<BuildStoreWrite> {- assert(Number.isSafeInteger(expectedRevision) && expectedRevision >= 0, "expected revision is invalid");- const durable = durableBuildState(state);+ async append(build: string, fact: BuildFact): Promise<void> {const result = this.#database.prepare(`- UPDATE narratage_builds- SET revision = ?, state_json = ?- WHERE build_id = ? AND revision = ?- `).run(expectedRevision + 1, canonicalStringify(durable), build, expectedRevision);- if (result.changes === 1) {- return {- status: "stored",- snapshot: { build, revision: expectedRevision + 1, state: copy(durable) },- };- }- const current = await this.read(build);- if (current === undefined) throw new Error(`build ${build} does not exist`);- return { status: "conflict", current };+ INSERT INTO narratage_build_facts (+ build_id, command_id, fact_json+ ) VALUES (?, ?, ?)+ `).run(build, fact.command, canonicalStringify(fact));+ if (result.changes !== 1) throw new Error(`Build ${build} Fact was not stored`);}}function parseCatalogEntry(row: Row): BuildCatalogEntry {assert(typeof row.build_id === "string", "SQLite Build Catalog row has no build id");assert(typeof row.created_at === "number", "SQLite Build Catalog row has no creation time");- assert(typeof row.updated_at === "number", "SQLite Build Catalog row has no update time");assert(typeof row.descriptor_json === "string", "SQLite Build Catalog row has no descriptor");const descriptor = JSON.parse(row.descriptor_json) as BuildCatalogDescriptor;const entry = {...descriptor,build: row.build_id,createdAt: row.created_at,- updatedAt: row.updated_at,};verifyBuildCatalogEntry(entry);return entry;@@ -258,28 +228,20 @@ class SqliteBuildCatalog implements BuildCatalog {async record(build: string, descriptor: BuildCatalogDescriptor): Promise<BuildCatalogEntry> {assert(build.trim().length > 0, "Build Catalog build id must not be empty");verifyBuildCatalogDescriptor(descriptor);- const existing = await this.read(build);- if (existing !== undefined) {- assert(sameBuildCatalogDescriptor(existing, descriptor),- `Build Catalog ${build} already has another source, Run Source or output naming`);- return existing;- }const now = Date.now();this.#database.prepare(`- INSERT OR IGNORE INTO narratage_build_catalog (- build_id, core_id, created_at, updated_at, descriptor_json- ) VALUES (?, ?, ?, ?, ?)- `).run(build, descriptor.core, now, now, canonicalStringify(descriptor));- const stored = await this.read(build);- if (stored === undefined) throw new Error(`Build Catalog ${build} disappeared after record`);- assert(sameBuildCatalogDescriptor(stored, descriptor),- `Build Catalog ${build} already has another source, Run Source or output naming`);- return stored;+ INSERT INTO narratage_build_catalog (+ build_id, created_at, descriptor_json+ ) VALUES (?, ?, ?)+ `).run(build, now, canonicalStringify(descriptor));+ const entry = { ...copy(descriptor), build, createdAt: now };+ verifyBuildCatalogEntry(entry);+ return entry;}async read(build: string): Promise<BuildCatalogEntry | undefined> {const row = this.#database.prepare(`- SELECT build_id, created_at, updated_at, descriptor_json+ SELECT build_id, created_at, descriptor_jsonFROM narratage_build_catalogWHERE build_id = ?`).get(build) as Row | undefined;@@ -288,7 +250,7 @@ class SqliteBuildCatalog implements BuildCatalog {async list(): Promise<readonly BuildCatalogEntry[]> {const rows = this.#database.prepare(`- SELECT build_id, created_at, updated_at, descriptor_json+ SELECT build_id, created_at, descriptor_jsonFROM narratage_build_catalogORDER BY created_at DESC, build_id ASC`).all() as Row[];@@ -303,30 +265,30 @@ class SqliteOperationStore implements OperationStore {this.#database = database;}- async create(identity: OperationIdentity): Promise<OperationCreate> {- verifyOperationIdentity(identity);+ 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 result = this.#database.prepare(`- INSERT OR IGNORE INTO narratage_operations (- operation_id, revision, status, identity_json,- checkpoint_json, completion_json, failure_json, cancellation_json- ) VALUES (?, 0, 'created', ?, NULL, NULL, NULL, NULL)- `).run(identity.id, canonicalStringify(identity));- const snapshot: OperationSnapshot = {- ...copy(identity),- revision: 0,- status: "created",- };- if (result.changes === 1) return { status: "created", snapshot };- const existing = await this.read(identity.id);- if (existing === undefined) throw new Error(`operation ${identity.id} disappeared after create`);- assert(canonicalStringify(operationIdentity(existing)) === canonicalStringify(identity),- `operation ${identity.id} identity differs from its stored value`);- return { status: "existing", snapshot: existing };+ INSERT INTO narratage_operations (+ operation_id, build_id, command_id, endpoint_id, pool_id, lane_id, status,+ handle_json, completion_json, failure_json+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)+ `).run(identity.id, identity.build, identity.command, identity.endpoint, identity.pool, identity.lane,+ operation.status, handle, completion, failure);+ if (result.changes !== 1) throw new Error(`Operation ${identity.id} was not created`);+ return copy(operation);}- async read(id: Digest): Promise<OperationSnapshot | undefined> {+ async read(id: string): Promise<OperationSnapshot | undefined> {const row = this.#database.prepare(`- SELECT identity_json, revision, status, checkpoint_json, completion_json, failure_json, cancellation_json+ SELECT operation_id, build_id, command_id, endpoint_id, pool_id, lane_id,+ status, handle_json, completion_json, failure_jsonFROM narratage_operationsWHERE operation_id = ?`).get(id) as Row | undefined;@@ -337,57 +299,33 @@ class SqliteOperationStore implements OperationStore {const predicates: string[] = [];const values: string[] = [];for (const [field, value] of [- ["build", query.build],- ["command", query.command],- ["endpoint", query.endpoint],- ["pool", query.pool],- ["lane", query.lane],+ ["build_id", query.build],+ ["command_id", query.command],+ ["endpoint_id", query.endpoint],+ ["pool_id", query.pool],+ ["lane_id", query.lane],] as const) {if (value === undefined) continue;- predicates.push(`json_extract(identity_json, '$.${field}') = ?`);+ predicates.push(`${field} = ?`);values.push(value);}const rows = this.#database.prepare(`- SELECT identity_json, revision, status, checkpoint_json, completion_json, failure_json, cancellation_json+ SELECT operation_id, build_id, command_id, endpoint_id, pool_id, lane_id,+ status, handle_json, completion_json, failure_jsonFROM narratage_operations${predicates.length === 0 ? "" : `WHERE ${predicates.join(" AND ")}`}ORDER BY operation_id ASC`).all(...values) as Row[];return rows.map(parseOperationSnapshot)- .sort((left, right) => left.attempt - right.attempt || left.id.localeCompare(right.id));+ .sort((left, right) => left.id.localeCompare(right.id));}- async compareAndSwap(- id: Digest,- expectedRevision: number,- update: OperationUpdate,- ): Promise<OperationStoreWrite> {- assert(Number.isSafeInteger(expectedRevision) && expectedRevision >= 0, "expected Operation revision is invalid");+ async update(id: string, update: OperationUpdate): Promise<OperationSnapshot> {const current = await this.read(id);if (current === undefined) throw new Error(`Operation ${id} does not exist`);- if (current.revision !== expectedRevision) return { status: "conflict", current };- if (update.status !== "control") {- assert(current.status !== "completed" && current.status !== "failed" && current.status !== "cancelled",- `Operation ${id} is already terminal`);- }- if (update.status === "control") {- const result = this.#database.prepare(`- UPDATE narratage_operations- SET revision = ?, cancellation_json = ?- WHERE operation_id = ? AND revision = ?- `).run(expectedRevision + 1, canonicalStringify(update.cancellation), id, expectedRevision);- if (result.changes !== 1) {- const conflict = await this.read(id);- if (conflict === undefined) throw new Error(`Operation ${id} disappeared during update`);- return { status: "conflict", current: conflict };- }- const stored = await this.read(id);- if (stored === undefined) throw new Error(`Operation ${id} disappeared after update`);- return { status: "stored", snapshot: stored };- }- const checkpoint = update.status === "pending" ? canonicalStringify({- format: "narratage.operation-pending@1",- checkpoint: update.checkpoint,+ 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;@@ -395,76 +333,55 @@ class SqliteOperationStore implements OperationStore {? canonicalStringify(update.completion): null;const failure = update.status === "failed" ? canonicalStringify(update.failure) : null;- const cancellation = update.status === "cancelled"- ? canonicalStringify(update.cancellation)- : current.cancellation === undefined ? null : canonicalStringify(current.cancellation);const result = this.#database.prepare(`UPDATE narratage_operations- SET revision = ?, status = ?, checkpoint_json = ?, completion_json = ?, failure_json = ?, cancellation_json = ?- WHERE operation_id = ? AND revision = ? AND status NOT IN ('completed', 'failed', 'cancelled')- `).run(expectedRevision + 1, update.status, checkpoint, completion, failure, cancellation, id, expectedRevision);- if (result.changes !== 1) {- const conflict = await this.read(id);- if (conflict === undefined) throw new Error(`Operation ${id} disappeared during update`);- return { status: "conflict", current: conflict };- }- const stored = await this.read(id);- if (stored === undefined) throw new Error(`Operation ${id} disappeared after update`);- return { status: "stored", snapshot: stored };+ SET status = ?, handle_json = ?, completion_json = ?, failure_json = ?+ WHERE operation_id = ?+ `).run(update.status, handle, completion, failure, id);+ if (result.changes !== 1) throw new Error(`Operation ${id} disappeared during update`);+ const mutable = update.status === "pending"+ ? {+ status: "pending" as const,+ handle: copy(update.handle),+ ...(update.wakeAt === undefined ? {} : { wakeAt: update.wakeAt }),+ ...(update.progress === undefined ? {} : { progress: copy(update.progress) }),+ }+ : update.status === "completed"+ ? { status: "completed" as const, completion: copy(update.completion) }+ : update.status === "failed"+ ? { status: "failed" as const, failure: copy(update.failure) }+ : { status: "cancelled" as const };+ return { ...operationIdentityFrom(current), ...mutable };}}-function dispatchLease(row: Row): DispatchLease | undefined {- if (row.lease_owner === null || row.lease_owner === undefined) return undefined;- assert(typeof row.lease_owner === "string", "SQLite Dispatch lease owner is invalid");- assert(typeof row.lease_token === "string", "SQLite Dispatch lease token is invalid");- assert(typeof row.lease_fence === "number", "SQLite Dispatch lease fence is invalid");- assert(typeof row.lease_expires_at === "number", "SQLite Dispatch lease expiry is invalid");- return {- owner: row.lease_owner,- token: row.lease_token,- fence: row.lease_fence,- expiresAt: row.lease_expires_at,- };-}function parseDispatchSnapshot(row: Row): BuildDispatchSnapshot {assert(typeof row.identity_json === "string", "SQLite Dispatch row has no identity");- assert(typeof row.revision === "number", "SQLite Dispatch row has no revision");- assert(typeof row.created_at === "number" && typeof row.updated_at === "number", "SQLite Dispatch timestamps are invalid");- assert(typeof row.priority === "number" && typeof row.available_at === "number", "SQLite Dispatch schedule is invalid");- assert(typeof row.admission === "string" && typeof row.phase === "string", "SQLite Dispatch state is invalid");+ assert(typeof row.created_at === "number", "SQLite Dispatch time is invalid");+ assert(typeof row.available_at === "number", "SQLite Dispatch schedule is invalid");+ assert(typeof row.phase === "string", "SQLite Dispatch state is invalid");const identity = JSON.parse(row.identity_json) as BuildDispatchIdentity;- const lease = dispatchLease(row);+ const cancellation = typeof row.cancel_requested_at === "number"+ ? {+ cancellation: {+ requestedAt: row.cancel_requested_at,+ ...(typeof row.cancel_reason === "string" ? { reason: row.cancel_reason } : {}),+ },+ }+ : {};const snapshot = {...identity,- revision: row.revision,createdAt: row.created_at,- updatedAt: row.updated_at,- priority: row.priority,availableAt: row.available_at,- admission: row.admission,phase: row.phase,- ...(lease === undefined ? {} : { lease }),...(typeof row.reason === "string" ? { reason: row.reason } : {}),- ...(typeof row.cancel_requested_at === "number"- ? { cancellation: {- requestedAt: row.cancel_requested_at,- ...(typeof row.cancel_reason === "string" ? { reason: row.cancel_reason } : {}),- } }- : {}),+ ...cancellation,...(typeof row.terminal === "string" ? { terminal: row.terminal } : {}),} as BuildDispatchSnapshot;- verifyBuildDispatchSnapshot(snapshot);return snapshot;}-function leaseMatches(row: Row, lease: DispatchLease): boolean {- return row.lease_owner === lease.owner- && row.lease_token === lease.token- && row.lease_fence === lease.fence;-}-class SqliteBuildDispatchStore implements BuildDispatchStore {readonly #database: DatabaseSync;@@ -474,44 +391,22 @@ class SqliteBuildDispatchStore implements BuildDispatchStore {async create(identity: BuildDispatchIdentity,- options: { readonly priority?: number; readonly availableAt?: number; readonly now?: number } = {},- ): Promise<BuildDispatchCreate> {- verifyBuildDispatchIdentity(identity);- const now = nonNegativeInteger(options.now ?? Date.now(), "Dispatch creation time");- const priority = options.priority ?? 0;- assert(Number.isSafeInteger(priority), "Dispatch priority must be a safe integer");- const availableAt = nonNegativeInteger(options.availableAt ?? now, "Dispatch availableAt");- return transaction(this.#database, () => {- const existingRow = this.#database.prepare(- "SELECT * FROM narratage_dispatches WHERE build_id = ?",- ).get(identity.build) as Row | undefined;- if (existingRow !== undefined) {- const existing = parseDispatchSnapshot(existingRow);- assert(canonicalStringify({- format: existing.format,- id: existing.id,- build: existing.build,- core: existing.core,- implementationPackages: existing.implementationPackages,- }) === canonicalStringify(identity),- `Dispatch ${identity.build} already names another Build`);- return { status: "existing", snapshot: existing };- }-- const result = this.#database.prepare(`- INSERT INTO narratage_dispatches (- build_id, identity_json, revision, created_at, updated_at, priority, available_at,- admission, phase, lease_owner, lease_token, lease_fence, lease_expires_at,- reason, cancel_requested_at, cancel_reason, terminal- ) VALUES (?, ?, 0, ?, ?, ?, ?, 'open', 'queued', NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL)- `).run(identity.build, canonicalStringify(identity), now, now, priority, availableAt);- assert(result.changes === 1, `Dispatch ${identity.build} was not created`);- const createdRow = this.#database.prepare(- "SELECT * FROM narratage_dispatches WHERE build_id = ?",- ).get(identity.build) as Row | undefined;- if (createdRow === undefined) throw new Error(`Dispatch ${identity.build} disappeared after create`);- return { status: "created", snapshot: parseDispatchSnapshot(createdRow) };- });+ options: { readonly now?: number } = {},+ ): Promise<BuildDispatchSnapshot> {+ const now = options.now ?? Date.now();+ nonNegativeInteger(now, "Dispatch creation time");+ this.#database.prepare(`+ INSERT INTO narratage_dispatches (+ build_id, identity_json, created_at, available_at,+ phase, reason, cancel_requested_at, cancel_reason, terminal+ ) VALUES (?, ?, ?, ?, 'queued', NULL, NULL, NULL, NULL)+ `).run(identity.build, canonicalStringify(identity), now, now);+ return {+ ...copy(identity),+ createdAt: now,+ availableAt: now,+ phase: "queued",+ };}async read(build: string): Promise<BuildDispatchSnapshot | undefined> {@@ -520,351 +415,204 @@ class SqliteBuildDispatchStore implements BuildDispatchStore {}async list(query: DispatchQuery = {}): Promise<readonly BuildDispatchSnapshot[]> {- const predicates: string[] = [];- const values: string[] = [];- if (query.phases !== undefined) {- if (query.phases.length === 0) return [];- predicates.push(`phase IN (${query.phases.map(() => "?").join(", ")})`);- values.push(...query.phases);- }- if (query.admission !== undefined) {- predicates.push("admission = ?");- values.push(query.admission);- }const rows = this.#database.prepare(`SELECT * FROM narratage_dispatches- ${predicates.length === 0 ? "" : `WHERE ${predicates.join(" AND ")}`}- ORDER BY priority DESC, available_at ASC, created_at ASC, build_id ASC- `).all(...values) as Row[];- return rows.map(parseDispatchSnapshot);+ ORDER BY created_at ASC, build_id ASC+ `).all() as Row[];+ return rows.map(parseDispatchSnapshot)+ .filter((item) => query.phases === undefined || query.phases.includes(item.phase));}- async claim(request: BuildDispatchClaim): Promise<BuildDispatchSnapshot | undefined> {- assert(request.owner.trim().length > 0 && request.token.trim().length > 0, "Dispatch claim identity is empty");- const now = nonNegativeInteger(request.now, "Dispatch claim time");- const leaseMs = positiveInteger(request.leaseMs, "Dispatch leaseMs");+ async claim(now = Date.now()): Promise<BuildDispatchSnapshot | undefined> {+ nonNegativeInteger(now, "Dispatch claim time");return transaction(this.#database, () => {const row = this.#database.prepare(`- SELECT * FROM narratage_dispatches- WHERE phase != 'terminal'- AND available_at <= ?- AND (lease_owner IS NULL OR lease_expires_at <= ?)- ORDER BY priority DESC, available_at ASC, created_at ASC, build_id ASC+ SELECT build_id FROM narratage_dispatches+ WHERE phase IN ('queued', 'waiting', 'blocked') AND available_at <= ?+ ORDER BY available_at ASC, created_at ASC, build_id ASCLIMIT 1- `).get(now, now) as Row | undefined;+ `).get(now) as Row | undefined;if (row === undefined) return undefined;- assert(typeof row.build_id === "string" && typeof row.revision === "number" && typeof row.lease_fence === "number",- "SQLite Dispatch claim row is invalid");- const fence = row.lease_fence + 1;+ assert(typeof row.build_id === "string", "SQLite ready Dispatch has no Build id");const updated = this.#database.prepare(`UPDATE narratage_dispatches- SET revision = revision + 1, updated_at = ?, phase = 'leased',- lease_owner = ?, lease_token = ?, lease_fence = ?, lease_expires_at = ?- WHERE build_id = ? AND revision = ?- AND phase != 'terminal' AND (lease_owner IS NULL OR lease_expires_at <= ?)- `).run(now, request.owner, request.token, fence, now + leaseMs, row.build_id, row.revision, now);- assert(updated.changes === 1, `Dispatch ${row.build_id} claim lost its transaction`);- const claimed = this.#database.prepare("SELECT * FROM narratage_dispatches WHERE build_id = ?").get(row.build_id) as Row;- return parseDispatchSnapshot(claimed);+ SET phase = 'running'+ WHERE build_id = ? AND phase IN ('queued', 'waiting', 'blocked') AND available_at <= ?+ `).run(row.build_id, now);+ if (updated.changes !== 1) return undefined;+ return parseDispatchSnapshot(+ this.#database.prepare("SELECT * FROM narratage_dispatches WHERE build_id = ?").get(row.build_id) as Row,+ );});}- async heartbeat(build: string, lease: DispatchLease, now: number, leaseMs: number): Promise<BuildDispatchSnapshot> {- verifyDispatchLease(lease);- nonNegativeInteger(now, "Dispatch heartbeat time");- positiveInteger(leaseMs, "Dispatch heartbeat leaseMs");- const updated = this.#database.prepare(`- UPDATE narratage_dispatches- SET revision = revision + 1, updated_at = ?, lease_expires_at = ?- WHERE build_id = ? AND phase = 'leased'- AND lease_owner = ? AND lease_token = ? AND lease_fence = ? AND lease_expires_at > ?- `).run(now, now + leaseMs, build, lease.owner, lease.token, lease.fence, now);- assert(updated.changes === 1, `Dispatch ${build} lease is stale`);- return (await this.read(build))!;- }-- async release(- build: string,- lease: DispatchLease,- update: BuildDispatchRelease,- now = Date.now(),- ): Promise<BuildDispatchSnapshot> {- verifyDispatchLease(lease);- nonNegativeInteger(now, "Dispatch release time");+ async release(build: string, update: BuildDispatchRelease): Promise<BuildDispatchSnapshot> {nonNegativeInteger(update.availableAt, "Dispatch release availableAt");- const updated = this.#database.prepare(`- UPDATE narratage_dispatches- SET revision = revision + 1, updated_at = ?, phase = ?,- available_at = CASE WHEN wake_at IS NULL THEN ? ELSE MIN(?, wake_at) END,- wake_at = NULL, reason = ?,- lease_owner = NULL, lease_token = NULL, lease_expires_at = NULL- WHERE build_id = ? AND phase = 'leased'- AND lease_owner = ? AND lease_token = ? AND lease_fence = ?- `).run(now, update.phase, update.availableAt, update.availableAt, update.reason ?? null,- build, lease.owner, lease.token, lease.fence);- assert(updated.changes === 1, `Dispatch ${build} lease is stale`);- return (await this.read(build))!;+ return transaction(this.#database, () => {+ const row = this.#database.prepare("SELECT * FROM narratage_dispatches WHERE build_id = ?").get(build) as Row | undefined;+ 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;+ assert(current.phase === "running", `Dispatch ${build} is not running`);+ this.#database.prepare(`+ UPDATE narratage_dispatches+ SET phase = ?, available_at = ?, reason = ?+ WHERE build_id = ?+ `).run(update.phase, update.availableAt, update.reason ?? null, build);+ return parseDispatchSnapshot(+ this.#database.prepare("SELECT * FROM narratage_dispatches WHERE build_id = ?").get(build) as Row,+ );+ });}async finish(build: string,- lease: DispatchLease,terminal: "complete" | "failed" | "cancelled",reason?: string,- now = Date.now(),): Promise<BuildDispatchSnapshot> {- verifyDispatchLease(lease);- const updated = this.#database.prepare(`- UPDATE narratage_dispatches- SET revision = revision + 1, updated_at = ?, phase = 'terminal', admission = 'closed',- terminal = ?, reason = ?, wake_at = NULL,- lease_owner = NULL, lease_token = NULL, lease_expires_at = NULL- WHERE build_id = ? AND phase = 'leased'- AND lease_owner = ? AND lease_token = ? AND lease_fence = ?- `).run(now, terminal, reason ?? null, build, lease.owner, lease.token, lease.fence);- assert(updated.changes === 1, `Dispatch ${build} lease is stale`);- return (await this.read(build))!;+ return transaction(this.#database, () => {+ const row = this.#database.prepare("SELECT * FROM narratage_dispatches WHERE build_id = ?").get(build) as Row | undefined;+ if (row === undefined) throw new Error(`Dispatch ${build} does not exist`);+ const current = parseDispatchSnapshot(row);+ if (current.phase === "terminal") return current;+ const effective = current.cancellation === undefined ? terminal : "cancelled";+ this.#database.prepare(`+ UPDATE narratage_dispatches+ SET phase = 'terminal', terminal = ?, reason = ?+ WHERE build_id = ?+ `).run(effective, reason ?? current.cancellation?.reason ?? null, build);+ return parseDispatchSnapshot(+ this.#database.prepare("SELECT * FROM narratage_dispatches WHERE build_id = ?").get(build) as Row,+ );+ });}async requestCancellation(build: string, reason?: string, now = Date.now()): Promise<BuildDispatchSnapshot> {- nonNegativeInteger(now, "cancellation request time");+ nonNegativeInteger(now, "Dispatch cancellation time");return transaction(this.#database, () => {- const row = this.#database.prepare(- "SELECT * FROM narratage_dispatches WHERE build_id = ?",- ).get(build) as Row | undefined;+ const row = this.#database.prepare("SELECT * FROM narratage_dispatches WHERE build_id = ?").get(build) as Row | undefined;if (row === undefined) throw new Error(`Dispatch ${build} does not exist`);const current = parseDispatchSnapshot(row);if (current.phase === "terminal") return current;-- // A zero fence proves that no Worker has ever owned this Build. Cancellation is therefore- // pure queue withdrawal: no Core command, capacity reservation or Provider Operation can- // exist. Resolve it in this same transaction so a racing claim has exactly one winner.- if (current.phase === "queued" && row.lease_fence === 0 && row.lease_owner === null) {- const updated = this.#database.prepare(`+ if (current.phase === "queued") {+ this.#database.prepare(`UPDATE narratage_dispatches- SET revision = revision + 1, updated_at = ?, admission = 'closed', phase = 'terminal',- terminal = 'cancelled', reason = ?, cancel_requested_at = ?, cancel_reason = ?,- wake_at = NULL, lease_owner = NULL, lease_token = NULL, lease_expires_at = NULL- WHERE build_id = ? AND phase = 'queued' AND lease_fence = 0 AND lease_owner IS NULL- `).run(now, reason ?? "cancelled before dispatch", now, reason ?? null, build);- assert(updated.changes === 1, `Dispatch ${build} cancellation lost its transaction`);+ SET phase = 'terminal', terminal = 'cancelled',+ reason = ?, cancel_requested_at = ?, cancel_reason = ?+ WHERE build_id = ?+ `).run(reason ?? "cancelled before execution", now, reason ?? null, build);} else {- const updated = this.#database.prepare(`+ this.#database.prepare(`UPDATE narratage_dispatches- SET revision = revision + 1, updated_at = ?, admission = 'closing',- cancel_requested_at = COALESCE(cancel_requested_at, ?),- cancel_reason = COALESCE(cancel_reason, ?),- available_at = CASE WHEN phase = 'leased' THEN available_at ELSE MIN(available_at, ?) END,- wake_at = CASE WHEN phase = 'leased' THEN MIN(COALESCE(wake_at, ?), ?) ELSE wake_at END- WHERE build_id = ? AND phase != 'terminal'- `).run(now, now, reason ?? null, now, now, now, build);- assert(updated.changes === 1, `Dispatch ${build} cancellation lost its transaction`);+ SET phase = CASE WHEN phase = 'running' THEN phase ELSE 'queued' END,+ available_at = CASE WHEN phase = 'running' THEN available_at ELSE ? END,+ reason = ?, cancel_requested_at = COALESCE(cancel_requested_at, ?),+ cancel_reason = COALESCE(cancel_reason, ?)+ WHERE build_id = ?+ `).run(now, reason ?? "cancellation requested", now, reason ?? null, build);}- const result = this.#database.prepare(- "SELECT * FROM narratage_dispatches WHERE build_id = ?",- ).get(build) as Row | undefined;- assert(result !== undefined, `Dispatch ${build} disappeared after cancellation`);- return parseDispatchSnapshot(result);+ return parseDispatchSnapshot(+ this.#database.prepare("SELECT * FROM narratage_dispatches WHERE build_id = ?").get(build) as Row,+ );});}async wake(build: string, now = Date.now()): Promise<BuildDispatchSnapshot> {nonNegativeInteger(now, "Dispatch wake time");- const updated = this.#database.prepare(`+ this.#database.prepare(`UPDATE narratage_dispatches- SET revision = revision + 1, updated_at = ?,- available_at = CASE WHEN phase = 'leased' THEN available_at ELSE MIN(available_at, ?) END,- wake_at = CASE WHEN phase = 'leased' THEN MIN(COALESCE(wake_at, ?), ?) ELSE wake_at END+ SET available_at = MIN(available_at, ?)WHERE build_id = ? AND phase != 'terminal'- `).run(now, now, now, now, build);- if (updated.changes === 0) {- const current = await this.read(build);- if (current === undefined) throw new Error(`Dispatch ${build} does not exist`);- return current;- }- return (await this.read(build))!;+ `).run(now, build);+ const current = await this.read(build);+ if (current === undefined) throw new Error(`Dispatch ${build} does not exist`);+ return current;}async acquireCapacity(request: CapacityAcquireRequest): Promise<CapacityAcquire> {- assert(request.owner.trim().length > 0 && request.token.trim().length > 0, "Capacity claimant is empty");- assert(request.resources.length > 0, "Capacity resources are empty");nonNegativeInteger(request.now, "Capacity acquisition time");- positiveInteger(request.leaseMs, "Capacity leaseMs");- verifyCapacityLimits(request.limits);- verifyDispatchLease(request.buildLease);- const resources = [...request.resources].map((resource) => ({ ...resource }))- .sort((left, right) => left.id.localeCompare(right.id));- const resourceIds = resources.map((resource) => {- assert(resource.id.trim().length > 0, "Capacity resource id is empty");- positiveInteger(resource.maxActive, `Capacity resource ${resource.id} maxActive`);- positiveInteger(resource.maxInFlight, `Capacity resource ${resource.id} maxInFlight`);- return resource.id;- });- assert(new Set(resourceIds).size === resourceIds.length, "Capacity resources contain duplicates");- if (request.queue !== undefined) {- assert(request.queue.pool.trim().length > 0 && request.queue.lane.trim().length > 0,- "Capacity queue pool and lane are required");- }- const resourcesJson = canonicalStringify(resources);- const queueJson = request.queue === undefined ? null : canonicalStringify(request.queue);- const id = capacityReservationId(request.build, request.command);+ 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,+ ...(request.queue === undefined ? {} : { queue: request.queue }),+ createdAt: request.now,+ };return transaction(this.#database, () => {- const dispatch = this.#database.prepare(`- SELECT lease_owner, lease_token, lease_fence FROM narratage_dispatches- WHERE build_id = ? AND phase = 'leased'- `).get(request.build) as Row | undefined;- assert(dispatch !== undefined && leaseMatches(dispatch, request.buildLease),- `Dispatch ${request.build} lease is stale`);- let existing = this.#database.prepare("SELECT * FROM narratage_capacity WHERE reservation_id = ?").get(id) as Row | undefined;- if (existing === undefined) {- this.#database.prepare(`- INSERT INTO narratage_capacity (- reservation_id, build_id, command_id, resources_json, queue_json, mode, in_flight,- active_owner, active_token, active_fence, active_expires_at, created_at, updated_at- ) VALUES (?, ?, ?, ?, ?, ?, 0, NULL, NULL, 0, NULL, ?, ?)- `).run(id, request.build, request.command, resourcesJson, queueJson, request.mode,- request.now, request.now);- existing = this.#database.prepare("SELECT * FROM narratage_capacity WHERE reservation_id = ?").get(id) as Row;+ const existing = this.#database.prepare(+ "SELECT * FROM narratage_capacity WHERE reservation_id = ?",+ ).get(reservation.id) as Row | undefined;+ if (existing !== undefined) {+ return { status: "acquired", reservation: parseCapacityReservation(existing) };}- assert(existing.build_id === request.build && existing.command_id === request.command- && existing.resources_json === resourcesJson && existing.queue_json === queueJson- && existing.mode === request.mode,- `Capacity reservation ${id} identity differs`);- if (existing !== undefined && typeof existing.active_expires_at === "number" && existing.active_expires_at > request.now) {- return { status: "blocked", retryAt: existing.active_expires_at, reason: "resource-active" };- }- const activeGlobal = this.#database.prepare(`- SELECT COUNT(*) AS count FROM narratage_capacity- WHERE active_owner IS NOT NULL AND active_expires_at > ? AND reservation_id != ?- `).get(request.now, id) as Row;- assert(typeof activeGlobal.count === "number", "SQLite global Capacity count is invalid");- const retryAtRow = this.#database.prepare(`- SELECT MIN(active_expires_at) AS retry_at FROM narratage_capacity- WHERE active_owner IS NOT NULL AND active_expires_at > ?- `).get(request.now) as Row;- const retryAt = typeof retryAtRow.retry_at === "number" ? retryAtRow.retry_at : request.now + 100;- if (activeGlobal.count >= request.limits.globalActive) return { status: "blocked", retryAt, reason: "global-active" };- const alreadyInFlight = existing?.in_flight === 1;for (const resource of resources) {- const active = this.#database.prepare(`+ const count = this.#database.prepare(`SELECT COUNT(DISTINCT capacity.reservation_id) AS countFROM narratage_capacity AS capacity, json_each(capacity.resources_json) AS claimWHERE json_extract(claim.value, '$.id') = ?- AND capacity.active_owner IS NOT NULL AND capacity.active_expires_at > ?- AND capacity.reservation_id != ?- `).get(resource.id, request.now, id) as Row;- const inFlight = this.#database.prepare(`- SELECT COUNT(DISTINCT capacity.reservation_id) AS count- FROM narratage_capacity AS capacity, json_each(capacity.resources_json) AS claim- WHERE json_extract(claim.value, '$.id') = ?- AND capacity.in_flight = 1 AND capacity.reservation_id != ?- `).get(resource.id, id) as Row;- assert(typeof active.count === "number" && typeof inFlight.count === "number",- `SQLite Capacity counts for ${resource.id} are invalid`);- if (active.count >= resource.maxActive) {- return { status: "blocked", retryAt, reason: "resource-active", resource: resource.id };- }- if (request.mode === "recoverable" && !alreadyInFlight && inFlight.count >= resource.maxInFlight) {- return { status: "blocked", retryAt, reason: "resource-in-flight", resource: resource.id };+ `).get(resource.id) as Row;+ assert(typeof count.count === "number", `SQLite Capacity count for ${resource.id} is invalid`);+ if (count.count >= resource.maxInFlight) {+ return {+ status: "blocked",+ availableAt: request.now + 250,+ reason: "resource-in-flight",+ resource: resource.id,+ };}}- const fence = typeof existing?.active_fence === "number" ? existing.active_fence + 1 : 1;this.#database.prepare(`- UPDATE narratage_capacity- SET in_flight = MAX(in_flight, ?), active_owner = ?, active_token = ?,- active_fence = ?, active_expires_at = ?, updated_at = ?- WHERE reservation_id = ?- `).run(request.mode === "recoverable" ? 1 : 0, request.owner, request.token,- fence, request.now + request.leaseMs, request.now, id);- return { status: "acquired", reservation: parseCapacityReservation(- this.#database.prepare("SELECT * FROM narratage_capacity WHERE reservation_id = ?").get(id) as Row,- ) };+ INSERT INTO narratage_capacity (+ reservation_id, 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),+ reservation.createdAt,+ );+ return { status: "acquired", reservation };});}- async heartbeatCapacity(id: Digest, lease: DispatchLease, now: number, leaseMs: number): Promise<CapacityReservation> {- verifyDispatchLease(lease, "Capacity lease");- const updated = this.#database.prepare(`- UPDATE narratage_capacity SET active_expires_at = ?, updated_at = ?- WHERE reservation_id = ? AND active_owner = ? AND active_token = ? AND active_fence = ? AND active_expires_at > ?- `).run(now + leaseMs, now, id, lease.owner, lease.token, lease.fence, now);- assert(updated.changes === 1, `Capacity reservation ${id} lease is stale`);- return parseCapacityReservation(this.#database.prepare("SELECT * FROM narratage_capacity WHERE reservation_id = ?").get(id) as Row);+ async releaseCapacity(id: string): Promise<void> {+ this.#database.prepare("DELETE FROM narratage_capacity WHERE reservation_id = ?").run(id);}- async parkCapacity(id: Digest, lease: DispatchLease, inFlight: boolean, now = Date.now()): Promise<CapacityReservation> {- verifyDispatchLease(lease, "Capacity lease");- const updated = this.#database.prepare(`- UPDATE narratage_capacity- SET in_flight = ?, active_owner = NULL, active_token = NULL, active_expires_at = NULL, updated_at = ?- WHERE reservation_id = ? AND active_owner = ? AND active_token = ? AND active_fence = ?- `).run(inFlight ? 1 : 0, now, id, lease.owner, lease.token, lease.fence);- assert(updated.changes === 1, `Capacity reservation ${id} lease is stale`);- return parseCapacityReservation(this.#database.prepare("SELECT * FROM narratage_capacity WHERE reservation_id = ?").get(id) as Row);- }-- async releaseCapacity(id: Digest, lease: DispatchLease): Promise<void> {- verifyDispatchLease(lease, "Capacity lease");- const removed = this.#database.prepare(`- DELETE FROM narratage_capacity- WHERE reservation_id = ? AND active_owner = ? AND active_token = ? AND active_fence = ?- `).run(id, lease.owner, lease.token, lease.fence);- assert(removed.changes === 1, `Capacity reservation ${id} lease is stale`);- }-- async clearCapacity(id: Digest, build: string, buildLease: DispatchLease): Promise<void> {- verifyDispatchLease(buildLease);- transaction(this.#database, () => {- const dispatch = this.#database.prepare(`- SELECT lease_owner, lease_token, lease_fence FROM narratage_dispatches- WHERE build_id = ? AND phase = 'leased'- `).get(build) as Row | undefined;- assert(dispatch !== undefined && leaseMatches(dispatch, buildLease),- `Dispatch ${build} lease is stale`);- this.#database.prepare(`- DELETE FROM narratage_capacity WHERE reservation_id = ? AND build_id = ?- `).run(id, build);- });+ async releaseBuildCapacity(build: string): Promise<void> {+ this.#database.prepare("DELETE FROM narratage_capacity WHERE build_id = ?").run(build);}async listCapacity(): Promise<readonly CapacityReservation[]> {- return (this.#database.prepare("SELECT * FROM narratage_capacity ORDER BY created_at ASC, reservation_id ASC").all() as Row[])- .map(parseCapacityReservation);+ return (this.#database.prepare(+ "SELECT * FROM narratage_capacity ORDER BY created_at ASC, reservation_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" && typeof row.mode === "string",- "SQLite Capacity row identity is invalid");- assert(typeof row.in_flight === "number" && typeof row.created_at === "number" && typeof row.updated_at === "number",- "SQLite Capacity row state is invalid");- const active = row.active_owner === null || row.active_owner === undefined ? undefined : (() => {- assert(typeof row.active_owner === "string" && typeof row.active_token === "string"- && typeof row.active_fence === "number" && typeof row.active_expires_at === "number",- "SQLite Capacity active lease is invalid");- return { owner: row.active_owner, token: row.active_token, fence: row.active_fence, expiresAt: row.active_expires_at };- })();+ && typeof row.command_id === "string" && typeof row.resources_json === "string"+ && typeof row.created_at === "number", "SQLite Capacity row is invalid");const value = {- format: "narratage.capacity-reservation@1" as const,- id: row.reservation_id as Digest,+ id: row.reservation_id,build: row.build_id,command: row.command_id,resources: JSON.parse(row.resources_json) as CapacityReservation["resources"],...(typeof row.queue_json === "string"? { queue: JSON.parse(row.queue_json) as NonNullable<CapacityReservation["queue"]> }: {}),- mode: row.mode,- inFlight: row.in_flight === 1,- ...(active === undefined ? {} : { active }),createdAt: row.created_at,- updatedAt: row.updated_at,- } as CapacityReservation;- verifyCapacityReservation(value);+ };return value;}-/** One local database with domain-neutral execution facts and Host presentation catalog. */export class SqliteRuntimeState {readonly path: string;readonly builds: BuildStore;@@ -915,65 +663,54 @@ export class SqliteRuntimeState {) STRICT;CREATE TABLE IF NOT EXISTS narratage_builds (build_id TEXT PRIMARY KEY,- revision INTEGER NOT NULL,- state_json TEXT NOT NULL+ definition_json TEXT NOT NULL+ ) STRICT;+ CREATE TABLE IF NOT EXISTS narratage_build_facts (+ sequence INTEGER PRIMARY KEY AUTOINCREMENT,+ build_id TEXT NOT NULL,+ command_id TEXT NOT NULL,+ fact_json TEXT NOT NULL,+ UNIQUE (build_id, command_id)) STRICT;CREATE TABLE IF NOT EXISTS narratage_operations (operation_id TEXT PRIMARY KEY,- revision INTEGER NOT NULL,- status TEXT NOT NULL CHECK (status IN ('created', 'pending', 'completed', 'failed', 'cancelled')),- identity_json TEXT NOT NULL,- checkpoint_json TEXT,+ build_id TEXT NOT NULL,+ command_id TEXT NOT NULL,+ endpoint_id TEXT NOT NULL,+ 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,- cancellation_json TEXT+ failure_json TEXT) STRICT;CREATE TABLE IF NOT EXISTS narratage_build_catalog (build_id TEXT PRIMARY KEY,- core_id TEXT NOT NULL,created_at INTEGER NOT NULL,- updated_at INTEGER NOT NULL,descriptor_json TEXT NOT NULL) STRICT;CREATE TABLE IF NOT EXISTS narratage_dispatches (build_id TEXT PRIMARY KEY,identity_json TEXT NOT NULL,- revision INTEGER NOT NULL,created_at INTEGER NOT NULL,- updated_at INTEGER NOT NULL,- priority INTEGER NOT NULL,available_at INTEGER NOT NULL,- wake_at INTEGER,- admission TEXT NOT NULL CHECK (admission IN ('open', 'closing', 'closed')),- phase TEXT NOT NULL CHECK (phase IN ('queued', 'leased', 'waiting', 'blocked', 'settling', 'terminal')),- lease_owner TEXT,- lease_token TEXT,- lease_fence INTEGER NOT NULL,- lease_expires_at INTEGER,+ phase TEXT NOT NULL CHECK (phase IN ('queued', 'running', 'waiting', 'blocked', 'terminal')),reason TEXT,cancel_requested_at INTEGER,cancel_reason TEXT,terminal TEXT CHECK (terminal IS NULL OR terminal IN ('complete', 'failed', 'cancelled'))) STRICT;CREATE INDEX IF NOT EXISTS narratage_dispatch_ready- ON narratage_dispatches (phase, available_at, priority DESC);+ ON 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,- mode TEXT NOT NULL CHECK (mode IN ('active', 'recoverable')),- in_flight INTEGER NOT NULL CHECK (in_flight IN (0, 1)),- active_owner TEXT,- active_token TEXT,- active_fence INTEGER NOT NULL,- active_expires_at INTEGER,created_at INTEGER NOT NULL,- updated_at INTEGER NOT NULL,UNIQUE (build_id, command_id)) STRICT;- CREATE INDEX IF NOT EXISTS narratage_capacity_state ON narratage_capacity (in_flight, active_expires_at);`);if (alreadyInitialized === undefined) {this.#database.prepare("INSERT INTO narratage_store_meta (singleton, schema_version) VALUES (1, ?)").run(databaseSchemaVersion);diff --git a/packages/store-sqlite/test/store.test.ts b/packages/store-sqlite/test/store.test.tsindex d6841b2c..0374c3a1 100644--- a/packages/store-sqlite/test/store.test.ts+++ b/packages/store-sqlite/test/store.test.ts@@ -3,12 +3,10 @@ import { mkdtemp, rm, stat } from "node:fs/promises";import { join } from "node:path";import { tmpdir } from "node:os";import test from "node:test";--import { digestOf } from "@narratage/core";+import { BuildMachine, buildDefinition } from "@narratage/core";import {createBuildDispatchIdentity,nonTerminalDispatchPhases,- operationCancellationRequestId,sealOperationIdentity,} from "@narratage/runtime";import {@@ -32,252 +30,116 @@ test("read-only SQLite observation of an absent archive creates no file", asyncawait rm(directory, { recursive: true, force: true });}});--test("SQLite stores verified Build facts and Operation checkpoints across reopen", async () => {+test("SQLite stores verified Build facts and Operation handles across reopen", async () => {const directory = await mkdtemp(join(tmpdir(), "narratage-sqlite-"));const path = join(directory, "runtime.sqlite");try {const first = new SqliteRuntimeState(path);const initial = createGreetingBuild();- const created = await first.builds.create("video", initial);- assert.equal(created.revision, 0);+ const definition = buildDefinition(initial);+ await first.builds.create("video", definition);const catalog = {format: "narratage.build-catalog-descriptor@1",- core: initial.id,- source: { path: "/project/main.svml", closure: digestOf("source-closure") },+ source: { path: "/project/main.svml" },aliases: [{name: "final.video",ref: { kind: "logical-output", id: initial.request.targets[0]!.output },}],} as const;await first.catalog.record("video", catalog);- assert.equal((await first.catalog.record("video", catalog)).aliases[0]?.name, "final.video");- await assert.rejects(first.catalog.record("video", {- ...catalog,- aliases: [{ ...catalog.aliases[0], name: "renamed.video" }],- }), /another source, Run Source or output naming/u);+ assert.equal((await first.catalog.read("video"))?.aliases[0]?.name, "final.video");const operation = sealOperationIdentity({+ id: "operation:generation",build: "video",command: "command:generation",endpoint: "kie.personal",pool: "kie.personal",lane: "fixture.generation",- attempt: 1,});- const operationCreated = await first.operations.create(operation);- assert.equal(operationCreated.status, "created");- const pending = await first.operations.compareAndSwap(operation.id, 0, {+ const pending = await first.operations.create({ ...operation,status: "pending",- checkpoint: { remoteJob: "job-1" },+ handle: { remoteJob: "job-1" },wakeAt: 12_345,progress: { phase: "generating" },});- assert.equal(pending.status, "stored");+ assert.equal(pending.status, "pending");first.close();const second = new SqliteRuntimeState(path);- assert.equal((await second.builds.read("video"))?.state.id, initial.id);+ assert.equal((await second.builds.read("video"))?.state.status, initial.status);assert.equal((await second.catalog.read("video"))?.aliases[0]?.name, "final.video");assert.deepEqual((await second.catalog.list()).map((item) => item.build), ["video"]);const restoredOperation = await second.operations.read(operation.id);assert.equal(restoredOperation?.status, "pending");- assert.deepEqual(restoredOperation?.checkpoint, { remoteJob: "job-1" });+ assert.deepEqual(restoredOperation?.handle, { remoteJob: "job-1" });assert.equal(restoredOperation?.wakeAt, 12_345);assert.deepEqual(restoredOperation?.progress, { phase: "generating" });assert.deepEqual((await second.operations.list({ build: "video" })).map((item) => item.id), [operation.id]);- const updated = await second.builds.compareAndSwap("video", 0, initial);- assert.equal(updated.status, "stored");- const stale = await second.builds.compareAndSwap("video", 0, initial);- assert.equal(stale.status, "conflict");- if (stale.status === "conflict") assert.equal(stale.current.revision, 1);+ const machine = new BuildMachine(definition);+ const command = machine.commands()[0]!;+ const content = { kind: "command-failed", command: command.id, code: "TEST", message: "test" } as const;+ const fact = machine.evaluate(content)!;+ await second.builds.append("video", fact);+ assert.equal((await second.builds.read("video"))?.facts.length, 1);second.close();} finally {await rm(directory, { recursive: true, force: true });}});-test("SQLite Operation CAS preserves a terminal completion", async () => {+test("SQLite Operation updates preserve a terminal completion", async () => {const directory = await mkdtemp(join(tmpdir(), "narratage-operation-"));try {const state = new SqliteRuntimeState(join(directory, "runtime.sqlite"));const identity = sealOperationIdentity({+ id: "operation:render",build: "video",command: "command:render",endpoint: "hyperframes.lambda",pool: "hyperframes.lambda",lane: "fixture.render",- attempt: 1,});- await state.operations.create(identity);- const completed = await state.operations.compareAndSwap(identity.id, 0, {+ const completed = await state.operations.create({ ...identity,status: "completed",completion: {value: { kind: "inline", value: { artifact: "video.mp4" } },},});- assert.equal(completed.status, "stored");+ assert.equal(completed.status, "completed");assert.equal((await state.operations.read(identity.id))?.status, "completed");- await assert.rejects(- state.operations.compareAndSwap(identity.id, 1, {- status: "pending",- checkpoint: { task: "tampered" },- }),- /already terminal/u,- );- state.close();- } finally {- await rm(directory, { recursive: true, force: true });- }-});--test("SQLite keeps cancellation control independent from execution state", async () => {- const directory = await mkdtemp(join(tmpdir(), "narratage-sqlite-cancel-"));- try {- const state = new SqliteRuntimeState(join(directory, "runtime.sqlite"));- const identity = sealOperationIdentity({- build: "cancel-build",- command: "command:cancel",- endpoint: "endpoint.cancel",- pool: "endpoint.cancel",- lane: "fixture.cancel",- attempt: 1,- });- await state.operations.create(identity);- const requestId = operationCancellationRequestId(identity.id, 100);- const controlled = await state.operations.compareAndSwap(identity.id, 0, {- status: "control",- cancellation: { requestedAt: 100, requestId, status: "unsupported", attempts: 1 },- });- assert.equal(controlled.status, "stored");- const completed = await state.operations.compareAndSwap(identity.id, 1, {- status: "completed",- completion: {- value: { kind: "inline", value: "late result" },- },- });- assert.equal(completed.status, "stored");- const reopened = await state.operations.read(identity.id);- assert.equal(reopened?.status, "completed");- assert.equal(reopened?.cancellation?.status, "unsupported");- state.close();- } finally {- await rm(directory, { recursive: true, force: true });- }-});--test("SQLite fences expired Workers and shares capacity across Build dispatches", async () => {- const directory = await mkdtemp(join(tmpdir(), "narratage-sqlite-dispatch-"));- try {- const state = new SqliteRuntimeState(join(directory, "runtime.sqlite"));- await state.dispatch.create(createBuildDispatchIdentity({- build: "build-a", core: digestOf("core:a"),- }), { now: 100 });- const first = await state.dispatch.claim({ owner: "worker-a", token: "lease-a", now: 100, leaseMs: 10 });- assert.ok(first?.lease);- assert.equal(first.build, "build-a");- assert.equal(first.lease.fence, 1);- assert.equal(await state.dispatch.claim({ owner: "worker-b", token: "early", now: 105, leaseMs: 10 }), undefined);- const second = await state.dispatch.claim({ owner: "worker-b", token: "lease-b", now: 111, leaseMs: 10 });- assert.ok(second?.lease);- assert.equal(second.lease.fence, 2);- const paidResources = [{ id: "pool:paid", maxActive: 1, maxInFlight: 1 }];- await assert.rejects(- state.dispatch.release("build-a", first.lease, { phase: "waiting", availableAt: 120 }, 112),- /stale/u,- );- await assert.rejects(- state.dispatch.acquireCapacity({- build: "build-a", command: "command:a", resources: paidResources, mode: "recoverable",- buildLease: first.lease, owner: "worker-a", token: "capacity-a", now: 112, leaseMs: 10,- limits: { globalActive: 1 },- }),- /stale/u,- );- const reserved = await state.dispatch.acquireCapacity({- build: "build-a", command: "command:a", resources: paidResources, mode: "recoverable",- buildLease: second.lease, owner: "worker-b", token: "capacity-b", now: 112, leaseMs: 10,- limits: { globalActive: 1 },- });- assert.equal(reserved.status, "acquired");-- // A control request can race with the leased Worker's final release. The Store must retain the- // wake independently of the old ready timestamp so that release cannot overwrite it.- await state.dispatch.wake("build-a", 113);- const woken = await state.dispatch.release(- "build-a",- second.lease,- { phase: "waiting", availableAt: 10_000 },- 114,- );- assert.equal(woken.availableAt, 113);- assert.equal(woken.phase, "waiting");-- await state.dispatch.create(createBuildDispatchIdentity({- build: "build-b", core: digestOf("core:b"),- }), { now: 112 });- const third = await state.dispatch.claim({ owner: "worker-c", token: "lease-c", now: 112, leaseMs: 10 });- assert.ok(third?.lease);- const blocked = await state.dispatch.acquireCapacity({- build: "build-b", command: "command:b", resources: paidResources, mode: "recoverable",- buildLease: third.lease, owner: "worker-c", token: "capacity-c", now: 112, leaseMs: 10,- limits: { globalActive: 1 },- });- assert.equal(blocked.status, "blocked");-- await state.dispatch.create(createBuildDispatchIdentity({- build: "build-c", core: digestOf("core:c"),- }), { now: 200, priority: 100 });- const fourth = await state.dispatch.claim({ owner: "worker-d", token: "lease-d", now: 200, leaseMs: 10 });- assert.ok(fourth?.lease);- await state.dispatch.requestCancellation("build-c", "stop", 201);- const cancelledWake = await state.dispatch.release(- "build-c",- fourth.lease,- { phase: "waiting", availableAt: 20_000 },- 202,- );- assert.equal(cancelledWake.admission, "closing");- assert.equal(cancelledWake.availableAt, 201,- "a cancellation racing with release cannot be delayed by the Worker's stale schedule");+ assert.equal((await state.operations.update(identity.id, {+ status: "pending",+ handle: { task: "ignored" },+ })).status, "completed");state.close();} finally {await rm(directory, { recursive: true, force: true });}});-test("cancelling a never-claimed Build atomically withdraws it from dispatch", async () => {const directory = await mkdtemp(join(tmpdir(), "narratage-sqlite-cancel-queued-"));try {const state = new SqliteRuntimeState(join(directory, "runtime.sqlite"));- await state.dispatch.create(createBuildDispatchIdentity({- build: "queued-build",- core: digestOf("core:queued-build"),- }), { now: 100 });+ await state.dispatch.create(createBuildDispatchIdentity({ build: "queued-build" }), { now: 100 });const cancelled = await state.dispatch.requestCancellation("queued-build", "no longer needed", 101);assert.equal(cancelled.phase, "terminal");- assert.equal(cancelled.admission, "closed");assert.equal(cancelled.terminal, "cancelled");assert.equal(cancelled.cancellation?.requestedAt, 101);assert.equal(cancelled.cancellation?.reason, "no longer needed");- assert.equal(await state.dispatch.claim({- owner: "worker",- token: "lease",- now: 102,- leaseMs: 1_000,- }), undefined, "a cancelled queued Build can never be claimed");+ assert.equal(await state.dispatch.claim(102), undefined);assert.deepEqual(await state.dispatch.list({ phases: nonTerminalDispatchPhases }), []);const repeated = await state.dispatch.requestCancellation("queued-build", "second reason", 103);- assert.deepEqual(repeated, cancelled, "terminal cancellation is idempotent");+ assert.deepEqual(repeated, cancelled);state.close();} finally {await rm(directory, { recursive: true, force: true });}});-test("Pool and Lane resources are acquired atomically across models", async () => {+test("Pool and Lane limits count only asynchronous Operations still in flight", async () => {const directory = await mkdtemp(join(tmpdir(), "narratage-sqlite-hierarchy-"));try {const state = new SqliteRuntimeState(join(directory, "runtime.sqlite"));@@ -285,77 +147,47 @@ test("Pool and Lane resources are acquired atomically across models", async () =const seedance = { id: "lane:kie.main/seedance-2-mini", maxActive: 1, maxInFlight: 1 };const minimax = { id: "lane:kie.main/minimax-h3", maxActive: 2, maxInFlight: 2 };- const leaseBuild = async (build: string, now: number) => {- await state.dispatch.create(createBuildDispatchIdentity({- build,- core: digestOf(`core:${build}`),- }), { now });- const claimed = await state.dispatch.claim({- owner: `worker:${build}`,- token: `lease:${build}`,- now,- leaseMs: 1_000,- });- assert.equal(claimed?.build, build);- assert.ok(claimed.lease);- return claimed.lease;- };-- const firstLease = await leaseBuild("seedance-a", 100);const first = await state.dispatch.acquireCapacity({build: "seedance-a",command: "generate:a",resources: [pool, seedance],queue: { pool: "kie.main", lane: "seedance-2-mini" },- mode: "recoverable",- buildLease: firstLease,- owner: "worker:seedance-a",- token: "capacity:a",now: 100,- leaseMs: 1_000,- limits: { globalActive: 10 },});assert.equal(first.status, "acquired");- assert.ok(first.reservation.active);- await state.dispatch.parkCapacity(first.reservation.id, first.reservation.active, true, 101);- const secondLease = await leaseBuild("seedance-b", 102);const sameRoute = await state.dispatch.acquireCapacity({build: "seedance-b",command: "generate:b",resources: [pool, seedance],queue: { pool: "kie.main", lane: "seedance-2-mini" },- mode: "recoverable",- buildLease: secondLease,- owner: "worker:seedance-b",- token: "capacity:b",now: 102,- leaseMs: 1_000,- limits: { globalActive: 10 },});assert.deepEqual(sameRoute, {status: "blocked",- retryAt: 202,+ availableAt: 352,reason: "resource-in-flight",resource: seedance.id,});- const thirdLease = await leaseBuild("minimax-a", 103);const otherRoute = await state.dispatch.acquireCapacity({build: "minimax-a",command: "generate:c",resources: [pool, minimax],queue: { pool: "kie.main", lane: "minimax-h3" },- mode: "recoverable",- buildLease: thirdLease,- owner: "worker:minimax-a",- token: "capacity:c",now: 103,- leaseMs: 1_000,- limits: { globalActive: 10 },});- assert.equal(otherRoute.status, "acquired",- "another model may use the remaining Provider Pool capacity");+ assert.equal(otherRoute.status, "acquired");++ if (first.status === "acquired") await state.dispatch.releaseCapacity(first.reservation.id);+ const retried = await state.dispatch.acquireCapacity({+ build: "seedance-b",+ command: "generate:b",+ resources: [pool, seedance],+ queue: { pool: "kie.main", lane: "seedance-2-mini" },+ now: 104,+ });+ assert.equal(retried.status, "acquired");state.close();} finally {await rm(directory, { recursive: true, force: true });diff --git a/packages/svs/src/frontend.ts b/packages/svs/src/frontend.tsindex 0cae6759..d08002e1 100644--- a/packages/svs/src/frontend.ts+++ b/packages/svs/src/frontend.ts@@ -17,7 +17,6 @@ export const svsFrontend: AuthorFrontend = {id: recipe.value.path,type: svsRecipeType,value: { kind: "inline", value: recipe.value },- origin: { kind: "authored" },}));const exports: AuthorSourceExport[] = records.map((record) => ({name: record.id,diff --git a/packages/svs/src/manifest.ts b/packages/svs/src/manifest.tsindex 0691d011..e8240882 100644--- a/packages/svs/src/manifest.ts+++ b/packages/svs/src/manifest.ts@@ -1,4 +1,3 @@-import { digestOf } from "@narratage/protocol";import type { ModuleManifest, TypeRef } from "@narratage/protocol";export const svsModuleRef = { name: "@narratage/svs", version: "1" } as const;diff --git a/packages/svs/test/source-closure.test.ts b/packages/svs/test/source-closure.test.tsindex 2f78077c..b28ccb30 100644--- a/packages/svs/test/source-closure.test.ts+++ b/packages/svs/test/source-closure.test.ts@@ -1,10 +1,7 @@import assert from "node:assert/strict";import test from "node:test";-import {- computeModuleDigest,createResolvedClosure,- digestOf,sealBuildRequest,start,} from "@narratage/core";@@ -14,7 +11,6 @@ import {compileSourceClosure,resolveCompiledSourceExport,sealGraphFragment,- verifySourceClosure,} from "@narratage/elaborator";import type { AuthorSourceUnit } from "@narratage/elaborator";import type {@@ -41,7 +37,7 @@ const laboratory = { name: "example.recipe-card", version: "1" } as const;const cardType = { module: laboratory, name: "Card" } satisfies TypeRef;const cardAppearanceType = { module: laboratory, name: "CardAppearance" } satisfies TypeRef;const renderProducer = { module: laboratory, name: "render-card" } satisfies ProducerRef;-const cardSurfaceDigest = digestOf("example.recipe-card/card-surface@1");+const cardSurfaceDigest = "surface:example.recipe-card/card";const cardSurface = {name: "card", tag: "Card", mode: "structured", outputs: [cardAppearanceType],} as const;@@ -133,7 +129,6 @@ function sourceRegistry(): MarkupSurfaceRegistry {kind: "inline",value: {contract: "example.card-appearance@1",- sourceRecipeDigest: resolved.record?.digest ?? "",fill,padding,},@@ -214,7 +209,6 @@ test("Text and SVS recursively compile one aliased Recipe into a Core BuildPlan"kind: "inline",value: {contract: "example.card-appearance@1",- sourceRecipeDigest: recipe?.digest,fill: "#73FBD3",padding: "16 24",},@@ -222,7 +216,6 @@ test("Text and SVS recursively compile one aliased Recipe into a Core BuildPlan"const target = resolveCompiledSourceExport(compiled, "answer.result", cardType);assert.equal(target.ref.kind, "logical-output");const state = start(compiled.program, compiled.graph, sealBuildRequest({- graph: compiled.graph.id,targets: [{ output: target.ref.kind === "logical-output" ? target.ref.id : "" }],}));assert.equal(state.plan.steps.length, 1);@@ -242,62 +235,6 @@ test("the consuming package rejects an invalid generic Recipe during author comp);});-test("source import alias changes source identity but not Recipe or Graph semantics", async () => {- const studio = await compileMain("studio");- const brand = await compileMain("brand");- assert.notEqual(studio.closure.id, brand.closure.id);- const studioAppearance = studio.program.records.find((record) => record.type.name === cardAppearanceType.name);- const brandAppearance = brand.program.records.find((record) => record.type.name === cardAppearanceType.name);- assert.equal(studioAppearance?.id, brandAppearance?.id);- assert.equal(studioAppearance?.digest, brandAppearance?.digest);- assert.equal(studio.graph.id, brand.graph.id);-});--test("relocating the same source tree preserves Source Closure and Graph identity", async () => {- const original = await compileMain("studio", "/project");- const relocated = await compileMain("studio", "/copy/project");- assert.equal(original.closure.id, relocated.closure.id);- assert.equal(original.program.semanticDigest, relocated.program.semanticDigest);- assert.equal(original.graph.id, relocated.graph.id);-});--test("Frontend identity changes Source Closure identity but not equal decoded author meaning", async () => {- const alternate = {- ...svsFrontend,- id: "example.svs-compatible@1",- };- const compileWith = async (frontend: typeof svsFrontend | typeof alternate) => {- const frontends = new AuthorFrontendRegistry();- frontends.register(frontend);- return await compileSourceClosure({- entry: unit("/project/studio.any", styleText, frontend.id),- closure,- frontends,- resolveSource() { throw new Error("not used"); },- });- };- const official = await compileWith(svsFrontend);- const compatible = await compileWith(alternate);- assert.notEqual(official.closure.id, compatible.closure.id);- assert.equal(official.closure.units[0]?.semanticDigest, compatible.closure.units[0]?.semanticDigest);- assert.equal(official.program.semanticDigest, compatible.program.semanticDigest);- assert.equal(official.graph.id, compatible.graph.id);-});--test("Source Closure binds every recursive SourceUnit digest", async () => {- const compiled = await compileMain("studio");- const first = compiled.closure.units[0]!;- const tampered = {- ...compiled.closure,- units: [{ ...first, sourceDigest: digestOf("tampered source") }, ...compiled.closure.units.slice(1)],- };- assert.throws(- () => verifySourceClosure(tampered),- (error: unknown) => error instanceof SourceClosureError- && error.code === "SOURCE_UNIT_DIGEST_MISMATCH",- );-});-test("Source Closure rejects recursive source import cycles", async () => {const frontends = new AuthorFrontendRegistry();frontends.register(createMarkupAuthorFrontend({diff --git a/packages/temporal/src/index.ts b/packages/temporal/src/index.tsindex b352af67..9bc03329 100644--- a/packages/temporal/src/index.ts+++ b/packages/temporal/src/index.ts@@ -1,4 +1,3 @@-import { digestOf } from "@narratage/protocol";import type { ModuleManifest } from "@narratage/protocol";export * from "./location.js";diff --git a/packages/text/src/frontend.ts b/packages/text/src/frontend.tsindex 832cf15d..2507a110 100644--- a/packages/text/src/frontend.ts+++ b/packages/text/src/frontend.ts@@ -27,7 +27,6 @@ export const textSvsFrontend: AuthorFrontend = {id,type: textTypes.template,value: { kind: "inline", value: template as unknown as import("@narratage/protocol").CanonicalValue },- origin: { kind: "authored" },});const exports: AuthorSourceExport[] = [{ name: id, ref: { kind: "record", id }, type: textTypes.template }];return {diff --git a/packages/transport-aws-lambda/src/index.ts b/packages/transport-aws-lambda/src/index.tsindex 5c786827..460cd672 100644--- a/packages/transport-aws-lambda/src/index.ts+++ b/packages/transport-aws-lambda/src/index.ts@@ -74,8 +74,7 @@ class AwsSdkLambdaInvocationClient implements LambdaInvocationClient {}/**- * Synchronous JSON transport. Endpoint packages still own request shape, checkpoint law,- * capability identity and conversion into an Endpoint outcome.+ * Synchronous JSON transport. Endpoint packages still own request shape and outcomes.*/export class AwsLambdaJsonInvoker implements JsonInvoker {readonly #client: LambdaInvocationClient;diff --git a/packages/typography-track/src/manifest.ts b/packages/typography-track/src/manifest.tsindex 3c862c34..56a33b62 100644--- a/packages/typography-track/src/manifest.ts+++ b/packages/typography-track/src/manifest.ts@@ -20,7 +20,6 @@ import {import { VISUAL_STYLE_ENUM_VALUES_V1, VISUAL_STYLE_NAMES_V1 } from "@narratage/visual-ir";import { mediaDependency, mediaTypes } from "@narratage/media";import { textDependency, textTypes } from "@narratage/text";-import { digestOf } from "@narratage/protocol";import type { ModuleManifest, ProducerRef, TypeRef, ValueSchema } from "@narratage/protocol";export const typographyTrackModuleRef = { name: "@narratage/typography-track", version: "1" } as const;@@ -154,7 +153,6 @@ const textMaskSpecSchema = object({materialFit: { schema: enumString(["contain", "cover", "fill"]) },});-const registered = (digest: ReturnType<typeof digestOf>) => ({ digest });export const typographyTrackMarkupSurfaces = [{ name: "style", tag: "Style", mode: "structured", outputs: [typographyTrackTypes.style] },diff --git a/packages/typography-track/src/program.ts b/packages/typography-track/src/program.tsindex 4f009358..5c72cf0d 100644--- a/packages/typography-track/src/program.ts+++ b/packages/typography-track/src/program.ts@@ -34,7 +34,7 @@ import type {} from "@narratage/composition";import { assertCompositableSurfaceRef } from "@narratage/media";import type { CompositableSurfaceRef } from "@narratage/media";-import { canonicalize, digestOf } from "@narratage/protocol";+import { canonicalize } from "@narratage/protocol";import { verifyText } from "@narratage/text";import type { Text } from "@narratage/text";diff --git a/packages/typography-track/test/typography-track.test.ts b/packages/typography-track/test/typography-track.test.tsindex 9fb2539b..4b836d9e 100644--- a/packages/typography-track/test/typography-track.test.ts+++ b/packages/typography-track/test/typography-track.test.ts@@ -1,10 +1,11 @@import assert from "node:assert/strict";import test from "node:test";+import { fixtureDigest } from "../../../test/fixture-digest.js";import { spatialComponent, videoContractManifests } from "../../../test/support/video-domain.js";import { registerTypeValidatorFacets } from "@narratage/component-kit";import { compositionTypes, sealComposition, sealVisualTrack } from "@narratage/composition";-import { computeModuleDigest, createResolvedClosure, sealBuildRequest, start } from "@narratage/core";+import { createResolvedClosure, sealBuildRequest, start } from "@narratage/core";import { AuthorFrontendRegistry, compileSourceClosure, resolveCompiledSourceExport } from "@narratage/elaborator";import { compileHyperframesDocument } from "@narratage/hyperframes";import { mediaDependency, mediaTypes } from "@narratage/media";@@ -12,7 +13,6 @@ import type { CompositableSurfaceRef, FontArtifactRef } from "@narratage/media";import type { NarrativeSelectionRef } from "@narratage/narrative";import { sealProgramSpace } from "@narratage/program-space";import { programSpaceDependency, programSpaceTypes } from "@narratage/program-space";-import { digestOf } from "@narratage/protocol";import type { ModuleManifest } from "@narratage/protocol";import type { CompleteSemanticMap } from "@narratage/semantic-map";import {@@ -55,7 +55,7 @@ const space = sealProgramSpace({const exactTestFont: FontArtifactRef = {sources: [{ artifact: {kind: "blob",- digest: digestOf("typography-track-test-font"),+ digest: fixtureDigest("typography-track-test-font"),size: 1,mediaType: "font/woff2",} }],@@ -66,7 +66,7 @@ const exactTestFont: FontArtifactRef = {const exactTestSurface: CompositableSurfaceRef = {artifact: {kind: "blob",- digest: digestOf("typography-track-test-surface"),+ digest: fixtureDigest("typography-track-test-surface"),size: 1,mediaType: "image/png",},@@ -219,7 +219,7 @@ test("Text Mask explicitly consumes one authored Text Program and one owned stil}],});const material: CompositableSurfaceRef = {- artifact: { kind: "blob", digest: digestOf("text-mask-material"), size: 1, mediaType: "image/png" },+ artifact: { kind: "blob", digest: fixtureDigest("text-mask-material"), size: 1, mediaType: "image/png" },width: 800, height: 240, colorSpace: "srgb", alphaMode: "straight", timing: { kind: "still" },};const track = renderTextMaskTrack(space, program, material, sealTextMaskSpec({@@ -307,7 +307,6 @@ test("Selection Text consumes explicit Selection, SemanticMap, Style, Motion andtest("the self-described Markup Surfaces compile Style, Motion and all three spatial forms", async () => {const fixtureModule = { name: "example.text-inputs", version: "1" } as const;- const fixtureDigest = digestOf("example.text-inputs/surface@1");const fixtureSurface = {name: "inputs", tag: "Inputs", mode: "structured",outputs: [@@ -461,7 +460,6 @@ test("the self-described Markup Surfaces compile Style, Motion and all three spaassert.equal(ordinaryTrack.ref.kind, "logical-output");assert.equal(track.ref.kind, "logical-output");const build = start(compiled.program, compiled.graph, sealBuildRequest({- graph: compiled.graph.id,targets: [{ output: ordinaryTrack.ref.kind === "logical-output" ? ordinaryTrack.ref.id : "" },{ output: track.ref.kind === "logical-output" ? track.ref.id : "" },diff --git a/packages/validation/README.md b/packages/validation/README.mdindex 5948a4bc..56228979 100644--- a/packages/validation/README.md+++ b/packages/validation/README.md@@ -9,7 +9,7 @@ digest here.`validateValue()` runs the owner validator as a Host admission gate. `admitRecord()` applies thesame gate to authored or externally provided Records. `NodeDriver` applies it to Producer and-Provider results before creating an Event. Core neither executes the validator nor learns the+Provider results before creating a Command result. Core neither executes the validator nor learns thedomain meaning; admitted Records carry no validation metadata through the graph.Validators are refinements, not converters. They may accept or reject a value; they may not rewritediff --git a/packages/validation/src/index.ts b/packages/validation/src/index.tsindex d33a0590..7f93faae 100644--- a/packages/validation/src/index.ts+++ b/packages/validation/src/index.ts@@ -88,11 +88,7 @@ export async function admitRecord(record: TypedRecord,registry: TypeValidatorRegistryLike,): Promise<TypedRecord> {- const {- digest: _digest,- ...draft- } = record;- const admitted = sealRecord(draft);+ const admitted = sealRecord(record);verifyRecordStructure(closure, admitted);await refineValue(record.type, record.value, registry);return admitted;diff --git a/packages/video-cli/README.md b/packages/video-cli/README.mdindex 83a307cd..f07a86d3 100644--- a/packages/video-cli/README.md+++ b/packages/video-cli/README.md@@ -42,7 +42,7 @@ Source because an Author Graph without execution intent is not a Build. The livethe explicit Vertex Gemini Caption package and real local/remote Endpoints; the CLI neverfabricates a Target, Candidate or missing fact.-`build` compiles the BuildState and passes it to the selected Runtime Profile with a fresh,+`build` compiles one immutable Build Definition and passes it to the selected Runtime Profile with a fresh,automatically assigned execution id. Source or Plan identity never reclaims an earlier Build; reuseacross Builds exists only through explicit Run Source Candidates. JSON Profiles resolve only adapters in their separatelyselected `use` fields and contain no executable callback. The CLI imports no Provider. A TypeScript config@@ -54,7 +54,7 @@ With `--follow`, the CLI observes dispatch and Operation facts until terminal st`--max-wait-ms`; Ctrl-C only detaches that observer. `status` reads durable verified state, and`status --watch` reattaches the same kind of observer to an existing Build. The CLIcontrols Builds, not individual Operations. Cancelling a Build atomically withdraws it before claim,-or closes admission and continues Provider cancellation reconciliation after claim. It never selects+or marks running work for one best-effort Provider cancellation call after claim. It never selectsanother Candidate. None of these commands creates or stores a ready-Command queue.`build` archives every accepted Record in the demanded closure and every referenced byte Artifact,@@ -72,7 +72,7 @@ loaded into CLI memory and a corrupt stream cannot overwrite an existing export.and presentation names only. It is not Core truth or a Runtime Closure facet; `inspect` and `get`always resolve the alias back through the verified BuildState before accepting it.`history` searches those frozen Catalog names across Builds, but reports only Logical Outputs whose-selected Record is present in verified BuildState. It neither infers renames nor lists unbuilt+selected Record is present in the reconstructed Build view. It neither infers renames nor lists unbuiltaliases; an old Catalog name and a current output name are connected explicitly in the Run Source.Historical Records, fixed files and generated previews are declared as ordinary Candidates in thediff --git a/packages/video-cli/src/distribution.ts b/packages/video-cli/src/distribution.tsindex cac2a6fd..31477008 100644--- a/packages/video-cli/src/distribution.ts+++ b/packages/video-cli/src/distribution.ts@@ -5,12 +5,12 @@ import {createVideoCompiler,} from "./compiler.js";-const fallbackPackageRoot = import.meta.dirname;+const packageRoot = import.meta.dirname;const installedLauncher = process.env.NARRATAGE_CLI_LAUNCHER;/** Official video authoring assembly for the generic CLI engine. */export const videoCliDistribution: CliDistribution = {- fallbackPackageRoot,+ packageRoot,bootstrapPackages: [],createCompiler: createVideoCompiler,discoverSourcePackages: async (path, options) => {diff --git a/packages/video-cli/test/packages.ts b/packages/video-cli/test/packages.tsindex 4a4869c4..29ca7e70 100644--- a/packages/video-cli/test/packages.ts+++ b/packages/video-cli/test/packages.ts@@ -31,11 +31,11 @@ import svs from "../../svs/src/activation.js";import typographyTrack from "../../typography-track/src/activation.js";import whisperX from "../../whisperx/src/activation.js";import type { NodePackageContribution } from "@narratage/package-loader-node";-import { digestOf } from "@narratage/protocol";+import { fixtureDigest } from "../../../test/fixture-digest.js";const bind = (specifier: string, contribution: NodePackageContribution) => ({specifier,- digest: digestOf(specifier),+ digest: fixtureDigest(specifier),contribution,});diff --git a/packages/visual-ir/src/index.ts b/packages/visual-ir/src/index.tsindex b06153fe..ff2407a5 100644--- a/packages/visual-ir/src/index.ts+++ b/packages/visual-ir/src/index.ts@@ -1,4 +1,3 @@-import { digestOf } from "@narratage/protocol";import type { ModuleManifest } from "@narratage/protocol";export { assertVisualStyleV1, VISUAL_IR_V1, VISUAL_STYLE_ENUM_VALUES_V1, VISUAL_STYLE_NAMES_V1 } from "./style.js";export type * from "./style.js";diff --git a/packages/whisperx/src/manifest.ts b/packages/whisperx/src/manifest.tsindex ef90b9f3..a9911876 100644--- a/packages/whisperx/src/manifest.ts+++ b/packages/whisperx/src/manifest.ts@@ -1,7 +1,6 @@import { speechDependency, speechTypes } from "@narratage/speech";import { speechEvidenceDependency, speechEvidenceTypes } from "@narratage/speech-evidence";import { semanticMapDependency, semanticMapTypes } from "@narratage/semantic-map";-import { digestOf } from "@narratage/protocol";import type { CapabilityRef, ModuleManifest, ProducerRef } from "@narratage/protocol";import { mediaPipelineManifest, mediaPipelineModuleRef } from "@narratage/media-pipeline";import { speechAlignmentManifest, speechAlignmentModuleRef } from "@narratage/speech-alignment";diff --git a/packages/whisperx/test/whisperx.test.ts b/packages/whisperx/test/whisperx.test.tsindex 29db9a43..ec4ae2c4 100644--- a/packages/whisperx/test/whisperx.test.ts+++ b/packages/whisperx/test/whisperx.test.ts@@ -7,8 +7,8 @@ import { sealSpeechBasis, sealSpeechEvidenceAudio } from "@narratage/speech";import type { SpeechBasis, SpeechEvidenceAudio } from "@narratage/speech";import assert from "node:assert/strict";import test from "node:test";+import { fixtureDigest } from "../../../test/fixture-digest.js";-import { digestOf } from "@narratage/protocol";import type { Digest, ProducerRef } from "@narratage/protocol";import {whisperXComponent,@@ -23,7 +23,7 @@ function basis() {});return sealSpeechBasis({programSpace,- audio: { kind: "blob", digest: digestOf("whisperx-test:audio"), size: 1, mediaType: "audio/wav" },+ audio: { kind: "blob", digest: fixtureDigest("whisperx-test:audio"), size: 1, mediaType: "audio/wav" },visualTrack: { clips: [] },segments: [{ segmentId: "line", startFrame: 0, endFrameExclusive: 30 }],});@@ -33,7 +33,7 @@ function evidenceAudio(basis: SpeechBasis): SpeechEvidenceAudio {return sealSpeechEvidenceAudio({artifact: {kind: "blob",- digest: digestOf("whisperx-test:evidence-audio"),+ digest: fixtureDigest("whisperx-test:evidence-audio"),size: 32_044,mediaType: "audio/wav",},diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex b3ca53b8..9ec3bdc5 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -2112,9 +2112,6 @@ importers:packages/run:dependencies:- '@narratage/core':- specifier: workspace:*- version: link:../core'@narratage/elaborator':specifier: workspace:*version: link:../elaboratordiff --git a/test/fixture-digest.ts b/test/fixture-digest.tsnew file mode 100644index 00000000..81ce4962--- /dev/null+++ b/test/fixture-digest.ts@@ -0,0 +1,9 @@+import type { Digest } from "@narratage/protocol";++/** Test-only Blob address. It encodes fixture text; it does not hash program meaning. */+export function fixtureDigest(value: unknown): Digest {+ const text = typeof value === "string" ? value : JSON.stringify(value);+ const bytes = new TextEncoder().encode(text);+ const hex = [...bytes].map((byte) => byte.toString(16).padStart(2, "0")).join("");+ return `sha256:${hex.slice(0, 64).padEnd(64, "0")}`;+}diff --git a/tools/caption-playground/src/main.ts b/tools/caption-playground/src/main.tsindex a9b27533..ab20af6f 100644--- a/tools/caption-playground/src/main.ts+++ b/tools/caption-playground/src/main.ts@@ -151,7 +151,6 @@ function editor(name: string, value: CanonicalValue | undefined, schema: ValueScconst parsed: CanonicalValue = schema.kind === "number" ? Number(control.value) : control.value;if (schema.kind === "number" && !Number.isFinite(parsed)) return;await patch("/__caption/recipe", {- expectedDigest: snapshot.recipe.digest,name,value: parsed,} satisfies RecipePatch);@@ -162,7 +161,6 @@ function editor(name: string, value: CanonicalValue | undefined, schema: ValueSccontrol.disabled = !enabled.checked;if (!enabled.checked) {void patch("/__caption/recipe", {- expectedDigest: snapshot.recipe.digest,name,remove: true,} satisfies RecipePatch);@@ -207,7 +205,6 @@ function fontPanel(value: CaptionPlaygroundSnapshot): HTMLElement {const choose = (family: CaptionPlaygroundFont, selectedWeight = family.previewWeight, selectedStyle = family.previewStyle): void => {void patch("/__caption/font", {- expectedDigest: snapshot!.font.digest,family: family.id,weight: selectedWeight,style: selectedStyle,diff --git a/tools/caption-playground/src/server.ts b/tools/caption-playground/src/server.tsindex 497153f1..93da9cdb 100644--- a/tools/caption-playground/src/server.ts+++ b/tools/caption-playground/src/server.ts@@ -1,4 +1,3 @@-import { createHash } from "node:crypto";import { readFileSync } from "node:fs";import { readFile, rename, writeFile } from "node:fs/promises";import { createRequire } from "node:module";@@ -47,10 +46,6 @@ type CurrentState = {readonly attachments: ReadonlyMap<string, ArtifactAttachment>;};-function sha256(value: string): string {- return `sha256:${createHash("sha256").update(value).digest("hex")}`;-}-function maskedSourceHeader(source: string): string {return source.replace(/^\s*<\?svml\s+using=(['"])[^'"]+\1\s*\?>/u, (header) =>header.replace(/[^\r\n]/gu, " "));@@ -244,14 +239,12 @@ export function captionPlaygroundPlugin(options: CaptionPlaygroundOptions): Plugrecipe: {file: relative(process.cwd(), options.recipeFile),path: options.recipePath,- digest: sha256(recipeText),values: recipe.value.properties,schema: fineCaptionRecipeSchema,},font: {file: relative(process.cwd(), options.fontFile),id: options.fontId,- digest: sha256(fontText),family: selectedFont.family,weight: selectedFont.weight,style: selectedFont.style,@@ -283,7 +276,6 @@ export function captionPlaygroundPlugin(options: CaptionPlaygroundOptions): Plugconst patchRecipe = async (patch: RecipePatch): Promise<void> => {const source = await readFile(options.recipeFile, "utf8");- if (sha256(source) !== patch.expectedDigest) throw Object.assign(new Error("SVS changed outside the browser; reloaded newest source"), { status: 409 });const parsed = parseSvs(options.recipeFile, maskedSourceHeader(source)).recipes.find((item) => item.value.path === options.recipePath);if (parsed === undefined) throw new Error(`Recipe ${options.recipePath} no longer exists`);@@ -313,7 +305,6 @@ export function captionPlaygroundPlugin(options: CaptionPlaygroundOptions): Plugthrow new Error(`${patch.family} does not provide ${patch.weight} ${patch.style}`);}const source = await readFile(options.fontFile, "utf8");- if (sha256(source) !== patch.expectedDigest) throw Object.assign(new Error("SVML changed outside the browser; reloaded newest source"), { status: 409 });const selected = fontTag(source, options.fontId);let replacement = replaceAttribute(selected.text, "family", patch.family);replacement = replaceAttribute(replacement, "weight", String(patch.weight));@@ -371,7 +362,7 @@ export function captionPlaygroundPlugin(options: CaptionPlaygroundOptions): Plug? splitFontFiles(family, weight, style): [{ file: defaultFontFile(family, weight, style) }];const css = files.map((item) => {- const token = createHash("sha256").update(item.file).digest("hex");+ const token = String(galleryFiles.size);galleryFiles.set(token, item.file);const range = "unicodeRange" in item ? `unicode-range:${item.unicodeRange};` : "";return `@font-face{font-family:${JSON.stringify(`caption-gallery-${familyId}`)};src:url('/__caption/font-file/${token}') format('woff2');font-style:${style};font-weight:${weight};${range}font-display:swap;}`;diff --git a/tools/caption-playground/src/shared.ts b/tools/caption-playground/src/shared.tsindex 512d4e6a..f102a6d9 100644--- a/tools/caption-playground/src/shared.ts+++ b/tools/caption-playground/src/shared.ts@@ -22,14 +22,12 @@ export type CaptionPlaygroundSnapshot = {readonly recipe: {readonly file: string;readonly path: string;- readonly digest: string;readonly values: Readonly<Record<string, CanonicalValue>>;readonly schema: ValueSchema;};readonly font: {readonly file: string;readonly id: string;- readonly digest: string;readonly family: string;readonly weight: number;readonly style: OpenFontStyle;@@ -53,14 +51,12 @@ export type CaptionPlaygroundFailure = {};export type RecipePatch = {- readonly expectedDigest: string;readonly name: string;readonly value?: CanonicalValue;readonly remove?: true;};export type FontPatch = {- readonly expectedDigest: string;readonly family: string;readonly weight: number;readonly style: OpenFontStyle;diff --git a/tools/svml-playground/src/pipeline/preview.ts b/tools/svml-playground/src/pipeline/preview.tsindex 0cb4af1c..f3569fb3 100644--- a/tools/svml-playground/src/pipeline/preview.ts+++ b/tools/svml-playground/src/pipeline/preview.ts@@ -10,7 +10,6 @@import { createHash } from "node:crypto";import { createProvidedCandidate } from "@narratage/run";-import { validateValue } from "@narratage/validation";import { officialVideoDomain } from "../official-video.js";import { compileSource } from "./compile.js";@@ -193,11 +192,10 @@ export async function preview(const supplied = output.type === TIMING ? estimated.map : estimated.space;const type = outputType(source, output.ref);const stored = { kind: "inline" as const, value: supplied as never };- const validation = await validateValue(domain.closure, type as never, stored, domain.validators);const candidate = createProvidedCandidate({+ id: `playground:timing:${output.ref}`,type: type as never,value: stored,- ...(validation === undefined ? {} : { validation }),} as never);added.push(candidate);satisfactions.push({ output: output.ref, candidate: candidate.id });@@ -300,6 +298,7 @@ export async function preview(}// The same bytes are the same Candidate, however many shots they cover.const candidate = createProvidedCandidate({+ id: `playground:stand-in:${shown.digest}`,type: outputType(source, output.ref) as never,value: {kind: "blob", digest: shown.digest,@@ -338,10 +337,9 @@ export async function preview(const plan = evenCaptionPlan(sequence, program as never);const type = outputType(source, output.ref);const stored = { kind: "inline" as const, value: plan as never };- const validation = await validateValue(domain.closure, type as never, stored, domain.validators);const candidate = createProvidedCandidate({+ id: `playground:caption:${output.ref}`,type: type as never, value: stored,- ...(validation === undefined ? {} : { validation }),} as never);added.set(candidate.id, candidate);satisfactions.push({ output: output.ref, candidate: candidate.id });diff --git a/tools/svml-playground/src/pipeline/run-source.ts b/tools/svml-playground/src/pipeline/run-source.tsindex e00d2b92..aca4056d 100644--- a/tools/svml-playground/src/pipeline/run-source.ts+++ b/tools/svml-playground/src/pipeline/run-source.ts@@ -98,7 +98,7 @@ export async function applyRunSource(input: {frontends.register(runMarkupFrontend);const text = readFileSync(input.runPath, "utf8");- const { document, closure } = await compileRunSource(+ const { document } = await compileRunSource({ id: input.runPath, name: input.runPath, text } as never,frontends,);@@ -132,7 +132,6 @@ export async function applyRunSource(input: {const beside = (from: string): string => resolve(dirname(input.runPath), from);const run = await resolveRunDocument(named as never, {compilation: input.compiled as never,- sourceClosure: closure,fragments: new RunFragmentRegistry(),readStoredValue(from: string): StoredValue {return { kind: "inline", value: JSON.parse(readFileSync(beside(from), "utf8")) };diff --git a/tools/svml-playground/test/markers.test.ts b/tools/svml-playground/test/markers.test.tsdeleted file mode 100644index c60caa63..00000000--- a/tools/svml-playground/test/markers.test.ts+++ /dev/null@@ -1,147 +0,0 @@-import assert from "node:assert/strict";-import { mkdtempSync, rmSync, writeFileSync } from "node:fs";-import { tmpdir } from "node:os";-import { join } from "node:path";-import test from "node:test";--import { readSource } from "../src/pipeline/session.js";-import { liveRanges, markerTones, spanAtOffset } from "../src/ui/markers.js";-import type { PlaygroundSnapshot } from "../src/shared.js";--/** `@outer` encloses `@inner`, and both sit inside the `<opening>` Segment. */-const SOURCE = `<?svml using="@narratage/markup@1"?>--<svml>- <import from="@narratage/script@1"/>- <import as="wording" from="@narratage/text@1"/>- <import as="seedance" from="@narratage/seedance@1"/>- <import as="whisperx" from="@narratage/whisperx@1"/>- <import as="speech" from="@narratage/speech-spine@1"/>- <import as="media-track" from="@narratage/media-track@1"/>- <import as="space" from="@narratage/spatial@1"/>- <import as="studio" source="./studio.svs"/>-- <script id="story">- <opening><HOST>One @outer two three @inner four five@/inner six@/outer seven.</opening>- <answer><HOST>Eight nine ten.</answer>- </script>-- <space:Canvas id="vertical" width="1080" height="1920"/>- <space:Frame id="full" within={vertical} left="0%" top="0%" right="100%" bottom="100%"/>-- <wording:Value id="direction">A person talking to camera.</wording:Value>- <seedance:TextVideo id="take" model="mini" prompt={direction} duration="10" generate-audio="true"/>-- <speech:Spine id="speech" frame-rate="30"- visual-frame={full} visual-appearance={studio.speech.visual} visual-z="0">- <speech:Take video={take.video} segment={story.segment.opening}/>- <speech:Take video={take.video} segment={story.segment.answer}/>- </speech:Spine>- <whisperx:Alignment id="timing" narrative={story} audio={speech.audio}/>-- <media-track:Track id="broll" map={timing.map} space={speech.space} canvas={vertical}>- <media-track:Item id="on-outer" video={take.video} during={story.selection.outer}- frame={full} appearance={studio.media.card}/>- </media-track:Track>-</svml>-`;--const STYLES = `<?svml using="@narratage/svs@1"?>--<sheet version="1">- speech.visual { fit: cover; }- media.card { stack-order: 40; fit: cover; frame-paint: #16161d; }-</sheet>-`;--let fixtureDirectory: string | undefined;-let fixtureSnapshot: Promise<PlaygroundSnapshot> | undefined;--function fixture(): Promise<PlaygroundSnapshot> {- if (fixtureSnapshot !== undefined) return fixtureSnapshot;- fixtureDirectory = mkdtempSync(join(tmpdir(), "svml-playground-markers-"));- writeFileSync(join(fixtureDirectory, "main.svml"), SOURCE, "utf8");- writeFileSync(join(fixtureDirectory, "studio.svs"), STYLES, "utf8");- fixtureSnapshot = readSource({- source: join(fixtureDirectory, "main.svml"), packageRoot: fixtureDirectory, revision: 1,- }).then((result) => result.snapshot);- return fixtureSnapshot;-}--test.after(() => {- if (fixtureDirectory !== undefined) rmSync(fixtureDirectory, { recursive: true, force: true });-});--test("a Segment is the outermost level and Selections nest inside it", async () => {- const snapshot = await fixture();- const segment = snapshot.script!.segments.find((item) => item.id === "opening")!;- const outer = snapshot.script!.selections.find((item) => item.id === "outer")!;- const inner = snapshot.script!.selections.find((item) => item.id === "inner")!;- assert.equal(segment.depth, 0, "nothing encloses a Segment");- assert.equal(outer.depth, 1, "its Segment encloses it");- assert.equal(inner.depth, 2, "its Segment and @outer both enclose it");-});--test("tones follow the level, not the identity", async () => {- const snapshot = await fixture();- const tones = markerTones(snapshot);- // Two Segments are the same kind of thing, so they read the same.- assert.equal(tones.get("opening"), tones.get("answer"));- assert.notEqual(tones.get("opening"), tones.get("outer"));- assert.notEqual(tones.get("outer"), tones.get("inner"));-});--test("an enclosing range stays live while an inner one is", async () => {- const snapshot = await fixture();- const inner = snapshot.script!.selections.find((item) => item.id === "inner")!;- const tokens = snapshot.script!.tokens.filter((token) =>- token.range.start >= inner.occurrences[0]!.open.start- && token.range.end <= inner.occurrences[0]!.close.end);- const middle = Math.floor((tokens[0]!.startFrame + tokens.at(-1)!.endFrame) / 2);-- // Nesting is the whole point of the markers: replacing the outer outline with- // the inner one would hide the relationship being expressed.- const live = liveRanges(snapshot, middle).map((span) => span.id);- assert.deepEqual(live, ["opening", "outer", "inner"], "outermost first");-});--test("a range that binds nothing is still live", async () => {- // Only @outer drives a Media Item. @inner places nothing, and still means- // something, so it must appear.- const snapshot = await fixture();- const bound = snapshot.tracks.flatMap((track) => track.clips).map((clip) => clip.authoredId);- assert.ok(!bound.includes("inner"));- const inner = snapshot.script!.selections.find((item) => item.id === "inner")!;- const tokens = snapshot.script!.tokens.filter((token) =>- token.range.start >= inner.occurrences[0]!.open.start- && token.range.end <= inner.occurrences[0]!.close.end);- const middle = Math.floor((tokens[0]!.startFrame + tokens.at(-1)!.endFrame) / 2);- assert.ok(liveRanges(snapshot, middle).some((span) => span.id === "inner"));-});--test("nothing is live outside the speech", async () => {- const snapshot = await fixture();- assert.deepEqual(liveRanges(snapshot, snapshot.space.frameCount + 100), []);-});--test("an offset resolves to the tightest range written around it", async () => {- const snapshot = await fixture();- const text = snapshot.source.text;- // Clicking marked prose should land in the innermost claim, not its parent.- assert.equal(spanAtOffset(snapshot, text.indexOf("four"))?.id, "inner");- assert.equal(spanAtOffset(snapshot, text.indexOf("two"))?.id, "outer");- assert.equal(spanAtOffset(snapshot, text.indexOf("seven"))?.id, "opening");- assert.equal(spanAtOffset(snapshot, text.indexOf("Eight"))?.id, "answer");- assert.equal(spanAtOffset(snapshot, text.indexOf("<space:Canvas")), undefined);-});--test("a resolved span carries the frame its speech starts on", async () => {- const snapshot = await fixture();- const text = snapshot.source.text;- const inner = spanAtOffset(snapshot, text.indexOf("four"))!;- const outer = spanAtOffset(snapshot, text.indexOf("two"))!;- // Landing on the inner marker's own first frame is what keeps a click from- // being thrown out to whatever encloses it.- assert.ok(inner.startFrame > outer.startFrame);- assert.ok(inner.endFrame <= outer.endFrame);-});diff --git a/tools/svml-playground/test/pipeline.test.ts b/tools/svml-playground/test/pipeline.test.tsdeleted file mode 100644index 0b7e6053..00000000--- a/tools/svml-playground/test/pipeline.test.ts+++ /dev/null@@ -1,264 +0,0 @@-import assert from "node:assert/strict";-import { execFileSync } from "node:child_process";-import { mkdtempSync, writeFileSync } from "node:fs";-import { tmpdir } from "node:os";-import { join } from "node:path";-import test from "node:test";--import { estimateTiming } from "../src/pipeline/estimate.js";-import { preview } from "../src/pipeline/preview.js";-import { readSource } from "../src/pipeline/session.js";--const SOURCE = `<?svml using="@narratage/markup@1"?>--<svml>- <import from="@narratage/script@1"/>- <import as="wording" from="@narratage/text@1"/>- <import as="seedance" from="@narratage/seedance@1"/>- <import as="whisperx" from="@narratage/whisperx@1"/>- <import as="speech" from="@narratage/speech-spine@1"/>- <import as="media-track" from="@narratage/media-track@1"/>- <import as="space" from="@narratage/spatial@1"/>- <import as="studio" source="./studio.svs"/>-- <script id="story">- <opening><HOST>One @claim two three four@/claim five six.</opening>- </script>-- <space:Canvas id="vertical" width="1080" height="1920"/>- <space:Frame id="full" within={vertical} left="0%" top="0%" right="100%" bottom="100%"/>-- <wording:Value id="direction">A person talking to camera.</wording:Value>- <seedance:TextVideo id="take" model="mini" prompt={direction} duration="10" generate-audio="true"/>- <seedance:TextVideo id="cut" model="mini" prompt={direction} duration="5"/>-- <speech:Spine id="speech" frame-rate="24"- visual-frame={full} visual-appearance={studio.speech.visual} visual-z="0">- <speech:Take video={take.video} segment={story.segment.opening}/>- </speech:Spine>- <whisperx:Alignment id="timing" narrative={story} audio={speech.audio}/>-- <media-track:Track id="broll" map={timing.map} space={speech.space} canvas={vertical}>- <media-track:Item id="on-claim" video={cut.video} during={story.selection.claim}- frame={full} appearance={studio.media.card}/>- </media-track:Track>-</svml>-`;--const STYLES = `<?svml using="@narratage/svs@1"?>--<sheet version="1">- speech.visual { fit: cover; }- media.card { stack-order: 40; fit: cover; frame-paint: #16161d; }-</sheet>-`;--/**- * Standing in for material nobody has shot needs a tool to draw with. Where- * there is none - which is every machine that has not installed ffmpeg, CI- * among them - a Track with no footage cannot be built, and the preview says so- * instead of drawing something.- */-const CAN_DRAW = (() => {- try {- execFileSync("ffmpeg", ["-version"], { stdio: "ignore", timeout: 10_000 });- return true;- } catch {- return false;- }-})();--function project(run?: string): string {- const directory = mkdtempSync(join(tmpdir(), "svml-playground-pipeline-"));- writeFileSync(join(directory, "main.svml"), SOURCE, "utf8");- writeFileSync(join(directory, "studio.svs"), STYLES, "utf8");- if (run !== undefined) writeFileSync(join(directory, "build.svrun"), run, "utf8");- return directory;-}--/**- * A Source on its own has neither footage nor timings, which is the state it- * spends most of its life in. It still has to be watchable, and it has to say- * which parts of what is shown were invented.- */-test("a Source with no Run Source still builds every Track", async () => {- const directory = project();- const built = await preview(join(directory, "main.svml"));-- // Timings are placed from the Script's own words, which needs no tool at all.- assert.equal(built.timing, "estimated", "nothing supplied timings");- assert.ok(built.tracks.length > 0, "the Source's Tracks were found");- if (!CAN_DRAW) {- assert.equal(built.placeholders.length, 0, "nothing was stood in for without a tool to draw it");- return;- }- assert.ok(built.placeholders.length > 0, "material nobody supplied stands in");- const drawn = built.tracks.filter((track) => track.track !== undefined);- assert.ok(drawn.length >= 2, `speech and B-roll both build; built ${drawn.map((t) => t.name).join(",")}`);- // Placing words at a guessed pace is not knowing when they are said, and a- // preview that does not distinguish the two is lying about what it shows.- assert.ok(- built.tracks.every((track) => track.track === undefined || track.unserved.length === 0),- "a Track that was built was not also reported as waiting",- );-});--test("a Track nobody can build costs only itself", async () => {- const directory = project();- const built = await preview(join(directory, "main.svml"));- // A Track that could not be built has to say what it was waiting for,- // whether that is a Provider or the material itself.- for (const track of built.tracks) {- if (track.track !== undefined) continue;- assert.ok(- track.unserved.length > 0 || track.errors.length > 0,- `${track.name} says why it has no picture`,- );- }- if (!CAN_DRAW) return;- assert.ok(- built.tracks.some((track) => track.track !== undefined),- "one Track waiting does not stop the others",- );-});--/**- * A Run Source that names material this machine has never produced is an- * ordinary state part-way through a production, not a broken Source.- */-test("material an earlier build produced is refused with a reason, not a crash", async () => {- const directory = project(`<?svml using="@narratage/run-markup@1"?>--<svrun version="1">- <author source="./main.svml"/>- <target output="broll.visual"/>- <build-record id="accepted" build="never-built-here" output="take.video"/>- <satisfy output="take.video" candidate="accepted"/>-</svrun>-`);- const built = await preview(join(directory, "main.svml"), join(directory, "build.svrun"));- // Reading the Run Source and reporting what it could not reach needs no tool;- // only drawing a stand-in does.- assert.ok(built.refused.length > 0, "the preview says what it could not read");- assert.ok(built.tracks.length > 0, "and still opens");- if (CAN_DRAW) assert.ok(built.tracks.some((track) => track.track !== undefined));- assert.match(built.refused.map((item) => item.reason).join(" "), /never-built-here/u);-});--test("a snapshot points its clips back at the tags that placed them", async () => {- const directory = project();- const { snapshot } = await readSource({- source: join(directory, "main.svml"), packageRoot: directory, revision: 7,- });- assert.equal(snapshot.revision, 7);- // Without a tool to draw stand-ins there are no Presents, so there is no- // timeline to point anywhere.- if (!CAN_DRAW) return;- assert.ok(snapshot.space.frameCount > 1);- const clips = snapshot.tracks.flatMap((track) => track.clips);- assert.ok(clips.length > 0, "the timeline is not empty");- const placed = clips.filter((clip) => clip.elementRange !== undefined);- assert.ok(placed.length > 0, "at least one clip knows which tag placed it");- for (const clip of placed) {- const written = snapshot.source.text.slice(clip.elementRange!.start, clip.elementRange!.end);- assert.match(written, /^</u, `${clip.id} points at a tag, not at the middle of one`);- }- // Every Present the picture draws must be addressable, or selecting a clip- // could not box it.- assert.match(snapshot.preview.srcdoc, /data-narratage-present-id/u);-});--test("the Script's markers carry the frames its words are said on", async () => {- const directory = project();- const { snapshot } = await readSource({- source: join(directory, "main.svml"), packageRoot: directory, revision: 1,- });- assert.ok(snapshot.script !== undefined, "the Script reported where its markers are");- assert.ok(snapshot.script.selections.some((item) => item.id === "claim"));- assert.ok(snapshot.script.tokens.length > 0, "words are placed on the timeline");- for (const token of snapshot.script.tokens) {- assert.ok(token.endFrame >= token.startFrame, `${token.id} does not run backwards`);- }-});--/** Estimation is arithmetic over identities the packages own, and must be stable. */-test("estimated timings are strictly increasing and reproducible", () => {- const narrative = {- segments: [{ id: "a", startAnchorId: "segment:a:start", endAnchorId: "segment:a:end" }],- tokens: [- { id: "t1", segmentId: "a", startAnchorId: "t1:s", endAnchorId: "t1:e", text: "Extraordinary" },- { id: "t2", segmentId: "a", startAnchorId: "t2:s", endAnchorId: "t2:e", text: "," },- { id: "t3", segmentId: "a", startAnchorId: "t3:s", endAnchorId: "t3:e", text: "yes" },- ],- };- const rate = { numerator: 24, denominator: 1 };- const first = estimateTiming(narrative, rate);- assert.deepEqual(first, estimateTiming(narrative, rate), "the same words give the same times");-- const anchors = (first.map as { anchors: readonly { identity: string; frame: number }[] }).anchors;- const frames = new Map(anchors.map((anchor) => [anchor.identity, anchor.frame]));- // A word that occupies no frames cannot be pointed at, and punctuation is- // still a word for this purpose.- for (const token of narrative.tokens) {- assert.ok(frames.get(token.endAnchorId)! > frames.get(token.startAnchorId)!, `${token.text} occupies time`);- }- assert.equal(frames.get("segment:a:start"), 0, "a Segment opens where its first word does");- assert.equal(frames.get("segment:a:end"), frames.get("t3:e"), "and closes where its last one ends");-});--/**- * A build reads assets only from the Source's own directory. A preview that- * read further would show a Source that cannot be built, which is the one thing- * it must never do.- */-test("an asset outside the Source's directory is refused, as a build refuses it", async () => {- const directory = mkdtempSync(join(tmpdir(), "svml-playground-boundary-"));- const outside = mkdtempSync(join(tmpdir(), "svml-playground-outside-"));- writeFileSync(join(outside, "stray.png"), "not really a picture", "utf8");- writeFileSync(join(directory, "main.svml"), `<?svml using="@narratage/markup@1"?>--<svml>- <import from="@narratage/script@1"/>- <import as="media" from="@narratage/media@1"/>- <script id="story"><opening><HOST>One two.</opening></script>- <media:Image id="stray" src="${join(outside, "stray.png")}"/>-</svml>-`, "utf8");-- await assert.rejects(- async () => await preview(join(directory, "main.svml")),- (error: unknown) => {- const message = (error as Error).message;- assert.match(message, /outside/u, "the reason names the boundary");- assert.match(message, /stray\.png/u, "and the file that crossed it");- return true;- },- );-});--/**- * How long a programme runs is what the Source says, not what this machine- * managed to draw. Stand-ins need a tool to draw with, and a machine without- * one drew nothing; the timeline shrank to a single frame while the words still- * ran to the end, so a marker past the end read as live. Every machine has to- * agree about the length, whatever it can picture.- */-test("the programme is as long as the Source says, drawn or not", async () => {- const directory = project();- const built = await preview(join(directory, "main.svml"));- const { snapshot } = await readSource({- source: join(directory, "main.svml"), packageRoot: directory, revision: 1,- });-- const spoken = Math.max(0, ...(snapshot.script?.tokens ?? []).map((token) => token.endFrame));- assert.ok(spoken > 0, "the Script was placed");- assert.ok(- snapshot.space.frameCount >= spoken,- `the timeline (${snapshot.space.frameCount}f) covers every word (${spoken}f)`,- );- // Which is the same claim the Tracks were built against.- const drawn = Math.max(0, ...built.tracks.flatMap((track) =>- ((track.track as { presents?: readonly { span: { endFrameExclusive: number } }[] } | undefined)?.presents ?? [])- .map((present) => present.span.endFrameExclusive)));- assert.ok(snapshot.space.frameCount >= drawn, "and covers every clip");-});