minerva 0.2.0

Causal ordering for distributed systems
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
//! Deterministic collation replay and refusal cases.

use super::*;

/// Typing under a live collation never replays or rebuilds: a birth is
/// pure incremental fold, whatever the move record holds.
#[test]
fn test_collation_follows_typing_without_replay() {
    let clk = clock(1);
    let mut text = Rhapsody::new();
    let dots = typed_chain(&mut text, &clk, 1, 1, 8);
    let mut moves = Metatheses::new();
    assert!(moves.insert(
        d(1, 100),
        Metathesis {
            target: dots[5].into(),
            to: locus_at(&clk, Anchor::After(dots[1].into())),
        }
    ));
    let mut view = text.recension(&moves);
    let (replays, rebuilds) = (view.collation_replays(), view.collation_rebuilds());
    for keystroke in 0..32u64 {
        let _ = typed_chain(&mut text, &clk, 1, 9 + keystroke, 1);
        view.collate(&text, &moves);
        view.check_collation(&text.recension(&moves));
    }
    assert_eq!(view.collation_replays(), replays, "typing never replays");
    assert_eq!(view.collation_rebuilds(), rebuilds, "typing never rebuilds");
}

/// A move arriving below already-collated ranks unwinds the suffix and
/// replays it: the out-of-order delivery the log exists for.
#[test]
fn test_an_out_of_order_move_unwinds_and_replays() {
    let clk = clock(1);
    let mut text = Rhapsody::new();
    let dots = typed_chain(&mut text, &clk, 1, 1, 6);
    // Two testimonies for one target, minted in rank order, delivered in
    // reverse: the higher rank collates first, then the lower arrives.
    let low = Metathesis {
        target: dots[4].into(),
        to: locus_at(&clk, Anchor::After(dots[0].into())),
    };
    let high = Metathesis {
        target: dots[4].into(),
        to: locus_at(&clk, Anchor::After(dots[2].into())),
    };
    let mut only_high = Metatheses::new();
    assert!(only_high.insert(d(1, 101), high));
    let mut view = text.recension(&Metatheses::new());
    view.collate(&text, &only_high);
    view.check_collation(&text.recension(&only_high));
    let mut both = only_high.clone();
    assert!(both.insert(d(1, 100), low));
    let replays = view.collation_replays();
    view.collate(&text, &both);
    view.check_collation(&text.recension(&both));
    assert!(
        view.collation_replays() > replays,
        "a below-rank arrival replays the suffix"
    );
    // The higher rank still wins: the unwound suffix re-earned the same
    // final placement.
    assert_eq!(
        view.locus(dots[4]),
        Some(high.to),
        "the highest-rank surviving testimony wins"
    );
}

/// A birth extending a chain some verdict walked while the link was
/// unborn re-earns that verdict: the collation flips an applied move to
/// refused exactly as the eager replay does.
#[test]
fn test_a_late_birth_reverdicts_a_walked_verdict() {
    let clk = clock(1);
    let mut text = Rhapsody::new();
    let dots = typed_chain(&mut text, &clk, 1, 1, 3);
    let unborn = d(2, 1);
    // The move lands its target after a dot this replica has not woven:
    // the verdict walk ends at the unborn link, so the move applies.
    let mut moves = Metatheses::new();
    assert!(moves.insert(
        d(1, 100),
        Metathesis {
            target: dots[1].into(),
            to: locus_at(&clk, Anchor::After(unborn.into())),
        }
    ));
    let mut view = text.recension(&Metatheses::new());
    view.collate(&text, &moves);
    view.check_collation(&text.recension(&moves));
    assert!(
        view.refused().is_empty(),
        "the walk ends at the unborn link"
    );
    // The link's birth arrives anchored under the moved element, closing
    // the cycle the earlier walk could not see; the collation must flip
    // the verdict without being told which one.
    assert!(text.weave(
        unborn,
        Locus {
            anchor: Anchor::After(dots[1].into()),
            rank: clk.now(0u16),
        }
    ));
    view.collate(&text, &moves);
    view.check_collation(&text.recension(&moves));
    assert_eq!(
        view.refused(),
        &[d(1, 100)],
        "the extended chain refuses the move"
    );
}

/// The property's first counterexample, pinned: a move whose CHILDLESS
/// target lets the near-linearity guard skip the verdict walk entirely,
/// anchored at an unborn dot. Walk-time sensitivity recording never runs
/// (there is no walk), so the fold-time structural rule is what re-earns
/// the verdict when the anchor's birth closes the cycle.
#[test]
fn test_a_guard_skipped_verdict_reverdicts_on_the_births_arrival() {
    let clk = clock(1);
    let mut text = Rhapsody::new();
    // One element, no children: the guard's skip case.
    let target = d(3, 1);
    assert!(text.weave(
        target,
        Locus {
            anchor: Anchor::Origin,
            rank: clk.now(0u16),
        }
    ));
    let unborn = d(1, 1);
    let mut moves = Metatheses::new();
    assert!(moves.insert(
        d(1, 100),
        Metathesis {
            target: target.into(),
            to: locus_at(&clk, Anchor::After(unborn.into())),
        }
    ));
    let mut view = text.recension(&Metatheses::new());
    view.collate(&text, &moves);
    view.check_collation(&text.recension(&moves));
    assert!(view.refused().is_empty(), "no walk ran and no cycle exists");
    // The anchor's birth lands under the moved target: the cycle closes,
    // and the collation must re-earn a verdict it never walked.
    assert!(text.weave(
        unborn,
        Locus {
            anchor: Anchor::After(target.into()),
            rank: clk.now(0u16),
        }
    ));
    view.collate(&text, &moves);
    view.check_collation(&text.recension(&moves));
    assert_eq!(
        view.refused(),
        &[d(1, 100)],
        "the guard-skipped verdict flips with the birth"
    );
}

/// The transitive twin of the guard-skip pin: the unborn link sits one
/// BORN element behind the move's anchor (a dangling weave folded through
/// an earlier collation), the childless target skips the walk, and the
/// link's birth closes the cycle two hops away. Only the fold-time
/// recording of the dangling weave's own reference can re-earn this one:
/// the play's anchor is born, so the play records nothing.
#[test]
fn test_a_dangling_weave_behind_the_anchor_reverdicts_on_its_birth() {
    let clk = clock(1);
    let mut text = Rhapsody::new();
    let target = d(3, 1);
    assert!(text.weave(
        target,
        Locus {
            anchor: Anchor::Origin,
            rank: clk.now(0u16),
        }
    ));
    let mut view = text.recension(&Metatheses::new());
    // The dangling weave arrives through a collation: a born element
    // anchored to an unborn link.
    let unborn = d(1, 1);
    let behind = d(2, 1);
    assert!(text.weave(
        behind,
        Locus {
            anchor: Anchor::After(unborn.into()),
            rank: clk.now(0u16),
        }
    ));
    view.collate(&text, &Metatheses::new());
    // The move anchors at the BORN dangling element; its childless
    // target skips the verdict walk.
    let mut moves = Metatheses::new();
    assert!(moves.insert(
        d(1, 100),
        Metathesis {
            target: target.into(),
            to: locus_at(&clk, Anchor::After(behind.into())),
        }
    ));
    view.collate(&text, &moves);
    view.check_collation(&text.recension(&moves));
    assert!(
        view.refused().is_empty(),
        "the chain ends at the unborn link"
    );
    // The link's birth lands under the moved target: the chain now runs
    // anchor, link, target, and the verdict flips two hops away from
    // anything the play itself recorded.
    assert!(text.weave(
        unborn,
        Locus {
            anchor: Anchor::After(target.into()),
            rank: clk.now(0u16),
        }
    ));
    view.collate(&text, &moves);
    view.check_collation(&text.recension(&moves));
    assert_eq!(
        view.refused(),
        &[d(1, 100)],
        "the dangling weave's fold-time reference re-earns the verdict"
    );
}

/// The construction-time twin of the guard-skip pin: the guard-skipped
/// move anchored at an unborn dot is already in the record when the view
/// is BUILT, so only the whole replay's own play-application recording
/// can re-earn the verdict when the birth arrives. Deliberately a lone
/// sensitive structure: a second one would mask this recording site (its
/// trigger's suffix replay would cover this key too).
#[test]
fn test_a_view_built_over_an_unborn_anchor_reverdicts_on_its_birth() {
    let clk = clock(1);
    let mut text = Rhapsody::new();
    let target = d(3, 1);
    assert!(text.weave(
        target,
        Locus {
            anchor: Anchor::Origin,
            rank: clk.now(0u16),
        }
    ));
    let unborn = d(1, 1);
    let mut moves = Metatheses::new();
    assert!(moves.insert(
        d(1, 100),
        Metathesis {
            target: target.into(),
            to: locus_at(&clk, Anchor::After(unborn.into())),
        }
    ));
    let mut view = text.recension(&moves);
    assert!(
        view.refused().is_empty(),
        "the chain ends at the unborn link"
    );
    assert!(text.weave(
        unborn,
        Locus {
            anchor: Anchor::After(target.into()),
            rank: clk.now(0u16),
        }
    ));
    view.collate(&text, &moves);
    view.check_collation(&text.recension(&moves));
    assert_eq!(
        view.refused(),
        &[d(1, 100)],
        "the whole replay's play-application recording re-earns the verdict"
    );
}

/// The construction-time twin of the dangling-weave pin: the dangling
/// weave behind a guard-skipped move's anchor is already woven when the
/// view is BUILT, so only the whole replay's own birth-fold recording can
/// re-earn the verdict when the link's birth arrives. Deliberately a
/// lone sensitive structure, as above.
#[test]
fn test_a_view_built_over_a_dangling_weave_reverdicts_on_its_birth() {
    let clk = clock(1);
    let mut text = Rhapsody::new();
    let target = d(3, 1);
    assert!(text.weave(
        target,
        Locus {
            anchor: Anchor::Origin,
            rank: clk.now(0u16),
        }
    ));
    let unborn = d(1, 1);
    let behind = d(2, 1);
    assert!(text.weave(
        behind,
        Locus {
            anchor: Anchor::After(unborn.into()),
            rank: clk.now(0u16),
        }
    ));
    let mut moves = Metatheses::new();
    assert!(moves.insert(
        d(1, 100),
        Metathesis {
            target: target.into(),
            to: locus_at(&clk, Anchor::After(behind.into())),
        }
    ));
    let mut view = text.recension(&moves);
    assert!(
        view.refused().is_empty(),
        "the chain ends at the unborn link"
    );
    assert!(text.weave(
        unborn,
        Locus {
            anchor: Anchor::After(target.into()),
            rank: clk.now(0u16),
        }
    ));
    view.collate(&text, &moves);
    view.check_collation(&text.recension(&moves));
    assert_eq!(
        view.refused(),
        &[d(1, 100)],
        "the whole replay's birth-fold recording re-earns the verdict"
    );
}

/// A retracted testimony unwinds through the collation: earlier
/// placements take over, exactly as a fresh replay decides.
#[test]
fn test_an_undone_move_restores_through_the_collation() {
    let clk = clock(1);
    let mut text = Rhapsody::new();
    let dots = typed_chain(&mut text, &clk, 1, 1, 6);
    let mut moves = Metatheses::new();
    assert!(moves.insert(
        d(1, 100),
        Metathesis {
            target: dots[4].into(),
            to: locus_at(&clk, Anchor::After(dots[0].into())),
        }
    ));
    let mut view = text.recension(&moves);
    let rearranged = view.order();
    // The undo is an observed-remove of the testimony: the collation sees
    // the departure and restores the birth order.
    let undone = Metatheses::new();
    view.collate(&text, &undone);
    view.check_collation(&text.recension(&undone));
    assert_eq!(view.order(), text.order(), "the birth order returns");
    assert_ne!(view.order(), rearranged, "the move had moved something");
}

/// A pending testimony (the move delta outran the weave) activates when
/// its target's birth arrives through a collation.
#[test]
fn test_a_pending_move_activates_through_the_collation() {
    let clk = clock(1);
    let mut text = Rhapsody::new();
    let dots = typed_chain(&mut text, &clk, 1, 1, 4);
    let foreign = d(2, 1);
    let mut moves = Metatheses::new();
    assert!(moves.insert(
        d(1, 100),
        Metathesis {
            target: foreign.into(),
            to: locus_at(&clk, Anchor::After(dots[0].into())),
        }
    ));
    let mut view = text.recension(&Metatheses::new());
    view.collate(&text, &moves);
    view.check_collation(&text.recension(&moves));
    assert_eq!(view.pending(), &[d(1, 100)], "the outrun move parks");
    assert!(text.weave(
        foreign,
        Locus {
            anchor: Anchor::After(dots[3].into()),
            rank: clk.now(0u16),
        }
    ));
    view.collate(&text, &moves);
    view.check_collation(&text.recension(&moves));
    assert!(view.pending().is_empty(), "the birth activates the move");
    assert_eq!(
        view.order_at(1),
        Some(foreign),
        "the activated move places its target"
    );
}

/// A condense round shrinks the skeleton, which the log cannot unwind:
/// the collation falls back to one whole replay and stays exact.
#[test]
fn test_a_condense_round_rebuilds_the_collation() {
    use crate::metis::DotStore;
    let clk = clock(1);
    let mut text = Rhapsody::new();
    let dots = typed_chain(&mut text, &clk, 1, 1, 6);
    let mut moves = Metatheses::new();
    assert!(moves.insert(
        d(1, 100),
        Metathesis {
            target: dots[2].into(),
            to: locus_at(&clk, Anchor::After(dots[4].into())),
        }
    ));
    let mut view = text.recension(&moves);
    let rebuilds = view.collation_rebuilds();
    // Delete the tail element through the survivor-law fold (a removal
    // delta: coverage without survival), then excise it under the
    // trusted claim: sterile, invisible, named.
    let tail = dots[5];
    let mut removal_context = DotSet::new();
    let _ = removal_context.insert(tail);
    text.causal_merge_from(&DotSet::new(), &Rhapsody::new(), &removal_context);
    assert!(!text.is_visible(tail), "the tail is a tombstone");
    let excised = text.condense(&Retired::trust(removal_context));
    assert_eq!(excised, 1, "the sterile tail excises");
    view.collate(&text, &moves);
    view.check_collation(&text.recension(&moves));
    assert_eq!(
        view.collation_rebuilds(),
        rebuilds + 1,
        "a shrunken skeleton takes the whole-replay fallback"
    );
}

/// The gate review's P1, pinned verbatim: two moves on one target, the
/// earlier anchored at an unborn dot, the later displacing it elsewhere,
/// then the dot's birth landing beneath the target. The unwind must run
/// BEFORE the birth folds: restoring the earlier displaced locus (target
/// under the now-born dot) into a state that already holds the birth
/// would land the restore inside its own reading region and panic on
/// legal delayed-delivery state. The eager replay refuses the earlier
/// move; the collation must agree, without crashing.
#[test]
fn test_a_late_birth_under_a_twice_moved_target_unwinds_safely() {
    let clk = clock(1);
    let mut text = Rhapsody::new();
    let dots = typed_chain(&mut text, &clk, 3, 1, 4);
    let target = dots[1];
    let unborn = d(1, 1);
    let mut moves = Metatheses::new();
    // The earlier move parks the target under the unborn dot...
    assert!(moves.insert(
        d(1, 100),
        Metathesis {
            target: target.into(),
            to: locus_at(&clk, Anchor::After(unborn.into())),
        }
    ));
    // ...and the later move displaces that placement elsewhere (outside
    // the target's own subtree), so the earlier locus survives only in
    // the unwind log.
    assert!(moves.insert(
        d(1, 101),
        Metathesis {
            target: target.into(),
            to: locus_at(&clk, Anchor::Before(dots[0].into())),
        }
    ));
    let mut view = text.recension(&moves);
    view.check_collation(&text.recension(&moves));
    assert!(view.refused().is_empty(), "both chains end lawfully today");
    // The unborn dot arrives beneath the moved target: the earlier
    // move's verdict re-earns as a refusal, through an unwind whose
    // every restore is a state the log actually passed through.
    assert!(text.weave(
        unborn,
        Locus {
            anchor: Anchor::After(target.into()),
            rank: clk.now(0u16),
        }
    ));
    view.collate(&text, &moves);
    view.check_collation(&text.recension(&moves));
    assert_eq!(
        view.refused(),
        &[d(1, 100)],
        "the earlier move refuses under the extended chain"
    );
}

/// A witness carrying a DIFFERENT testimony than the one collated (two
/// independently constructed records; within one causal system axis
/// law 3 forbids it, but the value is freely constructible) diffs by
/// content and collates as a replacement, never a silent keep: the gate
/// review's finding, the record half.
#[test]
fn test_a_replaced_testimony_collates_as_a_replacement() {
    let clk = clock(1);
    let mut text = Rhapsody::new();
    let dots = typed_chain(&mut text, &clk, 1, 1, 6);
    let witness = d(1, 100);
    let mut first = Metatheses::new();
    assert!(first.insert(
        witness,
        Metathesis {
            target: dots[4].into(),
            to: locus_at(&clk, Anchor::After(dots[0].into())),
        }
    ));
    let mut view = text.recension(&first);
    let first_order = view.order();
    // The same witness dot, a different testimony: a fresh record built
    // apart from the first (presence alone cannot tell them apart).
    let mut second = Metatheses::new();
    assert!(second.insert(
        witness,
        Metathesis {
            target: dots[2].into(),
            to: locus_at(&clk, Anchor::After(dots[5].into())),
        }
    ));
    view.collate(&text, &second);
    view.check_collation(&text.recension(&second));
    assert_ne!(view.order(), first_order, "the replacement re-placed");
}

/// The text half of the same finding: an already-collated dot
/// re-appearing with a different locus violates the collation's honest
/// basis (trait law 3, the same trust `causal_merge` extends), and the
/// debug belt refuses it loudly instead of reading stale content.
#[test]
#[cfg(debug_assertions)]
#[should_panic(expected = "a collated text keeps the honest basis")]
fn test_an_unrelated_text_violates_the_basis_loudly() {
    let clk = clock(1);
    let mut original = Rhapsody::new();
    let dot = d(1, 1);
    assert!(original.weave(
        dot,
        Locus {
            anchor: Anchor::Origin,
            rank: clk.now(0u16),
        }
    ));
    let mut view = original.recension(&Metatheses::new());
    // An unrelated document reusing the dot with a different locus: the
    // woven and visible sets agree, so only the belt can tell.
    let mut unrelated = Rhapsody::new();
    let decoy = d(2, 1);
    assert!(unrelated.weave(
        decoy,
        Locus {
            anchor: Anchor::Origin,
            rank: clk.now(0u16),
        }
    ));
    assert!(unrelated.weave(
        dot,
        Locus {
            anchor: Anchor::After(decoy.into()),
            rank: clk.now(0u16),
        }
    ));
    view.collate(&unrelated, &Metatheses::new());
}