briefcase-core 5.0.0

Compliance monitoring, auditing, and evaluating third-party artificial intelligence systems and models.
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
//! Tamper-evident mode for [`SqliteBackend`]: an append-only hash chain with
//! signed Merkle checkpoints.
//!
//! Opt in with [`SqliteBackend::with_chain`]. Every saved decision (or any
//! JSON record passed to [`SqliteBackend::append_record`]) becomes one row of
//! `chain_entries`, written in the same `BEGIN IMMEDIATE` transaction as the
//! snapshot itself:
//!
//! ```text
//! record_hash = sha256(jcs(record))
//! entry_hash  = sha256(jcs({"v":1, "hash_scheme":"jcs-1", "seq":n,
//!                           "record_id":id, "kind":k, "prev_hash":h(n-1),
//!                           "record_hash":r, "recorded_at":t}))
//! ```
//!
//! `jcs` is RFC 8785 ([`crate::integrity::canonical_json_v2`]), so the
//! Python and TypeScript verifiers recompute the same bytes. `prev_hash` of
//! seq 1 is 64 zeros. The immediate transaction takes SQLite's write lock
//! before the tail is read, so writers in different processes queue instead
//! of forking the chain; a UNIQUE index on `prev_hash` backs that up.
//!
//! Every `checkpoint_every` entries (and on demand, [`SqliteBackend::checkpoint`])
//! a checkpoint row records the head, a Merkle root over the entries since
//! the previous checkpoint (see [`crate::integrity::merkle`]), the previous
//! checkpoint's hash and, when a key is configured, an Ed25519 signature over
//! the checkpoint hash. One entry's inclusion can then be proven with a
//! log-sized audit path instead of the whole log.
//!
//! What [`SqliteBackend::verify_chain`] can and cannot see: edits, deletions,
//! reordering and insertions anywhere in the chain, and tampering with
//! checkpoints, are detected. Removing the newest entries (truncation) after
//! the last checkpoint, or a writer holding the signing key rewriting the
//! whole log, is only detectable against a checkpoint kept outside the
//! database: pass it as `against_checkpoint`.

use super::sqlite::{format_created_at, insert_error, SqliteBackend, INSERT_ONLY_SQL};
use super::StorageError;
use crate::integrity::merkle::{self, Hash};
use crate::integrity::signing::{self, SigningKey};
use crate::integrity::{
    canonical_json_v2, parse_canonical_text, sha256_hex, HashScheme, HASH_SCHEME_JCS1,
};
use crate::models::{Snapshot, SnapshotType};
use rusqlite::{params, Connection, OptionalExtension, TransactionBehavior};
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use std::collections::BTreeMap;
use std::sync::Arc;

/// `prev_hash` of the first entry and `prev_checkpoint_hash` of the first
/// checkpoint.
pub const GENESIS_HASH: &str = "0000000000000000000000000000000000000000000000000000000000000000";

/// Default checkpoint cadence: one signed checkpoint per 100 entries.
pub const DEFAULT_CHECKPOINT_EVERY: u64 = 100;

/// Options for [`SqliteBackend::with_chain`].
#[derive(Debug, Clone)]
pub struct ChainConfig {
    /// Write a checkpoint automatically after this many new entries; 0 means
    /// only when [`SqliteBackend::checkpoint`] is called.
    pub checkpoint_every: u64,
    /// Key that signs checkpoints. Without one, checkpoints carry hashes
    /// only, which proves internal consistency but not who wrote them.
    pub signing_key: Option<SigningKey>,
}

impl Default for ChainConfig {
    fn default() -> Self {
        Self {
            checkpoint_every: DEFAULT_CHECKPOINT_EVERY,
            signing_key: None,
        }
    }
}

impl ChainConfig {
    pub fn new() -> Self {
        Self::default()
    }
    pub fn with_checkpoint_every(mut self, every: u64) -> Self {
        self.checkpoint_every = every;
        self
    }
    pub fn with_signing_key(mut self, key: SigningKey) -> Self {
        self.signing_key = Some(key);
        self
    }
}

/// One link of the chain (the record itself is stored next to it).
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ChainEntry {
    pub seq: u64,
    pub record_id: String,
    pub kind: String,
    pub hash_scheme: String,
    pub prev_hash: String,
    pub record_hash: String,
    pub entry_hash: String,
    pub recorded_at: String,
}

/// A checkpoint over entries `from_seq..=seq`.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Checkpoint {
    pub checkpoint_no: u64,
    pub from_seq: u64,
    pub seq: u64,
    pub head_hash: String,
    pub merkle_root: String,
    pub prev_checkpoint_hash: String,
    pub created_at: String,
    pub hash_scheme: String,
    pub key_id: Option<String>,
    /// JWK `x` (base64url) of the signing key.
    pub public_key: Option<String>,
    /// base64url Ed25519 signature over the raw bytes of `checkpoint_hash`.
    pub signature: Option<String>,
    pub checkpoint_hash: String,
}

impl Checkpoint {
    /// The signed fields. `public_key` and `signature` are not part of it:
    /// `key_id` (the key's thumbprint) binds the key instead.
    pub fn body(&self) -> Value {
        json!({
            "v": 1,
            "hash_scheme": self.hash_scheme,
            "checkpoint_no": self.checkpoint_no,
            "from_seq": self.from_seq,
            "seq": self.seq,
            "head_hash": self.head_hash,
            "merkle_root": self.merkle_root,
            "prev_checkpoint_hash": self.prev_checkpoint_hash,
            "created_at": self.created_at,
            "key_id": self.key_id,
        })
    }

    /// Recompute `checkpoint_hash` from the body.
    pub fn compute_hash(&self) -> Result<String, StorageError> {
        let text = canonical_json_v2(&self.body())
            .map_err(|e| StorageError::SerializationError(e.to_string()))?;
        Ok(sha256_hex(text.as_bytes()))
    }
}

/// Proof that entry `seq` is leaf `leaf_index` of checkpoint `checkpoint_no`.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct InclusionProof {
    pub seq: u64,
    pub entry_hash: String,
    pub checkpoint_no: u64,
    pub leaf_index: u64,
    pub tree_size: u64,
    pub audit_path: Vec<String>,
    pub merkle_root: String,
}

/// One finding of [`SqliteBackend::verify_chain`].
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Problem {
    /// Stable machine-readable kind, e.g. `broken_link`, `modified_entry`.
    pub kind: String,
    pub seq: Option<u64>,
    pub detail: String,
}

/// Result of [`SqliteBackend::verify_chain`].
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
pub struct VerifyReport {
    pub intact: bool,
    pub entries_checked: u64,
    pub first_seq: Option<u64>,
    pub last_seq: Option<u64>,
    pub head_hash: Option<String>,
    pub checkpoints_checked: u64,
    pub signed_checkpoints: u64,
    pub signatures_verified: u64,
    pub last_checkpoint_seq: Option<u64>,
    pub entries_after_last_checkpoint: u64,
    pub key_ids: Vec<String>,
    pub hash_schemes: Vec<String>,
    pub first_broken_seq: Option<u64>,
    pub missing_ranges: Vec<(u64, u64)>,
    pub modified_seqs: Vec<u64>,
    pub anchored: bool,
    pub problems: Vec<Problem>,
    pub notes: Vec<String>,
}

impl VerifyReport {
    fn problem(&mut self, kind: &str, seq: Option<u64>, detail: String) {
        if let Some(s) = seq {
            if matches!(
                kind,
                "broken_link"
                    | "modified_entry"
                    | "missing_entries"
                    | "snapshot_modified"
                    | "snapshot_missing"
                    | "unknown_hash_scheme"
            ) {
                self.first_broken_seq = Some(self.first_broken_seq.map_or(s, |f| f.min(s)));
            }
            if matches!(kind, "modified_entry" | "snapshot_modified")
                && !self.modified_seqs.contains(&s)
            {
                self.modified_seqs.push(s);
            }
        }
        self.problems.push(Problem {
            kind: kind.to_string(),
            seq,
            detail,
        });
    }
}

/// Options for [`SqliteBackend::verify_chain`].
#[derive(Debug, Clone, Default)]
pub struct VerifyOptions {
    /// A checkpoint held outside the database (printed, emailed, stored in a
    /// WORM bucket). The log must still contain everything it covers,
    /// unchanged; this is the only way to detect truncation after the last
    /// checkpoint in the database.
    pub against_checkpoint: Option<Checkpoint>,
    /// Require every checkpoint to be signed by the key with this thumbprint.
    /// Without it a verifier trusts whatever key the database names.
    pub expected_key_id: Option<String>,
}

fn now_rfc3339() -> String {
    chrono::Utc::now()
        .format("%Y-%m-%dT%H:%M:%S%.6fZ")
        .to_string()
}

fn conn_err(context: &str) -> impl Fn(rusqlite::Error) -> StorageError + '_ {
    move |e| StorageError::ConnectionError(format!("{}: {}", context, e))
}

fn ser_err(e: impl std::fmt::Display) -> StorageError {
    StorageError::SerializationError(e.to_string())
}

/// `entry_hash` of a chain entry (see the module docs).
#[allow(clippy::too_many_arguments)]
pub fn compute_entry_hash(
    hash_scheme: &str,
    seq: u64,
    record_id: &str,
    kind: &str,
    prev_hash: &str,
    record_hash: &str,
    recorded_at: &str,
) -> Result<String, StorageError> {
    if HashScheme::parse(hash_scheme) != Some(HashScheme::Jcs1) {
        return Err(StorageError::SerializationError(format!(
            "chain entries use hash scheme {}; {:?} is not supported here",
            HASH_SCHEME_JCS1, hash_scheme
        )));
    }
    let body = json!({
        "v": 1,
        "hash_scheme": hash_scheme,
        "seq": seq,
        "record_id": record_id,
        "kind": kind,
        "prev_hash": prev_hash,
        "record_hash": record_hash,
        "recorded_at": recorded_at,
    });
    Ok(sha256_hex(
        canonical_json_v2(&body).map_err(ser_err)?.as_bytes(),
    ))
}

fn read_entry(row: &rusqlite::Row<'_>) -> rusqlite::Result<(ChainEntry, String)> {
    Ok((
        ChainEntry {
            seq: row.get::<_, i64>(0)? as u64,
            record_id: row.get(1)?,
            kind: row.get(2)?,
            hash_scheme: row.get(3)?,
            prev_hash: row.get(5)?,
            record_hash: row.get(6)?,
            entry_hash: row.get(7)?,
            recorded_at: row.get(8)?,
        },
        row.get(4)?,
    ))
}

const ENTRY_COLUMNS: &str =
    "seq, record_id, kind, hash_scheme, record_json, prev_hash, record_hash, entry_hash, recorded_at";
const CHECKPOINT_COLUMNS: &str = "checkpoint_no, from_seq, seq, head_hash, merkle_root, \
     prev_checkpoint_hash, created_at, hash_scheme, key_id, public_key, signature, checkpoint_hash";

fn read_checkpoint(row: &rusqlite::Row<'_>) -> rusqlite::Result<Checkpoint> {
    Ok(Checkpoint {
        checkpoint_no: row.get::<_, i64>(0)? as u64,
        from_seq: row.get::<_, i64>(1)? as u64,
        seq: row.get::<_, i64>(2)? as u64,
        head_hash: row.get(3)?,
        merkle_root: row.get(4)?,
        prev_checkpoint_hash: row.get(5)?,
        created_at: row.get(6)?,
        hash_scheme: row.get(7)?,
        key_id: row.get(8)?,
        public_key: row.get(9)?,
        signature: row.get(10)?,
        checkpoint_hash: row.get(11)?,
    })
}

fn last_checkpoint(conn: &Connection) -> Result<Option<Checkpoint>, StorageError> {
    conn.query_row(
        &format!(
            "SELECT {} FROM chain_checkpoints ORDER BY checkpoint_no DESC LIMIT 1",
            CHECKPOINT_COLUMNS
        ),
        [],
        read_checkpoint,
    )
    .optional()
    .map_err(conn_err("Failed to read the last checkpoint"))
}

fn tail(conn: &Connection) -> Result<(u64, String), StorageError> {
    Ok(conn
        .query_row(
            "SELECT seq, entry_hash FROM chain_entries ORDER BY seq DESC LIMIT 1",
            [],
            |row| Ok((row.get::<_, i64>(0)? as u64, row.get::<_, String>(1)?)),
        )
        .optional()
        .map_err(conn_err("Failed to read the chain tail"))?
        .unwrap_or((0, GENESIS_HASH.to_string())))
}

fn leaves_between(
    conn: &Connection,
    from: u64,
    to: u64,
) -> Result<Vec<(u64, String)>, StorageError> {
    let mut stmt = conn
        .prepare("SELECT seq, entry_hash FROM chain_entries WHERE seq BETWEEN ? AND ? ORDER BY seq")
        .map_err(conn_err("Failed to read entries"))?;
    let rows = stmt
        .query_map(params![from as i64, to as i64], |row| {
            Ok((row.get::<_, i64>(0)? as u64, row.get::<_, String>(1)?))
        })
        .map_err(conn_err("Failed to read entries"))?;
    rows.collect::<Result<Vec<_>, _>>()
        .map_err(conn_err("Failed to read entries"))
}

fn to_leaves(hashes: &[(u64, String)]) -> Option<Vec<Hash>> {
    hashes
        .iter()
        .map(|(_, h)| merkle::hash_from_hex(h))
        .collect()
}

/// Append one entry inside an open transaction.
fn append_in_tx(
    conn: &Connection,
    record_id: &str,
    kind: &str,
    record: &Value,
) -> Result<ChainEntry, StorageError> {
    let record_json = canonical_json_v2(record).map_err(ser_err)?;
    let record_hash = sha256_hex(record_json.as_bytes());
    let (last_seq, prev_hash) = tail(conn)?;
    let seq = last_seq + 1;
    let recorded_at = now_rfc3339();
    let entry_hash = compute_entry_hash(
        HASH_SCHEME_JCS1,
        seq,
        record_id,
        kind,
        &prev_hash,
        &record_hash,
        &recorded_at,
    )?;
    conn.execute(
        &format!(
            "INSERT INTO chain_entries ({}) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
            ENTRY_COLUMNS
        ),
        params![
            seq as i64,
            record_id,
            kind,
            HASH_SCHEME_JCS1,
            record_json,
            prev_hash,
            record_hash,
            entry_hash,
            recorded_at
        ],
    )
    .map_err(|e| match e {
        rusqlite::Error::SqliteFailure(ref code, _)
            if code.extended_code == rusqlite::ffi::SQLITE_CONSTRAINT_UNIQUE =>
        {
            StorageError::AlreadyExists(format!(
                "record {} is already in the chain (chained stores are append-only)",
                record_id
            ))
        }
        other => StorageError::ConnectionError(format!("Failed to append chain entry: {}", other)),
    })?;
    Ok(ChainEntry {
        seq,
        record_id: record_id.to_string(),
        kind: kind.to_string(),
        hash_scheme: HASH_SCHEME_JCS1.to_string(),
        prev_hash,
        record_hash,
        entry_hash,
        recorded_at,
    })
}

/// Write a checkpoint over everything since the last one, inside an open
/// transaction. Returns the last checkpoint unchanged when nothing is new.
fn checkpoint_in_tx(
    conn: &Connection,
    key: Option<&SigningKey>,
) -> Result<Option<Checkpoint>, StorageError> {
    let previous = last_checkpoint(conn)?;
    let from_seq = previous.as_ref().map_or(1, |c| c.seq + 1);
    let (to_seq, head_hash) = tail(conn)?;
    if to_seq < from_seq {
        return Ok(previous);
    }
    let hashes = leaves_between(conn, from_seq, to_seq)?;
    if hashes.len() as u64 != to_seq - from_seq + 1 {
        return Err(StorageError::ConnectionError(format!(
            "cannot checkpoint: entries {}..{} are not contiguous (run verify)",
            from_seq, to_seq
        )));
    }
    let leaves = to_leaves(&hashes)
        .ok_or_else(|| StorageError::SerializationError("malformed entry hash".into()))?;
    let mut checkpoint = Checkpoint {
        checkpoint_no: previous.as_ref().map_or(1, |c| c.checkpoint_no + 1),
        from_seq,
        seq: to_seq,
        head_hash,
        merkle_root: merkle::hash_to_hex(&merkle::merkle_root(&leaves)),
        prev_checkpoint_hash: previous
            .as_ref()
            .map_or(GENESIS_HASH.to_string(), |c| c.checkpoint_hash.clone()),
        created_at: now_rfc3339(),
        hash_scheme: HASH_SCHEME_JCS1.to_string(),
        key_id: key.map(|k| k.key_id()),
        public_key: key.map(|k| k.public_x()),
        signature: None,
        checkpoint_hash: String::new(),
    };
    checkpoint.checkpoint_hash = checkpoint.compute_hash()?;
    if let Some(key) = key {
        checkpoint.signature = Some(
            key.sign_digest(&checkpoint.checkpoint_hash)
                .map_err(|e| StorageError::SerializationError(e.to_string()))?,
        );
    }
    conn.execute(
        &format!(
            "INSERT INTO chain_checkpoints ({}) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
            CHECKPOINT_COLUMNS
        ),
        params![
            checkpoint.checkpoint_no as i64,
            checkpoint.from_seq as i64,
            checkpoint.seq as i64,
            checkpoint.head_hash,
            checkpoint.merkle_root,
            checkpoint.prev_checkpoint_hash,
            checkpoint.created_at,
            checkpoint.hash_scheme,
            checkpoint.key_id,
            checkpoint.public_key,
            checkpoint.signature,
            checkpoint.checkpoint_hash,
        ],
    )
    .map_err(conn_err("Failed to write checkpoint"))?;
    Ok(Some(checkpoint))
}

fn kind_of(snapshot: &Snapshot) -> &'static str {
    match snapshot.snapshot_type {
        SnapshotType::Decision => "decision",
        _ => "snapshot",
    }
}

impl SqliteBackend {
    /// Turn on the tamper-evident mode for this handle (and implies
    /// append-only: overwrites and deletes are refused).
    ///
    /// On a database that already holds snapshots but no chain, those rows
    /// are adopted into the chain first, oldest first; they are provably
    /// unaltered only from that moment on. Plain handles opened without
    /// `with_chain` still work on the same file, but anything they write is
    /// outside the chain and `verify_chain` reports it.
    pub fn with_chain(mut self, config: ChainConfig) -> Result<Self, StorageError> {
        {
            let mut guard = self.lock_conn();
            let tx = guard
                .transaction_with_behavior(TransactionBehavior::Immediate)
                .map_err(conn_err("Failed to begin"))?;
            let started: Option<String> = tx
                .query_row(
                    "SELECT value FROM chain_meta WHERE key = 'created_at'",
                    [],
                    |row| row.get(0),
                )
                .optional()
                .map_err(conn_err("Failed to read chain metadata"))?;
            if started.is_none() {
                let created_at = now_rfc3339();
                let mut stmt = tx
                    .prepare(
                        "SELECT id, data_json FROM snapshots \
                         WHERE id NOT IN (SELECT record_id FROM chain_entries) \
                         ORDER BY created_at, id",
                    )
                    .map_err(conn_err("Failed to read snapshots"))?;
                let existing: Vec<(String, String)> = stmt
                    .query_map([], |row| Ok((row.get(0)?, row.get(1)?)))
                    .map_err(conn_err("Failed to read snapshots"))?
                    .collect::<Result<_, _>>()
                    .map_err(conn_err("Failed to read snapshots"))?;
                drop(stmt);
                for (id, data_json) in &existing {
                    let value: Value = serde_json::from_str(data_json).map_err(ser_err)?;
                    let kind = serde_json::from_value::<Snapshot>(value.clone())
                        .map(|s| kind_of(&s))
                        .unwrap_or("snapshot");
                    append_in_tx(&tx, id, kind, &value)?;
                }
                for (key, value) in [
                    ("created_at", created_at),
                    ("hash_scheme", HASH_SCHEME_JCS1.to_string()),
                    ("adopted_rows", existing.len().to_string()),
                ] {
                    tx.execute(
                        "INSERT OR IGNORE INTO chain_meta (key, value) VALUES (?, ?)",
                        params![key, value],
                    )
                    .map_err(conn_err("Failed to write chain metadata"))?;
                }
            }
            tx.commit().map_err(conn_err("Failed to commit"))?;
        }
        self.append_only = true;
        self.chain = Some(Arc::new(config));
        Ok(self)
    }

    /// Whether this handle writes through the hash chain.
    pub fn is_chained(&self) -> bool {
        self.chain.is_some()
    }

    fn chain_config(&self) -> Result<Arc<ChainConfig>, StorageError> {
        self.chain.clone().ok_or_else(|| {
            StorageError::InvalidQuery(
                "this backend is not chained; open it with with_chain(...)".into(),
            )
        })
    }

    /// Run `f` in one immediate transaction, then the checkpoint cadence.
    fn in_chain_tx<T>(
        &self,
        f: impl FnOnce(&Connection) -> Result<T, StorageError>,
    ) -> Result<T, StorageError> {
        let config = self.chain_config()?;
        let mut guard = self.lock_conn();
        let tx = guard
            .transaction_with_behavior(TransactionBehavior::Immediate)
            .map_err(conn_err("Failed to begin"))?;
        let out = f(&tx)?;
        if config.checkpoint_every > 0 {
            let last = last_checkpoint(&tx)?.map_or(0, |c| c.seq);
            let (head, _) = tail(&tx)?;
            if head - last >= config.checkpoint_every {
                checkpoint_in_tx(&tx, config.signing_key.as_ref())?;
            }
        }
        tx.commit().map_err(conn_err("Failed to commit"))?;
        Ok(out)
    }

    pub(crate) fn save_snapshots_chained(
        &self,
        snapshots: &[Snapshot],
    ) -> Result<Vec<String>, StorageError> {
        self.in_chain_tx(|tx| {
            let mut ids = Vec::with_capacity(snapshots.len());
            for snapshot in snapshots {
                let id = snapshot.metadata.snapshot_id.to_string();
                let data_json = serde_json::to_string(snapshot).map_err(ser_err)?;
                tx.execute(
                    INSERT_ONLY_SQL,
                    params![
                        id,
                        format!("{:?}", snapshot.snapshot_type),
                        data_json,
                        format_created_at(&snapshot.metadata.timestamp),
                        snapshot.metadata.created_by,
                        snapshot.metadata.checksum,
                    ],
                )
                .map_err(|e| insert_error(&id, e))?;
                let value: Value = serde_json::to_value(snapshot).map_err(ser_err)?;
                append_in_tx(tx, &id, kind_of(snapshot), &value)?;
                ids.push(id);
            }
            Ok(ids)
        })
    }

    /// Append any JSON record (for example a Python `@capture` record) under
    /// `record_id`. The record is stored only in the chain, canonicalized.
    pub fn append_record(
        &self,
        record_id: &str,
        record: &Value,
    ) -> Result<ChainEntry, StorageError> {
        self.in_chain_tx(|tx| append_in_tx(tx, record_id, "record", record))
    }

    /// Append several records in one transaction (all or none).
    pub fn append_records(
        &self,
        records: &[(String, Value)],
    ) -> Result<Vec<ChainEntry>, StorageError> {
        self.in_chain_tx(|tx| {
            records
                .iter()
                .map(|(id, record)| append_in_tx(tx, id, "record", record))
                .collect()
        })
    }

    /// Write a checkpoint covering every entry since the previous one now.
    /// Returns the latest checkpoint (unchanged when nothing is new), or
    /// `None` for an empty chain.
    pub fn checkpoint(&self) -> Result<Option<Checkpoint>, StorageError> {
        let config = self.chain_config()?;
        let mut guard = self.lock_conn();
        let tx = guard
            .transaction_with_behavior(TransactionBehavior::Immediate)
            .map_err(conn_err("Failed to begin"))?;
        let out = checkpoint_in_tx(&tx, config.signing_key.as_ref())?;
        tx.commit().map_err(conn_err("Failed to commit"))?;
        Ok(out)
    }

    /// The newest entry, if any.
    pub fn chain_head(&self) -> Result<Option<ChainEntry>, StorageError> {
        let conn = self.lock_conn();
        conn.query_row(
            &format!(
                "SELECT {} FROM chain_entries ORDER BY seq DESC LIMIT 1",
                ENTRY_COLUMNS
            ),
            [],
            read_entry,
        )
        .optional()
        .map(|o| o.map(|(e, _)| e))
        .map_err(conn_err("Failed to read the chain head"))
    }

    /// Entries with `from <= seq <= to`, each with its canonical record text.
    pub fn chain_entries(
        &self,
        from: u64,
        to: u64,
    ) -> Result<Vec<(ChainEntry, String)>, StorageError> {
        let conn = self.lock_conn();
        let mut stmt = conn
            .prepare(&format!(
                "SELECT {} FROM chain_entries WHERE seq BETWEEN ? AND ? ORDER BY seq",
                ENTRY_COLUMNS
            ))
            .map_err(conn_err("Failed to read entries"))?;
        let rows = stmt
            .query_map(
                params![from as i64, to.min(i64::MAX as u64) as i64],
                read_entry,
            )
            .map_err(conn_err("Failed to read entries"))?;
        rows.collect::<Result<Vec<_>, _>>()
            .map_err(conn_err("Failed to read entries"))
    }

    /// Every checkpoint, oldest first.
    pub fn checkpoints(&self) -> Result<Vec<Checkpoint>, StorageError> {
        let conn = self.lock_conn();
        let mut stmt = conn
            .prepare(&format!(
                "SELECT {} FROM chain_checkpoints ORDER BY checkpoint_no",
                CHECKPOINT_COLUMNS
            ))
            .map_err(conn_err("Failed to read checkpoints"))?;
        let rows = stmt
            .query_map([], read_checkpoint)
            .map_err(conn_err("Failed to read checkpoints"))?;
        rows.collect::<Result<Vec<_>, _>>()
            .map_err(conn_err("Failed to read checkpoints"))
    }

    /// Merkle inclusion proof of entry `seq` against the checkpoint that
    /// covers it; `None` while no checkpoint covers it yet.
    pub fn inclusion_proof(&self, seq: u64) -> Result<Option<InclusionProof>, StorageError> {
        let checkpoint = self
            .checkpoints()?
            .into_iter()
            .find(|c| c.from_seq <= seq && seq <= c.seq);
        let Some(checkpoint) = checkpoint else {
            return Ok(None);
        };
        let hashes = {
            let conn = self.lock_conn();
            leaves_between(&conn, checkpoint.from_seq, checkpoint.seq)?
        };
        let leaves = to_leaves(&hashes)
            .ok_or_else(|| StorageError::SerializationError("malformed entry hash".into()))?;
        let index = (seq - checkpoint.from_seq) as usize;
        let path = merkle::inclusion_proof(&leaves, index)
            .ok_or_else(|| StorageError::NotFound(format!("entry {} is missing", seq)))?;
        Ok(Some(InclusionProof {
            seq,
            entry_hash: hashes[index].1.clone(),
            checkpoint_no: checkpoint.checkpoint_no,
            leaf_index: index as u64,
            tree_size: leaves.len() as u64,
            audit_path: path.iter().map(merkle::hash_to_hex).collect(),
            merkle_root: checkpoint.merkle_root,
        }))
    }

    /// Walk the whole chain and its checkpoints and report every problem.
    /// Works on any handle (chained or not), read-only.
    pub fn verify_chain(&self, options: &VerifyOptions) -> Result<VerifyReport, StorageError> {
        let mut report = VerifyReport::default();
        let entries = self.chain_entries(1, u64::MAX)?;
        let checkpoints = self.checkpoints()?;

        // Snapshot rows, to catch edits that bypass the chain table.
        let snapshots: BTreeMap<String, String> = {
            let conn = self.lock_conn();
            let mut stmt = conn
                .prepare("SELECT id, data_json FROM snapshots")
                .map_err(conn_err("Failed to read snapshots"))?;
            let rows = stmt
                .query_map([], |row| Ok((row.get(0)?, row.get(1)?)))
                .map_err(conn_err("Failed to read snapshots"))?;
            rows.collect::<Result<_, _>>()
                .map_err(conn_err("Failed to read snapshots"))?
        };

        let mut expected_seq = 1u64;
        let mut prev_hash = GENESIS_HASH.to_string();
        let mut by_seq: BTreeMap<u64, String> = BTreeMap::new();
        let mut chained_ids = std::collections::BTreeSet::new();
        for (entry, record_json) in &entries {
            report.entries_checked += 1;
            report.first_seq.get_or_insert(entry.seq);
            report.last_seq = Some(entry.seq);
            report.head_hash = Some(entry.entry_hash.clone());
            chained_ids.insert(entry.record_id.clone());
            by_seq.insert(entry.seq, entry.entry_hash.clone());
            if !report.hash_schemes.contains(&entry.hash_scheme) {
                report.hash_schemes.push(entry.hash_scheme.clone());
            }

            if entry.seq > expected_seq {
                report.missing_ranges.push((expected_seq, entry.seq - 1));
                report.problem(
                    "missing_entries",
                    Some(expected_seq),
                    format!(
                        "{} (deleted, or the log was cut)",
                        entries_span(expected_seq, entry.seq - 1, "is missing", "are missing")
                    ),
                );
            } else if entry.prev_hash != prev_hash {
                report.problem(
                    "broken_link",
                    Some(entry.seq),
                    format!(
                        "entry {} does not follow the entry before it (prev_hash {} expected {}): \
                         entries were reordered, inserted or replaced",
                        entry.seq,
                        short(&entry.prev_hash),
                        short(&prev_hash)
                    ),
                );
            }
            expected_seq = entry.seq + 1;
            prev_hash = entry.entry_hash.clone();

            if HashScheme::parse(&entry.hash_scheme) != Some(HashScheme::Jcs1) {
                report.problem(
                    "unknown_hash_scheme",
                    Some(entry.seq),
                    format!(
                        "entry {} names hash scheme {:?}",
                        entry.seq, entry.hash_scheme
                    ),
                );
                continue;
            }
            let record_ok = parse_canonical_text(record_json)
                .ok()
                .and_then(|v| canonical_json_v2(&v).ok())
                .is_some_and(|canonical| &canonical == record_json)
                && sha256_hex(record_json.as_bytes()) == entry.record_hash;
            let recomputed = compute_entry_hash(
                &entry.hash_scheme,
                entry.seq,
                &entry.record_id,
                &entry.kind,
                &entry.prev_hash,
                &entry.record_hash,
                &entry.recorded_at,
            )?;
            if !record_ok || recomputed != entry.entry_hash {
                report.problem(
                    "modified_entry",
                    Some(entry.seq),
                    format!(
                        "entry {} (record {}) was changed after it was written",
                        entry.seq, entry.record_id
                    ),
                );
            }
            if entry.kind == "decision" || entry.kind == "snapshot" {
                match snapshots.get(&entry.record_id) {
                    None => report.problem(
                        "snapshot_missing",
                        Some(entry.seq),
                        format!(
                            "snapshot {} (entry {}) was deleted",
                            entry.record_id, entry.seq
                        ),
                    ),
                    Some(data_json) => {
                        let same = parse_canonical_text(data_json)
                            .ok()
                            .and_then(|v| canonical_json_v2(&v).ok())
                            .is_some_and(|c| &c == record_json);
                        if !same {
                            report.problem(
                                "snapshot_modified",
                                Some(entry.seq),
                                format!(
                                    "snapshot {} (entry {}) differs from what was recorded",
                                    entry.record_id, entry.seq
                                ),
                            );
                        }
                    }
                }
            }
        }
        for id in snapshots.keys() {
            if !chained_ids.contains(id) {
                report.problem(
                    "unchained_record",
                    None,
                    format!(
                        "snapshot {} is not in the chain (written by a handle without the chain, or inserted directly)",
                        id
                    ),
                );
            }
        }

        // Checkpoints.
        let mut prev_cp_hash = GENESIS_HASH.to_string();
        let mut prev_cp_seq = 0u64;
        for (i, cp) in checkpoints.iter().enumerate() {
            report.checkpoints_checked += 1;
            report.last_checkpoint_seq = Some(cp.seq);
            if cp.checkpoint_no != i as u64 + 1 {
                report.problem(
                    "checkpoint_chain_broken",
                    None,
                    format!(
                        "checkpoint {} is out of sequence (expected {})",
                        cp.checkpoint_no,
                        i + 1
                    ),
                );
            }
            if cp.prev_checkpoint_hash != prev_cp_hash || cp.from_seq != prev_cp_seq + 1 {
                report.problem(
                    "checkpoint_chain_broken",
                    None,
                    format!(
                        "checkpoint {} does not follow the previous checkpoint (a checkpoint was removed or replaced)",
                        cp.checkpoint_no
                    ),
                );
            }
            prev_cp_hash = cp.checkpoint_hash.clone();
            prev_cp_seq = cp.seq;
            self.check_checkpoint(cp, &by_seq, options, &mut report, "checkpoint")?;
        }
        let head = report.last_seq.unwrap_or(0);
        if let Some(last_cp) = report.last_checkpoint_seq {
            if last_cp > head {
                report.problem(
                    "truncated",
                    Some(head + 1),
                    format!(
                        "the log ends at entry {} but a checkpoint covers up to entry {}: {}",
                        head,
                        last_cp,
                        entries_span(head + 1, last_cp, "was removed", "were removed")
                    ),
                );
            }
        }
        report.entries_after_last_checkpoint =
            head.saturating_sub(report.last_checkpoint_seq.unwrap_or(0));

        if let Some(anchor) = &options.against_checkpoint {
            report.anchored = true;
            if anchor.compute_hash()? != anchor.checkpoint_hash {
                report.problem(
                    "anchor_invalid",
                    None,
                    "the checkpoint given to verify against does not match its own hash".into(),
                );
            } else {
                if head < anchor.seq {
                    report.problem(
                        "truncated",
                        Some(head + 1),
                        format!(
                            "the log ends at entry {} but the anchored checkpoint {} covers up to entry {}: {}",
                            head,
                            anchor.checkpoint_no,
                            anchor.seq,
                            entries_span(head + 1, anchor.seq, "was removed", "were removed")
                        ),
                    );
                }
                if let Some(stored) = checkpoints
                    .iter()
                    .find(|c| c.checkpoint_no == anchor.checkpoint_no)
                {
                    if stored.checkpoint_hash != anchor.checkpoint_hash {
                        report.problem(
                            "checkpoint_replaced",
                            None,
                            format!(
                                "checkpoint {} in the database differs from the anchored copy",
                                anchor.checkpoint_no
                            ),
                        );
                    }
                }
                self.check_checkpoint(
                    anchor,
                    &by_seq,
                    options,
                    &mut report,
                    "anchored checkpoint",
                )?;
            }
        } else {
            report.notes.push(
                "Removal of the newest entries after the last checkpoint cannot be detected from the database alone; verify against a checkpoint kept outside it (against_checkpoint).".into(),
            );
        }
        if report.signed_checkpoints == 0 && report.checkpoints_checked > 0 {
            report.notes.push(
                "No checkpoint is signed: anyone able to write the database could have rebuilt it consistently.".into(),
            );
        }
        report.missing_ranges.sort();
        report.modified_seqs.sort();
        report.intact = report.problems.is_empty();
        Ok(report)
    }

    fn check_checkpoint(
        &self,
        cp: &Checkpoint,
        by_seq: &BTreeMap<u64, String>,
        options: &VerifyOptions,
        report: &mut VerifyReport,
        label: &str,
    ) -> Result<(), StorageError> {
        let no = cp.checkpoint_no;
        if cp.compute_hash()? != cp.checkpoint_hash {
            report.problem(
                "checkpoint_modified",
                None,
                format!("{} {} was changed after it was written", label, no),
            );
        }
        match (&cp.signature, &cp.public_key, &cp.key_id) {
            (Some(sig), Some(x), Some(key_id)) => {
                report.signed_checkpoints += 1;
                if !report.key_ids.contains(key_id) {
                    report.key_ids.push(key_id.clone());
                }
                if &signing::jwk_thumbprint(x) != key_id {
                    report.problem(
                        "checkpoint_key_mismatch",
                        None,
                        format!(
                            "{} {} names key {} but carries a different public key",
                            label,
                            no,
                            short(key_id)
                        ),
                    );
                } else if signing::verify_digest(&cp.checkpoint_hash, sig, x) {
                    report.signatures_verified += 1;
                } else {
                    report.problem(
                        "checkpoint_signature_invalid",
                        None,
                        format!("{} {} has a signature that does not verify", label, no),
                    );
                }
                if let Some(expected) = &options.expected_key_id {
                    if expected != key_id {
                        report.problem(
                            "unexpected_key",
                            None,
                            format!(
                                "{} {} is signed by key {}, not the expected key {}",
                                label,
                                no,
                                short(key_id),
                                short(expected)
                            ),
                        );
                    }
                }
            }
            (None, None, None) => {
                if options.expected_key_id.is_some() {
                    report.problem(
                        "unsigned_checkpoint",
                        None,
                        format!(
                            "{} {} is not signed but a signing key is required",
                            label, no
                        ),
                    );
                }
            }
            _ => report.problem(
                "checkpoint_signature_invalid",
                None,
                format!(
                    "{} {} has an incomplete signature (signature, key or key id missing)",
                    label, no
                ),
            ),
        }
        // Recompute the head and Merkle root from the entries as stored now.
        let mut leaves = Vec::new();
        let mut complete = true;
        for seq in cp.from_seq..=cp.seq {
            match by_seq.get(&seq).and_then(|h| merkle::hash_from_hex(h)) {
                Some(h) => leaves.push(h),
                None => {
                    complete = false;
                    break;
                }
            }
        }
        if !complete {
            // Missing entries are reported by the chain walk / truncation check.
            return Ok(());
        }
        if by_seq.get(&cp.seq) != Some(&cp.head_hash) {
            report.problem(
                "checkpoint_mismatch",
                Some(cp.seq),
                format!(
                    "{} {} recorded head {} at entry {}, the log now has a different entry there (rewritten after the checkpoint)",
                    label, no, short(&cp.head_hash), cp.seq
                ),
            );
        } else if merkle::hash_to_hex(&merkle::merkle_root(&leaves)) != cp.merkle_root {
            report.problem(
                "checkpoint_mismatch",
                Some(cp.from_seq),
                format!(
                    "{} {}: {} its Merkle root (rewritten after the checkpoint)",
                    label,
                    no,
                    entries_span(cp.from_seq, cp.seq, "no longer matches", "no longer match")
                ),
            );
        }
        Ok(())
    }
}

/// `entry 4 was removed` / `entries 3-5 were removed`.
fn entries_span(first: u64, last: u64, one: &str, many: &str) -> String {
    if first == last {
        format!("entry {} {}", first, one)
    } else {
        format!("entries {}-{} {}", first, last, many)
    }
}

fn short(hash: &str) -> &str {
    &hash[..hash.len().min(12)]
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::models::{DecisionSnapshot, Output};

    fn decision(answer: &str) -> DecisionSnapshot {
        DecisionSnapshot::new("review_claim")
            .add_output(Output::new("verdict", json!(answer), "string").with_confidence(0.93))
            .with_execution_time(12.5)
    }

    fn key() -> SigningKey {
        SigningKey::from_seed(&[9u8; 32]).unwrap()
    }

    fn chained(path: &std::path::Path, every: u64) -> SqliteBackend {
        SqliteBackend::new(path)
            .unwrap()
            .with_chain(
                ChainConfig::new()
                    .with_checkpoint_every(every)
                    .with_signing_key(key()),
            )
            .unwrap()
    }

    fn raw(path: &std::path::Path) -> Connection {
        let conn = Connection::open(path).unwrap();
        conn.execute_batch(
            "DROP TRIGGER chain_entries_no_update; DROP TRIGGER chain_entries_no_delete; \
             DROP TRIGGER chain_checkpoints_no_update; DROP TRIGGER chain_checkpoints_no_delete;",
        )
        .unwrap();
        conn
    }

    fn kinds(report: &VerifyReport) -> Vec<&str> {
        report.problems.iter().map(|p| p.kind.as_str()).collect()
    }

    fn fill(backend: &SqliteBackend, n: usize) -> Vec<String> {
        (0..n)
            .map(|i| {
                backend
                    .save_decision_internal(&decision(&format!("a{}", i)))
                    .unwrap()
            })
            .collect()
    }

    #[test]
    fn an_untouched_chain_verifies_and_links_every_entry() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("e.db");
        let backend = chained(&path, 4);
        fill(&backend, 10);
        backend
            .append_record("rec-1", &json!({"x": 1.5, "y": "é"}))
            .unwrap();
        let report = backend.verify_chain(&VerifyOptions::default()).unwrap();
        assert!(report.intact, "{:?}", report.problems);
        assert_eq!(report.entries_checked, 11);
        assert_eq!(report.checkpoints_checked, 2);
        assert_eq!(report.signatures_verified, 2);
        assert_eq!(report.entries_after_last_checkpoint, 3);
        assert_eq!(report.key_ids, vec![key().key_id()]);
        let entries = backend.chain_entries(1, 100).unwrap();
        for pair in entries.windows(2) {
            assert_eq!(pair[1].0.prev_hash, pair[0].0.entry_hash);
        }
        assert_eq!(entries[0].0.prev_hash, GENESIS_HASH);
    }

    #[test]
    fn saves_of_existing_ids_and_deletes_are_refused() {
        let dir = tempfile::tempdir().unwrap();
        let backend = chained(&dir.path().join("e.db"), 0);
        let d = decision("yes");
        backend.save_decision_internal(&d).unwrap();
        assert!(matches!(
            backend.save_decision_internal(&d),
            Err(StorageError::AlreadyExists(_))
        ));
        assert!(matches!(
            backend.delete_internal(&d.metadata.snapshot_id.to_string()),
            Err(StorageError::PermissionDenied(_))
        ));
        backend.append_record("r", &json!({})).unwrap();
        assert!(matches!(
            backend.append_record("r", &json!({})),
            Err(StorageError::AlreadyExists(_))
        ));
        assert_eq!(backend.chain_head().unwrap().unwrap().seq, 2);
    }

    #[test]
    fn inexact_integers_are_refused_not_rounded() {
        let backend = SqliteBackend::in_memory()
            .unwrap()
            .with_chain(ChainConfig::new())
            .unwrap();
        let err = backend
            .append_record("big", &json!({"id": 9007199254740993u64}))
            .unwrap_err();
        assert!(err.to_string().contains("encode it as a string"), "{}", err);
        assert!(backend.chain_head().unwrap().is_none());
    }

    #[test]
    fn in_place_edit_of_a_record_is_reported_with_its_seq() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("e.db");
        let backend = chained(&path, 0);
        let ids = fill(&backend, 5);
        raw(&path)
            .execute(
                "UPDATE snapshots SET data_json = replace(data_json, '\"a2\"', '\"a9\"') WHERE id = ?",
                params![ids[2]],
            )
            .unwrap();
        let report = backend.verify_chain(&VerifyOptions::default()).unwrap();
        assert!(!report.intact);
        assert_eq!(kinds(&report), vec!["snapshot_modified"]);
        assert_eq!(report.modified_seqs, vec![3]);
        assert_eq!(report.first_broken_seq, Some(3));
    }

    #[test]
    fn edit_of_the_chain_row_itself_is_reported() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("e.db");
        let backend = chained(&path, 0);
        backend.append_record("a", &json!({"v": 1})).unwrap();
        backend.append_record("b", &json!({"v": 2})).unwrap();
        raw(&path)
            .execute(
                "UPDATE chain_entries SET record_json = '{\"v\":3}' WHERE seq = 2",
                [],
            )
            .unwrap();
        let report = backend.verify_chain(&VerifyOptions::default()).unwrap();
        assert_eq!(kinds(&report), vec!["modified_entry"]);
        assert_eq!(report.modified_seqs, vec![2]);
    }

    #[test]
    fn deletion_reports_the_missing_range_and_the_checkpoint_mismatch() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("e.db");
        let backend = chained(&path, 0);
        fill(&backend, 6);
        backend.checkpoint().unwrap();
        raw(&path)
            .execute("DELETE FROM chain_entries WHERE seq IN (3, 4)", [])
            .unwrap();
        let report = backend.verify_chain(&VerifyOptions::default()).unwrap();
        assert!(!report.intact);
        assert_eq!(report.missing_ranges, vec![(3, 4)]);
        assert_eq!(report.first_broken_seq, Some(3));
        assert!(kinds(&report).contains(&"missing_entries"));
        // The snapshots for 3 and 4 are now outside the chain.
        assert_eq!(
            kinds(&report)
                .iter()
                .filter(|k| **k == "unchained_record")
                .count(),
            2
        );
    }

    #[test]
    fn reordering_breaks_the_links() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("e.db");
        let backend = chained(&path, 0);
        for i in 0..4 {
            backend
                .append_record(&format!("r{}", i), &json!({"i": i}))
                .unwrap();
        }
        let conn = raw(&path);
        // Swap entries 2 and 3 (renumbering them), as a reordering attack would.
        conn.execute_batch(
            "UPDATE chain_entries SET seq = -2 WHERE seq = 2; \
             UPDATE chain_entries SET seq = 2 WHERE seq = 3; \
             UPDATE chain_entries SET seq = 3 WHERE seq = -2;",
        )
        .unwrap();
        let report = backend.verify_chain(&VerifyOptions::default()).unwrap();
        assert!(kinds(&report).contains(&"broken_link"));
        assert_eq!(report.first_broken_seq, Some(2));
    }

    #[test]
    fn a_consistently_rewritten_tail_is_caught_by_the_signed_checkpoint() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("e.db");
        let backend = chained(&path, 0);
        for i in 0..4 {
            backend
                .append_record(&format!("r{}", i), &json!({"i": i}))
                .unwrap();
        }
        backend.checkpoint().unwrap();
        // Attacker without the key deletes entry 4 and appends a forged one
        // with correct hashes.
        raw(&path)
            .execute("DELETE FROM chain_entries WHERE seq = 4", [])
            .unwrap();
        let forger = SqliteBackend::new(&path)
            .unwrap()
            .with_chain(ChainConfig::new().with_checkpoint_every(0))
            .unwrap();
        forger.append_record("forged", &json!({"i": 99})).unwrap();
        let report = backend.verify_chain(&VerifyOptions::default()).unwrap();
        assert!(
            kinds(&report).contains(&"checkpoint_mismatch"),
            "{:?}",
            report.problems
        );
    }

    #[test]
    fn truncation_is_only_visible_against_an_anchored_checkpoint() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("e.db");
        let backend = chained(&path, 0);
        for i in 0..5 {
            backend
                .append_record(&format!("r{}", i), &json!({"i": i}))
                .unwrap();
        }
        let anchor = backend.checkpoint().unwrap().unwrap();
        // The attacker drops the newest two entries AND the checkpoint row.
        let conn = raw(&path);
        conn.execute("DELETE FROM chain_entries WHERE seq > 3", [])
            .unwrap();
        conn.execute("DELETE FROM chain_checkpoints", []).unwrap();
        let alone = backend.verify_chain(&VerifyOptions::default()).unwrap();
        assert!(
            alone.intact,
            "undetectable without an anchor: {:?}",
            alone.problems
        );
        assert!(alone.notes.iter().any(|n| n.contains("cannot be detected")));
        let anchored = backend
            .verify_chain(&VerifyOptions {
                against_checkpoint: Some(anchor),
                expected_key_id: Some(key().key_id()),
            })
            .unwrap();
        assert!(!anchored.intact);
        assert!(kinds(&anchored).contains(&"truncated"));
        assert!(anchored.anchored);
    }

    #[test]
    fn a_tampered_or_resigned_checkpoint_is_reported() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("e.db");
        let backend = chained(&path, 0);
        backend.append_record("a", &json!({})).unwrap();
        backend.checkpoint().unwrap();
        raw(&path)
            .execute(
                "UPDATE chain_checkpoints SET created_at = '2000-01-01T00:00:00Z'",
                [],
            )
            .unwrap();
        let report = backend.verify_chain(&VerifyOptions::default()).unwrap();
        assert!(kinds(&report).contains(&"checkpoint_modified"));

        // Key substitution: re-sign everything with the attacker's key. It
        // verifies internally; only a pinned key id exposes it.
        let dir2 = tempfile::tempdir().unwrap();
        let path2 = dir2.path().join("e.db");
        let attacker = SigningKey::from_seed(&[66u8; 32]).unwrap();
        let forged = SqliteBackend::new(&path2)
            .unwrap()
            .with_chain(
                ChainConfig::new()
                    .with_checkpoint_every(0)
                    .with_signing_key(attacker),
            )
            .unwrap();
        forged.append_record("a", &json!({})).unwrap();
        forged.checkpoint().unwrap();
        assert!(
            forged
                .verify_chain(&VerifyOptions::default())
                .unwrap()
                .intact
        );
        let pinned = forged
            .verify_chain(&VerifyOptions {
                against_checkpoint: None,
                expected_key_id: Some(key().key_id()),
            })
            .unwrap();
        assert_eq!(kinds(&pinned), vec!["unexpected_key"]);
    }

    #[test]
    fn inclusion_proofs_verify_against_the_checkpoint_root() {
        let dir = tempfile::tempdir().unwrap();
        let backend = chained(&dir.path().join("e.db"), 7);
        for i in 0..20 {
            backend
                .append_record(&format!("r{}", i), &json!({"i": i}))
                .unwrap();
        }
        assert!(
            backend.inclusion_proof(20).unwrap().is_none(),
            "not checkpointed yet"
        );
        for seq in 1..=14 {
            let proof = backend.inclusion_proof(seq).unwrap().unwrap();
            let leaf = merkle::hash_from_hex(&proof.entry_hash).unwrap();
            let path: Vec<Hash> = proof
                .audit_path
                .iter()
                .map(|h| merkle::hash_from_hex(h).unwrap())
                .collect();
            let root = merkle::hash_from_hex(&proof.merkle_root).unwrap();
            assert!(merkle::verify_inclusion(
                &leaf,
                proof.leaf_index as usize,
                proof.tree_size as usize,
                &path,
                &root
            ));
        }
    }

    #[test]
    fn opting_in_adopts_existing_rows_and_flags_later_unchained_writes() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("e.db");
        let plain = SqliteBackend::new(&path).unwrap();
        plain.save_decision_internal(&decision("before")).unwrap();
        let backend = chained(&path, 0);
        let report = backend.verify_chain(&VerifyOptions::default()).unwrap();
        assert!(report.intact);
        assert_eq!(report.entries_checked, 1);
        // A handle without the chain writes around it.
        plain.save_decision_internal(&decision("sneaky")).unwrap();
        let report = backend.verify_chain(&VerifyOptions::default()).unwrap();
        assert_eq!(kinds(&report), vec!["unchained_record"]);
    }

    #[test]
    fn a_plain_database_is_unchanged_by_the_migration() {
        let backend = SqliteBackend::in_memory().unwrap();
        let d = decision("x");
        let id = backend.save_decision_internal(&d).unwrap();
        // Default handles still replace by id and delete.
        backend.save_decision_internal(&d).unwrap();
        assert!(backend.delete_internal(&id).unwrap());
        assert!(!backend.is_chained());
        assert!(backend.chain_head().unwrap().is_none());
    }

    /// Two real processes append to one database at the same time; the chain
    /// must come out linear with no gaps.
    #[test]
    fn two_processes_appending_do_not_fork_the_chain() {
        const CHILD: &str = "BRIEFCASE_CHAIN_CHILD_DB";
        if let Ok(path) = std::env::var(CHILD) {
            let tag = std::env::var("BRIEFCASE_CHAIN_CHILD_TAG").unwrap();
            let backend = SqliteBackend::new(&path)
                .unwrap()
                .with_chain(
                    ChainConfig::new()
                        .with_checkpoint_every(25)
                        .with_signing_key(key()),
                )
                .unwrap();
            for i in 0..100 {
                backend
                    .append_record(
                        &format!("{}-{}", tag, i),
                        &json!({"tag": tag, "i": i, "f": 0.1 * i as f64}),
                    )
                    .unwrap();
            }
            return;
        }
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("shared.db");
        // Create the schema first so both children open an existing file.
        drop(chained(&path, 25));
        let exe = std::env::current_exe().unwrap();
        let spawn = |tag: &str| {
            std::process::Command::new(&exe)
                .args([
                    "--exact",
                    "storage::chain::tests::two_processes_appending_do_not_fork_the_chain",
                    "--nocapture",
                ])
                .env(CHILD, &path)
                .env("BRIEFCASE_CHAIN_CHILD_TAG", tag)
                .spawn()
                .unwrap()
        };
        let (mut a, mut b) = (spawn("a"), spawn("b"));
        assert!(a.wait().unwrap().success());
        assert!(b.wait().unwrap().success());
        let backend = chained(&path, 25);
        let report = backend.verify_chain(&VerifyOptions::default()).unwrap();
        assert!(report.intact, "{:?}", report.problems);
        assert_eq!(report.entries_checked, 200);
        assert_eq!(report.checkpoints_checked, 8);
        let entries = backend.chain_entries(1, 200).unwrap();
        let from_a = entries
            .iter()
            .filter(|(e, _)| e.record_id.starts_with("a-"))
            .count();
        assert_eq!(from_a, 100);
    }
}