fix(schema): enforce zero mapped-success events for denied lifecycle receipts

A denied core lifecycle receipt (outcome_code = 2) requires zero events
of the mapped success-transition kind. The previous blanket RETURN NULL
for outcome_code NOT IN (1, 3) let a denied receipt commit alongside its
mapped success-transition event, creating contradictory durable ledger
facts: denial plus enrolled/retired/revoked/rotated.

Replace the early return with a three-way branch in
authorization_operation_receipt_event_guard_v1():

  outcome_code IN (1, 3) — exactly-one mapped event (unchanged)
  outcome_code = 2       — zero events of the mapped transition kind;
                           raises authorization_denied_lifecycle_receipt_no_success_event
  other                  — skip (not a core lifecycle outcome)

Both deferred trigger directions share the same function body; a single
transaction with both INSERT paths exercises both directions at COMMIT.
The new negative fixture inserts a denied enroll receipt plus its mapped
success-transition event (event_kind = 1) in one transaction and asserts
COMMIT rejection with the exact constraint name.

Mutation: stashing the ELSIF branch lets COMMIT succeed, so expect_err
panics — confirming the branch is load-bearing (verified red).

Also correct three stale doc comments:
- Remove the "COMMIT succeeds" claim in the attempt-side shape guard
  mutation note; accurately state the pre-existing semantic-binding
  constraint fires instead.
- Remove the false claim that denial-attempt/admission-result tests
  protect applied/no-op lifecycle; replace with accurate coverage note.
- Update the semantic_fingerprint column comment to distinguish
  unresolved pre-auth kind-9 (actor_kind = 4) from authenticated kind-9
  (actor_kind 1-3).

All changes byte-mirrored between migration 0042 and schema/schema.sql.
All 9 NIP-FI ignored PostgreSQL regressions pass (--test-threads=1).

Co-authored-by: Will Pfleger <pfleger.will@gmail.com>
Signed-off-by: Will Pfleger <pfleger.will@gmail.com>
Signed-off-by: Hayt <9e1c23a3fd83f61da34420e4e88ff1b16e45cafcc0cd9019eb07d4ecfa8ca9b0@buzz.block.builderlab.xyz>
This commit is contained in:
Hayt
2026-08-31 14:32:17 -04:00
parent 3c2c919dab
commit 156ced4167
3 changed files with 188 additions and 57 deletions

View File

@@ -2978,8 +2978,10 @@ CREATE TABLE authorization_events (
correlation_id UUID NOT NULL,
attempt_id UUID NOT NULL,
-- Redaction-safe pre-authentication denial identity. Present and non-zero
-- for kind-9 events; NULL for all other event kinds. Binds the event to the
-- exact denial attempt's semantic_fingerprint (intent_digest) for exact replay.
-- for unresolved pre-auth kind-9 events (actor_kind = 4); NULL for
-- authenticated kind-9 events (actor_kind 1-3) and all other event kinds.
-- Binds the event to the exact denial attempt's semantic_fingerprint
-- (intent_digest) for exact replay.
semantic_fingerprint BYTEA CHECK (
semantic_fingerprint IS NULL OR octet_length(semantic_fingerprint) = 32
),
@@ -3730,28 +3732,44 @@ BEGIN
END IF;
-- Only applied (outcome_code = 1) and no-op (outcome_code = 3) lifecycle
-- receipts require a paired audit event. A denied lifecycle receipt
-- (outcome_code = 2) commits without one; fabricating a transition event
-- would falsely record that the lifecycle change occurred.
IF receipt.outcome_code NOT IN (1, 3) THEN
RETURN NULL;
END IF;
-- receipts require exactly one paired success-transition event. A denied
-- lifecycle receipt (outcome_code = 2) requires zero events of the mapped
-- transition kind: a success-transition event would falsely record that the
-- denied transition occurred, creating contradictory durable ledger facts.
-- Other outcome codes (4, 5) are not core lifecycle outcomes; skip.
IF receipt.outcome_code IN (1, 3) THEN
SELECT
count(*),
count(*) FILTER (WHERE event_kind = expected_event_kind)
INTO matching_event_count, expected_event_count
FROM authorization_events
WHERE community_id = receipt.community_id
AND operation_id = receipt.operation_id
AND request_fingerprint = receipt.request_fingerprint;
SELECT
count(*),
count(*) FILTER (WHERE event_kind = expected_event_kind)
INTO matching_event_count, expected_event_count
FROM authorization_events
WHERE community_id = receipt.community_id
AND operation_id = receipt.operation_id
AND request_fingerprint = receipt.request_fingerprint;
IF matching_event_count <> 1 OR expected_event_count <> 1 THEN
RAISE EXCEPTION
'lifecycle receipt requires exactly one event kind %, found % total and % expected',
expected_event_kind, matching_event_count, expected_event_count
USING ERRCODE = 'check_violation',
CONSTRAINT = 'authorization_operation_receipt_event_cardinality';
END IF;
ELSIF receipt.outcome_code = 2 THEN
SELECT count(*) FILTER (WHERE event_kind = expected_event_kind)
INTO expected_event_count
FROM authorization_events
WHERE community_id = receipt.community_id
AND operation_id = receipt.operation_id
AND request_fingerprint = receipt.request_fingerprint;
IF matching_event_count <> 1 OR expected_event_count <> 1 THEN
RAISE EXCEPTION
'lifecycle receipt requires exactly one event kind %, found % total and % expected',
expected_event_kind, matching_event_count, expected_event_count
USING ERRCODE = 'check_violation',
CONSTRAINT = 'authorization_operation_receipt_event_cardinality';
IF expected_event_count <> 0 THEN
RAISE EXCEPTION
'denied lifecycle receipt must not have a mapped success-transition event '
'(kind %); found % — contradictory durable facts are not permitted',
expected_event_kind, expected_event_count
USING ERRCODE = 'check_violation',
CONSTRAINT = 'authorization_denied_lifecycle_receipt_no_success_event';
END IF;
END IF;
RETURN NULL;
END;