@telorun/kernel 0.74.0 → 0.76.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 (81) hide show
  1. package/dist/bundle/module-artifact.d.ts +21 -4
  2. package/dist/bundle/module-artifact.d.ts.map +1 -1
  3. package/dist/bundle/module-artifact.js +42 -17
  4. package/dist/bundle/module-artifact.js.map +1 -1
  5. package/dist/bundle/module-manifest.d.ts +5 -1
  6. package/dist/bundle/module-manifest.d.ts.map +1 -1
  7. package/dist/bundle/module-manifest.js +5 -1
  8. package/dist/bundle/module-manifest.js.map +1 -1
  9. package/dist/controller-loader.d.ts +3 -2
  10. package/dist/controller-loader.d.ts.map +1 -1
  11. package/dist/controller-loader.js +10 -9
  12. package/dist/controller-loader.js.map +1 -1
  13. package/dist/controller-loaders/bundle-loader.d.ts +51 -4
  14. package/dist/controller-loaders/bundle-loader.d.ts.map +1 -1
  15. package/dist/controller-loaders/bundle-loader.js +260 -11
  16. package/dist/controller-loaders/bundle-loader.js.map +1 -1
  17. package/dist/controller-loaders/napi-loader.d.ts.map +1 -1
  18. package/dist/controller-loaders/napi-loader.js +8 -1
  19. package/dist/controller-loaders/napi-loader.js.map +1 -1
  20. package/dist/controller-loaders/npm-loader.d.ts.map +1 -1
  21. package/dist/controller-loaders/npm-loader.js +65 -3
  22. package/dist/controller-loaders/npm-loader.js.map +1 -1
  23. package/dist/controller-loaders/sibling-libraries.d.ts +93 -0
  24. package/dist/controller-loaders/sibling-libraries.d.ts.map +1 -0
  25. package/dist/controller-loaders/sibling-libraries.js +111 -0
  26. package/dist/controller-loaders/sibling-libraries.js.map +1 -0
  27. package/dist/controller-loaders/source-bundle-builder.d.ts +43 -1
  28. package/dist/controller-loaders/source-bundle-builder.d.ts.map +1 -1
  29. package/dist/controller-loaders/source-bundle-builder.js +249 -24
  30. package/dist/controller-loaders/source-bundle-builder.js.map +1 -1
  31. package/dist/controllers/module/import-controller.d.ts.map +1 -1
  32. package/dist/controllers/module/import-controller.js +5 -1
  33. package/dist/controllers/module/import-controller.js.map +1 -1
  34. package/dist/controllers/resource-definition/resource-definition-controller.d.ts.map +1 -1
  35. package/dist/controllers/resource-definition/resource-definition-controller.js +5 -1
  36. package/dist/controllers/resource-definition/resource-definition-controller.js.map +1 -1
  37. package/dist/index.d.ts +1 -1
  38. package/dist/index.d.ts.map +1 -1
  39. package/dist/index.js +1 -1
  40. package/dist/index.js.map +1 -1
  41. package/dist/kernel.d.ts +11 -1
  42. package/dist/kernel.d.ts.map +1 -1
  43. package/dist/kernel.js +44 -5
  44. package/dist/kernel.js.map +1 -1
  45. package/dist/manifest-schemas.d.ts +1 -1
  46. package/dist/manifest-schemas.d.ts.map +1 -1
  47. package/dist/manifest-schemas.js +6 -5
  48. package/dist/manifest-schemas.js.map +1 -1
  49. package/dist/manifest-sources/local-manifest-cache-source.d.ts +13 -2
  50. package/dist/manifest-sources/local-manifest-cache-source.d.ts.map +1 -1
  51. package/dist/manifest-sources/local-manifest-cache-source.js +19 -3
  52. package/dist/manifest-sources/local-manifest-cache-source.js.map +1 -1
  53. package/dist/module-context.d.ts +7 -0
  54. package/dist/module-context.d.ts.map +1 -1
  55. package/dist/module-context.js +27 -7
  56. package/dist/module-context.js.map +1 -1
  57. package/dist/resource-context.d.ts +5 -0
  58. package/dist/resource-context.d.ts.map +1 -1
  59. package/dist/resource-context.js +6 -0
  60. package/dist/resource-context.js.map +1 -1
  61. package/dist/schema-validator.d.ts.map +1 -1
  62. package/dist/schema-validator.js +2 -2
  63. package/dist/schema-validator.js.map +1 -1
  64. package/package.json +4 -4
  65. package/src/bundle/module-artifact.ts +50 -20
  66. package/src/bundle/module-manifest.ts +14 -1
  67. package/src/controller-loader.ts +25 -7
  68. package/src/controller-loaders/bundle-loader.ts +326 -10
  69. package/src/controller-loaders/napi-loader.ts +8 -1
  70. package/src/controller-loaders/npm-loader.ts +73 -3
  71. package/src/controller-loaders/sibling-libraries.ts +171 -0
  72. package/src/controller-loaders/source-bundle-builder.ts +303 -23
  73. package/src/controllers/module/import-controller.ts +8 -1
  74. package/src/controllers/resource-definition/resource-definition-controller.ts +7 -1
  75. package/src/index.ts +6 -1
  76. package/src/kernel.ts +59 -11
  77. package/src/manifest-schemas.ts +9 -5
  78. package/src/manifest-sources/local-manifest-cache-source.ts +20 -3
  79. package/src/module-context.ts +35 -7
  80. package/src/resource-context.ts +8 -0
  81. package/src/schema-validator.ts +1 -2
package/src/kernel.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import {
2
2
  AnalysisRegistry,
3
+ authoredModuleMetadata,
3
4
  buildEvalPaths,
4
5
  collectZoneModuleDocuments,
5
6
  flattenForAnalyzer,
@@ -63,12 +64,17 @@ import {
63
64
  resolveCacheRoot,
64
65
  resolveEntryDir,
65
66
  } from "./manifest-sources/local-manifest-cache-source.js";
66
- import { readOwnerManifest } from "./bundle/module-manifest.js";
67
+ import { readOwnerManifest, type OwnerManifest } from "./bundle/module-manifest.js";
67
68
  import {
68
69
  moduleArtifactFor,
69
70
  moduleDirectoryFor,
70
71
  type ModuleArtifact,
71
72
  } from "./bundle/module-artifact.js";
73
+ import {
74
+ NO_SIBLING_LIBRARIES,
75
+ buildSiblingLibraries,
76
+ type SiblingLibraryMap,
77
+ } from "./controller-loaders/sibling-libraries.js";
72
78
  import { defaultTransportRegistry } from "./transports/transport-registry.js";
73
79
  import {
74
80
  collectDeclaredEnvKeys,
@@ -158,6 +164,10 @@ export class Kernel implements IKernel {
158
164
  /** Per-module artifact handles, keyed by canonical manifest source. Rebuilt on
159
165
  * every `load()`; empty for a graph of purely local / manifest-only modules. */
160
166
  private readonly moduleArtifacts = new Map<string, ModuleArtifact>();
167
+ /** Per-module sibling-library resolution, keyed by the same canonical manifest
168
+ * source: which bare specifiers this module's controller bundles import, and
169
+ * which module's library layer each resolves to. Rebuilt on every `load()`. */
170
+ private readonly siblingLibraries = new Map<string, SiblingLibraryMap>();
161
171
  private _loadedGraph?: LoadedGraph;
162
172
  // Lifecycle state — guards boot/runTargets/teardown/invoke transitions.
163
173
  // teardown() is the only idempotent method; everything else throws on misuse.
@@ -689,6 +699,14 @@ export class Kernel implements IKernel {
689
699
  }
690
700
  });
691
701
  this.rootContext.setTargets(rawTargets as BootTarget[]);
702
+ // `module.<field>` — the module doc's own metadata, so a manifest reads
703
+ // its version instead of restating it in a second place nothing keeps in
704
+ // sync. Set from the module doc rather than the application specifically:
705
+ // an imported library's context gets its own, which is the only reading
706
+ // that is true of the resources inside it.
707
+ this.rootContext.setModuleMetadata(
708
+ authoredModuleMetadata(manifest.metadata as Record<string, unknown> | undefined),
709
+ );
692
710
  if (manifest.kind === "Telo.Application") {
693
711
  rootApplicationManifest = manifest;
694
712
  this._appName = (manifest.metadata as { name?: string } | undefined)?.name;
@@ -1005,7 +1023,9 @@ export class Kernel implements IKernel {
1005
1023
  }
1006
1024
 
1007
1025
  /**
1008
- * Build one {@link ModuleArtifact} per loaded module that ships a payload.
1026
+ * Read every loaded module's owner manifest once, and build from it the two
1027
+ * things a controller resolution needs: the module's artifact, and the sibling
1028
+ * libraries its bundles import by bare specifier.
1009
1029
  *
1010
1030
  * Here, and not inside a controller loader, because this is the only point
1011
1031
  * where both halves are in hand: the **pinned** ref the importer wrote
@@ -1029,26 +1049,48 @@ export class Kernel implements IKernel {
1029
1049
  */
1030
1050
  private buildModuleArtifacts(graph: LoadedGraph, manifestsDir: string | undefined): void {
1031
1051
  this.moduleArtifacts.clear();
1052
+ this.siblingLibraries.clear();
1032
1053
  const transports = defaultTransportRegistry(this.registryUrl);
1033
1054
  const entryDir = this._entryUrl ? resolveEntryDir(this._entryUrl) ?? "" : "";
1055
+ // Parsed once per module and shared with the sibling-library join below: a
1056
+ // `telo.yaml` is a large multi-document file, and re-reading each target's
1057
+ // once per import edge put dozens of redundant full-YAML parses on the boot
1058
+ // path of every standard-library app.
1059
+ const owners = new Map<string, OwnerManifest>();
1060
+ const directories = new Map<string, string | undefined>();
1034
1061
  for (const [, module] of graph.modules) {
1035
1062
  const file = module.owner;
1036
- if (this.moduleArtifacts.has(file.source)) continue;
1063
+ if (owners.has(file.source)) continue;
1064
+ const owner = readOwnerManifest(file.text);
1065
+ owners.set(file.source, owner);
1066
+ const moduleDir = moduleDirectoryFor(
1067
+ file.requestedUrl,
1068
+ file.source,
1069
+ entryDir,
1070
+ this.registryUrl,
1071
+ manifestsDir,
1072
+ );
1073
+ directories.set(file.source, moduleDir ?? undefined);
1037
1074
  const artifact = moduleArtifactFor({
1038
1075
  pinnedRef: file.requestedUrl,
1039
- layers: readOwnerManifest(file.text).layers,
1040
- moduleDir: moduleDirectoryFor(
1041
- file.requestedUrl,
1042
- file.source,
1043
- entryDir,
1044
- this.registryUrl,
1045
- manifestsDir,
1046
- ),
1076
+ layers: owner.layers,
1077
+ moduleDir,
1047
1078
  transports,
1048
1079
  log: this.logging.kernelLogger(),
1049
1080
  });
1050
1081
  if (artifact) this.moduleArtifacts.set(file.source, artifact);
1051
1082
  }
1083
+
1084
+ // The sibling-library join is a pure function of the graph and these three
1085
+ // lookups; it lives beside the model it produces (`sibling-libraries.ts`).
1086
+ for (const [source, map] of buildSiblingLibraries(graph, {
1087
+ ownerManifests: owners,
1088
+ artifactFor: (source) => this.moduleArtifacts.get(source),
1089
+ directoryFor: (source) => directories.get(source),
1090
+ log: this.logging.kernelLogger(),
1091
+ })) {
1092
+ this.siblingLibraries.set(source, map);
1093
+ }
1052
1094
  }
1053
1095
 
1054
1096
  /** The artifact of the module whose manifest resolved from `source`, or
@@ -1058,6 +1100,12 @@ export class Kernel implements IKernel {
1058
1100
  return source ? this.moduleArtifacts.get(source) : undefined;
1059
1101
  }
1060
1102
 
1103
+ /** The module-owned libraries the module at `source` imports by bare
1104
+ * specifier. Empty for a module that imports none. */
1105
+ getSiblingLibraries(source: string | undefined): SiblingLibraryMap {
1106
+ return (source ? this.siblingLibraries.get(source) : undefined) ?? NO_SIBLING_LIBRARIES;
1107
+ }
1108
+
1061
1109
  /** Authored `kind` of a declared resource by name, from the static manifest
1062
1110
  * set. Init-order-independent (unlike `resourceInstances`), so a controller
1063
1111
  * resolving a `!ref <name>` sentinel before the target initializes can still
@@ -5,15 +5,19 @@ import addFormats from "ajv-formats";
5
5
  import { OBSERVED_STATE_SCHEMA, registerTeloKeywords } from "@telorun/analyzer";
6
6
  const Ajv = AjvModule.default ?? AjvModule;
7
7
 
8
- // Re-export the shared ResourceRef fragment from the templating package
9
- // so consumers that pull it from the kernel manifest-schemas surface keep
10
- // working. The canonical home is `@telorun/templating` because the
11
- // fragment describes the parsed shape of the `!ref` tag's TaggedSentinel.
8
+ // Re-export the shared manifest fragments so consumers reaching them through the
9
+ // kernel's surface keep working. The canonical home is `@telorun/analyzer`: they
10
+ // describe manifest STRUCTURE, and the editor validates in a browser through the
11
+ // analyzer, which must not depend on this package a fragment here would exist
12
+ // only at runtime, invisible to `telo check` and to the editor.
12
13
  export {
14
+ InvokeStepSchema,
13
15
  MANIFEST_SCHEMA_URI,
14
16
  ManifestRootSchema,
17
+ manifestFragmentRef,
15
18
  ResourceRefSchema,
16
- } from "@telorun/templating";
19
+ RetryPolicySchema,
20
+ } from "@telorun/analyzer";
17
21
 
18
22
  const metadataSchema = {
19
23
  type: "object",
@@ -179,13 +179,29 @@ export async function writeManifestCache(
179
179
  return written;
180
180
  }
181
181
 
182
+ /** Matches a URL scheme, as distinct from a Windows drive letter. `D:\src`
183
+ * satisfies RFC 3986's scheme grammar, so the second character is required:
184
+ * no registered scheme is one letter, and every drive is. */
185
+ const URL_SCHEME = /^[a-zA-Z][a-zA-Z0-9+.-]+:/;
186
+
182
187
  /** Resolve the entry-anchor directory for the manifest cache.
183
188
  *
184
189
  * For a file path or `file://` URL: returns the containing directory.
185
190
  * For a directory path: returns the directory itself.
186
- * For an HTTP(S) URL: returns `null` (no local anchor; cache writes skipped). */
191
+ * For ANY other scheme: returns `null` (no local anchor; cache writes skipped).
192
+ *
193
+ * The rule is the scheme, not a list of known ones. Testing only for http(s)
194
+ * left every other scheme falling through to `path.resolve`, which reads it as
195
+ * a relative path: a `memory://app/telo.yaml` entry anchored its cache at
196
+ * `<cwd>/memory:/app/.telo`. On POSIX that is a legal directory name, so the
197
+ * kernel silently created one inside whatever directory it was run from and the
198
+ * cache appeared to work; on Windows `:` is illegal in a filename, so the same
199
+ * load failed at `mkdir`. A transient source has no local anchor by
200
+ * construction — that is what the whole-scheme test says, and it now covers
201
+ * `oci://` and any scheme added later without another edit here. */
187
202
  export function resolveEntryDir(entryPath: string): string | null {
188
- if (entryPath.startsWith("http://") || entryPath.startsWith("https://")) {
203
+ const scheme = URL_SCHEME.exec(entryPath)?.[0];
204
+ if (scheme !== undefined && scheme !== "file:") {
189
205
  return null;
190
206
  }
191
207
  let absolute: string;
@@ -208,7 +224,8 @@ export function resolveEntryDir(entryPath: string): string | null {
208
224
  *
209
225
  * `TELO_CACHE_DIR` (the relocated root a prebuilt image bakes its deps into)
210
226
  * wins; otherwise the root sits beside the entry at `<entry-dir>/.telo`.
211
- * Returns `null` for http(s) entries with no local anchor (disk cache skipped).
227
+ * Returns `null` for an entry with no local anchor an http(s), `memory://`
228
+ * or any other non-`file:` scheme — in which case the disk cache is skipped.
212
229
  * Consumers append the conventional subdirs: `manifests/`, `manifests/__validators/`,
213
230
  * `npm/`. */
214
231
  export function resolveCacheRoot(entryPath: string): string | null {
@@ -1,4 +1,10 @@
1
- import { deriveContext, executeInvokeStep, getRefIdentity, RuntimeError } from "@telorun/sdk";
1
+ import {
2
+ deriveContext,
3
+ executeInvokeStep,
4
+ getRefIdentity,
5
+ type InlineInvokeTarget,
6
+ RuntimeError,
7
+ } from "@telorun/sdk";
2
8
  import type { ScopeConfig } from "./logging/scope-config.js";
3
9
  import type {
4
10
  BootTarget,
@@ -69,6 +75,11 @@ export class ModuleContext extends EvaluationContext implements IModuleContext {
69
75
  * populated on the root context from the Application's `ports` block;
70
76
  * imported child modules keep this empty. */
71
77
  private _ports: Record<string, unknown> = {};
78
+ /** The module doc's own `metadata`, published as `module.<field>` so a
79
+ * manifest can read its own version instead of restating it. Per module, not
80
+ * per application: an imported library's resources read the LIBRARY's
81
+ * metadata, which is the only reading that is true of them. */
82
+ private _module: Record<string, unknown> = {};
72
83
 
73
84
  /** Maps import alias → real module name for kind resolution. */
74
85
  private readonly importAliases = new Map<string, string>();
@@ -175,6 +186,10 @@ export class ModuleContext extends EvaluationContext implements IModuleContext {
175
186
  return this._ports;
176
187
  }
177
188
 
189
+ get module(): Record<string, unknown> {
190
+ return this._module;
191
+ }
192
+
178
193
  setVariables(vars: Record<string, unknown>): void {
179
194
  this._variables = vars;
180
195
  this._rebuildContext();
@@ -185,6 +200,11 @@ export class ModuleContext extends EvaluationContext implements IModuleContext {
185
200
  this._rebuildContext();
186
201
  }
187
202
 
203
+ setModuleMetadata(metadata: Record<string, unknown>): void {
204
+ this._module = metadata;
205
+ this._rebuildContext();
206
+ }
207
+
188
208
  setTargets(vars: BootTarget[]): void {
189
209
  this.targets = vars;
190
210
  }
@@ -515,6 +535,7 @@ export class ModuleContext extends EvaluationContext implements IModuleContext {
515
535
  secrets,
516
536
  resources: this._resources,
517
537
  ports: this._ports,
538
+ module: this._module,
518
539
  };
519
540
  }
520
541
 
@@ -524,6 +545,7 @@ export class ModuleContext extends EvaluationContext implements IModuleContext {
524
545
  secrets: this._secrets,
525
546
  resources: this._resources,
526
547
  ports: this._ports,
548
+ module: this._module,
527
549
  };
528
550
  this._secretValues = collectSecretValues(this._secrets);
529
551
  }
@@ -647,16 +669,22 @@ export class ModuleContext extends EvaluationContext implements IModuleContext {
647
669
  // step — only a structural `{ invoke: <ref>, inputs }` spec (no `run`)
648
670
  // belongs here; the live instance falls through to the runnable branch.
649
671
  if (!isRunnableInstance(target) && "invoke" in target && target.invoke !== undefined) {
672
+ // SPREAD, not field-by-field. A dispatch site is one kernel-owned shape,
673
+ // and rebuilding it here by hand is what silently dropped `retry:` —
674
+ // the schema admitted the field and this reconstruction threw it away, so
675
+ // a boot target asking for three attempts got one. Forwarding the entry
676
+ // keeps this correct the next time the shape grows.
677
+ // A cross-module `!ref Alias.name` stays a `{kind, name, alias}` ref; the leaf's
678
+ // alias branch resolves it via `resolveImportedInstance` and dispatches through
679
+ // `invokeResolved` so invocation events and error wrapping still fire.
650
680
  const step: InvokeStep = {
681
+ ...(target as InlineInvokeTarget),
651
682
  name: target.name ?? `Target${i}`,
652
- when: target.when,
653
- // A cross-module `!ref Alias.name` stays a `{kind, name, alias}` ref; the leaf's
654
- // alias branch resolves it via `resolveImportedInstance` and dispatches through
655
- // `invokeResolved` so invocation events and error wrapping still fire.
656
683
  invoke: target.invoke,
657
- inputs: target.inputs,
658
684
  };
659
- await executeInvokeStep(step, stepCtx, { steps });
685
+ // The boot cancellation, so a target parked in a retry backoff ends on
686
+ // SIGINT rather than sitting out the remaining delay.
687
+ await executeInvokeStep(step, stepCtx, { steps, invokeCtx: ctx });
660
688
  continue;
661
689
  }
662
690
  if ("ref" in target && target.ref != null) {
@@ -35,6 +35,7 @@ import * as path from "path";
35
35
  import { pathToFileURL } from "url";
36
36
  import { withBigIntsAsNumbers } from "./bigint-schema-view.js";
37
37
  import type { ModuleArtifact } from "./bundle/module-artifact.js";
38
+ import type { SiblingLibraryMap } from "./controller-loaders/sibling-libraries.js";
38
39
  import { resolveModuleFileUri } from "./module-file-resolution.js";
39
40
  import { hostEnv } from "./host-env.js";
40
41
  import type { LoggingHost } from "./logging/logging-host.js";
@@ -781,6 +782,13 @@ export class ResourceContextImpl implements ResourceContext {
781
782
  return this.kernel.getModuleArtifact(source);
782
783
  }
783
784
 
785
+ /** The module-owned libraries the module at `source` imports by bare specifier
786
+ * — how a controller bundle's `@telorun/kv-store` finds the module that owns
787
+ * it. Kernel-only, on the same seam as {@link getModuleArtifact}. */
788
+ getSiblingLibraries(source: string | undefined): SiblingLibraryMap {
789
+ return this.kernel.getSiblingLibraries(source);
790
+ }
791
+
784
792
  /**
785
793
  * Resolve a module-relative reference against the declaring module's own
786
794
  * directory, materializing the layers that could carry it on first use.
@@ -7,7 +7,7 @@ import { createHash } from "node:crypto";
7
7
  import * as fs from "node:fs";
8
8
  import { createRequire } from "node:module";
9
9
  import * as path from "node:path";
10
- import { registerTeloKeywords } from "@telorun/analyzer";
10
+ import { ManifestRootSchema, registerTeloKeywords } from "@telorun/analyzer";
11
11
  import { X_TELO_TYPE } from "@telorun/sdk";
12
12
  import { mergeFilledDefaults, withBigIntsAsNumbers } from "./bigint-schema-view.js";
13
13
  import { formatAjvErrors } from "./manifest-schemas.js";
@@ -25,7 +25,6 @@ function describeValue(data: unknown): string {
25
25
  import {
26
26
  EXACT_TEMPLATE_REGEX,
27
27
  isTaggedSentinel,
28
- ManifestRootSchema,
29
28
  } from "@telorun/templating";
30
29
 
31
30
  const Ajv = AjvModule.default ?? AjvModule;