@crediolabs/policy-synth 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -4
- package/dist/adapters/interpreter/adapter.d.ts +19 -25
- package/dist/adapters/interpreter/adapter.js +54 -487
- package/dist/adapters/interpreter/index.d.ts +1 -1
- package/dist/adapters/interpreter/index.js +1 -1
- package/dist/adapters/oz/adapter.js +1 -15
- package/dist/errors.d.ts +1 -1
- package/dist/index.d.ts +1 -4
- package/dist/index.js +1 -4
- package/dist/install/build-add-context-rule.d.ts +3 -19
- package/dist/install/build-add-context-rule.js +3 -98
- package/dist/install/build-install-policy.d.ts +4 -53
- package/dist/install/build-install-policy.js +44 -123
- package/dist/install/index.d.ts +0 -2
- package/dist/install/index.js +0 -7
- package/dist/ir/index.d.ts +1 -1
- package/dist/ir/types.d.ts +7 -76
- package/dist/ir/types.js +0 -2
- package/dist/predicate/decode.d.ts +1 -1
- package/dist/predicate/decode.js +2 -70
- package/dist/predicate/encode.js +45 -229
- package/dist/predicate/from-json.js +1 -42
- package/dist/record/decode.js +3 -8
- package/dist/review-card/builder.d.ts +4 -3
- package/dist/review-card/builder.js +18 -162
- package/dist/review-card/cross-check.js +2 -105
- package/dist/review-card/render-leaf.d.ts +4 -0
- package/dist/review-card/render-leaf.js +47 -0
- package/dist/run/index.d.ts +36 -139
- package/dist/run/index.js +125 -437
- package/dist/run/schemas.d.ts +168 -1929
- package/dist/run/schemas.js +40 -269
- package/dist/seams/types.d.ts +0 -2
- package/dist/simulate/deny-cases.d.ts +26 -0
- package/dist/simulate/deny-cases.js +282 -0
- package/dist/simulate/evaluate.d.ts +18 -0
- package/dist/simulate/evaluate.js +271 -0
- package/dist/simulate/index.d.ts +4 -0
- package/dist/simulate/index.js +8 -0
- package/dist/synth/compose-from-recording.d.ts +24 -51
- package/dist/synth/compose-from-recording.js +120 -282
- package/dist/synth/deny-cases.d.ts +5 -7
- package/dist/synth/deny-cases.js +23 -373
- package/dist/synth/evaluate.d.ts +0 -21
- package/dist/synth/evaluate.js +56 -336
- package/dist/synth/index.d.ts +0 -5
- package/dist/synth/index.js +0 -5
- package/dist/synth/permit-context.d.ts +2 -9
- package/dist/synth/permit-context.js +1 -82
- package/dist/synth/synthesize-from-recording.d.ts +1 -29
- package/dist/synth/synthesize-from-recording.js +41 -356
- package/dist/types.d.ts +8 -60
- package/dist/types.js +5 -2
- package/dist/verify/simulate.d.ts +1 -14
- package/dist/verify/simulate.js +2 -88
- package/dist/verify/verify.d.ts +1 -7
- package/dist/verify/verify.js +0 -8
- package/dist-cjs/adapters/interpreter/adapter.d.ts +19 -25
- package/dist-cjs/adapters/interpreter/adapter.js +56 -489
- package/dist-cjs/adapters/interpreter/index.d.ts +1 -1
- package/dist-cjs/adapters/interpreter/index.js +1 -2
- package/dist-cjs/adapters/oz/adapter.js +1 -15
- package/dist-cjs/errors.d.ts +1 -1
- package/dist-cjs/index.d.ts +1 -4
- package/dist-cjs/index.js +1 -4
- package/dist-cjs/install/build-add-context-rule.d.ts +3 -19
- package/dist-cjs/install/build-add-context-rule.js +1 -97
- package/dist-cjs/install/build-install-policy.d.ts +4 -53
- package/dist-cjs/install/build-install-policy.js +44 -122
- package/dist-cjs/install/index.d.ts +0 -2
- package/dist-cjs/install/index.js +2 -23
- package/dist-cjs/ir/index.d.ts +1 -1
- package/dist-cjs/ir/types.d.ts +7 -76
- package/dist-cjs/ir/types.js +0 -2
- package/dist-cjs/predicate/decode.d.ts +1 -1
- package/dist-cjs/predicate/decode.js +2 -70
- package/dist-cjs/predicate/encode.js +45 -229
- package/dist-cjs/predicate/from-json.js +1 -42
- package/dist-cjs/record/decode.js +3 -8
- package/dist-cjs/review-card/builder.d.ts +4 -3
- package/dist-cjs/review-card/builder.js +23 -167
- package/dist-cjs/review-card/cross-check.js +7 -110
- package/dist-cjs/review-card/render-leaf.d.ts +4 -0
- package/dist-cjs/review-card/render-leaf.js +52 -0
- package/dist-cjs/run/index.d.ts +36 -139
- package/dist-cjs/run/index.js +125 -444
- package/dist-cjs/run/schemas.d.ts +168 -1929
- package/dist-cjs/run/schemas.js +41 -270
- package/dist-cjs/seams/types.d.ts +0 -2
- package/dist-cjs/simulate/deny-cases.d.ts +26 -0
- package/dist-cjs/simulate/deny-cases.js +286 -0
- package/dist-cjs/simulate/evaluate.d.ts +18 -0
- package/dist-cjs/simulate/evaluate.js +274 -0
- package/dist-cjs/simulate/index.d.ts +4 -0
- package/dist-cjs/simulate/index.js +13 -0
- package/dist-cjs/synth/compose-from-recording.d.ts +24 -51
- package/dist-cjs/synth/compose-from-recording.js +120 -282
- package/dist-cjs/synth/deny-cases.d.ts +5 -7
- package/dist-cjs/synth/deny-cases.js +23 -374
- package/dist-cjs/synth/evaluate.d.ts +0 -21
- package/dist-cjs/synth/evaluate.js +57 -337
- package/dist-cjs/synth/index.d.ts +0 -5
- package/dist-cjs/synth/index.js +1 -11
- package/dist-cjs/synth/permit-context.d.ts +2 -9
- package/dist-cjs/synth/permit-context.js +1 -82
- package/dist-cjs/synth/synthesize-from-recording.d.ts +1 -29
- package/dist-cjs/synth/synthesize-from-recording.js +39 -354
- package/dist-cjs/types.d.ts +8 -60
- package/dist-cjs/types.js +6 -3
- package/dist-cjs/verify/simulate.d.ts +1 -14
- package/dist-cjs/verify/simulate.js +2 -88
- package/dist-cjs/verify/verify.d.ts +1 -7
- package/dist-cjs/verify/verify.js +0 -8
- package/package.json +4 -4
- package/src/adapters/interpreter/adapter.ts +76 -572
- package/src/errors.ts +0 -19
- package/src/index.ts +1 -4
- package/src/install/build-add-context-rule.ts +5 -139
- package/src/install/build-install-policy.ts +93 -214
- package/src/install/index.ts +0 -34
- package/src/predicate/decode.ts +2 -70
- package/src/predicate/encode.ts +49 -261
- package/src/predicate/from-json.ts +1 -43
- package/src/record/decode.ts +3 -8
- package/src/review-card/builder.ts +19 -168
- package/src/review-card/cross-check.ts +3 -105
- package/src/review-card/render-leaf.ts +48 -0
- package/src/run/index.ts +144 -572
- package/src/run/schemas.ts +42 -291
- package/src/simulate/deny-cases.ts +334 -0
- package/src/simulate/evaluate.ts +284 -0
- package/src/simulate/index.ts +11 -0
- package/src/synth/compose-from-recording.ts +148 -347
- package/src/synth/index.ts +0 -13
- package/src/synth/synthesize-from-recording.ts +43 -456
- package/src/types.ts +13 -49
- package/dist/install/authority-overlap.d.ts +0 -134
- package/dist/install/authority-overlap.js +0 -0
- package/dist/install/build-install-predicate.d.ts +0 -96
- package/dist/install/build-install-predicate.js +0 -444
- package/dist/install/build-merge-policy.d.ts +0 -70
- package/dist/install/build-merge-policy.js +0 -130
- package/dist/install/plan-merge-policy.d.ts +0 -49
- package/dist/install/plan-merge-policy.js +0 -86
- package/dist/install/read-account-rules.d.ts +0 -100
- package/dist/install/read-account-rules.js +0 -283
- package/dist-cjs/install/authority-overlap.d.ts +0 -134
- package/dist-cjs/install/authority-overlap.js +0 -0
- package/dist-cjs/install/build-install-predicate.d.ts +0 -96
- package/dist-cjs/install/build-install-predicate.js +0 -479
- package/dist-cjs/install/build-merge-policy.d.ts +0 -70
- package/dist-cjs/install/build-merge-policy.js +0 -134
- package/dist-cjs/install/plan-merge-policy.d.ts +0 -49
- package/dist-cjs/install/plan-merge-policy.js +0 -90
- package/dist-cjs/install/read-account-rules.d.ts +0 -100
- package/dist-cjs/install/read-account-rules.js +0 -296
- package/src/adapters/interpreter/index.ts +0 -8
- package/src/adapters/oz/adapter.ts +0 -376
- package/src/adapters/oz/index.ts +0 -9
- package/src/codegen/compile-gate.ts +0 -167
- package/src/codegen/index.ts +0 -17
- package/src/codegen/template.ts +0 -165
- package/src/install/authority-overlap.ts +0 -0
- package/src/install/build-merge-policy.ts +0 -219
- package/src/install/plan-merge-policy.ts +0 -133
- package/src/install/read-account-rules.ts +0 -376
- package/src/ir/index.ts +0 -13
- package/src/ir/types.ts +0 -132
- package/src/mandate/index.ts +0 -4
- package/src/mandate/to-ir.ts +0 -71
- package/src/mandate/types.ts +0 -21
- package/src/seams/index.ts +0 -11
- package/src/seams/types.ts +0 -81
- package/src/synth/deny-cases.ts +0 -663
- package/src/synth/evaluate.ts +0 -613
- package/src/synth/harness.ts +0 -68
- package/src/synth/minimize.ts +0 -48
- package/src/synth/permit-context.ts +0 -136
- package/src/synth/predicate-literals.ts +0 -27
- package/src/synth/synthesize-from-mandate.ts +0 -82
- package/src/verify/envelope.ts +0 -28
- package/src/verify/index.ts +0 -5
- package/src/verify/simulate.ts +0 -311
- package/src/verify/verify.ts +0 -243
package/dist/types.d.ts
CHANGED
|
@@ -77,13 +77,11 @@ export declare const MAX_SCVAL_CLONE_DEPTH: 30;
|
|
|
77
77
|
* - MAX_DEPTH = 5: 3 walkthroughs need depth 2; budget = 2x growth + 1 for `not` chain.
|
|
78
78
|
* - MAX_LEAVES = 200: allowlists (recipients, paths) typically < 100; budget = 2x + 1 nested allowlist.
|
|
79
79
|
* - MAX_PREDICATE_BYTES = 32 KB: well under the per-ledger-entry 64 KB persistent-storage limit.
|
|
80
|
-
* - MAX_ORACLE_READS = 6: two-round confirmation per asset consumes 2 reads; cap accommodates a predicate that references up to 3 unique oracle assets (3 * 2 = 6 reads). Stays a fail-closed install-time cap.
|
|
81
80
|
* - MAX_IN_OPERAND_COUNT = 32: allowlist < 32 is a UX-natural bound. */
|
|
82
81
|
export declare const PREDICATE_CAPS: {
|
|
83
82
|
readonly MAX_DEPTH: 5;
|
|
84
83
|
readonly MAX_LEAVES: 200;
|
|
85
84
|
readonly MAX_PREDICATE_BYTES: number;
|
|
86
|
-
readonly MAX_ORACLE_READS: 6;
|
|
87
85
|
readonly MAX_IN_OPERAND_COUNT: 32;
|
|
88
86
|
};
|
|
89
87
|
/** OZ context-rule draft - the unit the install builder builds add_context_rule from. */
|
|
@@ -118,19 +116,20 @@ export type SignerDraft = {
|
|
|
118
116
|
};
|
|
119
117
|
/** Reference to one policy attached to a context rule. */
|
|
120
118
|
export type PolicyRef = {
|
|
121
|
-
kind: 'oz_builtin';
|
|
122
|
-
primitive: OZPrimitiveConfig;
|
|
123
|
-
instanceAddress: string;
|
|
124
|
-
} | {
|
|
125
119
|
kind: 'interpreter';
|
|
126
120
|
interpreterAddress: string;
|
|
127
121
|
predicateBlobBase64: string;
|
|
128
122
|
};
|
|
123
|
+
/** Grammar version baked into the interpreter wasm, mirroring `SELF_VERSION` in
|
|
124
|
+
* `contracts/policy-interpreter/src/version.rs`. Every value this package puts on
|
|
125
|
+
* the wire derives from here; `grammar-version-parity.test.ts` pins it to the
|
|
126
|
+
* contract so a skew cannot pass a green test run. */
|
|
127
|
+
export declare const GRAMMAR_VERSION: 3;
|
|
129
128
|
/** A serialised predicate document stored against a (smart_account, rule_id) pair. */
|
|
130
129
|
export interface PolicyDocument {
|
|
131
130
|
/** Grammar version baked into the interpreter wasm. Fail-closed strict match at install
|
|
132
131
|
* AND at evaluate (defence in depth). NOT the per-edit revision. */
|
|
133
|
-
grammarVersion:
|
|
132
|
+
grammarVersion: typeof GRAMMAR_VERSION;
|
|
134
133
|
/** Per-rule install nonce. Must equal the interpreter's stored nonce + 1 at install.
|
|
135
134
|
* First install accepts nonce = 1 with stored nonce = 0. Re-install is structurally
|
|
136
135
|
* possible by incrementing the nonce; uninstall removes the nonce with state so a
|
|
@@ -142,32 +141,17 @@ export interface PolicyDocument {
|
|
|
142
141
|
/** Hash of the post-canonicalisation bytes of encodedPredicate. Stored alongside the
|
|
143
142
|
* doc and re-verified at evaluate. */
|
|
144
143
|
predicateHash: string;
|
|
145
|
-
/** Per-policy overrides of oracle thresholds; absent = use defaults.
|
|
146
|
-
* Overrides may TIGHTEN only (never widen) - the interpreter rejects any per-policy
|
|
147
|
-
* value looser than the wasm-level defaults at install. */
|
|
148
|
-
oracleParams?: {
|
|
149
|
-
/** Seconds; must be <= MAX_ORACLE_STALENESS_SECONDS (600). */
|
|
150
|
-
maxStalenessSeconds?: number;
|
|
151
|
-
/** Basis points; must be <= MAX_ORACLE_DEVIATION_BPS (200). */
|
|
152
|
-
maxDeviationBps?: number;
|
|
153
|
-
};
|
|
154
144
|
}
|
|
155
145
|
/** The versioned predicate AST (interpreter side). Tagged union. */
|
|
156
146
|
export type PredicateNode = {
|
|
157
147
|
op: 'and';
|
|
158
148
|
children: PredicateNode[];
|
|
159
|
-
} | {
|
|
160
|
-
op: 'or';
|
|
161
|
-
children: PredicateNode[];
|
|
162
|
-
} | {
|
|
163
|
-
op: 'not';
|
|
164
|
-
child: PredicateNode;
|
|
165
149
|
} | {
|
|
166
150
|
op: 'eq';
|
|
167
151
|
left: PredicateLeaf;
|
|
168
152
|
right: PredicateLeaf;
|
|
169
153
|
} | {
|
|
170
|
-
op: '
|
|
154
|
+
op: 'lte';
|
|
171
155
|
left: PredicateLeaf;
|
|
172
156
|
right: PredicateLeaf;
|
|
173
157
|
} | {
|
|
@@ -190,32 +174,6 @@ export type PredicateLeaf = {
|
|
|
190
174
|
index: number;
|
|
191
175
|
element: number;
|
|
192
176
|
field: string;
|
|
193
|
-
} | {
|
|
194
|
-
kind: 'call_arg_scaled';
|
|
195
|
-
index: number;
|
|
196
|
-
num: string;
|
|
197
|
-
den: string;
|
|
198
|
-
} | {
|
|
199
|
-
kind: 'amount';
|
|
200
|
-
token: string;
|
|
201
|
-
} | {
|
|
202
|
-
kind: 'window_spent';
|
|
203
|
-
token: string;
|
|
204
|
-
windowSeconds: number;
|
|
205
|
-
} | {
|
|
206
|
-
kind: 'now';
|
|
207
|
-
} | {
|
|
208
|
-
kind: 'valid_until';
|
|
209
|
-
} | {
|
|
210
|
-
kind: 'invocation_count_in_window';
|
|
211
|
-
windowSecs: number;
|
|
212
|
-
} | {
|
|
213
|
-
kind: 'oracle_price';
|
|
214
|
-
asset: string;
|
|
215
|
-
} | {
|
|
216
|
-
kind: 'oracle_threshold';
|
|
217
|
-
value: string;
|
|
218
|
-
decimals: number;
|
|
219
177
|
} | {
|
|
220
178
|
kind: 'literal_address';
|
|
221
179
|
value: string;
|
|
@@ -228,20 +186,10 @@ export type PredicateLeaf = {
|
|
|
228
186
|
} | {
|
|
229
187
|
kind: 'literal_u32';
|
|
230
188
|
value: number;
|
|
231
|
-
} | {
|
|
232
|
-
kind: 'literal_u64';
|
|
233
|
-
value: string;
|
|
234
|
-
} | {
|
|
235
|
-
kind: 'literal_bytes';
|
|
236
|
-
value: string;
|
|
237
189
|
} | {
|
|
238
190
|
kind: 'literal_vec';
|
|
239
191
|
elements: PredicateLeaf[];
|
|
240
192
|
};
|
|
241
|
-
export interface OZPrimitiveConfig {
|
|
242
|
-
primitive: 'spending_limit' | 'simple_threshold' | 'weighted_threshold';
|
|
243
|
-
params: Record<string, unknown>;
|
|
244
|
-
}
|
|
245
193
|
export interface ContractInvocation {
|
|
246
194
|
contract: string;
|
|
247
195
|
fn: string;
|
|
@@ -297,7 +245,7 @@ export interface AmbiguityPrompt {
|
|
|
297
245
|
/** Catalogue of named ambiguity codes (the skill's prompt files are one-per-code).
|
|
298
246
|
* Adding a code is a synthesizer decision; the prompt authoring must not
|
|
299
247
|
* invent codes outside this union. */
|
|
300
|
-
export type AmbiguityCode = 'DURATION_UNSPECIFIED' | 'AMOUNT_BOUND_MISSING' | 'RECIPIENT_ALLOWLIST_EMPTY' | 'FREQUENCY_BOUND_MISSING' | '
|
|
248
|
+
export type AmbiguityCode = 'DURATION_UNSPECIFIED' | 'AMOUNT_BOUND_MISSING' | 'RECIPIENT_ALLOWLIST_EMPTY' | 'FREQUENCY_BOUND_MISSING' | 'MULTIPLE_UNRELATED_TARGETS';
|
|
301
249
|
/** Structured recording freshness. `overall` is the gate threshold (default 1.0); the
|
|
302
250
|
* breakdown is the diagnostic the user / agent sees when the gate fires. The
|
|
303
251
|
* computation rule is pinned: `overall = 1 - (unknownContracts + opaqueScVals) / total`.
|
package/dist/types.js
CHANGED
|
@@ -41,12 +41,15 @@ export const MAX_SCVAL_CLONE_DEPTH = 30;
|
|
|
41
41
|
* - MAX_DEPTH = 5: 3 walkthroughs need depth 2; budget = 2x growth + 1 for `not` chain.
|
|
42
42
|
* - MAX_LEAVES = 200: allowlists (recipients, paths) typically < 100; budget = 2x + 1 nested allowlist.
|
|
43
43
|
* - MAX_PREDICATE_BYTES = 32 KB: well under the per-ledger-entry 64 KB persistent-storage limit.
|
|
44
|
-
* - MAX_ORACLE_READS = 6: two-round confirmation per asset consumes 2 reads; cap accommodates a predicate that references up to 3 unique oracle assets (3 * 2 = 6 reads). Stays a fail-closed install-time cap.
|
|
45
44
|
* - MAX_IN_OPERAND_COUNT = 32: allowlist < 32 is a UX-natural bound. */
|
|
46
45
|
export const PREDICATE_CAPS = {
|
|
47
46
|
MAX_DEPTH: 5,
|
|
48
47
|
MAX_LEAVES: 200,
|
|
49
48
|
MAX_PREDICATE_BYTES: 32 * 1024,
|
|
50
|
-
MAX_ORACLE_READS: 6,
|
|
51
49
|
MAX_IN_OPERAND_COUNT: 32,
|
|
52
50
|
};
|
|
51
|
+
/** Grammar version baked into the interpreter wasm, mirroring `SELF_VERSION` in
|
|
52
|
+
* `contracts/policy-interpreter/src/version.rs`. Every value this package puts on
|
|
53
|
+
* the wire derives from here; `grammar-version-parity.test.ts` pins it to the
|
|
54
|
+
* contract so a skew cannot pass a green test run. */
|
|
55
|
+
export const GRAMMAR_VERSION = 3;
|
|
@@ -4,27 +4,14 @@ import type { SimulationResult } from './envelope.ts';
|
|
|
4
4
|
/** Options for `simulatePolicy`. `validUntilLedger` is propagated onto the
|
|
5
5
|
* permit EvalContext so the simulator exercises the expiry gate; absent ->
|
|
6
6
|
* no expiry check (mirrors the orchestrator's "no `validUntilLedger`
|
|
7
|
-
* supplied" path).
|
|
8
|
-
* uses to satisfy `oracle_price` leaves so the permit call evaluates under
|
|
9
|
-
* the bound; absent -> derive satisfying prices from the predicate itself
|
|
10
|
-
* (the orchestrator's oracle-satisfying-price logic). */
|
|
7
|
+
* supplied" path). */
|
|
11
8
|
export interface SimulateOptions {
|
|
12
9
|
validUntilLedger?: number;
|
|
13
|
-
/** Pre-populated oracle-price entries keyed by asset address. The fixture
|
|
14
|
-
* must satisfy every `oracle_price` leaf in the predicate; absent or
|
|
15
|
-
* unsatisfying entries cause `SIMULATION_ERROR`. */
|
|
16
|
-
oraclePricesByAsset?: Record<string, {
|
|
17
|
-
price: string;
|
|
18
|
-
timestampSeconds: number;
|
|
19
|
-
} | {
|
|
20
|
-
error: 'stale' | 'missing' | 'deviation' | 'paused' | 'decimals' | 'fingerprint';
|
|
21
|
-
}>;
|
|
22
10
|
}
|
|
23
11
|
/** Replay a recorded transaction against a proposed predicate and emit the
|
|
24
12
|
* `SimulationResult` envelope. The simulator returns the SAME permit
|
|
25
13
|
* verdict `runHarness` expects: the intended recorded call must permit; every
|
|
26
14
|
* generated deny dimension must deny. A runtime evaluation failure
|
|
27
|
-
* (malformed input, missing oracle fixture, etc.) returns a `SIMULATION_ERROR`
|
|
28
15
|
* `ToolError` - NOT a deny verdict, NOT `VERIFICATION_FAILED`. The boundary
|
|
29
16
|
* is pinned: minimality is a verify-time concern, runtime evaluation is a
|
|
30
17
|
* simulate-time concern. */
|
package/dist/verify/simulate.js
CHANGED
|
@@ -7,9 +7,7 @@
|
|
|
7
7
|
// Boundary (pinned, must not drift):
|
|
8
8
|
// - `SIMULATION_ERROR` = RUNTIME evaluation failed. Surfaced by
|
|
9
9
|
// `simulatePolicy`. Reasons: malformed `permitTx` input (no top-level
|
|
10
|
-
// invocation to build an EvalContext from), a referenced oracle asset
|
|
11
10
|
// without a satisfying price fixture, or a non-runtime propagation (a
|
|
12
|
-
// throw that is not an oracle error or a controlled deny). A
|
|
13
11
|
// runtime evaluation failure is NOT a policy-minimality problem; the
|
|
14
12
|
// policy may still be minimal. Re-running with a complete fixture MAY
|
|
15
13
|
// succeed.
|
|
@@ -34,7 +32,6 @@ const SIMULATOR_VERSION = 'ts-model-1.0.0';
|
|
|
34
32
|
* `SimulationResult` envelope. The simulator returns the SAME permit
|
|
35
33
|
* verdict `runHarness` expects: the intended recorded call must permit; every
|
|
36
34
|
* generated deny dimension must deny. A runtime evaluation failure
|
|
37
|
-
* (malformed input, missing oracle fixture, etc.) returns a `SIMULATION_ERROR`
|
|
38
35
|
* `ToolError` - NOT a deny verdict, NOT `VERIFICATION_FAILED`. The boundary
|
|
39
36
|
* is pinned: minimality is a verify-time concern, runtime evaluation is a
|
|
40
37
|
* simulate-time concern. */
|
|
@@ -51,7 +48,7 @@ export function simulatePolicy(predicate, permitTx, opts = {}) {
|
|
|
51
48
|
}
|
|
52
49
|
let permitCtx;
|
|
53
50
|
try {
|
|
54
|
-
permitCtx = buildPermitContext(
|
|
51
|
+
permitCtx = buildPermitContext(permitTx, topLevel, opts);
|
|
55
52
|
}
|
|
56
53
|
catch (e) {
|
|
57
54
|
return {
|
|
@@ -117,7 +114,7 @@ export function simulatePolicy(predicate, permitTx, opts = {}) {
|
|
|
117
114
|
* rather than import to keep `src/verify/` decoupled from the
|
|
118
115
|
* orchestrator's private helpers. The shape is pinned by tests so the
|
|
119
116
|
* two implementations stay in lockstep. */
|
|
120
|
-
function buildPermitContext(
|
|
117
|
+
function buildPermitContext(tx, topLevel, opts) {
|
|
121
118
|
const amountByToken = {};
|
|
122
119
|
const totals = new Map();
|
|
123
120
|
for (const m of tx.tokenMovements) {
|
|
@@ -127,41 +124,6 @@ function buildPermitContext(predicate, tx, topLevel, opts) {
|
|
|
127
124
|
for (const [token, total] of totals) {
|
|
128
125
|
amountByToken[token] = total.toString();
|
|
129
126
|
}
|
|
130
|
-
// Oracle prices: when the caller supplies a fixture, use it; otherwise
|
|
131
|
-
// derive a satisfying entry per `oracle_price` leaf in the predicate so
|
|
132
|
-
// the permit call permits. Negatives are clamped at 0 (Stellar oracle
|
|
133
|
-
// prices are non-negative).
|
|
134
|
-
const oraclePriceByAsset = {};
|
|
135
|
-
if (opts.oraclePricesByAsset) {
|
|
136
|
-
for (const [asset, entry] of Object.entries(opts.oraclePricesByAsset)) {
|
|
137
|
-
oraclePriceByAsset[asset] = entry;
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
if (predicate !== null) {
|
|
141
|
-
visitOracleLeaves(predicate, (asset, op, bound) => {
|
|
142
|
-
// A caller-supplied fixture entry always wins; we only fill gaps.
|
|
143
|
-
if (oraclePriceByAsset[asset] !== undefined)
|
|
144
|
-
return;
|
|
145
|
-
let price;
|
|
146
|
-
switch (op) {
|
|
147
|
-
case 'lt':
|
|
148
|
-
case 'gt':
|
|
149
|
-
price = op === 'lt' ? bound - 1n : bound + 1n;
|
|
150
|
-
break;
|
|
151
|
-
case 'lte':
|
|
152
|
-
case 'gte':
|
|
153
|
-
case 'eq':
|
|
154
|
-
price = bound;
|
|
155
|
-
break;
|
|
156
|
-
}
|
|
157
|
-
if (price < 0n)
|
|
158
|
-
price = 0n;
|
|
159
|
-
oraclePriceByAsset[asset] = {
|
|
160
|
-
price: price.toString(),
|
|
161
|
-
timestampSeconds: tx.fetchedAt,
|
|
162
|
-
};
|
|
163
|
-
});
|
|
164
|
-
}
|
|
165
127
|
const ctx = {
|
|
166
128
|
contract: topLevel.contract,
|
|
167
129
|
fn: topLevel.fn,
|
|
@@ -170,8 +132,6 @@ function buildPermitContext(predicate, tx, topLevel, opts) {
|
|
|
170
132
|
nowSeconds: tx.fetchedAt,
|
|
171
133
|
amountByToken,
|
|
172
134
|
windowSpentByToken: {},
|
|
173
|
-
invocationCountByWindow: {},
|
|
174
|
-
oraclePriceByAsset,
|
|
175
135
|
};
|
|
176
136
|
if (opts.validUntilLedger !== undefined) {
|
|
177
137
|
ctx.validUntilLedger = opts.validUntilLedger;
|
|
@@ -199,52 +159,6 @@ function cloneDepthError(value) {
|
|
|
199
159
|
err.depthContext = value.type;
|
|
200
160
|
throw err;
|
|
201
161
|
}
|
|
202
|
-
function visitOracleLeaves(node, visit) {
|
|
203
|
-
switch (node.op) {
|
|
204
|
-
case 'and':
|
|
205
|
-
case 'or':
|
|
206
|
-
for (const child of node.children)
|
|
207
|
-
visitOracleLeaves(child, visit);
|
|
208
|
-
return;
|
|
209
|
-
case 'not':
|
|
210
|
-
visitOracleLeaves(node.child, visit);
|
|
211
|
-
return;
|
|
212
|
-
case 'eq':
|
|
213
|
-
case 'lt':
|
|
214
|
-
case 'lte':
|
|
215
|
-
case 'gt':
|
|
216
|
-
case 'gte': {
|
|
217
|
-
const leftLeaf = node.left;
|
|
218
|
-
const rightLeaf = node.right;
|
|
219
|
-
let oracleAsset;
|
|
220
|
-
let literal;
|
|
221
|
-
if (leftLeaf.kind === 'oracle_price') {
|
|
222
|
-
oracleAsset = leftLeaf.asset;
|
|
223
|
-
literal = oracleLiteralFromLeaf(rightLeaf);
|
|
224
|
-
}
|
|
225
|
-
else if (rightLeaf.kind === 'oracle_price') {
|
|
226
|
-
oracleAsset = rightLeaf.asset;
|
|
227
|
-
literal = oracleLiteralFromLeaf(leftLeaf);
|
|
228
|
-
}
|
|
229
|
-
if (oracleAsset === undefined || literal === undefined)
|
|
230
|
-
return;
|
|
231
|
-
visit(oracleAsset, node.op, literal);
|
|
232
|
-
return;
|
|
233
|
-
}
|
|
234
|
-
case 'in':
|
|
235
|
-
return;
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
function oracleLiteralFromLeaf(leaf) {
|
|
239
|
-
if (leaf.kind !== 'literal_i128')
|
|
240
|
-
return undefined;
|
|
241
|
-
try {
|
|
242
|
-
return BigInt(leaf.value);
|
|
243
|
-
}
|
|
244
|
-
catch {
|
|
245
|
-
return undefined;
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
162
|
function simulationError(message, cause) {
|
|
249
163
|
const error = {
|
|
250
164
|
code: 'SIMULATION_ERROR',
|
package/dist/verify/verify.d.ts
CHANGED
|
@@ -1,16 +1,10 @@
|
|
|
1
1
|
import type { ToolResponse } from '../errors.ts';
|
|
2
2
|
import type { PredicateNode, RecordedTransaction } from '../types.ts';
|
|
3
3
|
/** Options for `verifyPolicy`. Mirrors the orchestrator's permit-context
|
|
4
|
-
* knobs (expiry
|
|
4
|
+
* knobs (expiry) so verify sees the same shape
|
|
5
5
|
* `simulatePolicy` sees. */
|
|
6
6
|
export interface VerifyOptions {
|
|
7
7
|
validUntilLedger?: number;
|
|
8
|
-
oraclePricesByAsset?: Record<string, {
|
|
9
|
-
price: string;
|
|
10
|
-
timestampSeconds: number;
|
|
11
|
-
} | {
|
|
12
|
-
error: 'stale' | 'missing' | 'deviation' | 'paused' | 'decimals' | 'fingerprint';
|
|
13
|
-
}>;
|
|
14
8
|
}
|
|
15
9
|
/** Run the static minimality check on a proposed predicate + recorded tx.
|
|
16
10
|
* Returns `{ ok: true }` when the predicate is minimal (every top-level
|
package/dist/verify/verify.js
CHANGED
|
@@ -104,12 +104,6 @@ function buildPermitContextForVerify(tx, topLevel, opts) {
|
|
|
104
104
|
for (const [token, total] of totals) {
|
|
105
105
|
amountByToken[token] = total.toString();
|
|
106
106
|
}
|
|
107
|
-
const oraclePriceByAsset = {};
|
|
108
|
-
if (opts.oraclePricesByAsset) {
|
|
109
|
-
for (const [asset, entry] of Object.entries(opts.oraclePricesByAsset)) {
|
|
110
|
-
oraclePriceByAsset[asset] = entry;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
107
|
const ctx = {
|
|
114
108
|
contract: topLevel.contract,
|
|
115
109
|
fn: topLevel.fn,
|
|
@@ -118,8 +112,6 @@ function buildPermitContextForVerify(tx, topLevel, opts) {
|
|
|
118
112
|
nowSeconds: tx.fetchedAt,
|
|
119
113
|
amountByToken,
|
|
120
114
|
windowSpentByToken: {},
|
|
121
|
-
invocationCountByWindow: {},
|
|
122
|
-
oraclePriceByAsset,
|
|
123
115
|
};
|
|
124
116
|
if (opts.validUntilLedger !== undefined) {
|
|
125
117
|
ctx.validUntilLedger = opts.validUntilLedger;
|
|
@@ -1,15 +1,8 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type
|
|
3
|
-
import type { Network, PredicateNode } from '../../types.ts';
|
|
1
|
+
import type { ComposedRule } from '../../synth/compose-from-recording.ts';
|
|
2
|
+
import { type Network, type PredicateNode, type ProposedPolicy } from '../../types.ts';
|
|
4
3
|
/** [VERIFY] NOT a real deployed address. The interpreter is a per-network
|
|
5
4
|
* deploy artifact we do not have yet; install is a later phase. */
|
|
6
5
|
export declare const PLACEHOLDER_INTERPRETER_ADDRESS = "VERIFY-interpreter-address";
|
|
7
|
-
/** Wasm-level oracle defaults (mirrors the interpreter's `OracleParams`).
|
|
8
|
-
* Per-policy overrides may TIGHTEN only - they may never exceed these. */
|
|
9
|
-
export declare const ORACLE_DEFAULTS: {
|
|
10
|
-
readonly maxStalenessSeconds: 600;
|
|
11
|
-
readonly maxDeviationBps: 200;
|
|
12
|
-
};
|
|
13
6
|
export interface InterpreterAdapterConfig {
|
|
14
7
|
network: Network;
|
|
15
8
|
/** Per-rule install nonce (first install = 1; replay-protected). */
|
|
@@ -18,20 +11,21 @@ export interface InterpreterAdapterConfig {
|
|
|
18
11
|
* by the self-call gate: any `in`-allowlist containing this address is
|
|
19
12
|
* rejected as `SCOPE_SELF_CALL`. */
|
|
20
13
|
smartAccountAddress: string;
|
|
21
|
-
/** Optional per-policy oracle overrides. Must TIGHTEN vs ORACLE_DEFAULTS
|
|
22
|
-
* (i.e. <= each default). A widening value throws ORACLE_PARAMS_OUT_OF_RANGE
|
|
23
|
-
* at compile time. */
|
|
24
|
-
oracleParams?: {
|
|
25
|
-
maxStalenessSeconds?: number;
|
|
26
|
-
maxDeviationBps?: number;
|
|
27
|
-
};
|
|
28
14
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
15
|
+
/** The result of compiling a PolicyIR for this backend. */
|
|
16
|
+
export interface CompileResult {
|
|
17
|
+
/** false => some IR construct this backend cannot express (see `uncovered`). */
|
|
18
|
+
covered: boolean;
|
|
19
|
+
/** Human-readable list of unsupported constructs. */
|
|
20
|
+
uncovered: string[];
|
|
21
|
+
/** The installable policy, assembled when a rule lowered. */
|
|
22
|
+
proposed?: ProposedPolicy;
|
|
23
|
+
}
|
|
24
|
+
/** Lower a single rule to the canonical pre-encoding `PredicateNode`. The
|
|
25
|
+
* orchestrator re-derives the PredicateNode through this helper so it holds the
|
|
26
|
+
* SAME shape the encoder saw. Pure and deterministic: same `rule + config` ->
|
|
27
|
+
* byte-identical PredicateNode. The `uncovered` list is NOT re-derived -
|
|
28
|
+
* callers needing it must use `compileInterpreterPolicy(rule, config)`. */
|
|
29
|
+
export declare function lowerRuleToPredicate(rule: ComposedRule, config: InterpreterAdapterConfig): PredicateNode;
|
|
30
|
+
/** Compile a composed rule to an installable interpreter policy. */
|
|
31
|
+
export declare function compileInterpreterPolicy(rule: ComposedRule, config: InterpreterAdapterConfig): CompileResult;
|