@ocap/rule-dsl 0.0.0-stage → 1.30.26
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/esm/compiler.d.mts +12 -0
- package/esm/compiler.mjs +277 -0
- package/esm/errors.d.mts +28 -0
- package/esm/errors.mjs +36 -0
- package/esm/expr.d.mts +29 -0
- package/esm/expr.mjs +218 -0
- package/esm/index.d.mts +9 -0
- package/esm/index.mjs +9 -0
- package/esm/parser.d.mts +12 -0
- package/esm/parser.mjs +398 -0
- package/esm/pipeline.d.mts +18 -0
- package/esm/pipeline.mjs +37 -0
- package/esm/primitives.d.mts +22 -0
- package/esm/primitives.mjs +57 -0
- package/esm/types.d.mts +139 -0
- package/esm/types.mjs +1 -0
- package/esm/validator.d.mts +12 -0
- package/esm/validator.mjs +72 -0
- package/lib/_virtual/rolldown_runtime.cjs +29 -0
- package/lib/compiler.cjs +277 -0
- package/lib/compiler.d.cts +12 -0
- package/lib/errors.cjs +38 -0
- package/lib/errors.d.cts +28 -0
- package/lib/expr.cjs +222 -0
- package/lib/expr.d.cts +29 -0
- package/lib/index.cjs +30 -0
- package/lib/index.d.cts +9 -0
- package/lib/parser.cjs +400 -0
- package/lib/parser.d.cts +12 -0
- package/lib/pipeline.cjs +37 -0
- package/lib/pipeline.d.cts +18 -0
- package/lib/primitives.cjs +69 -0
- package/lib/primitives.d.cts +22 -0
- package/lib/types.cjs +0 -0
- package/lib/types.d.cts +139 -0
- package/lib/validator.cjs +72 -0
- package/lib/validator.d.cts +12 -0
- package/package.json +72 -4
- package/README.md +0 -3
package/lib/types.d.cts
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* AST + DAG type definitions for Rule DSL v1 (planning/35 §6.6, L811-1129).
|
|
4
|
+
*
|
|
5
|
+
* This package is an ISOLATED PoC: text -> parse -> static-validate -> compile(DAG).
|
|
6
|
+
* It intentionally does NOT implement (see issue #870 scope):
|
|
7
|
+
* - on-chain governance issuance of rules (planning/15)
|
|
8
|
+
* - VC / NFT minting of a Rule
|
|
9
|
+
* - a runtime execution engine that actually applies a compiled plan to state
|
|
10
|
+
*/
|
|
11
|
+
/** The 6 comparison operators allowed in expressions (planning/35 Primitive 2). */
|
|
12
|
+
type ComparisonOp = '<' | '<=' | '>' | '>=' | '==' | '!=';
|
|
13
|
+
type ExprLiteral = {
|
|
14
|
+
kind: 'number';
|
|
15
|
+
value: number;
|
|
16
|
+
} | {
|
|
17
|
+
kind: 'string';
|
|
18
|
+
value: string;
|
|
19
|
+
} | {
|
|
20
|
+
kind: 'boolean';
|
|
21
|
+
value: boolean;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Reference to a tx field (`tx.amount`) or a state key.
|
|
25
|
+
*
|
|
26
|
+
* Spec note: the canonical examples in §6.6 use *bare* identifiers for state
|
|
27
|
+
* refs (`expr: remaining_budget >= 0`, no `state.` prefix) while explicitly
|
|
28
|
+
* prefixing tx refs (`tx.amount`). We accept both a bare identifier and an
|
|
29
|
+
* explicit `state.` prefix as equivalent state references, matching the
|
|
30
|
+
* examples while staying consistent with the prose ("操作数:state.*, tx.*").
|
|
31
|
+
*/
|
|
32
|
+
type ExprRef = {
|
|
33
|
+
kind: 'tx';
|
|
34
|
+
path: string;
|
|
35
|
+
} | {
|
|
36
|
+
kind: 'state';
|
|
37
|
+
path: string;
|
|
38
|
+
};
|
|
39
|
+
type ValueExpr = ExprLiteral | ExprRef;
|
|
40
|
+
interface BoolExpr {
|
|
41
|
+
op: ComparisonOp;
|
|
42
|
+
left: ValueExpr;
|
|
43
|
+
right: ValueExpr;
|
|
44
|
+
}
|
|
45
|
+
interface EligibilityMatch {
|
|
46
|
+
primitive: 'eligibility';
|
|
47
|
+
set: string;
|
|
48
|
+
mode: 'include' | 'exclude';
|
|
49
|
+
}
|
|
50
|
+
interface TimeMatch {
|
|
51
|
+
primitive: 'time';
|
|
52
|
+
timeType: 'at' | 'after' | 'window' | 'epoch';
|
|
53
|
+
value: string | number;
|
|
54
|
+
}
|
|
55
|
+
interface RateLimit {
|
|
56
|
+
count?: number;
|
|
57
|
+
sum?: ValueExpr;
|
|
58
|
+
}
|
|
59
|
+
interface RateMatch {
|
|
60
|
+
primitive: 'rate';
|
|
61
|
+
window: string;
|
|
62
|
+
limit: RateLimit;
|
|
63
|
+
}
|
|
64
|
+
type MatchNode = EligibilityMatch | TimeMatch | RateMatch;
|
|
65
|
+
interface CounterEffect {
|
|
66
|
+
primitive: 'counter';
|
|
67
|
+
op: 'increment' | 'decrement' | 'set';
|
|
68
|
+
key: string;
|
|
69
|
+
value: ValueExpr;
|
|
70
|
+
}
|
|
71
|
+
interface AllocateStrategy {
|
|
72
|
+
type: 'fixed' | 'weighted' | 'waterfall';
|
|
73
|
+
params: Record<string, unknown>;
|
|
74
|
+
}
|
|
75
|
+
interface AllocateEffect {
|
|
76
|
+
primitive: 'allocate';
|
|
77
|
+
source: string;
|
|
78
|
+
strategy: AllocateStrategy;
|
|
79
|
+
}
|
|
80
|
+
type EffectNode = CounterEffect | AllocateEffect;
|
|
81
|
+
interface ThresholdConstraint {
|
|
82
|
+
primitive: 'threshold';
|
|
83
|
+
expr: BoolExpr;
|
|
84
|
+
onFail: 'reject' | 'timelock' | 'skip';
|
|
85
|
+
}
|
|
86
|
+
type ConstraintNode = ThresholdConstraint;
|
|
87
|
+
interface Ordering {
|
|
88
|
+
priority?: number;
|
|
89
|
+
mode?: 'first_match' | 'all_apply';
|
|
90
|
+
}
|
|
91
|
+
interface Branch {
|
|
92
|
+
/** Absent only on the trailing catch-all branch. */
|
|
93
|
+
when?: ConstraintNode[];
|
|
94
|
+
decision: 'allow' | 'reject' | 'timelock';
|
|
95
|
+
delay?: string;
|
|
96
|
+
}
|
|
97
|
+
interface Metadata {
|
|
98
|
+
issuer?: string;
|
|
99
|
+
version?: number;
|
|
100
|
+
validFrom?: number;
|
|
101
|
+
validUntil?: number;
|
|
102
|
+
}
|
|
103
|
+
interface AppliesTo {
|
|
104
|
+
txKind: string;
|
|
105
|
+
scope?: string;
|
|
106
|
+
}
|
|
107
|
+
/** Fully parsed + shape-checked Rule DSL v1 document. */
|
|
108
|
+
interface RuleAst {
|
|
109
|
+
rule: string;
|
|
110
|
+
appliesTo: AppliesTo;
|
|
111
|
+
match: MatchNode[];
|
|
112
|
+
effects: EffectNode[];
|
|
113
|
+
constraints: ConstraintNode[];
|
|
114
|
+
ordering?: Ordering;
|
|
115
|
+
branches?: Branch[];
|
|
116
|
+
metadata?: Metadata;
|
|
117
|
+
}
|
|
118
|
+
type DagNodeKind = 'match' | 'effect' | 'constraint' | 'branch';
|
|
119
|
+
interface DagNode {
|
|
120
|
+
id: string;
|
|
121
|
+
kind: DagNodeKind;
|
|
122
|
+
/** Primitive name, e.g. 'eligibility' | 'time' | 'rate' | 'counter' | 'allocate' | 'threshold' | 'branch'. */
|
|
123
|
+
type: string;
|
|
124
|
+
data: Record<string, unknown>;
|
|
125
|
+
}
|
|
126
|
+
interface DagEdge {
|
|
127
|
+
from: string;
|
|
128
|
+
to: string;
|
|
129
|
+
}
|
|
130
|
+
interface CompiledRuleDag {
|
|
131
|
+
ruleId: string;
|
|
132
|
+
appliesTo: AppliesTo;
|
|
133
|
+
ordering?: Ordering;
|
|
134
|
+
metadata?: Metadata;
|
|
135
|
+
nodes: DagNode[];
|
|
136
|
+
edges: DagEdge[];
|
|
137
|
+
}
|
|
138
|
+
//#endregion
|
|
139
|
+
export { AllocateEffect, AllocateStrategy, AppliesTo, BoolExpr, Branch, ComparisonOp, CompiledRuleDag, ConstraintNode, CounterEffect, DagEdge, DagNode, DagNodeKind, EffectNode, EligibilityMatch, ExprLiteral, ExprRef, MatchNode, Metadata, Ordering, RateLimit, RateMatch, RuleAst, ThresholdConstraint, TimeMatch, ValueExpr };
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
const require_errors = require('./errors.cjs');
|
|
2
|
+
|
|
3
|
+
//#region src/validator.ts
|
|
4
|
+
/**
|
|
5
|
+
* Whole-AST static validation that needs more than one node in isolation.
|
|
6
|
+
*
|
|
7
|
+
* planning/35 Primitive 1 says a counter's `key` "必须在 module 的 state
|
|
8
|
+
* schema 中声明" (must be declared in the module's state schema). That state
|
|
9
|
+
* schema is an external, module-level concept that this isolated PoC package
|
|
10
|
+
* has no access to (and per issue #870 scope, must not depend on any
|
|
11
|
+
* existing pipeline/state code). We therefore make it an explicit, optional
|
|
12
|
+
* input to the validator: callers that know the state schema can pass
|
|
13
|
+
* `knownStateKeys` to get STATE_KEY_NOT_FOUND enforcement; callers that don't
|
|
14
|
+
* (e.g. editing a rule with no bound module yet) simply skip that check.
|
|
15
|
+
*/
|
|
16
|
+
function collectStateRefPaths(value, sink) {
|
|
17
|
+
if (!value) return;
|
|
18
|
+
if (value.kind === "state") sink.push(value.path);
|
|
19
|
+
}
|
|
20
|
+
function collectFromMatch(match, sink) {
|
|
21
|
+
if (match.primitive === "rate" && match.limit.sum) collectStateRefPaths(match.limit.sum, sink);
|
|
22
|
+
}
|
|
23
|
+
function collectFromEffect(effect, sink) {
|
|
24
|
+
if (effect.primitive === "counter") {
|
|
25
|
+
collectStateRefPaths(effect.value, sink);
|
|
26
|
+
return { key: effect.key };
|
|
27
|
+
}
|
|
28
|
+
return { key: effect.source };
|
|
29
|
+
}
|
|
30
|
+
function validateRule(ast, options = {}) {
|
|
31
|
+
const errors = [];
|
|
32
|
+
const { knownStateKeys } = options;
|
|
33
|
+
if (!knownStateKeys) return errors;
|
|
34
|
+
const known = new Set(knownStateKeys);
|
|
35
|
+
ast.match.forEach((m, idx) => {
|
|
36
|
+
const refs = [];
|
|
37
|
+
collectFromMatch(m, refs);
|
|
38
|
+
refs.forEach((key) => {
|
|
39
|
+
if (!known.has(key)) errors.push(require_errors.makeError("STATE_KEY_NOT_FOUND", `match[${idx}]`, `Unknown state key "${key}"`));
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
ast.effects.forEach((e, idx) => {
|
|
43
|
+
const refs = [];
|
|
44
|
+
const { key } = collectFromEffect(e, refs);
|
|
45
|
+
if (key && !known.has(key)) errors.push(require_errors.makeError("STATE_KEY_NOT_FOUND", `effects[${idx}]`, `Unknown state key "${key}"`));
|
|
46
|
+
refs.forEach((refKey) => {
|
|
47
|
+
if (!known.has(refKey)) errors.push(require_errors.makeError("STATE_KEY_NOT_FOUND", `effects[${idx}]`, `Unknown state key "${refKey}"`));
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
ast.constraints.forEach((c, idx) => {
|
|
51
|
+
const refs = [];
|
|
52
|
+
collectStateRefPaths(c.expr.left, refs);
|
|
53
|
+
collectStateRefPaths(c.expr.right, refs);
|
|
54
|
+
refs.forEach((key) => {
|
|
55
|
+
if (!known.has(key)) errors.push(require_errors.makeError("STATE_KEY_NOT_FOUND", `constraints[${idx}]`, `Unknown state key "${key}"`));
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
(ast.branches ?? []).forEach((branch, bIdx) => {
|
|
59
|
+
(branch.when ?? []).forEach((c, idx) => {
|
|
60
|
+
const refs = [];
|
|
61
|
+
collectStateRefPaths(c.expr.left, refs);
|
|
62
|
+
collectStateRefPaths(c.expr.right, refs);
|
|
63
|
+
refs.forEach((key) => {
|
|
64
|
+
if (!known.has(key)) errors.push(require_errors.makeError("STATE_KEY_NOT_FOUND", `branches[${bIdx}].when[${idx}]`, `Unknown state key "${key}"`));
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
return errors;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
//#endregion
|
|
72
|
+
exports.validateRule = validateRule;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { RuleDslError } from "./errors.cjs";
|
|
2
|
+
import { RuleAst } from "./types.cjs";
|
|
3
|
+
|
|
4
|
+
//#region src/validator.d.ts
|
|
5
|
+
|
|
6
|
+
interface ValidateOptions {
|
|
7
|
+
/** State keys declared by the target module's state schema, if known. */
|
|
8
|
+
knownStateKeys?: string[];
|
|
9
|
+
}
|
|
10
|
+
declare function validateRule(ast: RuleAst, options?: ValidateOptions): RuleDslError[];
|
|
11
|
+
//#endregion
|
|
12
|
+
export { ValidateOptions, validateRule };
|
package/package.json
CHANGED
|
@@ -1,6 +1,74 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ocap/rule-dsl",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
6
|
-
|
|
3
|
+
"version": "1.30.26",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Rule DSL v1 parser, static validator and DAG compiler PoC (planning/35 §6.6). Isolated package: does not hook into any existing tx-pipeline/protocol/state code.",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"arcblock",
|
|
8
|
+
"blockchain",
|
|
9
|
+
"dsl",
|
|
10
|
+
"rule-engine"
|
|
11
|
+
],
|
|
12
|
+
"publishConfig": {
|
|
13
|
+
"access": "public"
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"js-yaml": "^4.1.0"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@types/js-yaml": "^4.0.9",
|
|
20
|
+
"@types/node": "^22.7.5",
|
|
21
|
+
"tsdown": "^0.18.4"
|
|
22
|
+
},
|
|
23
|
+
"author": {
|
|
24
|
+
"name": "wangshijun",
|
|
25
|
+
"email": "shijun@arcblock.io",
|
|
26
|
+
"url": "https://github.com/wangshijun"
|
|
27
|
+
},
|
|
28
|
+
"contributors": [
|
|
29
|
+
"wangshijun <shijun@arcblock.io> (https://github.com/wangshijun)"
|
|
30
|
+
],
|
|
31
|
+
"homepage": "https://github.com/ArcBlock/blockchain/tree/master/core/rule-dsl",
|
|
32
|
+
"license": "Apache-2.0",
|
|
33
|
+
"main": "./lib/index.cjs",
|
|
34
|
+
"sideEffects": false,
|
|
35
|
+
"module": "./esm/index.mjs",
|
|
36
|
+
"types": "./esm/index.d.mts",
|
|
37
|
+
"exports": {
|
|
38
|
+
".": {
|
|
39
|
+
"types": "./esm/index.d.mts",
|
|
40
|
+
"import": "./esm/index.mjs",
|
|
41
|
+
"default": "./lib/index.cjs"
|
|
42
|
+
},
|
|
43
|
+
"./lib/*.js": {
|
|
44
|
+
"types": "./esm/*.d.mts",
|
|
45
|
+
"import": "./esm/*.mjs",
|
|
46
|
+
"default": "./lib/*.cjs"
|
|
47
|
+
},
|
|
48
|
+
"./lib/*": {
|
|
49
|
+
"types": "./esm/*.d.mts",
|
|
50
|
+
"import": "./esm/*.mjs",
|
|
51
|
+
"default": "./lib/*.cjs"
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
"files": [
|
|
55
|
+
"lib",
|
|
56
|
+
"esm"
|
|
57
|
+
],
|
|
58
|
+
"repository": {
|
|
59
|
+
"type": "git",
|
|
60
|
+
"url": "git+https://github.com/ArcBlock/blockchain.git"
|
|
61
|
+
},
|
|
62
|
+
"scripts": {
|
|
63
|
+
"lint": "biome check",
|
|
64
|
+
"lint:fix": "biome check --write",
|
|
65
|
+
"test": "bun test",
|
|
66
|
+
"coverage": "npm run test -- --coverage",
|
|
67
|
+
"build": "tsdown",
|
|
68
|
+
"build:watch": "tsdown -w",
|
|
69
|
+
"clean": "rm -rf lib esm"
|
|
70
|
+
},
|
|
71
|
+
"bugs": {
|
|
72
|
+
"url": "https://github.com/ArcBlock/blockchain/issues"
|
|
73
|
+
}
|
|
74
|
+
}
|
package/README.md
DELETED