@crediolabs/policy-synth 0.2.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.
Files changed (249) 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/errors.d.ts +1 -1
  5. package/dist/index.d.ts +1 -4
  6. package/dist/index.js +1 -4
  7. package/dist/install/build-add-context-rule.d.ts +3 -19
  8. package/dist/install/build-add-context-rule.js +3 -98
  9. package/dist/install/build-install-policy.d.ts +4 -53
  10. package/dist/install/build-install-policy.js +44 -123
  11. package/dist/install/index.d.ts +0 -2
  12. package/dist/install/index.js +0 -7
  13. package/dist/predicate/decode.d.ts +1 -1
  14. package/dist/predicate/decode.js +2 -70
  15. package/dist/predicate/encode.js +45 -229
  16. package/dist/predicate/from-json.js +1 -42
  17. package/dist/record/decode.js +3 -8
  18. package/dist/record/freshness.d.ts +17 -11
  19. package/dist/record/freshness.js +28 -18
  20. package/dist/review-card/builder.d.ts +4 -3
  21. package/dist/review-card/builder.js +18 -162
  22. package/dist/review-card/cross-check.js +2 -105
  23. package/dist/review-card/render-leaf.d.ts +4 -0
  24. package/dist/review-card/render-leaf.js +47 -0
  25. package/dist/run/index.d.ts +36 -139
  26. package/dist/run/index.js +125 -437
  27. package/dist/run/schemas.d.ts +168 -1929
  28. package/dist/run/schemas.js +40 -269
  29. package/dist/{synth → simulate}/deny-cases.d.ts +4 -8
  30. package/dist/simulate/deny-cases.js +282 -0
  31. package/dist/simulate/evaluate.d.ts +18 -0
  32. package/dist/simulate/evaluate.js +271 -0
  33. package/dist/simulate/index.d.ts +4 -0
  34. package/dist/simulate/index.js +8 -0
  35. package/dist/synth/compose-from-recording.d.ts +24 -51
  36. package/dist/synth/compose-from-recording.js +120 -282
  37. package/dist/synth/index.d.ts +0 -5
  38. package/dist/synth/index.js +0 -5
  39. package/dist/synth/synthesize-from-recording.d.ts +1 -29
  40. package/dist/synth/synthesize-from-recording.js +41 -356
  41. package/dist/types.d.ts +8 -60
  42. package/dist/types.js +5 -2
  43. package/dist-cjs/adapters/interpreter/adapter.d.ts +19 -25
  44. package/dist-cjs/adapters/interpreter/adapter.js +56 -489
  45. package/dist-cjs/errors.d.ts +1 -1
  46. package/dist-cjs/index.d.ts +1 -4
  47. package/dist-cjs/index.js +1 -4
  48. package/dist-cjs/install/build-add-context-rule.d.ts +3 -19
  49. package/dist-cjs/install/build-add-context-rule.js +1 -97
  50. package/dist-cjs/install/build-install-policy.d.ts +4 -53
  51. package/dist-cjs/install/build-install-policy.js +44 -122
  52. package/dist-cjs/install/index.d.ts +0 -2
  53. package/dist-cjs/install/index.js +2 -23
  54. package/dist-cjs/predicate/decode.d.ts +1 -1
  55. package/dist-cjs/predicate/decode.js +2 -70
  56. package/dist-cjs/predicate/encode.js +45 -229
  57. package/dist-cjs/predicate/from-json.js +1 -42
  58. package/dist-cjs/record/decode.js +3 -8
  59. package/dist-cjs/record/freshness.d.ts +17 -11
  60. package/dist-cjs/record/freshness.js +28 -18
  61. package/dist-cjs/review-card/builder.d.ts +4 -3
  62. package/dist-cjs/review-card/builder.js +23 -167
  63. package/dist-cjs/review-card/cross-check.js +7 -110
  64. package/dist-cjs/review-card/render-leaf.d.ts +4 -0
  65. package/dist-cjs/review-card/render-leaf.js +52 -0
  66. package/dist-cjs/run/index.d.ts +36 -139
  67. package/dist-cjs/run/index.js +125 -444
  68. package/dist-cjs/run/schemas.d.ts +168 -1929
  69. package/dist-cjs/run/schemas.js +41 -270
  70. package/dist-cjs/{synth → simulate}/deny-cases.d.ts +4 -8
  71. package/dist-cjs/simulate/deny-cases.js +286 -0
  72. package/dist-cjs/simulate/evaluate.d.ts +18 -0
  73. package/dist-cjs/simulate/evaluate.js +274 -0
  74. package/dist-cjs/simulate/index.d.ts +4 -0
  75. package/dist-cjs/simulate/index.js +13 -0
  76. package/dist-cjs/synth/compose-from-recording.d.ts +24 -51
  77. package/dist-cjs/synth/compose-from-recording.js +120 -282
  78. package/dist-cjs/synth/index.d.ts +0 -5
  79. package/dist-cjs/synth/index.js +1 -11
  80. package/dist-cjs/synth/synthesize-from-recording.d.ts +1 -29
  81. package/dist-cjs/synth/synthesize-from-recording.js +39 -354
  82. package/dist-cjs/types.d.ts +8 -60
  83. package/dist-cjs/types.js +6 -3
  84. package/package.json +5 -5
  85. package/src/adapters/interpreter/adapter.ts +76 -572
  86. package/src/errors.ts +0 -19
  87. package/src/index.ts +1 -4
  88. package/src/install/build-add-context-rule.ts +5 -139
  89. package/src/install/build-install-policy.ts +93 -214
  90. package/src/install/index.ts +0 -34
  91. package/src/predicate/decode.ts +2 -70
  92. package/src/predicate/encode.ts +49 -261
  93. package/src/predicate/from-json.ts +1 -43
  94. package/src/record/decode.ts +3 -8
  95. package/src/record/freshness.ts +29 -18
  96. package/src/review-card/builder.ts +19 -168
  97. package/src/review-card/cross-check.ts +3 -105
  98. package/src/review-card/render-leaf.ts +48 -0
  99. package/src/run/index.ts +144 -572
  100. package/src/run/schemas.ts +42 -291
  101. package/src/simulate/deny-cases.ts +334 -0
  102. package/src/simulate/evaluate.ts +284 -0
  103. package/src/simulate/index.ts +11 -0
  104. package/src/synth/compose-from-recording.ts +148 -347
  105. package/src/synth/index.ts +0 -13
  106. package/src/synth/synthesize-from-recording.ts +43 -456
  107. package/src/types.ts +13 -49
  108. package/dist/adapters/interpreter/index.d.ts +0 -1
  109. package/dist/adapters/interpreter/index.js +0 -2
  110. package/dist/adapters/oz/adapter.d.ts +0 -20
  111. package/dist/adapters/oz/adapter.js +0 -295
  112. package/dist/adapters/oz/index.d.ts +0 -1
  113. package/dist/adapters/oz/index.js +0 -2
  114. package/dist/codegen/compile-gate.d.ts +0 -33
  115. package/dist/codegen/compile-gate.js +0 -124
  116. package/dist/codegen/index.d.ts +0 -2
  117. package/dist/codegen/index.js +0 -8
  118. package/dist/codegen/template.d.ts +0 -18
  119. package/dist/codegen/template.js +0 -148
  120. package/dist/install/authority-overlap.d.ts +0 -134
  121. package/dist/install/authority-overlap.js +0 -0
  122. package/dist/install/build-install-predicate.d.ts +0 -96
  123. package/dist/install/build-install-predicate.js +0 -444
  124. package/dist/install/build-merge-policy.d.ts +0 -70
  125. package/dist/install/build-merge-policy.js +0 -130
  126. package/dist/install/plan-merge-policy.d.ts +0 -49
  127. package/dist/install/plan-merge-policy.js +0 -86
  128. package/dist/install/read-account-rules.d.ts +0 -100
  129. package/dist/install/read-account-rules.js +0 -283
  130. package/dist/ir/index.d.ts +0 -1
  131. package/dist/ir/index.js +0 -2
  132. package/dist/ir/types.d.ts +0 -140
  133. package/dist/ir/types.js +0 -11
  134. package/dist/mandate/index.d.ts +0 -2
  135. package/dist/mandate/index.js +0 -2
  136. package/dist/mandate/to-ir.d.ts +0 -3
  137. package/dist/mandate/to-ir.js +0 -60
  138. package/dist/mandate/types.d.ts +0 -20
  139. package/dist/mandate/types.js +0 -8
  140. package/dist/seams/index.d.ts +0 -1
  141. package/dist/seams/index.js +0 -2
  142. package/dist/seams/types.d.ts +0 -66
  143. package/dist/seams/types.js +0 -11
  144. package/dist/synth/deny-cases.js +0 -562
  145. package/dist/synth/evaluate.d.ts +0 -39
  146. package/dist/synth/evaluate.js +0 -551
  147. package/dist/synth/harness.d.ts +0 -28
  148. package/dist/synth/harness.js +0 -47
  149. package/dist/synth/minimize.d.ts +0 -4
  150. package/dist/synth/minimize.js +0 -38
  151. package/dist/synth/permit-context.d.ts +0 -15
  152. package/dist/synth/permit-context.js +0 -116
  153. package/dist/synth/predicate-literals.d.ts +0 -5
  154. package/dist/synth/predicate-literals.js +0 -25
  155. package/dist/synth/synthesize-from-mandate.d.ts +0 -20
  156. package/dist/synth/synthesize-from-mandate.js +0 -59
  157. package/dist/verify/envelope.d.ts +0 -15
  158. package/dist/verify/envelope.js +0 -22
  159. package/dist/verify/index.d.ts +0 -3
  160. package/dist/verify/index.js +0 -3
  161. package/dist/verify/simulate.d.ts +0 -31
  162. package/dist/verify/simulate.js +0 -258
  163. package/dist/verify/verify.d.ts +0 -21
  164. package/dist/verify/verify.js +0 -189
  165. package/dist-cjs/adapters/interpreter/index.d.ts +0 -1
  166. package/dist-cjs/adapters/interpreter/index.js +0 -8
  167. package/dist-cjs/adapters/oz/adapter.d.ts +0 -20
  168. package/dist-cjs/adapters/oz/adapter.js +0 -300
  169. package/dist-cjs/adapters/oz/index.d.ts +0 -1
  170. package/dist-cjs/adapters/oz/index.js +0 -8
  171. package/dist-cjs/codegen/compile-gate.d.ts +0 -33
  172. package/dist-cjs/codegen/compile-gate.js +0 -128
  173. package/dist-cjs/codegen/index.d.ts +0 -2
  174. package/dist-cjs/codegen/index.js +0 -14
  175. package/dist-cjs/codegen/template.d.ts +0 -18
  176. package/dist-cjs/codegen/template.js +0 -151
  177. package/dist-cjs/install/authority-overlap.d.ts +0 -134
  178. package/dist-cjs/install/authority-overlap.js +0 -0
  179. package/dist-cjs/install/build-install-predicate.d.ts +0 -96
  180. package/dist-cjs/install/build-install-predicate.js +0 -479
  181. package/dist-cjs/install/build-merge-policy.d.ts +0 -70
  182. package/dist-cjs/install/build-merge-policy.js +0 -134
  183. package/dist-cjs/install/plan-merge-policy.d.ts +0 -49
  184. package/dist-cjs/install/plan-merge-policy.js +0 -90
  185. package/dist-cjs/install/read-account-rules.d.ts +0 -100
  186. package/dist-cjs/install/read-account-rules.js +0 -296
  187. package/dist-cjs/ir/index.d.ts +0 -1
  188. package/dist-cjs/ir/index.js +0 -3
  189. package/dist-cjs/ir/types.d.ts +0 -140
  190. package/dist-cjs/ir/types.js +0 -12
  191. package/dist-cjs/mandate/index.d.ts +0 -2
  192. package/dist-cjs/mandate/index.js +0 -6
  193. package/dist-cjs/mandate/to-ir.d.ts +0 -3
  194. package/dist-cjs/mandate/to-ir.js +0 -63
  195. package/dist-cjs/mandate/types.d.ts +0 -20
  196. package/dist-cjs/mandate/types.js +0 -9
  197. package/dist-cjs/seams/index.d.ts +0 -1
  198. package/dist-cjs/seams/index.js +0 -3
  199. package/dist-cjs/seams/types.d.ts +0 -66
  200. package/dist-cjs/seams/types.js +0 -12
  201. package/dist-cjs/synth/deny-cases.js +0 -568
  202. package/dist-cjs/synth/evaluate.d.ts +0 -39
  203. package/dist-cjs/synth/evaluate.js +0 -554
  204. package/dist-cjs/synth/harness.d.ts +0 -28
  205. package/dist-cjs/synth/harness.js +0 -50
  206. package/dist-cjs/synth/minimize.d.ts +0 -4
  207. package/dist-cjs/synth/minimize.js +0 -41
  208. package/dist-cjs/synth/permit-context.d.ts +0 -15
  209. package/dist-cjs/synth/permit-context.js +0 -119
  210. package/dist-cjs/synth/predicate-literals.d.ts +0 -5
  211. package/dist-cjs/synth/predicate-literals.js +0 -28
  212. package/dist-cjs/synth/synthesize-from-mandate.d.ts +0 -20
  213. package/dist-cjs/synth/synthesize-from-mandate.js +0 -62
  214. package/dist-cjs/verify/envelope.d.ts +0 -15
  215. package/dist-cjs/verify/envelope.js +0 -23
  216. package/dist-cjs/verify/index.d.ts +0 -3
  217. package/dist-cjs/verify/index.js +0 -8
  218. package/dist-cjs/verify/simulate.d.ts +0 -31
  219. package/dist-cjs/verify/simulate.js +0 -261
  220. package/dist-cjs/verify/verify.d.ts +0 -21
  221. package/dist-cjs/verify/verify.js +0 -192
  222. package/src/adapters/interpreter/index.ts +0 -8
  223. package/src/adapters/oz/adapter.ts +0 -376
  224. package/src/adapters/oz/index.ts +0 -9
  225. package/src/codegen/compile-gate.ts +0 -167
  226. package/src/codegen/index.ts +0 -17
  227. package/src/codegen/template.ts +0 -165
  228. package/src/install/authority-overlap.ts +0 -0
  229. package/src/install/build-merge-policy.ts +0 -219
  230. package/src/install/plan-merge-policy.ts +0 -133
  231. package/src/install/read-account-rules.ts +0 -376
  232. package/src/ir/index.ts +0 -13
  233. package/src/ir/types.ts +0 -132
  234. package/src/mandate/index.ts +0 -4
  235. package/src/mandate/to-ir.ts +0 -71
  236. package/src/mandate/types.ts +0 -21
  237. package/src/seams/index.ts +0 -11
  238. package/src/seams/types.ts +0 -81
  239. package/src/synth/deny-cases.ts +0 -663
  240. package/src/synth/evaluate.ts +0 -613
  241. package/src/synth/harness.ts +0 -68
  242. package/src/synth/minimize.ts +0 -48
  243. package/src/synth/permit-context.ts +0 -136
  244. package/src/synth/predicate-literals.ts +0 -27
  245. package/src/synth/synthesize-from-mandate.ts +0 -82
  246. package/src/verify/envelope.ts +0 -28
  247. package/src/verify/index.ts +0 -5
  248. package/src/verify/simulate.ts +0 -311
  249. package/src/verify/verify.ts +0 -243
@@ -1,2 +0,0 @@
1
- export { mandateToPolicyIR } from './to-ir.ts';
2
- export type { MandateSpec } from './types.ts';
@@ -1,2 +0,0 @@
1
- // src/mandate/index.ts - re-export the Mandate source.
2
- export { mandateToPolicyIR } from "./to-ir.js";
@@ -1,3 +0,0 @@
1
- import type { PolicyIR } from '../ir/types.ts';
2
- import type { MandateSpec } from './types.ts';
3
- export declare function mandateToPolicyIR(spec: MandateSpec): PolicyIR;
@@ -1,60 +0,0 @@
1
- // src/mandate/to-ir.ts - deterministic lowering of a MandateSpec to a PolicyIR.
2
- //
3
- // `mandateToPolicyIR` is pure and total: the same spec always lowers to a
4
- // byte-identical PolicyIR. No decoding, no inference, no clock. Each mandate
5
- // field maps to exactly one IR construct:
6
- // contract/method -> rule.scope
7
- // spendingLimit -> a `window_spent(token,window) <= limit` compare
8
- // approvalThreshold-> rule.approval.threshold
9
- // recipients -> an `in` condition on the recipient arg (flagged as not
10
- // covered by the OZ adapter; that is expected)
11
- // expiry -> rule.expiry
12
- // The top-level default is `deny_all` (OZ context rules are deny-by-default).
13
- /** Arg index the recipient allowlist constrains. Pinned to the SEP-41
14
- * `transfer(from, to, amount)` convention where `to` is arg 1. This condition
15
- * is flagged as not covered by the OZ adapter (no built-in primitive expresses
16
- * an arg allowlist), so the index only needs to be deterministic in week-1. */
17
- const RECIPIENT_ARG_INDEX = 1;
18
- export function mandateToPolicyIR(spec) {
19
- const scope = { contract: spec.contract };
20
- if (spec.method !== undefined)
21
- scope.method = spec.method;
22
- const constraints = [];
23
- if (spec.spendingLimit) {
24
- constraints.push({
25
- op: 'compare',
26
- compare: {
27
- selector: {
28
- kind: 'window_spent',
29
- token: spec.spendingLimit.token,
30
- windowSeconds: spec.spendingLimit.windowSeconds,
31
- },
32
- operator: 'lte',
33
- value: spec.spendingLimit.limit,
34
- },
35
- });
36
- }
37
- if (spec.recipients && spec.recipients.length > 0) {
38
- const selector = {
39
- kind: 'arg',
40
- argIndex: RECIPIENT_ARG_INDEX,
41
- scalarType: 'address',
42
- };
43
- constraints.push({ op: 'in', selector, values: [...spec.recipients] });
44
- }
45
- const rule = { roles: [], scope, constraints };
46
- if (spec.approvalThreshold !== undefined) {
47
- rule.approval = { kind: 'threshold', threshold: spec.approvalThreshold };
48
- }
49
- if (spec.expiry) {
50
- const expiry = {};
51
- if (spec.expiry.validUntilLedger !== undefined) {
52
- expiry.validUntilLedger = spec.expiry.validUntilLedger;
53
- }
54
- if (spec.expiry.validUntilUnixSeconds !== undefined) {
55
- expiry.validUntilUnixSeconds = spec.expiry.validUntilUnixSeconds;
56
- }
57
- rule.expiry = expiry;
58
- }
59
- return { chain: spec.chain, defaultBehavior: 'deny_all', rules: [rule] };
60
- }
@@ -1,20 +0,0 @@
1
- export interface MandateSpec {
2
- chain: 'stellar';
3
- contract: string;
4
- method?: string;
5
- /** -> OZ `spending_limit` primitive. `limit` is an i128 decimal string. */
6
- spendingLimit?: {
7
- token: string;
8
- limit: string;
9
- windowSeconds: number;
10
- };
11
- /** -> OZ `simple_threshold` / `weighted_threshold` primitive. */
12
- approvalThreshold?: number;
13
- /** Recipient allowlist -> an `in` arg condition (not covered by an OZ built-in). */
14
- recipients?: string[];
15
- /** -> OZ context-rule expiry. */
16
- expiry?: {
17
- validUntilLedger?: number;
18
- validUntilUnixSeconds?: number;
19
- };
20
- }
@@ -1,8 +0,0 @@
1
- // src/mandate/types.ts - the deterministic Mandate source.
2
- //
3
- // A MandateSpec is a declarative policy statement: "this account may call this
4
- // contract, spending at most L per window, requiring M approvals, to these
5
- // recipients, until this expiry". It carries NO transaction to decode, NO
6
- // parseConfidence, and NO inference - it lowers deterministically to a
7
- // PolicyIR. It is the clean end-to-end demo path, co-equal with the recorder.
8
- export {};
@@ -1 +0,0 @@
1
- export type { ChainDecoder, CompileResult, CustodyAdapter, CustodyCapabilities, CustodyMode, PolicySource, SimulationResult, } from './types.ts';
@@ -1,2 +0,0 @@
1
- // src/seams/index.ts - re-export the three custody-seam contracts.
2
- export {};
@@ -1,66 +0,0 @@
1
- import type { ToolResponse } from '../errors.ts';
2
- import type { PolicyIR } from '../ir/types.ts';
3
- import type { Network, ProposedPolicy, RecordedTransaction, ScVal } from '../types.ts';
4
- /** INPUT: something that produces a normalized RecordedTransaction. */
5
- export interface PolicySource {
6
- readonly name: string;
7
- capture(input: {
8
- hash?: string;
9
- xdr?: string;
10
- network: Network;
11
- }): Promise<ToolResponse<RecordedTransaction>>;
12
- }
13
- /** DECODE: chain-specific call data -> the normalized facts the synth reasons
14
- * over. */
15
- export interface ChainDecoder {
16
- readonly chain: 'stellar' | 'evm';
17
- decodeInvocation(tx: RecordedTransaction): {
18
- contract: string;
19
- method: string;
20
- args: ScVal[];
21
- };
22
- }
23
- /** OZ = enforce; `log_only` is reserved for shadow mode (out of scope). */
24
- export type CustodyMode = 'enforce' | 'log_only';
25
- /** What a backend can express. A construct needing a false flag is flagged
26
- * `uncovered` by the adapter, never silently dropped. */
27
- export interface CustodyCapabilities {
28
- supportsOraclePrice: boolean;
29
- supportsSpendWindow: boolean;
30
- supportsInvocationCount: boolean;
31
- supportsTimeExpiry: boolean;
32
- supportsThreshold: boolean;
33
- supportsGeneralPredicate: boolean;
34
- }
35
- /** The result of compiling a PolicyIR for one backend. */
36
- export interface CompileResult {
37
- /** false => some IR construct this backend cannot express (see `uncovered`). */
38
- covered: boolean;
39
- /** Human-readable list of unsupported constructs. */
40
- uncovered: string[];
41
- /** The backend-native installable policy, assembled when a rule lowered. */
42
- proposed?: ProposedPolicy;
43
- }
44
- /** Result of a simulate() dry-run. `ts-model` is the off-chain TS evaluator.
45
- * Real permit/deny semantics wiring is a later phase; week-1 returns a
46
- * clearly-marked stub (empty `evaluations`, `permitted: null`). */
47
- export interface SimulationResult {
48
- backend: 'ts-model';
49
- /** Whether the permit tx would be allowed; null until real semantics land. */
50
- permitted: boolean | null;
51
- /** Per-construct evaluation trace; empty placeholder in this slice. */
52
- evaluations: unknown[];
53
- /** Diagnostics; marks the stub explicitly. */
54
- notes: string[];
55
- }
56
- /** OUTPUT: compile a PolicyIR to a backend-specific installable policy. */
57
- export interface CustodyAdapter {
58
- readonly name: string;
59
- readonly mode: CustodyMode;
60
- capabilities(): CustodyCapabilities;
61
- compile(ir: PolicyIR): CompileResult;
62
- /** Phase-03 wiring; a clearly-marked stub in week-1. */
63
- simulate(ir: PolicyIR, permitTx: RecordedTransaction): SimulationResult;
64
- /** Canonical JSON of the IR (portability / audit). */
65
- export(ir: PolicyIR): string;
66
- }
@@ -1,11 +0,0 @@
1
- // src/seams/types.ts - the three custody seams (contracts, not implementations).
2
- //
3
- // The synthesizer is decoupled from its edges by three seams:
4
- // - PolicySource (INPUT): produces a normalized RecordedTransaction. The
5
- // recorder is the reference source; a MandateSpec
6
- // lowering is a second, deterministic source.
7
- // - ChainDecoder (DECODE): chain-specific call data -> normalized facts.
8
- // - CustodyAdapter (OUTPUT): compile a PolicyIR to a backend-native policy +
9
- // verify/export it (OZ is the first adapter).
10
- // These are INTERFACES only. Implementations live in their own modules.
11
- export {};