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
feat: bind source assets into compilation
kashorin987274b16135
diff --git a/packages/cli/src/main.ts b/packages/cli/src/main.tsindex e800a40d..aed63e2d 100644--- a/packages/cli/src/main.ts+++ b/packages/cli/src/main.ts@@ -225,6 +225,7 @@ export async function runCli(argv: readonly string[], io: CliIo): Promise<void>moduleClosure: result.program.closure.digest,graph: result.elaboration.graph.id,units: result.closure.units.length,+ sourceAssets: result.sourceArtifacts.map((item) => item.artifact),modules: result.program.closure.modules.map((item) => `${item.ref.name}@${item.ref.version}`),exports: result.exports.map((item) => ({ name: item.name, type: item.type, kind: item.ref.kind })),}, null, 2)}\n`);@@ -242,6 +243,7 @@ export async function runCli(argv: readonly string[], io: CliIo): Promise<void>const built = await runtime.build({id: args.buildId ?? result.state.id,state: result.state,+ sourceArtifacts: result.compilation.sourceArtifacts,}, {follow: args.follow,...(args.maxWaitMs === undefined ? {} : { maxWaitMs: args.maxWaitMs }),diff --git a/packages/compiler-node/README.md b/packages/compiler-node/README.mdindex 301f3f33..721b940c 100644--- a/packages/compiler-node/README.md+++ b/packages/compiler-node/README.md@@ -23,6 +23,17 @@ canonicalizes real paths, confines reads to one declared root, rejects symlink eeach import edge to the first bytes read for the whole compilation. Source paths are diagnostics andcache keys; Source Closure semantic identity still depends on content rather than machine location.+The same Host owns a separate Source Asset capability. A Frontend/Surface may request a relative+asset and assign its exact media type, but it never receives a path or filesystem handle. The Host+reads each canonical asset once, rejects root and symlink escapes, returns a `BlobRef`, binds that+reference into the requesting SourceUnit and exposes defensive byte attachments on the Node+compilation result. `check` and `plan` perform no ArtifactStore write. `build` passes the attachments+to the selected Runtime for digest-checked staging.++SourceUnit recursion and Source Asset resolution are intentionally different capabilities: an+asset cannot import syntax, and a source import does not silently make arbitrary neighboring bytes+available to package code.+`NodeCompiler` runs a discovery pass first because Frontends must be known before source decoding,while the exact Module Closure must be known before typed Records can be verified. It then invokesthe ordinary Elaborator Source Closure implementation. `planFile()` resolves author-facing exportdiff --git a/packages/compiler-node/src/compiler.ts b/packages/compiler-node/src/compiler.tsindex da5ac21f..ad264193 100644--- a/packages/compiler-node/src/compiler.ts+++ b/packages/compiler-node/src/compiler.ts@@ -31,6 +31,7 @@ import type { TypeValidatorRegistryLike } from "@svml/validation";import { NodeCompilerError } from "./error.js";import type { ModulePackageRegistryLike } from "./modules.js";import { NodeSourceHost } from "./source.js";+import type { NodeSourceArtifact } from "./source.js";type DiscoveredUnit = {readonly source: AuthorSourceUnit;@@ -88,12 +89,17 @@ export type PlanFileOptions = {};export type PlannedSource = {- readonly compilation: CompiledSourceClosure;+ readonly compilation: NodeCompiledSourceClosure;readonly request: BuildRequest;readonly plan: BuildPlan;readonly state: BuildState;};+export type NodeCompiledSourceClosure = CompiledSourceClosure & {+ /** Host-side transfer bundle; bytes are not serialized into Core BuildState. */+ readonly sourceArtifacts: readonly NodeSourceArtifact[];+};+/** Domain-neutral Node facade from a real source file to a verified Source Closure or BuildPlan. */export class NodeCompiler {readonly #options: NodeCompilerOptions;@@ -111,7 +117,7 @@ export class NodeCompiler {?? createRecordAdmitter(options.validators ?? new TypeValidatorRegistry());}- async compileFile(file: string): Promise<CompiledSourceClosure> {+ async compileFile(file: string): Promise<NodeCompiledSourceClosure> {const entryPath = resolve(file);const sources = await NodeSourceHost.create(this.#options.root ?? dirname(entryPath));const entry = await sources.load(entryPath);@@ -124,14 +130,16 @@ export class NodeCompiler {const closure = this.#options.modules.createClosure(discovered.flatMap((unit) => unit.discovery.modules),);- return await compileSourceClosure({+ const compilation = await compileSourceClosure({entry,frontend: this.#options.entryFrontend,closure,frontends: this.#options.frontends,resolveSource: sources.resolveSource,+ resolveAsset: sources.resolveAsset,admitRecord: this.#admitRecord,});+ return { ...compilation, sourceArtifacts: sources.sourceArtifacts() };}async planFile(file: string, options: PlanFileOptions): Promise<PlannedSource> {diff --git a/packages/compiler-node/src/index.ts b/packages/compiler-node/src/index.tsindex d0fb945c..542abd26 100644--- a/packages/compiler-node/src/index.ts+++ b/packages/compiler-node/src/index.ts@@ -7,9 +7,11 @@ export type {RegisteredModulePackage,} from "./modules.js";export { NodeSourceHost } from "./source.js";+export type { NodeSourceArtifact } from "./source.js";export { NodeCompiler } from "./compiler.js";export type {NodeCompilerOptions,+ NodeCompiledSourceClosure,PlannedSource,PlanFileOptions,} from "./compiler.js";diff --git a/packages/compiler-node/src/source.ts b/packages/compiler-node/src/source.tsindex b198cf24..3bda85cf 100644--- a/packages/compiler-node/src/source.ts+++ b/packages/compiler-node/src/source.ts@@ -1,11 +1,15 @@+import { createHash } from "node:crypto";import { readFile, realpath } from "node:fs/promises";import { dirname, isAbsolute, relative, resolve } from "node:path";import type {AuthorSourceImport,+ AuthorSourceAssetResolver,+ AuthorSourceAssetRequest,AuthorSourceResolver,AuthorSourceUnit,} from "@svml/elaborator";+import type { BlobRef } from "@svml/protocol";import { NodeCompilerError } from "./error.js";@@ -19,6 +23,9 @@ export class NodeSourceHost {readonly root: string;readonly #cache = new Map<string, AuthorSourceUnit>();readonly #edges = new Map<string, AuthorSourceUnit>();+ readonly #assetBytes = new Map<string, Uint8Array>();+ readonly #assetEdges = new Map<string, string>();+ readonly #attachments = new Map<string, NodeSourceArtifact>();private constructor(root: string) {this.root = root;@@ -69,4 +76,64 @@ export class NodeSourceHost {this.#edges.set(edge, loaded);return loaded;};++ readonly resolveAsset: AuthorSourceAssetResolver = async (+ importer: AuthorSourceUnit,+ request: AuthorSourceAssetRequest,+ ) => {+ if (!request.from.startsWith("./") && !request.from.startsWith("../")) {+ throw new NodeCompilerError(+ "UNSUPPORTED_SOURCE_ASSET",+ `Source asset ${request.from} must be relative`,+ request.from,+ );+ }+ if (!isWithin(this.root, importer.id)) {+ throw new NodeCompilerError("UNKNOWN_SOURCE_IMPORTER", `${importer.id} is outside this Source Host`, 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 (!isWithin(this.root, canonical)) {+ throw new NodeCompilerError(+ "SOURCE_ASSET_OUTSIDE_ROOT",+ `Source asset ${canonical} is outside compiler root ${this.root}`,+ canonical,+ );+ }+ this.#assetEdges.set(edge, canonical);+ }+ let bytes = this.#assetBytes.get(canonical);+ if (bytes === undefined) {+ bytes = Uint8Array.from(await readFile(canonical));+ this.#assetBytes.set(canonical, bytes);+ }+ const artifact: BlobRef = {+ kind: "blob",+ digest: `sha256:${createHash("sha256").update(bytes).digest("hex")}`,+ size: bytes.byteLength,+ mediaType: request.mediaType,+ };+ const attachmentKey = `${artifact.digest}\u0000${artifact.mediaType}`;+ if (!this.#attachments.has(attachmentKey)) {+ this.#attachments.set(attachmentKey, { artifact: { ...artifact }, bytes: Uint8Array.from(bytes) });+ }+ return { artifact: { ...artifact } };+ };++ /** Exact bytes requested during this compilation, detached from Host caches. */+ sourceArtifacts(): readonly NodeSourceArtifact[] {+ return [...this.#attachments.values()]+ .sort((left, right) => {+ const byDigest = left.artifact.digest.localeCompare(right.artifact.digest);+ return byDigest === 0 ? left.artifact.mediaType.localeCompare(right.artifact.mediaType) : byDigest;+ })+ .map((item) => ({ artifact: { ...item.artifact }, bytes: Uint8Array.from(item.bytes) }));+ }}++export type NodeSourceArtifact = {+ readonly artifact: BlobRef;+ readonly bytes: Uint8Array;+};diff --git a/packages/compiler-node/test/compiler.test.ts b/packages/compiler-node/test/compiler.test.tsindex 05d20d01..61c77fec 100644--- a/packages/compiler-node/test/compiler.test.ts+++ b/packages/compiler-node/test/compiler.test.ts@@ -23,6 +23,7 @@ import {import {AuthorFrontendRegistry,sealGraphFragment,+ verifySourceClosure,} from "@svml/elaborator";import type {ModuleManifest,@@ -147,6 +148,25 @@ const fragment = sealGraphFragment({}],});+const assetLaboratory = { name: "example.asset-lab", version: "1" } as const;+const assetType = { module: assetLaboratory, name: "Asset" } satisfies TypeRef;+const assetSurfaceDigest = digestOf("example.asset-lab/surface@1");+const assetManifest: ModuleManifest = {+ ...emptyManifest(assetLaboratory.name),+ types: [{ name: assetType.name, schema: { kind: "blob" } }],+ surfaces: [{+ name: "asset",+ tag: "Asset",+ mode: "structured",+ outputs: [assetType],+ implementation: {+ kind: "trusted-frontend-surface",+ locator: "example.asset-lab/surface",+ digest: assetSurfaceDigest,+ },+ }],+};+function compiler(root: string): NodeCompiler {const modules = new ModulePackageRegistry();modules.register({ manifest: laboratoryManifest });@@ -178,6 +198,33 @@ function compiler(root: string): NodeCompiler {return new NodeCompiler({ modules, frontends, entryFrontend: textAuthorFrontendId, root });}+function assetCompiler(root: string): NodeCompiler {+ const modules = new ModulePackageRegistry();+ modules.register({ manifest: assetManifest });+ const surfaces = new TextSurfaceRegistry();+ surfaces.registerStructured(assetLaboratory, "asset", assetSurfaceDigest, async ({ element, resolveAsset }) => {+ const id = element.attributes.id;+ const src = element.attributes.src;+ if (typeof id !== "string" || typeof src !== "string") throw new Error("Asset id and src are required");+ const resolved = await resolveAsset({ from: src, mediaType: "application/octet-stream", range: element.range });+ return {+ records: [{ id, type: assetType, value: resolved.artifact, range: element.range }],+ components: [],+ fragments: [],+ };+ });+ const frontends = new AuthorFrontendRegistry();+ frontends.register(createTextAuthorFrontend({+ registry: surfaces,+ resolveModule(request): ModuleRef {+ const resolved = modules.resolve(request.from);+ if (resolved === undefined) throw new Error(`unknown module ${request.from}`);+ return resolved;+ },+ }));+ return new NodeCompiler({ modules, frontends, entryFrontend: textAuthorFrontendId, root });+}+test("Node Compiler discovers real imports and plans a named public export", async () => {const root = await mkdtemp(join(tmpdir(), "svml-compiler-node-"));const file = join(root, "main.svml");@@ -196,20 +243,67 @@ test("Node Compiler discovers real imports and plans a named public export", asyassert.equal(planned.plan.goals.length, 1);});+test("source assets are content addressed, closure-bound and returned as a Host transfer bundle", async () => {+ const root = await mkdtemp(join(tmpdir(), "svml-source-assets-"));+ const file = join(root, "main.svml");+ const asset = join(root, "reference.bin");+ await writeFile(file, `<svml>+ <import as="asset" from="example.asset-lab@1"/>+ <asset:Asset id="reference" src="./reference.bin"/>+ </svml>`, "utf8");+ await writeFile(asset, new Uint8Array([1, 2, 3, 4]));++ const first = await assetCompiler(root).compileFile(file);+ const unit = first.closure.units.find((item) => item.assets.length > 0);+ const attachment = first.sourceArtifacts[0];+ assert.equal(unit?.assets[0]?.from, "./reference.bin");+ assert.equal(unit?.assets[0]?.artifact.digest, attachment?.artifact.digest);+ assert.deepEqual(attachment?.bytes, new Uint8Array([1, 2, 3, 4]));+ assert.equal(first.module.records[0]?.value.kind, "blob");+ assert.equal(first.module.records[0]?.value.kind === "blob" ? first.module.records[0].value.digest : undefined, attachment?.artifact.digest);+ const tamperedUnit = first.closure.units.map((item) => item === unit+ ? {+ ...item,+ assets: item.assets.map((sourceAsset) => ({+ ...sourceAsset,+ artifact: { ...sourceAsset.artifact, size: sourceAsset.artifact.size + 1 },+ })),+ }+ : item);+ assert.throws(+ () => verifySourceClosure({ ...first.closure, units: tamperedUnit }),+ /digest differs/u,+ );++ await writeFile(asset, new Uint8Array([9, 8, 7]));+ const second = await assetCompiler(root).compileFile(file);+ assert.notEqual(second.closure.id, first.closure.id);+ assert.notEqual(second.sourceArtifacts[0]?.artifact.digest, attachment?.artifact.digest);+});+test("Node Source Host contains symlinks and reads each canonical source only once", async () => {const parent = await mkdtemp(join(tmpdir(), "svml-source-host-"));const root = join(parent, "project");await mkdir(root);const entryPath = join(root, "main.svml");const outsidePath = join(parent, "outside.svs");+ const assetPath = join(root, "asset.bin");await writeFile(entryPath, "first", "utf8");await writeFile(outsidePath, "outside", "utf8");+ await writeFile(assetPath, new Uint8Array([1, 2, 3]));await symlink(outsidePath, join(root, "escaped.svs"));const host = await NodeSourceHost.create(root);const entry = await host.load(entryPath);await writeFile(entryPath, "second", "utf8");assert.equal((await host.load(entryPath)).text, "first");assert.equal(await readFile(entryPath, "utf8"), "second");+ const firstAsset = await host.resolveAsset(entry, { from: "./asset.bin", mediaType: "application/octet-stream" });+ await writeFile(assetPath, new Uint8Array([4, 5, 6, 7]));+ const lockedAsset = await host.resolveAsset(entry, { from: "./asset.bin", mediaType: "application/octet-stream" });+ assert.deepEqual(lockedAsset, firstAsset, "one Host locks an asset edge to the first bytes read");+ const detached = host.sourceArtifacts();+ detached[0]?.bytes.fill(0);+ assert.deepEqual(host.sourceArtifacts()[0]?.bytes, new Uint8Array([1, 2, 3]));await assert.rejects(async () => await host.resolveSource(entry, {from: "./escaped.svs",@@ -218,4 +312,11 @@ test("Node Source Host contains symlinks and reads each canonical source only on}),(error: unknown) => error instanceof NodeCompilerError && error.code === "SOURCE_OUTSIDE_ROOT",);+ await assert.rejects(+ async () => await host.resolveAsset(entry, {+ from: "./escaped.svs",+ mediaType: "application/octet-stream",+ }),+ (error: unknown) => error instanceof NodeCompilerError && error.code === "SOURCE_ASSET_OUTSIDE_ROOT",+ );});diff --git a/packages/elaborator/README.md b/packages/elaborator/README.mdindex 44952b30..406d3ba2 100644--- a/packages/elaborator/README.md+++ b/packages/elaborator/README.md@@ -26,7 +26,9 @@ Source resolver. Source compilation discovers dependencies before decoding, rejeduplicate aliases, decodes dependencies first, hygienically qualifies private Record/componentidentities and binds only declared public exports into importer namespaces. Host filesystem pathsare excluded from Source Closure identity; source contents, Frontend digests and written import-edges remain covered.+edges remain covered. Every source asset actually requested during decode is also covered by its+author-written locator and exact content-addressed `BlobRef`; raw bytes remain a Host transfer+concern and never enter the parser-independent AuthorModule or Core BuildState.The orchestration ABI is asynchronous even when a local Text/SVS implementation is synchronous.This lets a browser, sandbox or remote repository provide SourceUnits without changing the@@ -34,6 +36,12 @@ compilation contract. The Host resolver is the authority for path canonicalizaticontainment, symlink policy and I/O; Elaborator receives bytes and identities but never opens filesor grants filesystem access itself.+Asset resolution follows the same inversion of authority. The decode context exposes only an+asynchronous `resolveAsset({ from, mediaType })` capability. Elaborator validates the returned+identity, rejects conflicting media assignments for one written locator and folds the dependency+into Source Closure identity. It does not know whether a Host obtained the bytes from a filesystem,+browser upload, repository object or remote content store.+Frontend output passes a Host-owned Record admission hook before linking. The hook is permitted toattach validation evidence but is forbidden to rewrite Record identity, Type, value, digest,conformance or origin. `@svml/compiler-node` wires this to `@svml/validation` by default, so authoreddiff --git a/packages/elaborator/src/index.ts b/packages/elaborator/src/index.tsindex ca01c13c..f22248cb 100644--- a/packages/elaborator/src/index.ts+++ b/packages/elaborator/src/index.ts@@ -25,6 +25,8 @@ export type {AuthorFrontendRegistryLike,AuthorSourceDecodeContext,AuthorSourceDiscovery,+ AuthorSourceAssetRequest,+ AuthorSourceAssetResolver,AuthorSourceExport,AuthorSourceImport,AuthorSourceResolver,@@ -35,8 +37,10 @@ export type {CompiledSourceClosure,CompiledSourceExport,DecodedAuthorSource,+ ResolvedAuthorSourceAsset,ResolvedAuthorSourceImport,SourceClosure,+ SourceClosureAsset,SourceClosureUnit,} from "./source.js";export {diff --git a/packages/elaborator/src/source.ts b/packages/elaborator/src/source.tsindex 18ba9dad..792f48ad 100644--- a/packages/elaborator/src/source.ts+++ b/packages/elaborator/src/source.ts@@ -8,6 +8,7 @@ import {verifyRecord,} from "@svml/core";import type {+ BlobRef,Digest,GraphValueRef,LinkedProgram,@@ -49,6 +50,19 @@ export type AuthorSourceDiscovery = {readonly sources: readonly AuthorSourceImport[];};+/** A semantic source dependency requested by a Frontend or package-owned Surface. */+export type AuthorSourceAssetRequest = {+ /** Author-written path or Host-specific asset locator. */+ readonly from: string;+ /** Exact media type the consuming Surface assigns to these bytes. */+ readonly mediaType: string;+ readonly range?: SourceRange;+};++export type ResolvedAuthorSourceAsset = {+ readonly artifact: BlobRef;+};+export type AuthorSourceExport = {/** Relative public name. The importing source contributes its own alias. */readonly name: string;@@ -67,6 +81,8 @@ export type ResolvedAuthorSourceImport = {export type AuthorSourceDecodeContext = {readonly closure: ResolvedModuleClosure;readonly imports: readonly ResolvedAuthorSourceImport[];+ /** Host authority: Frontends declare asset dependencies but never open files themselves. */+ readonly resolveAsset: (request: AuthorSourceAssetRequest) => Awaitable<ResolvedAuthorSourceAsset>;};export type DecodedAuthorSource = {@@ -113,6 +129,16 @@ export type AuthorSourceResolver = (request: AuthorSourceImport,) => Awaitable<AuthorSourceUnit>;+export type AuthorSourceAssetResolver = (+ importer: AuthorSourceUnit,+ request: AuthorSourceAssetRequest,+) => Awaitable<ResolvedAuthorSourceAsset>;++export type SourceClosureAsset = {+ readonly from: string;+ readonly artifact: BlobRef;+};+/** Host-owned admission hook; it may attach validation evidence but cannot rewrite author meaning. */export type AuthorRecordAdmitter = (closure: ResolvedModuleClosure,@@ -127,6 +153,7 @@ export type SourceClosureUnit = {readonly sourceDigest: Digest;readonly semanticDigest: Digest;readonly modules: readonly string[];+ readonly assets: readonly SourceClosureAsset[];readonly imports: readonly {readonly alias: string;readonly from: string;@@ -164,6 +191,7 @@ export type CompileSourceClosureRequest = {readonly closure: ResolvedModuleClosure;readonly frontends: AuthorFrontendRegistryLike;readonly resolveSource: AuthorSourceResolver;+ readonly resolveAsset?: AuthorSourceAssetResolver;readonly admitRecord?: AuthorRecordAdmitter;};@@ -187,6 +215,9 @@ function sourceUnitContent(unit: SourceClosureUnit): Omit<SourceClosureUnit, "idsourceDigest: unit.sourceDigest,semanticDigest: unit.semanticDigest,modules: [...unit.modules].sort(),+ assets: [...unit.assets]+ .map((item) => ({ from: item.from, artifact: item.artifact }))+ .sort((left, right) => left.from.localeCompare(right.from)),imports: [...unit.imports].map((item) => ({alias: item.alias,@@ -247,6 +278,7 @@ function hygienizeSource(discovery: AuthorSourceDiscovery,decoded: DecodedAuthorSource,imports: readonly ResolvedAuthorSourceImport[],+ assets: readonly SourceClosureAsset[],): HygienicSource {assert(decoded.module.closureDigest.length > 0, "INVALID_SOURCE_MODULE", `${source.name} returned no closure identity`);const fragmentIds = new Set<string>();@@ -297,6 +329,7 @@ function hygienizeSource(components: decoded.author.components,fragments: [...decoded.fragments].map((fragment) => fragment.id).sort(),exports: [...decoded.exports].sort((left, right) => left.name.localeCompare(right.name)),+ assets: [...assets].sort((left, right) => left.from.localeCompare(right.from)),});const recordIds = new Map(decoded.module.records.map((record) => [record.id,@@ -343,6 +376,7 @@ function hygienizeSource(sourceDigest: digestOf(source.text),semanticDigest,modules: [...discovery.modules].sort(),+ assets: [...assets].sort((left, right) => left.from.localeCompare(right.from)),imports: discovery.sources.map((request) => {const resolved = imports.find((item) => item.request === request);@@ -365,6 +399,7 @@ function hygienizeSource(sourceDigest: unitContent.sourceDigest,semanticDigest,modules: unitContent.modules,+ assets: unitContent.assets,imports: unitContent.imports,},records,@@ -428,9 +463,44 @@ export async function compileSourceClosure(records: compiled.records.filter((record) => publicRecordIds.has(record.id)),});}+ const assets = new Map<string, SourceClosureAsset>();const rawDecoded = await frontend.decode(source, {closure: request.closure,imports: canonicalize(imports) as unknown as readonly ResolvedAuthorSourceImport[],+ async resolveAsset(assetRequest) {+ assert(assetRequest.from.trim().length > 0, "EMPTY_SOURCE_ASSET", `${source.name} requested an empty asset`);+ assert(assetRequest.mediaType.trim().length > 0, "EMPTY_SOURCE_ASSET_MEDIA_TYPE", `${source.name} requested an asset without a media type`);+ const existing = assets.get(assetRequest.from);+ if (existing !== undefined) {+ assert(+ existing.artifact.mediaType === assetRequest.mediaType,+ "SOURCE_ASSET_MEDIA_TYPE_CONFLICT",+ `${source.name} assigns conflicting media types to ${assetRequest.from}`,+ assetRequest.from,+ );+ return { artifact: existing.artifact };+ }+ assert(+ request.resolveAsset !== undefined,+ "SOURCE_ASSET_RESOLVER_MISSING",+ `${source.name} requires source asset ${assetRequest.from}, but the Host has no asset resolver`,+ assetRequest.from,+ );+ const resolved = await request.resolveAsset(source, assetRequest);+ const artifact = resolved.artifact;+ assert(artifact.kind === "blob", "INVALID_SOURCE_ASSET", `${assetRequest.from} did not resolve to a BlobRef`);+ assert(isDigest(artifact.digest), "INVALID_SOURCE_ASSET_DIGEST", `${assetRequest.from} has an invalid digest`);+ assert(Number.isSafeInteger(artifact.size) && artifact.size >= 0, "INVALID_SOURCE_ASSET_SIZE", `${assetRequest.from} has an invalid size`);+ assert(+ artifact.mediaType === assetRequest.mediaType,+ "SOURCE_ASSET_MEDIA_TYPE_MISMATCH",+ `${assetRequest.from} resolved as ${artifact.mediaType}, expected ${assetRequest.mediaType}`,+ assetRequest.from,+ );+ const item = { from: assetRequest.from, artifact } as const;+ assets.set(assetRequest.from, item);+ return { artifact };+ },});assert(rawDecoded.module.closureDigest === request.closure.digest,@@ -474,7 +544,7 @@ export async function compileSourceClosure(}),};link(request.closure, [decoded.module]);- const result = hygienizeSource(source, frontend, discovery, decoded, imports);+ const result = hygienizeSource(source, frontend, discovery, decoded, imports, [...assets.values()]);visiting.pop();cache.set(key, result);ordered.push(result);@@ -549,6 +619,20 @@ export function verifySourceClosure(closure: SourceClosure): void {assert(isDigest(unit.frontendDigest), "INVALID_FRONTEND_DIGEST", `${unit.id} Frontend digest is invalid`);assert(isDigest(unit.sourceDigest), "INVALID_SOURCE_DIGEST", `${unit.id} source digest is invalid`);assert(isDigest(unit.semanticDigest), "INVALID_SOURCE_SEMANTIC_DIGEST", `${unit.id} semantic digest is invalid`);+ const assetPaths = new Set<string>();+ for (const asset of unit.assets) {+ assert(asset.from.length > 0, "EMPTY_SOURCE_ASSET", `${unit.id} contains an empty asset path`);+ assert(!assetPaths.has(asset.from), "DUPLICATE_SOURCE_ASSET", `${unit.id} repeats asset ${asset.from}`);+ assetPaths.add(asset.from);+ assert(asset.artifact.kind === "blob", "INVALID_SOURCE_ASSET", `${unit.id}:${asset.from} is not a BlobRef`);+ assert(isDigest(asset.artifact.digest), "INVALID_SOURCE_ASSET_DIGEST", `${unit.id}:${asset.from} digest is invalid`);+ assert(+ Number.isSafeInteger(asset.artifact.size) && asset.artifact.size >= 0,+ "INVALID_SOURCE_ASSET_SIZE",+ `${unit.id}:${asset.from} size is invalid`,+ );+ assert(asset.artifact.mediaType.length > 0, "EMPTY_SOURCE_ASSET_MEDIA_TYPE", `${unit.id}:${asset.from} media type is empty`);+ }units.set(unit.id, unit);}assert(units.has(closure.entry), "UNKNOWN_SOURCE_ENTRY", `Source Closure entry ${closure.entry} is absent`);diff --git a/packages/local/README.md b/packages/local/README.mdindex 99a65d51..dc06f3b5 100644--- a/packages/local/README.md+++ b/packages/local/README.md@@ -44,3 +44,10 @@ Endpoint 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 sameports instead of using the project defaults.++`LocalBuildRequest.sourceArtifacts` is the explicit ingress from a Compiler Host into the selected+ArtifactStore. Each attachment carries claimed `BlobRef` metadata plus bytes; Local Runtime copies+the bytes, stores them content-addressably and requires the Store's returned digest, size and media+type to match before any Core command can consume the reference. Attachments never enter+BuildState, SQLite or author source. Reusing a Build after restart needs no reattachment when the+chosen durable ArtifactStore still contains those digests.diff --git a/packages/local/src/runtime.ts b/packages/local/src/runtime.tsindex 90e0d98b..dfcb3699 100644--- a/packages/local/src/runtime.ts+++ b/packages/local/src/runtime.ts@@ -190,17 +190,30 @@ export async function createLocalRuntime(...(options.scheduling ?? {}),buildStore: options.buildStore,});+ const stageSourceArtifacts = async (request: LocalBuildRequest): Promise<void> => {+ for (const item of request.sourceArtifacts ?? []) {+ const stored = await options.artifactStore.put(Uint8Array.from(item.bytes), item.artifact.mediaType);+ assert(+ stored.digest === item.artifact.digest+ && stored.size === item.artifact.size+ && stored.mediaType === item.artifact.mediaType,+ `Source Artifact ${item.artifact.digest} does not match its staged bytes`,+ );+ }+ };+ const scheduled = (request: LocalBuildRequest) => ({ id: request.id, state: request.state });const runBuild = async (request: LocalBuildRequest,follow: LocalBuildOptions = {},) => {+ await stageSourceArtifacts(request);const startedAt = Date.now();const pollIntervalMs = nonNegativeInteger(follow.pollIntervalMs ?? 1_000, "pollIntervalMs");const maxWaitMs = follow.maxWaitMs === undefined? undefined: nonNegativeInteger(follow.maxWaitMs, "maxWaitMs");while (true) {- const [result] = await scheduler.run([request]);+ const [result] = await scheduler.run([scheduled(request)]);if (result === undefined) throw new Error(`Local Scheduler returned no result for ${request.id}`);if (follow.follow !== true || result.status !== "paused") return result;const pending = result.journal.filter((item) => item.status === "pending");@@ -214,7 +227,10 @@ export async function createLocalRuntime(};return {build: runBuild,- buildMany: (requests) => scheduler.run(requests),+ async buildMany(requests) {+ await Promise.all(requests.map(stageSourceArtifacts));+ return await scheduler.run(requests.map(scheduled));+ },async status(build) {return {build: await options.buildStore.read(build),diff --git a/packages/local/src/types.ts b/packages/local/src/types.tsindex 0d2aec4b..510d9c7c 100644--- a/packages/local/src/types.ts+++ b/packages/local/src/types.ts@@ -1,6 +1,7 @@import type { Awaitable, ComponentPackage } from "@svml/component-kit";import type { NodeProviderPackage } from "@svml/provider-kit";import type {+ BlobRef,BuildState,} from "@svml/protocol";import type {@@ -79,6 +80,13 @@ export type LocalBuildRequest = {/** Stable user/run identity. Reusing it resumes only the same Core Build identity. */readonly id: string;readonly state: BuildState;+ /** Host transfer bundle. It is staged before Core commands run and never enters BuildState. */+ readonly sourceArtifacts?: readonly LocalSourceArtifact[];+};++export type LocalSourceArtifact = {+ readonly artifact: BlobRef;+ readonly bytes: Uint8Array;};export type LocalBuildOptions = {diff --git a/packages/local/test/local.test.ts b/packages/local/test/local.test.tsindex b54150a2..119287a0 100644--- a/packages/local/test/local.test.ts+++ b/packages/local/test/local.test.ts@@ -1,4 +1,5 @@import assert from "node:assert/strict";+import { createHash } from "node:crypto";import {mkdir,mkdtemp,@@ -308,6 +309,31 @@ test("project local runtime accepts a permission-checked replacement ArtifactStoartifacts,allowedPermissions: ["network:remote-artifacts"],});+ const bytes = new Uint8Array([7, 8, 9]);+ const sourceArtifact = {+ kind: "blob" as const,+ digest: `sha256:${createHash("sha256").update(bytes).digest("hex")}` as const,+ size: bytes.byteLength,+ mediaType: "application/octet-stream",+ };+ assert.equal(await artifacts.store.has(sourceArtifact.digest), false);+ await assert.rejects(+ runtime.build({+ id: "source-artifact-staging",+ state: createGreetingBuild(),+ sourceArtifacts: [{ artifact: sourceArtifact, bytes }],+ }),+ /does not bind demanded capability/u,+ );+ assert.deepEqual(await artifacts.store.get(sourceArtifact.digest), bytes);+ await assert.rejects(+ runtime.build({+ id: "tampered-source-artifact",+ state: createGreetingBuild(),+ sourceArtifacts: [{ artifact: sourceArtifact, bytes: new Uint8Array([0]) }],+ }),+ /does not match its staged bytes/u,+ );await runtime.close();} finally {await rm(directory, { recursive: true, force: true });diff --git a/packages/text/README.md b/packages/text/README.mdindex 68aab481..db3d0557 100644--- a/packages/text/README.md+++ b/packages/text/README.md@@ -31,6 +31,12 @@ A Surface may contribute three kinds of inert data:- parser-independent Author Component drafts;- content-addressed Graph Fragments used by those components.+Raw and Structured Surface handlers are asynchronous and receive one narrow `resolveAsset()`+capability. This is the only way a Surface can turn an author-written asset locator into a+content-addressed `BlobRef`; Text never exposes filesystem APIs or a resolved local path. The+Compiler Host, not the Surface, owns containment, read-once behavior and byte transfer. A handler+that does not request assets remains unchanged apart from being awaitable.+Text validates source ranges, duplicate identities, Manifest-declared Record types and completeFragment references. It strips diagnostic ranges before sealing one `svml.author-module@1`, sosource reflow does not change author semantics. It never expands a Fragment or resolves a componentdiff --git a/packages/text/src/frontend.ts b/packages/text/src/frontend.tsindex 939eee57..c1fb9338 100644--- a/packages/text/src/frontend.ts+++ b/packages/text/src/frontend.ts@@ -48,7 +48,7 @@ import type {} from "./types.js";export const textFrontendRef = { module: "@svml/text", version: "0.0.0-dev", name: "text" } as const;-export const textFrontendImplementationDigest = digestOf("@svml/text/frontend@0");+export const textFrontendImplementationDigest = digestOf("@svml/text/frontend@1");export const textAuthorFrontendId = "@svml/text@1";type BoundSurface = {@@ -105,7 +105,7 @@ function surfaceScope(return scope;}-export function decodeText(source: SourceUnit, context: TextDecodeContext): TextDecodeResult {+export async function decodeText(source: SourceUnit, context: TextDecodeContext): Promise<TextDecodeResult> {verifyClosure(context.closure);const discovery = discoverText(source);const sourceImports = context.sourceImports ?? [];@@ -203,13 +203,19 @@ export function decodeText(source: SourceUnit, context: TextDecodeContext): Textlet output: SurfaceDecodeOutput;if (bound.declaration.mode === "raw") {if (opening.selfClosing) fail(source, "TEXT_RAW_SELF_CLOSING", `Raw Surface <${opening.name}> cannot be self-closing.`, cursor);- const rawOutput = (registered.handler as RawSurfaceHandler)({+ const rawOutput = await (registered.handler as RawSurfaceHandler)({sourceName: source.name,source: source.text,tag: opening.name,openingStart: opening.start,contentStart: opening.end,attributes: opening.attributes,+ resolveAsset(request) {+ if (context.resolveAsset === undefined) {+ fail(source, "TEXT_ASSET_RESOLVER_MISSING", `Surface <${opening.name}> requested ${request.from}, but this Text Host has no asset resolver.`, request.range?.start ?? opening.start);+ }+ return context.resolveAsset(request);+ },});if (!Number.isInteger(rawOutput.nextOffset) || rawOutput.nextOffset <= opening.end || rawOutput.nextOffset > source.text.length) {fail(source, "TEXT_SURFACE_CURSOR", `Raw Surface <${opening.name}> returned an invalid cursor.`, cursor);@@ -218,7 +224,7 @@ export function decodeText(source: SourceUnit, context: TextDecodeContext): Textoutput = rawOutput;} else {const parsed = parseStructuredElement(source, cursor);- output = (registered.handler as StructuredSurfaceHandler)({+ output = await (registered.handler as StructuredSurfaceHandler)({sourceName: source.name,element: parsed.element,resolveReference(path) {@@ -251,6 +257,12 @@ export function decodeText(source: SourceUnit, context: TextDecodeContext): Text}return undefined;},+ resolveAsset(request) {+ if (context.resolveAsset === undefined) {+ fail(source, "TEXT_ASSET_RESOLVER_MISSING", `Surface <${opening.name}> requested ${request.from}, but this Text Host has no asset resolver.`, request.range?.start ?? opening.start);+ }+ return context.resolveAsset(request);+ },});cursor = parsed.nextOffset;}@@ -435,14 +447,15 @@ export function createTextAuthorFrontend(options: TextAuthorFrontendOptions): Te})),};},- decode(source, context) {- const result = decodeText(+ async decode(source, context) {+ const result = await decodeText({ name: source.name, text: source.text },{closure: context.closure,registry: options.registry,resolveModule: options.resolveModule,sourceImports: context.imports,+ resolveAsset: context.resolveAsset,},);return {diff --git a/packages/text/src/types.ts b/packages/text/src/types.tsindex e9979611..8d9aeca4 100644--- a/packages/text/src/types.ts+++ b/packages/text/src/types.ts@@ -14,8 +14,10 @@ import type {AuthorComponent,AuthorFrontend,AuthorModule,+ AuthorSourceAssetRequest,AuthorSourceExport,AuthorValueRef,+ Awaitable,GraphFragment,ResolvedAuthorSourceImport,} from "@svml/elaborator";@@ -86,6 +88,7 @@ export type RawSurfaceInput = {readonly openingStart: number;readonly contentStart: number;readonly attributes: Readonly<Record<string, TextAttributeValue>>;+ readonly resolveAsset: (request: AuthorSourceAssetRequest) => Awaitable<import("@svml/elaborator").ResolvedAuthorSourceAsset>;};export type RawSurfaceOutput = SurfaceDecodeOutput & {@@ -101,6 +104,7 @@ export type StructuredSurfaceInput = {* A component output has a ref and Type but no compile-time Record value.*/readonly resolveReference: (path: string) => SurfaceResolvedReference | undefined;+ readonly resolveAsset: (request: AuthorSourceAssetRequest) => Awaitable<import("@svml/elaborator").ResolvedAuthorSourceAsset>;};export type SurfaceResolvedReference = {@@ -110,8 +114,8 @@ export type SurfaceResolvedReference = {readonly record?: TypedRecord;};-export type RawSurfaceHandler = (input: RawSurfaceInput) => RawSurfaceOutput;-export type StructuredSurfaceHandler = (input: StructuredSurfaceInput) => SurfaceDecodeOutput;+export type RawSurfaceHandler = (input: RawSurfaceInput) => Awaitable<RawSurfaceOutput>;+export type StructuredSurfaceHandler = (input: StructuredSurfaceInput) => Awaitable<SurfaceDecodeOutput>;export type RegisteredSurface = {readonly module: ModuleRef;@@ -126,6 +130,7 @@ export type TextDecodeContext = {readonly registry: TextSurfaceRegistryLike;readonly resolveModule: (request: TextImportRequest) => ModuleRef;readonly sourceImports?: readonly ResolvedAuthorSourceImport[];+ readonly resolveAsset?: (request: AuthorSourceAssetRequest) => Awaitable<import("@svml/elaborator").ResolvedAuthorSourceAsset>;};export type TextDecodeResult = {diff --git a/packages/text/test/author-integration.test.ts b/packages/text/test/author-integration.test.tsindex 35038367..4de3f9a8 100644--- a/packages/text/test/author-integration.test.ts+++ b/packages/text/test/author-integration.test.ts@@ -238,8 +238,8 @@ function decode(text: string, surfaceRegistry = registry()) {);}-test("Text Surfaces compile forward author references into a Core BuildPlan", () => {- const decoded = decode(`<svml>+test("Text Surfaces compile forward author references into a Core BuildPlan", async () => {+ const decoded = await decode(`<svml><import as="lab" from="example.text-laboratory@1"/><lab:Sample id="soil" value="soil"/><lab:Report id="final" measurement={measurement.result}/>@@ -270,9 +270,9 @@ test("Text Surfaces compile forward author references into a Core BuildPlan", ()assert.equal(reportStep?.inputs.measurement, measurementStep?.outputs.measurement);});-test("component source reflow does not change AuthorModule semantic identity", () => {- const compact = decode(`<svml><import as="lab" from="example.text-laboratory@1"/><lab:Sample id="soil" value="soil"/><lab:Measure id="measurement" sample={soil}/></svml>`);- const multiline = decode(`<svml>+test("component source reflow does not change AuthorModule semantic identity", async () => {+ const compact = await decode(`<svml><import as="lab" from="example.text-laboratory@1"/><lab:Sample id="soil" value="soil"/><lab:Measure id="measurement" sample={soil}/></svml>`);+ const multiline = await decode(`<svml><import as="lab" from="example.text-laboratory@1"/><lab:Sampleid="soil"@@ -286,9 +286,9 @@ test("component source reflow does not change AuthorModule semantic identity", (assert.equal(compact.author.id, multiline.author.id);});-test("Text rejects a component whose Surface omits its Fragment definition", () => {- assert.throws(- () => decode(`<svml>+test("Text rejects a component whose Surface omits its Fragment definition", async () => {+ await assert.rejects(+ decode(`<svml><import as="lab" from="example.text-laboratory@1"/><lab:Sample id="soil" value="soil"/><lab:Measure id="measurement" sample={soil}/>@@ -298,8 +298,8 @@ test("Text rejects a component whose Surface omits its Fragment definition", ());});-test("unknown component references remain inert until whole-document Author linking", () => {- const decoded = decode(`<svml>+test("unknown component references remain inert until whole-document Author linking", async () => {+ const decoded = await decode(`<svml><import as="lab" from="example.text-laboratory@1"/><lab:Report id="final" measurement={missing.result}/></svml>`);@@ -312,9 +312,9 @@ test("unknown component references remain inert until whole-document Author link);});-test("Text rejects duplicate component identities before Author linking", () => {- assert.throws(- () => decode(`<svml>+test("Text rejects duplicate component identities before Author linking", async () => {+ await assert.rejects(+ decode(`<svml><import as="lab" from="example.text-laboratory@1"/><lab:Sample id="soil" value="soil"/><lab:Measure id="measurement" sample={soil}/>diff --git a/packages/text/test/text.test.ts b/packages/text/test/text.test.tsindex 54d4173a..288633c0 100644--- a/packages/text/test/text.test.ts+++ b/packages/text/test/text.test.ts@@ -33,8 +33,8 @@ function scriptContext() {} as const;}-test("Text learns <script> only from an imported Script Manifest", () => {- const result = decodeText(+test("Text learns <script> only from an imported Script Manifest", async () => {+ const result = await decodeText({name: "talk.svml",text: `<svml>@@ -58,15 +58,15 @@ test("Text learns <script> only from an imported Script Manifest", () => {assert.doesNotThrow(() => link(scriptContext().closure, [result.module]));});-test("the same Script meaning has the same authored Record digest across reflow", () => {- const compact = decodeText(+test("the same Script meaning has the same authored Record digest across reflow", async () => {+ const compact = await decodeText({name: "compact.svml",text: `<svml><import from="@svml/script"/><script id="story"><opening><ALICE>Hello.<BOB>Hi.</opening></script></svml>`,},scriptContext(),);- const multiline = decodeText(+ const multiline = await decodeText({name: "multiline.svml",text: `<svml>@@ -89,9 +89,9 @@ test("the same Script meaning has the same authored Record digest across reflow");});-test("without the import, Text has no hard-coded knowledge of Script", () => {- assert.throws(- () => decodeText(+test("without the import, Text has no hard-coded knowledge of Script", async () => {+ await assert.rejects(+ decodeText({ name: "unknown.svml", text: "<svml><script><opening>Hello.</opening></script></svml>" },scriptContext(),),@@ -99,7 +99,7 @@ test("without the import, Text has no hard-coded knowledge of Script", () => {);});-test("a raw Surface cannot consume the Text document close", () => {+test("a raw Surface cannot consume the Text document close", async () => {const closure = createResolvedClosure([narrativeManifest, scriptManifest]);const registry = new TextSurfaceRegistry();registry.registerRaw(@@ -113,8 +113,8 @@ test("a raw Surface cannot consume the Text document close", () => {fragments: [],}),);- assert.throws(- () => decodeText(+ await assert.rejects(+ decodeText({name: "swallowed.svml",text: `<svml><import from="@svml/script"/><script><opening>Hello.</opening></script></svml>`,@@ -125,9 +125,9 @@ test("a raw Surface cannot consume the Text document close", () => {);});-test("imports are frozen before body decoding", () => {- assert.throws(- () => decodeText(+test("imports are frozen before body decoding", async () => {+ await assert.rejects(+ decodeText({name: "late.svml",text: `<svml>@@ -142,9 +142,9 @@ test("imports are frozen before body decoding", () => {);});-test("a body tag whose name merely starts with import is not treated as an import", () => {- assert.throws(- () => decodeText(+test("a body tag whose name merely starts with import is not treated as an import", async () => {+ await assert.rejects(+ decodeText({ name: "important.svml", text: "<svml><important/></svml>" },scriptContext(),),@@ -164,7 +164,7 @@ test("Text also exposes a generic structured Surface tree", () => {assert.equal(parsed.element.children[0]?.kind, "element");});-test("a module can use Text's generic structured parser without adding another parser", () => {+test("a module can use Text's generic structured parser without adding another parser", async () => {const module = { name: "example.card", version: "1" } as const;const type = { module, name: "Card" } as const;const implementationDigest = digestOf("example.card/surface@1");@@ -212,7 +212,7 @@ test("a module can use Text's generic structured parser without adding another pcomponents: [],fragments: [],}));- const result = decodeText(+ const result = await decodeText({name: "card.svml",text: `<svml><import from="example.card@1"/><card title="Hello"><line>World</line></card></svml>`,@@ -236,8 +236,8 @@ test("a module can use Text's generic structured parser without adding another pcomponents: [],fragments: [],}));- assert.throws(- () => decodeText(+ await assert.rejects(+ decodeText({ name: "overreach.svml", text: `<svml><import from="example.card@1"/><card/></svml>` },{ closure, registry: overreachingRegistry, resolveModule: () => module },),