Skip to content

armgit / hypit

Public Imported from GitHub
0

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

refactor: remove remaining defensive runtime residue

kashorin7638d03e81fc
diff --git a/packages/artifact-store-fs/src/store.ts b/packages/artifact-store-fs/src/store.ts
index fe66aaf0..cfacbc92 100644
--- a/packages/artifact-store-fs/src/store.ts
+++ b/packages/artifact-store-fs/src/store.ts
@@ -4,6 +4,7 @@ import {
mkdir,
open as openFile,
readdir,
+ rm,
stat,
unlink,
} from "node:fs/promises";
@@ -64,7 +65,7 @@ export class FileArtifactStore implements ArtifactStore {
await handle.close();
}
} catch (error) {
- await unlink(temporary).catch(() => undefined);
+ await rm(temporary, { force: true });
throw error;
}
const digest = `sha256:${hash.digest("hex")}` as Digest;
@@ -74,11 +75,11 @@ export class FileArtifactStore implements ArtifactStore {
await link(temporary, path);
} catch (error) {
if (!isNodeError(error, "EEXIST")) {
- await unlink(temporary).catch(() => undefined);
+ await rm(temporary, { force: true });
throw error;
}
}
- await unlink(temporary).catch(() => undefined);
+ await rm(temporary, { force: true });
return { kind: "blob", digest, size, mediaType };
}
diff --git a/packages/artifact-store-s3/src/activation.ts b/packages/artifact-store-s3/src/activation.ts
index fb2f8ee4..d771dddf 100644
--- a/packages/artifact-store-s3/src/activation.ts
+++ b/packages/artifact-store-s3/src/activation.ts
@@ -9,9 +9,7 @@ import {
import type { RuntimeAdapterFactoryContext } from "@narratage/runtime-kit";
import { AwsS3ObjectClient } from "./client.js";
-import { createS3ArtifactStore, s3ArtifactKey } from "./store.js";
-
-const VALIDATION_DIGEST = "sha256:0000000000000000000000000000000000000000000000000000000000000000" as const;
+import { createS3ArtifactStore, normalizeS3ArtifactPrefix } from "./store.js";
function validateConfig(context: RuntimeAdapterFactoryContext): void {
const config = runtimeConfigObject(context.config, "S3 ArtifactStore");
@@ -20,7 +18,7 @@ function validateConfig(context: RuntimeAdapterFactoryContext): void {
], "S3 ArtifactStore");
if (runtimeConfigString(config.bucket, "S3 bucket") === undefined) throw new Error("S3 bucket is required");
const prefix = runtimeConfigString(config.prefix, "S3 prefix");
- s3ArtifactKey(prefix, VALIDATION_DIGEST);
+ normalizeS3ArtifactPrefix(prefix);
runtimeConfigString(config.expectedBucketOwner, "S3 expectedBucketOwner");
runtimeConfigString(config.region, "S3 region");
runtimeConfigString(config.endpoint, "S3 endpoint");
diff --git a/packages/artifact-store-s3/src/store.ts b/packages/artifact-store-s3/src/store.ts
index ef657722..a8c55116 100644
--- a/packages/artifact-store-s3/src/store.ts
+++ b/packages/artifact-store-s3/src/store.ts
@@ -23,7 +23,7 @@ export function s3ArtifactKey(prefix: string | undefined, digest: Digest): strin
if (!isDigest(digest)) throw new Error("Artifact digest is invalid");
const [algorithm, hex] = digest.split(":");
if (algorithm !== "sha256" || hex === undefined) throw new Error(`unsupported Artifact digest ${digest}`);
- const normalized = normalizePrefix(prefix);
+ const normalized = normalizeS3ArtifactPrefix(prefix);
const relative = `${algorithm}/${hex.slice(0, 2)}/${hex}`;
return normalized.length === 0 ? relative : `${normalized}/${relative}`;
}
@@ -54,7 +54,7 @@ function assert(condition: unknown, message: string): asserts condition {
if (!condition) throw new Error(message);
}
-function normalizePrefix(prefix: string | undefined): string {
+export function normalizeS3ArtifactPrefix(prefix: string | undefined): string {
if (prefix === undefined || prefix.length === 0) return "";
const normalized = prefix.replace(/^\/+|\/+$/gu, "");
assert(normalized.length > 0, "S3 Artifact prefix must contain a non-slash character");
@@ -96,7 +96,7 @@ export class S3ArtifactStore implements ArtifactStore {
}
this.#client = options.client;
this.#bucket = options.bucket;
- this.#prefix = normalizePrefix(options.prefix);
+ this.#prefix = normalizeS3ArtifactPrefix(options.prefix);
this.#expectedBucketOwner = options.expectedBucketOwner;
this.#partSizeBytes = positiveInteger(options.partSizeBytes ?? 16 * 1024 * 1024, "partSizeBytes");
assert(this.#partSizeBytes >= 5 * 1024 * 1024, "S3 requires multipart parts of at least 5 MiB");
@@ -129,10 +129,6 @@ export class S3ArtifactStore implements ArtifactStore {
ContentLength: copy.byteLength,
ContentType: mediaType,
ChecksumSHA256: hash.digest("base64"),
- Metadata: {
- "narratage-digest": digest,
- "narratage-size": String(copy.byteLength),
- },
...(this.#expectedBucketOwner === undefined
? {}
: { ExpectedBucketOwner: this.#expectedBucketOwner }),
@@ -262,7 +258,6 @@ export class S3ArtifactStore implements ArtifactStore {
CopySource: `${this.#bucket}/${staging}`,
ContentType: mediaType,
MetadataDirective: "REPLACE",
- Metadata: { "narratage-digest": digest, "narratage-size": String(size) },
...owner,
});
} finally {
@@ -313,7 +308,7 @@ export class S3ArtifactStore implements ArtifactStore {
export function createS3ArtifactStore(
options: CreateS3ArtifactStorePackageOptions,
): S3ArtifactStore {
- const prefix = normalizePrefix(options.prefix);
+ const prefix = normalizeS3ArtifactPrefix(options.prefix);
const client = options.client ?? new AwsS3ObjectClient({
...(options.region === undefined ? {} : { region: options.region }),
...(options.endpoint === undefined ? {} : { endpoint: options.endpoint }),
diff --git a/packages/artifact-store-s3/test/store.test.ts b/packages/artifact-store-s3/test/store.test.ts
index 5b7e0501..a0c8b692 100644
--- a/packages/artifact-store-s3/test/store.test.ts
+++ b/packages/artifact-store-s3/test/store.test.ts
@@ -36,7 +36,6 @@ test("S3 artifacts use deterministic content-addressed keys", async () => {
/** A client that can do everything, backed by an in-memory bucket. */
class FullFakeS3 extends FakeS3 {
readonly uploads = new Map<string, Uint8Array[]>();
- copies = 0;
heads = 0;
async open(input: Parameters<NonNullable<S3ObjectClient["open"]>>[0]) {
@@ -83,7 +82,6 @@ class FullFakeS3 extends FakeS3 {
async abortMultipart() {}
async copy(input: Parameters<NonNullable<S3ObjectClient["copy"]>>[0]) {
- this.copies += 1;
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`);
@@ -130,16 +128,6 @@ test("an empty Artifact is a legitimate one, and S3 will not accept a partless u
assert.deepEqual(await store.get(ref.digest), new Uint8Array(0));
});
-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);
-});
-
test("a streamed read hands back bytes as they arrive", async () => {
const client = new FullFakeS3();
const store = new S3ArtifactStore({ client, bucket: "fixture" });
diff --git a/packages/cli/src/archive.ts b/packages/cli/src/archive.ts
index e8934cd7..964365e0 100644
--- a/packages/cli/src/archive.ts
+++ b/packages/cli/src/archive.ts
@@ -76,7 +76,6 @@ export async function materializeArtifact(
size += chunk.byteLength;
await output.write(chunk);
}
- await output.sync();
await output.close();
if (size !== artifact.size) throw new Error(`Artifact ${artifact.digest} size differs from ${subject}`);
// Replace an explicitly selected destination only after the stream completes.
diff --git a/packages/cli/src/main.ts b/packages/cli/src/main.ts
index 0baa153f..152ff8ea 100644
--- a/packages/cli/src/main.ts
+++ b/packages/cli/src/main.ts
@@ -447,7 +447,6 @@ function createCatalogDescriptor(options: {
return { name: item.name, ref: item.ref };
});
return {
- format: "narratage.build-catalog-descriptor@1",
source: {
path: resolve(options.source),
},
@@ -1074,12 +1073,12 @@ export async function runCli(
const [worker, external, selectedRuntime] = loaded;
runtime = selectedRuntime;
const queue = await runtime.queue();
- const counts = Object.fromEntries(["queued", "running", "waiting", "blocked", "terminal"]
+ const counts = Object.fromEntries(["queued", "running", "waiting", "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", "running", "waiting", "blocked"]
+ const active = ["queued", "running", "waiting"]
.reduce((total, phase) => total + (counts[phase] ?? 0), 0);
const attention = active > 0 && !ready;
const unavailable = external.programs.filter((item) => item.state.state !== "ready");
diff --git a/packages/cli/src/source-discovery.ts b/packages/cli/src/source-discovery.ts
index c58c51be..10d84cf2 100644
--- a/packages/cli/src/source-discovery.ts
+++ b/packages/cli/src/source-discovery.ts
@@ -3,6 +3,7 @@ import { dirname, isAbsolute, relative, resolve } from "node:path";
import { authorFrontendsFromHostFacets, prepareAuthorSource } from "@narratage/elaborator";
import type { AuthorFrontend } from "@narratage/elaborator";
+import { physicalPackageName } from "@narratage/package-loader-node";
import type { LogicalPackageAddress, LoadedPackage } from "@narratage/package-loader-node";
import { modulePackageAbi } from "@narratage/protocol";
import { prepareRunSource, runFragmentHostAbi, runFrontendsFromHostFacets } from "@narratage/run";
@@ -14,19 +15,10 @@ function isWithin(root: string, path: string): boolean {
return relation === "" || (!relation.startsWith("..") && !isAbsolute(relation));
}
-/** Conventional physical package name for a logical Source import. */
-function physicalPackage(request: string): string {
+function selectedPackage(request: string): string {
const version = request.lastIndexOf("@");
if (version <= 0) throw new Error(`Package request ${request} must end in @version`);
- const logical = request.slice(0, version);
- if (logical.startsWith("@")) {
- const slash = logical.indexOf("/", 1);
- if (slash < 0) throw new Error(`Scoped package request ${request} is invalid`);
- const subpath = logical.indexOf("/", slash + 1);
- return subpath < 0 ? logical : logical.slice(0, subpath);
- }
- const subpath = logical.indexOf("/");
- return subpath < 0 ? logical : logical.slice(0, subpath);
+ return physicalPackageName(request);
}
function relativeSource(importer: string, request: string): string {
@@ -75,7 +67,7 @@ export async function discoverSourcePackages(
const requireLogical = (address: LogicalPackageAddress, physical: string | undefined): void => {
logical.set(addressKey(address), address);
- selected.add(physical ?? physicalPackage(address.name));
+ selected.add(physical ?? selectedPackage(address.name));
};
const moduleOwner = (request: string): string | undefined => packages.find((item) =>
(item.contribution.modules ?? []).some((module) => [
diff --git a/packages/cli/test/control-commands.test.ts b/packages/cli/test/control-commands.test.ts
index 70ec57f1..07c7c17d 100644
--- a/packages/cli/test/control-commands.test.ts
+++ b/packages/cli/test/control-commands.test.ts
@@ -63,11 +63,8 @@ test("status --watch reattaches to one durable Build until it becomes terminal",
diagnostics: [],
};
const dispatch = (phase: "queued" | "terminal") => ({
- format: "narratage.build-dispatch-identity@1",
- id: "sha256:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
build: "build-watch",
- core: state.id,
- revision: phase === "queued" ? 0 : 1,
+ implementationPackages: [],
createdAt: 1,
availableAt: 1,
phase,
@@ -175,7 +172,7 @@ test("auth opens only one Endpoint credential control, never the execution Runti
slot: "apiKey",
label: "KIE API key",
kind: "secret",
- ref: { format: "narratage.credential-ref@1", store: "env", key: "KIE_API_KEY" },
+ ref: { store: "env", key: "KIE_API_KEY" },
configured: false,
writable: false,
}];
@@ -219,7 +216,7 @@ test("auth login rejects a read-only CredentialStore before asking for a secret"
slot: "apiKey",
label: "KIE API key",
kind: "secret",
- ref: { format: "narratage.credential-ref@1", store: "env", key: "KIE_API_KEY" },
+ ref: { store: "env", key: "KIE_API_KEY" },
configured: false,
writable: false,
}];
diff --git a/packages/credential-store-keychain/test/store.test.ts b/packages/credential-store-keychain/test/store.test.ts
index 1f6fd445..48ec62d5 100644
--- a/packages/credential-store-keychain/test/store.test.ts
+++ b/packages/credential-store-keychain/test/store.test.ts
@@ -25,7 +25,7 @@ test("an absent entry is absent, and an empty one is not a secret", async () =>
test("a malformed reference is refused before anything is looked up", async () => {
let asked = 0;
const store = new KeychainCredentialStore({ read: async () => { asked += 1; return "x"; } });
- await assert.rejects(async () => await store.resolve({ format: "narratage.credential-ref@1", store: "keychain", key: " " } as never));
+ await assert.rejects(async () => await store.resolve({ store: "keychain", key: " " } as never));
assert.equal(asked, 0);
});
diff --git a/packages/driver-node/src/driver.ts b/packages/driver-node/src/driver.ts
index 8cdd82ba..921cbd89 100644
--- a/packages/driver-node/src/driver.ts
+++ b/packages/driver-node/src/driver.ts
@@ -16,9 +16,6 @@ import {
validateValue,
} from "@narratage/validation";
import type { TypeValidatorRegistryLike } from "@narratage/validation";
-import {
- sealOperationIdentity,
-} from "@narratage/runtime";
import type {
CredentialStore,
CredentialValue,
@@ -159,11 +156,7 @@ export class NodeDriver {
return {
executable: {
command,
- resources: registration.scheduling?.resources ?? [{
- id: `producer:${producerRegistryKey(command.producer)}`,
- maxActive: Number.MAX_SAFE_INTEGER,
- maxInFlight: Number.MAX_SAFE_INTEGER,
- }],
+ resources: registration.scheduling?.resources ?? [],
run: async () =>
registration.handler({
command: structuredClone(command),
@@ -319,7 +312,7 @@ export class NodeDriver {
}
const fresh = latest === undefined;
const identity = fresh
- ? sealOperationIdentity({ id: `op_${randomUUID()}`, ...base })
+ ? { id: `op_${randomUUID()}`, ...base }
: latest;
const endpointContext = {
command: structuredClone(executable.command),
diff --git a/packages/elaborator/src/index.ts b/packages/elaborator/src/index.ts
index 044b9d19..9d48bce6 100644
--- a/packages/elaborator/src/index.ts
+++ b/packages/elaborator/src/index.ts
@@ -16,7 +16,6 @@ export {
compileSourceClosure,
prepareAuthorSource,
resolveCompiledSourceExport,
- verifySourceClosure,
} from "./source.js";
export {
authorFrontendsFromHostFacets,
diff --git a/packages/elaborator/src/source.ts b/packages/elaborator/src/source.ts
index f9eeb430..1c5a0d8b 100644
--- a/packages/elaborator/src/source.ts
+++ b/packages/elaborator/src/source.ts
@@ -10,10 +10,8 @@ import type {
import {
maskSourceHeader,
parseSourceHeader,
- verifyCompiledSourceIdentity,
} from "@narratage/source";
import type {
- CompiledSourceIdentity,
ResolvedSourceAsset,
SourceAssetRequest,
SourceAssetResolver,
@@ -118,9 +116,9 @@ export type AuthorRecordAdmitter = (
record: TypedRecord,
) => Awaitable<void>;
-export type SourceClosureUnit = CompiledSourceIdentity & {
- readonly format: "narratage.source-unit@1";
+export type SourceClosureUnit = {
readonly id: string;
+ readonly frontend: string;
readonly imports: readonly {
readonly alias: string;
readonly source: string;
@@ -128,7 +126,6 @@ export type SourceClosureUnit = CompiledSourceIdentity & {
};
export type SourceClosure = {
- readonly format: "narratage.source-closure@1";
readonly entry: string;
readonly units: readonly SourceClosureUnit[];
};
@@ -305,7 +302,6 @@ 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,
imports: imports
@@ -461,12 +457,10 @@ export async function compileSourceClosure(
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 sourceClosure: SourceClosure = {
- format: "narratage.source-closure@1" as const,
entry: entry.unit.id,
units,
};
const componentsById = new Map(components.map((component) => [component.id, component]));
- verifySourceClosure(sourceClosure);
return {
closure: sourceClosure,
program,
@@ -481,24 +475,6 @@ export async function compileSourceClosure(
};
}
-export function verifySourceClosure(closure: SourceClosure): void {
- assert(closure.format === "narratage.source-closure@1", "UNSUPPORTED_SOURCE_CLOSURE", "unsupported Source Closure format");
- 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(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);
- }
- assert(units.has(closure.entry), "UNKNOWN_SOURCE_ENTRY", `Source Closure entry ${closure.entry} is absent`);
- for (const unit of closure.units) {
- for (const item of unit.imports) {
- assert(units.has(item.source), "UNKNOWN_SOURCE_IMPORT", `${unit.id} imports absent SourceUnit ${item.source}`);
- }
- }
-}
-
export function resolveCompiledSourceExport(
compiled: CompiledSourceClosure,
name: string,
diff --git a/packages/package-loader-node/src/index.ts b/packages/package-loader-node/src/index.ts
index 87a3b2e6..4297b963 100644
--- a/packages/package-loader-node/src/index.ts
+++ b/packages/package-loader-node/src/index.ts
@@ -4,5 +4,6 @@ export {
export {
loadNodePackageSelection,
NodePackageSelectionMissingError,
+ physicalPackageName,
} from "./loader.js";
export type * from "./types.js";
diff --git a/packages/package-loader-node/src/loader.ts b/packages/package-loader-node/src/loader.ts
index a1238fb0..9c56009a 100644
--- a/packages/package-loader-node/src/loader.ts
+++ b/packages/package-loader-node/src/loader.ts
@@ -49,14 +49,18 @@ async function packageJson(root: string): Promise<PackageJson> {
return parsePackageJson(JSON.parse(await readFile(path, "utf8")), path);
}
+function missingFile(error: unknown): boolean {
+ return error instanceof Error && "code" in error && error.code === "ENOENT";
+}
+
async function packageRoot(entry: string, expectedName: string): Promise<ResolvedPackage> {
let cursor = dirname(await realpath(entry));
while (true) {
try {
const json = await packageJson(cursor);
if (json.name === expectedName) return { root: cursor, json };
- } catch {
- // Continue to the package that owns the resolved entry.
+ } catch (error) {
+ if (!missingFile(error)) throw error;
}
const parent = dirname(cursor);
if (parent === cursor) throw new Error(`resolved entry for ${expectedName} is outside its package`);
@@ -66,15 +70,17 @@ async function packageRoot(entry: string, expectedName: string): Promise<Resolve
async function resolvePackage(specifier: string, from: string): Promise<ResolvedPackage> {
const resolver = createRequire(join(resolve(from), "__narratage_package_loader__.cjs"));
+ let entry: string;
try {
- return await packageRoot(resolver.resolve(specifier), specifier);
+ entry = resolver.resolve(specifier);
} catch (entryError) {
try {
- return await packageRoot(resolver.resolve(`${specifier}/package.json`), specifier);
+ entry = resolver.resolve(`${specifier}/package.json`);
} catch {
throw new Error(`cannot resolve installed package ${specifier} from ${from}: ${entryError instanceof Error ? entryError.message : String(entryError)}`);
}
}
+ return await packageRoot(entry, specifier);
}
function activationPath(item: ResolvedPackage): string {
@@ -112,7 +118,7 @@ function offers(value: NodePackageContribution): readonly LogicalPackageAddress[
}
/** Conventional physical package spelling for a logical @scope/name@version address. */
-function physicalHint(logical: string): string {
+export function physicalPackageName(logical: string): string {
const slash = logical.startsWith("@") ? logical.indexOf("/", 1) : -1;
const version = logical.lastIndexOf("@");
const unversioned = version > Math.max(slash, 0) ? logical.slice(0, version) : logical;
@@ -143,7 +149,7 @@ export async function loadNodePackageSelection(
? { selected: request }
: request as NodePackageSelectionRequest;
const selected = new Set(normalized.selected);
- for (const address of normalized.logical ?? []) selected.add(physicalHint(address.name));
+ for (const address of normalized.logical ?? []) selected.add(physicalPackageName(address.name));
if (selected.size === 0) return [];
const roots = await Promise.all([...selected].sort().map(async (name) => await resolvePackage(name, root)));
@@ -175,7 +181,7 @@ export async function loadNodePackageSelection(
for (let cursor = 0; cursor < requirements.length; cursor += 1) {
const requirement = requirements[cursor] as { readonly key: string; readonly from: string };
if (providedModules.has(requirement.key)) continue;
- const providerPackage = physicalHint(requirement.key);
+ const providerPackage = physicalPackageName(requirement.key);
assert(!activated.has(providerPackage), `selected package ${providerPackage} provides the wrong ${requirement.key}`);
const physical = await resolvePackage(providerPackage, requirement.from);
const provider = { physical, contribution: await importContribution(physical) };
diff --git a/packages/runtime-host-node/src/index.ts b/packages/runtime-host-node/src/index.ts
index 4445b2ac..f5e7db90 100644
--- a/packages/runtime-host-node/src/index.ts
+++ b/packages/runtime-host-node/src/index.ts
@@ -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" | "complete" | "failed" | "cancelled";
+ readonly status: "queued" | "running" | "waiting" | "complete" | "failed" | "cancelled";
readonly dispatch: BuildDispatchSnapshot;
};
@@ -267,20 +267,23 @@ export function resolveRuntimeExecutable(root: string, value: string): string {
}
async function executableExists(value: string): Promise<boolean> {
+ const unavailable = (error: unknown) => error instanceof Error && "code" in error
+ && ["ENOENT", "ENOTDIR", "EACCES"].includes(String(error.code));
if (pathLike(value)) {
try {
await access(value, constants.X_OK);
return true;
- } catch {
- return false;
+ } catch (error) {
+ if (unavailable(error)) return false;
+ throw error;
}
}
for (const directory of (process.env.PATH ?? "").split(delimiter).filter(Boolean)) {
try {
await access(resolve(directory, value), constants.X_OK);
return true;
- } catch {
- // Continue searching PATH.
+ } catch (error) {
+ if (!unavailable(error)) throw error;
}
}
return false;
diff --git a/packages/runtime-kit/src/index.ts b/packages/runtime-kit/src/index.ts
index 638d5a60..6f943d0c 100644
--- a/packages/runtime-kit/src/index.ts
+++ b/packages/runtime-kit/src/index.ts
@@ -191,11 +191,8 @@ export class RuntimeAdapterRegistry {
this.#registrations.set(key, facet.implementation);
}
- has(use: string, kind?: RuntimeAdapterKind): boolean {
- return kind === undefined
- ? (["endpoint", "artifact-store", "credential-store"] as const).some((value) =>
- this.#registrations.has(this.#key(use, value)))
- : this.#registrations.has(this.#key(use, kind));
+ has(use: string, kind: RuntimeAdapterKind): boolean {
+ return this.#registrations.has(this.#key(use, kind));
}
async activateEndpoint(use: string, context: RuntimeAdapterFactoryContext): Promise<RuntimeEndpointActivation> {
diff --git a/packages/runtime-kit/test/registry.test.ts b/packages/runtime-kit/test/registry.test.ts
index 742b57d1..77778b98 100644
--- a/packages/runtime-kit/test/registry.test.ts
+++ b/packages/runtime-kit/test/registry.test.ts
@@ -42,7 +42,6 @@ test("a facet declares which kind it is, and the registry keeps the two apart",
assert.equal(registry.has("example.endpoint", "artifact-store"), false);
assert.ok(registry.has("example.service", "artifact-store"));
assert.equal(registry.has("example.service", "endpoint"), false);
- assert.equal(registry.has("example.absent"), false);
});
test("Endpoint and Artifact Store adapters may share one package address", () => {
@@ -143,15 +142,3 @@ test("Runtime config readers accept a value or refuse it; they never guess one",
assert.throws(() => runtimeConfigExact(config, ["a", "n"], "subject"), /does not accept b/u);
assert.doesNotThrow(() => runtimeConfigExact(config, ["a", "n", "b"], "subject"));
});
-
-/**
- * Pin.
- *
- * ABI and offers carry the address. Identity must not repeat it.
- */
-test("an adapter stores its address once", () => {
- const facet = endpoint("@narratage/example-provider");
- assert.equal("identity" in facet, false);
- assert.deepEqual(facet.offers, ["@narratage/example-provider"]);
- assert.equal(facet.abi, "narratage.runtime-endpoint-adapter-host@1");
-});
diff --git a/packages/runtime-local/src/control.ts b/packages/runtime-local/src/control.ts
index fd92cb84..35b69952 100644
--- a/packages/runtime-local/src/control.ts
+++ b/packages/runtime-local/src/control.ts
@@ -68,7 +68,7 @@ export function createLocalRuntimeArchiveControl(
},
async queue() {
const [dispatches, capacity] = await Promise.all([
- options.dispatchStore.list({ phases: ["queued", "running", "waiting", "blocked"] }),
+ options.dispatchStore.list({ phases: ["queued", "running", "waiting"] }),
options.dispatchStore.listCapacity(),
]);
const operationHistory = (await Promise.all(dispatches.map(async (item) =>
@@ -129,7 +129,7 @@ export function createLocalRuntimeControl(
assert(isEnumerableBuildStore(options.buildStore),
"selected BuildStore does not expose the maintenance index required for Artifact GC");
if (gc.apply === true) {
- const active = await options.dispatchStore.list({ phases: ["queued", "running", "waiting", "blocked"] });
+ const active = await options.dispatchStore.list({ phases: ["queued", "running", "waiting"] });
assert(active.length === 0,
`Artifact GC cannot delete while ${active.length} Build${active.length === 1 ? " is" : "s are"} active`);
}
diff --git a/packages/runtime-local/src/host.ts b/packages/runtime-local/src/host.ts
index 7fbcfb4e..0bf726e5 100644
--- a/packages/runtime-local/src/host.ts
+++ b/packages/runtime-local/src/host.ts
@@ -41,18 +41,15 @@ async function createLocalRuntimeHost(context: NodeRuntimeHostAdapterContext): P
const selection = await resolveRuntimeConfigPaths(profile, { packageRoot });
const implementationPackages = async (): Promise<readonly string[]> => {
const values = new Set(options.implementationPackages ?? []);
- let archive: Awaited<ReturnType<typeof createRuntimeArchiveFromConfig>> | undefined;
+ const archive = await createRuntimeArchiveFromConfig(profile, { packageRoot, readOnly: true });
try {
- archive = await createRuntimeArchiveFromConfig(profile, { packageRoot, readOnly: true });
const queue = await archive.queue();
for (const dispatch of queue.dispatches) {
if (dispatch.phase === "terminal") continue;
for (const item of dispatch.implementationPackages) values.add(item);
}
- } catch {
- // Before the first submission there may be no durable Runtime state to inspect.
} finally {
- await archive?.close();
+ await archive.close();
}
return [...values].sort();
};
diff --git a/packages/runtime-local/src/programs.ts b/packages/runtime-local/src/programs.ts
index 3a14d3f4..350f485b 100644
--- a/packages/runtime-local/src/programs.ts
+++ b/packages/runtime-local/src/programs.ts
@@ -62,6 +62,10 @@ function directory(root: string): string {
const LOG_ROTATE_BYTES = 10 * 1024 * 1024;
+function nodeError(error: unknown, code: string): boolean {
+ return error instanceof Error && "code" in error && error.code === code;
+}
+
async function withProgramLifecycleLock<T>(
root: string,
id: string,
@@ -76,22 +80,28 @@ async function withProgramLifecycleLock<T>(
try {
lock = await open(path, "wx");
} catch (error) {
- if (!(error instanceof Error && "code" in error && error.code === "EEXIST")) throw error;
- const owner = await readFile(path, "utf8").then((text) => JSON.parse(text) as {
- readonly pid?: unknown;
- }).catch(() => undefined);
- if (owner === undefined || !Number.isSafeInteger(owner.pid) || !alive(owner.pid as number)) {
+ if (!nodeError(error, "EEXIST")) throw error;
+ let owner: { readonly pid?: unknown } | undefined;
+ try {
+ owner = JSON.parse(await readFile(path, "utf8")) as { readonly pid?: unknown };
+ } catch (readError) {
+ if (nodeError(readError, "ENOENT")) continue;
+ if (Date.now() >= deadline) throw new Error(`External program ${id} lifecycle lock is unreadable: ${path}`);
+ await sleep(25);
+ continue;
+ }
+ if (Number.isSafeInteger(owner.pid) && !alive(owner.pid as number)) {
await rm(path, { force: true });
continue;
}
if (Date.now() >= deadline) {
- throw new Error(`External program ${id} lifecycle is busy in process ${String(owner.pid)}; lock: ${path}`);
+ throw new Error(`External program ${id} lifecycle is busy${Number.isSafeInteger(owner.pid) ? ` in process ${String(owner.pid)}` : ""}; lock: ${path}`);
}
await sleep(25);
continue;
}
try {
- await lock.writeFile(JSON.stringify({ pid: process.pid, acquiredAt: Date.now() }), "utf8");
+ await lock.writeFile(JSON.stringify({ pid: process.pid }), "utf8");
return await runLocked();
} finally {
await lock.close();
@@ -101,7 +111,12 @@ async function withProgramLifecycleLock<T>(
}
async function rotateLog(path: string): Promise<void> {
- const size = await stat(path).then((value) => value.size).catch(() => 0);
+ let size = 0;
+ try {
+ size = (await stat(path)).size;
+ } catch (error) {
+ if (!nodeError(error, "ENOENT")) throw error;
+ }
if (size <= LOG_ROTATE_BYTES) return;
const previous = `${path}.previous`;
await rm(previous, { force: true });
@@ -112,9 +127,11 @@ async function readPid(root: string, id: string): Promise<number | undefined> {
try {
const text = await readFile(join(directory(root), `${id}.pid`), "utf8");
const pid = Number.parseInt(text.trim(), 10);
- return Number.isSafeInteger(pid) && pid > 0 ? pid : undefined;
- } catch {
- return undefined;
+ if (!Number.isSafeInteger(pid) || pid < 1) throw new Error(`External program ${id} pid file is invalid`);
+ return pid;
+ } catch (error) {
+ if (nodeError(error, "ENOENT")) return undefined;
+ throw error;
}
}
@@ -309,7 +326,13 @@ export async function reportManagedPrograms(
const state = await program.probe();
const pid = await readPid(dataRoot, program.id);
const logPath = join(directory(dataRoot), `${program.id}.log`);
- const hasLog = await stat(logPath).then(() => true).catch(() => false);
+ let hasLog = false;
+ try {
+ await stat(logPath);
+ hasLog = true;
+ } catch (error) {
+ if (!nodeError(error, "ENOENT")) throw error;
+ }
return {
id: program.id,
instances,
diff --git a/packages/runtime-local/src/runtime.ts b/packages/runtime-local/src/runtime.ts
index 4781c67f..5a90668b 100644
--- a/packages/runtime-local/src/runtime.ts
+++ b/packages/runtime-local/src/runtime.ts
@@ -8,7 +8,6 @@ import {
EndpointRegistry,
} from "@narratage/driver-node";
import {
- createBuildDispatchIdentity,
isStreamingArtifactStore,
} from "@narratage/runtime";
import { TypeValidatorRegistry } from "@narratage/validation";
@@ -140,12 +139,10 @@ export async function createLocalRuntime(
}
await stageAttachments(request);
await options.buildStore.create(request.id, request.definition);
- await options.dispatchStore.create(createBuildDispatchIdentity({
+ await options.dispatchStore.create({
build: request.id,
- ...(request.implementationPackages === undefined ? {} : {
- implementationPackages: request.implementationPackages,
- }),
- }));
+ implementationPackages: [...new Set(request.implementationPackages ?? [])].sort(),
+ });
if (request.catalog !== undefined) {
await buildCatalog!.record(request.id, request.catalog);
}
diff --git a/packages/runtime-local/src/types.ts b/packages/runtime-local/src/types.ts
index 8d2b91df..6bb8b225 100644
--- a/packages/runtime-local/src/types.ts
+++ b/packages/runtime-local/src/types.ts
@@ -114,7 +114,7 @@ export type LocalCredentialStatus = import("@narratage/endpoint-kit").EndpointCr
export type LocalBuildSubmission = {
readonly id: string;
readonly state: BuildState;
- readonly status: "queued" | "running" | "waiting" | "blocked" | "complete" | "failed" | "cancelled";
+ readonly status: "queued" | "running" | "waiting" | "complete" | "failed" | "cancelled";
readonly dispatch: BuildDispatchSnapshot;
};
diff --git a/packages/runtime-local/src/worker-process.ts b/packages/runtime-local/src/worker-process.ts
index 1ea794f3..ac7778ec 100644
--- a/packages/runtime-local/src/worker-process.ts
+++ b/packages/runtime-local/src/worker-process.ts
@@ -1,5 +1,5 @@
import { spawn } from "node:child_process";
-import { mkdir, open, readFile, rename, stat, unlink, writeFile } from "node:fs/promises";
+import { mkdir, open, readFile, rename, rm, stat, writeFile } from "node:fs/promises";
import { dirname, join, resolve } from "node:path";
export type RuntimeWorkerLaunch = {
@@ -18,7 +18,6 @@ export type RuntimeProcessState = {
};
type ProcessRecord = {
- readonly format: "narratage.runtime-process@1";
readonly profile: string;
readonly pid: number;
readonly startedAt: number;
@@ -28,6 +27,10 @@ type ProcessRecord = {
const LOG_TAIL_BYTES = 1024 * 1024;
const LOG_ROTATE_BYTES = 10 * 1024 * 1024;
+function nodeError(error: unknown, code: string): boolean {
+ return error instanceof Error && "code" in error && error.code === code;
+}
+
function paths(dataRoot: string) {
const root = join(resolve(dataRoot), "worker");
return {
@@ -70,35 +73,46 @@ async function withLifecycleLock<T>(dataRoot: string, timeoutMs: number, run: ()
try {
lock = await open(location.lock, "wx");
} catch (error) {
- if (!(error instanceof Error && "code" in error && error.code === "EEXIST")) throw error;
- const owner = await readFile(location.lock, "utf8").then((text) => JSON.parse(text) as {
- readonly pid?: unknown;
- }).catch(() => undefined);
- if (owner === undefined || !Number.isSafeInteger(owner.pid) || !alive(owner.pid as number)) {
- await unlink(location.lock).catch(() => undefined);
+ if (!nodeError(error, "EEXIST")) throw error;
+ let owner: { readonly pid?: unknown } | undefined;
+ try {
+ owner = JSON.parse(await readFile(location.lock, "utf8")) as { readonly pid?: unknown };
+ } catch (readError) {
+ if (nodeError(readError, "ENOENT")) continue;
+ if (Date.now() >= deadline) throw new Error(`Runtime lifecycle lock is unreadable: ${location.lock}`);
+ await new Promise((resolveWait) => setTimeout(resolveWait, 25));
+ continue;
+ }
+ if (Number.isSafeInteger(owner.pid) && !alive(owner.pid as number)) {
+ await rm(location.lock, { force: true });
continue;
}
if (Date.now() >= deadline) {
- throw new Error(`Runtime lifecycle is busy in process ${String(owner.pid)}; lock: ${location.lock}`);
+ throw new Error(`Runtime lifecycle is busy${Number.isSafeInteger(owner.pid) ? ` in process ${String(owner.pid)}` : ""}; lock: ${location.lock}`);
}
await new Promise((resolveWait) => setTimeout(resolveWait, 25));
continue;
}
try {
- await lock.writeFile(JSON.stringify({ pid: process.pid, acquiredAt: Date.now() }), "utf8");
+ await lock.writeFile(JSON.stringify({ pid: process.pid }), "utf8");
return await run();
} finally {
await lock.close();
- await unlink(location.lock).catch(() => undefined);
+ await rm(location.lock, { force: true });
}
}
}
async function rotateLog(path: string): Promise<void> {
- const size = await stat(path).then((value) => value.size).catch(() => 0);
+ let size = 0;
+ try {
+ size = (await stat(path)).size;
+ } catch (error) {
+ if (!nodeError(error, "ENOENT")) throw error;
+ }
if (size <= LOG_ROTATE_BYTES) return;
const previous = `${path}.previous`;
- await unlink(previous).catch(() => undefined);
+ await rm(previous, { force: true });
await rename(path, previous);
}
@@ -106,14 +120,17 @@ async function record(profile: string, dataRoot: string): Promise<ProcessRecord
const path = paths(dataRoot).pid;
try {
const value = JSON.parse(await readFile(path, "utf8")) as ProcessRecord;
- if (value.format !== "narratage.runtime-process@1" || value.profile !== resolve(profile)
+ if (value.profile !== resolve(profile)
|| !Number.isSafeInteger(value.pid) || value.pid < 1
+ || !Number.isSafeInteger(value.startedAt) || value.startedAt < 0
|| !Array.isArray(value.implementationPackages)
- || !value.implementationPackages.every((item) => typeof item === "string" && item.length > 0)) return undefined;
+ || !value.implementationPackages.every((item) => typeof item === "string" && item.length > 0)) {
+ throw new Error(`Runtime Worker record is invalid: ${path}`);
+ }
return value;
} catch (error) {
- if (error instanceof Error && "code" in error && error.code === "ENOENT") return undefined;
- return undefined;
+ if (nodeError(error, "ENOENT")) return undefined;
+ throw error;
}
}
@@ -152,7 +169,12 @@ async function waitForReady(
while (Date.now() <= deadline) {
const state = await runtimeProcessStatus(profile, dataRoot, implementationPackages);
if (state.state === "stopped") {
- const log = await readFile(location.log, "utf8").catch(() => "");
+ let log = "";
+ try {
+ log = await readFile(location.log, "utf8");
+ } catch (error) {
+ if (!nodeError(error, "ENOENT")) throw error;
+ }
throw new Error(`Runtime Worker exited before becoming ready${log.length === 0 ? "" : `: ${log.trim().split("\n").at(-1)}`}`);
}
if (await exists(location.ready)) return state;
@@ -179,7 +201,7 @@ export async function ensureRuntimeProcess(
const absolute = resolve(profile);
const location = paths(dataRoot);
await mkdir(location.root, { recursive: true });
- await unlink(location.ready).catch(() => undefined);
+ await rm(location.ready, { force: true });
await rotateLog(location.log);
const log = await open(location.log, "a");
const child = spawn(launch.command, [
@@ -198,7 +220,6 @@ export async function ensureRuntimeProcess(
if (child.pid === undefined) throw new Error("Runtime Worker process has no pid");
const startedAt = Date.now();
await writeFile(location.pid, JSON.stringify({
- format: "narratage.runtime-process@1",
profile: absolute,
pid: child.pid,
startedAt,
@@ -214,8 +235,8 @@ async function stopRuntimeProcessUnlocked(profile: string, dataRoot: string, tim
const current = await record(profile, dataRoot);
const location = paths(dataRoot);
if (current === undefined || !alive(current.pid)) {
- await unlink(location.pid).catch(() => undefined);
- await unlink(location.ready).catch(() => undefined);
+ await rm(location.pid, { force: true });
+ await rm(location.ready, { force: true });
return { state: "stopped", profile: resolve(profile), logPath: location.log };
}
try {
@@ -233,8 +254,8 @@ async function stopRuntimeProcessUnlocked(profile: string, dataRoot: string, tim
await new Promise((resolveWait) => setTimeout(resolveWait, 50));
}
if (alive(current.pid)) throw new Error(`Runtime Worker ${current.pid} did not stop within ${timeoutMs}ms`);
- await unlink(location.pid).catch(() => undefined);
- await unlink(location.ready).catch(() => undefined);
+ await rm(location.pid, { force: true });
+ await rm(location.ready, { force: true });
return { state: "stopped", profile: resolve(profile), logPath: location.log };
}
@@ -263,5 +284,5 @@ export async function runtimeProcessLogs(dataRoot: string): Promise<{ readonly p
export async function markRuntimeProcessReady(path: string): Promise<void> {
await mkdir(dirname(resolve(path)), { recursive: true });
- await writeFile(resolve(path), `${process.pid}\n`, "utf8");
+ await writeFile(resolve(path), "", "utf8");
}
diff --git a/packages/runtime-local/src/worker.ts b/packages/runtime-local/src/worker.ts
index 46430bc9..d3339fb8 100644
--- a/packages/runtime-local/src/worker.ts
+++ b/packages/runtime-local/src/worker.ts
@@ -242,22 +242,20 @@ class DurableLocalWorker {
}
const pending = result.outcomes.filter((item) => item.status === "pending");
const deferred = result.outcomes.filter((item) => item.status === "deferred");
+ if (pending.length === 0 && deferred.length === 0 && result.blocked.length > 0) {
+ const reason = result.blocked.map((item) => `${item.reason}: ${item.subject}`).join(", ");
+ await this.#options.stores.dispatch.releaseBuildCapacity(dispatch.build);
+ return await this.#options.stores.dispatch.finish(dispatch.build, "failed", reason);
+ }
const now = Date.now();
const wakeTimes = [
...pending.map((item) => item.wakeAt ?? now + 1_000),
...deferred.map((item) => item.wakeAt ?? now + 1_000),
];
- const wakeAt = wakeTimes.length === 0
- ? result.blocked.length > 0 ? Number.MAX_SAFE_INTEGER : now
- : Math.min(...wakeTimes);
+ const wakeAt = wakeTimes.length === 0 ? now : Math.min(...wakeTimes);
const released = await this.#options.stores.dispatch.release(dispatch.build, {
- phase: pending.length > 0
- ? "waiting"
- : result.blocked.length > 0 ? "blocked" : "queued",
+ phase: pending.length > 0 ? "waiting" : "queued",
availableAt: wakeAt,
- ...(result.blocked.length === 0 ? {} : {
- reason: result.blocked.map((item) => `${item.reason}: ${item.subject}`).join(", "),
- }),
});
return released.cancellation === undefined ? released : await this.#cancel(released);
} catch (error) {
diff --git a/packages/runtime-local/test/local.test.ts b/packages/runtime-local/test/local.test.ts
index 0a191d18..d4d5dc5e 100644
--- a/packages/runtime-local/test/local.test.ts
+++ b/packages/runtime-local/test/local.test.ts
@@ -127,7 +127,6 @@ test("project local runtime queues, polls and cancels work with replaceable pack
const directory = await mkdtemp(join(tmpdir(), "narratage-local-"));
const initial = createGreetingBuild();
const catalog = {
- format: "narratage.build-catalog-descriptor@1" as const,
source: { path: join(directory, "main.svml") },
aliases: [{
name: "final.document",
@@ -424,8 +423,8 @@ test("project local runtime accepts an explicitly selected replacement ArtifactS
definition: buildDefinition(createGreetingBuild()),
attachments: [{ artifact: sourceArtifact, open: async () => (async function* () { yield bytes; })() }],
});
- const blocked = await runtime.workOnce();
- assert.equal(blocked?.phase, "blocked");
+ const failed = await runtime.workOnce();
+ assert.equal(failed?.terminal, "failed");
assert.deepEqual(await artifactStore.get(sourceArtifact.digest), bytes);
let reopened = false;
await runtime.build({
diff --git a/packages/runtime/src/catalog.ts b/packages/runtime/src/catalog.ts
index eeafbf2b..d1a2ab32 100644
--- a/packages/runtime/src/catalog.ts
+++ b/packages/runtime/src/catalog.ts
@@ -9,7 +9,6 @@ export type BuildCatalogAlias = {
};
export type BuildCatalogDescriptor = {
- readonly format: "narratage.build-catalog-descriptor@1";
readonly source: {
readonly path: string;
};
@@ -30,70 +29,3 @@ export type BuildCatalog = {
read(build: string): Promise<BuildCatalogEntry | undefined>;
list(): Promise<readonly BuildCatalogEntry[]>;
};
-
-function assert(condition: unknown, message: string): asserts condition {
- if (!condition) throw new Error(message);
-}
-
-export function verifyBuildCatalogDescriptor(descriptor: BuildCatalogDescriptor): void {
- assert(descriptor.format === "narratage.build-catalog-descriptor@1", "Build Catalog descriptor format is invalid");
- assert(descriptor.source.path.trim().length > 0, "Build Catalog source path is empty");
- if (descriptor.run !== undefined) {
- assert(descriptor.run.path.trim().length > 0, "Build Catalog Run path is empty");
- }
- const names = new Set<string>();
- for (const alias of descriptor.aliases) {
- assert(alias.name.trim().length > 0, "Build Catalog alias name is empty");
- assert(!names.has(alias.name), `Build Catalog repeats alias ${alias.name}`);
- names.add(alias.name);
- assert(alias.ref.kind === "record" || alias.ref.kind === "logical-output",
- `Build Catalog alias ${alias.name} has an unsupported reference`);
- assert(alias.ref.id.trim().length > 0, `Build Catalog alias ${alias.name} reference is empty`);
- }
-}
-
-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");
-}
-
-function copy<T>(value: T): T {
- return structuredClone(value);
-}
-
-export class MemoryBuildCatalog implements BuildCatalog {
- readonly #entries = new Map<string, BuildCatalogEntry>();
- readonly #now: () => number;
-
- constructor(now: () => number = Date.now) {
- this.#now = now;
- }
-
- async record(build: string, descriptor: BuildCatalogDescriptor): Promise<BuildCatalogEntry> {
- assert(build.trim().length > 0, "Build Catalog build id is empty");
- verifyBuildCatalogDescriptor(descriptor);
- 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,
- };
- verifyBuildCatalogEntry(entry);
- this.#entries.set(build, entry);
- return copy(entry);
- }
-
- async read(build: string): Promise<BuildCatalogEntry | undefined> {
- const entry = this.#entries.get(build);
- return entry === undefined ? undefined : copy(entry);
- }
-
- async list(): Promise<readonly BuildCatalogEntry[]> {
- return [...this.#entries.values()]
- .sort((left, right) => right.createdAt - left.createdAt || left.build.localeCompare(right.build))
- .map(copy);
- }
-}
diff --git a/packages/runtime/src/credentials.ts b/packages/runtime/src/credentials.ts
index 380b7de7..68b06a16 100644
--- a/packages/runtime/src/credentials.ts
+++ b/packages/runtime/src/credentials.ts
@@ -1,5 +1,4 @@
export type CredentialRef = {
- readonly format: "narratage.credential-ref@1";
/** CredentialStore implementation name, for example env, keychain or aws-default. */
readonly store: string;
/** Store-local opaque lookup key. It is configuration, never the secret value. */
@@ -27,13 +26,12 @@ function assert(condition: unknown, message: string): asserts condition {
}
export function credentialRef(store: string, key: string): CredentialRef {
- const value = { format: "narratage.credential-ref@1", store, key } as const;
+ const value = { store, key };
verifyCredentialRef(value);
return value;
}
export function verifyCredentialRef(ref: CredentialRef): void {
- assert(ref.format === "narratage.credential-ref@1", "unsupported CredentialRef format");
assert(ref.store.trim().length > 0, "CredentialRef store is empty");
assert(ref.key.trim().length > 0, "CredentialRef key is empty");
}
diff --git a/packages/runtime/src/dispatch.ts b/packages/runtime/src/dispatch.ts
index 1596b07b..109d1428 100644
--- a/packages/runtime/src/dispatch.ts
+++ b/packages/runtime/src/dispatch.ts
@@ -1,26 +1,25 @@
-export type DispatchPhase = "queued" | "running" | "waiting" | "blocked" | "terminal";
+export type DispatchPhase = "queued" | "running" | "waiting" | "terminal";
export type DispatchTerminal = "complete" | "failed" | "cancelled";
-export type BuildDispatchIdentity = {
+export type BuildDispatchRequest = {
readonly build: string;
/** Compute packages the Worker loads for this Build. */
readonly implementationPackages: readonly string[];
};
-export type BuildDispatchSnapshot = BuildDispatchIdentity & {
+export type BuildDispatchSnapshot = BuildDispatchRequest & {
readonly createdAt: number;
readonly availableAt: number;
readonly phase: DispatchPhase;
readonly reason?: string;
readonly cancellation?: {
- readonly requestedAt: number;
readonly reason?: string;
};
readonly terminal?: DispatchTerminal;
};
export type BuildDispatchRelease = {
- readonly phase: "queued" | "waiting" | "blocked";
+ readonly phase: "queued" | "waiting";
readonly availableAt: number;
readonly reason?: string;
};
@@ -29,13 +28,6 @@ export type DispatchQuery = {
readonly phases?: readonly DispatchPhase[];
};
-export const nonTerminalDispatchPhases = [
- "queued",
- "running",
- "waiting",
- "blocked",
-] as const satisfies readonly DispatchPhase[];
-
export type CapacityResourceClaim = {
readonly id: string;
readonly maxActive: number;
@@ -74,7 +66,7 @@ export type CapacityAcquire =
/** Durable queue state. Process ownership belongs to the Runtime Host, not each Build row. */
export type BuildDispatchStore = {
- create(identity: BuildDispatchIdentity, options?: {
+ create(request: BuildDispatchRequest, options?: {
readonly now?: number;
}): Promise<BuildDispatchSnapshot>;
read(build: string): Promise<BuildDispatchSnapshot | undefined>;
@@ -83,8 +75,7 @@ export type BuildDispatchStore = {
claim(now?: number, implementationPackages?: readonly string[]): 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>;
- wake(build: string, now?: number): Promise<BuildDispatchSnapshot>;
+ requestCancellation(build: string, reason?: string): Promise<BuildDispatchSnapshot>;
acquireCapacity(request: CapacityAcquireRequest): Promise<CapacityAcquire>;
releaseCapacity(id: string): Promise<void>;
releaseBuildCapacity(build: string): Promise<void>;
@@ -95,16 +86,6 @@ function assert(condition: unknown, message: string): asserts condition {
if (!condition) throw new Error(message);
}
-export function createBuildDispatchIdentity(input: {
- readonly build: string;
- readonly implementationPackages?: readonly string[];
-}): BuildDispatchIdentity {
- assert(input.build.trim().length > 0, "Build Dispatch build id is empty");
- const implementationPackages = [...new Set(input.implementationPackages ?? [])].sort();
- assert(implementationPackages.every((item) => item.trim().length > 0), "Build Dispatch implementation package is empty");
- return { build: input.build, implementationPackages };
-}
-
export function capacityReservationId(build: string, command: string): string {
assert(build.trim().length > 0 && command.trim().length > 0, "Capacity reservation identity is empty");
return `capacity:${encodeURIComponent(build)}:${encodeURIComponent(command)}`;
diff --git a/packages/runtime/src/index.ts b/packages/runtime/src/index.ts
index fa31e79f..59c4accc 100644
--- a/packages/runtime/src/index.ts
+++ b/packages/runtime/src/index.ts
@@ -1,10 +1,4 @@
export { plannedNeeds } from "@narratage/core";
-export { MemoryBuildStore } from "./memory.js";
-export {
- MemoryBuildCatalog,
- verifyBuildCatalogDescriptor,
- verifyBuildCatalogEntry,
-} from "./catalog.js";
export type * from "./catalog.js";
export {
CompositeCredentialStore,
@@ -16,13 +10,10 @@ export {
export type * from "./credentials.js";
export {
capacityReservationId,
- createBuildDispatchIdentity,
- nonTerminalDispatchPhases,
} from "./dispatch.js";
export type * from "./dispatch.js";
export {
MemoryOperationStore,
- sealOperationIdentity,
} from "./operations.js";
export type * from "./operations.js";
export { LocalBuildScheduler } from "./scheduler.js";
diff --git a/packages/runtime/src/memory.ts b/packages/runtime/src/memory.ts
deleted file mode 100644
index a83f5eda..00000000
--- a/packages/runtime/src/memory.ts
+++ /dev/null
@@ -1,59 +0,0 @@
-import { materializeBuild } from "@narratage/core";
-import type { BuildDefinition, BuildFact } from "@narratage/protocol";
-
-import type {
- BuildSnapshot,
- BuildStore,
-} from "./types.js";
-
-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),
- };
-}
-
-/** 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, StoredBuild>();
-
- 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 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 : snapshotBuild(snapshot);
- }
-
- async list(): Promise<readonly BuildSnapshot[]> {
- return [...this.#builds.values()]
- .sort((left, right) => left.build.localeCompare(right.build))
- .map(snapshotBuild);
- }
-
- 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`);
- const stored = {
- build,
- definition: current.definition,
- facts: [...current.facts, structuredClone(fact)],
- };
- this.#builds.set(build, stored);
- }
-}
diff --git a/packages/runtime/src/operations.ts b/packages/runtime/src/operations.ts
index ba5d3a53..6efe39f8 100644
--- a/packages/runtime/src/operations.ts
+++ b/packages/runtime/src/operations.ts
@@ -62,8 +62,6 @@ export type OperationQuery = {
readonly build?: string;
readonly command?: string;
readonly endpoint?: string;
- readonly pool?: string;
- readonly lane?: string;
};
export type OperationStore = {
@@ -78,18 +76,6 @@ function assert(condition: unknown, message: string): asserts condition {
if (!condition) throw new Error(message);
}
-export function sealOperationIdentity(
- 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");
- return { ...value };
-}
-
function copy(snapshot: OperationSnapshot): OperationSnapshot {
return structuredClone(snapshot);
}
@@ -114,8 +100,6 @@ export class MemoryOperationStore implements OperationStore {
.filter((snapshot) => query.build === undefined || snapshot.build === query.build)
.filter((snapshot) => query.command === undefined || snapshot.command === query.command)
.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.id.localeCompare(right.id))
.map(copy);
}
diff --git a/packages/runtime/src/scheduler.ts b/packages/runtime/src/scheduler.ts
index d4cf9afc..4d74ab25 100644
--- a/packages/runtime/src/scheduler.ts
+++ b/packages/runtime/src/scheduler.ts
@@ -1,4 +1,4 @@
-import { BuildMachine, buildDefinition, reduce } from "@narratage/core";
+import { BuildMachine, reduce } from "@narratage/core";
import type { BuildState } from "@narratage/protocol";
import type {
@@ -75,17 +75,12 @@ export class LocalBuildScheduler {
let state = structuredClone(request.state);
let machine: BuildMachine | undefined;
if (this.#buildStore !== undefined) {
- 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}`);
- }
+ const snapshot = request.snapshot;
if (snapshot === undefined) {
- try {
- snapshot = await this.#buildStore.create(request.id, buildDefinition(state));
- } catch (error) {
- snapshot = await this.#buildStore.read(request.id);
- if (snapshot === undefined) throw error;
- }
+ throw new Error(`scheduled Build ${request.id} has no durable snapshot`);
+ }
+ if (snapshot.build !== request.id) {
+ throw new Error(`scheduled Build snapshot ${snapshot.build} does not belong to ${request.id}`);
}
state = snapshot.state;
machine = new BuildMachine(snapshot.definition, snapshot.facts);
diff --git a/packages/runtime/test/catalog.test.ts b/packages/runtime/test/catalog.test.ts
deleted file mode 100644
index d1771e63..00000000
--- a/packages/runtime/test/catalog.test.ts
+++ /dev/null
@@ -1,24 +0,0 @@
-import assert from "node:assert/strict";
-import test from "node:test";
-import { MemoryBuildCatalog } from "@narratage/runtime";
-
-function descriptor() {
- return {
- format: "narratage.build-catalog-descriptor@1" as const,
- source: { path: "/project/main.svml" },
- run: { path: "/project/delivery.svrun" },
- aliases: [{
- name: "final.video",
- ref: { kind: "logical-output" as const, id: "logical:final" },
- }],
- };
-}
-
-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");
- assert.deepEqual((await catalog.list()).map((item) => item.build), ["delivery-01"]);
- assert.equal((await catalog.read("delivery-01"))?.source.path, "/project/main.svml");
-});
diff --git a/packages/runtime/test/operations.test.ts b/packages/runtime/test/operations.test.ts
index e0ee8fa9..ee790588 100644
--- a/packages/runtime/test/operations.test.ts
+++ b/packages/runtime/test/operations.test.ts
@@ -3,18 +3,17 @@ import test from "node:test";
import {
MemoryOperationStore,
- sealOperationIdentity,
} from "@narratage/runtime";
function operation() {
- return sealOperationIdentity({
+ return {
id: "operation:render",
build: "video-42",
command: "command:render",
endpoint: "hyperframes.local",
pool: "hyperframes.local",
lane: "render",
- });
+ };
}
test("OperationStore keeps the handle needed to poll external work", async () => {
diff --git a/packages/source/src/header.ts b/packages/source/src/header.ts
index 2db61b17..f5ca4b5e 100644
--- a/packages/source/src/header.ts
+++ b/packages/source/src/header.ts
@@ -1,5 +1,4 @@
export type SourceHeader = {
- readonly format: "narratage.source-header@1";
/** Logical Frontend request. A trusted Host resolves it to one exact implementation. */
readonly using: string;
readonly start: number;
@@ -27,7 +26,6 @@ export class SourceHeaderError extends Error {
}
}
-const MAX_HEADER_BYTES = 4_096;
const FRONTEND = /^[^\s<>&'"]+$/u;
function fail(sourceName: string, source: string, code: string, message: string, offset: number): never {
@@ -46,16 +44,12 @@ export function parseSourceHeader(sourceName: string, text: string): SourceHeade
if (!text.startsWith("<?svml", start)) {
fail(sourceName, text, "SOURCE_HEADER_MISSING", 'Source must begin with <?svml using="..."?>', start);
}
- const bounded = text.slice(start, start + MAX_HEADER_BYTES + 1);
- const relativeClose = bounded.indexOf("?>", "<?svml".length);
+ const relativeClose = text.indexOf("?>", start + "<?svml".length);
if (relativeClose < 0) {
- fail(sourceName, text, "SOURCE_HEADER_UNCLOSED", `Source Header must close within ${MAX_HEADER_BYTES} bytes`, start);
+ fail(sourceName, text, "SOURCE_HEADER_UNCLOSED", "Source Header is not closed", start);
}
- const close = start + relativeClose;
+ const close = relativeClose;
const raw = text.slice(start, close + 2);
- if (new TextEncoder().encode(raw).byteLength > MAX_HEADER_BYTES) {
- fail(sourceName, text, "SOURCE_HEADER_UNCLOSED", `Source Header must close within ${MAX_HEADER_BYTES} bytes`, start);
- }
const match = /^<\?svml[ \t]+using=(['"])([^'"\r\n]+)\1[ \t]*\?>$/u.exec(raw);
if (match === null) {
fail(
@@ -76,7 +70,7 @@ export function parseSourceHeader(sourceName: string, text: string): SourceHeade
if (nextContent !== null && after.startsWith("<?svml", nextContent.index)) {
fail(sourceName, text, "SOURCE_HEADER_DUPLICATE", "Source declares more than one Source Header", end + nextContent.index);
}
- return { format: "narratage.source-header@1", using, start, end };
+ return { using, start, end };
}
/** Preserve every original offset while making the Header ordinary whitespace to body Frontends. */
diff --git a/packages/source/src/index.ts b/packages/source/src/index.ts
index fbcfa9af..a6bfdf3b 100644
--- a/packages/source/src/index.ts
+++ b/packages/source/src/index.ts
@@ -6,7 +6,6 @@ export {
export type { SourceHeader } from "./header.js";
export type {
Awaitable,
- CompiledSourceIdentity,
ResolvedSourceAsset,
SourceAssetRequest,
SourceAssetResolver,
@@ -14,9 +13,5 @@ export type {
SourceResolver,
SourceUnit,
} from "./unit.js";
-export {
- compiledSourceIdentity,
- verifyCompiledSourceIdentity,
-} from "./unit.js";
/** Logical package address for Source Frontends selected by a Source Header. */
export const sourceFrontendPackageAbi = "narratage.source-frontend@1";
diff --git a/packages/source/src/unit.ts b/packages/source/src/unit.ts
index 93cffa19..ff7c0e1b 100644
--- a/packages/source/src/unit.ts
+++ b/packages/source/src/unit.ts
@@ -7,21 +7,6 @@ export type SourceUnit = {
readonly text: string;
};
-/** Shared identity fields of any successfully decoded self-described Source. */
-export type CompiledSourceIdentity = {
- readonly frontend: string;
-};
-
-export function compiledSourceIdentity(value: CompiledSourceIdentity): CompiledSourceIdentity {
- return {
- frontend: value.frontend,
- };
-}
-
-export function verifyCompiledSourceIdentity(value: CompiledSourceIdentity): void {
- if (value.frontend.trim().length === 0) throw new Error("Source Frontend is empty");
-}
-
export type SourceImportRequest = {
readonly from: string;
readonly alias: string;
diff --git a/packages/source/test/header.test.ts b/packages/source/test/header.test.ts
index 70751a62..d1b20c0b 100644
--- a/packages/source/test/header.test.ts
+++ b/packages/source/test/header.test.ts
@@ -36,8 +36,4 @@ test("Source Header accepts a UTF-8 BOM but no implicit or duplicate Frontend",
() => parseSourceHeader("padded.svml", '<?svml using=" a@1"?>\n<body/>'),
(error: unknown) => error instanceof SourceHeaderError && error.code === "SOURCE_HEADER_FRONTEND",
);
- assert.throws(
- () => parseSourceHeader("oversized.svml", `<?svml using="${"界".repeat(1_500)}"?>\n<body/>`),
- (error: unknown) => error instanceof SourceHeaderError && error.code === "SOURCE_HEADER_UNCLOSED",
- );
});
diff --git a/packages/store-sqlite/src/store.ts b/packages/store-sqlite/src/store.ts
index 28260680..e47d769d 100644
--- a/packages/store-sqlite/src/store.ts
+++ b/packages/store-sqlite/src/store.ts
@@ -11,12 +11,10 @@ import type {
BuildFact,
} from "@narratage/protocol";
import {
- verifyBuildCatalogDescriptor,
- verifyBuildCatalogEntry,
capacityReservationId,
} from "@narratage/runtime";
import type {
- BuildDispatchIdentity,
+ BuildDispatchRequest,
BuildDispatchRelease,
BuildDispatchSnapshot,
BuildDispatchStore,
@@ -36,7 +34,7 @@ import type {
OperationUpdate,
} from "@narratage/runtime";
-const databaseSchemaVersion = 11;
+const databaseSchemaVersion = 12;
export type SqliteRuntimeStateOptions = {
readonly busyTimeoutMs?: number;
@@ -196,13 +194,11 @@ function parseCatalogEntry(row: Row): BuildCatalogEntry {
assert(typeof row.created_at === "number", "SQLite Build Catalog row has no creation 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 = {
+ return {
...descriptor,
build: row.build_id,
createdAt: row.created_at,
};
- verifyBuildCatalogEntry(entry);
- return entry;
}
class SqliteBuildCatalog implements BuildCatalog {
@@ -214,16 +210,13 @@ 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 now = Date.now();
this.#database.prepare(`
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;
+ return { ...copy(descriptor), build, createdAt: now };
}
async read(build: string): Promise<BuildCatalogEntry | undefined> {
@@ -289,8 +282,6 @@ class SqliteOperationStore implements OperationStore {
["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(`${field} = ?`);
@@ -344,21 +335,22 @@ class SqliteOperationStore implements OperationStore {
function parseDispatchSnapshot(row: Row): BuildDispatchSnapshot {
- assert(typeof row.identity_json === "string", "SQLite Dispatch row has no identity");
+ assert(typeof row.build_id === "string", "SQLite Dispatch row has no Build id");
+ assert(typeof row.implementation_packages_json === "string", "SQLite Dispatch row has no implementation packages");
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 cancellation = typeof row.cancel_requested_at === "number"
+ const implementationPackages = JSON.parse(row.implementation_packages_json) as readonly string[];
+ const cancellation = row.cancel_requested === 1
? {
cancellation: {
- requestedAt: row.cancel_requested_at,
...(typeof row.cancel_reason === "string" ? { reason: row.cancel_reason } : {}),
},
}
: {};
const snapshot = {
- ...identity,
+ build: row.build_id,
+ implementationPackages,
createdAt: row.created_at,
availableAt: row.available_at,
phase: row.phase,
@@ -377,19 +369,19 @@ class SqliteBuildDispatchStore implements BuildDispatchStore {
}
async create(
- identity: BuildDispatchIdentity,
+ request: BuildDispatchRequest,
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);
+ build_id, implementation_packages_json, created_at, available_at,
+ phase, reason, cancel_requested, cancel_reason, terminal
+ ) VALUES (?, ?, ?, ?, 'queued', NULL, 0, NULL, NULL)
+ `).run(request.build, canonicalStringify(request.implementationPackages), now, now);
return {
- ...copy(identity),
+ ...copy(request),
createdAt: now,
availableAt: now,
phase: "queued",
@@ -418,21 +410,21 @@ class SqliteBuildDispatchStore implements BuildDispatchStore {
const available = new Set(implementationPackages);
return transaction(this.#database, () => {
const rows = this.#database.prepare(`
- SELECT build_id, identity_json FROM narratage_dispatches
- WHERE phase IN ('queued', 'waiting', 'blocked') AND available_at <= ?
+ SELECT build_id, implementation_packages_json FROM narratage_dispatches
+ WHERE phase IN ('queued', 'waiting') AND available_at <= ?
ORDER BY available_at ASC, created_at ASC, build_id ASC
`).all(now) as Row[];
const row = rows.find((candidate) => {
- assert(typeof candidate.identity_json === "string", "SQLite ready Dispatch has no identity");
- const identity = JSON.parse(candidate.identity_json) as BuildDispatchIdentity;
- return identity.implementationPackages.every((item) => available.has(item));
+ assert(typeof candidate.implementation_packages_json === "string", "SQLite ready Dispatch has no implementation packages");
+ const packages = JSON.parse(candidate.implementation_packages_json) as readonly string[];
+ return packages.every((item) => available.has(item));
});
if (row === undefined) return undefined;
assert(typeof row.build_id === "string", "SQLite ready Dispatch has no Build id");
const updated = this.#database.prepare(`
UPDATE narratage_dispatches
SET phase = 'running'
- WHERE build_id = ? AND phase IN ('queued', 'waiting', 'blocked') AND available_at <= ?
+ WHERE build_id = ? AND phase IN ('queued', 'waiting') AND available_at <= ?
`).run(row.build_id, now);
if (updated.changes !== 1) return undefined;
return parseDispatchSnapshot(
@@ -483,8 +475,8 @@ class SqliteBuildDispatchStore implements BuildDispatchStore {
});
}
- async requestCancellation(build: string, reason?: string, now = Date.now()): Promise<BuildDispatchSnapshot> {
- nonNegativeInteger(now, "Dispatch cancellation time");
+ async requestCancellation(build: string, reason?: string): Promise<BuildDispatchSnapshot> {
+ const now = Date.now();
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`);
@@ -494,18 +486,18 @@ class SqliteBuildDispatchStore implements BuildDispatchStore {
this.#database.prepare(`
UPDATE narratage_dispatches
SET phase = 'terminal', terminal = 'cancelled',
- reason = ?, cancel_requested_at = ?, cancel_reason = ?
+ reason = ?, cancel_requested = 1, cancel_reason = ?
WHERE build_id = ?
- `).run(reason ?? "cancelled before execution", now, reason ?? null, build);
+ `).run(reason ?? "cancelled before execution", reason ?? null, build);
} else {
this.#database.prepare(`
UPDATE narratage_dispatches
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, ?),
+ reason = ?, cancel_requested = 1,
cancel_reason = COALESCE(cancel_reason, ?)
WHERE build_id = ?
- `).run(now, reason ?? "cancellation requested", now, reason ?? null, build);
+ `).run(now, reason ?? "cancellation requested", reason ?? null, build);
}
return parseDispatchSnapshot(
this.#database.prepare("SELECT * FROM narratage_dispatches WHERE build_id = ?").get(build) as Row,
@@ -513,18 +505,6 @@ class SqliteBuildDispatchStore implements BuildDispatchStore {
});
}
- async wake(build: string, now = Date.now()): Promise<BuildDispatchSnapshot> {
- nonNegativeInteger(now, "Dispatch wake time");
- this.#database.prepare(`
- UPDATE narratage_dispatches
- SET available_at = MIN(available_at, ?)
- WHERE build_id = ? AND phase != 'terminal'
- `).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> {
nonNegativeInteger(request.now, "Capacity acquisition time");
assert(request.resources.length > 0, "Capacity resources are empty");
@@ -631,7 +611,7 @@ export class SqliteRuntimeState {
this.#database.exec(`PRAGMA busy_timeout = ${positiveInteger(options.busyTimeoutMs ?? 5_000, "busyTimeoutMs")}`);
if (!options.readOnly || emptyReadOnly) {
this.#database.exec("PRAGMA journal_mode = WAL");
- this.#database.exec("PRAGMA synchronous = FULL");
+ this.#database.exec("PRAGMA synchronous = NORMAL");
}
const alreadyInitialized = this.#database.prepare(`
SELECT 1 AS present FROM sqlite_master
@@ -686,12 +666,12 @@ export class SqliteRuntimeState {
) STRICT;
CREATE TABLE IF NOT EXISTS narratage_dispatches (
build_id TEXT PRIMARY KEY,
- identity_json TEXT NOT NULL,
+ implementation_packages_json TEXT NOT NULL,
created_at INTEGER NOT NULL,
available_at INTEGER NOT NULL,
- phase TEXT NOT NULL CHECK (phase IN ('queued', 'running', 'waiting', 'blocked', 'terminal')),
+ phase TEXT NOT NULL CHECK (phase IN ('queued', 'running', 'waiting', 'terminal')),
reason TEXT,
- cancel_requested_at INTEGER,
+ cancel_requested INTEGER NOT NULL DEFAULT 0 CHECK (cancel_requested IN (0, 1)),
cancel_reason TEXT,
terminal TEXT CHECK (terminal IS NULL OR terminal IN ('complete', 'failed', 'cancelled'))
) STRICT;
diff --git a/packages/store-sqlite/test/store.test.ts b/packages/store-sqlite/test/store.test.ts
index 241ecc55..18bf4d55 100644
--- a/packages/store-sqlite/test/store.test.ts
+++ b/packages/store-sqlite/test/store.test.ts
@@ -4,11 +4,6 @@ import { join } from "node:path";
import { tmpdir } from "node:os";
import test from "node:test";
import { BuildMachine, buildDefinition } from "@narratage/core";
-import {
- createBuildDispatchIdentity,
- nonTerminalDispatchPhases,
- sealOperationIdentity,
-} from "@narratage/runtime";
import { SqliteRuntimeState } from "@narratage/store-sqlite";
import { createGreetingBuild } from "../../core/test/greeting-fixture.js";
@@ -36,7 +31,6 @@ test("SQLite stores verified Build facts and Operation handles across reopen", a
const definition = buildDefinition(initial);
await first.builds.create("video", definition);
const catalog = {
- format: "narratage.build-catalog-descriptor@1",
source: { path: "/project/main.svml" },
aliases: [{
name: "final.video",
@@ -45,14 +39,14 @@ test("SQLite stores verified Build facts and Operation handles across reopen", a
} as const;
await first.catalog.record("video", catalog);
assert.equal((await first.catalog.read("video"))?.aliases[0]?.name, "final.video");
- const operation = sealOperationIdentity({
+ const operation = {
id: "operation:generation",
build: "video",
command: "command:generation",
endpoint: "kie.personal",
pool: "kie.personal",
lane: "fixture.generation",
- });
+ };
const pending = await first.operations.create({ ...operation,
status: "pending",
handle: { remoteJob: "job-1" },
@@ -89,14 +83,14 @@ 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({
+ const identity = {
id: "operation:render",
build: "video",
command: "command:render",
endpoint: "hyperframes.lambda",
pool: "hyperframes.lambda",
lane: "fixture.render",
- });
+ };
const completed = await state.operations.create({ ...identity,
status: "completed",
completion: {
@@ -118,17 +112,16 @@ test("cancelling a never-claimed Build atomically withdraws it from dispatch", a
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" }), { now: 100 });
+ await state.dispatch.create({ build: "queued-build", implementationPackages: [] }, { now: 100 });
- const cancelled = await state.dispatch.requestCancellation("queued-build", "no longer needed", 101);
+ const cancelled = await state.dispatch.requestCancellation("queued-build", "no longer needed");
assert.equal(cancelled.phase, "terminal");
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(102), undefined);
- assert.deepEqual(await state.dispatch.list({ phases: nonTerminalDispatchPhases }), []);
+ assert.deepEqual(await state.dispatch.list({ phases: ["queued", "running", "waiting"] }), []);
- const repeated = await state.dispatch.requestCancellation("queued-build", "second reason", 103);
+ const repeated = await state.dispatch.requestCancellation("queued-build", "second reason");
assert.deepEqual(repeated, cancelled);
state.close();
} finally {
@@ -140,13 +133,14 @@ test("a Worker skips ready Builds whose implementation packages it has not loade
const directory = await mkdtemp(join(tmpdir(), "narratage-sqlite-package-admission-"));
try {
const state = new SqliteRuntimeState(join(directory, "runtime.sqlite"));
- await state.dispatch.create(createBuildDispatchIdentity({
+ await state.dispatch.create({
build: "a-needs-image",
implementationPackages: ["@example/image"],
- }), { now: 100 });
- await state.dispatch.create(createBuildDispatchIdentity({
+ }, { now: 100 });
+ await state.dispatch.create({
build: "b-needs-nothing",
- }), { now: 100 });
+ implementationPackages: [],
+ }, { now: 100 });
assert.equal((await state.dispatch.claim(101, []))?.build, "b-needs-nothing");
assert.equal(await state.dispatch.claim(102, []), undefined);
diff --git a/packages/workspace-fs-node/src/workspace.ts b/packages/workspace-fs-node/src/workspace.ts
index 527046b9..14d3f143 100644
--- a/packages/workspace-fs-node/src/workspace.ts
+++ b/packages/workspace-fs-node/src/workspace.ts
@@ -26,9 +26,7 @@ class NodeFilesystemWorkspaceSession implements WorkspaceSession {
readonly entry: SourceUnit;
readonly #assetRoots: readonly string[];
readonly #sourceCache = new Map<string, SourceUnit>();
- readonly #sourceEdges = new Map<string, SourceUnit>();
readonly #assetIdentity = new Map<string, { readonly digest: BlobRef["digest"]; readonly size: number }>();
- readonly #assetEdges = new Map<string, string>();
readonly #attachments = new Map<string, ArtifactAttachment>();
private constructor(root: string, entry: SourceUnit, assetRoots: readonly string[]) {
@@ -95,12 +93,7 @@ class NodeFilesystemWorkspaceSession implements WorkspaceSession {
if (!isWithin(this.root, importer.id)) {
throw new WorkspaceError("UNKNOWN_SOURCE_IMPORTER", `${importer.id} is outside this Workspace`, importer.id);
}
- const edge = `${importer.id}\u0000${request.from}`;
- const locked = this.#sourceEdges.get(edge);
- if (locked !== undefined) return locked;
- const loaded = await this.#loadSource(resolve(dirname(importer.id), request.from));
- this.#sourceEdges.set(edge, loaded);
- return loaded;
+ return await this.#loadSource(resolve(dirname(importer.id), request.from));
};
readonly resolveAsset = async (
@@ -117,18 +110,13 @@ class NodeFilesystemWorkspaceSession implements WorkspaceSession {
if (!isWithin(this.root, importer.id)) {
throw new WorkspaceError("UNKNOWN_SOURCE_IMPORTER", `${importer.id} is outside this Workspace`, importer.id);
}
- const edge = `${importer.id}\u0000${request.from}`;
- let canonical = this.#assetEdges.get(edge);
- if (canonical === undefined) {
- canonical = await realpath(resolve(dirname(importer.id), request.from));
- if (!this.#assetRoots.some((root) => isWithin(root, canonical!))) {
- throw new WorkspaceError(
- "SOURCE_ASSET_OUTSIDE_ROOT",
- `Source asset ${canonical} is outside the workspace and every allowed asset root`,
- canonical,
- );
- }
- this.#assetEdges.set(edge, canonical);
+ const canonical = await realpath(resolve(dirname(importer.id), request.from));
+ if (!this.#assetRoots.some((root) => isWithin(root, canonical))) {
+ throw new WorkspaceError(
+ "SOURCE_ASSET_OUTSIDE_ROOT",
+ `Source asset ${canonical} is outside the workspace and every allowed asset root`,
+ canonical,
+ );
}
let identity = this.#assetIdentity.get(canonical);
if (identity === undefined) {