lean-ctx-cloud 3.12.11

The LeanCTX connector: enrollment, bus, knowledge sync, tasks and policy on top of LeanCTX | OSS.
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
// SPDX-License-Identifier: Apache-2.0
//! The driver against an in-memory place: real signed events, a signed
//! manifest, JSON-line segments and tail pages as C0 2.0 section 6 shapes
//! them.

use std::cell::RefCell;

use ed25519_dalek::{Signer as _, SigningKey};
use serde_json::{Value, json};

use super::*;
use crate::capture::CaptureCandidate;
use crate::events::build::{self, Place, Signer};
use crate::events::hlc::Stamp;
use crate::events::sign;
use lean_ctx::core::knowledge::{EvidenceLevel, NodeType};
use lean_ctx::core::sensitivity::SensitivityLevel;

const PLACE: &str = "vault:3f1c2b6a-8d4e-4a7b-9c10-5e2f6a7b8c01";
const SALT: [u8; 32] = [7; 32];

fn instance_key() -> SigningKey {
    SigningKey::from_bytes(&[0x49; 32])
}

fn snapshot_key() -> SigningKey {
    SigningKey::from_bytes(&[0x5a; 32])
}

/// A signed node.add for `statement`, as an engine pushes it.
fn claim(statement: &str, wall_ms: u64) -> Value {
    let c = CaptureCandidate {
        node_type: NodeType::Fact,
        statement: statement.into(),
        steps: vec![],
        commands: vec![],
        refs: vec![],
        evidence: EvidenceLevel::Stated,
        sensitivity: SensitivityLevel::Public,
    };
    let body = build::claim_body(&c, &["https://docs.rs/blake3/".into()], vec![], false).unwrap();
    let key = instance_key();
    let place = Place {
        kind: "vault".into(),
        id: "3f1c2b6a-8d4e-4a7b-9c10-5e2f6a7b8c01".into(),
        pk_id: SALT,
        causal_base: 0,
    };
    let signer = Signer {
        key: &key,
        hlc_node: "6f2b9c4e-1a3d-4e5f-8a7b-0c1d2e3f4a5b".into(),
        worker_id: "wkr_vk4oelpv5lzjmev44hun".into(),
    };
    build::node_add(
        &c,
        &body,
        &place,
        Stamp {
            wall_ms,
            counter: 0,
        },
        &signer,
    )
    .unwrap()
    .0
}

/// The server: its log, how many leading events its snapshot covers, and
/// how many segment fetches to answer with a corrupted record.
struct Server {
    log: RefCell<Vec<Value>>,
    snapshot_upto: usize,
    corrupt_fetches: RefCell<usize>,
    epoch: std::cell::Cell<u64>,
    /// The month's transfer is used up: E4 answers only the purge
    /// tombstones after `since`, `next_since` = `since`.
    quota_used: std::cell::Cell<bool>,
}

impl Server {
    fn new(statements: &[&str], snapshot_upto: usize) -> Self {
        let s = Self {
            log: RefCell::new(Vec::new()),
            snapshot_upto,
            corrupt_fetches: RefCell::new(0),
            epoch: std::cell::Cell::new(0),
            quota_used: std::cell::Cell::new(false),
        };
        for (i, st) in statements.iter().enumerate() {
            s.append(claim(st, 1_792_800_000_000 + i as u64));
        }
        s
    }

    fn append(&self, mut event: Value) -> u64 {
        let mut log = self.log.borrow_mut();
        let seq = log.len() as u64 + 1;
        event["seq"] = json!(seq);
        event["received_at"] = json!(1_792_800_100_000u64 + seq);
        event["provenance"] = json!("own");
        event["review_state"] = json!("confirmed");
        event["reviewed_by"] = json!("none");
        log.push(event);
        seq
    }

    /// A failover to a standby that lacks every event after `keep`; the
    /// promotion bumps the epoch, a backup restore does not.
    fn fail_over(&self, keep: usize, bump_epoch: bool) {
        self.log.borrow_mut().truncate(keep);
        if bump_epoch {
            self.epoch.set(self.epoch.get() + 1);
        }
    }

    /// The key-store row of `event_id` is gone: served without content.
    fn lose_key(&self, event_id: &str) {
        for e in self.log.borrow_mut().iter_mut() {
            if e["event_id"] == event_id {
                e["body"] = Value::Null;
                e["key_lost"] = json!(true);
            }
        }
    }

    fn event_ids(&self) -> std::collections::BTreeSet<String> {
        self.log
            .borrow()
            .iter()
            .map(|e| e["event_id"].as_str().unwrap().to_string())
            .collect()
    }

    fn record(event: &Value) -> Value {
        let mut envelope = sign::envelope(event).unwrap();
        envelope.insert("signature".into(), event["signature"].clone());
        let mut record = json!({
            "seq": event["seq"],
            "envelope": Value::Object(envelope),
            "server_fields": {
                "seq": event["seq"], "received_at": event["received_at"],
                "provenance": event["provenance"], "review_state": event["review_state"],
                "reviewed_by": event["reviewed_by"],
            },
            "body": event["body"],
        });
        // A shredded event before the snapshot rebuild: envelope, no body.
        if event.get("purged").is_some() {
            let obj = record.as_object_mut().unwrap();
            obj.remove("body");
            obj.insert("purged".into(), json!(true));
        }
        record
    }

    /// A purge of `targets` (event ids): their bodies are shredded and the
    /// tombstone is appended, signed like the place's other events.
    fn purge(&self, targets: &[String]) {
        for e in self.log.borrow_mut().iter_mut() {
            if targets.iter().any(|t| e["event_id"] == t.as_str()) {
                e["body"] = Value::Null;
                e["purged"] = json!(true);
            }
        }
        let body = json!({"purge_id": "prg_aaaaaaaaaaaaaaaaaaaaaaezcm", "reason": "leaked_secret",
                          "targets": targets, "cascade": [], "bus_cascade": []});
        let mut tombstone = json!({
            "schema": crate::events::build::EVENT_SCHEMA,
            "event_id": crate::events::ids::new_event_id(1_792_803_000_000).unwrap(),
            "place": {"kind": "vault", "id": "3f1c2b6a-8d4e-4a7b-9c10-5e2f6a7b8c01"},
            "op": "purge",
            "body_commit": sign::body_commit(&SALT, &body).unwrap(),
            "hlc": {"wall_ms": 1_792_803_000_000u64, "counter": 0,
                    "node": "6f2b9c4e-1a3d-4e5f-8a7b-0c1d2e3f4a5b"},
            "actor": {"kind": "server"},
            "body": body,
        });
        sign::sign(&mut tombstone, &instance_key()).unwrap();
        self.append(tombstone);
    }

    fn manifest(&self) -> Value {
        let log = self.log.borrow();
        let covered = &log[..self.snapshot_upto];
        let leaves: Vec<[u8; 32]> = covered
            .iter()
            .map(|e| records::leaf(&Self::record(e)["envelope"]).unwrap())
            .collect();
        let key = snapshot_key();
        let mut m = json!({
            "schema": "leanctx.cloud.snapshot/v1",
            "place": PLACE, "rev": 1, "seq": covered.len(), "epoch": self.epoch.get(),
            "id_salt": hex::encode(SALT),
            "created_at": 1_792_800_200_000u64, "server_time": 1_792_800_200_500u64,
            "segments": [{
                "n": 0, "seq_from": 1, "seq_to": covered.len(), "count": covered.len(),
                "envelope_root": hex::encode(records::root(&leaves).unwrap()),
            }],
            "key_id": sign::key_id(key.verifying_key().as_bytes()),
        });
        let input =
            lean_ctx_protocol::cloud::signing_input_without_signature(records::SNAPSHOT_DOMAIN, &m)
                .unwrap();
        m["signature"] = json!(hex::encode(key.sign(&input).to_bytes()));
        m
    }
}

impl PlaceApi for Server {
    fn get(&self, path: &str) -> Result<Value, CloudError> {
        if path == format!("/places/{PLACE}/snapshot") {
            return Ok(self.manifest());
        }
        if let Some(id) = path
            .strip_prefix(&format!("/places/{PLACE}/events/"))
            .and_then(|rest| rest.strip_suffix("/body"))
        {
            let log = self.log.borrow();
            let e = log.iter().find(|e| e["event_id"] == id).expect("E6 event");
            return Ok(if e.get("key_lost").is_some() {
                json!({"body": null, "key_lost": true})
            } else {
                json!({"body": e["body"]})
            });
        }
        let since: u64 = path
            .split_once("/events?since=")
            .map(|(_, s)| s.parse().unwrap())
            .expect("tail path");
        let log = self.log.borrow();
        let used = self.quota_used.get();
        let events: Vec<Value> = log
            .iter()
            .filter(|e| e["seq"].as_u64().unwrap() > since && (!used || e["op"] == "purge"))
            .take(if used { 100 } else { 2 })
            .cloned()
            .collect();
        let next = if used {
            Some(since)
        } else {
            events.last().map(|e| e["seq"].as_u64().unwrap())
        };
        Ok(
            json!({"epoch": self.epoch.get(), "events": events, "next_since": next,
                  "head_seq": log.len(), "server_time": 0, "withheld": [],
                  "released": [], "held_results": [], "trust_rev": 0}),
        )
    }

    fn get_bytes(&self, path: &str, _max: usize) -> Result<Vec<u8>, CloudError> {
        assert!(path.ends_with("/snapshot/1/segments/0"), "{path}");
        let log = self.log.borrow();
        let mut lines = String::new();
        for (i, e) in log[..self.snapshot_upto].iter().enumerate() {
            let mut r = Self::record(e);
            if i == 1 && *self.corrupt_fetches.borrow() > 0 {
                r["envelope"]["hlc"]["counter"] = json!(9);
            }
            lines.push_str(&serde_json::to_string(&r).unwrap());
            lines.push('\n');
        }
        let mut c = self.corrupt_fetches.borrow_mut();
        *c = c.saturating_sub(1);
        // E3 is served `Content-Encoding: zstd`.
        Ok(zstd::encode_all(lines.as_bytes(), 0).unwrap())
    }

    fn post(&self, path: &str, body: &Value) -> Result<Value, CloudError> {
        assert_eq!(path, format!("/places/{PLACE}/events"));
        let results: Vec<Value> = body["events"]
            .as_array()
            .unwrap()
            .iter()
            .map(|e| {
                // Idempotent by event id; a key_lost event gets its content
                // back from the identical re-push (repair).
                let held = self
                    .log
                    .borrow_mut()
                    .iter_mut()
                    .find(|l| l["event_id"] == e["event_id"])
                    .map(|l| {
                        let repaired = l.get("key_lost").is_some();
                        if repaired {
                            l["body"] = e["body"].clone();
                            l.as_object_mut().unwrap().remove("key_lost");
                        }
                        (l["seq"].clone(), repaired)
                    });
                match held {
                    Some((seq, true)) => json!({"event_id": e["event_id"], "seq": seq,
                        "status": "accepted", "review_state": "confirmed"}),
                    Some((seq, false)) => {
                        json!({"event_id": e["event_id"], "seq": seq, "status": "duplicate"})
                    }
                    None => {
                        let seq = self.append(e.clone());
                        json!({"event_id": e["event_id"], "seq": seq, "status": "accepted",
                               "review_state": "confirmed"})
                    }
                }
            })
            .collect();
        Ok(json!({"epoch": self.epoch.get(), "results": results, "server_time": 0}))
    }
}

fn verifier_parts() -> ([[u8; 32]; 1], [u8; 32]) {
    (
        [*snapshot_key().verifying_key().as_bytes()],
        *instance_key().verifying_key().as_bytes(),
    )
}

const STATEMENTS: [&str; 7] = [
    "The relay drops frames when the link is slow.",
    "The outbox retries a refused push after the lease is renewed.",
    "Sealed vaults wrap every content key with the epoch key.",
    "The scheduler tails every replica on each tick.",
    "Snapshot segments are verified against the signed manifest.",
    "A purge removes the body but keeps the signed envelope.",
    "Capture counts never carry any text from rejected facts.",
];

#[test]
fn a_clone_takes_the_snapshot_then_the_tail() {
    let server = Server::new(&STATEMENTS, 5);
    let data = tempfile::tempdir().unwrap();
    let (mut replica, _) = Replica::open(data.path(), "pl_clone", &[1; 32]).unwrap();
    let (snap, inst) = verifier_parts();
    let ok = |e: &Value| sign::verify(e, &inst);
    let v = Verifier {
        snapshot_keys: &snap,
        signature_ok: &ok,
    };
    let report = clone_place(&server, &mut replica, PLACE, &v).unwrap();
    assert!(report.cloned);
    assert_eq!(report.stored, 7);
    assert_eq!(report.cursor, Some(7));
    let graph = replica.refold().unwrap();
    assert_eq!(graph.live_nodes().count(), 7);
    assert_eq!(replica.id_salt().unwrap(), Some(SALT));

    // Nothing new: a tail stores nothing and keeps the cursor.
    let again = tail(&server, &mut replica, PLACE, &v).unwrap();
    assert_eq!((again.stored, again.cursor), (0, Some(7)));
}

/// T-SYNC-3 (SPEC-ENGINE §6.2, §3.4.3): a purge of 3 events in a 10 000-event
/// vault, then a clone before the snapshot rebuild. The zstd record stream
/// carries the purged envelopes as stubs, every root still matches, the 3
/// events fold as purged; a segment with a flipped envelope byte on every
/// fetch stops the clone with `segment_unavailable`.
#[test]
fn a_clone_before_the_snapshot_rebuild_folds_the_purge() {
    let texts: Vec<String> = (0..10_000)
        .map(|i| format!("Claim number {i} of the large vault holds true."))
        .collect();
    let refs: Vec<&str> = texts.iter().map(String::as_str).collect();
    let mut server = Server::new(&refs, 0);
    let ids: Vec<String> = [10, 20, 30]
        .iter()
        .map(|i| {
            server.log.borrow()[*i]["event_id"]
                .as_str()
                .unwrap()
                .to_string()
        })
        .collect();
    server.purge(&ids);
    server.snapshot_upto = server.log.borrow().len();
    let (snap, inst) = verifier_parts();
    let ok = |e: &Value| sign::verify(e, &inst);
    let v = Verifier {
        snapshot_keys: &snap,
        signature_ok: &ok,
    };
    let data = tempfile::tempdir().unwrap();
    let (mut replica, _) = Replica::open(data.path(), "pl_big", &[1; 32]).unwrap();
    let report = clone_place(&server, &mut replica, PLACE, &v).unwrap();
    assert_eq!(report.stored, 10_001);
    let graph = replica.refold().unwrap();
    assert_eq!(graph.purged_nodes.len(), 3);
    assert_eq!(graph.live_nodes().count(), 9_997);
    assert_eq!(graph.tombstones.len(), 1);

    *server.corrupt_fetches.borrow_mut() = 2;
    let (mut other, _) = Replica::open(data.path(), "pl_big_bad", &[1; 32]).unwrap();
    let err = clone_place(&server, &mut other, PLACE, &v).unwrap_err();
    assert!(err.to_string().contains("segment_unavailable"), "{err}");
    assert!(other.fold_events().unwrap().is_empty());
}

#[test]
fn a_bad_segment_is_fetched_once_more_then_refused() {
    let (snap, inst) = verifier_parts();
    let ok = |e: &Value| sign::verify(e, &inst);
    let v = Verifier {
        snapshot_keys: &snap,
        signature_ok: &ok,
    };
    let data = tempfile::tempdir().unwrap();

    let flaky = Server::new(&STATEMENTS, 5);
    *flaky.corrupt_fetches.borrow_mut() = 1;
    let (mut replica, _) = Replica::open(data.path(), "pl_flaky", &[1; 32]).unwrap();
    assert_eq!(
        clone_place(&flaky, &mut replica, PLACE, &v).unwrap().stored,
        7
    );

    let broken = Server::new(&STATEMENTS, 5);
    *broken.corrupt_fetches.borrow_mut() = 2;
    let (mut replica, _) = Replica::open(data.path(), "pl_broken", &[1; 32]).unwrap();
    let err = clone_place(&broken, &mut replica, PLACE, &v).unwrap_err();
    assert!(err.to_string().contains("segment_unavailable"), "{err}");
    assert!(
        replica.fold_events().unwrap().is_empty(),
        "nothing of it stored"
    );

    // A manifest signed by another key is refused before any segment.
    let other = [*SigningKey::from_bytes(&[1; 32]).verifying_key().as_bytes()];
    let wrong = Verifier {
        snapshot_keys: &other,
        signature_ok: &ok,
    };
    let (mut replica, _) = Replica::open(data.path(), "pl_wrong", &[1; 32]).unwrap();
    assert!(clone_place(&Server::new(&STATEMENTS, 5), &mut replica, PLACE, &wrong).is_err());
}

/// Two engines clone the same place; one pushes while offline-queued, both
/// tail: they converge on the same graph, and the pusher's pending queue
/// empties only once the log delivers its event.
#[test]
fn two_engines_converge_on_the_same_graph() {
    let server = Server::new(&STATEMENTS[..4], 4);
    let (snap, inst) = verifier_parts();
    let ok = |e: &Value| sign::verify(e, &inst);
    let v = Verifier {
        snapshot_keys: &snap,
        signature_ok: &ok,
    };
    let data = tempfile::tempdir().unwrap();
    let (mut a, _) = Replica::open(data.path(), "pl_a", &[1; 32]).unwrap();
    let (mut b, _) = Replica::open(&data.path().join("b"), "pl_a", &[2; 32]).unwrap();
    clone_place(&server, &mut a, PLACE, &v).unwrap();
    clone_place(&server, &mut b, PLACE, &v).unwrap();

    let own = claim(STATEMENTS[4], 1_792_800_900_000);
    let id = own["event_id"].as_str().unwrap().to_string();
    a.add_pending(
        &id,
        1_792_800_900_000,
        0,
        None,
        &serde_json::to_vec(&own).unwrap(),
    )
    .unwrap();
    let pushed = push_pending(&server, &a, PLACE, &[]).unwrap();
    assert_eq!((pushed.sent, pushed.logged), (1, 1));
    assert_eq!(
        a.pending().unwrap().len(),
        1,
        "pending until the tail has it"
    );

    // The other engine pushes its own claim before it has seen the first.
    let theirs = claim(STATEMENTS[5], 1_792_800_900_001);
    let their_id = theirs["event_id"].as_str().unwrap().to_string();
    b.add_pending(
        &their_id,
        1_792_800_900_001,
        0,
        None,
        &serde_json::to_vec(&theirs).unwrap(),
    )
    .unwrap();
    assert_eq!(push_pending(&server, &b, PLACE, &[]).unwrap().logged, 1);

    tail(&server, &mut a, PLACE, &v).unwrap();
    tail(&server, &mut b, PLACE, &v).unwrap();
    assert!(a.pending().unwrap().is_empty());
    assert!(b.pending().unwrap().is_empty());
    let (ga, gb) = (a.refold().unwrap(), b.refold().unwrap());
    assert_eq!(ga, gb);
    assert_eq!(ga.digest(), gb.digest(), "`cloud replica` shows one digest");
    // B knows A's claim for capture dedupe without ever having captured it.
    let dk = crate::capture::dedupe::exact_key(&SALT, NodeType::Fact, STATEMENTS[4]);
    assert!(b.dedupe_node(&dk).unwrap().is_some());
    assert_eq!(ga.live_nodes().count(), 6);
}

#[test]
fn a_forged_tail_event_is_stored_but_never_folded() {
    let server = Server::new(&STATEMENTS[..2], 2);
    let (snap, inst) = verifier_parts();
    let ok = |e: &Value| sign::verify(e, &inst);
    let v = Verifier {
        snapshot_keys: &snap,
        signature_ok: &ok,
    };
    let data = tempfile::tempdir().unwrap();
    let (mut r, _) = Replica::open(data.path(), "pl_forge", &[1; 32]).unwrap();
    clone_place(&server, &mut r, PLACE, &v).unwrap();
    let mut forged = claim(STATEMENTS[2], 1_792_800_950_000);
    forged["body"]["statement"] = json!("A different statement slipped in by someone.");
    server.append(forged);
    let report = tail(&server, &mut r, PLACE, &v).unwrap();
    assert_eq!(report.stored, 1);
    assert_eq!(r.refold().unwrap().live_nodes().count(), 2);
}

/// An event whose signer key was not usable yet (its worker registered
/// after the key was cached) is stored unverified; once the key verifies,
/// the re-check lets it fold. A forged body stays unverified.
#[test]
fn unverified_events_fold_after_a_later_check_passes() {
    let server = Server::new(&STATEMENTS[..3], 2);
    server.append({
        let mut forged = claim(STATEMENTS[3], 1_792_800_960_000);
        forged["body"]["statement"] = json!("Not what was signed.");
        forged
    });
    let (snap, inst) = verifier_parts();
    let unknown_yet = |_: &Value| false;
    let v = Verifier {
        snapshot_keys: &snap,
        signature_ok: &unknown_yet,
    };
    let data = tempfile::tempdir().unwrap();
    let (mut r, _) = Replica::open(data.path(), "pl_recheck", &[1; 32]).unwrap();
    clone_place(&server, &mut r, PLACE, &v).unwrap();
    assert_eq!(r.refold().unwrap().live_nodes().count(), 0);
    let stored = r.unverified().unwrap();
    assert_eq!(stored.len(), 4);
    assert!(stored.iter().all(|(_, e)| e.get("body").is_some()));

    let ok = |e: &Value| sign::verify(e, &inst);
    for (id, event) in stored {
        if records::event_verifies(&event, &SALT, &ok) {
            r.mark_verified(&id).unwrap();
        }
    }
    assert_eq!(r.refold().unwrap().live_nodes().count(), 3);
    assert_eq!(r.unverified().unwrap().len(), 1, "the forged one stays out");
}

/// Queue `n` own claims in `replica` and push them.
fn push_own(server: &Server, replica: &Replica, tag: &str, n: u64) -> Vec<String> {
    let mut ids = Vec::new();
    for i in 0..n {
        let wall = 1_792_801_000_000 + i;
        let e = claim(
            &format!("The {tag} engine learned claim number {i} about the relay."),
            wall,
        );
        let id = e["event_id"].as_str().unwrap().to_string();
        replica
            .add_pending(&id, wall, 0, None, &serde_json::to_vec(&e).unwrap())
            .unwrap();
        ids.push(id);
    }
    push_pending(server, replica, PLACE, &[]).unwrap();
    ids
}

/// T-EPOCH-REPUSH (SPEC-ENGINE §6.2, R48), in process: A and B push, the
/// place fails over to a standby that lost the last events and bumps its
/// epoch, someone else writes into the reused seqs; each engine re-pushes
/// its 24-h history once, re-tails, drops the rows whose seq changed, and
/// both converge on the place's log with the union of their events.
#[test]
fn a_failover_is_reconciled_from_both_histories() {
    let server = Server::new(&STATEMENTS[..4], 4);
    let (snap, inst) = verifier_parts();
    let ok = |e: &Value| sign::verify(e, &inst);
    let v = Verifier {
        snapshot_keys: &snap,
        signature_ok: &ok,
    };
    let data = tempfile::tempdir().unwrap();
    let (mut a, _) = Replica::open(data.path(), "pl_ea", &[1; 32]).unwrap();
    let (mut b, _) = Replica::open(data.path(), "pl_eb", &[2; 32]).unwrap();
    clone_place(&server, &mut a, PLACE, &v).unwrap();
    clone_place(&server, &mut b, PLACE, &v).unwrap();
    let mine = push_own(&server, &a, "first", 3);
    let theirs = push_own(&server, &b, "second", 2);
    tail(&server, &mut a, PLACE, &v).unwrap();
    tail(&server, &mut b, PLACE, &v).unwrap();
    let before = server.event_ids();
    assert_eq!(before.len(), 9);
    assert_eq!(a.epoch().unwrap(), Some(0));

    // The standby lacks the last 4 events (2 of A, 2 of B); someone else
    // writes seq 6 on the promoted primary.
    server.fail_over(5, true);
    server.append(claim(STATEMENTS[6], 1_792_802_000_000));

    let ra = tail(&server, &mut a, PLACE, &v).unwrap();
    assert!(ra.reconciled);
    assert_eq!(ra.repushed, 3, "A re-pushes its whole history once");
    assert!(ra.dropped >= 1, "A's rows whose seq changed are dropped");
    let rb = tail(&server, &mut b, PLACE, &v).unwrap();
    assert!(rb.reconciled);
    assert_eq!(rb.repushed, 2);
    // No new trigger: A only tails what B brought back.
    let again = tail(&server, &mut a, PLACE, &v).unwrap();
    assert!(!again.reconciled);

    let after = server.event_ids();
    assert!(before.is_subset(&after), "every pushed event is held again");
    for id in mine.iter().chain(&theirs) {
        assert!(after.contains(id));
    }
    let (ga, gb) = (a.refold().unwrap(), b.refold().unwrap());
    assert_eq!(ga, gb);
    assert_eq!(ga.digest(), gb.digest());
    assert_eq!(ga.live_nodes().count(), 10);
    assert_eq!(a.epoch().unwrap(), Some(1));
    assert_eq!(b.epoch().unwrap(), Some(1));
    assert_eq!(a.cursor().unwrap(), Some(10));
}

/// A backup restore: the log's `head_seq` drops below the cursor without an
/// epoch change, and the same reconcile runs.
#[test]
fn a_restored_log_below_the_cursor_is_reconciled() {
    let server = Server::new(&STATEMENTS[..3], 3);
    let (snap, inst) = verifier_parts();
    let ok = |e: &Value| sign::verify(e, &inst);
    let v = Verifier {
        snapshot_keys: &snap,
        signature_ok: &ok,
    };
    let data = tempfile::tempdir().unwrap();
    let (mut a, _) = Replica::open(data.path(), "pl_ra", &[1; 32]).unwrap();
    clone_place(&server, &mut a, PLACE, &v).unwrap();
    push_own(&server, &a, "restored", 2);
    tail(&server, &mut a, PLACE, &v).unwrap();
    assert_eq!(a.cursor().unwrap(), Some(5));
    server.fail_over(3, false);
    let r = tail(&server, &mut a, PLACE, &v).unwrap();
    assert!(r.reconciled);
    assert_eq!(r.repushed, 2);
    assert_eq!(server.event_ids().len(), 5);
    assert_eq!(a.cursor().unwrap(), Some(5));
    assert_eq!(a.epoch().unwrap(), Some(0), "no epoch change");
    assert!(a.pending().unwrap().is_empty());
}

/// A `key_lost` event is stored without content and never folded; its
/// author re-pushes it from the history at once and the place has its
/// content back.
#[test]
fn a_key_lost_event_is_kept_out_and_repaired_by_its_author() {
    let server = Server::new(&STATEMENTS[..2], 2);
    let (snap, inst) = verifier_parts();
    let ok = |e: &Value| sign::verify(e, &inst);
    let v = Verifier {
        snapshot_keys: &snap,
        signature_ok: &ok,
    };
    let data = tempfile::tempdir().unwrap();
    let (mut a, _) = Replica::open(data.path(), "pl_ka", &[1; 32]).unwrap();
    let (mut b, _) = Replica::open(data.path(), "pl_kb", &[2; 32]).unwrap();
    clone_place(&server, &mut a, PLACE, &v).unwrap();
    clone_place(&server, &mut b, PLACE, &v).unwrap();
    let lost = push_own(&server, &a, "lost", 1).remove(0);
    server.lose_key(&lost);

    let rb = tail(&server, &mut b, PLACE, &v).unwrap();
    assert_eq!(rb.repushed, 0, "B holds no copy of A's event");
    assert_eq!(b.key_lost_ids().unwrap(), vec![lost.clone()]);
    assert_eq!(b.refold().unwrap().live_nodes().count(), 2, "never folded");

    let ra = tail(&server, &mut a, PLACE, &v).unwrap();
    assert_eq!(ra.repushed, 1, "A repairs it from its history");
    {
        let held = server.log.borrow();
        let repaired = held
            .iter()
            .find(|e| e["event_id"] == lost.as_str())
            .unwrap();
        assert!(repaired.get("key_lost").is_none());
        assert!(!repaired["body"].is_null());
    }

    // The next tail of each engine fetches the content again (E6) and folds it.
    tail(&server, &mut a, PLACE, &v).unwrap();
    tail(&server, &mut b, PLACE, &v).unwrap();
    assert!(b.key_lost_ids().unwrap().is_empty());
    assert!(a.key_lost_ids().unwrap().is_empty());
    let (ga, gb) = (a.refold().unwrap(), b.refold().unwrap());
    assert_eq!(gb.live_nodes().count(), 3);
    assert_eq!(ga.digest(), gb.digest());
}

/// E2E-FINAL: with the month's transfer used up the tail carries only the
/// purge tombstones (`next_since` = `since`). They are applied on top of the
/// replica's graph and its purged bodies shredded, but never stored: the
/// cursor stays, so the events between arrive with the next full tail. An
/// ordinary page answers `None` (the quota is back).
#[test]
fn deletions_apply_while_transfer_is_used_up_without_moving_the_cursor() {
    let server = Server::new(&STATEMENTS[..5], 5);
    let data = tempfile::tempdir().unwrap();
    let (mut replica, _) = Replica::open(data.path(), "pl_quota", &[1; 32]).unwrap();
    let (snap, inst) = verifier_parts();
    let ok = |e: &Value| sign::verify(e, &inst);
    let v = Verifier {
        snapshot_keys: &snap,
        signature_ok: &ok,
    };
    clone_place(&server, &mut replica, PLACE, &v).unwrap();
    assert_eq!(replica.cursor().unwrap(), Some(5));
    // One more claim arrives, then a purge of the second one.
    server.append(claim(STATEMENTS[5], 1_792_800_900_000));
    let target = server.log.borrow()[1]["event_id"]
        .as_str()
        .unwrap()
        .to_string();
    server.purge(std::slice::from_ref(&target));
    server.quota_used.set(true);
    let graph = deletions(&server, &mut replica, PLACE, &v)
        .unwrap()
        .expect("a deletions-only page");
    assert_eq!(graph.tombstones.len(), 1);
    assert_eq!(graph.purged_nodes.len(), 1);
    assert!(graph.purged_events.contains(&target));
    assert_eq!(replica.cursor().unwrap(), Some(5), "the cursor stays");
    let shredded = replica
        .fold_events()
        .unwrap()
        .into_iter()
        .find(|e| e.event_id == target)
        .unwrap();
    assert!(
        shredded.body.is_none(),
        "the purged body is shredded at once"
    );
    // A forged tombstone is never applied.
    let (mut other, _) = Replica::open(data.path(), "pl_quota_forged", &[1; 32]).unwrap();
    server.quota_used.set(false);
    let snapshot_only = Server::new(&STATEMENTS[..5], 5);
    clone_place(&snapshot_only, &mut other, PLACE, &v).unwrap();
    server.quota_used.set(true);
    let none = |_: &Value| false;
    let strict = Verifier {
        snapshot_keys: &snap,
        signature_ok: &none,
    };
    let forged = deletions(&server, &mut other, PLACE, &strict)
        .unwrap()
        .unwrap();
    assert!(forged.purged_events.is_empty() && forged.tombstones.is_empty());
    // The quota is back: an ordinary page, then the full tail brings all.
    server.quota_used.set(false);
    assert!(
        deletions(&server, &mut replica, PLACE, &v)
            .unwrap()
            .is_none()
    );
    tail(&server, &mut replica, PLACE, &v).unwrap();
    assert_eq!(replica.cursor().unwrap(), Some(7));
    assert_eq!(replica.refold().unwrap().tombstones.len(), 1);
}

/// One sync of an Instance as `sync_vault_once` orders it: pull, capture
/// `fact` (if any) with the position seen before the pull, push and tail,
/// then the fact links, pushed and tailed.
fn sync_with_fact(
    server: &Server,
    replica: &mut Replica,
    v: &Verifier<'_>,
    project_hash: &str,
    checkout: &mut crate::capture::refs::Checkout,
    fact: Option<&crate::capture::CapturedFact>,
    wall_ms: u64,
) {
    use crate::capture::{conflicts, emit};
    let key = instance_key();
    let signer = Signer {
        key: &key,
        hlc_node: "6f2b9c4e-1a3d-4e5f-8a7b-0c1d2e3f4a5b".into(),
        worker_id: "wkr_vk4oelpv5lzjmev44hun".into(),
    };
    let place_at = |causal_base| Place {
        kind: "vault".into(),
        id: "3f1c2b6a-8d4e-4a7b-9c10-5e2f6a7b8c01".into(),
        pk_id: SALT,
        causal_base,
    };
    let clock = Stamp {
        wall_ms,
        counter: 0,
    };
    let (_, seen) = crate::sync::place::pull(server, replica, PLACE, v).unwrap();
    if let Some(fact) = fact {
        let place = place_at(seen);
        let target = emit::Target {
            replica,
            place: &place,
            signer: &signer,
            project_hash,
            decision_anchors: false,
            sessions: None,
        };
        let emitted = emit::emit(
            &target,
            checkout,
            std::slice::from_ref(fact),
            clock,
            wall_ms,
        )
        .unwrap();
        assert_eq!(emitted.queued.len(), 1);
    }
    push_pending(server, replica, PLACE, &[]).unwrap();
    tail(server, replica, PLACE, v).unwrap();
    let place = place_at(replica.cursor().unwrap().unwrap_or(0));
    let target = crate::review::maintain::Target {
        replica,
        place: &place,
        signer: &signer,
    };
    conflicts::detect(&target, &SALT, clock, wall_ms + 1).unwrap();
    push_pending(server, replica, PLACE, &[]).unwrap();
    tail(server, replica, PLACE, v).unwrap();
}

/// Review r2 (#286): Instance A syncs a fact's value v1; Instance B wrote
/// v2 before it had seen v1 and syncs later. B's sync pulls v1 before it
/// pushes v2, yet v2 was written without it: exactly one `contradicts`, no
/// `supersedes`. Had B pulled v1 in an earlier sync before writing v2, the
/// same values are an edit: one `supersedes`, no conflict.
#[test]
fn a_fact_written_before_its_sync_pulled_the_other_value_is_a_conflict() {
    let fact = |value: &str| crate::capture::CapturedFact {
        candidate: CaptureCandidate {
            node_type: NodeType::Fact,
            statement: value.into(),
            steps: vec![],
            commands: vec![],
            refs: vec![],
            evidence: EvidenceLevel::Observed,
            sensitivity: SensitivityLevel::Public,
        },
        refs: vec!["file:src/lib.rs".into()],
        category: "arch".into(),
        key: "relay-retries".into(),
        value: value.into(),
        derived_from: vec![],
        session: None,
    };
    let v1 = fact("The relay retries a dropped frame three times.");
    let v2 = fact("The relay retries a dropped frame five times.");
    let (snap, inst) = verifier_parts();
    let ok = |e: &Value| sign::verify(e, &inst);
    let v = Verifier {
        snapshot_keys: &snap,
        signature_ok: &ok,
    };
    for b_saw_v1 in [false, true] {
        let server = Server::new(&STATEMENTS[..1], 1);
        let (repo_a, repo_b) = (crate::capture::tests::repo(), crate::capture::tests::repo());
        let data = tempfile::tempdir().unwrap();
        let (mut a, _) = Replica::open(&data.path().join("a"), "pl_a", &[1; 32]).unwrap();
        let (mut b, _) = Replica::open(&data.path().join("b"), "pl_b", &[2; 32]).unwrap();
        let mut checkout_a = crate::capture::refs::Checkout::new(repo_a.path());
        let mut checkout_b = crate::capture::refs::Checkout::new(repo_b.path());
        let t = 1_792_801_000_000;
        // Both Instances are on the place before either writes the fact.
        sync_with_fact(&server, &mut a, &v, "ph-a", &mut checkout_a, None, t);
        sync_with_fact(&server, &mut b, &v, "ph-b", &mut checkout_b, None, t + 10);
        sync_with_fact(
            &server,
            &mut a,
            &v,
            "ph-a",
            &mut checkout_a,
            Some(&v1),
            t + 20,
        );
        if b_saw_v1 {
            sync_with_fact(&server, &mut b, &v, "ph-b", &mut checkout_b, None, t + 30);
        }
        sync_with_fact(
            &server,
            &mut b,
            &v,
            "ph-b",
            &mut checkout_b,
            Some(&v2),
            t + 40,
        );
        // Each Instance syncs once more: whoever writes the link has.
        sync_with_fact(&server, &mut a, &v, "ph-a", &mut checkout_a, None, t + 50);
        sync_with_fact(&server, &mut b, &v, "ph-b", &mut checkout_b, None, t + 60);

        let (ga, gb) = (a.refold().unwrap(), b.refold().unwrap());
        assert_eq!(ga.digest(), gb.digest(), "both Instances fold one graph");
        let count = |kind: &str| {
            ga.edges
                .values()
                .filter(|e| e.kind == kind && !e.retracted)
                .count()
        };
        let expected = if b_saw_v1 { (0, 1) } else { (1, 0) };
        assert_eq!(
            (count("contradicts"), count("supersedes")),
            expected,
            "b_saw_v1={b_saw_v1}"
        );
    }
}