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: make runtime services replaceable
kashorinfec6825544a6
diff --git a/README.md b/README.mdindex fae9ae56..def6ff40 100644--- a/README.md+++ b/README.md@@ -58,6 +58,9 @@> `@svml/local` now provide the durable zero-service developer assembly.> `@svml/endpoint-kit` and `@svml/credential-store-env` now bind static Endpoint identity,> non-secret configuration, exact credential slots and recoverable wake/retry/cancel behavior;+> Runtime service packages now bind Scheduler and Store implementations through the same exact+> Manifest/instance selection law, so `@svml/local` no longer contains package-specific store+> registration;> `@svml/artifact-store-s3` plus bounded Lambda/process transports provide replaceable external> execution plumbing without claiming any video capability;> `@svml/media-pipeline` and `@svml/provider-media-local` now provide all-stream ffprobe inspection,diff --git a/docs/README.md b/docs/README.mdindex b8167abd..6f219ce6 100644--- a/docs/README.md+++ b/docs/README.md@@ -66,7 +66,8 @@ application assembly.and stores. The environment-neutral executor, sealed Runtime Profile/Closure, in-memoryBuildStore/OperationStore CAS, queue-free local Scheduler and recoverable Endpoint lifecycle areimplemented. SQLite Build/Operation stores, filesystem/S3 artifacts and the `@svml/local`- developer assembly are now implemented together with Endpoint Kit, environment credential+ developer assembly are now implemented together with the generic Runtime service-package ABI,+ Endpoint Kit, environment credentialinjection, bounded Lambda/process transports, wake/follow, retry, status and cancellation, pluslocal KIE/media/HyperFrames/WhisperX execution packages; distributed leases and production hostedendpoints are not. Its older example packagediff --git a/packages/artifact-store-fs/README.md b/packages/artifact-store-fs/README.mdindex b9dace84..7ffeeaf0 100644--- a/packages/artifact-store-fs/README.md+++ b/packages/artifact-store-fs/README.md@@ -3,5 +3,9 @@A project-local, content-addressed implementation of the Runtime `ArtifactStore` port.The path is deployment configuration. Artifact identity is the SHA-256 digest of its bytes, so the-same adapter can later be replaced by S3 without changing Core, component or Provider contracts.-This package does not store BuildState, Provider checkpoints, credentials or author library records.+same adapter can later be replaced by S3 without changing Core, component or Endpoint contracts.+This package does not store BuildState, Endpoint checkpoints, credentials or author library records.++`createFileArtifactStorePackage()` contributes the configured store through the generic Runtime+service-package protocol. Its absolute root is bound into Runtime instance identity; the Local Host+does not need filesystem-specific registration code.diff --git a/packages/artifact-store-fs/src/index.ts b/packages/artifact-store-fs/src/index.tsindex b94232a4..7272956f 100644--- a/packages/artifact-store-fs/src/index.ts+++ b/packages/artifact-store-fs/src/index.ts@@ -1,7 +1,7 @@export {- fileArtifactStoreFacet,+ FileArtifactStore,+ createFileArtifactStorePackage,fileArtifactStoreImplementationDigest,fileArtifactStoreModuleRef,- fileArtifactStoreRuntimeManifest,-} from "./manifest.js";-export { FileArtifactStore } from "./store.js";+} from "./store.js";+export type * from "./store.js";diff --git a/packages/artifact-store-fs/src/manifest.ts b/packages/artifact-store-fs/src/manifest.tsdeleted file mode 100644index b141cc8f..00000000--- a/packages/artifact-store-fs/src/manifest.ts+++ /dev/null@@ -1,33 +0,0 @@-import { digestOf } from "@svml/protocol";-import type { RuntimeModuleManifest } from "@svml/runtime";--export const fileArtifactStoreModuleRef = {- name: "@svml/artifact-store-fs",- version: "1",-} as const;--export const fileArtifactStoreFacet = {- module: fileArtifactStoreModuleRef,- name: "artifact-store",-} as const;--export const fileArtifactStoreImplementationDigest = digestOf(- "@svml/artifact-store-fs/artifact-store@1",-);--export const fileArtifactStoreRuntimeManifest: RuntimeModuleManifest = {- format: "svml.runtime-module@2",- name: fileArtifactStoreModuleRef.name,- version: fileArtifactStoreModuleRef.version,- facets: [- {- name: fileArtifactStoreFacet.name,- role: "artifact-store",- implementation: {- locator: "@svml/artifact-store-fs/artifact-store",- digest: fileArtifactStoreImplementationDigest,- },- permissions: ["filesystem:artifacts"],- },- ],-};diff --git a/packages/artifact-store-fs/src/store.ts b/packages/artifact-store-fs/src/store.tsindex 9710ea2e..0ebf4e08 100644--- a/packages/artifact-store-fs/src/store.ts+++ b/packages/artifact-store-fs/src/store.ts@@ -2,9 +2,24 @@ import { createHash } from "node:crypto";import { mkdir, readFile, writeFile } from "node:fs/promises";import { dirname, join, resolve } from "node:path";-import { isDigest } from "@svml/protocol";+import { digestOf, isDigest } from "@svml/protocol";import type { BlobRef, Digest } from "@svml/protocol";-import type { ArtifactStore } from "@svml/runtime";+import { defineRuntimeServicePackage } from "@svml/runtime";+import type { ArtifactStore, RuntimeServicePackage } from "@svml/runtime";++export const fileArtifactStoreModuleRef = {+ name: "@svml/artifact-store-fs",+ version: "1",+} as const;++export const fileArtifactStoreImplementationDigest = digestOf(+ "@svml/artifact-store-fs/artifact-store@1",+);++export type CreateFileArtifactStorePackageOptions = {+ readonly root: string;+ readonly instance?: string;+};function digestPath(root: string, digest: Digest): string {if (!isDigest(digest)) throw new Error("Artifact digest is invalid");@@ -58,3 +73,25 @@ export class FileArtifactStore implements ArtifactStore {return (await this.get(digest)) !== undefined;}}++export function createFileArtifactStorePackage(+ options: CreateFileArtifactStorePackageOptions,+): RuntimeServicePackage {+ const instance = options.instance ?? "artifacts.fs";+ return defineRuntimeServicePackage({+ name: instance,+ module: fileArtifactStoreModuleRef,+ services: [{+ role: "artifact-store",+ facet: "artifact-store",+ instance,+ implementation: {+ locator: "@svml/artifact-store-fs/artifact-store",+ digest: fileArtifactStoreImplementationDigest,+ },+ permissions: ["filesystem:artifacts"],+ configuration: { root: resolve(options.root) },+ service: new FileArtifactStore(options.root),+ }],+ });+}diff --git a/packages/artifact-store-s3/README.md b/packages/artifact-store-s3/README.mdindex cebe7221..8ef0b935 100644--- a/packages/artifact-store-s3/README.md+++ b/packages/artifact-store-s3/README.md@@ -24,7 +24,7 @@ const artifacts = createS3ArtifactStorePackage({export default createProjectLocalRuntime({root: import.meta.dirname,- artifacts,+ runtimeServices: [artifacts],allowedPermissions: ["network:aws:s3"],endpoints: [],});diff --git a/packages/artifact-store-s3/src/index.ts b/packages/artifact-store-s3/src/index.tsindex 89b56012..10bb9ede 100644--- a/packages/artifact-store-s3/src/index.ts+++ b/packages/artifact-store-s3/src/index.ts@@ -2,14 +2,10 @@ export {AwsS3ObjectClient,} from "./client.js";export type { S3ObjectClient } from "./client.js";-export {- s3ArtifactStoreFacet,- s3ArtifactStoreImplementationDigest,- s3ArtifactStoreModuleRef,- s3ArtifactStoreRuntimeManifest,-} from "./manifest.js";export {S3ArtifactStore,createS3ArtifactStorePackage,+ s3ArtifactStoreImplementationDigest,+ s3ArtifactStoreModuleRef,} from "./store.js";export type * from "./store.js";diff --git a/packages/artifact-store-s3/src/manifest.ts b/packages/artifact-store-s3/src/manifest.tsdeleted file mode 100644index 0270b5c4..00000000--- a/packages/artifact-store-s3/src/manifest.ts+++ /dev/null@@ -1,31 +0,0 @@-import { digestOf } from "@svml/protocol";-import type { RuntimeModuleManifest } from "@svml/runtime";--export const s3ArtifactStoreModuleRef = {- name: "@svml/artifact-store-s3",- version: "1",-} as const;--export const s3ArtifactStoreFacet = {- module: s3ArtifactStoreModuleRef,- name: "artifact-store",-} as const;--export const s3ArtifactStoreImplementationDigest = digestOf(- "@svml/artifact-store-s3/artifact-store@1",-);--export const s3ArtifactStoreRuntimeManifest: RuntimeModuleManifest = {- format: "svml.runtime-module@2",- name: s3ArtifactStoreModuleRef.name,- version: s3ArtifactStoreModuleRef.version,- facets: [{- name: s3ArtifactStoreFacet.name,- role: "artifact-store",- implementation: {- locator: "@svml/artifact-store-s3/artifact-store",- digest: s3ArtifactStoreImplementationDigest,- },- permissions: ["network:aws:s3"],- }],-};diff --git a/packages/artifact-store-s3/src/store.ts b/packages/artifact-store-s3/src/store.tsindex e026427e..189b3848 100644--- a/packages/artifact-store-s3/src/store.ts+++ b/packages/artifact-store-s3/src/store.ts@@ -2,18 +2,20 @@ import { createHash } from "node:crypto";import { digestOf, isDigest } from "@svml/protocol";import type { BlobRef, Digest } from "@svml/protocol";-import type {- ArtifactStore,- RuntimeModuleManifest,- RuntimeProfileInstance,-} from "@svml/runtime";+import { defineRuntimeServicePackage } from "@svml/runtime";+import type { ArtifactStore, RuntimeServicePackage } from "@svml/runtime";import { AwsS3ObjectClient } from "./client.js";import type { S3ObjectClient } from "./client.js";-import {- s3ArtifactStoreFacet,- s3ArtifactStoreRuntimeManifest,-} from "./manifest.js";++export const s3ArtifactStoreModuleRef = {+ name: "@svml/artifact-store-s3",+ version: "1",+} as const;++export const s3ArtifactStoreImplementationDigest = digestOf(+ "@svml/artifact-store-s3/artifact-store@1",+);type S3Location = {readonly bucket: string;@@ -35,12 +37,7 @@ export type CreateS3ArtifactStorePackageOptions = S3Location & {readonly client?: S3ObjectClient;};-export type S3ArtifactStorePackage = {- readonly name: string;- readonly manifest: RuntimeModuleManifest;- readonly instance: RuntimeProfileInstance;- readonly store: ArtifactStore;-};+export type S3ArtifactStorePackage = RuntimeServicePackage;function assert(condition: unknown, message: string): asserts condition {if (!condition) throw new Error(message);@@ -173,21 +170,27 @@ export function createS3ArtifactStorePackage(...(options.endpoint === undefined ? {} : { endpoint: options.endpoint }),...(options.forcePathStyle === undefined ? {} : { forcePathStyle: options.forcePathStyle }),});- return {+ return defineRuntimeServicePackage({name: instance,- manifest: s3ArtifactStoreRuntimeManifest,- instance: {- id: instance,- facet: s3ArtifactStoreFacet,- configurationDigest: digestOf(configuration),- },- store: new S3ArtifactStore({- client,- bucket: options.bucket,- ...(prefix.length === 0 ? {} : { prefix }),- ...(options.expectedBucketOwner === undefined- ? {}- : { expectedBucketOwner: options.expectedBucketOwner }),- }),- };+ module: s3ArtifactStoreModuleRef,+ services: [{+ role: "artifact-store",+ facet: "artifact-store",+ instance,+ implementation: {+ locator: "@svml/artifact-store-s3/artifact-store",+ digest: s3ArtifactStoreImplementationDigest,+ },+ permissions: ["network:aws:s3"],+ configuration,+ service: new S3ArtifactStore({+ client,+ bucket: options.bucket,+ ...(prefix.length === 0 ? {} : { prefix }),+ ...(options.expectedBucketOwner === undefined+ ? {}+ : { expectedBucketOwner: options.expectedBucketOwner }),+ }),+ }],+ });}diff --git a/packages/artifact-store-s3/test/store.test.ts b/packages/artifact-store-s3/test/store.test.tsindex d1c5ea51..f71845d6 100644--- a/packages/artifact-store-s3/test/store.test.ts+++ b/packages/artifact-store-s3/test/store.test.ts@@ -54,7 +54,7 @@ test("configured S3 service locks location without putting AWS credentials in thregion: "us-east-1",expectedBucketOwner: "123456789012",});- assert.equal(configured.instance.id, "artifacts.team");- assert.ok(configured.instance.configurationDigest);+ assert.equal(configured.services[0]?.instance.id, "artifacts.team");+ assert.ok(configured.services[0]?.instance.configurationDigest);assert.equal(JSON.stringify(configured).includes("AWS_SECRET_ACCESS_KEY"), false);});diff --git a/packages/credential-store-env/README.md b/packages/credential-store-env/README.mdindex 3698f013..5c021c0f 100644--- a/packages/credential-store-env/README.md+++ b/packages/credential-store-env/README.md@@ -6,9 +6,11 @@ Minimal local `CredentialStore` backed by explicitly named environment variablescredentialRef("env", "KIE_API_KEY")```-The store resolves only the requested key at Provider invocation time. It never enumerates or-serializes the environment. Secret values are handed only to that Provider call and do not enter+The store resolves only the requested key at Endpoint invocation time. It never enumerates or+serializes the environment. Secret values are handed only to that Endpoint call and do not enterBuildState, Runtime Closure, Operation checkpoints, logs or SQLite through framework code.-`createProjectLocalRuntime()` activates this store by default. Production deployments may replace+`createEnvironmentCredentialStorePackage()` exposes it through the same generic Runtime service+package ABI as every other CredentialStore. `createProjectLocalRuntime()` activates it by default.+Production deployments may replaceit with a keychain, Vault, KMS or tenant-scoped implementation of the same `CredentialStore` port.diff --git a/packages/credential-store-env/src/index.ts b/packages/credential-store-env/src/index.tsindex 5257e206..51b3046e 100644--- a/packages/credential-store-env/src/index.ts+++ b/packages/credential-store-env/src/index.ts@@ -1,9 +1,10 @@import { digestOf } from "@svml/protocol";+import { defineRuntimeServicePackage } from "@svml/runtime";import type {CredentialRef,CredentialStore,CredentialValue,- RuntimeModuleManifest,+ RuntimeServicePackage,} from "@svml/runtime";import { verifyCredentialRef } from "@svml/runtime";@@ -12,28 +13,13 @@ export const environmentCredentialStoreModuleRef = {version: "1",} as const;-export const environmentCredentialStoreFacet = {- module: environmentCredentialStoreModuleRef,- name: "credential-store",-} as const;-export const environmentCredentialStoreImplementationDigest = digestOf("@svml/credential-store-env/credential-store@1",);-export const environmentCredentialStoreRuntimeManifest: RuntimeModuleManifest = {- format: "svml.runtime-module@2",- name: environmentCredentialStoreModuleRef.name,- version: environmentCredentialStoreModuleRef.version,- facets: [{- name: environmentCredentialStoreFacet.name,- role: "credential-store",- implementation: {- locator: "@svml/credential-store-env",- digest: environmentCredentialStoreImplementationDigest,- },- permissions: ["environment:credentials"],- }],+export type CreateEnvironmentCredentialStorePackageOptions = {+ readonly instance?: string;+ readonly environment?: NodeJS.ProcessEnv;};/** Resolves only explicitly requested environment variables and never snapshots or enumerates env. */@@ -52,3 +38,25 @@ export class EnvironmentCredentialStore implements CredentialStore {return { secret };}}++export function createEnvironmentCredentialStorePackage(+ options: CreateEnvironmentCredentialStorePackageOptions = {},+): RuntimeServicePackage {+ const instance = options.instance ?? "credentials.env";+ return defineRuntimeServicePackage({+ name: instance,+ module: environmentCredentialStoreModuleRef,+ services: [{+ role: "credential-store",+ facet: "credential-store",+ instance,+ implementation: {+ locator: "@svml/credential-store-env",+ digest: environmentCredentialStoreImplementationDigest,+ },+ permissions: ["environment:credentials"],+ configuration: { source: "process-environment", explicitKeysOnly: true },+ service: new EnvironmentCredentialStore(options.environment),+ }],+ });+}diff --git a/packages/local/README.md b/packages/local/README.mdindex d52ebaf8..c65fd1fc 100644--- a/packages/local/README.md+++ b/packages/local/README.md@@ -8,8 +8,8 @@ The convenience assembly uses:- `@svml/store-sqlite` for durable Build and Operation facts;- `@svml/artifact-store-fs` for content-addressed project bytes;-- optional replacement by any permission-checked ArtifactStore contribution, including- `@svml/artifact-store-s3`;+- optional replacement of Scheduler, BuildStore, OperationStore, ArtifactStore or CredentialStore+ by permission-checked Runtime service packages;- an in-process, queue-free Scheduler whose ready work always comes from Core;- an exact implementation package lock for deterministic component packages;- separately selected external Endpoint packages.@@ -22,11 +22,11 @@ services.export default await createProjectLocalRuntime({root: import.meta.dirname,packageLock: "./svml.packages.lock",- artifacts: createS3ArtifactStorePackage({+ runtimeServices: [createS3ArtifactStorePackage({bucket: "hypit-svml-artifacts",prefix: "development",region: "us-east-1",- }),+ })],endpoints: [createKieProvider({ apiKey: credentialRef("env", "KIE_API_KEY") })],allowedPermissions: ["network:aws:s3", "network:api.kie.ai", "network:kieai.redpandaai.co"],scheduling: {@@ -41,9 +41,10 @@ pipeline Producers without adding imports to this deployment source. Its digestBuildRequest, so a persisted Build cannot resume under another deterministic component closure.The KIE Endpoint remains an independently selected privileged endpoint; swapping it changes anEndpoint implementation and lane, not the `.svml` author document.-`createProjectLocalRuntime` accepts a configured ArtifactStore package directly;-advanced hosts may call `createLocalRuntime` with Postgres or other implementations of the same-ports instead of using the project defaults.+`createProjectLocalRuntime` infers a role when exactly one configured service package supplies it.+When several instances provide the same role, `runtimeSelection` must name the exact instance.+The same mechanism covers Postgres, S3, keychains and replacement Schedulers; none requires a+change to `@svml/local`. Advanced hosts may still call `createLocalRuntime` with raw ports.`LocalBuildRequest.attachments` is the explicit ingress from a trusted Host into the selectedArtifactStore. Each attachment carries claimed `BlobRef` metadata plus bytes; Local Runtime copiesdiff --git a/packages/local/src/index.ts b/packages/local/src/index.tsindex 3aac8be6..751a6d87 100644--- a/packages/local/src/index.ts+++ b/packages/local/src/index.ts@@ -1,9 +1,8 @@export {- localRuntimeManifest,+ createLocalSchedulerPackage,localRuntimeModuleRef,- localSchedulerFacet,localSchedulerImplementationDigest,-} from "./manifest.js";+} from "./scheduler-package.js";export {createLocalRuntime,createProjectLocalRuntime,diff --git a/packages/local/src/manifest.ts b/packages/local/src/manifest.tsdeleted file mode 100644index 6ee3ef48..00000000--- a/packages/local/src/manifest.ts+++ /dev/null@@ -1,31 +0,0 @@-import { digestOf } from "@svml/protocol";-import type { RuntimeModuleManifest } from "@svml/runtime";--export const localRuntimeModuleRef = {- name: "@svml/local",- version: "1",-} as const;--export const localSchedulerFacet = {- module: localRuntimeModuleRef,- name: "scheduler",-} as const;--export const localSchedulerImplementationDigest = digestOf("@svml/local/scheduler@1");--export const localRuntimeManifest: RuntimeModuleManifest = {- format: "svml.runtime-module@2",- name: localRuntimeModuleRef.name,- version: localRuntimeModuleRef.version,- facets: [- {- name: localSchedulerFacet.name,- role: "scheduler",- implementation: {- locator: "@svml/local/scheduler",- digest: localSchedulerImplementationDigest,- },- permissions: [],- },- ],-};diff --git a/packages/local/src/project-services.ts b/packages/local/src/project-services.tsnew file mode 100644index 00000000..67d8e8d6--- /dev/null+++ b/packages/local/src/project-services.ts@@ -0,0 +1,216 @@+import { resolve } from "node:path";++import { createFileArtifactStorePackage } from "@svml/artifact-store-fs";+import { createEnvironmentCredentialStorePackage } from "@svml/credential-store-env";+import {+ assembleRuntimeServices,+ verifyRuntimeServicePackage,+} from "@svml/runtime";+import type {+ ArtifactStore,+ BuildStore,+ CredentialStore,+ OperationStore,+ RuntimeServiceAssembly,+ RuntimeServiceFacetRole,+ RuntimeServicePackage,+} from "@svml/runtime";+import { createSqliteRuntimeServicePackage } from "@svml/store-sqlite";++import { createLocalSchedulerPackage } from "./scheduler-package.js";+import type { ProjectLocalRuntimeOptions } from "./types.js";++export type ProjectRuntimeServiceSelection = {+ readonly scheduler: string;+ readonly stores: {+ readonly build: string;+ readonly operations: string;+ readonly artifacts: string;+ readonly credentials: string;+ };+};++export type ProjectRuntimeServiceAssembly = RuntimeServiceAssembly & {+ readonly buildStore: BuildStore;+ readonly operationStore: OperationStore;+ readonly artifactStore: ArtifactStore;+ readonly credentialStore: CredentialStore;+};++export type AssembledProjectRuntimeServices = {+ readonly assembly: ProjectRuntimeServiceAssembly;+ readonly selection: ProjectRuntimeServiceSelection;+ readonly allowedPermissions: readonly string[];+};++function assert(condition: unknown, message: string): asserts condition {+ if (!condition) throw new Error(message);+}++function configuredServiceIds(+ packages: readonly RuntimeServicePackage[],+ role: RuntimeServiceFacetRole,+): readonly string[] {+ return packages.flatMap((item) => item.services+ .filter((service) => service.role === role)+ .map((service) => service.instance.id));+}++function chooseService(+ packages: readonly RuntimeServicePackage[],+ role: RuntimeServiceFacetRole,+ explicit: string | undefined,+ fallback: string,+): string {+ const configured = configuredServiceIds(packages, role);+ if (explicit !== undefined) return explicit;+ if (configured.length === 0) return fallback;+ if (configured.length === 1) return configured[0]!;+ throw new Error(`multiple ${role} services are configured; runtimeSelection must choose one`);+}++function hasConfiguredService(+ packages: readonly RuntimeServicePackage[],+ id: string,+): boolean {+ return packages.some((item) => item.services.some((service) => service.instance.id === id));+}++function manifestPermissions(packages: readonly RuntimeServicePackage[]): readonly string[] {+ return [...new Set(packages.flatMap((item) => item.manifest.facets.flatMap((facet) => facet.permissions)))];+}++async function closeServicePackages(packages: readonly RuntimeServicePackage[]): Promise<void> {+ const closed = new Set<RuntimeServicePackage>();+ for (const item of [...packages].reverse()) {+ if (closed.has(item)) continue;+ closed.add(item);+ await item.close?.();+ }+}++async function closeAfterFailure(+ packages: readonly RuntimeServicePackage[],+ error: unknown,+): Promise<never> {+ try {+ await closeServicePackages(packages);+ } catch (closeError) {+ throw new AggregateError([error, closeError], "Runtime service assembly and cleanup both failed");+ }+ throw error;+}++function projectSelection(+ packages: readonly RuntimeServicePackage[],+ options: ProjectLocalRuntimeOptions,+): ProjectRuntimeServiceSelection {+ return {+ scheduler: chooseService(+ packages,+ "scheduler",+ options.runtimeSelection?.scheduler,+ "scheduler.local",+ ),+ stores: {+ build: chooseService(+ packages,+ "build-store",+ options.runtimeSelection?.stores?.build,+ "builds.sqlite",+ ),+ operations: chooseService(+ packages,+ "operation-store",+ options.runtimeSelection?.stores?.operations,+ "operations.sqlite",+ ),+ artifacts: chooseService(+ packages,+ "artifact-store",+ options.runtimeSelection?.stores?.artifacts,+ "artifacts.fs",+ ),+ credentials: chooseService(+ packages,+ "credential-store",+ options.runtimeSelection?.stores?.credentials,+ "credentials.env",+ ),+ },+ };+}++/** Local-distribution defaults and exact service selection, separate from Build execution. */+export async function createProjectRuntimeServices(+ root: string,+ options: ProjectLocalRuntimeOptions,+): Promise<AssembledProjectRuntimeServices> {+ const configured = [...(options.runtimeServices ?? [])];+ let selection: ProjectRuntimeServiceSelection;+ try {+ for (const item of configured) verifyRuntimeServicePackage(item);+ selection = projectSelection(configured, options);+ } catch (error) {+ return await closeAfterFailure(configured, error);+ }++ const defaults: RuntimeServicePackage[] = [];+ try {+ if (!hasConfiguredService(configured, selection.scheduler)) {+ assert(selection.scheduler === "scheduler.local", `unknown selected scheduler ${selection.scheduler}`);+ defaults.push(createLocalSchedulerPackage(selection.scheduler));+ }+ const needsDefaultBuild = !hasConfiguredService(configured, selection.stores.build);+ const needsDefaultOperations = !hasConfiguredService(configured, selection.stores.operations);+ if (needsDefaultBuild || needsDefaultOperations) {+ assert((!needsDefaultBuild || selection.stores.build === "builds.sqlite")+ && (!needsDefaultOperations || selection.stores.operations === "operations.sqlite"),+ "unknown selected BuildStore or OperationStore");+ defaults.push(createSqliteRuntimeServicePackage({+ path: resolve(root, options.statePath ?? ".svml/runtime.sqlite"),+ buildInstance: "builds.sqlite",+ operationInstance: "operations.sqlite",+ }));+ } else if (options.statePath !== undefined) {+ throw new Error("statePath configures the default SQLite services, but neither was selected");+ }+ if (!hasConfiguredService(configured, selection.stores.artifacts)) {+ assert(selection.stores.artifacts === "artifacts.fs",+ `unknown selected ArtifactStore ${selection.stores.artifacts}`);+ defaults.push(createFileArtifactStorePackage({+ root: resolve(root, options.artifactPath ?? ".svml/artifacts"),+ instance: selection.stores.artifacts,+ }));+ } else if (options.artifactPath !== undefined) {+ throw new Error("artifactPath configures the default filesystem ArtifactStore, but it was not selected");+ }+ if (!hasConfiguredService(configured, selection.stores.credentials)) {+ assert(selection.stores.credentials === "credentials.env",+ `unknown selected CredentialStore ${selection.stores.credentials}`);+ defaults.push(createEnvironmentCredentialStorePackage({ instance: selection.stores.credentials }));+ }+ const assembly = assembleRuntimeServices([...configured, ...defaults], selection);+ assert(assembly.buildStore !== undefined, "project Runtime requires a BuildStore");+ assert(assembly.operationStore !== undefined, "project Runtime requires an OperationStore");+ assert(assembly.artifactStore !== undefined, "project Runtime requires an ArtifactStore");+ assert(assembly.credentialStore !== undefined, "project Runtime requires a CredentialStore");+ const complete: ProjectRuntimeServiceAssembly = {+ ...assembly,+ buildStore: assembly.buildStore,+ operationStore: assembly.operationStore,+ artifactStore: assembly.artifactStore,+ credentialStore: assembly.credentialStore,+ };+ return {+ assembly: complete,+ selection,+ allowedPermissions: [+ ...manifestPermissions(defaults),+ ...(options.allowedPermissions ?? []),+ ],+ };+ } catch (error) {+ return await closeAfterFailure([...configured, ...defaults], error);+ }+}diff --git a/packages/local/src/runtime.ts b/packages/local/src/runtime.tsindex dc037b39..8d8ba53a 100644--- a/packages/local/src/runtime.ts+++ b/packages/local/src/runtime.ts@@ -4,16 +4,6 @@ import {registerProducerFacets,registerTypeValidatorFacets,} from "@svml/component-kit";-import {- FileArtifactStore,- fileArtifactStoreFacet,- fileArtifactStoreRuntimeManifest,-} from "@svml/artifact-store-fs";-import {- EnvironmentCredentialStore,- environmentCredentialStoreFacet,- environmentCredentialStoreRuntimeManifest,-} from "@svml/credential-store-env";import {ProducerRegistry,NodeDriver,@@ -35,23 +25,13 @@ import type {RuntimeModuleManifest,RuntimeProfileInstance,} from "@svml/runtime";-import {- SqliteRuntimeState,- sqliteBuildStoreFacet,- sqliteOperationStoreFacet,- sqliteStoreRuntimeManifest,-} from "@svml/store-sqlite";-import {- localRuntimeManifest,- localSchedulerFacet,-} from "./manifest.js";+import { createProjectRuntimeServices } from "./project-services.js";import type {CreateLocalRuntimeOptions,LocalBuildOptions,LocalBuildRequest,LocalRuntime,- NodeArtifactStorePackage,EndpointPackage,ProjectLocalRuntimeOptions,} from "./types.js";@@ -134,16 +114,6 @@ function verifyEndpointPackages(packages: readonly EndpointPackage[]): void {}}-function verifyArtifactPackage(item: NodeArtifactStorePackage): void {- assert(item.name.trim().length > 0, "ArtifactStore package name must not be empty");- assert(item.instance.id.trim().length > 0, `${item.name} ArtifactStore instance id is empty`);- assert(item.manifest.name === item.instance.facet.module.name- && item.manifest.version === item.instance.facet.module.version,- `${item.name} instance ${facetKey(item.instance)} is outside its Runtime Manifest`);- const facet = item.manifest.facets.find((candidate) => candidate.name === item.instance.facet.name);- assert(facet?.role === "artifact-store", `${item.name} instance is not an artifact-store facet`);-}-export async function createLocalRuntime(options: CreateLocalRuntimeOptions,): Promise<LocalRuntime> {@@ -185,7 +155,11 @@ export async function createLocalRuntime(const closureScheduling = options.closure === undefined? {}: localSchedulerOptionsFromClosure(options.closure.value);- const scheduler = new LocalBuildScheduler(driver, {+ const scheduler = (options.scheduler ?? {+ create(executor, schedulerOptions) {+ return new LocalBuildScheduler(executor, schedulerOptions);+ },+ }).create(driver, {...closureScheduling,...(options.scheduling ?? {}),buildStore: options.buildStore,@@ -259,9 +233,6 @@ export async function createLocalRuntime(export async function createProjectLocalRuntime(options: ProjectLocalRuntimeOptions = {},): Promise<LocalRuntime> {- if (options.artifacts !== undefined && options.artifactPath !== undefined) {- throw new Error("artifactPath configures the default filesystem store and cannot accompany artifacts");- }const root = resolve(options.root ?? process.cwd());const lockedPackageSet = options.packageLock === undefined? undefined@@ -270,43 +241,30 @@ export async function createProjectLocalRuntime(? []: nodePackageComponents(lockedPackageSet.packages);const configuredComponents = [...lockedComponents, ...(options.components ?? [])];- const state = new SqliteRuntimeState(resolve(root, options.statePath ?? ".svml/runtime.sqlite"));- const defaultArtifacts: NodeArtifactStorePackage = {- name: "@svml/artifact-store-fs",- manifest: fileArtifactStoreRuntimeManifest,- instance: { id: "artifacts.fs", facet: fileArtifactStoreFacet },- store: new FileArtifactStore(resolve(root, options.artifactPath ?? ".svml/artifacts")),- };- const artifacts = options.artifacts ?? defaultArtifacts;- verifyArtifactPackage(artifacts);+ const projectServices = await createProjectRuntimeServices(root, options);+ const services = projectServices.assembly;+ const selection = projectServices.selection;const endpointPackages = options.endpoints ?? [];- verifyEndpointPackages(endpointPackages);try {+ verifyEndpointPackages(endpointPackages);const modules = new RuntimeModuleRegistry();registerManifests(modules, [- localRuntimeManifest,- sqliteStoreRuntimeManifest,- artifacts.manifest,- environmentCredentialStoreRuntimeManifest,+ ...services.manifests,...endpointPackages.map((item) => item.manifest),]);const profile = sealRuntimeProfile({name: "svml.local.project",instances: [- { id: "scheduler.local", facet: localSchedulerFacet },- { id: "builds.sqlite", facet: sqliteBuildStoreFacet },- { id: "operations.sqlite", facet: sqliteOperationStoreFacet },- artifacts.instance,- { id: "credentials.env", facet: environmentCredentialStoreFacet },+ ...services.instances,...endpointPackages.map((item) => item.instance),],- scheduler: "scheduler.local",+ scheduler: selection.scheduler,stores: {- build: "builds.sqlite",- operations: "operations.sqlite",- artifacts: artifacts.instance.id,- credentials: "credentials.env",+ build: selection.stores.build,+ operations: selection.stores.operations,+ artifacts: selection.stores.artifacts,+ credentials: selection.stores.credentials,},endpoints: endpointPackages.flatMap((item) => item.bindings),scheduling: {@@ -317,23 +275,20 @@ export async function createProjectLocalRuntime(})),},});- const allowedPermissions = [- "filesystem:state",- ...(options.artifacts === undefined ? ["filesystem:artifacts"] : []),- "environment:credentials",- ...(options.allowedPermissions ?? []),- ];- const closure = resolveRuntimeProfile(modules, profile, { allowedPermissions });+ const closure = resolveRuntimeProfile(modules, profile, {+ allowedPermissions: projectServices.allowedPermissions,+ });const runtime = await createLocalRuntime({- buildStore: state.builds,- operationStore: state.operations,- artifactStore: artifacts.store,- credentialStore: new EnvironmentCredentialStore(),+ buildStore: services.buildStore,+ operationStore: services.operationStore,+ artifactStore: services.artifactStore,+ credentialStore: services.credentialStore,+ scheduler: services.scheduler,...(configuredComponents.length === 0? {}: { components: configuredComponents }),endpoints: endpointPackages,- closure: { modules, value: closure, allowedPermissions },+ closure: { modules, value: closure, allowedPermissions: projectServices.allowedPermissions },scheduling: {...(options.scheduling?.maxEventsPerBuild === undefined? {}@@ -348,11 +303,11 @@ export async function createProjectLocalRuntime(status: runtime.status,cancel: runtime.cancel,close() {- state.close();+ return services.close();},};} catch (error) {- state.close();+ await services.close();throw error;}}diff --git a/packages/local/src/scheduler-package.ts b/packages/local/src/scheduler-package.tsnew file mode 100644index 00000000..c32f4abe--- /dev/null+++ b/packages/local/src/scheduler-package.ts@@ -0,0 +1,37 @@+import { digestOf } from "@svml/protocol";+import {+ LocalBuildScheduler,+ defineRuntimeServicePackage,+} from "@svml/runtime";+import type { RuntimeServicePackage } from "@svml/runtime";++export const localRuntimeModuleRef = {+ name: "@svml/local",+ version: "1",+} as const;++export const localSchedulerImplementationDigest = digestOf("@svml/local/scheduler@1");++export function createLocalSchedulerPackage(+ instance = "scheduler.local",+): RuntimeServicePackage {+ return defineRuntimeServicePackage({+ name: instance,+ module: localRuntimeModuleRef,+ services: [{+ role: "scheduler",+ facet: "scheduler",+ instance,+ implementation: {+ locator: "@svml/local/scheduler",+ digest: localSchedulerImplementationDigest,+ },+ configuration: { algorithm: "queue-free-fair-lanes", version: 1 },+ service: {+ create(executor, options) {+ return new LocalBuildScheduler(executor, options);+ },+ },+ }],+ });+}diff --git a/packages/local/src/types.ts b/packages/local/src/types.tsindex 7e1e1a7d..0abbb4ca 100644--- a/packages/local/src/types.ts+++ b/packages/local/src/types.ts@@ -4,15 +4,15 @@ import type { EndpointPackage } from "@svml/endpoint-kit";import type { BuildState } from "@svml/protocol";import type {ArtifactStore,+ BuildSchedulerFactory,+ BuildSchedulerOptions,BuildSnapshot,BuildStore,CredentialStore,- LocalBuildSchedulerOptions,OperationStore,OperationSnapshot,- RuntimeModuleManifest,RuntimeModuleRegistry,- RuntimeProfileInstance,+ RuntimeServicePackage,ScheduledBuildResult,} from "@svml/runtime";import type { TypeValidatorRegistrar, TypeValidatorRegistryLike } from "@svml/validation";@@ -23,15 +23,8 @@ export type { ComponentPackage } from "@svml/component-kit";export type LocalTypeValidatorRegistry = TypeValidatorRegistryLike & TypeValidatorRegistrar;-/** One configured ArtifactStore service selected by trusted deployment code. */-export type NodeArtifactStorePackage = {- readonly name: string;- readonly manifest: RuntimeModuleManifest;- readonly instance: RuntimeProfileInstance;- readonly store: ArtifactStore;-};-export type { EndpointPackage } from "@svml/endpoint-kit";+export type { RuntimeServicePackage } from "@svml/runtime";export type LocalRuntimeClosureOptions = {readonly modules: RuntimeModuleRegistry;@@ -44,10 +37,11 @@ export type CreateLocalRuntimeOptions = {readonly operationStore?: OperationStore;readonly artifactStore: ArtifactStore;readonly credentialStore?: CredentialStore;+ readonly scheduler?: BuildSchedulerFactory;readonly components?: readonly ComponentPackage[];readonly endpoints?: readonly EndpointPackage[];readonly closure?: LocalRuntimeClosureOptions;- readonly scheduling?: Omit<LocalBuildSchedulerOptions, "buildStore" | "runtimeClosure">;+ readonly scheduling?: Omit<BuildSchedulerOptions, "buildStore" | "runtimeClosure">;readonly validators?: LocalTypeValidatorRegistry;/** Expected implementation package closure already bound into BuildRequest. */readonly implementationClosure?: import("@svml/protocol").Digest;@@ -60,8 +54,18 @@ export type ProjectLocalRuntimeOptions = {readonly artifactPath?: string;/** Exact installed implementation package lock. Source imports cannot change this selection. */readonly packageLock?: string;- /** Replaces the default project filesystem ArtifactStore without changing Scheduler/Core. */- readonly artifacts?: NodeArtifactStorePackage;+ /** Additional configured Runtime services. A unique supplied role replaces that role's local default. */+ readonly runtimeServices?: readonly RuntimeServicePackage[];+ /** Required only when more than one supplied instance can fulfill the same Runtime service role. */+ readonly runtimeSelection?: {+ readonly scheduler?: string;+ readonly stores?: {+ readonly build?: string;+ readonly operations?: string;+ readonly artifacts?: string;+ readonly credentials?: string;+ };+ };readonly components?: readonly ComponentPackage[];readonly endpoints?: readonly EndpointPackage[];readonly allowedPermissions?: readonly string[];diff --git a/packages/local/test/local.test.ts b/packages/local/test/local.test.tsindex 980b55cc..71ecd19c 100644--- a/packages/local/test/local.test.ts+++ b/packages/local/test/local.test.ts@@ -13,7 +13,6 @@ import test from "node:test";import { MemoryArtifactStore } from "@svml/driver-node";import type { RecoverableEndpoint } from "@svml/endpoint-kit";import type {- NodeArtifactStorePackage,NodeComponentPackage,EndpointPackage,} from "@svml/local";@@ -23,6 +22,7 @@ import {createNodePackageLock,writeNodePackageLock,} from "@svml/package-loader-node";+import { LocalBuildScheduler, defineRuntimeServicePackage } from "@svml/runtime";import type { RuntimeModuleManifest } from "@svml/runtime";import {@@ -272,41 +272,114 @@ test("project local runtime activates locked compute facets without deployment s}});+test("project local runtime infers one supplied Scheduler service without knowing its package", async () => {+ const directory = await mkdtemp(join(tmpdir(), "svml-local-scheduler-service-"));+ let creates = 0;+ const scheduler = defineRuntimeServicePackage({+ module: { name: "example.scheduler", version: "1" },+ services: [{+ role: "scheduler",+ facet: "scheduler",+ instance: "scheduler.example",+ implementation: {+ locator: "example.scheduler/fair",+ digest: digestOf("example.scheduler/fair@1"),+ },+ service: {+ create(executor, options) {+ creates += 1;+ return new LocalBuildScheduler(executor, options);+ },+ },+ }],+ });+ try {+ const runtime = await createProjectLocalRuntime({+ root: directory,+ runtimeServices: [scheduler],+ });+ assert.equal(creates, 1);+ await runtime.close();+ } finally {+ await rm(directory, { recursive: true, force: true });+ }+});++test("multiple Scheduler services require exact selection and unselected code never gains authority", async () => {+ const directory = await mkdtemp(join(tmpdir(), "svml-local-scheduler-selection-"));+ const creates = { one: 0, two: 0 };+ const closes = { one: 0, two: 0 };+ const scheduler = (name: "one" | "two") => defineRuntimeServicePackage({+ name: `example.scheduler.${name}`,+ module: { name: `example.scheduler.${name}`, version: "1" },+ services: [{+ role: "scheduler",+ facet: "scheduler",+ instance: `scheduler.${name}`,+ implementation: {+ locator: `example.scheduler.${name}/fair`,+ digest: digestOf(`example.scheduler.${name}/fair@1`),+ },+ service: {+ create(executor, options) {+ creates[name] += 1;+ return new LocalBuildScheduler(executor, options);+ },+ },+ }],+ close() { closes[name] += 1; },+ });+ try {+ await assert.rejects(+ createProjectLocalRuntime({+ root: directory,+ runtimeServices: [scheduler("one"), scheduler("two")],+ }),+ /runtimeSelection must choose one/u,+ );+ assert.deepEqual(closes, { one: 1, two: 1 });++ const runtime = await createProjectLocalRuntime({+ root: directory,+ runtimeServices: [scheduler("one"), scheduler("two")],+ runtimeSelection: { scheduler: "scheduler.two" },+ });+ assert.deepEqual(creates, { one: 0, two: 1 });+ await runtime.close();+ assert.deepEqual(closes, { one: 2, two: 2 });+ } finally {+ await rm(directory, { recursive: true, force: true });+ }+});+test("project local runtime accepts a permission-checked replacement ArtifactStore package", async () => {const directory = await mkdtemp(join(tmpdir(), "svml-local-artifacts-"));const module = { name: "example.remote-artifacts", version: "1" } as const;- const facet = { module, name: "artifact-store" } as const;- const artifacts: NodeArtifactStorePackage = {+ const artifactStore = new MemoryArtifactStore();+ const artifacts = defineRuntimeServicePackage({name: "example.remote-artifacts",- manifest: {- format: "svml.runtime-module@2",- name: module.name,- version: module.version,- facets: [{- name: facet.name,+ module,+ services: [{+ facet: "artifact-store",+ instance: "artifacts.remote",role: "artifact-store",implementation: {locator: "example.remote-artifacts",digest: digestOf("example.remote-artifacts@1"),},permissions: ["network:remote-artifacts"],- }],- },- instance: {- id: "artifacts.remote",- facet,- configurationDigest: digestOf({ bucket: "fixture" }),- },- store: new MemoryArtifactStore(),- };+ configuration: { bucket: "fixture" },+ service: artifactStore,+ }],+ });try {await assert.rejects(- createProjectLocalRuntime({ root: directory, artifacts }),+ createProjectLocalRuntime({ root: directory, runtimeServices: [artifacts] }),/disallowed Runtime permission network:remote-artifacts/u,);const runtime = await createProjectLocalRuntime({root: directory,- artifacts,+ runtimeServices: [artifacts],allowedPermissions: ["network:remote-artifacts"],});const bytes = new Uint8Array([7, 8, 9]);@@ -316,7 +389,7 @@ test("project local runtime accepts a permission-checked replacement ArtifactStosize: bytes.byteLength,mediaType: "application/octet-stream",};- assert.equal(await artifacts.store.has(sourceArtifact.digest), false);+ assert.equal(await artifactStore.has(sourceArtifact.digest), false);await assert.rejects(runtime.build({id: "source-artifact-staging",@@ -325,7 +398,7 @@ test("project local runtime accepts a permission-checked replacement ArtifactSto}),/does not bind demanded capability/u,);- assert.deepEqual(await artifacts.store.get(sourceArtifact.digest), bytes);+ assert.deepEqual(await artifactStore.get(sourceArtifact.digest), bytes);await assert.rejects(runtime.build({id: "tampered-source-artifact",diff --git a/packages/runtime/README.md b/packages/runtime/README.mdindex c4a83f3a..71da6d95 100644--- a/packages/runtime/README.md+++ b/packages/runtime/README.md@@ -13,6 +13,13 @@ The first implementation contains:- a recoverable Endpoint lifecycle that journals before `start`, checkpoints `pending`,calls `resume` after a restart, and records wake, retry, failure and cancellation state;- a narrow `CredentialStore`/`CredentialRef` port that keeps secrets out of framework facts.+- one host-neutral `RuntimeServicePackage` ABI that binds configured Scheduler, BuildStore,+ OperationStore, ArtifactStore and CredentialStore implementations to their exact Manifest facet;+ one physical package may expose several separately selected services.++A Runtime assembly owns the lifecycle of the configured packages passed to it and closes each once.+Selection grants service authority; merely being installed never grants scheduling, storage or+credential authority. These remain trusted deployment packages, not author-importable modules.The Scheduler does not traverse Graphs, choose Candidates, rewrite Needs or accept arbitraryserialized commands. Core remains the sole source of readiness and the sole Event acceptance law.diff --git a/packages/runtime/src/index.ts b/packages/runtime/src/index.tsindex f6f204d2..2ace167d 100644--- a/packages/runtime/src/index.ts+++ b/packages/runtime/src/index.ts@@ -23,5 +23,11 @@ export {verifyRuntimeProfile,} from "./profile.js";export type * from "./profile.js";+export {+ assembleRuntimeServices,+ defineRuntimeServicePackage,+ verifyRuntimeServicePackage,+} from "./services.js";+export type * from "./services.js";export { LocalBuildScheduler } from "./scheduler.js";export type * from "./types.js";diff --git a/packages/runtime/src/profile.ts b/packages/runtime/src/profile.tsindex a32ff8e2..60374fda 100644--- a/packages/runtime/src/profile.ts+++ b/packages/runtime/src/profile.ts@@ -16,6 +16,8 @@ export type RuntimeFacetRole =| "credential-store"| "capability-endpoint";+export type RuntimeServiceFacetRole = Exclude<RuntimeFacetRole, "capability-endpoint">;+export type RuntimeFacetRef = {readonly module: ModuleRef;readonly name: string;@@ -31,9 +33,9 @@ export type RuntimeCapability = {readonly returns: TypeRef;};-type RuntimeServiceFacet = {+export type RuntimeServiceFacet = {readonly name: string;- readonly role: Exclude<RuntimeFacetRole, "capability-endpoint">;+ readonly role: RuntimeServiceFacetRole;readonly implementation: RuntimeImplementation;readonly permissions: readonly string[];};@@ -94,7 +96,7 @@ export type RuntimeProfile = {export type ResolvedRuntimeService = {readonly id: string;- readonly role: Exclude<RuntimeFacetRole, "capability-endpoint">;+ readonly role: RuntimeServiceFacetRole;readonly facet: RuntimeFacetRef;readonly implementation: RuntimeImplementation;readonly configurationDigest: Digest;diff --git a/packages/runtime/src/scheduler.ts b/packages/runtime/src/scheduler.tsindex 42d956d6..177a82e6 100644--- a/packages/runtime/src/scheduler.ts+++ b/packages/runtime/src/scheduler.ts@@ -4,7 +4,8 @@ import { verifyRuntimeClosure, verifyRuntimeCoverage } from "./profile.js";import type { RuntimeClosure } from "./profile.js";import type {- LocalBuildSchedulerOptions,+ BuildScheduler,+ BuildSchedulerOptions,RuntimeCommandExecutor,RuntimeExecutionResult,RuntimeRunnableCommand,@@ -50,15 +51,15 @@ function buildCommandKey(build: string, command: string): string {* Queue-free, one-process reference Scheduler. It shares lane limits across all submitted Builds,* but never searches a Graph: every runnable command is regenerated by Core through the Executor.*/-export class LocalBuildScheduler {+export class LocalBuildScheduler implements BuildScheduler {readonly #executor: RuntimeCommandExecutor;readonly #maxConcurrency: number;readonly #laneLimits: Readonly<Record<string, number>>;readonly #maxEventsPerBuild: number;readonly #runtimeClosure: RuntimeClosure | undefined;- readonly #buildStore: LocalBuildSchedulerOptions["buildStore"];+ readonly #buildStore: BuildSchedulerOptions["buildStore"];- constructor(executor: RuntimeCommandExecutor, options: LocalBuildSchedulerOptions = {}) {+ constructor(executor: RuntimeCommandExecutor, options: BuildSchedulerOptions = {}) {this.#executor = executor;this.#maxConcurrency = positiveInteger(options.maxConcurrency ?? 4, "maxConcurrency");this.#maxEventsPerBuild = positiveInteger(options.maxEventsPerBuild ?? 1_000, "maxEventsPerBuild");diff --git a/packages/runtime/src/services.ts b/packages/runtime/src/services.tsnew file mode 100644index 00000000..4e60c380--- /dev/null+++ b/packages/runtime/src/services.ts@@ -0,0 +1,280 @@+import { canonicalize, digestOf, isDigest } from "@svml/protocol";+import type { CanonicalValue, Digest, ModuleRef } from "@svml/protocol";++import type { CredentialStore } from "./credentials.js";+import type { OperationStore } from "./operations.js";+import {+ RuntimeModuleRegistry,+} from "./profile.js";+import type {+ RuntimeModuleManifest,+ RuntimeProfileInstance,+ RuntimeServiceFacetRole,+} from "./profile.js";+import type {+ ArtifactStore,+ BuildSchedulerFactory,+ BuildStore,+} from "./types.js";++type RuntimeServiceBase<Role extends RuntimeServiceFacetRole, Service> = {+ readonly role: Role;+ readonly instance: RuntimeProfileInstance;+ readonly service: Service;+};++export type RuntimeSchedulerService = RuntimeServiceBase<"scheduler", BuildSchedulerFactory>;+export type RuntimeBuildStoreService = RuntimeServiceBase<"build-store", BuildStore>;+export type RuntimeOperationStoreService = RuntimeServiceBase<"operation-store", OperationStore>;+export type RuntimeArtifactStoreService = RuntimeServiceBase<"artifact-store", ArtifactStore>;+export type RuntimeCredentialStoreService = RuntimeServiceBase<"credential-store", CredentialStore>;++export type RuntimeService =+ | RuntimeSchedulerService+ | RuntimeBuildStoreService+ | RuntimeOperationStoreService+ | RuntimeArtifactStoreService+ | RuntimeCredentialStoreService;++/** One configured deployment package may expose several exact Runtime services. */+export type RuntimeServicePackage = {+ readonly name: string;+ readonly manifest: RuntimeModuleManifest;+ readonly services: readonly RuntimeService[];+ close?(): void | Promise<void>;+};++type RuntimeServiceDefinitionBase<Role extends RuntimeServiceFacetRole, Service> = {+ readonly role: Role;+ readonly facet: string;+ readonly instance: string;+ readonly implementation: {+ readonly locator: string;+ readonly digest: Digest;+ };+ readonly permissions?: readonly string[];+ readonly configuration?: CanonicalValue;+ readonly service: Service;+};++export type RuntimeServiceDefinition =+ | RuntimeServiceDefinitionBase<"scheduler", BuildSchedulerFactory>+ | RuntimeServiceDefinitionBase<"build-store", BuildStore>+ | RuntimeServiceDefinitionBase<"operation-store", OperationStore>+ | RuntimeServiceDefinitionBase<"artifact-store", ArtifactStore>+ | RuntimeServiceDefinitionBase<"credential-store", CredentialStore>;++export type DefineRuntimeServicePackageOptions = {+ readonly name?: string;+ readonly module: ModuleRef;+ readonly services: readonly RuntimeServiceDefinition[];+ readonly close?: () => void | Promise<void>;+};++export type RuntimeServiceSelection = {+ readonly scheduler: string;+ readonly stores: {+ readonly build?: string;+ readonly operations?: string;+ readonly artifacts?: string;+ readonly credentials?: string;+ };+};++export type RuntimeServiceAssembly = {+ readonly manifests: readonly RuntimeModuleManifest[];+ readonly instances: readonly RuntimeProfileInstance[];+ readonly scheduler: BuildSchedulerFactory;+ readonly buildStore?: BuildStore;+ readonly operationStore?: OperationStore;+ readonly artifactStore?: ArtifactStore;+ readonly credentialStore?: CredentialStore;+ close(): Promise<void>;+};++function assert(condition: unknown, message: string): asserts condition {+ if (!condition) throw new Error(message);+}++function serviceFacetKey(service: RuntimeService): string {+ const ref = service.instance.facet;+ return `${ref.module.name}@${ref.module.version}#${ref.name}`;+}++function normalizePermissions(values: readonly string[]): readonly string[] {+ const permissions = [...values].map((value) => {+ assert(value.trim().length > 0, "Runtime service permission is empty");+ return value;+ }).sort();+ assert(new Set(permissions).size === permissions.length, "Runtime service repeats a permission");+ return permissions;+}++function callable(value: object, name: string, subject: string): void {+ assert(name in value && typeof (value as Record<string, unknown>)[name] === "function",+ `${subject} does not implement ${name}()`);+}++function verifyServicePort(service: RuntimeService): void {+ assert(typeof service.service === "object" && service.service !== null,+ `${service.instance.id} Runtime service implementation is invalid`);+ switch (service.role) {+ case "scheduler":+ callable(service.service, "create", service.instance.id);+ break;+ case "build-store":+ callable(service.service, "create", service.instance.id);+ callable(service.service, "read", service.instance.id);+ callable(service.service, "compareAndSwap", service.instance.id);+ break;+ case "operation-store":+ callable(service.service, "create", service.instance.id);+ callable(service.service, "read", service.instance.id);+ callable(service.service, "list", service.instance.id);+ callable(service.service, "compareAndSwap", service.instance.id);+ break;+ case "artifact-store":+ callable(service.service, "put", service.instance.id);+ callable(service.service, "get", service.instance.id);+ callable(service.service, "has", service.instance.id);+ break;+ case "credential-store":+ callable(service.service, "resolve", service.instance.id);+ break;+ }+}++export function defineRuntimeServicePackage(+ options: DefineRuntimeServicePackageOptions,+): RuntimeServicePackage {+ assert(options.module.name.trim().length > 0 && options.module.version.trim().length > 0,+ "Runtime service module identity is invalid");+ assert(options.services.length > 0, "Runtime service package is empty");+ const instances = new Set<string>();+ const facets = new Set<string>();+ const services = options.services.map((definition): RuntimeService => {+ assert(definition.facet.trim().length > 0, "Runtime service facet is empty");+ assert(definition.instance.trim().length > 0, "Runtime service instance is empty");+ assert(!instances.has(definition.instance), `Runtime service instance ${definition.instance} is duplicated`);+ assert(!facets.has(definition.facet), `Runtime service facet ${definition.facet} is duplicated`);+ assert(definition.implementation.locator.trim().length > 0, `${definition.instance} implementation locator is empty`);+ assert(isDigest(definition.implementation.digest), `${definition.instance} implementation digest is invalid`);+ instances.add(definition.instance);+ facets.add(definition.facet);+ return {+ role: definition.role,+ instance: {+ id: definition.instance,+ facet: { module: { ...options.module }, name: definition.facet },+ configurationDigest: digestOf(canonicalize(definition.configuration ?? null)),+ },+ service: definition.service,+ } as RuntimeService;+ });+ const manifest: RuntimeModuleManifest = {+ format: "svml.runtime-module@2",+ name: options.module.name,+ version: options.module.version,+ facets: options.services.map((definition) => ({+ name: definition.facet,+ role: definition.role,+ implementation: { ...definition.implementation },+ permissions: normalizePermissions(definition.permissions ?? []),+ })),+ };+ const result: RuntimeServicePackage = {+ name: options.name ?? options.module.name,+ manifest,+ services,+ ...(options.close === undefined ? {} : { close: options.close }),+ };+ verifyRuntimeServicePackage(result);+ return result;+}++export function verifyRuntimeServicePackage(value: RuntimeServicePackage): void {+ assert(value.name.trim().length > 0, "Runtime service package name is empty");+ assert(value.services.length > 0, `${value.name} contains no Runtime service`);+ const modules = new RuntimeModuleRegistry();+ modules.register(value.manifest);+ const ids = new Set<string>();+ const claimedFacets = new Set<string>();+ for (const service of value.services) {+ assert(service.instance.id.trim().length > 0, `${value.name} Runtime service instance is empty`);+ assert(!ids.has(service.instance.id), `${value.name} repeats Runtime service ${service.instance.id}`);+ ids.add(service.instance.id);+ if (service.instance.configurationDigest !== undefined) {+ assert(isDigest(service.instance.configurationDigest),+ `${value.name} service ${service.instance.id} configuration digest is invalid`);+ }+ verifyServicePort(service);+ const resolved = modules.resolve(service.instance.facet);+ assert(resolved !== undefined, `${value.name} service ${service.instance.id} has no Manifest facet`);+ assert(resolved.facet.role === service.role,+ `${value.name} service ${service.instance.id} role differs from its Manifest facet`);+ claimedFacets.add(serviceFacetKey(service));+ }+ for (const facet of value.manifest.facets) {+ assert(facet.role !== "capability-endpoint", `${value.name} Runtime service Manifest contains an Endpoint facet`);+ const key = `${value.manifest.name}@${value.manifest.version}#${facet.name}`;+ assert(claimedFacets.has(key), `${value.name} Manifest facet ${facet.name} has no configured service`);+ }+}++type SelectedRuntimeService<Role extends RuntimeServiceFacetRole> = {+ readonly package: RuntimeServicePackage;+ readonly service: Extract<RuntimeService, { readonly role: Role }>;+};++function selectedService<Role extends RuntimeServiceFacetRole>(+ services: ReadonlyMap<string, { readonly package: RuntimeServicePackage; readonly service: RuntimeService }>,+ id: string | undefined,+ role: Role,+): SelectedRuntimeService<Role> | undefined {+ if (id === undefined) return undefined;+ const selected = services.get(id);+ assert(selected !== undefined, `Runtime service selection refers to unknown instance ${id}`);+ assert(selected.service.role === role, `Runtime service ${id} is ${selected.service.role}, not ${role}`);+ return selected as SelectedRuntimeService<Role>;+}++/** Resolve exact configured service instances without giving any package routing authority. */+export function assembleRuntimeServices(+ packages: readonly RuntimeServicePackage[],+ selection: RuntimeServiceSelection,+): RuntimeServiceAssembly {+ const services = new Map<string, { readonly package: RuntimeServicePackage; readonly service: RuntimeService }>();+ const packageNames = new Set<string>();+ for (const item of packages) {+ verifyRuntimeServicePackage(item);+ assert(!packageNames.has(item.name), `Runtime service package ${item.name} is configured twice`);+ packageNames.add(item.name);+ for (const service of item.services) {+ assert(!services.has(service.instance.id), `Runtime service instance ${service.instance.id} is configured twice`);+ services.set(service.instance.id, { package: item, service });+ }+ }+ const scheduler = selectedService(services, selection.scheduler, "scheduler")!;+ const build = selectedService(services, selection.stores.build, "build-store");+ const operations = selectedService(services, selection.stores.operations, "operation-store");+ const artifacts = selectedService(services, selection.stores.artifacts, "artifact-store");+ const credentials = selectedService(services, selection.stores.credentials, "credential-store");+ const selected = [scheduler, build, operations, artifacts, credentials]+ .filter((item): item is NonNullable<typeof item> => item !== undefined);+ const selectedPackages = [...new Set(selected.map((item) => item.package))];+ let closed = false;+ return {+ manifests: selectedPackages.map((item) => item.manifest),+ instances: selected.map((item) => item.service.instance),+ scheduler: scheduler.service.service,+ ...(build === undefined ? {} : { buildStore: build.service.service }),+ ...(operations === undefined ? {} : { operationStore: operations.service.service }),+ ...(artifacts === undefined ? {} : { artifactStore: artifacts.service.service }),+ ...(credentials === undefined ? {} : { credentialStore: credentials.service.service }),+ async close() {+ if (closed) return;+ closed = true;+ for (const item of [...packages].reverse()) await item.close?.();+ },+ };+}diff --git a/packages/runtime/src/types.ts b/packages/runtime/src/types.tsindex 1a14a9fd..73f5f4b4 100644--- a/packages/runtime/src/types.ts+++ b/packages/runtime/src/types.ts@@ -97,7 +97,7 @@ export type ScheduledBuildResult = {readonly blocked: readonly RuntimeBlockedCommand[];};-export type LocalBuildSchedulerOptions = {+export type BuildSchedulerOptions = {readonly maxConcurrency?: number;readonly laneLimits?: Readonly<Record<string, number>>;readonly maxEventsPerBuild?: number;@@ -105,3 +105,16 @@ export type LocalBuildSchedulerOptions = {/** Optional durable authority. When present, every accepted Core Event is persisted by CAS. */readonly buildStore?: BuildStore;};++/** Environment-neutral scheduling authority selected by the Runtime Profile. */+export type BuildScheduler = {+ run(requests: readonly ScheduledBuild[]): Promise<readonly ScheduledBuildResult[]>;+};++/** Installed Scheduler implementation. It receives policy only after Runtime Closure resolution. */+export type BuildSchedulerFactory = {+ create(executor: RuntimeCommandExecutor, options?: BuildSchedulerOptions): BuildScheduler;+};++/** @deprecated The options are not specific to the reference local implementation. */+export type LocalBuildSchedulerOptions = BuildSchedulerOptions;diff --git a/packages/runtime/test/services.test.ts b/packages/runtime/test/services.test.tsnew file mode 100644index 00000000..2af8b1bb--- /dev/null+++ b/packages/runtime/test/services.test.ts@@ -0,0 +1,116 @@+import assert from "node:assert/strict";+import test from "node:test";++import { digestOf } from "@svml/protocol";+import {+ MemoryBuildStore,+ MemoryOperationStore,+ assembleRuntimeServices,+ defineRuntimeServicePackage,+ verifyRuntimeServicePackage,+} from "@svml/runtime";++const moduleRef = { name: "example.runtime-services", version: "1" } as const;++function servicePackage(close?: () => void) {+ return defineRuntimeServicePackage({+ name: "example.runtime-services.local",+ module: moduleRef,+ services: [+ {+ role: "scheduler",+ facet: "scheduler",+ instance: "scheduler.example",+ implementation: {+ locator: "example.runtime-services/scheduler",+ digest: digestOf("example.runtime-services/scheduler@1"),+ },+ configuration: { algorithm: "fixture" },+ service: {+ create() {+ return { async run() { return []; } };+ },+ },+ },+ {+ role: "build-store",+ facet: "build-store",+ instance: "builds.example",+ implementation: {+ locator: "example.runtime-services/build-store",+ digest: digestOf("example.runtime-services/build-store@1"),+ },+ configuration: { database: "fixture" },+ service: new MemoryBuildStore(),+ },+ {+ role: "operation-store",+ facet: "operation-store",+ instance: "operations.example",+ implementation: {+ locator: "example.runtime-services/operation-store",+ digest: digestOf("example.runtime-services/operation-store@1"),+ },+ configuration: { database: "fixture" },+ service: new MemoryOperationStore(),+ },+ ],+ ...(close === undefined ? {} : { close }),+ });+}++test("one physical Runtime package exposes separately selected Scheduler and Store services", async () => {+ let closes = 0;+ const configured = servicePackage(() => { closes += 1; });+ const assembly = assembleRuntimeServices([configured], {+ scheduler: "scheduler.example",+ stores: {+ build: "builds.example",+ operations: "operations.example",+ },+ });+ assert.equal(assembly.manifests.length, 1);+ assert.deepEqual(assembly.instances.map((item) => item.id), [+ "scheduler.example",+ "builds.example",+ "operations.example",+ ]);+ assert.ok(assembly.buildStore);+ assert.ok(assembly.operationStore);+ assert.equal(assembly.artifactStore, undefined);+ assert.equal((await assembly.scheduler.create({+ prepare() { throw new Error("unused"); },+ async executeCommand() { throw new Error("unused"); },+ }).run([])).length, 0);+ await assembly.close();+ await assembly.close();+ assert.equal(closes, 1);+});++test("Runtime service configuration is identity-bound and role selection is exact", () => {+ const configured = servicePackage();+ assert.ok(configured.services.every((item) => item.instance.configurationDigest !== undefined));+ assert.throws(+ () => assembleRuntimeServices([configured], {+ scheduler: "builds.example",+ stores: {},+ }),+ /not scheduler/u,+ );++ const tampered = {+ ...configured,+ manifest: structuredClone(configured.manifest),+ services: [...configured.services],+ };+ (tampered.manifest.facets[1] as { role: "artifact-store" }).role = "artifact-store";+ assert.throws(() => verifyRuntimeServicePackage(tampered), /role differs/u);++ const missingPort = {+ ...configured,+ services: configured.services.map((service) => service.role === "build-store"+ ? { ...service, service: {} as never }+ : service),+ };+ assert.throws(() => verifyRuntimeServicePackage(missingPort), /does not implement create/u);+});diff --git a/packages/store-sqlite/README.md b/packages/store-sqlite/README.mdindex 2e5b2289..7812920a 100644--- a/packages/store-sqlite/README.md+++ b/packages/store-sqlite/README.md@@ -9,4 +9,8 @@ One SQLite file may physically contain both tables, but the two interfaces remai- no ready-command queue is stored. Core regenerates readiness from BuildState after every restart.Artifacts and credentials never enter this database. The adapter owns its private, versioned schema;-Core and Provider packages do not import SQLite or run these migrations.+Core and Endpoint packages do not import SQLite or run these migrations.++`createSqliteRuntimeServicePackage()` returns one physical package with two separately selectable+logical services. They share one connection and one close lifecycle without pretending that+BuildStore and OperationStore are the same interface.diff --git a/packages/store-sqlite/src/index.ts b/packages/store-sqlite/src/index.tsindex 6ec9d41c..0727379c 100644--- a/packages/store-sqlite/src/index.ts+++ b/packages/store-sqlite/src/index.ts@@ -1,9 +1,8 @@export {- sqliteBuildStoreFacet,+ SqliteRuntimeState,+ createSqliteRuntimeServicePackage,sqliteBuildStoreImplementationDigest,- sqliteOperationStoreFacet,sqliteOperationStoreImplementationDigest,sqliteStoreModuleRef,- sqliteStoreRuntimeManifest,-} from "./manifest.js";-export { SqliteRuntimeState } from "./store.js";+} from "./store.js";+export type * from "./store.js";diff --git a/packages/store-sqlite/src/manifest.ts b/packages/store-sqlite/src/manifest.tsdeleted file mode 100644index 63332b92..00000000--- a/packages/store-sqlite/src/manifest.ts+++ /dev/null@@ -1,51 +0,0 @@-import { digestOf } from "@svml/core";-import type { RuntimeModuleManifest } from "@svml/runtime";--export const sqliteStoreModuleRef = {- name: "@svml/store-sqlite",- version: "1",-} as const;--export const sqliteBuildStoreFacet = {- module: sqliteStoreModuleRef,- name: "build-store",-} as const;--export const sqliteOperationStoreFacet = {- module: sqliteStoreModuleRef,- name: "operation-store",-} as const;--export const sqliteBuildStoreImplementationDigest = digestOf(- "@svml/store-sqlite/build-store@1",-);--export const sqliteOperationStoreImplementationDigest = digestOf(- "@svml/store-sqlite/operation-store@1",-);--export const sqliteStoreRuntimeManifest: RuntimeModuleManifest = {- format: "svml.runtime-module@2",- name: sqliteStoreModuleRef.name,- version: sqliteStoreModuleRef.version,- facets: [- {- name: sqliteBuildStoreFacet.name,- role: "build-store",- implementation: {- locator: "@svml/store-sqlite/build-store",- digest: sqliteBuildStoreImplementationDigest,- },- permissions: ["filesystem:state"],- },- {- name: sqliteOperationStoreFacet.name,- role: "operation-store",- implementation: {- locator: "@svml/store-sqlite/operation-store",- digest: sqliteOperationStoreImplementationDigest,- },- permissions: ["filesystem:state"],- },- ],-};diff --git a/packages/store-sqlite/src/store.ts b/packages/store-sqlite/src/store.tsindex 36ed9215..327c2b21 100644--- a/packages/store-sqlite/src/store.ts+++ b/packages/store-sqlite/src/store.ts@@ -6,11 +6,13 @@ import {canonicalStringify,verifyBuildState,} from "@svml/core";+import { digestOf } from "@svml/protocol";import type {BuildState,Digest,} from "@svml/protocol";import {+ defineRuntimeServicePackage,sealOperationCompletion,verifyOperationIdentity,verifyOperationSnapshot,@@ -26,14 +28,35 @@ import type {OperationStore,OperationStoreWrite,OperationUpdate,+ RuntimeServicePackage,} from "@svml/runtime";const schemaVersion = 1;-type SqliteRuntimeStateOptions = {+export const sqliteStoreModuleRef = {+ name: "@svml/store-sqlite",+ version: "1",+} as const;++export const sqliteBuildStoreImplementationDigest = digestOf(+ "@svml/store-sqlite/build-store@1",+);++export const sqliteOperationStoreImplementationDigest = digestOf(+ "@svml/store-sqlite/operation-store@1",+);++export type SqliteRuntimeStateOptions = {readonly busyTimeoutMs?: number;};+export type CreateSqliteRuntimeServicePackageOptions = SqliteRuntimeStateOptions & {+ readonly path: string;+ readonly name?: string;+ readonly buildInstance?: string;+ readonly operationInstance?: string;+};+type Row = Record<string, unknown>;function assert(condition: unknown, message: string): asserts condition {@@ -305,3 +328,57 @@ export class SqliteRuntimeState {this.#database.close();}}++export function createSqliteRuntimeServicePackage(+ options: CreateSqliteRuntimeServicePackageOptions,+): RuntimeServicePackage {+ const state = new SqliteRuntimeState(options.path, {+ ...(options.busyTimeoutMs === undefined ? {} : { busyTimeoutMs: options.busyTimeoutMs }),+ });+ const buildInstance = options.buildInstance ?? "builds.sqlite";+ const operationInstance = options.operationInstance ?? "operations.sqlite";+ try {+ return defineRuntimeServicePackage({+ name: options.name ?? "state.sqlite",+ module: sqliteStoreModuleRef,+ services: [+ {+ role: "build-store",+ facet: "build-store",+ instance: buildInstance,+ implementation: {+ locator: "@svml/store-sqlite/build-store",+ digest: sqliteBuildStoreImplementationDigest,+ },+ permissions: ["filesystem:state"],+ configuration: {+ path: state.path,+ schemaVersion,+ busyTimeoutMs: options.busyTimeoutMs ?? 5_000,+ },+ service: state.builds,+ },+ {+ role: "operation-store",+ facet: "operation-store",+ instance: operationInstance,+ implementation: {+ locator: "@svml/store-sqlite/operation-store",+ digest: sqliteOperationStoreImplementationDigest,+ },+ permissions: ["filesystem:state"],+ configuration: {+ path: state.path,+ schemaVersion,+ busyTimeoutMs: options.busyTimeoutMs ?? 5_000,+ },+ service: state.operations,+ },+ ],+ close: () => state.close(),+ });+ } catch (error) {+ state.close();+ throw error;+ }+}