@telorun/analyzer 0.56.0 → 0.57.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/README.md +2 -2
- package/dist/analyzer.d.ts +5 -0
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/analyzer.js +155 -94
- package/dist/flatten-for-analyzer.d.ts +20 -1
- package/dist/flatten-for-analyzer.d.ts.map +1 -1
- package/dist/flatten-for-analyzer.js +48 -3
- package/dist/index.d.ts +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/kernel-globals.d.ts +25 -11
- package/dist/kernel-globals.d.ts.map +1 -1
- package/dist/kernel-globals.js +54 -24
- package/dist/manifest-visitor.d.ts +4 -0
- package/dist/manifest-visitor.d.ts.map +1 -1
- package/dist/manifest-visitor.js +3 -3
- package/dist/module-file-claims.d.ts +65 -0
- package/dist/module-file-claims.d.ts.map +1 -0
- package/dist/module-file-claims.js +106 -0
- package/dist/schema-compat.d.ts.map +1 -1
- package/dist/schema-compat.js +12 -1
- package/dist/types.d.ts +34 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +6 -0
- package/dist/validate-include-placement.d.ts +26 -0
- package/dist/validate-include-placement.d.ts.map +1 -0
- package/dist/validate-include-placement.js +67 -0
- package/dist/validate-throws-coverage.d.ts.map +1 -1
- package/dist/validate-throws-coverage.js +15 -12
- package/package.json +4 -3
- package/src/analyzer.ts +189 -131
- package/src/flatten-for-analyzer.ts +61 -3
- package/src/index.ts +5 -1
- package/src/kernel-globals.ts +74 -25
- package/src/manifest-visitor.ts +11 -3
- package/src/module-file-claims.ts +168 -0
- package/src/schema-compat.ts +19 -1
- package/src/types.ts +37 -0
- package/src/validate-include-placement.ts +70 -0
- package/src/validate-throws-coverage.ts +16 -11
package/src/kernel-globals.ts
CHANGED
|
@@ -22,28 +22,43 @@ const SYSTEM_KINDS = new Set([
|
|
|
22
22
|
"Telo.Abstract",
|
|
23
23
|
]);
|
|
24
24
|
|
|
25
|
+
/** Kernel globals as ONE resource's declaring module sees them. */
|
|
26
|
+
export interface KernelGlobalsIndex {
|
|
27
|
+
forResource(m: ResourceManifest): Record<string, any>;
|
|
28
|
+
}
|
|
29
|
+
|
|
25
30
|
/**
|
|
26
|
-
* Build
|
|
27
|
-
*
|
|
28
|
-
* chain-access validation recognises kernel globals without module authors
|
|
31
|
+
* Build the typed JSON Schema describing the kernel globals available to each
|
|
32
|
+
* resource in a manifest set. Used to merge into `x-telo-context` schemas so
|
|
33
|
+
* that chain-access validation recognises kernel globals without module authors
|
|
29
34
|
* having to re-declare them.
|
|
30
35
|
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
36
|
+
* `variables` / `secrets` / `ports` are typed **per declaring module**, because
|
|
37
|
+
* that is the contract the resource's CEL is evaluated against at runtime. An
|
|
38
|
+
* application analysis is flattened — `selectModuleManifestsForAnalysis` drops
|
|
39
|
+
* an imported library's module doc and carries its config blocks across as
|
|
40
|
+
* `metadata.moduleGlobals` instead — so a resource forwarded from a library is
|
|
41
|
+
* typed from that stamp, and everything else from the entry module's own doc
|
|
42
|
+
* (the only module doc left in the set).
|
|
43
|
+
*
|
|
44
|
+
* Typing every resource from the entry doc is what made a library's
|
|
45
|
+
* `variables.x` a hard error the library author could not act on, and — in the
|
|
46
|
+
* other direction — let a library read a variable it never declared whenever the
|
|
47
|
+
* app happened to declare that name.
|
|
48
|
+
*
|
|
49
|
+
* `resources` is NOT per module: it enumerates every non-system resource name in
|
|
50
|
+
* the set, and stays OPEN for a forwarded manifest (see `readModuleGlobals` for
|
|
51
|
+
* why a name list cannot be carried across the boundary honestly).
|
|
37
52
|
*/
|
|
38
|
-
export function
|
|
53
|
+
export function buildKernelGlobalsIndex(
|
|
39
54
|
manifests: ResourceManifest[],
|
|
40
55
|
/** Every resource a CEL read can name, including scope-declared ones (see
|
|
41
56
|
* `buildObservedStateIndex`). Kinds that declare a `status:` get a typed,
|
|
42
57
|
* closed `status` node; every other resource node stays open, so no flat read
|
|
43
58
|
* that passes today can start failing. */
|
|
44
59
|
resources?: ReadonlyMap<string, { kind: string; status?: Record<string, any> }>,
|
|
45
|
-
):
|
|
46
|
-
const
|
|
60
|
+
): KernelGlobalsIndex {
|
|
61
|
+
const entryDoc =
|
|
47
62
|
(manifests.find((m) => m.kind === "Telo.Application") as
|
|
48
63
|
| Record<string, any>
|
|
49
64
|
| undefined) ??
|
|
@@ -51,6 +66,52 @@ export function buildKernelGlobalsSchema(
|
|
|
51
66
|
| Record<string, any>
|
|
52
67
|
| undefined);
|
|
53
68
|
|
|
69
|
+
const entrySchema = globalsSchema(entryDoc, buildResourcesSchema(manifests, resources));
|
|
70
|
+
const openResources = { type: "object", additionalProperties: true };
|
|
71
|
+
const byModule = new Map<string, Record<string, any>>();
|
|
72
|
+
|
|
73
|
+
return {
|
|
74
|
+
forResource(m: ResourceManifest): Record<string, any> {
|
|
75
|
+
const meta = m.metadata as { module?: string; moduleGlobals?: ModuleGlobals } | undefined;
|
|
76
|
+
const stamped = meta?.moduleGlobals;
|
|
77
|
+
if (!stamped) return entrySchema;
|
|
78
|
+
const key = meta?.module ?? "";
|
|
79
|
+
const cached = byModule.get(key);
|
|
80
|
+
if (cached) return cached;
|
|
81
|
+
const schema = globalsSchema(stamped, openResources);
|
|
82
|
+
byModule.set(key, schema);
|
|
83
|
+
return schema;
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** The blocks a declaring module contributes to its resources' CEL globals. */
|
|
89
|
+
interface ModuleGlobals {
|
|
90
|
+
variables?: Record<string, unknown>;
|
|
91
|
+
secrets?: Record<string, unknown>;
|
|
92
|
+
ports?: Record<string, unknown>;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function globalsSchema(
|
|
96
|
+
doc: ModuleGlobals | Record<string, any> | undefined,
|
|
97
|
+
resourcesSchema: Record<string, any>,
|
|
98
|
+
): Record<string, any> {
|
|
99
|
+
return {
|
|
100
|
+
type: "object",
|
|
101
|
+
properties: {
|
|
102
|
+
variables: buildSchemaMapSchema(doc?.variables as Record<string, any> | undefined),
|
|
103
|
+
secrets: buildSchemaMapSchema(doc?.secrets as Record<string, any> | undefined),
|
|
104
|
+
resources: resourcesSchema,
|
|
105
|
+
ports: buildPortsSchema(doc?.ports as Record<string, any> | undefined),
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Every non-system resource name in the set, plus the scope-declared ones. */
|
|
111
|
+
function buildResourcesSchema(
|
|
112
|
+
manifests: ResourceManifest[],
|
|
113
|
+
resources?: ReadonlyMap<string, { kind: string; status?: Record<string, any> }>,
|
|
114
|
+
): Record<string, any> {
|
|
54
115
|
const resourceProps: Record<string, any> = {};
|
|
55
116
|
for (const m of manifests) {
|
|
56
117
|
const name = m.metadata?.name as string | undefined;
|
|
@@ -76,19 +137,7 @@ export function buildKernelGlobalsSchema(
|
|
|
76
137
|
applyObservedStateNode(resourceProps, key, entry.status);
|
|
77
138
|
}
|
|
78
139
|
|
|
79
|
-
return {
|
|
80
|
-
type: "object",
|
|
81
|
-
properties: {
|
|
82
|
-
variables: buildSchemaMapSchema(moduleManifest?.variables),
|
|
83
|
-
secrets: buildSchemaMapSchema(moduleManifest?.secrets),
|
|
84
|
-
resources: {
|
|
85
|
-
type: "object",
|
|
86
|
-
properties: resourceProps,
|
|
87
|
-
additionalProperties: false,
|
|
88
|
-
},
|
|
89
|
-
ports: buildPortsSchema(moduleManifest?.ports),
|
|
90
|
-
},
|
|
91
|
-
};
|
|
140
|
+
return { type: "object", properties: resourceProps, additionalProperties: false };
|
|
92
141
|
}
|
|
93
142
|
|
|
94
143
|
/** Build the closed `ports` chain-access schema: each declared port is an
|
package/src/manifest-visitor.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import type { ResourceDefinition, ResourceManifest } from "@telorun/sdk";
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
isRefSentinel,
|
|
4
|
+
isTaggedSentinel,
|
|
5
|
+
walkCelExpressions,
|
|
6
|
+
type CelSurface,
|
|
7
|
+
} from "@telorun/templating";
|
|
3
8
|
import type { AliasResolver } from "./alias-resolver.js";
|
|
4
9
|
import type { DefinitionRegistry } from "./definition-registry.js";
|
|
5
10
|
import {
|
|
@@ -114,6 +119,9 @@ export interface CelSiteEvent {
|
|
|
114
119
|
contextSchema?: Record<string, any>;
|
|
115
120
|
/** Scope of the matched context (e.g. `$.routes[*].handler`), if matched. */
|
|
116
121
|
matchedScope?: string;
|
|
122
|
+
/** Where `expr` sits in the scalar at `path`, and the delimiters to restore
|
|
123
|
+
* around a corrected expression. See `CelSurface`. */
|
|
124
|
+
surface: CelSurface;
|
|
117
125
|
}
|
|
118
126
|
|
|
119
127
|
export interface ManifestVisitor {
|
|
@@ -352,7 +360,7 @@ export function visitManifest(
|
|
|
352
360
|
|
|
353
361
|
if (wantsCel) {
|
|
354
362
|
const contexts = definition?.schema ? extractContextsFromSchema(definition.schema) : [];
|
|
355
|
-
walkCelExpressions(r, "", (expr, path, engineName) => {
|
|
363
|
+
walkCelExpressions(r, "", (expr, path, engineName, surface) => {
|
|
356
364
|
let contextSchema: Record<string, any> | undefined;
|
|
357
365
|
let matchedScope: string | undefined;
|
|
358
366
|
for (const ctx of contexts) {
|
|
@@ -362,7 +370,7 @@ export function visitManifest(
|
|
|
362
370
|
break;
|
|
363
371
|
}
|
|
364
372
|
}
|
|
365
|
-
visitor.onCel!({ source: r, path, expr, engineName, contextSchema, matchedScope });
|
|
373
|
+
visitor.onCel!({ source: r, path, expr, engineName, contextSchema, matchedScope, surface });
|
|
366
374
|
});
|
|
367
375
|
}
|
|
368
376
|
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import {
|
|
2
|
+
defaultCustomTags,
|
|
3
|
+
defaultRegistry,
|
|
4
|
+
walkCelExpressions,
|
|
5
|
+
type TemplatingEngineRegistry,
|
|
6
|
+
} from "@telorun/templating";
|
|
7
|
+
import { PackageURL } from "packageurl-js";
|
|
8
|
+
import { parseAllDocuments } from "yaml";
|
|
9
|
+
import { selectorFromQualifiers, selectorKey, type ArtifactSelector } from "./artifact-selector.js";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* One module-relative file a manifest names, and the artifact layer it belongs
|
|
13
|
+
* to.
|
|
14
|
+
*
|
|
15
|
+
* The single answer to "why is this file in the payload", replacing two
|
|
16
|
+
* derivations that happened to agree: publish used to re-parse the manifest with
|
|
17
|
+
* PURL knowledge hardcoded into the CLI, and any second vocabulary — a tag that
|
|
18
|
+
* embeds a file, say — would have had to be added there by hand. Here the
|
|
19
|
+
* knowledge sits with whoever owns the syntax: a controller candidate is read by
|
|
20
|
+
* this module, and a tagged value is read by the engine that owns its tag, via
|
|
21
|
+
* `TemplatingEngine.fileClaims`. Publish maps role to layer and recognises
|
|
22
|
+
* neither.
|
|
23
|
+
*
|
|
24
|
+
* Deliberately NOT hung off `analyze()`. That pass runs over a flattened,
|
|
25
|
+
* import-inclusive manifest set, so its claims would mix in imported libraries'
|
|
26
|
+
* files — whose paths are relative to *their* module and must never join this
|
|
27
|
+
* artifact — and it would make packaging, today derivable offline from manifest
|
|
28
|
+
* text, a product of resolving the whole import graph. This is per-module by
|
|
29
|
+
* construction and needs nothing but the text.
|
|
30
|
+
*
|
|
31
|
+
* Browser-safe, like the rest of the analyzer: parsing and string work only, no
|
|
32
|
+
* filesystem. Whether a claimed file EXISTS is a separate question, asked by the
|
|
33
|
+
* Node-side caller that has a directory to look in.
|
|
34
|
+
*/
|
|
35
|
+
interface ClaimBase {
|
|
36
|
+
/** Module-root-relative POSIX path — relative to the directory holding
|
|
37
|
+
* `telo.yaml`, never to the file the claim was written in. Publish inlines
|
|
38
|
+
* every `include:` partial into the published `telo.yaml`, so a
|
|
39
|
+
* per-file-relative path would change meaning in the artifact. */
|
|
40
|
+
readonly path: string;
|
|
41
|
+
/** Where the claim came from, for diagnostics: the PURL, or `!<tag>` and the
|
|
42
|
+
* path of the value that carried it. */
|
|
43
|
+
readonly origin: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* A **discriminated union**, not one shape with optional fields: a controller
|
|
48
|
+
* layer is one per selector and carries sibling patterns, and an assets layer is
|
|
49
|
+
* neither. Optional fields on a single shape put the consumer one `!` away from
|
|
50
|
+
* a crash inside `selectorKey` with no useful message, and let a producer emit a
|
|
51
|
+
* controller claim with no selector that nothing would reject.
|
|
52
|
+
*/
|
|
53
|
+
export type ModuleFileClaim =
|
|
54
|
+
| (ClaimBase & {
|
|
55
|
+
readonly role: "controller";
|
|
56
|
+
readonly selector: ArtifactSelector;
|
|
57
|
+
/** Extra payload patterns that belong in the same layer as this claim —
|
|
58
|
+
* `.gitignore`-style globs over the selected files, matched by the
|
|
59
|
+
* caller, which is the side that knows what was selected. */
|
|
60
|
+
readonly siblings: readonly string[];
|
|
61
|
+
})
|
|
62
|
+
| (ClaimBase & { readonly role: "assets" });
|
|
63
|
+
|
|
64
|
+
/** `pkg:telo/local/<format>?path=…` — the bundled-controller delivery mode.
|
|
65
|
+
* Anything else (`pkg:npm`, `pkg:cargo`) fetches from its own ecosystem and
|
|
66
|
+
* contributes no layer. */
|
|
67
|
+
const BUNDLED_TYPE = "telo";
|
|
68
|
+
const BUNDLED_NAMESPACE = "local";
|
|
69
|
+
|
|
70
|
+
/** Qualifier naming extra files that belong in a controller's layer — what an
|
|
71
|
+
* entry point loads but the manifest cannot otherwise see (a `.wasm` beside its
|
|
72
|
+
* glue, a native library opened at runtime). */
|
|
73
|
+
const SIBLINGS_QUALIFIER = "siblings";
|
|
74
|
+
|
|
75
|
+
/** Normalize a `path=` / sibling value to the manifest-relative POSIX form the
|
|
76
|
+
* file selector returns, so membership is a string comparison. */
|
|
77
|
+
function normalizeRelative(value: string): string {
|
|
78
|
+
return value.replace(/^\.\//, "").replace(/\\/g, "/");
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Bundled-controller claims from one document's `controllers:` list. */
|
|
82
|
+
function controllerClaims(json: unknown): ModuleFileClaim[] {
|
|
83
|
+
const candidates = (json as { controllers?: unknown } | null)?.controllers;
|
|
84
|
+
if (!Array.isArray(candidates)) return [];
|
|
85
|
+
const claims: ModuleFileClaim[] = [];
|
|
86
|
+
for (const candidate of candidates) {
|
|
87
|
+
if (typeof candidate !== "string") continue;
|
|
88
|
+
let parsed: PackageURL;
|
|
89
|
+
try {
|
|
90
|
+
parsed = PackageURL.fromString(candidate);
|
|
91
|
+
} catch {
|
|
92
|
+
// Not a parseable PURL — claim collection is not the place to reject it;
|
|
93
|
+
// the analyzer's own validation and the controller loader both report it
|
|
94
|
+
// with better context.
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
if (parsed.type !== BUNDLED_TYPE || parsed.namespace !== BUNDLED_NAMESPACE) continue;
|
|
98
|
+
const entry = parsed.qualifiers?.path;
|
|
99
|
+
if (typeof entry !== "string" || entry === "") continue;
|
|
100
|
+
claims.push({
|
|
101
|
+
role: "controller",
|
|
102
|
+
path: normalizeRelative(entry),
|
|
103
|
+
selector: selectorFromQualifiers(parsed.name, parsed.qualifiers, `controller "${candidate}"`),
|
|
104
|
+
siblings: String(parsed.qualifiers?.[SIBLINGS_QUALIFIER] ?? "")
|
|
105
|
+
.split(",")
|
|
106
|
+
.map((p) => p.trim())
|
|
107
|
+
.filter((p) => p !== ""),
|
|
108
|
+
origin: candidate,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
return claims;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Claims contributed by tagged values, asked of the engine that owns each tag.
|
|
115
|
+
* The walk reaches every tagged scalar in the document, so an engine that
|
|
116
|
+
* embeds files is discovered wherever its tag was written.
|
|
117
|
+
*
|
|
118
|
+
* The layer role is assigned HERE, not by the engine: an engine reports what it
|
|
119
|
+
* embeds, and which layer that belongs in is this module's vocabulary. A file a
|
|
120
|
+
* tag embeds is read only when the resource holding it is created, so `assets`
|
|
121
|
+
* — the lazily-fetched layer — is what it is. */
|
|
122
|
+
function taggedClaims(json: unknown, registry: TemplatingEngineRegistry): ModuleFileClaim[] {
|
|
123
|
+
const claims: ModuleFileClaim[] = [];
|
|
124
|
+
walkCelExpressions(json, "", (source, path, engineName) => {
|
|
125
|
+
const engine = registry.get(engineName);
|
|
126
|
+
for (const claim of engine?.fileClaims?.(source) ?? []) {
|
|
127
|
+
claims.push({ role: "assets", path: claim.path, origin: `!${engineName} at '${path}'` });
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
return claims;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Identity of a claim for de-duplication: the same file claimed twice by two
|
|
134
|
+
* resources is one file in one layer. Role and selector are part of it because
|
|
135
|
+
* a file two controller candidates both claim is genuinely copied into each of
|
|
136
|
+
* their layers — dropping one would leave a platform's layer short a file it
|
|
137
|
+
* declared it needs. */
|
|
138
|
+
function claimKey(claim: ModuleFileClaim): string {
|
|
139
|
+
const selector = claim.role === "controller" ? selectorKey(claim.selector) : "";
|
|
140
|
+
return `${claim.role}\0${selector}\0${claim.path}`;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Every module-relative file the manifest names, from every syntax that can name
|
|
145
|
+
* one.
|
|
146
|
+
*
|
|
147
|
+
* `manifestText` is one module's `telo.yaml`. Publish passes the text it is
|
|
148
|
+
* about to ship — i.e. after `include:` partials have been inlined — but the
|
|
149
|
+
* answer does not depend on that: claims are root-relative, so collecting them
|
|
150
|
+
* before or after inlining gives the same set.
|
|
151
|
+
*/
|
|
152
|
+
export function collectModuleFileClaims(
|
|
153
|
+
manifestText: string,
|
|
154
|
+
registry: TemplatingEngineRegistry = defaultRegistry(),
|
|
155
|
+
): ModuleFileClaim[] {
|
|
156
|
+
const seen = new Set<string>();
|
|
157
|
+
const claims: ModuleFileClaim[] = [];
|
|
158
|
+
for (const doc of parseAllDocuments(manifestText, { customTags: defaultCustomTags() })) {
|
|
159
|
+
const json = doc.toJSON() as unknown;
|
|
160
|
+
for (const claim of [...controllerClaims(json), ...taggedClaims(json, registry)]) {
|
|
161
|
+
const key = claimKey(claim);
|
|
162
|
+
if (seen.has(key)) continue;
|
|
163
|
+
seen.add(key);
|
|
164
|
+
claims.push(claim);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return claims;
|
|
168
|
+
}
|
package/src/schema-compat.ts
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
import AjvModule from "ajv";
|
|
2
2
|
import addFormats from "ajv-formats";
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
INCLUDE_BYTES_ENGINE,
|
|
5
|
+
INCLUDE_ENGINE_NAMES,
|
|
6
|
+
isRefSentinel,
|
|
7
|
+
isTaggedSentinel,
|
|
8
|
+
ManifestRootSchema,
|
|
9
|
+
normalizeRefSlots,
|
|
10
|
+
} from "@telorun/templating";
|
|
4
11
|
import { binaryKeyword, isBinarySlot } from "./binary-slot.js";
|
|
5
12
|
|
|
6
13
|
const Ajv = (AjvModule as any).default ?? AjvModule;
|
|
@@ -564,6 +571,17 @@ export function substituteCelFields(
|
|
|
564
571
|
if (isRefSentinel(data)) {
|
|
565
572
|
return data;
|
|
566
573
|
}
|
|
574
|
+
// A file embed's type is a CONSTANT of the tag, not a function of the slot:
|
|
575
|
+
// `!include-text` always produces a string and `!include-bytes` always
|
|
576
|
+
// produces bytes. Collapsing them to a slot-shaped placeholder like a CEL
|
|
577
|
+
// expression would make every slot accept both, so a byte embed at a
|
|
578
|
+
// `type: string` field passed `telo check` and failed at resource creation —
|
|
579
|
+
// and the reverse (text at an `x-telo-binary` slot) did too. Substituting the
|
|
580
|
+
// real type lets AJV and the `x-telo-binary` keyword reject both directions
|
|
581
|
+
// statically, with no new diagnostic code.
|
|
582
|
+
if (isTaggedSentinel(data) && INCLUDE_ENGINE_NAMES.has(data.engine)) {
|
|
583
|
+
return data.engine === INCLUDE_BYTES_ENGINE ? new Uint8Array() : "";
|
|
584
|
+
}
|
|
567
585
|
if (isTaggedSentinel(data)) {
|
|
568
586
|
mark();
|
|
569
587
|
return celPlaceholderForSchema(resolved);
|
package/src/types.ts
CHANGED
|
@@ -31,6 +31,36 @@ export type PositionIndex = Map<string, Range>;
|
|
|
31
31
|
|
|
32
32
|
/** LSP-compatible Diagnostic shape. range is optional because parsed YAML may not carry
|
|
33
33
|
* position info when only the parsed object (not raw text) is available. */
|
|
34
|
+
/** A mechanically applicable repair, carried unchanged from whatever produced
|
|
35
|
+
* it (a templating engine, a kind-name suggestion) to every consumer: CLI
|
|
36
|
+
* JSON, IDE CodeActions, an agent applying it without re-deriving it from
|
|
37
|
+
* prose.
|
|
38
|
+
*
|
|
39
|
+
* `replacement` is the **whole** value at the diagnostic's `path`, corrected —
|
|
40
|
+
* never a fragment — so applying it needs no knowledge of the language inside.
|
|
41
|
+
* There is deliberately no sub-range: carrying one beside a whole-value
|
|
42
|
+
* replacement gives the field two readings, and the minimal-edit reading
|
|
43
|
+
* (splice `replacement` at `range`) produces garbage because the two measure
|
|
44
|
+
* different strings.
|
|
45
|
+
*
|
|
46
|
+
* One shape rather than one per producer: a `fix` field beside a
|
|
47
|
+
* `suggestedKind` field beside a CEL-specific one would leave every host
|
|
48
|
+
* wiring a separate action path for what is the same gesture. */
|
|
49
|
+
export interface DiagnosticFix {
|
|
50
|
+
readonly replacement: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** The `data` stamp diagnostics carry. Loose by design — passes bolt their own
|
|
54
|
+
* keys on — but the fields every consumer reads are declared. */
|
|
55
|
+
export interface DiagnosticData {
|
|
56
|
+
resource?: { kind: string; name: string };
|
|
57
|
+
filePath?: string;
|
|
58
|
+
/** Dotted path of the offending value within its resource. */
|
|
59
|
+
path?: string;
|
|
60
|
+
fix?: DiagnosticFix;
|
|
61
|
+
[key: string]: unknown;
|
|
62
|
+
}
|
|
63
|
+
|
|
34
64
|
export interface AnalysisDiagnostic {
|
|
35
65
|
range?: Range;
|
|
36
66
|
severity?: DiagnosticSeverity;
|
|
@@ -42,6 +72,13 @@ export interface AnalysisDiagnostic {
|
|
|
42
72
|
data?: unknown;
|
|
43
73
|
}
|
|
44
74
|
|
|
75
|
+
/** Single reader for a diagnostic's fix, so no consumer re-derives the shape
|
|
76
|
+
* by hand-casting `data`. */
|
|
77
|
+
export function diagnosticFix(d: AnalysisDiagnostic): DiagnosticFix | undefined {
|
|
78
|
+
const fix = (d.data as DiagnosticData | undefined)?.fix;
|
|
79
|
+
return fix && typeof fix.replacement === "string" ? fix : undefined;
|
|
80
|
+
}
|
|
81
|
+
|
|
45
82
|
export interface ManifestSource {
|
|
46
83
|
supports(url: string): boolean;
|
|
47
84
|
read(url: string): Promise<{ text: string; source: string }>;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { ResourceManifest } from "@telorun/sdk";
|
|
2
|
+
import { INCLUDE_ENGINE_NAMES, walkCelExpressions } from "@telorun/templating";
|
|
3
|
+
import { DiagnosticSeverity, type AnalysisDiagnostic } from "./types.js";
|
|
4
|
+
|
|
5
|
+
const SOURCE = "telo-analyzer";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Docs that are never instantiated as resources.
|
|
9
|
+
*
|
|
10
|
+
* `Telo.Application` / `Telo.Library` are module declarations, and `Telo.Import`
|
|
11
|
+
* is a dependency edge — none of the three reaches a controller's `create()`.
|
|
12
|
+
* `Telo.Definition` and `Telo.Abstract` are deliberately absent: a definition's
|
|
13
|
+
* template body (`resources:` / `invoke:` / `run:` / `provide:`) DOES become
|
|
14
|
+
* resources, so an embed there resolves normally.
|
|
15
|
+
*/
|
|
16
|
+
const NEVER_INSTANTIATED: ReadonlySet<string> = new Set([
|
|
17
|
+
"Telo.Application",
|
|
18
|
+
"Telo.Library",
|
|
19
|
+
"Telo.Import",
|
|
20
|
+
]);
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* An `!include-text` / `!include-bytes` in a doc that is never instantiated is
|
|
24
|
+
* never read.
|
|
25
|
+
*
|
|
26
|
+
* The two tags resolve when the resource owning them is created — deferred so
|
|
27
|
+
* that loading a manifest does not pull payload layers, which is the property
|
|
28
|
+
* the artifact spec protects by giving `telo.yaml` a layer of its own. The cost
|
|
29
|
+
* of that choice is this dead spot: a doc with no `create()` has no moment at
|
|
30
|
+
* which the file would be read, so the value stays an unresolved marker and
|
|
31
|
+
* whatever reads it sees a sentinel object instead of the file's contents.
|
|
32
|
+
*
|
|
33
|
+
* Nothing else would report it. There is no controller to validate against a
|
|
34
|
+
* schema and no runtime consumer to fail, so the manifest ships looking correct
|
|
35
|
+
* — exactly the silent-no-op failure mode that makes a descriptive field worth
|
|
36
|
+
* checking. Hence an error at the one place that can see it.
|
|
37
|
+
*
|
|
38
|
+
* Deliberately narrow: it reports only where the argument is complete ("this
|
|
39
|
+
* doc is never instantiated"). A JSON-Schema region *inside* a definition —
|
|
40
|
+
* `schema:`, `inputType:` — is equally unreadable, but that is a general
|
|
41
|
+
* question about tags in schema metadata rather than one about these two, and
|
|
42
|
+
* no pass answers it for any tag today.
|
|
43
|
+
*/
|
|
44
|
+
export function validateIncludePlacement(manifests: ResourceManifest[]): AnalysisDiagnostic[] {
|
|
45
|
+
const out: AnalysisDiagnostic[] = [];
|
|
46
|
+
for (const manifest of manifests) {
|
|
47
|
+
if (!NEVER_INSTANTIATED.has(manifest.kind)) continue;
|
|
48
|
+
const name = (manifest.metadata as { name?: string } | undefined)?.name;
|
|
49
|
+
const filePath = (manifest.metadata as { source?: string } | undefined)?.source;
|
|
50
|
+
walkCelExpressions(manifest, "", (source, path, engineName) => {
|
|
51
|
+
if (!INCLUDE_ENGINE_NAMES.has(engineName)) return;
|
|
52
|
+
out.push({
|
|
53
|
+
severity: DiagnosticSeverity.Error,
|
|
54
|
+
code: "INCLUDE_OUTSIDE_RESOURCE",
|
|
55
|
+
source: SOURCE,
|
|
56
|
+
message:
|
|
57
|
+
`${manifest.kind}${name ? `/${name}` : ""}: \`!${engineName} ${source}\` at '${path}' is ` +
|
|
58
|
+
`never read — a ${manifest.kind} doc is not instantiated, and a file embed is resolved ` +
|
|
59
|
+
`when the resource holding it is created. Move it onto the resource that needs the ` +
|
|
60
|
+
`file, or read the file at runtime with Fs.File.`,
|
|
61
|
+
data: {
|
|
62
|
+
resource: { kind: manifest.kind, name: name ?? "" },
|
|
63
|
+
filePath,
|
|
64
|
+
path,
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
return out;
|
|
70
|
+
}
|
|
@@ -238,6 +238,7 @@ function checkCatchesCoverage(
|
|
|
238
238
|
filePath: string | undefined,
|
|
239
239
|
arrayPath: string,
|
|
240
240
|
env: Environment,
|
|
241
|
+
handler: { kind: string; name?: string } | null,
|
|
241
242
|
): AnalysisDiagnostic[] {
|
|
242
243
|
const diagnostics: AnalysisDiagnostic[] = [];
|
|
243
244
|
const declaredCodes = new Set(union.codes.keys());
|
|
@@ -288,16 +289,20 @@ function checkCatchesCoverage(
|
|
|
288
289
|
}
|
|
289
290
|
|
|
290
291
|
if (!hasCatchAll) {
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
292
|
+
// One diagnostic per block, not per code: every uncovered code sits at the
|
|
293
|
+
// same `catches:` array, and one catch-all answers all of them at once. A
|
|
294
|
+
// diagnostic each repeated the same location and the same fix N times.
|
|
295
|
+
const uncovered = [...declaredCodes].filter((c) => !covered.has(c)).sort();
|
|
296
|
+
if (uncovered.length > 0) {
|
|
297
|
+
diagnostics.push({
|
|
298
|
+
severity: DiagnosticSeverity.Error,
|
|
299
|
+
code: "UNCOVERED_THROW_CODE",
|
|
300
|
+
source: SOURCE,
|
|
301
|
+
message:
|
|
302
|
+
`handler ${handler?.name ? `\`!ref ${handler.name}\`` : `\`${handler?.kind ?? "?"}\``} can throw ${uncovered.length} code${uncovered.length === 1 ? "" : "s"} that no catches: entry handles: ${uncovered.map((c) => `'${c}'`).join(", ")}. ` +
|
|
303
|
+
`Give each a matching \`when:\` (e.g. \`when: !cel "error.code == '${uncovered[0]}'"\`), or add a catch-all entry — one with no \`when:\`, placed last.`,
|
|
304
|
+
data: { resource, filePath, path: arrayPath, uncovered },
|
|
305
|
+
});
|
|
301
306
|
}
|
|
302
307
|
}
|
|
303
308
|
|
|
@@ -552,7 +557,7 @@ export function validateThrowsCoverage(
|
|
|
552
557
|
const handlerRef = resolveHandlerRef(siblingData[catchesFor]);
|
|
553
558
|
const union = handlerRefUnion(handlerRef, manifests, resolveCtx, scopeResolver);
|
|
554
559
|
diagnostics.push(
|
|
555
|
-
...checkCatchesCoverage(entries, union, resource, filePath, arrayPath, env),
|
|
560
|
+
...checkCatchesCoverage(entries, union, resource, filePath, arrayPath, env, handlerRef),
|
|
556
561
|
);
|
|
557
562
|
diagnostics.push(
|
|
558
563
|
...checkTypedErrorData(entries, union, resource, filePath, arrayPath, env),
|