chio-store-sqlite 0.1.2

SQLite-backed persistence, query, and report implementations for Chio
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
use super::super::*;
use super::support::*;

#[test]
fn append_chio_receipt_rejects_invalid_signature() {
    let path = unique_db_path("chio-receipts-invalid-signature");
    let store = SqliteReceiptStore::open(&path).test_unwrap();
    let mut receipt = sample_receipt_with_id("rcpt-invalid-signature");
    receipt.tool_name = "sh".to_string();

    let error = store.append_chio_receipt(&receipt).test_unwrap_err();
    assert!(matches!(
        error,
        chio_kernel::ReceiptStoreError::Conflict(message)
            if message.contains("invalid signature")
    ));

    let _ = fs::remove_file(path);
}

#[test]
fn receipt_operation_reports_keep_stable_null_json_fields() {
    let report = chio_kernel::ReceiptFlushReport::default();
    let value = serde_json::to_value(&report).test_unwrap();

    assert!(value.get("latestCheckpointSeq").test_unwrap().is_null());
    assert!(value.get("uncheckpointedStartSeq").test_unwrap().is_null());
    assert!(value.get("uncheckpointedEndSeq").test_unwrap().is_null());
    assert!(value.get("walCheckpoint").test_unwrap().is_null());
    assert!(value.get("dbSizeBytes").test_unwrap().is_null());
    assert!(value["writer"]
        .get("lastCommitUnixMs")
        .test_unwrap()
        .is_null());
    assert!(value["writer"].get("lastError").test_unwrap().is_null());
}

#[test]
fn append_chio_receipt_canonical_rejects_unsigned_extra_fields() {
    let path = unique_db_path("chio-receipts-canonical-extra-fields");
    let store = SqliteReceiptStore::open(&path).test_unwrap();
    let receipt = sample_receipt_with_id("rcpt-canonical-extra-fields");
    let mut value = serde_json::to_value(&receipt).test_unwrap();
    value
        .as_object_mut()
        .test_unwrap()
        .insert("unsigned_extra".to_string(), serde_json::json!("ignored"));
    let canonical = Arc::new(CanonicalBytes::from_value(&value).test_unwrap());

    let error = store
        .append_chio_receipt_canonical_returning_seq(canonical)
        .test_unwrap_err();
    assert!(matches!(
        error,
        chio_kernel::ReceiptStoreError::Canonical(message)
            if message.contains("do not match ChioReceipt serialization")
    ));

    let _ = fs::remove_file(path);
}

#[test]
fn append_chio_receipt_rejects_mismatched_parameter_hash() {
    let path = unique_db_path("chio-receipts-invalid-parameter-hash");
    let store = SqliteReceiptStore::open(&path).test_unwrap();
    let keypair = Keypair::generate();
    let receipt = ChioReceipt::sign(
        ChioReceiptBody {
            id: "rcpt-invalid-parameter-hash".to_string(),
            timestamp: 1,
            capability_id: "cap-1".to_string(),
            tool_server: "shell".to_string(),
            tool_name: "bash".to_string(),
            action: ToolCallAction {
                parameters: serde_json::json!({ "cmd": "echo changed" }),
                parameter_hash: "bad-parameter-hash".to_string(),
            },
            decision: Some(Decision::Allow),
            receipt_kind: Default::default(),
            boundary_class: Default::default(),
            observation_outcome: None,
            tool_origin: Default::default(),
            redaction_mode: Default::default(),
            actor_chain: Vec::new(),
            content_hash: "content-1".to_string(),
            policy_hash: "policy-1".to_string(),
            evidence: Vec::new(),
            metadata: None,
            trust_level: chio_core::receipt::kinds::TrustLevel::default(),
            tenant_id: None,
            kernel_key: keypair.public_key(),
            bbs_projection_version: None,
        },
        &keypair,
    )
    .test_unwrap();

    let error = store.append_chio_receipt(&receipt).test_unwrap_err();
    assert!(matches!(
        error,
        chio_kernel::ReceiptStoreError::Conflict(message)
            if message.contains("mismatched action parameter hash")
    ));

    let _ = fs::remove_file(path);
}

#[test]
fn append_child_receipt_rejects_invalid_signature() {
    let path = unique_db_path("chio-child-receipts-invalid-signature");
    let store = SqliteReceiptStore::open(&path).test_unwrap();
    let mut receipt = sample_child_receipt_with_id_and_timestamp("child-invalid-signature", 2);
    receipt.outcome_hash = "outcome-mutated".to_string();

    let error = store.append_child_receipt(&receipt).test_unwrap_err();
    assert!(matches!(
        error,
        chio_kernel::ReceiptStoreError::Conflict(message)
            if message.contains("child-invalid-signature")
                && message.contains("has invalid signature")
    ));

    let _ = fs::remove_file(path);
}

#[test]
fn evidence_export_rejects_tampered_persisted_tool_receipt() {
    let path = unique_db_path("chio-evidence-export-tamper");
    let store = SqliteReceiptStore::open(&path).test_unwrap();
    let receipt = sample_receipt_with_id("tampered-export-receipt");
    store.append_chio_receipt(&receipt).test_unwrap();
    tamper_persisted_tool_receipt(&store, &receipt.id, |receipt| {
        receipt.tool_name = "sh".to_string();
    });

    let error = store
        .build_evidence_export_bundle(&EvidenceExportQuery::admin_all())
        .test_unwrap_err();
    let message = error.to_string();
    assert!(message.contains("persisted tool receipt seq"));
    assert!(message.contains(&receipt.id));
    assert!(message.contains("invalid signature"));

    let _ = fs::remove_file(path);
}

#[test]
fn behavioral_feed_report_rejects_tampered_persisted_tool_receipt() {
    let path = unique_db_path("chio-behavioral-feed-tamper");
    let store = SqliteReceiptStore::open(&path).test_unwrap();
    let receipt = sample_receipt_with_id("tampered-report-receipt");
    store.append_chio_receipt(&receipt).test_unwrap();
    tamper_persisted_tool_receipt(&store, &receipt.id, |receipt| {
        receipt.tool_name = "sh".to_string();
    });

    let query = BehavioralFeedQuery {
        read_context: Some(chio_kernel::ReceiptReadContext::local_operator_admin_all()),
        ..BehavioralFeedQuery::default()
    };
    let error = store
        .query_behavioral_feed_receipts(&query)
        .test_unwrap_err();
    let message = error.to_string();
    assert!(message.contains("persisted tool receipt seq"));
    assert!(message.contains(&receipt.id));
    assert!(message.contains("invalid signature"));

    let _ = fs::remove_file(path);
}

#[test]
fn claim_log_replay_rejects_tampered_persisted_tool_receipt() {
    let path = unique_db_path("chio-claim-log-tamper");
    let store = SqliteReceiptStore::open(&path).test_unwrap();
    let receipt = sample_receipt_with_id("tampered-claim-log-receipt");
    let seq = store
        .append_chio_receipt_returning_seq(&receipt)
        .test_unwrap();
    tamper_claim_log_tool_receipt(&store, &receipt.id, |receipt| {
        receipt.tool_name = "sh".to_string();
    });

    let error = store
        .receipts_canonical_bytes_range(seq, seq)
        .test_unwrap_err();
    let message = error.to_string();
    assert!(message.contains("claim-log tool receipt seq"));
    assert!(message.contains(&receipt.id));
    assert!(message.contains("invalid signature"));

    let _ = fs::remove_file(path);
}

#[test]
fn append_child_receipt_fails_closed_on_existing_claim_log_projection_drift() {
    let path = unique_db_path("chio-claim-log-child-append-drift");
    let store = SqliteReceiptStore::open(&path).test_unwrap();
    let receipt = sample_receipt_with_id("tampered-claim-log-before-child-append");
    store.append_chio_receipt(&receipt).test_unwrap();
    tamper_claim_log_tool_receipt(&store, &receipt.id, |receipt| {
        receipt.tool_name = "sh".to_string();
    });

    // The default incremental append fast path uses an O(1) checkpoint-
    // predecessor check plus an O(b) delta cross-check instead of an O(N)
    // claim-log content re-derive-and-compare on every append; neither rescans
    // an existing row's content. An append that does not touch the tampered row
    // therefore succeeds; the store still fails closed on the drift, just
    // observed through the audit surfaces (`receipt_store_health` /
    // `receipt_checkpoint_status`), which keep the full per-row verification on
    // purpose.
    store
        .append_child_receipt(&sample_child_receipt_with_id_and_timestamp(
            "child-after-claim-log-drift",
            2,
        ))
        .test_unwrap();

    let error = store.receipt_store_health().test_unwrap_err();
    assert!(
        error.to_string().contains("claim receipt log entry")
            && error.to_string().contains("diverges"),
        "unexpected error: {error}"
    );

    let _ = fs::remove_file(path);
}

#[test]
fn receipt_base_tables_reject_update_and_delete() {
    let path = unique_db_path("chio-receipts-base-immutable");
    let store = SqliteReceiptStore::open(&path).test_unwrap();

    let tool = sample_receipt_with_id("base-immutable-tool");
    let child = sample_child_receipt_with_id_and_timestamp("base-immutable-child", 2);
    store.append_chio_receipt(&tool).test_unwrap();
    store.append_child_receipt(&child).test_unwrap();

    let connection = store.connection().test_unwrap();
    let error = connection
        .execute(
            "UPDATE chio_tool_receipts SET raw_json = raw_json WHERE receipt_id = ?1",
            rusqlite::params![tool.id],
        )
        .test_unwrap_err();
    assert!(
        error.to_string().contains("tool receipts are immutable"),
        "unexpected error: {error}"
    );

    let error = connection
        .execute(
            "DELETE FROM chio_tool_receipts WHERE receipt_id = ?1",
            rusqlite::params![tool.id],
        )
        .test_unwrap_err();
    assert!(
        error.to_string().contains("tool receipts are immutable"),
        "unexpected error: {error}"
    );

    let error = connection
        .execute(
            "UPDATE chio_child_receipts SET raw_json = raw_json WHERE receipt_id = ?1",
            rusqlite::params![child.id],
        )
        .test_unwrap_err();
    assert!(
        error.to_string().contains("child receipts are immutable"),
        "unexpected error: {error}"
    );

    let error = connection
        .execute(
            "DELETE FROM chio_child_receipts WHERE receipt_id = ?1",
            rusqlite::params![child.id],
        )
        .test_unwrap_err();
    assert!(
        error.to_string().contains("child receipts are immutable"),
        "unexpected error: {error}"
    );

    let _ = fs::remove_file(path);
}

#[test]
fn claim_log_projection_uses_capability_lineage_when_receipt_lacks_attribution() {
    let path = unique_db_path("chio-claim-log-lineage-projection");
    let store = SqliteReceiptStore::open(&path).test_unwrap();
    let subject_kp = Keypair::generate();
    let issuer_kp = Keypair::generate();
    let receipt_kp = Keypair::generate();
    let subject_hex = subject_kp.public_key().to_hex();
    let issuer_hex = issuer_kp.public_key().to_hex();

    let capability = CapabilityToken::sign(
        CapabilityTokenBody {
            id: "cap-claim-log-lineage".to_string(),
            issuer: issuer_kp.public_key(),
            subject: subject_kp.public_key(),
            scope: ChioScope {
                grants: vec![ToolGrant {
                    server_id: "shell".to_string(),
                    tool_name: "bash".to_string(),
                    operations: vec![Operation::Invoke],
                    constraints: Vec::new(),
                    max_invocations: None,
                    max_cost_per_invocation: None,
                    max_total_cost: None,
                    dpop_required: Some(true),
                }],
                ..ChioScope::default()
            },
            issued_at: 1_000,
            expires_at: 9_000,
            delegation_chain: Vec::new(),
            aggregate_invocation_budget: None,
        },
        &issuer_kp,
    )
    .test_unwrap();
    store
        .record_capability_snapshot(&capability, None)
        .test_unwrap();

    let receipt = ChioReceipt::sign(
        ChioReceiptBody {
            id: "rcpt-claim-log-lineage".to_string(),
            timestamp: 2_000,
            capability_id: capability.id.clone(),
            tool_server: "shell".to_string(),
            tool_name: "bash".to_string(),
            action: valid_tool_action(serde_json::json!({ "cmd": "echo projection" })),
            decision: Some(Decision::Allow),
            receipt_kind: Default::default(),
            boundary_class: Default::default(),
            observation_outcome: None,
            tool_origin: Default::default(),
            redaction_mode: Default::default(),
            actor_chain: Vec::new(),
            content_hash: "content-claim-log-lineage".to_string(),
            policy_hash: "policy-claim-log-lineage".to_string(),
            evidence: Vec::new(),
            metadata: Some(serde_json::json!({
                "financial": FinancialReceiptMetadata {
                    grant_index: 0,
                    cost_charged: 10,
                    currency: "USD".to_string(),
                    budget_remaining: 990,
                    budget_total: 1_000,
                    delegation_depth: 0,
                    root_budget_holder: subject_hex.clone(),
                    payment_reference: None,
                    settlement_status: SettlementStatus::Settled,
                    cost_breakdown: None,
                    oracle_evidence: None,
                    attempted_cost: None,
                }
            })),
            trust_level: chio_core::receipt::kinds::TrustLevel::default(),
            tenant_id: None,
            kernel_key: receipt_kp.public_key(),
            bbs_projection_version: None,
        },
        &receipt_kp,
    )
    .test_unwrap();

    store.append_chio_receipt(&receipt).test_unwrap();
    drop(store);

    let reopened = SqliteReceiptStore::open(&path).test_unwrap();
    let (projected_subject_key, projected_issuer_key) =
        load_claim_log_identity(&reopened, &receipt.id);
    assert_eq!(projected_subject_key.as_deref(), Some(subject_hex.as_str()));
    assert_eq!(projected_issuer_key.as_deref(), Some(issuer_hex.as_str()));

    let _ = fs::remove_file(path);
}

#[test]
fn store_checkpoint_rejects_merkle_root_mismatch_with_claim_log() {
    let path = unique_db_path("chio-receipts-cp-merkle-mismatch");
    let store = SqliteReceiptStore::open(&path).test_unwrap();
    let receipt = sample_receipt_with_id("rcpt-merkle-mismatch");
    let seq = store
        .append_chio_receipt_returning_seq(&receipt)
        .test_unwrap();
    let kp = receipt_test_keypair();
    let checkpoint = build_checkpoint(1, seq, seq, &[b"wrong-bytes".to_vec()], &kp).test_unwrap();

    let error = ReceiptStore::store_checkpoint(&store, &checkpoint).test_unwrap_err();
    assert!(
        error
            .to_string()
            .contains("merkle_root does not match claim receipt log range"),
        "unexpected error: {error}"
    );

    let _ = fs::remove_file(path);
}

#[test]
fn append_receipt_fails_closed_when_earlier_checkpoint_row_is_corrupted() {
    let path = unique_db_path("chio-receipts-cp-fail-closed");
    let store = SqliteReceiptStore::open(&path).test_unwrap();
    let kp = receipt_test_keypair();

    let first = build_checkpoint(1, 1, 2, &[b"one".to_vec(), b"two".to_vec()], &kp).test_unwrap();
    let second = build_checkpoint_with_previous(
        2,
        3,
        4,
        &[b"three".to_vec(), b"four".to_vec()],
        &kp,
        Some(&first),
        &[chio_kernel::checkpoint::checkpoint_chain_leaf_hash(&first.body).test_unwrap()],
    )
    .test_unwrap();
    let third = build_checkpoint_with_previous(
        3,
        5,
        6,
        &[b"five".to_vec(), b"six".to_vec()],
        &kp,
        Some(&second),
        &[
            chio_kernel::checkpoint::checkpoint_chain_leaf_hash(&first.body).test_unwrap(),
            chio_kernel::checkpoint::checkpoint_chain_leaf_hash(&second.body).test_unwrap(),
        ],
    )
    .test_unwrap();

    let mut corrupted_first_body = first.body.clone();
    corrupted_first_body.batch_end_seq += 1;
    let corrupted_first_json = serde_json::to_string(&corrupted_first_body).test_unwrap();
    insert_checkpoint_row_with_statement_json(
        &store,
        &first,
        first.body.batch_end_seq,
        &corrupted_first_json,
    );
    insert_checkpoint_row(&store, &second, second.body.batch_end_seq);
    insert_checkpoint_row(&store, &third, third.body.batch_end_seq);

    let error = ReceiptStore::append_chio_receipt_returning_seq(
        &store,
        &sample_receipt_with_id("rcpt-fail-closed"),
    )
    .test_unwrap_err();
    assert!(
        error.to_string().contains("does not match signed body"),
        "unexpected error: {error}"
    );

    let _ = fs::remove_file(path);
}

#[test]
fn trait_store_checkpoint_rejects_conflicting_rewritten_checkpoint_rows() {
    let path = unique_db_path("chio-receipts-cp-rewrite-detect");
    let store = SqliteReceiptStore::open(&path).test_unwrap();

    let mut seqs = Vec::new();
    for i in 0..4usize {
        let receipt = sample_receipt_with_id(&format!("rcpt-rewrite-{i}"));
        seqs.push(
            store
                .append_chio_receipt_returning_seq(&receipt)
                .test_unwrap(),
        );
    }

    let checkpoint_kp = receipt_test_keypair();
    let first = build_checkpoint(
        1,
        seqs[0],
        seqs[1],
        &canonical_receipt_bytes(&store, seqs[0], seqs[1]),
        &checkpoint_kp,
    )
    .test_unwrap();
    insert_checkpoint_row(&store, &first, seqs[1] + 1);

    let second = build_checkpoint(
        2,
        seqs[2],
        seqs[3],
        &canonical_receipt_bytes(&store, seqs[2], seqs[3]),
        &checkpoint_kp,
    )
    .test_unwrap();
    let error = ReceiptStore::store_checkpoint(&store, &second).test_unwrap_err();
    assert!(
        error.to_string().contains("does not match signed body"),
        "unexpected error: {error}"
    );

    let _ = fs::remove_file(path);
}

#[test]
fn store_checkpoint_rejects_wrong_predecessor_digest() {
    let path = unique_db_path("chio-receipts-cp-continuity");
    let store = SqliteReceiptStore::open(&path).test_unwrap();
    let kp = receipt_test_keypair();

    let mut seqs = Vec::new();
    for i in 0..4usize {
        let receipt = sample_receipt_with_id(&format!("rcpt-predecessor-digest-{i}"));
        seqs.push(
            store
                .append_chio_receipt_returning_seq(&receipt)
                .test_unwrap(),
        );
    }

    let first = build_checkpoint(
        1,
        seqs[0],
        seqs[1],
        &canonical_receipt_bytes(&store, seqs[0], seqs[1]),
        &kp,
    )
    .test_unwrap();
    ReceiptStore::store_checkpoint(&store, &first).test_unwrap();

    let mut second = build_checkpoint_with_previous(
        2,
        seqs[2],
        seqs[3],
        &canonical_receipt_bytes(&store, seqs[2], seqs[3]),
        &kp,
        Some(&first),
        &[chio_kernel::checkpoint::checkpoint_chain_leaf_hash(&first.body).test_unwrap()],
    )
    .test_unwrap();
    second.body.previous_checkpoint_sha256 = Some("00".repeat(32));
    second.signature = kp.sign(&chio_core::canonical_json_bytes(&second.body).test_unwrap());
    let error = ReceiptStore::store_checkpoint(&store, &second).test_unwrap_err();
    assert!(error
        .to_string()
        .contains("does not match predecessor digest"));

    let _ = fs::remove_file(path);
}

#[test]
fn store_checkpoint_rejects_conflicting_rewrite() {
    let path = unique_db_path("chio-receipts-cp-rewrite");
    let store = SqliteReceiptStore::open(&path).test_unwrap();
    let kp = receipt_test_keypair();

    let first = sample_receipt_with_id("rcpt-rewrite-first");
    let second = sample_receipt_with_id("rcpt-rewrite-second");
    let first_seq = store
        .append_chio_receipt_returning_seq(&first)
        .test_unwrap();
    let second_seq = store
        .append_chio_receipt_returning_seq(&second)
        .test_unwrap();
    let checkpoint = build_checkpoint(
        1,
        first_seq,
        second_seq,
        &canonical_receipt_bytes(&store, first_seq, second_seq),
        &kp,
    )
    .test_unwrap();
    ReceiptStore::store_checkpoint(&store, &checkpoint).test_unwrap();

    let conflicting = build_checkpoint(
        1,
        first_seq,
        second_seq,
        &[b"one".to_vec(), b"changed".to_vec()],
        &kp,
    )
    .test_unwrap();
    let error = ReceiptStore::store_checkpoint(&store, &conflicting).test_unwrap_err();
    assert!(error
        .to_string()
        .contains("already exists with different content"));

    let _ = fs::remove_file(path);
}

#[test]
fn store_checkpoint_rejects_missing_projection_atomically() {
    let path = unique_db_path("chio-receipts-cp-projection-missing");
    let store = SqliteReceiptStore::open(&path).test_unwrap();
    let receipt = sample_receipt_with_id("rcpt-projection-missing");
    let seq = store
        .append_chio_receipt_returning_seq(&receipt)
        .test_unwrap();
    let checkpoint_kp = receipt_test_keypair();
    let checkpoint = build_checkpoint(
        1,
        seq,
        seq,
        &canonical_receipt_bytes(&store, seq, seq),
        &checkpoint_kp,
    )
    .test_unwrap();

    store
        .connection()
        .test_unwrap()
        .execute_batch("DROP TRIGGER IF EXISTS kernel_checkpoints_project_tree_head;")
        .test_unwrap();
    let error = ReceiptStore::store_checkpoint(&store, &checkpoint).test_unwrap_err();

    assert!(
        error.to_string().contains("projection") && error.to_string().contains("missing"),
        "unexpected error: {error}"
    );
    let count: i64 = store
        .connection()
        .test_unwrap()
        .query_row(
            "SELECT COUNT(*) FROM kernel_checkpoints WHERE checkpoint_seq = 1",
            [],
            |row| row.get(0),
        )
        .test_unwrap();
    assert_eq!(count, 0);

    let _ = fs::remove_file(path);
}

#[test]
fn store_checkpoint_idempotent_rechecks_projection_rows() {
    let path = unique_db_path("chio-receipts-cp-idempotent-projection-drift");
    let store = SqliteReceiptStore::open(&path).test_unwrap();
    let receipt = sample_receipt_with_id("rcpt-idempotent-projection-drift");
    let seq = store
        .append_chio_receipt_returning_seq(&receipt)
        .test_unwrap();
    let checkpoint_kp = receipt_test_keypair();
    let checkpoint = build_checkpoint(
        1,
        seq,
        seq,
        &canonical_receipt_bytes(&store, seq, seq),
        &checkpoint_kp,
    )
    .test_unwrap();
    ReceiptStore::store_checkpoint(&store, &checkpoint).test_unwrap();

    let connection = store.connection().test_unwrap();
    connection
        .execute_batch(
            r#"
            DROP TRIGGER IF EXISTS checkpoint_tree_heads_reject_delete;
            DELETE FROM checkpoint_tree_heads WHERE checkpoint_seq = 1;
            "#,
        )
        .test_unwrap();

    let error = ReceiptStore::store_checkpoint(&store, &checkpoint).test_unwrap_err();
    assert!(
        error.to_string().contains("projection") && error.to_string().contains("missing"),
        "unexpected error: {error}"
    );

    let _ = fs::remove_file(path);
}

#[test]
fn receipt_checkpoint_status_reports_projection_drift() {
    let path = unique_db_path("chio-receipts-cp-status-projection-drift");
    let store = SqliteReceiptStore::open(&path).test_unwrap();
    let receipt = sample_receipt_with_id("rcpt-status-projection-drift");
    let seq = store
        .append_chio_receipt_returning_seq(&receipt)
        .test_unwrap();
    let checkpoint_kp = receipt_test_keypair();
    let checkpoint = build_checkpoint(
        1,
        seq,
        seq,
        &canonical_receipt_bytes(&store, seq, seq),
        &checkpoint_kp,
    )
    .test_unwrap();
    ReceiptStore::store_checkpoint(&store, &checkpoint).test_unwrap();

    let connection = store.connection().test_unwrap();
    connection
        .execute_batch(
            r#"
            DROP TRIGGER IF EXISTS checkpoint_publication_metadata_reject_delete;
            DELETE FROM checkpoint_publication_metadata WHERE checkpoint_seq = 1;
            "#,
        )
        .test_unwrap();

    let report = store.receipt_checkpoint_status(Some(10)).test_unwrap();
    assert!(!report.healthy);
    assert!(
        report
            .checkpoint_error
            .as_deref()
            .unwrap_or_default()
            .contains("projection"),
        "unexpected report: {report:?}"
    );

    let _ = fs::remove_file(path);
}

#[test]
fn open_existing_checkpoint_status_does_not_repair_missing_projection() {
    let path = unique_db_path("chio-receipts-cp-open-existing-projection-drift");
    let store = SqliteReceiptStore::open(&path).test_unwrap();
    let receipt = sample_receipt_with_id("rcpt-open-existing-projection-drift");
    let seq = store
        .append_chio_receipt_returning_seq(&receipt)
        .test_unwrap();
    let checkpoint_kp = receipt_test_keypair();
    let checkpoint = build_checkpoint(
        1,
        seq,
        seq,
        &canonical_receipt_bytes(&store, seq, seq),
        &checkpoint_kp,
    )
    .test_unwrap();
    ReceiptStore::store_checkpoint(&store, &checkpoint).test_unwrap();

    let connection = store.connection().test_unwrap();
    connection
        .execute_batch(
            r#"
            DROP TRIGGER IF EXISTS checkpoint_publication_metadata_reject_delete;
            DELETE FROM checkpoint_publication_metadata WHERE checkpoint_seq = 1;
            "#,
        )
        .test_unwrap();
    drop(connection);
    drop(store);

    let reopened = SqliteReceiptStore::open_existing(&path).test_unwrap();
    let report = reopened.receipt_checkpoint_status(Some(10)).test_unwrap();
    assert!(!report.healthy);
    assert!(
        report
            .checkpoint_error
            .as_deref()
            .unwrap_or_default()
            .contains("projection"),
        "unexpected report: {report:?}"
    );
    let missing_count: i64 = reopened
        .connection()
        .test_unwrap()
        .query_row(
            "SELECT COUNT(*) FROM checkpoint_publication_metadata WHERE checkpoint_seq = 1",
            [],
            |row| row.get(0),
        )
        .test_unwrap();
    assert_eq!(
        missing_count, 0,
        "open_existing/status must not repair checkpoint projection rows"
    );

    let _ = fs::remove_file(path);
}

/// The read-only health sampler the SIEM serve-mode watchdog uses must NOT
/// propagate a checkpoint-chain-integrity Err. On the fixed interval the
/// watchdog logs-and-skips a sample error with no gauge update, so a corrupt
/// store would look silent instead of alarming. Under chain corruption the
/// read-only path must instead return a report carrying the checkpoint_error and
/// a large-backlog range (checkpointed defaults to 0, spanning the whole
/// committed log) so the watchdog still emits a gauge, matching the fail-open
/// shape of `receipt_checkpoint_status`.
#[test]
fn health_read_only_reports_checkpoint_error_under_chain_corruption() {
    let path = unique_db_path("chio-receipts-health-ro-chain-corruption");
    let store = SqliteReceiptStore::open(&path).test_unwrap();
    let receipt = sample_receipt_with_id("rcpt-health-ro-chain-corruption");
    let seq = store
        .append_chio_receipt_returning_seq(&receipt)
        .test_unwrap();
    let checkpoint_kp = receipt_test_keypair();
    let checkpoint = build_checkpoint(
        1,
        seq,
        seq,
        &canonical_receipt_bytes(&store, seq, seq),
        &checkpoint_kp,
    )
    .test_unwrap();
    ReceiptStore::store_checkpoint(&store, &checkpoint).test_unwrap();

    // Corrupt the checkpoint chain: drop the immutability trigger and delete a
    // projection row so verify_checkpoint_chain_integrity fails on reopen.
    let connection = store.connection().test_unwrap();
    connection
        .execute_batch(
            r#"
            DROP TRIGGER IF EXISTS checkpoint_publication_metadata_reject_delete;
            DELETE FROM checkpoint_publication_metadata WHERE checkpoint_seq = 1;
            "#,
        )
        .test_unwrap();
    drop(connection);
    drop(store);

    let report = SqliteReceiptStore::receipt_store_health_read_only(&path).test_unwrap();
    assert!(
        !report.healthy,
        "a corrupt chain is not healthy: {report:?}"
    );
    assert!(
        report
            .checkpoint_error
            .as_deref()
            .unwrap_or_default()
            .contains("projection"),
        "the read-only sampler must attach the chain-integrity error: {report:?}"
    );
    assert_eq!(report.latest_checkpointed_entry_seq, 0);
    assert_eq!(report.latest_checkpoint_seq, None);
    // The uncheckpointed range must span the whole committed log so the watchdog
    // emits a large-backlog gauge rather than skipping the sample.
    assert_eq!(report.uncheckpointed_start_seq, Some(1));
    assert_eq!(report.uncheckpointed_end_seq, Some(seq));

    let _ = fs::remove_file(path);
}

#[test]
fn receipt_checkpoint_status_reports_extra_projection_drift() {
    let path = unique_db_path("chio-receipts-cp-status-extra-projection");
    let store = SqliteReceiptStore::open(&path).test_unwrap();
    let receipt = sample_receipt_with_id("rcpt-status-extra-projection");
    let seq = store
        .append_chio_receipt_returning_seq(&receipt)
        .test_unwrap();
    let checkpoint_kp = receipt_test_keypair();
    let checkpoint = build_checkpoint(
        1,
        seq,
        seq,
        &canonical_receipt_bytes(&store, seq, seq),
        &checkpoint_kp,
    )
    .test_unwrap();
    ReceiptStore::store_checkpoint(&store, &checkpoint).test_unwrap();

    let connection = store.connection().test_unwrap();
    connection
        .execute_batch(
            r#"
            PRAGMA foreign_keys = OFF;
            INSERT INTO checkpoint_tree_heads (
                checkpoint_seq,
                batch_start_seq,
                batch_end_seq,
                tree_size,
                merkle_root,
                issued_at,
                kernel_key,
                previous_checkpoint_sha256,
                statement_json,
                signature
            )
            SELECT
                999,
                batch_start_seq,
                batch_end_seq,
                tree_size,
                merkle_root,
                issued_at,
                kernel_key,
                previous_checkpoint_sha256,
                statement_json,
                signature
            FROM checkpoint_tree_heads
            WHERE checkpoint_seq = 1;
            PRAGMA foreign_keys = ON;
            "#,
        )
        .test_unwrap();

    let report = store.receipt_checkpoint_status(Some(10)).test_unwrap();
    assert!(!report.healthy);
    assert!(
        report
            .checkpoint_error
            .as_deref()
            .unwrap_or_default()
            .contains("projection drift"),
        "unexpected report: {report:?}"
    );

    let _ = fs::remove_file(path);
}

#[test]
fn store_checkpoint_rejects_mixed_receipt_signer_range() {
    let path = unique_db_path("chio-receipts-cp-mixed-signer-range");
    let store = SqliteReceiptStore::open(&path).test_unwrap();
    let first_keypair = receipt_test_keypair();
    let second_keypair = Keypair::from_seed(&[0x43; 32]);
    let first_receipt = sample_receipt_with_keypair("rcpt-mixed-signer-1", 1, &first_keypair);
    let second_receipt = sample_receipt_with_keypair("rcpt-mixed-signer-2", 2, &second_keypair);
    let first_seq = store
        .append_chio_receipt_returning_seq(&first_receipt)
        .test_unwrap();
    let second_seq = store
        .append_chio_receipt_returning_seq(&second_receipt)
        .test_unwrap();
    let checkpoint = build_checkpoint(
        1,
        first_seq,
        second_seq,
        &canonical_receipt_bytes(&store, first_seq, second_seq),
        &receipt_test_keypair(),
    )
    .test_unwrap();

    let error = ReceiptStore::store_checkpoint(&store, &checkpoint).test_unwrap_err();
    assert!(
        error.to_string().contains("mixed receipt signer"),
        "unexpected error: {error}"
    );

    let _ = fs::remove_file(path);
}

#[test]
fn store_checkpoint_rejects_checkpoint_key_that_does_not_match_receipt_signer() {
    let path = unique_db_path("chio-receipts-cp-wrong-kernel-key");
    let store = SqliteReceiptStore::open(&path).test_unwrap();
    let receipt = sample_receipt_with_id("rcpt-wrong-checkpoint-key");
    let seq = store
        .append_chio_receipt_returning_seq(&receipt)
        .test_unwrap();
    let checkpoint = build_checkpoint(
        1,
        seq,
        seq,
        &canonical_receipt_bytes(&store, seq, seq),
        &Keypair::from_seed(&[0x44; 32]),
    )
    .test_unwrap();

    let error = ReceiptStore::store_checkpoint(&store, &checkpoint).test_unwrap_err();
    assert!(
        error
            .to_string()
            .contains("does not match receipt signer key"),
        "unexpected error: {error}"
    );

    let _ = fs::remove_file(path);
}

#[test]
fn health_detects_invalid_uncheckpointed_receipt_bytes() {
    // `receipt_store_health()` scans the uncheckpointed range via
    // `load_claim_tree_canonical_bytes_range`, which decodes and
    // signature-verifies each receipt. Corrupting the canonical bytes
    // of a receipt that has not yet been checkpointed must surface as
    // an error out of `receipt_store_health()`. We bypass the
    // checkpointer entirely by never calling
    // `create_next_receipt_checkpoint`, leaving the appended receipts
    // in the uncheckpointed range.
    let path = unique_db_path("chio-receipts-health-uncheckpointed");
    let store = SqliteReceiptStore::open(&path).test_unwrap();

    let receipt = sample_receipt_with_id_and_timestamp("rcpt-health-tamper", 1);
    let seq = store
        .append_chio_receipt_returning_seq(&receipt)
        .test_unwrap();
    store.flush_receipt_writes().test_unwrap();

    // Sanity: with the receipt appended but no checkpoint produced, the
    // health probe must classify the receipt as uncheckpointed.
    let healthy = store.receipt_store_health().test_unwrap();
    assert!(
        healthy.healthy,
        "store must be healthy before tampering: {healthy:?}"
    );
    assert_eq!(healthy.latest_committed_entry_seq, seq);
    assert_eq!(healthy.latest_checkpointed_entry_seq, 0);
    assert_eq!(healthy.uncheckpointed_start_seq, Some(seq));
    assert_eq!(healthy.uncheckpointed_end_seq, Some(seq));

    tamper_canonical_bytes_of_uncheckpointed_receipt(&store, &receipt.id, |receipt| {
        receipt.tool_name = "sh".to_string();
    });

    let error = store.receipt_store_health().test_unwrap_err();
    let message = error.to_string();
    assert!(
        message.contains("invalid signature") || message.contains("verification failed"),
        "unexpected health error: {message}"
    );

    let _ = fs::remove_file(path);
}