@telorun/templating 0.107.0 → 0.109.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 (60) hide show
  1. package/dist/builtins.d.ts +2 -2
  2. package/dist/builtins.d.ts.map +1 -1
  3. package/dist/cel/analyze.d.ts +6 -6
  4. package/dist/cel/analyze.d.ts.map +1 -1
  5. package/dist/cel/analyze.js +138 -183
  6. package/dist/cel/compile.d.ts +47 -17
  7. package/dist/cel/compile.d.ts.map +1 -1
  8. package/dist/cel/compile.js +135 -83
  9. package/dist/cel/diagnose.d.ts +52 -57
  10. package/dist/cel/diagnose.d.ts.map +1 -1
  11. package/dist/cel/diagnose.js +69 -291
  12. package/dist/cel/environment.d.ts +33 -37
  13. package/dist/cel/environment.d.ts.map +1 -1
  14. package/dist/cel/environment.js +54 -86
  15. package/dist/cel/module-call.d.ts +68 -79
  16. package/dist/cel/module-call.d.ts.map +1 -1
  17. package/dist/cel/module-call.js +118 -194
  18. package/dist/cel/value-brands.d.ts +14 -12
  19. package/dist/cel/value-brands.d.ts.map +1 -1
  20. package/dist/cel/value-brands.js +26 -24
  21. package/dist/cel/verdict-codes.d.ts +13 -2
  22. package/dist/cel/verdict-codes.d.ts.map +1 -1
  23. package/dist/cel/verdict-codes.js +20 -1
  24. package/dist/engine.d.ts +4 -4
  25. package/dist/engine.d.ts.map +1 -1
  26. package/dist/engines/cel.d.ts +2 -2
  27. package/dist/engines/cel.d.ts.map +1 -1
  28. package/dist/engines/cel.js +53 -81
  29. package/dist/engines/hole-analysis.d.ts +3 -3
  30. package/dist/engines/hole-analysis.d.ts.map +1 -1
  31. package/dist/engines/interpolate.js +11 -8
  32. package/dist/index.d.ts +4 -5
  33. package/dist/index.d.ts.map +1 -1
  34. package/dist/index.js +8 -5
  35. package/package.json +4 -4
  36. package/src/builtins.ts +2 -2
  37. package/src/cel/analyze.ts +152 -193
  38. package/src/cel/compile.ts +160 -77
  39. package/src/cel/diagnose.ts +115 -370
  40. package/src/cel/environment.ts +64 -87
  41. package/src/cel/module-call.ts +129 -292
  42. package/src/cel/value-brands.ts +26 -25
  43. package/src/cel/verdict-codes.ts +20 -1
  44. package/src/engine.ts +4 -4
  45. package/src/engines/cel.ts +52 -84
  46. package/src/engines/hole-analysis.ts +3 -3
  47. package/src/engines/interpolate.ts +16 -13
  48. package/src/index.ts +8 -11
  49. package/dist/cel/catalog.d.ts +0 -70
  50. package/dist/cel/catalog.d.ts.map +0 -1
  51. package/dist/cel/catalog.js +0 -1320
  52. package/dist/cel/json-prefix-scan.d.ts +0 -17
  53. package/dist/cel/json-prefix-scan.d.ts.map +0 -1
  54. package/dist/cel/json-prefix-scan.js +0 -160
  55. package/dist/cel/value-identity.d.ts +0 -18
  56. package/dist/cel/value-identity.d.ts.map +0 -1
  57. package/dist/cel/value-identity.js +0 -25
  58. package/src/cel/catalog.ts +0 -1455
  59. package/src/cel/json-prefix-scan.ts +0 -152
  60. package/src/cel/value-identity.ts +0 -34
@@ -1,102 +1,185 @@
1
- import type { CompiledValue } from "@telorun/sdk";
2
- import { ParseError, type ASTNode, type Environment } from "@marcbachmann/cel-js";
3
- import { extractAccessChains } from "./analyze.js";
4
- import { CEL_FUNCTIONS } from "./catalog.js";
5
- import { resolveModuleCalls } from "./module-call.js";
1
+ /**
2
+ * Compiling one CEL expression into a `CompiledValue`, and the repeatability verdict on its
3
+ * text.
4
+ *
5
+ * **The module-call rewrite is gone from this package.** It used to happen here, on the
6
+ * parsed tree, through internals the previous engine did not expose — a `setMeta('macro')`
7
+ * redirect that made a qualified call check and evaluate as something else. `@telorun/cel`
8
+ * has `qcall` as a node of its own, produced by a total tree-to-tree pass over the name set
9
+ * (`parseExpression({ namespaces })`), and a `CelExpression` records the set it was resolved
10
+ * under, so an environment refuses a tree resolved for a different site rather than
11
+ * silently answering a different question. What is left here is reading the result.
12
+ */
13
+ import { hostValueOf, type CompiledValue } from "@telorun/sdk";
14
+ import {
15
+ functionCatalog,
16
+ qualifiedCalls,
17
+ rootReferences,
18
+ walkTree,
19
+ type CelEnvironment,
20
+ type CelExpression,
21
+ type CelNode,
22
+ type NamespaceImplementation,
23
+ } from "@telorun/cel";
24
+ import {
25
+ celNamespaceNames,
26
+ MODULE_CALL_DISPATCH_KEY,
27
+ type ModuleCallDispatch,
28
+ } from "./module-call.js";
6
29
 
7
- /** Root variable identifiers an expression reads, from its parsed AST — the
8
- * first element of every member-access chain (`self.table` → `self`). Returns
9
- * undefined when the parse result carries no AST so consumers can tell "no
10
- * refs" from "unknown". */
11
- function rootRefs(ast: ASTNode): readonly string[] {
12
- const roots = new Set<string>();
13
- for (const chain of extractAccessChains(ast)) {
14
- if (chain.length > 0) roots.add(chain[0]!);
15
- }
16
- return [...roots];
17
- }
30
+ const NO_NAMESPACES: ReadonlySet<string> = new Set();
18
31
 
32
+ /** Catalog functions whose result differs per call, by name. */
19
33
  const NON_DETERMINISTIC = new Set(
20
- CEL_FUNCTIONS.filter((f) => !f.deterministic).map((f) => f.name),
34
+ functionCatalog()
35
+ .filter((entry) => !entry.deterministic)
36
+ .map((entry) => entry.name),
21
37
  );
22
38
 
23
- /** True when the expression calls a catalog function whose result differs per
24
- * call (`uuidv4()`, `nowMillis()`), so evaluating it once and reusing the value
25
- * would change what it means. */
26
- function callsNonDeterministic(root: ASTNode): boolean {
27
- const visit = (node: unknown): boolean => {
28
- if (Array.isArray(node)) return node.some(visit);
29
- if (!node || typeof node !== "object" || !("op" in node)) return false;
30
- const n = node as ASTNode;
31
- if ((n.op === "call" || n.op === "rcall") && Array.isArray(n.args)) {
32
- const name = (n.args as unknown[])[0];
33
- if (typeof name === "string" && NON_DETERMINISTIC.has(name)) return true;
39
+ /**
40
+ * True when the expression calls a catalog function whose result differs per call
41
+ * (`uuidv4()`, `nowMillis()`), so evaluating it once and reusing the value would change
42
+ * what it means.
43
+ */
44
+ function callsNonDeterministic(root: CelNode): boolean {
45
+ for (const node of walkTree(root)) {
46
+ if ((node.kind === "call" || node.kind === "receiverCall") && NON_DETERMINISTIC.has(node.name)) {
47
+ return true;
34
48
  }
35
- const args = n.args as unknown;
36
- return Array.isArray(args) ? args.some(visit) : visit(args);
37
- };
38
- return visit(root);
49
+ }
50
+ return false;
39
51
  }
40
52
 
41
- /** True when the expression calls a method on a bare identifier (`Billing.f(x)`).
42
- * Whether that identifier names a module is the declaring module's to say, and
43
- * the text alone does not, so every such call may be a module call. */
44
- function callsOnBareReceiver(root: ASTNode): boolean {
45
- const visit = (node: unknown): boolean => {
46
- if (Array.isArray(node)) return node.some(visit);
47
- if (!node || typeof node !== "object" || !("op" in node)) return false;
48
- const n = node as ASTNode;
49
- if (n.op === "rcall" && Array.isArray(n.args)) {
50
- const receiver = (n.args as unknown[])[1] as { op?: unknown } | null;
51
- if (receiver && typeof receiver === "object" && receiver.op === "id") return true;
52
- }
53
- const args = n.args as unknown;
54
- return Array.isArray(args) ? args.some(visit) : visit(args);
55
- };
56
- return visit(root);
53
+ /**
54
+ * True when the expression calls a method on a bare identifier (`Billing.f(x)`). Whether
55
+ * that identifier names a module is the declaring module's to say, and the text alone does
56
+ * not, so every such call may be a module call.
57
+ *
58
+ * A call the name set DID resolve is a `qcall` and is not this: it is already known to be a
59
+ * module's, and its determinism is its callee's.
60
+ */
61
+ function callsOnBareReceiver(root: CelNode): boolean {
62
+ for (const node of walkTree(root)) {
63
+ if (node.kind === "receiverCall" && node.receiver.kind === "ident") return true;
64
+ }
65
+ return false;
57
66
  }
58
67
 
59
68
  /**
60
- * Whether an expression's text, written twice in one value, means one value
61
- * both times: it calls no catalog function whose result differs per call, and
62
- * makes no call that may be a module's — a module function's determinism is its
63
- * callee's, which the text does not say. Text that does not parse means no
64
- * value, so it is not repeatable; `analyze` is what reports why.
69
+ * Whether an expression's text, written twice in one value, means one value both times: it
70
+ * calls no catalog function whose result differs per call, and makes no call that may be a
71
+ * module's — a module function's determinism is its callee's, which the text does not say.
72
+ *
73
+ * Text that does not read whole means no value, so it is not repeatable; `analyze` is what
74
+ * reports why. Reading never throws here, which is the engine's own contract: the first
75
+ * unreadable thing is one ranged diagnostic and the tree keeps the longest prefix.
65
76
  */
66
- export function repeatableExpression(expr: string, env: Environment): boolean {
67
- let ast: ASTNode;
68
- try {
69
- ast = env.parse(expr).ast;
70
- } catch (error) {
71
- if (error instanceof ParseError) return false;
72
- throw error;
73
- }
74
- return !callsNonDeterministic(ast) && !callsOnBareReceiver(ast);
77
+ export function repeatableExpression(expr: string, environment: CelEnvironment): boolean {
78
+ const parsed = environment.parse(expr);
79
+ if (parsed.diagnostics.length > 0) return false;
80
+ return !callsNonDeterministic(parsed.root) && !callsOnBareReceiver(parsed.root);
75
81
  }
76
82
 
77
- /** Compile a single CEL expression into a CompiledValue. Throws on syntax
78
- * errors. The `!cel` engine compiles its whole scalar with it, and the tags
79
- * with holes compile each hole.
83
+ /**
84
+ * Compile one CEL expression into a `CompiledValue`. The `!cel` engine compiles its whole
85
+ * scalar with it, and the tags with holes compile each hole.
80
86
  *
81
- * `moduleNames` are the declaring module's own names; a call whose receiver is
82
- * one of them resolves to a late-bound module call HERE, on the parsed tree, so
83
- * every later reader — the root references below, the analyzer's passes, the
84
- * kernel's dependency walk — sees the resolved shape rather than re-deriving
85
- * it from the source text. */
87
+ * `moduleNames` are the declaring module's own names; a call whose receiver is one of them
88
+ * is resolved to a `qcall` as the expression is READ, so every later reader — the root
89
+ * references below, the analyzer's passes, the kernel's dependency walk — sees the resolved
90
+ * shape rather than re-deriving it from the source text.
91
+ */
86
92
  export function compileExpression(
87
93
  expr: string,
88
- env: Environment,
94
+ environment: CelEnvironment,
89
95
  moduleNames?: ReadonlySet<string>,
90
96
  ): CompiledValue {
91
- const fn = env.parse(expr);
92
- const ast: ASTNode = fn.ast;
93
- const calls = resolveModuleCalls(ast, moduleNames);
97
+ // Only the names CEL can read as a namespace: a set holding `my-module` is refused WHOLE
98
+ // at registration, and a name that cannot be one resolves no call anyway.
99
+ const namespaces = moduleNames === undefined ? NO_NAMESPACES : celNamespaceNames(moduleNames);
100
+ if (namespaces.size === 0) {
101
+ return compiledValueOf(expr, environment.parse(expr), environment);
102
+ }
103
+ // A clone carrying the site's names: the tree is resolved under them, and the environment
104
+ // that compiles it must declare the same set or it refuses the tree.
105
+ const site = environment.clone();
106
+ for (const name of namespaces) site.registerNamespace(name);
107
+ return compiledValueOf(expr, site.parse(expr), site);
108
+ }
109
+
110
+ /** A `CompiledValue` over an expression already read against its site's names. */
111
+ export function compiledValueOf(
112
+ expr: string,
113
+ parsed: CelExpression,
114
+ environment: CelEnvironment,
115
+ ): CompiledValue {
116
+ const program = environment.compile(parsed);
117
+ // As written — `CompiledValue.calls` is the qualified spellings, not their shapes.
118
+ const calls = qualifiedCalls(parsed.root).map((call) => call.qualifiedName);
94
119
  return {
95
120
  __compiled: true,
96
121
  source: expr,
97
- refs: rootRefs(ast),
122
+ refs: rootReferences(parsed.root),
98
123
  calls,
99
- ...(callsNonDeterministic(ast) ? { volatile: true as const } : {}),
100
- call: (ctx: Record<string, unknown>) => fn(ctx),
124
+ ...(callsNonDeterministic(parsed.root) ? { volatile: true as const } : {}),
125
+ // **A module call is dispatched through the engine's namespace seam**, reading the table
126
+ // the owning scope bound on the activation. Evaluating without it reaches no function at
127
+ // all: the engine would answer its own `unbound_function` for every qualified call,
128
+ // whatever the kernel bound, so the whole dispatch is silently dead.
129
+ call: (ctx: Record<string, unknown>) =>
130
+ hostValueOf(program.evaluate(ctx as never, { namespaceFunction: dispatchFrom(ctx) })),
131
+ };
132
+ }
133
+
134
+ /**
135
+ * How a qualified call reaches its function: the per-scope table the kernel binds on the
136
+ * activation under a key outside CEL's identifier grammar, so no author can call past the
137
+ * export gate or the dependency edge.
138
+ *
139
+ * **A name nothing bound answers `undefined`, and that ordering is load-bearing.** The engine
140
+ * looks a qualified call's function up BEFORE it evaluates the arguments, so returning
141
+ * `undefined` refuses the call where it is written and nothing inside it runs. Answering a
142
+ * stub that throws instead makes the lookup succeed: the arguments are evaluated first, so
143
+ * `Billing.missing(Billing.total(1))` DISPATCHES `Billing.total` — observable work, possibly
144
+ * host-backed — for a call that was never going to reach a function. The vectors pin exactly
145
+ * that (`dispatched: []`), and the repair advice belongs to the static verdict the analyzer
146
+ * reports at the call, which is where an author reads it, not to a runtime message.
147
+ *
148
+ * **A module function's arguments are a host boundary, so they cross through the one seam**
149
+ * (`hostValueOf`). What is behind a qualified call is a `Telo.Callable` — a CEL body whose
150
+ * parameters are AJV-checked against its declared `params`, or a controller's own `call` —
151
+ * and neither reads a `CelMap`: a map literal written at a call site arrived as
152
+ * `{"entries":{}}` and was refused for a property its author had written.
153
+ */
154
+ function dispatchFrom(
155
+ activation: Record<string, unknown>,
156
+ ): (namespace: string, name: string) => NamespaceImplementation | undefined {
157
+ // With no table bound, every qualified call answers `undefined` — the engine's
158
+ // own `unbound_function`, refused before its arguments are evaluated.
159
+ return (
160
+ namespaceDispatchOf(activation[MODULE_CALL_DISPATCH_KEY] as ModuleCallDispatch | undefined) ??
161
+ (() => undefined)
162
+ );
163
+ }
164
+
165
+ /**
166
+ * A scope's module functions as the engine's own per-evaluation dispatch — **the one
167
+ * adapter**, so every host that binds a table converts its arguments the same way.
168
+ *
169
+ * The table is keyed by qualified name (`Billing.total`), the form the kernel and the
170
+ * analyzer both hold it in; the engine asks by namespace and name, never learning that a
171
+ * qualified name is one string with a dot in it. A second adapter is what made `telo check`
172
+ * and the kernel disagree: the analyzer kept its own, called the bound function with the raw
173
+ * values, and handed a map literal written at a call site a `CelMap` where the kernel handed
174
+ * a plain object — a check/run divergence by construction, in the one direction nothing
175
+ * reports.
176
+ */
177
+ export function namespaceDispatchOf(
178
+ table: ModuleCallDispatch | undefined,
179
+ ): ((namespace: string, name: string) => NamespaceImplementation | undefined) | undefined {
180
+ if (!table) return undefined;
181
+ return (namespace, name) => {
182
+ const bound = table.get(`${namespace}.${name}`);
183
+ return bound ? (args) => bound(args.map(hostValueOf)) as never : undefined;
101
184
  };
102
185
  }