@t3foundry/tasra-sdk 0.3.1-next.1 → 0.4.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/CHANGELOG.md +1067 -28
- package/README.md +15 -0
- package/dist/app/approvals.d.ts +66 -4
- package/dist/app/approvals.js +153 -16
- package/dist/app/authorization.d.ts +1 -1
- package/dist/app/authorization.js +1 -1
- package/dist/app/browser.d.ts +63 -0
- package/dist/app/browser.js +242 -0
- package/dist/app/client.d.ts +114 -13
- package/dist/app/client.js +302 -76
- package/dist/app/identity.js +21 -9
- package/dist/app/index.d.ts +28 -3
- package/dist/app/index.js +16 -1
- package/dist/app/memory-store.d.ts +7 -0
- package/dist/app/memory-store.js +55 -0
- package/dist/app/node.d.ts +34 -6
- package/dist/app/node.js +387 -64
- package/dist/app/paid-allowance.d.ts +325 -0
- package/dist/app/paid-allowance.js +1290 -0
- package/dist/app/paid-approval.d.ts +122 -0
- package/dist/app/paid-approval.js +379 -0
- package/dist/app/paid-journal.d.ts +51 -0
- package/dist/app/paid-journal.js +117 -0
- package/dist/app/paid-keys.d.ts +12 -0
- package/dist/app/paid-keys.js +20 -0
- package/dist/app/paid-operation.d.ts +821 -0
- package/dist/app/paid-operation.js +1720 -0
- package/dist/app/paid-reservation.d.ts +157 -0
- package/dist/app/paid-reservation.js +323 -0
- package/dist/app/paid-session.d.ts +147 -0
- package/dist/app/paid-session.js +578 -0
- package/dist/app/payer-queue.d.ts +11 -0
- package/dist/app/payer-queue.js +27 -0
- package/dist/app/policy-bootstrap.d.ts +342 -0
- package/dist/app/policy-bootstrap.js +365 -0
- package/dist/app/policy-operation.d.ts +47 -0
- package/dist/app/policy-operation.js +101 -0
- package/dist/app/ready-slot.d.ts +12 -0
- package/dist/app/ready-slot.js +5 -2
- package/dist/app/rotation-lock.d.ts +38 -0
- package/dist/app/rotation-lock.js +42 -0
- package/dist/app/session-decrypt.d.ts +135 -0
- package/dist/app/session-decrypt.js +335 -0
- package/dist/app/session-grants.d.ts +349 -0
- package/dist/app/session-grants.js +811 -0
- package/dist/app/signing-intents.d.ts +78 -0
- package/dist/app/signing-intents.js +95 -0
- package/dist/app/store-commit.d.ts +63 -0
- package/dist/app/store-commit.js +126 -0
- package/dist/app/store-errors.d.ts +8 -0
- package/dist/app/store-errors.js +11 -0
- package/dist/app/tasra-client.d.ts +2 -0
- package/dist/app/tasra-client.js +1 -1
- package/dist/app/wallet.js +4 -1
- package/dist/auth/holderProof.d.ts +3 -0
- package/dist/auth/holderProof.js +8 -7
- package/dist/auth/verifier.d.ts +4 -2
- package/dist/auth/verifier.js +5 -2
- package/dist/chain/abis/index.d.ts +6178 -4101
- package/dist/chain/abis/index.js +3 -0
- package/dist/chain/abis/keyRegistry.d.ts +256 -0
- package/dist/chain/abis/keyRegistry.js +333 -0
- package/dist/chain/abis/prepaidFaultOracle.d.ts +321 -0
- package/dist/chain/abis/prepaidFaultOracle.js +413 -0
- package/dist/chain/abis/settlement.d.ts +2203 -703
- package/dist/chain/abis/settlement.js +2685 -764
- package/dist/chain/allowance.d.ts +358 -0
- package/dist/chain/allowance.js +800 -0
- package/dist/chain/billing.d.ts +85 -0
- package/dist/chain/billing.js +197 -0
- package/dist/chain/chainId.d.ts +13 -0
- package/dist/chain/chainId.js +26 -0
- package/dist/chain/client.d.ts +15 -1
- package/dist/chain/client.js +57 -1
- package/dist/chain/commitmentRecovery.d.ts +51 -0
- package/dist/chain/commitmentRecovery.js +38 -0
- package/dist/chain/committeeClient.d.ts +17 -10
- package/dist/chain/committeeClient.js +64 -16
- package/dist/chain/committeeDraw.d.ts +12 -0
- package/dist/chain/committeeDraw.js +56 -0
- package/dist/chain/custodyHealth.d.ts +212 -0
- package/dist/chain/custodyHealth.js +365 -0
- package/dist/chain/deployments.js +2 -0
- package/dist/chain/events.js +2 -0
- package/dist/chain/headFollower.d.ts +77 -0
- package/dist/chain/headFollower.js +219 -0
- package/dist/chain/headPrefetch.d.ts +23 -0
- package/dist/chain/headPrefetch.js +67 -0
- package/dist/chain/index.d.ts +50 -8
- package/dist/chain/index.js +28 -3
- package/dist/chain/keeperExecution.d.ts +44 -0
- package/dist/chain/keeperExecution.js +127 -0
- package/dist/chain/keeperRecovery.d.ts +78 -0
- package/dist/chain/keeperRecovery.js +91 -0
- package/dist/chain/node.d.ts +24 -1
- package/dist/chain/node.js +88 -43
- package/dist/chain/offchain.d.ts +2 -0
- package/dist/chain/paidAgentTransport.d.ts +38 -0
- package/dist/chain/paidAgentTransport.js +1 -0
- package/dist/chain/paidEpoch.d.ts +66 -0
- package/dist/chain/paidEpoch.js +139 -0
- package/dist/chain/paidPacing.d.ts +81 -0
- package/dist/chain/paidPacing.js +136 -0
- package/dist/chain/pinnedReads.d.ts +49 -0
- package/dist/chain/pinnedReads.js +218 -0
- package/dist/chain/prepaid.d.ts +383 -0
- package/dist/chain/prepaid.js +1046 -0
- package/dist/chain/prepaidEscape.d.ts +274 -0
- package/dist/chain/prepaidEscape.js +432 -0
- package/dist/chain/prepaidFault.d.ts +94 -0
- package/dist/chain/prepaidFault.js +148 -0
- package/dist/chain/receipt.d.ts +24 -0
- package/dist/chain/receipt.js +82 -0
- package/dist/chain/registeredAgent.d.ts +68 -3
- package/dist/chain/registeredAgent.js +143 -42
- package/dist/chain/registeredOperation.d.ts +10 -1
- package/dist/chain/registeredOperation.js +18 -3
- package/dist/chain/registeredPaidAgent.d.ts +254 -0
- package/dist/chain/registeredPaidAgent.js +889 -0
- package/dist/chain/registeredRelay.d.ts +31 -1
- package/dist/chain/registeredRelay.js +164 -43
- package/dist/chain/relayVoucher.d.ts +64 -0
- package/dist/chain/relayVoucher.js +69 -0
- package/dist/chain/reservation.d.ts +239 -0
- package/dist/chain/reservation.js +746 -0
- package/dist/chain/reshareRoster.d.ts +64 -0
- package/dist/chain/reshareRoster.js +85 -0
- package/dist/chain/revertReason.d.ts +8 -0
- package/dist/chain/revertReason.js +2 -1
- package/dist/chain/rotationLock.d.ts +130 -0
- package/dist/chain/rotationLock.js +168 -0
- package/dist/chain/serviceRequestPolicy.d.ts +47 -0
- package/dist/chain/serviceRequestPolicy.js +86 -0
- package/dist/chain/services.d.ts +7 -0
- package/dist/chain/services.js +44 -4
- package/dist/chain/slotClient.js +11 -2
- package/dist/chain/slotHandoff.d.ts +81 -0
- package/dist/chain/slotHandoff.js +166 -0
- package/dist/chain/slotSeed.d.ts +47 -5
- package/dist/chain/slotSeed.js +173 -42
- package/dist/chain/verifierSnapshot.d.ts +36 -0
- package/dist/chain/verifierSnapshot.js +197 -0
- package/dist/chain/write.d.ts +111 -21
- package/dist/chain/write.js +378 -146
- package/dist/committee/blsSharing.d.ts +53 -0
- package/dist/committee/blsSharing.js +90 -0
- package/dist/committee/client.d.ts +62 -23
- package/dist/committee/client.js +288 -72
- package/dist/committee/dual-operation.d.ts +165 -0
- package/dist/committee/dual-operation.js +229 -0
- package/dist/committee/dual-sign.d.ts +26 -1
- package/dist/committee/dual-sign.js +85 -7
- package/dist/committee/ecdsa.d.ts +38 -0
- package/dist/committee/ecdsa.js +87 -15
- package/dist/committee/evidence.d.ts +30 -0
- package/dist/committee/evidence.js +65 -0
- package/dist/committee/execution.d.ts +414 -0
- package/dist/committee/execution.js +446 -0
- package/dist/committee/extraction.js +33 -26
- package/dist/committee/index.d.ts +30 -7
- package/dist/committee/index.js +18 -6
- package/dist/committee/keeperExecution.d.ts +37 -0
- package/dist/committee/keeperExecution.js +141 -0
- package/dist/committee/oauth.d.ts +14 -4
- package/dist/committee/oauth.js +17 -5
- package/dist/committee/payment.d.ts +369 -0
- package/dist/committee/payment.js +339 -0
- package/dist/committee/recovery.d.ts +136 -0
- package/dist/committee/recovery.js +204 -0
- package/dist/committee/recoveryJson.d.ts +2 -0
- package/dist/committee/recoveryJson.js +87 -0
- package/dist/committee/request.d.ts +63 -27
- package/dist/committee/request.js +77 -66
- package/dist/committee/session-grant.d.ts +320 -0
- package/dist/committee/session-grant.js +397 -0
- package/dist/committee/status-refresh.d.ts +257 -0
- package/dist/committee/status-refresh.js +619 -0
- package/dist/committee/token.d.ts +163 -15
- package/dist/committee/token.js +247 -10
- package/dist/crypto/bn254.d.ts +70 -0
- package/dist/crypto/bn254.js +222 -0
- package/dist/crypto/ibe.d.ts +32 -0
- package/dist/crypto/ibe.js +56 -8
- package/dist/crypto/index.d.ts +1 -1
- package/dist/crypto/index.js +1 -1
- package/dist/crypto/kem.d.ts +31 -0
- package/dist/crypto/kem.js +113 -7
- package/dist/crypto/recipient.d.ts +75 -0
- package/dist/crypto/recipient.js +107 -0
- package/dist/decryption/client.d.ts +10 -16
- package/dist/decryption/client.js +8 -76
- package/dist/decryption/index.d.ts +2 -0
- package/dist/decryption/index.js +2 -0
- package/dist/decryption/sealed.d.ts +72 -0
- package/dist/decryption/sealed.js +147 -0
- package/dist/decryption/session.d.ts +115 -0
- package/dist/decryption/session.js +358 -0
- package/dist/errors.d.ts +406 -1
- package/dist/errors.js +400 -5
- package/dist/index.d.ts +9 -2
- package/dist/index.js +6 -1
- package/dist/internal/bounded.d.ts +66 -0
- package/dist/internal/bounded.js +126 -0
- package/dist/internal/network.d.ts +4 -0
- package/dist/internal/network.js +40 -0
- package/dist/internal/retryAfter.d.ts +8 -0
- package/dist/internal/retryAfter.js +28 -0
- package/dist/internal/sleep.d.ts +6 -0
- package/dist/internal/sleep.js +15 -0
- package/dist/internal/typedData.d.ts +13 -0
- package/dist/internal/typedData.js +77 -0
- package/dist/internal/viemErrors.d.ts +26 -0
- package/dist/internal/viemErrors.js +49 -0
- package/dist/oid4vp/binding.d.ts +27 -1
- package/dist/oid4vp/binding.js +36 -1
- package/dist/oid4vp/jose.d.ts +4 -2
- package/dist/oid4vp/jose.js +39 -7
- package/dist/oid4vp/request-object.d.ts +61 -4
- package/dist/oid4vp/request-object.js +140 -5
- package/dist/oid4vp/verifier-agent.d.ts +18 -8
- package/dist/oid4vp/verifier-agent.js +48 -6
- package/dist/oid4vp/wallet.d.ts +2 -4
- package/dist/oid4vp/wallet.js +1 -0
- package/dist/retry.d.ts +78 -0
- package/dist/retry.js +137 -0
- package/dist/signing/frost.d.ts +1 -1
- package/dist/verifier-agent/index.d.ts +41 -5
- package/dist/verifier-agent/index.js +124 -9
- package/dist/verifier-agent/payment.d.ts +7 -0
- package/dist/verifier-agent/payment.js +17 -0
- package/docs/api.md +8 -2
- package/docs/application-api.md +25 -2
- package/docs/chain.md +155 -9
- package/docs/consumer-migrations.md +61 -0
- package/docs/encrypted-notes.md +3 -2
- package/docs/encryption.md +4 -2
- package/docs/errors.md +14 -1
- package/docs/flows.md +2 -2
- package/docs/funding.md +902 -0
- package/docs/native-approvals.md +63 -0
- package/docs/reference/README.md +9 -8
- package/docs/reference/app-browser.md +232 -0
- package/docs/reference/app-node.md +38 -8
- package/docs/reference/app.md +4621 -805
- package/docs/reference/chain-node.md +74 -16
- package/docs/reference/chain.md +6740 -1592
- package/docs/reference/committee.md +5630 -987
- package/docs/reference/main.md +1439 -140
- package/docs/reference/oid4vp.md +297 -144
- package/docs/reference/verifier-agent.md +77 -35
- package/docs/signing.md +6 -0
- package/examples/encrypted-notes.ts +21 -6
- package/examples/getting-started.ts +2 -7
- package/examples/live-config.ts +24 -17
- package/package.json +17 -5
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { type RegisteredPaidAgentSession, type RetainedPaidApproval } from '../chain/registeredPaidAgent.js';
|
|
2
|
+
import type { ApplicationStore } from './ready-slot.js';
|
|
3
|
+
import { type Rederivable } from './store-commit.js';
|
|
4
|
+
/** Ledger format carrying a generation. Anything else is legacy and never resent. */
|
|
5
|
+
export declare const APPROVAL_LEDGER_SCHEMA = 2;
|
|
6
|
+
/** Generation of a new approval's first send. */
|
|
7
|
+
export declare const FIRST_APPROVAL_GENERATION = 1;
|
|
8
|
+
/** Journal entry of one paid operator approval. */
|
|
9
|
+
export interface ApprovalJournalRecord {
|
|
10
|
+
/** The stage's original identity (stage, operator key, unsigned body). */
|
|
11
|
+
original: unknown;
|
|
12
|
+
/** `prepared` before the first send, `confirmed` once the agent accepted it. */
|
|
13
|
+
phase: 'prepared' | 'confirmed';
|
|
14
|
+
/** The retained signed request body; absent on records written by older SDK versions. */
|
|
15
|
+
approval?: RetainedPaidApproval & {
|
|
16
|
+
schema: 2;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
/** One generation-bound permit state. */
|
|
20
|
+
export type ApprovalLedgerState = {
|
|
21
|
+
state: 'permit' | 'in_flight';
|
|
22
|
+
generation: number;
|
|
23
|
+
} | {
|
|
24
|
+
state: 'legacy';
|
|
25
|
+
};
|
|
26
|
+
/** What a durable approval needs: the store (with `claim`), the session, the journal entry and cancellation. */
|
|
27
|
+
export interface DurableApprovalContext {
|
|
28
|
+
/** Application store; must implement `claim`. */
|
|
29
|
+
store: ApplicationStore;
|
|
30
|
+
/** Prepared session with the retained-approval methods. */
|
|
31
|
+
session: RegisteredPaidAgentSession;
|
|
32
|
+
/** Journal entry key of this approval stage. */
|
|
33
|
+
key: string;
|
|
34
|
+
/** Cancellation; an abort during a send leaves that generation unresolved. */
|
|
35
|
+
signal: AbortSignal;
|
|
36
|
+
/**
|
|
37
|
+
* The driving flow's re-derivable writes: a confirmation is put here (published with the flow's
|
|
38
|
+
* next durable commit), and a first approval's commit publishes what is pending. Absent: each
|
|
39
|
+
* confirmation is written on its own.
|
|
40
|
+
*/
|
|
41
|
+
rederivable?: Rederivable;
|
|
42
|
+
}
|
|
43
|
+
/** Ledger entry beside the approval's journal entry. */
|
|
44
|
+
export declare const approvalLedgerKey: (key: string) => string;
|
|
45
|
+
/** Exclusive claim record of generation `generation`. */
|
|
46
|
+
export declare const approvalClaimKey: (key: string, generation: number) => string;
|
|
47
|
+
/**
|
|
48
|
+
* Whether this store and session support durable approval resends for journal entry `key`: the store
|
|
49
|
+
* has an atomic `claim`, the session can sign without sending and send retained bytes, and every
|
|
50
|
+
* derived key fits the store's key grammar. Otherwise approvals are sent once and never resent.
|
|
51
|
+
*/
|
|
52
|
+
export declare function supportsDurableApproval(store: ApplicationStore, session: RegisteredPaidAgentSession, key: string): boolean;
|
|
53
|
+
/**
|
|
54
|
+
* The recorded permit state, or undefined when no ledger was ever written.
|
|
55
|
+
* @param store Application store.
|
|
56
|
+
* @param key Journal entry key.
|
|
57
|
+
* @param proposalId Delivery the ledger must belong to.
|
|
58
|
+
*/
|
|
59
|
+
export declare function readApprovalLedger(store: ApplicationStore, key: string, proposalId: string): Promise<ApprovalLedgerState | undefined>;
|
|
60
|
+
/**
|
|
61
|
+
* The only way to start a send of `generation`: win its exclusive claim, confirm the ledger still
|
|
62
|
+
* reads `permit(generation)`, then durably mark `in_flight(generation)`. A lost claim or a moved
|
|
63
|
+
* generation sends nothing and is unresolved.
|
|
64
|
+
* @param ctx Durable approval context.
|
|
65
|
+
* @param proposalId Delivery identity.
|
|
66
|
+
* @param generation Generation to send.
|
|
67
|
+
*/
|
|
68
|
+
export declare function beginApprovalSend(ctx: DurableApprovalContext, proposalId: string, generation: number): Promise<void>;
|
|
69
|
+
/**
|
|
70
|
+
* Sends the retained approval under the ledger's permit for `generation`, which the caller read.
|
|
71
|
+
* Before EVERY send the deadline (the approval's signed expiry) is re-read and the permit is
|
|
72
|
+
* exclusively claimed and consumed; only a 429/503 answer restores the next one, after which the
|
|
73
|
+
* SAME bytes may be sent again inside the deadline. Throws `PaidBackoffError` when waiting stops
|
|
74
|
+
* (the permit for the next generation stays durable for a later resume).
|
|
75
|
+
* @param ctx Durable approval context.
|
|
76
|
+
* @param approval The retained approval, as loaded from the store.
|
|
77
|
+
* @param generation Generation the ledger permits.
|
|
78
|
+
*/
|
|
79
|
+
export declare function deliverRetainedApproval(ctx: DurableApprovalContext, approval: RetainedPaidApproval, generation: number): Promise<void>;
|
|
80
|
+
/**
|
|
81
|
+
* First approval of a stage: signs ONCE, then, under the record's lock and only if no record exists,
|
|
82
|
+
* commits the record, its exact body and `in_flight(1)` together (one flush pair; nothing has been
|
|
83
|
+
* sent), then sends the body loaded back from the store. Returns false, sending nothing, when a
|
|
84
|
+
* record already exists.
|
|
85
|
+
* @param ctx Durable approval context.
|
|
86
|
+
* @param original The stage's original identity.
|
|
87
|
+
* @param sign Signs the approval without sending it.
|
|
88
|
+
*/
|
|
89
|
+
export declare function retainAndDeliverApproval(ctx: DurableApprovalContext, original: unknown, sign: () => Promise<RetainedPaidApproval>): Promise<boolean>;
|
|
90
|
+
/**
|
|
91
|
+
* Reconciles a retained, unconfirmed approval: reads its delivery state and resends the identical
|
|
92
|
+
* retained body only when BOTH hold: the agent still shows it `pending`, and the ledger holds a
|
|
93
|
+
* `permit` (nothing in flight; the last send, if any, was answered busy). Accepted consent is
|
|
94
|
+
* confirmed, never resent; anything else is unresolved.
|
|
95
|
+
* @param ctx Durable approval context.
|
|
96
|
+
* @param record The stage's journal record.
|
|
97
|
+
* @returns `accepted` when the agent already held it, `resent` when the identical body was accepted now.
|
|
98
|
+
*/
|
|
99
|
+
export declare function reconcileRetainedApproval(ctx: DurableApprovalContext, record: ApprovalJournalRecord): Promise<'accepted' | 'resent'>;
|
|
100
|
+
/** One first approval of a batch: its own journal entry, original identity and one-time signer. */
|
|
101
|
+
export interface BatchedApproval {
|
|
102
|
+
/** Durable approval context of this entry (its own journal key). */
|
|
103
|
+
ctx: DurableApprovalContext;
|
|
104
|
+
/** The stage's original identity. */
|
|
105
|
+
original: unknown;
|
|
106
|
+
/** Signs this approval without sending it. */
|
|
107
|
+
sign: () => Promise<RetainedPaidApproval>;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* First approvals of several stages in ONE agent request (agents advertising batch approvals).
|
|
111
|
+
* Each entry is handled exactly as {@link retainAndDeliverApproval} handles one: signed once, its
|
|
112
|
+
* record and exact body retained under its own key with one permit, then its permit claimed and
|
|
113
|
+
* marked in flight; only the send is shared. Per entry: 204 confirms; 409 is resolved by reading
|
|
114
|
+
* the delivery state (accepted confirms, anything else is unresolved); 404/410/422 are decided
|
|
115
|
+
* refusals (in flight stays, never resent); a transport failure or malformed reply leaves every
|
|
116
|
+
* entry in flight, unresolved. A busy refusal of the whole request permits each entry's next
|
|
117
|
+
* generation, and after one wait each continues alone through the single-approval route.
|
|
118
|
+
* Resolves, once every entry has settled, to whether each entry was retained (false: a record
|
|
119
|
+
* already existed and nothing was sent for it); rejects with the first entry's error in order.
|
|
120
|
+
* @param items Entries in proposal order; distinct journal keys.
|
|
121
|
+
*/
|
|
122
|
+
export declare function retainAndDeliverApprovals(items: readonly BatchedApproval[]): Promise<boolean[]>;
|
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
// Durable, generation-bound resend authority for ONE retained paid operator approval.
|
|
2
|
+
//
|
|
3
|
+
// The agent's per-client limiter answers 429 BEFORE any handler runs, and its handlers answer
|
|
4
|
+
// 429/503 before changing delivery state, so a busy refusal of an approval POST means it was not
|
|
5
|
+
// processed. Clients behind one egress address share that limiter: a burst of distinct paid users
|
|
6
|
+
// sees 429 after their TASRA is reserved. Turning that into a dead end loses a paid request, but a
|
|
7
|
+
// blind resend could approve twice or after an unknown outcome. The rules (mirroring `tasra-cli`):
|
|
8
|
+
//
|
|
9
|
+
// - The approval is signed ONCE, and its exact request body is retained in the application store
|
|
10
|
+
// BEFORE the first send. Every send, in this or any later process, sends those bytes; nothing is
|
|
11
|
+
// ever re-signed.
|
|
12
|
+
// - `<entry>.attempt` holds `permit` | `in_flight` with a generation g (schema 2). Nothing is sent
|
|
13
|
+
// for g unless `in_flight(g)` is durable first, and exactly one sender may have written it:
|
|
14
|
+
// - the FIRST send (g = 1): the record, its body and `in_flight(1)` are committed together under
|
|
15
|
+
// the record's lock by the one caller that found no record (a crash may keep any subset: a record
|
|
16
|
+
// without a ledger is never sent, a ledger without a record is replaced by a fresh first
|
|
17
|
+
// approval, nothing having been sent). No `permit(1)` exists, so nobody else can begin g = 1;
|
|
18
|
+
// - every RESEND (g > 1) must (1) win the exclusive claim record `<entry>.claim.<g>` (the store's
|
|
19
|
+
// atomic insert-if-absent), (2) re-read the ledger and still find `permit(g)`, (3) durably write
|
|
20
|
+
// `in_flight(g)`. A lost claim or a moved generation sends nothing.
|
|
21
|
+
// - Only the holder of g, on a 429/503 answer to THAT send and with the ledger still `in_flight(g)`,
|
|
22
|
+
// writes `permit(g+1)`; after its wait it must claim exactly g+1 again.
|
|
23
|
+
// - A transport error, timeout, abort, crash or any other answer leaves `in_flight(g)`: unresolved,
|
|
24
|
+
// reconciled by reading the delivery state, never by resending. No ledger, a ledger of another
|
|
25
|
+
// schema, or a record without a retained body (written by an older SDK) is never resent.
|
|
26
|
+
// - A 409 that follows a busy refusal is resolved by reading the delivery state.
|
|
27
|
+
import { PaidApprovalUnresolvedError } from '../errors.js';
|
|
28
|
+
import { PaidAgentControlError } from '../chain/registeredPaidAgent.js';
|
|
29
|
+
import { PaidBackoff } from '../chain/paidPacing.js';
|
|
30
|
+
import { saveRederivable, withCommit } from './store-commit.js';
|
|
31
|
+
/** Ledger format carrying a generation. Anything else is legacy and never resent. */
|
|
32
|
+
export const APPROVAL_LEDGER_SCHEMA = 2;
|
|
33
|
+
/** Generation of a new approval's first send. */
|
|
34
|
+
export const FIRST_APPROVAL_GENERATION = 1;
|
|
35
|
+
/** Generations are bounded so a claim key always fits the store's key grammar. */
|
|
36
|
+
const MAX_GENERATION = 1_000_000;
|
|
37
|
+
const ROUTE = 'POST /v1/sessions/{id}/execution/{delivery}/approve';
|
|
38
|
+
const KEY = /^[a-zA-Z0-9][a-zA-Z0-9._-]{0,127}$/;
|
|
39
|
+
/** Ledger entry beside the approval's journal entry. */
|
|
40
|
+
export const approvalLedgerKey = (key) => key + '.attempt';
|
|
41
|
+
/** Exclusive claim record of generation `generation`. */
|
|
42
|
+
export const approvalClaimKey = (key, generation) => key + '.claim.' + generation;
|
|
43
|
+
/**
|
|
44
|
+
* Whether this store and session support durable approval resends for journal entry `key`: the store
|
|
45
|
+
* has an atomic `claim`, the session can sign without sending and send retained bytes, and every
|
|
46
|
+
* derived key fits the store's key grammar. Otherwise approvals are sent once and never resent.
|
|
47
|
+
*/
|
|
48
|
+
export function supportsDurableApproval(store, session, key) {
|
|
49
|
+
return typeof store.claim === 'function' && session.pacing !== undefined && typeof session.prepareRequestApproval === 'function'
|
|
50
|
+
&& typeof session.prepareCredentialApproval === 'function' && typeof session.sendRetainedApproval === 'function'
|
|
51
|
+
&& typeof session.approvalState === 'function' && KEY.test(approvalLedgerKey(key)) && KEY.test(approvalClaimKey(key, MAX_GENERATION));
|
|
52
|
+
}
|
|
53
|
+
const unresolved = (ctx, reason, proposalId, generation, cause) => new PaidApprovalUnresolvedError({ reason, key: ctx.key, ...(proposalId !== undefined ? { proposalId } : {}), ...(generation !== undefined ? { generation } : {}),
|
|
54
|
+
...(cause !== undefined ? { cause } : {}) });
|
|
55
|
+
/**
|
|
56
|
+
* The recorded permit state, or undefined when no ledger was ever written.
|
|
57
|
+
* @param store Application store.
|
|
58
|
+
* @param key Journal entry key.
|
|
59
|
+
* @param proposalId Delivery the ledger must belong to.
|
|
60
|
+
*/
|
|
61
|
+
export async function readApprovalLedger(store, key, proposalId) {
|
|
62
|
+
const value = await store.load(approvalLedgerKey(key));
|
|
63
|
+
if (value === undefined)
|
|
64
|
+
return undefined;
|
|
65
|
+
if (!value || typeof value !== 'object' || value.schema !== APPROVAL_LEDGER_SCHEMA || value.proposalId !== proposalId
|
|
66
|
+
|| (value.state !== 'permit' && value.state !== 'in_flight') || !Number.isSafeInteger(value.generation)
|
|
67
|
+
|| value.generation < FIRST_APPROVAL_GENERATION || value.generation > MAX_GENERATION)
|
|
68
|
+
return { state: 'legacy' };
|
|
69
|
+
return { state: value.state, generation: value.generation };
|
|
70
|
+
}
|
|
71
|
+
async function writeLedger(store, key, proposalId, state, generation) {
|
|
72
|
+
await store.save(approvalLedgerKey(key), { schema: APPROVAL_LEDGER_SCHEMA, proposalId, state, generation });
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* The only way to start a send of `generation`: win its exclusive claim, confirm the ledger still
|
|
76
|
+
* reads `permit(generation)`, then durably mark `in_flight(generation)`. A lost claim or a moved
|
|
77
|
+
* generation sends nothing and is unresolved.
|
|
78
|
+
* @param ctx Durable approval context.
|
|
79
|
+
* @param proposalId Delivery identity.
|
|
80
|
+
* @param generation Generation to send.
|
|
81
|
+
*/
|
|
82
|
+
export async function beginApprovalSend(ctx, proposalId, generation) {
|
|
83
|
+
if (!Number.isSafeInteger(generation) || generation < FIRST_APPROVAL_GENERATION || generation > MAX_GENERATION)
|
|
84
|
+
throw unresolved(ctx, 'generation_moved', proposalId, generation);
|
|
85
|
+
if (!await ctx.store.claim(approvalClaimKey(ctx.key, generation)))
|
|
86
|
+
throw unresolved(ctx, 'claim_lost', proposalId, generation);
|
|
87
|
+
const ledger = await readApprovalLedger(ctx.store, ctx.key, proposalId);
|
|
88
|
+
if (ledger?.state !== 'permit' || ledger.generation !== generation)
|
|
89
|
+
throw unresolved(ctx, 'generation_moved', proposalId, generation);
|
|
90
|
+
await writeLedger(ctx.store, ctx.key, proposalId, 'in_flight', generation);
|
|
91
|
+
}
|
|
92
|
+
/** A 429/503 answered the send of `generation`: only while the ledger still reads `in_flight(generation)` is `permit(generation+1)` written. */
|
|
93
|
+
async function releaseBusy(ctx, proposalId, generation) {
|
|
94
|
+
const ledger = await readApprovalLedger(ctx.store, ctx.key, proposalId);
|
|
95
|
+
if (ledger?.state !== 'in_flight' || ledger.generation !== generation || generation >= MAX_GENERATION)
|
|
96
|
+
throw unresolved(ctx, 'generation_moved', proposalId, generation);
|
|
97
|
+
await writeLedger(ctx.store, ctx.key, proposalId, 'permit', generation + 1);
|
|
98
|
+
return generation + 1;
|
|
99
|
+
}
|
|
100
|
+
const accepted = (state) => state === 'approved' || state === 'dispatched' || state === 'finished';
|
|
101
|
+
/**
|
|
102
|
+
* Mark an approval the agent accepted. Re-derivable, so it pays no directory sync of its own: a
|
|
103
|
+
* crash before the next sync leaves `prepared` with `in_flight`, which is never resent; a resume
|
|
104
|
+
* that meets the stage again reads its delivery state (as for any unconfirmed approval) and
|
|
105
|
+
* confirms it, and one that has moved past the stage never reads this record again.
|
|
106
|
+
*/
|
|
107
|
+
async function confirm(ctx) {
|
|
108
|
+
const record = await ctx.store.load(ctx.key);
|
|
109
|
+
const confirmed = { original: record?.original, phase: 'confirmed' };
|
|
110
|
+
if (ctx.rederivable)
|
|
111
|
+
ctx.rederivable.put(ctx.key, confirmed);
|
|
112
|
+
else
|
|
113
|
+
await saveRederivable(ctx.store, ctx.key, confirmed);
|
|
114
|
+
}
|
|
115
|
+
/** Run a first-approval commit that also publishes the flow's pending re-derivable writes. */
|
|
116
|
+
async function commitWithPending(ctx, keys, action) {
|
|
117
|
+
const ride = ctx.rederivable?.take() ?? [];
|
|
118
|
+
try {
|
|
119
|
+
return await withCommit(ctx.store, keys, async (commit) => {
|
|
120
|
+
for (const [key, value] of ride)
|
|
121
|
+
commit.stage(key, value);
|
|
122
|
+
return action(commit);
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
catch (error) {
|
|
126
|
+
ctx.rederivable?.restore(ride);
|
|
127
|
+
throw error;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
/** The retained approval of a journal record, or undefined for a record without one (older SDK). */
|
|
131
|
+
function retainedOf(record) {
|
|
132
|
+
const a = record?.approval;
|
|
133
|
+
if (!a || typeof a !== 'object' || a.schema !== APPROVAL_LEDGER_SCHEMA)
|
|
134
|
+
return undefined;
|
|
135
|
+
return { proposalId: a.proposalId, body: a.body, expires: a.expires };
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Sends the retained approval under the ledger's permit for `generation`, which the caller read.
|
|
139
|
+
* Before EVERY send the deadline (the approval's signed expiry) is re-read and the permit is
|
|
140
|
+
* exclusively claimed and consumed; only a 429/503 answer restores the next one, after which the
|
|
141
|
+
* SAME bytes may be sent again inside the deadline. Throws `PaidBackoffError` when waiting stops
|
|
142
|
+
* (the permit for the next generation stays durable for a later resume).
|
|
143
|
+
* @param ctx Durable approval context.
|
|
144
|
+
* @param approval The retained approval, as loaded from the store.
|
|
145
|
+
* @param generation Generation the ledger permits.
|
|
146
|
+
*/
|
|
147
|
+
export function deliverRetainedApproval(ctx, approval, generation) {
|
|
148
|
+
return deliver(ctx, approval, generation, false);
|
|
149
|
+
}
|
|
150
|
+
/** `inFlight`: the caller already made `in_flight(generation)` durable as the one first sender. */
|
|
151
|
+
async function deliver(ctx, approval, generation, inFlight) {
|
|
152
|
+
const { session, signal } = ctx, pacing = session.pacing;
|
|
153
|
+
const backoff = new PaidBackoff(pacing.clock, ROUTE, approval.expires * 1000, pacing.attemptBudgetMs);
|
|
154
|
+
let current = generation, begun = inFlight;
|
|
155
|
+
for (;;) {
|
|
156
|
+
signal.throwIfAborted();
|
|
157
|
+
// Not sent yet: a refusal here leaves the permit in place (or, for a first send, `in_flight(1)`).
|
|
158
|
+
backoff.readyToSend();
|
|
159
|
+
if (!begun)
|
|
160
|
+
await beginApprovalSend(ctx, approval.proposalId, current);
|
|
161
|
+
begun = false;
|
|
162
|
+
let reply;
|
|
163
|
+
try {
|
|
164
|
+
reply = await session.sendRetainedApproval(approval, signal);
|
|
165
|
+
}
|
|
166
|
+
catch (cause) {
|
|
167
|
+
if (cause instanceof PaidAgentControlError) {
|
|
168
|
+
// An earlier busy attempt may have been accepted since: read, never assume.
|
|
169
|
+
if (cause.httpStatus === 409 && backoff.retries > 0) {
|
|
170
|
+
if (accepted(await session.approvalState(approval.proposalId, signal))) {
|
|
171
|
+
await confirm(ctx);
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
throw unresolved(ctx, 'conflict', approval.proposalId, current, cause);
|
|
175
|
+
}
|
|
176
|
+
// A decided answer: `in_flight` stays, never resent.
|
|
177
|
+
throw cause;
|
|
178
|
+
}
|
|
179
|
+
throw unresolved(ctx, 'outcome_unknown', approval.proposalId, current, cause);
|
|
180
|
+
}
|
|
181
|
+
if (reply.status === 'accepted') {
|
|
182
|
+
await confirm(ctx);
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
// THIS send was definitely refused before processing: permit the NEXT generation, which this
|
|
186
|
+
// caller must again claim exclusively after its wait.
|
|
187
|
+
current = await releaseBusy(ctx, approval.proposalId, current);
|
|
188
|
+
await backoff.wait(reply.httpStatus ?? 429, reply.hintMs, signal);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* First approval of a stage: signs ONCE, then, under the record's lock and only if no record exists,
|
|
193
|
+
* commits the record, its exact body and `in_flight(1)` together (one flush pair; nothing has been
|
|
194
|
+
* sent), then sends the body loaded back from the store. Returns false, sending nothing, when a
|
|
195
|
+
* record already exists.
|
|
196
|
+
* @param ctx Durable approval context.
|
|
197
|
+
* @param original The stage's original identity.
|
|
198
|
+
* @param sign Signs the approval without sending it.
|
|
199
|
+
*/
|
|
200
|
+
export async function retainAndDeliverApproval(ctx, original, sign) {
|
|
201
|
+
const approval = await sign();
|
|
202
|
+
ctx.signal.throwIfAborted();
|
|
203
|
+
const retained = await commitWithPending(ctx, [ctx.key], async (commit) => stageFirstApproval(ctx, commit, original, approval));
|
|
204
|
+
if (!retained)
|
|
205
|
+
return false;
|
|
206
|
+
// The bytes come from the record just retained, exactly as any later resend's do.
|
|
207
|
+
const kept = retainedOf(await ctx.store.load(ctx.key));
|
|
208
|
+
if (!kept)
|
|
209
|
+
throw unresolved(ctx, 'no_permit', approval.proposalId);
|
|
210
|
+
await deliver(ctx, kept, FIRST_APPROVAL_GENERATION, true);
|
|
211
|
+
return true;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Under the record's lock: unless a record exists, stage it with its retained body and the ledger
|
|
215
|
+
* `in_flight(1)`, published together with the unlock. A crash may keep any subset: a record without
|
|
216
|
+
* a ledger is unresolved and never sent; a ledger without a record is overwritten by the next first
|
|
217
|
+
* approval of the stage. Either way nothing was sent: the send starts after the commit resolved.
|
|
218
|
+
*/
|
|
219
|
+
async function stageFirstApproval(ctx, commit, original, approval) {
|
|
220
|
+
if (await ctx.store.load(ctx.key) !== undefined)
|
|
221
|
+
return false;
|
|
222
|
+
return stageFirst(ctx, commit, original, approval);
|
|
223
|
+
}
|
|
224
|
+
/** {@link stageFirstApproval} once the caller found no record under the held lock. */
|
|
225
|
+
function stageFirst(ctx, commit, original, approval) {
|
|
226
|
+
commit.stage(ctx.key, { original, phase: 'prepared', approval: { schema: APPROVAL_LEDGER_SCHEMA, ...approval } });
|
|
227
|
+
commit.stage(approvalLedgerKey(ctx.key), { schema: APPROVAL_LEDGER_SCHEMA, proposalId: approval.proposalId, state: 'in_flight', generation: FIRST_APPROVAL_GENERATION });
|
|
228
|
+
return true;
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Reconciles a retained, unconfirmed approval: reads its delivery state and resends the identical
|
|
232
|
+
* retained body only when BOTH hold: the agent still shows it `pending`, and the ledger holds a
|
|
233
|
+
* `permit` (nothing in flight; the last send, if any, was answered busy). Accepted consent is
|
|
234
|
+
* confirmed, never resent; anything else is unresolved.
|
|
235
|
+
* @param ctx Durable approval context.
|
|
236
|
+
* @param record The stage's journal record.
|
|
237
|
+
* @returns `accepted` when the agent already held it, `resent` when the identical body was accepted now.
|
|
238
|
+
*/
|
|
239
|
+
export async function reconcileRetainedApproval(ctx, record) {
|
|
240
|
+
const approval = retainedOf(record);
|
|
241
|
+
if (!approval)
|
|
242
|
+
throw unresolved(ctx, 'legacy_state');
|
|
243
|
+
const state = await ctx.session.approvalState(approval.proposalId, ctx.signal);
|
|
244
|
+
if (accepted(state)) {
|
|
245
|
+
await confirm(ctx);
|
|
246
|
+
return 'accepted';
|
|
247
|
+
}
|
|
248
|
+
const ledger = await readApprovalLedger(ctx.store, ctx.key, approval.proposalId);
|
|
249
|
+
if (state !== 'pending' || ledger?.state !== 'permit') {
|
|
250
|
+
const reason = ledger?.state === 'legacy' ? 'legacy_state' : ledger?.state === 'in_flight' ? 'outcome_unknown' : 'no_permit';
|
|
251
|
+
throw unresolved(ctx, reason, approval.proposalId, ledger && ledger.state !== 'legacy' ? ledger.generation : undefined);
|
|
252
|
+
}
|
|
253
|
+
await deliverRetainedApproval(ctx, approval, ledger.generation);
|
|
254
|
+
return 'resent';
|
|
255
|
+
}
|
|
256
|
+
const BATCH_ROUTE = 'POST /v1/sessions/{id}/execution/approve';
|
|
257
|
+
/**
|
|
258
|
+
* First approvals of several stages in ONE agent request (agents advertising batch approvals).
|
|
259
|
+
* Each entry is handled exactly as {@link retainAndDeliverApproval} handles one: signed once, its
|
|
260
|
+
* record and exact body retained under its own key with one permit, then its permit claimed and
|
|
261
|
+
* marked in flight; only the send is shared. Per entry: 204 confirms; 409 is resolved by reading
|
|
262
|
+
* the delivery state (accepted confirms, anything else is unresolved); 404/410/422 are decided
|
|
263
|
+
* refusals (in flight stays, never resent); a transport failure or malformed reply leaves every
|
|
264
|
+
* entry in flight, unresolved. A busy refusal of the whole request permits each entry's next
|
|
265
|
+
* generation, and after one wait each continues alone through the single-approval route.
|
|
266
|
+
* Resolves, once every entry has settled, to whether each entry was retained (false: a record
|
|
267
|
+
* already existed and nothing was sent for it); rejects with the first entry's error in order.
|
|
268
|
+
* @param items Entries in proposal order; distinct journal keys.
|
|
269
|
+
*/
|
|
270
|
+
export async function retainAndDeliverApprovals(items) {
|
|
271
|
+
if (items.length === 0)
|
|
272
|
+
return [];
|
|
273
|
+
const session = items[0].ctx.session, signal = items[0].ctx.signal;
|
|
274
|
+
if (new Set(items.map(i => i.ctx.key)).size !== items.length || items.some(i => i.ctx.session !== session))
|
|
275
|
+
throw new Error('Invalid approval batch');
|
|
276
|
+
// Signed before anything is retained: a signing failure retains and sends nothing.
|
|
277
|
+
const approvals = await Promise.all(items.map(i => i.sign()));
|
|
278
|
+
signal.throwIfAborted();
|
|
279
|
+
const pacing = session.pacing;
|
|
280
|
+
const backoff = new PaidBackoff(pacing.clock, BATCH_ROUTE, Math.min(...approvals.map(a => a.expires)) * 1000, pacing.attemptBudgetMs);
|
|
281
|
+
// Not sent yet: a refusal here retains and sends nothing.
|
|
282
|
+
backoff.readyToSend();
|
|
283
|
+
const outcome = items.map(() => ({ ok: true, value: false }));
|
|
284
|
+
const live = [];
|
|
285
|
+
// Every entry's record, body and `in_flight(1)` in ONE commit under all of their locks (taken
|
|
286
|
+
// in sorted order), each exactly as a single first approval stages them.
|
|
287
|
+
const store = items[0].ctx.store;
|
|
288
|
+
if (items.some(i => i.ctx.store !== store))
|
|
289
|
+
throw new Error('Invalid approval batch');
|
|
290
|
+
const retained = await commitWithPending(items[0].ctx, items.map(i => i.ctx.key), async (commit) => {
|
|
291
|
+
// Every record is read under the held locks at once; they are staged in proposal order.
|
|
292
|
+
const existing = await Promise.all(items.map(item => item.ctx.store.load(item.ctx.key)));
|
|
293
|
+
return items.map((item, index) => existing[index] === undefined && stageFirst(item.ctx, commit, item.original, approvals[index]));
|
|
294
|
+
});
|
|
295
|
+
await Promise.all(items.map(async (item, index) => {
|
|
296
|
+
const approval = approvals[index];
|
|
297
|
+
try {
|
|
298
|
+
if (!retained[index])
|
|
299
|
+
return;
|
|
300
|
+
outcome[index] = { ok: true, value: true };
|
|
301
|
+
const kept = retainedOf(await item.ctx.store.load(item.ctx.key));
|
|
302
|
+
if (!kept)
|
|
303
|
+
throw unresolved(item.ctx, 'no_permit', approval.proposalId);
|
|
304
|
+
live.push({ index, approval: kept });
|
|
305
|
+
}
|
|
306
|
+
catch (error) {
|
|
307
|
+
outcome[index] = { ok: false, error };
|
|
308
|
+
}
|
|
309
|
+
}));
|
|
310
|
+
live.sort((a, b) => a.index - b.index);
|
|
311
|
+
if (live.length > 0) {
|
|
312
|
+
let reply, failure;
|
|
313
|
+
try {
|
|
314
|
+
reply = await session.sendRetainedApprovals(live.map(l => l.approval), signal);
|
|
315
|
+
}
|
|
316
|
+
catch (cause) {
|
|
317
|
+
failure = cause;
|
|
318
|
+
}
|
|
319
|
+
if (reply === undefined) {
|
|
320
|
+
// Unknown outcome for every entry of the request: in flight stays, nothing is resent.
|
|
321
|
+
for (const l of live)
|
|
322
|
+
outcome[l.index] = { ok: false, error: unresolved(items[l.index].ctx, 'outcome_unknown', l.approval.proposalId, FIRST_APPROVAL_GENERATION, failure) };
|
|
323
|
+
}
|
|
324
|
+
else if (reply.kind === 'busy') {
|
|
325
|
+
// Refused before processing: each entry's next generation is permitted; after the wait each
|
|
326
|
+
// continues alone under the single route's permit rules.
|
|
327
|
+
const next = await Promise.all(live.map(l => releaseBusy(items[l.index].ctx, l.approval.proposalId, FIRST_APPROVAL_GENERATION).then(g => ({ g }), (error) => ({ error }))));
|
|
328
|
+
let waited;
|
|
329
|
+
try {
|
|
330
|
+
await backoff.wait(reply.httpStatus, reply.hintMs, signal);
|
|
331
|
+
}
|
|
332
|
+
catch (error) {
|
|
333
|
+
waited = error;
|
|
334
|
+
}
|
|
335
|
+
await Promise.all(live.map(async (l, i) => {
|
|
336
|
+
const n = next[i];
|
|
337
|
+
if ('error' in n) {
|
|
338
|
+
outcome[l.index] = { ok: false, error: n.error };
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
if (waited !== undefined) {
|
|
342
|
+
outcome[l.index] = { ok: false, error: waited };
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
try {
|
|
346
|
+
await deliverRetainedApproval(items[l.index].ctx, l.approval, n.g);
|
|
347
|
+
}
|
|
348
|
+
catch (error) {
|
|
349
|
+
outcome[l.index] = { ok: false, error };
|
|
350
|
+
}
|
|
351
|
+
}));
|
|
352
|
+
}
|
|
353
|
+
else {
|
|
354
|
+
await Promise.all(live.map(async (l) => {
|
|
355
|
+
const ctx = items[l.index].ctx, status = reply.results.get(l.approval.proposalId);
|
|
356
|
+
try {
|
|
357
|
+
if (status === 204)
|
|
358
|
+
await confirm(ctx);
|
|
359
|
+
else if (status === 409) {
|
|
360
|
+
// Already decided: read it, never assume.
|
|
361
|
+
if (accepted(await session.approvalState(l.approval.proposalId, signal)))
|
|
362
|
+
await confirm(ctx);
|
|
363
|
+
else
|
|
364
|
+
throw unresolved(ctx, 'conflict', l.approval.proposalId, FIRST_APPROVAL_GENERATION, new PaidAgentControlError(session.sessionId, 409));
|
|
365
|
+
}
|
|
366
|
+
else
|
|
367
|
+
throw new PaidAgentControlError(session.sessionId, status ?? 0);
|
|
368
|
+
}
|
|
369
|
+
catch (error) {
|
|
370
|
+
outcome[l.index] = { ok: false, error };
|
|
371
|
+
}
|
|
372
|
+
}));
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
for (const o of outcome)
|
|
376
|
+
if (!o.ok)
|
|
377
|
+
throw o.error;
|
|
378
|
+
return outcome.map(o => o.value);
|
|
379
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { Hex } from 'viem';
|
|
2
|
+
import { type PaymentIntentWire } from '../committee/payment.js';
|
|
3
|
+
import type { PaidReservationCheckpoint, PaidReservationRecord, ReservePaidOperationOptions } from '../chain/reservation.js';
|
|
4
|
+
import type { ApplicationStore } from './ready-slot.js';
|
|
5
|
+
/** Private session preimage retained with a reservation for recovery after process loss. */
|
|
6
|
+
export interface PaidReservationSession {
|
|
7
|
+
/** Fixed 32-byte presentation random committed before reservation. */
|
|
8
|
+
random: Hex;
|
|
9
|
+
/** Independently approved verifier-agent audience committed before reservation. */
|
|
10
|
+
audience: string;
|
|
11
|
+
}
|
|
12
|
+
/** Durable reservation evidence; its phase never substitutes for fresh canonical chain checks. */
|
|
13
|
+
export interface ApplicationReservationJournal {
|
|
14
|
+
/** Storage schema. */
|
|
15
|
+
schemaVersion: 1;
|
|
16
|
+
/** Last durably saved checkpoint. */
|
|
17
|
+
phase: PaidReservationCheckpoint['phase'];
|
|
18
|
+
/** Original transaction, signed fee quote and creator intent. */
|
|
19
|
+
record: PaidReservationRecord;
|
|
20
|
+
/** Original random and audience; keep this journal private. */
|
|
21
|
+
session: PaidReservationSession;
|
|
22
|
+
/** Locally signed hash journaled before broadcast, or the RPC-reported hash; absence does not prove no submission occurred. */
|
|
23
|
+
transactionHash?: Hex;
|
|
24
|
+
/** Observed inclusion block at confirmation; it may later reorganize. */
|
|
25
|
+
blockHash?: Hex;
|
|
26
|
+
}
|
|
27
|
+
/** Persistent callback adapter for reservePaidOperation, with immutable recovery identity. */
|
|
28
|
+
export interface PaidReservationJournal {
|
|
29
|
+
/** Locally derived operation identity. */
|
|
30
|
+
readonly requestId: Hex;
|
|
31
|
+
/** Callback that durably records each checkpoint before allowing submission to continue. */
|
|
32
|
+
readonly persist: ReservePaidOperationOptions['persist'];
|
|
33
|
+
/** Read the original saved evidence, including after failures or process restart. */
|
|
34
|
+
load(): Promise<ApplicationReservationJournal | undefined>;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Load retained private evidence by request ID without transmitting or resubmitting anything.
|
|
38
|
+
* Reverify the original transaction and reservation on chain before acting on a saved phase.
|
|
39
|
+
* @param store - Durable private application store with atomic reads/writes.
|
|
40
|
+
* @param requestId - Original operation identity, never a replacement nonce.
|
|
41
|
+
*/
|
|
42
|
+
export declare function loadPaidReservationJournal(store: ApplicationStore, requestId: Hex): Promise<ApplicationReservationJournal | undefined>;
|
|
43
|
+
/**
|
|
44
|
+
* Persist reservation checkpoints and their session preimage using the application's store.
|
|
45
|
+
* Preparation exclusively creates its request ID; every existing record refuses a new attempt.
|
|
46
|
+
* For coordinated gas payer nonces and recovery, use reserveApplicationPayment instead.
|
|
47
|
+
* @param store - Durable private store with locks shared by all participating processes/tabs.
|
|
48
|
+
* @param original - Fixed creator intent supplied to reservePaidOperation.
|
|
49
|
+
* @param originalSession - Random and approved audience committed by that intent.
|
|
50
|
+
*/
|
|
51
|
+
export declare function createPaidReservationJournal(store: ApplicationStore, original: PaymentIntentWire, originalSession: PaidReservationSession): PaidReservationJournal;
|