omgbase-store 15.0.0

omgbase store: the embedded SQLite database that owns block identity, history and current state — Rust implementation
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
//! Archive segments (`spec/store/README.md` §7.1 "Retention: plan, export,
//! squash", §6.2 "Through a segment", 15.0). Port of
//! `packages/core/src/core/store/archive.ts`.
//!
//! A segment is ONE SQLite file per `history export`, never appended: the
//! bodies (blobs, tree nodes) of the revisions the policy archived,
//! self-contained, plus copies of their `revisions`/`commits` rows, the
//! `dispositions` and `block_changes` of the commits that are wholly archived,
//! and one `manifest` row ([`ARCHIVE_SQL`] = `spec/store/archive.sql`,
//! embedded verbatim). The main store then SQUASHES in one transaction: an
//! archived revision keeps its row (id, doc, seq, path, rendered hash, commit
//! — so the `revisions` target, `docs_history` and `changes_since` keep
//! listing it) and loses `root_tree` and `frontmatter_blob`, gaining
//! `archive` = the segment id; the wholly archived commits' dispositions go,
//! their block_changes go except each block's EARLIEST row (the mint oracle of
//! §2.1 reads `block_changes`, so a minted id stays known); the GC sweep
//! reclaims the bodies now reachable from nothing; the segment is recorded in
//! `archives`. Two phases on purpose: the file is complete and durable before
//! a single row of the store moves, and a crash between the phases leaves a
//! complete, unrecorded segment that the next run finds and finishes (the
//! crash rule).
//!
//! Reading an archived revision ATTACHes its segment on demand (one alias per
//! segment, `arc_<id>`, read-only by construction — the file is written with
//! mode 0444 and the engine never writes through an alias). The attached
//! aliases are the connection's own state (`PRAGMA database_list`), so every
//! read over a `&Connection` finds them; past eight the oldest is detached.

use std::collections::{BTreeSet, HashSet};
use std::path::{Path, PathBuf};

use omgbase_format::hash::hex;
use rusqlite::{Connection, OpenFlags, OptionalExtension, params};
use serde_json::{Map, Value as Json};

use crate::derived::sweep_unreachable;
use crate::error::{Error, Result};
use crate::mint::Mint;
use crate::retention::{
    LAST_SEGMENT_SETTINGS_KEY, RETENTION_SETTINGS_KEY, ReachMarker, RetentionOptions,
    RetentionPlan, RevBodies, evaluate_retention, repo_settings,
};
pub use crate::schema::ARCHIVE_SQL;
use crate::schema::SCHEMA_VERSION;
use crate::tree::from_hex;

/// The file suffix every segment carries.
pub const ARCHIVE_SUFFIX: &str = ".omgarchive.db";
/// The default directory under `.omgbase/`.
pub const ARCHIVE_DIR: &str = "archives";
/// The suffix of a segment still being written (never a complete segment).
const PARTIAL_SUFFIX: &str = ".partial";
/// How many segments stay attached to one connection (SQLite's default cap is 10).
const MAX_ATTACHED: usize = 8;

/// `<repo_id>-<seq_lo>-<seq_hi>-<segment_id>.omgarchive.db`
#[must_use]
pub fn segment_file_name(repo_id: &str, seq_lo: i64, seq_hi: i64, segment_id: &str) -> String {
    format!("{repo_id}-{seq_lo}-{seq_hi}-{segment_id}{ARCHIVE_SUFFIX}")
}

/// The parts a segment file name carries.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SegmentName {
    pub repo_id: String,
    pub seq_lo: i64,
    pub seq_hi: i64,
    pub segment_id: String,
}

/// The parts of a segment file name, or `None` when the name is not one
/// (`^([a-z]+_[0-9a-z]+)-(\d+)-(\d+)-([a-z]+_[0-9a-z]+)\.omgarchive\.db$`).
#[must_use]
pub fn parse_segment_file_name(name: &str) -> Option<SegmentName> {
    let stem = name.strip_suffix(ARCHIVE_SUFFIX)?;
    let is_id = |s: &str| {
        let (p, rest) = s.split_once('_')?;
        (!p.is_empty()
            && p.bytes().all(|b| b.is_ascii_lowercase())
            && !rest.is_empty()
            && rest
                .bytes()
                .all(|b| b.is_ascii_digit() || b.is_ascii_lowercase()))
        .then_some(())
    };
    let is_num = |s: &str| !s.is_empty() && s.bytes().all(|b| b.is_ascii_digit());
    // The ids may carry a `-`-free body only, so split on `-` is exact.
    let parts: Vec<&str> = stem.split('-').collect();
    if parts.len() != 4 {
        return None;
    }
    is_id(parts[0])?;
    is_id(parts[3])?;
    if !is_num(parts[1]) || !is_num(parts[2]) {
        return None;
    }
    Some(SegmentName {
        repo_id: parts[0].to_owned(),
        seq_lo: parts[1].parse().ok()?,
        seq_hi: parts[2].parse().ok()?,
        segment_id: parts[3].to_owned(),
    })
}

/// A segment's `manifest` row.
#[derive(Clone, Debug, PartialEq)]
pub struct Manifest {
    pub segment_id: String,
    pub repo_id: String,
    pub created_at: String,
    pub policy: Json,
    pub seq_lo: i64,
    pub seq_hi: i64,
    pub revision_count: i64,
    pub previous_segment: Option<String>,
    pub schema_user_version: i64,
}

fn not_a_segment(path: &Path, detail: &str) -> Error {
    Error::NotASegment {
        path: path.to_string_lossy().into_owned(),
        detail: detail.to_owned(),
    }
}

fn open_read_only(path: &Path) -> rusqlite::Result<Connection> {
    Connection::open_with_flags(
        path,
        OpenFlags::SQLITE_OPEN_READ_ONLY | OpenFlags::SQLITE_OPEN_NO_MUTEX,
    )
}

/// Open a segment read-only and return its manifest; [`Error::NotASegment`]
/// when the file is not one.
pub fn read_manifest(path: &Path) -> Result<Manifest> {
    if !path.exists() {
        return Err(not_a_segment(path, "no such file"));
    }
    let read = || -> rusqlite::Result<Option<Manifest>> {
        let db = open_read_only(path)?;
        let row = db
            .query_row(
                "SELECT segment_id, repo_id, created_at, policy, seq_lo, seq_hi, revision_count, previous_segment, schema_user_version FROM manifest",
                [],
                |r| {
                    let policy: String = r.get(3)?;
                    Ok(Manifest {
                        segment_id: r.get(0)?,
                        repo_id: r.get(1)?,
                        created_at: r.get(2)?,
                        policy: serde_json::from_str(&policy).unwrap_or(Json::Null),
                        seq_lo: r.get(4)?,
                        seq_hi: r.get(5)?,
                        revision_count: r.get(6)?,
                        previous_segment: r.get(7)?,
                        schema_user_version: r.get(8)?,
                    })
                },
            )
            .optional()?;
        Ok(row)
    };
    match read() {
        Ok(Some(m)) => Ok(m),
        Ok(None) => Err(not_a_segment(path, "no manifest row")),
        Err(_) => Err(not_a_segment(path, "not an archive segment")),
    }
}

/// The revision ids a segment holds, in `(doc_id, seq)` order.
fn segment_revision_ids(path: &Path) -> Result<Vec<String>> {
    let db = open_read_only(path).map_err(|_| not_a_segment(path, "not an archive segment"))?;
    let mut stmt = db
        .prepare("SELECT rev_id FROM revisions ORDER BY doc_id, seq")
        .map_err(|_| not_a_segment(path, "not an archive segment"))?;
    let rows = stmt.query_map([], |r| r.get::<_, String>(0))?;
    Ok(rows.collect::<std::result::Result<Vec<_>, _>>()?)
}

// ---- the registry ------------------------------------------------------------------

/// An `archives` row.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ArchiveRow {
    pub segment_id: String,
    pub repo_id: String,
    pub path: String,
    pub seq_lo: i64,
    pub seq_hi: i64,
    pub created_at: String,
    pub revision_count: i64,
    pub bytes: i64,
}

fn archive_row(conn: &Connection, segment_id: &str) -> Result<Option<ArchiveRow>> {
    Ok(conn
        .query_row(
            "SELECT segment_id, repo_id, path, seq_lo, seq_hi, created_at, revision_count, bytes FROM archives WHERE segment_id = ?1",
            params![segment_id],
            |r| {
                Ok(ArchiveRow {
                    segment_id: r.get(0)?,
                    repo_id: r.get(1)?,
                    path: r.get(2)?,
                    seq_lo: r.get(3)?,
                    seq_hi: r.get(4)?,
                    created_at: r.get(5)?,
                    revision_count: r.get(6)?,
                    bytes: r.get(7)?,
                })
            },
        )
        .optional()?)
}

fn record_archive(conn: &Connection, row: &ArchiveRow) -> Result<()> {
    conn.execute(
        "INSERT OR REPLACE INTO archives (segment_id, repo_id, path, seq_lo, seq_hi, created_at, revision_count, bytes) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)",
        params![
            row.segment_id,
            row.repo_id,
            row.path,
            row.seq_lo,
            row.seq_hi,
            row.created_at,
            row.revision_count,
            row.bytes
        ],
    )?;
    Ok(())
}

/// A file's size on disk; 0 when it cannot be read.
#[must_use]
pub fn file_bytes(path: &Path) -> i64 {
    std::fs::metadata(path).map_or(0, |m| i64::try_from(m.len()).unwrap_or(i64::MAX))
}

/// One segment `attach` recorded.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AttachedSegment {
    pub segment_id: String,
    pub path: String,
    pub seq_lo: i64,
    pub seq_hi: i64,
    pub revision_count: i64,
    pub bytes: i64,
}

/// `attach`'s result.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AttachResult {
    pub attached: Vec<AttachedSegment>,
    /// Files in a directory that are not this repository's segments.
    pub skipped: usize,
}

/// `omg history attach <file|dir>` (§7.1 "Attach / detach"): record a segment
/// (idempotent — a known segment's path is re-recorded) or every
/// `.omgarchive.db` of a directory whose manifest `repo_id` is this
/// repository's. A single file of another repository or a non-segment is
/// [`Error::NotASegment`]; in a directory they are skipped.
pub fn attach_archive(conn: &Connection, repo_id: &str, target: &Path) -> Result<AttachResult> {
    let mut attached = Vec::new();
    let mut skipped = 0usize;
    let mut one = |file: &Path, strict: bool| -> Result<()> {
        let manifest = match read_manifest(file) {
            Ok(m) => m,
            Err(e) => {
                if strict {
                    return Err(e);
                }
                skipped += 1;
                return Ok(());
            }
        };
        if manifest.repo_id != repo_id {
            if strict {
                return Err(not_a_segment(
                    file,
                    &format!(
                        "a segment of repository {}, not {repo_id}",
                        manifest.repo_id
                    ),
                ));
            }
            skipped += 1;
            return Ok(());
        }
        let row = ArchiveRow {
            segment_id: manifest.segment_id.clone(),
            repo_id: repo_id.to_owned(),
            path: file.to_string_lossy().into_owned(),
            seq_lo: manifest.seq_lo,
            seq_hi: manifest.seq_hi,
            created_at: manifest.created_at,
            revision_count: manifest.revision_count,
            bytes: file_bytes(file),
        };
        // A re-attached segment may have moved: drop a stale alias so the
        // next read re-attaches.
        detach_alias(conn, &manifest.segment_id)?;
        record_archive(conn, &row)?;
        attached.push(AttachedSegment {
            segment_id: row.segment_id,
            path: row.path,
            seq_lo: row.seq_lo,
            seq_hi: row.seq_hi,
            revision_count: row.revision_count,
            bytes: row.bytes,
        });
        Ok(())
    };
    if target.is_dir() {
        let mut names: Vec<String> = std::fs::read_dir(target)
            .map_err(|e| Error::Other(format!("cannot read {}: {e}", target.display())))?
            .filter_map(|e| e.ok().map(|e| e.file_name().to_string_lossy().into_owned()))
            .filter(|n| n.ends_with(ARCHIVE_SUFFIX))
            .collect();
        names.sort();
        for name in names {
            one(&target.join(name), false)?;
        }
    } else {
        one(target, true)?;
    }
    Ok(AttachResult { attached, skipped })
}

/// `omg history detach <segment_id|path>`: forget a segment. The revisions it
/// holds keep their `archive` pointer; reading one is `revision_archived`
/// until the segment is attached again. Returns the id, or `None` when
/// nothing matched.
pub fn detach_archive(conn: &Connection, repo_id: &str, r: &str) -> Result<Option<String>> {
    let by_id: Option<String> = conn
        .query_row(
            "SELECT segment_id FROM archives WHERE repo_id = ?1 AND segment_id = ?2",
            params![repo_id, r],
            |row| row.get(0),
        )
        .optional()?;
    let id = match by_id {
        Some(id) => Some(id),
        None => conn
            .query_row(
                "SELECT segment_id FROM archives WHERE repo_id = ?1 AND path = ?2",
                params![repo_id, r],
                |row| row.get::<_, String>(0),
            )
            .optional()?,
    };
    let Some(id) = id else {
        return Ok(None);
    };
    detach_alias(conn, &id)?;
    conn.execute("DELETE FROM archives WHERE segment_id = ?1", params![id])?;
    Ok(Some(id))
}

/// A segment's state (§7.1): recorded and on disk (`attached`), recorded but
/// not on disk (`missing`); or unrecorded under the archives directory —
/// `pending` (complete, its revisions still live: the crash rule finishes it
/// on the next `export --prune`), `detached` (complete and squashed: `attach`
/// brings it back), `stale` (some of its revisions live, some not: it belongs
/// to another store or predates a different squash).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ArchiveState {
    Attached,
    Missing,
    Pending,
    Detached,
    Stale,
}

impl ArchiveState {
    /// The wire spelling.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            ArchiveState::Attached => "attached",
            ArchiveState::Missing => "missing",
            ArchiveState::Pending => "pending",
            ArchiveState::Detached => "detached",
            ArchiveState::Stale => "stale",
        }
    }
}

/// One line of `omg history list`.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ArchiveListing {
    pub segment_id: String,
    pub path: String,
    pub seq_lo: i64,
    pub seq_hi: i64,
    pub revision_count: i64,
    pub bytes: i64,
    pub created_at: String,
    pub state: ArchiveState,
}

impl ArchiveListing {
    /// The `--json` row (`spec/cli` §6 `history list`).
    #[must_use]
    pub fn to_json(&self) -> Json {
        serde_json::json!({
            "segment_id": self.segment_id,
            "path": self.path,
            "seq_lo": self.seq_lo,
            "seq_hi": self.seq_hi,
            "revision_count": self.revision_count,
            "bytes": self.bytes,
            "created_at": self.created_at,
            "state": self.state.as_str(),
        })
    }
}

/// `omg history list`: the repository's recorded segments by `seq_lo` then
/// id, followed by the unrecorded complete segments found in `dest`, by file
/// name.
pub fn list_archives(
    conn: &Connection,
    repo_id: &str,
    dest: Option<&Path>,
) -> Result<Vec<ArchiveListing>> {
    let rows: Vec<ArchiveRow> = {
        let mut stmt = conn.prepare(
            "SELECT segment_id, repo_id, path, seq_lo, seq_hi, created_at, revision_count, bytes FROM archives WHERE repo_id = ?1 ORDER BY seq_lo, segment_id",
        )?;
        let it = stmt.query_map(params![repo_id], |r| {
            Ok(ArchiveRow {
                segment_id: r.get(0)?,
                repo_id: r.get(1)?,
                path: r.get(2)?,
                seq_lo: r.get(3)?,
                seq_hi: r.get(4)?,
                created_at: r.get(5)?,
                revision_count: r.get(6)?,
                bytes: r.get(7)?,
            })
        })?;
        it.collect::<std::result::Result<Vec<_>, _>>()?
    };
    let mut out: Vec<ArchiveListing> = rows
        .into_iter()
        .map(|r| {
            let state = if Path::new(&r.path).exists() {
                ArchiveState::Attached
            } else {
                ArchiveState::Missing
            };
            ArchiveListing {
                segment_id: r.segment_id,
                path: r.path,
                seq_lo: r.seq_lo,
                seq_hi: r.seq_hi,
                revision_count: r.revision_count,
                bytes: r.bytes,
                created_at: r.created_at,
                state,
            }
        })
        .collect();
    if let Some(dest) = dest {
        for u in unrecorded_segments(conn, repo_id, dest)? {
            let m = read_manifest(&u.path)?;
            out.push(ArchiveListing {
                segment_id: m.segment_id,
                path: u.path.to_string_lossy().into_owned(),
                seq_lo: m.seq_lo,
                seq_hi: m.seq_hi,
                revision_count: m.revision_count,
                bytes: file_bytes(&u.path),
                created_at: m.created_at,
                state: u.state,
            });
        }
    }
    Ok(out)
}

/// A complete segment under the archives directory that no `archives` row records.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct UnrecordedSegment {
    pub path: PathBuf,
    /// `Pending`, `Detached` or `Stale`.
    pub state: ArchiveState,
}

/// The complete segments of this repository under `dest` that no `archives`
/// row records, by file name, each classified by its revisions: all live →
/// `pending` (the crash rule's input), all archived → `detached`, else `stale`.
pub fn unrecorded_segments(
    conn: &Connection,
    repo_id: &str,
    dest: &Path,
) -> Result<Vec<UnrecordedSegment>> {
    if !dest.exists() {
        return Ok(Vec::new());
    }
    let known: HashSet<String> = {
        let mut stmt = conn.prepare("SELECT segment_id FROM archives")?;
        let it = stmt.query_map([], |r| r.get::<_, String>(0))?;
        it.collect::<std::result::Result<HashSet<_>, _>>()?
    };
    let mut names: Vec<String> = std::fs::read_dir(dest)
        .map_err(|e| Error::Other(format!("cannot read {}: {e}", dest.display())))?
        .filter_map(|e| e.ok().map(|e| e.file_name().to_string_lossy().into_owned()))
        .filter(|name| {
            parse_segment_file_name(name)
                .is_some_and(|p| p.repo_id == repo_id && !known.contains(&p.segment_id))
        })
        .collect();
    names.sort();
    let mut out = Vec::new();
    for name in names {
        let path = dest.join(&name);
        let (mut live, mut archived) = (0usize, 0usize);
        for id in segment_revision_ids(&path)? {
            let row: Option<Option<String>> = conn
                .query_row(
                    "SELECT archive FROM revisions WHERE rev_id = ?1",
                    params![id],
                    |r| r.get(0),
                )
                .optional()?;
            match row {
                Some(None) => live += 1,
                _ => archived += 1,
            }
        }
        let state = if archived == 0 {
            ArchiveState::Pending
        } else if live == 0 {
            ArchiveState::Detached
        } else {
            ArchiveState::Stale
        };
        out.push(UnrecordedSegment { path, state });
    }
    Ok(out)
}

// ---- reading through a segment -----------------------------------------------------------

/// The alias a segment is attached under: `arc_<segment_id>` (every
/// character outside `[A-Za-z0-9_]` becomes `_`).
#[must_use]
pub fn archive_alias(segment_id: &str) -> String {
    let body: String = segment_id
        .chars()
        .map(|c| {
            if c.is_ascii_alphanumeric() || c == '_' {
                c
            } else {
                '_'
            }
        })
        .collect();
    format!("arc_{body}")
}

/// The archive aliases attached to the connection, in attach order.
fn attached_aliases(conn: &Connection) -> Result<Vec<String>> {
    let mut stmt = conn.prepare("PRAGMA database_list")?;
    let rows = stmt.query_map([], |r| r.get::<_, String>(1))?;
    let mut out = Vec::new();
    for name in rows {
        let name = name?;
        if name.starts_with("arc_") {
            out.push(name);
        }
    }
    Ok(out)
}

fn detach_alias(conn: &Connection, segment_id: &str) -> Result<()> {
    let alias = archive_alias(segment_id);
    if attached_aliases(conn)?.contains(&alias) {
        // Already gone with the connection, or inside a transaction: either
        // way the next read decides afresh.
        let _ = conn.execute_batch(&format!("DETACH DATABASE {alias}"));
    }
    Ok(())
}

/// The table prefix a revision's bodies are read under: `""` for a live
/// revision, `"<alias>."` for an archived one whose segment is attached on
/// demand (and verified to be the segment it claims). [`Error::RevisionArchived`]
/// when the segment is not recorded or not on disk.
pub fn archive_prefix(conn: &Connection, rev_id: &str, archive: Option<&str>) -> Result<String> {
    let Some(archive) = archive else {
        return Ok(String::new());
    };
    let alias = archive_alias(archive);
    let attached = attached_aliases(conn)?;
    if attached.contains(&alias) {
        return Ok(format!("{alias}."));
    }
    let Some(row) = archive_row(conn, archive)? else {
        return Err(Error::RevisionArchived {
            rev: rev_id.to_owned(),
            archive: archive.to_owned(),
            path: None,
        });
    };
    if !Path::new(&row.path).exists() {
        return Err(Error::RevisionArchived {
            rev: rev_id.to_owned(),
            archive: archive.to_owned(),
            path: Some(row.path),
        });
    }
    if attached.len() >= MAX_ATTACHED {
        if let Some(oldest) = attached.first() {
            let _ = conn.execute_batch(&format!("DETACH DATABASE {oldest}"));
        }
    }
    conn.execute(&format!("ATTACH DATABASE ?1 AS {alias}"), params![row.path])?;
    let claims: Option<String> = conn
        .query_row(
            &format!("SELECT segment_id FROM {alias}.manifest"),
            [],
            |r| r.get(0),
        )
        .optional()
        .unwrap_or(None);
    if claims.as_deref() != Some(archive) {
        let _ = conn.execute_batch(&format!("DETACH DATABASE {alias}"));
        return Err(Error::RevisionArchived {
            rev: rev_id.to_owned(),
            archive: archive.to_owned(),
            path: Some(row.path),
        });
    }
    Ok(format!("{alias}."))
}

/// A revision's row with its bodies resolved.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RevisionBody {
    pub rev_id: String,
    pub doc_id: String,
    pub path: String,
    pub rendered_hash: Vec<u8>,
    pub root_tree: Vec<u8>,
    pub frontmatter_blob: Option<Vec<u8>>,
    /// `""` or `"<alias>."` — where `tree_nodes` and `blobs` for this revision live.
    pub prefix: String,
    pub archived: bool,
}

/// A revision's row with its bodies resolved: a live revision's from the
/// store, an archived one's from its segment (attached on demand). `None`
/// when `rev_id` is not a revision (of `doc_id`, when given);
/// [`Error::RevisionArchived`] when its segment is unavailable.
pub fn revision_body(
    conn: &Connection,
    rev_id: &str,
    doc_id: Option<&str>,
) -> Result<Option<RevisionBody>> {
    type Row = (
        String,
        String,
        String,
        Vec<u8>,
        Option<Vec<u8>>,
        Option<Vec<u8>>,
        Option<String>,
    );
    let map = |r: &rusqlite::Row<'_>| -> rusqlite::Result<Row> {
        Ok((
            r.get(0)?,
            r.get(1)?,
            r.get(2)?,
            r.get(3)?,
            r.get(4)?,
            r.get(5)?,
            r.get(6)?,
        ))
    };
    const COLS: &str = "rev_id, doc_id, path, rendered_hash, root_tree, frontmatter_blob, archive";
    let row: Option<Row> = match doc_id {
        Some(d) => conn
            .query_row(
                &format!("SELECT {COLS} FROM revisions WHERE rev_id = ?1 AND doc_id = ?2"),
                params![rev_id, d],
                map,
            )
            .optional()?,
        None => conn
            .query_row(
                &format!("SELECT {COLS} FROM revisions WHERE rev_id = ?1"),
                params![rev_id],
                map,
            )
            .optional()?,
    };
    let Some((rev_id, doc_id, path, rendered_hash, root_tree, frontmatter_blob, archive)) = row
    else {
        return Ok(None);
    };
    let Some(archive) = archive else {
        return Ok(Some(RevisionBody {
            rev_id,
            doc_id,
            path,
            rendered_hash,
            root_tree: root_tree.unwrap_or_default(),
            frontmatter_blob,
            prefix: String::new(),
            archived: false,
        }));
    };
    let prefix = archive_prefix(conn, &rev_id, Some(&archive))?;
    let body: Option<(Vec<u8>, Option<Vec<u8>>)> = conn
        .query_row(
            &format!("SELECT root_tree, frontmatter_blob FROM {prefix}revisions WHERE rev_id = ?1"),
            params![rev_id],
            |r| Ok((r.get(0)?, r.get(1)?)),
        )
        .optional()?;
    let Some((root_tree, frontmatter_blob)) = body else {
        return Err(Error::RevisionArchived {
            path: archive_row(conn, &archive)?.map(|r| r.path),
            rev: rev_id,
            archive,
        });
    };
    Ok(Some(RevisionBody {
        rev_id,
        doc_id,
        path,
        rendered_hash,
        root_tree,
        frontmatter_blob,
        prefix,
        archived: true,
    }))
}

// ---- export: write a segment, then squash -------------------------------------------------

/// The segment an export wrote (or finished).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SegmentInfo {
    pub id: String,
    pub path: String,
    pub seq_lo: i64,
    pub seq_hi: i64,
    pub created_at: String,
    pub revision_count: i64,
    /// The file's size on disk.
    pub bytes: i64,
    pub previous_segment: Option<String>,
}

impl SegmentInfo {
    /// The `--json` shape (`spec/cli` §6 `history export`).
    #[must_use]
    pub fn to_json(&self) -> Json {
        serde_json::json!({
            "id": self.id,
            "path": self.path,
            "seq_lo": self.seq_lo,
            "seq_hi": self.seq_hi,
            "created_at": self.created_at,
            "revision_count": self.revision_count,
            "bytes": self.bytes,
            "previous_segment": self.previous_segment,
        })
    }
}

/// What the squash changed (rows).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct SquashResult {
    pub revisions: usize,
    /// Commits every revision of which is archived: their dispositions and block changes moved.
    pub commits: usize,
    pub dispositions: usize,
    /// The `block_changes` rows deleted.
    pub block_changes: usize,
}

/// What the squash's sweep reclaimed.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct SweptBodies {
    pub blobs: usize,
    pub tree_nodes: usize,
}

/// `history export`'s result.
#[derive(Clone, Debug, PartialEq)]
pub struct ExportResult {
    pub plan: RetentionPlan,
    /// `None` when nothing was to archive (no file written).
    pub segment: Option<SegmentInfo>,
    /// An unrecorded complete segment was finished instead of writing a new one (the crash rule).
    pub reused: bool,
    /// `None` without `prune`.
    pub squashed: Option<SquashResult>,
    /// `None` without `prune`.
    pub gc: Option<SweptBodies>,
}

/// What [`export_history`] runs under.
#[derive(Clone, Debug)]
pub struct ExportOptions {
    pub retention: RetentionOptions,
    /// The directory the segment is written to (created when missing).
    pub dest: PathBuf,
    /// Squash the store after the segment is durable.
    pub prune: bool,
}

/// The commits of `rev_ids` every revision of which is in `rev_ids` or already
/// archived — whose change rows move with the bodies. Sorted by id.
fn wholly_archived_commits(conn: &Connection, rev_ids: &HashSet<String>) -> Result<Vec<String>> {
    let mut commits: BTreeSet<String> = BTreeSet::new();
    for id in rev_ids {
        let c: Option<String> = conn
            .query_row(
                "SELECT commit_id FROM revisions WHERE rev_id = ?1",
                params![id],
                |r| r.get(0),
            )
            .optional()?;
        if let Some(c) = c {
            commits.insert(c);
        }
    }
    let mut out = Vec::new();
    let mut stmt = conn.prepare("SELECT rev_id, archive FROM revisions WHERE commit_id = ?1")?;
    for c in commits {
        let revs = stmt
            .query_map(params![c], |r| {
                Ok((r.get::<_, String>(0)?, r.get::<_, Option<String>>(1)?))
            })?
            .collect::<std::result::Result<Vec<_>, _>>()?;
        if revs
            .iter()
            .all(|(id, archive)| archive.is_some() || rev_ids.contains(id))
        {
            out.push(c);
        }
    }
    Ok(out)
}

fn last_segment(conn: &Connection, repo_id: &str) -> Result<Option<String>> {
    let settings = repo_settings(conn, repo_id)?;
    Ok(settings
        .get(RETENTION_SETTINGS_KEY)
        .and_then(Json::as_object)
        .and_then(|h| h.get(LAST_SEGMENT_SETTINGS_KEY))
        .and_then(Json::as_str)
        .map(str::to_owned))
}

fn set_last_segment(conn: &Connection, repo_id: &str, segment_id: &str) -> Result<()> {
    let mut settings = repo_settings(conn, repo_id)?;
    let mut history: Map<String, Json> = match settings.get(RETENTION_SETTINGS_KEY) {
        Some(Json::Object(h)) => h.clone(),
        _ => Map::new(),
    };
    history.insert(
        LAST_SEGMENT_SETTINGS_KEY.to_owned(),
        Json::String(segment_id.to_owned()),
    );
    settings.insert(RETENTION_SETTINGS_KEY.to_owned(), Json::Object(history));
    conn.execute(
        "UPDATE repos SET settings = ?1 WHERE repo_id = ?2",
        params![Json::Object(settings).to_string(), repo_id],
    )?;
    Ok(())
}

type RevisionRow = (
    String,
    String,
    i64,
    Vec<u8>,
    Option<Vec<u8>>,
    Vec<u8>,
    String,
    String,
);

/// Phase 1: write the segment for `rev_ids` to `dest`, durably — into a
/// `.partial` file, `synchronous = FULL`, one transaction, closed, made
/// read-only, then renamed to its final name, so a complete segment is
/// exactly a file with the final name.
fn write_segment(
    conn: &Connection,
    minter: &mut Mint<'_>,
    repo_id: &str,
    rev_ids: &[String],
    plan: &RetentionPlan,
    dest: &Path,
) -> Result<SegmentInfo> {
    let mut rev_stmt = conn.prepare(
        "SELECT rev_id, doc_id, seq, root_tree, frontmatter_blob, rendered_hash, path, commit_id FROM revisions WHERE rev_id = ?1",
    )?;
    let mut revisions: Vec<RevisionRow> = Vec::with_capacity(rev_ids.len());
    for id in rev_ids {
        let row: Option<RevisionRow> = rev_stmt
            .query_row(params![id], |r| {
                Ok((
                    r.get(0)?,
                    r.get(1)?,
                    r.get(2)?,
                    r.get::<_, Option<Vec<u8>>>(3)?.unwrap_or_default(),
                    r.get(4)?,
                    r.get(5)?,
                    r.get(6)?,
                    r.get(7)?,
                ))
            })
            .optional()?;
        if let Some(row) = row {
            revisions.push(row);
        }
    }
    let mut commit_ids: Vec<String> = Vec::new();
    for r in &revisions {
        if !commit_ids.contains(&r.7) {
            commit_ids.push(r.7.clone());
        }
    }
    type CommitRow = (
        String,
        String,
        i64,
        String,
        String,
        Option<String>,
        Option<String>,
        Option<String>,
        Option<String>,
    );
    let mut commits: Vec<CommitRow> = Vec::with_capacity(commit_ids.len());
    {
        let mut stmt = conn.prepare(
            "SELECT commit_id, repo_id, seq, ts, origin, actor, reason, checkpoint_id, ops FROM commits WHERE commit_id = ?1",
        )?;
        for c in &commit_ids {
            let row: Option<CommitRow> = stmt
                .query_row(params![c], |r| {
                    Ok((
                        r.get(0)?,
                        r.get(1)?,
                        r.get(2)?,
                        r.get(3)?,
                        r.get(4)?,
                        r.get(5)?,
                        r.get(6)?,
                        r.get(7)?,
                        r.get(8)?,
                    ))
                })
                .optional()?;
            if let Some(row) = row {
                commits.push(row);
            }
        }
    }
    commits.sort_by_key(|c| c.2);
    let seq_lo = commits.first().map_or(0, |c| c.2);
    let seq_hi = commits.last().map_or(0, |c| c.2);
    let segment_id = minter.mint("s")?;
    let created_at = plan.now.clone();
    let previous = last_segment(conn, repo_id)?;

    let mut marker = ReachMarker::new(conn);
    let mut trees = HashSet::new();
    let mut blobs = HashSet::new();
    let bodies: Vec<RevBodies> = revisions
        .iter()
        .map(|r| (Some(r.3.clone()), r.4.clone()))
        .collect();
    marker.mark(&bodies, &mut trees, &mut blobs)?;
    let mut trees: Vec<String> = trees.into_iter().collect();
    trees.sort();
    let mut blobs: Vec<String> = blobs.into_iter().collect();
    blobs.sort();

    let wholly = wholly_archived_commits(conn, &rev_ids.iter().cloned().collect())?;

    std::fs::create_dir_all(dest)
        .map_err(|e| Error::Other(format!("cannot create {}: {e}", dest.display())))?;
    for entry in std::fs::read_dir(dest)
        .map_err(|e| Error::Other(format!("cannot read {}: {e}", dest.display())))?
        .flatten()
    {
        let name = entry.file_name().to_string_lossy().into_owned();
        if name.ends_with(&format!("{ARCHIVE_SUFFIX}{PARTIAL_SUFFIX}")) {
            let _ = std::fs::remove_file(entry.path());
        }
    }
    let name = segment_file_name(repo_id, seq_lo, seq_hi, &segment_id);
    let final_path = dest.join(&name);
    let partial_path = dest.join(format!("{name}{PARTIAL_SUFFIX}"));

    {
        let seg = Connection::open(&partial_path)?;
        seg.pragma_update(None, "journal_mode", "DELETE")?;
        seg.pragma_update(None, "synchronous", "FULL")?;
        seg.execute_batch(ARCHIVE_SQL)?;
        let tx = seg.unchecked_transaction()?;
        tx.execute(
            "INSERT INTO manifest (segment_id, repo_id, created_at, policy, seq_lo, seq_hi, revision_count, previous_segment, schema_user_version) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)",
            params![
                segment_id,
                repo_id,
                created_at,
                plan.policy.to_json().to_string(),
                seq_lo,
                seq_hi,
                i64::try_from(revisions.len()).unwrap_or(i64::MAX),
                previous,
                SCHEMA_VERSION
            ],
        )?;
        {
            let mut ins = tx.prepare(
                "INSERT INTO revisions (rev_id, doc_id, seq, root_tree, frontmatter_blob, rendered_hash, path, commit_id) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)",
            )?;
            for r in &revisions {
                ins.execute(params![r.0, r.1, r.2, r.3, r.4, r.5, r.6, r.7])?;
            }
            let mut ins = tx.prepare(
                "INSERT INTO commits (commit_id, repo_id, seq, ts, origin, actor, reason, checkpoint_id, ops) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)",
            )?;
            for c in &commits {
                ins.execute(params![c.0, c.1, c.2, c.3, c.4, c.5, c.6, c.7, c.8])?;
            }
            let mut tree_row =
                conn.prepare("SELECT hash, entries FROM tree_nodes WHERE hash = ?1")?;
            let mut ins = tx.prepare("INSERT INTO tree_nodes (hash, entries) VALUES (?1, ?2)")?;
            for t in &trees {
                let row: Option<(Vec<u8>, String)> = tree_row
                    .query_row(params![from_hex(t)?], |r| Ok((r.get(0)?, r.get(1)?)))
                    .optional()?;
                if let Some((hash, entries)) = row {
                    ins.execute(params![hash, entries])?;
                }
            }
            let mut blob_row =
                conn.prepare("SELECT hash, bytes, size FROM blobs WHERE hash = ?1")?;
            let mut ins =
                tx.prepare("INSERT INTO blobs (hash, bytes, size) VALUES (?1, ?2, ?3)")?;
            for b in &blobs {
                let row: Option<(Vec<u8>, Vec<u8>, i64)> = blob_row
                    .query_row(params![from_hex(b)?], |r| {
                        Ok((r.get(0)?, r.get(1)?, r.get(2)?))
                    })
                    .optional()?;
                if let Some((hash, bytes, size)) = row {
                    ins.execute(params![hash, bytes, size])?;
                }
            }
            let mut disp_rows = conn.prepare(
                "SELECT commit_id, block_id, kind, confidence, reason, matcher_v, detail FROM dispositions WHERE commit_id = ?1 ORDER BY block_id, kind",
            )?;
            let mut bc_rows = conn.prepare(
                "SELECT block_id, commit_id, kind FROM block_changes WHERE commit_id = ?1 ORDER BY block_id, kind",
            )?;
            let mut ins_disp = tx.prepare(
                "INSERT INTO dispositions (commit_id, block_id, kind, confidence, reason, matcher_v, detail) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
            )?;
            let mut ins_bc = tx.prepare(
                "INSERT INTO block_changes (block_id, commit_id, kind) VALUES (?1, ?2, ?3)",
            )?;
            for c in &wholly {
                type Disp = (
                    String,
                    String,
                    String,
                    Option<f64>,
                    Option<String>,
                    Option<String>,
                    String,
                );
                let disps = disp_rows
                    .query_map(params![c], |r| {
                        Ok::<Disp, _>((
                            r.get(0)?,
                            r.get(1)?,
                            r.get(2)?,
                            r.get(3)?,
                            r.get(4)?,
                            r.get(5)?,
                            r.get(6)?,
                        ))
                    })?
                    .collect::<std::result::Result<Vec<_>, _>>()?;
                for d in disps {
                    ins_disp.execute(params![d.0, d.1, d.2, d.3, d.4, d.5, d.6])?;
                }
                let bcs = bc_rows
                    .query_map(params![c], |r| {
                        Ok((
                            r.get::<_, String>(0)?,
                            r.get::<_, String>(1)?,
                            r.get::<_, String>(2)?,
                        ))
                    })?
                    .collect::<std::result::Result<Vec<_>, _>>()?;
                for b in bcs {
                    ins_bc.execute(params![b.0, b.1, b.2])?;
                }
            }
        }
        tx.commit()?;
        seg.close().map_err(|(_, e)| Error::Sqlite(e))?;
    }
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        // A filesystem without modes: the engine still never writes through an alias.
        let _ = std::fs::set_permissions(&partial_path, std::fs::Permissions::from_mode(0o444));
    }
    std::fs::rename(&partial_path, &final_path).map_err(|e| {
        Error::Other(format!(
            "cannot rename {} to {}: {e}",
            partial_path.display(),
            final_path.display()
        ))
    })?;
    Ok(SegmentInfo {
        id: segment_id,
        path: final_path.to_string_lossy().into_owned(),
        seq_lo,
        seq_hi,
        created_at,
        revision_count: i64::try_from(revisions.len()).unwrap_or(i64::MAX),
        bytes: file_bytes(&final_path),
        previous_segment: previous,
    })
}

/// Phase 2: squash the store for a complete segment, in one transaction — the
/// archived revisions lose their bodies and gain `archive`; the wholly
/// archived commits lose their dispositions and every block_changes row but
/// each block's earliest; the GC sweep reclaims what is reachable from
/// nothing; the segment is recorded in `archives` and as the repo's
/// `history.last_segment`.
fn squash(
    conn: &Connection,
    repo_id: &str,
    segment: &SegmentInfo,
    rev_ids: &[String],
) -> Result<(SquashResult, SweptBodies)> {
    let tx = conn.unchecked_transaction()?;
    let wholly = wholly_archived_commits(&tx, &rev_ids.iter().cloned().collect())?;
    {
        let mut upd = tx.prepare(
            "UPDATE revisions SET root_tree = NULL, frontmatter_blob = NULL, archive = ?1 WHERE rev_id = ?2",
        )?;
        for id in rev_ids {
            upd.execute(params![segment.id, id])?;
        }
    }
    let mut dispositions = 0usize;
    let mut block_changes = 0usize;
    {
        let mut del_disp = tx.prepare("DELETE FROM dispositions WHERE commit_id = ?1")?;
        // The rows that stay: a block's earliest block_changes row (by commit
        // seq) — the mint oracle (§2.1) reads this table, so an id ever minted
        // stays in use after its row, its pool entry and its history are gone.
        let mut del_bc = tx.prepare(
            "DELETE FROM block_changes WHERE commit_id = ?1 AND EXISTS (
               SELECT 1 FROM block_changes b2 JOIN commits c2 ON c2.commit_id = b2.commit_id JOIN commits c ON c.commit_id = block_changes.commit_id
               WHERE b2.block_id = block_changes.block_id AND c2.seq < c.seq)",
        )?;
        for c in &wholly {
            dispositions += del_disp.execute(params![c])?;
            block_changes += del_bc.execute(params![c])?;
        }
    }
    let (blobs, tree_nodes) = sweep_unreachable(&tx)?;
    record_archive(
        &tx,
        &ArchiveRow {
            segment_id: segment.id.clone(),
            repo_id: repo_id.to_owned(),
            path: segment.path.clone(),
            seq_lo: segment.seq_lo,
            seq_hi: segment.seq_hi,
            created_at: segment.created_at.clone(),
            revision_count: segment.revision_count,
            bytes: segment.bytes,
        },
    )?;
    set_last_segment(&tx, repo_id, &segment.id)?;
    tx.commit()?;
    Ok((
        SquashResult {
            revisions: rev_ids.len(),
            commits: wholly.len(),
            dispositions,
            block_changes,
        },
        SweptBodies { blobs, tree_nodes },
    ))
}

/// `omg history export [--prune]` (§7.1): evaluate the policy, write ONE new
/// segment for the revisions to archive (or finish an unrecorded one — the
/// crash rule), and with `prune` squash the store. Nothing to archive → no
/// file, `segment: None`.
pub fn export_history(
    conn: &Connection,
    minter: &mut Mint<'_>,
    repo_id: &str,
    opts: &ExportOptions,
) -> Result<ExportResult> {
    let plan = evaluate_retention(conn, repo_id, &opts.retention)?;
    let dest = opts.dest.as_path();

    // The crash rule: a complete segment nobody recorded, whose revisions are
    // still live, is finished first; a detached one is left alone (`attach`
    // brings it back); a stale one is refused rather than guessed about.
    let unrecorded = unrecorded_segments(conn, repo_id, dest)?;
    if let Some(stale) = unrecorded.iter().find(|u| u.state == ArchiveState::Stale) {
        let mut offending = "?".to_owned();
        for id in segment_revision_ids(&stale.path)? {
            let row: Option<Option<String>> = conn
                .query_row(
                    "SELECT archive FROM revisions WHERE rev_id = ?1",
                    params![id],
                    |r| r.get(0),
                )
                .optional()?;
            if !matches!(row, Some(None)) {
                offending = id;
                break;
            }
        }
        return Err(Error::StaleSegment {
            path: stale.path.to_string_lossy().into_owned(),
            rev: offending,
        });
    }
    if let Some(pending) = unrecorded.iter().find(|u| u.state == ArchiveState::Pending) {
        let manifest = read_manifest(&pending.path)?;
        let rev_ids = segment_revision_ids(&pending.path)?;
        let segment = SegmentInfo {
            id: manifest.segment_id,
            path: pending.path.to_string_lossy().into_owned(),
            seq_lo: manifest.seq_lo,
            seq_hi: manifest.seq_hi,
            created_at: manifest.created_at,
            revision_count: manifest.revision_count,
            bytes: file_bytes(&pending.path),
            previous_segment: manifest.previous_segment,
        };
        if !opts.prune {
            return Ok(ExportResult {
                plan,
                segment: Some(segment),
                reused: true,
                squashed: None,
                gc: None,
            });
        }
        let (squashed, gc) = squash(conn, repo_id, &segment, &rev_ids)?;
        return Ok(ExportResult {
            plan,
            segment: Some(segment),
            reused: true,
            squashed: Some(squashed),
            gc: Some(gc),
        });
    }

    let rev_ids = plan.archived_revisions();
    if rev_ids.is_empty() {
        return Ok(ExportResult {
            plan,
            segment: None,
            reused: false,
            squashed: None,
            gc: None,
        });
    }
    let segment = write_segment(conn, minter, repo_id, &rev_ids, &plan, dest)?;
    if !opts.prune {
        return Ok(ExportResult {
            plan,
            segment: Some(segment),
            reused: false,
            squashed: None,
            gc: None,
        });
    }
    let (squashed, gc) = squash(conn, repo_id, &segment, &rev_ids)?;
    Ok(ExportResult {
        plan,
        segment: Some(segment),
        reused: false,
        squashed: Some(squashed),
        gc: Some(gc),
    })
}

/// The archived-history figures the size report carries (§7.2, 15.0):
/// `(revisions with archive IS NOT NULL, archives rows, the recorded files'
/// bytes on disk)`.
pub fn archived_summary(conn: &Connection) -> Result<(u64, u64, u64)> {
    let revisions: i64 = conn.query_row(
        "SELECT count(*) FROM revisions WHERE archive IS NOT NULL",
        [],
        |r| r.get(0),
    )?;
    let mut stmt = conn.prepare("SELECT path FROM archives")?;
    let paths = stmt
        .query_map([], |r| r.get::<_, String>(0))?
        .collect::<std::result::Result<Vec<_>, _>>()?;
    let bytes: u64 = paths
        .iter()
        .map(|p| u64::try_from(file_bytes(Path::new(p))).unwrap_or(0))
        .sum();
    Ok((
        u64::try_from(revisions).unwrap_or(0),
        u64::try_from(paths.len()).unwrap_or(0),
        bytes,
    ))
}

/// The file name of a segment path, for messages and fixtures.
#[must_use]
pub fn segment_basename(path: &str) -> String {
    Path::new(path)
        .file_name()
        .map_or_else(|| path.to_owned(), |n| n.to_string_lossy().into_owned())
}

/// `hex` re-exported for callers assembling tree hashes (the fixture runners).
#[must_use]
pub fn hash_hex(h: &[u8]) -> String {
    hex(h)
}

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

    #[test]
    fn segment_names_round_trip() {
        let name = segment_file_name("rp_0", 2, 21, "s_0");
        assert_eq!(name, "rp_0-2-21-s_0.omgarchive.db");
        assert_eq!(
            parse_segment_file_name(&name),
            Some(SegmentName {
                repo_id: "rp_0".into(),
                seq_lo: 2,
                seq_hi: 21,
                segment_id: "s_0".into()
            })
        );
        assert_eq!(
            parse_segment_file_name("rp_k7z2p9q-1-1-s_abc1234.omgarchive.db").map(|p| p.segment_id),
            Some("s_abc1234".to_owned())
        );
        assert!(parse_segment_file_name("rp_0-2-21-s_0.omgarchive.db.partial").is_none());
        assert!(parse_segment_file_name("notes.txt").is_none());
        assert!(parse_segment_file_name("rp_0-x-21-s_0.omgarchive.db").is_none());
        assert!(parse_segment_file_name("RP_0-2-21-s_0.omgarchive.db").is_none());
        assert_eq!(archive_alias("s_0"), "arc_s_0");
        assert_eq!(archive_alias("s-x.y"), "arc_s_x_y");
        assert_eq!(
            segment_basename("/a/b/rp_0-1-1-s_0.omgarchive.db"),
            "rp_0-1-1-s_0.omgarchive.db"
        );
    }
}