arkhe-kernel 0.15.0

Domain-neutral deterministic microkernel for virtual worlds. WAL-backed, bit-identical replay, invariant-lifetime shell brand, no async / no unsafe / no floating-point in canonical paths.
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
//! WAL → Kernel replay (A1 D1-Total bit-identical reconstruction).
//!
//! Replay is the from-fresh-state path: the caller re-creates the
//! instances referenced by the WAL with matching configs before calling
//! `replay_into`. The snapshot path (`KernelSnapshot` plus
//! `Kernel::from_snapshot`) is the alternative — restore from a
//! point-in-time blob without re-running history.

use crate::abi::{CapabilityMask, InstanceId};
use crate::runtime::Kernel;

use super::wal::{
    StepVerdict, TrustAnchor, Wal, WalError, WalHeader, WalRecordContent, WalWriter,
};

/// Aggregated outcome of [`replay_into`].
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct ReplayReport {
    /// Number of `Submit` records re-admitted.
    pub submits_replayed: u32,
    /// Number of `Step` records re-executed.
    pub steps_replayed: u32,
    /// Sum of `effects_applied` across all replayed steps.
    pub total_effects_applied: u32,
    /// Sum of `effects_denied` across all replayed steps.
    pub total_effects_denied: u32,
    /// Chain tip MEASURED by re-sealing the replayed records through a
    /// header-rebuilt writer (matches the sealed WAL's tip when the replay is
    /// bit-identical — it is a recomputed witness, never a copy of the tip).
    pub final_chain_tip: [u8; 32],
}

/// Failure modes for [`replay_into`].
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum ReplayError {
    /// WAL header magic doesn't match `WalHeader::MAGIC`.
    HeaderIncompatible(String),
    /// `kernel_semver` differs between WAL header and the running kernel.
    KernelSemverMismatch {
        /// Semver pinned in the WAL header.
        expected: (u16, u16, u16),
        /// Current running kernel semver.
        got: (u16, u16, u16),
    },
    /// `abi_version` differs between WAL header and the running kernel.
    AbiVersionMismatch {
        /// ABI version pinned in the WAL header.
        expected: (u16, u16),
        /// Current running kernel ABI version.
        got: (u16, u16),
    },
    /// `postcard_version` differs between WAL header and the running build.
    PostcardVersionMismatch {
        /// Postcard major pinned in the WAL header.
        expected: u32,
        /// Current running postcard major.
        got: u32,
    },
    /// `blake3_version` differs between WAL header and the running build.
    Blake3VersionMismatch {
        /// BLAKE3 major pinned in the WAL header.
        expected: u32,
        /// Current running BLAKE3 major.
        got: u32,
    },
    /// Underlying WAL chain/signature verification failure.
    WalCorrupted(WalError),
    /// `Kernel::submit_with_id` failed during replay (carries the formatted
    /// upstream error).
    SubmitFailed(String),
    /// A `Step` record was reached but the instance had no due action to pop
    /// — the re-derived scheduler diverged from the recorded history.
    StepUnderflow {
        /// Instance the absent step targeted.
        instance: InstanceId,
    },
    /// A re-executed step popped a different `ScheduledActionId` than recorded
    /// — the re-derived scheduler order diverged.
    PoppedIdDivergence {
        /// Sequence of the offending `Step` record.
        seq: u64,
    },
    /// A re-executed step reached a different verdict than recorded — the
    /// re-derived authorization/budget outcome diverged.
    VerdictDivergence {
        /// Sequence of the offending `Step` record.
        seq: u64,
        /// Verdict pinned in the WAL.
        expected: StepVerdict,
        /// Verdict re-derived on replay.
        got: StepVerdict,
    },
    /// A re-executed step produced a different full-state digest than recorded
    /// — the re-derived state diverged bit-for-bit (A1 failure).
    StateDigestDivergence {
        /// Sequence of the offending `Step` record.
        seq: u64,
    },
    /// The chain tip re-measured from the replayed records does not equal the
    /// sealed WAL's tip — the replay was not bit-identical end-to-end.
    ChainTipDivergence {
        /// Tip sealed in the source WAL.
        expected: [u8; 32],
        /// Tip re-measured from the replayed records.
        measured: [u8; 32],
    },
    /// The WAL header's `manifest_digest` does not equal the replaying
    /// kernel's declared manifest (A14) — the WAL was written under a
    /// different `ModuleManifest`. Closes the otherwise-vacuous default-path
    /// manifest gate without requiring a `TrustAnchor`.
    ManifestDigestMismatch {
        /// Digest pinned in the WAL header.
        expected: [u8; 32],
        /// Digest the replaying kernel declares.
        got: [u8; 32],
    },
}

impl From<WalError> for ReplayError {
    fn from(e: WalError) -> Self {
        Self::WalCorrupted(e)
    }
}

impl core::fmt::Display for ReplayError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::HeaderIncompatible(m) => write!(f, "wal header incompatible: {}", m),
            Self::KernelSemverMismatch { expected, got } => {
                write!(
                    f,
                    "kernel semver mismatch: expected {:?}, got {:?}",
                    expected, got
                )
            }
            Self::AbiVersionMismatch { expected, got } => {
                write!(
                    f,
                    "abi version mismatch: expected {:?}, got {:?}",
                    expected, got
                )
            }
            Self::PostcardVersionMismatch { expected, got } => {
                write!(
                    f,
                    "postcard version mismatch: expected {}, got {}",
                    expected, got
                )
            }
            Self::Blake3VersionMismatch { expected, got } => {
                write!(f, "blake3 version mismatch: expected {}, got {}", expected, got)
            }
            Self::WalCorrupted(e) => write!(f, "wal corrupted: {}", e),
            Self::SubmitFailed(m) => write!(f, "submit failed: {}", m),
            Self::StepUnderflow { instance } => {
                write!(f, "replay step underflow: no due action for {:?}", instance)
            }
            Self::PoppedIdDivergence { seq } => {
                write!(f, "replay popped-id divergence at record {}", seq)
            }
            Self::VerdictDivergence { seq, expected, got } => write!(
                f,
                "replay verdict divergence at record {}: expected {:?}, got {:?}",
                seq, expected, got
            ),
            Self::StateDigestDivergence { seq } => {
                write!(f, "replay state-digest divergence at record {}", seq)
            }
            Self::ChainTipDivergence { expected, measured } => write!(
                f,
                "replay chain-tip divergence: expected {}, measured {}",
                blake3::Hash::from(*expected).to_hex(),
                blake3::Hash::from(*measured).to_hex()
            ),
            Self::ManifestDigestMismatch { expected, got } => write!(
                f,
                "replay manifest_digest mismatch: wal {}, kernel {}",
                blake3::Hash::from(*expected).to_hex(),
                blake3::Hash::from(*got).to_hex()
            ),
        }
    }
}

impl std::error::Error for ReplayError {}

/// Header-compatibility gates shared by [`replay_into`] and
/// [`replay_into_verified`] (A14): magic, kernel semver major, ABI
/// version, postcard / BLAKE3 major. A mismatch is a structural error.
fn check_header_gates(wal: &Wal) -> Result<(), ReplayError> {
    if wal.header.magic != WalHeader::MAGIC {
        return Err(ReplayError::HeaderIncompatible(
            "magic mismatch (expected ARKHEWAL)".to_string(),
        ));
    }
    if wal.header.kernel_semver.0 != WalHeader::CURRENT_KERNEL_SEMVER.0 {
        return Err(ReplayError::KernelSemverMismatch {
            expected: WalHeader::CURRENT_KERNEL_SEMVER,
            got: wal.header.kernel_semver,
        });
    }
    if wal.header.abi_version != WalHeader::ABI_VERSION {
        return Err(ReplayError::AbiVersionMismatch {
            expected: WalHeader::ABI_VERSION,
            got: wal.header.abi_version,
        });
    }
    // postcard / BLAKE3 major versions are wire-format determinants (A14
    // header pinning): a mismatch means the bytes were produced under a
    // different codec/hash generation and must NOT be silently accepted.
    if wal.header.postcard_version != WalHeader::POSTCARD_MAJOR {
        return Err(ReplayError::PostcardVersionMismatch {
            expected: WalHeader::POSTCARD_MAJOR,
            got: wal.header.postcard_version,
        });
    }
    if wal.header.blake3_version != WalHeader::BLAKE3_MAJOR {
        return Err(ReplayError::Blake3VersionMismatch {
            expected: WalHeader::BLAKE3_MAJOR,
            got: wal.header.blake3_version,
        });
    }
    Ok(())
}

/// Replay the (already chain-verified) Canonical Input Log into `kernel`.
///
/// Each `Submit` is re-admitted under its exact recorded id and ceiling; each
/// `Step` is re-executed via the SAME per-instance driver the live kernel
/// uses ([`Kernel::process_instance_record`]). Replay applies NO effect from
/// the log and COPIES no hash: it re-derives every deterministic effect by
/// re-execution, asserts each step's popped-id / verdict / full-state digest
/// against the record (fail-fast tripwires), and re-seals the records through
/// a header-rebuilt writer to MEASURE the chain tip — which must equal the
/// sealed WAL's tip. This is the structural guarantee that internal
/// scheduling, signal routing, and id allocation cannot drift on replay.
fn replay_records(kernel: &mut Kernel, wal: &Wal) -> Result<ReplayReport, ReplayError> {
    let mut report = ReplayReport::default();
    // The replay-side measurement writer: same world_id-derived chain key,
    // None signing (the chain hash is over the body, which excludes
    // signatures, so an unsigned re-seal reproduces every `this_chain_hash`).
    let mut writer = WalWriter::rebuild_from_header(&wal.header);

    for rec in &wal.records {
        match &rec.content {
            WalRecordContent::Submit {
                seq: _,
                instance,
                principal,
                actor,
                caps_at_submit,
                at,
                action_type_code,
                action_bytes,
                allocated_id,
            } => {
                let caps = CapabilityMask::from_bits_retain(*caps_at_submit);
                kernel
                    .submit_with_id(
                        *instance,
                        principal.clone(),
                        *actor,
                        caps,
                        *allocated_id,
                        *at,
                        *action_type_code,
                        action_bytes.clone(),
                    )
                    .map_err(|e| ReplayError::SubmitFailed(format!("{:?}", e)))?;
                writer.append_submit(
                    *instance,
                    principal.clone(),
                    *actor,
                    *caps_at_submit,
                    *at,
                    *action_type_code,
                    action_bytes.clone(),
                    *allocated_id,
                )?;
                report.submits_replayed = report.submits_replayed.saturating_add(1);
            }
            WalRecordContent::Step {
                seq,
                instance,
                popped_id,
                now,
                session_caps,
                verdict,
                post_state_digest,
            } => {
                let session = CapabilityMask::from_bits_retain(*session_caps);
                let result = kernel
                    .process_instance_record(*instance, *now, session, true)
                    .ok_or(ReplayError::StepUnderflow {
                        instance: *instance,
                    })?;
                // Tripwires — replay must re-reach the recorded facts exactly.
                if result.popped_id != *popped_id {
                    return Err(ReplayError::PoppedIdDivergence { seq: *seq });
                }
                if result.verdict != *verdict {
                    return Err(ReplayError::VerdictDivergence {
                        seq: *seq,
                        expected: *verdict,
                        got: result.verdict,
                    });
                }
                if result.digest != *post_state_digest {
                    return Err(ReplayError::StateDigestDivergence { seq: *seq });
                }
                writer.append_step(
                    *instance,
                    *popped_id,
                    *now,
                    *session_caps,
                    *verdict,
                    *post_state_digest,
                )?;
                report.steps_replayed = report.steps_replayed.saturating_add(1);
                report.total_effects_applied = report
                    .total_effects_applied
                    .saturating_add(result.effects_applied);
                report.total_effects_denied = report
                    .total_effects_denied
                    .saturating_add(result.effects_denied);
            }
        }
    }

    // The tip is MEASURED from the re-sealed records, then checked against the
    // sealed WAL — never copied from it.
    let measured = writer.chain_tip();
    let sealed = wal.chain_tip();
    if measured != sealed {
        return Err(ReplayError::ChainTipDivergence {
            expected: sealed,
            measured,
        });
    }
    report.final_chain_tip = measured;
    Ok(report)
}

/// Replay every record into `kernel` (integrity-only). The caller must
/// already have created the instances referenced by the WAL; for the
/// integrated path (no manual pre-creation), use `Kernel::from_snapshot`
/// against a `KernelSnapshot` instead.
///
/// This verifies the chain's internal self-consistency but TRUSTS the
/// WAL's provenance — it derives the verification policy/keys and the
/// chain `world_id` from the (potentially attacker-controlled) header. For
/// an untrusted WAL (tampered log / peer snapshot), use
/// [`replay_into_verified`] with a [`TrustAnchor`].
pub fn replay_into(kernel: &mut Kernel, wal: &Wal) -> Result<ReplayReport, ReplayError> {
    check_header_gates(wal)?;
    // A14 default-path manifest gate: the WAL must have been written under the
    // same `ModuleManifest` the replaying kernel declares. This is a
    // self-consistency check (no `TrustAnchor` required) — for untrusted WAL
    // bytes, `replay_into_verified` additionally pins keys/tier/tip.
    if wal.header.manifest_digest != kernel.manifest_digest() {
        return Err(ReplayError::ManifestDigestMismatch {
            expected: wal.header.manifest_digest,
            got: kernel.manifest_digest(),
        });
    }
    wal.verify_chain(wal.header.world_id)?;
    replay_records(kernel, wal)
}

/// Replay every record into `kernel`, authenticating the WAL against a
/// caller-supplied [`TrustAnchor`] and a caller-supplied `world_id` (NOT
/// read from the untrusted header). Rejects a tier downgrade, a
/// verifying-key substitution, a manifest mismatch, and a tail truncation
/// before any record is applied. Use this for WAL bytes from an untrusted
/// source.
pub fn replay_into_verified(
    kernel: &mut Kernel,
    wal: &Wal,
    world_id: [u8; 32],
    anchor: &TrustAnchor,
) -> Result<ReplayReport, ReplayError> {
    check_header_gates(wal)?;
    wal.verify_chain_anchored(world_id, anchor)?;
    replay_records(kernel, wal)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::abi::{EntityId, ExternalId, InstanceId, Principal, Tick, TypeCode};
    use crate::persist::wal::{SkipReason, Wal, WalWriter};
    use crate::runtime::Kernel;
    use crate::state::traits::_sealed::Sealed;
    use crate::state::{
        Action, ActionCompute, ActionContext, ActionDeriv, InstanceConfig, Op,
    };
    use bytes::Bytes;
    use serde::{Deserialize, Serialize};

    fn world() -> [u8; 32] {
        [11u8; 32]
    }

    /// Instance config that grants System full authority (SendSignal /
    /// ScheduleAction need SYSTEM in the resolved effective caps — there is no
    /// blanket System bypass).
    fn priv_cfg() -> InstanceConfig {
        InstanceConfig {
            default_caps: CapabilityMask::SYSTEM,
            max_entities: 1024,
            max_scheduled: 1024,
            max_inbox_per_route: 16,
            memory_budget_bytes: 1 << 20,
            ..Default::default()
        }
    }

    // ---- Test actions ----

    /// Spawns one entity with a fixed id. Basic op (no SYSTEM needed).
    #[derive(Serialize, Deserialize)]
    struct SpawnAction {
        id: u64,
    }
    impl Sealed for SpawnAction {}
    impl ActionDeriv for SpawnAction {
        const TYPE_CODE: TypeCode = TypeCode(100);
        const SCHEMA_VERSION: u32 = 1;
    }
    impl ActionCompute for SpawnAction {
        fn compute(&self, _ctx: &ActionContext) -> Vec<Op> {
            vec![Op::SpawnEntity {
                id: EntityId::new(self.id).unwrap(),
                owner: Principal::System,
            }]
        }
    }

    /// Schedules a child `SpawnAction` for a future tick — an INTERNAL
    /// `Op::ScheduleAction` that is re-derived on replay (never logged as a
    /// Submit). The regression target for the double-execution bug.
    #[derive(Serialize, Deserialize)]
    struct ScheduleChildAction {
        child_id: u64,
        at: u64,
    }
    impl Sealed for ScheduleChildAction {}
    impl ActionDeriv for ScheduleChildAction {
        const TYPE_CODE: TypeCode = TypeCode(200);
        const SCHEMA_VERSION: u32 = 1;
    }
    impl ActionCompute for ScheduleChildAction {
        fn compute(&self, _ctx: &ActionContext) -> Vec<Op> {
            let child = SpawnAction { id: self.child_id };
            vec![Op::ScheduleAction {
                at: Tick(self.at),
                actor: None,
                action_type_code: SpawnAction::TYPE_CODE,
                action_bytes: Bytes::from(Action::canonical_bytes(&child)),
            }]
        }
    }

    /// Sends a signal to `target` on a fixed route. Needs SYSTEM.
    #[derive(Serialize, Deserialize)]
    struct SignalAction {
        target: u64,
    }
    impl Sealed for SignalAction {}
    impl ActionDeriv for SignalAction {
        const TYPE_CODE: TypeCode = TypeCode(300);
        const SCHEMA_VERSION: u32 = 1;
    }
    impl ActionCompute for SignalAction {
        fn compute(&self, _ctx: &ActionContext) -> Vec<Op> {
            vec![Op::SendSignal {
                target: InstanceId::new(self.target).unwrap(),
                route: crate::abi::RouteId(1),
                payload: Bytes::from_static(b"ping"),
            }]
        }
    }

    /// Two SetComponents on one entity; the second can exceed a tight budget
    /// (per-Op budget skip → `BudgetPartial`).
    #[derive(Serialize, Deserialize)]
    struct TwoSetAction {
        a: u64,
        b: u64,
    }
    impl Sealed for TwoSetAction {}
    impl ActionDeriv for TwoSetAction {
        const TYPE_CODE: TypeCode = TypeCode(400);
        const SCHEMA_VERSION: u32 = 1;
    }
    impl ActionCompute for TwoSetAction {
        fn compute(&self, _ctx: &ActionContext) -> Vec<Op> {
            let e = EntityId::new(1).unwrap();
            vec![
                Op::SpawnEntity {
                    id: e,
                    owner: Principal::System,
                },
                Op::SetComponent {
                    entity: e,
                    type_code: TypeCode(7),
                    bytes: Bytes::from(vec![0u8; self.a as usize]),
                    size: self.a,
                },
                Op::SetComponent {
                    entity: e,
                    type_code: TypeCode(8),
                    bytes: Bytes::from(vec![0u8; self.b as usize]),
                    size: self.b,
                },
            ]
        }
    }

    /// Emits a single `Op::ScheduleAction` (needs SYSTEM) — used to force an
    /// AuthDenied verdict under External caps.
    #[derive(Serialize, Deserialize)]
    struct ScheduleNeedsSystemAction;
    impl Sealed for ScheduleNeedsSystemAction {}
    impl ActionDeriv for ScheduleNeedsSystemAction {
        const TYPE_CODE: TypeCode = TypeCode(500);
        const SCHEMA_VERSION: u32 = 1;
    }
    impl ActionCompute for ScheduleNeedsSystemAction {
        fn compute(&self, _ctx: &ActionContext) -> Vec<Op> {
            vec![Op::ScheduleAction {
                at: Tick(9),
                actor: None,
                action_type_code: SpawnAction::TYPE_CODE,
                action_bytes: Bytes::from_static(b""),
            }]
        }
    }

    // ---- header-gate tests (empty WAL) ----

    #[test]
    fn replay_empty_wal_succeeds() {
        let w = WalWriter::new(world(), [0u8; 32]);
        let wal = Wal::from_writer(w);
        let mut kernel = Kernel::new();
        let report = replay_into(&mut kernel, &wal).unwrap();
        assert_eq!(report.submits_replayed, 0);
        assert_eq!(report.steps_replayed, 0);
    }

    #[test]
    fn replay_rejects_wrong_magic() {
        let w = WalWriter::new(world(), [0u8; 32]);
        let mut wal = Wal::from_writer(w);
        wal.header.magic = *b"BADMAGIC";
        let mut kernel = Kernel::new();
        assert!(matches!(
            replay_into(&mut kernel, &wal),
            Err(ReplayError::HeaderIncompatible(_))
        ));
    }

    #[test]
    fn replay_rejects_kernel_semver_major_mismatch() {
        let w = WalWriter::new(world(), [0u8; 32]);
        let mut wal = Wal::from_writer(w);
        wal.header.kernel_semver = (99, 0, 0);
        let mut kernel = Kernel::new();
        assert!(matches!(
            replay_into(&mut kernel, &wal),
            Err(ReplayError::KernelSemverMismatch { .. })
        ));
    }

    #[test]
    fn replay_rejects_postcard_version_mismatch() {
        let w = WalWriter::new(world(), [0u8; 32]);
        let mut wal = Wal::from_writer(w);
        wal.header.postcard_version = 999;
        let mut kernel = Kernel::new();
        assert!(matches!(
            replay_into(&mut kernel, &wal),
            Err(ReplayError::PostcardVersionMismatch { .. })
        ));
    }

    #[test]
    fn replay_rejects_corrupted_chain() {
        let mut w = WalWriter::new(world(), [0u8; 32]);
        w.append_step(
            InstanceId::new(1).unwrap(),
            crate::state::ScheduledActionId::new(1).unwrap(),
            Tick(0),
            CapabilityMask::SYSTEM.bits(),
            StepVerdict::Committed,
            [9u8; 32],
        )
        .unwrap();
        let mut wal = Wal::from_writer(w);
        wal.records[0].this_chain_hash = [0xFFu8; 32];
        let mut kernel = Kernel::new();
        assert!(matches!(
            replay_into(&mut kernel, &wal),
            Err(ReplayError::WalCorrupted(_))
        ));
    }

    // ---- CIL replay-determinism tests ----

    /// THE critical-bug regression. The original schedules a child internally;
    /// the WAL records only the parent Submit + two Step verdicts (parent and
    /// child), NOT a Submit for the child. Replay re-derives the child
    /// schedule by re-executing the parent — so the child runs exactly once,
    /// under the same id, reaching a bit-identical chain tip. A double-execution
    /// or id-drift would diverge a digest or the tip.
    #[test]
    fn replay_internal_scheduling_is_bit_identical() {
        let build = || -> Kernel {
            let mut k = Kernel::new_with_wal(world(), [0u8; 32]);
            k.register_action::<ScheduleChildAction>();
            k.register_action::<SpawnAction>();
            let inst = k.create_instance(priv_cfg());
            let parent = ScheduleChildAction {
                child_id: 77,
                at: 1,
            };
            k.submit(
                inst,
                Principal::System,
                None,
                CapabilityMask::SYSTEM,
                Tick(0),
                ScheduleChildAction::TYPE_CODE,
                Action::canonical_bytes(&parent),
            )
            .unwrap();
            k.step(Tick(0), CapabilityMask::SYSTEM); // runs parent → schedules child
            k.step(Tick(1), CapabilityMask::SYSTEM); // runs child → spawns entity 77
            k
        };
        let k1 = build();
        let original_tip = k1.wal_chain_tip().unwrap();
        let wal = k1.export_wal().unwrap();
        // The child schedule is NOT a Submit — only the parent is.
        let submit_count = wal
            .records
            .iter()
            .filter(|r| matches!(r.kind(), crate::persist::WalRecordKind::Submit))
            .count();
        let step_count = wal
            .records
            .iter()
            .filter(|r| matches!(r.kind(), crate::persist::WalRecordKind::Step))
            .count();
        assert_eq!(submit_count, 1, "only the parent is an exogenous submit");
        assert_eq!(step_count, 2, "two pops: parent then re-derived child");

        let mut k2 = Kernel::new_with_wal(world(), [0u8; 32]);
        k2.register_action::<ScheduleChildAction>();
        k2.register_action::<SpawnAction>();
        let _ = k2.create_instance(priv_cfg());
        let report = replay_into(&mut k2, &wal).expect("bit-identical replay");
        assert_eq!(report.submits_replayed, 1);
        assert_eq!(report.steps_replayed, 2);
        assert_eq!(report.final_chain_tip, original_tip);
    }

    /// A step whose op authorize-denies (External lacking SYSTEM tries to
    /// ScheduleAction → rollback → AuthDenied) replays to the same verdict and
    /// the same (unchanged) post-state digest.
    #[test]
    fn replay_history_with_authorize_denial() {
        let build = || -> Kernel {
            let mut k = Kernel::new_with_wal(world(), [0u8; 32]);
            k.register_action::<ScheduleNeedsSystemAction>();
            k.register_action::<SpawnAction>();
            // default_caps WITHOUT system: External submission can never reach
            // SYSTEM, so ScheduleAction is denied.
            let inst = k.create_instance(InstanceConfig {
                max_scheduled: 1024,
                ..Default::default()
            });
            k.submit(
                inst,
                Principal::External(ExternalId(7)),
                None,
                CapabilityMask::empty(),
                Tick(0),
                ScheduleNeedsSystemAction::TYPE_CODE,
                Vec::new(),
            )
            .unwrap();
            k.step(Tick(0), CapabilityMask::SYSTEM);
            k
        };
        let k1 = build();
        let original_tip = k1.wal_chain_tip().unwrap();
        let wal = k1.export_wal().unwrap();
        assert!(matches!(
            wal.records[1].content,
            WalRecordContent::Step {
                verdict: StepVerdict::AuthDenied,
                ..
            }
        ));

        let mut k2 = Kernel::new_with_wal(world(), [0u8; 32]);
        k2.register_action::<ScheduleNeedsSystemAction>();
        k2.register_action::<SpawnAction>();
        let _ = k2.create_instance(InstanceConfig {
            max_scheduled: 1024,
            ..Default::default()
        });
        let report = replay_into(&mut k2, &wal).expect("replay ok");
        assert_eq!(report.final_chain_tip, original_tip);
    }

    /// A committed step that skips one Op on the per-Op budget gate replays to
    /// the same `BudgetPartial { denied: 1 }` verdict and digest.
    #[test]
    fn replay_history_with_budget_partial() {
        let build = || -> Kernel {
            let mut k = Kernel::new_with_wal(world(), [0u8; 32]);
            k.register_action::<TwoSetAction>();
            // budget admits the spawn + the first 40-byte component, denies the
            // second 80-byte one (40+80 > 100).
            let inst = k.create_instance(InstanceConfig {
                memory_budget_bytes: 100,
                max_entities: 1024,
                ..Default::default()
            });
            let a = TwoSetAction { a: 40, b: 80 };
            k.submit(
                inst,
                Principal::System,
                None,
                CapabilityMask::SYSTEM,
                Tick(0),
                TwoSetAction::TYPE_CODE,
                Action::canonical_bytes(&a),
            )
            .unwrap();
            k.step(Tick(0), CapabilityMask::SYSTEM);
            k
        };
        let k1 = build();
        let original_tip = k1.wal_chain_tip().unwrap();
        let wal = k1.export_wal().unwrap();
        assert!(matches!(
            wal.records[1].content,
            WalRecordContent::Step {
                verdict: StepVerdict::BudgetPartial { denied: 1 },
                ..
            }
        ));

        let mut k2 = Kernel::new_with_wal(world(), [0u8; 32]);
        k2.register_action::<TwoSetAction>();
        let _ = k2.create_instance(InstanceConfig {
            memory_budget_bytes: 100,
            max_entities: 1024,
            ..Default::default()
        });
        let report = replay_into(&mut k2, &wal).expect("replay ok");
        assert_eq!(report.final_chain_tip, original_tip);
    }

    /// Skipped verdicts (unregistered type, undeserializable bytes) are
    /// recorded and replay re-reaches them (the kernels skip identically).
    #[test]
    fn replay_skipped_unregistered_and_deserfailed() {
        let build = || -> Kernel {
            let mut k = Kernel::new_with_wal(world(), [0u8; 32]);
            // SpawnAction registered; ScheduleChildAction (200) intentionally NOT.
            k.register_action::<SpawnAction>();
            let inst = k.create_instance(priv_cfg());
            // Unregistered type 200.
            k.submit(
                inst,
                Principal::System,
                None,
                CapabilityMask::SYSTEM,
                Tick(0),
                TypeCode(200),
                vec![1, 2, 3],
            )
            .unwrap();
            // Registered type 100 but undeserializable bytes (SpawnAction needs a u64).
            k.submit(
                inst,
                Principal::System,
                None,
                CapabilityMask::SYSTEM,
                Tick(1),
                SpawnAction::TYPE_CODE,
                Vec::new(),
            )
            .unwrap();
            k.step(Tick(0), CapabilityMask::SYSTEM);
            k.step(Tick(1), CapabilityMask::SYSTEM);
            k
        };
        let k1 = build();
        let original_tip = k1.wal_chain_tip().unwrap();
        let wal = k1.export_wal().unwrap();
        let verdicts: Vec<StepVerdict> = wal
            .records
            .iter()
            .filter_map(|r| match &r.content {
                WalRecordContent::Step { verdict, .. } => Some(*verdict),
                WalRecordContent::Submit { .. } => None,
            })
            .collect();
        assert!(verdicts.contains(&StepVerdict::Skipped {
            reason: SkipReason::Unregistered
        }));
        assert!(verdicts.contains(&StepVerdict::Skipped {
            reason: SkipReason::DeserFailed
        }));

        let mut k2 = Kernel::new_with_wal(world(), [0u8; 32]);
        k2.register_action::<SpawnAction>();
        let _ = k2.create_instance(priv_cfg());
        let report = replay_into(&mut k2, &wal).expect("replay ok");
        assert_eq!(report.final_chain_tip, original_tip);
    }

    /// The scheduler's `next_seq` tiebreak counter is not a logged fact — it is
    /// re-derived by re-execution. Two same-tick children scheduled in one step
    /// keep their FIFO-by-seq order across replay (digest pins it).
    #[test]
    fn next_seq_tiebreak_survives_replay() {
        #[derive(Serialize, Deserialize)]
        struct ScheduleTwoSameTick;
        impl Sealed for ScheduleTwoSameTick {}
        impl ActionDeriv for ScheduleTwoSameTick {
            const TYPE_CODE: TypeCode = TypeCode(600);
            const SCHEMA_VERSION: u32 = 1;
        }
        impl ActionCompute for ScheduleTwoSameTick {
            fn compute(&self, _ctx: &ActionContext) -> Vec<Op> {
                let mk = |id: u64| {
                    let child = SpawnAction { id };
                    Op::ScheduleAction {
                        at: Tick(5),
                        actor: None,
                        action_type_code: SpawnAction::TYPE_CODE,
                        action_bytes: Bytes::from(Action::canonical_bytes(&child)),
                    }
                };
                vec![mk(11), mk(22)]
            }
        }

        let build = || -> Kernel {
            let mut k = Kernel::new_with_wal(world(), [0u8; 32]);
            k.register_action::<ScheduleTwoSameTick>();
            k.register_action::<SpawnAction>();
            let inst = k.create_instance(priv_cfg());
            k.submit(
                inst,
                Principal::System,
                None,
                CapabilityMask::SYSTEM,
                Tick(0),
                TypeCode(600),
                Vec::new(),
            )
            .unwrap();
            k.step(Tick(0), CapabilityMask::SYSTEM);
            k.step(Tick(5), CapabilityMask::SYSTEM); // first same-tick child
            k.step(Tick(5), CapabilityMask::SYSTEM); // second same-tick child
            k
        };
        let k1 = build();
        let original_tip = k1.wal_chain_tip().unwrap();
        let wal = k1.export_wal().unwrap();

        let mut k2 = Kernel::new_with_wal(world(), [0u8; 32]);
        k2.register_action::<ScheduleTwoSameTick>();
        k2.register_action::<SpawnAction>();
        let _ = k2.create_instance(priv_cfg());
        let report = replay_into(&mut k2, &wal).expect("replay ok");
        assert_eq!(report.final_chain_tip, original_tip);
    }

    /// Cross-instance signal routing (delivery into the target inbox + the
    /// delivery-time refcount) is unlogged and re-derived on replay, reaching
    /// a bit-identical tip.
    #[test]
    fn signal_routing_replays_bit_identical() {
        let build = || -> Kernel {
            let mut k = Kernel::new_with_wal(world(), [0u8; 32]);
            k.register_action::<SignalAction>();
            let a = k.create_instance(priv_cfg()); // inst 1
            let _b = k.create_instance(priv_cfg()); // inst 2 (target)
            let act = SignalAction { target: 2 };
            k.submit(
                a,
                Principal::System,
                None,
                CapabilityMask::SYSTEM,
                Tick(0),
                SignalAction::TYPE_CODE,
                Action::canonical_bytes(&act),
            )
            .unwrap();
            k.step(Tick(0), CapabilityMask::SYSTEM);
            k
        };
        let k1 = build();
        let original_tip = k1.wal_chain_tip().unwrap();
        let wal = k1.export_wal().unwrap();

        let mut k2 = Kernel::new_with_wal(world(), [0u8; 32]);
        k2.register_action::<SignalAction>();
        let _ = k2.create_instance(priv_cfg());
        let _ = k2.create_instance(priv_cfg());
        let report = replay_into(&mut k2, &wal).expect("replay ok");
        assert_eq!(report.final_chain_tip, original_tip);
    }

    /// The default `replay_into` path validates the header `manifest_digest`
    /// against the replaying kernel's declared manifest (A14) — no `TrustAnchor`
    /// needed. A mismatch is rejected before any record is applied.
    #[test]
    fn manifest_digest_validated_on_default_path() {
        let mut k1 = Kernel::new_with_wal(world(), [0xAB; 32]);
        k1.register_action::<SpawnAction>();
        let inst = k1.create_instance(priv_cfg());
        let a = SpawnAction { id: 1 };
        k1.submit(
            inst,
            Principal::System,
            None,
            CapabilityMask::SYSTEM,
            Tick(0),
            SpawnAction::TYPE_CODE,
            Action::canonical_bytes(&a),
        )
        .unwrap();
        k1.step(Tick(0), CapabilityMask::SYSTEM);
        let wal = k1.export_wal().unwrap();

        // Replaying kernel declares a DIFFERENT manifest → rejected.
        let mut k2 = Kernel::new_with_wal(world(), [0xCD; 32]);
        k2.register_action::<SpawnAction>();
        let _ = k2.create_instance(priv_cfg());
        assert!(matches!(
            replay_into(&mut k2, &wal),
            Err(ReplayError::ManifestDigestMismatch { .. })
        ));

        // Matching manifest → accepted.
        let mut k3 = Kernel::new_with_wal(world(), [0xAB; 32]);
        k3.register_action::<SpawnAction>();
        let _ = k3.create_instance(priv_cfg());
        assert!(replay_into(&mut k3, &wal).is_ok());
    }

    /// Replay MEASURES the chain tip by re-sealing the re-derived records — it
    /// does not copy the stored tip. A kernel that re-executes a step
    /// differently (here: the action is unregistered on replay, so the step
    /// Skips instead of Commits) is caught as a verdict divergence rather than
    /// silently echoing the recorded tip.
    #[test]
    fn replay_measures_not_copies_tip() {
        let mut k1 = Kernel::new_with_wal(world(), [0u8; 32]);
        k1.register_action::<SpawnAction>();
        let inst = k1.create_instance(priv_cfg());
        let a = SpawnAction { id: 5 };
        k1.submit(
            inst,
            Principal::System,
            None,
            CapabilityMask::SYSTEM,
            Tick(0),
            SpawnAction::TYPE_CODE,
            Action::canonical_bytes(&a),
        )
        .unwrap();
        k1.step(Tick(0), CapabilityMask::SYSTEM);
        let wal = k1.export_wal().unwrap();

        // Replaying kernel does NOT register SpawnAction → the step Skips
        // (Unregistered) instead of Committing → VerdictDivergence, proving the
        // tip is re-derived, not copied.
        let mut k2 = Kernel::new_with_wal(world(), [0u8; 32]);
        let _ = k2.create_instance(priv_cfg());
        assert!(matches!(
            replay_into(&mut k2, &wal),
            Err(ReplayError::VerdictDivergence { .. })
        ));
    }

    /// Replaying into a kernel that did not pre-create the referenced instance
    /// returns a graceful error — never a panic (A12 panic-free discipline
    /// holds for untrusted / malformed WAL input).
    #[test]
    fn replay_into_kernel_missing_instance_is_graceful_error() {
        let mut k1 = Kernel::new_with_wal(world(), [0u8; 32]);
        k1.register_action::<SpawnAction>();
        let inst = k1.create_instance(priv_cfg());
        let a = SpawnAction { id: 1 };
        k1.submit(
            inst,
            Principal::System,
            None,
            CapabilityMask::SYSTEM,
            Tick(0),
            SpawnAction::TYPE_CODE,
            Action::canonical_bytes(&a),
        )
        .unwrap();
        k1.step(Tick(0), CapabilityMask::SYSTEM);
        let wal = k1.export_wal().unwrap();

        // k2 registers the action but creates NO instance → the Submit cannot
        // be admitted; replay fails gracefully rather than panicking.
        let mut k2 = Kernel::new_with_wal(world(), [0u8; 32]);
        k2.register_action::<SpawnAction>();
        let result = replay_into(&mut k2, &wal);
        assert!(
            matches!(result, Err(ReplayError::SubmitFailed(_))),
            "missing instance must surface as a graceful ReplayError, got {result:?}"
        );
    }
}