previously-on 0.1.0-alpha.1

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

const DATABASE_SCHEMA_VERSION: i64 = 1;
const PURGE_TOMBSTONE_VERSION: u32 = 1;

#[derive(Debug, Clone)]
pub struct Store {
    path: PathBuf,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum InsertOutcome {
    Inserted,
    Duplicate,
}

#[derive(Debug, Clone, Copy, Default)]
struct InsertFault {
    before_insert: bool,
    before_commit: bool,
    after_commit: bool,
}

pub fn is_sqlite_full(error: &anyhow::Error) -> bool {
    error.chain().any(|cause| {
        cause
            .downcast_ref::<rusqlite::Error>()
            .is_some_and(|error| {
                matches!(
                    error,
                    rusqlite::Error::SqliteFailure(inner, _)
                        if inner.code == rusqlite::ErrorCode::DiskFull
                )
            })
    })
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct StoreHealth {
    pub schema_version: i64,
    pub journal_mode: String,
    pub integrity_check: String,
    pub canonical_event_count: u64,
    pub projection_task_count: u64,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct RetentionReport {
    pub retained_events: usize,
    pub removed_events: usize,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
enum PurgeJournalPhase {
    Tombstoned,
    RelatedDataPurged,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
struct PurgeRecoveryJournalV1 {
    version: u32,
    repository_id: String,
    phase: PurgeJournalPhase,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
struct RepositoryPurgeTombstoneV1 {
    version: u32,
    generation: String,
    created_at: String,
}

/// Reject data for a repository that has been explicitly purged. The marker deliberately stores
/// only a hash of the repository identity so purge does not retain the deleted local path.
pub fn ensure_repository_not_purged(data_dir: &Path, repository_id: &str) -> Result<()> {
    let path = repository_tombstone_path(data_dir, repository_id);
    match fs::metadata(&path) {
        Ok(_) => {
            bail!(
                "repository {repository_id} was purged; run setup again before capturing new data"
            )
        }
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
        Err(error) => {
            return Err(error)
                .with_context(|| format!("inspect repository purge tombstone {}", path.display()))
        }
    }

    let journal_path = data_dir.join("previously.sqlite3.purge-recovery.json");
    let journal_bytes = match fs::read(&journal_path) {
        Ok(bytes) => bytes,
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(()),
        Err(error) => {
            return Err(error)
                .with_context(|| format!("inspect purge journal {}", journal_path.display()))
        }
    };
    let journal: PurgeRecoveryJournalV1 = serde_json::from_slice(&journal_bytes)
        .with_context(|| format!("parse purge recovery journal {}", journal_path.display()))?;
    if journal.version != 1 {
        bail!("unsupported purge recovery journal version");
    }
    if journal.repository_id == repository_id {
        bail!("repository {repository_id} is being purged; capture remains disabled");
    }
    Ok(())
}

/// A successful, explicit setup is the only operation that re-authorizes capture after purge.
pub fn reactivate_repository(data_dir: &Path, repository_id: &str) -> Result<()> {
    fs::create_dir_all(data_dir)?;
    set_directory_permissions(data_dir)?;
    let database = data_dir.join("previously.sqlite3");
    let _lock = acquire_database_lock(&database)?;
    let journal_path = data_dir.join("previously.sqlite3.purge-recovery.json");
    match fs::metadata(&journal_path) {
        Ok(_) => {
            bail!("cannot reactivate repository while purge recovery is pending; rerun purge first")
        }
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
        Err(error) => {
            return Err(error)
                .with_context(|| format!("inspect purge journal {}", journal_path.display()))
        }
    }
    remove_file_and_sync_parent(&repository_tombstone_path(data_dir, repository_id))
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TaskSearchHit {
    pub task: TaskV1,
    pub rank: f64,
}

impl Store {
    pub fn open(path: impl AsRef<Path>) -> Result<Self> {
        let path = path.as_ref().to_path_buf();
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent)
                .with_context(|| format!("create database directory {}", parent.display()))?;
            set_directory_permissions(parent)?;
        }
        let store = Self { path };
        let mut connection = store.connect()?;
        store.migrate(&mut connection)?;
        set_file_permissions(&store.path)?;
        for suffix in ["wal", "shm"] {
            let sidecar = PathBuf::from(format!("{}-{suffix}", store.path.to_string_lossy()));
            set_file_permissions(&sidecar)?;
        }
        store.recover_purge_if_ready()?;
        Ok(store)
    }

    pub fn path(&self) -> &Path {
        &self.path
    }

    pub fn insert_event(&self, event: &EventEnvelopeV1) -> Result<InsertOutcome> {
        self.insert_event_inner(event, InsertFault::default())
    }

    fn insert_event_inner(
        &self,
        event: &EventEnvelopeV1,
        fault: InsertFault,
    ) -> Result<InsertOutcome> {
        let _lock = self.acquire_maintenance_lock()?;
        ensure_repository_not_purged(
            self.path.parent().context("database path has no parent")?,
            &event.repository_id,
        )?;
        if self
            .read_purge_journal()?
            .is_some_and(|journal| journal.repository_id == event.repository_id)
        {
            bail!(
                "repository {} is tombstoned by an incomplete purge; resume purge before ingestion",
                event.repository_id
            );
        }
        if event.schema_version != SCHEMA_VERSION_V1 {
            bail!(
                "unsupported canonical event schema version {}; expected {}",
                event.schema_version,
                SCHEMA_VERSION_V1
            );
        }
        let mut event = event.clone();
        event.source_id = redact_text(&event.source_id);
        event.payload = redact_value(&event.payload);
        event.coverage.captured = event
            .coverage
            .captured
            .into_iter()
            .map(|item| redact_excerpt(&item))
            .collect();
        event.coverage.missing = event
            .coverage
            .missing
            .into_iter()
            .map(|item| redact_excerpt(&item))
            .collect();
        event.coverage.warnings = event
            .coverage
            .warnings
            .into_iter()
            .map(|item| redact_excerpt(&item))
            .collect();
        let mut connection = self.connect()?;
        let transaction = connection.transaction()?;
        let latest_key = transaction
            .query_row(
                "SELECT occurred_at, COALESCE(sequence_no, 9223372036854775807), event_id
                 FROM canonical_events
                 ORDER BY occurred_at DESC, COALESCE(sequence_no, 9223372036854775807) DESC, event_id DESC
                 LIMIT 1",
                [],
                |row| Ok((row.get::<_, String>(0)?, row.get::<_, i64>(1)?, row.get::<_, String>(2)?)),
            )
            .optional()?;
        let serialized = serde_json::to_string(&event).context("serialize canonical event")?;
        if fault.before_insert {
            bail!("injected failure before canonical insert");
        }
        let inserted = transaction.execute(
            "INSERT OR IGNORE INTO canonical_events
             (event_id, dedupe_key, repository_id, session_id, task_id, sequence_no, occurred_at,
              received_at, kind, event_json)
             VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10)",
            params![
                event.event_id,
                event.dedupe_key,
                event.repository_id,
                event.session_id,
                event.task_id,
                event.sequence,
                timestamp(event.occurred_at),
                timestamp(event.received_at),
                enum_text(event.kind)?,
                serialized,
            ],
        )?;
        if inserted == 0 {
            transaction.commit()?;
            return Ok(InsertOutcome::Duplicate);
        }

        let event_key = (
            timestamp(event.occurred_at),
            event.sequence.unwrap_or(i64::MAX),
            event.event_id.clone(),
        );
        if latest_key.map(|latest| event_key < latest).unwrap_or(false) {
            rebuild_projections_tx(&transaction)?;
        } else {
            apply_event_projection(&transaction, &event)?;
        }
        if fault.before_commit {
            bail!("injected failure before canonical commit");
        }
        transaction.commit()?;
        if fault.after_commit {
            bail!("injected failure after canonical commit");
        }
        Ok(InsertOutcome::Inserted)
    }

    pub fn list_events(&self, repository_id: Option<&str>) -> Result<Vec<EventEnvelopeV1>> {
        let connection = self.connect()?;
        load_events(&connection, repository_id)
    }

    pub fn rebuild_projections(&self) -> Result<()> {
        let mut connection = self.connect()?;
        let transaction = connection.transaction()?;
        rebuild_projections_tx(&transaction)?;
        transaction.commit()?;
        Ok(())
    }

    pub fn project_checkpoint(
        &self,
        repository_id: &str,
        session_id: &str,
        git_before: Option<GitSnapshotV1>,
        git_after: GitSnapshotV1,
        changes: Vec<FileChangeV1>,
        tests: Vec<TestResultV1>,
    ) -> Result<CheckpointV1> {
        let events = self
            .list_events(Some(repository_id))?
            .into_iter()
            .filter(|event| event.session_id == session_id)
            .collect::<Vec<_>>();
        let checkpoint = CheckpointV1::project(&events, git_before, git_after, changes, tests);
        self.upsert_checkpoint(&checkpoint)?;
        Ok(checkpoint)
    }

    pub fn upsert_repository(&self, repository: &RepositoryV1) -> Result<()> {
        let mut connection = self.connect()?;
        let transaction = connection.transaction()?;
        upsert_repository_tx(&transaction, repository)?;
        transaction.commit()?;
        Ok(())
    }

    pub fn upsert_task(&self, task: &TaskV1) -> Result<()> {
        let mut connection = self.connect()?;
        let transaction = connection.transaction()?;
        upsert_task_tx(&transaction, task)?;
        transaction.commit()?;
        Ok(())
    }

    pub fn upsert_session(&self, session: &SessionV1) -> Result<()> {
        let mut connection = self.connect()?;
        let transaction = connection.transaction()?;
        upsert_session_tx(&transaction, session)?;
        transaction.commit()?;
        Ok(())
    }

    pub fn upsert_checkpoint(&self, checkpoint: &CheckpointV1) -> Result<()> {
        let mut connection = self.connect()?;
        let transaction = connection.transaction()?;
        upsert_checkpoint_tx(&transaction, checkpoint)?;
        transaction.commit()?;
        Ok(())
    }

    pub fn upsert_fact(&self, fact: &FactV1) -> Result<()> {
        let mut fact = fact.clone();
        fact.content = redact_text(&fact.content);
        let mut connection = self.connect()?;
        let transaction = connection.transaction()?;
        upsert_fact_tx(&transaction, &fact)?;
        transaction.commit()?;
        Ok(())
    }

    pub fn upsert_evidence(&self, evidence: &EvidenceV1) -> Result<()> {
        let mut evidence = evidence.clone();
        evidence.excerpt = redact_excerpt(&evidence.excerpt);
        evidence.excerpt_sha256 = hex::encode(sha2::Sha256::digest(evidence.excerpt.as_bytes()));
        let mut connection = self.connect()?;
        let transaction = connection.transaction()?;
        upsert_evidence_tx(&transaction, &evidence)?;
        transaction.commit()?;
        Ok(())
    }

    pub fn upsert_file_change(&self, change: &FileChangeV1) -> Result<()> {
        let mut connection = self.connect()?;
        let transaction = connection.transaction()?;
        upsert_file_change_tx(&transaction, change)?;
        transaction.commit()?;
        Ok(())
    }

    pub fn upsert_test_result(&self, test: &TestResultV1) -> Result<()> {
        let mut test = test.clone();
        test.command = redact_text(&test.command);
        test.summary = test.summary.map(|summary| redact_excerpt(&summary));
        let mut connection = self.connect()?;
        let transaction = connection.transaction()?;
        upsert_test_result_tx(&transaction, &test)?;
        transaction.commit()?;
        Ok(())
    }

    pub fn list_repositories(&self) -> Result<Vec<RepositoryV1>> {
        query_json_rows(
            &self.connect()?,
            "SELECT repository_json FROM repositories ORDER BY path, id",
            [],
        )
    }

    pub fn get_task(&self, task_id: &str) -> Result<Option<TaskV1>> {
        query_json_optional(
            &self.connect()?,
            "SELECT task_json FROM tasks WHERE id = ?1",
            [task_id],
        )
    }

    pub fn get_session(&self, session_id: &str) -> Result<Option<SessionV1>> {
        query_json_optional(
            &self.connect()?,
            "SELECT session_json FROM sessions WHERE id = ?1",
            [session_id],
        )
    }

    pub fn list_session_events(
        &self,
        repository_id: &str,
        session_id: &str,
    ) -> Result<Vec<EventEnvelopeV1>> {
        let connection = self.connect()?;
        let mut statement = connection.prepare(
            "SELECT event_json FROM canonical_events
             WHERE repository_id = ?1 AND session_id = ?2
             ORDER BY occurred_at, COALESCE(sequence_no, 9223372036854775807), event_id",
        )?;
        let rows = statement.query_map(params![repository_id, session_id], |row| {
            row.get::<_, String>(0)
        })?;
        rows.map(|row| {
            let json = row?;
            serde_json::from_str(&json).context("deserialize session event")
        })
        .collect()
    }

    pub fn session_event_count(&self, session_id: &str, kind: EventKind) -> Result<u64> {
        let connection = self.connect()?;
        connection
            .query_row(
                "SELECT COUNT(*) FROM canonical_events WHERE session_id = ?1 AND kind = ?2",
                params![session_id, enum_text(kind)?],
                |row| row.get(0),
            )
            .context("count session events")
    }

    pub fn get_fact(&self, fact_id: &str) -> Result<Option<FactV1>> {
        query_json_optional(
            &self.connect()?,
            "SELECT fact_json FROM facts WHERE id = ?1",
            [fact_id],
        )
    }

    pub fn get_evidence(&self, evidence_id: &str) -> Result<Option<EvidenceV1>> {
        query_json_optional(
            &self.connect()?,
            "SELECT evidence_json FROM evidence WHERE id = ?1",
            [evidence_id],
        )
    }

    pub fn list_facts(&self, task_id: &str) -> Result<Vec<FactV1>> {
        query_json_rows(
            &self.connect()?,
            "SELECT fact_json FROM facts WHERE task_id = ?1 ORDER BY updated_at DESC, id",
            [task_id],
        )
    }

    pub fn list_evidence(&self, task_id: &str) -> Result<Vec<EvidenceV1>> {
        query_json_rows(
            &self.connect()?,
            "SELECT evidence_json FROM evidence WHERE task_id = ?1 ORDER BY created_at, id",
            [task_id],
        )
    }

    pub fn list_checkpoints(&self, task_id: &str) -> Result<Vec<CheckpointV1>> {
        query_json_rows(
            &self.connect()?,
            "SELECT checkpoint_json FROM checkpoints WHERE task_id = ?1 ORDER BY created_at, id",
            [task_id],
        )
    }

    pub fn list_file_changes(&self, task_id: &str) -> Result<Vec<FileChangeV1>> {
        query_json_rows(
            &self.connect()?,
            "SELECT change_json FROM file_changes WHERE task_id = ?1 ORDER BY path, id",
            [task_id],
        )
    }

    pub fn list_test_results(&self, task_id: &str) -> Result<Vec<TestResultV1>> {
        query_json_rows(
            &self.connect()?,
            "SELECT test_json FROM test_results WHERE task_id = ?1 ORDER BY occurred_at DESC, id",
            [task_id],
        )
    }

    pub fn get_task_timeline(&self, task_id: &str) -> Result<Option<TaskTimelineV1>> {
        let Some(task) = self.get_task(task_id)? else {
            return Ok(None);
        };
        let sessions = query_json_rows(
            &self.connect()?,
            "SELECT session_json FROM sessions WHERE task_id = ?1 ORDER BY started_at, id",
            [task_id],
        )?;
        Ok(Some(TaskTimelineV1 {
            task,
            sessions,
            checkpoints: self.list_checkpoints(task_id)?,
            facts: self.list_facts(task_id)?,
        }))
    }

    pub fn search_tasks(&self, query: &str, limit: usize) -> Result<Vec<TaskSearchHit>> {
        let connection = self.connect()?;
        let query = fts_query(query);
        if query.is_empty() {
            let tasks: Vec<TaskV1> = query_json_rows(
                &connection,
                "SELECT task_json FROM tasks ORDER BY updated_at DESC, id LIMIT ?1",
                [limit as i64],
            )?;
            return Ok(tasks
                .into_iter()
                .enumerate()
                .map(|(index, task)| TaskSearchHit {
                    task,
                    rank: index as f64,
                })
                .collect());
        }
        let mut statement = connection.prepare(
            "SELECT tasks.task_json, bm25(task_fts) AS rank
             FROM task_fts JOIN tasks ON tasks.id = task_fts.task_id
             WHERE task_fts MATCH ?1
             ORDER BY rank, tasks.updated_at DESC, tasks.id
             LIMIT ?2",
        )?;
        let rows = statement.query_map(params![query, limit as i64], |row| {
            Ok((row.get::<_, String>(0)?, row.get::<_, f64>(1)?))
        })?;
        rows.map(|row| {
            let (json, rank) = row?;
            Ok(TaskSearchHit {
                task: serde_json::from_str(&json).context("deserialize task search result")?,
                rank,
            })
        })
        .collect()
    }

    pub fn search_facts(&self, query: &str, limit: usize) -> Result<Vec<FactV1>> {
        let query = fts_query(query);
        if query.is_empty() {
            return Ok(Vec::new());
        }
        let connection = self.connect()?;
        let mut statement = connection.prepare(
            "SELECT facts.fact_json
             FROM fact_fts JOIN facts ON facts.id = fact_fts.fact_id
             WHERE fact_fts MATCH ?1
             ORDER BY bm25(fact_fts), facts.updated_at DESC, facts.id
             LIMIT ?2",
        )?;
        let rows =
            statement.query_map(params![query, limit as i64], |row| row.get::<_, String>(0))?;
        rows.map(|row| {
            let json = row?;
            serde_json::from_str(&json).context("deserialize fact search result")
        })
        .collect()
    }

    pub fn suggest_resume(
        &self,
        repository_id: &str,
        query: &str,
        limit: usize,
    ) -> Result<Vec<TaskSuggestionV1>> {
        self.suggest_resume_for_branch(repository_id, query, limit, None)
    }

    pub fn suggest_resume_for_branch(
        &self,
        repository_id: &str,
        query: &str,
        limit: usize,
        current_branch: Option<&str>,
    ) -> Result<Vec<TaskSuggestionV1>> {
        let now = Utc::now();
        let repository_path = self
            .list_repositories()?
            .into_iter()
            .find(|repository| repository.id == repository_id)
            .map(|repository| repository.path);
        let current_snapshot = repository_path
            .as_deref()
            .and_then(|path| crate::git::capture_snapshot(path).ok());
        let effective_branch = current_branch.or_else(|| {
            current_snapshot
                .as_ref()
                .and_then(|snapshot| snapshot.branch.as_deref())
        });
        let current_head = current_snapshot
            .as_ref()
            .and_then(|snapshot| snapshot.head.as_deref());
        let mut suggestions = Vec::new();
        for hit in self.search_tasks(query, limit.saturating_mul(3).max(limit))? {
            if hit.task.repository_id != repository_id
                || hit.task.lifecycle != TaskLifecycle::Active
            {
                continue;
            }
            let age_days = now
                .signed_duration_since(hit.task.updated_at)
                .num_days()
                .max(0);
            let recency = (1.0 / (1.0 + age_days as f64 / 30.0)).min(1.0);
            let lexical = lexical_overlap(
                query,
                &format!(
                    "{} {}",
                    hit.task.title,
                    hit.task.goal.as_deref().unwrap_or_default()
                ),
            );
            let branch_match = effective_branch
                .zip(hit.task.branch.as_deref())
                .is_some_and(|(current, task)| current == task);
            let task_head = self
                .list_checkpoints(&hit.task.id)?
                .last()
                .and_then(|checkpoint| checkpoint.git_after.head.clone());
            let branch_ancestor = !branch_match
                && task_head
                    .as_deref()
                    .zip(current_head)
                    .is_some_and(|(ancestor, descendant)| {
                        repository_path.as_deref().is_some_and(|path| {
                            crate::git::is_ancestor(path, ancestor, descendant).unwrap_or(false)
                        })
                    });
            let mut matching_reasons = vec!["same_repository".to_string()];
            if lexical > 0.0 {
                matching_reasons.push("full_text_match".to_string());
            }
            if branch_match {
                matching_reasons.push("same_branch".to_string());
            } else if branch_ancestor {
                matching_reasons.push("branch_ancestor".to_string());
            }
            let latest_session = self.get_task_timeline(&hit.task.id)?.and_then(|timeline| {
                timeline
                    .sessions
                    .into_iter()
                    .max_by_key(|session| session.last_activity_at.unwrap_or(session.started_at))
            });
            let continuation_advice = latest_session.and_then(|session| {
                (session.continuation_state != ContinuationStateV1::Normal).then(|| {
                    let mut reasons = Vec::new();
                    if session.compaction_count >= 6 {
                        reasons.push(ContinuationReasonV1::CompactionLimit);
                    }
                    if session
                        .context_usage
                        .as_ref()
                        .and_then(ContextUsageV1::utilization)
                        .is_some_and(|ratio| ratio >= 0.8)
                    {
                        reasons.push(ContinuationReasonV1::ContextUsageLimit);
                    }
                    ContinuationAdviceV1 {
                        action: "new_thread".to_string(),
                        reasons,
                        task_id: hit.task.id.clone(),
                        task_title: hit.task.title.clone(),
                        last_activity_at: session.last_activity_at.unwrap_or(session.started_at),
                        compaction_count: session.compaction_count,
                        context_usage: session.context_usage,
                    }
                })
            });
            suggestions.push(TaskSuggestionV1 {
                task_id: hit.task.id,
                title: hit.task.title,
                score: (0.65 * lexical
                    + 0.20 * recency
                    + if branch_match || branch_ancestor {
                        0.15
                    } else {
                        0.0
                    })
                .clamp(0.0, 1.0),
                last_activity_at: hit.task.updated_at,
                matching_reasons,
                continuation_advice,
            });
        }
        suggestions.sort_by(|a, b| {
            b.score
                .total_cmp(&a.score)
                .then_with(|| b.last_activity_at.cmp(&a.last_activity_at))
                .then_with(|| a.task_id.cmp(&b.task_id))
        });
        suggestions.truncate(limit);
        Ok(suggestions)
    }

    pub fn export_json(&self, repository_id: Option<&str>) -> Result<Value> {
        let events = self.list_events(repository_id)?;
        let repositories = self
            .list_repositories()?
            .into_iter()
            .filter(|repository| repository_id.map(|id| id == repository.id).unwrap_or(true))
            .collect::<Vec<_>>();
        let connection = self.connect()?;
        let mut tasks: Vec<TaskV1> = query_json_rows(
            &connection,
            "SELECT task_json FROM tasks ORDER BY repository_id, updated_at, id",
            [],
        )?;
        tasks.retain(|task| {
            repository_id
                .map(|id| id == task.repository_id)
                .unwrap_or(true)
        });
        let task_ids = tasks.iter().map(|task| task.id.clone()).collect::<Vec<_>>();
        let mut sessions = Vec::new();
        let mut checkpoints = Vec::new();
        let mut facts = Vec::new();
        let mut evidence = Vec::new();
        let mut file_changes = Vec::new();
        let mut test_results = Vec::new();
        for task_id in task_ids {
            if let Some(timeline) = self.get_task_timeline(&task_id)? {
                sessions.extend(timeline.sessions);
                checkpoints.extend(timeline.checkpoints);
                facts.extend(timeline.facts);
            }
            evidence.extend(self.list_evidence(&task_id)?);
            file_changes.extend(self.list_file_changes(&task_id)?);
            test_results.extend(self.list_test_results(&task_id)?);
        }
        Ok(json!({
            "schema_version": SCHEMA_VERSION_V1,
            "exported_at": timestamp(Utc::now()),
            "repositories": repositories,
            "tasks": tasks,
            "sessions": sessions,
            "canonical_events": events,
            "checkpoints": checkpoints,
            "facts": facts,
            "evidence": evidence,
            "file_changes": file_changes,
            "test_results": test_results,
        }))
    }

    pub fn purge_repository(&self, repository_id: &str) -> Result<()> {
        self.purge_repository_with(repository_id, || {
            self.purge_recovery_queue_files(repository_id)
        })
    }

    pub fn purge_repository_with<F>(&self, repository_id: &str, purge_related: F) -> Result<()>
    where
        F: FnOnce() -> Result<()>,
    {
        let _lock = self.acquire_maintenance_lock()?;
        let existing_journal = self.read_purge_journal()?;
        if let Some(existing) = existing_journal.as_ref() {
            if existing.repository_id != repository_id {
                bail!(
                    "cannot purge repository {repository_id}; purge for {} is already pending",
                    existing.repository_id
                );
            }
        }
        let mut journal = match existing_journal {
            Some(existing) => existing,
            None => {
                let journal = PurgeRecoveryJournalV1 {
                    version: 1,
                    repository_id: repository_id.to_string(),
                    phase: PurgeJournalPhase::Tombstoned,
                };
                self.write_purge_journal(&journal)?;
                journal
            }
        };
        // This marker outlives the recovery journal. It closes the race where a hook begins an
        // append during purge, waits for the maintenance lock, and would otherwise write after
        // the journal had been removed. The recovery journal is written first so every crash
        // boundary has an automatic completion path; queue append treats either file as a gate.
        self.write_repository_tombstone(repository_id)?;
        // Queue/cache cleanup must finish before the canonical DB is replaced. If this callback
        // fails, the durable tombstone remains and blocks ingestion/replay for this repository.
        if journal.phase == PurgeJournalPhase::Tombstoned {
            purge_related()?;
            journal.phase = PurgeJournalPhase::RelatedDataPurged;
            self.write_purge_journal(&journal)?;
        }
        let retained = self
            .list_events(None)?
            .into_iter()
            .filter(|event| event.repository_id != repository_id)
            .collect::<Vec<_>>();
        self.replace_with_events(&retained)?;
        self.verify_repository_absent(repository_id)?;
        self.clean_purge_artifacts()?;
        self.remove_purge_journal()?;
        Ok(())
    }

    pub fn apply_retention(
        &self,
        now: DateTime<Utc>,
        retention_days: i64,
    ) -> Result<RetentionReport> {
        let _lock = self.acquire_maintenance_lock()?;
        let events = self.list_events(None)?;
        let cutoff = now - ChronoDuration::days(retention_days.max(1));
        let connection = self.connect()?;
        let facts: Vec<FactV1> = query_json_rows(
            &connection,
            "SELECT fact_json FROM facts WHERE lifecycle = 'pinned'",
            [],
        )?;
        let pinned_fact_ids = facts
            .iter()
            .map(|fact| fact.id.clone())
            .collect::<std::collections::BTreeSet<_>>();
        let pinned_evidence_ids = facts
            .iter()
            .flat_map(|fact| fact.evidence_ids.iter().cloned())
            .collect::<std::collections::BTreeSet<_>>();
        let evidence: Vec<EvidenceV1> =
            query_json_rows(&connection, "SELECT evidence_json FROM evidence", [])?;
        let pinned_source_ids = evidence
            .iter()
            .filter(|item| pinned_evidence_ids.contains(&item.id))
            .map(|item| item.source_id.clone())
            .collect::<std::collections::BTreeSet<_>>();
        drop(connection);

        let retained = events
            .iter()
            .filter(|event| {
                event.occurred_at >= cutoff
                    || pinned_source_ids.contains(&event.source_id)
                    || event
                        .payload
                        .pointer("/fact/id")
                        .and_then(Value::as_str)
                        .is_some_and(|id| pinned_fact_ids.contains(id))
                    || event
                        .payload
                        .pointer("/evidence/id")
                        .and_then(Value::as_str)
                        .is_some_and(|id| pinned_evidence_ids.contains(id))
            })
            .cloned()
            .collect::<Vec<_>>();
        let report = RetentionReport {
            retained_events: retained.len(),
            removed_events: events.len().saturating_sub(retained.len()),
        };
        if report.removed_events > 0 {
            self.replace_with_events(&retained)?;
        }
        Ok(report)
    }

    pub fn health(&self) -> Result<StoreHealth> {
        let connection = self.connect()?;
        let journal_mode = connection.query_row("PRAGMA journal_mode", [], |row| row.get(0))?;
        let integrity_check =
            connection.query_row("PRAGMA integrity_check", [], |row| row.get(0))?;
        let schema_version = connection.query_row("PRAGMA user_version", [], |row| row.get(0))?;
        let canonical_event_count =
            connection.query_row("SELECT COUNT(*) FROM canonical_events", [], |row| {
                row.get::<_, u64>(0)
            })?;
        let projection_task_count =
            connection.query_row("SELECT COUNT(*) FROM tasks", [], |row| row.get::<_, u64>(0))?;
        Ok(StoreHealth {
            schema_version,
            journal_mode,
            integrity_check,
            canonical_event_count,
            projection_task_count,
        })
    }

    fn connect(&self) -> Result<Connection> {
        let connection = Connection::open(&self.path)
            .with_context(|| format!("open PreviouslyOn database {}", self.path.display()))?;
        connection.busy_timeout(std::time::Duration::from_secs(5))?;
        connection.execute_batch(
            "PRAGMA foreign_keys = ON;
             PRAGMA journal_mode = WAL;
             PRAGMA synchronous = FULL;
             PRAGMA temp_store = MEMORY;",
        )?;
        Ok(connection)
    }

    fn acquire_maintenance_lock(&self) -> Result<fs::File> {
        acquire_database_lock(&self.path)
    }

    fn purge_journal_path(&self) -> PathBuf {
        PathBuf::from(format!(
            "{}.purge-recovery.json",
            self.path.to_string_lossy()
        ))
    }

    fn read_purge_journal(&self) -> Result<Option<PurgeRecoveryJournalV1>> {
        let path = self.purge_journal_path();
        let bytes = match fs::read(&path) {
            Ok(bytes) => bytes,
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None),
            Err(error) => return Err(error.into()),
        };
        let journal: PurgeRecoveryJournalV1 = serde_json::from_slice(&bytes)
            .with_context(|| format!("parse purge recovery journal {}", path.display()))?;
        if journal.version != 1 {
            bail!("unsupported purge recovery journal version");
        }
        Ok(Some(journal))
    }

    fn write_purge_journal(&self, journal: &PurgeRecoveryJournalV1) -> Result<()> {
        let path = self.purge_journal_path();
        let temporary = path.with_extension(format!("tmp-{}", uuid::Uuid::now_v7()));
        let mut options = OpenOptions::new();
        options.create_new(true).write(true);
        #[cfg(unix)]
        {
            use std::os::unix::fs::OpenOptionsExt;
            options.mode(0o600);
        }
        let mut file = options.open(&temporary)?;
        file.write_all(&serde_json::to_vec(journal)?)?;
        file.sync_all()?;
        drop(file);
        fs::rename(&temporary, &path)?;
        set_file_permissions(&path)?;
        fs::File::open(path.parent().context("purge journal has no parent")?)?.sync_all()?;
        Ok(())
    }

    fn remove_purge_journal(&self) -> Result<()> {
        let path = self.purge_journal_path();
        match fs::remove_file(&path) {
            Ok(()) => {
                fs::File::open(path.parent().context("purge journal has no parent")?)?
                    .sync_all()?;
                Ok(())
            }
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(()),
            Err(error) => Err(error.into()),
        }
    }

    fn recover_purge_if_ready(&self) -> Result<()> {
        let Some(mut journal) = self.read_purge_journal()? else {
            return Ok(());
        };
        let _lock = self.acquire_maintenance_lock()?;
        self.write_repository_tombstone(&journal.repository_id)?;
        if journal.phase == PurgeJournalPhase::Tombstoned {
            // v0.1 supports a single registered repository. On recovery, malformed queue data
            // cannot be proven unrelated and is therefore discarded with the repository rather
            // than risking resurrection after the compacted DB is swapped in.
            self.purge_recovery_queue_files(&journal.repository_id)?;
            journal.phase = PurgeJournalPhase::RelatedDataPurged;
            self.write_purge_journal(&journal)?;
        }
        let retained = self
            .list_events(None)?
            .into_iter()
            .filter(|event| event.repository_id != journal.repository_id)
            .collect::<Vec<_>>();
        self.replace_with_events(&retained)?;
        self.verify_repository_absent(&journal.repository_id)?;
        self.clean_purge_artifacts()?;
        self.remove_purge_journal()
    }

    fn write_repository_tombstone(&self, repository_id: &str) -> Result<()> {
        let data_dir = self.path.parent().context("database path has no parent")?;
        let path = repository_tombstone_path(data_dir, repository_id);
        match fs::metadata(&path) {
            Ok(_) => return Ok(()),
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
            Err(error) => {
                return Err(error).with_context(|| {
                    format!("inspect repository purge tombstone {}", path.display())
                })
            }
        }
        let directory = path.parent().context("purge tombstone has no parent")?;
        fs::create_dir_all(directory)?;
        set_directory_permissions(directory)?;
        fs::File::open(data_dir)?.sync_all()?;
        let tombstone = RepositoryPurgeTombstoneV1 {
            version: PURGE_TOMBSTONE_VERSION,
            generation: uuid::Uuid::now_v7().to_string(),
            created_at: timestamp(Utc::now()),
        };
        write_private_atomic_file(&path, &serde_json::to_vec(&tombstone)?)
    }

    fn purge_recovery_queue_files(&self, repository_id: &str) -> Result<()> {
        let data_dir = self.path.parent().context("database path has no parent")?;
        let queue = data_dir.join("queue/events.jsonl");
        for path in [
            queue.clone(),
            queue.with_extension("replay.jsonl"),
            queue.with_extension("corrupt.jsonl"),
        ] {
            self.rewrite_recovery_queue(&path, repository_id)?;
        }
        let cache = data_dir.join("cache");
        if cache.exists() {
            fs::remove_dir_all(&cache)
                .with_context(|| format!("remove purge cache {}", cache.display()))?;
            fs::File::open(data_dir)?.sync_all()?;
        }
        Ok(())
    }

    fn rewrite_recovery_queue(&self, path: &Path, repository_id: &str) -> Result<()> {
        let bytes = match fs::read(path) {
            Ok(bytes) => bytes,
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(()),
            Err(error) => return Err(error.into()),
        };
        if path
            .file_name()
            .and_then(|name| name.to_str())
            .is_some_and(|name| name.contains("corrupt"))
        {
            return remove_file_and_sync_parent(path);
        }
        let Ok(contents) = std::str::from_utf8(&bytes) else {
            return remove_file_and_sync_parent(path);
        };
        let mut retained = Vec::new();
        for line in contents.lines().filter(|line| !line.trim().is_empty()) {
            let Ok(event) = serde_json::from_str::<EventEnvelopeV1>(line) else {
                return remove_file_and_sync_parent(path);
            };
            if event.repository_id != repository_id {
                retained.push(line);
            }
        }
        let mut replacement = retained.join("\n");
        if !replacement.is_empty() {
            replacement.push('\n');
        }
        write_private_atomic_file(path, replacement.as_bytes())
    }

    fn clean_purge_artifacts(&self) -> Result<()> {
        remove_sidecar_if_present(&self.path, "wal")?;
        remove_sidecar_if_present(&self.path, "shm")?;
        let data_dir = self.path.parent().context("database path has no parent")?;
        let cache = data_dir.join("cache");
        if cache.exists() {
            fs::remove_dir_all(&cache)
                .with_context(|| format!("remove purge cache {}", cache.display()))?;
        }
        fs::File::open(data_dir)?.sync_all()?;
        Ok(())
    }

    fn verify_repository_absent(&self, repository_id: &str) -> Result<()> {
        let connection = self.connect()?;
        let remaining: u64 = connection.query_row(
            "SELECT COUNT(*) FROM canonical_events WHERE repository_id = ?1",
            [repository_id],
            |row| row.get(0),
        )?;
        if remaining != 0 {
            bail!("purge verification failed for repository {repository_id}");
        }
        Ok(())
    }

    fn replace_with_events(&self, events: &[EventEnvelopeV1]) -> Result<()> {
        let parent = self.path.parent().context("database path has no parent")?;
        let temp_path = parent.join(format!(
            ".previously-compaction-{}.sqlite3",
            uuid::Uuid::now_v7()
        ));
        let temp_store = Store::open(&temp_path)?;
        for event in events {
            temp_store.insert_event(event)?;
        }
        {
            let connection = temp_store.connect()?;
            let integrity: String =
                connection.query_row("PRAGMA integrity_check", [], |row| row.get(0))?;
            if integrity != "ok" {
                bail!("compacted database failed integrity check: {integrity}");
            }
            connection.execute_batch(
                "PRAGMA wal_checkpoint(TRUNCATE); PRAGMA journal_mode=DELETE; VACUUM;",
            )?;
        }
        set_file_permissions(&temp_path)?;
        fs::File::open(&temp_path)?.sync_all()?;
        {
            let connection = self.connect()?;
            connection.execute_batch("PRAGMA wal_checkpoint(TRUNCATE);")?;
        }
        remove_sidecar_if_present(&self.path, "wal")?;
        remove_sidecar_if_present(&self.path, "shm")?;
        fs::rename(&temp_path, &self.path).context("atomically replace compacted database")?;
        fs::File::open(parent)?.sync_all()?;
        set_file_permissions(&self.path)?;
        Ok(())
    }

    fn migrate(&self, connection: &mut Connection) -> Result<()> {
        connection.execute_batch(MIGRATION_V1)?;
        connection.pragma_update(None, "user_version", DATABASE_SCHEMA_VERSION)?;
        Ok(())
    }
}

fn lexical_overlap(query: &str, candidate: &str) -> f64 {
    let query_tokens = query
        .split(|character: char| !character.is_alphanumeric())
        .filter(|token| token.chars().count() >= 2)
        .map(|token| token.to_lowercase())
        .collect::<std::collections::BTreeSet<_>>();
    if query_tokens.is_empty() {
        return 0.0;
    }
    let candidate_tokens = candidate
        .split(|character: char| !character.is_alphanumeric())
        .filter(|token| token.chars().count() >= 2)
        .map(|token| token.to_lowercase())
        .collect::<std::collections::BTreeSet<_>>();
    query_tokens.intersection(&candidate_tokens).count() as f64 / query_tokens.len() as f64
}

const MIGRATION_V1: &str = r#"
CREATE TABLE IF NOT EXISTS repositories (
  id TEXT PRIMARY KEY,
  repository_id TEXT NOT NULL UNIQUE,
  path TEXT NOT NULL,
  updated_at TEXT NOT NULL,
  repository_json TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS canonical_events (
  event_id TEXT PRIMARY KEY,
  dedupe_key TEXT NOT NULL UNIQUE,
  repository_id TEXT NOT NULL,
  session_id TEXT NOT NULL,
  task_id TEXT,
  sequence_no INTEGER,
  occurred_at TEXT NOT NULL,
  received_at TEXT NOT NULL,
  kind TEXT NOT NULL,
  event_json TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS canonical_events_replay
  ON canonical_events(occurred_at, sequence_no, event_id);
CREATE INDEX IF NOT EXISTS canonical_events_repository
  ON canonical_events(repository_id, session_id, occurred_at);
CREATE TRIGGER IF NOT EXISTS canonical_events_no_update
BEFORE UPDATE ON canonical_events BEGIN SELECT RAISE(ABORT, 'canonical events are immutable'); END;

CREATE TABLE IF NOT EXISTS tasks (
  id TEXT PRIMARY KEY,
  repository_id TEXT NOT NULL,
  updated_at TEXT NOT NULL,
  task_json TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS tasks_repository ON tasks(repository_id, updated_at);
CREATE TABLE IF NOT EXISTS sessions (
  id TEXT PRIMARY KEY,
  repository_id TEXT NOT NULL,
  task_id TEXT,
  started_at TEXT NOT NULL,
  session_json TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS sessions_task ON sessions(task_id, started_at);
CREATE TABLE IF NOT EXISTS checkpoints (
  id TEXT PRIMARY KEY,
  repository_id TEXT NOT NULL,
  task_id TEXT NOT NULL,
  session_id TEXT NOT NULL,
  created_at TEXT NOT NULL,
  checkpoint_json TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS checkpoints_task ON checkpoints(task_id, created_at);
CREATE TABLE IF NOT EXISTS facts (
  id TEXT PRIMARY KEY,
  repository_id TEXT NOT NULL,
  task_id TEXT NOT NULL,
  lifecycle TEXT NOT NULL,
  freshness TEXT NOT NULL,
  updated_at TEXT NOT NULL,
  fact_json TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS facts_task ON facts(task_id, updated_at);
CREATE TABLE IF NOT EXISTS evidence (
  id TEXT PRIMARY KEY,
  repository_id TEXT NOT NULL,
  task_id TEXT NOT NULL,
  fact_id TEXT,
  session_id TEXT NOT NULL,
  created_at TEXT NOT NULL,
  evidence_json TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS evidence_task ON evidence(task_id, fact_id, created_at);
CREATE TABLE IF NOT EXISTS file_changes (
  id TEXT PRIMARY KEY,
  repository_id TEXT NOT NULL,
  task_id TEXT,
  session_id TEXT NOT NULL,
  path TEXT NOT NULL,
  change_json TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS file_changes_task ON file_changes(task_id, path);
CREATE TABLE IF NOT EXISTS test_results (
  id TEXT PRIMARY KEY,
  repository_id TEXT NOT NULL,
  task_id TEXT,
  session_id TEXT NOT NULL,
  occurred_at TEXT NOT NULL,
  test_json TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS test_results_task ON test_results(task_id, occurred_at);
CREATE VIRTUAL TABLE IF NOT EXISTS task_fts USING fts5(task_id UNINDEXED, title, goal, tokenize='unicode61');
CREATE VIRTUAL TABLE IF NOT EXISTS fact_fts USING fts5(fact_id UNINDEXED, content, tokenize='unicode61');
"#;

fn rebuild_projections_tx(transaction: &Transaction<'_>) -> Result<()> {
    let events = load_events(transaction, None)?;
    transaction.execute_batch(
        "DELETE FROM task_fts;
         DELETE FROM fact_fts;
         DELETE FROM test_results;
         DELETE FROM file_changes;
         DELETE FROM evidence;
         DELETE FROM facts;
         DELETE FROM checkpoints;
         DELETE FROM sessions;
         DELETE FROM tasks;",
    )?;
    for event in events {
        apply_event_projection(transaction, &event)?;
    }
    Ok(())
}

fn apply_event_projection(transaction: &Transaction<'_>, event: &EventEnvelopeV1) -> Result<()> {
    ensure_repository_tx(transaction, event)?;
    ensure_session_tx(transaction, event)?;
    ensure_task_tx(transaction, event)?;
    match event.kind {
        EventKind::SessionStarted => {
            if let Some(session) = payload_as::<SessionV1>(&event.payload, "session") {
                upsert_session_tx(transaction, &session)?;
            }
        }
        EventKind::SessionStopped => {
            if let Some(session) = payload_as::<SessionV1>(&event.payload, "session") {
                upsert_session_tx(transaction, &session)?;
            } else {
                let existing: Option<SessionV1> = query_json_optional(
                    transaction,
                    "SELECT session_json FROM sessions WHERE id = ?1",
                    [&event.session_id],
                )?;
                if let Some(mut session) = existing {
                    session.lifecycle = SessionLifecycle::Completed;
                    session.ended_at = Some(event.occurred_at);
                    upsert_session_tx(transaction, &session)?;
                }
            }
        }
        EventKind::Checkpoint => {
            if let Some(checkpoint) = payload_as::<CheckpointV1>(&event.payload, "checkpoint") {
                upsert_checkpoint_tx(transaction, &checkpoint)?;
            }
        }
        EventKind::FactCandidate | EventKind::FactConfirmed => {
            if let Some(mut fact) = payload_as::<FactV1>(&event.payload, "fact") {
                fact.content = redact_text(&fact.content);
                if event.kind == EventKind::FactConfirmed
                    && fact.lifecycle == FactLifecycle::Candidate
                {
                    fact.lifecycle = FactLifecycle::Confirmed;
                }
                upsert_fact_tx(transaction, &fact)?;
            }
            if let Some(mut evidence) = payload_as::<EvidenceV1>(&event.payload, "evidence") {
                evidence.excerpt = redact_excerpt(&evidence.excerpt);
                upsert_evidence_tx(transaction, &evidence)?;
            }
        }
        EventKind::ToolFinished => {
            for mut test in payload_array::<TestResultV1>(&event.payload, "test_results") {
                test.command = redact_text(&test.command);
                test.summary = test.summary.map(|summary| redact_excerpt(&summary));
                upsert_test_result_tx(transaction, &test)?;
            }
            if let Some(mut test) = payload_as::<TestResultV1>(&event.payload, "test_result") {
                test.command = redact_text(&test.command);
                test.summary = test.summary.map(|summary| redact_excerpt(&summary));
                upsert_test_result_tx(transaction, &test)?;
            }
            for change in payload_array::<FileChangeV1>(&event.payload, "file_changes") {
                upsert_file_change_tx(transaction, &change)?;
            }
        }
        _ => {}
    }
    Ok(())
}

fn ensure_repository_tx(transaction: &Transaction<'_>, event: &EventEnvelopeV1) -> Result<()> {
    let exists = transaction.query_row(
        "SELECT EXISTS(SELECT 1 FROM repositories WHERE id = ?1)",
        [&event.repository_id],
        |row| row.get::<_, bool>(0),
    )?;
    if exists {
        return Ok(());
    }
    let repository = RepositoryV1 {
        schema_version: SCHEMA_VERSION_V1,
        id: event.repository_id.clone(),
        path: event
            .payload
            .get("repository_path")
            .and_then(Value::as_str)
            .map(redact_text)
            .unwrap_or_default(),
        remote_url: None,
        created_at: event.occurred_at,
        updated_at: event.occurred_at,
    };
    upsert_repository_tx(transaction, &repository)
}

fn ensure_session_tx(transaction: &Transaction<'_>, event: &EventEnvelopeV1) -> Result<()> {
    let existing: Option<SessionV1> = query_json_optional(
        transaction,
        "SELECT session_json FROM sessions WHERE id = ?1",
        [&event.session_id],
    )?;
    if let Some(mut session) = existing {
        let mut changed = false;
        if event.task_id.is_some() && session.task_id != event.task_id {
            session.task_id = event.task_id.clone();
            changed = true;
        }
        if let Some(branch) = event.payload.get("branch").and_then(Value::as_str) {
            if session.branch.as_deref() != Some(branch) {
                session.branch = Some(branch.to_string());
                changed = true;
            }
        }
        if let Some(head) = event.payload.get("head").and_then(Value::as_str) {
            if session.head.as_deref() != Some(head) {
                session.head = Some(head.to_string());
                changed = true;
            }
        }
        if let Some(thread_id) = event
            .payload
            .get("source_thread_id")
            .or_else(|| event.payload.get("thread_id"))
            .and_then(Value::as_str)
        {
            if session.source_thread_id.as_deref() != Some(thread_id) {
                session.source_thread_id = Some(thread_id.to_string());
                changed = true;
            }
        }
        let last_activity = event
            .payload
            .get("thread_updated_at")
            .and_then(Value::as_str)
            .and_then(|value| value.parse().ok())
            .unwrap_or(event.occurred_at);
        if session
            .last_activity_at
            .is_none_or(|current| last_activity > current)
        {
            session.last_activity_at = Some(last_activity);
            changed = true;
        }
        if let Some(turn_count) = event
            .payload
            .get("turn_count")
            .and_then(Value::as_u64)
            .and_then(|value| u32::try_from(value).ok())
        {
            if turn_count > session.turn_count {
                session.turn_count = turn_count;
                changed = true;
            }
        } else if event.kind == EventKind::UserPrompt && session.source_thread_id.is_none() {
            session.turn_count = session.turn_count.saturating_add(1);
            changed = true;
        }
        if event.kind == EventKind::ContextCompaction {
            session.compaction_count = session.compaction_count.saturating_add(1);
            if session.compaction_count >= 6
                && session.continuation_state == ContinuationStateV1::Normal
            {
                session.continuation_state = ContinuationStateV1::Eligible;
            }
            changed = true;
        }
        if let Some(usage) = context_usage(&event.payload, event.occurred_at) {
            if usage.utilization().is_some_and(|ratio| ratio >= 0.8)
                && session.continuation_state == ContinuationStateV1::Normal
            {
                session.continuation_state = ContinuationStateV1::Eligible;
            }
            session.context_usage = Some(usage);
            changed = true;
        }
        if event.kind == EventKind::ContinuationSuggested {
            let next_state = if event.payload.get("delivery_state").and_then(Value::as_str)
                == Some("pending_replay")
            {
                ContinuationStateV1::Eligible
            } else {
                ContinuationStateV1::Suggested
            };
            if session.continuation_state != next_state {
                session.continuation_state = next_state;
                changed = true;
            }
        }
        let coverage = CoverageV1::merge([&session.coverage, &event.coverage]);
        if coverage != session.coverage {
            session.coverage = coverage;
            changed = true;
        }
        if changed {
            upsert_session_tx(transaction, &session)?;
        }
        return Ok(());
    }
    let started_at = event
        .payload
        .get("thread_created_at")
        .and_then(Value::as_str)
        .and_then(|value| value.parse().ok())
        .unwrap_or(event.occurred_at);
    let context_usage = context_usage(&event.payload, event.occurred_at);
    let compaction_count = u32::from(event.kind == EventKind::ContextCompaction);
    let session = SessionV1 {
        schema_version: SCHEMA_VERSION_V1,
        id: event.session_id.clone(),
        repository_id: event.repository_id.clone(),
        task_id: event.task_id.clone(),
        lifecycle: SessionLifecycle::Active,
        started_at,
        ended_at: None,
        branch: event
            .payload
            .get("branch")
            .and_then(Value::as_str)
            .map(str::to_string),
        head: event
            .payload
            .get("head")
            .and_then(Value::as_str)
            .map(str::to_string),
        source_thread_id: event
            .payload
            .get("source_thread_id")
            .or_else(|| event.payload.get("thread_id"))
            .and_then(Value::as_str)
            .map(str::to_string),
        last_activity_at: Some(
            event
                .payload
                .get("thread_updated_at")
                .and_then(Value::as_str)
                .and_then(|value| value.parse().ok())
                .unwrap_or(event.occurred_at),
        ),
        turn_count: event
            .payload
            .get("turn_count")
            .and_then(Value::as_u64)
            .and_then(|value| u32::try_from(value).ok())
            .unwrap_or(u32::from(event.kind == EventKind::UserPrompt)),
        compaction_count,
        context_usage: context_usage.clone(),
        continuation_state: if event.kind == EventKind::ContinuationSuggested {
            if event.payload.get("delivery_state").and_then(Value::as_str) == Some("pending_replay")
            {
                ContinuationStateV1::Eligible
            } else {
                ContinuationStateV1::Suggested
            }
        } else if compaction_count >= 6
            || context_usage
                .as_ref()
                .and_then(ContextUsageV1::utilization)
                .is_some_and(|ratio| ratio >= 0.8)
        {
            ContinuationStateV1::Eligible
        } else {
            ContinuationStateV1::Normal
        },
        coverage: event.coverage.clone(),
    };
    upsert_session_tx(transaction, &session)
}

fn context_usage(payload: &Value, occurred_at: DateTime<Utc>) -> Option<ContextUsageV1> {
    let usage = payload.get("context_usage").unwrap_or(payload);
    let total_tokens = usage
        .get("total_tokens")
        .or_else(|| usage.get("totalTokens"))
        .and_then(Value::as_u64)?;
    let model_context_window = usage
        .get("model_context_window")
        .or_else(|| usage.get("modelContextWindow"))
        .and_then(Value::as_u64)?;
    Some(ContextUsageV1 {
        total_tokens,
        model_context_window,
        observed_at: Some(occurred_at),
    })
}

fn ensure_task_tx(transaction: &Transaction<'_>, event: &EventEnvelopeV1) -> Result<()> {
    let Some(task_id) = event.task_id.as_deref() else {
        return Ok(());
    };
    if let Some(mut task) = payload_as::<TaskV1>(&event.payload, "task") {
        if task.id == task_id && task.repository_id == event.repository_id {
            if event.occurred_at > task.updated_at {
                task.updated_at = event.occurred_at;
            }
            return upsert_task_tx(transaction, &task);
        }
    }
    let existing: Option<TaskV1> = query_json_optional(
        transaction,
        "SELECT task_json FROM tasks WHERE id = ?1",
        [task_id],
    )?;
    if let Some(mut task) = existing {
        if event.occurred_at > task.updated_at {
            task.updated_at = event.occurred_at;
        }
        if task.branch.is_none() {
            task.branch = event
                .payload
                .get("branch")
                .and_then(Value::as_str)
                .map(str::to_string);
        }
        upsert_task_tx(transaction, &task)?;
        return Ok(());
    }
    let goal = payload_text(&event.payload);
    let title = event
        .payload
        .get("task_title")
        .and_then(Value::as_str)
        .map(str::to_string)
        .or_else(|| {
            goal.as_ref().map(|goal| {
                goal.lines()
                    .next()
                    .unwrap_or(goal)
                    .chars()
                    .take(120)
                    .collect()
            })
        })
        .unwrap_or_else(|| task_id.to_string());
    upsert_task_tx(
        transaction,
        &TaskV1 {
            schema_version: SCHEMA_VERSION_V1,
            id: task_id.to_string(),
            repository_id: event.repository_id.clone(),
            title: redact_excerpt(&title),
            goal: goal.map(|goal| redact_excerpt(&goal)),
            lifecycle: TaskLifecycle::Active,
            branch: event
                .payload
                .get("branch")
                .and_then(Value::as_str)
                .map(str::to_string),
            created_at: event.occurred_at,
            updated_at: event.occurred_at,
        },
    )
}

fn upsert_repository_tx(transaction: &Transaction<'_>, repository: &RepositoryV1) -> Result<()> {
    let mut repository = repository.clone();
    repository.path = redact_text(&repository.path);
    repository.remote_url = repository.remote_url.map(|url| redact_text(&url));
    let json = serde_json::to_string(&repository)?;
    transaction.execute(
        "INSERT INTO repositories(id, repository_id, path, updated_at, repository_json)
         VALUES (?1, ?1, ?2, ?3, ?4)
         ON CONFLICT(id) DO UPDATE SET path=excluded.path, updated_at=excluded.updated_at,
           repository_json=excluded.repository_json",
        params![
            repository.id,
            repository.path,
            timestamp(repository.updated_at),
            json
        ],
    )?;
    Ok(())
}

fn upsert_task_tx(transaction: &Transaction<'_>, task: &TaskV1) -> Result<()> {
    let mut task = task.clone();
    task.title = redact_excerpt(&task.title);
    task.goal = task.goal.map(|goal| redact_excerpt(&goal));
    let json = serde_json::to_string(&task)?;
    transaction.execute(
        "INSERT INTO tasks(id, repository_id, updated_at, task_json) VALUES (?1, ?2, ?3, ?4)
         ON CONFLICT(id) DO UPDATE SET repository_id=excluded.repository_id,
           updated_at=excluded.updated_at, task_json=excluded.task_json",
        params![
            task.id,
            task.repository_id,
            timestamp(task.updated_at),
            json
        ],
    )?;
    transaction.execute("DELETE FROM task_fts WHERE task_id = ?1", [&task.id])?;
    transaction.execute(
        "INSERT INTO task_fts(task_id, title, goal) VALUES (?1, ?2, ?3)",
        params![task.id, task.title, task.goal.unwrap_or_default()],
    )?;
    Ok(())
}

fn upsert_session_tx(transaction: &Transaction<'_>, session: &SessionV1) -> Result<()> {
    let json = serde_json::to_string(session)?;
    transaction.execute(
        "INSERT INTO sessions(id, repository_id, task_id, started_at, session_json)
         VALUES (?1, ?2, ?3, ?4, ?5)
         ON CONFLICT(id) DO UPDATE SET repository_id=excluded.repository_id,
           task_id=excluded.task_id, started_at=excluded.started_at, session_json=excluded.session_json",
        params![session.id, session.repository_id, session.task_id, timestamp(session.started_at), json],
    )?;
    Ok(())
}

fn upsert_checkpoint_tx(transaction: &Transaction<'_>, checkpoint: &CheckpointV1) -> Result<()> {
    let checkpoint: CheckpointV1 = serde_json::from_value(redact_value(
        &serde_json::to_value(checkpoint).context("serialize checkpoint for redaction")?,
    ))
    .context("deserialize redacted checkpoint")?;
    let json = serde_json::to_string(&checkpoint)?;
    transaction.execute(
        "INSERT INTO checkpoints(id, repository_id, task_id, session_id, created_at, checkpoint_json)
         VALUES (?1, ?2, ?3, ?4, ?5, ?6)
         ON CONFLICT(id) DO UPDATE SET checkpoint_json=excluded.checkpoint_json",
        params![checkpoint.id, checkpoint.repository_id, checkpoint.task_id, checkpoint.session_id, timestamp(checkpoint.created_at), json],
    )?;
    for change in &checkpoint.changed_files {
        upsert_file_change_tx(transaction, change)?;
    }
    for test in &checkpoint.tests {
        upsert_test_result_tx(transaction, test)?;
    }
    Ok(())
}

fn upsert_fact_tx(transaction: &Transaction<'_>, fact: &FactV1) -> Result<()> {
    let mut fact = fact.clone();
    fact.content = redact_text(&fact.content);
    let json = serde_json::to_string(&fact)?;
    transaction.execute(
        "INSERT INTO facts(id, repository_id, task_id, lifecycle, freshness, updated_at, fact_json)
         VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)
         ON CONFLICT(id) DO UPDATE SET repository_id=excluded.repository_id,
           task_id=excluded.task_id, lifecycle=excluded.lifecycle, freshness=excluded.freshness,
           updated_at=excluded.updated_at, fact_json=excluded.fact_json",
        params![
            fact.id,
            fact.repository_id,
            fact.task_id,
            enum_text(fact.lifecycle)?,
            enum_text(fact.freshness)?,
            timestamp(fact.updated_at),
            json
        ],
    )?;
    transaction.execute("DELETE FROM fact_fts WHERE fact_id = ?1", [&fact.id])?;
    transaction.execute(
        "INSERT INTO fact_fts(fact_id, content) VALUES (?1, ?2)",
        params![fact.id, fact.content],
    )?;
    Ok(())
}

fn upsert_evidence_tx(transaction: &Transaction<'_>, evidence: &EvidenceV1) -> Result<()> {
    let mut evidence = evidence.clone();
    evidence.excerpt = redact_excerpt(&evidence.excerpt);
    evidence.excerpt_sha256 = hex::encode(sha2::Sha256::digest(evidence.excerpt.as_bytes()));
    let json = serde_json::to_string(&evidence)?;
    transaction.execute(
        "INSERT INTO evidence(id, repository_id, task_id, fact_id, session_id, created_at, evidence_json)
         VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)
         ON CONFLICT(id) DO UPDATE SET fact_id=excluded.fact_id, evidence_json=excluded.evidence_json",
        params![evidence.id, evidence.repository_id, evidence.task_id, evidence.fact_id, evidence.session_id, timestamp(evidence.created_at), json],
    )?;
    Ok(())
}

fn upsert_file_change_tx(transaction: &Transaction<'_>, change: &FileChangeV1) -> Result<()> {
    let id = deterministic_id(
        "change",
        &[
            &change.repository_id,
            &change.session_id,
            change.task_id.as_deref().unwrap_or_default(),
            &change.path,
            change.previous_path.as_deref().unwrap_or_default(),
            change.before_head.as_deref().unwrap_or_default(),
            change.after_head.as_deref().unwrap_or_default(),
        ],
    );
    let mut change = change.clone();
    change.path = redact_text(&change.path);
    change.previous_path = change.previous_path.map(|path| redact_text(&path));
    let json = serde_json::to_string(&change)?;
    transaction.execute(
        "INSERT INTO file_changes(id, repository_id, task_id, session_id, path, change_json)
         VALUES (?1, ?2, ?3, ?4, ?5, ?6)
         ON CONFLICT(id) DO UPDATE SET change_json=excluded.change_json",
        params![
            id,
            change.repository_id,
            change.task_id,
            change.session_id,
            change.path,
            json
        ],
    )?;
    Ok(())
}

fn upsert_test_result_tx(transaction: &Transaction<'_>, test: &TestResultV1) -> Result<()> {
    let mut test = test.clone();
    test.command = redact_text(&test.command);
    test.summary = test.summary.map(|summary| redact_excerpt(&summary));
    let json = serde_json::to_string(&test)?;
    transaction.execute(
        "INSERT INTO test_results(id, repository_id, task_id, session_id, occurred_at, test_json)
         VALUES (?1, ?2, ?3, ?4, ?5, ?6)
         ON CONFLICT(id) DO UPDATE SET test_json=excluded.test_json",
        params![
            test.id,
            test.repository_id,
            test.task_id,
            test.session_id,
            timestamp(test.occurred_at),
            json
        ],
    )?;
    Ok(())
}

fn load_events(
    connection: &Connection,
    repository_id: Option<&str>,
) -> Result<Vec<EventEnvelopeV1>> {
    let (sql, parameter): (&str, Option<&str>) = if repository_id.is_some() {
        (
            "SELECT event_json FROM canonical_events WHERE repository_id = ?1
             ORDER BY occurred_at, COALESCE(sequence_no, 9223372036854775807), event_id",
            repository_id,
        )
    } else {
        (
            "SELECT event_json FROM canonical_events
             ORDER BY occurred_at, COALESCE(sequence_no, 9223372036854775807), event_id",
            None,
        )
    };
    let mut statement = connection.prepare(sql)?;
    let json_rows = if let Some(parameter) = parameter {
        statement
            .query_map([parameter], |row| row.get::<_, String>(0))?
            .collect::<rusqlite::Result<Vec<_>>>()?
    } else {
        statement
            .query_map([], |row| row.get::<_, String>(0))?
            .collect::<rusqlite::Result<Vec<_>>>()?
    };
    json_rows
        .into_iter()
        .map(|json| serde_json::from_str(&json).context("deserialize canonical event"))
        .collect()
}

fn query_json_optional<T, P>(connection: &Connection, sql: &str, params: P) -> Result<Option<T>>
where
    T: DeserializeOwned,
    P: rusqlite::Params,
{
    let json = connection
        .query_row(sql, params, |row| row.get::<_, String>(0))
        .optional()?;
    json.map(|json| serde_json::from_str(&json).context("deserialize database projection"))
        .transpose()
}

fn query_json_rows<T, P>(connection: &Connection, sql: &str, params: P) -> Result<Vec<T>>
where
    T: DeserializeOwned,
    P: rusqlite::Params,
{
    let mut statement = connection.prepare(sql)?;
    let rows = statement.query_map(params, |row| row.get::<_, String>(0))?;
    rows.map(|row| {
        let json = row?;
        serde_json::from_str(&json).context("deserialize database projection")
    })
    .collect()
}

fn payload_as<T: DeserializeOwned>(payload: &Value, key: &str) -> Option<T> {
    serde_json::from_value(payload.clone()).ok().or_else(|| {
        payload
            .get(key)
            .cloned()
            .and_then(|value| serde_json::from_value(value).ok())
    })
}

fn payload_array<T: DeserializeOwned>(payload: &Value, key: &str) -> Vec<T> {
    payload
        .get(key)
        .and_then(Value::as_array)
        .into_iter()
        .flatten()
        .filter_map(|item| serde_json::from_value(item.clone()).ok())
        .collect()
}

fn payload_text(payload: &Value) -> Option<String> {
    payload
        .as_str()
        .map(str::to_string)
        .or_else(|| {
            payload
                .get("text")
                .and_then(Value::as_str)
                .map(str::to_string)
        })
        .or_else(|| {
            payload
                .get("prompt")
                .and_then(Value::as_str)
                .map(str::to_string)
        })
        .or_else(|| {
            payload
                .get("content")
                .and_then(Value::as_str)
                .map(str::to_string)
        })
}

fn fts_query(query: &str) -> String {
    query
        .split(|character: char| !character.is_alphanumeric() && character != '_')
        .filter(|token| !token.is_empty())
        .take(12)
        .map(|token| format!("\"{}\"*", token.replace('"', "")))
        .collect::<Vec<_>>()
        .join(" AND ")
}

fn timestamp(timestamp: DateTime<Utc>) -> String {
    timestamp.to_rfc3339_opts(SecondsFormat::Micros, true)
}

fn enum_text<T: Serialize>(value: T) -> Result<String> {
    let value = serde_json::to_value(value)?;
    Ok(value.as_str().unwrap_or_default().to_string())
}

fn repository_tombstone_path(data_dir: &Path, repository_id: &str) -> PathBuf {
    let identity_hash = hex::encode(sha2::Sha256::digest(repository_id.as_bytes()));
    data_dir
        .join("purge-tombstones")
        .join(format!("{identity_hash}.json"))
}

fn acquire_database_lock(database_path: &Path) -> Result<fs::File> {
    let lock_path = PathBuf::from(format!("{}.lock", database_path.to_string_lossy()));
    if let Some(parent) = lock_path.parent() {
        fs::create_dir_all(parent)?;
        set_directory_permissions(parent)?;
    }
    let mut options = OpenOptions::new();
    options.create(true).read(true).write(true);
    #[cfg(unix)]
    {
        use std::os::unix::fs::OpenOptionsExt;
        options.mode(0o600);
    }
    let file = options
        .open(&lock_path)
        .with_context(|| format!("open database maintenance lock {}", lock_path.display()))?;
    file.lock()
        .with_context(|| format!("lock database maintenance file {}", lock_path.display()))?;
    set_file_permissions(&lock_path)?;
    Ok(file)
}

fn write_private_atomic_file(path: &Path, bytes: &[u8]) -> Result<()> {
    let parent = path.parent().context("atomic file path has no parent")?;
    fs::create_dir_all(parent)?;
    set_directory_permissions(parent)?;
    let temporary = path.with_extension(format!("tmp-{}", uuid::Uuid::now_v7()));
    let mut options = OpenOptions::new();
    options.create_new(true).write(true);
    #[cfg(unix)]
    {
        use std::os::unix::fs::OpenOptionsExt;
        options.mode(0o600);
    }
    let mut file = options.open(&temporary)?;
    file.write_all(bytes)?;
    file.sync_all()?;
    drop(file);
    fs::rename(&temporary, path)?;
    set_file_permissions(path)?;
    fs::File::open(parent)?.sync_all()?;
    Ok(())
}

fn remove_file_and_sync_parent(path: &Path) -> Result<()> {
    match fs::remove_file(path) {
        Ok(()) => {
            if let Some(parent) = path.parent() {
                fs::File::open(parent)?.sync_all()?;
            }
            Ok(())
        }
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(()),
        Err(error) => Err(error.into()),
    }
}

fn remove_sidecar_if_present(database: &Path, suffix: &str) -> Result<()> {
    let sidecar = PathBuf::from(format!("{}-{suffix}", database.to_string_lossy()));
    match fs::remove_file(&sidecar) {
        Ok(()) => Ok(()),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(()),
        Err(error) => {
            Err(error).with_context(|| format!("remove database sidecar {}", sidecar.display()))
        }
    }
}

#[cfg(unix)]
fn set_directory_permissions(path: &Path) -> Result<()> {
    use std::os::unix::fs::PermissionsExt;
    fs::set_permissions(path, fs::Permissions::from_mode(0o700))?;
    Ok(())
}

#[cfg(not(unix))]
fn set_directory_permissions(_path: &Path) -> Result<()> {
    Ok(())
}

#[cfg(unix)]
fn set_file_permissions(path: &Path) -> Result<()> {
    use std::os::unix::fs::PermissionsExt;
    if path.exists() {
        fs::set_permissions(path, fs::Permissions::from_mode(0o600))?;
    }
    Ok(())
}

#[cfg(not(unix))]
fn set_file_permissions(_path: &Path) -> Result<()> {
    Ok(())
}

#[cfg(test)]
mod insert_fault_tests {
    use super::*;
    use crate::domain::{EventKind, SCHEMA_VERSION_V1};
    use serde_json::json;

    fn event() -> EventEnvelopeV1 {
        let mut event = EventEnvelopeV1::new(
            "fault-source",
            "repo-fault",
            "session-fault",
            EventKind::UserPrompt,
            Utc::now(),
            json!({"prompt":"safe retry"}),
        );
        event.schema_version = SCHEMA_VERSION_V1;
        event
    }

    #[test]
    fn failures_before_commit_roll_back_and_retry_to_one_canonical_event() {
        for fault in [
            InsertFault {
                before_insert: true,
                ..InsertFault::default()
            },
            InsertFault {
                before_commit: true,
                ..InsertFault::default()
            },
        ] {
            let temp = tempfile::TempDir::new().unwrap();
            let store = Store::open(temp.path().join("previously.sqlite3")).unwrap();
            let event = event();
            assert!(store.insert_event_inner(&event, fault).is_err());
            assert_eq!(store.health().unwrap().canonical_event_count, 0);
            assert_eq!(store.insert_event(&event).unwrap(), InsertOutcome::Inserted);
            assert_eq!(store.health().unwrap().canonical_event_count, 1);
        }
    }

    #[test]
    fn failure_after_commit_replays_as_duplicate_without_projection_duplication() {
        let temp = tempfile::TempDir::new().unwrap();
        let store = Store::open(temp.path().join("previously.sqlite3")).unwrap();
        let event = event();
        let error = store
            .insert_event_inner(
                &event,
                InsertFault {
                    after_commit: true,
                    ..InsertFault::default()
                },
            )
            .unwrap_err();
        assert!(error.to_string().contains("after canonical commit"));
        assert_eq!(store.health().unwrap().canonical_event_count, 1);
        assert_eq!(
            store.insert_event(&event).unwrap(),
            InsertOutcome::Duplicate
        );
        assert_eq!(store.health().unwrap().canonical_event_count, 1);
    }

    #[test]
    fn sqlite_full_errors_are_classified_for_reserve_queue_fallback() {
        let sqlite = rusqlite::Error::SqliteFailure(
            rusqlite::ffi::Error::new(rusqlite::ffi::SQLITE_FULL),
            Some("disk full".to_string()),
        );
        assert!(is_sqlite_full(&anyhow::Error::new(sqlite)));
        assert!(!is_sqlite_full(&anyhow::anyhow!("other failure")));
    }
}