@telorun/analyzer 0.52.0 → 0.54.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 (80) hide show
  1. package/dist/analysis-registry.d.ts +8 -0
  2. package/dist/analysis-registry.d.ts.map +1 -1
  3. package/dist/analysis-registry.js +21 -3
  4. package/dist/analyzer.d.ts +3 -2
  5. package/dist/analyzer.d.ts.map +1 -1
  6. package/dist/analyzer.js +193 -26
  7. package/dist/builtins.d.ts.map +1 -1
  8. package/dist/builtins.js +32 -12
  9. package/dist/call-graph.d.ts +189 -0
  10. package/dist/call-graph.d.ts.map +1 -0
  11. package/dist/call-graph.js +617 -0
  12. package/dist/dependency-graph.d.ts +17 -7
  13. package/dist/dependency-graph.d.ts.map +1 -1
  14. package/dist/dependency-graph.js +36 -65
  15. package/dist/flatten-for-analyzer.d.ts +8 -0
  16. package/dist/flatten-for-analyzer.d.ts.map +1 -1
  17. package/dist/flatten-for-analyzer.js +32 -0
  18. package/dist/index.d.ts +15 -1
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.js +11 -1
  21. package/dist/manifest-navigation.d.ts +32 -0
  22. package/dist/manifest-navigation.d.ts.map +1 -0
  23. package/dist/manifest-navigation.js +91 -0
  24. package/dist/manifest-visitor.js +1 -1
  25. package/dist/ref-slot.d.ts +125 -0
  26. package/dist/ref-slot.d.ts.map +1 -0
  27. package/dist/ref-slot.js +226 -0
  28. package/dist/reference-field-map.d.ts +15 -1
  29. package/dist/reference-field-map.d.ts.map +1 -1
  30. package/dist/reference-field-map.js +29 -35
  31. package/dist/resolve-schema-ref-kinds.d.ts +4 -0
  32. package/dist/resolve-schema-ref-kinds.d.ts.map +1 -1
  33. package/dist/resolve-schema-ref-kinds.js +31 -8
  34. package/dist/resolve-zone-requirements.d.ts +110 -0
  35. package/dist/resolve-zone-requirements.d.ts.map +1 -0
  36. package/dist/resolve-zone-requirements.js +541 -0
  37. package/dist/types.d.ts +8 -0
  38. package/dist/types.d.ts.map +1 -1
  39. package/dist/validate-module-metadata.d.ts +38 -0
  40. package/dist/validate-module-metadata.d.ts.map +1 -0
  41. package/dist/validate-module-metadata.js +256 -0
  42. package/dist/validate-observed-state.d.ts +14 -13
  43. package/dist/validate-observed-state.d.ts.map +1 -1
  44. package/dist/validate-observed-state.js +21 -88
  45. package/dist/validate-ref-slots.d.ts +48 -0
  46. package/dist/validate-ref-slots.d.ts.map +1 -0
  47. package/dist/validate-ref-slots.js +219 -0
  48. package/dist/validate-references.d.ts.map +1 -1
  49. package/dist/validate-references.js +8 -1
  50. package/dist/validate-zone-slots.d.ts +39 -0
  51. package/dist/validate-zone-slots.d.ts.map +1 -0
  52. package/dist/validate-zone-slots.js +114 -0
  53. package/dist/zone-module-documents.d.ts +27 -0
  54. package/dist/zone-module-documents.d.ts.map +1 -0
  55. package/dist/zone-module-documents.js +1 -0
  56. package/dist/zone-slot.d.ts +61 -0
  57. package/dist/zone-slot.d.ts.map +1 -0
  58. package/dist/zone-slot.js +91 -0
  59. package/package.json +3 -3
  60. package/src/analysis-registry.ts +20 -2
  61. package/src/analyzer.ts +211 -24
  62. package/src/builtins.ts +32 -12
  63. package/src/call-graph.ts +827 -0
  64. package/src/dependency-graph.ts +34 -68
  65. package/src/flatten-for-analyzer.ts +32 -0
  66. package/src/index.ts +51 -0
  67. package/src/manifest-navigation.ts +91 -0
  68. package/src/manifest-visitor.ts +1 -1
  69. package/src/ref-slot.ts +273 -0
  70. package/src/reference-field-map.ts +39 -36
  71. package/src/resolve-schema-ref-kinds.ts +34 -7
  72. package/src/resolve-zone-requirements.ts +781 -0
  73. package/src/types.ts +8 -0
  74. package/src/validate-module-metadata.ts +335 -0
  75. package/src/validate-observed-state.ts +26 -92
  76. package/src/validate-ref-slots.ts +293 -0
  77. package/src/validate-references.ts +8 -1
  78. package/src/validate-zone-slots.ts +175 -0
  79. package/src/zone-module-documents.ts +27 -0
  80. package/src/zone-slot.ts +116 -0
package/src/types.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import type { ZoneModuleDocuments } from "./zone-module-documents.js";
1
2
  /** Matches LSP DiagnosticSeverity values exactly.
2
3
  * https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#diagnosticSeverity */
3
4
  export const DiagnosticSeverity = {
@@ -82,6 +83,13 @@ export interface LoaderInitOptions {
82
83
 
83
84
  export interface AnalysisOptions {
84
85
  strictContexts?: boolean;
86
+ /** Imported libraries' FULL document sets, for the zone stage's per-library
87
+ * export derivation — the flattened analysis view forwards only each
88
+ * library's export surface, never its internal dispatch chain. Collected
89
+ * from a LoadedGraph via `collectZoneModuleDocuments`. Omitting it skips
90
+ * the derivation (the under-approximating direction — the runtime check
91
+ * remains the enforcement). */
92
+ moduleDocuments?: ZoneModuleDocuments[];
85
93
  /** When true, `analyze()` runs the state-mutating setup (module identity /
86
94
  * alias / definition registration plus `normalizeInlineResources`) but
87
95
  * skips every diagnostic-producing pass — per-resource validation, the
@@ -0,0 +1,335 @@
1
+ import type { ResourceManifest } from "@telorun/sdk";
2
+
3
+ import type { AliasResolver } from "./alias-resolver.js";
4
+ import type { DefinitionRegistry } from "./definition-registry.js";
5
+ import { distance } from "./levenshtein.js";
6
+ import { DiagnosticSeverity, type AnalysisDiagnostic } from "./types.js";
7
+
8
+ const SOURCE = "telo-analyzer";
9
+
10
+ /**
11
+ * Static validation of the `metadata:` block on module docs (`Telo.Application` /
12
+ * `Telo.Library`) and of `metadata.deprecated` wherever it appears.
13
+ *
14
+ * These fields are descriptive — nothing in the kernel branches on them — but they
15
+ * are the module's public face: a hub indexes them, and a consumer reads them
16
+ * before deciding to import. That is exactly why they need checking. A field the
17
+ * runtime ignores has no failure mode that would ever surface it, so a mistyped
18
+ * `licence:` or `deprecatd:` is invisible forever, and the module ships claiming
19
+ * nothing while its author believes otherwise.
20
+ *
21
+ * The vocabulary stays **open** — `metadata` accepts any key, because a publisher
22
+ * may carry their own — so an unknown key is only reported when it is a near-miss
23
+ * of a known one. That catches the typo without closing the set.
24
+ *
25
+ * **Everything here is a WARNING, and fatal only at `telo publish`** (see
26
+ * {@link PUBLISH_BLOCKING_CODES}). Refusing to *run* a manifest over a field no
27
+ * runtime reads gets the cost backwards: `version: 1.0` is a YAML float rather
28
+ * than a string, which is a real mistake worth reporting, but stopping the app
29
+ * from starting over it is worse than the mistake. Publication is the moment
30
+ * these fields become consequential — they are projected onto the artifact's
31
+ * annotations and indexed by the hub — so that is where they block.
32
+ */
33
+
34
+ /**
35
+ * Codes that must not block running a manifest but MUST block publishing one.
36
+ *
37
+ * Kept as a set rather than a severity because the two audiences differ: a
38
+ * developer running a manifest wants to know, a publisher must be stopped. If a
39
+ * later check earns the same treatment, add its code here rather than inventing
40
+ * a third severity level.
41
+ */
42
+ export const PUBLISH_BLOCKING_CODES: ReadonlySet<string> = new Set([
43
+ "METADATA_INVALID_TYPE",
44
+ "METADATA_UNKNOWN_FIELD",
45
+ "INVALID_DEPRECATION",
46
+ "DEPRECATION_REPLACEMENT_UNRESOLVED",
47
+ ]);
48
+
49
+ type FieldType = "string" | "string[]" | "object";
50
+
51
+ /** Conventional module-doc metadata, and the type each carries. Descriptive only;
52
+ * `name` is the sole field anything resolves against. */
53
+ const MODULE_METADATA_TYPES: Record<string, FieldType> = {
54
+ name: "string",
55
+ module: "string",
56
+ version: "string",
57
+ description: "string",
58
+ repository: "string",
59
+ homepage: "string",
60
+ documentation: "string",
61
+ license: "string",
62
+ namespace: "string",
63
+ categories: "string[]",
64
+ deprecated: "object",
65
+ };
66
+
67
+ /** What a kind doc's `metadata:` may carry.
68
+ *
69
+ * Deliberately narrower than a module's: `version`, `license` and the rest
70
+ * belong to the module, and a kind restating them means nothing. `categories`
71
+ * is legal and *replaces* the module's for that kind; `description` is hub
72
+ * search text. Both have exactly the failure mode this file exists for — a
73
+ * `descriptoin:` on a kind doc is read by nothing and reported by nothing, so
74
+ * it ships silently — which is why kind docs are checked rather than exempt. */
75
+ const KIND_METADATA_TYPES: Record<string, FieldType> = {
76
+ name: "string",
77
+ module: "string",
78
+ description: "string",
79
+ categories: "string[]",
80
+ deprecated: "object",
81
+ };
82
+
83
+ /** Alias-qualified kind — `Self.Migrations`, `Cache.Store`, `Telo.JsonSchema`. */
84
+ const ALIAS_KIND_RE = /^[A-Z][A-Za-z0-9_]*\.[A-Z][A-Za-z0-9_]*$/;
85
+
86
+ /** The built-in namespace, resolvable without an import — mirrors `validate-extends`. */
87
+ const TELO_BUILTIN_ALIAS = "Telo";
88
+
89
+ function typeOf(value: unknown): "string" | "string[]" | "object" | "other" {
90
+ if (typeof value === "string") return "string";
91
+ if (Array.isArray(value)) return value.every((v) => typeof v === "string") ? "string[]" : "other";
92
+ if (value !== null && typeof value === "object") return "object";
93
+ return "other";
94
+ }
95
+
96
+ export function validateModuleMetadata(
97
+ manifests: ResourceManifest[],
98
+ registry: DefinitionRegistry,
99
+ aliases: AliasResolver,
100
+ ): AnalysisDiagnostic[] {
101
+ const out: AnalysisDiagnostic[] = [];
102
+
103
+ // Docs forwarded from imported libraries carry `metadata.module` set to that
104
+ // library's name. Their `replacedBy` aliases — `Self`, or any alias private to
105
+ // that library — belong to the library's OWN scope, which the consumer's
106
+ // resolver knows nothing about, so re-checking them here reports a false
107
+ // DEPRECATION_REPLACEMENT_UNRESOLVED against a manifest the consumer does not
108
+ // own. They are validated when that library is analyzed as a root, which is
109
+ // its author's concern. Same rule, and the same reason, as `validate-extends`.
110
+ const importedModules = new Set<string>();
111
+ for (const m of manifests) {
112
+ if (m.kind !== "Telo.Import") continue;
113
+ const resolved = (m.metadata as { resolvedModuleName?: string } | undefined)
114
+ ?.resolvedModuleName;
115
+ if (resolved) importedModules.add(resolved);
116
+ }
117
+
118
+ for (const manifest of manifests) {
119
+ const isModuleDoc = manifest.kind === "Telo.Application" || manifest.kind === "Telo.Library";
120
+ const isKindDoc = manifest.kind === "Telo.Definition" || manifest.kind === "Telo.Abstract";
121
+ if (!isModuleDoc && !isKindDoc) continue;
122
+
123
+ const metadata = manifest.metadata as Record<string, unknown> | undefined;
124
+ if (!metadata) continue;
125
+
126
+ const ownModule = (metadata as { module?: string }).module;
127
+ if (ownModule && importedModules.has(ownModule)) continue;
128
+
129
+ const name = typeof metadata.name === "string" ? metadata.name : undefined;
130
+ const filePath = typeof metadata.source === "string" ? metadata.source : undefined;
131
+ const ctx = {
132
+ label: `${manifest.kind}/${name ?? "(unnamed)"}`,
133
+ resource: { kind: manifest.kind, name },
134
+ filePath,
135
+ };
136
+
137
+ validateFieldTypes(
138
+ metadata,
139
+ isModuleDoc ? MODULE_METADATA_TYPES : KIND_METADATA_TYPES,
140
+ ctx,
141
+ out,
142
+ );
143
+ validateDeprecation(metadata, isModuleDoc, ctx, registry, aliases, out);
144
+ }
145
+ return out;
146
+ }
147
+
148
+ interface DocContext {
149
+ label: string;
150
+ resource: { kind: string; name: string | undefined };
151
+ filePath: string | undefined;
152
+ }
153
+
154
+ /** How far a key may be from a known one and still be called a typo.
155
+ *
156
+ * Scaled, not absolute: at a flat 2, `date:` (a perfectly ordinary key an
157
+ * author might carry) is two edits from `name` and gets told it is a
158
+ * misspelling of it. The vocabulary is open, so a false accusation on a short
159
+ * key is worse than missing a typo on one. */
160
+ function typoThreshold(key: string, known: string): number {
161
+ return Math.max(1, Math.floor(Math.min(key.length, known.length) / 3));
162
+ }
163
+
164
+ function validateFieldTypes(
165
+ metadata: Record<string, unknown>,
166
+ allowed: Record<string, FieldType>,
167
+ ctx: DocContext,
168
+ out: AnalysisDiagnostic[],
169
+ ): void {
170
+ const known = Object.keys(allowed);
171
+ for (const [key, value] of Object.entries(metadata)) {
172
+ // Stamped by the loader, not authored — never a typo to report on.
173
+ if (key === "source") continue;
174
+
175
+ // Listed as known so a typo still gets suggested against it, but its shape
176
+ // belongs to `validateDeprecation`, which can say what is actually wrong.
177
+ // Type-checking it here too would report one mistake twice.
178
+ if (key === "deprecated") continue;
179
+
180
+ const expected = allowed[key];
181
+ if (expected === undefined) {
182
+ const near = known.find((k) => distance(key, k) <= typoThreshold(key, k));
183
+ if (near) {
184
+ out.push({
185
+ severity: DiagnosticSeverity.Warning,
186
+ code: "METADATA_UNKNOWN_FIELD",
187
+ source: SOURCE,
188
+ message:
189
+ `${ctx.label}: 'metadata.${key}' is not a known field — did you mean '${near}'? ` +
190
+ `Nothing reads an unrecognized key, so this declares nothing.`,
191
+ data: { resource: ctx.resource, filePath: ctx.filePath, path: `metadata.${key}` },
192
+ });
193
+ }
194
+ continue;
195
+ }
196
+
197
+ if (typeOf(value) !== expected) {
198
+ out.push({
199
+ severity: DiagnosticSeverity.Warning,
200
+ code: "METADATA_INVALID_TYPE",
201
+ source: SOURCE,
202
+ message: `${ctx.label}: 'metadata.${key}' must be ${describeType(expected)}.`,
203
+ data: { resource: ctx.resource, filePath: ctx.filePath, path: `metadata.${key}` },
204
+ });
205
+ }
206
+ }
207
+ }
208
+
209
+ function describeType(t: "string" | "string[]" | "object"): string {
210
+ if (t === "string[]") return "an array of strings";
211
+ if (t === "object") return "an object";
212
+ return "a string";
213
+ }
214
+
215
+ /**
216
+ * `metadata.deprecated: { reason, replacedBy? }`.
217
+ *
218
+ * `replacedBy` is deliberately resolvable rather than free text, and its form
219
+ * follows the level: a module doc names another **module ref** (the `imports:`
220
+ * source grammar), a kind doc names an **alias-qualified kind** resolved through
221
+ * this file's own imports — the same grammar `kind:` / `extends:` use, so the
222
+ * replacement is a link a consumer can follow rather than a sentence they have to
223
+ * interpret.
224
+ *
225
+ * A kind whose replacement lives in a module this one does not import cannot be
226
+ * named; that case deprecates at module level with a module ref instead. Accepted
227
+ * over inventing a second grammar for it.
228
+ */
229
+ function validateDeprecation(
230
+ metadata: Record<string, unknown>,
231
+ isModuleDoc: boolean,
232
+ ctx: DocContext,
233
+ registry: DefinitionRegistry,
234
+ aliases: AliasResolver,
235
+ out: AnalysisDiagnostic[],
236
+ ): void {
237
+ const deprecated = metadata.deprecated;
238
+ if (deprecated === undefined) return;
239
+
240
+ const at = "metadata.deprecated";
241
+ const push = (
242
+ code: string,
243
+ message: string,
244
+ path = at,
245
+ severity = DiagnosticSeverity.Warning,
246
+ ): void => {
247
+ out.push({
248
+ severity,
249
+ code,
250
+ source: SOURCE,
251
+ message: `${ctx.label}: ${message}`,
252
+ data: { resource: ctx.resource, filePath: ctx.filePath, path },
253
+ });
254
+ };
255
+
256
+ if (typeOf(deprecated) !== "object") {
257
+ push(
258
+ "INVALID_DEPRECATION",
259
+ `'${at}' must be an object with a 'reason' (and an optional 'replacedBy'). ` +
260
+ `A bare 'true' says a thing is deprecated without saying what to do instead.`,
261
+ );
262
+ return;
263
+ }
264
+
265
+ const block = deprecated as Record<string, unknown>;
266
+ const allowed = new Set(["reason", "replacedBy"]);
267
+ for (const key of Object.keys(block)) {
268
+ if (!allowed.has(key)) {
269
+ push("INVALID_DEPRECATION", `'${at}.${key}' is not a recognized key (reason, replacedBy).`, `${at}.${key}`);
270
+ }
271
+ }
272
+
273
+ if (typeof block.reason !== "string" || block.reason.trim() === "") {
274
+ push(
275
+ "INVALID_DEPRECATION",
276
+ `'${at}.reason' is required and must be a non-empty string — it is what a consumer reads to know what to do instead.`,
277
+ `${at}.reason`,
278
+ );
279
+ }
280
+
281
+ const replacedBy = block.replacedBy;
282
+ if (replacedBy === undefined) return;
283
+ const path = `${at}.replacedBy`;
284
+ if (typeof replacedBy !== "string" || replacedBy.trim() === "") {
285
+ push("INVALID_DEPRECATION", `'${path}' must be a non-empty string.`, path);
286
+ return;
287
+ }
288
+
289
+ if (isModuleDoc) {
290
+ // A module is replaced by another module, addressed the way an import is.
291
+ // Catching alias form here is worth a dedicated message: it is the natural
292
+ // mistake, and it would otherwise be stored as an unresolvable ref.
293
+ if (ALIAS_KIND_RE.test(replacedBy)) {
294
+ push(
295
+ "INVALID_DEPRECATION",
296
+ `'${path}: ${replacedBy}' looks like a kind reference, but a module doc's replacement is a ` +
297
+ `module ref (e.g. 'oci://ghcr.io/acme/thing'). Deprecate the kind itself to point at another kind.`,
298
+ path,
299
+ );
300
+ }
301
+ return;
302
+ }
303
+
304
+ // Kind level: resolve through this file's imports, exactly as `extends` does.
305
+ if (!ALIAS_KIND_RE.test(replacedBy)) {
306
+ push(
307
+ "INVALID_DEPRECATION",
308
+ `'${path}: ${replacedBy}' must be an alias-qualified kind ("<Alias>.<Kind>", ` +
309
+ `e.g. 'Self.Migrations'), resolved via this file's imports.`,
310
+ path,
311
+ );
312
+ return;
313
+ }
314
+
315
+ const prefix = replacedBy.slice(0, replacedBy.indexOf("."));
316
+ if (prefix !== TELO_BUILTIN_ALIAS && !aliases.hasAlias(prefix)) {
317
+ push(
318
+ "DEPRECATION_REPLACEMENT_UNRESOLVED",
319
+ `'${path}: ${replacedBy}' — alias '${prefix}' is not an import in this file's scope. ` +
320
+ `Declare the import or correct the alias.`,
321
+ path,
322
+ );
323
+ return;
324
+ }
325
+
326
+ const canonical = aliases.resolveKind(replacedBy);
327
+ if (!canonical || !registry.resolve(canonical)) {
328
+ push(
329
+ "DEPRECATION_REPLACEMENT_UNRESOLVED",
330
+ `'${path}: ${replacedBy}' does not resolve to a known kind. A replacement a consumer ` +
331
+ `cannot follow is no better than none.`,
332
+ path,
333
+ );
334
+ }
335
+ }
@@ -1,24 +1,20 @@
1
1
  import type { ResourceDefinition, ResourceManifest } from "@telorun/sdk";
2
2
  import { OBSERVED_STATE_KEY } from "@telorun/sdk";
3
- import { type DefResolver, effectiveStatusSchema } from "./extends-resolution.js";
3
+ import { effectiveStatusSchema } from "./extends-resolution.js";
4
4
  import { parseExportEntry } from "./flatten-for-analyzer.js";
5
- import { moduleScopedDefResolver, type ModuleScopes } from "./alias-resolver.js";
5
+ import {
6
+ moduleScopedDefResolver,
7
+ type AliasResolver,
8
+ type ModuleScopes,
9
+ } from "./alias-resolver.js";
10
+ import type { CallGraph } from "./call-graph.js";
11
+ import type { DefinitionRegistry } from "./definition-registry.js";
6
12
  import {
7
13
  buildReferenceFieldMap,
8
- isRefEntry,
9
14
  isScopeEntry,
10
15
  resolveFieldValues,
11
16
  } from "./reference-field-map.js";
12
17
 
13
- /** The kernel capabilities whose `run()` the kernel dispatches. A ref slot that
14
- * accepts one of them is a slot that can start a resource — `targets:` on an
15
- * Application or a `Run.Sequence`, and a step's `invoke:` (whose schema accepts
16
- * `Telo.Runnable` alongside `Telo.Invocable`, and which the kernel dispatches
17
- * through `run()` when the target has no `invoke()`). Keyed on the declared
18
- * capability, never on a field name or a kind, so any composer that accepts a
19
- * runnable participates without the analyzer knowing about it. */
20
- const RUN_DISPATCH_CONTRACTS = new Set(["Telo.Runnable", "Telo.Service"]);
21
-
22
18
  const SYSTEM_KINDS = new Set([
23
19
  "Telo.Definition",
24
20
  "Telo.Abstract",
@@ -99,92 +95,30 @@ export function observedStateRead(chain: readonly string[]): ObservedStateRead |
99
95
  }
100
96
 
101
97
  /**
102
- * The names of every resource some slot can start: referenced from a ref slot
103
- * that accepts a `Telo.Runnable` / `Telo.Service`, or named as a step's
104
- * `invoke:` target. A resource in none of them can never `run()`, so it can
105
- * never report observed state.
98
+ * The names of every resource some slot can start.
106
99
  *
107
- * Deliberately an over-approximation a name reachable through any of these
108
- * routes counts as runnable because the cost of a false "can never run" is a
109
- * valid manifest rejected, while the cost of a miss is only that the reader
110
- * finds out at runtime instead, with a message that names the same fix.
100
+ * One question, one answer: a resource is run-reachable when a control-
101
+ * transferring edge reaches it in the typed reference graph. `call`, `detached`,
102
+ * `trigger.inbound` and `trigger.consumer` all mean control arrives; `schema` and
103
+ * `dependency` mean it never does.
104
+ *
105
+ * This replaced two independent over-approximations that had to agree by
106
+ * coincidence: a field-map scan keeping slots whose *constraint capability*
107
+ * looked runnable, plus an untyped whole-manifest scan for the declared step
108
+ * invoke key at any depth. Both were guesses at the question `use` now answers —
109
+ * and the first was wrong in the direction that rejects valid manifests, since a
110
+ * slot constrained to `Telo.Invocable` can still be dispatched through `run()`.
111
111
  */
112
- export function collectRunReachableNames(
113
- manifests: readonly ResourceManifest[],
114
- defs: { resolve(kind: string): ResourceDefinition | undefined },
115
- aliases?: { resolveKind(kind: string): string | undefined },
116
- ): Set<string> {
112
+ export function collectRunReachableNames(graph: CallGraph): Set<string> {
113
+ // By NAME, not by resolved node: a `with:`-scoped resource is started by its
114
+ // sequence's `targets:` while never being a top-level node, so resolving first
115
+ // would report it as unstartable. The graph is passed in, never built here —
116
+ // one build per analysis, shared with every other graph consumer.
117
117
  const names = new Set<string>();
118
- const resolve: DefResolver = (kind) =>
119
- defs.resolve(aliases?.resolveKind(kind) ?? kind) ?? defs.resolve(kind);
120
-
121
- for (const manifest of manifests) {
122
- const def = resolve(manifest.kind as string);
123
- const schema = def?.schema as Record<string, any> | undefined;
124
- if (!schema) continue;
125
-
126
- for (const [path, entry] of buildReferenceFieldMap(schema)) {
127
- if (!isRefEntry(entry)) continue;
128
- if (!entry.refs.some((ref) => RUN_DISPATCH_CONTRACTS.has(ref))) continue;
129
- for (const value of resolveFieldValues(manifest, path)) collectRefName(value, names);
130
- }
131
-
132
- // Step arrays nest through `if` / `while` / `switch` / `try`, and the step
133
- // `invoke:` slot sits behind a local `$ref` the field map does not follow.
134
- // Match the declared invoke key at any depth instead of re-deriving the
135
- // nesting rules — over-approximating in the safe direction.
136
- const invokeKey = stepInvokeKey(schema);
137
- if (invokeKey) collectKeyedRefs(manifest, invokeKey, names);
138
- }
139
-
118
+ for (const edge of graph.controlEdges()) names.add(edge.toName);
140
119
  return names;
141
120
  }
142
121
 
143
- /** The property name a kind's `x-telo-step-context` declares as its dispatch
144
- * slot (`invoke`), or undefined when the kind has no step array. */
145
- function stepInvokeKey(schema: Record<string, any>): string | undefined {
146
- for (const fieldSchema of Object.values(
147
- (schema.properties ?? {}) as Record<string, any>,
148
- )) {
149
- const stepCtx = fieldSchema?.["x-telo-step-context"] as
150
- | Record<string, string>
151
- | undefined;
152
- if (stepCtx?.invoke) return stepCtx.invoke;
153
- }
154
- return undefined;
155
- }
156
-
157
- /** Collect ref names at every `key` property anywhere in `node`. */
158
- function collectKeyedRefs(node: unknown, key: string, out: Set<string>): void {
159
- if (Array.isArray(node)) {
160
- for (const item of node) collectKeyedRefs(item, key, out);
161
- return;
162
- }
163
- if (node === null || typeof node !== "object") return;
164
- for (const [k, value] of Object.entries(node as Record<string, unknown>)) {
165
- if (k === key) collectRefName(value, out);
166
- collectKeyedRefs(value, key, out);
167
- }
168
- }
169
-
170
- /** Record the resource name a slot value points at — a resolved `{kind, name}`
171
- * ref, an unresolved `!ref` sentinel, or a `{ ref }` / `{ invoke }` wrapper. */
172
- function collectRefName(value: unknown, out: Set<string>): void {
173
- if (value === null || typeof value !== "object") return;
174
- if (Array.isArray(value)) {
175
- for (const item of value) collectRefName(item, out);
176
- return;
177
- }
178
- const v = value as Record<string, unknown>;
179
- if (typeof v.name === "string") out.add(v.name);
180
- if (typeof v.source === "string") {
181
- const dot = v.source.lastIndexOf(".");
182
- out.add(dot >= 0 ? v.source.slice(dot + 1) : v.source);
183
- }
184
- for (const wrapper of ["ref", "invoke"]) {
185
- if (v[wrapper] !== undefined) collectRefName(v[wrapper], out);
186
- }
187
- }
188
122
 
189
123
  /** What a resource name resolves to for CEL purposes. `status` is present only
190
124
  * when the kind declares one; `scoped` marks a resource declared inside an