chio-kernel 0.1.2

Chio runtime kernel: capability validation, guard evaluation, receipt signing
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
/// Retention and archival tests for SqliteReceiptStore.
///
/// These tests cover COMP-03 (configurable retention) and COMP-04 (archived
/// receipt verification). Archival removes only the whole checkpointed prefix
/// that has aged past the cutoff, co-archiving the claim-log projection and
/// deleting it together with the source rows. Tenant-scoped rotation is
/// rejected fail-closed. Per-table co-archival mechanics are unit-tested in the
/// store crate at
/// `crates/platform/chio-store-sqlite/src/receipt_store/tests/retention.rs`.
/// All tests use temporary files that are cleaned up after each test.
#[cfg(test)]
#[allow(clippy::expect_used, clippy::unwrap_used)]
mod retention {
    use std::fs;
    use std::time::{SystemTime, UNIX_EPOCH};

    use chio_core::capability::{
        scope::{ChioScope, Operation, ToolGrant},
        token::{CapabilityToken, CapabilityTokenBody},
    };
    use chio_core::crypto::Keypair;
    use chio_core::merkle::MerkleTree;
    use chio_core::receipt::{
        body::ChioReceipt, body::ChioReceiptBody, decision::Decision, decision::ToolCallAction,
        lineage::ChildRequestReceipt, lineage::ChildRequestReceiptBody,
    };
    use chio_core::session::{OperationKind, OperationTerminalState, RequestId, SessionId};

    use chio_kernel::build_checkpoint;
    use chio_kernel::build_checkpoint_with_previous;
    use chio_kernel::build_inclusion_proof;
    use chio_kernel::verify_checkpoint_signature;
    use chio_kernel::{ReceiptStore, RetentionConfig};
    use chio_store_sqlite::SqliteReceiptStore;

    fn unique_db_path(prefix: &str) -> std::path::PathBuf {
        let nonce = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("time before epoch")
            .as_nanos();
        std::env::temp_dir().join(format!("{prefix}-{nonce}.sqlite3"))
    }

    fn receipt_with_capability_and_ts(
        id: &str,
        capability_id: &str,
        timestamp: u64,
    ) -> ChioReceipt {
        let keypair = Keypair::generate();
        receipt_with_capability_ts_and_keypair(id, capability_id, timestamp, &keypair)
    }

    /// Sign a receipt with a caller-supplied keypair so a batch of receipts
    /// shares one signer. The checkpoint protocol requires every receipt in a
    /// checkpoint range to have the same `kernel_key` as the checkpoint itself,
    /// so tests that build a checkpoint over a receipt batch route through this
    /// helper.
    fn receipt_with_capability_ts_and_keypair(
        id: &str,
        capability_id: &str,
        timestamp: u64,
        keypair: &Keypair,
    ) -> ChioReceipt {
        let action = ToolCallAction::from_parameters(serde_json::json!({}))
            .expect("hash receipt parameters");
        ChioReceipt::sign(
            ChioReceiptBody {
                id: id.to_string(),
                timestamp,
                capability_id: capability_id.to_string(),
                tool_server: "shell".to_string(),
                tool_name: "bash".to_string(),
                action,
                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,
        )
        .expect("sign receipt")
    }

    fn receipt_with_ts(id: &str, timestamp: u64) -> ChioReceipt {
        receipt_with_capability_and_ts(id, "cap-1", timestamp)
    }

    fn receipt_with_ts_and_keypair(id: &str, timestamp: u64, keypair: &Keypair) -> ChioReceipt {
        receipt_with_capability_ts_and_keypair(id, "cap-1", timestamp, keypair)
    }

    fn child_receipt_with_ts_and_keypair(
        id: &str,
        timestamp: u64,
        keypair: &Keypair,
    ) -> ChildRequestReceipt {
        ChildRequestReceipt::sign(
            ChildRequestReceiptBody {
                id: id.to_string(),
                timestamp,
                session_id: SessionId::new("sess-retention"),
                parent_request_id: RequestId::new("parent-retention"),
                request_id: RequestId::new(format!("request-{id}")),
                operation_kind: OperationKind::CreateMessage,
                terminal_state: OperationTerminalState::Completed,
                outcome_hash: format!("outcome-{id}"),
                policy_hash: "policy-retention".to_string(),
                metadata: None,
                kernel_key: keypair.public_key(),
            },
            keypair,
        )
        .expect("sign child receipt")
    }

    fn capability_with_id(id: &str, subject: &Keypair, issuer: &Keypair) -> CapabilityToken {
        CapabilityToken::sign(
            CapabilityTokenBody {
                id: id.to_string(),
                issuer: issuer.public_key(),
                subject: subject.public_key(),
                scope: ChioScope {
                    grants: vec![ToolGrant {
                        server_id: "shell".to_string(),
                        tool_name: "bash".to_string(),
                        operations: vec![Operation::Invoke],
                        constraints: vec![],
                        max_invocations: None,
                        max_cost_per_invocation: None,
                        max_total_cost: None,
                        dpop_required: None,
                    }],
                    ..ChioScope::default()
                },
                issued_at: 100,
                expires_at: 10_000,
                delegation_chain: vec![],
                aggregate_invocation_budget: None,
            },
            issuer,
        )
        .expect("sign capability")
    }

    /// Checkpoint a receipt batch over the given entry_seq range on `store`,
    /// signed with `keypair` (all receipts in the range must share it).
    fn checkpoint_range(
        store: &SqliteReceiptStore,
        checkpoint_seq: u64,
        start_seq: u64,
        end_seq: u64,
        keypair: &Keypair,
        previous: Option<&chio_kernel::checkpoint::KernelCheckpoint>,
    ) -> chio_kernel::checkpoint::KernelCheckpoint {
        let canonical = store
            .receipts_canonical_bytes_range(start_seq, end_seq)
            .unwrap();
        let bytes: Vec<Vec<u8>> = canonical.iter().map(|(_, b)| b.clone()).collect();
        let checkpoint = match previous {
            None => build_checkpoint(checkpoint_seq, start_seq, end_seq, &bytes, keypair).unwrap(),
            Some(previous) => build_checkpoint_with_previous(
                checkpoint_seq,
                start_seq,
                end_seq,
                &bytes,
                keypair,
                Some(previous),
                &[chio_kernel::checkpoint::checkpoint_chain_leaf_hash(&previous.body).unwrap()],
            )
            .unwrap(),
        };
        store.store_checkpoint(&checkpoint).unwrap();
        checkpoint
    }

    /// Time-based rotation: the aged, checkpointed prefix is archived; the fresh
    /// uncheckpointed receipts remain in the live DB.
    #[test]
    fn retention_rotates_at_time_boundary() {
        let live_path = unique_db_path("retention-time-live");
        let archive_path = unique_db_path("retention-time-archive");

        let store = SqliteReceiptStore::open(&live_path).unwrap();
        let kp = Keypair::generate();

        // 10 aged receipts (timestamps 100-109), all signed by `kp` so a
        // checkpoint over the batch passes the signer-binding check.
        let mut seqs = Vec::new();
        for i in 0..10usize {
            let receipt =
                receipt_with_ts_and_keypair(&format!("rcpt-old-{i}"), 100 + i as u64, &kp);
            seqs.push(store.append_chio_receipt_returning_seq(&receipt).unwrap());
        }
        // Checkpoint the aged prefix [1, 10] so it can age out.
        checkpoint_range(&store, 1, seqs[0], seqs[9], &kp, None);

        // 5 fresh receipts (timestamps 200-204) stay uncheckpointed.
        for i in 0..5usize {
            let receipt = receipt_with_ts(&format!("rcpt-new-{i}"), 200 + i as u64);
            store.append_chio_receipt_returning_seq(&receipt).unwrap();
        }

        // Cutoff at timestamp 150: the entire checkpointed prefix has aged.
        let archived = store
            .archive_receipts_before(150, archive_path.to_str().unwrap())
            .unwrap();
        assert_eq!(
            archived, 10,
            "should archive the 10 checkpointed aged receipts"
        );

        // Live DB should have the 5 fresh receipts.
        assert_eq!(
            store.tool_receipt_count().unwrap(),
            5,
            "live DB should have 5 receipts after archival"
        );

        // Archive DB should have 10 receipts.
        let archive_store = SqliteReceiptStore::open(&archive_path).unwrap();
        assert_eq!(
            archive_store.tool_receipt_count().unwrap(),
            10,
            "archive DB should have 10 receipts"
        );

        let _ = fs::remove_file(&live_path);
        let _ = fs::remove_file(&archive_path);
    }

    /// Once a receipt id has been co-archived and deleted, re-appending it must
    /// be rejected: the live UNIQUE(receipt_id) sentinel is gone, but the
    /// archive already holds it, so accepting it as a new live receipt would
    /// overlap the archived and live histories and make a lookup by that id
    /// ambiguous.
    #[test]
    fn archived_receipt_id_cannot_be_reappended() {
        let live_path = unique_db_path("retention-reuse-live");
        let archive_path = unique_db_path("retention-reuse-archive");

        let store = SqliteReceiptStore::open(&live_path).unwrap();
        let kp = Keypair::generate();
        let mut seqs = Vec::new();
        for i in 0..3usize {
            let receipt = receipt_with_ts_and_keypair(&format!("reuse-{i}"), 100 + i as u64, &kp);
            seqs.push(store.append_chio_receipt_returning_seq(&receipt).unwrap());
        }
        checkpoint_range(&store, 1, seqs[0], seqs[2], &kp, None);

        let archived = store
            .archive_receipts_before(150, archive_path.to_str().unwrap())
            .unwrap();
        assert_eq!(archived, 3, "the checkpointed prefix archives");
        assert_eq!(store.tool_receipt_count().unwrap(), 0);

        // Replaying an archived id (a retry or a forged append) must fail rather
        // than silently insert a second live copy.
        let replay = receipt_with_ts_and_keypair("reuse-0", 100, &kp);
        assert!(
            store.append_chio_receipt_returning_seq(&replay).is_err(),
            "an archived receipt id must not be re-appended as a new live receipt"
        );
        assert_eq!(
            store.tool_receipt_count().unwrap(),
            0,
            "the rejected replay must not add an overlapping live row"
        );

        let _ = fs::remove_file(&live_path);
        let _ = fs::remove_file(&archive_path);
    }

    /// A repeated rotation at the same cutoff is a clean no-op: the watermark
    /// already covers the archived prefix, so nothing new is archived and the
    /// live store is untouched (monotonic watermark, idempotent re-run).
    #[test]
    fn retention_retry_is_idempotent() {
        let live_path = unique_db_path("retention-retry-live");
        let archive_path = unique_db_path("retention-retry-archive");

        let store = SqliteReceiptStore::open(&live_path).unwrap();
        let kp = Keypair::generate();
        let mut seqs = Vec::new();
        for i in 0..3usize {
            let receipt =
                receipt_with_ts_and_keypair(&format!("rcpt-retry-{i}"), 100 + i as u64, &kp);
            seqs.push(store.append_chio_receipt_returning_seq(&receipt).unwrap());
        }
        checkpoint_range(&store, 1, seqs[0], seqs[2], &kp, None);

        let first = store
            .archive_receipts_before(150, archive_path.to_str().unwrap())
            .unwrap();
        assert_eq!(first, 3, "first rotation archives the checkpointed prefix");
        assert_eq!(
            store.tool_receipt_count().unwrap(),
            0,
            "live rows are deleted after the first rotation"
        );

        let second = store
            .archive_receipts_before(150, archive_path.to_str().unwrap())
            .unwrap();
        assert_eq!(
            second, 0,
            "retry over an already-archived prefix is a no-op"
        );
        assert_eq!(store.tool_receipt_count().unwrap(), 0);

        let archive_store = SqliteReceiptStore::open(&archive_path).unwrap();
        assert_eq!(archive_store.tool_receipt_count().unwrap(), 3);

        let _ = fs::remove_file(&live_path);
        let _ = fs::remove_file(&archive_path);
    }

    /// Tenant-scoped retention is not expressible as a prefix watermark (the
    /// checkpoint-aligned watermark spans every tenant in a batch), so a
    /// rotation with `tenant_id: Some(..)` is rejected fail-closed with
    /// `RetentionTenantScopeUnsupported` and modifies nothing: no receipts
    /// are archived, no archive file is created, and the live store is left
    /// exactly as it was.
    #[test]
    fn tenant_scoped_rotation_is_rejected_and_leaves_counts_unchanged() {
        let live_path = unique_db_path("retention-tenant-rejected-live");
        let archive_path = unique_db_path("retention-tenant-rejected-archive");

        let store = SqliteReceiptStore::open(&live_path).unwrap();
        let kp = Keypair::generate();

        // Aged, checkpointed receipts that WOULD be eligible for a global
        // rotation, to prove the tenant-scoped rejection leaves them alone.
        let mut seqs = Vec::new();
        for i in 0..3usize {
            let receipt =
                receipt_with_ts_and_keypair(&format!("rcpt-tenant-{i}"), 100 + i as u64, &kp);
            seqs.push(store.append_chio_receipt_returning_seq(&receipt).unwrap());
        }
        checkpoint_range(&store, 1, seqs[0], seqs[2], &kp, None);

        let config = RetentionConfig {
            tenant_id: Some("tenant-a".to_string()),
            archive_path: archive_path.to_str().unwrap().to_string(),
            explicit_cutoff_unix_secs: Some(150),
            ..RetentionConfig::default()
        };

        let error = store.rotate_if_needed(&config);
        let message = error
            .expect_err("expected RetentionTenantScopeUnsupported")
            .to_string();
        assert!(
            message.contains("tenant-scoped retention"),
            "unexpected error: {message}"
        );

        // Fail-closed: nothing archived, live counts unchanged, no archive
        // file materializes.
        assert_eq!(
            store.tool_receipt_count().unwrap(),
            3,
            "tenant-scoped rejection must not modify the live store"
        );
        assert!(
            !archive_path.exists(),
            "tenant-scoped rejection must not create an archive file"
        );

        let _ = fs::remove_file(&live_path);
        let _ = fs::remove_file(&archive_path);
    }

    /// Size-based rotation: with the time threshold disabled, exceeding
    /// `max_size_bytes` rotates at the median timestamp, archiving the whole
    /// checkpointed prefix that has aged past that median.
    #[test]
    fn retention_rotates_at_size_boundary() {
        let live_path = unique_db_path("retention-size-live");
        let archive_path = unique_db_path("retention-size-archive");

        let store = SqliteReceiptStore::open(&live_path).unwrap();
        let kp = Keypair::generate();

        // Insert 100 receipts (timestamps 1000-1099), all signed by `kp`.
        let mut seqs = Vec::new();
        for i in 0..100usize {
            let receipt =
                receipt_with_ts_and_keypair(&format!("rcpt-sz-{i}"), 1000 + i as u64, &kp);
            seqs.push(store.append_chio_receipt_returning_seq(&receipt).unwrap());
        }
        // Two checkpoints of 50 each: [1, 50] and [51, 100]. The median cutoff
        // (timestamp 1050) fully ages the first batch only.
        let cp1 = checkpoint_range(&store, 1, seqs[0], seqs[49], &kp, None);
        checkpoint_range(&store, 2, seqs[50], seqs[99], &kp, Some(&cp1));

        let current_size = store.db_size_bytes().unwrap();
        assert!(current_size > 0, "DB should have nonzero size");

        // retention_days = u64::MAX disables the time threshold (time_cutoff
        // saturates to 0), so only the size threshold can trigger.
        let config = RetentionConfig {
            retention_days: u64::MAX,
            max_size_bytes: current_size.saturating_sub(1),
            archive_path: archive_path.to_str().unwrap().to_string(),
            tenant_id: None,
            explicit_cutoff_unix_secs: None,
            ..RetentionConfig::default()
        };

        let archived = store.rotate_if_needed(&config).unwrap();
        assert_eq!(
            archived, 50,
            "size-triggered rotation archives the first checkpointed batch"
        );

        let remaining = store.tool_receipt_count().unwrap();
        assert_eq!(
            remaining, 50,
            "live DB retains the un-aged second checkpointed batch"
        );

        let _ = fs::remove_file(&live_path);
        let _ = fs::remove_file(&archive_path);
    }

    /// Archived receipts must be verifiable against the checkpoint roots stored
    /// in the archive database (COMP-04).
    #[test]
    fn archived_receipt_verifies_against_checkpoint() {
        let live_path = unique_db_path("retention-verify-live");
        let archive_path = unique_db_path("retention-verify-archive");

        let store = SqliteReceiptStore::open(&live_path).unwrap();
        let kp = Keypair::generate();

        // Insert 10 receipts with timestamp < 500. Every receipt is signed with
        // `kp` so the checkpoint's signer-binding check passes.
        let mut seqs = Vec::new();
        for i in 0..10usize {
            let receipt =
                receipt_with_ts_and_keypair(&format!("rcpt-verify-{i}"), 100 + i as u64, &kp);
            let seq = store.append_chio_receipt_returning_seq(&receipt).unwrap();
            seqs.push(seq);
        }

        // Build a Merkle checkpoint over those 10 receipts using canonical bytes.
        let cp = checkpoint_range(&store, 1, seqs[0], seqs[9], &kp, None);

        // Archive all 10 receipts (cutoff = 500, all timestamps < 500).
        let archived = store
            .archive_receipts_before(500, archive_path.to_str().unwrap())
            .unwrap();
        assert_eq!(archived, 10, "should archive all 10 receipts");

        // Open archive DB and load the checkpoint.
        let archive_store = SqliteReceiptStore::open(&archive_path).unwrap();
        let loaded_cp = archive_store
            .load_checkpoint_by_seq(1)
            .unwrap()
            .expect("checkpoint should be in archive");
        assert_eq!(loaded_cp.body.merkle_root, cp.body.merkle_root);

        // Verify the checkpoint signature.
        assert!(
            verify_checkpoint_signature(&loaded_cp).unwrap(),
            "checkpoint signature should verify in archive"
        );

        // Load canonical bytes from archive and verify inclusion proof.
        let archive_canonical = archive_store
            .receipts_canonical_bytes_range(seqs[0], seqs[9])
            .unwrap();
        assert_eq!(
            archive_canonical.len(),
            10,
            "archive should contain all 10 receipts"
        );

        // Build a Merkle tree from the archived bytes and verify inclusion.
        let archived_bytes: Vec<Vec<u8>> =
            archive_canonical.iter().map(|(_, b)| b.clone()).collect();
        let tree = MerkleTree::from_leaves(&archived_bytes).unwrap();

        let proof = build_inclusion_proof(&tree, 0, 1, seqs[0]).unwrap();
        assert!(
            proof.verify(&archived_bytes[0], &loaded_cp.body.merkle_root),
            "receipt 0 inclusion proof should verify against archived checkpoint root"
        );

        let _ = fs::remove_file(&live_path);
        let _ = fs::remove_file(&archive_path);
    }

    /// Archiving batch 1 copies batch 1's checkpoint row into the archive but
    /// leaves batch 2's checkpoint in the live DB (batch 2 has not aged).
    #[test]
    fn archive_preserves_checkpoint_rows() {
        let live_path = unique_db_path("retention-cp-rows-live");
        let archive_path = unique_db_path("retention-cp-rows-archive");

        let store = SqliteReceiptStore::open(&live_path).unwrap();
        let kp = Keypair::generate();

        // Insert 10 receipts for batch 1 (timestamps 100-109).
        let mut batch1_seqs = Vec::new();
        for i in 0..10usize {
            let receipt =
                receipt_with_ts_and_keypair(&format!("rcpt-batch1-{i}"), 100 + i as u64, &kp);
            batch1_seqs.push(store.append_chio_receipt_returning_seq(&receipt).unwrap());
        }
        let cp1 = checkpoint_range(&store, 1, batch1_seqs[0], batch1_seqs[9], &kp, None);

        // Insert 10 receipts for batch 2 (timestamps 200-209).
        let mut batch2_seqs = Vec::new();
        for i in 0..10usize {
            let receipt =
                receipt_with_ts_and_keypair(&format!("rcpt-batch2-{i}"), 200 + i as u64, &kp);
            batch2_seqs.push(store.append_chio_receipt_returning_seq(&receipt).unwrap());
        }
        checkpoint_range(&store, 2, batch2_seqs[0], batch2_seqs[9], &kp, Some(&cp1));

        // Archive only batch 1 receipts (cutoff = 150, timestamps 100-109 < 150).
        let archived = store
            .archive_receipts_before(150, archive_path.to_str().unwrap())
            .unwrap();
        assert_eq!(archived, 10, "should archive 10 receipts from batch 1");

        // Archive DB should have batch 1's checkpoint row.
        let archive_store = SqliteReceiptStore::open(&archive_path).unwrap();
        assert!(
            archive_store.load_checkpoint_by_seq(1).unwrap().is_some(),
            "archive DB should have batch 1 checkpoint"
        );

        // Archive DB should NOT have batch 2's checkpoint row.
        assert!(
            archive_store.load_checkpoint_by_seq(2).unwrap().is_none(),
            "archive DB should NOT have batch 2 checkpoint"
        );

        // Live DB should still have batch 2's checkpoint row (rotation never
        // deletes checkpoint rows from the live store).
        assert!(
            store.load_checkpoint_by_seq(2).unwrap().is_some(),
            "live DB should still have batch 2 checkpoint"
        );

        let _ = fs::remove_file(&live_path);
        let _ = fs::remove_file(&archive_path);
    }

    /// With no checkpoints there is no aged checkpointed prefix, so archival is
    /// a fail-safe no-op: nothing is archived and the live store is untouched.
    #[test]
    fn archive_with_no_checkpoints_is_a_noop() {
        let live_path = unique_db_path("retention-no-cp-live");
        let archive_path = unique_db_path("retention-no-cp-archive");

        let store = SqliteReceiptStore::open(&live_path).unwrap();

        // Insert 5 receipts, but store NO checkpoints.
        for i in 0..5usize {
            let receipt = receipt_with_ts(&format!("rcpt-no-cp-{i}"), 100 + i as u64);
            store.append_chio_receipt_returning_seq(&receipt).unwrap();
        }
        assert!(
            store.load_checkpoint_by_seq(1).unwrap().is_none(),
            "should have no checkpoints before archive"
        );

        // archive_receipts_before is a no-op with no checkpointed prefix.
        let archived = store
            .archive_receipts_before(500, archive_path.to_str().unwrap())
            .unwrap();
        assert_eq!(archived, 0, "no checkpointed prefix has aged");

        // Live DB is untouched.
        assert_eq!(
            store.tool_receipt_count().unwrap(),
            5,
            "live DB retains every receipt (nothing archived)"
        );

        // The archive file was never created; opening it yields an empty store.
        let archive_store = SqliteReceiptStore::open(&archive_path).unwrap();
        assert_eq!(archive_store.tool_receipt_count().unwrap(), 0);

        let _ = fs::remove_file(&live_path);
        let _ = fs::remove_file(&archive_path);
    }

    /// A checkpointed tool+child prefix co-archives both receipt kinds and
    /// deletes them together from the live store.
    #[test]
    fn archive_copies_and_deletes_child_receipts() {
        let live_path = unique_db_path("retention-child-live");
        let archive_path = unique_db_path("retention-child-archive");

        let store = SqliteReceiptStore::open(&live_path).unwrap();
        let kp = Keypair::generate();
        // Parent tool receipt (entry_seq 1) and child receipt (entry_seq 2),
        // both signed by `kp` so the checkpoint over [1, 2] binds one signer.
        store
            .append_chio_receipt_returning_seq(&receipt_with_ts_and_keypair(
                "rcpt-parent",
                100,
                &kp,
            ))
            .unwrap();
        store
            .append_child_receipt(&child_receipt_with_ts_and_keypair("child-parent", 100, &kp))
            .unwrap();
        checkpoint_range(&store, 1, 1, 2, &kp, None);

        let archived = store
            .archive_receipts_before(500, archive_path.to_str().unwrap())
            .unwrap();
        assert_eq!(
            archived, 1,
            "tool receipt archival count should remain stable"
        );
        assert_eq!(store.child_receipt_count().unwrap(), 0);

        let archive_store = SqliteReceiptStore::open(&archive_path).unwrap();
        assert_eq!(archive_store.child_receipt_count().unwrap(), 1);

        let _ = fs::remove_file(&live_path);
        let _ = fs::remove_file(&archive_path);
    }

    /// Archiving a checkpointed receipt co-archives its capability lineage while
    /// the live lineage snapshot remains (rotation never deletes lineage rows).
    #[test]
    fn archive_copies_capability_lineage_for_archived_receipts() {
        let live_path = unique_db_path("retention-lineage-live");
        let archive_path = unique_db_path("retention-lineage-archive");

        let store = SqliteReceiptStore::open(&live_path).unwrap();
        let issuer = Keypair::generate();
        let subject = Keypair::generate();
        let kp = Keypair::generate();
        let capability = capability_with_id("cap-retention-lineage", &subject, &issuer);
        store.record_capability_snapshot(&capability, None).unwrap();
        store
            .append_chio_receipt_returning_seq(&receipt_with_capability_ts_and_keypair(
                "rcpt-lineage",
                &capability.id,
                100,
                &kp,
            ))
            .unwrap();
        checkpoint_range(&store, 1, 1, 1, &kp, None);

        let archived = store
            .archive_receipts_before(500, archive_path.to_str().unwrap())
            .unwrap();
        assert_eq!(archived, 1);

        let archive_store = SqliteReceiptStore::open(&archive_path).unwrap();
        let archived_lineage = archive_store
            .get_lineage("cap-retention-lineage")
            .unwrap()
            .expect("archived lineage snapshot");
        assert_eq!(archived_lineage.subject_key, subject.public_key().to_hex());

        let live_lineage = store
            .get_lineage("cap-retention-lineage")
            .unwrap()
            .expect("live lineage snapshot should remain");
        assert_eq!(live_lineage.issuer_key, issuer.public_key().to_hex());

        let _ = fs::remove_file(&live_path);
        let _ = fs::remove_file(&archive_path);
    }

    #[test]
    fn kernel_maintenance_task_rotates_when_configured() {
        use std::sync::Arc;
        let live = unique_db_path("kernel-maint-live");
        let archive = unique_db_path("kernel-maint-archive");
        let keypair = Keypair::generate();
        let store = SqliteReceiptStore::open(&live).unwrap();
        // `SqliteReceiptStore` also has an inherent `enable_background_checkpoints`
        // that takes a `BackgroundCheckpointSigner` (not exported from this
        // integration-test crate); UFCS pins the call to the public
        // `ReceiptStore` trait method (`keypair`, `max_batch`) instead.
        <SqliteReceiptStore as ReceiptStore>::enable_background_checkpoints(
            &store,
            keypair.clone(),
            2,
        )
        .unwrap();
        // Every receipt in a checkpointed range must share the checkpoint's
        // kernel key, so all four receipts are signed with the same keypair
        // installed as the background checkpoint signer above.
        for i in 0..4u64 {
            let r =
                receipt_with_capability_ts_and_keypair(&format!("km-{i}"), "cap", 100, &keypair);
            store.append_chio_receipt_returning_seq(&r).unwrap();
        }
        store.flush_receipt_writes().unwrap();

        // Directly exercise the trait entry point the maintenance task calls
        // (the task itself is a timed loop; here we assert the wiring closes).
        let handle: Arc<dyn ReceiptStore> = Arc::new(store);
        let config = RetentionConfig {
            retention_days: 0,
            check_interval_secs: 1,
            archive_path: archive.to_str().unwrap().to_string(),
            explicit_cutoff_unix_secs: Some(1_000),
            ..RetentionConfig::default()
        };
        let archived = handle.rotate_receipts(&config).unwrap();
        assert_eq!(
            archived, 4,
            "the maintenance entry point archived the aged prefix"
        );
        assert!(handle.receipt_store_health().unwrap().healthy);

        let _ = fs::remove_file(&live);
        let _ = fs::remove_file(&archive);
    }
}