@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.
- package/dist/builtins.d.ts +2 -2
- package/dist/builtins.d.ts.map +1 -1
- package/dist/cel/analyze.d.ts +6 -6
- package/dist/cel/analyze.d.ts.map +1 -1
- package/dist/cel/analyze.js +138 -183
- package/dist/cel/compile.d.ts +47 -17
- package/dist/cel/compile.d.ts.map +1 -1
- package/dist/cel/compile.js +135 -83
- package/dist/cel/diagnose.d.ts +52 -57
- package/dist/cel/diagnose.d.ts.map +1 -1
- package/dist/cel/diagnose.js +69 -291
- package/dist/cel/environment.d.ts +33 -37
- package/dist/cel/environment.d.ts.map +1 -1
- package/dist/cel/environment.js +54 -86
- package/dist/cel/module-call.d.ts +68 -79
- package/dist/cel/module-call.d.ts.map +1 -1
- package/dist/cel/module-call.js +118 -194
- package/dist/cel/value-brands.d.ts +14 -12
- package/dist/cel/value-brands.d.ts.map +1 -1
- package/dist/cel/value-brands.js +26 -24
- package/dist/cel/verdict-codes.d.ts +13 -2
- package/dist/cel/verdict-codes.d.ts.map +1 -1
- package/dist/cel/verdict-codes.js +20 -1
- package/dist/engine.d.ts +4 -4
- package/dist/engine.d.ts.map +1 -1
- package/dist/engines/cel.d.ts +2 -2
- package/dist/engines/cel.d.ts.map +1 -1
- package/dist/engines/cel.js +53 -81
- package/dist/engines/hole-analysis.d.ts +3 -3
- package/dist/engines/hole-analysis.d.ts.map +1 -1
- package/dist/engines/interpolate.js +11 -8
- package/dist/index.d.ts +4 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -5
- package/package.json +4 -4
- package/src/builtins.ts +2 -2
- package/src/cel/analyze.ts +152 -193
- package/src/cel/compile.ts +160 -77
- package/src/cel/diagnose.ts +115 -370
- package/src/cel/environment.ts +64 -87
- package/src/cel/module-call.ts +129 -292
- package/src/cel/value-brands.ts +26 -25
- package/src/cel/verdict-codes.ts +20 -1
- package/src/engine.ts +4 -4
- package/src/engines/cel.ts +52 -84
- package/src/engines/hole-analysis.ts +3 -3
- package/src/engines/interpolate.ts +16 -13
- package/src/index.ts +8 -11
- package/dist/cel/catalog.d.ts +0 -70
- package/dist/cel/catalog.d.ts.map +0 -1
- package/dist/cel/catalog.js +0 -1320
- package/dist/cel/json-prefix-scan.d.ts +0 -17
- package/dist/cel/json-prefix-scan.d.ts.map +0 -1
- package/dist/cel/json-prefix-scan.js +0 -160
- package/dist/cel/value-identity.d.ts +0 -18
- package/dist/cel/value-identity.d.ts.map +0 -1
- package/dist/cel/value-identity.js +0 -25
- package/src/cel/catalog.ts +0 -1455
- package/src/cel/json-prefix-scan.ts +0 -152
- package/src/cel/value-identity.ts +0 -34
package/src/cel/compile.ts
CHANGED
|
@@ -1,102 +1,185 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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
|
-
|
|
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
|
-
|
|
34
|
+
functionCatalog()
|
|
35
|
+
.filter((entry) => !entry.deterministic)
|
|
36
|
+
.map((entry) => entry.name),
|
|
21
37
|
);
|
|
22
38
|
|
|
23
|
-
/**
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
-
|
|
36
|
-
|
|
37
|
-
};
|
|
38
|
-
return visit(root);
|
|
49
|
+
}
|
|
50
|
+
return false;
|
|
39
51
|
}
|
|
40
52
|
|
|
41
|
-
/**
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
* value, so it is not repeatable; `analyze` is what
|
|
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,
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
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
|
-
/**
|
|
78
|
-
*
|
|
79
|
-
*
|
|
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
|
-
*
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
|
|
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
|
-
|
|
94
|
+
environment: CelEnvironment,
|
|
89
95
|
moduleNames?: ReadonlySet<string>,
|
|
90
96
|
): CompiledValue {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
const
|
|
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:
|
|
122
|
+
refs: rootReferences(parsed.root),
|
|
98
123
|
calls,
|
|
99
|
-
...(callsNonDeterministic(
|
|
100
|
-
call
|
|
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
|
}
|