@telorun/kernel 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (29) hide show
  1. package/dist/controller-loader.d.ts +57 -0
  2. package/dist/controller-loader.d.ts.map +1 -1
  3. package/dist/controller-loader.js +33 -3
  4. package/dist/controller-loader.js.map +1 -1
  5. package/dist/controller-loaders/napi-loader.d.ts +33 -1
  6. package/dist/controller-loaders/napi-loader.d.ts.map +1 -1
  7. package/dist/controller-loaders/napi-loader.js +101 -8
  8. package/dist/controller-loaders/napi-loader.js.map +1 -1
  9. package/dist/controller-loaders/npm-loader.d.ts +12 -1
  10. package/dist/controller-loaders/npm-loader.d.ts.map +1 -1
  11. package/dist/controller-loaders/npm-loader.js +9 -4
  12. package/dist/controller-loaders/npm-loader.js.map +1 -1
  13. package/dist/controllers/resource-definition/resource-definition-controller.d.ts +1 -3
  14. package/dist/controllers/resource-definition/resource-definition-controller.d.ts.map +1 -1
  15. package/dist/controllers/resource-definition/resource-definition-controller.js +13 -14
  16. package/dist/controllers/resource-definition/resource-definition-controller.js.map +1 -1
  17. package/dist/evaluation-context.d.ts +17 -2
  18. package/dist/evaluation-context.d.ts.map +1 -1
  19. package/dist/evaluation-context.js +105 -28
  20. package/dist/evaluation-context.js.map +1 -1
  21. package/dist/kernel.js +22 -0
  22. package/dist/kernel.js.map +1 -1
  23. package/package.json +3 -3
  24. package/src/controller-loader.ts +82 -6
  25. package/src/controller-loaders/napi-loader.ts +143 -10
  26. package/src/controller-loaders/npm-loader.ts +27 -6
  27. package/src/controllers/resource-definition/resource-definition-controller.ts +21 -23
  28. package/src/evaluation-context.ts +107 -30
  29. package/src/kernel.ts +21 -0
@@ -39,7 +39,66 @@ export class ControllerEnvMissingError extends Error {
39
39
  * crate path (not on the caller's baseUri) ensures two distinct paths that
40
40
  * point at the same crate share one cache entry.
41
41
  */
42
- const _napiModuleCache = new Map<string, ControllerInstance>();
42
+ /**
43
+ * Holds the *raw* `require()`'d module object — i.e. the flat export bag
44
+ * returned by the napi addon — keyed by canonical crate path. The PURL
45
+ * fragment (`#entry`) projects out a sub-export at call time; caching the
46
+ * raw module here means two PURLs that differ only by fragment share one
47
+ * cargo build / one mmap of the dylib, instead of paying for either twice.
48
+ */
49
+ const _napiModuleCache = new Map<string, any>();
50
+
51
+ interface BuildAndLoadResult {
52
+ rawModule: any;
53
+ nodePath: string;
54
+ }
55
+
56
+ /**
57
+ * Single-flight dedupe for concurrent loads of the same crate. Without this,
58
+ * two callers (e.g. parallel test kernels) both miss the module cache, both
59
+ * run cargo + `fs.copyFile` over the same `.node` file, and the second
60
+ * `copyFile` overwrites a dylib Node has already mmapped — leading to a
61
+ * segfault when napi finalize callbacks run against torn pages. Keeping the
62
+ * in-flight promise here lets late arrivals await the in-progress load and
63
+ * read from the populated module cache when it resolves.
64
+ */
65
+ const _napiInFlight = new Map<string, Promise<BuildAndLoadResult>>();
66
+
67
+ /**
68
+ * @internal Slow-path entry counter — the regression test for concurrent
69
+ * loads asserts this stays at 1 across N parallel callers, proving the
70
+ * single-flight gate held. Production code never reads it.
71
+ */
72
+ let _napiBuildAttempts = 0;
73
+ export function __getNapiBuildAttempts(): number {
74
+ return _napiBuildAttempts;
75
+ }
76
+ export function __resetNapiLoaderForTest(): void {
77
+ _napiBuildAttempts = 0;
78
+ _napiModuleCache.clear();
79
+ _napiInFlight.clear();
80
+ }
81
+
82
+ /**
83
+ * Which branch of the resolver served this load.
84
+ *
85
+ * - `cache` — process-lifetime in-memory hit on `_napiModuleCache`.
86
+ * - `local` — `local_path` qualifier; cargo was invoked but the work
87
+ * is conceptually equivalent to "found source on disk and
88
+ * used it" (parallel to the npm `local_path` branch). The
89
+ * CLI silences these by default; the first cold build on
90
+ * a fresh checkout is silent for the same reason.
91
+ * - `cargo-build`— reserved for distribution-mode resolution (fetch from a
92
+ * registry + compile). Not produced today; the dispatcher
93
+ * errors with `ControllerEnvMissingError` for non-local
94
+ * PURLs (see [napi-loader.ts:62-66] in this file).
95
+ */
96
+ export type NapiResolveSource = "cache" | "local" | "cargo-build";
97
+
98
+ export interface NapiLoadResult {
99
+ instance: ControllerInstance;
100
+ source: NapiResolveSource;
101
+ }
43
102
 
44
103
  export class NapiControllerLoader {
45
104
  /**
@@ -52,9 +111,19 @@ export class NapiControllerLoader {
52
111
  *
53
112
  * Distribution mode (no local_path): out of scope for the PoC; the hook is
54
113
  * left in place so the dispatcher reports env-missing and falls through.
114
+ *
115
+ * Fragment (`#entry`) is optional. When absent, the whole `require()`'d
116
+ * module is returned as the controller — the legacy single-export
117
+ * convention. When present, the fragment is treated as a property name on
118
+ * the loaded module: e.g. `pkg:cargo/foo?local_path=...#bar` returns
119
+ * `module.bar` as the controller. This mirrors the npm `#entry` semantics
120
+ * but indexes into the flat napi export bag instead of opening a different
121
+ * file. The convention is "one source file per controller, top-level
122
+ * export name matches the file" — files-as-controllers in spirit, even
123
+ * though all exports come from one linked dylib.
55
124
  */
56
- async load(purl: string, baseUri: string): Promise<ControllerInstance> {
57
- const [, , name, , qualifiers] = PackageURL.parseString(purl);
125
+ async load(purl: string, baseUri: string): Promise<NapiLoadResult> {
126
+ const [, , name, , qualifiers, entry] = PackageURL.parseString(purl);
58
127
  const localPath = (qualifiers as any)?.get("local_path");
59
128
 
60
129
  const isLocalManifest =
@@ -82,9 +151,44 @@ export class NapiControllerLoader {
82
151
  const cacheKey = canonicalCratePath;
83
152
  const cached = _napiModuleCache.get(cacheKey);
84
153
  if (cached) {
85
- return cached;
154
+ return { instance: project(cached, entry, cratePath), source: "cache" };
155
+ }
156
+
157
+ // Concurrent callers for the same crate await one shared build. They
158
+ // report `local` (not `cache`): they paid the same wall-clock cost as
159
+ // the originator, just by sharing one cargo invocation rather than
160
+ // running their own. Reporting `cache` here would mislead metrics/event
161
+ // consumers into thinking it was a sub-ms hit.
162
+ const existingInFlight = _napiInFlight.get(cacheKey);
163
+ if (existingInFlight) {
164
+ const { rawModule } = await existingInFlight;
165
+ return { instance: project(rawModule, entry, cratePath), source: "local" };
86
166
  }
87
167
 
168
+ const buildPromise = this.buildAndLoad(cratePath, name ?? "", cacheKey);
169
+ _napiInFlight.set(cacheKey, buildPromise);
170
+ let rawModule: any;
171
+ let nodePath: string;
172
+ try {
173
+ ({ rawModule, nodePath } = await buildPromise);
174
+ } finally {
175
+ _napiInFlight.delete(cacheKey);
176
+ }
177
+ // `local` rather than `cargo-build` because the only mode currently
178
+ // wired up is `local_path` dev-mode — cargo's incremental cache means
179
+ // every run after the first is ~50ms of cargo-startup with no real
180
+ // compilation, conceptually the same as the npm `local_path` branch
181
+ // that just imports source already on disk. Distribution mode (when
182
+ // implemented) will return `cargo-build` from its own branch.
183
+ return { instance: project(rawModule, entry, nodePath), source: "local" };
184
+ }
185
+
186
+ private async buildAndLoad(
187
+ cratePath: string,
188
+ fallbackName: string,
189
+ cacheKey: string,
190
+ ): Promise<BuildAndLoadResult> {
191
+ _napiBuildAttempts++;
88
192
  try {
89
193
  await execFileAsync("rustc", ["--version"]);
90
194
  } catch {
@@ -109,7 +213,7 @@ export class NapiControllerLoader {
109
213
  );
110
214
  }
111
215
 
112
- const { targetDir, libName } = await resolveCrateMetadata(cratePath, name ?? "");
216
+ const { targetDir, libName } = await resolveCrateMetadata(cratePath, fallbackName);
113
217
 
114
218
  const dylibPath = await findDylib(targetDir, libName);
115
219
  if (!dylibPath) {
@@ -122,9 +226,9 @@ export class NapiControllerLoader {
122
226
  const nodePath = path.join(path.dirname(dylibPath), `${libName}.node`);
123
227
  await fs.copyFile(dylibPath, nodePath);
124
228
 
125
- let module: any;
229
+ let rawModule: any;
126
230
  try {
127
- module = requireFromHere(nodePath);
231
+ rawModule = requireFromHere(nodePath);
128
232
  } catch (err: any) {
129
233
  throw new RuntimeError(
130
234
  "ERR_CONTROLLER_INVALID",
@@ -132,15 +236,44 @@ export class NapiControllerLoader {
132
236
  );
133
237
  }
134
238
 
135
- if (!module || (!module.create && !module.register)) {
239
+ _napiModuleCache.set(cacheKey, rawModule);
240
+ return { rawModule, nodePath };
241
+ }
242
+ }
243
+
244
+ /**
245
+ * Pick the controller out of the raw napi module. With no fragment, the
246
+ * whole module *is* the controller (legacy single-export shape). With a
247
+ * fragment, look up `module[entry]` — convention: one source file per
248
+ * controller, top-level export name matches the file.
249
+ */
250
+ function project(module: any, entry: string | undefined, where: string): ControllerInstance {
251
+ if (!module) {
252
+ throw new RuntimeError("ERR_CONTROLLER_INVALID", `napi module from ${where} is empty`);
253
+ }
254
+ if (!entry) {
255
+ if (!module.create && !module.register) {
136
256
  throw new RuntimeError(
137
257
  "ERR_CONTROLLER_INVALID",
138
- `pkg:cargo controller at ${nodePath} exports neither create nor register`,
258
+ `pkg:cargo controller at ${where} exports neither create nor register`,
139
259
  );
140
260
  }
141
- _napiModuleCache.set(cacheKey, module);
142
261
  return module;
143
262
  }
263
+ const sub = module[entry];
264
+ if (!sub) {
265
+ throw new RuntimeError(
266
+ "ERR_CONTROLLER_INVALID",
267
+ `pkg:cargo controller at ${where}#${entry}: module has no export named "${entry}"`,
268
+ );
269
+ }
270
+ if (!sub.create && !sub.register) {
271
+ throw new RuntimeError(
272
+ "ERR_CONTROLLER_INVALID",
273
+ `pkg:cargo controller at ${where}#${entry} exports neither create nor register`,
274
+ );
275
+ }
276
+ return sub;
144
277
  }
145
278
 
146
279
  async function resolveCrateMetadata(
@@ -14,13 +14,26 @@ const cacheRoot = process.env.TELO_CACHE_DIR
14
14
  const npmCacheRoot = path.join(cacheRoot, "npm");
15
15
  const isBun = typeof (globalThis as any).Bun !== "undefined";
16
16
 
17
+ /**
18
+ * Tells the dispatcher (and any UI consumer downstream) which branch the
19
+ * resolver actually took. `npm-install` is the only one that hits the network;
20
+ * the rest are sub-second cache/local lookups, so the CLI uses this to decide
21
+ * whether a "downloading…" line was honest or should be silently dropped.
22
+ */
23
+ export type NpmResolveSource = "local" | "node_modules" | "npm-install" | "cache";
24
+
25
+ export interface NpmLoadResult {
26
+ instance: ControllerInstance;
27
+ source: NpmResolveSource;
28
+ }
29
+
17
30
  export class NpmControllerLoader {
18
31
  /**
19
32
  * Resolve a `pkg:npm/...` PURL to a controller module instance. Tries, in order:
20
33
  * a relative `local_path` qualifier, the workspace's `node_modules`, and finally
21
34
  * an isolated install under `~/.cache/telo/npm/<hash>`.
22
35
  */
23
- async load(purl: string, baseUri: string): Promise<ControllerInstance> {
36
+ async load(purl: string, baseUri: string): Promise<NpmLoadResult> {
24
37
  const [, namespace, name, versionSpec, qualifiers, entry] = PackageURL.parseString(purl);
25
38
 
26
39
  const localPath = (qualifiers as any)?.get("local_path");
@@ -28,6 +41,7 @@ export class NpmControllerLoader {
28
41
  const installDir = path.join(npmCacheRoot, cacheKey);
29
42
 
30
43
  let packageRoot: string;
44
+ let source: NpmResolveSource;
31
45
  const isLocalManifest =
32
46
  baseUri && !baseUri.startsWith("http://") && !baseUri.startsWith("https://");
33
47
  if (localPath && isLocalManifest) {
@@ -36,12 +50,14 @@ export class NpmControllerLoader {
36
50
  const resolvedLocalPath = path.resolve(manifestDir, localPath);
37
51
  if (await this.pathExists(resolvedLocalPath)) {
38
52
  packageRoot = resolvedLocalPath;
53
+ source = "local";
39
54
  } else {
40
55
  const nodeModulesPath = await this.findInNodeModules(`${namespace}/${name}`);
41
56
  if (nodeModulesPath) {
42
57
  packageRoot = nodeModulesPath;
58
+ source = "node_modules";
43
59
  } else {
44
- await this.ensureNpmPackageInstalled(installDir, `${namespace}/${name}@${versionSpec}`);
60
+ source = await this.ensureNpmPackageInstalled(installDir, `${namespace}/${name}@${versionSpec}`);
45
61
  packageRoot = this.getInstalledPackageRoot(installDir, `${namespace}/${name}`);
46
62
  }
47
63
  }
@@ -49,8 +65,9 @@ export class NpmControllerLoader {
49
65
  const nodeModulesPath = await this.findInNodeModules(`${namespace}/${name}`);
50
66
  if (nodeModulesPath) {
51
67
  packageRoot = nodeModulesPath;
68
+ source = "node_modules";
52
69
  } else {
53
- await this.ensureNpmPackageInstalled(installDir, `${namespace}/${name}@${versionSpec}`);
70
+ source = await this.ensureNpmPackageInstalled(installDir, `${namespace}/${name}@${versionSpec}`);
54
71
  packageRoot = this.getInstalledPackageRoot(installDir, `${namespace}/${name}`);
55
72
  }
56
73
  }
@@ -62,10 +79,13 @@ export class NpmControllerLoader {
62
79
  `Invalid controller loaded from "${purl}": missing create or register function`,
63
80
  );
64
81
  }
65
- return instance;
82
+ return { instance, source };
66
83
  }
67
84
 
68
- private async ensureNpmPackageInstalled(installDir: string, packageSpec: string): Promise<void> {
85
+ private async ensureNpmPackageInstalled(
86
+ installDir: string,
87
+ packageSpec: string,
88
+ ): Promise<"cache" | "npm-install"> {
69
89
  const packageName = this.getPackageName(
70
90
  packageSpec.startsWith(".") || path.isAbsolute(packageSpec)
71
91
  ? await this.getLocalPackageName(packageSpec)
@@ -74,7 +94,7 @@ export class NpmControllerLoader {
74
94
  const packageRoot = this.getInstalledPackageRoot(installDir, packageName);
75
95
  const packageJsonPath = path.join(packageRoot, "package.json");
76
96
  if (await this.pathExists(packageJsonPath)) {
77
- return;
97
+ return "cache";
78
98
  }
79
99
 
80
100
  await fs.mkdir(installDir, { recursive: true });
@@ -98,6 +118,7 @@ export class NpmControllerLoader {
98
118
  ];
99
119
 
100
120
  await execFileAsync("npm", args);
121
+ return "npm-install";
101
122
  }
102
123
 
103
124
  private getPackageName(packageSpec: string): string {
@@ -27,10 +27,7 @@ type ResourceDefinitionResource = RuntimeResource & {
27
27
  class ResourceDefinition implements ResourceInstance {
28
28
  readonly kind: "ResourceDefinition" = "ResourceDefinition";
29
29
 
30
- constructor(
31
- readonly resource: ResourceDefinitionResource,
32
- private controllerLoader: ControllerLoader,
33
- ) {}
30
+ constructor(readonly resource: ResourceDefinitionResource) {}
34
31
 
35
32
  async init(ctx: ResourceContext) {
36
33
  if (!this.resource.controllers?.length) {
@@ -43,24 +40,25 @@ class ResourceDefinition implements ResourceInstance {
43
40
  );
44
41
  return;
45
42
  }
46
- ctx.emit("ControllerLoading", { controllers: this.resource.controllers });
47
- try {
48
- const controllerInstance = await this.controllerLoader.load(
49
- this.resource.controllers,
50
- this.resource.metadata.source,
51
- ctx.getControllerPolicy(),
52
- );
53
- ctx.emit("ControllerLoaded", { schema: controllerInstance.schema });
54
- ctx.registerDefinition(this.resource);
55
- await ctx.registerController(
56
- this.resource.metadata.module,
57
- this.resource.metadata.name,
58
- controllerInstance,
59
- );
60
- } catch (err) {
61
- ctx.emit("ControllerLoadFailed", { error: (err as Error).message });
62
- throw err;
63
- }
43
+ // The loader owns ControllerLoading / ControllerLoaded / ControllerLoadFailed
44
+ // emission so it can fire one event per attempted candidate (env-missing
45
+ // fallback chains), and so the payload can include the actually-picked PURL,
46
+ // which branch resolved it (`source`), and timing — none of which are known
47
+ // here at the call site.
48
+ const loader = new ControllerLoader({
49
+ emit: (e) => ctx.emit(e.name, e.payload),
50
+ });
51
+ const controllerInstance = await loader.load(
52
+ this.resource.controllers,
53
+ this.resource.metadata.source,
54
+ ctx.getControllerPolicy(),
55
+ );
56
+ ctx.registerDefinition(this.resource);
57
+ await ctx.registerController(
58
+ this.resource.metadata.module,
59
+ this.resource.metadata.name,
60
+ controllerInstance,
61
+ );
64
62
  }
65
63
  }
66
64
 
@@ -78,7 +76,7 @@ export async function create(resource: any, ctx: ResourceContext): Promise<Resou
78
76
 
79
77
  // Return a fully-formed ResourceDefinition instance
80
78
  const definition = resource as unknown as ResourceDefinitionResource;
81
- return new ResourceDefinition(definition, new ControllerLoader());
79
+ return new ResourceDefinition(definition);
82
80
  }
83
81
 
84
82
  export const schema = {
@@ -18,6 +18,52 @@ import { RuntimeError } from "@telorun/sdk";
18
18
 
19
19
  export { resourceKey };
20
20
 
21
+ type Walker = (ctx: Record<string, unknown>) => unknown;
22
+
23
+ /** Compile a manifest subtree into a tightly-bound walker closure. The returned
24
+ * function takes an activation object and rebuilds a fresh container of the
25
+ * same shape with all `${{ }}` CompiledValues evaluated against the activation.
26
+ * Per-call overhead is one closure invocation per node — no isCompiledValue /
27
+ * Array.isArray / typeof / Object.entries checks at runtime. */
28
+ function compileWalker(value: unknown): Walker {
29
+ if (isCompiledValue(value)) {
30
+ const compiled = value;
31
+ return (ctx) => {
32
+ try {
33
+ return compiled.call(ctx);
34
+ } catch (error) {
35
+ const expr = compiled.source ? `\${{ ${compiled.source} }}` : "unknown expression";
36
+ const msg = error instanceof Error ? error.message : String(error);
37
+ throw new Error(`Expression ${expr} failed: ${msg}`);
38
+ }
39
+ };
40
+ }
41
+ if (Array.isArray(value)) {
42
+ const childWalkers = value.map(compileWalker);
43
+ const n = childWalkers.length;
44
+ return (ctx) => {
45
+ const out = new Array(n);
46
+ for (let i = 0; i < n; i++) out[i] = childWalkers[i]!(ctx);
47
+ return out;
48
+ };
49
+ }
50
+ if (value !== null && typeof value === "object") {
51
+ const entries = Object.entries(value as Record<string, unknown>).map(
52
+ ([k, v]) => [k, compileWalker(v)] as const,
53
+ );
54
+ const n = entries.length;
55
+ return (ctx) => {
56
+ const out: Record<string, unknown> = {};
57
+ for (let i = 0; i < n; i++) {
58
+ const [k, fn] = entries[i]!;
59
+ out[k] = fn(ctx);
60
+ }
61
+ return out;
62
+ };
63
+ }
64
+ return () => value;
65
+ }
66
+
21
67
  /**
22
68
  * Base class for all evaluation contexts. Owns template
23
69
  * expansion, secrets redaction, and the generic resource lifecycle tree.
@@ -453,7 +499,9 @@ export class EvaluationContext implements IEvaluationContext {
453
499
  if (declaredCodes && !declaredCodes.has(err.code)) {
454
500
  await this.emit(`${kind}.${name}.InvokeRejected.Undeclared`, payload);
455
501
  }
456
- } else if (err instanceof Error) {
502
+ throw err;
503
+ }
504
+ if (err instanceof Error) {
457
505
  await this.emit(`${kind}.${name}.InvokeFailed`, {
458
506
  name: err.name,
459
507
  message: err.message,
@@ -464,7 +512,22 @@ export class EvaluationContext implements IEvaluationContext {
464
512
  message: String(err),
465
513
  });
466
514
  }
467
- throw err;
515
+ // Already enriched at an inner invoke: keep the innermost (most
516
+ // specific) resource as the failure location.
517
+ if (err instanceof RuntimeError && err.diagnostics?.length) throw err;
518
+ const message = err instanceof Error ? err.message : String(err);
519
+ const code = err instanceof Error ? err.name : undefined;
520
+ // Keep `message` raw so callers (Run.Sequence catch blocks, assertions)
521
+ // see the original error text unchanged. Resource context lives only on
522
+ // the attached diagnostic, which the CLI's formatter renders as the
523
+ // location prefix. Attach the original error as `cause` so
524
+ // formatErrorForDiagnostic walks the chain and surfaces the underlying
525
+ // stack and well-known error fields (AWS, pg, Node system errors).
526
+ const wrapped = new RuntimeError("ERR_EXECUTION_FAILED", message, [
527
+ { kind, resource: name, message, code },
528
+ ]);
529
+ (wrapped as { cause?: unknown }).cause = err;
530
+ throw wrapped;
468
531
  }
469
532
  }
470
533
 
@@ -497,48 +560,62 @@ export class EvaluationContext implements IEvaluationContext {
497
560
 
498
561
  /**
499
562
  * Expand a value that may contain precompiled ${{ }} templates.
500
- * Works recursively over CompiledValues, arrays, and objects.
563
+ *
564
+ * Hot path: each unique manifest subtree is compiled once into a tightly-bound
565
+ * walker closure (no per-call `isCompiledValue` / `Array.isArray` / `typeof` /
566
+ * `Object.entries` overhead, no recursive method dispatch). The walker tree is
567
+ * cached by the input value's identity in `walkerCache`, so subsequent calls
568
+ * with the same manifest data reuse it. The walker reads from `this._context`
569
+ * — which `expandWith` mutates in place — and emits a fresh container per call
570
+ * to preserve the original recursive `expand`'s semantics.
501
571
  */
502
572
  expand(value: unknown): unknown {
503
- if (isCompiledValue(value)) {
504
- try {
505
- return value.call(this._context);
506
- } catch (error) {
507
- const expr = value.source ? `\${{ ${value.source} }}` : "unknown expression";
508
- const msg = error instanceof Error ? error.message : String(error);
509
- throw new Error(`Expression ${expr} failed: ${msg}`);
510
- }
511
- }
512
- if (Array.isArray(value)) {
513
- return value.map((entry) => this.expand(entry));
514
- }
515
- if (value !== null && typeof value === "object") {
516
- const resolved: Record<string, unknown> = {};
517
- for (const [key, entry] of Object.entries(value as Record<string, unknown>)) {
518
- resolved[key] = this.expand(entry);
519
- }
520
- return resolved;
521
- }
522
- return value;
573
+ if (value === null || typeof value !== "object") return value;
574
+ const cached = this.walkerCache.get(value as object);
575
+ if (cached) return cached(this._context);
576
+ const walker = compileWalker(value);
577
+ this.walkerCache.set(value as object, walker);
578
+ return walker(this._context);
523
579
  }
524
580
 
525
581
  /**
526
582
  * Expand a value using this context merged with additional properties.
527
- * Equivalent to merge(extraContext).expand(value) without allocating a context object.
583
+ *
584
+ * Hot path optimisation: rather than allocate a fresh prototype-less object
585
+ * per call (one allocation + N property copies for N keys in the saved
586
+ * context), we mutate `_context` in place — adding or overwriting only the
587
+ * `extraContext` keys — and restore the previous values on exit. Safe because
588
+ * `expand` is synchronous; cel-vm closures only read from the activation.
528
589
  */
529
590
  expandWith(value: unknown, extraContext: Record<string, unknown>): unknown {
530
- const saved = this._context;
531
- this._context = Object.assign(Object.create(null), saved, extraContext) as Record<
532
- string,
533
- unknown
534
- >;
591
+ const ctx = this._context as Record<string, unknown>;
592
+ const keys = Object.keys(extraContext);
593
+ const savedValues: unknown[] = new Array(keys.length);
594
+ const hadKey: boolean[] = new Array(keys.length);
595
+ for (let i = 0; i < keys.length; i++) {
596
+ const k = keys[i]!;
597
+ hadKey[i] = k in ctx;
598
+ if (hadKey[i]) savedValues[i] = ctx[k];
599
+ ctx[k] = extraContext[k];
600
+ }
535
601
  try {
536
602
  return this.expand(value);
537
603
  } finally {
538
- this._context = saved;
604
+ for (let i = 0; i < keys.length; i++) {
605
+ const k = keys[i]!;
606
+ if (hadKey[i]) ctx[k] = savedValues[i];
607
+ else delete ctx[k];
608
+ }
539
609
  }
540
610
  }
541
611
 
612
+ /** Cache of compiled walker closures keyed on the manifest subtree they walk.
613
+ * WeakMap so entries are GC'd if the manifest is reloaded. */
614
+ private readonly walkerCache = new WeakMap<
615
+ object,
616
+ (ctx: Record<string, unknown>) => unknown
617
+ >();
618
+
542
619
  /**
543
620
  * Expand specific dot-paths within an object. '**' expands the entire object.
544
621
  * Paths listed in excludePaths are left untouched (runtime takes precedence).
package/src/kernel.ts CHANGED
@@ -766,6 +766,27 @@ function injectAtPath(
766
766
  function traverse(obj: unknown, partsLeft: string[]): void {
767
767
  if (!obj || typeof obj !== "object" || partsLeft.length === 0) return;
768
768
  const [head, ...rest] = partsLeft;
769
+
770
+ // Map iteration: descend into every value of the current object (used for
771
+ // schema fields with `additionalProperties` like `content[mime]`).
772
+ if (head === "{}") {
773
+ const container = obj as Record<string, unknown>;
774
+ for (const mapKey of Object.keys(container)) {
775
+ const elem = container[mapKey];
776
+ if (!elem || typeof elem !== "object") continue;
777
+ if (rest.length === 0) {
778
+ const ref = elem as Record<string, unknown>;
779
+ if (typeof ref.kind === "string" && typeof ref.name === "string") {
780
+ const instance = getInstance(ref.name);
781
+ if (instance) container[mapKey] = instance;
782
+ }
783
+ } else {
784
+ traverse(elem, rest);
785
+ }
786
+ }
787
+ return;
788
+ }
789
+
769
790
  const isArr = head.endsWith("[]");
770
791
  const key = isArr ? head.slice(0, -2) : head;
771
792
  const container = obj as Record<string, unknown>;