anda_brain 0.10.0

🧠 Anda Brain (大脑) — Autonomous Graph Memory for AI Agents
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
//! WikiDigest: distills committed wiki versions into the Cognitive Nexus
//! (PRD §7.3), the graph half of the "graph understands, wiki proves" story.
//!
//! Provenance-by-construction: the LLM only proposes structured facts
//! (subject/predicate/object + section anchor); this module renders the KIP
//! KML itself, attaching `source/citation/checksum/extractor/confidence`
//! metadata to every proposition. A prompt can forget provenance — a
//! renderer cannot. Superseding works the same way: when a new version is
//! digested, facts the new extraction no longer asserts get their
//! propositions marked `superseded` (graph maintenance owns any deeper
//! contradiction resolution). Every digest is recorded as a
//! `DigestExtracted` wiki event whose fact list doubles as the citation
//! sample for verification.

use anda_core::{BoxError, CompletionFeatures, CompletionRequest, Usage};
use anda_db::{
    query::{Filter, Fv, Query, RangeQuery},
    schema::Json,
};
use anda_engine::{context::AgentCtx, memory::MemoryManagement, model::Models};
use anda_kip::{parse_kml, parse_kql};
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::{
    collections::{BTreeMap, BTreeSet},
    sync::{
        Arc,
        atomic::{AtomicU64, Ordering},
    },
};

use super::{
    Collection, EVENT_DIGEST_EXTRACTED, EVENT_DIGEST_FAILED, WikiChunkRecord, WikiDocRecord,
    WikiError, WikiService, WikiVerifyInput, WikiVerifyStatus, WikiVersionRecord,
    chunk::chunk_checksum, citation_uri, evalset::EVAL_NAMESPACE,
};

/// Extractor fingerprint prefix written into proposition metadata; bump on
/// prompt or renderer changes so maintenance can bulk-invalidate old
/// extractions. The full fingerprint appends the model id (PRD §7.3):
/// `wiki_digest@v1/<model_id>`.
pub const WIKI_DIGEST_EXTRACTOR: &str = "wiki_digest@v1";
const DIGEST_PROMPT: &str = include_str!("../../assets/BrainWikiDigest.md");
/// Collection-extension key holding the digest high-water mark (version id).
const DIGEST_CURSOR_KEY: &str = "wiki_digested";
const DIGEST_USAGE_KEY: &str = "wiki_digest_usage";
/// Collection-extension key tracking consecutive failures of one version
/// (the poison-version fuse). Single slot: both the main loop and the
/// pending pass stop at their first retryable failure, so the same version
/// keeps re-bumping this slot until it succeeds or fuses off.
const DIGEST_FAILURE_KEY: &str = "wiki_digest_failure";
/// Collection-extension key holding doc ids queued for a digest catch-up
/// (documents restored from archive after the cursor passed their version).
pub(super) const DIGEST_PENDING_KEY: &str = "wiki_digest_pending";
/// After this many consecutive failures a version is skipped (with a
/// `DigestFailed` event) instead of wedging the pipeline and re-burning
/// tokens every run.
const MAX_VERSION_FAILURES: u64 = 3;
const MAX_FACTS_PER_VERSION: usize = 64;
const MAX_EXTRA_CONCEPTS: usize = 64;
const MAX_BATCH_BYTES: usize = 24 * 1024;
const MAX_VERSIONS_PER_RUN: usize = 20;
const MAX_IDENT_CHARS: usize = 120;
/// How many recent digests the post-run citation sample re-verifies.
const VERIFY_SAMPLE_EVENTS: usize = 5;

/// Resets the running flag on drop so a panicking digest never wedges.
struct RunningGuard(Arc<AtomicU64>);
impl Drop for RunningGuard {
    fn drop(&mut self) {
        self.0.store(0, Ordering::SeqCst);
    }
}

#[derive(Clone)]
pub struct WikiDigest {
    wiki: Arc<WikiService>,
    memory: Arc<MemoryManagement>,
    /// For the extractor fingerprint: `wiki_digest@v1/<model_id>`.
    models: Arc<Models>,
    /// 0 = idle; otherwise the version id currently being digested.
    running: Arc<AtomicU64>,
}

/// Outcome of one version's digest attempt.
enum DigestOutcome {
    Digested,
    /// Permanently not digestible (superseded, archived, labeled, eval
    /// corpus, reclaimed): the cursor advances past it.
    Skipped,
    /// The version row exists but its document has not flipped to it yet
    /// (commit in flight, or a crash leftover awaiting the orphan sweep):
    /// the cursor must NOT advance, or the version would silently never be
    /// digested.
    NotReady,
}

#[derive(Debug, Clone, Default, Serialize)]
pub struct WikiDigestReport {
    /// Versions digested into the graph this run.
    pub digested: usize,
    /// Propositions written (across all digested versions).
    pub facts: usize,
    /// Propositions from older versions marked superseded.
    pub superseded: usize,
    /// Pending versions skipped (already superseded, archived, eval corpus).
    pub skipped: usize,
    /// Post-run citation sample: how many were checked / found corrupt.
    pub citations_checked: usize,
    pub citations_invalid: usize,
    pub usage: Usage,
}

/// LLM output schema (see assets/BrainWikiDigest.md).
#[derive(Debug, Clone, Default, Deserialize)]
struct Extraction {
    #[serde(default)]
    concepts: Vec<ExtractedConcept>,
    #[serde(default)]
    facts: Vec<ExtractedFact>,
}

#[derive(Debug, Clone, Deserialize)]
struct ExtractedConcept {
    r#type: String,
    name: String,
    #[serde(default)]
    attributes: serde_json::Map<String, Json>,
}

#[derive(Debug, Clone, Deserialize)]
struct ConceptRef {
    r#type: String,
    name: String,
}

#[derive(Debug, Clone, Deserialize)]
struct ExtractedFact {
    subject: ConceptRef,
    predicate: String,
    object: ConceptRef,
    #[serde(default)]
    confidence: Option<f64>,
    #[serde(default)]
    anchor: Option<String>,
}

/// A validated fact with its resolved citation, as persisted in the
/// `DigestExtracted` event (the digest ledger used for superseding and
/// citation sampling).
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct DigestedFact {
    pub subject_type: String,
    pub subject_name: String,
    pub predicate: String,
    pub object_type: String,
    pub object_name: String,
    pub confidence: f64,
    pub citation: String,
    pub checksum: String,
}

/// (subject_type, subject_name, predicate, object_type, object_name)
type TripleKey = (String, String, String, String, String);

impl DigestedFact {
    fn triple_key(&self) -> TripleKey {
        (
            self.subject_type.clone(),
            self.subject_name.clone(),
            self.predicate.clone(),
            self.object_type.clone(),
            self.object_name.clone(),
        )
    }
}

impl WikiDigest {
    pub fn new(wiki: Arc<WikiService>, memory: Arc<MemoryManagement>, models: Arc<Models>) -> Self {
        Self {
            wiki,
            memory,
            models,
            running: Arc::new(AtomicU64::new(0)),
        }
    }

    pub fn is_processing(&self) -> bool {
        self.running.load(Ordering::SeqCst) != 0
    }

    /// The extractor fingerprint, model id included, so §13's
    /// "bulk-invalidate by fingerprint" can target one model's extractions.
    fn extractor(&self) -> String {
        match self.models.get_model() {
            Some(model) => format!("{WIKI_DIGEST_EXTRACTOR}/{}", model.model_name()),
            None => WIKI_DIGEST_EXTRACTOR.to_string(),
        }
    }

    /// High-water mark: the largest version id already digested (or skipped).
    pub fn cursor(&self) -> u64 {
        self.wiki
            .docs
            .get_extension_as::<u64>(DIGEST_CURSOR_KEY)
            .unwrap_or_default()
    }

    /// Digests all pending versions (bounded per run), supersedes stale
    /// facts, then re-verifies a citation sample from recent digests.
    /// Single-flight per space; failures stop the run without advancing the
    /// cursor past the failed version, so the next run retries it.
    pub async fn run_pending(
        &self,
        ctx: AgentCtx,
        now_ms: u64,
    ) -> Result<WikiDigestReport, BoxError> {
        if self
            .running
            .compare_exchange(0, u64::MAX, Ordering::SeqCst, Ordering::SeqCst)
            .is_err()
        {
            return Err("wiki digest is already running".into());
        }
        let _guard = RunningGuard(self.running.clone());

        let mut report = WikiDigestReport::default();
        let mut cursor = self.cursor();
        let mut processed = 0usize;

        'run: while processed < MAX_VERSIONS_PER_RUN {
            let versions: Vec<WikiVersionRecord> = self
                .wiki
                .versions
                .search_as(Query {
                    search: None,
                    filter: Some(Filter::Field((
                        "_id".to_string(),
                        RangeQuery::Gt(Fv::U64(cursor)),
                    ))),
                    limit: Some(MAX_VERSIONS_PER_RUN),
                })
                .await
                .map_err(WikiError::from)?;
            if versions.is_empty() {
                break;
            }

            for version in versions {
                if processed >= MAX_VERSIONS_PER_RUN {
                    break 'run;
                }
                processed += 1;
                self.running.store(version._id, Ordering::SeqCst);

                match self
                    .digest_version(&ctx, &version, now_ms, &mut report)
                    .await
                {
                    Ok(DigestOutcome::NotReady) => {
                        // Commit in flight: retry from here next run (the
                        // orphan sweep reclaims it if the commit crashed).
                        break 'run;
                    }
                    Ok(_) => {
                        cursor = version._id;
                        self.save_cursor(cursor).await;
                        self.clear_failure();
                    }
                    Err(err) => {
                        let failures = self.bump_failure(version._id);
                        if failures >= MAX_VERSION_FAILURES {
                            // Poison-version fuse: skip it after repeated
                            // failures so one bad document cannot wedge the
                            // pipeline and re-burn tokens forever.
                            log::error!(
                                target: "brain",
                                version_id = version._id,
                                doc_id = version.doc_id;
                                "wiki digest failed {failures} times, skipping version: {err:?}"
                            );
                            self.fuse_version(
                                version.doc_id,
                                version._id,
                                err.to_string(),
                                failures,
                                now_ms,
                                &mut report,
                            )
                            .await;
                            cursor = version._id;
                            self.save_cursor(cursor).await;
                            continue;
                        }
                        // Leave the cursor before the failed version: the
                        // next run retries it instead of silently skipping.
                        log::error!(
                            target: "brain",
                            version_id = version._id,
                            doc_id = version.doc_id;
                            "wiki digest failed (attempt {failures}/{MAX_VERSION_FAILURES}): {err:?}"
                        );
                        self.save_usage(&report.usage).await;
                        return Err(err);
                    }
                }
            }
        }

        self.digest_pending_restores(&ctx, now_ms, cursor, &mut processed, &mut report)
            .await;

        let (checked, invalid) = self.verify_recent(now_ms).await?;
        report.citations_checked = checked;
        report.citations_invalid = invalid;
        self.save_usage(&report.usage).await;
        Ok(report)
    }

    /// Catch-up pass for documents restored from archive after the cursor
    /// passed their version: the main loop never revisits those ids, so
    /// without this their facts would never enter the graph. Each queued
    /// document's current version is digested unless the ledger shows it
    /// already was. Never fails the run; a retryable error stops this
    /// round's pass (the entry stays queued and is retried first next run,
    /// which keeps the single-slot poison fuse counting consecutive
    /// failures of one version), while `NotFound` unqueues the entry.
    async fn digest_pending_restores(
        &self,
        ctx: &AgentCtx,
        now_ms: u64,
        cursor: u64,
        processed: &mut usize,
        report: &mut WikiDigestReport,
    ) {
        let pending = self
            .wiki
            .docs
            .get_extension_as::<BTreeSet<u64>>(DIGEST_PENDING_KEY)
            .unwrap_or_default();
        for doc_id in pending {
            if *processed >= MAX_VERSIONS_PER_RUN {
                break; // budget spent; the rest stays queued for the next run
            }
            let doc = match self.wiki.doc_record(doc_id).await {
                Ok(doc) => doc,
                Err(WikiError::NotFound(_)) => {
                    // Reclaimed or re-initializing: nothing to catch up.
                    self.clear_pending(doc_id);
                    continue;
                }
                Err(err) => {
                    log::warn!(
                        target: "brain",
                        doc_id = doc_id;
                        "pending digest doc load failed, kept queued: {err:?}"
                    );
                    break;
                }
            };
            if doc.current_version > cursor {
                // The main cursor loop reaches this version by itself; once
                // its DigestExtracted lands, the ledger check below clears
                // the entry on the next run.
                continue;
            }
            match version_digested(&self.wiki, doc_id, doc.current_version).await {
                Ok(true) => {
                    self.clear_pending(doc_id);
                    continue;
                }
                Ok(false) => {}
                Err(err) => {
                    log::warn!(
                        target: "brain",
                        doc_id = doc_id;
                        "pending digest ledger check failed, kept queued: {err:?}"
                    );
                    break;
                }
            }
            let version = match self.wiki.version_record(doc.current_version).await {
                Ok(version) => version,
                Err(WikiError::NotFound(_)) => {
                    // The current version row is gone for good: unqueue.
                    log::warn!(
                        target: "brain",
                        doc_id = doc_id,
                        version_id = doc.current_version;
                        "pending digest version row missing, dropped"
                    );
                    self.clear_pending(doc_id);
                    continue;
                }
                Err(err) => {
                    log::warn!(
                        target: "brain",
                        doc_id = doc_id,
                        version_id = doc.current_version;
                        "pending digest version load failed, kept queued: {err:?}"
                    );
                    break;
                }
            };
            *processed += 1;
            self.running.store(version._id, Ordering::SeqCst);
            match self.digest_version(ctx, &version, now_ms, report).await {
                // Digested, or permanently skipped (re-archived, labeled,
                // eval corpus): either way the queue entry is done. A
                // later restore re-queues re-archived documents.
                Ok(_) => {
                    self.clear_pending(doc_id);
                    self.clear_failure();
                }
                Err(err) => {
                    let failures = self.bump_failure(version._id);
                    log::error!(
                        target: "brain",
                        version_id = version._id,
                        doc_id = doc_id;
                        "pending digest failed (attempt {failures}/{MAX_VERSION_FAILURES}): {err:?}"
                    );
                    if failures >= MAX_VERSION_FAILURES {
                        self.fuse_version(
                            doc_id,
                            version._id,
                            err.to_string(),
                            failures,
                            now_ms,
                            report,
                        )
                        .await;
                        self.clear_pending(doc_id);
                    } else {
                        // Retryable: stop this round so the next run bumps
                        // the same fuse slot instead of interleaving.
                        break;
                    }
                }
            }
        }
    }

    /// Poison-version fuse trip: records the `DigestFailed` event, clears
    /// the failure slot, and counts the skip. Callers decide what retiring
    /// the version means (cursor advance in the main loop, unqueue in the
    /// pending pass).
    async fn fuse_version(
        &self,
        doc_id: u64,
        version_id: u64,
        err_text: String,
        failures: u64,
        now_ms: u64,
        report: &mut WikiDigestReport,
    ) {
        let _ = self
            .wiki
            .write_event(
                EVENT_DIGEST_FAILED,
                Some(doc_id),
                Some(version_id),
                "wiki_digest".to_string(),
                BTreeMap::from([
                    ("error".to_string(), Json::from(err_text)),
                    ("attempts".to_string(), Json::from(failures)),
                    ("extractor".to_string(), Json::from(self.extractor())),
                ]),
                now_ms,
            )
            .await;
        self.clear_failure();
        report.skipped += 1;
    }

    fn clear_pending(&self, doc_id: u64) {
        let _ = self.wiki.docs.set_extension_from_with::<_, BTreeSet<u64>>(
            DIGEST_PENDING_KEY.to_string(),
            |v| {
                let mut set = v.unwrap_or_default();
                set.remove(&doc_id);
                Some(set)
            },
        );
    }

    async fn digest_version(
        &self,
        ctx: &AgentCtx,
        version: &WikiVersionRecord,
        now_ms: u64,
        report: &mut WikiDigestReport,
    ) -> Result<DigestOutcome, BoxError> {
        let doc = match self.wiki.doc_record(version.doc_id).await {
            Ok(doc) => doc,
            // Orphan or reclaimed document: nothing to digest.
            Err(WikiError::NotFound(_)) => {
                report.skipped += 1;
                return Ok(DigestOutcome::Skipped);
            }
            Err(err) => return Err(err.into()),
        };
        if version._id > doc.current_version {
            // Written but not flipped: a concurrent commit is between step 1
            // and its activation point. Advancing past it here would leave
            // the graph stale until the document's next commit.
            return Ok(DigestOutcome::NotReady);
        }
        if doc.current_version != version._id {
            // Stale version: the current version's own digest supersedes
            // for it, so skipping loses nothing.
            report.skipped += 1;
            return Ok(DigestOutcome::Skipped);
        }
        if doc.status != super::DOC_STATUS_ACTIVE
            || doc.namespace == EVAL_NAMESPACE
            // The Cognitive Nexus has no ACL: distilling a labeled document
            // would let any Read principal recall its facts (and citation
            // URIs) through the graph.
            || !doc.acl_label.is_empty()
        {
            // The document reached a state the digest refuses to distill —
            // but an earlier version may already be in the graph, and no
            // other path ever retracts it. Without this, committing an
            // `acl_label` onto a public document would leave its previously
            // digested facts recallable by every Read principal forever.
            report.superseded += self.retract_digested(&doc, version, now_ms).await?;
            report.skipped += 1;
            return Ok(DigestOutcome::Skipped);
        }

        let Some(chunks) = digest_chunks(&self.wiki, version).await? else {
            // The doc pointed at this version a moment ago but its chunks
            // are gone: a concurrent commit superseded it mid-digest. Skip —
            // running an empty extraction here would let `supersede_stale`
            // mark every fact of the previous digest superseded.
            report.skipped += 1;
            return Ok(DigestOutcome::Skipped);
        };
        let extraction = self.extract(ctx, &doc, version, &chunks, report).await?;
        let extractor = self.extractor();
        let (facts, alive) =
            normalize_facts(&self.wiki.space_id, &doc, version, &chunks, &extraction);

        let mut proposition_ids: Vec<String> = Vec::new();
        if !facts.is_empty() {
            let kml = render_digest_kml(
                &self.wiki.space_id,
                &doc,
                version,
                &extraction,
                &facts,
                &extractor,
            );
            let response = self
                .memory
                .nexus
                .execute_kml(parse_kml(&kml)?, false)
                .await?;
            proposition_ids = response
                .get("upsert_proposition_links")
                .and_then(|v| v.as_array())
                .map(|ids| {
                    ids.iter()
                        .filter_map(|v| v.as_str().map(str::to_string))
                        .collect()
                })
                .unwrap_or_default();
        }

        let superseded = self.supersede_stale(&doc, version, &alive, now_ms).await?;

        self.wiki
            .write_event(
                EVENT_DIGEST_EXTRACTED,
                Some(doc._id),
                Some(version._id),
                "wiki_digest".to_string(),
                BTreeMap::from([
                    (
                        "facts".to_string(),
                        serde_json::to_value(&facts).unwrap_or(Json::Null),
                    ),
                    (
                        "proposition_ids".to_string(),
                        Json::from(proposition_ids.clone()),
                    ),
                    ("superseded".to_string(), Json::from(superseded as u64)),
                    ("extractor".to_string(), Json::from(extractor)),
                ]),
                now_ms,
            )
            .await?;

        report.digested += 1;
        report.facts += facts.len();
        report.superseded += superseded;
        Ok(DigestOutcome::Digested)
    }

    /// Runs the extraction prompt over section batches and merges the
    /// results. One retry per batch on non-JSON replies.
    async fn extract(
        &self,
        ctx: &AgentCtx,
        doc: &WikiDocRecord,
        version: &WikiVersionRecord,
        chunks: &[WikiChunkRecord],
        report: &mut WikiDigestReport,
    ) -> Result<Extraction, BoxError> {
        let header = format!(
            "Document: {}\nURI: {}\nNamespace: {}\nTags: {}\n",
            doc.title,
            citation_uri(&self.wiki.space_id, doc._id, version._id, 0, version.size),
            doc.namespace,
            doc.tags.join(", "),
        );

        let mut batches: Vec<String> = Vec::new();
        let mut batch = String::new();
        for chunk in chunks {
            let section = format!(
                "\n[anchor: {}] {}\n{}\n",
                chunk.anchor,
                chunk.heading_path.join(" > "),
                chunk.text,
            );
            if !batch.is_empty() && batch.len() + section.len() > MAX_BATCH_BYTES {
                batches.push(std::mem::take(&mut batch));
            }
            batch.push_str(&section);
        }
        if !batch.is_empty() {
            batches.push(batch);
        }

        let mut merged = Extraction::default();
        for batch in batches {
            let prompt = format!("{header}{batch}");
            let extraction = self.extract_batch(ctx, prompt, report).await?;
            merged.concepts.extend(extraction.concepts);
            merged.facts.extend(extraction.facts);
        }
        Ok(merged)
    }

    async fn extract_batch(
        &self,
        ctx: &AgentCtx,
        prompt: String,
        report: &mut WikiDigestReport,
    ) -> Result<Extraction, BoxError> {
        let mut attempt_prompt = prompt.clone();
        for attempt in 0..2 {
            let res = ctx
                .completion(
                    CompletionRequest {
                        instructions: DIGEST_PROMPT.to_string(),
                        prompt: attempt_prompt.clone(),
                        ..Default::default()
                    },
                    Vec::new(),
                )
                .await?;
            report.usage.accumulate(&res.usage);
            if let Some(reason) = res.failed_reason {
                return Err(format!("digest completion failed: {reason}").into());
            }
            match parse_extraction(&res.content) {
                Ok(extraction) => return Ok(extraction),
                Err(err) if attempt == 0 => {
                    attempt_prompt = format!(
                        "{prompt}\n\nYour previous reply was not the required JSON object ({err}). Reply with ONLY the JSON object."
                    );
                }
                Err(err) => {
                    return Err(format!("digest extraction returned invalid JSON: {err}").into());
                }
            }
        }
        unreachable!("extract_batch loops at most twice")
    }

    /// Marks propositions asserted by this document's previous digest but
    /// absent from the new one as superseded. Per-fact capsules keep one
    /// missing endpoint (e.g. merged away by maintenance) from aborting the
    /// rest. `alive` covers every valid extracted triple (not just the
    /// persisted, capped prefix) so a large document never mis-supersedes
    /// facts that were merely truncated away.
    async fn supersede_stale(
        &self,
        doc: &WikiDocRecord,
        version: &WikiVersionRecord,
        alive: &BTreeSet<TripleKey>,
        _now_ms: u64,
    ) -> Result<usize, BoxError> {
        let previous = self.previous_digest_facts(doc._id, version._id).await?;
        if previous.is_empty() {
            return Ok(0);
        }
        let superseded_by =
            citation_uri(&self.wiki.space_id, doc._id, version._id, 0, version.size);
        Ok(self
            .supersede_facts(doc._id, &previous, alive, &superseded_by)
            .await)
    }

    /// Retracts the document's newest digest at or before `version`: every
    /// fact it recorded is marked superseded, and an empty `retracted`
    /// ledger entry becomes the document's digest head so later supersede
    /// passes see a clean slate. `version_digested` treats the marker as
    /// "not digested", which keeps the restore catch-up re-digesting the
    /// version if the document becomes distillable again. No-op — and no
    /// ledger entry — when nothing is recorded (never digested, or the head
    /// is already a retraction), so repeated passes stay cheap and quiet.
    async fn retract_digested(
        &self,
        doc: &WikiDocRecord,
        version: &WikiVersionRecord,
        now_ms: u64,
    ) -> Result<usize, BoxError> {
        // `+ 1`: unlike superseding during a digest, retraction must cover
        // the given version's own digest — an archived document's facts are
        // recorded at its still-current version.
        let previous = self
            .previous_digest_facts(doc._id, version._id.saturating_add(1))
            .await?;
        if previous.is_empty() {
            return Ok(0);
        }
        let superseded_by =
            citation_uri(&self.wiki.space_id, doc._id, version._id, 0, version.size);
        let superseded = self
            .supersede_facts(doc._id, &previous, &BTreeSet::new(), &superseded_by)
            .await;
        self.wiki
            .write_event(
                EVENT_DIGEST_EXTRACTED,
                Some(doc._id),
                Some(version._id),
                "wiki_digest".to_string(),
                BTreeMap::from([
                    ("facts".to_string(), json!([])),
                    ("superseded".to_string(), Json::from(superseded as u64)),
                    ("retracted".to_string(), Json::from(true)),
                    ("extractor".to_string(), Json::from(self.extractor())),
                ]),
                now_ms,
            )
            .await?;
        log::info!(
            target: "brain",
            doc_id = doc._id,
            version_id = version._id;
            "wiki digest retracted {superseded} facts (document no longer distillable)"
        );
        Ok(superseded)
    }

    /// Marks each fact's proposition superseded unless its triple is in
    /// `alive`. Per-fact capsules keep one missing endpoint (e.g. merged
    /// away by maintenance) from aborting the rest.
    async fn supersede_facts(
        &self,
        doc_id: u64,
        facts: &[DigestedFact],
        alive: &BTreeSet<TripleKey>,
        superseded_by: &str,
    ) -> usize {
        let mut superseded = 0usize;
        for fact in facts {
            if alive.contains(&fact.triple_key()) {
                continue;
            }
            // Graph maintenance may have metabolized the proposition away; a
            // supersede UPSERT would then resurrect it as a tombstone. Only
            // touch propositions that still exist.
            if !self.proposition_exists(fact).await {
                continue;
            }
            let kml = render_supersede_kml(fact, superseded_by);
            match parse_kml(&kml) {
                Ok(cmd) => match self.memory.nexus.execute_kml(cmd, false).await {
                    Ok(_) => superseded += 1,
                    Err(err) => {
                        log::warn!(
                            target: "brain",
                            doc_id = doc_id;
                            "supersede skipped for {:?}: {err:?}",
                            fact.predicate
                        );
                    }
                },
                Err(err) => {
                    log::warn!(target: "brain", doc_id = doc_id; "supersede kml parse failed: {err:?}");
                }
            }
        }
        superseded
    }

    /// Existence probe for one (subject, predicate, object) proposition.
    /// Fails toward `true`: a redundant superseded marker is cheaper than a
    /// stale fact staying active because the probe errored.
    async fn proposition_exists(&self, fact: &DigestedFact) -> bool {
        let kql = format!(
            "FIND(?link) WHERE {{ ?link ({}, {}, {}) }} LIMIT 1",
            concept_literal(&fact.subject_type, &fact.subject_name),
            serde_json::to_string(&fact.predicate).unwrap_or_default(),
            concept_literal(&fact.object_type, &fact.object_name),
        );
        let query = match parse_kql(&kql) {
            Ok(query) => query,
            Err(err) => {
                log::warn!(target: "brain", "proposition existence kql parse failed: {err:?}");
                return true;
            }
        };
        match self.memory.nexus.execute_kql(query).await {
            Ok((result, _)) => kql_has_rows(&result),
            Err(err) => {
                log::warn!(target: "brain", "proposition existence probe failed: {err:?}");
                true
            }
        }
    }

    /// Facts recorded by the most recent digest of this document before the
    /// given version.
    async fn previous_digest_facts(
        &self,
        doc_id: u64,
        before_version: u64,
    ) -> Result<Vec<DigestedFact>, BoxError> {
        let events = self
            .wiki
            .list_events(
                Some(EVENT_DIGEST_EXTRACTED.to_string()),
                Some(doc_id),
                None,
                Some(20),
            )
            .await?;
        let latest = events
            .events
            .iter()
            .filter(|e| e.version_id.is_some_and(|v| v < before_version))
            .max_by_key(|e| e.id);
        let Some(event) = latest else {
            return Ok(Vec::new());
        };
        let facts = match event
            .detail
            .get("facts")
            .cloned()
            .map(serde_json::from_value::<Vec<DigestedFact>>)
        {
            Some(Ok(facts)) => facts,
            Some(Err(err)) => {
                // Schema drift would otherwise disable superseding silently.
                log::warn!(
                    target: "brain",
                    doc_id = doc_id,
                    event_id = event.id;
                    "digest ledger facts unreadable, superseding skipped for this pass: {err}"
                );
                Vec::new()
            }
            None => Vec::new(),
        };
        Ok(facts)
    }

    /// Re-verifies every citation recorded by recent digests; `Invalid`
    /// results are corruption signals (evented inside `verify` when the
    /// stored content itself is corrupt).
    pub async fn verify_recent(&self, now_ms: u64) -> Result<(usize, usize), BoxError> {
        verify_recent_citations(&self.wiki, now_ms).await
    }

    async fn save_cursor(&self, cursor: u64) {
        if let Err(err) = self
            .wiki
            .docs
            .save_extension(DIGEST_CURSOR_KEY.to_string(), cursor.into())
            .await
        {
            // Non-fatal: the next run re-digests from the stale cursor
            // (idempotent for the graph, but re-billed), so be loud.
            log::warn!(
                target: "brain",
                cursor = cursor;
                "wiki digest cursor save failed (next run will re-digest): {err:?}"
            );
        }
    }

    async fn save_usage(&self, usage: &Usage) {
        if usage.requests == 0 {
            return;
        }
        // In-memory metadata update, flushed with the collection; the return
        // value is the previous entry (None on first write), not an error.
        let _ =
            self.wiki
                .docs
                .set_extension_from_with::<_, Usage>(DIGEST_USAGE_KEY.to_string(), |v| {
                    let mut total: Usage = v.unwrap_or_default();
                    total.accumulate(usage);
                    Some(total)
                });
    }

    /// Consecutive-failure counter for the poison fuse; resets whenever a
    /// different version fails or any version succeeds. Single-slot is
    /// sound because both loops stop at their first retryable failure, so
    /// the failing version is always the next one retried.
    fn bump_failure(&self, version_id: u64) -> u64 {
        let mut count = 1u64;
        let _ = self.wiki.docs.set_extension_from_with::<_, (u64, u64)>(
            DIGEST_FAILURE_KEY.to_string(),
            |v| {
                if let Some((prev, prev_count)) = v
                    && prev == version_id
                {
                    count = prev_count + 1;
                }
                Some((version_id, count))
            },
        );
        count
    }

    fn clear_failure(&self) {
        let _ = self
            .wiki
            .docs
            .set_extension_from_with::<_, (u64, u64)>(DIGEST_FAILURE_KEY.to_string(), |_| {
                Some((0, 0))
            });
    }
}

/// Chunk rows for one version, or `None` when they raced away: a concurrent
/// commit can activate a newer version and delete this version's chunks
/// between the caller's doc check and this read. A committed version always
/// has at least one chunk (content is never empty), so an empty set is that
/// race, not a real state — callers must skip WITHOUT extracting or
/// superseding, or the previous digest's facts would all be marked stale.
async fn digest_chunks(
    wiki: &WikiService,
    version: &WikiVersionRecord,
) -> Result<Option<Vec<WikiChunkRecord>>, BoxError> {
    let mut rows: Vec<WikiChunkRecord> = wiki
        .chunks
        .search_as(Query {
            search: None,
            filter: Some(Filter::And(vec![
                Box::new(Filter::Field((
                    "doc_id".to_string(),
                    RangeQuery::Eq(Fv::U64(version.doc_id)),
                ))),
                Box::new(Filter::Field((
                    "version_id".to_string(),
                    RangeQuery::Eq(Fv::U64(version._id)),
                ))),
            ])),
            limit: Some(Collection::MAX_SEARCH_LIMIT),
        })
        .await
        .map_err(WikiError::from)?;
    if rows.is_empty() {
        log::warn!(
            target: "brain",
            doc_id = version.doc_id,
            version_id = version._id;
            "version has no chunk rows; digest skipped without superseding"
        );
        return Ok(None);
    }
    rows.sort_by_key(|row| row.ordinal);
    Ok(Some(rows))
}

/// Whether the digest ledger already covers (doc, version): true when the
/// document's newest `DigestExtracted` event points at that version. A
/// retraction marker does NOT count — its facts were withdrawn, so a
/// restored document must be re-digested for them to re-enter the graph.
async fn version_digested(
    wiki: &WikiService,
    doc_id: u64,
    version_id: u64,
) -> Result<bool, BoxError> {
    let events = wiki
        .list_events(
            Some(EVENT_DIGEST_EXTRACTED.to_string()),
            Some(doc_id),
            None,
            Some(20),
        )
        .await?;
    Ok(events.events.iter().max_by_key(|e| e.id).is_some_and(|e| {
        e.version_id == Some(version_id)
            && !e
                .detail
                .get("retracted")
                .and_then(Json::as_bool)
                .unwrap_or(false)
    }))
}

/// Re-verifies the citations recorded by the most recent digests. All facts
/// of one digest cite the same (doc, version), so both rows are loaded once
/// per event and each fact only re-checks its own range and checksum.
async fn verify_recent_citations(
    wiki: &WikiService,
    now_ms: u64,
) -> Result<(usize, usize), BoxError> {
    let events = wiki
        .list_events(
            Some(EVENT_DIGEST_EXTRACTED.to_string()),
            None,
            None,
            Some(VERIFY_SAMPLE_EVENTS),
        )
        .await?;
    let verify_input = |fact: &DigestedFact| WikiVerifyInput {
        uri: Some(fact.citation.clone()),
        checksum: Some(fact.checksum.clone()),
        ..Default::default()
    };
    let mut checked = 0usize;
    let mut invalid = 0usize;
    for event in events.events {
        let Some(facts) = event
            .detail
            .get("facts")
            .cloned()
            .and_then(|v| serde_json::from_value::<Vec<DigestedFact>>(v).ok())
        else {
            continue;
        };
        let Some(first) = facts.first() else {
            continue;
        };
        let (doc_id, version_id, _, _) = wiki.verify_target(&verify_input(first))?;
        let loaded = match wiki.doc_record(doc_id).await {
            Ok(doc) => match wiki.version_record(version_id).await {
                Ok(version) => Some((doc, version)),
                Err(_) => None,
            },
            Err(_) => None,
        };
        for fact in &facts {
            let (_, _, start, end) = wiki.verify_target(&verify_input(fact))?;
            let status = match &loaded {
                Some((doc, version)) => {
                    wiki.verify_resolved(
                        "wiki_digest".to_string(),
                        doc,
                        version,
                        (start, end),
                        Some(&fact.checksum),
                        now_ms,
                    )
                    .await?
                    .status
                }
                None => WikiVerifyStatus::NotFound,
            };
            checked += 1;
            if status == WikiVerifyStatus::Invalid {
                invalid += 1;
            }
        }
    }
    Ok((checked, invalid))
}

/// Whether a KQL FIND result contains any row.
fn kql_has_rows(result: &Json) -> bool {
    match result {
        Json::Array(rows) => !rows.is_empty(),
        Json::Object(map) => map.values().any(kql_has_rows),
        Json::Null => false,
        _ => true,
    }
}

/// Parses the extraction JSON, tolerating markdown fences and surrounding
/// prose (first `{` to last `}`).
fn parse_extraction(content: &str) -> Result<Extraction, String> {
    let trimmed = content.trim();
    if let Ok(extraction) = serde_json::from_str::<Extraction>(trimmed) {
        return Ok(extraction);
    }
    let start = trimmed.find('{').ok_or("no JSON object found")?;
    let end = trimmed.rfind('}').ok_or("no JSON object found")?;
    if start >= end {
        return Err("no JSON object found".to_string());
    }
    serde_json::from_str::<Extraction>(&trimmed[start..=end]).map_err(|err| err.to_string())
}

fn clean_ident(value: &str) -> Option<String> {
    let value = value.trim();
    if value.is_empty()
        || value.chars().count() > MAX_IDENT_CHARS
        || value.starts_with('$')
        || value.starts_with('_')
    {
        return None;
    }
    Some(value.to_string())
}

/// Validates extracted facts and resolves each anchor to its chunk's byte
/// range and checksum; facts with unknown anchors cite the whole version.
/// Returns the persisted facts (capped at [`MAX_FACTS_PER_VERSION`]) plus
/// the alive-set of ALL valid triples: superseding compares against the
/// uncapped set, so truncation never marks a still-asserted fact stale.
fn normalize_facts(
    space_id: &str,
    doc: &WikiDocRecord,
    version: &WikiVersionRecord,
    chunks: &[WikiChunkRecord],
    extraction: &Extraction,
) -> (Vec<DigestedFact>, BTreeSet<TripleKey>) {
    let by_anchor: BTreeMap<&str, &WikiChunkRecord> = chunks
        .iter()
        .map(|chunk| (chunk.anchor.as_str(), chunk))
        .collect();
    let whole_doc = (
        citation_uri(space_id, doc._id, version._id, 0, version.size),
        chunk_checksum(
            &version.checksum,
            0,
            version.size as usize,
            &version.content,
        ),
    );

    let mut alive = BTreeSet::new();
    let mut facts = Vec::new();
    for fact in &extraction.facts {
        let (Some(s_type), Some(s_name), Some(predicate), Some(o_type), Some(o_name)) = (
            clean_ident(&fact.subject.r#type),
            clean_ident(&fact.subject.name),
            clean_ident(&fact.predicate),
            clean_ident(&fact.object.r#type),
            clean_ident(&fact.object.name),
        ) else {
            continue;
        };
        let (citation, checksum) = fact
            .anchor
            .as_deref()
            .and_then(|anchor| by_anchor.get(anchor))
            .map(|chunk| {
                (
                    citation_uri(
                        space_id,
                        doc._id,
                        version._id,
                        chunk.byte_start,
                        chunk.byte_end,
                    ),
                    chunk.checksum.clone(),
                )
            })
            .unwrap_or_else(|| whole_doc.clone());
        let normalized = DigestedFact {
            subject_type: s_type,
            subject_name: s_name,
            predicate,
            object_type: o_type,
            object_name: o_name,
            confidence: fact.confidence.unwrap_or(0.7).clamp(0.0, 1.0),
            citation,
            checksum,
        };
        if alive.insert(normalized.triple_key()) && facts.len() < MAX_FACTS_PER_VERSION {
            facts.push(normalized);
        }
    }
    (facts, alive)
}

/// Renders a KIP object literal with unquoted identifier keys (the concept
/// matcher grammar requires them) and JSON-encoded values.
fn kip_object(pairs: &[(&str, Json)]) -> String {
    let body = pairs
        .iter()
        .filter(|(key, _)| is_kip_identifier(key))
        .map(|(key, value)| format!("{key}: {value}"))
        .collect::<Vec<_>>()
        .join(", ");
    format!("{{{body}}}")
}

fn is_kip_identifier(key: &str) -> bool {
    let mut chars = key.chars();
    matches!(chars.next(), Some(c) if c.is_ascii_alphabetic() || c == '_')
        && chars.all(|c| c.is_ascii_alphanumeric() || c == '_')
}

fn concept_literal(r#type: &str, name: &str) -> String {
    kip_object(&[("type", json!(r#type)), ("name", json!(name))])
}

/// Renders one atomic UPSERT capsule: schema registration for every type
/// and predicate used (KIP requires define-before-use), concept blocks,
/// then one proposition block per fact with its citation metadata. The
/// global metadata block carries the shared provenance envelope.
fn render_digest_kml(
    space_id: &str,
    doc: &WikiDocRecord,
    version: &WikiVersionRecord,
    extraction: &Extraction,
    facts: &[DigestedFact],
    extractor: &str,
) -> String {
    let mut concept_types = BTreeSet::new();
    let mut predicates = BTreeSet::new();
    let mut endpoints: Vec<(String, String)> = Vec::new();
    let mut endpoint_seen = BTreeSet::new();
    let mut push_endpoint = |t: &str, n: &str, endpoints: &mut Vec<(String, String)>| {
        if endpoint_seen.insert((t.to_string(), n.to_string())) {
            endpoints.push((t.to_string(), n.to_string()));
        }
    };
    for fact in facts {
        concept_types.insert(fact.subject_type.clone());
        concept_types.insert(fact.object_type.clone());
        predicates.insert(fact.predicate.clone());
        push_endpoint(&fact.subject_type, &fact.subject_name, &mut endpoints);
        push_endpoint(&fact.object_type, &fact.object_name, &mut endpoints);
    }

    // Optional descriptions from the extraction, only for endpoints in use.
    let mut attributes: BTreeMap<(String, String), serde_json::Map<String, Json>> = BTreeMap::new();
    for concept in extraction.concepts.iter().take(MAX_EXTRA_CONCEPTS) {
        let (Some(t), Some(n)) = (clean_ident(&concept.r#type), clean_ident(&concept.name)) else {
            continue;
        };
        if !concept.attributes.is_empty() {
            attributes.insert((t, n), concept.attributes.clone());
        }
    }

    let mut lines = vec!["UPSERT {".to_string()];
    for (idx, kind) in concept_types.iter().enumerate() {
        lines.push(format!(
            "  CONCEPT ?ct{idx} {{ {} }}",
            concept_literal("$ConceptType", kind)
        ));
    }
    for (idx, predicate) in predicates.iter().enumerate() {
        lines.push(format!(
            "  CONCEPT ?pt{idx} {{ {} }}",
            concept_literal("$PropositionType", predicate)
        ));
    }
    let mut handles: BTreeMap<(String, String), String> = BTreeMap::new();
    for (idx, (t, n)) in endpoints.iter().enumerate() {
        let handle = format!("?c{idx}");
        let mut block = format!("  CONCEPT {handle} {{ {}", concept_literal(t, n));
        if let Some(attrs) = attributes.get(&(t.clone(), n.clone())) {
            let pairs: Vec<(&str, Json)> =
                attrs.iter().map(|(k, v)| (k.as_str(), v.clone())).collect();
            block.push_str(&format!(" SET ATTRIBUTES {}", kip_object(&pairs)));
        }
        block.push_str(" }");
        lines.push(block);
        handles.insert((t.clone(), n.clone()), handle);
    }
    for (idx, fact) in facts.iter().enumerate() {
        let subject = &handles[&(fact.subject_type.clone(), fact.subject_name.clone())];
        let object = &handles[&(fact.object_type.clone(), fact.object_name.clone())];
        lines.push(format!(
            "  PROPOSITION ?f{idx} {{ ({subject}, {}, {object}) }}",
            serde_json::to_string(&fact.predicate).unwrap_or_default()
        ));
        lines.push(format!(
            "  WITH METADATA {}",
            kip_object(&[
                ("confidence", json!(fact.confidence)),
                ("citation", json!(fact.citation)),
                ("checksum", json!(fact.checksum)),
                // A fact can vanish in one revision (marked superseded) and
                // return in a later one; nexus metadata merges are shallow
                // and keep stale keys, so the re-assertion must explicitly
                // clear the tombstone.
                ("status", json!("active")),
                ("superseded_by", Json::Null),
            ])
        ));
    }
    lines.push("}".to_string());
    lines.push(format!(
        "WITH METADATA {}",
        kip_object(&[
            ("source", json!("wiki")),
            ("author", json!("$self")),
            ("extractor", json!(extractor)),
            ("doc_id", json!(doc._id)),
            ("version_id", json!(version._id)),
            (
                "citation",
                json!(citation_uri(
                    space_id,
                    doc._id,
                    version._id,
                    0,
                    version.size
                )),
            ),
            ("confidence", json!(0.7)),
        ])
    ));
    lines.join("\n")
}

/// One capsule per stale fact: marks the proposition superseded without
/// touching its other metadata (shallow merge).
fn render_supersede_kml(fact: &DigestedFact, superseded_by: &str) -> String {
    format!(
        "UPSERT {{\n  PROPOSITION ?p {{ ({}, {}, {}) }}\n  WITH METADATA {}\n}}\nWITH METADATA {}",
        concept_literal(&fact.subject_type, &fact.subject_name),
        serde_json::to_string(&fact.predicate).unwrap_or_default(),
        concept_literal(&fact.object_type, &fact.object_name),
        kip_object(&[
            ("status", json!("superseded")),
            ("superseded_by", json!(superseded_by)),
        ]),
        kip_object(&[("source", json!("wiki")), ("author", json!("$self"))]),
    )
}

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

    fn fact(s: (&str, &str), p: &str, o: (&str, &str)) -> DigestedFact {
        DigestedFact {
            subject_type: s.0.to_string(),
            subject_name: s.1.to_string(),
            predicate: p.to_string(),
            object_type: o.0.to_string(),
            object_name: o.1.to_string(),
            confidence: 0.9,
            citation: "wiki://sp/1@2#0-10".to_string(),
            checksum: "sha3-256:x".to_string(),
        }
    }

    #[test]
    fn parse_extraction_tolerates_fences_and_prose() {
        let strict = r#"{"facts": [{"subject": {"type": "A", "name": "a"}, "predicate": "p", "object": {"type": "B", "name": "b"}}]}"#;
        assert_eq!(parse_extraction(strict).unwrap().facts.len(), 1);

        let fenced = format!("Here you go:\n```json\n{strict}\n```\nDone.");
        assert_eq!(parse_extraction(&fenced).unwrap().facts.len(), 1);

        assert!(parse_extraction("no json here").is_err());
    }

    #[test]
    fn clean_ident_rejects_reserved_and_oversized() {
        assert_eq!(clean_ident(" Person "), Some("Person".to_string()));
        assert!(clean_ident("$ConceptType").is_none());
        assert!(clean_ident("_hidden").is_none());
        assert!(clean_ident("").is_none());
        assert!(clean_ident(&"x".repeat(200)).is_none());
    }

    #[test]
    fn render_digest_kml_registers_schema_and_attaches_provenance() {
        let doc = WikiDocRecord {
            _id: 3,
            namespace: "kb".to_string(),
            slug: "policy".to_string(),
            title: "安全政策".to_string(),
            status: super::super::DOC_STATUS_ACTIVE.to_string(),
            current_version: 7,
            current_checksum: "sha3-256:doc".to_string(),
            tags: vec![],
            acl_label: String::new(),
            source_uri: None,
            metadata: BTreeMap::new(),
            created_by: "a".to_string(),
            updated_by: "a".to_string(),
            created_at: 0,
            updated_at: 0,
        };
        let version = WikiVersionRecord {
            _id: 7,
            doc_id: 3,
            parent_version: None,
            checksum: "sha3-256:v".to_string(),
            content: "c".to_string(),
            size: 1,
            author: "a".to_string(),
            message: None,
            created_at: 0,
        };
        let facts = vec![fact(
            ("Organization", "Acme \"quoted\""),
            "publishes",
            ("Policy", "安全政策"),
        )];
        let kml = render_digest_kml(
            "sp",
            &doc,
            &version,
            &Extraction::default(),
            &facts,
            "wiki_digest@v1/test-model",
        );

        assert!(kml.contains(r#"{type: "$ConceptType", name: "Organization"}"#));
        assert!(kml.contains(r#"{type: "$ConceptType", name: "Policy"}"#));
        assert!(kml.contains(r#"{type: "$PropositionType", name: "publishes"}"#));
        assert!(kml.contains(r#"{type: "Organization", name: "Acme \"quoted\""}"#));
        assert!(kml.contains(r#"(?c0, "publishes", ?c1)"#));
        assert!(kml.contains(r#"citation: "wiki://sp/1@2#0-10""#));
        assert!(kml.contains(r#"extractor: "wiki_digest@v1/test-model""#));
        assert!(kml.contains(r#"source: "wiki""#));
        // A re-asserted fact must clear a stale superseded tombstone.
        assert!(kml.contains(r#"status: "active""#));
        assert!(kml.contains("superseded_by: null"));
        // Renderer output must parse as valid KML.
        assert!(parse_kml(&kml).is_ok());

        let supersede = render_supersede_kml(&facts[0], "wiki://sp/1@9#0-20");
        assert!(supersede.contains(r#"status: "superseded""#));
        assert!(supersede.contains(r#"superseded_by: "wiki://sp/1@9#0-20""#));
        assert!(parse_kml(&supersede).is_ok());
    }

    #[test]
    fn normalize_facts_resolves_anchors_and_dedupes() {
        let doc = WikiDocRecord {
            _id: 1,
            namespace: "kb".to_string(),
            slug: "d".to_string(),
            title: "t".to_string(),
            status: super::super::DOC_STATUS_ACTIVE.to_string(),
            current_version: 2,
            current_checksum: String::new(),
            tags: vec![],
            acl_label: String::new(),
            source_uri: None,
            metadata: BTreeMap::new(),
            created_by: String::new(),
            updated_by: String::new(),
            created_at: 0,
            updated_at: 0,
        };
        let version = WikiVersionRecord {
            _id: 2,
            doc_id: 1,
            parent_version: None,
            checksum: "sha3-256:v".to_string(),
            content: "0123456789".to_string(),
            size: 10,
            author: String::new(),
            message: None,
            created_at: 0,
        };
        let chunk = WikiChunkRecord {
            _id: 5,
            doc_id: 1,
            version_id: 2,
            namespace: "kb".to_string(),
            current: 1,
            title: "t".to_string(),
            heading_path: vec![],
            anchor: "sec-0".to_string(),
            ordinal: 0,
            text: "01234".to_string(),
            byte_start: 0,
            byte_end: 5,
            checksum: "sha3-256:chunk".to_string(),
            chunker_version: 1,
            acl_label: String::new(),
        };
        let extraction = Extraction {
            concepts: vec![],
            facts: vec![
                ExtractedFact {
                    subject: ConceptRef {
                        r#type: "A".into(),
                        name: "a".into(),
                    },
                    predicate: "p".into(),
                    object: ConceptRef {
                        r#type: "B".into(),
                        name: "b".into(),
                    },
                    confidence: Some(2.0),
                    anchor: Some("sec-0".into()),
                },
                // Duplicate triple: dropped.
                ExtractedFact {
                    subject: ConceptRef {
                        r#type: "A".into(),
                        name: "a".into(),
                    },
                    predicate: "p".into(),
                    object: ConceptRef {
                        r#type: "B".into(),
                        name: "b".into(),
                    },
                    confidence: None,
                    anchor: None,
                },
                // Unknown anchor: cites the whole version.
                ExtractedFact {
                    subject: ConceptRef {
                        r#type: "A".into(),
                        name: "a".into(),
                    },
                    predicate: "q".into(),
                    object: ConceptRef {
                        r#type: "B".into(),
                        name: "b".into(),
                    },
                    confidence: None,
                    anchor: Some("missing".into()),
                },
                // Reserved type: dropped.
                ExtractedFact {
                    subject: ConceptRef {
                        r#type: "$Evil".into(),
                        name: "x".into(),
                    },
                    predicate: "p".into(),
                    object: ConceptRef {
                        r#type: "B".into(),
                        name: "b".into(),
                    },
                    confidence: None,
                    anchor: None,
                },
            ],
        };

        let (facts, alive) = normalize_facts("sp", &doc, &version, &[chunk], &extraction);
        assert_eq!(facts.len(), 2);
        assert_eq!(alive.len(), 2);
        assert_eq!(facts[0].confidence, 1.0);
        assert_eq!(facts[0].citation, "wiki://sp/1@2#0-5");
        assert_eq!(facts[0].checksum, "sha3-256:chunk");
        assert_eq!(facts[1].citation, "wiki://sp/1@2#0-10");
    }

    #[test]
    fn alive_set_is_not_capped_by_fact_truncation() {
        let doc = WikiDocRecord {
            _id: 1,
            namespace: "kb".to_string(),
            slug: "d".to_string(),
            title: "t".to_string(),
            status: super::super::DOC_STATUS_ACTIVE.to_string(),
            current_version: 2,
            current_checksum: String::new(),
            tags: vec![],
            acl_label: String::new(),
            source_uri: None,
            metadata: BTreeMap::new(),
            created_by: String::new(),
            updated_by: String::new(),
            created_at: 0,
            updated_at: 0,
        };
        let version = WikiVersionRecord {
            _id: 2,
            doc_id: 1,
            parent_version: None,
            checksum: "sha3-256:v".to_string(),
            content: "x".to_string(),
            size: 1,
            author: String::new(),
            message: None,
            created_at: 0,
        };
        let extraction = Extraction {
            concepts: vec![],
            facts: (0..MAX_FACTS_PER_VERSION + 10)
                .map(|i| ExtractedFact {
                    subject: ConceptRef {
                        r#type: "A".into(),
                        name: format!("a{i}"),
                    },
                    predicate: "p".into(),
                    object: ConceptRef {
                        r#type: "B".into(),
                        name: "b".into(),
                    },
                    confidence: None,
                    anchor: None,
                })
                .collect(),
        };
        let (facts, alive) = normalize_facts("sp", &doc, &version, &[], &extraction);
        // Persisted facts are capped, but the alive set keeps every valid
        // triple so superseding never treats truncated facts as stale.
        assert_eq!(facts.len(), MAX_FACTS_PER_VERSION);
        assert_eq!(alive.len(), MAX_FACTS_PER_VERSION + 10);
        let truncated = &extraction.facts[MAX_FACTS_PER_VERSION + 5];
        let key = (
            "A".to_string(),
            truncated.subject.name.clone(),
            "p".to_string(),
            "B".to_string(),
            "b".to_string(),
        );
        assert!(alive.contains(&key));
    }

    #[test]
    fn kql_has_rows_detects_emptiness() {
        assert!(!kql_has_rows(&json!({"?link": []})));
        assert!(!kql_has_rows(&json!(null)));
        assert!(kql_has_rows(&json!({"?link": [{"id": "P1"}]})));
        assert!(kql_has_rows(&json!([{"id": "P1"}])));
    }

    use super::super::EVENT_CITATION_VERIFY_FAILED;
    use super::super::tests::{commit_input, test_wiki};

    #[tokio::test]
    async fn digest_chunks_skips_versions_raced_by_commits() {
        let wiki = test_wiki("wiki_digest_race").await;
        let v1 = wiki
            .commit(
                "a".to_string(),
                commit_input("竞态", "# 竞态\n\n第一版内容。\n"),
                1000,
            )
            .await
            .unwrap();
        let v1_record = wiki
            .versions
            .get_as::<WikiVersionRecord>(v1.version.id)
            .await
            .unwrap();

        // A concurrent commit lands v2 and removes v1's chunk set — exactly
        // what a digest can observe between its doc check and chunk read.
        let mut update = commit_input("竞态", "# 竞态\n\n第二版内容。\n");
        update.doc_id = Some(v1.doc.id);
        update.parent_version = Some(v1.version.id);
        let v2 = wiki.commit("a".to_string(), update, 2000).await.unwrap();

        // v1 resolves to "raced away" (None), never to an empty chunk list
        // that would supersede the previous digest's facts wholesale.
        assert!(digest_chunks(&wiki, &v1_record).await.unwrap().is_none());
        let v2_record = wiki
            .versions
            .get_as::<WikiVersionRecord>(v2.version.id)
            .await
            .unwrap();
        let chunks = digest_chunks(&wiki, &v2_record).await.unwrap().unwrap();
        assert!(!chunks.is_empty());
    }

    #[tokio::test]
    async fn restore_queues_doc_for_digest_catchup() {
        let wiki = test_wiki("wiki_digest_restore_pending").await;
        let out = wiki
            .commit(
                "a".to_string(),
                commit_input("归档文档", "# 归档文档\n\n内容。\n"),
                1000,
            )
            .await
            .unwrap();
        wiki.archive("a".to_string(), out.doc.id, 2000)
            .await
            .unwrap();
        wiki.restore("a".to_string(), out.doc.id, 3000)
            .await
            .unwrap();
        let pending = wiki
            .docs
            .get_extension_as::<BTreeSet<u64>>(DIGEST_PENDING_KEY)
            .unwrap_or_default();
        assert!(pending.contains(&out.doc.id));

        // The ledger check driving the catch-up: false until a
        // DigestExtracted event covers the document's current version.
        assert!(
            !version_digested(&wiki, out.doc.id, out.doc.current_version)
                .await
                .unwrap()
        );
        wiki.write_event(
            EVENT_DIGEST_EXTRACTED,
            Some(out.doc.id),
            Some(out.doc.current_version),
            "wiki_digest".to_string(),
            BTreeMap::new(),
            4000,
        )
        .await
        .unwrap();
        assert!(
            version_digested(&wiki, out.doc.id, out.doc.current_version)
                .await
                .unwrap()
        );
    }

    #[tokio::test]
    async fn verify_recent_checks_ledger_citations_with_grouped_loads() {
        let wiki = test_wiki("wiki_digest_verify_recent").await;
        let out = wiki
            .commit(
                "a".to_string(),
                commit_input("样本", "# 样本\n\n引用样本内容。\n"),
                1000,
            )
            .await
            .unwrap();
        let version = wiki
            .versions
            .get_as::<WikiVersionRecord>(out.version.id)
            .await
            .unwrap();
        let ok = fact(("A", "a"), "p", ("B", "b"));
        let ok = DigestedFact {
            citation: citation_uri("test_space", out.doc.id, out.version.id, 0, version.size),
            checksum: chunk_checksum(
                &version.checksum,
                0,
                version.size as usize,
                &version.content,
            ),
            ..ok
        };
        let mut stale = ok.clone();
        stale.predicate = "q".into();
        stale.checksum = "sha3-256:wrong".to_string();
        wiki.write_event(
            EVENT_DIGEST_EXTRACTED,
            Some(out.doc.id),
            Some(out.version.id),
            "wiki_digest".to_string(),
            BTreeMap::from([(
                "facts".to_string(),
                serde_json::to_value(vec![ok, stale]).unwrap(),
            )]),
            2000,
        )
        .await
        .unwrap();

        let (checked, invalid) = verify_recent_citations(&wiki, 3000).await.unwrap();
        assert_eq!((checked, invalid), (2, 1));
        // The mismatched recorded checksum sits over intact content: a
        // reference error, not corruption — no audit event.
        let events = wiki
            .list_events(
                Some(EVENT_CITATION_VERIFY_FAILED.to_string()),
                None,
                None,
                Some(10),
            )
            .await
            .unwrap();
        assert!(events.events.is_empty());
    }

    use anda_cognitive_nexus::CognitiveNexus;
    use anda_db::{database::AndaDB, database::DBConfig, storage::StorageConfig};
    use object_store::memory::InMemory;

    /// A digest engine over an in-memory space with a live Cognitive Nexus
    /// (no LLM: only the non-extracting paths may run).
    async fn test_digest(name: &str) -> (Arc<WikiService>, WikiDigest) {
        let db = Arc::new(
            AndaDB::create(
                Arc::new(InMemory::new()),
                DBConfig {
                    name: name.to_string(),
                    description: "wiki digest test db".to_string(),
                    storage: StorageConfig::default(),
                    lock: None,
                },
            )
            .await
            .unwrap(),
        );
        let nexus = Arc::new(
            CognitiveNexus::connect(db.clone(), async |_| Ok(()))
                .await
                .unwrap(),
        );
        let memory = Arc::new(MemoryManagement::connect(db.clone(), nexus).await.unwrap());
        let wiki = Arc::new(
            WikiService::connect("test_space".to_string(), db)
                .await
                .unwrap(),
        );
        let digest = WikiDigest::new(wiki.clone(), memory, Arc::new(Models::default()));
        (wiki, digest)
    }

    async fn proposition_status_superseded(digest: &WikiDigest, fact: &DigestedFact) -> bool {
        let kql = format!(
            "FIND(?link) WHERE {{ ?link ({}, {}, {}) FILTER(?link.metadata.status == \"superseded\") }} LIMIT 1",
            concept_literal(&fact.subject_type, &fact.subject_name),
            serde_json::to_string(&fact.predicate).unwrap(),
            concept_literal(&fact.object_type, &fact.object_name),
        );
        let (result, _) = digest
            .memory
            .nexus
            .execute_kql(parse_kql(&kql).unwrap())
            .await
            .unwrap();
        kql_has_rows(&result)
    }

    #[tokio::test]
    async fn labeling_a_document_retracts_its_digested_facts() {
        let (wiki, digest) = test_digest("wiki_digest_retract").await;
        let v1 = wiki
            .commit(
                "a".to_string(),
                commit_input("秘密文档", "# 秘密文档\n\n内容甲。\n"),
                1000,
            )
            .await
            .unwrap();
        let doc = wiki.doc_record(v1.doc.id).await.unwrap();
        let v1_version = wiki
            .versions
            .get_as::<WikiVersionRecord>(v1.version.id)
            .await
            .unwrap();

        // Seed the graph and the ledger as if v1 had been digested.
        let fact = DigestedFact {
            subject_type: "Person".to_string(),
            subject_name: "alice".to_string(),
            predicate: "knows".to_string(),
            object_type: "Topic".to_string(),
            object_name: "secret_topic".to_string(),
            confidence: 0.9,
            citation: citation_uri("test_space", doc._id, v1_version._id, 0, v1_version.size),
            checksum: "sha3-256:x".to_string(),
        };
        let kml = render_digest_kml(
            "test_space",
            &doc,
            &v1_version,
            &Extraction::default(),
            std::slice::from_ref(&fact),
            "wiki_digest@v1/test",
        );
        digest
            .memory
            .nexus
            .execute_kml(parse_kml(&kml).unwrap(), false)
            .await
            .unwrap();
        wiki.write_event(
            EVENT_DIGEST_EXTRACTED,
            Some(doc._id),
            Some(v1_version._id),
            "wiki_digest".to_string(),
            BTreeMap::from([(
                "facts".to_string(),
                serde_json::to_value(vec![fact.clone()]).unwrap(),
            )]),
            1500,
        )
        .await
        .unwrap();
        assert!(digest.proposition_exists(&fact).await);
        assert!(!proposition_status_superseded(&digest, &fact).await);

        // v2 labels the document — the state the digest refuses to distill.
        let mut update = commit_input("秘密文档", "# 秘密文档\n\n内容乙。\n");
        update.doc_id = Some(doc._id);
        update.parent_version = Some(v1_version._id);
        update.acl_label = Some("secret".to_string());
        let v2 = wiki.commit("a".to_string(), update, 2000).await.unwrap();
        let doc = wiki.doc_record(v2.doc.id).await.unwrap();
        assert_eq!(doc.acl_label, "secret");
        let v2_version = wiki
            .versions
            .get_as::<WikiVersionRecord>(v2.version.id)
            .await
            .unwrap();

        let retracted = digest
            .retract_digested(&doc, &v2_version, 3000)
            .await
            .unwrap();
        assert_eq!(retracted, 1);
        // The proposition is tombstoned in the graph…
        assert!(proposition_status_superseded(&digest, &fact).await);
        // …the digest head is a clean, retracted slate…
        let head = digest
            .previous_digest_facts(doc._id, v2_version._id + 1)
            .await
            .unwrap();
        assert!(head.is_empty());
        // …and the retraction marker does NOT count as "digested", so a
        // restore/unlabel catch-up would re-digest the version.
        assert!(
            !version_digested(&wiki, doc._id, v2_version._id)
                .await
                .unwrap()
        );

        // Idempotent: a second retraction is a no-op with no ledger growth.
        let events_before = wiki
            .list_events(
                Some(EVENT_DIGEST_EXTRACTED.to_string()),
                Some(doc._id),
                None,
                Some(20),
            )
            .await
            .unwrap()
            .events
            .len();
        assert_eq!(
            digest
                .retract_digested(&doc, &v2_version, 4000)
                .await
                .unwrap(),
            0
        );
        let events_after = wiki
            .list_events(
                Some(EVENT_DIGEST_EXTRACTED.to_string()),
                Some(doc._id),
                None,
                Some(20),
            )
            .await
            .unwrap()
            .events
            .len();
        assert_eq!(events_before, events_after);
    }

    #[tokio::test]
    async fn poison_fuse_counts_consecutive_failures_of_one_version() {
        let (_wiki, digest) = test_digest("wiki_digest_fuse").await;
        assert_eq!(digest.bump_failure(10), 1);
        assert_eq!(digest.bump_failure(10), 2);
        assert_eq!(digest.bump_failure(10), 3);
        assert!(digest.bump_failure(10) >= MAX_VERSION_FAILURES);

        // A different version failing takes over the single slot…
        assert_eq!(digest.bump_failure(20), 1);
        assert_eq!(digest.bump_failure(20), 2);
        // …and any success resets it.
        digest.clear_failure();
        assert_eq!(digest.bump_failure(20), 1);
    }
}