exochain-gatekeeper 0.2.0-beta

EXOCHAIN judicial branch — CGR kernel, combinator algebra, invariant enforcement, Holon runtime, MCP middleware
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
//! DAG DB write gate: active consent, Ed25519 provenance, then `M12` persistence.

use std::{collections::BTreeMap, sync::Arc};

use exo_api::dagdb::{
    ConsentPurpose, DagDbGraphContextPacket, DagDbGraphContextSelectionResponse, ReceiptEventType,
    SubjectKind,
};
use exo_consent::ConsentError;
use exo_core::{Hash256, PublicKey, Signature, Timestamp, crypto};
use exo_dag_db_core::hash::ReceiptHashMaterial;
use exo_dag_db_domain::{
    context_packet_persistence::ContextPacketRecord,
    continuation_persistence::{ContinuationPersistResult, ContinuationRecord},
    default_route::DefaultRouteRecord,
    lifecycle_action::{LifecycleAction, LifecycleApplyResult},
    scoring::{DomainError, hash_event_body},
};
use exo_dag_db_exchange::kg_import::{hash_from_hex, stable_hash};
use exo_dag_db_postgres::postgres::{
    context_packet_persistence::persist_context_packet_record,
    continuation_persistence::persist_continuation_record,
    default_route::persist_default_route,
    kg_context_selection_write::{
        DbWriteSummary, UsageEventMemoryMetadata, persist_context_packet_receipt_to_db,
        persist_usage_event_to_db, persist_usage_event_to_db_with_metadata,
    },
    lifecycle_action::persist_lifecycle_action,
};
use exo_identity::error::IdentityError;
use sqlx::PgPool;
use tracing::warn;

use crate::{
    error::GatekeeperError,
    invariants::{
        ConstitutionalInvariant, InvariantContext, InvariantEngine, InvariantSet, enforce_all,
    },
    types::{BailmentState, ConsentRecord, GovernedRoleName, Role},
};

const USAGE_EVENT_MEMORY_ID_DOMAIN: &str =
    "exo.dagdb.graph_context_selection.usage_event.memory_id";
const DAGDB_WRITE_SIGNATURE_DOMAIN: &str = "exo.gatekeeper.dagdb_write_signature.v1";
const CREATED_AT: Timestamp = Timestamp::new(1, 0);
const WRITER_DID: &str = "did:exo:dagdb-context-selection-writer";

// PRD-D5: subject-id domains for the four PRD17 lifecycle/persistence surfaces.
// Each is domain-separated so the signed payload hash binds to one surface and
// cannot be transplanted across surfaces.
const LIFECYCLE_ACTION_SUBJECT_DOMAIN: &str = "exo.dagdb.lifecycle_action.subject_id.v1";
const DEFAULT_ROUTE_SUBJECT_DOMAIN: &str = "exo.dagdb.default_route.subject_id.v1";
const CONTINUATION_SUBJECT_DOMAIN: &str = "exo.dagdb.continuation_record.subject_id.v1";
const CONTEXT_PACKET_RECORD_SUBJECT_DOMAIN: &str = "exo.dagdb.context_packet_record.subject_id.v1";

/// Constitutional invariants enforced on the dag-db write path.
///
/// This is the subset of [`InvariantSet::all`] that the dag-db authorization
/// state can construct honestly from the consent/identity DB rows the T2
/// resolver loads. Two invariants from the full set are deliberately excluded
/// here, NOT silently dropped:
///
/// * [`ConstitutionalInvariant::AuthorityChainValid`] requires a non-empty,
///   per-link Ed25519-signed [`AuthorityChain`](crate::types::AuthorityChain)
///   bound to independently-resolved grantor keys. The dag-db consent schema
///   stores a bailment + consent grant, not a signed delegation chain, so a
///   context built from it has an empty chain and this invariant would
///   fail-closed-block EVERY legitimate dag-db write — a deadlock, not
///   enforcement. Authorization on the dag-db path is instead established by the
///   tenant-scoped consent grant (`ConsentRequired`) plus the route-layer
///   session-authority binding.
/// * [`ConstitutionalInvariant::ProvenanceVerifiable`] requires a signed
///   [`Provenance`](crate::types::Provenance) object with trusted actor keys.
///   The dag-db write path already enforces Ed25519 actor provenance directly
///   in [`DagDbGatekeeperService::validate_write`] via [`verify_write_signature`]
///   over the canonical payload hash (the same Ed25519 binding the invariant
///   would re-check), so it is enforced — just not a second time through the
///   engine.
///
/// `ConsentRequired` IS run through the engine here (in addition to the
/// pre-engine `verify_write_consent`) because the engine check additionally
/// asserts bailor/bailee/scope coherence and that the consent scope covers the
/// requested permission.
#[must_use]
fn dagdb_invariant_set() -> InvariantSet {
    InvariantSet::with(vec![
        ConstitutionalInvariant::ConsentRequired,
        ConstitutionalInvariant::SeparationOfPowers,
        ConstitutionalInvariant::NoSelfGrant,
        ConstitutionalInvariant::HumanOverride,
        ConstitutionalInvariant::KernelImmutability,
        ConstitutionalInvariant::QuorumLegitimate,
    ])
}

/// Active bailment + consent lookup for DAG DB writeback.
#[derive(Debug, Clone, Default)]
pub struct ConsentEngine {
    bailments: BTreeMap<String, BailmentState>,
    records: BTreeMap<(String, String, ConsentPurpose), DagDbConsentRecord>,
}

/// Scoped consent row for a tenant/agent/purpose triple.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DagDbConsentRecord {
    pub tenant_id: String,
    pub agent_did: String,
    pub purpose: ConsentPurpose,
    pub active: bool,
}

impl ConsentEngine {
    /// Register bailment state for a tenant (BCTS).
    #[must_use]
    pub fn with_bailment(mut self, tenant_id: impl Into<String>, state: BailmentState) -> Self {
        self.bailments.insert(tenant_id.into(), state);
        self
    }

    /// Register a consent record.
    #[must_use]
    pub fn with_consent_record(mut self, record: DagDbConsentRecord) -> Self {
        let key = (
            record.tenant_id.clone(),
            record.agent_did.clone(),
            record.purpose,
        );
        self.records.insert(key, record);
        self
    }

    fn bailment_for(&self, tenant_id: &str) -> Option<&BailmentState> {
        self.bailments.get(tenant_id)
    }

    /// Snapshot the bailment state registered for `tenant_id`, for constructing
    /// the constitutional [`InvariantContext`] over the dag-db write path.
    #[must_use]
    pub fn bailment_state(&self, tenant_id: &str) -> BailmentState {
        self.bailment_for(tenant_id)
            .cloned()
            .unwrap_or(BailmentState::None)
    }

    fn consent_for(
        &self,
        tenant_id: &str,
        agent_did: &str,
        purpose: ConsentPurpose,
    ) -> Option<&DagDbConsentRecord> {
        self.records
            .get(&(tenant_id.to_owned(), agent_did.to_owned(), purpose))
    }
}

/// Actor public-key registry for provenance verification.
#[derive(Debug, Clone, Default)]
pub struct IdentityRegistry {
    keys: BTreeMap<String, [u8; 32]>,
    roles: BTreeMap<String, Vec<Role>>,
}

impl IdentityRegistry {
    /// Register an Ed25519 public key for a DID string.
    #[must_use]
    pub fn with_public_key(mut self, agent_did: impl Into<String>, public_key: [u8; 32]) -> Self {
        self.keys.insert(agent_did.into(), public_key);
        self
    }

    /// Register an existing governed role for a DID.
    #[must_use]
    pub fn with_role(mut self, agent_did: impl Into<String>, role: Role) -> Self {
        self.roles.entry(agent_did.into()).or_default().push(role);
        self
    }

    /// Register a governed role by enum name for a DID.
    #[must_use]
    pub fn with_governed_role(
        self,
        agent_did: impl Into<String>,
        role_name: GovernedRoleName,
    ) -> Self {
        self.with_role(agent_did, Role::governed(role_name))
    }

    fn public_key_for(&self, agent_did: &str) -> Option<&[u8; 32]> {
        self.keys.get(agent_did)
    }

    /// Return true when the DID holds an existing governed role allowed to issue
    /// production DAG DB finality receipts.
    #[must_use]
    pub fn has_production_finality_authority(&self, authority_did: &str) -> bool {
        self.roles
            .get(authority_did)
            .is_some_and(|roles| roles.iter().any(role_can_issue_production_finality))
    }
}

fn role_can_issue_production_finality(role: &Role) -> bool {
    matches!(
        role.validate_governed(),
        Ok(GovernedRoleName::Operator
            | GovernedRoleName::Executive
            | GovernedRoleName::ExecutiveAdmin
            | GovernedRoleName::Judge
            | GovernedRoleName::TransitionJudge)
    )
}

/// Returns `true` when active bailment (BCTS) and consent exist for writeback.
pub fn verify_write_consent(
    engine: &ConsentEngine,
    tenant_id: &str,
    agent_did: &str,
    purpose: ConsentPurpose,
) -> Result<bool, ConsentError> {
    let Some(bailment) = engine.bailment_for(tenant_id) else {
        return Err(ConsentError::NoConsent(format!(
            "no bailment for tenant {tenant_id}"
        )));
    };
    if !bailment.is_active() {
        return Err(ConsentError::Denied(format!(
            "bailment inactive for tenant {tenant_id}"
        )));
    }
    // An active bailment is not sufficient: the bailor must have entrusted THIS
    // agent (bailee) with the writeback scope. A bailment for a different bailee
    // or scope must not authorize this agent's writeback.
    if purpose == ConsentPurpose::Writeback && !bailment.authorizes_writeback(agent_did) {
        return Err(ConsentError::Denied(format!(
            "bailment does not authorize {agent_did} for writeback on tenant {tenant_id}"
        )));
    }
    let Some(record) = engine.consent_for(tenant_id, agent_did, purpose) else {
        return Err(ConsentError::NoConsent(format!(
            "no consent record for {agent_did} purpose {purpose:?}"
        )));
    };
    if !record.active {
        return Err(ConsentError::Denied(format!(
            "consent inactive for {agent_did} purpose {purpose:?}"
        )));
    }
    Ok(true)
}

/// Returns `true` when the Ed25519 signature verifies over the payload hash bytes.
pub fn verify_write_signature(
    registry: &IdentityRegistry,
    payload_hash: &[u8; 32],
    signature: &str,
    agent_did: &str,
) -> Result<bool, IdentityError> {
    let did =
        exo_core::Did::new(agent_did).map_err(|_| IdentityError::InvalidDidDocumentField {
            did: agent_did.to_owned(),
            field: "did".into(),
            reason: "invalid agent DID".into(),
        })?;
    let public_key_bytes = registry
        .public_key_for(agent_did)
        .ok_or(IdentityError::KeyNotFound(did))?;
    let signature_bytes = decode_ed25519_signature_hex(signature)?;
    let message = dagdb_write_signature_message(payload_hash)?;
    let public_key = PublicKey::from_bytes(*public_key_bytes);
    let sig = Signature::from_bytes(signature_bytes);
    Ok(crypto::verify(message.as_bytes(), &sig, &public_key))
}

/// Verify that an external production finality authority, not merely any
/// registered DID, signed the canonical finality payload.
pub fn verify_production_finality_authority(
    registry: &IdentityRegistry,
    requesting_agent_did: &str,
    authority_did: &str,
    payload_hash: &[u8; 32],
    signature: &str,
) -> Result<bool, IdentityError> {
    if authority_did.trim().is_empty() || authority_did == requesting_agent_did {
        return Ok(false);
    }
    if !registry.has_production_finality_authority(authority_did) {
        return Ok(false);
    }
    verify_write_signature(registry, payload_hash, signature, authority_did)
}

/// Gatekeeper service that enforces consent and provenance before `M12` writes.
pub struct DagDbGatekeeperService {
    pub pool: PgPool,
    pub consent_engine: Arc<ConsentEngine>,
    pub identity_registry: Arc<IdentityRegistry>,
}

impl DagDbGatekeeperService {
    /// Construct a gatekeeper service over a Postgres pool and policy engines.
    #[must_use]
    pub fn new(
        pool: PgPool,
        consent_engine: Arc<ConsentEngine>,
        identity_registry: Arc<IdentityRegistry>,
    ) -> Self {
        Self {
            pool,
            consent_engine,
            identity_registry,
        }
    }

    /// Build the constitutional [`InvariantContext`] for a dag-db write from the
    /// consent/identity authorization state this service already holds.
    ///
    /// The context is enforced through `dagdb_invariant_set` (NOT the full
    /// `all()`): the dag-db consent schema yields a bailment + consent grant, so
    /// `ConsentRequired`, `SeparationOfPowers`, `NoSelfGrant`, `HumanOverride`,
    /// `KernelImmutability`, and `QuorumLegitimate` are all genuinely checkable.
    /// `AuthorityChainValid`/`ProvenanceVerifiable` are excluded — see
    /// `dagdb_invariant_set`.
    ///
    /// Returns `None` only when `agent_did` is not a structurally-valid DID
    /// (caller already validated it upstream; the gate fails closed before any
    /// write regardless).
    #[must_use]
    pub fn dagdb_invariant_context(
        &self,
        tenant_id: &str,
        agent_did: &str,
    ) -> Option<InvariantContext> {
        let actor = exo_core::Did::new(agent_did).ok()?;
        let bailment_state = self.consent_engine.bailment_state(tenant_id);
        // Mirror the bailment grant as a gatekeeper consent record so the engine's
        // `ConsentRequired` check (bailor/bailee/scope coherence) resolves against
        // the same DB-derived grant the pre-engine `verify_write_consent` used.
        let consent_records = match &bailment_state {
            BailmentState::Active {
                bailor,
                bailee,
                scope,
            } => vec![ConsentRecord {
                subject: bailor.clone(),
                granted_to: bailee.clone(),
                scope: scope.clone(),
                active: true,
            }],
            _ => Vec::new(),
        };
        Some(InvariantContext {
            actor,
            // The dag-db agent acts under a bailment, not a governed multi-branch
            // role; no roles => SeparationOfPowers passes (single/zero branch).
            actor_roles: Vec::new(),
            bailment_state,
            consent_records,
            // A writeback is not a permission grant: the agent is not expanding
            // its own permissions, so this is never a self-grant.
            authority_chain: Default::default(),
            is_self_grant: false,
            // Human override is preserved: the dag-db write path never disables
            // emergency human intervention.
            human_override_preserved: true,
            // A graph-memory write never mutates immutable kernel configuration.
            kernel_modification_attempted: false,
            // No quorum is gathered for a single-actor writeback; `None` makes
            // QuorumLegitimate vacuously pass.
            quorum_evidence: None,
            // Ed25519 provenance is enforced directly in `validate_write`, not via
            // the engine, so no `Provenance` object is constructed here.
            provenance: None,
            // No permission expansion is requested, so the consent-scope coverage
            // check passes with an empty requested set.
            actor_permissions: Default::default(),
            requested_permissions: Default::default(),
            trusted_authority_keys: Default::default(),
            trusted_provenance_keys: Default::default(),
        })
    }

    /// Persist a usage event after consent, signature, and optional invariant checks.
    pub async fn persist_usage_event(
        &self,
        event: &DagDbGraphContextSelectionResponse,
        agent_did: &str,
        signature: &str,
        invariant_context: Option<&InvariantContext>,
    ) -> Result<DbWriteSummary, GatekeeperError> {
        let payload_hash = usage_event_payload_hash(event)?;
        self.validate_write(
            &event.tenant_id,
            agent_did,
            ConsentPurpose::Writeback,
            &payload_hash,
            signature,
            invariant_context,
        )?;
        persist_usage_event_to_db(&self.pool, event)
            .await
            .map_err(domain_to_gatekeeper)
    }

    /// Persist a usage event with searchable metadata after consent and provenance checks.
    pub async fn persist_usage_event_with_metadata(
        &self,
        event: &DagDbGraphContextSelectionResponse,
        agent_did: &str,
        signature: &str,
        invariant_context: Option<&InvariantContext>,
        metadata: Option<&UsageEventMemoryMetadata>,
    ) -> Result<DbWriteSummary, GatekeeperError> {
        let payload_hash = usage_event_payload_hash(event)?;
        self.validate_write(
            &event.tenant_id,
            agent_did,
            ConsentPurpose::Writeback,
            &payload_hash,
            signature,
            invariant_context,
        )?;
        persist_usage_event_to_db_with_metadata(&self.pool, event, metadata)
            .await
            .map_err(domain_to_gatekeeper)
    }

    /// Validate usage-event write consent, provenance, and optional invariants
    /// without opening a persistence transaction.
    pub fn validate_usage_event_write(
        &self,
        event: &DagDbGraphContextSelectionResponse,
        agent_did: &str,
        signature: &str,
        invariant_context: Option<&InvariantContext>,
    ) -> Result<(), GatekeeperError> {
        let payload_hash = usage_event_payload_hash(event)?;
        self.validate_write(
            &event.tenant_id,
            agent_did,
            ConsentPurpose::Writeback,
            &payload_hash,
            signature,
            invariant_context,
        )
    }

    /// Persist a context-packet receipt after consent, signature, and optional invariant checks.
    pub async fn persist_context_packet_receipt(
        &self,
        packet: &DagDbGraphContextPacket,
        agent_did: &str,
        signature: &str,
        invariant_context: Option<&InvariantContext>,
    ) -> Result<DbWriteSummary, GatekeeperError> {
        let payload_hash = context_packet_payload_hash(packet)?;
        self.validate_write(
            &packet.tenant_id,
            agent_did,
            ConsentPurpose::Writeback,
            &payload_hash,
            signature,
            invariant_context,
        )?;
        persist_context_packet_receipt_to_db(&self.pool, packet)
            .await
            .map_err(domain_to_gatekeeper)
    }

    /// PRD-D5: persist a lifecycle action after consent, signature, and optional
    /// invariant checks. Lifecycle actions mutate the graph (writeback / relink /
    /// supersede / recycle / archive); this routes them through the same
    /// gatekeeper chain as the other graph-mutating write paths instead of the
    /// prior `validate()`-only persistence.
    ///
    /// Runtime caller status: served gateway routes now invoke these gated D5
    /// persistence methods when `production-db` is enabled and a governed pool is
    /// configured. Default-route persistence calls [`Self::persist_default_route`],
    /// context-packet persistence calls [`Self::persist_context_packet_record`],
    /// and writeback calls this method plus
    /// [`Self::persist_continuation_record`]. The method-boundary regression tests
    /// still prove the shared gate chain independently of route plumbing.
    pub async fn persist_lifecycle_action(
        &self,
        action: &LifecycleAction,
        agent_did: &str,
        signature: &str,
        invariant_context: Option<&InvariantContext>,
    ) -> Result<LifecycleApplyResult, GatekeeperError> {
        let payload_hash = lifecycle_action_payload_hash(action)?;
        self.validate_write(
            &action.tenant_id,
            agent_did,
            ConsentPurpose::Writeback,
            &payload_hash,
            signature,
            invariant_context,
        )?;
        persist_lifecycle_action(&self.pool, action)
            .await
            .map_err(|error| domain_blocked("lifecycle_action_postgres", &error))
    }

    /// PRD-D5: persist a default-route record after consent, signature, and
    /// optional invariant checks.
    ///
    /// Runtime caller status: see [`Self::persist_lifecycle_action`].
    pub async fn persist_default_route(
        &self,
        route: &DefaultRouteRecord,
        agent_did: &str,
        signature: &str,
        invariant_context: Option<&InvariantContext>,
    ) -> Result<u64, GatekeeperError> {
        let payload_hash = default_route_payload_hash(route)?;
        self.validate_write(
            &route.tenant_id,
            agent_did,
            ConsentPurpose::Writeback,
            &payload_hash,
            signature,
            invariant_context,
        )?;
        persist_default_route(&self.pool, route)
            .await
            .map_err(|error| domain_blocked("default_route_postgres", &error))
    }

    /// PRD-D5: persist a continuation record after consent, signature, and
    /// optional invariant checks.
    ///
    /// Runtime caller status: see [`Self::persist_lifecycle_action`].
    pub async fn persist_continuation_record(
        &self,
        record: &ContinuationRecord,
        now_epoch_seconds: u64,
        agent_did: &str,
        signature: &str,
        invariant_context: Option<&InvariantContext>,
    ) -> Result<ContinuationPersistResult, GatekeeperError> {
        let payload_hash = continuation_record_payload_hash(record)?;
        self.validate_write(
            &record.tenant_id,
            agent_did,
            ConsentPurpose::Writeback,
            &payload_hash,
            signature,
            invariant_context,
        )?;
        persist_continuation_record(&self.pool, record, now_epoch_seconds)
            .await
            .map_err(|error| domain_blocked("continuation_postgres", &error))
    }

    /// PRD-D5: persist a context-packet record after consent, signature, and
    /// optional invariant checks.
    ///
    /// Runtime caller status: see [`Self::persist_lifecycle_action`].
    pub async fn persist_context_packet_record(
        &self,
        record: &ContextPacketRecord,
        agent_did: &str,
        signature: &str,
        invariant_context: Option<&InvariantContext>,
    ) -> Result<u64, GatekeeperError> {
        let payload_hash = context_packet_record_payload_hash(record)?;
        self.validate_write(
            &record.tenant_id,
            agent_did,
            ConsentPurpose::Writeback,
            &payload_hash,
            signature,
            invariant_context,
        )?;
        persist_context_packet_record(&self.pool, record)
            .await
            .map_err(|error| domain_blocked("context_packet_record_postgres", &error))
    }

    fn validate_write(
        &self,
        tenant_id: &str,
        agent_did: &str,
        purpose: ConsentPurpose,
        payload_hash: &[u8; 32],
        signature: &str,
        invariant_context: Option<&InvariantContext>,
    ) -> Result<(), GatekeeperError> {
        match verify_write_consent(self.consent_engine.as_ref(), tenant_id, agent_did, purpose) {
            Ok(true) => {}
            Ok(false) => {
                return Err(consent_gatekeeper_error(
                    tenant_id,
                    agent_did,
                    "consent verification returned false",
                ));
            }
            Err(error) => {
                log_invariant_violation(
                    ConstitutionalInvariant::ConsentRequired,
                    tenant_id,
                    agent_did,
                    &error.to_string(),
                );
                return Err(consent_gatekeeper_error(
                    tenant_id,
                    agent_did,
                    &error.to_string(),
                ));
            }
        }

        match verify_write_signature(
            self.identity_registry.as_ref(),
            payload_hash,
            signature,
            agent_did,
        ) {
            Ok(true) => {}
            Ok(false) => {
                log_invariant_violation(
                    ConstitutionalInvariant::ProvenanceVerifiable,
                    tenant_id,
                    agent_did,
                    "signature verification returned false",
                );
                return Err(provenance_gatekeeper_error(
                    tenant_id,
                    agent_did,
                    "invalid Ed25519 signature",
                ));
            }
            Err(error) => {
                log_invariant_violation(
                    ConstitutionalInvariant::ProvenanceVerifiable,
                    tenant_id,
                    agent_did,
                    &error.to_string(),
                );
                return Err(provenance_gatekeeper_error(
                    tenant_id,
                    agent_did,
                    &error.to_string(),
                ));
            }
        }

        if let Some(context) = invariant_context {
            // Enforce the constructible dag-db invariant subset, not the full
            // `InvariantEngine::all()`: a context built from the dag-db consent
            // schema carries no signed authority chain, so `all()` would
            // fail-closed-block every legitimate write. See `dagdb_invariant_set`
            // for which invariants are engine-enforced vs enforced directly
            // above (Ed25519 provenance) on this path.
            let engine = InvariantEngine::new(dagdb_invariant_set());
            let violations = enforce_all(&engine, context);
            if let Err(violations) = violations {
                let detail = violations
                    .iter()
                    .map(|v| format!("{}: {}", v.invariant.id(), v.description))
                    .collect::<Vec<_>>()
                    .join("; ");
                for violation in &violations {
                    log_invariant_violation(
                        violation.invariant,
                        tenant_id,
                        agent_did,
                        &violation.description,
                    );
                }
                return Err(GatekeeperError::InvariantViolation(detail));
            }
        }
        Ok(())
    }
}

/// Produce a lowercase hex Ed25519 signature over the canonical write payload hash.
pub fn sign_write_payload(
    keypair: &exo_core::crypto::KeyPair,
    payload_hash: &[u8; 32],
) -> Result<String, IdentityError> {
    let message = dagdb_write_signature_message(payload_hash)?;
    Ok(format!("{}", keypair.sign(message.as_ref())))
}

/// Compute the canonical receipt-hash bytes used as the signed payload for usage events.
pub fn usage_event_payload_hash(
    event: &DagDbGraphContextSelectionResponse,
) -> Result<[u8; 32], GatekeeperError> {
    let memory_id = stable_hash(
        USAGE_EVENT_MEMORY_ID_DOMAIN,
        &[
            &event.tenant_id,
            &event.namespace,
            &event.request_id,
            &event.task_hash,
        ],
    )
    .map_err(|error| GatekeeperError::Core(error.to_string()))?;
    let event_body_hash = hash_event_body(event).map_err(domain_to_gatekeeper)?;
    let receipt_hash = ReceiptHashMaterial {
        tenant_id: event.tenant_id.clone(),
        namespace: event.namespace.clone(),
        subject_kind: SubjectKind::Memory,
        subject_id: memory_id,
        prev_receipt_hash: Hash256::ZERO,
        seq: 1,
        event_type: ReceiptEventType::IntakeCreated,
        actor_did: WRITER_DID.to_owned(),
        event_hlc: CREATED_AT,
        event_body_hash,
    }
    .hash()
    .map_err(|error| GatekeeperError::Core(error.to_string()))?;
    Ok(*receipt_hash.as_bytes())
}

/// Compute the canonical receipt-hash bytes used as the signed payload for packet receipts.
pub fn context_packet_payload_hash(
    packet: &DagDbGraphContextPacket,
) -> Result<[u8; 32], GatekeeperError> {
    let subject_id = hash_from_hex("packet_hash", &packet.packet_hash)
        .map_err(|_| GatekeeperError::InvariantViolation("invalid context packet hash".into()))?;
    let event_body_hash = hash_event_body(packet).map_err(domain_to_gatekeeper)?;
    let receipt_hash = ReceiptHashMaterial {
        tenant_id: packet.tenant_id.clone(),
        namespace: packet.namespace.clone(),
        subject_kind: SubjectKind::ContextPacket,
        subject_id,
        prev_receipt_hash: Hash256::ZERO,
        seq: 1,
        event_type: ReceiptEventType::ContextPacketCreated,
        actor_did: WRITER_DID.to_owned(),
        event_hlc: CREATED_AT,
        event_body_hash,
    }
    .hash()
    .map_err(|error| GatekeeperError::Core(error.to_string()))?;
    Ok(*receipt_hash.as_bytes())
}

/// PRD-D5: canonical receipt-hash bytes used as the signed payload for a
/// lifecycle action. The subject id is domain-separated over the action's
/// deterministic identity (tenant/namespace/action id/idempotency key) and the
/// full action body is folded into `event_body_hash`, so any change to the
/// persisted action invalidates the signature.
pub fn lifecycle_action_payload_hash(
    action: &LifecycleAction,
) -> Result<[u8; 32], GatekeeperError> {
    let idempotency_key = action
        .idempotency_key()
        .map_err(|error| GatekeeperError::InvariantViolation(error.to_string()))?;
    surface_payload_hash(
        LIFECYCLE_ACTION_SUBJECT_DOMAIN,
        &action.tenant_id,
        &action.memory_namespace,
        SubjectKind::Memory,
        ReceiptEventType::MemorySuperseded,
        &[
            &action.tenant_id,
            &action.memory_namespace,
            &action.action_id,
            &idempotency_key,
        ],
        action,
    )
}

/// PRD-D5: canonical receipt-hash bytes used as the signed payload for a
/// default-route record.
pub fn default_route_payload_hash(route: &DefaultRouteRecord) -> Result<[u8; 32], GatekeeperError> {
    surface_payload_hash(
        DEFAULT_ROUTE_SUBJECT_DOMAIN,
        &route.tenant_id,
        &route.memory_namespace,
        SubjectKind::Route,
        ReceiptEventType::RouteCreated,
        &[&route.tenant_id, &route.memory_namespace, &route.route_id],
        route,
    )
}

/// PRD-D5: canonical receipt-hash bytes used as the signed payload for a
/// continuation record.
pub fn continuation_record_payload_hash(
    record: &ContinuationRecord,
) -> Result<[u8; 32], GatekeeperError> {
    let idempotency_key = record
        .idempotency_key()
        .map_err(|error| GatekeeperError::InvariantViolation(error.to_string()))?;
    surface_payload_hash(
        CONTINUATION_SUBJECT_DOMAIN,
        &record.tenant_id,
        &record.memory_namespace,
        SubjectKind::Memory,
        ReceiptEventType::IntakeCreated,
        &[
            &record.tenant_id,
            &record.memory_namespace,
            &record.continuation_id,
            &idempotency_key,
        ],
        record,
    )
}

/// PRD-D5: canonical receipt-hash bytes used as the signed payload for a
/// context-packet record.
pub fn context_packet_record_payload_hash(
    record: &ContextPacketRecord,
) -> Result<[u8; 32], GatekeeperError> {
    surface_payload_hash(
        CONTEXT_PACKET_RECORD_SUBJECT_DOMAIN,
        &record.tenant_id,
        &record.memory_namespace,
        SubjectKind::ContextPacket,
        ReceiptEventType::ContextPacketCreated,
        &[
            &record.tenant_id,
            &record.memory_namespace,
            &record.packet_id,
            &record.idempotency_key,
        ],
        record,
    )
}

/// Shared PRD-D5 helper: build the signed payload hash for a persistence surface
/// from a domain-separated subject id plus the full record body. Mirrors
/// `usage_event_payload_hash`/`context_packet_payload_hash` so all gated surfaces
/// share one signature-binding shape.
fn surface_payload_hash<T: serde::Serialize>(
    subject_domain: &str,
    tenant_id: &str,
    namespace: &str,
    subject_kind: SubjectKind,
    event_type: ReceiptEventType,
    subject_parts: &[&str],
    body: &T,
) -> Result<[u8; 32], GatekeeperError> {
    let subject_id = stable_hash(subject_domain, subject_parts)
        .map_err(|error| GatekeeperError::Core(error.to_string()))?;
    let event_body_hash = hash_event_body(body).map_err(domain_to_gatekeeper)?;
    let receipt_hash = ReceiptHashMaterial {
        tenant_id: tenant_id.to_owned(),
        namespace: namespace.to_owned(),
        subject_kind,
        subject_id,
        prev_receipt_hash: Hash256::ZERO,
        seq: 1,
        event_type,
        actor_did: WRITER_DID.to_owned(),
        event_hlc: CREATED_AT,
        event_body_hash,
    }
    .hash()
    .map_err(|error| GatekeeperError::Core(error.to_string()))?;
    Ok(*receipt_hash.as_bytes())
}

fn dagdb_write_signature_message(payload_hash: &[u8; 32]) -> Result<Hash256, IdentityError> {
    exo_core::hash::hash_structured(&DagDbWriteSignaturePayload {
        domain: DAGDB_WRITE_SIGNATURE_DOMAIN,
        payload_hash,
    })
    .map_err(|error| IdentityError::InvalidDidDocumentField {
        did: "dagdb-write".into(),
        field: "signature_payload".into(),
        reason: error.to_string(),
    })
}

#[derive(serde::Serialize)]
struct DagDbWriteSignaturePayload<'a> {
    domain: &'static str,
    payload_hash: &'a [u8; 32],
}

fn decode_ed25519_signature_hex(signature: &str) -> Result<[u8; 64], IdentityError> {
    let bytes = hex::decode(signature).map_err(|error| IdentityError::InvalidDidDocumentField {
        did: "dagdb-write".into(),
        field: "signature".into(),
        reason: format!("hex decode failed: {error}"),
    })?;
    bytes
        .try_into()
        .map_err(|bytes: Vec<u8>| IdentityError::InvalidDidDocumentField {
            did: "dagdb-write".into(),
            field: "signature".into(),
            reason: format!("expected 64 bytes, got {}", bytes.len()),
        })
}

fn consent_gatekeeper_error(tenant_id: &str, agent_did: &str, reason: &str) -> GatekeeperError {
    GatekeeperError::InvariantViolation(format!(
        "ConsentRequired: tenant={tenant_id} actor={agent_did} reason={reason}"
    ))
}

fn provenance_gatekeeper_error(tenant_id: &str, agent_did: &str, reason: &str) -> GatekeeperError {
    GatekeeperError::InvariantViolation(format!(
        "ProvenanceVerifiable: tenant={tenant_id} actor={agent_did} reason={reason}"
    ))
}

fn domain_to_gatekeeper(error: DomainError) -> GatekeeperError {
    GatekeeperError::InvariantViolation(format!("dagdb write blocked: {error}"))
}

/// PRD-D5: map a lifecycle/persistence-surface error into the classified
/// gatekeeper error shape. A database/transaction failure carries the
/// `surface_database_unavailable` marker the gateway maps to 503; every other
/// failure (contract, idempotency replay, serialization) is a request-level
/// rejection the gateway maps to 422. The decision is deterministic from the
/// error's own Display string, so a DB outage is never reported as a policy
/// rejection and a contract reject is never reported as DB unavailability.
fn domain_blocked<E: std::error::Error>(surface: &str, error: &E) -> GatekeeperError {
    let rendered = error.to_string();
    let is_db_unavailable = rendered.contains("postgres_failed")
        || rendered.contains("sql_failed")
        || surface_error_source_is_sqlx(error);
    if is_db_unavailable {
        GatekeeperError::InvariantViolation(format!(
            "dagdb write blocked: surface_database_unavailable surface={surface} detail={rendered}"
        ))
    } else {
        GatekeeperError::InvariantViolation(format!(
            "dagdb write blocked: metadata rejected surface={surface} detail={rendered}"
        ))
    }
}

/// Returns `true` when the error (or any error in its `source()` chain) is a
/// `sqlx::Error`, i.e. a live database/transaction failure rather than a
/// request-level contract rejection.
fn surface_error_source_is_sqlx<E: std::error::Error>(error: &E) -> bool {
    let mut current: Option<&(dyn std::error::Error + 'static)> = error.source();
    while let Some(source) = current {
        if source.is::<sqlx::Error>() {
            return true;
        }
        current = source.source();
    }
    false
}

fn log_invariant_violation(
    invariant: ConstitutionalInvariant,
    tenant_id: &str,
    agent_did: &str,
    reason: &str,
) {
    warn!(
        validation_status = "denied",
        invariant = invariant.id(),
        tenant_id,
        actor_did = agent_did,
        reason,
        "InvariantViolated"
    );
}

#[cfg(test)]
mod tests {
    use exo_api::dagdb::{
        ConsentPurpose, DagDbGraphContextPacket, DagDbGraphContextSelectionResponse,
        DagDbGraphContextSelectionStatus,
    };
    use exo_core::crypto::KeyPair;

    use super::*;
    use crate::{
        invariants::ConstitutionalInvariant,
        types::{BailmentState, GovernmentBranch, Role},
    };

    fn active_consent_engine(tenant_id: &str, agent_did: &str) -> ConsentEngine {
        ConsentEngine::default()
            .with_bailment(
                tenant_id,
                BailmentState::Active {
                    bailor: exo_core::Did::new("did:exo:bailor").expect("bailor"),
                    bailee: exo_core::Did::new(agent_did).expect("bailee"),
                    scope: "dag-db:writeback".into(),
                },
            )
            .with_consent_record(DagDbConsentRecord {
                tenant_id: tenant_id.to_owned(),
                agent_did: agent_did.to_owned(),
                purpose: ConsentPurpose::Writeback,
                active: true,
            })
    }

    #[test]
    fn verify_write_consent_requires_active_bailment_and_record() {
        let engine = active_consent_engine("tenant-a", "did:exo:agent");
        assert!(
            verify_write_consent(
                &engine,
                "tenant-a",
                "did:exo:agent",
                ConsentPurpose::Writeback
            )
            .expect("consent ok")
        );

        let missing = ConsentEngine::default().with_consent_record(DagDbConsentRecord {
            tenant_id: "tenant-a".into(),
            agent_did: "did:exo:agent".into(),
            purpose: ConsentPurpose::Writeback,
            active: true,
        });
        assert!(
            verify_write_consent(
                &missing,
                "tenant-a",
                "did:exo:agent",
                ConsentPurpose::Writeback
            )
            .is_err()
        );
    }

    #[test]
    fn verify_write_signature_accepts_valid_ed25519() {
        let keypair = KeyPair::generate();
        let payload_hash = [7u8; 32];
        let message = dagdb_write_signature_message(&payload_hash).expect("message");
        let signature = keypair.sign(message.as_bytes());
        let signature_hex = format!("{signature}");
        let registry = IdentityRegistry::default()
            .with_public_key("did:exo:agent", *keypair.public_key().as_bytes());
        assert!(
            verify_write_signature(&registry, &payload_hash, &signature_hex, "did:exo:agent")
                .expect("verify ok")
        );
    }

    #[test]
    fn verify_write_signature_rejects_forged_signature() {
        let keypair = KeyPair::generate();
        let other = KeyPair::generate();
        let payload_hash = [9u8; 32];
        let message = dagdb_write_signature_message(&payload_hash).expect("message");
        let forged = other.sign(message.as_bytes());
        let forged_hex = format!("{forged}");
        let registry = IdentityRegistry::default()
            .with_public_key("did:exo:agent", *keypair.public_key().as_bytes());
        assert!(
            !verify_write_signature(&registry, &payload_hash, &forged_hex, "did:exo:agent")
                .expect("verify completes")
        );
    }

    #[test]
    fn separation_of_powers_blocks_multi_branch_actor() {
        let engine = InvariantEngine::new(crate::invariants::InvariantSet::with(vec![
            ConstitutionalInvariant::SeparationOfPowers,
        ]));
        let actor = exo_core::Did::new("did:exo:agent").expect("actor");
        let context = InvariantContext {
            actor: actor.clone(),
            actor_roles: vec![
                Role {
                    name: "senator".into(),
                    branch: GovernmentBranch::Legislative,
                },
                Role {
                    name: "executor".into(),
                    branch: GovernmentBranch::Executive,
                },
            ],
            bailment_state: BailmentState::Active {
                bailor: exo_core::Did::new("did:exo:bailor").expect("bailor"),
                bailee: actor,
                scope: "dag-db".into(),
            },
            consent_records: Vec::new(),
            authority_chain: Default::default(),
            is_self_grant: false,
            human_override_preserved: true,
            kernel_modification_attempted: false,
            quorum_evidence: None,
            provenance: None,
            actor_permissions: Default::default(),
            requested_permissions: Default::default(),
            trusted_authority_keys: Default::default(),
            trusted_provenance_keys: Default::default(),
        };
        assert!(enforce_all(&engine, &context).is_err());
    }

    fn sample_selection() -> DagDbGraphContextSelectionResponse {
        DagDbGraphContextSelectionResponse {
            tenant_id: "tenant-a".into(),
            namespace: "primary".into(),
            request_id: "req-gate-1".into(),
            task_hash: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".into(),
            selection_status: DagDbGraphContextSelectionStatus::Selected,
            selected_memory_refs: Vec::new(),
            selected_graph_edges: Vec::new(),
            omitted_memory_refs: Vec::new(),
            selection_trace: Vec::new(),
            selected_token_estimate: 0,
            token_budget: 1_000,
            boundary_warnings: Vec::new(),
        }
    }

    fn sample_packet() -> DagDbGraphContextPacket {
        DagDbGraphContextPacket {
            schema_version: "dagdb.graph_context_packet.v1".into(),
            tenant_id: "tenant-a".into(),
            namespace: "primary".into(),
            request_id: "req-gate-1".into(),
            task: "Build packet".into(),
            task_hash: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".into(),
            packet_hash: "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb".into(),
            selected_memory_refs: Vec::new(),
            selected_graph_edges: Vec::new(),
            citation_refs: Vec::new(),
            packet_metrics: exo_api::dagdb::DagDbContextPacketMetrics {
                token_budget: 1_000,
                selected_token_estimate: 0,
                selected_memory_ref_count: 0,
                selected_graph_edge_count: 0,
                citation_ref_count: 0,
                end_to_end_savings_status: "blocked".into(),
                cost_savings_status: "blocked".into(),
            },
            boundaries: exo_api::dagdb::DagDbContextPacketBoundaries {
                repository_test_level_only: true,
                production_runtime: "blocked".into(),
                default_context_replacement: "blocked".into(),
                citation_locator_status: "omitted_citation_locator_blocked".into(),
                billing_savings: "blocked".into(),
            },
            agent_usage_instructions: Vec::new(),
            markdown: "# packet".into(),
        }
    }

    #[test]
    fn verify_write_consent_requires_consent_record_for_purpose() {
        let bailment_only = ConsentEngine::default().with_bailment(
            "tenant-a",
            BailmentState::Active {
                bailor: exo_core::Did::new("did:exo:bailor").expect("bailor"),
                bailee: exo_core::Did::new("did:exo:agent").expect("bailee"),
                scope: "dag-db:writeback".into(),
            },
        );
        assert!(
            verify_write_consent(
                &bailment_only,
                "tenant-a",
                "did:exo:agent",
                ConsentPurpose::Writeback
            )
            .is_err()
        );
    }

    #[test]
    fn verify_write_consent_rejects_inactive_bailment_and_consent() {
        let inactive_bailment = ConsentEngine::default()
            .with_bailment(
                "tenant-a",
                BailmentState::Suspended {
                    reason: "test".into(),
                },
            )
            .with_consent_record(DagDbConsentRecord {
                tenant_id: "tenant-a".into(),
                agent_did: "did:exo:agent".into(),
                purpose: ConsentPurpose::Writeback,
                active: true,
            });
        assert!(
            verify_write_consent(
                &inactive_bailment,
                "tenant-a",
                "did:exo:agent",
                ConsentPurpose::Writeback
            )
            .is_err()
        );

        let inactive_consent = active_consent_engine("tenant-a", "did:exo:agent")
            .with_consent_record(DagDbConsentRecord {
                tenant_id: "tenant-a".into(),
                agent_did: "did:exo:agent".into(),
                purpose: ConsentPurpose::Writeback,
                active: false,
            });
        assert!(
            verify_write_consent(
                &inactive_consent,
                "tenant-a",
                "did:exo:agent",
                ConsentPurpose::Writeback
            )
            .is_err()
        );
    }

    #[test]
    fn verify_write_consent_rejects_active_bailment_for_other_bailee() {
        // Active bailment, valid consent record, but the bailment was entrusted
        // to a different bailee than the acting agent. Must fail closed.
        let engine = ConsentEngine::default()
            .with_bailment(
                "tenant-a",
                BailmentState::Active {
                    bailor: exo_core::Did::new("did:exo:bailor").expect("bailor"),
                    bailee: exo_core::Did::new("did:exo:other-agent").expect("bailee"),
                    scope: "dag-db:writeback".into(),
                },
            )
            .with_consent_record(DagDbConsentRecord {
                tenant_id: "tenant-a".into(),
                agent_did: "did:exo:agent".into(),
                purpose: ConsentPurpose::Writeback,
                active: true,
            });
        assert!(
            verify_write_consent(
                &engine,
                "tenant-a",
                "did:exo:agent",
                ConsentPurpose::Writeback
            )
            .is_err()
        );
    }

    #[test]
    fn verify_write_consent_rejects_active_bailment_with_wrong_scope() {
        // Active bailment for the correct bailee but a non-writeback scope.
        let engine = ConsentEngine::default()
            .with_bailment(
                "tenant-a",
                BailmentState::Active {
                    bailor: exo_core::Did::new("did:exo:bailor").expect("bailor"),
                    bailee: exo_core::Did::new("did:exo:agent").expect("bailee"),
                    scope: "dag-db:retrieval".into(),
                },
            )
            .with_consent_record(DagDbConsentRecord {
                tenant_id: "tenant-a".into(),
                agent_did: "did:exo:agent".into(),
                purpose: ConsentPurpose::Writeback,
                active: true,
            });
        assert!(
            verify_write_consent(
                &engine,
                "tenant-a",
                "did:exo:agent",
                ConsentPurpose::Writeback
            )
            .is_err()
        );
    }

    #[test]
    fn verify_write_signature_rejects_invalid_hex_and_did() {
        let keypair = KeyPair::generate();
        let registry = IdentityRegistry::default()
            .with_public_key("did:exo:agent", *keypair.public_key().as_bytes());
        let payload_hash = [1u8; 32];
        assert!(
            verify_write_signature(&registry, &payload_hash, "not-hex", "did:exo:agent").is_err()
        );
        assert!(verify_write_signature(&registry, &payload_hash, "abcd", "not-a-did").is_err());
        let short_hex = "aa".repeat(32);
        assert!(
            verify_write_signature(&registry, &payload_hash, &short_hex, "did:exo:agent").is_err()
        );
    }

    #[test]
    fn sign_write_payload_and_payload_hash_helpers_are_deterministic() {
        let keypair = KeyPair::generate();
        let selection = sample_selection();
        let payload_hash = usage_event_payload_hash(&selection).expect("usage payload hash");
        let signature = sign_write_payload(&keypair, &payload_hash).expect("signature");
        let again = sign_write_payload(&keypair, &payload_hash).expect("signature again");
        assert_eq!(signature, again);

        let packet = sample_packet();
        let packet_hash = context_packet_payload_hash(&packet).expect("packet payload hash");
        assert_ne!(payload_hash, packet_hash);
    }

    #[test]
    fn context_packet_payload_hash_rejects_invalid_packet_hash() {
        let mut packet = sample_packet();
        packet.packet_hash = "invalid".into();
        assert!(context_packet_payload_hash(&packet).is_err());
    }

    fn lazy_postgres_pool() -> sqlx::PgPool {
        use std::time::Duration;

        use sqlx::postgres::{PgConnectOptions, PgPoolOptions};

        if let Ok(database_url) = std::env::var("EXO_DAGDB_TEST_DATABASE_URL") {
            return PgPoolOptions::new()
                .connect_lazy(&database_url)
                .expect("lazy postgres pool");
        }
        let options = PgConnectOptions::new()
            .host("127.0.0.1")
            .port(1)
            .username("postgres")
            .database("postgres");
        PgPoolOptions::new()
            .acquire_timeout(Duration::from_millis(100))
            .connect_lazy_with(options)
    }

    #[tokio::test]
    async fn persist_usage_event_fails_before_db_when_consent_missing() {
        use std::sync::Arc;

        let pool = lazy_postgres_pool();
        let service = DagDbGatekeeperService::new(
            pool,
            Arc::new(ConsentEngine::default()),
            Arc::new(IdentityRegistry::default()),
        );
        let event = sample_selection();
        let err = service
            .persist_usage_event(&event, "did:exo:agent", &"aa".repeat(64), None)
            .await
            .expect_err("missing consent must fail closed");
        assert!(matches!(err, GatekeeperError::InvariantViolation(_)));
    }

    #[test]
    fn authority_resolver_unavailable_is_a_distinct_fail_closed_variant() {
        // The gateway's request-time DB resolver returns this variant when it
        // cannot establish consent/identity state (pool absent or a resolver
        // query failed). It must be a distinct typed variant — NOT folded into
        // an InvariantViolation policy denial — so the gateway can map it to a
        // 5xx availability fault instead of a hard policy deny, and so the
        // resolver never silently falls back to empty registries.
        let error = GatekeeperError::AuthorityResolverUnavailable("pool absent".into());
        assert!(matches!(
            error,
            GatekeeperError::AuthorityResolverUnavailable(_)
        ));
        assert!(!matches!(error, GatekeeperError::InvariantViolation(_)));
        assert!(error.to_string().contains("authority resolver unavailable"));
    }

    #[tokio::test]
    async fn persist_usage_event_fails_before_db_when_signature_invalid() {
        use std::sync::Arc;

        let keypair = KeyPair::generate();
        let other = KeyPair::generate();
        let event = sample_selection();
        let payload_hash = usage_event_payload_hash(&event).expect("payload hash");
        let forged = other.sign(
            dagdb_write_signature_message(&payload_hash)
                .expect("message")
                .as_bytes(),
        );
        let registry = Arc::new(
            IdentityRegistry::default()
                .with_public_key("did:exo:agent", *keypair.public_key().as_bytes()),
        );
        let pool = lazy_postgres_pool();
        let service = DagDbGatekeeperService::new(
            pool,
            Arc::new(active_consent_engine("tenant-a", "did:exo:agent")),
            registry,
        );
        let err = service
            .persist_usage_event(&event, "did:exo:agent", &format!("{forged}"), None)
            .await
            .expect_err("invalid signature must fail closed");
        assert!(matches!(err, GatekeeperError::InvariantViolation(_)));
    }

    #[tokio::test]
    async fn persist_usage_event_maps_db_errors_after_validation() {
        use std::sync::Arc;

        let keypair = KeyPair::generate();
        let event = sample_selection();
        let payload_hash = usage_event_payload_hash(&event).expect("payload hash");
        let signature = sign_write_payload(&keypair, &payload_hash).expect("signature");
        let registry = Arc::new(
            IdentityRegistry::default()
                .with_public_key("did:exo:agent", *keypair.public_key().as_bytes()),
        );
        let service = DagDbGatekeeperService::new(
            lazy_postgres_pool(),
            Arc::new(active_consent_engine("tenant-a", "did:exo:agent")),
            registry,
        );
        let err = service
            .persist_usage_event(&event, "did:exo:agent", &signature, None)
            .await
            .expect_err("unscoped lazy pool must fail closed at db layer");
        assert!(matches!(err, GatekeeperError::InvariantViolation(_)));
    }

    #[test]
    fn log_invariant_violation_emits_structured_warning() {
        log_invariant_violation(
            ConstitutionalInvariant::ConsentRequired,
            "tenant-a",
            "did:exo:agent",
            "coverage fixture",
        );
    }

    #[tokio::test]
    async fn persist_usage_event_fails_before_db_when_signature_decode_errors() {
        use std::sync::Arc;

        let keypair = KeyPair::generate();
        let event = sample_selection();
        let registry = Arc::new(
            IdentityRegistry::default()
                .with_public_key("did:exo:agent", *keypair.public_key().as_bytes()),
        );
        let service = DagDbGatekeeperService::new(
            lazy_postgres_pool(),
            Arc::new(active_consent_engine("tenant-a", "did:exo:agent")),
            registry,
        );
        let err = service
            .persist_usage_event(&event, "did:exo:agent", "not-hex", None)
            .await
            .expect_err("malformed signature must fail closed");
        assert!(matches!(err, GatekeeperError::InvariantViolation(_)));
        assert!(err.to_string().contains("ProvenanceVerifiable"));
    }

    #[tokio::test]
    async fn persist_context_packet_receipt_maps_db_errors_after_validation() {
        use std::sync::Arc;

        let keypair = KeyPair::generate();
        let packet = sample_packet();
        let payload_hash = context_packet_payload_hash(&packet).expect("payload hash");
        let signature = sign_write_payload(&keypair, &payload_hash).expect("signature");
        let registry = Arc::new(
            IdentityRegistry::default()
                .with_public_key("did:exo:agent", *keypair.public_key().as_bytes()),
        );
        let service = DagDbGatekeeperService::new(
            lazy_postgres_pool(),
            Arc::new(active_consent_engine("tenant-a", "did:exo:agent")),
            registry,
        );
        let err = service
            .persist_context_packet_receipt(&packet, "did:exo:agent", &signature, None)
            .await
            .expect_err("unscoped lazy pool must fail closed at db layer");
        assert!(matches!(err, GatekeeperError::InvariantViolation(_)));
    }

    #[tokio::test]
    async fn persist_context_packet_receipt_fails_before_db_when_invariants_violated() {
        use std::sync::Arc;

        let keypair = KeyPair::generate();
        let packet = sample_packet();
        let payload_hash = context_packet_payload_hash(&packet).expect("payload hash");
        let signature = sign_write_payload(&keypair, &payload_hash).expect("signature");
        let registry = Arc::new(
            IdentityRegistry::default()
                .with_public_key("did:exo:agent", *keypair.public_key().as_bytes()),
        );
        let pool = lazy_postgres_pool();
        let service = DagDbGatekeeperService::new(
            pool,
            Arc::new(active_consent_engine("tenant-a", "did:exo:agent")),
            registry,
        );
        let actor = exo_core::Did::new("did:exo:agent").expect("actor");
        let invariant_context = InvariantContext {
            actor: actor.clone(),
            actor_roles: vec![
                Role {
                    name: "senator".into(),
                    branch: GovernmentBranch::Legislative,
                },
                Role {
                    name: "executor".into(),
                    branch: GovernmentBranch::Executive,
                },
            ],
            bailment_state: BailmentState::Active {
                bailor: exo_core::Did::new("did:exo:bailor").expect("bailor"),
                bailee: actor,
                scope: "dag-db".into(),
            },
            consent_records: Vec::new(),
            authority_chain: Default::default(),
            is_self_grant: false,
            human_override_preserved: true,
            kernel_modification_attempted: false,
            quorum_evidence: None,
            provenance: None,
            actor_permissions: Default::default(),
            requested_permissions: Default::default(),
            trusted_authority_keys: Default::default(),
            trusted_provenance_keys: Default::default(),
        };
        let err = service
            .persist_context_packet_receipt(
                &packet,
                "did:exo:agent",
                &signature,
                Some(&invariant_context),
            )
            .await
            .expect_err("invariant violation must fail closed");
        assert!(matches!(err, GatekeeperError::InvariantViolation(_)));
    }

    #[test]
    fn dagdb_invariant_set_excludes_authority_chain_and_provenance() {
        // The dag-db subset must NOT include the two invariants that need a
        // signed authority chain / provenance object the dag-db consent schema
        // does not carry, or every legitimate write would fail closed.
        let set = dagdb_invariant_set();
        assert!(
            set.invariants
                .contains(&ConstitutionalInvariant::ConsentRequired)
        );
        assert!(
            !set.invariants
                .contains(&ConstitutionalInvariant::AuthorityChainValid),
            "AuthorityChainValid must be excluded from the dag-db engine subset"
        );
        assert!(
            !set.invariants
                .contains(&ConstitutionalInvariant::ProvenanceVerifiable),
            "ProvenanceVerifiable is enforced directly via Ed25519, not the engine"
        );
    }

    #[tokio::test]
    async fn dagdb_invariant_context_from_active_consent_passes_subset_engine() {
        use crate::invariants::{InvariantEngine, enforce_all};

        let service = DagDbGatekeeperService::new(
            lazy_postgres_pool(),
            Arc::new(active_consent_engine("tenant-a", "did:exo:agent")),
            Arc::new(IdentityRegistry::default()),
        );
        let context = service
            .dagdb_invariant_context("tenant-a", "did:exo:agent")
            .expect("context for valid DID");
        // Built from a real active bailment + consent grant, the dag-db subset
        // engine passes (no fail-closed deadlock for a legitimately-authorized
        // agent).
        let engine = InvariantEngine::new(dagdb_invariant_set());
        assert!(
            enforce_all(&engine, &context).is_ok(),
            "active-consent dag-db context must satisfy the enforced invariant subset"
        );
    }

    #[tokio::test]
    async fn dagdb_invariant_context_without_bailment_fails_consent_required() {
        use crate::invariants::{InvariantEngine, enforce_all};

        // No bailment registered for this tenant => no consent record mirrored
        // => ConsentRequired (in the subset) fails closed.
        let service = DagDbGatekeeperService::new(
            lazy_postgres_pool(),
            Arc::new(ConsentEngine::default()),
            Arc::new(IdentityRegistry::default()),
        );
        let context = service
            .dagdb_invariant_context("tenant-a", "did:exo:agent")
            .expect("context for valid DID");
        let engine = InvariantEngine::new(dagdb_invariant_set());
        let violations =
            enforce_all(&engine, &context).expect_err("missing bailment must fail closed");
        assert!(
            violations
                .iter()
                .any(|v| v.invariant == ConstitutionalInvariant::ConsentRequired),
            "missing bailment must surface a ConsentRequired violation"
        );
    }

    #[tokio::test]
    async fn dagdb_invariant_context_rejects_malformed_did() {
        let service = DagDbGatekeeperService::new(
            lazy_postgres_pool(),
            Arc::new(ConsentEngine::default()),
            Arc::new(IdentityRegistry::default()),
        );
        assert!(
            service.dagdb_invariant_context("tenant-a", "").is_none(),
            "a structurally-invalid DID must not yield an invariant context"
        );
    }

    // ---------------------------------------------------------------------
    // PRD-D5: route-contract coverage for the four lifecycle/persistence
    // surfaces now routed through the gatekeeper chain. Each surface proves:
    //   * consented + signed reaches the DB layer (and a DB outage is
    //     classified as `surface_database_unavailable`, i.e. 503 at the
    //     gateway), never a silent pass;
    //   * missing consent fails closed before any DB access;
    //   * a forged signature fails closed before any DB access.
    // Error-classification coverage (422 reject vs 503 unavailable) follows.
    // ---------------------------------------------------------------------

    use exo_dag_db_domain::{
        context_packet_persistence::{
            CONTEXT_PACKET_RECORD_SCHEMA_VERSION, DefaultContextQuality, PacketFreshnessStatus,
            PacketPersistenceStatus, PacketValidationStatus, canonical_idempotency_key,
        },
        continuation_persistence::{ContinuationRetrievalStatus, PRD17_CONTINUATION_RECORD_SCHEMA},
        default_route::{
            DEFAULT_ROUTE_SCHEMA_VERSION, DefaultRouteMemoryRef, DefaultRouteSource,
            DefaultRouteStatus, RouteFreshnessStatus,
        },
        lifecycle_action::{
            LifecycleActionType, LifecycleEvidenceRef, LifecycleMemoryRef, LifecycleRollbackRef,
            LifecycleTerminalState, PRD17_LIFECYCLE_ACTION_SCHEMA, ProductionLifecycleApproval,
        },
    };
    use exo_dag_db_postgres::postgres::{
        context_packet_persistence::ContextPacketPostgresError,
        lifecycle_action::LifecycleActionPostgresError,
    };

    const SURFACE_TENANT: &str = "tenant-a";
    const SURFACE_AGENT: &str = "did:exo:agent";
    const SURFACE_NAMESPACE: &str = "project_memory_v3";

    fn surface_memory_ref(memory_id: &str) -> LifecycleMemoryRef {
        LifecycleMemoryRef {
            tenant_id: SURFACE_TENANT.to_owned(),
            project_id: "dag_db".to_owned(),
            memory_namespace: SURFACE_NAMESPACE.to_owned(),
            memory_id: memory_id.to_owned(),
        }
    }

    fn sample_lifecycle_action() -> LifecycleAction {
        let action_id = "lifecycle-writeback-d5";
        let validation_report_id = format!("validation-{action_id}");
        LifecycleAction {
            schema_version: PRD17_LIFECYCLE_ACTION_SCHEMA.to_owned(),
            action_id: action_id.to_owned(),
            action_type: LifecycleActionType::Writeback,
            tenant_id: SURFACE_TENANT.to_owned(),
            project_id: "dag_db".to_owned(),
            memory_namespace: SURFACE_NAMESPACE.to_owned(),
            actor_id: SURFACE_AGENT.to_owned(),
            source_packet_id: "packet-d5-001".to_owned(),
            source_receipt_id: "receipt-d5-001".to_owned(),
            parent_memory_ids: vec![surface_memory_ref("memory-parent-a")],
            target_memory_ids: vec![surface_memory_ref("memory-target-a")],
            validation_report_id: validation_report_id.clone(),
            policy_ref: "policy-d5-local-mutation".to_owned(),
            rollback_ref: LifecycleRollbackRef {
                rollback_id: format!("rollback-{action_id}"),
                action_id: action_id.to_owned(),
                inverse_action_type: LifecycleActionType::Writeback.inverse(),
                before_refs: vec![surface_memory_ref("memory-parent-a")],
                after_refs: vec![surface_memory_ref("memory-target-a")],
                validation_ref: validation_report_id,
                operator_required: true,
            },
            route_invalidation_event_ids: vec!["route-event-d5-001".to_owned()],
            evidence_refs: vec![LifecycleEvidenceRef {
                evidence_id: "evidence-d5-001".to_owned(),
                receipt_id: "receipt-evidence-d5-001".to_owned(),
                digest: "a".repeat(64),
                summary_ref: "summary-evidence-d5-001".to_owned(),
                preserved: true,
            }],
            terminal_state: LifecycleTerminalState::OperatorDeferred,
            production_lifecycle_approval: ProductionLifecycleApproval::OperatorDeferred,
            created_at: "2026-06-12T00:00:00Z".to_owned(),
        }
    }

    fn sample_default_route() -> DefaultRouteRecord {
        DefaultRouteRecord {
            schema_version: DEFAULT_ROUTE_SCHEMA_VERSION.to_owned(),
            route_id: "route-d5-default".to_owned(),
            request_id: "request-route-d5-default".to_owned(),
            tenant_id: SURFACE_TENANT.to_owned(),
            project_id: "dag_db".to_owned(),
            memory_namespace: SURFACE_NAMESPACE.to_owned(),
            status: DefaultRouteStatus::Active,
            route_source: DefaultRouteSource::Persisted,
            policy_ref: "policy:d5-default-route".to_owned(),
            freshness_ref: "freshness:current".to_owned(),
            policy_allowed: true,
            freshness_status: RouteFreshnessStatus::Current,
            invalidated: false,
            production_default_route_approval_status: "operator_deferred".to_owned(),
            packet_quality_review_status: "operator_deferred".to_owned(),
            selected_memory_refs: vec![DefaultRouteMemoryRef {
                memory_id: "memory-a".to_owned(),
                latest_receipt_hash: "memory-a-receipt".to_owned(),
                validation_status: "passed".to_owned(),
                citation_ref: "citation:memory-a".to_owned(),
            }],
            created_at: "hlc:1".to_owned(),
            updated_at: "hlc:2".to_owned(),
        }
    }

    fn sample_continuation_record() -> ContinuationRecord {
        ContinuationRecord {
            schema_version: PRD17_CONTINUATION_RECORD_SCHEMA.to_owned(),
            continuation_id: "continuation-d5-001".to_owned(),
            task_id: "task-d5-next-agent".to_owned(),
            tenant_id: SURFACE_TENANT.to_owned(),
            project_id: "dag_db".to_owned(),
            memory_namespace: SURFACE_NAMESPACE.to_owned(),
            actor_id: SURFACE_AGENT.to_owned(),
            route_id: "route-d5-default".to_owned(),
            summary_ref: "summary-continuation-d5-001".to_owned(),
            memory_refs: vec![surface_memory_ref("memory-target-a")],
            blocker_refs: vec!["blocker-production-lifecycle-approval-deferred".to_owned()],
            validation_refs: vec!["validation-continuation-d5-001".to_owned()],
            expiry_epoch_seconds: 2_000,
            later_retrieval_status: ContinuationRetrievalStatus::Pending,
            production_lifecycle_approval: ProductionLifecycleApproval::OperatorDeferred,
            created_at: "2026-06-12T00:00:00Z".to_owned(),
        }
    }

    fn sample_context_packet_record() -> ContextPacketRecord {
        ContextPacketRecord {
            schema_version: CONTEXT_PACKET_RECORD_SCHEMA_VERSION.to_owned(),
            packet_id: "packet-d5-001".to_owned(),
            route_id: "route-d5-001".to_owned(),
            query_hash: "query-hash-d5-001".to_owned(),
            tenant_id: SURFACE_TENANT.to_owned(),
            project_id: "dag_db".to_owned(),
            memory_namespace: SURFACE_NAMESPACE.to_owned(),
            selected_memory_ids: vec!["memory-d5-001".to_owned()],
            selected_edge_ids: Vec::new(),
            token_budget: 1_000,
            token_estimate: 200,
            context_quality: DefaultContextQuality::UsableContext,
            citation_coverage_bp: 10_000,
            validation_coverage_bp: 10_000,
            freshness_status: PacketFreshnessStatus::Current,
            validation_status: PacketValidationStatus::Passed,
            source_proof_refs: vec!["receipt-d5-001".to_owned()],
            fallback_reason: None,
            idempotency_key: canonical_idempotency_key("route-d5-001", "query-hash-d5-001", 1_000),
            persistence_status: PacketPersistenceStatus::ProofBound,
            production_default_route_approval_status: "operator_deferred".to_owned(),
            packet_quality_review_status: "operator_deferred".to_owned(),
            created_at: "2026-06-12T00:00:00Z".to_owned(),
        }
    }

    /// An unreachable (port-1) lazy pool, independent of
    /// `EXO_DAGDB_TEST_DATABASE_URL`. The consented-pass surface tests use this
    /// so they deterministically observe the gate-passed-then-DB-unavailable
    /// path and never write synthetic rows into a live shared store.
    fn unreachable_postgres_pool() -> PgPool {
        use std::time::Duration;

        use sqlx::postgres::{PgConnectOptions, PgPoolOptions};

        let options = PgConnectOptions::new()
            .host("127.0.0.1")
            .port(1)
            .username("postgres")
            .database("postgres");
        PgPoolOptions::new()
            .acquire_timeout(Duration::from_millis(100))
            .connect_lazy_with(options)
    }

    fn surface_service_consented(registry: Arc<IdentityRegistry>) -> DagDbGatekeeperService {
        DagDbGatekeeperService::new(
            unreachable_postgres_pool(),
            Arc::new(active_consent_engine(SURFACE_TENANT, SURFACE_AGENT)),
            registry,
        )
    }

    fn registry_for(keypair: &KeyPair) -> Arc<IdentityRegistry> {
        Arc::new(
            IdentityRegistry::default()
                .with_public_key(SURFACE_AGENT, *keypair.public_key().as_bytes()),
        )
    }

    /// Assert a consented + signed call passed the gate: the gatekeeper let it
    /// reach the persistence layer, so the outcome is either success or a
    /// classified database failure — never a consent/provenance rejection. This
    /// holds whether or not a live `EXO_DAGDB_TEST_DATABASE_URL` is configured
    /// (no DB => `surface_database_unavailable`; live DB => the write may
    /// succeed), so the test is robust under both the DB-independent suite and a
    /// live-DB run.
    fn assert_gate_passed<T>(result: Result<T, GatekeeperError>) {
        if let Err(error) = result {
            let detail = error.to_string();
            assert!(
                detail.contains("surface_database_unavailable"),
                "consented+signed call must not be gate-rejected; got: {detail}"
            );
            assert!(
                !detail.contains("ConsentRequired") && !detail.contains("ProvenanceVerifiable"),
                "consented+signed call must not be gate-rejected; got: {detail}"
            );
        }
    }

    // D5-S1: lifecycle action through the gate.
    #[tokio::test]
    async fn lifecycle_action_consented_signed_reaches_db_layer() {
        let keypair = KeyPair::generate();
        let action = sample_lifecycle_action();
        let payload_hash = lifecycle_action_payload_hash(&action).expect("payload hash");
        let signature = sign_write_payload(&keypair, &payload_hash).expect("signature");
        let service = surface_service_consented(registry_for(&keypair));
        let result = service
            .persist_lifecycle_action(&action, SURFACE_AGENT, &signature, None)
            .await;
        assert_gate_passed(result);
    }

    #[tokio::test]
    async fn lifecycle_action_unconsented_fails_closed_before_db() {
        let keypair = KeyPair::generate();
        let action = sample_lifecycle_action();
        let payload_hash = lifecycle_action_payload_hash(&action).expect("payload hash");
        let signature = sign_write_payload(&keypair, &payload_hash).expect("signature");
        let service = DagDbGatekeeperService::new(
            lazy_postgres_pool(),
            Arc::new(ConsentEngine::default()),
            registry_for(&keypair),
        );
        let err = service
            .persist_lifecycle_action(&action, SURFACE_AGENT, &signature, None)
            .await
            .expect_err("missing consent must fail closed");
        assert!(err.to_string().contains("ConsentRequired"), "{err}");
    }

    #[tokio::test]
    async fn lifecycle_action_forged_signature_fails_closed_before_db() {
        let keypair = KeyPair::generate();
        let forger = KeyPair::generate();
        let action = sample_lifecycle_action();
        let payload_hash = lifecycle_action_payload_hash(&action).expect("payload hash");
        let forged = sign_write_payload(&forger, &payload_hash).expect("forged signature");
        let service = surface_service_consented(registry_for(&keypair));
        let err = service
            .persist_lifecycle_action(&action, SURFACE_AGENT, &forged, None)
            .await
            .expect_err("forged signature must fail closed");
        assert!(err.to_string().contains("ProvenanceVerifiable"), "{err}");
    }

    // D5-S2: default route through the gate.
    #[tokio::test]
    async fn default_route_consented_signed_reaches_db_layer() {
        let keypair = KeyPair::generate();
        let route = sample_default_route();
        let payload_hash = default_route_payload_hash(&route).expect("payload hash");
        let signature = sign_write_payload(&keypair, &payload_hash).expect("signature");
        let service = surface_service_consented(registry_for(&keypair));
        let result = service
            .persist_default_route(&route, SURFACE_AGENT, &signature, None)
            .await;
        assert_gate_passed(result);
    }

    #[tokio::test]
    async fn default_route_unconsented_fails_closed_before_db() {
        let keypair = KeyPair::generate();
        let route = sample_default_route();
        let payload_hash = default_route_payload_hash(&route).expect("payload hash");
        let signature = sign_write_payload(&keypair, &payload_hash).expect("signature");
        let service = DagDbGatekeeperService::new(
            lazy_postgres_pool(),
            Arc::new(ConsentEngine::default()),
            registry_for(&keypair),
        );
        let err = service
            .persist_default_route(&route, SURFACE_AGENT, &signature, None)
            .await
            .expect_err("missing consent must fail closed");
        assert!(err.to_string().contains("ConsentRequired"), "{err}");
    }

    #[tokio::test]
    async fn default_route_forged_signature_fails_closed_before_db() {
        let keypair = KeyPair::generate();
        let forger = KeyPair::generate();
        let route = sample_default_route();
        let payload_hash = default_route_payload_hash(&route).expect("payload hash");
        let forged = sign_write_payload(&forger, &payload_hash).expect("forged signature");
        let service = surface_service_consented(registry_for(&keypair));
        let err = service
            .persist_default_route(&route, SURFACE_AGENT, &forged, None)
            .await
            .expect_err("forged signature must fail closed");
        assert!(err.to_string().contains("ProvenanceVerifiable"), "{err}");
    }

    // D5-S3: continuation + context-packet through the gate.
    #[tokio::test]
    async fn continuation_consented_signed_reaches_db_layer() {
        let keypair = KeyPair::generate();
        let record = sample_continuation_record();
        let payload_hash = continuation_record_payload_hash(&record).expect("payload hash");
        let signature = sign_write_payload(&keypair, &payload_hash).expect("signature");
        let service = surface_service_consented(registry_for(&keypair));
        let result = service
            .persist_continuation_record(&record, 1_000, SURFACE_AGENT, &signature, None)
            .await;
        assert_gate_passed(result);
    }

    #[tokio::test]
    async fn continuation_unconsented_fails_closed_before_db() {
        let keypair = KeyPair::generate();
        let record = sample_continuation_record();
        let payload_hash = continuation_record_payload_hash(&record).expect("payload hash");
        let signature = sign_write_payload(&keypair, &payload_hash).expect("signature");
        let service = DagDbGatekeeperService::new(
            lazy_postgres_pool(),
            Arc::new(ConsentEngine::default()),
            registry_for(&keypair),
        );
        let err = service
            .persist_continuation_record(&record, 1_000, SURFACE_AGENT, &signature, None)
            .await
            .expect_err("missing consent must fail closed");
        assert!(err.to_string().contains("ConsentRequired"), "{err}");
    }

    #[tokio::test]
    async fn continuation_forged_signature_fails_closed_before_db() {
        let keypair = KeyPair::generate();
        let forger = KeyPair::generate();
        let record = sample_continuation_record();
        let payload_hash = continuation_record_payload_hash(&record).expect("payload hash");
        let forged = sign_write_payload(&forger, &payload_hash).expect("forged signature");
        let service = surface_service_consented(registry_for(&keypair));
        let err = service
            .persist_continuation_record(&record, 1_000, SURFACE_AGENT, &forged, None)
            .await
            .expect_err("forged signature must fail closed");
        assert!(err.to_string().contains("ProvenanceVerifiable"), "{err}");
    }

    #[tokio::test]
    async fn context_packet_record_consented_signed_reaches_db_layer() {
        let keypair = KeyPair::generate();
        let record = sample_context_packet_record();
        let payload_hash = context_packet_record_payload_hash(&record).expect("payload hash");
        let signature = sign_write_payload(&keypair, &payload_hash).expect("signature");
        let service = surface_service_consented(registry_for(&keypair));
        let result = service
            .persist_context_packet_record(&record, SURFACE_AGENT, &signature, None)
            .await;
        assert_gate_passed(result);
    }

    #[tokio::test]
    async fn context_packet_record_unconsented_fails_closed_before_db() {
        let keypair = KeyPair::generate();
        let record = sample_context_packet_record();
        let payload_hash = context_packet_record_payload_hash(&record).expect("payload hash");
        let signature = sign_write_payload(&keypair, &payload_hash).expect("signature");
        let service = DagDbGatekeeperService::new(
            lazy_postgres_pool(),
            Arc::new(ConsentEngine::default()),
            registry_for(&keypair),
        );
        let err = service
            .persist_context_packet_record(&record, SURFACE_AGENT, &signature, None)
            .await
            .expect_err("missing consent must fail closed");
        assert!(err.to_string().contains("ConsentRequired"), "{err}");
    }

    #[tokio::test]
    async fn context_packet_record_forged_signature_fails_closed_before_db() {
        let keypair = KeyPair::generate();
        let forger = KeyPair::generate();
        let record = sample_context_packet_record();
        let payload_hash = context_packet_record_payload_hash(&record).expect("payload hash");
        let forged = sign_write_payload(&forger, &payload_hash).expect("forged signature");
        let service = surface_service_consented(registry_for(&keypair));
        let err = service
            .persist_context_packet_record(&record, SURFACE_AGENT, &forged, None)
            .await
            .expect_err("forged signature must fail closed");
        assert!(err.to_string().contains("ProvenanceVerifiable"), "{err}");
    }

    // D5-S4: error classification — a contract/replay reject carries the
    // `metadata rejected` marker (422 at the gateway); a DB/transaction outage
    // carries the `surface_database_unavailable` marker (503). The two markers
    // are mutually exclusive so a reject is never reported as a DB outage and a
    // DB outage is never reported as a policy reject.
    #[test]
    fn surface_db_failure_classified_as_unavailable() {
        let db_error = LifecycleActionPostgresError::Postgres {
            source: sqlx::Error::PoolClosed,
        };
        let mapped = domain_blocked("lifecycle_action_postgres", &db_error);
        let detail = mapped.to_string();
        assert!(detail.contains("surface_database_unavailable"), "{detail}");
        assert!(!detail.contains("metadata rejected"), "{detail}");
    }

    #[test]
    fn surface_contract_reject_classified_as_metadata_rejected() {
        let json_error = ContextPacketPostgresError::UnsafeReplay {
            packet_id: "packet-d5-001".to_owned(),
        };
        let mapped = domain_blocked("context_packet_record_postgres", &json_error);
        let detail = mapped.to_string();
        assert!(detail.contains("metadata rejected"), "{detail}");
        assert!(!detail.contains("surface_database_unavailable"), "{detail}");
    }
}