@emilia-protocol/gate 0.9.2 → 0.9.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,114 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ /**
3
+ * EP-RELIANCE-KERNEL-v1 — runtime enforcement wrapper.
4
+ *
5
+ * The pure verdict lives in @emilia-protocol/verify/reliance (evaluateReliance).
6
+ * This is the deny-by-default RUNTIME point a relying party puts in front of a
7
+ * consequential action: it evaluates the evidence packet against the relying
8
+ * party's pinned EP-RELIANCE-PROFILE-v1, appends the decision to a tamper-evident
9
+ * evidence log, and — on anything other than `rely` — returns a machine-readable
10
+ * refusal (HTTP 428, the same Receipt-Required status the Gate uses) naming the
11
+ * closed verdict and what evidence was required.
12
+ *
13
+ * ALLOW iff verdict === 'rely'. Every other closed verdict, a thrown verifier,
14
+ * or a strict evidence-log failure denies. The kernel never re-derives a verdict
15
+ * of its own — it enforces the one the pure offline verifier computed.
16
+ */
17
+ import { createEvidenceLog } from './evidence.js';
18
+
19
+ const RECEIPT_REQUIRED_STATUS = 428;
20
+
21
+ // Same cross-package resolution the Gate uses: prefer the published verifier,
22
+ // fall back to the in-repo source so the monorepo builds without a node_modules
23
+ // link. evaluateReliance is pure/offline; no DB, no network.
24
+ const { evaluateReliance, RELIANCE_VERDICTS, RELIANCE_PROFILE_VERSION } = await import('@emilia-protocol/verify/reliance')
25
+ .catch(() => import('../verify/reliance.js'));
26
+
27
+ export { RELIANCE_VERDICTS, RELIANCE_PROFILE_VERSION };
28
+
29
+ /** Build the 428 refusal for a non-`rely` verdict. */
30
+ function relianceChallenge(verdict, reasons, profile) {
31
+ return {
32
+ status: RECEIPT_REQUIRED_STATUS,
33
+ error: 'do_not_rely',
34
+ verdict,
35
+ reasons: Array.isArray(reasons) ? reasons : [],
36
+ required_assurance: profile?.required_assurance ?? null,
37
+ required_authority: profile?.required_authority === true,
38
+ required_evidence: Array.isArray(profile?.required_evidence) ? profile.required_evidence : [],
39
+ header: { name: 'Reliance-Refused', value: verdict },
40
+ };
41
+ }
42
+
43
+ /**
44
+ * Create a reliance kernel bound to one relying-party profile.
45
+ *
46
+ * @param {object} cfg
47
+ * @param {object} cfg.profile the pinned EP-RELIANCE-PROFILE-v1
48
+ * @param {object} [cfg.log] an evidence log (createEvidenceLog); one is created if absent
49
+ * @param {boolean} [cfg.strictEvidence=true] fail closed if the evidence log sink fails
50
+ * @returns {{ check: Function, evidence: object }}
51
+ */
52
+ export function createRelianceKernel({ profile, log, strictEvidence = true } = {}) {
53
+ const evidence = log || createEvidenceLog({ strict: strictEvidence });
54
+
55
+ /**
56
+ * Evaluate + enforce one evidence packet.
57
+ * @param {object} input the evaluateReliance input MINUS relying_party_profile (bound here)
58
+ * @param {object} [opts] verifier options { approverKeys, logPublicKey, rpId, revokerKeys }
59
+ * @returns {Promise<{ allow:boolean, status:number, verdict:string, reasons:string[], checks:object, challenge:(object|null), decision:object }>}
60
+ */
61
+ async function check(input = {}, opts = {}) {
62
+ let result;
63
+ try {
64
+ result = evaluateReliance({ ...input, relying_party_profile: profile }, opts);
65
+ } catch (err) {
66
+ // A thrown verifier is not a maybe — it is a refusal.
67
+ result = { verdict: 'do_not_rely_unsigned', rely: false, reasons: [`verifier_error:${err?.message || 'threw'}`], checks: {} };
68
+ }
69
+
70
+ const allow = result.verdict === 'rely';
71
+ const actionHash = input?.action?.action_hash ?? input?.receipt?.action_hash ?? null;
72
+
73
+ // Deny-by-default: record the decision to the tamper-evident log first. In
74
+ // strict mode a log-sink failure THROWS, which we convert to a refusal — an
75
+ // action whose decision cannot be durably recorded must not proceed.
76
+ let decision;
77
+ try {
78
+ decision = await evidence.record({
79
+ type: 'reliance.decision',
80
+ verdict: result.verdict,
81
+ allow,
82
+ action_hash: actionHash,
83
+ reasons: result.reasons,
84
+ checks: result.checks,
85
+ profile: result.profile ?? null,
86
+ });
87
+ } catch (err) {
88
+ return {
89
+ allow: false,
90
+ status: RECEIPT_REQUIRED_STATUS,
91
+ verdict: 'do_not_rely_unsigned',
92
+ reasons: [`evidence_log_failed:${err?.message || 'sink'}`],
93
+ checks: result.checks || {},
94
+ challenge: relianceChallenge('do_not_rely_unsigned', ['evidence_log_failed'], profile),
95
+ decision: null,
96
+ };
97
+ }
98
+
99
+ return {
100
+ allow,
101
+ status: allow ? 200 : RECEIPT_REQUIRED_STATUS,
102
+ verdict: result.verdict,
103
+ reasons: result.reasons,
104
+ checks: result.checks,
105
+ challenge: allow ? null : relianceChallenge(result.verdict, result.reasons, profile),
106
+ decision,
107
+ };
108
+ }
109
+
110
+ return { check, evidence };
111
+ }
112
+
113
+ const relianceKernelApi = { createRelianceKernel };
114
+ export default relianceKernelApi;
@@ -0,0 +1,287 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ /**
3
+ * EP-ASSURANCE-PACKAGE-v1 — the reliance assurance layer.
4
+ *
5
+ * The layer above the reliance kernel. The kernel answers "may this party rely on
6
+ * this action?" The assurance package answers the question an INDEPENDENT assurer
7
+ * (an audit firm, a regulator, an insurer) asks: "can I reproduce, test, and
8
+ * attest that an organization's automated actions were governed by admissible
9
+ * evidence under the organization's OWN pinned rule?"
10
+ *
11
+ * Two halves, mirroring the re-performance discipline of reperform.js:
12
+ * buildAssurancePackage(decisions, ...) the organization bundles its
13
+ * automated decisions + the evidence each relied on into ONE portable,
14
+ * content-addressed package (action, receipt, profile, authority proof,
15
+ * revocation check, consumption, denial/exception history, policy hash).
16
+ * reperformAssurancePackage(pkg, ...) the assurer RE-PERFORMS every
17
+ * reliance verdict offline from that evidence, TRUSTING NOTHING the package
18
+ * asserts: it recomputes each verdict with evaluateReliance, compares to the
19
+ * verdict the org's runtime CLAIMED (drift = a control failure the assurer
20
+ * caught), maps every verdict to a control objective, and emits an
21
+ * auditor-style workpaper. It does not conclude; the assurer concludes.
22
+ *
23
+ * PCAOB AS 1105 alignment (why this is audit evidence): the source is
24
+ * cryptographic, the decision trail is immutable and content-addressed, the rule
25
+ * is a pinned profile, and the verdict is re-performable directly. This module
26
+ * SUPPORTS a re-performance procedure; it never issues an opinion.
27
+ */
28
+ import { hashCanonical } from '../execution-binding.js';
29
+
30
+ export const ASSURANCE_PACKAGE_VERSION = 'EP-ASSURANCE-PACKAGE-v1';
31
+ export const ASSURANCE_REPERFORMANCE_VERSION = 'EP-ASSURANCE-REPERFORMANCE-v1';
32
+
33
+ // Cross-package resolution, same pattern the Gate uses: the pure offline reliance
34
+ // verifier. No DB, no network.
35
+ const { evaluateReliance, RELIANCE_VERDICTS } = await import('@emilia-protocol/verify/reliance')
36
+ .catch(() => import('../../verify/reliance.js'));
37
+
38
+ export { RELIANCE_VERDICTS };
39
+
40
+ /**
41
+ * The reliance control catalog: every reliance verdict maps to the control
42
+ * objective it exercises. A `rely` shows the control PASSING; every do_not_rely_*
43
+ * shows the control OPERATING (it refused a non-admissible action). Denials are
44
+ * the control working, not the control failing.
45
+ */
46
+ export const RELIANCE_CONTROL_CATALOG = Object.freeze({
47
+ 'RC-1': { objective: 'Only a human with valid organization-bound, scoped authority for THIS exact action may authorize it', verdicts: ['do_not_rely_authority_missing', 'do_not_rely_authority_subject_mismatch', 'do_not_rely_authority_organization_mismatch', 'do_not_rely_authority_revoked', 'do_not_rely_authority_expired', 'do_not_rely_scope_mismatch', 'do_not_rely_amount_exceeded', 'do_not_rely_registry_unavailable'] },
48
+ 'RC-2': { objective: 'Authorization uses a device-bound named-human ceremony (Class-A or quorum)', verdicts: ['do_not_rely_no_class_a', 'do_not_rely_quorum_unsatisfied'] },
49
+ 'RC-3': { objective: 'The action conforms to a pinned, accepted policy', verdicts: ['do_not_rely_policy_mismatch'] },
50
+ 'RC-4': { objective: 'Authorization is consumed exactly once (no replay)', verdicts: ['do_not_rely_already_consumed'] },
51
+ 'RC-5': { objective: 'Reliance is evaluated against fresh revocation state', verdicts: ['do_not_rely_stale_revocation'] },
52
+ 'RC-6': { objective: 'Evidence is signed by a trusted issuer and evaluated under a pinned rule', verdicts: ['do_not_rely_unsigned', 'do_not_rely_untrusted_issuer', 'do_not_rely_no_profile'] },
53
+ });
54
+
55
+ const VERDICT_TO_CONTROL = Object.freeze(
56
+ Object.entries(RELIANCE_CONTROL_CATALOG).reduce((m, [cid, c]) => {
57
+ for (const v of c.verdicts) m[v] = cid;
58
+ return m;
59
+ }, {}),
60
+ );
61
+
62
+ /** Which control objective a verdict exercises (rely passes ALL, so returns null). */
63
+ function controlForVerdict(verdict) {
64
+ if (verdict === 'rely') return null;
65
+ return VERDICT_TO_CONTROL[verdict] || null;
66
+ }
67
+
68
+ function toIso(now) {
69
+ const ms = typeof now === 'function' ? now() : now;
70
+ return new Date(ms == null ? 0 : ms).toISOString();
71
+ }
72
+
73
+ /**
74
+ * Bundle N automated reliance decisions + the evidence each relied on into one
75
+ * portable, content-addressed assurance package. Does NOT re-perform (that is the
76
+ * assurer's independent step); it packages faithfully, including the verdict the
77
+ * org's runtime CLAIMED, so drift is checkable later.
78
+ *
79
+ * @param {Array<object>} decisions each: { decision_id, action, receipt, quorum?,
80
+ * authority_proof?, revocation_state?, consumption?, stated_verdict? }
81
+ * @param {object} opts
82
+ * @param {object} opts.profile the pinned EP-RELIANCE-PROFILE-v1 the org operated under
83
+ * @param {object} [opts.organization] { id, name } (no PHI)
84
+ * @param {number|function} [opts.now]
85
+ * @returns {object} EP-ASSURANCE-PACKAGE-v1
86
+ */
87
+ export function buildAssurancePackage(decisions = [], { profile, organization = null, now = 0 } = {}) {
88
+ if (!Array.isArray(decisions)) throw new Error('assurance-package: decisions must be an array');
89
+ const items = decisions.map((d, i) => {
90
+ const decision_id = d?.decision_id ?? `decision-${i}`;
91
+ return {
92
+ decision_id,
93
+ action: d?.action ?? null,
94
+ policy_hash: d?.action?.policy_hash ?? null,
95
+ stated_verdict: typeof d?.stated_verdict === 'string' ? d.stated_verdict : null,
96
+ evidence: {
97
+ receipt: d?.receipt ?? null,
98
+ quorum: d?.quorum ?? null,
99
+ authority_proof: d?.authority_proof ?? null,
100
+ revocation_state: d?.revocation_state ?? null,
101
+ consumption: d?.consumption ?? null,
102
+ },
103
+ };
104
+ });
105
+ // Denial/exception history = the decisions the org itself recorded as refused.
106
+ const exceptions = items.filter((it) => it.stated_verdict && it.stated_verdict !== 'rely')
107
+ .map((it) => ({ decision_id: it.decision_id, stated_verdict: it.stated_verdict, control_id: controlForVerdict(it.stated_verdict) }));
108
+
109
+ const body = {
110
+ '@version': ASSURANCE_PACKAGE_VERSION,
111
+ organization,
112
+ reliance_profile: profile ?? null,
113
+ profile_hash: profile ? hashCanonical(profile) : null,
114
+ control_catalog: RELIANCE_CONTROL_CATALOG,
115
+ decisions: items,
116
+ exception_history: exceptions,
117
+ counts: {
118
+ decisions: items.length,
119
+ stated_admissible: items.filter((it) => it.stated_verdict === 'rely').length,
120
+ stated_refused: items.filter((it) => it.stated_verdict && it.stated_verdict !== 'rely').length,
121
+ stated_unknown: items.filter((it) => !it.stated_verdict).length,
122
+ },
123
+ assembled_at: toIso(now),
124
+ };
125
+ // Content address over everything EXCEPT the timestamp (so the same evidence
126
+ // yields the same digest regardless of when it was packaged).
127
+ const { assembled_at: _t, ...digestScope } = body;
128
+ return { ...body, package_digest: hashCanonical(digestScope) };
129
+ }
130
+
131
+ /**
132
+ * INDEPENDENT re-performance. Recompute every reliance verdict offline from the
133
+ * packaged evidence under the package's pinned profile and AUDITOR-supplied keys,
134
+ * trusting nothing the package asserts. Detect drift (recomputed ≠ stated), map
135
+ * to control objectives, and emit an auditor-style workpaper. Conclusion fields
136
+ * are ALWAYS null: the assurer concludes, not this tool.
137
+ *
138
+ * @param {object} pkg an EP-ASSURANCE-PACKAGE-v1
139
+ * @param {object} opts
140
+ * @param {object} [opts.approverKeys] auditor-pinned approver keys (out of band)
141
+ * @param {string} [opts.logPublicKey] auditor-pinned transparency-log key
142
+ * @param {string} [opts.rpId]
143
+ * @param {object} [opts.revokerKeys]
144
+ * @param {(key:object)=>boolean} [opts.isConsumed] auditor-owned consumption lookup
145
+ * @param {number|string|Date} [opts.now] reliance-evaluation clock (pin for determinism)
146
+ * @returns {object} EP-ASSURANCE-REPERFORMANCE-v1
147
+ */
148
+ export function reperformAssurancePackage(pkg, { approverKeys = {}, logPublicKey = null, rpId = null, revokerKeys = {}, isConsumed, now = 0 } = {}) {
149
+ if (!pkg || pkg['@version'] !== ASSURANCE_PACKAGE_VERSION) throw new Error('assurance-reperform: not an EP-ASSURANCE-PACKAGE-v1');
150
+ const profile = pkg.reliance_profile;
151
+ const evalOpts = { approverKeys, logPublicKey, rpId, revokerKeys, ...(typeof isConsumed === 'function' ? { isConsumed } : {}) };
152
+ const relianceNow = typeof now === 'function' ? now() : now;
153
+
154
+ const results = (Array.isArray(pkg.decisions) ? pkg.decisions : []).map((it) => {
155
+ const ev = it.evidence || {};
156
+ const input = {
157
+ action: it.action || {},
158
+ receipt: ev.receipt,
159
+ quorum: ev.quorum || undefined,
160
+ authority_proof: ev.authority_proof || undefined,
161
+ revocation_state: ev.revocation_state || undefined,
162
+ consumption: ev.consumption || undefined,
163
+ relying_party_profile: profile,
164
+ now: relianceNow,
165
+ };
166
+ let recomputed;
167
+ try {
168
+ recomputed = evaluateReliance(input, evalOpts);
169
+ } catch (err) {
170
+ recomputed = { verdict: 'do_not_rely_unsigned', rely: false, reasons: [`reperform_error:${err?.message || 'threw'}`] };
171
+ }
172
+ const recomputed_verdict = recomputed.verdict;
173
+ const stated_verdict = it.stated_verdict ?? null;
174
+ // Drift: the org's runtime claimed one outcome, independent re-performance
175
+ // computed another. A claimed `rely` that recomputes to a refusal is the
176
+ // material finding — the org relied on evidence that does not support reliance.
177
+ const drift = stated_verdict !== null && stated_verdict !== recomputed_verdict;
178
+ return {
179
+ decision_id: it.decision_id,
180
+ action_type: it.action?.action_type ?? null,
181
+ stated_verdict,
182
+ recomputed_verdict,
183
+ admissible: recomputed_verdict === 'rely',
184
+ drift,
185
+ drift_severity: drift ? (stated_verdict === 'rely' ? 'relied_on_inadmissible_evidence' : 'refused_admissible_or_reclassified') : null,
186
+ control_id: controlForVerdict(recomputed_verdict),
187
+ reasons: recomputed.reasons || [],
188
+ };
189
+ });
190
+
191
+ const byVerdict = Object.create(null);
192
+ const byControl = Object.create(null);
193
+ let admissible = 0;
194
+ let refused = 0;
195
+ let drift = 0;
196
+ for (const r of results) {
197
+ byVerdict[r.recomputed_verdict] = (byVerdict[r.recomputed_verdict] || 0) + 1;
198
+ if (r.admissible) admissible += 1; else refused += 1;
199
+ if (r.drift) drift += 1;
200
+ if (r.control_id) byControl[r.control_id] = (byControl[r.control_id] || 0) + 1;
201
+ }
202
+
203
+ // Recompute the package digest from the package's OWN contents rather than
204
+ // trusting pkg.package_digest. "No value the package asserts is trusted" has to
205
+ // include the digest: copying the stated one verbatim would let a tampered
206
+ // package carry a lying content-address through re-performance unchecked. Mirror
207
+ // buildAssurancePackage's digestScope exactly (body minus assembled_at and the
208
+ // digest field itself), then compare.
209
+ const { assembled_at: _statedAt, package_digest: _statedDigest, ...digestScope } = pkg;
210
+ const recomputedPackageDigest = hashCanonical(digestScope);
211
+ const packageDigestVerified = pkg.package_digest != null && recomputedPackageDigest === pkg.package_digest;
212
+
213
+ const doc = {
214
+ '@version': ASSURANCE_REPERFORMANCE_VERSION,
215
+ product: 'EMILIA Reliance Assurance',
216
+ package_digest: recomputedPackageDigest,
217
+ stated_package_digest: pkg.package_digest ?? null,
218
+ package_digest_verified: packageDigestVerified,
219
+ profile_hash: pkg.profile_hash ?? null,
220
+ generated_at: toIso(now),
221
+ honesty: {
222
+ reperforms:
223
+ 'Independent recomputation of every reliance verdict from the packaged evidence, under the package\'s pinned '
224
+ + 'EP-RELIANCE-PROFILE-v1 and auditor-supplied keys, using the offline reliance kernel. No value the package '
225
+ + 'asserts (including the stated verdict) is trusted; the stated verdict is compared, never relied on.',
226
+ does_not_establish: [
227
+ 'Completeness of the decision population: decisions withheld before packaging are not detectable from the package alone. Bind the population to an externally anchored count to close this.',
228
+ 'Runtime freshness or one-time consumption AT THE MOMENT OF DECISION: those were live properties; re-performance checks the evidence as packaged, not the runtime state that existed then.',
229
+ 'Issuer, approver, and registrar key custody, enrollment, or identity proofing, which remain external trust roots the auditor supplies out of band.',
230
+ 'The business correctness or wisdom of any authorized action.',
231
+ ],
232
+ status: 'Support for an audit re-performance procedure. This document does not conclude, opine, or certify; any conclusion is the auditor\'s.',
233
+ },
234
+ population: {
235
+ decisions: results.length,
236
+ admissible,
237
+ refused,
238
+ drift,
239
+ relied_on_inadmissible_evidence: results.filter((r) => r.drift_severity === 'relied_on_inadmissible_evidence').length,
240
+ by_recomputed_verdict: byVerdict,
241
+ by_control: byControl,
242
+ },
243
+ control_catalog: RELIANCE_CONTROL_CATALOG,
244
+ results,
245
+ reperformance_digest: null, // filled below
246
+ // Conclusion fields are ALWAYS null: a machine may support re-performance, it
247
+ // may never fill in the auditor's sign-off. A renderer must refuse to print a
248
+ // non-null conclusion here.
249
+ conclusion: { supportable: null, opinion: null, signed_off_by: null },
250
+ };
251
+ // Deterministic re-performance digest over the recomputed results + population
252
+ // (excludes timestamps), so a second assurer reproduces it byte-for-byte.
253
+ doc.reperformance_digest = hashCanonical({ package_digest: doc.package_digest, population: doc.population, results });
254
+ return doc;
255
+ }
256
+
257
+ /** Render a plain-text auditor workpaper. Refuses to print a filled conclusion. */
258
+ export function renderAssuranceWorkpaper(doc) {
259
+ if (!doc || doc['@version'] !== ASSURANCE_REPERFORMANCE_VERSION) throw new Error('render: not an EP-ASSURANCE-REPERFORMANCE-v1');
260
+ if (doc.conclusion && (doc.conclusion.supportable !== null || doc.conclusion.opinion !== null || doc.conclusion.signed_off_by !== null)) {
261
+ throw new Error('render refused: conclusion fields must be null (the auditor concludes, not the tool)');
262
+ }
263
+ const p = doc.population;
264
+ const lines = [];
265
+ lines.push(`EMILIA Reliance Assurance — re-performance workpaper (${doc['@version']})`);
266
+ lines.push(`package_digest: ${doc.package_digest} (recomputed)`);
267
+ lines.push(`package_digest match: ${doc.package_digest_verified ? 'YES — recomputed digest equals the package\'s stated digest' : 'NO — stated digest does NOT match recomputed contents (tamper or drift)'}`);
268
+ lines.push(`reperformance_digest: ${doc.reperformance_digest}`);
269
+ lines.push('');
270
+ lines.push(`Population: ${p.decisions} decisions | admissible(rely): ${p.admissible} | refused: ${p.refused} | drift: ${p.drift}`);
271
+ lines.push(`Relied on INADMISSIBLE evidence (claimed rely, recomputed refusal): ${p.relied_on_inadmissible_evidence}`);
272
+ lines.push('');
273
+ lines.push('By recomputed verdict:');
274
+ for (const [v, n] of Object.entries(p.by_recomputed_verdict)) lines.push(` ${v}: ${n}`);
275
+ lines.push('');
276
+ lines.push('Control objectives exercised (denials are the control operating):');
277
+ for (const [cid, n] of Object.entries(p.by_control)) lines.push(` ${cid} (${RELIANCE_CONTROL_CATALOG[cid].objective}): ${n}`);
278
+ lines.push('');
279
+ const drifts = doc.results.filter((r) => r.drift);
280
+ if (drifts.length) {
281
+ lines.push('DRIFT (independent re-performance disagrees with the runtime\'s stated verdict):');
282
+ for (const r of drifts) lines.push(` ${r.decision_id}: stated=${r.stated_verdict} recomputed=${r.recomputed_verdict} [${r.drift_severity}]`);
283
+ lines.push('');
284
+ }
285
+ lines.push('Conclusion: NULL by construction. The auditor concludes; this workpaper only supports the procedure.');
286
+ return lines.join('\n');
287
+ }
package/store-postgres.js CHANGED
@@ -1,26 +1,24 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
2
  /**
3
3
  * EMILIA Gate — reference DURABLE consumption backend: Postgres
4
- * (EP-GATE-PG-CONSUMPTION-v1).
4
+ * (EP-GATE-PG-CONSUMPTION-v2).
5
5
  *
6
6
  * Replay defense that survives restarts. This module implements the backend
7
7
  * contract consumed by `createDurableConsumptionStore` in ./store.js:
8
8
  *
9
9
  * backend = {
10
10
  * async addIfAbsent(key, value, { ttlSeconds }?) : boolean // true iff inserted
11
- * async set(key, value, { ttlSeconds }?) : void // overwrite
12
- * async delete(key) : void
11
+ * async compareAndSet(key, expected, replacement, { ttlSeconds }?) : boolean
12
+ * async deleteIfValue(key, expected) : boolean
13
13
  * async has(key) : boolean
14
14
  * }
15
15
  *
16
16
  * plus a `cleanupExpired(now)` garbage-collection statement.
17
17
  *
18
- * THE ONE CORRECTNESS PRIMITIVE atomic insert-if-absent. `addIfAbsent` is a
19
- * SINGLE statement: `INSERT ... ON CONFLICT (consumption_key) DO NOTHING`, and
20
- * the returned row count decides consumed-vs-replay. There is no read-then-
21
- * write, so there is no race: when two pods consume the same receipt id
22
- * concurrently, Postgres serializes the inserts on the primary key and exactly
23
- * one caller sees rowCount === 1. Everyone else is a replay.
18
+ * Each transition is one SQL statement. `addIfAbsent` decides first use;
19
+ * `compareAndSet` and `deleteIfValue` bind commit/release to the opaque owner of
20
+ * the current reservation. There is no read-then-write interval in which a
21
+ * delayed worker can overwrite or remove a newer worker's reservation.
24
22
  *
25
23
  * FAIL-CLOSED CONTRACT: if the injected `query` THROWS (connection down,
26
24
  * timeout, constraint other than the PK, ...) the error PROPAGATES to the
@@ -37,7 +35,7 @@
37
35
  * fake with real ON CONFLICT semantics — no network, no database.
38
36
  */
39
37
 
40
- export const PG_CONSUMPTION_VERSION = 'EP-GATE-PG-CONSUMPTION-v1';
38
+ export const PG_CONSUMPTION_VERSION = 'EP-GATE-PG-CONSUMPTION-v2';
41
39
 
42
40
  /** Single consumption table. Timestamps are epoch milliseconds (BIGINT) so the
43
41
  * injected JS clock maps 1:1 onto column values with no timezone ambiguity. */
@@ -58,16 +56,16 @@ export const CONSUMPTION_SQL = {
58
56
  /** $1 key, $2 state, $3 consumed_at ms, $4 expires_at ms|null. rowCount 1 = consumed, 0 = replay. */
59
57
  addIfAbsent: `INSERT INTO ${CONSUMPTION_TABLE} (consumption_key, state, consumed_at, expires_at) `
60
58
  + 'VALUES ($1, $2, $3, $4) ON CONFLICT (consumption_key) DO NOTHING',
61
- /** $1 key, $2 state, $3 consumed_at ms, $4 expires_at ms|null. Upsert (overwrite). */
62
- set: `INSERT INTO ${CONSUMPTION_TABLE} (consumption_key, state, consumed_at, expires_at) `
63
- + 'VALUES ($1, $2, $3, $4) ON CONFLICT (consumption_key) DO UPDATE '
64
- + 'SET state = EXCLUDED.state, consumed_at = EXCLUDED.consumed_at, expires_at = EXCLUDED.expires_at',
65
- /** $1 key. */
66
- delete: `DELETE FROM ${CONSUMPTION_TABLE} WHERE consumption_key = $1`,
59
+ /** $1 key, $2 expected state, $3 replacement, $4 consumed_at ms, $5 expires_at ms|null. */
60
+ compareAndSet: `UPDATE ${CONSUMPTION_TABLE} SET state = $3, consumed_at = $4, expires_at = $5 `
61
+ + 'WHERE consumption_key = $1 AND state = $2',
62
+ /** $1 key, $2 expected state. */
63
+ deleteIfValue: `DELETE FROM ${CONSUMPTION_TABLE} WHERE consumption_key = $1 AND state = $2`,
67
64
  /** $1 key. Any row — even an expired one — counts as consumed until cleaned. */
68
65
  has: `SELECT 1 FROM ${CONSUMPTION_TABLE} WHERE consumption_key = $1`,
69
66
  /** $1 now ms. Removes ONLY rows whose TTL has elapsed; NULL expires_at never expires. */
70
- cleanupExpired: `DELETE FROM ${CONSUMPTION_TABLE} WHERE expires_at IS NOT NULL AND expires_at <= $1`,
67
+ cleanupExpired: `DELETE FROM ${CONSUMPTION_TABLE} WHERE state LIKE 'committed:%' `
68
+ + 'AND expires_at IS NOT NULL AND expires_at <= $1',
71
69
  };
72
70
 
73
71
  /**
@@ -84,13 +82,25 @@ export function createPostgresBackend({ query, now = Date.now } = {}) {
84
82
  throw new Error('createPostgresBackend: query must be an async (text, params) => { rowCount } function '
85
83
  + '(e.g. pg pool.query). It must THROW on failure — a backend error must never look like a verdict.');
86
84
  }
87
- const nowMs = () => (typeof now === 'function' ? now() : now);
85
+ let lastObservedNow = Number.NEGATIVE_INFINITY;
86
+ const checkedNow = (candidate) => {
87
+ if (!Number.isSafeInteger(candidate) || candidate < 0) {
88
+ throw new Error('consumption clock must return a non-negative safe-integer epoch millisecond');
89
+ }
90
+ if (candidate < lastObservedNow) {
91
+ throw new Error(`consumption clock regression refused: ${candidate} < ${lastObservedNow}`);
92
+ }
93
+ lastObservedNow = candidate;
94
+ return candidate;
95
+ };
96
+ const nowMs = () => checkedNow(typeof now === 'function' ? now() : now);
88
97
  const expiryFor = (opt) => {
89
98
  const ttl = Number(opt?.ttlSeconds);
90
99
  return Number.isFinite(ttl) && ttl > 0 ? nowMs() + ttl * 1000 : null;
91
100
  };
92
101
 
93
102
  return {
103
+ durable: true,
94
104
  /** True iff THIS call inserted the row — the atomic consumed-vs-replay decision. */
95
105
  async addIfAbsent(key, value, opt) {
96
106
  const res = await query(CONSUMPTION_SQL.addIfAbsent, [key, value, nowMs(), expiryFor(opt)]);
@@ -102,14 +112,22 @@ export function createPostgresBackend({ query, now = Date.now } = {}) {
102
112
  return res.rowCount === 1;
103
113
  },
104
114
 
105
- /** Overwrite (used by commit(): 'reserved' -> 'committed'). */
106
- async set(key, value, opt) {
107
- await query(CONSUMPTION_SQL.set, [key, value, nowMs(), expiryFor(opt)]);
115
+ /** Ownership-fenced reserved -> committed transition. */
116
+ async compareAndSet(key, expected, replacement, opt) {
117
+ const res = await query(CONSUMPTION_SQL.compareAndSet, [key, expected, replacement, nowMs(), expiryFor(opt)]);
118
+ if (!res || typeof res.rowCount !== 'number') {
119
+ throw new Error('compareAndSet: query result has no numeric rowCount — cannot prove reservation ownership');
120
+ }
121
+ return res.rowCount === 1;
108
122
  },
109
123
 
110
- /** Remove (used by release(): a failed action leaves the approval retryable). */
111
- async delete(key) {
112
- await query(CONSUMPTION_SQL.delete, [key]);
124
+ /** Remove only the reservation owned by the caller. */
125
+ async deleteIfValue(key, expected) {
126
+ const res = await query(CONSUMPTION_SQL.deleteIfValue, [key, expected]);
127
+ if (!res || typeof res.rowCount !== 'number') {
128
+ throw new Error('deleteIfValue: query result has no numeric rowCount — cannot prove reservation ownership');
129
+ }
130
+ return res.rowCount === 1;
113
131
  },
114
132
 
115
133
  /** Present = consumed. Expired-but-uncleaned rows still count (conservative). */
@@ -119,8 +137,9 @@ export function createPostgresBackend({ query, now = Date.now } = {}) {
119
137
  },
120
138
 
121
139
  /** Garbage-collect rows whose TTL elapsed. Returns the number removed. */
122
- async cleanupExpired(at = nowMs()) {
123
- const res = await query(CONSUMPTION_SQL.cleanupExpired, [at]);
140
+ async cleanupExpired(at) {
141
+ const cleanupAt = at === undefined ? nowMs() : checkedNow(at);
142
+ const res = await query(CONSUMPTION_SQL.cleanupExpired, [cleanupAt]);
124
143
  return res?.rowCount ?? 0;
125
144
  },
126
145
  };