@bolyra/receipts 0.10.0 → 0.12.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.
@@ -0,0 +1,251 @@
1
+ /**
2
+ * Approval-to-execution binding primitive
3
+ * (docs/superpowers/specs/2026-10-25-approval-binding-design.md).
4
+ *
5
+ * Three pieces, all inside the signed payloads:
6
+ *
7
+ * 1. An approval receipt (`bolyra.approval`, scheme `bab1`) binds the reviewed
8
+ * action — requester, audience, tool, operation id — and a digest of the
9
+ * exact reviewed arguments, together with who approved it and when.
10
+ * 2. `executeApproved` verifies that receipt immediately before dispatch and
11
+ * dispatches only the immutable argument snapshot it checked. It is the
12
+ * only path that dispatches; there is no standalone guard to misuse.
13
+ * 3. An execution observation (`bolyra.execution`, scheme `beo1`) links to the
14
+ * approval receipt hash, the operation and attempt ids and the digest of the
15
+ * dispatched snapshot, and verifies offline with
16
+ * `verifyApprovalExecutionLink`.
17
+ *
18
+ * What this does NOT establish (the README repeats the list): verified human
19
+ * identity because a name was signed; authorization correctness; downstream
20
+ * commitment from dispatch; exactly-once execution; protection against
21
+ * bypassing the hook; log completeness; live vendor integration. The receipt
22
+ * records what the operator asserted was reviewed; it carries the argument
23
+ * digest only, never the arguments.
24
+ *
25
+ * No function here logs, writes files or makes network calls.
26
+ */
27
+ import type { ReceiptSignerConfig, SignedReceipt } from './types';
28
+ /** Domain-separation tag for the arguments digest, terminated by an explicit 0x00. */
29
+ export declare const ARGUMENTS_DIGEST_DST = "bolyra.approval.arguments/1";
30
+ /** Maximum container nesting depth of the argument object (the root object is depth 1). */
31
+ export declare const ARGUMENTS_MAX_DEPTH = 64;
32
+ /** Maximum serialized size (UTF-8 bytes) of the argument text and of its canonical form. */
33
+ export declare const ARGUMENTS_MAX_BYTES: number;
34
+ /** Fixed text returned with every link verification result. */
35
+ export declare const LINK_VERIFICATION_STATEMENT: string;
36
+ export type JsonPrimitive = string | number | boolean | null;
37
+ export type JsonValue = JsonPrimitive | JsonValue[] | {
38
+ [key: string]: JsonValue;
39
+ };
40
+ export type JsonObject = {
41
+ [key: string]: JsonValue;
42
+ };
43
+ export interface ApprovalBinding {
44
+ scheme: 'bab1';
45
+ /** Identity that requested the action, as asserted by the operator. */
46
+ requester: string;
47
+ /** Execution audience: the server / system that will execute. */
48
+ audience: string;
49
+ /** Action / tool name. */
50
+ tool: string;
51
+ /** Operator-assigned, unique per operation. */
52
+ operationId: string;
53
+ /** computeArgumentsDigest() of the reviewed arguments (64 lowercase hex). */
54
+ argumentsDigest: string;
55
+ }
56
+ export type ApprovalDecision = 'approved' | 'denied';
57
+ export type ApprovalValidity = {
58
+ kind: 'once';
59
+ } | {
60
+ kind: 'until';
61
+ notAfter: string;
62
+ };
63
+ export interface ApprovalAssertion {
64
+ /** Exactly as the approval source asserted it (printable ASCII, 1..256). */
65
+ approver: string;
66
+ /** Provenance label of the approval source. */
67
+ source: string;
68
+ decision: ApprovalDecision;
69
+ /** RFC 3339 UTC with exactly 3-digit milliseconds and a Z suffix. */
70
+ decidedAt: string;
71
+ validity: ApprovalValidity;
72
+ /** External approval-request identifier, verbatim. */
73
+ requestId?: string;
74
+ }
75
+ export interface ApprovalReceiptPayload {
76
+ v: 1;
77
+ kind: 'bolyra.approval';
78
+ /** Unix seconds. */
79
+ issuedAt: number;
80
+ issuer: string;
81
+ keyId: string;
82
+ binding: ApprovalBinding;
83
+ approval: ApprovalAssertion;
84
+ }
85
+ export interface ExecutionFields {
86
+ scheme: 'beo1';
87
+ /** computeReceiptHash(approval receipt); verifiers recompute. */
88
+ approvalReceiptHash: string;
89
+ /** Approval envelope id; verifiers re-derive it from payloadHash. */
90
+ approvalReceiptId: string;
91
+ operationId: string;
92
+ /** Operator-assigned per dispatch attempt. */
93
+ attemptId: string;
94
+ requester: string;
95
+ audience: string;
96
+ tool: string;
97
+ /** computeArgumentsDigest() of the dispatched snapshot (same DST). */
98
+ actionDigest: string;
99
+ /** Captured immediately before invoking dispatch. */
100
+ dispatchedAt: string;
101
+ /** What the dispatch promise did — NOT a confirmed effect. */
102
+ outcome: 'returned' | 'threw';
103
+ }
104
+ export interface ExecutionReceiptPayload {
105
+ v: 1;
106
+ kind: 'bolyra.execution';
107
+ issuedAt: number;
108
+ issuer: string;
109
+ keyId: string;
110
+ execution: ExecutionFields;
111
+ }
112
+ export interface ApprovalReceiptInput {
113
+ /** Defaults to 1; any other value throws. */
114
+ v?: 1;
115
+ /** Defaults to 'bolyra.approval'; any other value throws. */
116
+ kind?: 'bolyra.approval';
117
+ /** Unix seconds; defaults to now. */
118
+ issuedAt?: number;
119
+ binding: {
120
+ /** Defaults to 'bab1'; any other value throws. */
121
+ scheme?: 'bab1';
122
+ requester: string;
123
+ audience: string;
124
+ tool: string;
125
+ operationId: string;
126
+ };
127
+ approval: ApprovalAssertion;
128
+ /** The reviewed arguments as JSON text; only their digest enters the receipt. */
129
+ argumentsJson: string;
130
+ }
131
+ export interface ApprovalContext {
132
+ requester: string;
133
+ audience: string;
134
+ tool: string;
135
+ operationId: string;
136
+ }
137
+ export interface ConsumeClaim {
138
+ /** Recovered signer address, lowercase. Namespace the store by it. */
139
+ signer: string;
140
+ /** Full payloadHash of the approval receipt — the replay identity. */
141
+ payloadHash: string;
142
+ operationId: string;
143
+ }
144
+ /**
145
+ * Operator-owned, atomic, durable claim across all executors. Only the literal
146
+ * `true` permits dispatch. The guard cannot know whether the approved operation
147
+ * already produced its effect; business-state checks belong to the operator and
148
+ * must be made atomically with the mutation.
149
+ */
150
+ export type ConsumeFn = (claim: ConsumeClaim) => boolean;
151
+ export type DispatchFn<R> = (snapshot: Readonly<JsonObject>, checkedContext: Readonly<ApprovalContext>) => R | Promise<R>;
152
+ export interface ExecuteApprovedParams {
153
+ receipt: SignedReceipt<ApprovalReceiptPayload>;
154
+ /** Address the approval receipt must recover to. Required, non-empty. */
155
+ expectedSigner: string;
156
+ context: ApprovalContext;
157
+ argumentsJson: string;
158
+ attemptId: string;
159
+ /** Required for `once` receipts. */
160
+ consume?: ConsumeFn;
161
+ /** When present, a `bolyra.execution` observation is signed after settlement. */
162
+ observer?: ReceiptSignerConfig;
163
+ /** Defaults to `() => new Date()`. */
164
+ clock?: () => Date;
165
+ }
166
+ export type BlockedExecutionCode = 'malformed_input' | 'malformed_receipt' | 'wrong_kind' | 'keyid_mismatch' | 'signature_invalid' | 'untrusted_signer' | 'decision_not_approved' | 'future_decision' | 'expired' | 'context_mismatch' | 'arguments_out_of_domain' | 'arguments_mismatch' | 'consume_required' | 'consume_rejected' | 'observer_invalid' | 'invalid_clock';
167
+ export interface BlockedExecution {
168
+ outcome: 'blocked';
169
+ code: BlockedExecutionCode;
170
+ detail: string;
171
+ }
172
+ export interface ReturnedExecution<R> {
173
+ outcome: 'returned';
174
+ value: R;
175
+ observation?: SignedReceipt<ExecutionReceiptPayload>;
176
+ observationError?: Error;
177
+ }
178
+ export type ApprovedExecution<R> = BlockedExecution | ReturnedExecution<R>;
179
+ /** The dispatch promise rejected (or dispatch threw). Carries the observation when one was signed. */
180
+ export declare class ApprovedExecutionError extends Error {
181
+ readonly cause: unknown;
182
+ readonly observation?: SignedReceipt<ExecutionReceiptPayload>;
183
+ readonly observationError?: Error;
184
+ constructor(cause: unknown, observation?: SignedReceipt<ExecutionReceiptPayload>, observationError?: Error);
185
+ }
186
+ export type LinkCheckId = 'A1' | 'A2' | 'A3' | 'E1' | 'E2' | 'L1' | 'L2' | 'L3' | 'L4';
187
+ export interface LinkCheck {
188
+ id: LinkCheckId;
189
+ status: 'PASS' | 'FAIL';
190
+ detail: string;
191
+ }
192
+ export interface LinkVerificationResult {
193
+ ok: boolean;
194
+ checks: LinkCheck[];
195
+ statement: string;
196
+ }
197
+ export interface VerifyLinkParams {
198
+ /** Untrusted input; validated as unknown. */
199
+ approval: unknown;
200
+ /** Untrusted input; validated as unknown. */
201
+ observation: unknown;
202
+ expectedApprovalSigner: string;
203
+ expectedExecutionSigner: string;
204
+ }
205
+ export type ArgumentsValidation = {
206
+ ok: true;
207
+ } | {
208
+ ok: false;
209
+ detail: string;
210
+ };
211
+ /**
212
+ * bab1 domain check for argument JSON text: invalid JSON → ok:false with an
213
+ * "invalid JSON" detail; otherwise the parsed tree must be a non-array object
214
+ * whose values are objects, arrays, strings, finite numbers, booleans or null,
215
+ * with no -0, no `__proto__`/`constructor`/`prototype` key at any depth,
216
+ * nesting depth ≤ 64, text and canonical form ≤ 1 MiB.
217
+ */
218
+ export declare function validateArgumentsJson(argumentsJson: string): ArgumentsValidation;
219
+ /**
220
+ * argumentsDigest = sha256hex( utf8("bolyra.approval.arguments/1") || 0x00 || utf8(canonicalize(value)) )
221
+ * where `value` is the parsed, bab1-validated argument object. Throws on
222
+ * invalid JSON or out-of-domain values.
223
+ */
224
+ export declare function computeArgumentsDigest(argumentsJson: string): string;
225
+ /**
226
+ * Validates every member of `input` and the bab1 domain of `argumentsJson`,
227
+ * computes the digest, builds the §4.1 payload and signs it. Throws on any
228
+ * invalid input, naming the first offending member. Provenance (`approver`,
229
+ * `source`, `decidedAt`, `decision`, `validity`) is never defaulted; only the
230
+ * fixed discriminators (`v`, `kind`, `scheme`) and `issuedAt` have defaults.
231
+ */
232
+ export declare function createApprovalReceipt(input: ApprovalReceiptInput, signerConfig: ReceiptSignerConfig): SignedReceipt<ApprovalReceiptPayload>;
233
+ /**
234
+ * The only path that dispatches. Follows spec §5.3 steps 1–7 in order:
235
+ * snapshot once, preflight the observer, validate the receipt against the
236
+ * snapshots, sample the clock, consume (once receipts), re-check the clock and
237
+ * dispatch the frozen snapshot, then sign the observation.
238
+ *
239
+ * Result contract: any failure before dispatch — including an unexpected
240
+ * exception — resolves to a typed blocked result and dispatch is never called.
241
+ * After dispatch the result is always `returned` or an ApprovedExecutionError
242
+ * rejection carrying the original cause; an observation failure of any kind
243
+ * is reported as `observationError` and never changes the settlement.
244
+ */
245
+ export declare function executeApproved<R>(params: ExecuteApprovedParams, dispatch: DispatchFn<R>): Promise<ApprovedExecution<R>>;
246
+ /**
247
+ * Offline, pure link verification. Always reports all nine checks; a check
248
+ * whose prerequisite failed is reported FAIL as "not evaluated". Never throws:
249
+ * hostile inputs (cycles, bigint, throwing getters) yield FAIL checks.
250
+ */
251
+ export declare function verifyApprovalExecutionLink(params: VerifyLinkParams): LinkVerificationResult;