axon-lang 1.38.5

AXON v1.5.1 — first crates.io publication of the AXON language full-stack runtime. Lexer/parser/type-checker/IR generator (re-exported from axon-frontend) plus the native Rust runtime: typed channels (TypedEventBus with QoS×5, π-calculus mobility, capability extrusion via shield D8 — Fase 13.f.2), Free Monad CPS handlers (Fase 2), lease kernel + reconcile loop (Fase 3+5), Epistemic Security Kernel (ESK Fase 6), Trust Types + ReplayLog (Fase 11.a+11.c), Stateful PEM over WebSocket (Fase 11.d), Ontological Tool Synthesis (Fase 11.e), Mobile Typed Channels (Fase 13). Crate publishes as `axon-lang` to mirror the Python PyPI package; library import remains `use axon::*` so existing call sites keep working unchanged.
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
//! §Fase 35.d (v1.30.0) — `StoreRegistry`, the closed-catalog
//! SQL-vs-KV dispatch chokepoint of the `axonstore` cognitive data
//! plane.
//!
//! The registry is the **single point** at which a store-op's
//! `store_name` is resolved to a backend. Both execution paths — the
//! sync runner (35.e) and the streaming dispatcher (35.f) — route
//! through it, so there is exactly one SQL-vs-KV decision site and no
//! path divergence (the SSE-gap lesson).
//!
//! # D2 — store resolution is a total function over a closed catalog
//!
//! [`StoreRegistry::build`] is the catalog gate: every `IRAxonStore`'s
//! `backend` must classify into the closed set `{in_memory,
//! postgresql}` ([`classify_backend`]). An unknown backend (`sqlite`,
//! `mysql`, a typo) fails the build with a named [`RegistryError`] —
//! pure, no I/O, fail-fast at deploy. After build, [`resolve`] is
//! total: every `store_name` yields a [`StoreHandle`] or a typed
//! [`StoreError`] — never a panic.
//!
//! [`resolve`]: StoreRegistry::resolve
//!
//! # D3 — zero regression on the key-value path (absolute)
//!
//! `in_memory` is the **implicit default**: a store that is undeclared,
//! declared with an empty `backend`, or declared `in_memory` resolves
//! to [`StoreHandle::InMemory`] — the byte-identical pre-35 key-value
//! path. The SQL path is entered *iff* a matching `IRAxonStore` has
//! `backend == "postgresql"`.
//!
//! Crucially: a declared `postgresql` store whose connection cannot be
//! resolved (a missing `env:` variable, a malformed DSN) yields a typed
//! error — **never** a silent fallback to the key-value store. Silently
//! degrading a misconfigured SQL store to KV would lose writes and
//! serve stale reads; the registry refuses to do it.
//!
//! # Lazy, per-DSN pool cache (D7)
//!
//! The registry build is pure (catalog validation only). A
//! `PostgresStoreBackend` — and therefore its pool and its `env:`
//! resolution — is created on the **first** `resolve` of a given
//! postgresql store, then cached **by resolved DSN**: stores that share
//! a DSN share one pool. A store that is never used never resolves its
//! connection — so a broken `postgresql` store cannot break an
//! unrelated `in_memory` flow (D3).

use std::collections::HashMap;
use std::fmt;
use std::sync::Mutex;

use crate::ir_nodes::{IRAxonStore, IRStoreColumnSchema};
use crate::store::postgres_backend::{
    resolve_dsn, PostgresStoreBackend, StoreError,
};
use crate::store_schema::StoreColumnType;
use crate::store_schema_manifest::{Manifest, ManifestStore};

// ════════════════════════════════════════════════════════════════════
//  Closed backend catalog (D2)
// ════════════════════════════════════════════════════════════════════

/// The closed catalog of `axonstore` backends honored by the v1.30.0
/// runtime. Growth (e.g. `sqlite`) is a deliberate language decision —
/// a new variant here plus a backend implementation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StoreBackendKind {
    /// The in-process key-value path — the pre-35 behavior, and the
    /// implicit default for an undeclared or empty-`backend` store.
    InMemory,
    /// A `sqlx::PgPool`-backed SQL store (35.c `PostgresStoreBackend`).
    Postgresql,
}

impl StoreBackendKind {
    /// The canonical `backend:` spelling.
    pub fn as_str(self) -> &'static str {
        match self {
            StoreBackendKind::InMemory => "in_memory",
            StoreBackendKind::Postgresql => "postgresql",
        }
    }
}

impl fmt::Display for StoreBackendKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Classify an `IRAxonStore.backend` string into the closed catalog.
///
/// The match is trimmed + case-insensitive. An empty string is the
/// implicit `in_memory` default. `None` means the value is outside the
/// closed catalog — the caller turns that into a build error.
pub fn classify_backend(backend: &str) -> Option<StoreBackendKind> {
    match backend.trim().to_ascii_lowercase().as_str() {
        "" | "in_memory" => Some(StoreBackendKind::InMemory),
        "postgresql" => Some(StoreBackendKind::Postgresql),
        _ => None,
    }
}

// ════════════════════════════════════════════════════════════════════
//  Build-phase error catalog
// ════════════════════════════════════════════════════════════════════

/// A failure building a [`StoreRegistry`] from `IRProgram`'s
/// `axonstore_specs`. These are deploy-time errors — pure, no I/O.
#[derive(Debug, Clone, PartialEq)]
pub enum RegistryError {
    /// An `axonstore` declares a `backend` outside the closed catalog.
    UnknownBackend { store: String, backend: String },
    /// Two `axonstore` declarations share a name.
    DuplicateStore { store: String },
}

impl fmt::Display for RegistryError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            RegistryError::UnknownBackend { store, backend } => write!(
                f,
                "axonstore `{store}` declares unknown backend `{backend}` \
                 — the v1.30.0 closed catalog is {{in_memory, postgresql}} \
                 (sqlite is a documented future fase)"
            ),
            RegistryError::DuplicateStore { store } => write!(
                f,
                "axonstore `{store}` is declared more than once — store \
                 names must be unique"
            ),
        }
    }
}

impl std::error::Error for RegistryError {}

// ════════════════════════════════════════════════════════════════════
//  Store handle — the resolved dispatch target
// ════════════════════════════════════════════════════════════════════

/// The resolved backend for a store operation. The runner (35.e) and
/// the dispatcher (35.f) match on this to route to SQL or to the
/// key-value path.
#[derive(Debug, Clone)]
pub enum StoreHandle {
    /// The in-process key-value path (D3 — byte-identical to pre-35).
    InMemory,
    /// A Postgres-backed store, with its (shared, cached) backend.
    Postgres(PostgresStoreBackend),
}

impl StoreHandle {
    /// `true` iff this resolves to the key-value path.
    pub fn is_in_memory(&self) -> bool {
        matches!(self, StoreHandle::InMemory)
    }

    /// `true` iff this resolves to the SQL path.
    pub fn is_postgres(&self) -> bool {
        matches!(self, StoreHandle::Postgres(_))
    }
}

// ════════════════════════════════════════════════════════════════════
//  §Fase 37.x.g (D8) — deploy-time schema-verification report
// ════════════════════════════════════════════════════════════════════

/// The outcome of [`StoreRegistry::verify_postgres_schemas`] — the
/// eager, deploy-time check that every declared `postgresql` store's
/// table resolves against the live database (D8). The failure of a
/// store schema moves from the first production request to deploy.
#[derive(Debug, Default, Clone, PartialEq)]
pub struct SchemaVerifyReport {
    /// Stores whose table resolved + introspected cleanly at deploy.
    /// Their schema is now a deploy-verified contract — warm in the
    /// process cache before the first runtime operation.
    pub verified: Vec<String>,
    /// Stores REACHABLE at deploy whose table does not resolve —
    /// `(store_name, diagnostic)`. A FATAL deploy error (D8
    /// fail-closed): a flow's store table is genuinely missing /
    /// ambiguous and would otherwise fail at runtime.
    pub missing: Vec<(String, String)>,
    /// Stores UNREACHABLE or unconfigured at deploy — `(store_name,
    /// diagnostic)`. A NON-fatal warning: the deploy proceeds and
    /// resolution defers to the D9 runtime path. "Deploy is honest,
    /// never brittle" — a transiently-down database does not block a
    /// deploy.
    pub unreachable: Vec<(String, String)>,
}

impl SchemaVerifyReport {
    /// `true` iff the deploy must FAIL — at least one reachable store
    /// has a table that does not resolve (D8 fail-closed).
    pub fn has_fatal(&self) -> bool {
        !self.missing.is_empty()
    }

    /// A human-readable summary of the fatal failures, for the deploy
    /// error response. Empty when there are none.
    pub fn fatal_summary(&self) -> String {
        if self.missing.is_empty() {
            return String::new();
        }
        let detail = self
            .missing
            .iter()
            .map(|(store, diag)| format!("`{store}` — {diag}"))
            .collect::<Vec<_>>()
            .join("; ");
        format!(
            "deploy-time store-schema verification failed: {} declared \
             postgresql store table(s) do not resolve on a reachable \
             database: {detail}",
            self.missing.len()
        )
    }
}

// ════════════════════════════════════════════════════════════════════
//  Registered store entry
// ════════════════════════════════════════════════════════════════════

/// One validated `axonstore` declaration held by the registry.
#[derive(Debug, Clone)]
struct RegisteredStore {
    spec: IRAxonStore,
    kind: StoreBackendKind,
}

// ════════════════════════════════════════════════════════════════════
//  StoreRegistry
// ════════════════════════════════════════════════════════════════════

/// The closed-catalog store resolver. Built once from a program's
/// `axonstore` declarations; shared (behind an `Arc`) across concurrent
/// dispatch. `Send + Sync`.
pub struct StoreRegistry {
    /// `store_name` → its validated declaration.
    stores: HashMap<String, RegisteredStore>,
    /// resolved DSN → connected backend. Lazy: an entry appears on the
    /// first `resolve` of a postgresql store with that DSN. Stores that
    /// share a DSN share one pool.
    pool_cache: Mutex<HashMap<String, PostgresStoreBackend>>,
}

impl fmt::Debug for StoreRegistry {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // List names + kinds only — never dump raw `connection` strings
        // (a literal DSN can carry a password).
        let mut kinds: Vec<(&str, StoreBackendKind)> = self
            .stores
            .iter()
            .map(|(name, r)| (name.as_str(), r.kind))
            .collect();
        kinds.sort_by(|a, b| a.0.cmp(b.0));
        f.debug_struct("StoreRegistry")
            .field("stores", &kinds)
            .field("cached_pools", &self.cached_pool_count())
            .finish()
    }
}

impl StoreRegistry {
    /// Build a registry from a program's `axonstore` declarations.
    ///
    /// D2 catalog gate — pure, no I/O. Fails fast if any declaration
    /// names an unknown backend or if two declarations collide on name.
    /// Connection validity is **not** checked here — that is resolved
    /// lazily, per store, so a broken `postgresql` store cannot fail
    /// the build for an unrelated `in_memory` flow (D3).
    pub fn build(specs: &[IRAxonStore]) -> Result<StoreRegistry, RegistryError> {
        let mut stores: HashMap<String, RegisteredStore> =
            HashMap::with_capacity(specs.len());

        for spec in specs {
            let kind = classify_backend(&spec.backend).ok_or_else(|| {
                RegistryError::UnknownBackend {
                    store: spec.name.clone(),
                    backend: spec.backend.clone(),
                }
            })?;
            if stores.contains_key(&spec.name) {
                return Err(RegistryError::DuplicateStore {
                    store: spec.name.clone(),
                });
            }
            stores.insert(
                spec.name.clone(),
                RegisteredStore { spec: spec.clone(), kind },
            );
        }

        Ok(StoreRegistry {
            stores,
            pool_cache: Mutex::new(HashMap::new()),
        })
    }

    /// An empty registry — a program that declares no `axonstore`. Every
    /// `resolve` then yields [`StoreHandle::InMemory`] (D3).
    pub fn empty() -> StoreRegistry {
        StoreRegistry {
            stores: HashMap::new(),
            pool_cache: Mutex::new(HashMap::new()),
        }
    }

    /// Resolve a store name to its dispatch target.
    ///
    /// - An undeclared store, or one declared `in_memory` / empty →
    ///   [`StoreHandle::InMemory`] (the implicit default, D3).
    /// - A `postgresql` store → [`StoreHandle::Postgres`], its backend
    ///   lazily connected and cached by resolved DSN.
    /// - A `postgresql` store whose connection cannot be resolved → a
    ///   typed [`StoreError`]. **Never** a silent KV fallback.
    ///
    /// Total: every input yields `Ok(handle)` or `Err(StoreError)`.
    /// Must be called within a Tokio runtime context when it may
    /// connect a postgresql backend (the lazy pool, per 35.c).
    pub fn resolve(&self, store_name: &str) -> Result<StoreHandle, StoreError> {
        let registered = match self.stores.get(store_name) {
            // Undeclared → implicit in_memory default (D3 — pre-35
            // behavior: a store needs no declaration to be key-value).
            None => return Ok(StoreHandle::InMemory),
            Some(r) => r,
        };

        match registered.kind {
            StoreBackendKind::InMemory => Ok(StoreHandle::InMemory),
            StoreBackendKind::Postgresql => {
                // Resolve the DSN first — this is the cache key, and
                // the point at which a missing `env:` var surfaces as
                // a typed error rather than a silent KV fallback.
                let dsn = resolve_dsn(&registered.spec.connection)?;

                let mut cache = self.lock_cache();
                if let Some(backend) = cache.get(&dsn) {
                    return Ok(StoreHandle::Postgres(backend.clone()));
                }
                let backend = PostgresStoreBackend::connect_named(
                    &registered.spec.connection,
                    store_name,
                )?;
                cache.insert(dsn, backend.clone());
                Ok(StoreHandle::Postgres(backend))
            }
        }
    }

    /// §Fase 37.x.g (D8) — EAGERLY verify every declared `postgresql`
    /// store's schema against the live database, at deploy time.
    ///
    /// For each `postgresql` store — the table name is the store name
    /// (D12) — the backend is resolved and the table introspected NOW:
    /// the resolution + schema become a deploy-verified contract, warm
    /// in the process cache before the first runtime operation.
    ///
    /// A store REACHABLE at deploy whose table does not resolve is a
    /// FATAL [`SchemaVerifyReport::missing`] entry (the deploy fails —
    /// D8 fail-closed); a store unreachable / unconfigured at deploy is
    /// a non-fatal [`SchemaVerifyReport::unreachable`] warning (the
    /// deploy proceeds — "honest, never brittle" — and the D9 runtime
    /// resolution still applies). `in_memory` stores are skipped.
    ///
    /// Must be called within a Tokio runtime context.
    pub async fn verify_postgres_schemas(&self) -> SchemaVerifyReport {
        self.verify_postgres_schemas_with_manifest(None).await
    }

    /// §Fase 38.f (D3 + D8 strengthening) — extended deploy-time
    /// verification that honors a declared column schema on each
    /// `axonstore`.
    ///
    /// When the optional `manifest` argument is `Some`, the verifier
    /// resolves the three closed Fase 38 `schema:` declaration forms
    /// against it:
    ///
    ///   * **Form (a) — inline column block** — the columns live on the
    ///     IR. The verifier proves every declared column EXISTS in the
    ///     live introspection AND its type matches the declared
    ///     [`StoreColumnType`]. A mismatch is a
    ///     [`StoreError::DeclaredVsLiveDrift`] fatal entry (axon-T807).
    ///
    ///   * **Form (b) — manifest reference** (`schema: "qualified.name"`)
    ///     — the verifier looks up the manifest entry; missing entry is
    ///     a fatal `missing` row; present entry is proven against live
    ///     identically to form (a).
    ///
    ///   * **Form (c) — per-tenant env-var namespace**
    ///     (`schema: env:VAR`) — the verifier resolves the env var; a
    ///     missing var is [`StoreError::MissingPerTenantSchemaEnv`]
    ///     (axon-T806). The resolved namespace prefixes the manifest
    ///     lookup key (`<namespace>.<store_name>`) AND the connection's
    ///     `application_name` (`axon-store/<store>/<namespace>` — Gap-3
    ///     inheritance) so a DBA sees the resolved tenant on every
    ///     session.
    ///
    /// `None` manifest preserves the 37.x verification verbatim — only
    /// table existence is proven, declared columns are not inspected.
    /// `None` is also what the v1.37.0 deploy handler passes today.
    ///
    /// Honest scope (38.f.1): NOT-NULL parity is NOT yet proven by
    /// T807 — the 37.x introspection query doesn't capture `attnotnull`.
    /// The runtime catches NOT-NULL drift via SQLSTATE 23502 at the
    /// first failing `persist`, so defense-in-depth remains. A 38.f.2
    /// follow-on can extend `introspect_conn` to include nullability.
    pub async fn verify_postgres_schemas_with_manifest(
        &self,
        manifest: Option<&Manifest>,
    ) -> SchemaVerifyReport {
        let mut report = SchemaVerifyReport::default();
        let mut pg_stores: Vec<&str> = self
            .stores
            .iter()
            .filter(|(_, r)| r.kind == StoreBackendKind::Postgresql)
            .map(|(name, _)| name.as_str())
            .collect();
        pg_stores.sort_unstable();

        for name in pg_stores {
            let column_schema = self
                .stores
                .get(name)
                .and_then(|r| r.spec.column_schema.clone());

            // §38.f — resolve per-tenant env-var FIRST when present
            // (form c), so a T806 fails fast without touching the DB.
            let resolved_namespace = match &column_schema {
                Some(IRStoreColumnSchema::EnvVar { var_name }) => {
                    match std::env::var(var_name) {
                        Ok(v) if !v.trim().is_empty() => Some(v),
                        _ => {
                            let err = StoreError::MissingPerTenantSchemaEnv {
                                store: name.to_string(),
                                var: var_name.clone(),
                            };
                            report
                                .missing
                                .push((name.to_string(), err.to_string()));
                            continue;
                        }
                    }
                }
                _ => None,
            };

            // §38.f — for form (c) with a resolved namespace, REPLACE
            // the pool-cache entry with a namespace-stamped backend
            // so every runtime session carries the tenant in its
            // `application_name`. The replacement is idempotent — a
            // re-verify of the same store with the same namespace is
            // a no-op.
            if let Some(ns) = &resolved_namespace {
                if let Err(e) = self.restamp_backend_with_namespace(name, ns) {
                    report.missing.push((name.to_string(), e.to_string()));
                    continue;
                }
            }

            match self.resolve(name) {
                Ok(StoreHandle::Postgres(backend)) => {
                    let masked = backend.masked_dsn();
                    match backend.warm_schema(name).await {
                        Ok(()) => {
                            // §38.f D8 strengthening — when a column
                            // schema is declared, compare declared
                            // columns vs live introspection (T807).
                            if let Some(drift) = verify_declared_columns(
                                name,
                                &backend,
                                column_schema.as_ref(),
                                resolved_namespace.as_deref(),
                                manifest,
                                &masked,
                            ) {
                                report.missing.push((name.to_string(), drift));
                            } else {
                                report.verified.push(name.to_string());
                            }
                        }
                        Err(
                            e @ (StoreError::TableNotResolved { .. }
                            | StoreError::AmbiguousTable { .. }),
                        ) => {
                            // Reachable store, table genuinely missing
                            // / ambiguous — a fatal deploy error.
                            report.missing.push((
                                name.to_string(),
                                format!("{e} (database: {masked})"),
                            ));
                        }
                        Err(e) => {
                            // Unreachable / transient — non-fatal.
                            report.unreachable.push((
                                name.to_string(),
                                format!("{e} (database: {masked})"),
                            ));
                        }
                    }
                }
                // `kind` is Postgresql — `resolve` cannot yield InMemory.
                Ok(StoreHandle::InMemory) => {}
                Err(e) => {
                    // The connection could not even be resolved (a
                    // missing `env:` var, a malformed DSN) — non-fatal;
                    // the store is unconfigured at deploy time. No
                    // backend was constructed, so no masked DSN to
                    // append: the error text already names the
                    // configuration site.
                    report
                        .unreachable
                        .push((name.to_string(), e.to_string()));
                }
            }
        }
        report
    }

    /// §Fase 38.f (D3) — re-stamp a postgresql store's pooled backend
    /// with the resolved per-tenant namespace so every session's
    /// `application_name` carries `axon-store/<store>/<namespace>`.
    ///
    /// Idempotent: if a backend is already cached for the resolved
    /// DSN, it is REPLACED. Future `resolve(<store>)` calls hand out
    /// the new namespace-stamped pool from the cache. The old pool's
    /// connections are dropped on the next acquire.
    fn restamp_backend_with_namespace(
        &self,
        store_name: &str,
        namespace: &str,
    ) -> Result<(), StoreError> {
        let registered = self.stores.get(store_name).ok_or_else(|| {
            StoreError::Query {
                op: "verify",
                source: format!("axonstore `{store_name}` is not declared"),
            }
        })?;
        let dsn = resolve_dsn(&registered.spec.connection)?;
        let backend = PostgresStoreBackend::connect_named_with_namespace(
            &registered.spec.connection,
            store_name,
            Some(namespace),
        )?;
        let mut cache = self.lock_cache();
        cache.insert(dsn, backend);
        Ok(())
    }

    /// The declaration backing a store name, if any. The pillars
    /// consult it — 35.g for `confidence_floor`, 35.h for `on_breach`.
    pub fn spec(&self, store_name: &str) -> Option<&IRAxonStore> {
        self.stores.get(store_name).map(|r| &r.spec)
    }

    /// The backend kind a store name resolves to, if declared.
    pub fn backend_kind(&self, store_name: &str) -> Option<StoreBackendKind> {
        self.stores.get(store_name).map(|r| r.kind)
    }

    /// The number of declared stores.
    pub fn len(&self) -> usize {
        self.stores.len()
    }

    /// `true` iff no `axonstore` is declared.
    pub fn is_empty(&self) -> bool {
        self.stores.is_empty()
    }

    /// The number of distinct connection pools currently cached — one
    /// per resolved DSN actually used. Useful for a health surface.
    pub fn cached_pool_count(&self) -> usize {
        self.lock_cache().len()
    }

    /// Lock the pool cache, recovering the guard if a prior holder
    /// panicked (the critical section only does infallible map ops, so
    /// poisoning is effectively impossible — but recovery keeps the
    /// registry panic-free regardless).
    fn lock_cache(
        &self,
    ) -> std::sync::MutexGuard<'_, HashMap<String, PostgresStoreBackend>> {
        self.pool_cache
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
    }
}

// ════════════════════════════════════════════════════════════════════
//  §Fase 38.f (D8 strengthening) — declared-vs-live column verification
// ════════════════════════════════════════════════════════════════════

/// Resolve the declared columns for a store from its IR `column_schema`
/// + the optional deploy-time manifest. Returns:
///
///   - `Ok(Some(columns))` — declared columns are known; the caller
///     proves them against the live introspection.
///   - `Ok(None)` — no schema declaration OR the manifest lookup
///     couldn't find a matching entry (form b/c without manifest in
///     scope today). The 37.x existence-only verification suffices.
///   - `Err(_)` — propagated up as a fatal `missing` row.
fn declared_columns_for(
    store_name: &str,
    column_schema: Option<&IRStoreColumnSchema>,
    resolved_namespace: Option<&str>,
    manifest: Option<&Manifest>,
) -> Result<Option<std::collections::BTreeMap<String, StoreColumnType>>, String> {
    let Some(schema) = column_schema else {
        return Ok(None);
    };
    match schema {
        IRStoreColumnSchema::Inline { columns } => {
            let mut out = std::collections::BTreeMap::new();
            for col in columns {
                let Some(ty) = StoreColumnType::from_token(&col.col_type) else {
                    return Err(format!(
                        "axonstore `{store_name}` inline schema column \
                         `{}` declares unknown type `{}` — the closed \
                         catalog is {{{}}}",
                        col.name,
                        col.col_type,
                        StoreColumnType::all_canonical_names().join(", ")
                    ));
                };
                out.insert(col.name.clone(), ty);
            }
            Ok(Some(out))
        }
        IRStoreColumnSchema::ManifestRef { qualified_name } => {
            let Some(m) = manifest else {
                // No manifest available at deploy time — fall through
                // to 37.x existence-only verification. 38.h's CLI +
                // 38.j's CI lane plumb the manifest; until then the
                // deploy is honest about this gap (no T807 raised
                // for what we can't prove).
                return Ok(None);
            };
            let Some(store) = m.lookup(qualified_name) else {
                return Err(format!(
                    "axonstore `{store_name}` declares `schema: \
                     \"{qualified_name}\"` but no manifest entry \
                     matches that qualified name. Available manifest \
                     entries: {{{}}}.",
                    m.stores.keys().cloned().collect::<Vec<_>>().join(", ")
                ));
            };
            Ok(Some(manifest_store_to_btreemap(store)))
        }
        IRStoreColumnSchema::EnvVar { .. } => {
            let Some(m) = manifest else {
                return Ok(None);
            };
            let ns = resolved_namespace.unwrap_or("");
            let key = format!("{ns}.{store_name}");
            if let Some(store) = m.lookup(&key) {
                return Ok(Some(manifest_store_to_btreemap(store)));
            }
            // First-match heuristic mirrors 38.d's `load_columns_for_schema`:
            // when an exact `<namespace>.<store>` entry is missing,
            // accept any `*.<store_name>` shape (per-tenant schemas
            // typically have identical column shapes at deploy time).
            let suffix = format!(".{store_name}");
            for (k, s) in &m.stores {
                if k.ends_with(&suffix) {
                    return Ok(Some(manifest_store_to_btreemap(s)));
                }
            }
            // Manifest present but no matching entry — honest fall-
            // through to existence-only (not T807, because the
            // manifest is the proof source).
            Ok(None)
        }
    }
}

fn manifest_store_to_btreemap(
    s: &ManifestStore,
) -> std::collections::BTreeMap<String, StoreColumnType> {
    let mut out = std::collections::BTreeMap::new();
    for (col_name, col) in &s.columns {
        out.insert(col_name.clone(), col.col_type);
    }
    out
}

/// Compare a store's DECLARED columns against the LIVE introspected
/// columns. Returns `Some(drift_summary)` when they disagree (the
/// caller surfaces this as an axon-T807 fatal entry); `None` when
/// the declared shape matches live (or when there's nothing to prove
/// — no schema declared, or a form b/c without a matching manifest
/// entry).
///
/// The check has two arms: every declared column EXISTS in live
/// introspection (column-name match) AND its type matches the
/// declared [`StoreColumnType`] under [`pg_udt_matches_catalog_type`].
/// Honest scope: NOT-NULL parity is NOT yet checked — the 37.x
/// introspection query doesn't capture `attnotnull`. Documented as
/// 38.f.1 deferral.
fn verify_declared_columns(
    store_name: &str,
    backend: &PostgresStoreBackend,
    column_schema: Option<&IRStoreColumnSchema>,
    resolved_namespace: Option<&str>,
    manifest: Option<&Manifest>,
    masked_dsn: &str,
) -> Option<String> {
    let declared = match declared_columns_for(
        store_name,
        column_schema,
        resolved_namespace,
        manifest,
    ) {
        Ok(Some(d)) => d,
        Ok(None) => return None, // nothing to prove — preserve 37.x behavior
        Err(msg) => return Some(format!("{msg} (database: {masked_dsn})")),
    };
    let cached = backend.cached_schema(store_name);
    let Some(resolved) = cached else {
        // warm_schema just succeeded, so the cache should be hot.
        // Defensive fall-through.
        return None;
    };
    let live = &resolved.column_types;

    let mut missing_cols: Vec<String> = Vec::new();
    let mut type_drifts: Vec<String> = Vec::new();
    for (col_name, declared_type) in &declared {
        match live.get(col_name) {
            None => missing_cols.push(col_name.clone()),
            Some(pg_udt) => {
                if !pg_udt_matches_catalog_type(pg_udt, *declared_type) {
                    type_drifts.push(format!(
                        "`{col_name}` declared as `{}` but live type is `{pg_udt}`",
                        declared_type.canonical_name()
                    ));
                }
            }
        }
    }

    if missing_cols.is_empty() && type_drifts.is_empty() {
        return None;
    }

    let mut parts: Vec<String> = Vec::new();
    if !missing_cols.is_empty() {
        parts.push(format!(
            "missing on live database: {{{}}}",
            missing_cols.join(", ")
        ));
    }
    if !type_drifts.is_empty() {
        parts.push(format!("type mismatches: {{{}}}", type_drifts.join("; ")));
    }
    let drift = parts.join("; ");
    let err = StoreError::DeclaredVsLiveDrift {
        store: store_name.to_string(),
        drift,
    };
    Some(format!("{err} (database: {masked_dsn})"))
}

/// `true` iff the live introspected Postgres UDT name is compatible
/// with the declared axon-language [`StoreColumnType`]. The matrix
/// mirrors the v1.30.0 runtime `classify_pg_type` mapping — `Text`
/// accepts `text`/`varchar`/`bpchar`/`name`; `Int` accepts `int4`;
/// `BigInt` accepts `int8`; etc. Case-insensitive (Postgres lower-
/// cases udt names by convention).
fn pg_udt_matches_catalog_type(udt: &str, declared: StoreColumnType) -> bool {
    let u = udt.to_ascii_lowercase();
    use StoreColumnType as C;
    match declared {
        C::Uuid => u == "uuid",
        C::Text => matches!(u.as_str(), "text" | "varchar" | "bpchar" | "name"),
        C::Int => matches!(u.as_str(), "int4" | "integer"),
        C::BigInt => matches!(u.as_str(), "int8" | "bigint"),
        C::Float => matches!(u.as_str(), "float4" | "real"),
        C::Double => matches!(u.as_str(), "float8" | "double precision"),
        C::Bool => u == "bool" || u == "boolean",
        C::Timestamptz => u == "timestamptz",
        C::Timestamp => u == "timestamp",
        C::Date => u == "date",
        C::Time => u == "time",
        C::Jsonb => u == "jsonb",
        C::Json => u == "json",
        C::Bytea => u == "bytea",
        C::Numeric => matches!(u.as_str(), "numeric" | "decimal"),
    }
}

// ════════════════════════════════════════════════════════════════════
//  Unit tests
// ════════════════════════════════════════════════════════════════════

#[cfg(test)]
mod tests {
    use super::*;

    /// Build an `IRAxonStore` test fixture.
    fn spec(name: &str, backend: &str, connection: &str) -> IRAxonStore {
        IRAxonStore {
            node_type: "axonstore",
            source_line: 0,
            source_column: 0,
            name: name.to_string(),
            backend: backend.to_string(),
            connection: connection.to_string(),
            confidence_floor: None,
            isolation: String::new(),
            on_breach: String::new(),
            capability: String::new(),
            column_schema: None,
        }
    }

    // ── classify_backend ─────────────────────────────────────────────

    #[test]
    fn classify_postgresql() {
        assert_eq!(
            classify_backend("postgresql"),
            Some(StoreBackendKind::Postgresql)
        );
    }

    #[test]
    fn classify_in_memory_and_empty_default() {
        assert_eq!(
            classify_backend("in_memory"),
            Some(StoreBackendKind::InMemory)
        );
        assert_eq!(classify_backend(""), Some(StoreBackendKind::InMemory));
    }

    #[test]
    fn classify_is_trimmed_and_case_insensitive() {
        assert_eq!(
            classify_backend("  PostgreSQL  "),
            Some(StoreBackendKind::Postgresql)
        );
        assert_eq!(
            classify_backend("IN_MEMORY"),
            Some(StoreBackendKind::InMemory)
        );
    }

    #[test]
    fn classify_unknown_backends_are_none() {
        // `sqlite` / `mysql` are syntactically valid in the frontend
        // but outside the v1.30.0 runtime catalog.
        for backend in ["sqlite", "mysql", "postgres", "mongodb", "redis"] {
            assert_eq!(classify_backend(backend), None, "backend {backend}");
        }
    }

    // ── build — D2 catalog gate ──────────────────────────────────────

    #[test]
    fn build_accepts_valid_specs() {
        let specs = [
            spec("cache", "in_memory", ""),
            spec("tenants", "postgresql", "env:DATABASE_URL"),
            spec("scratch", "", ""),
        ];
        let registry = StoreRegistry::build(&specs).unwrap();
        assert_eq!(registry.len(), 3);
        assert!(!registry.is_empty());
    }

    #[test]
    fn build_rejects_unknown_backend() {
        let specs = [spec("legacy", "sqlite", "file:./db.sqlite")];
        match StoreRegistry::build(&specs) {
            Err(RegistryError::UnknownBackend { store, backend }) => {
                assert_eq!(store, "legacy");
                assert_eq!(backend, "sqlite");
            }
            other => panic!("expected UnknownBackend, got {other:?}"),
        }
    }

    #[test]
    fn build_rejects_duplicate_store_name() {
        let specs = [
            spec("tenants", "in_memory", ""),
            spec("tenants", "postgresql", "env:DB"),
        ];
        match StoreRegistry::build(&specs) {
            Err(RegistryError::DuplicateStore { store }) => {
                assert_eq!(store, "tenants");
            }
            other => panic!("expected DuplicateStore, got {other:?}"),
        }
    }

    #[test]
    fn build_empty_specs_yields_empty_registry() {
        let registry = StoreRegistry::build(&[]).unwrap();
        assert!(registry.is_empty());
        assert_eq!(registry.len(), 0);
    }

    #[test]
    fn empty_constructor_is_empty() {
        assert!(StoreRegistry::empty().is_empty());
    }

    // ── resolve — D3 key-value path ──────────────────────────────────

    #[test]
    fn resolve_undeclared_store_is_in_memory() {
        // The load-bearing D3 test: a store that was never declared
        // resolves to the byte-identical pre-35 key-value path.
        let registry = StoreRegistry::empty();
        let handle = registry.resolve("never_declared").unwrap();
        assert!(handle.is_in_memory());
    }

    #[test]
    fn resolve_declared_in_memory_store() {
        let registry =
            StoreRegistry::build(&[spec("cache", "in_memory", "")]).unwrap();
        assert!(registry.resolve("cache").unwrap().is_in_memory());
    }

    #[test]
    fn resolve_empty_backend_store_is_in_memory() {
        let registry = StoreRegistry::build(&[spec("s", "", "")]).unwrap();
        assert!(registry.resolve("s").unwrap().is_in_memory());
    }

    #[test]
    fn resolve_empty_store_name_is_in_memory() {
        assert!(StoreRegistry::empty().resolve("").unwrap().is_in_memory());
    }

    // ── resolve — D2: never a silent KV fallback ─────────────────────

    #[test]
    fn resolve_postgres_with_missing_env_var_errors_not_kv_fallback() {
        // A declared postgresql store whose `env:` var is unset MUST
        // surface a typed error — never degrade silently to KV.
        let registry = StoreRegistry::build(&[spec(
            "tenants",
            "postgresql",
            "env:AXON_NONEXISTENT_VAR_FASE35D",
        )])
        .unwrap();
        match registry.resolve("tenants") {
            Err(StoreError::MissingEnvVar { var }) => {
                assert_eq!(var, "AXON_NONEXISTENT_VAR_FASE35D");
            }
            other => panic!("expected MissingEnvVar, got {other:?}"),
        }
    }

    #[test]
    fn resolve_postgres_with_empty_connection_errors() {
        let registry =
            StoreRegistry::build(&[spec("t", "postgresql", "")]).unwrap();
        assert!(matches!(
            registry.resolve("t"),
            Err(StoreError::EmptyConnection)
        ));
    }

    // ── resolve — postgres path + per-DSN pool cache ─────────────────

    #[tokio::test]
    async fn resolve_postgres_store_yields_a_postgres_handle() {
        let registry = StoreRegistry::build(&[spec(
            "tenants",
            "postgresql",
            "postgresql://u:p@localhost:5432/axon",
        )])
        .unwrap();
        assert!(registry.resolve("tenants").unwrap().is_postgres());
    }

    #[tokio::test]
    async fn resolving_one_store_twice_reuses_one_pool() {
        let registry = StoreRegistry::build(&[spec(
            "tenants",
            "postgresql",
            "postgresql://u:p@localhost:5432/axon",
        )])
        .unwrap();
        assert_eq!(registry.cached_pool_count(), 0);
        registry.resolve("tenants").unwrap();
        registry.resolve("tenants").unwrap();
        assert_eq!(
            registry.cached_pool_count(),
            1,
            "the second resolve must hit the cache, not reconnect"
        );
    }

    #[tokio::test]
    async fn two_stores_sharing_a_dsn_share_one_pool() {
        let dsn = "postgresql://u:p@localhost:5432/shared";
        let registry = StoreRegistry::build(&[
            spec("alpha", "postgresql", dsn),
            spec("beta", "postgresql", dsn),
        ])
        .unwrap();
        registry.resolve("alpha").unwrap();
        registry.resolve("beta").unwrap();
        assert_eq!(
            registry.cached_pool_count(),
            1,
            "stores on the same DSN must share one pool"
        );
    }

    #[tokio::test]
    async fn two_stores_with_distinct_dsns_get_distinct_pools() {
        let registry = StoreRegistry::build(&[
            spec("alpha", "postgresql", "postgresql://u:p@localhost/db_a"),
            spec("beta", "postgresql", "postgresql://u:p@localhost/db_b"),
        ])
        .unwrap();
        registry.resolve("alpha").unwrap();
        registry.resolve("beta").unwrap();
        assert_eq!(registry.cached_pool_count(), 2);
    }

    #[tokio::test]
    async fn malformed_dsn_errors_and_is_not_cached() {
        let registry = StoreRegistry::build(&[spec(
            "broken",
            "postgresql",
            "this is not a dsn",
        )])
        .unwrap();
        assert!(matches!(
            registry.resolve("broken"),
            Err(StoreError::PoolInit { .. })
        ));
        assert_eq!(
            registry.cached_pool_count(),
            0,
            "a failed connect must not populate the cache"
        );
    }

    // ── accessors ────────────────────────────────────────────────────

    #[test]
    fn spec_accessor_returns_the_declaration() {
        let registry = StoreRegistry::build(&[spec(
            "tenants",
            "postgresql",
            "env:DB",
        )])
        .unwrap();
        let s = registry.spec("tenants").unwrap();
        assert_eq!(s.name, "tenants");
        assert_eq!(s.backend, "postgresql");
        assert!(registry.spec("absent").is_none());
    }

    #[test]
    fn backend_kind_accessor() {
        let registry = StoreRegistry::build(&[
            spec("kv", "in_memory", ""),
            spec("pg", "postgresql", "env:DB"),
        ])
        .unwrap();
        assert_eq!(
            registry.backend_kind("kv"),
            Some(StoreBackendKind::InMemory)
        );
        assert_eq!(
            registry.backend_kind("pg"),
            Some(StoreBackendKind::Postgresql)
        );
        assert_eq!(registry.backend_kind("absent"), None);
    }

    // ── StoreHandle + display + Debug safety ─────────────────────────

    #[test]
    fn store_handle_predicates() {
        assert!(StoreHandle::InMemory.is_in_memory());
        assert!(!StoreHandle::InMemory.is_postgres());
    }

    #[test]
    fn backend_kind_display() {
        assert_eq!(StoreBackendKind::InMemory.to_string(), "in_memory");
        assert_eq!(StoreBackendKind::Postgresql.to_string(), "postgresql");
    }

    #[test]
    fn registry_debug_does_not_leak_connection_strings() {
        // A literal DSN with a password must not appear in Debug.
        let registry = StoreRegistry::build(&[spec(
            "tenants",
            "postgresql",
            "postgresql://user:fakecred0@localhost/db",
        )])
        .unwrap();
        let debug = format!("{registry:?}");
        assert!(!debug.contains("fakecred0"), "Debug must not leak the DSN");
        assert!(debug.contains("tenants"));
        // The kind surfaces via the `StoreBackendKind` enum's derived
        // Debug (`Postgresql`) — case-insensitive check.
        assert!(debug.to_lowercase().contains("postgresql"));
    }

    #[test]
    fn registry_errors_have_non_empty_display() {
        let errors = [
            RegistryError::UnknownBackend {
                store: "s".into(),
                backend: "mysql".into(),
            },
            RegistryError::DuplicateStore { store: "s".into() },
        ];
        for e in errors {
            assert!(!e.to_string().is_empty());
        }
    }

    // ── §Fase 37.x.g — deploy-time schema verification (D8) ──────────

    #[test]
    fn schema_verify_report_has_fatal_iff_a_table_is_missing() {
        let mut report = SchemaVerifyReport::default();
        assert!(!report.has_fatal(), "an empty report is not fatal");
        assert!(report.fatal_summary().is_empty());
        report.unreachable.push(("s".into(), "down".into()));
        assert!(
            !report.has_fatal(),
            "an unreachable store is a non-fatal warning"
        );
        report.missing.push(("t".into(), "no such table".into()));
        assert!(report.has_fatal(), "a missing table is fatal");
        assert!(report.fatal_summary().contains("`t`"));
    }

    #[tokio::test]
    async fn verify_postgres_schemas_skips_in_memory_and_warns_on_unreachable() {
        // An `in_memory` store is skipped; a postgresql store with a
        // malformed DSN cannot resolve → a non-fatal `unreachable`
        // warning (NOT a `missing` fatal — the database was never
        // reached, so the table's existence is unknown). D8 — "deploy
        // is honest, never brittle".
        let registry = StoreRegistry::build(&[
            spec("cache", "in_memory", ""),
            spec("tenants", "postgresql", "this is not a dsn"),
        ])
        .unwrap();
        let report = registry.verify_postgres_schemas().await;
        assert!(report.verified.is_empty());
        assert!(
            report.missing.is_empty(),
            "an unreachable store must not be a fatal `missing` entry"
        );
        assert_eq!(report.unreachable.len(), 1);
        assert_eq!(report.unreachable[0].0, "tenants");
        assert!(
            !report.has_fatal(),
            "an unreachable store must NOT fail the deploy"
        );
    }

    #[tokio::test]
    async fn verify_postgres_schemas_empty_registry_is_clean() {
        let report = StoreRegistry::empty().verify_postgres_schemas().await;
        assert!(report.verified.is_empty());
        assert!(report.missing.is_empty());
        assert!(!report.has_fatal());
    }

    // ── §Fase 38.f — D3 per-tenant env-var + D8 strengthening (T807) ─

    /// Build an `IRAxonStore` with a declared `column_schema`.
    fn spec_with_schema(
        name: &str,
        connection: &str,
        schema: crate::ir_nodes::IRStoreColumnSchema,
    ) -> IRAxonStore {
        IRAxonStore {
            node_type: "axonstore",
            source_line: 0,
            source_column: 0,
            name: name.to_string(),
            backend: "postgresql".to_string(),
            connection: connection.to_string(),
            confidence_floor: None,
            isolation: String::new(),
            on_breach: String::new(),
            capability: String::new(),
            column_schema: Some(schema),
        }
    }

    #[tokio::test]
    async fn t806_missing_per_tenant_env_var_fails_deploy_with_named_code() {
        // The env var is intentionally unset — the deploy must surface
        // axon-T806 as a fatal `missing` entry. NO database needed:
        // the env-var resolution short-circuits before any pool work.
        let var_name = "AXON_T806_FASE38F_UNSET_VAR_XYZ_DO_NOT_SET";
        std::env::remove_var(var_name);
        let registry = StoreRegistry::build(&[spec_with_schema(
            "tenants",
            "postgresql://u:p@localhost:5432/axon",
            crate::ir_nodes::IRStoreColumnSchema::EnvVar {
                var_name: var_name.to_string(),
            },
        )])
        .unwrap();
        let report = registry.verify_postgres_schemas_with_manifest(None).await;
        assert!(report.has_fatal(), "T806 must fail-close the deploy");
        let (store, diag) = &report.missing[0];
        assert_eq!(store, "tenants");
        assert!(diag.contains("axon-T806"), "diag must carry T806 slug: {diag}");
        assert!(diag.contains(var_name), "diag must name the env var: {diag}");
    }

    #[tokio::test]
    async fn t806_empty_string_env_var_also_fails_t806() {
        // An exported-but-empty env var is the same configuration
        // accident as a missing one — never a silent fallback.
        let var_name = "AXON_T806_FASE38F_EMPTY_VAR";
        std::env::set_var(var_name, "");
        let registry = StoreRegistry::build(&[spec_with_schema(
            "tenants",
            "postgresql://u:p@localhost:5432/axon",
            crate::ir_nodes::IRStoreColumnSchema::EnvVar {
                var_name: var_name.to_string(),
            },
        )])
        .unwrap();
        let report = registry.verify_postgres_schemas_with_manifest(None).await;
        std::env::remove_var(var_name);
        assert!(report.has_fatal(), "empty-string env var must fail-close");
        assert!(report.missing[0].1.contains("axon-T806"));
    }

    #[tokio::test]
    async fn three_tenants_each_get_their_namespace_resolved_independently() {
        // Three different env vars resolve to three different
        // namespaces — every restamping is independent. (No live DB:
        // we only verify the restamp doesn't error.)
        for (var, value) in [
            ("AXON_T806_FASE38F_T1", "tenant_a"),
            ("AXON_T806_FASE38F_T2", "tenant_b"),
            ("AXON_T806_FASE38F_T3", "tenant_c"),
        ] {
            std::env::set_var(var, value);
        }
        let specs: Vec<IRAxonStore> = ["AXON_T806_FASE38F_T1", "AXON_T806_FASE38F_T2", "AXON_T806_FASE38F_T3"]
            .iter()
            .enumerate()
            .map(|(i, v)| {
                spec_with_schema(
                    &format!("tenants_{i}"),
                    "postgresql://u:p@localhost:5432/axon",
                    crate::ir_nodes::IRStoreColumnSchema::EnvVar {
                        var_name: (*v).to_string(),
                    },
                )
            })
            .collect();
        let registry = StoreRegistry::build(&specs).unwrap();
        // The restamping happens synchronously inside verify;
        // because the connections are lazy, the verify will fail at
        // `warm_schema` (no live DB) but the env-var resolution +
        // restamping itself must succeed. We check the pool cache.
        let _ = registry.verify_postgres_schemas_with_manifest(None).await;
        // After verify, each of the three stores has its own pool
        // stamped with its namespace. The pool cache is keyed by DSN
        // — three same-DSN stores share one entry, with the LAST
        // restamping winning. That's correct: the runtime cache holds
        // ONE pool per (DSN, namespace) — same DSN with different
        // namespaces is reachable, but for THIS test we just confirm
        // the restamp didn't error.
        for var in ["AXON_T806_FASE38F_T1", "AXON_T806_FASE38F_T2", "AXON_T806_FASE38F_T3"] {
            std::env::remove_var(var);
        }
        // The reachable pool count is at most 1 (same DSN); the
        // important property is that the restamping completed without
        // panicking and produced a backend.
        assert!(registry.cached_pool_count() <= 1);
    }

    #[test]
    fn application_name_stamping_includes_resolved_namespace() {
        use crate::store::postgres_backend::application_name_for_with_namespace;
        assert_eq!(
            application_name_for_with_namespace("claims", None),
            "axon-store/claims"
        );
        assert_eq!(
            application_name_for_with_namespace("claims", Some("tenant_42")),
            "axon-store/claims/tenant_42"
        );
        // Empty namespace falls back to the no-namespace shape.
        assert_eq!(
            application_name_for_with_namespace("claims", Some("")),
            "axon-store/claims"
        );
        // Empty store name + namespace.
        assert_eq!(
            application_name_for_with_namespace("", Some("tenant_42")),
            "axon-store/tenant_42"
        );
    }

    #[test]
    fn application_name_stamping_truncates_at_namedatalen_with_char_boundary() {
        use crate::store::postgres_backend::application_name_for_with_namespace;
        // A long store name + long namespace MUST not exceed 63
        // bytes (Postgres NAMEDATALEN-1), and the cut must land on a
        // UTF-8 char boundary.
        let long_store = "s".repeat(50);
        let long_ns = "é".repeat(50);
        let stamped = application_name_for_with_namespace(&long_store, Some(&long_ns));
        assert!(stamped.len() <= 63, "got {}: {stamped}", stamped.len());
        assert!(stamped.is_char_boundary(stamped.len()));
        assert!(stamped.starts_with("axon-store/"));
    }

    #[test]
    fn pg_udt_matches_catalog_type_recognises_text_class_aliases() {
        // Text accepts text/varchar/bpchar/name (case-insensitive).
        for udt in ["text", "varchar", "bpchar", "name", "TEXT", "VARCHAR"] {
            assert!(
                pg_udt_matches_catalog_type(udt, StoreColumnType::Text),
                "Text class must accept `{udt}`"
            );
        }
        // Int accepts int4/integer.
        for udt in ["int4", "integer", "INT4"] {
            assert!(pg_udt_matches_catalog_type(udt, StoreColumnType::Int));
        }
        // BigInt accepts int8/bigint.
        for udt in ["int8", "bigint"] {
            assert!(pg_udt_matches_catalog_type(udt, StoreColumnType::BigInt));
        }
    }

    #[test]
    fn pg_udt_matches_catalog_type_rejects_off_class_udts() {
        // Cross-class checks must NOT match.
        assert!(!pg_udt_matches_catalog_type("int4", StoreColumnType::Text));
        assert!(!pg_udt_matches_catalog_type("uuid", StoreColumnType::Int));
        assert!(!pg_udt_matches_catalog_type("varchar", StoreColumnType::Uuid));
        assert!(!pg_udt_matches_catalog_type("bool", StoreColumnType::Numeric));
    }

    #[test]
    fn verify_declared_columns_no_schema_means_nothing_to_prove() {
        // When `column_schema` is None, the v1.37.0 existence-only
        // verification suffices — verify_declared_columns must return
        // None (no T807 raised).
        //
        // We can't easily invoke `verify_declared_columns` directly
        // without a `PostgresStoreBackend` + cached_schema entry, so
        // we test `declared_columns_for` (the pure half).
        let result = declared_columns_for("tenants", None, None, None);
        assert!(matches!(result, Ok(None)));
    }

    #[test]
    fn declared_columns_for_inline_returns_btreemap_keyed_on_column_names() {
        let schema = crate::ir_nodes::IRStoreColumnSchema::Inline {
            columns: vec![
                crate::ir_nodes::IRStoreColumn {
                    name: "tenant_id".to_string(),
                    col_type: "Uuid".to_string(),
                    primary_key: true,
                    auto_increment: false,
                    not_null: false,
                    unique: false,
                    default_value: String::new(),
                    identity: false,
                },
                crate::ir_nodes::IRStoreColumn {
                    name: "tier".to_string(),
                    col_type: "Text".to_string(),
                    primary_key: false,
                    auto_increment: false,
                    not_null: true,
                    unique: false,
                    default_value: String::new(),
                    identity: false,
                },
            ],
        };
        let cols = declared_columns_for("tenants", Some(&schema), None, None)
            .unwrap()
            .unwrap();
        assert_eq!(cols.len(), 2);
        assert_eq!(cols.get("tenant_id").copied(), Some(StoreColumnType::Uuid));
        assert_eq!(cols.get("tier").copied(), Some(StoreColumnType::Text));
    }

    #[test]
    fn declared_columns_for_inline_unknown_type_returns_a_named_error() {
        let schema = crate::ir_nodes::IRStoreColumnSchema::Inline {
            columns: vec![crate::ir_nodes::IRStoreColumn {
                name: "loc".to_string(),
                col_type: "Geometry".to_string(),
                primary_key: false,
                auto_increment: false,
                not_null: false,
                unique: false,
                default_value: String::new(),
                identity: false,
            }],
        };
        let result = declared_columns_for("tenants", Some(&schema), None, None);
        match result {
            Err(msg) => {
                assert!(msg.contains("Geometry"));
                assert!(msg.contains("closed catalog"));
            }
            other => panic!("expected named error, got {other:?}"),
        }
    }

    #[test]
    fn declared_columns_for_manifest_ref_returns_none_when_no_manifest_in_scope() {
        // The current deploy handler passes None for manifest; form (b)
        // honest-falls-through to 37.x existence-only verification.
        let schema = crate::ir_nodes::IRStoreColumnSchema::ManifestRef {
            qualified_name: "public.tenants".to_string(),
        };
        let result = declared_columns_for("tenants", Some(&schema), None, None);
        assert!(matches!(result, Ok(None)));
    }

    #[test]
    fn declared_columns_for_env_var_no_manifest_returns_none() {
        let schema = crate::ir_nodes::IRStoreColumnSchema::EnvVar {
            var_name: "TENANT_SCHEMA".to_string(),
        };
        let result = declared_columns_for("tenants", Some(&schema), Some("tenant_42"), None);
        assert!(matches!(result, Ok(None)));
    }

    #[test]
    fn declared_columns_for_manifest_ref_resolves_against_provided_manifest() {
        let m = Manifest::parse_json(
            r#"{
                "version": 1,
                "stores": {
                    "public.tenants": {
                        "columns": {
                            "tenant_id": { "type": "Uuid", "primary_key": true },
                            "tier":      { "type": "Text", "not_null":    true }
                        }
                    }
                }
            }"#,
        )
        .unwrap();
        let schema = crate::ir_nodes::IRStoreColumnSchema::ManifestRef {
            qualified_name: "public.tenants".to_string(),
        };
        let cols = declared_columns_for("tenants", Some(&schema), None, Some(&m))
            .unwrap()
            .unwrap();
        assert_eq!(cols.len(), 2);
        assert_eq!(cols.get("tenant_id").copied(), Some(StoreColumnType::Uuid));
    }

    #[test]
    fn declared_columns_for_env_var_uses_first_match_heuristic_at_deploy() {
        // Mirrors §38.d's load_columns_for_schema heuristic — the
        // deploy verifier uses the same shape.
        let m = Manifest::parse_json(
            r#"{
                "version": 1,
                "stores": {
                    "tenant_42.events": {
                        "columns": {
                            "event_id": { "type": "Uuid" }
                        }
                    }
                }
            }"#,
        )
        .unwrap();
        let schema = crate::ir_nodes::IRStoreColumnSchema::EnvVar {
            var_name: "TENANT_SCHEMA".to_string(),
        };
        // Exact `tenant_99.events` is not present → first-match
        // heuristic finds `tenant_42.events`.
        let cols = declared_columns_for("events", Some(&schema), Some("tenant_99"), Some(&m))
            .unwrap()
            .unwrap();
        assert!(cols.contains_key("event_id"));
    }

    #[test]
    fn declared_columns_for_manifest_ref_missing_entry_is_a_named_error() {
        let m = Manifest::parse_json(
            r#"{"version":1,"stores":{"public.other":{"columns":{"x":{"type":"Uuid"}}}}}"#,
        )
        .unwrap();
        let schema = crate::ir_nodes::IRStoreColumnSchema::ManifestRef {
            qualified_name: "public.tenants".to_string(),
        };
        let result = declared_columns_for("tenants", Some(&schema), None, Some(&m));
        match result {
            Err(msg) => {
                assert!(msg.contains("public.tenants"));
                assert!(msg.contains("Available manifest entries"));
            }
            other => panic!("expected named missing-entry error, got {other:?}"),
        }
    }

    #[test]
    fn store_error_t806_and_t807_display_carries_the_slug_and_remedy() {
        let t806 = StoreError::MissingPerTenantSchemaEnv {
            store: "tenants".to_string(),
            var: "TENANT_SCHEMA".to_string(),
        };
        let msg = t806.to_string();
        assert!(msg.contains("axon-T806"));
        assert!(msg.contains("TENANT_SCHEMA"));
        assert!(msg.contains("Never a silent fallback"));

        let t807 = StoreError::DeclaredVsLiveDrift {
            store: "tenants".to_string(),
            drift: "missing on live database: {tier}".to_string(),
        };
        let msg = t807.to_string();
        assert!(msg.contains("axon-T807"));
        assert!(msg.contains("tenants"));
        assert!(msg.contains("axon store introspect"), "remedy must point at the CLI: {msg}");
    }
}