@crediolabs/policy-synth 0.3.0 → 0.3.1
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/record/freshness.d.ts +17 -11
- package/dist/record/freshness.js +28 -18
- package/dist-cjs/record/freshness.d.ts +17 -11
- package/dist-cjs/record/freshness.js +28 -18
- package/package.json +2 -2
- package/src/record/freshness.ts +29 -18
- package/dist/adapters/interpreter/index.d.ts +0 -1
- package/dist/adapters/interpreter/index.js +0 -2
- package/dist/adapters/oz/adapter.d.ts +0 -20
- package/dist/adapters/oz/adapter.js +0 -281
- package/dist/adapters/oz/index.d.ts +0 -1
- package/dist/adapters/oz/index.js +0 -2
- package/dist/codegen/compile-gate.d.ts +0 -33
- package/dist/codegen/compile-gate.js +0 -124
- package/dist/codegen/index.d.ts +0 -2
- package/dist/codegen/index.js +0 -8
- package/dist/codegen/template.d.ts +0 -18
- package/dist/codegen/template.js +0 -148
- package/dist/ir/index.d.ts +0 -1
- package/dist/ir/index.js +0 -2
- package/dist/ir/types.d.ts +0 -71
- package/dist/ir/types.js +0 -9
- package/dist/mandate/index.d.ts +0 -2
- package/dist/mandate/index.js +0 -2
- package/dist/mandate/to-ir.d.ts +0 -3
- package/dist/mandate/to-ir.js +0 -60
- package/dist/mandate/types.d.ts +0 -20
- package/dist/mandate/types.js +0 -8
- package/dist/seams/index.d.ts +0 -1
- package/dist/seams/index.js +0 -2
- package/dist/seams/types.d.ts +0 -64
- package/dist/seams/types.js +0 -11
- package/dist/synth/deny-cases.d.ts +0 -28
- package/dist/synth/deny-cases.js +0 -212
- package/dist/synth/evaluate.d.ts +0 -18
- package/dist/synth/evaluate.js +0 -271
- package/dist/synth/harness.d.ts +0 -28
- package/dist/synth/harness.js +0 -47
- package/dist/synth/minimize.d.ts +0 -4
- package/dist/synth/minimize.js +0 -38
- package/dist/synth/permit-context.d.ts +0 -8
- package/dist/synth/permit-context.js +0 -35
- package/dist/synth/predicate-literals.d.ts +0 -5
- package/dist/synth/predicate-literals.js +0 -25
- package/dist/synth/synthesize-from-mandate.d.ts +0 -20
- package/dist/synth/synthesize-from-mandate.js +0 -59
- package/dist/verify/envelope.d.ts +0 -15
- package/dist/verify/envelope.js +0 -22
- package/dist/verify/index.d.ts +0 -3
- package/dist/verify/index.js +0 -3
- package/dist/verify/simulate.d.ts +0 -18
- package/dist/verify/simulate.js +0 -172
- package/dist/verify/verify.d.ts +0 -15
- package/dist/verify/verify.js +0 -181
- package/dist-cjs/adapters/interpreter/index.d.ts +0 -1
- package/dist-cjs/adapters/interpreter/index.js +0 -7
- package/dist-cjs/adapters/oz/adapter.d.ts +0 -20
- package/dist-cjs/adapters/oz/adapter.js +0 -286
- package/dist-cjs/adapters/oz/index.d.ts +0 -1
- package/dist-cjs/adapters/oz/index.js +0 -8
- package/dist-cjs/codegen/compile-gate.d.ts +0 -33
- package/dist-cjs/codegen/compile-gate.js +0 -128
- package/dist-cjs/codegen/index.d.ts +0 -2
- package/dist-cjs/codegen/index.js +0 -14
- package/dist-cjs/codegen/template.d.ts +0 -18
- package/dist-cjs/codegen/template.js +0 -151
- package/dist-cjs/ir/index.d.ts +0 -1
- package/dist-cjs/ir/index.js +0 -3
- package/dist-cjs/ir/types.d.ts +0 -71
- package/dist-cjs/ir/types.js +0 -10
- package/dist-cjs/mandate/index.d.ts +0 -2
- package/dist-cjs/mandate/index.js +0 -6
- package/dist-cjs/mandate/to-ir.d.ts +0 -3
- package/dist-cjs/mandate/to-ir.js +0 -63
- package/dist-cjs/mandate/types.d.ts +0 -20
- package/dist-cjs/mandate/types.js +0 -9
- package/dist-cjs/seams/index.d.ts +0 -1
- package/dist-cjs/seams/index.js +0 -3
- package/dist-cjs/seams/types.d.ts +0 -64
- package/dist-cjs/seams/types.js +0 -12
- package/dist-cjs/synth/deny-cases.d.ts +0 -28
- package/dist-cjs/synth/deny-cases.js +0 -217
- package/dist-cjs/synth/evaluate.d.ts +0 -18
- package/dist-cjs/synth/evaluate.js +0 -274
- package/dist-cjs/synth/harness.d.ts +0 -28
- package/dist-cjs/synth/harness.js +0 -50
- package/dist-cjs/synth/minimize.d.ts +0 -4
- package/dist-cjs/synth/minimize.js +0 -41
- package/dist-cjs/synth/permit-context.d.ts +0 -8
- package/dist-cjs/synth/permit-context.js +0 -38
- package/dist-cjs/synth/predicate-literals.d.ts +0 -5
- package/dist-cjs/synth/predicate-literals.js +0 -28
- package/dist-cjs/synth/synthesize-from-mandate.d.ts +0 -20
- package/dist-cjs/synth/synthesize-from-mandate.js +0 -62
- package/dist-cjs/verify/envelope.d.ts +0 -15
- package/dist-cjs/verify/envelope.js +0 -23
- package/dist-cjs/verify/index.d.ts +0 -3
- package/dist-cjs/verify/index.js +0 -8
- package/dist-cjs/verify/simulate.d.ts +0 -18
- package/dist-cjs/verify/simulate.js +0 -175
- package/dist-cjs/verify/verify.d.ts +0 -15
- package/dist-cjs/verify/verify.js +0 -184
|
@@ -1,274 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// src/synth/evaluate.ts - TypeScript reference evaluator for grammar version 3.
|
|
3
|
-
//
|
|
4
|
-
// Pure function. Determinism: same `(predicate, ctx)` -> byte-identical result,
|
|
5
|
-
// no clock, no randomness. Deny order (deny on FIRST violation, stable reason):
|
|
6
|
-
// 1. contract mismatch -> 'CONTRACT_SCOPE'
|
|
7
|
-
// 2. per-ScVal equality on fn/args; EXACT ordered vector
|
|
8
|
-
// equality; fail-closed on opaque args -> 'ARG_MISMATCH'
|
|
9
|
-
// 3. `in` membership; empty haystack ALWAYS denies -> 'NOT_IN_ALLOWLIST'
|
|
10
|
-
// 4. otherwise permit.
|
|
11
|
-
//
|
|
12
|
-
// Grammar version 3 nodes: and, eq, lte
|
|
13
|
-
// Grammar version 3 leaves: call_contract, call_fn, call_arg(i),
|
|
14
|
-
// call_arg_len(i), call_arg_field(i, element, field),
|
|
15
|
-
// literal_address, literal_i128, literal_symbol, literal_u32, literal_vec
|
|
16
|
-
// Grammar version 3 deny reasons: ARG_MISMATCH, CONTRACT_SCOPE,
|
|
17
|
-
// UNSUPPORTED_NODE, NOT_IN_ALLOWLIST
|
|
18
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
|
-
exports.evaluate = evaluate;
|
|
20
|
-
/** Evaluate a `PredicateNode` against the candidate call described by `ctx`.
|
|
21
|
-
* Pure function. Returns `{ permit: true }` or `{ permit: false; reason }`. */
|
|
22
|
-
function evaluate(predicate, ctx) {
|
|
23
|
-
return walk(predicate, ctx);
|
|
24
|
-
}
|
|
25
|
-
/** Walk the predicate tree. Returns the FIRST deny reason encountered on
|
|
26
|
-
* the active branch (so `and` fails-fast). */
|
|
27
|
-
function walk(node, ctx) {
|
|
28
|
-
switch (node.op) {
|
|
29
|
-
case 'and': {
|
|
30
|
-
let lastDeny = null;
|
|
31
|
-
for (const child of node.children) {
|
|
32
|
-
const r = walk(child, ctx);
|
|
33
|
-
if (!r.permit)
|
|
34
|
-
return r;
|
|
35
|
-
lastDeny = r;
|
|
36
|
-
}
|
|
37
|
-
return lastDeny ?? { permit: true };
|
|
38
|
-
}
|
|
39
|
-
case 'eq':
|
|
40
|
-
case 'lte':
|
|
41
|
-
return evalCompare(node.op, node.left, node.right, ctx);
|
|
42
|
-
case 'in':
|
|
43
|
-
return evalIn(node.needle, node.haystack, ctx);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
/** Comparison leaf evaluation. */
|
|
47
|
-
function evalCompare(op, left, right, ctx) {
|
|
48
|
-
// CONTRACT_SCOPE on call_contract eq
|
|
49
|
-
if (left.kind === 'call_contract' && op === 'eq') {
|
|
50
|
-
if (right.kind !== 'literal_address')
|
|
51
|
-
return { permit: false, reason: 'CONTRACT_SCOPE' };
|
|
52
|
-
return right.value === ctx.contract
|
|
53
|
-
? { permit: true }
|
|
54
|
-
: { permit: false, reason: 'CONTRACT_SCOPE' };
|
|
55
|
-
}
|
|
56
|
-
// call_fn equality
|
|
57
|
-
if (left.kind === 'call_fn' && op === 'eq') {
|
|
58
|
-
if (right.kind !== 'literal_symbol')
|
|
59
|
-
return { permit: false, reason: 'ARG_MISMATCH' };
|
|
60
|
-
return right.value === ctx.fn ? { permit: true } : { permit: false, reason: 'ARG_MISMATCH' };
|
|
61
|
-
}
|
|
62
|
-
// call_arg comparison (eq / exact-vec, or lte ordered numeric bound)
|
|
63
|
-
if (left.kind === 'call_arg') {
|
|
64
|
-
const actual = ctx.args[left.index];
|
|
65
|
-
if (op !== 'eq')
|
|
66
|
-
return evalArgOrderedCompare(op, actual, right);
|
|
67
|
-
return evalArgEq(op, actual, right, ctx);
|
|
68
|
-
}
|
|
69
|
-
// call_arg_len: length of a vec-typed argument as u32.
|
|
70
|
-
// Fails closed on a non-vec / absent arg or a non-u32 literal.
|
|
71
|
-
if (left.kind === 'call_arg_len') {
|
|
72
|
-
const actual = ctx.args[left.index];
|
|
73
|
-
if (actual?.type !== 'vec')
|
|
74
|
-
return { permit: false, reason: 'ARG_MISMATCH' };
|
|
75
|
-
if (right.kind !== 'literal_u32')
|
|
76
|
-
return { permit: false, reason: 'ARG_MISMATCH' };
|
|
77
|
-
return actual.value.length === right.value
|
|
78
|
-
? { permit: true }
|
|
79
|
-
: { permit: false, reason: 'ARG_MISMATCH' };
|
|
80
|
-
}
|
|
81
|
-
// call_arg_field: value of a field in the map at element i of
|
|
82
|
-
// the vec at argument index. Fails closed on shape / type / range issues.
|
|
83
|
-
if (left.kind === 'call_arg_field') {
|
|
84
|
-
const actual = ctx.args[left.index];
|
|
85
|
-
if (actual?.type !== 'vec')
|
|
86
|
-
return { permit: false, reason: 'ARG_MISMATCH' };
|
|
87
|
-
const element = actual.value[left.element];
|
|
88
|
-
if (element?.type !== 'map')
|
|
89
|
-
return { permit: false, reason: 'ARG_MISMATCH' };
|
|
90
|
-
if (!Array.isArray(element.value))
|
|
91
|
-
return { permit: false, reason: 'ARG_MISMATCH' };
|
|
92
|
-
const entry = element.value.find((e) => e.key === left.field);
|
|
93
|
-
if (!entry)
|
|
94
|
-
return { permit: false, reason: 'ARG_MISMATCH' };
|
|
95
|
-
if (op === 'eq')
|
|
96
|
-
return evalArgEq(op, entry.val, right, ctx);
|
|
97
|
-
return evalArgOrderedCompare(op, entry.val, right);
|
|
98
|
-
}
|
|
99
|
-
// Unknown leaf/op combination - structural fail-closed.
|
|
100
|
-
return { permit: false, reason: 'UNSUPPORTED_NODE' };
|
|
101
|
-
}
|
|
102
|
-
/** Per-ScVal equality. Handles literal_vec as an EXACT ordered sequence:
|
|
103
|
-
* compare element-by-element in order; deny if length or any element differs.
|
|
104
|
-
* Opaque args (`type: 'other'`) fail closed. */
|
|
105
|
-
function evalArgEq(op, actual, right, _ctx) {
|
|
106
|
-
// eq(call_arg[i], literal_vec) -> EXACT ordered vector equality.
|
|
107
|
-
if (op === 'eq' && right.kind === 'literal_vec') {
|
|
108
|
-
if (actual?.type !== 'vec')
|
|
109
|
-
return { permit: false, reason: 'ARG_MISMATCH' };
|
|
110
|
-
return compareVecExact(actual.value, right.elements)
|
|
111
|
-
? { permit: true }
|
|
112
|
-
: { permit: false, reason: 'ARG_MISMATCH' };
|
|
113
|
-
}
|
|
114
|
-
if (op === 'eq' && right.kind === 'literal_address') {
|
|
115
|
-
if (!actual)
|
|
116
|
-
return { permit: false, reason: 'ARG_MISMATCH' };
|
|
117
|
-
if (actual.type === 'other')
|
|
118
|
-
return { permit: false, reason: 'ARG_MISMATCH' };
|
|
119
|
-
return actual.type === 'address' && actual.value === right.value
|
|
120
|
-
? { permit: true }
|
|
121
|
-
: { permit: false, reason: 'ARG_MISMATCH' };
|
|
122
|
-
}
|
|
123
|
-
if (op === 'eq' && right.kind === 'literal_i128') {
|
|
124
|
-
if (actual?.type !== 'i128')
|
|
125
|
-
return { permit: false, reason: 'ARG_MISMATCH' };
|
|
126
|
-
return BigInt(actual.value) === BigInt(right.value)
|
|
127
|
-
? { permit: true }
|
|
128
|
-
: { permit: false, reason: 'ARG_MISMATCH' };
|
|
129
|
-
}
|
|
130
|
-
if (op === 'eq' && right.kind === 'literal_symbol') {
|
|
131
|
-
if (!actual)
|
|
132
|
-
return { permit: false, reason: 'ARG_MISMATCH' };
|
|
133
|
-
return actual.type === 'symbol' && actual.value === right.value
|
|
134
|
-
? { permit: true }
|
|
135
|
-
: { permit: false, reason: 'ARG_MISMATCH' };
|
|
136
|
-
}
|
|
137
|
-
if (op === 'eq' && right.kind === 'literal_u32') {
|
|
138
|
-
if (!actual)
|
|
139
|
-
return { permit: false, reason: 'ARG_MISMATCH' };
|
|
140
|
-
return actual.type === 'u32' && actual.value === String(right.value)
|
|
141
|
-
? { permit: true }
|
|
142
|
-
: { permit: false, reason: 'ARG_MISMATCH' };
|
|
143
|
-
}
|
|
144
|
-
// Anything else: fail closed on opacity (cannot decode the arg reliably).
|
|
145
|
-
if (!actual || actual.type === 'other')
|
|
146
|
-
return { permit: false, reason: 'ARG_MISMATCH' };
|
|
147
|
-
return { permit: false, reason: 'ARG_MISMATCH' };
|
|
148
|
-
}
|
|
149
|
-
/** Ordered numeric comparison (lte) on a `call_arg`. The interpreter reads the
|
|
150
|
-
* arg as an integer (i128 on the recorder's ScVal surface) and compares it
|
|
151
|
-
* to a numeric literal via BigInt. A non-numeric arg or a non-numeric literal
|
|
152
|
-
* fails closed (ARG_MISMATCH) rather than permitting an undecidable bound. */
|
|
153
|
-
function evalArgOrderedCompare(op, actual, right) {
|
|
154
|
-
const actualInt = argNumericBigInt(actual);
|
|
155
|
-
const literalInt = literalNumericBigInt(right);
|
|
156
|
-
if (actualInt === null || literalInt === null) {
|
|
157
|
-
return { permit: false, reason: 'ARG_MISMATCH' };
|
|
158
|
-
}
|
|
159
|
-
return bigintCmp(op, actualInt.toString(), literalInt.toString())
|
|
160
|
-
? { permit: true }
|
|
161
|
-
: { permit: false, reason: 'ARG_MISMATCH' };
|
|
162
|
-
}
|
|
163
|
-
/** BigInt value of a numeric-integer ScVal arg (i128 / u32), or null when
|
|
164
|
-
* the arg is absent, opaque, or a non-numeric type. */
|
|
165
|
-
function argNumericBigInt(actual) {
|
|
166
|
-
if (!actual)
|
|
167
|
-
return null;
|
|
168
|
-
if (actual.type === 'i128' || actual.type === 'u32') {
|
|
169
|
-
try {
|
|
170
|
-
return BigInt(actual.value);
|
|
171
|
-
}
|
|
172
|
-
catch {
|
|
173
|
-
return null;
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
return null;
|
|
177
|
-
}
|
|
178
|
-
/** BigInt value of a numeric-integer literal leaf (`literal_i128` / `literal_u32`),
|
|
179
|
-
* or null for a non-numeric literal. Callers fail closed on null. */
|
|
180
|
-
function literalNumericBigInt(leaf) {
|
|
181
|
-
switch (leaf.kind) {
|
|
182
|
-
case 'literal_i128':
|
|
183
|
-
try {
|
|
184
|
-
return BigInt(leaf.value);
|
|
185
|
-
}
|
|
186
|
-
catch {
|
|
187
|
-
return null;
|
|
188
|
-
}
|
|
189
|
-
case 'literal_u32':
|
|
190
|
-
return BigInt(leaf.value);
|
|
191
|
-
default:
|
|
192
|
-
return null;
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
/** Element-by-element ordered comparison of an `ScVal[]` against a
|
|
196
|
-
* `PredicateLeaf[]`. Equal-length and equal at every index => permit.
|
|
197
|
-
* Length mismatch OR any element mismatch => deny ARG_MISMATCH. */
|
|
198
|
-
function compareVecExact(actual, expected) {
|
|
199
|
-
if (actual.length !== expected.length)
|
|
200
|
-
return false;
|
|
201
|
-
for (let i = 0; i < expected.length; i++) {
|
|
202
|
-
const e = expected[i];
|
|
203
|
-
const a = actual[i];
|
|
204
|
-
if (!e || !a)
|
|
205
|
-
return false;
|
|
206
|
-
const r = evalArgEq('eq', a, e, {});
|
|
207
|
-
if (!r.permit)
|
|
208
|
-
return false;
|
|
209
|
-
}
|
|
210
|
-
return true;
|
|
211
|
-
}
|
|
212
|
-
/** `in` membership. Empty haystack ALWAYS denies. Opaque needle fails closed. */
|
|
213
|
-
function evalIn(needle, haystack, ctx) {
|
|
214
|
-
if (haystack.length === 0)
|
|
215
|
-
return { permit: false, reason: 'NOT_IN_ALLOWLIST' };
|
|
216
|
-
const actual = resolveLeaf(needle, ctx);
|
|
217
|
-
if (!actual || actual.type === 'other')
|
|
218
|
-
return { permit: false, reason: 'NOT_IN_ALLOWLIST' };
|
|
219
|
-
for (const h of haystack) {
|
|
220
|
-
const r = evalArgEq('eq', actual, h, ctx);
|
|
221
|
-
if (r.permit)
|
|
222
|
-
return { permit: true };
|
|
223
|
-
}
|
|
224
|
-
return { permit: false, reason: 'NOT_IN_ALLOWLIST' };
|
|
225
|
-
}
|
|
226
|
-
/** Resolve a selector leaf to its current ScVal against the candidate call. */
|
|
227
|
-
function resolveLeaf(leaf, ctx) {
|
|
228
|
-
switch (leaf.kind) {
|
|
229
|
-
case 'call_contract':
|
|
230
|
-
return { type: 'address', value: ctx.contract };
|
|
231
|
-
case 'call_fn':
|
|
232
|
-
return { type: 'symbol', value: ctx.fn };
|
|
233
|
-
case 'call_arg':
|
|
234
|
-
return ctx.args[leaf.index];
|
|
235
|
-
case 'call_arg_len':
|
|
236
|
-
// No direct ScVal projection: the length is an integer the comparator
|
|
237
|
-
// resolves against the right-hand literal. Returning undefined keeps
|
|
238
|
-
// the `in` membership path structurally informed (no haystack match).
|
|
239
|
-
return undefined;
|
|
240
|
-
case 'call_arg_field': {
|
|
241
|
-
const actual = ctx.args[leaf.index];
|
|
242
|
-
if (actual?.type !== 'vec')
|
|
243
|
-
return undefined;
|
|
244
|
-
const element = actual.value[leaf.element];
|
|
245
|
-
if (element?.type !== 'map')
|
|
246
|
-
return undefined;
|
|
247
|
-
if (!Array.isArray(element.value))
|
|
248
|
-
return undefined;
|
|
249
|
-
const entry = element.value.find((e) => e.key === leaf.field);
|
|
250
|
-
return entry ? entry.val : undefined;
|
|
251
|
-
}
|
|
252
|
-
case 'literal_address':
|
|
253
|
-
return { type: 'address', value: leaf.value };
|
|
254
|
-
case 'literal_i128':
|
|
255
|
-
return { type: 'i128', value: leaf.value };
|
|
256
|
-
case 'literal_symbol':
|
|
257
|
-
return { type: 'symbol', value: leaf.value };
|
|
258
|
-
case 'literal_u32':
|
|
259
|
-
return { type: 'u32', value: String(leaf.value) };
|
|
260
|
-
case 'literal_vec':
|
|
261
|
-
return undefined;
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
/** BigInt compare helper. */
|
|
265
|
-
function bigintCmp(op, aStr, bStr) {
|
|
266
|
-
const a = BigInt(aStr);
|
|
267
|
-
const b = BigInt(bStr);
|
|
268
|
-
switch (op) {
|
|
269
|
-
case 'eq':
|
|
270
|
-
return a === b;
|
|
271
|
-
case 'lte':
|
|
272
|
-
return a <= b;
|
|
273
|
-
}
|
|
274
|
-
}
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import type { PredicateNode } from '../types.ts';
|
|
2
|
-
import type { GeneratedCases } from './deny-cases.ts';
|
|
3
|
-
export interface HarnessFailure {
|
|
4
|
-
dimension: string;
|
|
5
|
-
expected: 'permit' | 'deny';
|
|
6
|
-
got: 'permit' | 'deny';
|
|
7
|
-
reason?: string;
|
|
8
|
-
/** When `expectedReason` is set on the deny case and the case actually
|
|
9
|
-
* denies, this carries the canonical reason the TS evaluator MUST emit
|
|
10
|
-
* to stay in lockstep with the Rust interpreter (the cross-layer
|
|
11
|
-
* reason-code contract). A future TS/Rust reason divergence surfaces
|
|
12
|
-
* here in CI. */
|
|
13
|
-
expectedReason?: string;
|
|
14
|
-
actualReason?: string;
|
|
15
|
-
}
|
|
16
|
-
export type HarnessResult = {
|
|
17
|
-
ok: true;
|
|
18
|
-
} | {
|
|
19
|
-
ok: false;
|
|
20
|
-
failures: HarnessFailure[];
|
|
21
|
-
};
|
|
22
|
-
/** Evaluate the intended call and every generated denial without side effects.
|
|
23
|
-
* Asserts both the boolean decision AND, for deny cases that carry an
|
|
24
|
-
* `expectedReason`, the concrete reason the TS evaluator emits. The reason
|
|
25
|
-
* is what the Rust interpreter returns; the cross-layer conformance test in
|
|
26
|
-
* `contracts/policy-interpreter/tests/conformance` mirrors the same contract
|
|
27
|
-
* for the verifyLive path. */
|
|
28
|
-
export declare function runHarness(predicate: PredicateNode, cases: GeneratedCases): HarnessResult;
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.runHarness = runHarness;
|
|
4
|
-
const evaluate_ts_1 = require("./evaluate.js");
|
|
5
|
-
/** Evaluate the intended call and every generated denial without side effects.
|
|
6
|
-
* Asserts both the boolean decision AND, for deny cases that carry an
|
|
7
|
-
* `expectedReason`, the concrete reason the TS evaluator emits. The reason
|
|
8
|
-
* is what the Rust interpreter returns; the cross-layer conformance test in
|
|
9
|
-
* `contracts/policy-interpreter/tests/conformance` mirrors the same contract
|
|
10
|
-
* for the verifyLive path. */
|
|
11
|
-
function runHarness(predicate, cases) {
|
|
12
|
-
const failures = [];
|
|
13
|
-
const permitResult = (0, evaluate_ts_1.evaluate)(predicate, cases.permit);
|
|
14
|
-
if (!permitResult.permit) {
|
|
15
|
-
failures.push({
|
|
16
|
-
dimension: 'PERMIT_CASE_FAILED',
|
|
17
|
-
expected: 'permit',
|
|
18
|
-
got: 'deny',
|
|
19
|
-
reason: permitResult.reason,
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
for (const deny of cases.denies) {
|
|
23
|
-
const result = (0, evaluate_ts_1.evaluate)(predicate, deny.ctx);
|
|
24
|
-
if (result.permit) {
|
|
25
|
-
failures.push({
|
|
26
|
-
dimension: deny.dimension,
|
|
27
|
-
expected: 'deny',
|
|
28
|
-
got: 'permit',
|
|
29
|
-
reason: 'DENY_CASE_FAILURE',
|
|
30
|
-
});
|
|
31
|
-
continue;
|
|
32
|
-
}
|
|
33
|
-
// Reason-code assertion (cross-layer contract). When a deny case carries
|
|
34
|
-
// an expected reason, the TS evaluator MUST emit that exact string; a
|
|
35
|
-
// divergence means the TS model and the Rust interpreter disagree on
|
|
36
|
-
// what the user sees in CI - the harness fails so the diff is visible
|
|
37
|
-
// here, not at runtime in production.
|
|
38
|
-
if (deny.expectedReason !== undefined && result.reason !== deny.expectedReason) {
|
|
39
|
-
failures.push({
|
|
40
|
-
dimension: deny.dimension,
|
|
41
|
-
expected: 'deny',
|
|
42
|
-
got: 'deny',
|
|
43
|
-
reason: 'REASON_MISMATCH',
|
|
44
|
-
expectedReason: deny.expectedReason,
|
|
45
|
-
actualReason: result.reason,
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
return failures.length === 0 ? { ok: true } : { ok: false, failures };
|
|
50
|
-
}
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import type { PredicateNode } from '../types.ts';
|
|
2
|
-
import type { EvalContext } from './evaluate.ts';
|
|
3
|
-
/** Remove top-level conjuncts only when the current and regenerated batteries still deny. */
|
|
4
|
-
export declare function minimize(predicate: PredicateNode, permitCtx: EvalContext, dimensions?: string[]): PredicateNode;
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.minimize = minimize;
|
|
4
|
-
const deny_cases_ts_1 = require("./deny-cases.js");
|
|
5
|
-
const harness_ts_1 = require("./harness.js");
|
|
6
|
-
/** Remove top-level conjuncts only when the current and regenerated batteries still deny. */
|
|
7
|
-
function minimize(predicate, permitCtx, dimensions) {
|
|
8
|
-
if (predicate.op !== 'and')
|
|
9
|
-
return predicate;
|
|
10
|
-
let children = [...predicate.children];
|
|
11
|
-
let index = 0;
|
|
12
|
-
while (index < children.length) {
|
|
13
|
-
const current = { op: 'and', children };
|
|
14
|
-
const currentCases = (0, deny_cases_ts_1.generateCases)(current, permitCtx, dimensions);
|
|
15
|
-
const candidateChildren = children.filter((_, childIndex) => childIndex !== index);
|
|
16
|
-
const candidate = { op: 'and', children: candidateChildren };
|
|
17
|
-
const candidateCases = (0, deny_cases_ts_1.generateCases)(candidate, permitCtx, dimensions);
|
|
18
|
-
const verificationCases = {
|
|
19
|
-
permit: candidateCases.permit,
|
|
20
|
-
denies: mergeDenyCases(currentCases.denies, candidateCases.denies),
|
|
21
|
-
};
|
|
22
|
-
if ((0, harness_ts_1.runHarness)(candidate, verificationCases).ok) {
|
|
23
|
-
children = candidateChildren;
|
|
24
|
-
continue;
|
|
25
|
-
}
|
|
26
|
-
index += 1;
|
|
27
|
-
}
|
|
28
|
-
return { op: 'and', children };
|
|
29
|
-
}
|
|
30
|
-
function mergeDenyCases(current, candidate) {
|
|
31
|
-
const merged = [];
|
|
32
|
-
const seen = new Set();
|
|
33
|
-
for (const deny of [...current, ...candidate]) {
|
|
34
|
-
const key = `${deny.dimension}:${JSON.stringify(deny.ctx)}`;
|
|
35
|
-
if (seen.has(key))
|
|
36
|
-
continue;
|
|
37
|
-
seen.add(key);
|
|
38
|
-
merged.push(deny);
|
|
39
|
-
}
|
|
40
|
-
return merged;
|
|
41
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { RecordedTransaction } from '../types.ts';
|
|
2
|
-
import type { EvalContext } from './evaluate.ts';
|
|
3
|
-
export interface PermitContextResponses {
|
|
4
|
-
windowSeconds: number;
|
|
5
|
-
limitAmount?: string;
|
|
6
|
-
validUntilLedger: number;
|
|
7
|
-
}
|
|
8
|
-
export declare function buildPermitContext(tx: RecordedTransaction, responses: PermitContextResponses): EvalContext;
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// src/synth/permit-context.ts - build the EvalContext that represents the
|
|
3
|
-
// recorded call being replayed.
|
|
4
|
-
//
|
|
5
|
-
// Extracted from the over-permissiveness harness so the same construction
|
|
6
|
-
// feeds both the in-process battery and the on-chain replay
|
|
7
|
-
// (scripts/verify-mutations-testnet.ts). Two callers building this
|
|
8
|
-
// separately would be two chances to disagree about what "the recorded
|
|
9
|
-
// call" means, which is the one thing both must share.
|
|
10
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
-
exports.buildPermitContext = buildPermitContext;
|
|
12
|
-
const deny_cases_ts_1 = require("./deny-cases.js");
|
|
13
|
-
function buildPermitContext(tx, responses) {
|
|
14
|
-
const amountByToken = {};
|
|
15
|
-
const totals = new Map();
|
|
16
|
-
for (const m of tx.tokenMovements) {
|
|
17
|
-
const current = totals.get(m.token) ?? 0n;
|
|
18
|
-
totals.set(m.token, current + BigInt(m.amount));
|
|
19
|
-
}
|
|
20
|
-
for (const [token, total] of totals) {
|
|
21
|
-
amountByToken[token] = total.toString();
|
|
22
|
-
}
|
|
23
|
-
const topLevel = tx.invocations[0];
|
|
24
|
-
const scopeContract = topLevel?.contract ?? '';
|
|
25
|
-
const ctx = {
|
|
26
|
-
contract: scopeContract,
|
|
27
|
-
fn: topLevel?.fn ?? '',
|
|
28
|
-
args: (topLevel?.args ?? []).map(deny_cases_ts_1.cloneScVal),
|
|
29
|
-
atLedger: tx.ledgerSequence,
|
|
30
|
-
nowSeconds: tx.fetchedAt,
|
|
31
|
-
amountByToken,
|
|
32
|
-
windowSpentByToken: {},
|
|
33
|
-
};
|
|
34
|
-
if (responses.validUntilLedger !== undefined) {
|
|
35
|
-
ctx.validUntilLedger = responses.validUntilLedger;
|
|
36
|
-
}
|
|
37
|
-
return ctx;
|
|
38
|
-
}
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import type { PredicateLeaf } from '../types.ts';
|
|
2
|
-
/** BigInt value of a numeric-integer literal leaf (`literal_i128` / `literal_u64`
|
|
3
|
-
* / `literal_u32`), or null for a non-numeric literal (address / symbol / bytes
|
|
4
|
-
* / vec). Callers fail closed on null. */
|
|
5
|
-
export declare function literalNumericBigInt(leaf: PredicateLeaf): bigint | null;
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// src/synth/predicate-literals.ts - read scalar values out of predicate literal leaves.
|
|
3
|
-
//
|
|
4
|
-
// Small shared helpers for interpreting a `PredicateLeaf` literal. Kept out of
|
|
5
|
-
// `evaluate.ts` so the evaluator stays focused on the deny-order semantics; the
|
|
6
|
-
// deny-case generator uses the same reader so both sides agree on what a numeric
|
|
7
|
-
// literal means.
|
|
8
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.literalNumericBigInt = literalNumericBigInt;
|
|
10
|
-
/** BigInt value of a numeric-integer literal leaf (`literal_i128` / `literal_u64`
|
|
11
|
-
* / `literal_u32`), or null for a non-numeric literal (address / symbol / bytes
|
|
12
|
-
* / vec). Callers fail closed on null. */
|
|
13
|
-
function literalNumericBigInt(leaf) {
|
|
14
|
-
switch (leaf.kind) {
|
|
15
|
-
case 'literal_i128':
|
|
16
|
-
case 'literal_u64':
|
|
17
|
-
try {
|
|
18
|
-
return BigInt(leaf.value);
|
|
19
|
-
}
|
|
20
|
-
catch {
|
|
21
|
-
return null;
|
|
22
|
-
}
|
|
23
|
-
case 'literal_u32':
|
|
24
|
-
return BigInt(leaf.value);
|
|
25
|
-
default:
|
|
26
|
-
return null;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import type { OzAdapterConfig } from '../adapters/oz/adapter.ts';
|
|
2
|
-
import type { ToolResponse } from '../errors.ts';
|
|
3
|
-
import type { MandateSpec } from '../mandate/types.ts';
|
|
4
|
-
import type { PredicateNode, ProposedPolicy } from '../types.ts';
|
|
5
|
-
import type { SimulationResult } from '../verify/envelope.ts';
|
|
6
|
-
export declare function synthesizeFromMandate(spec: MandateSpec, ozConfig: OzAdapterConfig,
|
|
7
|
-
/** --explain opt-in. When true, the success envelope carries the
|
|
8
|
-
* in-memory predicate tree (always null for the mandate path - the
|
|
9
|
-
* declarative MandateSpec lowers to OZ built-ins, not to an
|
|
10
|
-
* interpreter predicate) + a minimal honest SimulationResult. The
|
|
11
|
-
* flag is ADDITIVE: the existing ProposedPolicy fields are never
|
|
12
|
-
* altered. */
|
|
13
|
-
opts?: {
|
|
14
|
-
explain?: true;
|
|
15
|
-
}): ToolResponse<ProposedPolicy> & {
|
|
16
|
-
explain?: {
|
|
17
|
-
predicateTree: PredicateNode | null;
|
|
18
|
-
simulation: SimulationResult;
|
|
19
|
-
};
|
|
20
|
-
};
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// src/synth/synthesize-from-mandate.ts - the deterministic Mandate front-end.
|
|
3
|
-
//
|
|
4
|
-
// synthesizeFromMandate is the clean end-to-end demo path: a declarative
|
|
5
|
-
// MandateSpec is lowered deterministically to a PolicyIR and compiled by the OZ
|
|
6
|
-
// adapter to a ProposedPolicy. No decoding, no inference, so parseConfidence is
|
|
7
|
-
// the full/not-applicable value. Constructs the OZ built-in primitives cannot
|
|
8
|
-
// express (compile's `uncovered`) are surfaced in `ProposedPolicy.warnings`
|
|
9
|
-
// rather than failing the call - the covered primitives still install; the
|
|
10
|
-
// uncovered ones are reported.
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.synthesizeFromMandate = synthesizeFromMandate;
|
|
13
|
-
const adapter_ts_1 = require("../adapters/oz/adapter.js");
|
|
14
|
-
const to_ir_ts_1 = require("../mandate/to-ir.js");
|
|
15
|
-
const UNCOVERED_PREFIX = 'Not covered by OZ built-in primitives: ';
|
|
16
|
-
function synthesizeFromMandate(spec, ozConfig,
|
|
17
|
-
/** --explain opt-in. When true, the success envelope carries the
|
|
18
|
-
* in-memory predicate tree (always null for the mandate path - the
|
|
19
|
-
* declarative MandateSpec lowers to OZ built-ins, not to an
|
|
20
|
-
* interpreter predicate) + a minimal honest SimulationResult. The
|
|
21
|
-
* flag is ADDITIVE: the existing ProposedPolicy fields are never
|
|
22
|
-
* altered. */
|
|
23
|
-
opts) {
|
|
24
|
-
const ir = (0, to_ir_ts_1.mandateToPolicyIR)(spec);
|
|
25
|
-
const adapter = (0, adapter_ts_1.createOzAdapter)(ozConfig);
|
|
26
|
-
const result = adapter.compile(ir);
|
|
27
|
-
if (!result.proposed) {
|
|
28
|
-
return {
|
|
29
|
-
ok: false,
|
|
30
|
-
error: {
|
|
31
|
-
code: 'SYNTHESIS_ERROR',
|
|
32
|
-
message: `mandate lowered to no installable OZ policy: ${result.uncovered.join('; ')}`,
|
|
33
|
-
severity: 'error',
|
|
34
|
-
retryable: false,
|
|
35
|
-
details: { uncovered: result.uncovered },
|
|
36
|
-
},
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
const proposed = {
|
|
40
|
-
...result.proposed,
|
|
41
|
-
warnings: result.uncovered.map((u) => `${UNCOVERED_PREFIX}${u}`),
|
|
42
|
-
};
|
|
43
|
-
// Same --explain envelope pattern as the recording path. The mandate path
|
|
44
|
-
// never produces an interpreter predicate, so predicateTree is null and
|
|
45
|
-
// the simulation is a minimal honest deny (no self-verify was performed).
|
|
46
|
-
const envelope = { ok: true, data: proposed };
|
|
47
|
-
if (opts?.explain) {
|
|
48
|
-
envelope.explain = {
|
|
49
|
-
predicateTree: null,
|
|
50
|
-
simulation: {
|
|
51
|
-
permit: {
|
|
52
|
-
tx: 'deny',
|
|
53
|
-
reason: 'No self-verification was performed (mandate path is OZ-only)',
|
|
54
|
-
},
|
|
55
|
-
evaluatedCases: [],
|
|
56
|
-
backend: 'ts-model',
|
|
57
|
-
simulatorVersion: 'not-run',
|
|
58
|
-
},
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
return envelope;
|
|
62
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
export type SimulationResult = {
|
|
2
|
-
permit: {
|
|
3
|
-
tx: 'permit';
|
|
4
|
-
} | {
|
|
5
|
-
tx: 'deny';
|
|
6
|
-
reason: string;
|
|
7
|
-
};
|
|
8
|
-
evaluatedCases: Array<{
|
|
9
|
-
dimension: string;
|
|
10
|
-
outcome: 'permit' | 'deny';
|
|
11
|
-
reason: string;
|
|
12
|
-
}>;
|
|
13
|
-
backend: 'interpreter-v1' | 'ts-model';
|
|
14
|
-
simulatorVersion: string;
|
|
15
|
-
};
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// src/verify/envelope.ts - the post-simulation result envelope used by
|
|
3
|
-
// review-card rendering and verification.
|
|
4
|
-
//
|
|
5
|
-
// `SimulationResult` is the structured verdict a `simulate_policy` run
|
|
6
|
-
// produces; the review-card builder reads it as one of its inputs (so the
|
|
7
|
-
// rendered card can quote the backend that evaluated the policy) and the
|
|
8
|
-
// verification pipeline reads `permit` + `evaluatedCases` to confirm every
|
|
9
|
-
// generated deny case really did deny.
|
|
10
|
-
//
|
|
11
|
-
// This envelope is intentionally separate from the CustodyAdapter
|
|
12
|
-
// `SimulationResult` in `src/seams/types.ts` (which is the dry-run stub
|
|
13
|
-
// returned by `adapter.simulate(ir, permitTx)`); the seam result is the
|
|
14
|
-
// adapter contract, this envelope is the post-simulation record consumed by
|
|
15
|
-
// downstream rendering + verification.
|
|
16
|
-
//
|
|
17
|
-
// Fields:
|
|
18
|
-
// - `permit` is the single verdict for the candidate recorded tx.
|
|
19
|
-
// - `evaluatedCases` is the deny-case battery outcome (every dimension
|
|
20
|
-
// must report `deny` when the policy is minimal).
|
|
21
|
-
// - `backend` is the actual evaluator that produced the verdict.
|
|
22
|
-
// - `simulatorVersion` lets the reviewer / audit log distinguish runs.
|
|
23
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
package/dist-cjs/verify/index.js
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// src/verify/index.ts - re-export the simulation / verification surface.
|
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.verifyPolicy = exports.simulatePolicy = void 0;
|
|
5
|
-
var simulate_ts_1 = require("./simulate.js");
|
|
6
|
-
Object.defineProperty(exports, "simulatePolicy", { enumerable: true, get: function () { return simulate_ts_1.simulatePolicy; } });
|
|
7
|
-
var verify_ts_1 = require("./verify.js");
|
|
8
|
-
Object.defineProperty(exports, "verifyPolicy", { enumerable: true, get: function () { return verify_ts_1.verifyPolicy; } });
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import type { ToolResponse } from '../errors.ts';
|
|
2
|
-
import type { PredicateNode, RecordedTransaction } from '../types.ts';
|
|
3
|
-
import type { SimulationResult } from './envelope.ts';
|
|
4
|
-
/** Options for `simulatePolicy`. `validUntilLedger` is propagated onto the
|
|
5
|
-
* permit EvalContext so the simulator exercises the expiry gate; absent ->
|
|
6
|
-
* no expiry check (mirrors the orchestrator's "no `validUntilLedger`
|
|
7
|
-
* supplied" path). */
|
|
8
|
-
export interface SimulateOptions {
|
|
9
|
-
validUntilLedger?: number;
|
|
10
|
-
}
|
|
11
|
-
/** Replay a recorded transaction against a proposed predicate and emit the
|
|
12
|
-
* `SimulationResult` envelope. The simulator returns the SAME permit
|
|
13
|
-
* verdict `runHarness` expects: the intended recorded call must permit; every
|
|
14
|
-
* generated deny dimension must deny. A runtime evaluation failure
|
|
15
|
-
* `ToolError` - NOT a deny verdict, NOT `VERIFICATION_FAILED`. The boundary
|
|
16
|
-
* is pinned: minimality is a verify-time concern, runtime evaluation is a
|
|
17
|
-
* simulate-time concern. */
|
|
18
|
-
export declare function simulatePolicy(predicate: PredicateNode | null, permitTx: RecordedTransaction, opts?: SimulateOptions): ToolResponse<SimulationResult>;
|