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
//! Deterministic seal-consignment boundary examples.

use super::*;
use crate::metis::dot::RawDot;

mod ledger;
mod refusals;

/// The rank-contest falsifier, the third S213-class exhibit (ruling R-50):
/// verbatim anchors under translation cannot carry window content across
/// the seal. Re-founding re-bases a cut-live sibling's rank to the fixed
/// base, so an honest window birth that LOST its old bucket's contest
/// (its minter's clock ran behind) wins the translated bucket, flipping
/// the pair's reading. Only the judged outcome is stable, and the consignment
/// carries exactly that.
#[test]
fn a_verbatim_anchor_carry_flips_a_rank_contest_the_consignment_preserves() {
    let mut text = Text::new();
    weave(&mut text, d(1, 1), locus(Anchor::Origin, rank(1)));
    // The cut-live sibling: honest clock rank 5, delivered everywhere.
    weave(
        &mut text,
        d(1, 2),
        locus(
            Anchor::After(RawDot {
                station: 1,
                counter: 1,
            }),
            rank(5),
        ),
    );

    // The window birth: same bucket, honest concurrent rank 3 (station 2
    // had not seen the sibling when it minted), so the old world reads it
    // after the sibling.
    let window_rank = Kairos::new(3, 0, 2, 0u16);
    let birth = text_delta(
        d(2, 1),
        locus(
            Anchor::After(RawDot {
                station: 1,
                counter: 1,
            }),
            window_rank,
        ),
    );

    let boundary = Cut::floor_of(text.context());
    let (adopted, sealed) = seal_mill(1, &boundary, &[(2, 1)]);
    let refounded = text.store().refound(&Metatheses::new()).unwrap();
    let mut shadow = EpochStratum::new(&adopted, text, Moves::new())
        .unwrap()
        .into_transition()
        .shadow;
    shadow.deliver_text(&birth).unwrap();
    let projection = shadow.projection().clone();
    assert_eq!(projection.order(), [d(1, 1), d(1, 2), d(2, 1)]);

    // The verbatim carry: anchor and rank kept under translation. The
    // sibling's rank was re-based by the re-foundation, so the window
    // birth now wins the bucket it had lost: the reading flips.
    let mut verbatim = refounded.store().clone();
    assert!(verbatim.weave(
        d(2, 1),
        locus(
            Anchor::After(RawDot {
                station: 1,
                counter: 1
            }),
            window_rank
        )
    ));
    assert_eq!(verbatim.order(), [d(1, 1), d(2, 1), d(1, 2)]);

    // The consignment bakes the judged outcome instead: exact agreement.
    let consignment = shadow.consign(&sealed).unwrap().0;
    assert_eq!(consignment.text().store().order(), projection.order());
    assert_eq!(consignment.moves().store().testimonies().count(), 0);
}

/// The first S213 falsifier through the seal: a window move lawful on the
/// old root-sibling topology is a refused cycle-former against the
/// re-founded chain, so the testimony must not cross; the consignment bakes
/// its outcome into the spelled skeleton and the movement record starts
/// empty.
#[test]
fn the_old_world_move_verdict_crosses_baked_never_as_testimony() {
    let mut text = Text::new();
    weave(&mut text, d(1, 1), locus(Anchor::Origin, rank(2)));
    weave(&mut text, d(1, 2), locus(Anchor::Origin, rank(1)));
    let boundary = Cut::floor_of(text.context());
    let (adopted, sealed) = seal_mill(2, &boundary, &[(2, 2)]);
    let refounded = text.store().refound(&Metatheses::new()).unwrap();

    // "(1, 1) after (1, 2)": lawful on the old topology (root siblings).
    let testimony = movement(
        d(2, 2),
        (1, 1),
        Anchor::After(RawDot {
            station: 1,
            counter: 2,
        }),
        rank(1),
    );
    let mut shadow = EpochStratum::new(&adopted, text, Moves::new())
        .unwrap()
        .into_transition()
        .shadow;
    shadow.deliver_moves(&testimony).unwrap();
    assert_eq!(shadow.recension().refused(), []);
    let projection = shadow.projection().clone();
    assert_eq!(projection.order(), [d(1, 2), d(1, 1)]);

    // Carried verbatim into the re-founded chain, the same testimony is
    // refused: the incumbent edge relation changed under it (S213).
    let translated = Metatheses::singleton(
        d(2, 2),
        Metathesis {
            target: RawDot::new(1, 1),
            to: locus(
                Anchor::After(RawDot {
                    station: 1,
                    counter: 2,
                }),
                rank(1),
            ),
        },
    );
    assert_eq!(
        refounded.store().recension(&translated).refused(),
        [d(2, 2)]
    );

    // The consignment carries the outcome, and nothing re-judges it.
    let consignment = shadow.consign(&sealed).unwrap().0;
    assert_eq!(consignment.text().store().order(), projection.order());
    assert_eq!(consignment.moves().store().testimonies().count(), 0);
    assert!(consignment.moves().context().contains(d(2, 2)));
}

/// One window of every traffic kind crosses the seal whole: the birth is
/// woven at its judged place, the moved element sits where the old world's
/// replay put it, the deleted element keeps its locus as an order
/// tombstone a native anchor can still name, and both contexts carry the
/// image of every window event dot.
#[test]
fn the_consignment_carries_births_moves_and_deletes_whole() {
    let mut text = Text::new();
    weave(&mut text, d(1, 1), locus(Anchor::Origin, rank(3)));
    weave(&mut text, d(1, 2), locus(Anchor::Origin, rank(2)));
    weave(&mut text, d(1, 3), locus(Anchor::Origin, rank(1)));
    let boundary = Cut::floor_of(text.context());
    let (adopted, sealed) = seal_mill(1, &boundary, &[(2, 1), (2, 2), (2, 3)]);

    let mut shadow = EpochStratum::new(&adopted, text, Moves::new())
        .unwrap()
        .into_transition()
        .shadow;
    // A window birth after (1, 1); a window delete of (1, 2); a window
    // move of (1, 3) to the document head.
    let birth = text_delta(
        d(2, 1),
        locus(
            Anchor::After(RawDot {
                station: 1,
                counter: 1,
            }),
            Kairos::new(9, 0, 2, 0u16),
        ),
    );
    shadow.deliver_text(&birth).unwrap();
    let mut removal = DotSet::new();
    assert!(removal.insert(d(2, 2)));
    assert!(removal.insert(d(1, 2)));
    shadow.deliver_text(&Dotted::from_context(removal)).unwrap();
    let moved = movement(d(2, 3), (1, 3), Anchor::Origin, Kairos::new(11, 0, 2, 0u16));
    shadow.deliver_moves(&moved).unwrap();

    let projection = shadow.projection().clone();
    assert_eq!(projection.order(), [d(1, 3), d(1, 1), d(2, 1)]);
    let consignment = shadow.consign(&sealed).unwrap().0;
    let base = consignment.text().store();
    assert_eq!(base.order(), projection.order());

    // The window-deleted element crossed as an order tombstone at its
    // judged place: a native weave anchored to it still lands there.
    assert!(base.locus(d(1, 2)).is_some());
    assert!(!base.is_visible(d(1, 2)));
    let mut native = base.clone();
    assert!(native.weave(
        d(3, 1),
        locus(
            Anchor::After(RawDot {
                station: 1,
                counter: 2
            }),
            rank(9)
        )
    ));
    assert_eq!(native.order(), [d(1, 3), d(1, 1), d(2, 1), d(3, 1)]);

    // Both pairs share one translated context carrying every event dot's
    // image the seal accounts for (text, movement, and declaration alike),
    // so each plane's floors are born gap-free on its own.
    assert_eq!(consignment.text().context(), consignment.moves().context());
    let floor = consignment.text().context().floor();
    assert_eq!(floor.get(1), 4);
    assert_eq!(floor.get(2), 3);
    assert!(consignment.moves().context().contains(d(2, 1)));
    assert!(consignment.moves().context().contains(d(2, 3)));
    assert!(consignment.text().context().contains(d(2, 2)));
}

/// The consignment is a pure function of the shadow's delta set: both arrival
/// orders derive byte-identical base pairs.
#[test]
fn the_consignment_reads_identically_in_both_arrival_orders() {
    let mut text = Text::new();
    weave(&mut text, d(1, 1), locus(Anchor::Origin, rank(2)));
    weave(&mut text, d(1, 2), locus(Anchor::Origin, rank(1)));
    let boundary = Cut::floor_of(text.context());

    let birth = text_delta(
        d(2, 1),
        locus(
            Anchor::After(RawDot {
                station: 1,
                counter: 1,
            }),
            Kairos::new(7, 0, 2, 0u16),
        ),
    );
    let moved = movement(d(2, 2), (1, 2), Anchor::Origin, Kairos::new(8, 0, 2, 0u16));

    let mut consignments = Vec::new();
    for reversed in [false, true] {
        let (adopted, sealed) = seal_mill(1, &boundary, &[(2, 1), (2, 2)]);
        let mut shadow = EpochStratum::new(&adopted, text.clone(), Moves::new())
            .unwrap()
            .into_transition()
            .shadow;
        if reversed {
            shadow.deliver_moves(&moved).unwrap();
            shadow.deliver_text(&birth).unwrap();
        } else {
            shadow.deliver_text(&birth).unwrap();
            shadow.deliver_moves(&moved).unwrap();
        }
        consignments.push(shadow.consign(&sealed).unwrap().0);
    }
    assert_eq!(
        consignments[0].text().store().to_bytes(),
        consignments[1].text().store().to_bytes()
    );
    assert_eq!(
        consignments[0].text().context(),
        consignments[1].text().context()
    );
    assert_eq!(
        consignments[0].moves().context(),
        consignments[1].moves().context()
    );
}

/// An identity born and superseded inside the window crosses as context
/// only: nothing in the new plane can anchor it, and its image in the
/// floors is all the next generation needs.
#[test]
fn a_window_dead_birth_crosses_as_context_only() {
    let mut text = Text::new();
    weave(&mut text, d(1, 1), locus(Anchor::Origin, rank(1)));
    let boundary = Cut::floor_of(text.context());
    let (adopted, sealed) = seal_mill(1, &boundary, &[(2, 1), (2, 2)]);

    let mut shadow = EpochStratum::new(&adopted, text, Moves::new())
        .unwrap()
        .into_transition()
        .shadow;
    let birth = text_delta(
        d(2, 1),
        locus(
            Anchor::After(RawDot {
                station: 1,
                counter: 1,
            }),
            Kairos::new(5, 0, 2, 0u16),
        ),
    );
    shadow.deliver_text(&birth).unwrap();
    let mut removal = DotSet::new();
    assert!(removal.insert(d(2, 2)));
    assert!(removal.insert(d(2, 1)));
    shadow.deliver_text(&Dotted::from_context(removal)).unwrap();

    let consignment = shadow.consign(&sealed).unwrap().0;
    assert_eq!(consignment.text().store().order(), [d(1, 1)]);
    assert!(consignment.text().store().locus(d(2, 1)).is_none());
    assert!(consignment.text().context().contains(d(2, 1)));
    assert!(consignment.text().context().contains(d(2, 2)));
}

/// An identity dead at the cut is swept by the re-foundation: the consignment
/// carries neither its locus nor a context image for it (it is answerable
/// only in the shadow's lifetime and at the recognizer afterwards), and
/// the compacted floors stay perfect around the gap it leaves.
#[test]
fn a_dead_at_cut_identity_does_not_cross_the_consignment() {
    let mut text = Text::new();
    weave(&mut text, d(1, 1), locus(Anchor::Origin, rank(2)));
    weave(
        &mut text,
        d(1, 2),
        locus(
            Anchor::After(RawDot {
                station: 1,
                counter: 1,
            }),
            rank(1),
        ),
    );
    weave(
        &mut text,
        d(2, 1),
        locus(
            Anchor::After(RawDot {
                station: 1,
                counter: 2,
            }),
            rank(1),
        ),
    );
    remove(&mut text, d(2, 1));
    let boundary = Cut::floor_of(text.context());
    let (adopted, sealed) = seal_mill(1, &boundary, &[]);
    let shadow = EpochStratum::new(&adopted, text, Moves::new())
        .unwrap()
        .into_transition()
        .shadow;
    let consignment = shadow.consign(&sealed).unwrap().0;
    assert_eq!(consignment.text().store().order(), [d(1, 1), d(1, 2)]);
    assert_eq!(consignment.text().store().skeleton_len(), 2);
    assert!(!consignment.text().context().contains(d(2, 1)));
    assert_eq!(consignment.text().context().floor().get(2), 0);
}

/// The chain-adjacency guard at the counter ceiling: a lawful affine
/// image can be `u64::MAX`, and a lower same-station image can follow it
/// in the final order, so the guard must test adjacency without
/// overflowing (the spelling falls back to an anchored head, exactly as
/// for any non-adjacent pair).
#[test]
fn a_ceiling_image_does_not_overflow_the_chain_guard() {
    let mut text = Rhapsody::new();
    assert!(text.weave(d(2, u64::MAX), locus(Anchor::Origin, rank(5))));
    assert!(text.weave(
        d(2, 1),
        locus(
            Anchor::After(RawDot {
                station: 2,
                counter: u64::MAX
            }),
            rank(1)
        )
    ));
    let map = Rhapsody::new()
        .refound(&Metatheses::new())
        .unwrap()
        .map()
        .clone();
    let store = text
        .recension(&Metatheses::new())
        .consign_store(&map)
        .unwrap();
    assert_eq!(store.order(), [d(2, u64::MAX), d(2, 1)]);
}

/// A window move can flip a native move's verdict at the seal: the
/// consignment re-spells the topology movement acceptance depends on, so a
/// native testimony lawful against the pristine plane can be a
/// cycle-former against the consigned base (the S213 exposure, one
/// plane over). This is why the recipe defers native movement
/// testimonies until the seal: the window is short, and a verdict minted
/// against a plane the seal will replace is a verdict that can silently
/// invert.
#[test]
fn a_window_move_can_flip_a_native_move_verdict_hence_the_deferral_duty() {
    let mut text = Text::new();
    weave(&mut text, d(1, 1), locus(Anchor::Origin, rank(2)));
    weave(&mut text, d(1, 2), locus(Anchor::Origin, rank(1)));
    let boundary = Cut::floor_of(text.context());
    let (adopted, sealed) = seal_mill(1, &boundary, &[(2, 1)]);
    let refounded = text.store().refound(&Metatheses::new()).unwrap();

    // The native testimony, minted in the new dot space against the
    // pristine re-founded chain (1,1) -> (1,2): "(1,2) after (1,1)" is
    // lawful there (it restates the chain).
    let native = Metatheses::singleton(
        d(2, 5),
        Metathesis {
            target: RawDot::new(1, 2),
            to: locus(
                Anchor::After(RawDot {
                    station: 1,
                    counter: 1,
                }),
                Kairos::new(9, 0, 2, 0u16),
            ),
        },
    );
    assert_eq!(refounded.store().recension(&native).refused(), []);

    // The old-window move reverses the pair; the consignment re-spells the
    // chain as (1,2) -> (1,1), and the same native testimony is now a
    // cycle-former.
    let mut shadow = EpochStratum::new(&adopted, text, Moves::new())
        .unwrap()
        .into_transition()
        .shadow;
    shadow
        .deliver_moves(&movement(
            d(2, 1),
            (1, 1),
            Anchor::After(RawDot {
                station: 1,
                counter: 2,
            }),
            rank(1),
        ))
        .unwrap();
    let consignment = shadow.consign(&sealed).unwrap().0;
    assert_eq!(consignment.text().store().order(), [d(1, 2), d(1, 1)]);
    assert_eq!(
        consignment.text().store().recension(&native).refused(),
        [d(2, 5)]
    );
}