deepstrike-core 0.2.71

Cross-language agent runtime kernel — pure computation, zero I/O
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
//! The durable kernel record, its canonical bytes and its digest chain
//! (spec §8.1, §8.2, §12.1, §15.2).
//!
//! Three properties define this module, and every public item exists to make one of them
//! unrepresentable-if-violated rather than merely documented:
//!
//! 1. **Core is the only implementation of canonical bytes and digests** (§15.2). A record is
//!    built by [`KernelRecord::chain`], which computes every digest itself; the fields are private
//!    and read-only, so a host cannot hand-assemble a record or re-serialise one to recompute a
//!    hash. Decoding a record re-verifies it, which is why a tampered journal entry fails at the
//!    boundary instead of somewhere deep in a replay.
//! 2. **The durable record never carries the planned step** (§8.1, §22.12). It stores the
//!    normalised input plus a `step_digest`; a rebuild re-runs the deterministic transition over
//!    the canonical input and compares digests. That is what keeps rendered provider contexts and
//!    large action payloads out of the journal, so record size is a function of the *input*, never
//!    of the step it produced.
//! 3. **The chain is the operation's identity** (§12.1). The genesis record binds the
//!    [`ResolvedOperationConfig`] — not the sparse config, and not "whatever this binary defaults
//!    to today" — and carries no previous digest at all; its `record_digest` is the operation's
//!    `genesis_digest`.

use std::fmt;
use std::fmt::Write as _;

use serde::de::Deserializer;
use serde::{Deserialize, Serialize};
use sha2::{Digest as _, Sha256};

use super::config::{ConfigDefaults, ResolvedOperationConfig};
use super::effect::Digest;
use super::envelope::{
    DeliverExternalEvent, HostControl, KernelInput, OperationLifecycle, ResolveEffect,
    StartOperation, WireEnvelope, WireRejection,
};
use super::fault::KernelFaultCode;
use super::scalar::{CanonicalBytes, InputId, JS_SAFE_INTEGER_MAX, OperationId, WireU64};

// ---------------------------------------------------------------------------------------------
// errors
// ---------------------------------------------------------------------------------------------

/// Prefix of every record-layer rejection, so all four hosts can classify on one marker.
pub const RECORD_ERROR_MARKER: &str = "kernel record rejected";

/// Why a record could not be built, decoded or verified.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RecordError {
    /// A value has no canonical byte representation (a non-finite float, an integer no JSON host
    /// can hold exactly, a document nested past the canonical bound).
    NotCanonical(String),
    /// The record does not follow its predecessor: wrong operation, wrong sequence, wrong
    /// previous digest, a second genesis, or a genesis that is not a configuration.
    ChainBroken(String),
    /// A stored digest disagrees with the bytes it claims to summarise — the record was edited
    /// after core produced it.
    DigestMismatch(String),
}

impl RecordError {
    pub fn message(&self) -> &str {
        match self {
            Self::NotCanonical(message)
            | Self::ChainBroken(message)
            | Self::DigestMismatch(message) => message,
        }
    }

    /// Fault code a host-facing rejection carries (§7.13).
    ///
    /// `DigestMismatch` is [`KernelFaultCode::RecordCorrupted`] — its own code since the 2026-07-29
    /// adjudication, because a broken record and a broken checkpoint have different recovery
    /// ladders and folding them together told a host to fall back to a checkpoint when the journal
    /// itself was the thing that no longer verified.
    ///
    /// `ChainBroken` stays [`KernelFaultCode::TransactionConflict`]: the same variant reports "this
    /// input has no legal position after that head" (a caller error, no corruption involved) and
    /// "this stored chain does not link up". The transaction layer, which knows it is verifying
    /// *stored* records rather than placing a new one, re-labels the latter as `RecordCorrupted`.
    pub fn code(&self) -> KernelFaultCode {
        match self {
            Self::NotCanonical(_) => KernelFaultCode::MalformedEnvelope,
            Self::ChainBroken(_) => KernelFaultCode::TransactionConflict,
            Self::DigestMismatch(_) => KernelFaultCode::RecordCorrupted,
        }
    }
}

impl fmt::Display for RecordError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{RECORD_ERROR_MARKER}: {}", self.message())
    }
}

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

// ---------------------------------------------------------------------------------------------
// canonical bytes (§7.1.1)
// ---------------------------------------------------------------------------------------------

/// Nesting bound of the canonical writer. Well above the §7.3 bootstrap depth ceiling: this is a
/// stack guard for a recursive writer, not a second contract limit.
pub const CANONICAL_MAX_DEPTH: usize = 128;

/// Digest algorithm label. Carried in every [`Digest`] as an explicit `sha256:` prefix so a future
/// algorithm change is a visible wire change rather than a silent reinterpretation.
pub const DIGEST_ALGORITHM: &str = "sha256";

/// Serialise a value to canonical bytes.
///
/// The rules, in full — they are the contract every host validator re-implements in Phase 6:
///
/// | shape | canonical form |
/// | --- | --- |
/// | `null` / `true` / `false` | the literal |
/// | string | JSON string, minimal escaping (`"`, `\`, C0 controls) |
/// | integer | shortest decimal; `-0` is `0`; magnitudes above 2^53−1 are **rejected** |
/// | non-integral float | shortest round-trip decimal |
/// | array | `[a,b]` — no whitespace |
/// | object | `{"a":1,"b":2}` — keys ascending by Unicode code point, no whitespace |
///
/// Two rules earn their keep. Integers beyond the double-safe range are rejected rather than
/// emitted, because a JS host that parses record bytes would silently round them — every logical
/// `u64` on this wire already travels as a decimal string (§7.1.1), so the only way to hit this is
/// an opaque host payload, and failing closed beats a digest that means two different numbers in
/// two languages. Key ordering is by **code point**, which for UTF-8 is byte order; a JavaScript
/// validator must therefore sort with a code-point comparator rather than the default
/// UTF-16 `Array.prototype.sort`, which differs above the BMP.
pub fn canonical_bytes<T: Serialize + ?Sized>(value: &T) -> Result<CanonicalBytes, RecordError> {
    let value = serde_json::to_value(value).map_err(|error| {
        RecordError::NotCanonical(format!("value is not serialisable: {error}"))
    })?;
    let mut out = String::new();
    write_canonical(&value, 1, &mut out)?;
    Ok(CanonicalBytes::new(out.into_bytes()))
}

/// SHA-256 over exactly these bytes, projected as `sha256:<64 lowercase hex digits>`.
pub fn canonical_digest(bytes: &[u8]) -> Digest {
    let hash = Sha256::digest(bytes);
    let mut text = String::with_capacity(DIGEST_ALGORITHM.len() + 1 + hash.len() * 2);
    text.push_str(DIGEST_ALGORITHM);
    text.push(':');
    for byte in hash {
        write!(text, "{byte:02x}").expect("writing to a String cannot fail");
    }
    Digest::new(text).expect("an algorithm-prefixed hex digest is always a legal branded ref")
}

fn write_canonical(
    value: &serde_json::Value,
    depth: usize,
    out: &mut String,
) -> Result<(), RecordError> {
    if depth > CANONICAL_MAX_DEPTH {
        return Err(RecordError::NotCanonical(format!(
            "value nests deeper than {CANONICAL_MAX_DEPTH}"
        )));
    }
    match value {
        serde_json::Value::Null => out.push_str("null"),
        serde_json::Value::Bool(true) => out.push_str("true"),
        serde_json::Value::Bool(false) => out.push_str("false"),
        serde_json::Value::Number(number) => write_canonical_number(number, out)?,
        serde_json::Value::String(text) => write_canonical_string(text, out),
        serde_json::Value::Array(items) => {
            out.push('[');
            for (index, item) in items.iter().enumerate() {
                if index > 0 {
                    out.push(',');
                }
                write_canonical(item, depth + 1, out)?;
            }
            out.push(']');
        }
        serde_json::Value::Object(map) => {
            let mut keys: Vec<&String> = map.keys().collect();
            keys.sort_unstable();
            out.push('{');
            for (index, key) in keys.into_iter().enumerate() {
                if index > 0 {
                    out.push(',');
                }
                write_canonical_string(key, out);
                out.push(':');
                write_canonical(&map[key], depth + 1, out)?;
            }
            out.push('}');
        }
    }
    Ok(())
}

fn write_canonical_string(text: &str, out: &mut String) {
    let encoded = serde_json::to_string(text).expect("a Rust string is always JSON-encodable");
    out.push_str(&encoded);
}

fn write_canonical_number(
    number: &serde_json::Number,
    out: &mut String,
) -> Result<(), RecordError> {
    const SAFE: i128 = JS_SAFE_INTEGER_MAX as i128;

    let unsafe_integer = |value: i128| {
        RecordError::NotCanonical(format!(
            "integer {value} exceeds the cross-language exact-integer range \{JS_SAFE_INTEGER_MAX}); logical u64 travels as a decimal string"
        ))
    };

    if let Some(value) = number.as_u64() {
        if i128::from(value) > SAFE {
            return Err(unsafe_integer(i128::from(value)));
        }
        write!(out, "{value}").expect("writing to a String cannot fail");
        return Ok(());
    }
    if let Some(value) = number.as_i64() {
        if i128::from(value) < -SAFE {
            return Err(unsafe_integer(i128::from(value)));
        }
        write!(out, "{value}").expect("writing to a String cannot fail");
        return Ok(());
    }

    let float = number
        .as_f64()
        .filter(|value| value.is_finite())
        .ok_or_else(|| RecordError::NotCanonical(format!("number {number} is not finite")))?;
    // An integral float and the same integer must produce the same bytes, or two numerically
    // equal documents would digest differently. `-0.0` collapses into `0` here too.
    if float.fract() == 0.0 && float.abs() <= JS_SAFE_INTEGER_MAX as f64 {
        write!(out, "{}", float as i64).expect("writing to a String cannot fail");
    } else {
        let encoded = serde_json::to_string(&float).expect("a finite f64 is always JSON-encodable");
        out.push_str(&encoded);
    }
    Ok(())
}

// ---------------------------------------------------------------------------------------------
// §8.1 · the normalised input a record stores
// ---------------------------------------------------------------------------------------------

/// One accepted envelope after normalisation — the shape whose canonical bytes the record stores
/// (§12.1 calls the serialised form a `CanonicalInput`).
///
/// It mirrors [`WireEnvelope`] with exactly one difference: the genesis arm carries the **resolved**
/// configuration instead of the sparse one. That single substitution is what makes a journal
/// replayable across kernel upgrades (§15.2, Task 6b) — every default this operation runs on is
/// frozen in its first record, so changing a compile-time default cannot change an old operation's
/// decisions.
///
/// The envelope-shaped wrapper is deliberate: `observed_at_ms` is the operation's only clock fact
/// (§11.2) and `input_id` is its idempotency key (§7.1), so a canonical input that dropped them
/// could not be replayed on its own.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct NormalizedInput {
    pub operation_id: OperationId,
    pub input_id: InputId,
    pub observed_at_ms: WireU64,
    pub input: NormalizedPayload,
}

/// The five input classes after normalisation. Same tag vocabulary as [`KernelInput`] — a record
/// does not invent a second name for an input class.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum NormalizedPayload {
    /// Genesis. Carries the dense [`ResolvedOperationConfig`], never the sparse wire config.
    ConfigureOperation(ResolvedConfiguration),
    StartOperation(StartOperation),
    ResolveEffect(ResolveEffect),
    DeliverExternalEvent(DeliverExternalEvent),
    HostControl(HostControl),
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ResolvedConfiguration {
    pub config: ResolvedOperationConfig,
}

impl NormalizedPayload {
    /// Whether this payload may only appear as the first record of an operation.
    pub fn is_genesis(&self) -> bool {
        matches!(self, Self::ConfigureOperation(_))
    }

    pub fn kind(&self) -> &'static str {
        match self {
            Self::ConfigureOperation(_) => "configure_operation",
            Self::StartOperation(_) => "start_operation",
            Self::ResolveEffect(_) => "resolve_effect",
            Self::DeliverExternalEvent(_) => "deliver_external_event",
            Self::HostControl(_) => "host_control",
        }
    }

    /// §6.1 · the lifecycles this class is admissible in, read off the *normalised* payload.
    ///
    /// The same table [`KernelInput::admissible_lifecycles`] states, reachable from a record. A
    /// restore replays canonical inputs rather than envelopes and must apply exactly the same
    /// lifecycle gate, so the table has to be reachable from both sides of normalisation — and
    /// delegating keeps there being one table.
    pub fn admissible_lifecycles(&self) -> &'static [OperationLifecycle] {
        match self {
            Self::ConfigureOperation(_) => &[OperationLifecycle::Created],
            Self::StartOperation(_) => &[OperationLifecycle::Configured],
            Self::ResolveEffect(_) | Self::DeliverExternalEvent(_) => {
                &[OperationLifecycle::Running, OperationLifecycle::Suspended]
            }
            Self::HostControl(_) => &[
                OperationLifecycle::Configured,
                OperationLifecycle::Running,
                OperationLifecycle::Suspended,
            ],
        }
    }
}

impl NormalizedInput {
    /// Normalise one decoded envelope. The genesis arm resolves its configuration against the
    /// binary's defaults **once**, here; every later reader takes the resolved value from the
    /// record instead of re-deriving it.
    pub fn normalize(
        envelope: &WireEnvelope,
        defaults: &ConfigDefaults,
    ) -> Result<Self, WireRejection> {
        let input = match &envelope.input {
            KernelInput::ConfigureOperation(configure) => {
                NormalizedPayload::ConfigureOperation(ResolvedConfiguration {
                    config: configure.config.resolve(defaults)?,
                })
            }
            KernelInput::StartOperation(start) => NormalizedPayload::StartOperation(start.clone()),
            KernelInput::ResolveEffect(resolve) => {
                NormalizedPayload::ResolveEffect(resolve.clone())
            }
            KernelInput::DeliverExternalEvent(event) => {
                NormalizedPayload::DeliverExternalEvent(event.clone())
            }
            KernelInput::HostControl(control) => NormalizedPayload::HostControl(control.clone()),
        };
        Ok(Self {
            operation_id: envelope.operation_id.clone(),
            input_id: envelope.input_id.clone(),
            observed_at_ms: envelope.observed_at_ms,
            input,
        })
    }

    pub fn is_genesis(&self) -> bool {
        self.input.is_genesis()
    }

    /// The resolved configuration, on the genesis input only.
    pub fn resolved_config(&self) -> Option<&ResolvedOperationConfig> {
        match &self.input {
            NormalizedPayload::ConfigureOperation(configure) => Some(&configure.config),
            _ => None,
        }
    }
}

// ---------------------------------------------------------------------------------------------
// §8.1 · the durable record
// ---------------------------------------------------------------------------------------------

/// One durable transition (§8.1).
///
/// Every field is private and every digest is computed by [`KernelRecord::chain`]: there is no
/// constructor that takes a digest, so "the host recomputed the hash and disagreed" is not a
/// reachable state. [`KernelRecord::record_bytes`] is what the host hands to
/// `KernelJournal::compare_and_append`, and [`KernelRecord::expected_head`] is the CAS precondition
/// that goes with it.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct KernelRecord {
    operation_id: OperationId,
    input_id: InputId,
    step_seq: WireU64,
    /// `None` on the genesis record only — the CAS expected head of an operation's first append is
    /// empty (§8.1).
    previous_record_digest: Option<Digest>,
    /// Canonical bytes of the [`NormalizedInput`]. Projected as an explicit base64 envelope in
    /// JSON (§7.1.1); native bindings see bytes.
    canonical_input: CanonicalBytes,
    input_digest: Digest,
    /// Digest of the ephemeral planned step. The step itself never enters the journal (§22.12).
    step_digest: Digest,
    record_digest: Digest,
}

/// Everything a chain successor needs to know about its predecessor.
///
/// Exactly three facts — the operation it belongs to, where it sits, and what it hashes to — and
/// deliberately not the record itself. §12.2 restores a runtime whose predecessor record may have
/// been pruned under an acked checkpoint; the anchor is what survives that.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ChainAnchor {
    pub operation_id: OperationId,
    pub step_seq: WireU64,
    pub record_digest: Digest,
}

/// The digested body: every field of a record except the digest that summarises it.
#[derive(Serialize)]
struct RecordBody<'a> {
    operation_id: &'a OperationId,
    input_id: &'a InputId,
    step_seq: WireU64,
    previous_record_digest: Option<&'a Digest>,
    canonical_input: &'a CanonicalBytes,
    input_digest: &'a Digest,
    step_digest: &'a Digest,
}

impl KernelRecord {
    /// Build the next record of an operation.
    ///
    /// `previous = None` builds the genesis record: `step_seq` 0, no previous digest, and a payload
    /// that **must** be the resolved configuration. Every later record must carry a non-genesis
    /// payload, the same `operation_id`, and links to its predecessor's digest — a second
    /// `ConfigureOperation` has no legal position in a chain (§6.1: it is admissible only in
    /// `Created`).
    pub fn chain<S: Serialize + ?Sized>(
        previous: Option<&Self>,
        input: &NormalizedInput,
        planned_step: &S,
    ) -> Result<Self, RecordError> {
        Self::chain_after(previous.map(Self::anchor).as_ref(), input, planned_step)
    }

    /// The three facts a successor reads off this record.
    pub fn anchor(&self) -> ChainAnchor {
        ChainAnchor {
            operation_id: self.operation_id.clone(),
            step_seq: self.step_seq,
            record_digest: self.record_digest.clone(),
        }
    }

    /// [`Self::chain`], anchored on the predecessor's *facts* rather than on the predecessor.
    ///
    /// The distinction is what makes §12.2's bounded-tail restore possible at all: a restored
    /// runtime replays the tail on top of a checkpoint whose covered prefix may already have been
    /// pruned, so the record before the first tail entry no longer exists anywhere — only its
    /// digest and its sequence do, and the checkpoint carries them.
    pub fn chain_after<S: Serialize + ?Sized>(
        previous: Option<&ChainAnchor>,
        input: &NormalizedInput,
        planned_step: &S,
    ) -> Result<Self, RecordError> {
        let (step_seq, previous_record_digest) = match previous {
            None => {
                if !input.is_genesis() {
                    return Err(RecordError::ChainBroken(format!(
                        "an operation's first record must be its resolved configuration, got {}",
                        input.input.kind()
                    )));
                }
                (WireU64::ZERO, None)
            }
            Some(previous) => {
                if input.is_genesis() {
                    return Err(RecordError::ChainBroken(
                        "an operation is configured exactly once; a second configure_operation \
                         has no position in the chain"
                            .to_string(),
                    ));
                }
                if previous.operation_id != input.operation_id {
                    return Err(RecordError::ChainBroken(format!(
                        "input belongs to operation {}, but the chain head belongs to {}",
                        input.operation_id, previous.operation_id
                    )));
                }
                let next = previous.step_seq.get().checked_add(1).ok_or_else(|| {
                    RecordError::ChainBroken("step sequence overflowed u64".to_string())
                })?;
                (WireU64::new(next), Some(previous.record_digest.clone()))
            }
        };

        let canonical_input = canonical_bytes(input)?;
        let input_digest = canonical_digest(canonical_input.as_slice());
        let step_digest = canonical_digest(canonical_bytes(planned_step)?.as_slice());
        let record_digest = Self::body_digest(
            &input.operation_id,
            &input.input_id,
            step_seq,
            previous_record_digest.as_ref(),
            &canonical_input,
            &input_digest,
            &step_digest,
        )?;

        Ok(Self {
            operation_id: input.operation_id.clone(),
            input_id: input.input_id.clone(),
            step_seq,
            previous_record_digest,
            canonical_input,
            input_digest,
            step_digest,
            record_digest,
        })
    }

    #[allow(clippy::too_many_arguments)]
    fn body_digest(
        operation_id: &OperationId,
        input_id: &InputId,
        step_seq: WireU64,
        previous_record_digest: Option<&Digest>,
        canonical_input: &CanonicalBytes,
        input_digest: &Digest,
        step_digest: &Digest,
    ) -> Result<Digest, RecordError> {
        let body = RecordBody {
            operation_id,
            input_id,
            step_seq,
            previous_record_digest,
            canonical_input,
            input_digest,
            step_digest,
        };
        Ok(canonical_digest(canonical_bytes(&body)?.as_slice()))
    }

    // ----- read-only accessors -----

    pub fn operation_id(&self) -> &OperationId {
        &self.operation_id
    }

    pub fn input_id(&self) -> &InputId {
        &self.input_id
    }

    pub fn step_seq(&self) -> WireU64 {
        self.step_seq
    }

    pub fn previous_record_digest(&self) -> Option<&Digest> {
        self.previous_record_digest.as_ref()
    }

    /// The CAS precondition for appending this record (§8.2 line 5). `None` means "the operation
    /// has no journal head yet", which only its genesis record may assert.
    pub fn expected_head(&self) -> Option<&Digest> {
        self.previous_record_digest.as_ref()
    }

    pub fn canonical_input(&self) -> &CanonicalBytes {
        &self.canonical_input
    }

    pub fn input_digest(&self) -> &Digest {
        &self.input_digest
    }

    pub fn step_digest(&self) -> &Digest {
        &self.step_digest
    }

    pub fn record_digest(&self) -> &Digest {
        &self.record_digest
    }

    pub fn is_genesis(&self) -> bool {
        self.previous_record_digest.is_none()
    }

    // ----- journal projection -----

    /// Canonical bytes of the whole record — what the journal stores and what
    /// [`Self::from_record_bytes`] reads back.
    pub fn record_bytes(&self) -> CanonicalBytes {
        canonical_bytes(self).expect("a record contains only canonical scalars")
    }

    /// Decode a record from its journal bytes, verifying every digest it carries.
    pub fn from_record_bytes(bytes: &[u8]) -> Result<Self, RecordError> {
        let text = std::str::from_utf8(bytes).map_err(|error| {
            RecordError::NotCanonical(format!("record bytes are not UTF-8: {error}"))
        })?;
        serde_json::from_str(text).map_err(|error| decode_error(&error.to_string()))
    }

    /// Decode the normalised input this record froze — the entry point of a rebuild (§12.2).
    pub fn normalized_input(&self) -> Result<NormalizedInput, RecordError> {
        let text = std::str::from_utf8(self.canonical_input.as_slice()).map_err(|error| {
            RecordError::NotCanonical(format!("canonical input is not UTF-8: {error}"))
        })?;
        serde_json::from_str(text).map_err(|error| {
            RecordError::NotCanonical(format!("canonical input does not decode: {error}"))
        })
    }

    // ----- verification -----

    /// Recompute this record's own digests from the bytes it carries.
    pub fn verify(&self) -> Result<(), RecordError> {
        let input_digest = canonical_digest(self.canonical_input.as_slice());
        if input_digest != self.input_digest {
            return Err(RecordError::DigestMismatch(format!(
                "record {} step {}: canonical input hashes to {input_digest}, \
                 but the record claims {}",
                self.operation_id, self.step_seq, self.input_digest
            )));
        }
        let record_digest = Self::body_digest(
            &self.operation_id,
            &self.input_id,
            self.step_seq,
            self.previous_record_digest.as_ref(),
            &self.canonical_input,
            &self.input_digest,
            &self.step_digest,
        )?;
        if record_digest != self.record_digest {
            return Err(RecordError::DigestMismatch(format!(
                "record {} step {}: body hashes to {record_digest}, but the record claims {}",
                self.operation_id, self.step_seq, self.record_digest
            )));
        }
        Ok(())
    }

    /// Verify a rebuilt step against the digest this record froze (§8.1).
    pub fn verify_step<S: Serialize + ?Sized>(&self, planned_step: &S) -> Result<(), RecordError> {
        let digest = canonical_digest(canonical_bytes(planned_step)?.as_slice());
        if digest != self.step_digest {
            return Err(RecordError::DigestMismatch(format!(
                "record {} step {}: the rebuilt step hashes to {digest}, \
                 but the record froze {}",
                self.operation_id, self.step_seq, self.step_digest
            )));
        }
        Ok(())
    }

    /// Verify this record follows `previous` (`None` = it claims to be a genesis).
    pub fn verify_follows(&self, previous: Option<&Self>) -> Result<(), RecordError> {
        self.verify()?;
        match (previous, self.previous_record_digest.as_ref()) {
            (None, None) => {
                if self.step_seq != WireU64::ZERO {
                    return Err(RecordError::ChainBroken(format!(
                        "a genesis record is step 0, got step {}",
                        self.step_seq
                    )));
                }
                if self.normalized_input()?.is_genesis() {
                    Ok(())
                } else {
                    Err(RecordError::ChainBroken(
                        "a genesis record must carry the resolved configuration".to_string(),
                    ))
                }
            }
            (None, Some(digest)) => Err(RecordError::ChainBroken(format!(
                "record {} step {} expects head {digest}, but the operation has no head",
                self.operation_id, self.step_seq
            ))),
            (Some(previous), None) => Err(RecordError::ChainBroken(format!(
                "record {} step {} claims to be a genesis, but the operation head is {}",
                self.operation_id, self.step_seq, previous.record_digest
            ))),
            (Some(previous), Some(digest)) => {
                if previous.operation_id != self.operation_id {
                    return Err(RecordError::ChainBroken(format!(
                        "record belongs to operation {}, its predecessor to {}",
                        self.operation_id, previous.operation_id
                    )));
                }
                if digest != &previous.record_digest {
                    return Err(RecordError::ChainBroken(format!(
                        "record {} step {} expects head {digest}, but the head is {}",
                        self.operation_id, self.step_seq, previous.record_digest
                    )));
                }
                if previous.step_seq.get().checked_add(1) != Some(self.step_seq.get()) {
                    return Err(RecordError::ChainBroken(format!(
                        "record {} is step {}, but its predecessor is step {}",
                        self.operation_id, self.step_seq, previous.step_seq
                    )));
                }
                Ok(())
            }
        }
    }
}

/// The §7.13 preparation result once its durable half is known: [`KernelRecord`] is the `Record`
/// instance of [`KernelPreparation`], and Task 7 fills in the ephemeral planned step.
pub type RecordPreparation<Step> = super::fault::KernelPreparation<KernelRecord, Step>;

/// Verify a whole chain and return the operation's `genesis_digest` (§12.1).
///
/// The returned digest is the identity a checkpoint binds itself to: an operation is its genesis
/// record's digest, so a checkpoint built from another operation's journal cannot be installed by
/// accident.
pub fn verify_record_chain(records: &[KernelRecord]) -> Result<&Digest, RecordError> {
    let Some(genesis) = records.first() else {
        return Err(RecordError::ChainBroken(
            "an operation chain starts at its genesis record; this one is empty".to_string(),
        ));
    };
    genesis.verify_follows(None)?;
    for pair in records.windows(2) {
        pair[1].verify_follows(Some(&pair[0]))?;
    }
    Ok(&genesis.record_digest)
}

// ---------------------------------------------------------------------------------------------
// decoding
// ---------------------------------------------------------------------------------------------

/// Wire projection of a record, used only as the decode target. Decoding goes through it so that
/// [`KernelRecord`]'s fields stay private and every decoded record is verified before it exists.
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct RecordProjection {
    operation_id: OperationId,
    input_id: InputId,
    step_seq: WireU64,
    previous_record_digest: Option<Digest>,
    canonical_input: CanonicalBytes,
    input_digest: Digest,
    step_digest: Digest,
    record_digest: Digest,
}

fn decode_error(message: &str) -> RecordError {
    if message.contains(RECORD_ERROR_MARKER) {
        RecordError::DigestMismatch(message.to_string())
    } else {
        RecordError::NotCanonical(format!("record does not decode: {message}"))
    }
}

impl<'de> Deserialize<'de> for KernelRecord {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let projection = RecordProjection::deserialize(deserializer)?;
        let record = Self {
            operation_id: projection.operation_id,
            input_id: projection.input_id,
            step_seq: projection.step_seq,
            previous_record_digest: projection.previous_record_digest,
            canonical_input: projection.canonical_input,
            input_digest: projection.input_digest,
            step_digest: projection.step_digest,
            record_digest: projection.record_digest,
        };
        record
            .verify()
            .map_err(|error| serde::de::Error::custom(error.to_string()))?;
        Ok(record)
    }
}

#[cfg(test)]
mod tests {
    use std::collections::BTreeSet;
    use std::fs;
    use std::path::PathBuf;

    use serde_json::{Value, json};

    use super::super::*;

    // -----------------------------------------------------------------------------------------
    // helpers
    // -----------------------------------------------------------------------------------------

    fn operation() -> OperationId {
        OperationId::new("op-record-1").unwrap()
    }

    fn input_id(seq: u32) -> InputId {
        InputId::new(format!("in-{seq}")).unwrap()
    }

    fn boot_config() -> OperationConfig {
        OperationConfig {
            execution_policy: Some(ExecutionPolicy {
                max_turns: Some(12),
                ..ExecutionPolicy::default()
            }),
            host_effect_support: HostEffectSupport::new([
                EffectKindTag::CallProvider,
                EffectKindTag::ExecuteTools,
            ]),
            ..OperationConfig::default()
        }
    }

    fn envelope(seq: u32, observed_at_ms: u64, input: KernelInput) -> WireEnvelope {
        WireEnvelope::new(
            operation(),
            input_id(seq),
            WireU64::new(observed_at_ms),
            input,
        )
    }

    fn configure_envelope() -> WireEnvelope {
        envelope(
            0,
            1_700_000_000_000,
            KernelInput::ConfigureOperation(ConfigureOperation {
                config: boot_config(),
            }),
        )
    }

    fn start_envelope() -> WireEnvelope {
        envelope(
            1,
            1_700_000_000_500,
            KernelInput::StartOperation(StartOperation {
                entry: RootEntry::Agent(RootAgentEntry {
                    task: LogicalTask::new("write the brief"),
                    run_spec: None,
                }),
                initial_context: InitialContext::default(),
            }),
        )
    }

    fn resolve_envelope() -> WireEnvelope {
        envelope(
            2,
            1_700_000_001_000,
            KernelInput::ResolveEffect(ResolveEffect {
                effect_id: EffectId::new("op-record-1:step:1:effect:0").unwrap(),
                outcome: EffectOutcome::Failed(EffectFailed {
                    failure: HostEffectFailure {
                        kind: HostEffectFailureKind::ProtocolError,
                        message: "provider refused the request".to_string(),
                        retryable: Some(false),
                    },
                }),
            }),
        )
    }

    fn cancel_envelope() -> WireEnvelope {
        envelope(
            3,
            1_700_000_002_000,
            KernelInput::HostControl(HostControl {
                command: HostCommand::Cancel(CancelCommand {
                    reason: CancellationReason::User,
                    pending_call_ids: vec![],
                }),
            }),
        )
    }

    fn normalize(envelope: &WireEnvelope) -> NormalizedInput {
        NormalizedInput::normalize(envelope, &ConfigDefaults::default())
            .expect("the sample envelope normalises")
    }

    fn step(name: &str) -> Value {
        json!({ "planned": name, "effects": [{ "kind": "call_provider" }] })
    }

    fn genesis_record() -> KernelRecord {
        KernelRecord::chain(None, &normalize(&configure_envelope()), &step("configure")).unwrap()
    }

    /// genesis then start then resolve
    fn sample_chain() -> Vec<KernelRecord> {
        let genesis = genesis_record();
        let started = KernelRecord::chain(
            Some(&genesis),
            &normalize(&start_envelope()),
            &step("start"),
        )
        .unwrap();
        let resolved = KernelRecord::chain(
            Some(&started),
            &normalize(&resolve_envelope()),
            &step("resolve"),
        )
        .unwrap();
        vec![genesis, started, resolved]
    }

    fn canonical_text<T: Serialize + ?Sized>(value: &T) -> String {
        String::from_utf8(canonical_bytes(value).unwrap().into_vec()).unwrap()
    }

    fn fixture_dir() -> PathBuf {
        PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../tests/fixtures/kernel-wire")
    }

    /// Read a frozen golden, or rewrite it when `BLESS_KERNEL_RECORD_FIXTURES=1`.
    ///
    /// The blessing path exists so a deliberate contract change is a one-command, reviewable diff;
    /// the default path is an assertion that today's bytes are the frozen bytes.
    fn golden(name: &str, produced: &Value) -> Value {
        let path = fixture_dir().join(name);
        if std::env::var("BLESS_KERNEL_RECORD_FIXTURES").as_deref() == Ok("1") {
            let mut text = serde_json::to_string_pretty(produced).unwrap();
            text.push('\n');
            fs::write(&path, text).unwrap_or_else(|e| panic!("cannot bless {name}: {e}"));
            return produced.clone();
        }
        let raw = fs::read_to_string(&path).unwrap_or_else(|e| {
            panic!("missing golden {name} ({e}); re-bless with BLESS_KERNEL_RECORD_FIXTURES=1")
        });
        serde_json::from_str(&raw).unwrap_or_else(|e| panic!("{name} is not JSON: {e}"))
    }

    // -----------------------------------------------------------------------------------------
    // canonical bytes (spec 7.1.1)
    // -----------------------------------------------------------------------------------------

    #[test]
    fn canonical_bytes_sort_keys_and_carry_no_whitespace() {
        assert_eq!(
            canonical_text(&json!({ "b": 1, "a": { "d": [1, 2], "c": true }, "": null })),
            r#"{"":null,"a":{"c":true,"d":[1,2]},"b":1}"#
        );
        assert_eq!(canonical_text(&json!([])), "[]");
        assert_eq!(canonical_text(&json!({})), "{}");
        assert_eq!(
            canonical_text(&json!("quote \" backslash \\ newline \n tab \t")),
            r#""quote \" backslash \\ newline \n tab \t""#
        );
        assert_eq!(canonical_text(&json!("\u{1}")), "\"\\u0001\"");
    }

    #[test]
    fn canonical_object_keys_sort_by_code_point_not_utf16() {
        // U+10000 precedes U+FFFD under UTF-16 ordering (its lead surrogate is 0xD800) and follows
        // it under code-point ordering. Canonical bytes use code points, so a JavaScript validator
        // must sort with a code-point comparator rather than the default one.
        assert_eq!(
            canonical_text(&json!({ "\u{10000}": 1, "\u{fffd}": 2, "z": 3 })),
            "{\"z\":3,\"\u{fffd}\":2,\"\u{10000}\":1}"
        );
    }

    #[test]
    fn canonical_numbers_are_language_neutral() {
        assert_eq!(canonical_text(&json!(0)), "0");
        assert_eq!(canonical_text(&json!(-0.0)), "0", "-0 and 0 are one value");
        assert_eq!(canonical_text(&json!(2.0)), "2", "2 and 2.0 are one value");
        assert_eq!(canonical_text(&json!(-17)), "-17");
        assert_eq!(canonical_text(&json!(0.5)), "0.5");
        assert_eq!(
            canonical_text(&json!(JS_SAFE_INTEGER_MAX)),
            "9007199254740991"
        );
    }

    #[test]
    fn canonical_bytes_reject_what_no_host_can_read_back() {
        let too_large = serde_json::from_str::<Value>("9007199254740992").unwrap();
        let error = canonical_bytes(&too_large).expect_err("beyond the exact-integer range");
        assert!(matches!(error, RecordError::NotCanonical(_)), "{error}");

        let too_negative = serde_json::from_str::<Value>("-9007199254740992").unwrap();
        assert!(canonical_bytes(&too_negative).is_err());

        let mut deep = json!(0);
        for _ in 0..(CANONICAL_MAX_DEPTH + 2) {
            deep = Value::Array(vec![deep]);
        }
        assert!(canonical_bytes(&deep).is_err(), "recursion must be bounded");
    }

    #[test]
    fn canonical_bytes_are_byte_identical_across_repeated_runs() {
        let input = normalize(&configure_envelope());
        let first = canonical_bytes(&input).unwrap();
        for _ in 0..8 {
            assert_eq!(canonical_bytes(&input).unwrap(), first);
        }

        // and a value assembled in a different key order is the same value
        let one = canonical_bytes(&json!({ "a": 1, "b": 2 })).unwrap();
        let other = canonical_bytes(&json!({ "b": 2, "a": 1 })).unwrap();
        assert_eq!(one, other);
    }

    #[test]
    fn the_digest_is_sha256_over_exactly_the_canonical_bytes() {
        // known answers: the algorithm is plain SHA-256, hex, lowercase
        assert_eq!(
            canonical_digest(b"").as_str(),
            "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
        );
        assert_eq!(
            canonical_digest(b"abc").as_str(),
            "sha256:ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
        );
        assert_eq!(
            canonical_digest(canonical_text(&json!({ "a": 1 })).as_bytes()).as_str(),
            canonical_digest(br#"{"a":1}"#).as_str()
        );
    }

    // -----------------------------------------------------------------------------------------
    // genesis (spec 8.1, 12.1, Task 6b)
    // -----------------------------------------------------------------------------------------

    #[test]
    fn genesis_binds_the_resolved_config_and_an_empty_previous_head() {
        let record = genesis_record();

        assert!(record.is_genesis());
        assert_eq!(record.step_seq(), WireU64::ZERO);
        assert_eq!(record.previous_record_digest(), None);
        assert_eq!(
            record.expected_head(),
            None,
            "genesis appends against an empty head"
        );

        let stored = record.normalized_input().unwrap();
        let resolved = stored
            .resolved_config()
            .expect("genesis stores a resolved config");
        assert_eq!(
            resolved,
            &boot_config().resolve(&ConfigDefaults::default()).unwrap(),
            "the genesis record freezes the resolved configuration, not the sparse one"
        );
        assert_eq!(resolved.execution_policy.max_turns, 12);

        // and it is dense: every field a later replay needs is present, no Option stands for
        // "ask the binary".
        let value = serde_json::to_value(resolved).unwrap();
        for required in [
            "execution_policy",
            "governance_policy",
            "scheduler_policy",
            "resource_quota",
            "signal_policy",
            "context_policy",
            "recovery_policy",
            "payload_policy",
            "kernel_limits",
            "memory_policy",
            "feature_policy",
            "host_effect_support",
        ] {
            assert!(
                value.get(required).is_some(),
                "resolved config lacks {required}"
            );
        }
    }

    #[test]
    fn the_genesis_digest_is_the_operations_identity() {
        let chain = sample_chain();
        let genesis_digest = verify_record_chain(&chain).unwrap();
        assert_eq!(genesis_digest, chain[0].record_digest());
        assert_eq!(
            chain[1].previous_record_digest(),
            Some(genesis_digest),
            "the first transition links straight to the genesis digest"
        );
    }

    #[test]
    fn a_genesis_replay_survives_kernel_default_drift() {
        // A newer binary with different compile-time defaults resolves *new* configs differently...
        let mut drifted = ConfigDefaults::default();
        drifted.baseline.execution_policy.max_context_tokens = 999;
        drifted.baseline.recovery_policy.provider_recovery_attempts = 9;

        let record = genesis_record();
        // ...but a rebuild reads the frozen resolved config out of the record instead of resolving
        // again, so the drift cannot reach the replay.
        let replayed = record.normalized_input().unwrap();
        assert_eq!(
            replayed
                .resolved_config()
                .unwrap()
                .execution_policy
                .max_context_tokens,
            ConfigDefaults::default()
                .baseline
                .execution_policy
                .max_context_tokens
        );
        assert_ne!(
            drifted.baseline.execution_policy.max_context_tokens,
            replayed
                .resolved_config()
                .unwrap()
                .execution_policy
                .max_context_tokens,
            "the fixture must actually drift, or this test proves nothing"
        );
        assert_eq!(
            canonical_bytes(&replayed).unwrap(),
            *record.canonical_input()
        );
        record
            .verify_step(&step("configure"))
            .expect("the frozen step still verifies");
    }

    // -----------------------------------------------------------------------------------------
    // the chain (spec 8.1, 15.2)
    // -----------------------------------------------------------------------------------------

    #[test]
    fn each_record_links_to_its_predecessor() {
        let chain = sample_chain();
        for (index, record) in chain.iter().enumerate() {
            assert_eq!(record.step_seq(), WireU64::new(index as u64));
            assert_eq!(record.operation_id(), &operation());
            if index == 0 {
                assert!(record.previous_record_digest().is_none());
            } else {
                assert_eq!(
                    record.previous_record_digest(),
                    Some(chain[index - 1].record_digest())
                );
            }
        }
        verify_record_chain(&chain).unwrap();
    }

    #[test]
    fn the_chain_refuses_a_genesis_in_the_wrong_position() {
        // a non-configuration cannot open an operation
        let error = KernelRecord::chain(None, &normalize(&start_envelope()), &step("start"))
            .expect_err("only a resolved configuration opens a chain");
        assert!(matches!(error, RecordError::ChainBroken(_)), "{error}");

        // and a configuration cannot re-open one
        let genesis = genesis_record();
        let error = KernelRecord::chain(
            Some(&genesis),
            &normalize(&configure_envelope()),
            &step("configure again"),
        )
        .expect_err("an operation is configured exactly once");
        assert!(matches!(error, RecordError::ChainBroken(_)), "{error}");
    }

    #[test]
    fn a_record_cannot_chain_onto_another_operation() {
        let genesis = genesis_record();
        let mut foreign = normalize(&start_envelope());
        foreign.operation_id = OperationId::new("op-other").unwrap();
        let error = KernelRecord::chain(Some(&genesis), &foreign, &step("start"))
            .expect_err("operations do not share a chain");
        assert!(matches!(error, RecordError::ChainBroken(_)), "{error}");
    }

    #[test]
    fn tampering_with_any_link_is_detected() {
        let chain = sample_chain();

        // 1. an edited field inside a record no longer matches its own digest
        for field in [
            "operation_id",
            "input_id",
            "step_seq",
            "input_digest",
            "step_digest",
            "previous_record_digest",
        ] {
            let mut value = serde_json::to_value(&chain[2]).unwrap();
            value[field] = match field {
                "step_seq" => json!("99"),
                "previous_record_digest" | "input_digest" | "step_digest" => {
                    json!(canonical_digest(b"forged").as_str())
                }
                _ => json!("forged"),
            };
            let error = serde_json::from_value::<KernelRecord>(value)
                .expect_err(&format!("editing {field} must be detected"));
            assert!(
                error.to_string().contains(RECORD_ERROR_MARKER),
                "{field}: {error}"
            );
        }

        // 2. an edited canonical input no longer matches input_digest
        let mut value = serde_json::to_value(&chain[1]).unwrap();
        value["canonical_input"]["data"] =
            serde_json::to_value(CanonicalBytes::new(b"{}".to_vec())).unwrap()["data"].clone();
        assert!(serde_json::from_value::<KernelRecord>(value).is_err());

        // 3. a re-digested forgery passes verify() but breaks the chain
        let forged = KernelRecord::chain(
            Some(&chain[0]),
            &normalize(&cancel_envelope()),
            &step("forged"),
        )
        .unwrap();
        let mut broken = chain.clone();
        broken[1] = forged;
        let error = verify_record_chain(&broken).expect_err("link 2 no longer follows link 1");
        assert!(matches!(error, RecordError::ChainBroken(_)), "{error}");

        // 4. dropping a link is a broken chain, not a shorter one
        let gapped = vec![chain[0].clone(), chain[2].clone()];
        assert!(verify_record_chain(&gapped).is_err());

        // 5. an empty journal has no genesis
        assert!(verify_record_chain(&[]).is_err());
    }

    #[test]
    fn a_record_verifies_the_step_a_rebuild_recomputes() {
        let chain = sample_chain();
        chain[1]
            .verify_step(&step("start"))
            .expect("the same step verifies");
        let error = chain[1]
            .verify_step(&step("something else"))
            .expect_err("a different step must not verify");
        assert!(matches!(error, RecordError::DigestMismatch(_)), "{error}");
    }

    // -----------------------------------------------------------------------------------------
    // the record never carries the step (spec 8.1, 22.12)
    // -----------------------------------------------------------------------------------------

    #[test]
    fn the_record_is_exactly_the_eight_declared_fields() {
        let value = serde_json::to_value(genesis_record()).unwrap();
        let keys: BTreeSet<String> = value.as_object().unwrap().keys().cloned().collect();
        assert_eq!(
            keys,
            BTreeSet::from([
                "operation_id".to_string(),
                "input_id".to_string(),
                "step_seq".to_string(),
                "previous_record_digest".to_string(),
                "canonical_input".to_string(),
                "input_digest".to_string(),
                "step_digest".to_string(),
                "record_digest".to_string(),
            ])
        );
    }

    #[test]
    fn no_planned_step_survives_into_the_durable_record() {
        const BANNED: [&str; 8] = [
            "step",
            "planned_step",
            "committed_step",
            "actions",
            "effects",
            "rendered_context",
            "messages",
            "faults",
        ];

        let record = KernelRecord::chain(
            None,
            &normalize(&configure_envelope()),
            &json!({
                "actions": [{ "kind": "call_provider" }],
                "rendered_context": { "messages": [{ "role": "user", "content": "x" }] },
                "effects": [{ "effect_id": "op-record-1:step:0:effect:0" }],
            }),
        )
        .unwrap();

        let text = String::from_utf8(record.record_bytes().into_vec()).unwrap();
        let value: Value = serde_json::from_str(&text).unwrap();
        let mut keys = BTreeSet::new();
        collect_keys(&value, &mut keys);
        for banned in BANNED {
            assert!(
                !keys.contains(banned),
                "the record leaked the step key {banned:?}"
            );
        }
        assert!(!text.contains("rendered_context"));
        assert!(!text.contains("call_provider"));
    }

    #[test]
    fn record_size_is_a_function_of_the_input_not_of_the_step() {
        let input = normalize(&resolve_envelope());
        let genesis = genesis_record();

        let tiny = KernelRecord::chain(Some(&genesis), &input, &json!({})).unwrap();
        let huge = KernelRecord::chain(
            Some(&genesis),
            &input,
            &json!({ "rendered_context": "x".repeat(200_000) }),
        )
        .unwrap();

        assert_eq!(
            tiny.record_bytes().len(),
            huge.record_bytes().len(),
            "a 200 KiB step must not grow the journal record by one byte"
        );
        assert_ne!(tiny.step_digest(), huge.step_digest());
        assert_eq!(tiny.input_digest(), huge.input_digest());
    }

    fn collect_keys(value: &Value, out: &mut BTreeSet<String>) {
        match value {
            Value::Object(map) => {
                for (key, item) in map {
                    out.insert(key.clone());
                    collect_keys(item, out);
                }
            }
            Value::Array(items) => items.iter().for_each(|item| collect_keys(item, out)),
            _ => {}
        }
    }

    // -----------------------------------------------------------------------------------------
    // journal projection (spec 8.2)
    // -----------------------------------------------------------------------------------------

    #[test]
    fn records_round_trip_through_their_journal_bytes() {
        for record in sample_chain() {
            let bytes = record.record_bytes();
            let back = KernelRecord::from_record_bytes(bytes.as_slice()).unwrap();
            assert_eq!(back, record);
            assert_eq!(back.record_bytes(), bytes, "journal bytes are stable");

            // the canonical input decodes back into the typed input a rebuild replays
            let input = record.normalized_input().unwrap();
            assert_eq!(canonical_bytes(&input).unwrap(), *record.canonical_input());
        }
    }

    #[test]
    fn a_record_with_removed_or_unknown_fields_fails_closed() {
        let mut value = serde_json::to_value(genesis_record()).unwrap();
        value["abi_version"] = json!(1);
        assert!(serde_json::from_value::<KernelRecord>(value).is_err());

        let mut value = serde_json::to_value(genesis_record()).unwrap();
        value["surprise"] = json!(true);
        assert!(
            serde_json::from_value::<KernelRecord>(value).is_err(),
            "a record with an unknown field is not a record"
        );
    }

    #[test]
    fn the_record_is_the_durable_half_of_a_preparation() {
        let record = genesis_record();
        let preparation: RecordPreparation<Value> =
            KernelPreparation::Prepared(PreparedTransition {
                token: PrepareToken::new("prepare-1").unwrap(),
                record: record.clone(),
                planned_step: step("configure"),
            });

        assert_eq!(preparation.record(), Some(&record));
        assert!(
            preparation.token().is_some(),
            "only a prepared transition commits"
        );

        let text = serde_json::to_string(&preparation).unwrap();
        let back: RecordPreparation<Value> = serde_json::from_str(&text).unwrap();
        assert_eq!(back, preparation);

        // the planned step travels with the preparation and stops there
        record
            .verify_step(preparation.step().unwrap())
            .expect("the preparation's step is the one the record froze");
    }

    // -----------------------------------------------------------------------------------------
    // golden fixtures: the Phase 6 cross-language source of truth
    // -----------------------------------------------------------------------------------------

    #[test]
    fn golden_canonical_bytes_vectors() {
        let vectors = vec![
            ("empty_object", json!({})),
            ("empty_array", json!([])),
            ("key_order", json!({ "b": 1, "A": 2, "a": 3, "": 4 })),
            (
                "code_point_key_order",
                json!({ "\u{10000}": 1, "\u{fffd}": 2, "z": 3 }),
            ),
            ("escapes", json!("\" \\ \n \t \u{1} \u{e9} \u{1f600}")),
            (
                "numbers",
                json!([0, -0.0, 2.0, -17, 0.5, 9007199254740991u64]),
            ),
            (
                "null_and_bools",
                json!({ "a": null, "b": true, "c": false }),
            ),
            (
                "nested",
                json!({ "outer": { "inner": [1, { "deep": "value" }] } }),
            ),
            (
                "wire_u64_is_a_string",
                json!({ "step_seq": "18446744073709551615" }),
            ),
        ];

        let produced = json!({
            "description":
                "Canonical byte vectors (spec 7.1.1). `canonical` is the exact UTF-8 byte string \
                 core produces; `digest` is SHA-256 over those bytes, hex, sha256-prefixed. \
                 Object keys sort by Unicode code point.",
            "vectors": vectors
                .iter()
                .map(|(name, value)| {
                    let canonical = canonical_text(value);
                    json!({
                        "name": name,
                        "value": value,
                        "canonical": canonical,
                        "digest": canonical_digest(canonical.as_bytes()).as_str(),
                    })
                })
                .collect::<Vec<_>>(),
            "rejected": [
                { "name": "integer_beyond_exact_range", "value": 9007199254740992u64 },
                { "name": "negative_integer_beyond_exact_range", "value": -9007199254740992i64 },
            ],
        });

        let expected = golden("golden_record_canonical_bytes.json", &produced);
        assert_eq!(produced, expected, "canonical byte vectors drifted");

        for rejected in expected["rejected"].as_array().unwrap() {
            assert!(
                canonical_bytes(&rejected["value"]).is_err(),
                "{} must have no canonical form",
                rejected["name"]
            );
        }
    }

    #[test]
    fn golden_genesis_record() {
        let envelope = configure_envelope();
        let input = normalize(&envelope);
        let planned = step("configure");
        let record = KernelRecord::chain(None, &input, &planned).unwrap();

        let produced = json!({
            "description":
                "Genesis record (spec 8.1, 12.1). `canonical_input` holds the resolved \
                 configuration, `previous_record_digest` is null, and `record_digest` is the \
                 operation's genesis_digest.",
            "envelope": serde_json::to_value(&envelope).unwrap(),
            "step": planned,
            "normalized_input": serde_json::to_value(&input).unwrap(),
            "canonical_input": canonical_text(&input),
            "record": serde_json::to_value(&record).unwrap(),
            "record_bytes": String::from_utf8(record.record_bytes().into_vec()).unwrap(),
            "genesis_digest": record.record_digest().as_str(),
        });

        let expected = golden("golden_record_genesis.json", &produced);
        assert_eq!(produced, expected, "the genesis record drifted");
        assert_eq!(expected["record"]["previous_record_digest"], Value::Null);
    }

    #[test]
    fn golden_transition_record() {
        let genesis = genesis_record();
        let envelope = resolve_envelope();
        let input = normalize(&envelope);
        let planned = step("resolve");
        let record = KernelRecord::chain(Some(&genesis), &input, &planned).unwrap();

        let produced = json!({
            "description":
                "A non-genesis record (spec 8.1). It stores the normalised envelope plus a step \
                 digest, never the planned step, and links to `previous_record`'s digest.",
            "previous_record": serde_json::to_value(&genesis).unwrap(),
            "envelope": serde_json::to_value(&envelope).unwrap(),
            "step": planned,
            "normalized_input": serde_json::to_value(&input).unwrap(),
            "canonical_input": canonical_text(&input),
            "record": serde_json::to_value(&record).unwrap(),
            "record_bytes": String::from_utf8(record.record_bytes().into_vec()).unwrap(),
        });

        let expected = golden("golden_record_transition.json", &produced);
        assert_eq!(produced, expected, "the transition record drifted");

        // the fixture is self-checking: its previous record really is this record's head
        let previous: KernelRecord =
            serde_json::from_value(expected["previous_record"].clone()).unwrap();
        let decoded: KernelRecord = serde_json::from_value(expected["record"].clone()).unwrap();
        decoded.verify_follows(Some(&previous)).unwrap();
    }

    #[test]
    fn golden_record_chain_of_three() {
        let envelopes = [configure_envelope(), start_envelope(), resolve_envelope()];
        let steps = [step("configure"), step("start"), step("resolve")];

        let mut records: Vec<KernelRecord> = Vec::new();
        let mut links = Vec::new();
        for (envelope, planned) in envelopes.iter().zip(steps.iter()) {
            let input = normalize(envelope);
            let record = KernelRecord::chain(records.last(), &input, planned).unwrap();
            links.push(json!({
                "envelope": serde_json::to_value(envelope).unwrap(),
                "step": planned,
                "record": serde_json::to_value(&record).unwrap(),
            }));
            records.push(record);
        }

        let produced = json!({
            "description":
                "Three-link record chain (spec 8.1, 12.1). Replaying the envelopes through \
                 normalisation and KernelRecord::chain must reproduce every record byte for \
                 byte; `genesis_digest` is the operation's identity and `head_digest` its CAS head.",
            "genesis_digest": records[0].record_digest().as_str(),
            "head_digest": records[2].record_digest().as_str(),
            "links": links,
        });

        let expected = golden("golden_record_chain.json", &produced);
        assert_eq!(produced, expected, "the record chain drifted");

        let decoded: Vec<KernelRecord> = expected["links"]
            .as_array()
            .unwrap()
            .iter()
            .map(|link| serde_json::from_value(link["record"].clone()).unwrap())
            .collect();
        assert_eq!(
            verify_record_chain(&decoded).unwrap().as_str(),
            expected["genesis_digest"].as_str().unwrap()
        );
    }
}