@telorun/kernel 0.60.0 → 0.62.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.
- package/dist/bundle/module-artifact.d.ts +28 -2
- package/dist/bundle/module-artifact.d.ts.map +1 -1
- package/dist/bundle/module-artifact.js +36 -9
- package/dist/bundle/module-artifact.js.map +1 -1
- package/dist/controller-loader.d.ts +7 -4
- package/dist/controller-loader.d.ts.map +1 -1
- package/dist/controller-loader.js.map +1 -1
- package/dist/controller-loaders/bundle-loader.d.ts.map +1 -1
- package/dist/controller-loaders/bundle-loader.js +9 -4
- package/dist/controller-loaders/bundle-loader.js.map +1 -1
- package/dist/controller-loaders/npm-loader.d.ts.map +1 -1
- package/dist/controller-loaders/npm-loader.js +62 -10
- package/dist/controller-loaders/npm-loader.js.map +1 -1
- package/dist/controllers/resource-definition/resource-inherited-controller.d.ts.map +1 -1
- package/dist/controllers/resource-definition/resource-inherited-controller.js +57 -10
- package/dist/controllers/resource-definition/resource-inherited-controller.js.map +1 -1
- package/dist/controllers/type/json-schema-controller.d.ts +8 -0
- package/dist/controllers/type/json-schema-controller.d.ts.map +1 -0
- package/dist/controllers/type/json-schema-controller.js +91 -0
- package/dist/controllers/type/json-schema-controller.js.map +1 -0
- package/dist/evaluation-context.d.ts +5 -0
- package/dist/evaluation-context.d.ts.map +1 -1
- package/dist/evaluation-context.js +63 -33
- package/dist/evaluation-context.js.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/init-failure-diagnostics.d.ts +61 -0
- package/dist/init-failure-diagnostics.d.ts.map +1 -0
- package/dist/init-failure-diagnostics.js +141 -0
- package/dist/init-failure-diagnostics.js.map +1 -0
- package/dist/invocation-contract-binding.d.ts +105 -0
- package/dist/invocation-contract-binding.d.ts.map +1 -0
- package/dist/invocation-contract-binding.js +296 -0
- package/dist/invocation-contract-binding.js.map +1 -0
- package/dist/kernel.d.ts +17 -0
- package/dist/kernel.d.ts.map +1 -1
- package/dist/kernel.js +76 -25
- package/dist/kernel.js.map +1 -1
- package/dist/module-context.d.ts.map +1 -1
- package/dist/module-context.js +21 -0
- package/dist/module-context.js.map +1 -1
- package/dist/resource-context.d.ts +45 -0
- package/dist/resource-context.d.ts.map +1 -1
- package/dist/resource-context.js +79 -0
- package/dist/resource-context.js.map +1 -1
- package/dist/schema-compiled-values.d.ts +9 -1
- package/dist/schema-compiled-values.d.ts.map +1 -1
- package/dist/schema-compiled-values.js +55 -16
- package/dist/schema-compiled-values.js.map +1 -1
- package/dist/schema-validator.d.ts.map +1 -1
- package/dist/schema-validator.js +15 -1
- package/dist/schema-validator.js.map +1 -1
- package/dist/static-analysis-diagnostics.d.ts +19 -0
- package/dist/static-analysis-diagnostics.d.ts.map +1 -0
- package/dist/static-analysis-diagnostics.js +52 -0
- package/dist/static-analysis-diagnostics.js.map +1 -0
- package/dist/transports/oci/oci-transport.d.ts +17 -0
- package/dist/transports/oci/oci-transport.d.ts.map +1 -1
- package/dist/transports/oci/oci-transport.js +33 -8
- package/dist/transports/oci/oci-transport.js.map +1 -1
- package/dist/transports/transport-registry.d.ts +6 -4
- package/dist/transports/transport-registry.d.ts.map +1 -1
- package/dist/transports/transport-registry.js +6 -4
- package/dist/transports/transport-registry.js.map +1 -1
- package/package.json +4 -4
- package/src/bundle/module-artifact.ts +54 -12
- package/src/controller-loader.ts +7 -4
- package/src/controller-loaders/bundle-loader.ts +9 -4
- package/src/controller-loaders/npm-loader.ts +75 -13
- package/src/controllers/resource-definition/resource-inherited-controller.ts +69 -10
- package/src/controllers/type/json-schema-controller.ts +114 -0
- package/src/evaluation-context.ts +81 -32
- package/src/index.ts +3 -1
- package/src/init-failure-diagnostics.ts +169 -0
- package/src/invocation-contract-binding.ts +392 -0
- package/src/kernel.ts +95 -23
- package/src/module-context.ts +27 -0
- package/src/resource-context.ts +81 -0
- package/src/schema-compiled-values.ts +55 -15
- package/src/schema-validator.ts +15 -1
- package/src/static-analysis-diagnostics.ts +51 -0
- package/src/transports/oci/oci-transport.ts +34 -8
- package/src/transports/transport-registry.ts +6 -4
|
@@ -26,6 +26,17 @@ export interface MaterializedLayer {
|
|
|
26
26
|
files: string[];
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
+
/** A materialized controller layer plus whether reaching it cost a transfer.
|
|
30
|
+
*
|
|
31
|
+
* `transferred` is out of band rather than a field on `MaterializedLayer`
|
|
32
|
+
* because it describes THIS call, not the layer: the value is memoized and
|
|
33
|
+
* shared, so a flag on it would be meaningless to every other caller. Only the
|
|
34
|
+
* controller path reports progress, so only it asks. */
|
|
35
|
+
export interface ResolvedControllerLayer {
|
|
36
|
+
layer: MaterializedLayer;
|
|
37
|
+
transferred: boolean;
|
|
38
|
+
}
|
|
39
|
+
|
|
29
40
|
/**
|
|
30
41
|
* Map Node's platform vocabulary onto the canonical OCI/GOOS names selectors are
|
|
31
42
|
* published with. Node says `win32`/`x64`; OCI descriptors say
|
|
@@ -123,8 +134,11 @@ export class ModuleArtifact {
|
|
|
123
134
|
private readonly transports: TransportRegistry;
|
|
124
135
|
private readonly log: Logger;
|
|
125
136
|
/** In-flight / completed materializations, keyed by blob digest. Rejections
|
|
126
|
-
* are dropped so a transient fetch failure retries on the next ask.
|
|
127
|
-
|
|
137
|
+
* are dropped so a transient fetch failure retries on the next ask. Each
|
|
138
|
+
* records whether the work it wraps transferred the layer or read it off
|
|
139
|
+
* disk — the memoized entry is shared, so a joining caller reads the flag
|
|
140
|
+
* but never claims it. */
|
|
141
|
+
private readonly inFlight = new Map<string, Promise<ResolvedControllerLayer>>();
|
|
128
142
|
|
|
129
143
|
constructor(opts: {
|
|
130
144
|
pinnedRef: string;
|
|
@@ -165,14 +179,23 @@ export class ModuleArtifact {
|
|
|
165
179
|
* Returns `undefined` when the artifact ships no layer for this selector, which
|
|
166
180
|
* is how a loader learns to fall through to the next candidate.
|
|
167
181
|
*/
|
|
168
|
-
async materializeController(
|
|
182
|
+
async materializeController(
|
|
183
|
+
selector: ArtifactSelector,
|
|
184
|
+
): Promise<ResolvedControllerLayer | undefined> {
|
|
169
185
|
const key = selectorKey(selector);
|
|
170
186
|
const layer = this.layers.find(
|
|
171
187
|
(l) => l.role === "controller" && l.selector !== undefined && selectorKey(l.selector) === key,
|
|
172
188
|
);
|
|
173
189
|
if (!layer) return undefined;
|
|
174
|
-
await this.
|
|
175
|
-
|
|
190
|
+
const common = await this.materializeCommonTracked();
|
|
191
|
+
const controller = await this.materializeTracked(layer);
|
|
192
|
+
// Either half is a transfer the caller waited on: the common layer's bytes
|
|
193
|
+
// come down on this call too, so they are as much of a wait as the
|
|
194
|
+
// controller layer's.
|
|
195
|
+
return {
|
|
196
|
+
layer: controller.layer,
|
|
197
|
+
transferred: controller.transferred || (common?.transferred ?? false),
|
|
198
|
+
};
|
|
176
199
|
}
|
|
177
200
|
|
|
178
201
|
/**
|
|
@@ -197,8 +220,15 @@ export class ModuleArtifact {
|
|
|
197
220
|
|
|
198
221
|
/** Materialize the `common` layer, if the module ships one. */
|
|
199
222
|
async materializeCommon(): Promise<MaterializedLayer | undefined> {
|
|
223
|
+
return (await this.materializeCommonTracked())?.layer;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** As `materializeCommon`, reporting whether this call transferred it — the
|
|
227
|
+
* controller path rides the common layer along and has to count its bytes as
|
|
228
|
+
* part of the wait. One lookup, so "common comes too" is encoded once. */
|
|
229
|
+
private async materializeCommonTracked(): Promise<ResolvedControllerLayer | undefined> {
|
|
200
230
|
const layer = singletonLayer(this.layers, "common");
|
|
201
|
-
return layer ? this.
|
|
231
|
+
return layer ? this.materializeTracked(layer) : undefined;
|
|
202
232
|
}
|
|
203
233
|
|
|
204
234
|
/**
|
|
@@ -226,9 +256,21 @@ export class ModuleArtifact {
|
|
|
226
256
|
.join(", ");
|
|
227
257
|
}
|
|
228
258
|
|
|
229
|
-
private materialize(layer: ArtifactLayer): Promise<MaterializedLayer> {
|
|
259
|
+
private async materialize(layer: ArtifactLayer): Promise<MaterializedLayer> {
|
|
260
|
+
return (await this.materializeTracked(layer)).layer;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* As `materialize`, but also reporting whether THIS call transferred the layer.
|
|
265
|
+
*
|
|
266
|
+
* A caller that joins work already in flight reports `false`: several
|
|
267
|
+
* controller candidates of one module share a layer, and attributing the
|
|
268
|
+
* transfer to all of them would print one progress line per candidate for a
|
|
269
|
+
* single download. The call that started the work owns the report.
|
|
270
|
+
*/
|
|
271
|
+
private materializeTracked(layer: ArtifactLayer): Promise<ResolvedControllerLayer> {
|
|
230
272
|
const pending = this.inFlight.get(layer.blob);
|
|
231
|
-
if (pending) return pending;
|
|
273
|
+
if (pending) return pending.then((r) => ({ layer: r.layer, transferred: false }));
|
|
232
274
|
const work = this.materializeUncached(layer).catch((err) => {
|
|
233
275
|
// Drop the rejection so a transient fetch failure is retried rather than
|
|
234
276
|
// cached for the lifetime of the module.
|
|
@@ -247,10 +289,10 @@ export class ModuleArtifact {
|
|
|
247
289
|
return path.join(this.dir, `.telo-layer-${layer.role}-${short}`);
|
|
248
290
|
}
|
|
249
291
|
|
|
250
|
-
private async materializeUncached(layer: ArtifactLayer): Promise<
|
|
292
|
+
private async materializeUncached(layer: ArtifactLayer): Promise<ResolvedControllerLayer> {
|
|
251
293
|
const marker = this.markerPath(layer);
|
|
252
294
|
if (existsSync(marker)) {
|
|
253
|
-
return { dir: this.dir, files: await readMarker(marker) };
|
|
295
|
+
return { layer: { dir: this.dir, files: await readMarker(marker) }, transferred: false };
|
|
254
296
|
}
|
|
255
297
|
|
|
256
298
|
return withDirectoryLock(
|
|
@@ -260,7 +302,7 @@ export class ModuleArtifact {
|
|
|
260
302
|
// Re-check inside the lock: a peer may have extracted this layer between
|
|
261
303
|
// the fast-path miss and our acquisition.
|
|
262
304
|
if (existsSync(marker)) {
|
|
263
|
-
return { dir: this.dir, files: await readMarker(marker) };
|
|
305
|
+
return { layer: { dir: this.dir, files: await readMarker(marker) }, transferred: false };
|
|
264
306
|
}
|
|
265
307
|
|
|
266
308
|
const files = await this.transports.fetchLayer(this.pinnedRef, layer.blob);
|
|
@@ -283,7 +325,7 @@ export class ModuleArtifact {
|
|
|
283
325
|
"telo.layer.role": layer.role,
|
|
284
326
|
"telo.layer.files": written.length,
|
|
285
327
|
});
|
|
286
|
-
return { dir: this.dir, files: written };
|
|
328
|
+
return { layer: { dir: this.dir, files: written }, transferred: true };
|
|
287
329
|
},
|
|
288
330
|
this.log,
|
|
289
331
|
);
|
package/src/controller-loader.ts
CHANGED
|
@@ -8,10 +8,13 @@ import { ControllerPolicy, DEFAULT_POLICY, POLICY_WILDCARD } from "./runtime-reg
|
|
|
8
8
|
export type { ControllerPolicy } from "./runtime-registry.js";
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
|
-
* Which branch the per-scheme loader actually took. Cache/local hits resolve
|
|
12
|
-
*
|
|
13
|
-
* do real (network or compile) work
|
|
14
|
-
*
|
|
11
|
+
* Which branch the per-scheme loader actually took. Cache/local hits resolve in
|
|
12
|
+
* milliseconds; `npm-install`, `cargo-build` and `bundle` are the branches that
|
|
13
|
+
* do real (network or compile) work — `bundle` is reported only when the resolve
|
|
14
|
+
* fetched the module's controller layer, never when it found it already
|
|
15
|
+
* extracted. The CLI uses this to decide whether a "downloading…" line was
|
|
16
|
+
* honest or should be erased, so a source that names work no one waited for
|
|
17
|
+
* turns every warm start into noise.
|
|
15
18
|
*/
|
|
16
19
|
export type ControllerResolveSource =
|
|
17
20
|
| "local"
|
|
@@ -220,17 +220,22 @@ export class BundleControllerLoader {
|
|
|
220
220
|
// fetched here: the artifact handle owns the pinned ref and the verified
|
|
221
221
|
// layer index, so an `oci://` module ref never reaches this loader as a path.
|
|
222
222
|
let bundleDir: string;
|
|
223
|
+
// What this resolve actually cost. A module already on disk is `local`; an
|
|
224
|
+
// artifact layer found extracted is `cache`; only a layer this call pulled
|
|
225
|
+
// down reports `bundle`, the branch that made the user wait.
|
|
226
|
+
let source: ControllerResolveSource = "local";
|
|
223
227
|
if (artifact) {
|
|
224
228
|
// By its own selector, not by re-matching the host: this candidate IS one
|
|
225
229
|
// selector, and it is exactly the key of the layer that carries it.
|
|
226
|
-
const
|
|
227
|
-
if (!
|
|
230
|
+
const resolved = await artifact.materializeController(selector);
|
|
231
|
+
if (!resolved) {
|
|
228
232
|
throw new ControllerEnvMissingError(
|
|
229
233
|
`pkg:telo controller "${purl}": the module artifact ships no layer for ` +
|
|
230
234
|
`${describeSelector(selector)} (has: ${artifact.describeLayers()})`,
|
|
231
235
|
);
|
|
232
236
|
}
|
|
233
|
-
bundleDir = layer.dir;
|
|
237
|
+
bundleDir = resolved.layer.dir;
|
|
238
|
+
source = resolved.transferred ? "bundle" : "cache";
|
|
234
239
|
} else {
|
|
235
240
|
// No artifact: a module already on disk (local development, or a manifest
|
|
236
241
|
// served from the on-disk cache). Its files sit next to the manifest.
|
|
@@ -258,7 +263,7 @@ export class BundleControllerLoader {
|
|
|
258
263
|
|
|
259
264
|
const fragment = parsed.subpath;
|
|
260
265
|
return {
|
|
261
|
-
source
|
|
266
|
+
source,
|
|
262
267
|
importInstance: async () => {
|
|
263
268
|
// A broken bundle (syntax / failed import) is a real user-code failure —
|
|
264
269
|
// let it propagate rather than masking it as env-missing.
|
|
@@ -149,7 +149,7 @@ export class NpmControllerLoader {
|
|
|
149
149
|
* same spec share one `npm install <spec>` invocation rather than each
|
|
150
150
|
* acquiring the fs-lock and reinstalling.
|
|
151
151
|
*/
|
|
152
|
-
private readonly inFlight = new Map<string, Promise<
|
|
152
|
+
private readonly inFlight = new Map<string, Promise<boolean>>();
|
|
153
153
|
|
|
154
154
|
/**
|
|
155
155
|
* Promise that resolves when the install root has been materialized
|
|
@@ -264,15 +264,20 @@ export class NpmControllerLoader {
|
|
|
264
264
|
dependencies[name] = `file:${resolvedPkgRoot}`;
|
|
265
265
|
}
|
|
266
266
|
|
|
267
|
-
const packageJson = {
|
|
268
|
-
name: "telo-runtime-install",
|
|
269
|
-
private: true,
|
|
270
|
-
version: "0.0.0",
|
|
271
|
-
dependencies,
|
|
272
|
-
};
|
|
273
267
|
const packageJsonPath = path.join(installRoot, "package.json");
|
|
274
268
|
const stateFile = path.join(installRoot, ".telo-state.json");
|
|
275
|
-
|
|
269
|
+
// Keyed on the realm deps alone — the only part this function owns. The
|
|
270
|
+
// controller aliases below join the file but never the identity: they are
|
|
271
|
+
// added one `--save` at a time, so hashing them would make every controller
|
|
272
|
+
// install invalidate the root and trigger another root install.
|
|
273
|
+
const newHash = sha256(
|
|
274
|
+
JSON.stringify({
|
|
275
|
+
name: "telo-runtime-install",
|
|
276
|
+
private: true,
|
|
277
|
+
version: "0.0.0",
|
|
278
|
+
dependencies,
|
|
279
|
+
}),
|
|
280
|
+
);
|
|
276
281
|
|
|
277
282
|
await fs.mkdir(installRoot, { recursive: true });
|
|
278
283
|
|
|
@@ -301,7 +306,25 @@ export class NpmControllerLoader {
|
|
|
301
306
|
return;
|
|
302
307
|
}
|
|
303
308
|
|
|
304
|
-
|
|
309
|
+
// Rewrite the root's package.json REALM DEPS ONLY, keeping every
|
|
310
|
+
// controller alias a previous `installPackage --save` recorded. Writing a
|
|
311
|
+
// fresh file instead drops them, and the install below then prunes their
|
|
312
|
+
// `node_modules` folders — so a realm change (a different kernel checkout,
|
|
313
|
+
// e.g. a globally installed CLI and a repo one addressing the same app)
|
|
314
|
+
// evicted every controller and reinstalled the lot, on every switch.
|
|
315
|
+
const existingDeps = (await readPackageDeps(installRoot)) ?? {};
|
|
316
|
+
const merged: Record<string, string> = {
|
|
317
|
+
...(await liveControllerDeps(existingDeps, installRoot)),
|
|
318
|
+
...dependencies,
|
|
319
|
+
};
|
|
320
|
+
await fs.writeFile(
|
|
321
|
+
packageJsonPath,
|
|
322
|
+
JSON.stringify(
|
|
323
|
+
{ name: "telo-runtime-install", private: true, version: "0.0.0", dependencies: merged },
|
|
324
|
+
null,
|
|
325
|
+
2,
|
|
326
|
+
) + "\n",
|
|
327
|
+
);
|
|
305
328
|
await runPackageManager(installRoot, [
|
|
306
329
|
"install",
|
|
307
330
|
"--no-audit",
|
|
@@ -414,8 +437,11 @@ export class NpmControllerLoader {
|
|
|
414
437
|
}
|
|
415
438
|
}
|
|
416
439
|
|
|
440
|
+
// Resolves to whether the package manager actually ran: the re-check below
|
|
441
|
+
// routinely wins the race, and reporting an install that never happened
|
|
442
|
+
// makes the CLI print a "downloading…" line nobody waited for.
|
|
417
443
|
const work = (async () => {
|
|
418
|
-
|
|
444
|
+
return withDirectoryLock(installRoot, "controller install", async () => {
|
|
419
445
|
// Re-check inside the lock: a peer process may have installed the
|
|
420
446
|
// spec between the fast-path miss and our acquisition.
|
|
421
447
|
if (kind === "registry") {
|
|
@@ -424,7 +450,7 @@ export class NpmControllerLoader {
|
|
|
424
450
|
installedVersion !== null &&
|
|
425
451
|
(requestedVersion === null || requestedVersion === installedVersion)
|
|
426
452
|
) {
|
|
427
|
-
return;
|
|
453
|
+
return false;
|
|
428
454
|
}
|
|
429
455
|
} else {
|
|
430
456
|
// Normalize the on-disk record to absolute form before comparing —
|
|
@@ -436,7 +462,7 @@ export class NpmControllerLoader {
|
|
|
436
462
|
(await pathExists(targetPath))
|
|
437
463
|
) {
|
|
438
464
|
this.rootDeps[alias] = lockedSpec;
|
|
439
|
-
return;
|
|
465
|
+
return false;
|
|
440
466
|
}
|
|
441
467
|
}
|
|
442
468
|
|
|
@@ -458,16 +484,21 @@ export class NpmControllerLoader {
|
|
|
458
484
|
const written = await readDepSpec(installRoot, alias);
|
|
459
485
|
if (written !== undefined) this.rootDeps[alias] = written;
|
|
460
486
|
else this.rootDeps[alias] = spec;
|
|
487
|
+
return true;
|
|
461
488
|
}, this.log);
|
|
462
489
|
})();
|
|
463
490
|
|
|
464
491
|
this.inFlight.set(cacheKey, work);
|
|
492
|
+
let installed: boolean;
|
|
465
493
|
try {
|
|
466
|
-
await work;
|
|
494
|
+
installed = await work;
|
|
467
495
|
} finally {
|
|
468
496
|
this.inFlight.delete(cacheKey);
|
|
469
497
|
}
|
|
470
498
|
this.installedSpecs.add(cacheKey);
|
|
499
|
+
// A peer that won the lock had already put the package there, so nothing
|
|
500
|
+
// was installed — that is a cache hit, whatever the fast path thought.
|
|
501
|
+
if (!installed) return "cache";
|
|
471
502
|
// First-touch local installs report `local` (silenced in CLI progress);
|
|
472
503
|
// fresh registry installs report `npm-install` (the only branch a
|
|
473
504
|
// user-facing "downloading…" line should ever surface for).
|
|
@@ -693,6 +724,37 @@ function normalizeFileSpec(spec: string, installRoot: string): string {
|
|
|
693
724
|
* Returns null when the file is missing or unreadable; callers decide whether
|
|
694
725
|
* to seed from a synthesized map instead.
|
|
695
726
|
*/
|
|
727
|
+
/**
|
|
728
|
+
* The subset of a root's recorded dependencies still worth carrying into a
|
|
729
|
+
* rewrite: the package manager has to resolve every entry, so one dead record
|
|
730
|
+
* fails the whole install.
|
|
731
|
+
*
|
|
732
|
+
* Dropped: a `file:` spec whose target no longer exists (the app was run from a
|
|
733
|
+
* checkout that has since moved, or the app directory was copied without it),
|
|
734
|
+
* and an alias with no folder in `node_modules`. Both are free to drop —
|
|
735
|
+
* `installPackage` reinstalls a controller the moment something asks for it —
|
|
736
|
+
* and keeping them is not: a dangling `file:` dep makes the root install fail
|
|
737
|
+
* with an ENOENT that names nothing in the manifest, and since the state file is
|
|
738
|
+
* only written after a successful install, every later run repeats it. This is
|
|
739
|
+
* also what stops aliases accumulating forever now that the rewrite preserves
|
|
740
|
+
* them: a version an app has moved off drops out on the next root install.
|
|
741
|
+
*/
|
|
742
|
+
async function liveControllerDeps(
|
|
743
|
+
deps: Record<string, string>,
|
|
744
|
+
installRoot: string,
|
|
745
|
+
): Promise<Record<string, string>> {
|
|
746
|
+
const live: Record<string, string> = {};
|
|
747
|
+
for (const [name, spec] of Object.entries(deps)) {
|
|
748
|
+
if (spec.startsWith("file:")) {
|
|
749
|
+
const target = normalizeFileSpec(spec, installRoot).slice("file:".length);
|
|
750
|
+
if (!(await pathExists(target))) continue;
|
|
751
|
+
}
|
|
752
|
+
if (!(await pathExists(path.join(installRoot, "node_modules", name)))) continue;
|
|
753
|
+
live[name] = spec;
|
|
754
|
+
}
|
|
755
|
+
return live;
|
|
756
|
+
}
|
|
757
|
+
|
|
696
758
|
async function readPackageDeps(installRoot: string): Promise<Record<string, string> | null> {
|
|
697
759
|
try {
|
|
698
760
|
const text = await fs.readFile(path.join(installRoot, "package.json"), "utf8");
|
|
@@ -66,33 +66,89 @@ function resolveRefSlot(value: unknown, ctx: ResourceContext): ResourceInstance
|
|
|
66
66
|
return instance ?? undefined;
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
-
/** Expand a
|
|
70
|
-
* a pure `self.<path>` access is navigated directly so live
|
|
71
|
-
* through.
|
|
72
|
-
|
|
69
|
+
/** Expand a mapping node against a CEL scope. `self`-only CEL resolves to
|
|
70
|
+
* literals now; a pure `self.<path>` access is navigated directly so live
|
|
71
|
+
* instances pass through (CEL's output type checker rejects them). Shared by
|
|
72
|
+
* `base:` (construction, scope `{ self }`) and the dispatch mappings
|
|
73
|
+
* `inputs:` / `result:` (scope `{ self, inputs }` / `{ self, result }`). */
|
|
74
|
+
function expandMappingNode(
|
|
75
|
+
value: unknown,
|
|
76
|
+
scope: Record<string, unknown>,
|
|
77
|
+
ctx: EvaluationContext,
|
|
78
|
+
): unknown {
|
|
73
79
|
if (isCompiledValue(value)) {
|
|
74
80
|
const src = typeof value.source === "string" ? value.source.trim() : "";
|
|
75
81
|
const m = src.match(SELF_PATH);
|
|
76
82
|
if (m) {
|
|
77
|
-
let cur: unknown = self;
|
|
83
|
+
let cur: unknown = scope.self;
|
|
78
84
|
for (const key of m[1].split(".").slice(1)) {
|
|
79
85
|
cur = (cur as Record<string, unknown> | undefined)?.[key];
|
|
80
86
|
}
|
|
81
87
|
return cur;
|
|
82
88
|
}
|
|
83
|
-
return ctx.expandWith(value,
|
|
89
|
+
return ctx.expandWith(value, scope);
|
|
84
90
|
}
|
|
85
|
-
if (Array.isArray(value)) return value.map((v) =>
|
|
91
|
+
if (Array.isArray(value)) return value.map((v) => expandMappingNode(v, scope, ctx));
|
|
86
92
|
if (value !== null && typeof value === "object" && Object.getPrototypeOf(value) === Object.prototype) {
|
|
87
93
|
const out: Record<string, unknown> = {};
|
|
88
94
|
for (const [k, v] of Object.entries(value as Record<string, unknown>)) {
|
|
89
|
-
out[k] =
|
|
95
|
+
out[k] = expandMappingNode(v, scope, ctx);
|
|
90
96
|
}
|
|
91
97
|
return out;
|
|
92
98
|
}
|
|
93
99
|
return value;
|
|
94
100
|
}
|
|
95
101
|
|
|
102
|
+
/**
|
|
103
|
+
* Bind the dispatch mappings of a child that REPLACED its inherited contract.
|
|
104
|
+
*
|
|
105
|
+
* A contract resolves to the nearest declaration along `extends` and never
|
|
106
|
+
* merges, so a child declaring `inputType` presents a signature the inherited
|
|
107
|
+
* controller has never heard of. `inputs:` is the adapter that turns the child's
|
|
108
|
+
* signature into the parent's call, and `result:` turns the parent's result back
|
|
109
|
+
* into the child's declared output — the same top-level-sibling factoring a
|
|
110
|
+
* template definition uses for `invoke:`.
|
|
111
|
+
*
|
|
112
|
+
* Bound onto the parent instance rather than wrapping it, so the child still IS
|
|
113
|
+
* a parent instance: `init`, `snapshot`, `teardown` and status plumbing are
|
|
114
|
+
* untouched, and nothing new appears in a declared ref slot. It composes with
|
|
115
|
+
* the kernel's contract binding by position — the parent's contract was bound
|
|
116
|
+
* when the parent instance was produced, this mapping goes on next, and the
|
|
117
|
+
* child's own contract is bound outermost by the caller. One dispatch therefore
|
|
118
|
+
* checks the child's inputs, maps, checks the parent's inputs, runs, checks the
|
|
119
|
+
* parent's result, maps back, and checks the child's result.
|
|
120
|
+
*/
|
|
121
|
+
function bindDispatchMapping(
|
|
122
|
+
instance: ResourceInstance,
|
|
123
|
+
definition: ResourceDefinition,
|
|
124
|
+
self: Record<string, unknown>,
|
|
125
|
+
ctx: EvaluationContext,
|
|
126
|
+
): void {
|
|
127
|
+
const body = definition as unknown as { inputs?: unknown; result?: unknown };
|
|
128
|
+
const inputsMapping = body.inputs;
|
|
129
|
+
const resultMapping = body.result;
|
|
130
|
+
if (inputsMapping == null && resultMapping == null) return;
|
|
131
|
+
if (typeof instance.invoke !== "function") return;
|
|
132
|
+
|
|
133
|
+
// Every argument is forwarded — the mapping rewrites `inputs`, while the
|
|
134
|
+
// InvokeContext (cancellation, tracing) in later parameters belongs to the
|
|
135
|
+
// caller and must reach the inherited controller untouched.
|
|
136
|
+
const original = instance.invoke.bind(instance) as (
|
|
137
|
+
inputs: any,
|
|
138
|
+
...rest: unknown[]
|
|
139
|
+
) => Promise<unknown>;
|
|
140
|
+
instance.invoke = async (inputs: any, ...rest: unknown[]) => {
|
|
141
|
+
const mappedInputs =
|
|
142
|
+
inputsMapping != null
|
|
143
|
+
? (expandMappingNode(inputsMapping, { self, inputs }, ctx) as Record<string, unknown>)
|
|
144
|
+
: inputs;
|
|
145
|
+
const result = await original(mappedInputs, ...rest);
|
|
146
|
+
return resultMapping != null
|
|
147
|
+
? expandMappingNode(resultMapping, { self, result }, ctx)
|
|
148
|
+
: result;
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
|
|
96
152
|
/**
|
|
97
153
|
* Controller for a definition that inherits its controller by delegation — it
|
|
98
154
|
* `extends` a concrete kind, declares no own `controllers:` / template body, and
|
|
@@ -143,7 +199,7 @@ export function createInheritedController(
|
|
|
143
199
|
// instances in `self`), minus the reserved keys.
|
|
144
200
|
let parentConfig: Record<string, unknown>;
|
|
145
201
|
if (base != null) {
|
|
146
|
-
parentConfig =
|
|
202
|
+
parentConfig = expandMappingNode(base, { self }, definingContext) as Record<string, unknown>;
|
|
147
203
|
} else {
|
|
148
204
|
const { kind: _kind, metadata: _metadata, name: _name, ...config } = self;
|
|
149
205
|
parentConfig = config;
|
|
@@ -159,7 +215,10 @@ export function createInheritedController(
|
|
|
159
215
|
`Telo.Definition '${definition.metadata.name}': inherited controller requires a ResourceContext host that implements createInheritedInstance().`,
|
|
160
216
|
);
|
|
161
217
|
}
|
|
162
|
-
|
|
218
|
+
const instance = await host.createInheritedInstance(definingContext, parentResource);
|
|
219
|
+
if (!instance) return null;
|
|
220
|
+
bindDispatchMapping(instance, definition, self, definingContext);
|
|
221
|
+
return instance;
|
|
163
222
|
},
|
|
164
223
|
};
|
|
165
224
|
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { evaluate } from "@marcbachmann/cel-js";
|
|
2
|
+
import type {
|
|
3
|
+
ResourceContext,
|
|
4
|
+
ResourceInstance,
|
|
5
|
+
ResourceManifest,
|
|
6
|
+
TypeRule,
|
|
7
|
+
} from "@telorun/sdk";
|
|
8
|
+
import { canonicalTypeSchemaId, mergeTypeSchemas, RuntimeError } from "@telorun/sdk";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* `Telo.JsonSchema` — a named data shape.
|
|
12
|
+
*
|
|
13
|
+
* It lives in the kernel rather than in an installable module for the same
|
|
14
|
+
* reason the mandatory log sinks do: declaring a shape is not optional. Every
|
|
15
|
+
* kind that carries an invocation contract needs one, so requiring an import to
|
|
16
|
+
* write `inputType:` would tax the exact thing contracts want authors to do —
|
|
17
|
+
* and a library declaring its own contract would have to import a module purely
|
|
18
|
+
* to describe itself.
|
|
19
|
+
*
|
|
20
|
+
* `type.JsonSchema` is retained as a deprecated alias with the same behaviour, so
|
|
21
|
+
* published manifests keep resolving.
|
|
22
|
+
*/
|
|
23
|
+
class JsonSchemaType {
|
|
24
|
+
constructor(
|
|
25
|
+
private readonly qualifiedName: string,
|
|
26
|
+
private readonly rules: TypeRule[],
|
|
27
|
+
/** The fully-resolved (post-`extends`), self-contained JSON Schema. Read by
|
|
28
|
+
* consumers that need the effective shape — e.g. a templated resource
|
|
29
|
+
* threading `${{ self.model.schema }}` into a request validation schema. */
|
|
30
|
+
readonly schema: Record<string, unknown>,
|
|
31
|
+
) {}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Validate data against this type's CEL rules — the invariant layer on top of
|
|
35
|
+
* the schema itself, which AJV enforces through the schema registry.
|
|
36
|
+
*/
|
|
37
|
+
validateRules(data: unknown): void {
|
|
38
|
+
for (const rule of this.rules) {
|
|
39
|
+
let result: unknown;
|
|
40
|
+
try {
|
|
41
|
+
result = evaluate(rule.condition, { this: data });
|
|
42
|
+
} catch (err) {
|
|
43
|
+
throw new RuntimeError(
|
|
44
|
+
"ERR_TYPE_VALIDATION_FAILED",
|
|
45
|
+
`Type "${this.qualifiedName}" rule evaluation failed: ${err instanceof Error ? err.message : String(err)}`,
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
if (result !== true) {
|
|
49
|
+
throw new RuntimeError(
|
|
50
|
+
rule.code,
|
|
51
|
+
rule.message ??
|
|
52
|
+
`Type "${this.qualifiedName}" validation failed: rule "${rule.code}" not satisfied`,
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** A type resource is pure declaration: it registers a schema and holds the
|
|
60
|
+
* resolved shape, implementing none of the lifecycle verbs. `ResourceInstance`
|
|
61
|
+
* is entirely optional members, so a class with none of them satisfies it only
|
|
62
|
+
* by assertion — the same shape the module-loaded controller had, where the
|
|
63
|
+
* dynamic-import boundary erased the type instead of asserting it. */
|
|
64
|
+
export async function create(
|
|
65
|
+
resource: ResourceManifest,
|
|
66
|
+
ctx: ResourceContext,
|
|
67
|
+
): Promise<ResourceInstance | null> {
|
|
68
|
+
const qualifiedName = `${resource.metadata.module}.${resource.metadata.name}`;
|
|
69
|
+
const ownSchema = resource.schema as Record<string, unknown>;
|
|
70
|
+
|
|
71
|
+
let schema: Record<string, unknown> = ownSchema;
|
|
72
|
+
|
|
73
|
+
const extendsField = resource.extends as string | string[] | undefined;
|
|
74
|
+
if (extendsField) {
|
|
75
|
+
const parents = Array.isArray(extendsField) ? extendsField : [extendsField];
|
|
76
|
+
|
|
77
|
+
const parentSchemas: Record<string, unknown>[] = [];
|
|
78
|
+
for (const parent of parents) {
|
|
79
|
+
const parentSchema = ctx.lookupSchema(parent);
|
|
80
|
+
// Defer if any parent schema isn't registered yet (multi-pass resolution).
|
|
81
|
+
if (!parentSchema) return null;
|
|
82
|
+
parentSchemas.push(parentSchema as Record<string, unknown>);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Each parent's registered schema is itself already resolved, so merging
|
|
86
|
+
// them makes inheritance transitive through grandparents with no `$ref`s
|
|
87
|
+
// left in the result.
|
|
88
|
+
schema = mergeTypeSchemas([...parentSchemas, ownSchema]);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const rules = (Array.isArray(resource.rules) ? resource.rules : []) as TypeRule[];
|
|
92
|
+
|
|
93
|
+
ctx.registerSchema(qualifiedName, schema);
|
|
94
|
+
ctx.registerTypeRules(qualifiedName, rules);
|
|
95
|
+
|
|
96
|
+
// Also register under the short name so types can be referenced without a
|
|
97
|
+
// module prefix.
|
|
98
|
+
const shortName = resource.metadata.name;
|
|
99
|
+
if (shortName !== qualifiedName) {
|
|
100
|
+
ctx.registerSchema(shortName, schema);
|
|
101
|
+
ctx.registerTypeRules(shortName, rules);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Canonical module-scoped id — the target of `$ref: "telo://Self/<name>"` (and
|
|
105
|
+
// `telo://<Alias>/<name>` across imports) once the loader resolves the
|
|
106
|
+
// authority to this module. Authority-free, so a validator can actually
|
|
107
|
+
// resolve the reference; see `canonicalTypeSchemaId`.
|
|
108
|
+
const moduleName = resource.metadata.module as string | undefined;
|
|
109
|
+
if (moduleName) {
|
|
110
|
+
ctx.registerSchema(canonicalTypeSchemaId(moduleName, shortName), schema);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return new JsonSchemaType(qualifiedName, rules, schema) as unknown as ResourceInstance;
|
|
114
|
+
}
|