@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.
Files changed (184) hide show
  1. package/README.md +4 -4
  2. package/dist/adapters/interpreter/adapter.d.ts +19 -25
  3. package/dist/adapters/interpreter/adapter.js +54 -487
  4. package/dist/adapters/interpreter/index.d.ts +1 -1
  5. package/dist/adapters/interpreter/index.js +1 -1
  6. package/dist/adapters/oz/adapter.js +1 -15
  7. package/dist/errors.d.ts +1 -1
  8. package/dist/index.d.ts +1 -4
  9. package/dist/index.js +1 -4
  10. package/dist/install/build-add-context-rule.d.ts +3 -19
  11. package/dist/install/build-add-context-rule.js +3 -98
  12. package/dist/install/build-install-policy.d.ts +4 -53
  13. package/dist/install/build-install-policy.js +44 -123
  14. package/dist/install/index.d.ts +0 -2
  15. package/dist/install/index.js +0 -7
  16. package/dist/ir/index.d.ts +1 -1
  17. package/dist/ir/types.d.ts +7 -76
  18. package/dist/ir/types.js +0 -2
  19. package/dist/predicate/decode.d.ts +1 -1
  20. package/dist/predicate/decode.js +2 -70
  21. package/dist/predicate/encode.js +45 -229
  22. package/dist/predicate/from-json.js +1 -42
  23. package/dist/record/decode.js +3 -8
  24. package/dist/review-card/builder.d.ts +4 -3
  25. package/dist/review-card/builder.js +18 -162
  26. package/dist/review-card/cross-check.js +2 -105
  27. package/dist/review-card/render-leaf.d.ts +4 -0
  28. package/dist/review-card/render-leaf.js +47 -0
  29. package/dist/run/index.d.ts +36 -139
  30. package/dist/run/index.js +125 -437
  31. package/dist/run/schemas.d.ts +168 -1929
  32. package/dist/run/schemas.js +40 -269
  33. package/dist/seams/types.d.ts +0 -2
  34. package/dist/simulate/deny-cases.d.ts +26 -0
  35. package/dist/simulate/deny-cases.js +282 -0
  36. package/dist/simulate/evaluate.d.ts +18 -0
  37. package/dist/simulate/evaluate.js +271 -0
  38. package/dist/simulate/index.d.ts +4 -0
  39. package/dist/simulate/index.js +8 -0
  40. package/dist/synth/compose-from-recording.d.ts +24 -51
  41. package/dist/synth/compose-from-recording.js +120 -282
  42. package/dist/synth/deny-cases.d.ts +5 -7
  43. package/dist/synth/deny-cases.js +23 -373
  44. package/dist/synth/evaluate.d.ts +0 -21
  45. package/dist/synth/evaluate.js +56 -336
  46. package/dist/synth/index.d.ts +0 -5
  47. package/dist/synth/index.js +0 -5
  48. package/dist/synth/permit-context.d.ts +2 -9
  49. package/dist/synth/permit-context.js +1 -82
  50. package/dist/synth/synthesize-from-recording.d.ts +1 -29
  51. package/dist/synth/synthesize-from-recording.js +41 -356
  52. package/dist/types.d.ts +8 -60
  53. package/dist/types.js +5 -2
  54. package/dist/verify/simulate.d.ts +1 -14
  55. package/dist/verify/simulate.js +2 -88
  56. package/dist/verify/verify.d.ts +1 -7
  57. package/dist/verify/verify.js +0 -8
  58. package/dist-cjs/adapters/interpreter/adapter.d.ts +19 -25
  59. package/dist-cjs/adapters/interpreter/adapter.js +56 -489
  60. package/dist-cjs/adapters/interpreter/index.d.ts +1 -1
  61. package/dist-cjs/adapters/interpreter/index.js +1 -2
  62. package/dist-cjs/adapters/oz/adapter.js +1 -15
  63. package/dist-cjs/errors.d.ts +1 -1
  64. package/dist-cjs/index.d.ts +1 -4
  65. package/dist-cjs/index.js +1 -4
  66. package/dist-cjs/install/build-add-context-rule.d.ts +3 -19
  67. package/dist-cjs/install/build-add-context-rule.js +1 -97
  68. package/dist-cjs/install/build-install-policy.d.ts +4 -53
  69. package/dist-cjs/install/build-install-policy.js +44 -122
  70. package/dist-cjs/install/index.d.ts +0 -2
  71. package/dist-cjs/install/index.js +2 -23
  72. package/dist-cjs/ir/index.d.ts +1 -1
  73. package/dist-cjs/ir/types.d.ts +7 -76
  74. package/dist-cjs/ir/types.js +0 -2
  75. package/dist-cjs/predicate/decode.d.ts +1 -1
  76. package/dist-cjs/predicate/decode.js +2 -70
  77. package/dist-cjs/predicate/encode.js +45 -229
  78. package/dist-cjs/predicate/from-json.js +1 -42
  79. package/dist-cjs/record/decode.js +3 -8
  80. package/dist-cjs/review-card/builder.d.ts +4 -3
  81. package/dist-cjs/review-card/builder.js +23 -167
  82. package/dist-cjs/review-card/cross-check.js +7 -110
  83. package/dist-cjs/review-card/render-leaf.d.ts +4 -0
  84. package/dist-cjs/review-card/render-leaf.js +52 -0
  85. package/dist-cjs/run/index.d.ts +36 -139
  86. package/dist-cjs/run/index.js +125 -444
  87. package/dist-cjs/run/schemas.d.ts +168 -1929
  88. package/dist-cjs/run/schemas.js +41 -270
  89. package/dist-cjs/seams/types.d.ts +0 -2
  90. package/dist-cjs/simulate/deny-cases.d.ts +26 -0
  91. package/dist-cjs/simulate/deny-cases.js +286 -0
  92. package/dist-cjs/simulate/evaluate.d.ts +18 -0
  93. package/dist-cjs/simulate/evaluate.js +274 -0
  94. package/dist-cjs/simulate/index.d.ts +4 -0
  95. package/dist-cjs/simulate/index.js +13 -0
  96. package/dist-cjs/synth/compose-from-recording.d.ts +24 -51
  97. package/dist-cjs/synth/compose-from-recording.js +120 -282
  98. package/dist-cjs/synth/deny-cases.d.ts +5 -7
  99. package/dist-cjs/synth/deny-cases.js +23 -374
  100. package/dist-cjs/synth/evaluate.d.ts +0 -21
  101. package/dist-cjs/synth/evaluate.js +57 -337
  102. package/dist-cjs/synth/index.d.ts +0 -5
  103. package/dist-cjs/synth/index.js +1 -11
  104. package/dist-cjs/synth/permit-context.d.ts +2 -9
  105. package/dist-cjs/synth/permit-context.js +1 -82
  106. package/dist-cjs/synth/synthesize-from-recording.d.ts +1 -29
  107. package/dist-cjs/synth/synthesize-from-recording.js +39 -354
  108. package/dist-cjs/types.d.ts +8 -60
  109. package/dist-cjs/types.js +6 -3
  110. package/dist-cjs/verify/simulate.d.ts +1 -14
  111. package/dist-cjs/verify/simulate.js +2 -88
  112. package/dist-cjs/verify/verify.d.ts +1 -7
  113. package/dist-cjs/verify/verify.js +0 -8
  114. package/package.json +4 -4
  115. package/src/adapters/interpreter/adapter.ts +76 -572
  116. package/src/errors.ts +0 -19
  117. package/src/index.ts +1 -4
  118. package/src/install/build-add-context-rule.ts +5 -139
  119. package/src/install/build-install-policy.ts +93 -214
  120. package/src/install/index.ts +0 -34
  121. package/src/predicate/decode.ts +2 -70
  122. package/src/predicate/encode.ts +49 -261
  123. package/src/predicate/from-json.ts +1 -43
  124. package/src/record/decode.ts +3 -8
  125. package/src/review-card/builder.ts +19 -168
  126. package/src/review-card/cross-check.ts +3 -105
  127. package/src/review-card/render-leaf.ts +48 -0
  128. package/src/run/index.ts +144 -572
  129. package/src/run/schemas.ts +42 -291
  130. package/src/simulate/deny-cases.ts +334 -0
  131. package/src/simulate/evaluate.ts +284 -0
  132. package/src/simulate/index.ts +11 -0
  133. package/src/synth/compose-from-recording.ts +148 -347
  134. package/src/synth/index.ts +0 -13
  135. package/src/synth/synthesize-from-recording.ts +43 -456
  136. package/src/types.ts +13 -49
  137. package/dist/install/authority-overlap.d.ts +0 -134
  138. package/dist/install/authority-overlap.js +0 -0
  139. package/dist/install/build-install-predicate.d.ts +0 -96
  140. package/dist/install/build-install-predicate.js +0 -444
  141. package/dist/install/build-merge-policy.d.ts +0 -70
  142. package/dist/install/build-merge-policy.js +0 -130
  143. package/dist/install/plan-merge-policy.d.ts +0 -49
  144. package/dist/install/plan-merge-policy.js +0 -86
  145. package/dist/install/read-account-rules.d.ts +0 -100
  146. package/dist/install/read-account-rules.js +0 -283
  147. package/dist-cjs/install/authority-overlap.d.ts +0 -134
  148. package/dist-cjs/install/authority-overlap.js +0 -0
  149. package/dist-cjs/install/build-install-predicate.d.ts +0 -96
  150. package/dist-cjs/install/build-install-predicate.js +0 -479
  151. package/dist-cjs/install/build-merge-policy.d.ts +0 -70
  152. package/dist-cjs/install/build-merge-policy.js +0 -134
  153. package/dist-cjs/install/plan-merge-policy.d.ts +0 -49
  154. package/dist-cjs/install/plan-merge-policy.js +0 -90
  155. package/dist-cjs/install/read-account-rules.d.ts +0 -100
  156. package/dist-cjs/install/read-account-rules.js +0 -296
  157. package/src/adapters/interpreter/index.ts +0 -8
  158. package/src/adapters/oz/adapter.ts +0 -376
  159. package/src/adapters/oz/index.ts +0 -9
  160. package/src/codegen/compile-gate.ts +0 -167
  161. package/src/codegen/index.ts +0 -17
  162. package/src/codegen/template.ts +0 -165
  163. package/src/install/authority-overlap.ts +0 -0
  164. package/src/install/build-merge-policy.ts +0 -219
  165. package/src/install/plan-merge-policy.ts +0 -133
  166. package/src/install/read-account-rules.ts +0 -376
  167. package/src/ir/index.ts +0 -13
  168. package/src/ir/types.ts +0 -132
  169. package/src/mandate/index.ts +0 -4
  170. package/src/mandate/to-ir.ts +0 -71
  171. package/src/mandate/types.ts +0 -21
  172. package/src/seams/index.ts +0 -11
  173. package/src/seams/types.ts +0 -81
  174. package/src/synth/deny-cases.ts +0 -663
  175. package/src/synth/evaluate.ts +0 -613
  176. package/src/synth/harness.ts +0 -68
  177. package/src/synth/minimize.ts +0 -48
  178. package/src/synth/permit-context.ts +0 -136
  179. package/src/synth/predicate-literals.ts +0 -27
  180. package/src/synth/synthesize-from-mandate.ts +0 -82
  181. package/src/verify/envelope.ts +0 -28
  182. package/src/verify/index.ts +0 -5
  183. package/src/verify/simulate.ts +0 -311
  184. package/src/verify/verify.ts +0 -243
@@ -10,7 +10,7 @@
10
10
  Object.defineProperty(exports, "__esModule", { value: true });
11
11
  exports.buildPermitContext = buildPermitContext;
12
12
  const deny_cases_ts_1 = require("./deny-cases.js");
13
- function buildPermitContext(tx, responses, predicate) {
13
+ function buildPermitContext(tx, responses) {
14
14
  const amountByToken = {};
15
15
  const totals = new Map();
16
16
  for (const m of tx.tokenMovements) {
@@ -22,23 +22,6 @@ function buildPermitContext(tx, responses, predicate) {
22
22
  }
23
23
  const topLevel = tx.invocations[0];
24
24
  const scopeContract = topLevel?.contract ?? '';
25
- const oraclePriceByAsset = {};
26
- visitOracleLeaves(predicate, (asset, op, bound) => {
27
- let price;
28
- switch (op) {
29
- case 'lt':
30
- price = bound - 1n;
31
- break;
32
- case 'gt':
33
- price = bound + 1n;
34
- break;
35
- default:
36
- price = bound;
37
- }
38
- if (price < 0n)
39
- price = 0n;
40
- oraclePriceByAsset[asset] = { price: price.toString(), timestampSeconds: tx.fetchedAt };
41
- });
42
25
  const ctx = {
43
26
  contract: scopeContract,
44
27
  fn: topLevel?.fn ?? '',
@@ -47,73 +30,9 @@ function buildPermitContext(tx, responses, predicate) {
47
30
  nowSeconds: tx.fetchedAt,
48
31
  amountByToken,
49
32
  windowSpentByToken: {},
50
- invocationCountByWindow: {},
51
- oraclePriceByAsset,
52
33
  };
53
34
  if (responses.validUntilLedger !== undefined) {
54
35
  ctx.validUntilLedger = responses.validUntilLedger;
55
36
  }
56
37
  return ctx;
57
38
  }
58
- function visitOracleLeaves(node, visit) {
59
- switch (node.op) {
60
- case 'and':
61
- case 'or':
62
- for (const child of node.children)
63
- visitOracleLeaves(child, visit);
64
- return;
65
- case 'not':
66
- visitOracleLeaves(node.child, visit);
67
- return;
68
- case 'eq':
69
- case 'lt':
70
- case 'lte':
71
- case 'gt':
72
- case 'gte': {
73
- const leftIsOracle = node.left.kind === 'oracle_price';
74
- const rightIsOracle = node.right.kind === 'oracle_price';
75
- let asset;
76
- let literal;
77
- if (leftIsOracle) {
78
- asset = node.left.kind === 'oracle_price' ? node.left.asset : undefined;
79
- literal = oracleThresholdNormalised(node.right);
80
- }
81
- else if (rightIsOracle) {
82
- asset = node.right.kind === 'oracle_price' ? node.right.asset : undefined;
83
- literal = oracleThresholdNormalised(node.left);
84
- }
85
- if (asset !== undefined && literal !== undefined) {
86
- visit(asset, node.op, literal);
87
- }
88
- return;
89
- }
90
- case 'in':
91
- return;
92
- }
93
- }
94
- /** Oracle prices normalise to 9 decimals; mirrors NORMALISED_DECIMALS in
95
- * oracle.rs. */
96
- const NORMALISED_DECIMALS = 9n;
97
- /** A threshold restated on the normalised basis, so a derived permit price is
98
- * comparable to it. Thresholds carry their own basis, so the conversion has
99
- * to happen here - reading the digits raw would build a context off by a
100
- * factor of 10^(decimals-9). Returns undefined for any other leaf. */
101
- function oracleThresholdNormalised(leaf) {
102
- if (leaf.kind !== 'oracle_threshold')
103
- return undefined;
104
- let value;
105
- try {
106
- value = BigInt(leaf.value);
107
- }
108
- catch {
109
- return undefined;
110
- }
111
- const decimals = BigInt(leaf.decimals);
112
- if (decimals <= NORMALISED_DECIMALS) {
113
- return value * 10n ** (NORMALISED_DECIMALS - decimals);
114
- }
115
- // Floor: a finer-grained threshold has no exact 9-dp representation. The
116
- // caller only needs a price that lands on the right side of the bound, and
117
- // it offsets by one from here.
118
- return value / 10n ** (decimals - NORMALISED_DECIMALS);
119
- }
@@ -1,7 +1,5 @@
1
- import type { OzAdapterConfig } from '../adapters/oz/adapter.ts';
2
1
  import type { ToolError, ToolResponse } from '../errors.ts';
3
2
  import { type Network, type PredicateNode, type ProposedPolicy, type RecordedTransaction } from '../types.ts';
4
- import type { SimulationResult } from '../verify/envelope.ts';
5
3
  import { type ComposeUserResponses } from './compose-from-recording.ts';
6
4
  /** Per-policy interpreter adapter config the caller opts into. Absent ->
7
5
  * recording path runs in week-1 mode (OZ adapter only, warnings for the
@@ -13,13 +11,6 @@ export interface InterpreterAdapterOptions {
13
11
  smartAccountAddress: string;
14
12
  /** Per-rule install nonce (first install = 1). Defaults to 1. */
15
13
  installNonce?: number;
16
- /** Per-policy oracle overrides. Tighten-only vs the wasm defaults
17
- * (maxStalenessSeconds <= 600, maxDeviationBps <= 200); widening is
18
- * rejected at the options boundary. */
19
- oracleParams?: {
20
- maxStalenessSeconds?: number;
21
- maxDeviationBps?: number;
22
- };
23
14
  }
24
15
  /** PRIVATE test-only extension of `InterpreterAdapterOptions` for the
25
16
  * `__testPredicateNode` seam. Exported under a `__` prefix so production
@@ -43,7 +34,7 @@ export interface SynthesizeFromRecordingOptions {
43
34
  * never altered by enabling explain. Absent -> success envelope is byte-identical. */
44
35
  explain?: true;
45
36
  }
46
- export declare function synthesizeFromRecording(tx: RecordedTransaction, opts: SynthesizeFromRecordingOptions, ozConfig: OzAdapterConfig): ToolResponse<ProposedPolicy> & {
37
+ export declare function synthesizeFromRecording(tx: RecordedTransaction, opts: SynthesizeFromRecordingOptions): ToolResponse<ProposedPolicy> & {
47
38
  /** Present iff `opts.explain === true` and the synthesis succeeded. The
48
39
  * `predicateTree` is the exact in-memory `PredicateNode` (canonical
49
40
  * JSON shape) that was encoded into `proposed.policyDocuments[*].encodedPredicate`
@@ -56,28 +47,9 @@ export declare function synthesizeFromRecording(tx: RecordedTransaction, opts: S
56
47
  * builds a minimal honest SimulationResult downstream. */
57
48
  explain?: {
58
49
  predicateTree: PredicateNode | null;
59
- simulation: SimulationResult;
60
50
  };
61
51
  };
62
52
  /** ToolError-shaped error helper used by the body to surface a structured
63
53
  * failure that the envelope converts to `{ok:false, error}`. */
64
54
  declare function throwToolError(code: ToolError['code'], message: string): never;
65
55
  export { throwToolError };
66
- /** Diagnostic reported by `visitOracleLeaves` when an oracle comparison
67
- * cannot be normalised to oracle-on-left. Currently only "oracle-on-right
68
- * with a non-literal LHS" - the other branches already produce a parseable
69
- * threshold and are returned to the caller. */
70
- export interface OracleNormalisationWarning {
71
- /** The dimension name (currently always "oracle_normalisation_dropped"). */
72
- dimension: 'oracle_normalisation_dropped';
73
- /** The operator as written. */
74
- op: 'eq' | 'lt' | 'lte' | 'gt' | 'gte';
75
- /** The asset the oracle leaf is bound to. */
76
- asset: string;
77
- /** The non-literal RHS (oracle-on-left) or LHS (oracle-on-right) leaf
78
- * whose shape stopped the normalisation. Surfaced for diagnostics;
79
- * intentionally a partial view - the full leaf is the caller's job. */
80
- otherKind: string;
81
- /** Human-readable message (matches the warn-on-drop text). */
82
- message: string;
83
- }