@emilia-protocol/gate 0.13.0 → 0.15.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 (57) hide show
  1. package/CHANGELOG.md +53 -0
  2. package/README.md +89 -0
  3. package/aeb-consumption-store.js +3 -0
  4. package/dist/aeb-consumption-store.d.ts +80 -0
  5. package/dist/aeb-consumption-store.d.ts.map +1 -0
  6. package/dist/aeb-consumption-store.js +430 -0
  7. package/dist/aeb-consumption-store.js.map +1 -0
  8. package/dist/capability-receipt.d.ts +8 -0
  9. package/dist/capability-receipt.d.ts.map +1 -1
  10. package/dist/capability-receipt.js +17 -0
  11. package/dist/capability-receipt.js.map +1 -1
  12. package/dist/index.d.ts +13 -3
  13. package/dist/index.d.ts.map +1 -1
  14. package/dist/index.js +17 -4
  15. package/dist/index.js.map +1 -1
  16. package/dist/proposal-to-effect-postgres.d.ts +129 -0
  17. package/dist/proposal-to-effect-postgres.d.ts.map +1 -0
  18. package/dist/proposal-to-effect-postgres.js +1511 -0
  19. package/dist/proposal-to-effect-postgres.js.map +1 -0
  20. package/dist/proposal-to-effect-status.d.ts +55 -0
  21. package/dist/proposal-to-effect-status.d.ts.map +1 -0
  22. package/dist/proposal-to-effect-status.js +280 -0
  23. package/dist/proposal-to-effect-status.js.map +1 -0
  24. package/dist/proposal-to-effect.d.ts +294 -0
  25. package/dist/proposal-to-effect.d.ts.map +1 -0
  26. package/dist/proposal-to-effect.js +970 -0
  27. package/dist/proposal-to-effect.js.map +1 -0
  28. package/dist/remedy-case-set-postgres.d.ts +73 -0
  29. package/dist/remedy-case-set-postgres.d.ts.map +1 -0
  30. package/dist/remedy-case-set-postgres.js +846 -0
  31. package/dist/remedy-case-set-postgres.js.map +1 -0
  32. package/dist/remedy-case-set.d.ts +53 -0
  33. package/dist/remedy-case-set.d.ts.map +1 -0
  34. package/dist/remedy-case-set.js +571 -0
  35. package/dist/remedy-case-set.js.map +1 -0
  36. package/dist/remedy-program-adapters.d.ts +137 -0
  37. package/dist/remedy-program-adapters.d.ts.map +1 -0
  38. package/dist/remedy-program-adapters.js +590 -0
  39. package/dist/remedy-program-adapters.js.map +1 -0
  40. package/dist/trust-program-revocation.js.map +1 -1
  41. package/package.json +280 -63
  42. package/proposal-to-effect-postgres.js +3 -0
  43. package/proposal-to-effect-status.js +2 -0
  44. package/proposal-to-effect.js +4 -0
  45. package/remedy-case-set-postgres.js +3 -0
  46. package/remedy-case-set.js +3 -0
  47. package/remedy-program-adapters.js +3 -0
  48. package/src/aeb-consumption-store.ts +521 -0
  49. package/src/capability-receipt.ts +17 -0
  50. package/src/index.ts +57 -3
  51. package/src/proposal-to-effect-postgres.ts +1740 -0
  52. package/src/proposal-to-effect-status.ts +357 -0
  53. package/src/proposal-to-effect.ts +1276 -0
  54. package/src/remedy-case-set-postgres.ts +1008 -0
  55. package/src/remedy-case-set.ts +603 -0
  56. package/src/remedy-program-adapters.ts +657 -0
  57. package/src/trust-program-revocation.ts +1 -1
package/CHANGELOG.md CHANGED
@@ -4,6 +4,59 @@
4
4
  All notable changes to `@emilia-protocol/gate` are documented here.
5
5
  This package follows [Semantic Versioning](https://semver.org/).
6
6
 
7
+ ## 0.15.0 (2026-07-22)
8
+
9
+ ### Added
10
+
11
+ - `./proposal-to-effect`, an orchestration profile over CAID, the existing
12
+ `EP-APPROVAL-v1` acquisition rail, signed AEB evaluation, Gate authorization,
13
+ durable operation reservation, exact effect invocation, and authenticated
14
+ reconciliation.
15
+ - An executable end-to-end example and profile vectors covering exact-action
16
+ mutation, stale evidence, one-time replay, indeterminate effects, and
17
+ post-effect commit failure.
18
+ - `./aeb-consumption-store`, a tenant- and relying-party-namespaced PostgreSQL
19
+ store that atomically fences the AEB operation and all native replay units,
20
+ with opaque owner tokens and authorized restart recovery.
21
+ - `./proposal-to-effect-status`, which verifies server-resolved
22
+ `EP-STATUS-v1` heads and separately requires authenticated local consumption
23
+ state before the atomic execution reservation.
24
+ - `./proposal-to-effect-postgres`, a private-schema PostgreSQL store for
25
+ consequence attempts with HMAC-hidden owner capabilities, tenant/provider
26
+ namespaces, owner-fenced transitions, immutable terminal states, exact
27
+ provider-evidence reconciliation, database leases, disjoint executor and
28
+ recovery roles, tenant-principal bindings, and stale-only restart recovery.
29
+ - Concrete Remedy Program evidence adapters for signed disputes,
30
+ authorizations, provider outcomes, Action Escrow state, and late revocation.
31
+ - A heterogeneous remedy case-set coordinator that completes only after every
32
+ exact child state and signed remedy receipt verifies.
33
+ - A PostgreSQL remedy case-set store with tenant RLS, immutable manifests,
34
+ append-only state events, database-clock custody, and owner/revision CAS.
35
+
36
+ ### Security
37
+
38
+ - A proposal is explicitly non-authoritative and carries no signature, permit,
39
+ or presenter-controlled authorization decision.
40
+ - AEB and Gate make independent, fail-closed decisions over the same operation,
41
+ initiator, CAID, normalized action, and relying-party pins.
42
+ - Once an effect may have executed, failed bookkeeping cannot release its AEB
43
+ reservation. The operation remains frozen until authenticated provider
44
+ evidence proves `COMMITTED` or `NOT_COMMITTED`.
45
+ - A committed effect consumes AEB replay authority before its consequence row
46
+ becomes terminal. `repairAeb` converges legacy or crash-window terminal rows
47
+ without invoking an effect, and opaque attempt-owner capabilities are kept
48
+ out of enumerable results and errors.
49
+ - A recovered worker receives a rotated owner capability; the stale worker can
50
+ no longer transition or reconcile the attempt. An in-flight attempt is
51
+ conservatively recovered as `INDETERMINATE`, never retried as unexecuted.
52
+ - AEB production consumption state is RPC-only behind tenant-bound, no-bypass
53
+ executor and recovery roles with physically separate pools. Supabase
54
+ `service_role` and both runtime roles have no direct table authority;
55
+ in-memory stores remain test-only.
56
+ - Remedy case-set state and append-only history are likewise RPC-only behind a
57
+ tenant-bound no-bypass executor; generic service credentials cannot rewrite
58
+ current or historical remedy state.
59
+
7
60
  ## 0.13.0 (2026-07-20)
8
61
 
9
62
  ### Added
package/README.md CHANGED
@@ -59,6 +59,95 @@ if (!out.ok) throw out.body; // 428 Receipt Required
59
59
  console.log(out.packet.verdict); // "rely"
60
60
  ```
61
61
 
62
+ ## Proposal to effect
63
+
64
+ `@emilia-protocol/gate/proposal-to-effect` closes the loop from an agent's
65
+ proposal to a controlled consequence without minting another authorization
66
+ format. The proposal is an unsigned, short-lived request; authority remains in
67
+ `EP-RECEIPT-v1` and the relying party's pinned AEB requirement.
68
+
69
+ ```js
70
+ import { createProposalToEffect } from '@emilia-protocol/gate/proposal-to-effect';
71
+ import { createProposalToEffectPostgresStore } from '@emilia-protocol/gate/proposal-to-effect-postgres';
72
+
73
+ const controller = createProposalToEffect({
74
+ gate,
75
+ proposal_integrity: { hmac_sha256_key: serverHeld32ByteKey },
76
+ consequence: {
77
+ tenant_id: tenantId,
78
+ provider_id: providerId,
79
+ provider_account_id: providerAccountId,
80
+ environment,
81
+ executor_id: executorId,
82
+ store: durableConsequenceAttemptStore,
83
+ },
84
+ profiles: {
85
+ 'payment-release': {
86
+ id: 'payment-release',
87
+ action_type: 'payment.release',
88
+ selector: { protocol: 'mcp', tool: 'release_payment' },
89
+ required_fields: [
90
+ 'action_type', 'amount_usd', 'currency',
91
+ 'payment_instruction_id', 'beneficiary_account_hash',
92
+ ],
93
+ authorization: {
94
+ authorization_endpoint: 'https://authorize.example/v1/approvals',
95
+ flow: 'EP-APPROVAL-v1',
96
+ },
97
+ aeb_requirement_ref: 'requirement:payment-release',
98
+ ttl_sec: 300,
99
+ canonicalize_action: deriveCanonicalPaymentActionAndCaid,
100
+ },
101
+ },
102
+ aeb: {
103
+ config: pinnedAebConfig,
104
+ adapters: pinnedAebAdapters,
105
+ store: durableOperationStore,
106
+ resolve_artifacts,
107
+ currentStatusResolver,
108
+ statusVerifier,
109
+ verify_provider_evidence,
110
+ },
111
+ });
112
+
113
+ const proposal = controller.prepare({
114
+ proposal_id, profile_id: 'payment-release', operation_id,
115
+ initiator_id: authenticatedAgentId, action: paymentFromSystemOfRecord,
116
+ });
117
+
118
+ const result = await controller.execute(
119
+ { proposal, receipt, evaluation: signedAebEvaluation },
120
+ ({ action }) => paymentProvider.release(action),
121
+ );
122
+ ```
123
+
124
+ The controller re-verifies the signed AEB evaluation, runs Gate policy,
125
+ reserves the operation before invoking the effect, and consumes it after
126
+ success. An uncertain provider result remains reserved; only authenticated
127
+ provider evidence bound to the same operation, CAID, and action digest can
128
+ reconcile it. Owner capabilities are not serialized in results or errors;
129
+ same-process services retrieve them with `getReconciliationHandle(object)`,
130
+ while restarted services use the PostgreSQL store's authorized recovery path.
131
+ Production PostgreSQL wiring uses different executor and recovery pools. Each
132
+ database login is explicitly bound to a tenant and receives only its matching
133
+ non-login group role; `service_role` receives neither RPC surface. Recovery is
134
+ possible only after the database lease is stale, rotates the owner capability,
135
+ and preserves `INDETERMINATE` whenever provider execution may have started.
136
+ `repairAeb()` converges a durable terminal attempt with a stranded AEB
137
+ reservation without invoking the effect again. See
138
+ [`docs/protocol/proposal-to-effect-profile-v1.md`](../../docs/protocol/proposal-to-effect-profile-v1.md)
139
+ and run `node examples/proposal-to-effect/demo.mjs` from the repository root.
140
+
141
+ The AEB consumption store has its own disjoint PostgreSQL credentials:
142
+ `createPostgresAebDurableConsumptionStore()` requires an `ep_aeb_executor`
143
+ pool and a physically distinct `ep_aeb_recovery` pool. Each login must be
144
+ listed for the tenant in `ep_aeb_private.tenant_principals`. Neither runtime
145
+ role receives table privileges; reserve, commit, release, and recovery claim
146
+ are narrow security-definer functions. Supabase `service_role` receives no
147
+ table, schema, or function authority. Remedy case sets use the same custody
148
+ shape through a tenant-bound `ep_remedy_executor` login and
149
+ `ep_remedy_private.tenant_principals`.
150
+
62
151
  ## Three-plane deployment
63
152
 
64
153
  High-consequence infrastructure separates three jobs instead of asking one
@@ -0,0 +1,3 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ export * from './dist/aeb-consumption-store.js';
3
+ export { default } from './dist/aeb-consumption-store.js';
@@ -0,0 +1,80 @@
1
+ import type { AebDurableConsumptionStore } from '@emilia-protocol/verify/aeb-adapter-contract';
2
+ export declare const AEB_PG_CONSUMPTION_STORE_VERSION = "EP-GATE-AEB-PG-CONSUMPTION-v1";
3
+ export declare const AEB_CONSUMPTION_OPERATION_TABLE = "ep_aeb_consumption_operations";
4
+ export declare const AEB_CONSUMPTION_REPLAY_TABLE = "ep_aeb_consumption_replay_fences";
5
+ export declare const AEB_CONSUMPTION_EXECUTOR_ROLE = "ep_aeb_executor";
6
+ export declare const AEB_CONSUMPTION_RECOVERY_ROLE = "ep_aeb_recovery";
7
+ export declare const AEB_CONSUMPTION_OWNER_ROLE = "ep_aeb_store_owner";
8
+ /** Exact schema required by createPostgresAebDurableConsumptionStore(). */
9
+ export declare const AEB_CONSUMPTION_DDL = "CREATE TABLE IF NOT EXISTS ep_aeb_consumption_operations (\n tenant_id TEXT NOT NULL CHECK (octet_length(tenant_id) BETWEEN 1 AND 512),\n relying_party_id TEXT NOT NULL CHECK (octet_length(relying_party_id) BETWEEN 1 AND 512),\n operation_key TEXT NOT NULL CHECK (octet_length(operation_key) BETWEEN 1 AND 4096),\n state TEXT NOT NULL CHECK (state IN ('RESERVED', 'CONSUMED')),\n owner_token TEXT NULL CHECK (owner_token IS NULL OR octet_length(owner_token) BETWEEN 16 AND 512),\n reserved_at TIMESTAMPTZ NOT NULL DEFAULT transaction_timestamp(),\n consumed_at TIMESTAMPTZ NULL,\n PRIMARY KEY (tenant_id, relying_party_id, operation_key),\n CHECK (\n (state = 'RESERVED' AND owner_token IS NOT NULL AND consumed_at IS NULL)\n OR (state = 'CONSUMED' AND owner_token IS NULL AND consumed_at IS NOT NULL)\n )\n);\nCREATE TABLE IF NOT EXISTS ep_aeb_consumption_replay_fences (\n tenant_id TEXT NOT NULL CHECK (octet_length(tenant_id) BETWEEN 1 AND 512),\n relying_party_id TEXT NOT NULL CHECK (octet_length(relying_party_id) BETWEEN 1 AND 512),\n replay_key TEXT NOT NULL CHECK (octet_length(replay_key) BETWEEN 1 AND 4096),\n operation_key TEXT NOT NULL CHECK (octet_length(operation_key) BETWEEN 1 AND 4096),\n reserved_at TIMESTAMPTZ NOT NULL DEFAULT transaction_timestamp(),\n PRIMARY KEY (tenant_id, relying_party_id, replay_key),\n FOREIGN KEY (tenant_id, relying_party_id, operation_key)\n REFERENCES ep_aeb_consumption_operations (tenant_id, relying_party_id, operation_key)\n ON DELETE CASCADE\n);\nCREATE INDEX IF NOT EXISTS ep_aeb_consumption_replay_fences_operation_idx\n ON ep_aeb_consumption_replay_fences (tenant_id, relying_party_id, operation_key);\nDO $roles$\nBEGIN\n IF NOT EXISTS (SELECT 1 FROM pg_catalog.pg_roles WHERE rolname = 'ep_aeb_executor') THEN\n CREATE ROLE ep_aeb_executor NOLOGIN\n NOSUPERUSER NOCREATEDB NOCREATEROLE NOREPLICATION NOBYPASSRLS;\n END IF;\n IF NOT EXISTS (SELECT 1 FROM pg_catalog.pg_roles WHERE rolname = 'ep_aeb_recovery') THEN\n CREATE ROLE ep_aeb_recovery NOLOGIN\n NOSUPERUSER NOCREATEDB NOCREATEROLE NOREPLICATION NOBYPASSRLS;\n END IF;\n IF NOT EXISTS (SELECT 1 FROM pg_catalog.pg_roles WHERE rolname = 'ep_aeb_store_owner') THEN\n CREATE ROLE ep_aeb_store_owner NOLOGIN\n NOSUPERUSER NOCREATEDB NOCREATEROLE NOREPLICATION NOBYPASSRLS;\n END IF;\nEND\n$roles$;\nGRANT ep_aeb_store_owner TO CURRENT_USER;\nCREATE SCHEMA IF NOT EXISTS ep_aeb_private;\nREVOKE ALL ON SCHEMA ep_aeb_private FROM PUBLIC, anon, authenticated, service_role;\nCREATE TABLE IF NOT EXISTS ep_aeb_private.tenant_principals (\n principal_name NAME NOT NULL,\n tenant_id TEXT NOT NULL CHECK (octet_length(tenant_id) BETWEEN 1 AND 512),\n can_execute BOOLEAN NOT NULL DEFAULT FALSE,\n can_recover BOOLEAN NOT NULL DEFAULT FALSE,\n PRIMARY KEY (principal_name, tenant_id),\n CHECK (can_execute OR can_recover)\n);\nALTER SCHEMA ep_aeb_private OWNER TO ep_aeb_store_owner;\nALTER TABLE ep_aeb_private.tenant_principals OWNER TO ep_aeb_store_owner;\nALTER TABLE ep_aeb_consumption_operations OWNER TO ep_aeb_store_owner;\nALTER TABLE ep_aeb_consumption_replay_fences OWNER TO ep_aeb_store_owner;\nALTER TABLE ep_aeb_private.tenant_principals ENABLE ROW LEVEL SECURITY;\nALTER TABLE ep_aeb_private.tenant_principals FORCE ROW LEVEL SECURITY;\nALTER TABLE ep_aeb_consumption_operations ENABLE ROW LEVEL SECURITY;\nALTER TABLE ep_aeb_consumption_operations FORCE ROW LEVEL SECURITY;\nALTER TABLE ep_aeb_consumption_replay_fences ENABLE ROW LEVEL SECURITY;\nALTER TABLE ep_aeb_consumption_replay_fences FORCE ROW LEVEL SECURITY;\nDROP POLICY IF EXISTS ep_aeb_principals_owner_only ON ep_aeb_private.tenant_principals;\nCREATE POLICY ep_aeb_principals_owner_only ON ep_aeb_private.tenant_principals\n TO ep_aeb_store_owner USING (TRUE) WITH CHECK (TRUE);\nDROP POLICY IF EXISTS ep_aeb_operations_owner_only ON ep_aeb_consumption_operations;\nCREATE POLICY ep_aeb_operations_owner_only ON ep_aeb_consumption_operations\n TO ep_aeb_store_owner USING (TRUE) WITH CHECK (TRUE);\nDROP POLICY IF EXISTS ep_aeb_replay_owner_only ON ep_aeb_consumption_replay_fences;\nCREATE POLICY ep_aeb_replay_owner_only ON ep_aeb_consumption_replay_fences\n TO ep_aeb_store_owner USING (TRUE) WITH CHECK (TRUE);\nREVOKE ALL ON ep_aeb_private.tenant_principals FROM PUBLIC, anon, authenticated, service_role,\n ep_aeb_executor, ep_aeb_recovery;\nREVOKE ALL ON ep_aeb_consumption_operations FROM PUBLIC, anon, authenticated, service_role,\n ep_aeb_executor, ep_aeb_recovery;\nREVOKE ALL ON ep_aeb_consumption_replay_fences FROM PUBLIC, anon, authenticated, service_role,\n ep_aeb_executor, ep_aeb_recovery;\nCREATE OR REPLACE FUNCTION ep_aeb_private.assert_tenant_principal(\n p_tenant_id TEXT, p_recovery BOOLEAN\n) RETURNS VOID\nLANGUAGE plpgsql VOLATILE SECURITY DEFINER SET search_path = ''\nAS $fn$\nDECLARE v_role_ok BOOLEAN; v_binding_ok BOOLEAN;\nBEGIN\n v_role_ok := CASE WHEN p_recovery\n THEN pg_catalog.pg_has_role(SESSION_USER, 'ep_aeb_recovery', 'MEMBER')\n ELSE pg_catalog.pg_has_role(SESSION_USER, 'ep_aeb_executor', 'MEMBER')\n END;\n SELECT EXISTS (\n SELECT 1 FROM ep_aeb_private.tenant_principals AS principals\n WHERE principals.principal_name = SESSION_USER\n AND principals.tenant_id = p_tenant_id\n AND CASE WHEN p_recovery THEN principals.can_recover ELSE principals.can_execute END\n ) INTO v_binding_ok;\n IF v_role_ok IS NOT TRUE OR v_binding_ok IS NOT TRUE THEN\n RAISE EXCEPTION 'AEB_TENANT_PRINCIPAL_REFUSED' USING ERRCODE = '42501';\n END IF;\nEND\n$fn$;\nCREATE OR REPLACE FUNCTION ep_aeb_private.reserve_operation(\n p_tenant_id TEXT, p_relying_party_id TEXT, p_operation_key TEXT, p_owner_token TEXT\n) RETURNS TABLE(operation_key TEXT)\nLANGUAGE plpgsql SECURITY DEFINER SET search_path = ''\nAS $fn$\nBEGIN\n PERFORM ep_aeb_private.assert_tenant_principal(p_tenant_id, FALSE);\n RETURN QUERY INSERT INTO public.ep_aeb_consumption_operations\n (tenant_id, relying_party_id, operation_key, state, owner_token)\n VALUES (p_tenant_id, p_relying_party_id, p_operation_key, 'RESERVED', p_owner_token)\n ON CONFLICT ON CONSTRAINT ep_aeb_consumption_operations_pkey DO NOTHING\n RETURNING ep_aeb_consumption_operations.operation_key;\nEND\n$fn$;\nCREATE OR REPLACE FUNCTION ep_aeb_private.reserve_replay_keys(\n p_tenant_id TEXT, p_relying_party_id TEXT, p_operation_key TEXT, p_replay_keys TEXT[]\n) RETURNS TABLE(replay_key TEXT)\nLANGUAGE plpgsql SECURITY DEFINER SET search_path = ''\nAS $fn$\nBEGIN\n PERFORM ep_aeb_private.assert_tenant_principal(p_tenant_id, FALSE);\n RETURN QUERY INSERT INTO public.ep_aeb_consumption_replay_fences\n (tenant_id, relying_party_id, replay_key, operation_key)\n SELECT p_tenant_id, p_relying_party_id, requested.replay_key, p_operation_key\n FROM pg_catalog.unnest(p_replay_keys) AS requested(replay_key)\n ORDER BY requested.replay_key\n ON CONFLICT ON CONSTRAINT ep_aeb_consumption_replay_fences_pkey DO NOTHING\n RETURNING ep_aeb_consumption_replay_fences.replay_key;\nEND\n$fn$;\nCREATE OR REPLACE FUNCTION ep_aeb_private.commit_operation(\n p_tenant_id TEXT, p_relying_party_id TEXT, p_operation_key TEXT, p_owner_token TEXT\n) RETURNS TABLE(operation_key TEXT)\nLANGUAGE plpgsql SECURITY DEFINER SET search_path = ''\nAS $fn$\nBEGIN\n PERFORM ep_aeb_private.assert_tenant_principal(p_tenant_id, FALSE);\n RETURN QUERY UPDATE public.ep_aeb_consumption_operations\n SET state = 'CONSUMED', owner_token = NULL, consumed_at = pg_catalog.transaction_timestamp()\n WHERE tenant_id = p_tenant_id AND relying_party_id = p_relying_party_id\n AND ep_aeb_consumption_operations.operation_key = p_operation_key\n AND state = 'RESERVED' AND owner_token = p_owner_token\n RETURNING ep_aeb_consumption_operations.operation_key;\nEND\n$fn$;\nCREATE OR REPLACE FUNCTION ep_aeb_private.claim_operation(\n p_tenant_id TEXT, p_relying_party_id TEXT, p_operation_key TEXT, p_owner_token TEXT\n) RETURNS TABLE(operation_key TEXT)\nLANGUAGE plpgsql SECURITY DEFINER SET search_path = ''\nAS $fn$\nBEGIN\n PERFORM ep_aeb_private.assert_tenant_principal(p_tenant_id, TRUE);\n RETURN QUERY UPDATE public.ep_aeb_consumption_operations\n SET owner_token = p_owner_token\n WHERE tenant_id = p_tenant_id AND relying_party_id = p_relying_party_id\n AND ep_aeb_consumption_operations.operation_key = p_operation_key\n AND state = 'RESERVED'\n RETURNING ep_aeb_consumption_operations.operation_key;\nEND\n$fn$;\nCREATE OR REPLACE FUNCTION ep_aeb_private.release_operation(\n p_tenant_id TEXT, p_relying_party_id TEXT, p_operation_key TEXT, p_owner_token TEXT\n) RETURNS TABLE(operation_key TEXT)\nLANGUAGE plpgsql SECURITY DEFINER SET search_path = ''\nAS $fn$\nBEGIN\n PERFORM ep_aeb_private.assert_tenant_principal(p_tenant_id, FALSE);\n RETURN QUERY DELETE FROM public.ep_aeb_consumption_operations\n WHERE tenant_id = p_tenant_id AND relying_party_id = p_relying_party_id\n AND ep_aeb_consumption_operations.operation_key = p_operation_key\n AND state = 'RESERVED' AND owner_token = p_owner_token\n RETURNING ep_aeb_consumption_operations.operation_key;\nEND\n$fn$;\nALTER FUNCTION ep_aeb_private.assert_tenant_principal(TEXT, BOOLEAN)\n OWNER TO ep_aeb_store_owner;\nALTER FUNCTION ep_aeb_private.reserve_operation(TEXT, TEXT, TEXT, TEXT)\n OWNER TO ep_aeb_store_owner;\nALTER FUNCTION ep_aeb_private.reserve_replay_keys(TEXT, TEXT, TEXT, TEXT[])\n OWNER TO ep_aeb_store_owner;\nALTER FUNCTION ep_aeb_private.commit_operation(TEXT, TEXT, TEXT, TEXT)\n OWNER TO ep_aeb_store_owner;\nALTER FUNCTION ep_aeb_private.claim_operation(TEXT, TEXT, TEXT, TEXT)\n OWNER TO ep_aeb_store_owner;\nALTER FUNCTION ep_aeb_private.release_operation(TEXT, TEXT, TEXT, TEXT)\n OWNER TO ep_aeb_store_owner;\nREVOKE ALL ON ALL FUNCTIONS IN SCHEMA ep_aeb_private\n FROM PUBLIC, anon, authenticated, service_role;\nGRANT USAGE ON SCHEMA ep_aeb_private TO ep_aeb_executor, ep_aeb_recovery;\nGRANT EXECUTE ON FUNCTION ep_aeb_private.reserve_operation(TEXT, TEXT, TEXT, TEXT),\n ep_aeb_private.reserve_replay_keys(TEXT, TEXT, TEXT, TEXT[]),\n ep_aeb_private.commit_operation(TEXT, TEXT, TEXT, TEXT),\n ep_aeb_private.release_operation(TEXT, TEXT, TEXT, TEXT)\n TO ep_aeb_executor;\nGRANT EXECUTE ON FUNCTION ep_aeb_private.claim_operation(TEXT, TEXT, TEXT, TEXT)\n TO ep_aeb_recovery;\nREVOKE ep_aeb_store_owner FROM CURRENT_USER;";
10
+ /** Exact statements issued by the store, exported for audit and deterministic fakes. */
11
+ export declare const AEB_CONSUMPTION_SQL: Readonly<{
12
+ reserveOperation: "SELECT operation_key FROM ep_aeb_private.reserve_operation($1::text, $2::text, $3::text, $4::text)";
13
+ reserveReplayKeys: "SELECT replay_key FROM ep_aeb_private.reserve_replay_keys($1::text, $2::text, $3::text, $4::text[])";
14
+ commitOperation: "SELECT operation_key FROM ep_aeb_private.commit_operation($1::text, $2::text, $3::text, $4::text)";
15
+ claimOperation: "SELECT operation_key FROM ep_aeb_private.claim_operation($1::text, $2::text, $3::text, $4::text)";
16
+ releaseOperation: "SELECT operation_key FROM ep_aeb_private.release_operation($1::text, $2::text, $3::text, $4::text)";
17
+ }>;
18
+ type QueryResult = {
19
+ rowCount: number | null;
20
+ rows?: Record<string, unknown>[];
21
+ };
22
+ export type AebConsumptionPgClient = {
23
+ query: (text: string, params?: any[]) => Promise<QueryResult>;
24
+ release: () => void;
25
+ };
26
+ export type AebConsumptionPgPool = {
27
+ connect: () => Promise<AebConsumptionPgClient>;
28
+ };
29
+ export interface PostgresAebDurableConsumptionStoreOptions {
30
+ /** Pool authenticated as a tenant-bound member of ep_aeb_executor. */
31
+ pool?: AebConsumptionPgPool;
32
+ /** Distinct pool authenticated as a tenant-bound member of ep_aeb_recovery. */
33
+ recoveryPool?: AebConsumptionPgPool;
34
+ tenantId?: string;
35
+ relyingPartyId?: string;
36
+ /** Must return an unpredictable opaque string in production. */
37
+ ownerTokenFactory?: () => string;
38
+ /** Verify a caller credential bound to the exact reservation being claimed. */
39
+ authorizeRecoveryClaim?: AebRecoveryClaimAuthorizer;
40
+ }
41
+ export interface AebRecoveryClaimAuthorization {
42
+ authorization: unknown;
43
+ tenantId: string;
44
+ relyingPartyId: string;
45
+ operationKey: string;
46
+ requiredState: 'RESERVED';
47
+ }
48
+ export type AebRecoveryClaimAuthorizer = (claim: Readonly<AebRecoveryClaimAuthorization>) => boolean | Promise<boolean>;
49
+ export interface PostgresAebDurableConsumptionStore extends AebDurableConsumptionStore {
50
+ recoveryClaimSupported: true;
51
+ /**
52
+ * Rotate ownership of an existing RESERVED row after external authorization.
53
+ * The stored and replacement owner tokens are never returned or passed to
54
+ * the authorizer.
55
+ */
56
+ claimReservation(key: string, authorization: unknown): Promise<boolean>;
57
+ }
58
+ /**
59
+ * Create the durable AEB store consumed by authorizeAebExecutionDurable().
60
+ * The pool must return a pinned node-postgres-style client for each transaction.
61
+ */
62
+ export declare function createPostgresAebDurableConsumptionStore({ pool, recoveryPool, tenantId, relyingPartyId, ownerTokenFactory, authorizeRecoveryClaim, }?: PostgresAebDurableConsumptionStoreOptions): PostgresAebDurableConsumptionStore;
63
+ declare const _default: {
64
+ AEB_PG_CONSUMPTION_STORE_VERSION: string;
65
+ AEB_CONSUMPTION_OPERATION_TABLE: string;
66
+ AEB_CONSUMPTION_REPLAY_TABLE: string;
67
+ AEB_CONSUMPTION_EXECUTOR_ROLE: string;
68
+ AEB_CONSUMPTION_RECOVERY_ROLE: string;
69
+ AEB_CONSUMPTION_DDL: string;
70
+ AEB_CONSUMPTION_SQL: Readonly<{
71
+ reserveOperation: "SELECT operation_key FROM ep_aeb_private.reserve_operation($1::text, $2::text, $3::text, $4::text)";
72
+ reserveReplayKeys: "SELECT replay_key FROM ep_aeb_private.reserve_replay_keys($1::text, $2::text, $3::text, $4::text[])";
73
+ commitOperation: "SELECT operation_key FROM ep_aeb_private.commit_operation($1::text, $2::text, $3::text, $4::text)";
74
+ claimOperation: "SELECT operation_key FROM ep_aeb_private.claim_operation($1::text, $2::text, $3::text, $4::text)";
75
+ releaseOperation: "SELECT operation_key FROM ep_aeb_private.release_operation($1::text, $2::text, $3::text, $4::text)";
76
+ }>;
77
+ createPostgresAebDurableConsumptionStore: typeof createPostgresAebDurableConsumptionStore;
78
+ };
79
+ export default _default;
80
+ //# sourceMappingURL=aeb-consumption-store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"aeb-consumption-store.d.ts","sourceRoot":"","sources":["../src/aeb-consumption-store.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EACV,0BAA0B,EAE3B,MAAM,8CAA8C,CAAC;AAEtD,eAAO,MAAM,gCAAgC,kCAAkC,CAAC;AAChF,eAAO,MAAM,+BAA+B,kCAAkC,CAAC;AAC/E,eAAO,MAAM,4BAA4B,qCAAqC,CAAC;AAC/E,eAAO,MAAM,6BAA6B,oBAAoB,CAAC;AAC/D,eAAO,MAAM,6BAA6B,oBAAoB,CAAC;AAC/D,eAAO,MAAM,0BAA0B,uBAAuB,CAAC;AAE/D,2EAA2E;AAC3E,eAAO,MAAM,mBAAmB,uxUAqMwB,CAAC;AAEzD,wFAAwF;AACxF,eAAO,MAAM,mBAAmB;;;;;;EAM9B,CAAC;AAEH,KAAK,WAAW,GAAG;IACjB,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,CAAC;CAClC,CAAC;AAEF,MAAM,MAAM,sBAAsB,GAAG;IACnC,KAAK,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,GAAG,EAAE,KAAK,OAAO,CAAC,WAAW,CAAC,CAAC;IAC9D,OAAO,EAAE,MAAM,IAAI,CAAC;CACrB,CAAC;AAEF,MAAM,MAAM,oBAAoB,GAAG;IACjC,OAAO,EAAE,MAAM,OAAO,CAAC,sBAAsB,CAAC,CAAC;CAChD,CAAC;AAEF,MAAM,WAAW,yCAAyC;IACxD,sEAAsE;IACtE,IAAI,CAAC,EAAE,oBAAoB,CAAC;IAC5B,+EAA+E;IAC/E,YAAY,CAAC,EAAE,oBAAoB,CAAC;IACpC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,gEAAgE;IAChE,iBAAiB,CAAC,EAAE,MAAM,MAAM,CAAC;IACjC,+EAA+E;IAC/E,sBAAsB,CAAC,EAAE,0BAA0B,CAAC;CACrD;AAED,MAAM,WAAW,6BAA6B;IAC5C,aAAa,EAAE,OAAO,CAAC;IACvB,QAAQ,EAAE,MAAM,CAAC;IACjB,cAAc,EAAE,MAAM,CAAC;IACvB,YAAY,EAAE,MAAM,CAAC;IACrB,aAAa,EAAE,UAAU,CAAC;CAC3B;AAED,MAAM,MAAM,0BAA0B,GAAG,CACvC,KAAK,EAAE,QAAQ,CAAC,6BAA6B,CAAC,KAC3C,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;AAEhC,MAAM,WAAW,kCAAmC,SAAQ,0BAA0B;IACpF,sBAAsB,EAAE,IAAI,CAAC;IAC7B;;;;OAIG;IACH,gBAAgB,CAAC,GAAG,EAAE,MAAM,EAAE,aAAa,EAAE,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CACzE;AAkDD;;;GAGG;AACH,wBAAgB,wCAAwC,CAAC,EACvD,IAAI,EACJ,YAAY,EACZ,QAAQ,EACR,cAAc,EACd,iBAAqC,EACrC,sBAAsB,GACvB,GAAE,yCAA8C,GAAG,kCAAkC,CA0KrF;;;;;;;;;;;;;;;;;AAED,wBASE"}