trilean 1.13.10 → 1.13.11
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 +1 -1
- package/dist/evaluator-budget.cjs +16 -2
- package/dist/evaluator-budget.d.cts +5 -1
- package/dist/evaluator-budget.d.ts +5 -1
- package/dist/evaluator-budget.js +15 -3
- package/dist/evaluator-compiled.cjs +1 -1
- package/dist/evaluator-compiled.js +2 -2
- package/dist/evaluator.cjs +4 -4
- package/dist/evaluator.js +4 -4
- package/dist-cli/trilean.mjs +19 -7
- package/package.json +1 -1
- package/schemas/trilean.schema.json +1 -1
package/README.md
CHANGED
|
@@ -448,7 +448,7 @@ Evaluate `operand` first; if it is indeterminate, the whole leaf is indeterminat
|
|
|
448
448
|
|
|
449
449
|
### `exists`
|
|
450
450
|
|
|
451
|
-
Evaluates `true` if the given `ExpressionNode` can be resolved to some value at all, `false` if it definitely cannot be resolved (the data point is genuinely absent), independent of whether that value would itself be usable in further computation. Concretely: evaluate the operand; if the result is definite, `exists` is `true`; if the result is indeterminate with reason `not-found`, `exists` is `false`; if the result is indeterminate with reason `wrong-type` or `domain-error
|
|
451
|
+
Evaluates `true` if the given `ExpressionNode` can be resolved to some value at all, `false` if it definitely cannot be resolved (the data point is genuinely absent), independent of whether that value would itself be usable in further computation. Concretely: evaluate the operand; if the result is definite, `exists` is `true`; if the result is indeterminate with reason `not-found`, `exists` is `false`; if the result is indeterminate with reason `wrong-type` or `domain-error` (other than an exhausted limit, below), `exists` is still `true` — the underlying data point *did* resolve to something, it merely wasn't usable for whatever computation was attempted around it, which is exactly why section [The evaluation model](#the-evaluation-model) distinguishes "did not exist" from "existed but unusable" in the first place. If the result is indeterminate with reason `not-yet-known`, `exists` is indeterminate with that same reason: the value may still arrive, so neither `true` nor `false` is yet an answer a later evaluation could not contradict. If the result is indeterminate because a resource limit was exhausted (the node budget, the nesting depth or the `treeReference` chain depth), `exists` is indeterminate with that limit's own `domain-error` and message: the operand was not evaluated to its end, so nothing is known about the data point. Every other `domain-error` stays `true`, whatever produced it: an arithmetic or built-in function outside its domain (division, modulo and power by zero, a result out of range or not finite, a logarithm of zero), a registered function's own domain error, a `unique` conditional with more than one matching case, a `max` or `min` fold with no participating item, and a circular `treeReference`. In each the data point resolved to something, or the tree is at fault, and no limit cut the evaluation short; a cycle is detected, not exhausted, so it is not a limit. The one family that propagates is the exhausted resource limits. `exists` is otherwise never indeterminate, and always produces a definite boolean for a value that is known to be present or known to be absent.
|
|
452
452
|
|
|
453
453
|
### `some`, `every`
|
|
454
454
|
|
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
const require_evaluation = require("./evaluation.cjs");
|
|
3
3
|
// src/evaluator-budget.ts
|
|
4
|
+
/** The reason objects that report an exhausted resource limit (node budget, nesting depth, treeReference chain depth). Every such reason has the code `domain-error`, so the code alone cannot tell a limit from any other domain error; membership here is what `exists` reads to let a limit propagate instead of counting as "the value resolved". Identity is preserved by every node that forwards an operand's reason, so a reason keeps its membership as it propagates. */
|
|
5
|
+
const exhaustedReasons = /* @__PURE__ */ new WeakSet();
|
|
6
|
+
/** The indeterminate result for an exhausted resource limit: code `domain-error` with the limit's own message, recorded so `isResourceExhaustion` recognises it. */
|
|
7
|
+
function resourceExhausted(message) {
|
|
8
|
+
const result = require_evaluation.indeterminate("domain-error", message);
|
|
9
|
+
if (result.status === "indeterminate") exhaustedReasons.add(result.reason);
|
|
10
|
+
return result;
|
|
11
|
+
}
|
|
12
|
+
/** Whether `reason` reports an exhausted resource limit, as opposed to any other indeterminacy. */
|
|
13
|
+
function isResourceExhaustion(reason) {
|
|
14
|
+
return exhaustedReasons.has(reason);
|
|
15
|
+
}
|
|
4
16
|
/** A defense-in-depth guard against a long acyclic `treeReference` chain exhausting the call stack -- distinct from, and layered on top of, the cycle detector in evaluator.ts (`visitedTreeKeys`), which catches an actual repeat immediately and more precisely. */
|
|
5
17
|
const MAX_TREE_REFERENCE_DEPTH = 100;
|
|
6
18
|
/** Default cap on the total number of predicate/expression nodes a single `evaluatePredicate`/`evaluateValue` call may visit -- see `createEvaluator`'s `maxNodes` option, threaded through to `createEvaluationBudget` below. Chosen generously above any realistic hand-authored business-rule, eligibility, or formula tree (see README.md's own consumer use cases), while still bounding an untrusted, adversarially-authored tree's total evaluation cost to a fixed, small amount of work regardless of how it is shaped. */
|
|
@@ -13,11 +25,13 @@ function createEvaluationBudget(maxNodes, maxNestingDepth) {
|
|
|
13
25
|
let nodesVisited = 0;
|
|
14
26
|
return { checkNode(nestingDepth) {
|
|
15
27
|
nodesVisited += 1;
|
|
16
|
-
if (nodesVisited > maxNodes) return
|
|
17
|
-
if (nestingDepth >= maxNestingDepth) return
|
|
28
|
+
if (nodesVisited > maxNodes) return resourceExhausted(`evaluation exceeded the maximum of ${maxNodes.toString()} nodes visited in a single call (resource exhausted)`);
|
|
29
|
+
if (nestingDepth >= maxNestingDepth) return resourceExhausted(`evaluation nesting depth exceeds the maximum of ${maxNestingDepth.toString()} (resource exhausted)`);
|
|
18
30
|
} };
|
|
19
31
|
}
|
|
20
32
|
exports.DEFAULT_MAX_EVALUATION_NODES = DEFAULT_MAX_EVALUATION_NODES;
|
|
21
33
|
exports.DEFAULT_MAX_NESTING_DEPTH = DEFAULT_MAX_NESTING_DEPTH;
|
|
22
34
|
exports.MAX_TREE_REFERENCE_DEPTH = MAX_TREE_REFERENCE_DEPTH;
|
|
23
35
|
exports.createEvaluationBudget = createEvaluationBudget;
|
|
36
|
+
exports.isResourceExhaustion = isResourceExhaustion;
|
|
37
|
+
exports.resourceExhausted = resourceExhausted;
|
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
import { Evaluation } from "./evaluation.cjs";
|
|
1
|
+
import { Evaluation, IndeterminateReason } from "./evaluation.cjs";
|
|
2
|
+
/** The indeterminate result for an exhausted resource limit: code `domain-error` with the limit's own message, recorded so `isResourceExhaustion` recognises it. */
|
|
3
|
+
export declare function resourceExhausted(message: string): Evaluation<never>;
|
|
4
|
+
/** Whether `reason` reports an exhausted resource limit, as opposed to any other indeterminacy. */
|
|
5
|
+
export declare function isResourceExhaustion(reason: Readonly<IndeterminateReason>): boolean;
|
|
2
6
|
/** A defense-in-depth guard against a long acyclic `treeReference` chain exhausting the call stack -- distinct from, and layered on top of, the cycle detector in evaluator.ts (`visitedTreeKeys`), which catches an actual repeat immediately and more precisely. */
|
|
3
7
|
export declare const MAX_TREE_REFERENCE_DEPTH = 100;
|
|
4
8
|
/** Default cap on the total number of predicate/expression nodes a single `evaluatePredicate`/`evaluateValue` call may visit -- see `createEvaluator`'s `maxNodes` option, threaded through to `createEvaluationBudget` below. Chosen generously above any realistic hand-authored business-rule, eligibility, or formula tree (see README.md's own consumer use cases), while still bounding an untrusted, adversarially-authored tree's total evaluation cost to a fixed, small amount of work regardless of how it is shaped. */
|
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
import { Evaluation } from "./evaluation.js";
|
|
1
|
+
import { Evaluation, IndeterminateReason } from "./evaluation.js";
|
|
2
|
+
/** The indeterminate result for an exhausted resource limit: code `domain-error` with the limit's own message, recorded so `isResourceExhaustion` recognises it. */
|
|
3
|
+
export declare function resourceExhausted(message: string): Evaluation<never>;
|
|
4
|
+
/** Whether `reason` reports an exhausted resource limit, as opposed to any other indeterminacy. */
|
|
5
|
+
export declare function isResourceExhaustion(reason: Readonly<IndeterminateReason>): boolean;
|
|
2
6
|
/** A defense-in-depth guard against a long acyclic `treeReference` chain exhausting the call stack -- distinct from, and layered on top of, the cycle detector in evaluator.ts (`visitedTreeKeys`), which catches an actual repeat immediately and more precisely. */
|
|
3
7
|
export declare const MAX_TREE_REFERENCE_DEPTH = 100;
|
|
4
8
|
/** Default cap on the total number of predicate/expression nodes a single `evaluatePredicate`/`evaluateValue` call may visit -- see `createEvaluator`'s `maxNodes` option, threaded through to `createEvaluationBudget` below. Chosen generously above any realistic hand-authored business-rule, eligibility, or formula tree (see README.md's own consumer use cases), while still bounding an untrusted, adversarially-authored tree's total evaluation cost to a fixed, small amount of work regardless of how it is shaped. */
|
package/dist/evaluator-budget.js
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
import { indeterminate } from "./evaluation.js";
|
|
2
2
|
// src/evaluator-budget.ts
|
|
3
|
+
/** The reason objects that report an exhausted resource limit (node budget, nesting depth, treeReference chain depth). Every such reason has the code `domain-error`, so the code alone cannot tell a limit from any other domain error; membership here is what `exists` reads to let a limit propagate instead of counting as "the value resolved". Identity is preserved by every node that forwards an operand's reason, so a reason keeps its membership as it propagates. */
|
|
4
|
+
const exhaustedReasons = /* @__PURE__ */ new WeakSet();
|
|
5
|
+
/** The indeterminate result for an exhausted resource limit: code `domain-error` with the limit's own message, recorded so `isResourceExhaustion` recognises it. */
|
|
6
|
+
function resourceExhausted(message) {
|
|
7
|
+
const result = indeterminate("domain-error", message);
|
|
8
|
+
if (result.status === "indeterminate") exhaustedReasons.add(result.reason);
|
|
9
|
+
return result;
|
|
10
|
+
}
|
|
11
|
+
/** Whether `reason` reports an exhausted resource limit, as opposed to any other indeterminacy. */
|
|
12
|
+
function isResourceExhaustion(reason) {
|
|
13
|
+
return exhaustedReasons.has(reason);
|
|
14
|
+
}
|
|
3
15
|
/** A defense-in-depth guard against a long acyclic `treeReference` chain exhausting the call stack -- distinct from, and layered on top of, the cycle detector in evaluator.ts (`visitedTreeKeys`), which catches an actual repeat immediately and more precisely. */
|
|
4
16
|
const MAX_TREE_REFERENCE_DEPTH = 100;
|
|
5
17
|
/** Default cap on the total number of predicate/expression nodes a single `evaluatePredicate`/`evaluateValue` call may visit -- see `createEvaluator`'s `maxNodes` option, threaded through to `createEvaluationBudget` below. Chosen generously above any realistic hand-authored business-rule, eligibility, or formula tree (see README.md's own consumer use cases), while still bounding an untrusted, adversarially-authored tree's total evaluation cost to a fixed, small amount of work regardless of how it is shaped. */
|
|
@@ -12,8 +24,8 @@ function createEvaluationBudget(maxNodes, maxNestingDepth) {
|
|
|
12
24
|
let nodesVisited = 0;
|
|
13
25
|
return { checkNode(nestingDepth) {
|
|
14
26
|
nodesVisited += 1;
|
|
15
|
-
if (nodesVisited > maxNodes) return
|
|
16
|
-
if (nestingDepth >= maxNestingDepth) return
|
|
27
|
+
if (nodesVisited > maxNodes) return resourceExhausted(`evaluation exceeded the maximum of ${maxNodes.toString()} nodes visited in a single call (resource exhausted)`);
|
|
28
|
+
if (nestingDepth >= maxNestingDepth) return resourceExhausted(`evaluation nesting depth exceeds the maximum of ${maxNestingDepth.toString()} (resource exhausted)`);
|
|
17
29
|
} };
|
|
18
30
|
}
|
|
19
|
-
export { DEFAULT_MAX_EVALUATION_NODES, DEFAULT_MAX_NESTING_DEPTH, MAX_TREE_REFERENCE_DEPTH, createEvaluationBudget };
|
|
31
|
+
export { DEFAULT_MAX_EVALUATION_NODES, DEFAULT_MAX_NESTING_DEPTH, MAX_TREE_REFERENCE_DEPTH, createEvaluationBudget, isResourceExhaustion, resourceExhausted };
|
|
@@ -142,7 +142,7 @@ var NodeCompiler = class {
|
|
|
142
142
|
return this.charged(depth, (context, resolvers, budget) => {
|
|
143
143
|
const result = operand(context, resolvers, budget);
|
|
144
144
|
if (result.status === "indeterminate") {
|
|
145
|
-
if (result.reason.code === "not-yet-known") return result;
|
|
145
|
+
if (result.reason.code === "not-yet-known" || require_evaluator_budget.isResourceExhaustion(result.reason)) return result;
|
|
146
146
|
if (result.reason.code === "not-found") return require_evaluation.definite(false);
|
|
147
147
|
}
|
|
148
148
|
return require_evaluation.definite(true);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { definite, firstIndeterminate, indeterminate } from "./evaluation.js";
|
|
2
|
-
import { createEvaluationBudget } from "./evaluator-budget.js";
|
|
2
|
+
import { createEvaluationBudget, isResourceExhaustion } from "./evaluator-budget.js";
|
|
3
3
|
import { evaluatePredicateInternal, evaluateValueInternal } from "./evaluator.js";
|
|
4
4
|
import { runSync, settleSync } from "./evaluator-effect.js";
|
|
5
5
|
import { literalValue } from "./evaluator-literals.js";
|
|
@@ -141,7 +141,7 @@ var NodeCompiler = class {
|
|
|
141
141
|
return this.charged(depth, (context, resolvers, budget) => {
|
|
142
142
|
const result = operand(context, resolvers, budget);
|
|
143
143
|
if (result.status === "indeterminate") {
|
|
144
|
-
if (result.reason.code === "not-yet-known") return result;
|
|
144
|
+
if (result.reason.code === "not-yet-known" || isResourceExhaustion(result.reason)) return result;
|
|
145
145
|
if (result.reason.code === "not-found") return definite(false);
|
|
146
146
|
}
|
|
147
147
|
return definite(true);
|
package/dist/evaluator.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
const require_evaluation = require("./evaluation.cjs");
|
|
3
|
-
require("./evaluator-budget.cjs");
|
|
3
|
+
const require_evaluator_budget = require("./evaluator-budget.cjs");
|
|
4
4
|
const require_evaluator_collection = require("./evaluator-collection.cjs");
|
|
5
5
|
const require_evaluator_operations = require("./evaluator-operations.cjs");
|
|
6
6
|
const require_evaluator_literals = require("./evaluator-literals.cjs");
|
|
@@ -60,7 +60,7 @@ function* evaluatePredicateInternal(node, context, resolvers, accumulator, funct
|
|
|
60
60
|
case "exists": {
|
|
61
61
|
const operandResult = yield* evaluateValueInternal(node.operand, context, resolvers, accumulator, functions, visitedTreeKeys, treeReferenceDepth, budget, nestingDepth + 1);
|
|
62
62
|
if (operandResult.status === "indeterminate") {
|
|
63
|
-
if (operandResult.reason.code === "not-yet-known") return operandResult;
|
|
63
|
+
if (operandResult.reason.code === "not-yet-known" || require_evaluator_budget.isResourceExhaustion(operandResult.reason)) return operandResult;
|
|
64
64
|
if (operandResult.reason.code === "not-found") return require_evaluation.definite(false);
|
|
65
65
|
}
|
|
66
66
|
return require_evaluation.definite(true);
|
|
@@ -81,7 +81,7 @@ function* evaluatePredicateInternal(node, context, resolvers, accumulator, funct
|
|
|
81
81
|
if (resolvers.resolveTree === void 0) return require_evaluation.indeterminate("wrong-type", "no tree resolver registered for treeReference nodes");
|
|
82
82
|
const keyString = JSON.stringify(node.key);
|
|
83
83
|
if (visitedTreeKeys.has(keyString)) return require_evaluation.indeterminate("domain-error", "circular treeReference detected");
|
|
84
|
-
if (treeReferenceDepth >= 100) return
|
|
84
|
+
if (treeReferenceDepth >= 100) return require_evaluator_budget.resourceExhausted(`treeReference chain exceeds the maximum depth of ${100 .toString()}`);
|
|
85
85
|
const resolution = yield* require_evaluator_effect.wait(resolvers.resolveTree(node.key, context));
|
|
86
86
|
if (!resolution.found) return require_evaluator_operations.absenceEvaluation(resolution, "treeReference key did not resolve to a tree", "the tree for the treeReference key is not yet known");
|
|
87
87
|
const parsed = require_tree.PredicateNodeSchema.safeParse(resolution.node);
|
|
@@ -214,7 +214,7 @@ function* evaluateValueInternal(node, context, resolvers, accumulator, functions
|
|
|
214
214
|
if (resolvers.resolveTree === void 0) return require_evaluation.indeterminate("wrong-type", "no tree resolver registered for treeReference nodes");
|
|
215
215
|
const keyString = JSON.stringify(node.key);
|
|
216
216
|
if (visitedTreeKeys.has(keyString)) return require_evaluation.indeterminate("domain-error", "circular treeReference detected");
|
|
217
|
-
if (treeReferenceDepth >= 100) return
|
|
217
|
+
if (treeReferenceDepth >= 100) return require_evaluator_budget.resourceExhausted(`treeReference chain exceeds the maximum depth of ${100 .toString()}`);
|
|
218
218
|
const resolution = yield* require_evaluator_effect.wait(resolvers.resolveTree(node.key, context));
|
|
219
219
|
if (!resolution.found) return require_evaluator_operations.absenceEvaluation(resolution, "treeReference key did not resolve to a tree", "the tree for the treeReference key is not yet known");
|
|
220
220
|
const parsed = require_tree.ExpressionNodeSchema.safeParse(resolution.node);
|
package/dist/evaluator.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { definite, firstIndeterminate, indeterminate } from "./evaluation.js";
|
|
2
|
-
import "./evaluator-budget.js";
|
|
2
|
+
import { isResourceExhaustion, resourceExhausted } from "./evaluator-budget.js";
|
|
3
3
|
import { firstFilterIndeterminate, resolveParticipatingItems } from "./evaluator-collection.js";
|
|
4
4
|
import { absenceEvaluation, applyArithmetic, applyNegate, checkReferenceUnit, combineAnd, combineOr, compareText, compareValues, computeMembershipMatch, invokeRegisteredFunction } from "./evaluator-operations.js";
|
|
5
5
|
import { literalValue } from "./evaluator-literals.js";
|
|
@@ -59,7 +59,7 @@ function* evaluatePredicateInternal(node, context, resolvers, accumulator, funct
|
|
|
59
59
|
case "exists": {
|
|
60
60
|
const operandResult = yield* evaluateValueInternal(node.operand, context, resolvers, accumulator, functions, visitedTreeKeys, treeReferenceDepth, budget, nestingDepth + 1);
|
|
61
61
|
if (operandResult.status === "indeterminate") {
|
|
62
|
-
if (operandResult.reason.code === "not-yet-known") return operandResult;
|
|
62
|
+
if (operandResult.reason.code === "not-yet-known" || isResourceExhaustion(operandResult.reason)) return operandResult;
|
|
63
63
|
if (operandResult.reason.code === "not-found") return definite(false);
|
|
64
64
|
}
|
|
65
65
|
return definite(true);
|
|
@@ -80,7 +80,7 @@ function* evaluatePredicateInternal(node, context, resolvers, accumulator, funct
|
|
|
80
80
|
if (resolvers.resolveTree === void 0) return indeterminate("wrong-type", "no tree resolver registered for treeReference nodes");
|
|
81
81
|
const keyString = JSON.stringify(node.key);
|
|
82
82
|
if (visitedTreeKeys.has(keyString)) return indeterminate("domain-error", "circular treeReference detected");
|
|
83
|
-
if (treeReferenceDepth >= 100) return
|
|
83
|
+
if (treeReferenceDepth >= 100) return resourceExhausted(`treeReference chain exceeds the maximum depth of ${100 .toString()}`);
|
|
84
84
|
const resolution = yield* wait(resolvers.resolveTree(node.key, context));
|
|
85
85
|
if (!resolution.found) return absenceEvaluation(resolution, "treeReference key did not resolve to a tree", "the tree for the treeReference key is not yet known");
|
|
86
86
|
const parsed = PredicateNodeSchema.safeParse(resolution.node);
|
|
@@ -213,7 +213,7 @@ function* evaluateValueInternal(node, context, resolvers, accumulator, functions
|
|
|
213
213
|
if (resolvers.resolveTree === void 0) return indeterminate("wrong-type", "no tree resolver registered for treeReference nodes");
|
|
214
214
|
const keyString = JSON.stringify(node.key);
|
|
215
215
|
if (visitedTreeKeys.has(keyString)) return indeterminate("domain-error", "circular treeReference detected");
|
|
216
|
-
if (treeReferenceDepth >= 100) return
|
|
216
|
+
if (treeReferenceDepth >= 100) return resourceExhausted(`treeReference chain exceeds the maximum depth of ${100 .toString()}`);
|
|
217
217
|
const resolution = yield* wait(resolvers.resolveTree(node.key, context));
|
|
218
218
|
if (!resolution.found) return absenceEvaluation(resolution, "treeReference key did not resolve to a tree", "the tree for the treeReference key is not yet known");
|
|
219
219
|
const parsed = ExpressionNodeSchema.safeParse(resolution.node);
|
package/dist-cli/trilean.mjs
CHANGED
|
@@ -4,7 +4,7 @@ import { parseArgs } from "node:util";
|
|
|
4
4
|
import { z } from "zod";
|
|
5
5
|
import { RegexParseError, compilePattern } from "trilean-regex";
|
|
6
6
|
// package.json
|
|
7
|
-
var version = "1.13.
|
|
7
|
+
var version = "1.13.11";
|
|
8
8
|
// src/computed-value.ts
|
|
9
9
|
/** Dimension symbol -\> exponent, e.g. `{ m: 1, s: -1 }` for metres per second. A bare symbol like `"kg"` is shorthand for `{ kg: 1 }`. */
|
|
10
10
|
const UnitSchema = z.record(z.string(), z.number());
|
|
@@ -118,6 +118,18 @@ function firstIndeterminate(...results) {
|
|
|
118
118
|
return first;
|
|
119
119
|
}
|
|
120
120
|
// src/evaluator-budget.ts
|
|
121
|
+
/** The reason objects that report an exhausted resource limit (node budget, nesting depth, treeReference chain depth). Every such reason has the code `domain-error`, so the code alone cannot tell a limit from any other domain error; membership here is what `exists` reads to let a limit propagate instead of counting as "the value resolved". Identity is preserved by every node that forwards an operand's reason, so a reason keeps its membership as it propagates. */
|
|
122
|
+
const exhaustedReasons = /* @__PURE__ */ new WeakSet();
|
|
123
|
+
/** The indeterminate result for an exhausted resource limit: code `domain-error` with the limit's own message, recorded so `isResourceExhaustion` recognises it. */
|
|
124
|
+
function resourceExhausted(message) {
|
|
125
|
+
const result = indeterminate("domain-error", message);
|
|
126
|
+
if (result.status === "indeterminate") exhaustedReasons.add(result.reason);
|
|
127
|
+
return result;
|
|
128
|
+
}
|
|
129
|
+
/** Whether `reason` reports an exhausted resource limit, as opposed to any other indeterminacy. */
|
|
130
|
+
function isResourceExhaustion(reason) {
|
|
131
|
+
return exhaustedReasons.has(reason);
|
|
132
|
+
}
|
|
121
133
|
/** Default cap on the total number of predicate/expression nodes a single `evaluatePredicate`/`evaluateValue` call may visit -- see `createEvaluator`'s `maxNodes` option, threaded through to `createEvaluationBudget` below. Chosen generously above any realistic hand-authored business-rule, eligibility, or formula tree (see README.md's own consumer use cases), while still bounding an untrusted, adversarially-authored tree's total evaluation cost to a fixed, small amount of work regardless of how it is shaped. */
|
|
122
134
|
const DEFAULT_MAX_EVALUATION_NODES = 1e4;
|
|
123
135
|
/** Constructs a fresh `EvaluationBudget` for one top-level `evaluatePredicate`/`evaluateValue` call -- see `createEvaluator`'s `maxNodes`/`maxNestingDepth` options, which supply `maxNodes`/`maxNestingDepth` here. A fresh closure per call is what keeps `nodesVisited` from leaking between unrelated evaluations. */
|
|
@@ -126,8 +138,8 @@ function createEvaluationBudget(maxNodes, maxNestingDepth) {
|
|
|
126
138
|
let nodesVisited = 0;
|
|
127
139
|
return { checkNode(nestingDepth) {
|
|
128
140
|
nodesVisited += 1;
|
|
129
|
-
if (nodesVisited > maxNodes) return
|
|
130
|
-
if (nestingDepth >= maxNestingDepth) return
|
|
141
|
+
if (nodesVisited > maxNodes) return resourceExhausted(`evaluation exceeded the maximum of ${maxNodes.toString()} nodes visited in a single call (resource exhausted)`);
|
|
142
|
+
if (nestingDepth >= maxNestingDepth) return resourceExhausted(`evaluation nesting depth exceeds the maximum of ${maxNestingDepth.toString()} (resource exhausted)`);
|
|
131
143
|
} };
|
|
132
144
|
}
|
|
133
145
|
// src/evaluator-effect.ts
|
|
@@ -1184,7 +1196,7 @@ function* evaluatePredicateInternal(node, context, resolvers, accumulator, funct
|
|
|
1184
1196
|
case "exists": {
|
|
1185
1197
|
const operandResult = yield* evaluateValueInternal(node.operand, context, resolvers, accumulator, functions, visitedTreeKeys, treeReferenceDepth, budget, nestingDepth + 1);
|
|
1186
1198
|
if (operandResult.status === "indeterminate") {
|
|
1187
|
-
if (operandResult.reason.code === "not-yet-known") return operandResult;
|
|
1199
|
+
if (operandResult.reason.code === "not-yet-known" || isResourceExhaustion(operandResult.reason)) return operandResult;
|
|
1188
1200
|
if (operandResult.reason.code === "not-found") return definite(false);
|
|
1189
1201
|
}
|
|
1190
1202
|
return definite(true);
|
|
@@ -1204,7 +1216,7 @@ function* evaluatePredicateInternal(node, context, resolvers, accumulator, funct
|
|
|
1204
1216
|
if (resolvers.resolveTree === void 0) return indeterminate("wrong-type", "no tree resolver registered for treeReference nodes");
|
|
1205
1217
|
const keyString = JSON.stringify(node.key);
|
|
1206
1218
|
if (visitedTreeKeys.has(keyString)) return indeterminate("domain-error", "circular treeReference detected");
|
|
1207
|
-
if (treeReferenceDepth >= 100) return
|
|
1219
|
+
if (treeReferenceDepth >= 100) return resourceExhausted(`treeReference chain exceeds the maximum depth of ${100 .toString()}`);
|
|
1208
1220
|
const resolution = yield* wait(resolvers.resolveTree(node.key, context));
|
|
1209
1221
|
if (!resolution.found) return absenceEvaluation(resolution, "treeReference key did not resolve to a tree", "the tree for the treeReference key is not yet known");
|
|
1210
1222
|
const parsed = PredicateNodeSchema.safeParse(resolution.node);
|
|
@@ -1337,7 +1349,7 @@ function* evaluateValueInternal(node, context, resolvers, accumulator, functions
|
|
|
1337
1349
|
if (resolvers.resolveTree === void 0) return indeterminate("wrong-type", "no tree resolver registered for treeReference nodes");
|
|
1338
1350
|
const keyString = JSON.stringify(node.key);
|
|
1339
1351
|
if (visitedTreeKeys.has(keyString)) return indeterminate("domain-error", "circular treeReference detected");
|
|
1340
|
-
if (treeReferenceDepth >= 100) return
|
|
1352
|
+
if (treeReferenceDepth >= 100) return resourceExhausted(`treeReference chain exceeds the maximum depth of ${100 .toString()}`);
|
|
1341
1353
|
const resolution = yield* wait(resolvers.resolveTree(node.key, context));
|
|
1342
1354
|
if (!resolution.found) return absenceEvaluation(resolution, "treeReference key did not resolve to a tree", "the tree for the treeReference key is not yet known");
|
|
1343
1355
|
const parsed = ExpressionNodeSchema.safeParse(resolution.node);
|
|
@@ -1480,7 +1492,7 @@ var NodeCompiler = class {
|
|
|
1480
1492
|
return this.charged(depth, (context, resolvers, budget) => {
|
|
1481
1493
|
const result = operand(context, resolvers, budget);
|
|
1482
1494
|
if (result.status === "indeterminate") {
|
|
1483
|
-
if (result.reason.code === "not-yet-known") return result;
|
|
1495
|
+
if (result.reason.code === "not-yet-known" || isResourceExhaustion(result.reason)) return result;
|
|
1484
1496
|
if (result.reason.code === "not-found") return definite(false);
|
|
1485
1497
|
}
|
|
1486
1498
|
return definite(true);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "trilean",
|
|
3
3
|
"description": "Three-valued predicate and expression evaluation trees, stored as JSON and evaluated against injected resolvers, for domain logic — business rules, eligibility checks, formulas, search filters, and more — that needs to be data instead of code.",
|
|
4
|
-
"version": "1.13.
|
|
4
|
+
"version": "1.13.11",
|
|
5
5
|
"bin": {
|
|
6
6
|
"trilean": "./dist-cli/trilean.mjs"
|
|
7
7
|
},
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"$defs":{"ExpressionNode":{"anyOf":[{"oneOf":[{"additionalProperties":false,"properties":{"kind":{"const":"numberLiteral","type":"string"},"unit":{"additionalProperties":{"type":"number"},"propertyNames":{"type":"string"},"type":"object"},"value":{"type":"number"}},"required":["kind","value"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"textLiteral","type":"string"},"value":{"type":"string"}},"required":["kind","value"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"booleanLiteral","type":"string"},"value":{"type":"boolean"}},"required":["kind","value"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"instantLiteral","type":"string"},"value":{"type":"string"}},"required":["kind","value"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"durationLiteral","type":"string"},"unit":{"enum":["ms","s","min","h","d"],"type":"string"},"value":{"type":"number"}},"required":["kind","value","unit"],"type":"object"},{"additionalProperties":false,"properties":{"key":{"$ref":"#/$defs/schema0"},"kind":{"const":"reference","type":"string"},"unit":{"additionalProperties":{"type":"number"},"propertyNames":{"type":"string"},"type":"object"}},"required":["kind","key"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"arithmetic","type":"string"},"left":{"$ref":"#/$defs/ExpressionNode"},"op":{"enum":["add","subtract","multiply","divide","power","modulo"],"type":"string"},"right":{"$ref":"#/$defs/ExpressionNode"}},"required":["kind","op","left","right"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"negate","type":"string"},"operand":{"$ref":"#/$defs/ExpressionNode"}},"required":["kind","operand"],"type":"object"},{"additionalProperties":false,"properties":{"args":{"items":{"$ref":"#/$defs/ExpressionNode"},"type":"array"},"fn":{"type":"string"},"kind":{"const":"call","type":"string"}},"required":["kind","fn","args"],"type":"object"},{"additionalProperties":false,"properties":{"keys":{"items":{"$ref":"#/$defs/ExpressionNode"},"type":"array"},"kind":{"const":"lookup","type":"string"},"table":{"$ref":"#/$defs/schema0"}},"required":["kind","table","keys"],"type":"object"},{"additionalProperties":false,"properties":{"cases":{"items":{"additionalProperties":false,"properties":{"then":{"$ref":"#/$defs/ExpressionNode"},"when":{"$ref":"#/$defs/PredicateNode"}},"required":["when","then"],"type":"object"},"type":"array"},"fallback":{"$ref":"#/$defs/ExpressionNode"},"hitPolicy":{"enum":["first","unique"],"type":"string"},"kind":{"const":"conditional","type":"string"}},"required":["kind","cases","fallback"],"type":"object"},{"additionalProperties":false,"properties":{"collection":{"$ref":"#/$defs/schema0"},"combiner":{"oneOf":[{"additionalProperties":false,"properties":{"item":{"$ref":"#/$defs/ExpressionNode"},"mode":{"const":"max","type":"string"}},"required":["mode","item"],"type":"object"},{"additionalProperties":false,"properties":{"item":{"$ref":"#/$defs/ExpressionNode"},"mode":{"const":"min","type":"string"}},"required":["mode","item"],"type":"object"},{"additionalProperties":false,"properties":{"combine":{"$ref":"#/$defs/ExpressionNode"},"initial":{"$ref":"#/$defs/ExpressionNode"},"mode":{"const":"reduce","type":"string"}},"required":["mode","initial","combine"],"type":"object"}]},"filter":{"$ref":"#/$defs/PredicateNode"},"kind":{"const":"fold","type":"string"}},"required":["kind","collection","combiner"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"accumulator","type":"string"}},"required":["kind"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"delegate","type":"string"},"payload":{"$ref":"#/$defs/schema0"},"system":{"type":"string"}},"required":["kind","system","payload"],"type":"object"},{"additionalProperties":false,"properties":{"key":{"$ref":"#/$defs/schema0"},"kind":{"const":"treeReference","type":"string"}},"required":["kind","key"],"type":"object"}]},{"anyOf":[{"additionalProperties":false,"properties":{"im":{"type":"number"},"kind":{"const":"complexLiteral","type":"string"},"re":{"type":"number"},"unit":{"additionalProperties":{"type":"number"},"propertyNames":{"type":"string"},"type":"object"}},"required":["kind","re","im"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"complexLiteral","type":"string"},"magnitude":{"type":"number"},"phase":{"type":"number"},"unit":{"additionalProperties":{"type":"number"},"propertyNames":{"type":"string"},"type":"object"}},"required":["kind","magnitude","phase"],"type":"object"}]}]},"PredicateNode":{"oneOf":[{"additionalProperties":false,"properties":{"kind":{"const":"not","type":"string"},"operand":{"$ref":"#/$defs/PredicateNode"}},"required":["kind","operand"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"and","type":"string"},"left":{"$ref":"#/$defs/PredicateNode"},"right":{"$ref":"#/$defs/PredicateNode"}},"required":["kind","left","right"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"or","type":"string"},"left":{"$ref":"#/$defs/PredicateNode"},"right":{"$ref":"#/$defs/PredicateNode"}},"required":["kind","left","right"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"allOf","type":"string"},"operands":{"items":{"$ref":"#/$defs/PredicateNode"},"type":"array"}},"required":["kind","operands"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"anyOf","type":"string"},"operands":{"items":{"$ref":"#/$defs/PredicateNode"},"type":"array"}},"required":["kind","operands"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"compare","type":"string"},"left":{"$ref":"#/$defs/ExpressionNode"},"op":{"enum":["gt","gte","lt","lte","eq","neq"],"type":"string"},"right":{"$ref":"#/$defs/ExpressionNode"}},"required":["kind","op","left","right"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"textCompare","type":"string"},"left":{"$ref":"#/$defs/ExpressionNode"},"op":{"enum":["equals","notEquals","matches","notMatches","portableMatches","portableNotMatches"],"type":"string"},"right":{"$ref":"#/$defs/ExpressionNode"}},"required":["kind","op","left","right"],"type":"object"},{"additionalProperties":false,"properties":{"candidates":{"items":{"$ref":"#/$defs/ExpressionNode"},"type":"array"},"kind":{"const":"memberOf","type":"string"},"op":{"enum":["in","notIn"],"type":"string"},"operand":{"$ref":"#/$defs/ExpressionNode"}},"required":["kind","op","operand","candidates"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"exists","type":"string"},"operand":{"$ref":"#/$defs/ExpressionNode"}},"required":["kind","operand"],"type":"object"},{"additionalProperties":false,"properties":{"collection":{"$ref":"#/$defs/schema0"},"filter":{"$ref":"#/$defs/PredicateNode"},"item":{"$ref":"#/$defs/PredicateNode"},"kind":{"const":"some","type":"string"}},"required":["kind","collection","item"],"type":"object"},{"additionalProperties":false,"properties":{"collection":{"$ref":"#/$defs/schema0"},"filter":{"$ref":"#/$defs/PredicateNode"},"item":{"$ref":"#/$defs/PredicateNode"},"kind":{"const":"every","type":"string"}},"required":["kind","collection","item"],"type":"object"},{"additionalProperties":false,"properties":{"key":{"$ref":"#/$defs/schema0"},"kind":{"const":"treeReference","type":"string"}},"required":["kind","key"],"type":"object"}]},"schema0":{"anyOf":[{"type":"string"},{"type":"number"},{"type":"boolean"},{"type":"null"},{"items":{"$ref":"#/$defs/schema0"},"type":"array"},{"additionalProperties":{"$ref":"#/$defs/schema0"},"propertyNames":{"type":"string"},"type":"object"}]}},"$id":"https://cdn.jsdelivr.net/npm/trilean@1.13.
|
|
1
|
+
{"$defs":{"ExpressionNode":{"anyOf":[{"oneOf":[{"additionalProperties":false,"properties":{"kind":{"const":"numberLiteral","type":"string"},"unit":{"additionalProperties":{"type":"number"},"propertyNames":{"type":"string"},"type":"object"},"value":{"type":"number"}},"required":["kind","value"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"textLiteral","type":"string"},"value":{"type":"string"}},"required":["kind","value"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"booleanLiteral","type":"string"},"value":{"type":"boolean"}},"required":["kind","value"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"instantLiteral","type":"string"},"value":{"type":"string"}},"required":["kind","value"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"durationLiteral","type":"string"},"unit":{"enum":["ms","s","min","h","d"],"type":"string"},"value":{"type":"number"}},"required":["kind","value","unit"],"type":"object"},{"additionalProperties":false,"properties":{"key":{"$ref":"#/$defs/schema0"},"kind":{"const":"reference","type":"string"},"unit":{"additionalProperties":{"type":"number"},"propertyNames":{"type":"string"},"type":"object"}},"required":["kind","key"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"arithmetic","type":"string"},"left":{"$ref":"#/$defs/ExpressionNode"},"op":{"enum":["add","subtract","multiply","divide","power","modulo"],"type":"string"},"right":{"$ref":"#/$defs/ExpressionNode"}},"required":["kind","op","left","right"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"negate","type":"string"},"operand":{"$ref":"#/$defs/ExpressionNode"}},"required":["kind","operand"],"type":"object"},{"additionalProperties":false,"properties":{"args":{"items":{"$ref":"#/$defs/ExpressionNode"},"type":"array"},"fn":{"type":"string"},"kind":{"const":"call","type":"string"}},"required":["kind","fn","args"],"type":"object"},{"additionalProperties":false,"properties":{"keys":{"items":{"$ref":"#/$defs/ExpressionNode"},"type":"array"},"kind":{"const":"lookup","type":"string"},"table":{"$ref":"#/$defs/schema0"}},"required":["kind","table","keys"],"type":"object"},{"additionalProperties":false,"properties":{"cases":{"items":{"additionalProperties":false,"properties":{"then":{"$ref":"#/$defs/ExpressionNode"},"when":{"$ref":"#/$defs/PredicateNode"}},"required":["when","then"],"type":"object"},"type":"array"},"fallback":{"$ref":"#/$defs/ExpressionNode"},"hitPolicy":{"enum":["first","unique"],"type":"string"},"kind":{"const":"conditional","type":"string"}},"required":["kind","cases","fallback"],"type":"object"},{"additionalProperties":false,"properties":{"collection":{"$ref":"#/$defs/schema0"},"combiner":{"oneOf":[{"additionalProperties":false,"properties":{"item":{"$ref":"#/$defs/ExpressionNode"},"mode":{"const":"max","type":"string"}},"required":["mode","item"],"type":"object"},{"additionalProperties":false,"properties":{"item":{"$ref":"#/$defs/ExpressionNode"},"mode":{"const":"min","type":"string"}},"required":["mode","item"],"type":"object"},{"additionalProperties":false,"properties":{"combine":{"$ref":"#/$defs/ExpressionNode"},"initial":{"$ref":"#/$defs/ExpressionNode"},"mode":{"const":"reduce","type":"string"}},"required":["mode","initial","combine"],"type":"object"}]},"filter":{"$ref":"#/$defs/PredicateNode"},"kind":{"const":"fold","type":"string"}},"required":["kind","collection","combiner"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"accumulator","type":"string"}},"required":["kind"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"delegate","type":"string"},"payload":{"$ref":"#/$defs/schema0"},"system":{"type":"string"}},"required":["kind","system","payload"],"type":"object"},{"additionalProperties":false,"properties":{"key":{"$ref":"#/$defs/schema0"},"kind":{"const":"treeReference","type":"string"}},"required":["kind","key"],"type":"object"}]},{"anyOf":[{"additionalProperties":false,"properties":{"im":{"type":"number"},"kind":{"const":"complexLiteral","type":"string"},"re":{"type":"number"},"unit":{"additionalProperties":{"type":"number"},"propertyNames":{"type":"string"},"type":"object"}},"required":["kind","re","im"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"complexLiteral","type":"string"},"magnitude":{"type":"number"},"phase":{"type":"number"},"unit":{"additionalProperties":{"type":"number"},"propertyNames":{"type":"string"},"type":"object"}},"required":["kind","magnitude","phase"],"type":"object"}]}]},"PredicateNode":{"oneOf":[{"additionalProperties":false,"properties":{"kind":{"const":"not","type":"string"},"operand":{"$ref":"#/$defs/PredicateNode"}},"required":["kind","operand"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"and","type":"string"},"left":{"$ref":"#/$defs/PredicateNode"},"right":{"$ref":"#/$defs/PredicateNode"}},"required":["kind","left","right"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"or","type":"string"},"left":{"$ref":"#/$defs/PredicateNode"},"right":{"$ref":"#/$defs/PredicateNode"}},"required":["kind","left","right"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"allOf","type":"string"},"operands":{"items":{"$ref":"#/$defs/PredicateNode"},"type":"array"}},"required":["kind","operands"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"anyOf","type":"string"},"operands":{"items":{"$ref":"#/$defs/PredicateNode"},"type":"array"}},"required":["kind","operands"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"compare","type":"string"},"left":{"$ref":"#/$defs/ExpressionNode"},"op":{"enum":["gt","gte","lt","lte","eq","neq"],"type":"string"},"right":{"$ref":"#/$defs/ExpressionNode"}},"required":["kind","op","left","right"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"textCompare","type":"string"},"left":{"$ref":"#/$defs/ExpressionNode"},"op":{"enum":["equals","notEquals","matches","notMatches","portableMatches","portableNotMatches"],"type":"string"},"right":{"$ref":"#/$defs/ExpressionNode"}},"required":["kind","op","left","right"],"type":"object"},{"additionalProperties":false,"properties":{"candidates":{"items":{"$ref":"#/$defs/ExpressionNode"},"type":"array"},"kind":{"const":"memberOf","type":"string"},"op":{"enum":["in","notIn"],"type":"string"},"operand":{"$ref":"#/$defs/ExpressionNode"}},"required":["kind","op","operand","candidates"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"exists","type":"string"},"operand":{"$ref":"#/$defs/ExpressionNode"}},"required":["kind","operand"],"type":"object"},{"additionalProperties":false,"properties":{"collection":{"$ref":"#/$defs/schema0"},"filter":{"$ref":"#/$defs/PredicateNode"},"item":{"$ref":"#/$defs/PredicateNode"},"kind":{"const":"some","type":"string"}},"required":["kind","collection","item"],"type":"object"},{"additionalProperties":false,"properties":{"collection":{"$ref":"#/$defs/schema0"},"filter":{"$ref":"#/$defs/PredicateNode"},"item":{"$ref":"#/$defs/PredicateNode"},"kind":{"const":"every","type":"string"}},"required":["kind","collection","item"],"type":"object"},{"additionalProperties":false,"properties":{"key":{"$ref":"#/$defs/schema0"},"kind":{"const":"treeReference","type":"string"}},"required":["kind","key"],"type":"object"}]},"schema0":{"anyOf":[{"type":"string"},{"type":"number"},{"type":"boolean"},{"type":"null"},{"items":{"$ref":"#/$defs/schema0"},"type":"array"},{"additionalProperties":{"$ref":"#/$defs/schema0"},"propertyNames":{"type":"string"},"type":"object"}]}},"$id":"https://cdn.jsdelivr.net/npm/trilean@1.13.11/schemas/trilean.schema.json","$schema":"https://json-schema.org/draft/2020-12/schema","oneOf":[{"$ref":"#/$defs/PredicateNode"},{"$ref":"#/$defs/ExpressionNode"}]}
|