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
//! Movement flows: the identity-fork exhibit flipped, the fixed-rule
//! conflict verdicts, undo, tidy re-moves, marks over moves, and the
//! out-of-order surfaces.

extern crate alloc;

use alloc::vec::Vec;

use crate::metis::{
    Anchor, Composer, DotSet, Extent, Metatheses, Metathesis, Rhapsody, Scholion, Verge,
};

use super::{Moves, Text, clock, d, locus_at, move_to, type_after};

/// Unplaced birth cycles are accepted `Rhapsody` state. Moving a placed
/// subtree beneath one does not form a cycle through the move target, so the
/// bounded validator must terminate without refusing the testimony.
#[test]
fn test_a_move_into_an_unplaced_birth_cycle_is_not_refused() {
    let clk = clock(1);
    let target = d(1, 1);
    let child = d(1, 2);
    let first_unplaced = d(2, 1);
    let second_unplaced = d(2, 2);
    let mut text = Rhapsody::new();

    for (dot, anchor) in [
        (target, Anchor::Origin),
        (child, Anchor::After(target.into())),
        (first_unplaced, Anchor::After(second_unplaced.into())),
        (second_unplaced, Anchor::After(first_unplaced.into())),
    ] {
        assert!(text.weave(dot, locus_at(&clk, anchor)));
    }
    assert_eq!(text.order(), [target, child]);

    let mut moves = Metatheses::new();
    let witness = d(3, 1);
    let destination = locus_at(&clk, Anchor::After(first_unplaced.into()));
    assert!(moves.insert(
        witness,
        Metathesis {
            target: target.into(),
            to: destination,
        },
    ));

    let recension = text.recension(&moves);
    assert!(recension.refused().is_empty());
    assert!(recension.pending().is_empty());
    assert_eq!(recension.locus(target), Some(destination));
    assert!(recension.order().is_empty());
}

/// THE exhibit, flipped (alma COLLAB-8; PRD 0019 stage A's identity fork).
/// A concurrent edit anchored to a moved element follows it to its new
/// place, because the move preserved the identity the edit anchored to.
/// Under move-as-delete-plus-reinsert the same script converges with the
/// edit stranded at the old identity's tombstone.
#[test]
fn test_the_exhibit_flips_a_concurrent_edit_follows_the_move() {
    let mut text_a: Text = Composer::new(1);
    let mut moves_a: Moves = Composer::new(1);
    let mut text_b: Text = Composer::new(2);
    let mut moves_b: Moves = Composer::new(2);
    let clk_a = clock(1);
    let clk_b = clock(2);

    // Alice types "a b"; bob learns it.
    let first = type_after(&mut text_a, &clk_a, None);
    let second = type_after(&mut text_a, &clk_a, Some(first.into()));
    let _ = text_b.absorb(1, &text_a.owed_to(text_b.state().context()));

    // Concurrently: alice MOVES the second element to the front (a
    // testimony, identity preserved), while bob TYPES after it.
    let (_, move_delta) = move_to(
        &mut moves_a,
        second.into(),
        locus_at(&clk_a, Anchor::Before(first.into())),
    );
    let edit = type_after(&mut text_b, &clk_b, Some(second.into()));

    // Converge both pairs.
    let _ = text_a.absorb(2, &text_b.owed_to(text_a.state().context()));
    let _ = text_b.absorb(1, &text_a.owed_to(text_b.state().context()));
    let _ = moves_b.absorb(1, &move_delta);
    assert_eq!(text_a.state(), text_b.state());
    assert_eq!(moves_a.state().store(), moves_b.state().store());

    // The recension at both replicas: the moved element reads at the
    // front, and the concurrent edit followed it there. Nothing strands,
    // nothing is refused, and the canonical store still reads the birth
    // order (the record is untouched by the decision).
    for (text, moves) in [(&text_a, &moves_a), (&text_b, &moves_b)] {
        let recension = text.state().store().recension(moves.state().store());
        assert_eq!(recension.order(), [second, edit, first]);
        assert!(recension.refused().is_empty());
        assert!(recension.pending().is_empty());
    }
    assert_eq!(text_a.state().store().order(), [first, second, edit]);
}

/// Concurrent moves of ONE element are not a cycle: the highest-rank
/// surviving testimony wins by the fixed public rule, identically at every
/// replica, with nothing refused.
#[test]
fn test_concurrent_moves_of_one_element_resolve_by_rank() {
    let mut text: Text = Composer::new(1);
    let mut moves_a: Moves = Composer::new(1);
    let mut moves_b: Moves = Composer::new(2);
    let clk_a = clock(1);
    let clk_b = clock(2);

    // "a b c", then two replicas concurrently move c: alice to the front,
    // bob (with the later rank) between a and b.
    let a = type_after(&mut text, &clk_a, None);
    let b = type_after(&mut text, &clk_a, Some(a.into()));
    let c = type_after(&mut text, &clk_a, Some(b.into()));
    let (_, from_a) = move_to(
        &mut moves_a,
        c.into(),
        locus_at(&clk_a, Anchor::Before(a.into())),
    );
    clk_b.observe(clk_a.now(0u16)); // bob's clock runs later
    let (_, from_b) = move_to(
        &mut moves_b,
        c.into(),
        locus_at(&clk_b, Anchor::After(a.into())),
    );
    let _ = moves_a.absorb(2, &from_b);
    let _ = moves_b.absorb(1, &from_a);
    assert_eq!(moves_a.state().store(), moves_b.state().store());

    for moves in [&moves_a, &moves_b] {
        let recension = text.state().store().recension(moves.state().store());
        assert_eq!(recension.order(), [a, c, b], "the later rank wins");
        assert!(recension.refused().is_empty());
    }
}

/// The genuine cycle: two elements concurrently moved after each other.
/// Exactly one testimony applies (the replay order is fixed and public),
/// the other is REFUSED, readably, identically at every replica; both
/// elements stay rendered.
#[test]
fn test_a_move_cycle_is_refused_deterministically() {
    let mut text: Text = Composer::new(1);
    let mut moves_a: Moves = Composer::new(1);
    let mut moves_b: Moves = Composer::new(2);
    let clk_a = clock(1);
    let clk_b = clock(2);

    // Two genuine root siblings (both Origin-anchored; the later rank
    // reads first under the rank-descending bucket order): "b a".
    let weave_at_origin = |text: &mut Text, rank| {
        let (dot, _) = text.compose(|dot| {
            let mut delta = crate::metis::Rhapsody::new();
            assert!(delta.weave(
                dot,
                crate::metis::Locus {
                    anchor: Anchor::Origin,
                    rank,
                }
            ));
            (delta, DotSet::new())
        });
        dot
    };
    let a = weave_at_origin(&mut text, clk_a.now(0u16));
    let b = weave_at_origin(&mut text, clk_a.now(0u16));
    assert_eq!(text.state().store().order(), [b, a]);

    // Concurrently: a after b, and b after a.
    let (_, from_a) = move_to(
        &mut moves_a,
        a.into(),
        locus_at(&clk_a, Anchor::After(b.into())),
    );
    clk_b.observe(clk_a.now(0u16));
    let (second_move, from_b) = move_to(
        &mut moves_b,
        b.into(),
        locus_at(&clk_b, Anchor::After(a.into())),
    );
    let _ = moves_a.absorb(2, &from_b);
    let _ = moves_b.absorb(1, &from_a);

    for moves in [&moves_a, &moves_b] {
        let recension = text.state().store().recension(moves.state().store());
        // The lower-rank move applied first (a now hangs after b); the
        // higher-rank move would close the cycle and is refused, at its
        // own point in the fixed replay order.
        assert_eq!(recension.order(), [b, a]);
        assert_eq!(recension.refused(), [second_move]);
        assert_eq!(
            recension.locus(a).map(|l| l.anchor),
            Some(Anchor::After(b.into()))
        );
        // Every element still renders: refusal detaches nothing.
        assert_eq!(recension.visible_len(), 2);
    }
}

/// A self-anchoring move (a leaf placed after itself, which a public
/// `Metathesis` can carry) is a degenerate cycle: it is refused, and the
/// element stays rendered at its birth place, never detached. Pins the
/// regression the review caught, where a leaf's self-move slipped past the
/// cheap `anchored` cycle guard and silently detached the element.
#[test]
fn test_a_self_move_is_refused() {
    let mut text: Text = Composer::new(1);
    let mut moves: Moves = Composer::new(1);
    let clk = clock(1);

    let a = type_after(&mut text, &clk, None);
    let b = type_after(&mut text, &clk, Some(a.into()));
    let c = type_after(&mut text, &clk, Some(b.into())); // a leaf: nothing anchors to it

    // Move the leaf to hang after itself: a self-cycle no `anchored` entry
    // warns of, since nothing anchors to `c` yet.
    let (testimony, _) = move_to(
        &mut moves,
        c.into(),
        locus_at(&clk, Anchor::After(c.into())),
    );

    let recension = text.state().store().recension(moves.state().store());
    assert_eq!(recension.refused(), [testimony], "the self-move is refused");
    // Nothing detached: every element still renders, in birth order.
    assert_eq!(recension.order(), [a, b, c]);
    assert!(recension.is_visible(c));
}

/// A malformed record (constructible through the public `Metathesis` fields)
/// can carry a move ranked *below* its anchor's birth: the target at the
/// origin, the anchor woven after it at a later rank, and a move of the
/// target to after the anchor stamped between them. The move names the
/// anchor before it exists, so the replay must refuse the MOVE and never a
/// birth: births are canonical and nothing may vanish. Pins the review's
/// second finding, which the honest generator cannot reach (a mover cannot
/// anchor to an element it has not yet observed, so a move always outranks
/// its anchor's birth).
#[test]
fn test_a_move_ranked_below_its_anchor_birth_refuses_the_move() {
    let clk = clock(1);
    let r_target = clk.now(0u16);
    let r_move = clk.now(0u16);
    let r_anchor = clk.now(0u16);

    let mut text: Text = Composer::new(1);
    let (target, _) = text.compose(|dot| {
        let mut delta = crate::metis::Rhapsody::new();
        assert!(delta.weave(
            dot,
            crate::metis::Locus {
                anchor: Anchor::Origin,
                rank: r_target,
            }
        ));
        (delta, DotSet::new())
    });
    let (anchor, _) = text.compose(|dot| {
        let mut delta = crate::metis::Rhapsody::new();
        assert!(delta.weave(
            dot,
            crate::metis::Locus {
                anchor: Anchor::After(target.into()),
                rank: r_anchor,
            }
        ));
        (delta, DotSet::new())
    });
    assert_eq!(text.state().store().order(), [target, anchor]);

    // The move is stamped at `r_move` (between the two births) yet anchors to
    // the later-born `anchor`: an anchor chain that closes a cycle only once
    // the anchor's birth has replayed.
    let mut moves: Moves = Composer::new(1);
    let (m, _) = moves.compose(|dot| {
        (
            Metatheses::singleton(
                dot,
                crate::metis::Metathesis {
                    target: target.into(),
                    to: crate::metis::Locus {
                        anchor: Anchor::After(anchor.into()),
                        rank: r_move,
                    },
                },
            ),
            DotSet::new(),
        )
    });

    let recension = text.state().store().recension(moves.state().store());
    assert_eq!(recension.refused(), [m], "the move is refused, not a birth");
    assert_eq!(
        recension.order(),
        [target, anchor],
        "both births stay rendered"
    );
    assert!(recension.is_visible(target));
    assert!(recension.is_visible(anchor));
}

/// Undoing a move is an observed-remove of its testimony: the birth order
/// returns, convergently.
#[test]
fn test_undoing_a_move_by_retract_restores_the_birth_order() {
    let mut text: Text = Composer::new(1);
    let mut moves: Moves = Composer::new(1);
    let clk = clock(1);

    let a = type_after(&mut text, &clk, None);
    let b = type_after(&mut text, &clk, Some(a.into()));
    let (testimony, _) = move_to(
        &mut moves,
        b.into(),
        locus_at(&clk, Anchor::Before(a.into())),
    );
    let store = text.state().store();
    assert_eq!(store.recension(moves.state().store()).order(), [b, a]);

    let mut undo = DotSet::new();
    assert!(undo.insert(testimony));
    let _ = moves.retract(undo);
    assert_eq!(store.recension(moves.state().store()).order(), [a, b]);
    assert!(moves.state().store().is_empty());
}

/// A tidy re-move supersedes the mover's earlier testimonies for the same
/// target (`moves_of` is the supersede set), so the record holds one
/// surviving testimony and the read follows it.
#[test]
fn test_a_re_move_supersedes_tidily() {
    let mut text: Text = Composer::new(1);
    let mut moves: Moves = Composer::new(1);
    let clk = clock(1);

    let a = type_after(&mut text, &clk, None);
    let b = type_after(&mut text, &clk, Some(a.into()));
    let c = type_after(&mut text, &clk, Some(b.into()));

    let (_, _) = move_to(
        &mut moves,
        c.into(),
        locus_at(&clk, Anchor::Before(a.into())),
    );
    // Re-move c between a and b, superseding the first testimony.
    let displaced = moves.state().store().moves_of(c.into());
    let to = locus_at(&clk, Anchor::After(a.into()));
    let (_, _) = moves.compose_super(
        |dot| {
            Metatheses::singleton(
                dot,
                crate::metis::Metathesis {
                    target: c.into(),
                    to,
                },
            )
        },
        |_| displaced.clone(),
    );

    assert_eq!(moves.state().store().len(), 1, "one surviving testimony");
    let recension = text.state().store().recension(moves.state().store());
    assert_eq!(recension.order(), [a, c, b]);
}

/// Marks compose with moves: a span whose verges name a moved element
/// projects at the element's NEW place in the recension (PRD 0021 meets
/// PRD 0022; the intersection both charters recorded as open now has its
/// first pinned floor).
#[test]
fn test_marks_follow_moved_content() {
    let mut text: Text = Composer::new(1);
    let mut moves: Moves = Composer::new(1);
    let clk = clock(1);

    let a = type_after(&mut text, &clk, None);
    let b = type_after(&mut text, &clk, Some(a.into()));
    let c = type_after(&mut text, &clk, Some(b.into()));
    let mark = Scholion {
        start: Verge::Before(b.into()),
        end: Verge::After(b.into()),
        tag: "comment",
    };

    // Move the marked element to the front; the mark rides its identity,
    // and so does everything anchored through it (c was typed after b,
    // so it follows: the subtree-follows semantics).
    let (_, _) = move_to(
        &mut moves,
        b.into(),
        locus_at(&clk, Anchor::Before(a.into())),
    );
    let recension = text.state().store().recension(moves.state().store());
    assert_eq!(recension.order(), [b, c, a]);
    assert_eq!(
        recension.extent(mark.start, mark.end),
        Extent::Covered([b].into()),
    );
    // Against the canonical (unmoved) store the same mark reads at the
    // birth place: the mark is anchored to identity, and WHERE that
    // identity currently reads is exactly the recension's decision.
    assert_eq!(
        text.state().store().extent(mark.start, mark.end),
        Extent::Covered([b].into()),
    );
}

/// A move whose target has not been woven here yet takes no part in the
/// replay and is surfaced as pending; the weave's arrival resolves it.
#[test]
fn test_a_pending_move_surfaces_and_resolves() {
    let mut text_a: Text = Composer::new(1);
    let mut moves_a: Moves = Composer::new(1);
    let mut text_b: Text = Composer::new(2);
    let mut moves_b: Moves = Composer::new(2);
    let clk = clock(1);

    let a = type_after(&mut text_a, &clk, None);
    let b = type_after(&mut text_a, &clk, Some(a.into()));
    let (testimony, move_delta) = move_to(
        &mut moves_a,
        b.into(),
        locus_at(&clk, Anchor::Before(a.into())),
    );

    // Bob receives the MOVE before any text: the target has no birth
    // there, so the replay parks it, fabricating nothing.
    let _ = moves_b.absorb(1, &move_delta);
    let empty = text_b.state().store().recension(moves_b.state().store());
    assert_eq!(empty.pending(), [testimony]);
    assert!(empty.order().is_empty());

    // The weave arrives; the same record now applies.
    let _ = text_b.absorb(1, &text_a.owed_to(text_b.state().context()));
    let resolved = text_b.state().store().recension(moves_b.state().store());
    assert!(resolved.pending().is_empty());
    assert_eq!(resolved.order(), [b, a]);
}

/// Moving an order tombstone is meaningful: the element stays invisible,
/// and everything anchored through it follows its slot to the new place.
#[test]
fn test_moving_a_tombstone_carries_its_subtree() {
    let mut text: Text = Composer::new(1);
    let mut moves: Moves = Composer::new(1);
    let clk = clock(1);

    let a = type_after(&mut text, &clk, None);
    let b = type_after(&mut text, &clk, Some(a.into()));
    let tail = type_after(&mut text, &clk, Some(b.into()));

    // Delete b (its slot persists as an order tombstone), then move it
    // to the front.
    let mut gone = DotSet::new();
    assert!(gone.insert(b));
    let _ = text.retract(gone);
    let (_, _) = move_to(
        &mut moves,
        b.into(),
        locus_at(&clk, Anchor::Before(a.into())),
    );

    let recension = text.state().store().recension(moves.state().store());
    // b stays invisible; its follower moved with its slot.
    assert!(!recension.is_visible(b));
    assert_eq!(recension.order(), [tail, a]);
}

/// The empty record is the identity: a recension under no testimony reads
/// exactly the canonical order, with nothing refused and nothing pending.
#[test]
fn test_recension_of_an_empty_record_is_the_order() {
    let mut text: Text = Composer::new(1);
    let clk = clock(1);
    let mut typed = Vec::new();
    let mut caret = None;
    for _ in 0..5 {
        let dot = type_after(&mut text, &clk, caret);
        typed.push(dot);
        caret = Some(dot.into());
    }
    let store = text.state().store();
    assert_eq!(
        store.order(),
        typed,
        "the fixture is a plain forward sequence"
    );
    let recension = store.recension(&Metatheses::new());
    assert_eq!(recension.order(), store.order());
    assert!(recension.refused().is_empty());
    assert!(recension.pending().is_empty());
}