@emilia-protocol/gate 0.14.0 → 0.15.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/CHANGELOG.md +60 -1
  2. package/README.md +32 -1
  3. package/aeb-consumption-store.js +3 -0
  4. package/dist/aeb-consumption-store.d.ts +88 -0
  5. package/dist/aeb-consumption-store.d.ts.map +1 -0
  6. package/dist/aeb-consumption-store.js +471 -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 +16 -4
  15. package/dist/index.js.map +1 -1
  16. package/dist/proposal-to-effect-postgres.d.ts +144 -0
  17. package/dist/proposal-to-effect-postgres.d.ts.map +1 -0
  18. package/dist/proposal-to-effect-postgres.js +1578 -0
  19. package/dist/proposal-to-effect-postgres.js.map +1 -0
  20. package/dist/proposal-to-effect-status-head-store.d.ts +72 -0
  21. package/dist/proposal-to-effect-status-head-store.d.ts.map +1 -0
  22. package/dist/proposal-to-effect-status-head-store.js +355 -0
  23. package/dist/proposal-to-effect-status-head-store.js.map +1 -0
  24. package/dist/proposal-to-effect-status.d.ts +55 -0
  25. package/dist/proposal-to-effect-status.d.ts.map +1 -0
  26. package/dist/proposal-to-effect-status.js +294 -0
  27. package/dist/proposal-to-effect-status.js.map +1 -0
  28. package/dist/proposal-to-effect.d.ts +151 -3
  29. package/dist/proposal-to-effect.d.ts.map +1 -1
  30. package/dist/proposal-to-effect.js +589 -46
  31. package/dist/proposal-to-effect.js.map +1 -1
  32. package/dist/remedy-case-set-postgres.d.ts +73 -0
  33. package/dist/remedy-case-set-postgres.d.ts.map +1 -0
  34. package/dist/remedy-case-set-postgres.js +846 -0
  35. package/dist/remedy-case-set-postgres.js.map +1 -0
  36. package/dist/remedy-case-set.d.ts +53 -0
  37. package/dist/remedy-case-set.d.ts.map +1 -0
  38. package/dist/remedy-case-set.js +571 -0
  39. package/dist/remedy-case-set.js.map +1 -0
  40. package/dist/remedy-program-adapters.d.ts +137 -0
  41. package/dist/remedy-program-adapters.d.ts.map +1 -0
  42. package/dist/remedy-program-adapters.js +590 -0
  43. package/dist/remedy-program-adapters.js.map +1 -0
  44. package/dist/trust-program-revocation.js.map +1 -1
  45. package/package.json +283 -63
  46. package/proposal-to-effect-postgres.js +3 -0
  47. package/proposal-to-effect-status-head-store.js +2 -0
  48. package/proposal-to-effect-status.js +2 -0
  49. package/remedy-case-set-postgres.js +3 -0
  50. package/remedy-case-set.js +3 -0
  51. package/remedy-program-adapters.js +3 -0
  52. package/src/aeb-consumption-store.ts +568 -0
  53. package/src/capability-receipt.ts +17 -0
  54. package/src/index.ts +66 -3
  55. package/src/proposal-to-effect-postgres.ts +1833 -0
  56. package/src/proposal-to-effect-status-head-store.ts +449 -0
  57. package/src/proposal-to-effect-status.ts +388 -0
  58. package/src/proposal-to-effect.ts +755 -50
  59. package/src/remedy-case-set-postgres.ts +1008 -0
  60. package/src/remedy-case-set.ts +603 -0
  61. package/src/remedy-program-adapters.ts +657 -0
  62. package/src/trust-program-revocation.ts +1 -1
package/CHANGELOG.md CHANGED
@@ -4,7 +4,35 @@
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.14.0 (2026-07-22)
7
+ ## 0.15.1 (2026-07-23)
8
+
9
+ ### Added
10
+
11
+ - A tenant-authenticated `hasReplayFence()` observation on the durable AEB
12
+ consumption store. It reports an exact native replay unit as unavailable
13
+ when either a reserved or permanently consumed fence exists.
14
+ - Durable accepted `EP-STATUS-v1` head custody, scoped by tenant, relying
15
+ party, and complete status target, with database-side compare-and-advance.
16
+ - A tenant-authorized Proposal-to-Effect attempt lookup over the immutable
17
+ provider tuple and request digest, so a lost indeterminate HTTP response can
18
+ be rediscovered without invoking the effect again or rotating recovery
19
+ ownership.
20
+
21
+ ### Security
22
+
23
+ - Proposal-to-Effect status verification can now obtain exact, server-side
24
+ replay state without granting direct table access. The observation is a
25
+ preflight check; the immediately following atomic reservation remains the
26
+ race-closing authority boundary.
27
+ - Proposal-to-Effect no longer accepts a caller-configurable previous-head
28
+ resolver. Status candidates are verified against the relying party's stored
29
+ predecessor and admitted only if that head still wins an atomic comparison.
30
+ - The production readiness contract proves distinct executor and recovery
31
+ session identities, exclusive tenant capabilities, exact role/RPC grants,
32
+ and the required replay, status-head, and attempt-store schema before
33
+ admitting traffic.
34
+
35
+ ## 0.15.0 (2026-07-22)
8
36
 
9
37
  ### Added
10
38
 
@@ -15,6 +43,23 @@ This package follows [Semantic Versioning](https://semver.org/).
15
43
  - An executable end-to-end example and profile vectors covering exact-action
16
44
  mutation, stale evidence, one-time replay, indeterminate effects, and
17
45
  post-effect commit failure.
46
+ - `./aeb-consumption-store`, a tenant- and relying-party-namespaced PostgreSQL
47
+ store that atomically fences the AEB operation and all native replay units,
48
+ with opaque owner tokens and authorized restart recovery.
49
+ - `./proposal-to-effect-status`, which verifies server-resolved
50
+ `EP-STATUS-v1` heads and separately requires authenticated local consumption
51
+ state before the atomic execution reservation.
52
+ - `./proposal-to-effect-postgres`, a private-schema PostgreSQL store for
53
+ consequence attempts with HMAC-hidden owner capabilities, tenant/provider
54
+ namespaces, owner-fenced transitions, immutable terminal states, exact
55
+ provider-evidence reconciliation, database leases, disjoint executor and
56
+ recovery roles, tenant-principal bindings, and stale-only restart recovery.
57
+ - Concrete Remedy Program evidence adapters for signed disputes,
58
+ authorizations, provider outcomes, Action Escrow state, and late revocation.
59
+ - A heterogeneous remedy case-set coordinator that completes only after every
60
+ exact child state and signed remedy receipt verifies.
61
+ - A PostgreSQL remedy case-set store with tenant RLS, immutable manifests,
62
+ append-only state events, database-clock custody, and owner/revision CAS.
18
63
 
19
64
  ### Security
20
65
 
@@ -25,6 +70,20 @@ This package follows [Semantic Versioning](https://semver.org/).
25
70
  - Once an effect may have executed, failed bookkeeping cannot release its AEB
26
71
  reservation. The operation remains frozen until authenticated provider
27
72
  evidence proves `COMMITTED` or `NOT_COMMITTED`.
73
+ - A committed effect consumes AEB replay authority before its consequence row
74
+ becomes terminal. `repairAeb` converges legacy or crash-window terminal rows
75
+ without invoking an effect, and opaque attempt-owner capabilities are kept
76
+ out of enumerable results and errors.
77
+ - A recovered worker receives a rotated owner capability; the stale worker can
78
+ no longer transition or reconcile the attempt. An in-flight attempt is
79
+ conservatively recovered as `INDETERMINATE`, never retried as unexecuted.
80
+ - AEB production consumption state is RPC-only behind tenant-bound, no-bypass
81
+ executor and recovery roles with physically separate pools. Supabase
82
+ `service_role` and both runtime roles have no direct table authority;
83
+ in-memory stores remain test-only.
84
+ - Remedy case-set state and append-only history are likewise RPC-only behind a
85
+ tenant-bound no-bypass executor; generic service credentials cannot rewrite
86
+ current or historical remedy state.
28
87
 
29
88
  ## 0.13.0 (2026-07-20)
30
89
 
package/README.md CHANGED
@@ -68,9 +68,19 @@ format. The proposal is an unsigned, short-lived request; authority remains in
68
68
 
69
69
  ```js
70
70
  import { createProposalToEffect } from '@emilia-protocol/gate/proposal-to-effect';
71
+ import { createProposalToEffectPostgresStore } from '@emilia-protocol/gate/proposal-to-effect-postgres';
71
72
 
72
73
  const controller = createProposalToEffect({
73
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
+ },
74
84
  profiles: {
75
85
  'payment-release': {
76
86
  id: 'payment-release',
@@ -94,6 +104,8 @@ const controller = createProposalToEffect({
94
104
  adapters: pinnedAebAdapters,
95
105
  store: durableOperationStore,
96
106
  resolve_artifacts,
107
+ currentStatusResolver,
108
+ statusVerifier,
97
109
  verify_provider_evidence,
98
110
  },
99
111
  });
@@ -113,10 +125,29 @@ The controller re-verifies the signed AEB evaluation, runs Gate policy,
113
125
  reserves the operation before invoking the effect, and consumes it after
114
126
  success. An uncertain provider result remains reserved; only authenticated
115
127
  provider evidence bound to the same operation, CAID, and action digest can
116
- reconcile it. See
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
117
138
  [`docs/protocol/proposal-to-effect-profile-v1.md`](../../docs/protocol/proposal-to-effect-profile-v1.md)
118
139
  and run `node examples/proposal-to-effect/demo.mjs` from the repository root.
119
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
+
120
151
  ## Three-plane deployment
121
152
 
122
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,88 @@
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.has_replay_fence(\n p_tenant_id TEXT, p_relying_party_id TEXT, p_replay_key TEXT\n) RETURNS TABLE(fenced BOOLEAN)\nLANGUAGE plpgsql VOLATILE SECURITY DEFINER SET search_path = ''\nAS $fn$\nBEGIN\n PERFORM ep_aeb_private.assert_tenant_principal(p_tenant_id, FALSE);\n RETURN QUERY SELECT EXISTS (\n SELECT 1\n FROM public.ep_aeb_consumption_replay_fences AS fences\n WHERE fences.tenant_id = p_tenant_id\n AND fences.relying_party_id = p_relying_party_id\n AND fences.replay_key = p_replay_key\n );\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.has_replay_fence(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.has_replay_fence(TEXT, TEXT, TEXT),\n 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
+ hasReplayFence: "SELECT fenced FROM ep_aeb_private.has_replay_fence($1::text, $2::text, $3::text)";
13
+ reserveOperation: "SELECT operation_key FROM ep_aeb_private.reserve_operation($1::text, $2::text, $3::text, $4::text)";
14
+ reserveReplayKeys: "SELECT replay_key FROM ep_aeb_private.reserve_replay_keys($1::text, $2::text, $3::text, $4::text[])";
15
+ commitOperation: "SELECT operation_key FROM ep_aeb_private.commit_operation($1::text, $2::text, $3::text, $4::text)";
16
+ claimOperation: "SELECT operation_key FROM ep_aeb_private.claim_operation($1::text, $2::text, $3::text, $4::text)";
17
+ releaseOperation: "SELECT operation_key FROM ep_aeb_private.release_operation($1::text, $2::text, $3::text, $4::text)";
18
+ }>;
19
+ type QueryResult = {
20
+ rowCount: number | null;
21
+ rows?: Record<string, unknown>[];
22
+ };
23
+ export type AebConsumptionPgClient = {
24
+ query: (text: string, params?: any[]) => Promise<QueryResult>;
25
+ release: () => void;
26
+ };
27
+ export type AebConsumptionPgPool = {
28
+ connect: () => Promise<AebConsumptionPgClient>;
29
+ };
30
+ export interface PostgresAebDurableConsumptionStoreOptions {
31
+ /** Pool authenticated as a tenant-bound member of ep_aeb_executor. */
32
+ pool?: AebConsumptionPgPool;
33
+ /** Distinct pool authenticated as a tenant-bound member of ep_aeb_recovery. */
34
+ recoveryPool?: AebConsumptionPgPool;
35
+ tenantId?: string;
36
+ relyingPartyId?: string;
37
+ /** Must return an unpredictable opaque string in production. */
38
+ ownerTokenFactory?: () => string;
39
+ /** Verify a caller credential bound to the exact reservation being claimed. */
40
+ authorizeRecoveryClaim?: AebRecoveryClaimAuthorizer;
41
+ }
42
+ export interface AebRecoveryClaimAuthorization {
43
+ authorization: unknown;
44
+ tenantId: string;
45
+ relyingPartyId: string;
46
+ operationKey: string;
47
+ requiredState: 'RESERVED';
48
+ }
49
+ export type AebRecoveryClaimAuthorizer = (claim: Readonly<AebRecoveryClaimAuthorization>) => boolean | Promise<boolean>;
50
+ export interface PostgresAebDurableConsumptionStore extends AebDurableConsumptionStore {
51
+ recoveryClaimSupported: true;
52
+ /**
53
+ * Authenticated pre-reservation observation for one exact native replay key.
54
+ * A true result includes both RESERVED and CONSUMED fences. Atomic reserve
55
+ * remains the race-closing operation.
56
+ */
57
+ hasReplayFence(replayKey: string): Promise<boolean>;
58
+ /**
59
+ * Rotate ownership of an existing RESERVED row after external authorization.
60
+ * The stored and replacement owner tokens are never returned or passed to
61
+ * the authorizer.
62
+ */
63
+ claimReservation(key: string, authorization: unknown): Promise<boolean>;
64
+ }
65
+ /**
66
+ * Create the durable AEB store consumed by authorizeAebExecutionDurable().
67
+ * The pool must return a pinned node-postgres-style client for each transaction.
68
+ */
69
+ export declare function createPostgresAebDurableConsumptionStore({ pool, recoveryPool, tenantId, relyingPartyId, ownerTokenFactory, authorizeRecoveryClaim, }?: PostgresAebDurableConsumptionStoreOptions): PostgresAebDurableConsumptionStore;
70
+ declare const _default: {
71
+ AEB_PG_CONSUMPTION_STORE_VERSION: string;
72
+ AEB_CONSUMPTION_OPERATION_TABLE: string;
73
+ AEB_CONSUMPTION_REPLAY_TABLE: string;
74
+ AEB_CONSUMPTION_EXECUTOR_ROLE: string;
75
+ AEB_CONSUMPTION_RECOVERY_ROLE: string;
76
+ AEB_CONSUMPTION_DDL: string;
77
+ AEB_CONSUMPTION_SQL: Readonly<{
78
+ hasReplayFence: "SELECT fenced FROM ep_aeb_private.has_replay_fence($1::text, $2::text, $3::text)";
79
+ reserveOperation: "SELECT operation_key FROM ep_aeb_private.reserve_operation($1::text, $2::text, $3::text, $4::text)";
80
+ reserveReplayKeys: "SELECT replay_key FROM ep_aeb_private.reserve_replay_keys($1::text, $2::text, $3::text, $4::text[])";
81
+ commitOperation: "SELECT operation_key FROM ep_aeb_private.commit_operation($1::text, $2::text, $3::text, $4::text)";
82
+ claimOperation: "SELECT operation_key FROM ep_aeb_private.claim_operation($1::text, $2::text, $3::text, $4::text)";
83
+ releaseOperation: "SELECT operation_key FROM ep_aeb_private.release_operation($1::text, $2::text, $3::text, $4::text)";
84
+ }>;
85
+ createPostgresAebDurableConsumptionStore: typeof createPostgresAebDurableConsumptionStore;
86
+ };
87
+ export default _default;
88
+ //# 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,++VAwNwB,CAAC;AAEzD,wFAAwF;AACxF,eAAO,MAAM,mBAAmB;;;;;;;EAO9B,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,cAAc,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IACpD;;;;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,CA+LrF;;;;;;;;;;;;;;;;;;AAED,wBASE"}