lonca-core 0.2.0

Deterministic building blocks of Lonca: change ids, the signed operation log, keys and the Git store
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
//! The operation log: an append-only, hash-chained, signed list of every
//! operation, stored as a chain of Git commits on `refs/lonca/oplog` so that
//! it travels with `git push`.
//!
//! The chain does not rely on Git's SHA-1: every entry carries the SHA-256 of
//! the previous entry, and the daemon signs the SHA-256 of each entry.
//!
//! Every entry carries a format version (`docs/decisions.md`, "Log format
//! versions"). Entries are never rewritten, so every version ever written
//! stays readable; a version this build does not know is refused.

use serde::{Deserialize, Serialize};

#[cfg(feature = "store")]
use crate::canonical::canonical_bytes;
use crate::canonical::digest_of;
use crate::error::{Error, Result};
use crate::keys::{KeyPair, verify_hex};
#[cfg(feature = "store")]
use crate::objects::{FileEntry, FileMap, FileMode, GitId};
use crate::ops::{
    Actor, Attestation, Decision, Facts, HumanSignature, LeaseItem, NegotiationMsg, ObsKind, Op,
};
#[cfg(feature = "store")]
use crate::store::{CommitSpec, Store};

pub const OPLOG_REF: &str = "refs/lonca/oplog";
pub const GENESIS_PREV: &str = "0000000000000000000000000000000000000000000000000000000000000000";
#[cfg(feature = "store")]
const OP_FILE: &str = "op.json";

/// Entries written before the format field existed. The field is left out of
/// their encoding, so their bytes, hashes and signatures are unchanged.
pub const FORMAT_LEGACY: u32 = 0;
/// The format this build writes. Version 1 is version 0 with the field
/// written out; version 2 adds `symbols_engine`; version 3 adds the human
/// key operations; version 4 adds the daemon key operations; version 5
/// adds `scope.scratch` on `session.start` and `cap.attenuate`; version 6
/// adds `reject`; version 7 changes what `change.propose` means for
/// leases (they are pinned to the change) and prunes dead lease sets;
/// version 8 gives heartbeats and denied acquires a meaning for leases;
/// version 9 lets a `session.start` continue an earlier session; version 10
/// adds `lease.interest`, `to` on a yield and `leases` on `cap.attenuate`;
/// version 11 adds the `unproposed` observation; version 12 makes a lease
/// say why and lets a waiting interest go first; version 13 lets a name
/// stand for every symbol of that name; version 14 adds `renames` to facts
/// and follows them in leases; version 15 adds `unparsed_files` to facts;
/// version 16 adds `unread_files`; version 17 adds `change.integrate_batch`;
/// version 18 adds `affected` to facts and reports who holds it; version 19
/// adds `session.isolate`; version 20 adds `harness` to `session.start`, and
/// a level rests on it; version 21 adds the lease kind `shared`; version 22
/// adds reserved `names` to an `insert` and makes a new file need a lease;
/// version 23 gives a delegated lease back to the parent when the child
/// expires or stops beating, as when it ends; version 24 adds the
/// `unhooked` observation, which taints a session; version 25 adds named
/// `checks` to `approve`.
pub const FORMAT_CURRENT: u32 = 25;
/// The first format whose `change.propose` and `change.integrate` entries
/// record the symbol engine version.
pub const FORMAT_SYMBOLS_ENGINE: u32 = 2;
/// The first format with `human.rotate`, `human.revoke`,
/// `human.recovery_key` and `human.recover`.
pub const FORMAT_HUMAN_KEYS: u32 = 3;
/// The first format with `daemon.rotate` and `daemon.recover`.
pub const FORMAT_DAEMON_KEYS: u32 = 4;
/// The first format whose session scopes may name scratch roots.
pub const FORMAT_SCRATCH: u32 = 5;
/// The first format with `reject`.
pub const FORMAT_REJECT: u32 = 6;
/// The first format whose entries pin leases to the changes they propose
/// and prune dead lease sets. No new field: the meaning of existing entries
/// changes, so a replay must apply it to these entries and no older ones.
pub const FORMAT_LEASE_PINS: u32 = 7;
/// The first format in which a heartbeat keeps a session's leases alive
/// (and its absence ends them), a denied acquire registers interest and
/// `leases.max_hold_minutes` limits a renewal. No new field, again.
pub const FORMAT_LEASE_LIVENESS: u32 = 8;
/// The first format in which a `session.start` may continue a session.
pub const FORMAT_SESSION_CONTINUE: u32 = 9;
/// The first format with `lease.interest`, a yield to a session and lease
/// sets carried by `cap.attenuate`.
pub const FORMAT_LEASE_QUEUE: u32 = 10;
/// The first format with the `unproposed` observation.
pub const FORMAT_UNPROPOSED: u32 = 11;
/// The first format in which a lease or an interest needs an intent and a
/// waiting interest stops new leases that conflict with it. No new field.
pub const FORMAT_LEASE_INTENT: u32 = 12;
/// The first format in which `path#Name` covers `path#Name(key)`, in leases
/// and in scopes. No new field.
pub const FORMAT_SYMBOL_KEYS: u32 = 13;
/// The first format with `renames` in facts. Older entries have none, so
/// nothing is gated on it.
pub const FORMAT_RENAMES: u32 = 14;
/// The first format with `unparsed_files` in facts. Older entries have none,
/// so nothing is gated on it.
pub const FORMAT_UNPARSED: u32 = 15;
/// The first format with `unread_files` in facts. Older entries have none,
/// so nothing is gated on it.
pub const FORMAT_UNREAD: u32 = 16;
/// The first format with `change.integrate_batch`.
pub const FORMAT_BATCH: u32 = 17;
/// The first format with `affected` in facts, and whose proposals report the
/// lease sets it reaches. Older entries report none and replay as they were.
pub const FORMAT_AFFECTED: u32 = 18;
/// The first format with `session.isolate`.
pub const FORMAT_ISOLATE: u32 = 19;
/// The first format with `harness` on `session.start`. From here a
/// `session.bind` is held to it, and can not raise a level already bound.
/// Older entries replay as they were.
pub const FORMAT_SESSION_HARNESS: u32 = 20;
/// The first format with the lease kind `shared`.
pub const FORMAT_SHARED_LEASES: u32 = 21;
/// The first format with reserved `names` on an `insert` item. From here a
/// change that adds a file needs an exclusive lease on its `path#<file>`.
/// Older entries replay as they were.
pub const FORMAT_INSERT_NAMES: u32 = 22;
/// The first format in which a lease set delegated to a child session goes
/// back to the parent that gave it when the child expires or stops beating,
/// and not only when it ends or is revoked. No new field: older entries
/// replay as they were.
pub const FORMAT_GIVE_BACK: u32 = 23;
/// The first format with the `unhooked` observation: a call to `lonca mcp`
/// whose before-tool event never came. From here it taints the session.
pub const FORMAT_UNHOOKED: u32 = 24;
/// The first format with named `checks` on `approve`: the checks of
/// `verification.checks` a change owes. Evidence of them is `named`.
pub const FORMAT_NAMED_CHECKS: u32 = 25;

fn is_legacy(format: &u32) -> bool {
    *format == FORMAT_LEGACY
}

/// Refuses a format this build can not read.
pub fn check_format(seq: u64, format: u32) -> Result<()> {
    if format > FORMAT_CURRENT {
        return Err(Error::Unsupported(format!(
            "op {seq}: log format {format} is newer than this build of lonca knows \
             (up to {FORMAT_CURRENT}); upgrade lonca"
        )));
    }
    Ok(())
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct OpEntry {
    /// Entry format version, covered by the digest and the signature.
    #[serde(default, skip_serializing_if = "is_legacy")]
    pub format: u32,
    pub seq: u64,
    pub prev: String,
    /// Assigned by the daemon; never decreases along the chain.
    pub time_ms: i64,
    pub actor: Actor,
    pub op: Op,
    pub evidence: Vec<Attestation>,
    pub facts: Option<Facts>,
    /// Version of the symbol engine that computed this entry's facts or
    /// merge. Present exactly on `change.propose` and `change.integrate`
    /// entries from format 2 on; absent means version 0.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub symbols_engine: Option<u32>,
    pub decision: Decision,
    pub human: Option<HumanSignature>,
}

impl OpEntry {
    pub fn digest(&self) -> String {
        digest_of(self)
    }

    /// Whether this entry's facts or merge come from the symbol engine.
    pub fn uses_symbols_engine(&self) -> bool {
        matches!(
            self.op,
            Op::ChangePropose { .. } | Op::ChangeIntegrate { .. } | Op::ChangeIntegrateBatch { .. }
        )
    }

    /// The first format that can contain this entry's operation type.
    pub fn min_format(&self) -> u32 {
        match self.op {
            Op::HumanRotate { .. }
            | Op::HumanRevoke { .. }
            | Op::HumanRecoveryKey { .. }
            | Op::HumanRecover { .. } => FORMAT_HUMAN_KEYS,
            Op::DaemonRotate { .. } | Op::DaemonRecover { .. } => FORMAT_DAEMON_KEYS,
            Op::Reject { .. } => FORMAT_REJECT,
            Op::Approve { ref checks, .. } if !checks.is_empty() => FORMAT_NAMED_CHECKS,
            Op::ChangeIntegrateBatch { .. } => FORMAT_BATCH,
            Op::SessionIsolate { .. } => FORMAT_ISOLATE,
            Op::LeaseAcquire { ref items, .. } | Op::LeaseInterest { ref items, .. }
                if items.iter().any(|i| !i.names.is_empty()) =>
            {
                FORMAT_INSERT_NAMES
            }
            Op::LeaseAcquire { ref items, .. } | Op::LeaseInterest { ref items, .. }
                if items.iter().any(LeaseItem::is_shared) =>
            {
                FORMAT_SHARED_LEASES
            }
            Op::Observe { ref events, .. }
                if events
                    .iter()
                    .any(|e| matches!(e.kind, ObsKind::Unhooked { .. })) =>
            {
                FORMAT_UNHOOKED
            }
            Op::Observe { ref events, .. }
                if events
                    .iter()
                    .any(|e| matches!(e.kind, ObsKind::Unproposed { .. })) =>
            {
                FORMAT_UNPROPOSED
            }
            Op::LeaseInterest { .. }
            | Op::Negotiate {
                message: NegotiationMsg::Yield { to: Some(_), .. },
                ..
            } => FORMAT_LEASE_QUEUE,
            Op::CapAttenuate { ref leases, .. } if !leases.is_empty() => FORMAT_LEASE_QUEUE,
            Op::SessionStart {
                harness: Some(_), ..
            } => FORMAT_SESSION_HARNESS,
            Op::SessionStart {
                continues: Some(_), ..
            } => FORMAT_SESSION_CONTINUE,
            Op::SessionStart { ref scope, .. } | Op::CapAttenuate { ref scope, .. }
                if !scope.scratch.is_empty() =>
            {
                FORMAT_SCRATCH
            }
            _ => FORMAT_LEGACY,
        }
    }

    /// The symbol engine version the auditor must recompute with, for
    /// entries that use the engine.
    pub fn engine(&self) -> Option<u32> {
        self.uses_symbols_engine()
            .then(|| self.symbols_engine.unwrap_or(0))
    }

    /// The daemon key in force after this entry, if the entry switched it:
    /// an allowed `daemon.rotate` or `daemon.recover`.
    pub fn switches_daemon_key(&self) -> Option<&str> {
        self.op
            .daemon_key_switch()
            .filter(|_| self.decision.is_allow())
    }

    /// The daemon key that must have signed this entry, given the key in
    /// force before it. An allowed `daemon.recover` is signed by the key it
    /// installs, because the old key is gone; every other entry, a rotation
    /// included, by the key in force.
    pub fn signer<'a>(&'a self, in_force: &'a str) -> &'a str {
        match (&self.op, self.switches_daemon_key()) {
            (Op::DaemonRecover { .. }, Some(new)) => new,
            _ => in_force,
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct SignedOp {
    pub entry: OpEntry,
    pub hash: String,
    pub sig: String,
}

impl SignedOp {
    pub fn sign(entry: OpEntry, key: &KeyPair) -> Self {
        let hash = entry.digest();
        let sig = key.sign_hex(hash.as_bytes());
        Self { entry, hash, sig }
    }

    pub fn verify(&self, daemon_key: &str) -> Result<()> {
        if self.entry.digest() != self.hash {
            return Err(Error::Integrity(format!(
                "op {}: hash does not match content",
                self.entry.seq
            )));
        }
        if !verify_hex(daemon_key, self.hash.as_bytes(), &self.sig) {
            return Err(Error::Integrity(format!(
                "op {}: bad daemon signature",
                self.entry.seq
            )));
        }
        Ok(())
    }
}

/// The log in a store.
#[cfg(feature = "store")]
pub struct OpLog<'a> {
    store: &'a Store,
}

#[cfg(feature = "store")]
impl<'a> OpLog<'a> {
    pub fn new(store: &'a Store) -> Self {
        Self { store }
    }

    pub fn head(&self) -> Result<Option<GitId>> {
        self.store.ref_get(OPLOG_REF)
    }

    /// Appends one signed entry. `expected_head` must be the current head
    /// commit (compare-and-swap), so two writers can never interleave.
    pub fn append(&self, op: &SignedOp, expected_head: Option<&GitId>) -> Result<GitId> {
        if op.entry.format != FORMAT_CURRENT {
            return Err(Error::Invalid(format!(
                "op {}: refusing to append log format {} (this build writes {FORMAT_CURRENT})",
                op.entry.seq, op.entry.format
            )));
        }
        let blob = self.store.write_blob(&canonical_bytes(op))?;
        let mut files = FileMap::new();
        files.insert(
            OP_FILE.into(),
            FileEntry {
                mode: FileMode::Regular,
                blob,
            },
        );
        let tree = self.store.write_tree(&files)?;
        let commit = self.store.write_commit(&CommitSpec {
            tree,
            parents: expected_head.into_iter().cloned().collect(),
            author_name: "lonca".into(),
            author_email: "daemon@lonca.invalid".into(),
            time_ms: op.entry.time_ms,
            message: format!(
                "lonca op {} {} {:?}\n\nactor: {}\n",
                op.entry.seq,
                op.entry.op.kind().as_str(),
                op.entry.decision.outcome,
                op.entry.actor
            ),
            headers: vec![],
        })?;
        self.store.ref_cas(OPLOG_REF, &commit, expected_head)?;
        Ok(commit)
    }

    pub fn read_at(&self, commit: &GitId) -> Result<SignedOp> {
        let bytes = self.store.read_file(commit, OP_FILE)?.ok_or_else(|| {
            Error::Integrity(format!("oplog commit {} has no {OP_FILE}", commit.short()))
        })?;
        decode(&bytes)
    }

    /// All entries, oldest first.
    pub fn read_all(&self) -> Result<Vec<SignedOp>> {
        self.read_from(0)
    }

    /// The entries from sequence `seq` on, oldest first. The log is walked
    /// from its head, so this costs what it returns, however long the log.
    pub fn read_from(&self, seq: u64) -> Result<Vec<SignedOp>> {
        let mut out = Vec::new();
        let mut cur = self.head()?;
        while let Some(c) = cur {
            let op = self.read_at(&c)?;
            if op.entry.seq < seq {
                break;
            }
            out.push(op);
            let info = self.store.read_commit(&c)?;
            if info.parents.len() > 1 {
                return Err(Error::Integrity(format!(
                    "oplog commit {} is a merge",
                    c.short()
                )));
            }
            cur = info.parents.into_iter().next();
        }
        out.reverse();
        Ok(out)
    }
}

/// Decodes a stored entry. The format version is read first, so an entry
/// from a newer lonca is refused as such rather than as a decoding or hash
/// error.
pub fn decode(bytes: &[u8]) -> Result<SignedOp> {
    #[derive(Deserialize)]
    struct Probe {
        entry: ProbeEntry,
    }
    #[derive(Deserialize)]
    struct ProbeEntry {
        #[serde(default)]
        format: u32,
        #[serde(default)]
        seq: u64,
    }
    let probe: Probe = serde_json::from_slice(bytes)?;
    check_format(probe.entry.seq, probe.entry.format)?;
    Ok(serde_json::from_slice(bytes)?)
}

/// Checks sequence numbers, the hash chain, signatures, time order and
/// format versions (known, and never decreasing along the chain).
/// The daemon key is taken from the genesis `repo.init` entry and follows
/// every allowed `daemon.rotate` and `daemon.recover`.
///
/// This checks who signed each entry, not whether a key switch was
/// authorised: that needs the human keys in force, so the daemon
/// (`lonca_policy::chain::replay_checked`) and the auditor decide every
/// switch again.
pub fn verify_chain(ops: &[SignedOp]) -> Result<String> {
    let Some(first) = ops.first() else {
        return Err(Error::Integrity("empty operation log".into()));
    };
    let mut daemon_key = match &first.entry.op {
        Op::RepoInit { daemon_key, .. } => daemon_key.clone(),
        _ => return Err(Error::Integrity("first operation is not repo.init".into())),
    };
    let mut prev = GENESIS_PREV.to_string();
    let mut last_time = i64::MIN;
    let mut last_format = FORMAT_LEGACY;
    for (i, op) in ops.iter().enumerate() {
        check_format(op.entry.seq, op.entry.format)?;
        if op.entry.format < last_format {
            return Err(Error::Integrity(format!(
                "op {i}: log format goes back from {last_format} to {}",
                op.entry.format
            )));
        }
        let wants_engine =
            op.entry.format >= FORMAT_SYMBOLS_ENGINE && op.entry.uses_symbols_engine();
        if op.entry.symbols_engine.is_some() != wants_engine {
            return Err(Error::Integrity(format!(
                "op {i}: symbols_engine must be {} in a format {} {} entry",
                if wants_engine { "present" } else { "absent" },
                op.entry.format,
                op.entry.op.kind().as_str()
            )));
        }
        if op.entry.format < op.entry.min_format() {
            return Err(Error::Integrity(format!(
                "op {i}: {} needs log format {} or later, found {}",
                op.entry.op.kind().as_str(),
                op.entry.min_format(),
                op.entry.format
            )));
        }
        if op.entry.seq != i as u64 {
            return Err(Error::Integrity(format!(
                "expected seq {i}, found {}",
                op.entry.seq
            )));
        }
        if op.entry.prev != prev {
            return Err(Error::Integrity(format!("op {i}: broken hash chain")));
        }
        if op.entry.time_ms < last_time {
            return Err(Error::Integrity(format!("op {i}: time goes backwards")));
        }
        op.verify(op.entry.signer(&daemon_key))?;
        if let Some(new) = op.entry.switches_daemon_key() {
            daemon_key = new.to_string();
        }
        prev = op.hash.clone();
        last_time = op.entry.time_ms;
        last_format = op.entry.format;
    }
    Ok(prev)
}

#[cfg(test)]
#[cfg(feature = "store")]
mod tests {
    use super::*;
    use crate::ChangeId;
    use crate::ops::{Continuation, Decision, HumanKey, LeaseKind, Scope};

    fn genesis(key: &KeyPair, store: &Store) -> OpEntry {
        let tree = store.empty_tree().unwrap();
        OpEntry {
            format: FORMAT_CURRENT,
            seq: 0,
            prev: GENESIS_PREV.into(),
            time_ms: 1000,
            actor: Actor::Human {
                name: "ayse".into(),
            },
            op: Op::RepoInit {
                repo: "demo".into(),
                daemon_key: key.public_hex(),
                human: "ayse".into(),
                human_key: HumanKey {
                    alg: "ed25519".into(),
                    public: "00".into(),
                },
                policy: tree.clone(),
                trunk: tree,
            },
            evidence: vec![],
            facts: None,
            symbols_engine: None,
            decision: Decision::allow(),
            human: None,
        }
    }

    fn next(prev: &SignedOp, time_ms: i64) -> OpEntry {
        OpEntry {
            format: FORMAT_CURRENT,
            seq: prev.entry.seq + 1,
            prev: prev.hash.clone(),
            time_ms,
            actor: Actor::Daemon,
            op: Op::Freeze {
                reason: "test".into(),
            },
            evidence: vec![],
            facts: None,
            symbols_engine: None,
            decision: Decision::allow(),
            human: None,
        }
    }

    #[test]
    fn append_read_verify() {
        let dir = tempfile::tempdir().unwrap();
        let store = Store::init_bare(&dir.path().join("r.git")).unwrap();
        let key = KeyPair::generate();
        let log = OpLog::new(&store);

        let g = SignedOp::sign(genesis(&key, &store), &key);
        let h0 = log.append(&g, None).unwrap();
        let o1 = SignedOp::sign(next(&g, 2000), &key);
        log.append(&o1, Some(&h0)).unwrap();

        let all = log.read_all().unwrap();
        assert_eq!(all, vec![g.clone(), o1.clone()]);
        assert_eq!(verify_chain(&all).unwrap(), o1.hash);

        // A writer with a stale head cannot append.
        let o2 = SignedOp::sign(next(&o1, 3000), &key);
        assert!(log.append(&o2, Some(&h0)).is_err());
    }

    #[test]
    fn tampering_is_detected() {
        let dir = tempfile::tempdir().unwrap();
        let store = Store::init_bare(&dir.path().join("r.git")).unwrap();
        let key = KeyPair::generate();
        let g = SignedOp::sign(genesis(&key, &store), &key);
        let o1 = SignedOp::sign(next(&g, 2000), &key);

        let mut edited = o1.clone();
        edited.entry.decision = Decision::deny("rewritten");
        assert!(
            verify_chain(&[g.clone(), edited]).is_err(),
            "content change"
        );

        let mut resigned = o1.clone();
        resigned.entry.time_ms = 5000;
        let forged = SignedOp::sign(resigned.entry, &KeyPair::generate());
        assert!(verify_chain(&[g.clone(), forged]).is_err(), "other key");

        let backwards = SignedOp::sign(next(&g, 500), &key);
        assert!(verify_chain(&[g.clone(), backwards]).is_err(), "time");

        let skipped = SignedOp::sign(
            OpEntry {
                seq: 2,
                ..next(&g, 2000)
            },
            &key,
        );
        assert!(verify_chain(&[g, skipped]).is_err(), "seq gap");
    }

    #[test]
    fn format_is_encoded_and_signed_except_for_legacy_entries() {
        let dir = tempfile::tempdir().unwrap();
        let store = Store::init_bare(&dir.path().join("r.git")).unwrap();
        let key = KeyPair::generate();
        let current = genesis(&key, &store);
        let legacy = OpEntry {
            format: FORMAT_LEGACY,
            ..current.clone()
        };
        let enc = |e: &OpEntry| String::from_utf8(canonical_bytes(e)).unwrap();
        assert!(enc(&legacy).starts_with("{\"seq\":0,"), "{}", enc(&legacy));
        assert!(
            enc(&current).starts_with(&format!("{{\"format\":{FORMAT_CURRENT},\"seq\":0,")),
            "{}",
            enc(&current)
        );
        assert_ne!(legacy.digest(), current.digest(), "format is in the digest");

        // Changing the format of a signed entry breaks its signature.
        let mut edited = SignedOp::sign(current, &key);
        edited.entry.format = FORMAT_LEGACY;
        assert!(edited.verify(&key.public_hex()).is_err());

        // A legacy entry round-trips to the same bytes.
        let signed = SignedOp::sign(legacy, &key);
        let bytes = canonical_bytes(&signed);
        let back = decode(&bytes).unwrap();
        assert_eq!(back, signed);
        assert_eq!(canonical_bytes(&back), bytes);
    }

    #[test]
    fn unknown_formats_are_refused_before_decoding() {
        let dir = tempfile::tempdir().unwrap();
        let store = Store::init_bare(&dir.path().join("r.git")).unwrap();
        let key = KeyPair::generate();
        let g = SignedOp::sign(genesis(&key, &store), &key);
        let mut v: serde_json::Value = serde_json::from_slice(&canonical_bytes(&g)).unwrap();
        v["entry"]["format"] = (FORMAT_CURRENT + 1).into();
        // A newer lonca may also have changed the shape of other fields.
        v["entry"]["op"] = serde_json::json!({"type": "from.the.future"});
        let bytes = serde_json::to_vec(&v).unwrap();
        match decode(&bytes) {
            Err(Error::Unsupported(m)) => assert!(
                m.contains(&format!("op 0: log format {}", FORMAT_CURRENT + 1)),
                "{m}"
            ),
            other => panic!("expected Unsupported, got {other:?}"),
        }

        let future = SignedOp::sign(
            OpEntry {
                format: FORMAT_CURRENT + 1,
                ..genesis(&key, &store)
            },
            &key,
        );
        assert!(matches!(
            verify_chain(std::slice::from_ref(&future)),
            Err(Error::Unsupported(_))
        ));
        assert!(matches!(
            OpLog::new(&store).append(&future, None),
            Err(Error::Invalid(_))
        ));
    }

    #[test]
    fn formats_may_rise_along_the_chain_but_never_fall() {
        let dir = tempfile::tempdir().unwrap();
        let store = Store::init_bare(&dir.path().join("r.git")).unwrap();
        let key = KeyPair::generate();
        let with = |e: OpEntry, format| OpEntry { format, ..e };
        let g = SignedOp::sign(with(genesis(&key, &store), FORMAT_LEGACY), &key);
        let o1 = SignedOp::sign(with(next(&g, 2000), FORMAT_LEGACY), &key);
        let o2 = SignedOp::sign(with(next(&o1, 3000), FORMAT_CURRENT), &key);
        assert_eq!(
            verify_chain(&[g.clone(), o1.clone(), o2.clone()]).unwrap(),
            o2.hash
        );

        let back = SignedOp::sign(with(next(&o2, 4000), FORMAT_LEGACY), &key);
        let err = verify_chain(&[g, o1, o2, back]).unwrap_err();
        assert!(
            err.to_string().contains("op 3: log format goes back"),
            "{err}"
        );
    }

    #[test]
    fn the_symbols_engine_is_recorded_exactly_on_proposals_and_integrations() {
        let dir = tempfile::tempdir().unwrap();
        let store = Store::init_bare(&dir.path().join("r.git")).unwrap();
        let key = KeyPair::generate();
        let g = SignedOp::sign(genesis(&key, &store), &key);
        let tree = store.empty_tree().unwrap();
        let integrate = |prev: &SignedOp, format, symbols_engine| OpEntry {
            format,
            symbols_engine,
            op: Op::ChangeIntegrate {
                change: crate::ChangeId::random(),
                trunk_before: tree.clone(),
                merged: None,
                conflicts: vec![],
            },
            ..next(prev, 2000)
        };
        let ok = |e: OpEntry| verify_chain(&[g.clone(), SignedOp::sign(e, &key)]);

        let recorded = integrate(&g, FORMAT_CURRENT, Some(7));
        assert_eq!(recorded.engine(), Some(7));
        assert!(ok(recorded).is_ok());
        assert!(ok(integrate(&g, FORMAT_CURRENT, None)).is_err(), "missing");
        let other = OpEntry {
            symbols_engine: Some(0),
            ..next(&g, 2000)
        };
        assert_eq!(other.engine(), None);
        assert!(ok(other).is_err(), "on an op that uses no engine");

        // Before format 2 the field did not exist; absent means engine 0.
        let old = SignedOp::sign(with_format(genesis(&key, &store), 1), &key);
        let v1 = |e| verify_chain(&[old.clone(), SignedOp::sign(e, &key)]);
        let legacy = integrate(&old, 1, None);
        assert_eq!(legacy.engine(), Some(0));
        assert!(v1(legacy).is_ok());
        assert!(v1(integrate(&old, 1, Some(0))).is_err());
    }

    #[test]
    fn human_key_operations_need_format_3() {
        let dir = tempfile::tempdir().unwrap();
        let store = Store::init_bare(&dir.path().join("r.git")).unwrap();
        let key = KeyPair::generate();
        let g = SignedOp::sign(with_format(genesis(&key, &store), 2), &key);
        let k = || HumanKey {
            alg: "ed25519".into(),
            public: "01".into(),
        };
        let ops = [
            Op::HumanRotate {
                human: "ayse".into(),
                key: k(),
            },
            Op::HumanRevoke {
                human: "ayse".into(),
                reason: "lost".into(),
            },
            Op::HumanRecoveryKey {
                human: "ayse".into(),
                key: k(),
            },
            Op::HumanRecover {
                human: "ayse".into(),
                key: k(),
            },
        ];
        for op in ops {
            let at = |format| OpEntry {
                format,
                op: op.clone(),
                ..next(&g, 2000)
            };
            assert_eq!(at(FORMAT_HUMAN_KEYS).min_format(), FORMAT_HUMAN_KEYS);
            let chain = |format| verify_chain(&[g.clone(), SignedOp::sign(at(format), &key)]);
            assert!(chain(FORMAT_HUMAN_KEYS).is_ok(), "{op:?}");
            let err = chain(FORMAT_HUMAN_KEYS - 1).unwrap_err();
            assert!(
                err.to_string()
                    .contains(&format!("{} needs log format 3", op.kind().as_str())),
                "{err}"
            );
        }
        assert_eq!(next(&g, 2000).min_format(), FORMAT_LEGACY);
    }

    #[test]
    fn continuing_a_session_needs_format_9() {
        let dir = tempfile::tempdir().unwrap();
        let store = Store::init_bare(&dir.path().join("r.git")).unwrap();
        let key = KeyPair::generate();
        let g = SignedOp::sign(with_format(genesis(&key, &store), 2), &key);
        let start = |continues| Op::SessionStart {
            session: "s".into(),
            agent: "bot".into(),
            model: None,
            task: String::new(),
            scope: Scope {
                paths: vec!["**".into()],
                symbols: None,
                ops: vec![],
                max_files: None,
                max_lines: None,
                scratch: vec![],
            },
            expires_ms: 9000,
            continues,
            harness: None,
        };
        let plain = OpEntry {
            format: FORMAT_LEGACY,
            op: start(None),
            ..next(&g, 2000)
        };
        assert_eq!(plain.min_format(), FORMAT_LEGACY);
        let at = |format| OpEntry {
            format,
            op: start(Some(Continuation {
                session: "old".into(),
                widen: false,
            })),
            ..next(&g, 2000)
        };
        assert_eq!(at(9).min_format(), FORMAT_SESSION_CONTINUE);
        let chain = |format| verify_chain(&[g.clone(), SignedOp::sign(at(format), &key)]);
        assert!(chain(9).is_ok());
        let err = chain(8).unwrap_err();
        assert!(err.to_string().contains("needs log format 9"), "{err}");
    }

    /// A harness needs its format whatever else the start carries.
    #[test]
    fn a_named_harness_needs_format_20() {
        let dir = tempfile::tempdir().unwrap();
        let store = Store::init_bare(&dir.path().join("r.git")).unwrap();
        let key = KeyPair::generate();
        let g = SignedOp::sign(with_format(genesis(&key, &store), 2), &key);
        let start = |continues: Option<Continuation>, scratch: Vec<String>| Op::SessionStart {
            session: "s".into(),
            agent: "bot".into(),
            model: None,
            task: String::new(),
            scope: Scope {
                paths: vec!["**".into()],
                symbols: None,
                ops: vec![],
                max_files: None,
                max_lines: None,
                scratch,
            },
            expires_ms: 9000,
            continues,
            harness: Some("codex".into()),
        };
        let continued = Continuation {
            session: "old".into(),
            widen: false,
        };
        for op in [
            start(None, vec![]),
            start(Some(continued), vec![]),
            start(None, vec!["/tmp/x".into()]),
        ] {
            let at = |format| OpEntry {
                format,
                op: op.clone(),
                ..next(&g, 2000)
            };
            assert_eq!(at(20).min_format(), FORMAT_SESSION_HARNESS, "{op:?}");
            let chain = |format| verify_chain(&[g.clone(), SignedOp::sign(at(format), &key)]);
            assert!(chain(20).is_ok());
            let err = chain(19).unwrap_err();
            assert!(err.to_string().contains("needs log format 20"), "{err}");
        }
    }

    /// A `shared` item needs its format, in an acquire or an interest, among
    /// other items or alone; the other kinds keep theirs.
    #[test]
    fn a_shared_lease_needs_format_21() {
        let dir = tempfile::tempdir().unwrap();
        let store = Store::init_bare(&dir.path().join("r.git")).unwrap();
        let key = KeyPair::generate();
        let g = SignedOp::sign(with_format(genesis(&key, &store), 2), &key);
        let items = |kinds: &[LeaseKind]| -> Vec<LeaseItem> {
            let item = |&kind| LeaseItem {
                symbol: "a.rs#f".into(),
                kind,
                names: Vec::new(),
            };
            kinds.iter().map(item).collect()
        };
        let acquire = |items| Op::LeaseAcquire {
            set: "l".into(),
            items,
            intent: "x".into(),
            ttl_ms: 1000,
        };
        let interest = |items| Op::LeaseInterest {
            items,
            intent: "x".into(),
        };
        let at = |op: &Op, format| OpEntry {
            format,
            op: op.clone(),
            ..next(&g, 2000)
        };
        let shared = [LeaseKind::Edit, LeaseKind::Shared];
        for op in [
            acquire(items(&shared)),
            acquire(items(&[LeaseKind::Shared])),
            interest(items(&shared)),
        ] {
            assert_eq!(at(&op, 21).min_format(), FORMAT_SHARED_LEASES, "{op:?}");
            let chain = |format| verify_chain(&[g.clone(), SignedOp::sign(at(&op, format), &key)]);
            assert!(chain(21).is_ok(), "{op:?}");
            let err = chain(20).unwrap_err();
            assert!(err.to_string().contains("needs log format 21"), "{err}");
        }
        let others = [LeaseKind::Edit, LeaseKind::Insert, LeaseKind::Rename];
        assert_eq!(at(&acquire(items(&others)), 2).min_format(), FORMAT_LEGACY);
        assert_eq!(
            at(&interest(items(&others)), 10).min_format(),
            FORMAT_LEASE_QUEUE
        );
    }

    /// Reserved names need their format, in an acquire or an interest, and
    /// come before `shared` in what an entry needs.
    #[test]
    fn reserved_names_need_format_22() {
        let dir = tempfile::tempdir().unwrap();
        let store = Store::init_bare(&dir.path().join("r.git")).unwrap();
        let key = KeyPair::generate();
        let g = SignedOp::sign(with_format(genesis(&key, &store), 2), &key);
        let named = LeaseItem {
            names: vec!["throttle".into()],
            ..LeaseItem::new("a.rs#T", LeaseKind::Insert)
        };
        let shared = LeaseItem::new("a.rs#f", LeaseKind::Shared);
        let acquire = |items| Op::LeaseAcquire {
            set: "l".into(),
            items,
            intent: "x".into(),
            ttl_ms: 1000,
        };
        let interest = |items| Op::LeaseInterest {
            items,
            intent: "x".into(),
        };
        let at = |op: &Op, format| OpEntry {
            format,
            op: op.clone(),
            ..next(&g, 2000)
        };
        for op in [
            acquire(vec![named.clone()]),
            acquire(vec![shared.clone(), named.clone()]),
            interest(vec![named.clone()]),
        ] {
            assert_eq!(at(&op, 22).min_format(), FORMAT_INSERT_NAMES, "{op:?}");
            let chain = |format| verify_chain(&[g.clone(), SignedOp::sign(at(&op, format), &key)]);
            assert!(chain(22).is_ok(), "{op:?}");
            let err = chain(21).unwrap_err();
            assert!(err.to_string().contains("needs log format 22"), "{err}");
        }
        let unnamed = acquire(vec![LeaseItem::new("a.rs#T", LeaseKind::Insert)]);
        assert_eq!(at(&unnamed, 2).min_format(), FORMAT_LEGACY);
        // No names, no field: the bytes of an older item.
        let bytes = serde_json::to_string(&LeaseItem::new("a.rs#T", LeaseKind::Insert)).unwrap();
        assert_eq!(bytes, r#"{"symbol":"a.rs#T","kind":"insert"}"#);
    }

    #[test]
    fn the_lease_queue_needs_format_10() {
        let dir = tempfile::tempdir().unwrap();
        let store = Store::init_bare(&dir.path().join("r.git")).unwrap();
        let key = KeyPair::generate();
        let g = SignedOp::sign(with_format(genesis(&key, &store), 2), &key);
        let scope = Scope {
            paths: vec!["**".into()],
            symbols: None,
            ops: vec![],
            max_files: None,
            max_lines: None,
            scratch: vec![],
        };
        let attenuate = |leases| Op::CapAttenuate {
            parent: "p".into(),
            child: "c".into(),
            agent: "bot".into(),
            scope: scope.clone(),
            expires_ms: 9000,
            leases,
        };
        let negotiate = |to| Op::Negotiate {
            thread: "t".into(),
            to_session: "s".into(),
            message: NegotiationMsg::Yield {
                set: "l".into(),
                to,
            },
        };
        let at = |op: &Op, format| OpEntry {
            format,
            op: op.clone(),
            ..next(&g, 2000)
        };
        for op in [
            Op::LeaseInterest {
                items: vec![],
                intent: String::new(),
            },
            negotiate(Some("s".into())),
            attenuate(vec!["l".into()]),
        ] {
            assert_eq!(at(&op, 10).min_format(), FORMAT_LEASE_QUEUE, "{op:?}");
            let chain = |format| verify_chain(&[g.clone(), SignedOp::sign(at(&op, format), &key)]);
            assert!(chain(10).is_ok(), "{op:?}");
            let err = chain(9).unwrap_err();
            assert!(err.to_string().contains("needs log format 10"), "{err}");
        }
        for op in [negotiate(None), attenuate(vec![])] {
            assert_eq!(at(&op, 2).min_format(), FORMAT_LEGACY, "{op:?}");
        }
    }

    #[test]
    fn isolating_a_session_needs_its_format() {
        let dir = tempfile::tempdir().unwrap();
        let store = Store::init_bare(&dir.path().join("r.git")).unwrap();
        let key = KeyPair::generate();
        let g = SignedOp::sign(with_format(genesis(&key, &store), 2), &key);
        let op = Op::SessionIsolate {
            session: "s".into(),
            sandbox: "seatbelt".into(),
            profile: "ab".repeat(32),
        };
        let at = |format| OpEntry {
            format,
            op: op.clone(),
            ..next(&g, 2000)
        };
        assert_eq!(at(FORMAT_ISOLATE).min_format(), FORMAT_ISOLATE);
        let chain = |format| verify_chain(&[g.clone(), SignedOp::sign(at(format), &key)]);
        assert!(chain(FORMAT_ISOLATE).is_ok());
        let err = chain(FORMAT_ISOLATE - 1).unwrap_err();
        let needs = format!("needs log format {FORMAT_ISOLATE}");
        assert!(err.to_string().contains(&needs), "{err}");
    }

    #[test]
    fn a_batch_needs_its_format() {
        let dir = tempfile::tempdir().unwrap();
        let store = Store::init_bare(&dir.path().join("r.git")).unwrap();
        let key = KeyPair::generate();
        let g = SignedOp::sign(with_format(genesis(&key, &store), 2), &key);
        let tip = GitId::of_blob(b"tip");
        let op = Op::ChangeIntegrateBatch {
            trunk_before: tip.clone(),
            merged: vec![(ChangeId::random(), tip)],
        };
        let at = |format| OpEntry {
            format,
            op: op.clone(),
            symbols_engine: Some(1),
            ..next(&g, 2000)
        };
        assert_eq!(at(FORMAT_BATCH).min_format(), FORMAT_BATCH);
        let chain = |format| verify_chain(&[g.clone(), SignedOp::sign(at(format), &key)]);
        assert!(chain(FORMAT_BATCH).is_ok());
        let err = chain(FORMAT_BATCH - 1).unwrap_err();
        let needs = format!("needs log format {FORMAT_BATCH}");
        assert!(err.to_string().contains(&needs), "{err}");
    }

    #[test]
    fn the_unproposed_observation_needs_format_11() {
        let dir = tempfile::tempdir().unwrap();
        let store = Store::init_bare(&dir.path().join("r.git")).unwrap();
        let key = KeyPair::generate();
        let g = SignedOp::sign(with_format(genesis(&key, &store), 2), &key);
        let observe = |kind| Op::Observe {
            session: "s".into(),
            events: vec![crate::ops::Observation {
                phase: crate::ops::Phase::Post,
                kind,
                external: false,
            }],
        };
        let at = |op: &Op, format| OpEntry {
            format,
            op: op.clone(),
            ..next(&g, 2000)
        };
        let report = observe(ObsKind::Unproposed {
            paths: vec![],
            total: 0,
        });
        assert_eq!(at(&report, 11).min_format(), FORMAT_UNPROPOSED);
        let chain = |format| verify_chain(&[g.clone(), SignedOp::sign(at(&report, format), &key)]);
        assert!(chain(11).is_ok());
        let err = chain(10).unwrap_err();
        assert!(err.to_string().contains("needs log format 11"), "{err}");
        assert_eq!(
            at(&observe(ObsKind::Heartbeat), 2).min_format(),
            FORMAT_LEGACY
        );
    }

    #[test]
    fn the_unhooked_observation_needs_format_24() {
        let dir = tempfile::tempdir().unwrap();
        let store = Store::init_bare(&dir.path().join("r.git")).unwrap();
        let key = KeyPair::generate();
        let g = SignedOp::sign(with_format(genesis(&key, &store), 2), &key);
        let observe = |kinds: Vec<ObsKind>| Op::Observe {
            session: "s".into(),
            events: kinds
                .into_iter()
                .map(|kind| crate::ops::Observation {
                    phase: crate::ops::Phase::Pre,
                    kind,
                    external: false,
                })
                .collect(),
        };
        let at = |op: &Op, format| OpEntry {
            format,
            op: op.clone(),
            ..next(&g, 2000)
        };
        let unhooked = || ObsKind::Unhooked {
            tool: "propose".into(),
        };
        let mark = observe(vec![unhooked()]);
        assert_eq!(at(&mark, 24).min_format(), FORMAT_UNHOOKED);
        // With an older kind beside it, the newer one decides.
        let unproposed = ObsKind::Unproposed {
            paths: vec![],
            total: 0,
        };
        let both = observe(vec![unproposed, unhooked()]);
        assert_eq!(at(&both, 24).min_format(), FORMAT_UNHOOKED);
        let chain = |format| verify_chain(&[g.clone(), SignedOp::sign(at(&mark, format), &key)]);
        assert!(chain(24).is_ok());
        let err = chain(23).unwrap_err();
        assert!(err.to_string().contains("needs log format 24"), "{err}");
    }

    #[test]
    fn named_checks_on_an_approval_need_format_25() {
        let dir = tempfile::tempdir().unwrap();
        let store = Store::init_bare(&dir.path().join("r.git")).unwrap();
        let key = KeyPair::generate();
        let g = SignedOp::sign(with_format(genesis(&key, &store), 2), &key);
        let approve = |checks: &[&str]| Op::Approve {
            target_seq: 1,
            note: String::new(),
            checks: checks.iter().map(|c| c.to_string()).collect(),
        };
        let at = |op: Op, format| OpEntry {
            format,
            op,
            ..next(&g, 2000)
        };
        assert_eq!(at(approve(&["api"]), 25).min_format(), FORMAT_NAMED_CHECKS);
        // Without checks an approval is what it always was.
        assert_eq!(at(approve(&[]), 2).min_format(), FORMAT_LEGACY);
        let chain = |format| {
            let e = SignedOp::sign(at(approve(&["api"]), format), &key);
            verify_chain(&[g.clone(), e])
        };
        assert!(chain(25).is_ok());
        let err = chain(24).unwrap_err();
        assert!(err.to_string().contains("needs log format 25"), "{err}");
        // Old approvals keep their bytes: no `checks` key at all.
        let text = serde_json::to_string(&approve(&[])).unwrap();
        assert!(!text.contains("checks"), "{text}");
    }

    #[test]
    fn daemon_key_operations_need_format_4() {
        let dir = tempfile::tempdir().unwrap();
        let store = Store::init_bare(&dir.path().join("r.git")).unwrap();
        let key = KeyPair::generate();
        let g = SignedOp::sign(with_format(genesis(&key, &store), 3), &key);
        // Denied, so the key in force signs them and nothing switches.
        for op in [
            Op::DaemonRotate { key: "01".into() },
            Op::DaemonRecover { key: "01".into() },
        ] {
            let at = |format| OpEntry {
                format,
                op: op.clone(),
                decision: Decision::deny("test"),
                ..next(&g, 2000)
            };
            assert_eq!(at(FORMAT_DAEMON_KEYS).min_format(), FORMAT_DAEMON_KEYS);
            let chain = |format| verify_chain(&[g.clone(), SignedOp::sign(at(format), &key)]);
            assert!(chain(FORMAT_DAEMON_KEYS).is_ok(), "{op:?}");
            let err = chain(FORMAT_DAEMON_KEYS - 1).unwrap_err();
            assert!(
                err.to_string()
                    .contains(&format!("{} needs log format 4", op.kind().as_str())),
                "{err}"
            );
        }
    }

    /// An entry of `op` after `prev`, signed by `by`.
    fn switch(prev: &SignedOp, op: Op, decision: Decision, by: &KeyPair) -> SignedOp {
        SignedOp::sign(
            OpEntry {
                op,
                decision,
                ..next(prev, prev.entry.time_ms + 1)
            },
            by,
        )
    }

    fn signed_by_error(ops: &[SignedOp]) -> String {
        verify_chain(ops).unwrap_err().to_string()
    }

    #[test]
    fn the_daemon_key_follows_rotations() {
        let dir = tempfile::tempdir().unwrap();
        let store = Store::init_bare(&dir.path().join("r.git")).unwrap();
        let (old, new) = (KeyPair::generate(), KeyPair::generate());
        let g = SignedOp::sign(genesis(&old, &store), &old);
        let rotate = Op::DaemonRotate {
            key: new.public_hex(),
        };
        // The rotation itself is signed by the old key, what follows by the
        // new one.
        let r = switch(&g, rotate.clone(), Decision::allow(), &old);
        assert_eq!(
            r.entry.switches_daemon_key(),
            Some(new.public_hex().as_str())
        );
        let after = SignedOp::sign(next(&r, 3000), &new);
        assert_eq!(
            verify_chain(&[g.clone(), r.clone(), after.clone()]).unwrap(),
            after.hash
        );
        let stale = SignedOp::sign(next(&r, 3000), &old);
        assert!(
            signed_by_error(&[g.clone(), r.clone(), stale]).contains("op 2: bad daemon signature")
        );
        let later = SignedOp::sign(next(&after, 4000), &old);
        assert!(
            signed_by_error(&[g.clone(), r, after, later]).contains("op 3: bad daemon signature")
        );
        let by_new = switch(&g, rotate.clone(), Decision::allow(), &new);
        assert!(signed_by_error(&[g.clone(), by_new]).contains("op 1: bad daemon signature"));

        // A denied rotation switches nothing.
        let denied = switch(&g, rotate, Decision::deny("x"), &old);
        assert_eq!(denied.entry.switches_daemon_key(), None);
        let after = SignedOp::sign(next(&denied, 3000), &old);
        assert!(verify_chain(&[g.clone(), denied.clone(), after]).is_ok());
        let after = SignedOp::sign(next(&denied, 3000), &new);
        assert!(signed_by_error(&[g, denied, after]).contains("op 2: bad daemon signature"));
    }

    #[test]
    fn a_recovery_is_signed_by_the_key_it_installs() {
        let dir = tempfile::tempdir().unwrap();
        let store = Store::init_bare(&dir.path().join("r.git")).unwrap();
        let (old, new) = (KeyPair::generate(), KeyPair::generate());
        let g = SignedOp::sign(genesis(&old, &store), &old);
        let recover = Op::DaemonRecover {
            key: new.public_hex(),
        };
        let r = switch(&g, recover.clone(), Decision::allow(), &new);
        assert_eq!(r.entry.signer(&old.public_hex()), new.public_hex());
        let after = SignedOp::sign(next(&r, 3000), &new);
        assert!(verify_chain(&[g.clone(), r.clone(), after]).is_ok());
        let stale = SignedOp::sign(next(&r, 3000), &old);
        assert!(signed_by_error(&[g.clone(), r, stale]).contains("op 2: bad daemon signature"));

        let by_old = switch(&g, recover.clone(), Decision::allow(), &old);
        assert!(signed_by_error(&[g.clone(), by_old]).contains("op 1: bad daemon signature"));
        // A denied recovery is an ordinary entry of the key in force.
        let denied = switch(&g, recover, Decision::deny("x"), &old);
        assert_eq!(denied.entry.signer(&old.public_hex()), old.public_hex());
        assert!(verify_chain(&[g, denied]).is_ok());
    }

    fn with_format(e: OpEntry, format: u32) -> OpEntry {
        OpEntry { format, ..e }
    }
}