@telorun/analyzer 0.12.1 → 1.0.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 (51) hide show
  1. package/dist/analysis-registry.d.ts +13 -0
  2. package/dist/analysis-registry.d.ts.map +1 -1
  3. package/dist/analysis-registry.js +15 -0
  4. package/dist/analyzer.d.ts.map +1 -1
  5. package/dist/analyzer.js +154 -83
  6. package/dist/builtins.d.ts.map +1 -1
  7. package/dist/builtins.js +85 -0
  8. package/dist/cel-environment.d.ts +1 -1
  9. package/dist/cel-environment.d.ts.map +1 -1
  10. package/dist/cel-environment.js +40 -2
  11. package/dist/dependency-graph.d.ts.map +1 -1
  12. package/dist/dependency-graph.js +41 -62
  13. package/dist/index.d.ts +2 -0
  14. package/dist/index.d.ts.map +1 -1
  15. package/dist/index.js +1 -0
  16. package/dist/kernel-globals.d.ts +1 -1
  17. package/dist/kernel-globals.d.ts.map +1 -1
  18. package/dist/kernel-globals.js +19 -1
  19. package/dist/manifest-visitor.d.ts +124 -0
  20. package/dist/manifest-visitor.d.ts.map +1 -0
  21. package/dist/manifest-visitor.js +181 -0
  22. package/dist/reference-field-map.js +16 -0
  23. package/dist/resolve-throws-union.d.ts +10 -0
  24. package/dist/resolve-throws-union.d.ts.map +1 -1
  25. package/dist/resolve-throws-union.js +35 -7
  26. package/dist/schema-compat.d.ts +10 -0
  27. package/dist/schema-compat.d.ts.map +1 -1
  28. package/dist/schema-compat.js +32 -0
  29. package/dist/validate-cel-context.d.ts +14 -0
  30. package/dist/validate-cel-context.d.ts.map +1 -1
  31. package/dist/validate-cel-context.js +38 -0
  32. package/dist/validate-references.d.ts.map +1 -1
  33. package/dist/validate-references.js +124 -160
  34. package/dist/validate-unused-declarations.d.ts +25 -0
  35. package/dist/validate-unused-declarations.d.ts.map +1 -0
  36. package/dist/validate-unused-declarations.js +91 -0
  37. package/package.json +3 -3
  38. package/src/analysis-registry.ts +20 -0
  39. package/src/analyzer.ts +256 -168
  40. package/src/builtins.ts +85 -0
  41. package/src/cel-environment.ts +42 -1
  42. package/src/dependency-graph.ts +37 -52
  43. package/src/index.ts +11 -0
  44. package/src/kernel-globals.ts +22 -1
  45. package/src/manifest-visitor.ts +340 -0
  46. package/src/reference-field-map.ts +14 -0
  47. package/src/resolve-throws-union.ts +36 -8
  48. package/src/schema-compat.ts +32 -0
  49. package/src/validate-cel-context.ts +50 -0
  50. package/src/validate-references.ts +175 -211
  51. package/src/validate-unused-declarations.ts +95 -0
@@ -1,6 +1,13 @@
1
1
  import { Environment } from "@marcbachmann/cel-js";
2
2
  import type { ResourceManifest } from "@telorun/sdk";
3
- import { jsonSchemaToCelType } from "./schema-compat.js";
3
+ import { jsonSchemaToCelType, VALUE_BRAND_BASE } from "./schema-compat.js";
4
+
5
+ /** Transport protocol on a `ports` entry → the nominal CEL brand its resolved
6
+ * value carries. Mirrors the `protocol` enum in the Application schema. */
7
+ const PORT_PROTOCOL_BRAND: Record<string, string> = {
8
+ tcp: "TcpPort",
9
+ udp: "UdpPort",
10
+ };
4
11
 
5
12
  export { buildCelEnvironment } from "@telorun/templating";
6
13
  export type { CelHandlers } from "@telorun/templating";
@@ -19,10 +26,23 @@ export function buildTypedCelEnvironment(
19
26
  baseEnv: Environment,
20
27
  manifest: ResourceManifest,
21
28
  extraContextSchema?: Record<string, any> | null,
29
+ // The `ports` namespace is Application-only and lives on the module doc, not
30
+ // on the resource being analyzed. When validating a resource, the caller
31
+ // passes the module manifest here so `${{ ports.X }}` types cross-doc.
32
+ rootModuleManifest?: ResourceManifest,
22
33
  ): Environment {
23
34
  try {
24
35
  const env = baseEnv.clone();
25
36
 
37
+ // Register nominal value brands (TcpPort/UdpPort/…) on the *clone* so the
38
+ // type-checker can distinguish structurally-identical values. The base env
39
+ // (shared with the kernel runtime) is untouched — a branded value flows as
40
+ // a plain integer at runtime, so only static checking needs these. cel-js
41
+ // auto-generates a field-less wrapper class; no runtime constructor needed.
42
+ for (const brand of Object.keys(VALUE_BRAND_BASE)) {
43
+ (env as any).registerType(brand, { fields: {} });
44
+ }
45
+
26
46
  // Build typed ObjectSchema from manifest.variables if it looks like a schema map
27
47
  const vars = (manifest as Record<string, unknown>).variables;
28
48
  if (vars !== null && typeof vars === "object" && !Array.isArray(vars)) {
@@ -42,6 +62,27 @@ export function buildTypedCelEnvironment(
42
62
  env.registerVariable("variables", "map");
43
63
  }
44
64
 
65
+ // `ports` namespace: each entry types as the brand its `protocol` selects
66
+ // (tcp → TcpPort, udp → UdpPort), so `${{ ports.http }}` carries a nominal
67
+ // type that consuming fields can check against.
68
+ const portsManifest = ((rootModuleManifest ?? manifest) as Record<string, unknown>).ports;
69
+ if (portsManifest !== null && typeof portsManifest === "object" && !Array.isArray(portsManifest)) {
70
+ const portEntries = Object.entries(portsManifest as Record<string, any>).filter(
71
+ ([, v]) => v !== null && typeof v === "object" && !Array.isArray(v),
72
+ );
73
+ if (portEntries.length > 0) {
74
+ const schema: Record<string, string> = {};
75
+ for (const [k, v] of portEntries) {
76
+ schema[k] = PORT_PROTOCOL_BRAND[(v as { protocol?: string }).protocol ?? "tcp"] ?? "int";
77
+ }
78
+ (env as any).registerVariable({ name: "ports", schema });
79
+ } else {
80
+ env.registerVariable("ports", "map");
81
+ }
82
+ } else {
83
+ env.registerVariable("ports", "map");
84
+ }
85
+
45
86
  env.registerVariable("secrets", "map");
46
87
  env.registerVariable("resources", "map");
47
88
  env.registerVariable("env", "map");
@@ -2,7 +2,7 @@ import type { ResourceManifest } from "@telorun/sdk";
2
2
  import { isRefSentinel } from "@telorun/templating";
3
3
  import type { AliasResolver } from "./alias-resolver.js";
4
4
  import type { DefinitionRegistry } from "./definition-registry.js";
5
- import { isRefEntry, isScopeEntry, resolveFieldValues } from "./reference-field-map.js";
5
+ import { visitManifest } from "./manifest-visitor.js";
6
6
  import { DEPENDENCY_GRAPH_SKIP_KINDS as SYSTEM_KINDS } from "./system-kinds.js";
7
7
 
8
8
  export interface ResourceNode {
@@ -57,64 +57,49 @@ export function buildDependencyGraph(
57
57
  const deps = new Map<string, Set<string>>();
58
58
  for (const key of nodes.keys()) deps.set(key, new Set());
59
59
 
60
- for (const r of resources) {
61
- if (!r.metadata?.name || !r.kind || SYSTEM_KINDS.has(r.kind)) continue;
62
-
63
- const sourceKey = nodeKey(r.kind, r.metadata.name as string);
64
- // Use the expanded map so refs nested behind x-telo-schema-from contribute
65
- // edges to the DAG. Without these, a parent (e.g. Http.Server) can init
66
- // before its extracted encoder and Phase 5 injection fires against a
67
- // not-yet-created dependency.
68
- const fieldMap =
69
- aliases && aliasesByModule
70
- ? registry.expandedFieldMapForResource(r, aliases, aliasesByModule)
71
- : registry.getFieldMapForKind(r.kind, aliases);
72
- if (!fieldMap) continue;
73
-
74
- // Collect names of resources declared inside scope fields — these are initialized
75
- // on-demand at runtime, not at boot, so edges pointing to them are excluded from the DAG.
76
- const scopedNames = new Set<string>();
77
- for (const [scopeFieldPath, entry] of fieldMap) {
78
- if (!isScopeEntry(entry)) continue;
79
- const scopeVal = (r as Record<string, unknown>)[scopeFieldPath];
80
- if (!Array.isArray(scopeVal)) continue;
81
- for (const item of scopeVal) {
82
- const name = (item as any)?.metadata?.name;
83
- if (typeof name === "string") scopedNames.add(name);
84
- }
85
- }
86
-
87
- for (const [fieldPath, entry] of fieldMap) {
88
- if (!isRefEntry(entry)) continue;
89
-
90
- for (const val of resolveFieldValues(r, fieldPath)) {
91
- if (!val) continue;
92
-
93
- // `!ref <name>` sentinel — look up the target's kind from the
94
- // name (resources are unique by name) so the edge carries the
95
- // concrete kind, matching the {kind, name} edge shape below.
60
+ // Names of resources declared inside the *current* resource's scope fields —
61
+ // initialized on-demand at runtime, not at boot, so edges pointing to them
62
+ // are excluded. Scoping is per-source-resource: an edge A → B is dropped only
63
+ // when B is declared inside A's own scope (the visitor's ScopeBoundary fires
64
+ // before that resource's RefSites, so this is set before any edge is added).
65
+ let scopedNames = new Set<string>();
66
+
67
+ // Expanded map so refs nested behind x-telo-schema-from contribute edges to
68
+ // the DAG. Without these, a parent (e.g. Http.Server) can init before its
69
+ // extracted encoder and Phase 5 injection fires against a not-yet-created
70
+ // dependency.
71
+ visitManifest(
72
+ resources,
73
+ registry,
74
+ {
75
+ onScope: (e) => {
76
+ scopedNames = e.enclosedNames;
77
+ },
78
+ onRef: (e) => {
79
+ const sourceKey = nodeKey(e.source.kind, e.source.metadata!.name as string);
80
+ const val = e.value;
81
+
82
+ // `!ref <name>` sentinel look up the target's kind from the name
83
+ // (resources are unique by name) so the edge carries the concrete kind,
84
+ // matching the {kind, name} edge shape below.
96
85
  if (isRefSentinel(val)) {
97
86
  const refName = val.source;
98
- if (scopedNames.has(refName)) continue;
87
+ if (scopedNames.has(refName)) return;
99
88
  const node = nodesByName.get(refName);
100
- if (node) {
101
- deps.get(sourceKey)!.add(nodeKey(node.kind, node.name));
102
- }
103
- continue;
89
+ if (node) deps.get(sourceKey)!.add(nodeKey(node.kind, node.name));
90
+ return;
104
91
  }
105
92
 
106
- if (typeof val !== "object") continue;
93
+ if (typeof val !== "object") return;
107
94
  const ref = val as Record<string, unknown>;
108
- if (!ref.kind || !ref.name) continue;
109
- // Edges to scoped resources are runtime deps, not boot-time deps — exclude from DAG
110
- if (scopedNames.has(ref.name as string)) continue;
95
+ if (!ref.kind || !ref.name) return;
96
+ if (scopedNames.has(ref.name as string)) return;
111
97
  const targetKey = nodeKey(ref.kind as string, ref.name as string);
112
- if (nodes.has(targetKey)) {
113
- deps.get(sourceKey)!.add(targetKey);
114
- }
115
- }
116
- }
117
- }
98
+ if (nodes.has(targetKey)) deps.get(sourceKey)!.add(targetKey);
99
+ },
100
+ },
101
+ { aliases, aliasesByModule, skipKinds: SYSTEM_KINDS, expand: true },
102
+ );
118
103
 
119
104
  // --- Kahn's topological sort ---
120
105
  // in-degree[X] = number of X's dependencies (size of deps[X])
package/src/index.ts CHANGED
@@ -9,6 +9,17 @@ export type {
9
9
  ParseError,
10
10
  } from "./loaded-types.js";
11
11
  export { flattenForAnalyzer, flattenLoadedModule } from "./flatten-for-analyzer.js";
12
+ export { visitManifest } from "./manifest-visitor.js";
13
+ export type {
14
+ CelSiteEvent,
15
+ ManifestVisitor,
16
+ RefSiteEvent,
17
+ ResourceEnterEvent,
18
+ ResourceExitEvent,
19
+ ScopeBoundaryEvent,
20
+ SchemaFromSiteEvent,
21
+ VisitOptions,
22
+ } from "./manifest-visitor.js";
12
23
  export { Loader } from "./manifest-loader.js";
13
24
  export { isModuleKind, MODULE_KINDS } from "./module-kinds.js";
14
25
  export type { ModuleKind } from "./module-kinds.js";
@@ -12,7 +12,7 @@ import { residualEntrySchemaMap } from "./residual-schema.js";
12
12
  * There is no `imports` namespace at runtime — import snapshots are stored
13
13
  * under `resources.<alias>`.
14
14
  */
15
- export const KERNEL_GLOBAL_NAMES = ["variables", "secrets", "resources", "env"] as const;
15
+ export const KERNEL_GLOBAL_NAMES = ["variables", "secrets", "resources", "ports", "env"] as const;
16
16
 
17
17
  const SYSTEM_KINDS = new Set([
18
18
  "Telo.Definition",
@@ -67,11 +67,32 @@ export function buildKernelGlobalsSchema(
67
67
  properties: resourceProps,
68
68
  additionalProperties: false,
69
69
  },
70
+ ports: buildPortsSchema(moduleManifest?.ports),
70
71
  env: { type: "object", additionalProperties: true },
71
72
  },
72
73
  };
73
74
  }
74
75
 
76
+ /** Build the closed `ports` chain-access schema: each declared port is an
77
+ * integer, so `ports.<name>` resolves and `ports.typo` (or member access past
78
+ * a port, like `ports.http.foo`) is flagged. Falls back to an open map when
79
+ * the module declares no ports. */
80
+ function buildPortsSchema(
81
+ ports: Record<string, any> | null | undefined,
82
+ ): Record<string, any> {
83
+ if (!ports || typeof ports !== "object" || Array.isArray(ports)) {
84
+ return { type: "object", additionalProperties: true };
85
+ }
86
+ const props: Record<string, any> = {};
87
+ for (const name of Object.keys(ports)) {
88
+ props[name] = { type: "integer" };
89
+ }
90
+ if (Object.keys(props).length === 0) {
91
+ return { type: "object", additionalProperties: true };
92
+ }
93
+ return { type: "object", properties: props, additionalProperties: false };
94
+ }
95
+
75
96
  /** Wrap a JSON Schema property map (like `Telo.Application.variables`) into a
76
97
  * closed object schema suitable for chain-access validation. For Application
77
98
  * entries the per-entry shape carries kernel-specific keys (`env`, `default`)
@@ -0,0 +1,340 @@
1
+ import type { ResourceDefinition, ResourceManifest } from "@telorun/sdk";
2
+ import { isRefSentinel, isTaggedSentinel, walkCelExpressions } from "@telorun/templating";
3
+ import type { AliasResolver } from "./alias-resolver.js";
4
+ import type { DefinitionRegistry } from "./definition-registry.js";
5
+ import {
6
+ isRefEntry,
7
+ isSchemaFromEntry,
8
+ isScopeEntry,
9
+ resolveFieldEntries,
10
+ resolveFieldValues,
11
+ type RefFieldEntry,
12
+ type SchemaFromFieldEntry,
13
+ } from "./reference-field-map.js";
14
+ import { extractContextsFromSchema, pathMatchesScope } from "./validate-cel-context.js";
15
+
16
+ /**
17
+ * One descent surface over a manifest's resources, emitting the annotation
18
+ * sites every analyzer pass needs. It replaces the iteration scaffolding that
19
+ * `validate-references`, `dependency-graph`, and the analyzer's CEL walk each
20
+ * reimplemented (field-map fetch, scope collection, ref/schema-from iteration,
21
+ * CEL expression walk + context matching).
22
+ *
23
+ * Two discovery mechanics ride one per-resource pass:
24
+ *
25
+ * - **Path-driven** — ref / scope / schema-from sites come from the resource's
26
+ * per-kind field map (`RefSite`, `ScopeBoundary`, `SchemaFromSite`). This is
27
+ * map iteration resolved against the resource value, not a node-by-node tree
28
+ * descent; the field map already unifies all three annotation types.
29
+ * - **Value-tree-driven** — compiled `${{...}}` / `!cel` nodes are found by
30
+ * scanning the resource value tree (`CelSite`). CEL can sit in any string
31
+ * field, including ones the field map never lists, so its discovery is
32
+ * fundamentally not path-driven; the field map only supplies the matched
33
+ * `x-telo-context` schema at the enclosing path.
34
+ *
35
+ * Handlers are optional (Babel-style): the walker computes and emits only what
36
+ * the visitor subscribes to, and skips the work behind absent handlers.
37
+ *
38
+ * **Scope is per-resource.** `ScopeBoundary` is emitted once per resource at
39
+ * enter time, before that resource's `RefSite`s, carrying both the source
40
+ * enclosure prefixes (for refs written *inside* a scope) and the enclosed
41
+ * resource-name set (for consumers that drop edges to scoped targets). No
42
+ * cross-resource ordering or global enclosed-name union is implied — every
43
+ * consumer's scope decision is local to the resource being visited, matching
44
+ * the semantics each pass had before this walker existed.
45
+ */
46
+
47
+ export interface ResourceEnterEvent {
48
+ source: ResourceManifest;
49
+ /** Resolved definition for the resource's kind, or undefined when unknown. */
50
+ definition?: ResourceDefinition;
51
+ }
52
+
53
+ export interface ResourceExitEvent {
54
+ source: ResourceManifest;
55
+ }
56
+
57
+ export interface ScopeBoundaryEvent {
58
+ source: ResourceManifest;
59
+ /** Dot-form prefixes of every `x-telo-scope` field on this resource. */
60
+ scopePrefixes: string[];
61
+ /** Scope-field JSON Pointer → manifests declared within that scope. */
62
+ manifestsByPointer: Map<string, ResourceManifest[]>;
63
+ /** Names of every resource declared inside this resource's scopes. Used by
64
+ * the dependency graph to drop boot edges to scoped (on-demand) targets. */
65
+ enclosedNames: Set<string>;
66
+ }
67
+
68
+ export interface RefSiteEvent {
69
+ source: ResourceManifest;
70
+ /** Field-map path with `[]` / `{}` markers (e.g. `steps[].invoke`). */
71
+ fieldPath: string;
72
+ /** Concrete path with `[N]` / map keys, matching `buildPositionIndex` keys. */
73
+ concretePath: string;
74
+ /** The ref value at this concrete site (sentinel, string, or `{kind,name}`). */
75
+ value: unknown;
76
+ /** The ref constraint (`refs[]`, `isArray`, optional `context`). */
77
+ entry: RefFieldEntry;
78
+ /** True when `fieldPath` falls within one of this resource's scope prefixes —
79
+ * source enclosure, used to scope a ref's candidate set. */
80
+ inScope: boolean;
81
+ /** Scope manifests visible to this ref path (non-empty only when `inScope`). */
82
+ visibleScopeManifests: ResourceManifest[];
83
+ /** True when the site was found by value-tree scanning rather than the field
84
+ * map (only when `discoverNestedRefs` is set) — a ref nested behind a `$ref`
85
+ * the field map doesn't descend (e.g. `Run.Sequence` `steps[].invoke`).
86
+ * Nested sites carry no x-telo-ref constraint (`entry.refs` is empty) and no
87
+ * scope info; `concretePath` still points at the exact location, so consumers
88
+ * can anchor to it. */
89
+ nested?: boolean;
90
+ }
91
+
92
+ export interface SchemaFromSiteEvent {
93
+ source: ResourceManifest;
94
+ /** Field-map path of the `x-telo-schema-from` slot. */
95
+ fieldPath: string;
96
+ entry: SchemaFromFieldEntry;
97
+ }
98
+
99
+ export interface CelSiteEvent {
100
+ source: ResourceManifest;
101
+ /** Concrete dotted path of the expression (from `walkCelExpressions`). */
102
+ path: string;
103
+ /** The CEL source expression. */
104
+ expr: string;
105
+ /** Engine that owns the expression (`cel`, `literal`, …). */
106
+ engineName: string;
107
+ /** Raw `x-telo-context` schema matched at the enclosing path, if any. The
108
+ * consumer resolves `x-telo-context-*` annotations and merges its own
109
+ * globals — the walker only does the path → context match. */
110
+ contextSchema?: Record<string, any>;
111
+ /** Scope of the matched context (e.g. `$.routes[*].handler`), if matched. */
112
+ matchedScope?: string;
113
+ }
114
+
115
+ export interface ManifestVisitor {
116
+ onResourceEnter?(e: ResourceEnterEvent): void;
117
+ onScope?(e: ScopeBoundaryEvent): void;
118
+ onRef?(e: RefSiteEvent): void;
119
+ onSchemaFrom?(e: SchemaFromSiteEvent): void;
120
+ onCel?(e: CelSiteEvent): void;
121
+ onResourceExit?(e: ResourceExitEvent): void;
122
+ }
123
+
124
+ export interface VisitOptions {
125
+ aliases?: AliasResolver;
126
+ aliasesByModule?: Map<string, AliasResolver>;
127
+ /** Resource kinds to skip entirely (kind blueprints, import metadata, …). */
128
+ skipKinds?: ReadonlySet<string>;
129
+ /** When true, ref / scope sites come from the schema-from-expanded field map
130
+ * so refs nested behind `x-telo-schema-from` are surfaced. `SchemaFromSite`
131
+ * events are always emitted from the base map regardless of this flag. */
132
+ expand?: boolean;
133
+ /** When true, additionally discover refs by scanning each resource's value
134
+ * tree for `!ref` sentinels and `{kind, name}` reference objects — surfacing
135
+ * refs the field map never lists because they sit behind a `$ref` it doesn't
136
+ * descend (notably `Run.Sequence` step `invoke`s). Emitted as `RefSite`s with
137
+ * `nested: true`, deduped against the field-map sites by concrete path.
138
+ * Opt-in: the validators / dependency graph must NOT enable it (those refs
139
+ * are runtime-resolved, not boot dependencies). */
140
+ discoverNestedRefs?: boolean;
141
+ }
142
+
143
+ /** Synthetic entry for a value-tree-discovered ref — these carry no declared
144
+ * x-telo-ref constraint. */
145
+ const NESTED_REF_ENTRY: RefFieldEntry = { refs: [], isArray: false };
146
+
147
+ /** Scans a value tree for ref-shaped values, emitting each with its concrete
148
+ * path. Recognizes `!ref <name>` sentinels and named `{kind, name}` reference
149
+ * objects. Other tagged sentinels (`!cel`, `!literal`) and precompiled nodes
150
+ * are leaves. Path format matches `resolveFieldEntries` / `walkCelExpressions`
151
+ * (`a.b[0].c`).
152
+ *
153
+ * Stops at every `{kind, …}` resource boundary: a named ref is emitted, an
154
+ * inline resource (`{kind}` with no name) is left alone, and **neither is
155
+ * descended into**. A nested resource's own refs belong to its inner topology,
156
+ * not the enclosing node — e.g. an inline `Sql.Exec` step's `connection` is the
157
+ * Exec's dependency, not the surrounding `Run.Sequence`'s. The scan is started
158
+ * per top-level field (not on the resource object) so the resource's own
159
+ * `kind` doesn't trip this boundary. */
160
+ function walkRefValues(
161
+ value: unknown,
162
+ path: string,
163
+ cb: (value: unknown, path: string) => void,
164
+ ): void {
165
+ if (isRefSentinel(value)) {
166
+ cb(value, path);
167
+ return;
168
+ }
169
+ if (isTaggedSentinel(value)) return;
170
+ if (Array.isArray(value)) {
171
+ value.forEach((v, i) => walkRefValues(v, `${path}[${i}]`, cb));
172
+ return;
173
+ }
174
+ if (value === null || typeof value !== "object") return;
175
+ if ((value as { __compiled?: unknown }).__compiled) return;
176
+ const obj = value as Record<string, unknown>;
177
+ if (typeof obj.kind === "string") {
178
+ // Resource boundary — emit if it's a named ref, then stop descending.
179
+ if (typeof obj.name === "string") cb(value, path);
180
+ return;
181
+ }
182
+ for (const [k, v] of Object.entries(obj)) {
183
+ walkRefValues(v, path ? `${path}.${k}` : k, cb);
184
+ }
185
+ }
186
+
187
+ const scopePrefixOf = (pointer: string): string =>
188
+ pointer.replace(/^\//, "").replace(/\//g, ".");
189
+
190
+ const pathUnderPrefix = (fieldPath: string, prefix: string): boolean =>
191
+ fieldPath === prefix ||
192
+ fieldPath.startsWith(prefix + ".") ||
193
+ fieldPath.startsWith(prefix + "[");
194
+
195
+ export function visitManifest(
196
+ resources: ResourceManifest[],
197
+ registry: DefinitionRegistry,
198
+ visitor: ManifestVisitor,
199
+ options: VisitOptions = {},
200
+ ): void {
201
+ const { aliases, aliasesByModule, skipKinds, expand, discoverNestedRefs } = options;
202
+
203
+ const wantsRefs = !!visitor.onRef;
204
+ const wantsScope = !!visitor.onScope;
205
+ const wantsSchemaFrom = !!visitor.onSchemaFrom;
206
+ const wantsCel = !!visitor.onCel;
207
+ const wantsNested = wantsRefs && !!discoverNestedRefs;
208
+
209
+ for (const r of resources) {
210
+ if (!r.metadata?.name || !r.kind) continue;
211
+ if (skipKinds?.has(r.kind)) continue;
212
+
213
+ const resolvedKind = aliases?.resolveKind(r.kind);
214
+ const definition =
215
+ registry.resolve(r.kind) ??
216
+ (resolvedKind ? registry.resolve(resolvedKind) : undefined);
217
+
218
+ visitor.onResourceEnter?.({ source: r, definition });
219
+
220
+ // Concrete paths emitted from the field map — so the value-tree scan below
221
+ // doesn't re-emit a ref the field map already covered.
222
+ const emittedRefPaths = wantsNested ? new Set<string>() : null;
223
+
224
+ if (wantsRefs || wantsScope || wantsSchemaFrom) {
225
+ const baseMap = aliases
226
+ ? registry.getFieldMapForKind(r.kind, aliases)
227
+ : registry.getFieldMap(r.kind);
228
+
229
+ // Expanded map drives ref/scope sites when requested; schema-from sites
230
+ // come from the base map (expansion replaces them with nested refs).
231
+ const refScopeMap =
232
+ expand && aliases && aliasesByModule
233
+ ? registry.expandedFieldMapForResource(r, aliases, aliasesByModule)
234
+ : baseMap;
235
+
236
+ if (refScopeMap && (wantsRefs || wantsScope)) {
237
+ const manifestsByPointer = new Map<string, ResourceManifest[]>();
238
+ for (const [fieldPath, entry] of refScopeMap) {
239
+ if (!isScopeEntry(entry)) continue;
240
+ const raw = resolveFieldValues(r, fieldPath)
241
+ .flatMap((v) => (Array.isArray(v) ? v : [v]))
242
+ .filter((v): v is ResourceManifest => !!v && typeof v === "object");
243
+ const pointers = Array.isArray(entry.scope) ? entry.scope : [entry.scope];
244
+ for (const pointer of pointers) manifestsByPointer.set(pointer, raw);
245
+ }
246
+ const scopePrefixes = Array.from(manifestsByPointer.keys()).map(scopePrefixOf);
247
+
248
+ if (wantsScope) {
249
+ const enclosedNames = new Set<string>();
250
+ for (const manifests of manifestsByPointer.values()) {
251
+ for (const m of manifests) {
252
+ const name = m.metadata?.name;
253
+ if (typeof name === "string") enclosedNames.add(name);
254
+ }
255
+ }
256
+ visitor.onScope!({ source: r, scopePrefixes, manifestsByPointer, enclosedNames });
257
+ }
258
+
259
+ if (wantsRefs) {
260
+ for (const [fieldPath, entry] of refScopeMap) {
261
+ if (!isRefEntry(entry)) continue;
262
+
263
+ const inScope = scopePrefixes.some((prefix) => pathUnderPrefix(fieldPath, prefix));
264
+ const visibleScopeManifests: ResourceManifest[] = [];
265
+ if (inScope) {
266
+ for (const [pointer, manifests] of manifestsByPointer) {
267
+ if (pathUnderPrefix(fieldPath, scopePrefixOf(pointer))) {
268
+ visibleScopeManifests.push(...manifests);
269
+ }
270
+ }
271
+ }
272
+
273
+ for (const { value, path: concretePath } of resolveFieldEntries(r, fieldPath)) {
274
+ if (!value) continue;
275
+ emittedRefPaths?.add(concretePath);
276
+ visitor.onRef!({
277
+ source: r,
278
+ fieldPath,
279
+ concretePath,
280
+ value,
281
+ entry,
282
+ inScope,
283
+ visibleScopeManifests,
284
+ });
285
+ }
286
+ }
287
+ }
288
+ }
289
+
290
+ if (wantsSchemaFrom && baseMap) {
291
+ for (const [fieldPath, entry] of baseMap) {
292
+ if (!isSchemaFromEntry(entry)) continue;
293
+ visitor.onSchemaFrom!({ source: r, fieldPath, entry });
294
+ }
295
+ }
296
+ }
297
+
298
+ // Value-tree-driven nested ref discovery — refs the field map can't reach
299
+ // because they sit behind a `$ref` it doesn't descend (e.g. Run.Sequence
300
+ // step `invoke`s). Deduped against the field-map sites by concrete path.
301
+ // Scanned per top-level field so the resource's own `kind` isn't treated as
302
+ // a resource boundary by `walkRefValues`.
303
+ if (wantsNested) {
304
+ const emitNested = (value: unknown, path: string) => {
305
+ if (emittedRefPaths!.has(path)) return;
306
+ visitor.onRef!({
307
+ source: r,
308
+ fieldPath: path,
309
+ concretePath: path,
310
+ value,
311
+ entry: NESTED_REF_ENTRY,
312
+ inScope: false,
313
+ visibleScopeManifests: [],
314
+ nested: true,
315
+ });
316
+ };
317
+ for (const [key, value] of Object.entries(r as Record<string, unknown>)) {
318
+ walkRefValues(value, key, emitNested);
319
+ }
320
+ }
321
+
322
+ if (wantsCel) {
323
+ const contexts = definition?.schema ? extractContextsFromSchema(definition.schema) : [];
324
+ walkCelExpressions(r, "", (expr, path, engineName) => {
325
+ let contextSchema: Record<string, any> | undefined;
326
+ let matchedScope: string | undefined;
327
+ for (const ctx of contexts) {
328
+ if (pathMatchesScope(path, ctx.scope)) {
329
+ contextSchema = ctx.schema;
330
+ matchedScope = ctx.scope;
331
+ break;
332
+ }
333
+ }
334
+ visitor.onCel!({ source: r, path, expr, engineName, contextSchema, matchedScope });
335
+ });
336
+ }
337
+
338
+ visitor.onResourceExit?.({ source: r });
339
+ }
340
+ }
@@ -213,6 +213,20 @@ function traverseNode(
213
213
  const entry: RefFieldEntry = { refs, isArray: path.includes("[]") };
214
214
  if (node["x-telo-context"]) entry.context = node["x-telo-context"] as Record<string, any>;
215
215
  map.set(path, entry);
216
+ // A node can mix item-level ref branches (a bare string / `{kind, name}`)
217
+ // with object branches that carry their OWN nested refs — e.g. Application
218
+ // `targets`: a bare ref vs inline `{ invoke }` vs gated `{ ref }`. Descend
219
+ // into the variant objects so those nested slots register too (and their
220
+ // `!ref` sentinels resolve). Pure x-telo-ref branches have no properties
221
+ // and contribute nothing here.
222
+ for (const variantKey of ["oneOf", "anyOf", "allOf"] as const) {
223
+ const variants = node[variantKey];
224
+ if (!Array.isArray(variants)) continue;
225
+ for (const variant of variants) {
226
+ if (!variant || typeof variant !== "object") continue;
227
+ traverseVariant(variant as Record<string, any>, path, map, root, visitedRefs);
228
+ }
229
+ }
216
230
  return;
217
231
  }
218
232