@crediolabs/policy-synth 0.1.17 → 0.2.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 (113) hide show
  1. package/README.md +44 -246
  2. package/dist/adapters/interpreter/adapter.d.ts +6 -7
  3. package/dist/adapters/interpreter/adapter.js +28 -48
  4. package/dist/adapters/oz/adapter.js +12 -13
  5. package/dist/codegen/compile-gate.js +5 -0
  6. package/dist/codegen/template.js +17 -0
  7. package/dist/install/authority-overlap.d.ts +134 -0
  8. package/dist/install/authority-overlap.js +0 -0
  9. package/dist/install/build-add-context-rule.d.ts +8 -0
  10. package/dist/install/build-add-context-rule.js +17 -46
  11. package/dist/install/build-install-policy.d.ts +12 -5
  12. package/dist/install/build-install-policy.js +32 -23
  13. package/dist/install/build-merge-policy.d.ts +70 -0
  14. package/dist/install/build-merge-policy.js +130 -0
  15. package/dist/install/get-interpreter-info.js +2 -2
  16. package/dist/install/index.d.ts +3 -1
  17. package/dist/install/index.js +8 -1
  18. package/dist/install/oz-auth.js +5 -3
  19. package/dist/install/plan-merge-policy.d.ts +49 -0
  20. package/dist/install/plan-merge-policy.js +86 -0
  21. package/dist/install/read-account-rules.d.ts +100 -0
  22. package/dist/install/read-account-rules.js +283 -0
  23. package/dist/predicate/decode.js +1 -1
  24. package/dist/predicate/encode.js +171 -10
  25. package/dist/record/decode.js +5 -6
  26. package/dist/registry/protocols.d.ts +1 -1
  27. package/dist/registry/protocols.js +1 -1
  28. package/dist/review-card/builder.d.ts +6 -0
  29. package/dist/review-card/builder.js +9 -1
  30. package/dist/run/index.d.ts +104 -18
  31. package/dist/run/index.js +359 -75
  32. package/dist/run/schemas.d.ts +479 -18
  33. package/dist/run/schemas.js +132 -24
  34. package/dist/synth/compose-from-recording.d.ts +10 -15
  35. package/dist/synth/compose-from-recording.js +79 -124
  36. package/dist/synth/deny-cases.d.ts +5 -0
  37. package/dist/synth/deny-cases.js +52 -13
  38. package/dist/synth/evaluate.js +69 -119
  39. package/dist/synth/harness.d.ts +13 -1
  40. package/dist/synth/harness.js +22 -1
  41. package/dist/synth/index.d.ts +1 -1
  42. package/dist/synth/synthesize-from-recording.d.ts +31 -21
  43. package/dist/synth/synthesize-from-recording.js +129 -155
  44. package/dist-cjs/adapters/interpreter/adapter.d.ts +6 -7
  45. package/dist-cjs/adapters/interpreter/adapter.js +28 -48
  46. package/dist-cjs/adapters/oz/adapter.js +12 -13
  47. package/dist-cjs/codegen/compile-gate.js +5 -0
  48. package/dist-cjs/codegen/template.js +17 -0
  49. package/dist-cjs/install/authority-overlap.d.ts +134 -0
  50. package/dist-cjs/install/authority-overlap.js +0 -0
  51. package/dist-cjs/install/build-add-context-rule.d.ts +8 -0
  52. package/dist-cjs/install/build-add-context-rule.js +17 -45
  53. package/dist-cjs/install/build-install-policy.d.ts +12 -5
  54. package/dist-cjs/install/build-install-policy.js +32 -23
  55. package/dist-cjs/install/build-merge-policy.d.ts +70 -0
  56. package/dist-cjs/install/build-merge-policy.js +134 -0
  57. package/dist-cjs/install/get-interpreter-info.js +2 -2
  58. package/dist-cjs/install/index.d.ts +3 -1
  59. package/dist-cjs/install/index.js +24 -3
  60. package/dist-cjs/install/oz-auth.js +5 -3
  61. package/dist-cjs/install/plan-merge-policy.d.ts +49 -0
  62. package/dist-cjs/install/plan-merge-policy.js +90 -0
  63. package/dist-cjs/install/read-account-rules.d.ts +100 -0
  64. package/dist-cjs/install/read-account-rules.js +296 -0
  65. package/dist-cjs/predicate/decode.js +1 -1
  66. package/dist-cjs/predicate/encode.js +171 -10
  67. package/dist-cjs/record/decode.js +5 -6
  68. package/dist-cjs/registry/protocols.d.ts +1 -1
  69. package/dist-cjs/registry/protocols.js +1 -1
  70. package/dist-cjs/review-card/builder.d.ts +6 -0
  71. package/dist-cjs/review-card/builder.js +9 -1
  72. package/dist-cjs/run/index.d.ts +104 -18
  73. package/dist-cjs/run/index.js +360 -74
  74. package/dist-cjs/run/schemas.d.ts +479 -18
  75. package/dist-cjs/run/schemas.js +133 -25
  76. package/dist-cjs/synth/compose-from-recording.d.ts +10 -15
  77. package/dist-cjs/synth/compose-from-recording.js +79 -124
  78. package/dist-cjs/synth/deny-cases.d.ts +5 -0
  79. package/dist-cjs/synth/deny-cases.js +52 -13
  80. package/dist-cjs/synth/evaluate.js +69 -119
  81. package/dist-cjs/synth/harness.d.ts +13 -1
  82. package/dist-cjs/synth/harness.js +22 -1
  83. package/dist-cjs/synth/index.d.ts +1 -1
  84. package/dist-cjs/synth/synthesize-from-recording.d.ts +31 -21
  85. package/dist-cjs/synth/synthesize-from-recording.js +129 -153
  86. package/package.json +1 -1
  87. package/src/adapters/interpreter/adapter.ts +28 -48
  88. package/src/adapters/oz/adapter.ts +12 -13
  89. package/src/codegen/compile-gate.ts +5 -0
  90. package/src/codegen/template.ts +17 -0
  91. package/src/install/authority-overlap.ts +0 -0
  92. package/src/install/build-add-context-rule.ts +29 -56
  93. package/src/install/build-install-policy.ts +41 -23
  94. package/src/install/build-merge-policy.ts +219 -0
  95. package/src/install/get-interpreter-info.ts +2 -2
  96. package/src/install/index.ts +36 -2
  97. package/src/install/oz-auth.ts +5 -3
  98. package/src/install/plan-merge-policy.ts +133 -0
  99. package/src/install/read-account-rules.ts +376 -0
  100. package/src/predicate/decode.ts +1 -1
  101. package/src/predicate/encode.ts +176 -10
  102. package/src/record/decode.ts +5 -6
  103. package/src/registry/protocols.ts +1 -1
  104. package/src/review-card/builder.ts +17 -1
  105. package/src/run/index.ts +500 -102
  106. package/src/run/schemas.ts +145 -24
  107. package/src/synth/compose-from-recording.ts +87 -132
  108. package/src/synth/deny-cases.ts +59 -13
  109. package/src/synth/evaluate.ts +70 -118
  110. package/src/synth/harness.ts +29 -1
  111. package/src/synth/index.ts +1 -0
  112. package/src/synth/synthesize-from-recording.ts +209 -192
  113. package/src/contracts/policy-template/OZ_POLICY_TRAIT.md +0 -196
@@ -1,14 +1,16 @@
1
1
  import { type ErrorCode, type PredicateNode, type ProposedPolicy, type RecordedTransaction, type ToolError, type ToolResponse } from '../index.ts';
2
+ import { type AuthorityOverlap } from '../install/authority-overlap.ts';
2
3
  import { type BuildInstallPolicyResult, type BuildRevokePolicyResult } from '../install/build-install-policy.ts';
3
4
  import { getInterpreterInfo } from '../install/get-interpreter-info.ts';
4
5
  import type { SimulationResult } from '../verify/envelope.ts';
5
6
  import { type RecordTransactionInput, type SimulatePolicyInput, type SynthesizePolicyInput, type VerifyPolicyInput } from './schemas.ts';
6
7
  export type { GetInterpreterInfoInput, InstallPolicyInput, RecordTransactionInput, RevokePolicyInput, SimulatePolicyInput, SynthesizePolicyInput, VerifyPolicyInput, } from './schemas.ts';
7
- export { ComposeUserResponsesSchema, GetInterpreterInfoInputSchema, InstallPolicyInputSchema, InterpreterOptionsSchema, MAINNET_RPC_URL, MandateSpecSchema, NetworkSchema, OraclePriceFixtureSchema, OzAdapterConfigSchema, PINNED_INTERPRETER_ADDRESS_BY_NETWORK, PINNED_INTERPRETER_GRAMMAR_VERSION, PINNED_INTERPRETER_MAINNET_ADDRESS, PINNED_INTERPRETER_TESTNET_ADDRESS, PINNED_INTERPRETER_WASM_SHA256, PredicateLeafSchema, PredicateNodeSchema, RecordedTransactionSchema, RecordTransactionInputSchema, RevokePolicyInputSchema, RPC_URL_BY_NETWORK, SimulatePolicyInputSchema, SynthesizePolicyInputSchema, TESTNET_RPC_URL, ToolErrorSchema, VerifyPolicyInputSchema, } from './schemas.ts';
8
+ export { ComposeUserResponsesSchema, GetInterpreterInfoInputSchema, InstallPolicyInputSchema, InterpreterOptionsSchema, MAINNET_RPC_URL, MandateSpecSchema, NetworkSchema, OraclePriceFixtureSchema, OzAdapterConfigSchema, PINNED_INTERPRETER_ADDRESS_BY_NETWORK, PINNED_INTERPRETER_GRAMMAR_VERSION, PINNED_INTERPRETER_MAINNET_ADDRESS, PINNED_INTERPRETER_TESTNET_ADDRESS, PINNED_INTERPRETER_WASM_SHA256_BY_NETWORK, PredicateLeafSchema, PredicateNodeSchema, RecordedTransactionSchema, RecordTransactionInputSchema, RevokePolicyInputSchema, RPC_URL_BY_NETWORK, SimulatePolicyInputSchema, SynthesizePolicyInputSchema, TESTNET_RPC_URL, ToolErrorSchema, VerifyPolicyInputSchema, } from './schemas.ts';
8
9
  export type RunRecordTransactionInput = RecordTransactionInput;
9
10
  export type RunSynthesizePolicyInput = SynthesizePolicyInput;
10
11
  export type RunSimulatePolicyInput = SimulatePolicyInput;
11
12
  export type RunVerifyPolicyInput = VerifyPolicyInput;
13
+ type RunToolName = 'record_transaction' | 'synthesize_policy' | 'simulate_policy' | 'verify_policy' | 'install_policy' | 'revoke_policy' | 'get_interpreter_info';
12
14
  /** `record_transaction` body - wraps `recordTransaction`. The tool input
13
15
  * matches the core RecordInput minus the injected `fetcher` (the transport
14
16
  * layer does not own the RPC). Returns the core ToolResponse unchanged.
@@ -65,7 +67,91 @@ export declare function runVerifyPolicy(raw: unknown): Promise<ToolResponse<true
65
67
  * comes from the RPC). Both gates accept an explicit opt-in flag.
66
68
  * Pin selection follows `input.network` (defaults to `testnet` so the
67
69
  * pre-mainnet callers keep working unchanged). */
68
- export declare function runInstallPolicy(raw: unknown): Promise<ToolResponse<BuildInstallPolicyResult>>;
70
+ export declare function runInstallPolicy(raw: unknown): Promise<ToolResponse<InstallPolicyResult>>;
71
+ /** Default-deny on the cross-rule scan.
72
+ *
73
+ * Refuses whenever the scan cannot establish that this policy binds the calls
74
+ * it names. An unpoliced neighbour provably does not constrain them. An
75
+ * opaque one, policed by a contract this tool cannot decode, is not KNOWN to,
76
+ * and "not known to" is not "safe" - the same posture as the interpreter and
77
+ * RPC pins. An incomplete scan is refused for the same reason: the overlap
78
+ * list is then a subset of the account, so an empty list proves nothing.
79
+ *
80
+ * `not-restricting` is reported but does NOT block. Both rules are ours and
81
+ * both constrain the calls, and the conjunction remedy is offered; refusing
82
+ * there would also block the legitimate act of adding a separate capability,
83
+ * which OZ composes correctly as a union.
84
+ *
85
+ * Returns a ToolError or null, matching `enforceInterpreterPin`. */
86
+ export declare function enforceAuthorityScan(scan: AuthorityScanReport | undefined, allowOverlap: boolean | undefined): ToolError | null;
87
+ /** The install response, plus what the cross-rule scan found. The scan is
88
+ * advisory data about the account, not part of the transaction, so it is
89
+ * additive: a caller that ignores it gets exactly the previous shape. */
90
+ export type InstallPolicyResult = BuildInstallPolicyResult & {
91
+ authorityScan?: AuthorityScanReport;
92
+ };
93
+ /** What the cross-rule scan found, carried on the install response so the
94
+ * review surface can show it alongside the transaction being signed.
95
+ *
96
+ * SCOPE, and it is narrow: this answers "can a signer OF THIS RULE reach the
97
+ * same calls through a different rule". A rule sharing no signer with this
98
+ * one cannot be reached by this rule's signers, so it is not a way around
99
+ * this policy; it is a different principal's authority, which no policy
100
+ * installed here was ever going to constrain. Other rules keep their own
101
+ * signers, and an account administrator can add signers or rules afterwards.
102
+ *
103
+ * An empty `overlaps` is therefore NOT a statement that the account is safe,
104
+ * only that this rule's own signers gain no unconstrained path through the
105
+ * rules that exist right now. */
106
+ export interface AuthorityScanReport {
107
+ /** False when the scan did not run. `reason` then says why, and the absence
108
+ * of overlaps proves nothing. */
109
+ ran: boolean;
110
+ /** True when the caller passed `skipAuthorityScan`. Distinguishes a
111
+ * deliberate skip from a scan that tried and failed: both carry
112
+ * `ran: false`, but only the failure refuses the install. Recorded rather
113
+ * than omitted so the response shows that no opinion was formed, instead
114
+ * of looking like a version that never had the check. */
115
+ skipped?: boolean;
116
+ /** True when the account has more rules than the scan accounted for, so the
117
+ * overlap list is a subset. */
118
+ incomplete?: boolean;
119
+ reason?: string;
120
+ overlaps: AuthorityOverlap[];
121
+ }
122
+ /** The merge response: one step's transaction plus what it will cost. */
123
+ export interface MergePolicyResult {
124
+ unsignedXdr: string;
125
+ smartAccount: string;
126
+ sourceAccount: string;
127
+ step: 'detach' | 'reinstall';
128
+ call: {
129
+ contract: string;
130
+ fn: string;
131
+ ruleId: number;
132
+ };
133
+ authNonce: string;
134
+ authValidUntilLedger: number;
135
+ rootInvocationXdr: string;
136
+ /** sha256 of the merged predicate, so the caller can pin what step 2 will
137
+ * install while they are still looking at step 1. */
138
+ mergedPredicateHash: string;
139
+ mergedPredicateBlobBase64: string;
140
+ warnings: string[];
141
+ followUp: string;
142
+ }
143
+ /** `merge_policy` body - the tightening remedy for a cross-rule overlap.
144
+ *
145
+ * Replaces a rule's predicate with the conjunction of it and a new one. This
146
+ * is the action `install_policy` recommends when it reports an overlap
147
+ * between two rules our interpreter polices, and it is deliberately NOT
148
+ * something `install_policy` does on its own: it detaches a live policy, so
149
+ * the operator has to ask for it.
150
+ *
151
+ * Two transactions in order. `add_policy` refuses a policy already on the
152
+ * rule, so the old attachment goes first, and the second transaction cannot
153
+ * be simulated until the first confirms. */
154
+ export declare function runMergePolicy(raw: unknown): Promise<ToolResponse<MergePolicyResult>>;
69
155
  /** `revoke_policy` body - thin wrapper over `buildRevokePolicyXdr`.
70
156
  * Emits an unsigned XDR for `account.remove_context_rule(ruleId)`; the
71
157
  * smart account itself handles uninstalling each attached policy. Auth
@@ -83,23 +169,23 @@ export declare function runRevokePolicy(raw: unknown): Promise<ToolResponse<Buil
83
169
  * fabricating it would be a lie on a security surface; the live
84
170
  * mismatch check is worth MORE).
85
171
  *
86
- * Network-aware: `input.network` selects which interpreter pin and RPC
87
- * to use. Mainnet was rolled out 2026-08-04 - the same wasm hash was
88
- * uploaded to mainnet as was exercised on testnet, so a single
89
- * `PINNED_INTERPRETER_WASM_SHA256` constant backs both networks
90
- * (DEPLOYMENTS.md:25-27). The address differs because instance ids are
91
- * network-scoped. UNAUDITED at the time of writing - see
92
- * DEPLOYMENTS.md:29-32 for what is still pending. */
172
+ * Network-aware: `input.network` selects the interpreter address, the RPC
173
+ * and the wasm hash. The networks run different binaries - testnet carries
174
+ * the selector-leaf minimum and the signer-set cap, mainnet predates both -
175
+ * so the hash is read through
176
+ * `PINNED_INTERPRETER_WASM_SHA256_BY_NETWORK`. UNAUDITED at the time of
177
+ * writing.
178
+ *
179
+ * Same RPC pin as install/revoke: when `verifyLive` triggers an outbound
180
+ * call, the auth-digest + the answer bind to whichever RPC answered, so
181
+ * a non-pinned `rpcUrl` would silently bind the caller to a host they
182
+ * picked. The pin is enforced here too, with the same `allowUnpinnedRpcUrl`
183
+ * opt-in as install/revoke. */
93
184
  export declare function runGetInterpreterInfo(raw: unknown): Promise<ToolResponse<ReturnType<typeof getInterpreterInfo>>>;
94
185
  /** Build a canonical ToolError for a thrown exception caught by the tool
95
186
  * envelope. The MCP SDK stringifies thrown objects as "[object Object]" by
96
187
  * default, so we extract a string-friendly message and tag the original
97
- * error in `details` for the agent to inspect. The `code` is the tool's
98
- * domain code (RECORDING_FAILED for `record_transaction`, SYNTHESIS_ERROR
99
- * for `synthesize_policy`) so the agent dispatches on the same code the
100
- * structured ToolError would carry.
101
- *
102
- * Exported as `_caughtError` (the leading underscore signals the test-only
103
- * seam) so the suite in run/index.test.ts can drive the envelope path
104
- * without standing up a full recordTransaction pipeline. */
105
- export declare function caughtError(toolName: 'record_transaction' | 'synthesize_policy' | 'simulate_policy' | 'verify_policy' | 'install_policy' | 'revoke_policy' | 'get_interpreter_info', code: ErrorCode, e: unknown): ToolError;
188
+ * error in `details` for the agent to inspect. Exported as a test-only seam
189
+ * so the suite in run/index.test.ts can drive the envelope path without
190
+ * standing up a full recordTransaction pipeline. */
191
+ export declare function caughtError(toolName: RunToolName, code: ErrorCode, e: unknown): ToolError;