formualizer-eval 0.10.0

High-performance Arrow-backed Excel formula engine with dependency graph and incremental recalculation
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
//! M1a correction round (review B1–B5, store level): counter-based scaling
//! of slot planning and accounting, bounded retention under churn at a
//! fixed live size, complete scratch admission, and transactional
//! allocation under injected allocation failures (with the allocator's
//! measured peak and net checked against the prediction).

use super::super::geom::{Cell, Rect};
use super::super::identity::Vid;
use super::super::proj::RefProj;
use super::super::store::{
    AuthorityError, Counts, Digest, EdgeSpec, FormulaFacts, LkKey, MutationReport, OriginSpec,
    Store, Tag,
};
use super::alloc::measure;
use super::support::*;

fn one(l: u64, literal: u32) -> FormulaFacts {
    Formula {
        refs: vec![],
        l,
        literal,
    }
    .facts()
}

fn with_refs(refs: Vec<RefProj>, l: u64, literal: u32) -> FormulaFacts {
    Formula { refs, l, literal }.facts()
}

// ---------------------------------------------------------------- B1

/// Bulk build and range clear of N `=1` formulas (one literal, no edges):
/// the slot planning work is at most 2 visits per formula at every N (one
/// per row, plus one per page run and per touched page), and the whole
/// clear's dry-run work at most 4 per formula.
#[test]
fn slot_planning_work_is_linear_in_build_and_clear() {
    let mut rows = Vec::new();
    for n in [4_096u32, 16_384, 65_536] {
        let input = (0..n).map(|r| ((0, r, 0), one(1, 1))).collect();
        let mut s = Store::build(input);
        let build = s.stats.slot_work;
        let (w0, p0) = (s.stats.slot_work, s.stats.plan_work);
        s.clear_rect(0, Rect::new(0, 0, n - 1, 0)).unwrap();
        assert_eq!(s.formula_count(), 0);
        assert_eq!(s.slots().rows(), 0);
        assert_eq!(s.slots().pages(), 0, "emptied pages are freed");
        let (clear_slot, clear_plan) = (s.stats.slot_work - w0, s.stats.plan_work - p0);
        s.check().unwrap();
        let n64 = u64::from(n);
        assert!(build <= 2 * n64, "build slot work {build} for {n}");
        assert!(
            clear_slot <= 2 * n64,
            "clear slot work {clear_slot} for {n}"
        );
        assert!(
            clear_plan <= 4 * n64,
            "clear plan work {clear_plan} for {n}"
        );
        rows.push((n, build, clear_slot, clear_plan));
    }
    eprintln!("B1 work (n, build slot, clear slot, clear plan): {rows:?}");
}

// ---------------------------------------------------------------- B2

/// N unique-template appends, then N updates of one formula in a store of
/// N singletons: the dry-run/accounting work of every mutation is bounded
/// by the same constant at every N, and no census runs on the mutation
/// path.
#[test]
fn mutation_accounting_work_is_independent_of_store_size() {
    let mut maxes = Vec::new();
    for n in [1_024u32, 4_096, 16_384] {
        let mut s = Store::new();
        let census = super::super::store::census_calls();
        let mut max_append = 0;
        for r in 0..n {
            let w = s.stats.plan_work;
            s.set_formula((0, r, 0), &one(u64::from(r), 1)).unwrap();
            max_append = max_append.max(s.stats.plan_work - w);
        }
        let mut max_update = 0;
        for i in 0..n {
            let w = s.stats.plan_work;
            s.set_formula((0, n / 2, 0), &one(u64::from(n + i), 2))
                .unwrap();
            max_update = max_update.max(s.stats.plan_work - w);
        }
        assert_eq!(
            super::super::store::census_calls(),
            census,
            "a census ran on the mutation path"
        );
        assert_eq!(s.formula_count(), u64::from(n));
        s.check().unwrap();
        maxes.push((n, max_append, max_update));
    }
    eprintln!("B2 max work per mutation (n, append, update): {maxes:?}");
    let (_, a0, u0) = maxes[0];
    for &(n, a, u) in &maxes {
        assert!(
            a <= a0 && u <= u0,
            "work per mutation grew at {n}: {maxes:?}"
        );
    }
    assert!(a0 <= 32 && u0 <= 32, "{maxes:?}");
}

/// Re-review R2: a point edit's identity planning visits the mutated sheet
/// only. S sheets hold one independent `=1` each; editing a formula on
/// sheet 0, and appending a formula to a fresh sheet, cost the same work at
/// every S.
#[test]
fn point_edit_work_is_independent_of_sheet_count() {
    let mut maxes = Vec::new();
    for sheets in [16u16, 64, 256, 1_024] {
        let mut s = Store::new();
        let mut max_append = 0;
        for sh in 0..sheets {
            let w = s.stats.plan_work;
            s.set_formula((sh, 0, 0), &one(u64::from(sh), 1)).unwrap();
            max_append = max_append.max(s.stats.plan_work - w);
        }
        let mut max_edit = 0;
        for i in 0..64u64 {
            let w = s.stats.plan_work;
            s.set_formula((0, 0, 0), &one(10_000 + i, 2)).unwrap();
            max_edit = max_edit.max(s.stats.plan_work - w);
        }
        s.check().unwrap();
        maxes.push((sheets, max_append, max_edit));
    }
    eprintln!("R2 max work per mutation (sheets, append, edit): {maxes:?}");
    let (_, a0, e0) = maxes[0];
    for &(n, a, e) in &maxes {
        assert!(a <= a0 && e <= e0, "work grew with {n} sheets: {maxes:?}");
    }
}

/// Re-review N1: N formulas on sheet 0, formula i reading one value cell
/// on its own sheet i + 1, cleared by one range clear. Index planning looks
/// sheets up in O(log d), so the clear's work is O(N log N), where the
/// linear touched-sheet search was Θ(N²).
#[test]
fn range_clear_over_distinct_target_sheets_is_not_quadratic() {
    let mut runs = Vec::new();
    for n in [256u32, 1_024, 4_096] {
        let mut s = Store::new();
        for r in 0..n {
            let f = with_refs(vec![abs(0, 0, (r + 1) as u16)], 1, 1);
            s.set_formula((0, r, 0), &f).unwrap();
        }
        let w = s.stats.plan_work;
        s.clear_rect(0, Rect::new(0, 0, n - 1, 0)).unwrap();
        let work = s.stats.plan_work - w;
        assert_eq!(s.formula_count(), 0);
        s.check().unwrap();
        runs.push((n, work));
    }
    eprintln!("N1 range-clear work (n, work): {runs:?}");
    for &(n, work) in &runs {
        let log = u64::from(32 - n.leading_zeros());
        assert!(
            work <= 16 * u64::from(n) * log,
            "clear of {n} formulas over {n} sheets did {work} work: {runs:?}"
        );
    }
    // Quadrupling N must not come near sixteen-fold work.
    for w in runs.windows(2) {
        assert!(w[1].1 <= w[0].1 * 6, "super-linear growth: {runs:?}");
    }
}

// ---------------------------------------------------------------- B3

/// Run 128 warm-up steps, then 10,000 more; return the maximum retained
/// bytes of each phase.
fn churn(mut step: impl FnMut(&mut Store, u32)) -> (u64, u64) {
    let mut s = Store::new();
    let mut warm = 0;
    for i in 0..128 {
        step(&mut s, i);
        warm = warm.max(s.heap_bytes());
    }
    let mut late = 0;
    for i in 128..10_128 {
        step(&mut s, i);
        late = late.max(s.heap_bytes());
        if i % 1_000 == 0 {
            s.check().unwrap();
        }
    }
    s.check().unwrap();
    assert_eq!(s.formula_count(), 1);
    (warm, late)
}

/// One live formula (a literal, a relative and an absolute reference)
/// cleared and restored 10,000 times: fresh ids every time, bounded bytes.
#[test]
fn one_live_formula_clear_restore_is_bounded() {
    let f = with_refs(vec![rel(0, -1, 0), abs(2, 1, 0)], 1, 7);
    let (warm, late) = churn(|s, _| {
        let _ = s.clear_cell((0, 4, 3)).unwrap();
        s.set_formula((0, 4, 3), &f).unwrap();
    });
    assert!(
        late <= warm,
        "retention grew with history: warm {warm}, late {late}"
    );
}

/// One live formula replaced 10,000 times by a new unique template with a
/// new absolute reference (a new node group and a new edge group each
/// time, the old ones reclaimed): bounded bytes.
#[test]
fn one_live_formula_unique_template_replace_is_bounded() {
    let (warm, late) = churn(|s, i| {
        let f = with_refs(vec![abs(i, 1, 0), rel(0, -1, 0)], u64::from(i) + 100, i % 3);
        s.set_formula((0, 4, 3), &f).unwrap();
    });
    assert!(
        late <= warm,
        "retention grew with history: warm {warm}, late {late}"
    );
}

/// Review B3 repro: same formula punched and refilled.
#[test]
fn review_same_formula_punch_refill_has_bounded_slot_memory() {
    let f = Formula {
        refs: vec![],
        l: 1,
        literal: 1,
    }
    .facts();
    let mut s = Store::new();
    for _ in 0..128 {
        s.set_formula((0, 0, 0), &f).unwrap();
        s.clear_cell((0, 0, 0)).unwrap();
    }
    let warm = s.slots().heap_bytes();
    for _ in 0..4096 {
        s.set_formula((0, 0, 0), &f).unwrap();
        s.clear_cell((0, 0, 0)).unwrap();
    }
    assert_eq!(s.formula_count(), 0);
    assert!(
        s.slots().heap_bytes() <= warm * 2,
        "one-cell churn: warm={warm}, now={}",
        s.slots().heap_bytes()
    );
}

/// Review B3 repro: replaced unique groups.
#[test]
fn review_replaced_unique_groups_have_bounded_retention() {
    let mut s = Store::new();
    for l in 1..=128 {
        let mut f = Formula {
            refs: vec![],
            l,
            literal: 0,
        }
        .facts();
        f.literals.clear();
        s.set_formula((0, 0, 0), &f).unwrap();
    }
    let warm = s.heap_bytes();
    for l in 129..=4224 {
        let mut f = Formula {
            refs: vec![],
            l,
            literal: 0,
        }
        .facts();
        f.literals.clear();
        s.set_formula((0, 0, 0), &f).unwrap();
    }
    assert_eq!(s.formula_count(), 1);
    assert!(
        s.heap_bytes() <= warm * 2,
        "one live owner: warm={warm}, now={}",
        s.heap_bytes()
    );
}

/// Re-review R5 (the review's repro): build scratch includes what the input
/// owns (here a 1,000,000-token L stream).
#[test]
fn review_build_scratch_includes_owned_tokens() {
    let mut f = Formula {
        refs: vec![],
        l: 1,
        literal: 0,
    }
    .facts();
    f.literals.clear();
    f.ltokens = Some(vec![17u64; 1_000_000].into_boxed_slice());
    let token_bytes = f.ltokens.as_ref().unwrap().len() * size_of::<u64>();
    let (_s, scratch) = Store::build_keeping(vec![((0, 0, 0), f)], None).unwrap();
    assert!(
        scratch >= token_bytes as u64,
        "owned input tokens omitted: scratch={scratch}, tokens={token_bytes}"
    );
}

/// Re-review R5: a rebuild whose input and previous store already exceed
/// the scratch budget is rejected before the build allocates anything.
#[test]
fn rebuild_preflight_rejects_before_building() {
    use super::super::store::Budget;
    let mut f = one(1, 1);
    f.ltokens = Some(vec![17u64; 100_000].into_boxed_slice());
    let budget = Budget {
        scratch: Some(64 * 1024),
        ..Budget::default()
    };
    let (r, alloc) = measure(None, || {
        Store::rebuild(vec![((0, 0, 0), f)], None, budget).map(|_| ())
    });
    match r {
        Err(AuthorityError::Admission { resource, .. }) => assert_eq!(resource, "scratch"),
        other => panic!("preflight did not reject: {other:?}"),
    }
    // The rejected build never ran: nothing near a store's worth allocated.
    assert!(alloc.peak < 4 * 1024, "{alloc:?}");
}

/// Final gate R3, store level: one live formula whose single symbol edge
/// gets an ever-new LK key. The directory is compacted in place (no
/// rebuild), so its bytes stay bounded; each compaction's work is paid by
/// the dead keys it drops.
#[test]
fn ever_new_lk_keys_at_one_live_formula_stay_bounded() {
    let step = |s: &mut Store, i: u32| {
        let mut f = one(1, 1);
        f.edges = vec![EdgeSpec {
            proj: abs(5, 5, 0),
            tag: Tag::R1,
            origin: OriginSpec::Symbol(LkKey {
                ctx: 0,
                kind: 1,
                name: format!("Key_{i:08}").into_boxed_str(),
            }),
        }];
        s.set_formula((0, 0, 0), &f).unwrap();
    };
    let mut s = Store::new();
    let mut warm = 0;
    for i in 0..128 {
        step(&mut s, i);
        warm = warm.max(
            s.bytes_breakdown()
                .iter()
                .find(|c| c.0 == "lk_dir")
                .unwrap()
                .1,
        );
    }
    let mut late = 0;
    for i in 128..10_128 {
        step(&mut s, i);
        late = late.max(
            s.bytes_breakdown()
                .iter()
                .find(|c| c.0 == "lk_dir")
                .unwrap()
                .1,
        );
    }
    s.check().unwrap();
    eprintln!(
        "R3 store: lk_dir warm {warm} late {late}, compactions {}",
        s.stats.lk_compactions
    );
    assert!(s.lk_len() <= 2 * (s.egroup_slots() + 8) + 1);
    assert!(
        late <= warm,
        "LK directory grew with history: {warm} -> {late}"
    );
    assert!(s.stats.lk_compactions > 100);
    // Amortized: total compaction work stays within a constant per key ever
    // interned (10,128 here), whatever the history.
    assert!(
        s.stats.lk_compaction_work <= 3 * 10_128,
        "compaction work {}",
        s.stats.lk_compaction_work
    );
}

/// B-23: every allocation of a compaction may fail; the mutation still
/// commits, the compaction is skipped (counted), and the logical state
/// equals the unfailed run's.
#[test]
fn lk_compaction_allocation_failures_skip_cleanly() {
    let key = |i: u32| LkKey {
        ctx: 0,
        kind: 1,
        name: format!("Key_{i:08}").into_boxed_str(),
    };
    let facts = |i: u32| {
        let mut f = one(1, 1);
        f.edges = vec![EdgeSpec {
            proj: abs(5, 5, 0),
            tag: Tag::R1,
            origin: OriginSpec::Symbol(key(i)),
        }];
        f
    };
    // Grow the directory to just below the trigger; the next set compacts.
    let mut base = Store::new();
    let mut i = 0;
    loop {
        let c = base.stats.lk_compactions;
        let mut probe = base.clone();
        probe.set_formula((0, 0, 0), &facts(i)).unwrap();
        if probe.stats.lk_compactions > c {
            break;
        }
        base.set_formula((0, 0, 0), &facts(i)).unwrap();
        i += 1;
    }
    let next = facts(i);
    let mut clean = base.clone();
    let (_, m) = measure(None, || clean.set_formula((0, 0, 0), &next).unwrap());
    assert!(clean.stats.lk_compactions > base.stats.lk_compactions);
    let want = logical(&clean);
    let mut skipped = 0;
    for n in 1..=m.allocs {
        let mut s = base.clone();
        let skips = s.stats.lk_compaction_skips;
        let (r, _) = measure(Some(n), || s.set_formula((0, 0, 0), &next));
        s.check().unwrap();
        match r {
            Ok(_) => {
                // `logical` excludes bytes, which differ when skipped.
                assert_eq!(logical(&s), want, "allocation {n}");
                if s.stats.lk_compaction_skips > skips {
                    skipped += 1;
                }
            }
            Err(e) => {
                assert_eq!(e, AuthorityError::Alloc, "allocation {n}");
                assert_eq!(logical(&s), logical(&base), "allocation {n}");
            }
        }
    }
    eprintln!(
        "B-23 compaction fail-Nth: {} allocations, {skipped} skipped compactions",
        m.allocs
    );
    assert!(skipped > 0, "no failure landed in the compaction");
}

/// Final gate 2: a compaction's measured peak stays within the mutation's
/// reported peak, with one-byte names (no slack from key heap), and a
/// scratch budget below the compaction's transient skips it cleanly.
#[test]
fn lk_compaction_peak_is_admitted_and_reported() {
    let names = "abcdefghijklmnopqrstuvwxyz";
    let facts = |i: usize| {
        let mut f = one(1, 1);
        f.edges = vec![EdgeSpec {
            proj: abs(5, 5, 0),
            tag: Tag::R1,
            origin: OriginSpec::Symbol(LkKey {
                ctx: (i / 26) as u16,
                kind: 1,
                name: names[i % 26..i % 26 + 1].into(),
            }),
        }];
        f
    };
    let mut s = Store::new();
    let mut checked = 0;
    for i in 0..400usize {
        let f = facts(i);
        let c = s.stats.lk_compactions;
        let (r, m) = measure(None, || s.set_formula((0, 0, 0), &f).unwrap());
        if s.stats.lk_compactions > c {
            assert!(
                m.peak as u64 <= r.predicted_peak_above_before,
                "step {i}: measured peak {} above reported {}",
                m.peak,
                r.predicted_peak_above_before
            );
            checked += 1;
        }
    }
    assert!(checked > 5, "only {checked} compactions checked");
    s.check().unwrap();

    // Tight scratch: the compaction is skipped, nothing else changes. 200
    // live keys make the compaction's transient larger than the edit's.
    let mut base = Store::new();
    for r in 1..=200u32 {
        let mut f = one(1, 1);
        f.edges = vec![EdgeSpec {
            proj: abs(5, 5, 0),
            tag: Tag::R1,
            origin: OriginSpec::Symbol(LkKey {
                ctx: 999,
                kind: 1,
                name: format!("live{r}").into_boxed_str(),
            }),
        }];
        base.set_formula((0, r, 0), &f).unwrap();
    }
    let mut i = 0usize;
    loop {
        let mut probe = base.clone();
        probe.set_formula((0, 0, 0), &facts(i)).unwrap();
        if probe.stats.lk_compactions > 0 {
            break;
        }
        base.set_formula((0, 0, 0), &facts(i)).unwrap();
        i += 1;
    }
    let f = facts(i);
    let mut unbudgeted = base.clone();
    let r = unbudgeted.set_formula((0, 0, 0), &f).unwrap();
    let mut tight = base.clone();
    // Enough for the mutation, below the compaction's own transient.
    tight.budget.scratch = Some(r.predicted_transient);
    let before_lk = tight.lk_len();
    tight.set_formula((0, 0, 0), &f).unwrap();
    assert_eq!(tight.stats.lk_compactions, 0);
    assert_eq!(tight.stats.lk_compaction_skips, 1);
    assert_eq!(tight.lk_len(), before_lk + 1, "directory changed on skip");
    assert_eq!(logical(&tight), logical(&unbudgeted));
    tight.check().unwrap();
}

/// Final gate R5 (the review's repro): a zero-edge singleton on a high
/// sheet builds no sheet-sized role directories, and the measured peak
/// stays within retained + reported scratch.
#[test]
fn review_sparse_high_sheet_build_peak_is_bounded() {
    let ((s, scratch), alloc) = measure(None, || {
        let mut f = Formula {
            refs: vec![],
            l: 1,
            literal: 0,
        }
        .facts();
        f.literals.clear();
        Store::build_keeping(vec![((4095, 0, 0), f)], None).unwrap()
    });
    assert!(
        alloc.peak as u64 <= s.heap_bytes() + scratch,
        "peak={}, retained={}, scratch={scratch}",
        alloc.peak,
        s.heap_bytes()
    );
}

// ---------------------------------------------------------------- B5

/// Review B5(a) repro: zero scratch refuses the first literal-bearing
/// formula and leaves the state unchanged.
#[test]
fn review_zero_scratch_must_not_admit_allocating_plan() {
    let f = Formula {
        refs: vec![],
        l: 1,
        literal: 1,
    }
    .facts();
    let mut s = Store::new();
    s.budget.scratch = Some(0);
    let before = s.counts();
    let result = s.set_formula((0, 0, 0), &f);
    assert!(
        matches!(
            result,
            Err(AuthorityError::Admission {
                resource: "scratch",
                ..
            })
        ),
        "allocated plan/apply scratch under zero budget: {result:?}"
    );
    assert_eq!(s.counts(), before);
}

type Op = Box<dyn Fn(&mut Store) -> Result<MutationReport, AuthorityError>>;

/// Logical state: counts without the retained bytes (spare capacity may
/// remain after a failed reservation), live model bytes, the canonical
/// digest and every live cell's id.
fn logical(s: &Store) -> (Counts, u64, Digest, Vec<(Cell, Option<Vid>)>) {
    let mut c = s.counts();
    c.bytes = 0;
    let ids = s
        .formula_cells()
        .into_iter()
        .map(|c| (c, s.ids().id_of(c)))
        .collect();
    (c, s.live_model_bytes(), s.digest(), ids)
}

fn family(m: &mut Model, col: u32, rows: std::ops::Range<u32>, l: u64) {
    for r in rows {
        m.cells.insert(
            (0, r, col),
            Formula {
                refs: vec![rel(0, -1, 0)],
                l,
                literal: 1,
            },
        );
    }
}

/// Mutations covering every staged container: a new symbol key and new
/// groups, family punches with index flushes, a range clear with index
/// rebuilds and group reclamation, a kept id whose row grows, a unique
/// template replacing another (index rebuilds of the group tables), and
/// mutations whose repartitions commit.
fn scenarios() -> Vec<(String, Store, Op)> {
    let mut out: Vec<(String, Store, Op)> = Vec::new();

    let mut m = Model::default();
    family(&mut m, 3, 0..300, 0);
    family(&mut m, 5, 10..200, 0);
    let fams = build_from(&m);

    let sym = FormulaFacts {
        edges: vec![
            EdgeSpec {
                proj: abs(3, 3, 0),
                tag: Tag::X,
                origin: OriginSpec::Symbol(LkKey {
                    ctx: 0,
                    kind: 1,
                    name: "SymbolUnderTest".into(),
                }),
            },
            EdgeSpec {
                proj: rel(0, -1, 0),
                tag: Tag::R1,
                origin: OriginSpec::Text,
            },
        ],
        ..with_refs(vec![], 9, 4)
    };
    out.push((
        "new singleton with a new symbol key".into(),
        fams.clone(),
        Box::new(move |s| s.set_formula((0, 400, 9), &sym)),
    ));
    out.push((
        "punch a family".into(),
        fams.clone(),
        Box::new(|s| s.clear_cell((0, 150, 3))),
    ));
    let mut long = with_refs(vec![rel(0, -1, 0)], 0, 1);
    long.literals.extend([
        crate::engine::arena::ValueRef::from_raw(5),
        crate::engine::arena::ValueRef::from_raw(6),
    ]);
    out.push((
        "kept id with a longer literal row".into(),
        fams.clone(),
        Box::new(move |s| s.set_formula((0, 77, 3), &long)),
    ));

    // Many singletons with edges: indexes in levels, groups beyond the
    // minimum index size.
    let mut m = Model::default();
    for r in 0..400u32 {
        m.cells.insert(
            (0, r, 1 + r % 5),
            Formula {
                refs: vec![abs(r, 0, 0), rel(0, -1, 0)],
                l: u64::from(r) + 10,
                literal: r % 3,
            },
        );
    }
    let singles = build_from(&m);
    out.push((
        "range clear with index rebuilds and group reclamation".into(),
        singles.clone(),
        Box::new(|s| s.clear_rect(0, Rect::new(0, 0, 399, 3))),
    ));
    let fresh = with_refs(vec![abs(900, 7, 0)], 9_999, 2);
    out.push((
        "unique template replaces another".into(),
        singles.clone(),
        Box::new(move |s| s.set_formula((0, 13, 4), &fresh)),
    ));

    // Repartitions: refills between singletons until a trigger commits.
    let f = with_refs(vec![rel(0, -1, 0)], 0, 1);
    let mut s = Store::new();
    s.set_formula((0, 0, 1), &f).unwrap();
    s.set_formula((0, 2, 1), &f).unwrap();
    let mut found = 0;
    for r in [1u32, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] {
        let base = s.clone();
        let rep = s.set_formula((0, r, 1), &f).unwrap();
        if rep.repartitions > 0 && found < 2 {
            let g = f.clone();
            out.push((
                format!("refill at row {r} commits a repartition"),
                base,
                Box::new(move |s| s.set_formula((0, r, 1), &g)),
            ));
            found += 1;
        }
    }
    assert!(found > 0, "no repartition scenario");
    // Edge and node repartitions on a punched-and-refilled family.
    let mut m = Model::default();
    family(&mut m, 3, 0..300, 0);
    let mut s = build_from(&m);
    let g = with_refs(vec![rel(0, -1, 0)], 0, 1);
    let mut found = false;
    for r in (10..200u32).step_by(3) {
        s.clear_cell((0, r, 3)).unwrap();
        let base = s.clone();
        let committed = s.stats.repartitions_committed;
        s.set_formula((0, r, 3), &g).unwrap();
        if s.stats.repartitions_committed > committed + 1 {
            let g = g.clone();
            out.push((
                format!("refill at row {r} of a family commits edge and node repartitions"),
                base,
                Box::new(move |s| s.set_formula((0, r, 3), &g)),
            ));
            found = true;
            break;
        }
    }
    assert!(found, "no family refill commits two repartitions");
    out
}

/// Fail every allocation of every scenario in turn: a failure before the
/// apply returns `Alloc` with the logical state unchanged; a failure in a
/// repartition skips it (the mutation stands, the canonical relation and
/// ids equal the unfailed run's). Unfailed, the allocator's peak is within
/// the predicted peak and its net equals the accounted retained change.
#[test]
fn every_allocation_failure_is_transactional() {
    for (name, base, op) in scenarios() {
        let mut s = base.clone();
        let (r, m) = measure(None, || op(&mut s));
        let r = r.unwrap_or_else(|e| panic!("{name}: {e}"));
        assert!(!m.failed);
        assert!(
            m.peak >= 0 && m.peak as u64 <= r.predicted_peak_above_before,
            "{name}: measured peak {} above predicted {}",
            m.peak,
            r.predicted_peak_above_before
        );
        assert_eq!(
            m.net,
            r.after.bytes as i64 - r.before.bytes as i64,
            "{name}: allocator net != accounted retained change"
        );
        s.check().unwrap_or_else(|e| panic!("{name}: {e}"));
        let expected = (s.digest(), logical(&s).3);
        let base_state = logical(&base);
        let (mut refused, mut skipped, mut spare) = (0, 0, 0);
        for n in 0..m.allocs {
            let mut s = base.clone();
            let (r, fm) = measure(Some(n), || op(&mut s));
            assert!(fm.failed, "{name}: allocation {n} not reached");
            match r {
                Err(AuthorityError::Alloc) => {
                    refused += 1;
                    assert_eq!(
                        logical(&s),
                        base_state,
                        "{name}: failure at allocation {n} changed the state"
                    );
                    if s.heap_bytes() != base.heap_bytes() {
                        spare += 1;
                    }
                }
                Ok(_) => {
                    skipped += 1;
                    assert_eq!(
                        (s.digest(), logical(&s).3),
                        expected,
                        "{name}: repartition failure at {n}"
                    );
                    assert!(s.stats.alloc_failures > base.stats.alloc_failures);
                }
                Err(e) => panic!("{name}: allocation {n}: unexpected {e}"),
            }
            s.check()
                .unwrap_or_else(|e| panic!("{name}: allocation {n}: {e}"));
        }
        eprintln!(
            "B5 {name}: {} allocations; {refused} refused (state unchanged, {spare} with spare capacity kept), {skipped} repartition skips; peak {} of predicted {}",
            m.allocs, m.peak, r.predicted_peak_above_before
        );
        assert!(refused > 0, "{name}");
    }
}