formualizer-eval 0.7.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
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
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
//! Stage 2 — SCC evaluation under `CycleDetection::Runtime` (RFC #112).
//!
//! Test inventory from `formualizer-stage2-scc-evaluation-design.md` §5 and
//! the spec's §7 Error-policy subset: phantom (guarded) cycles produce
//! values, live cycles produce `#CIRC!` with live-cycle-only blast radius,
//! and `CycleDetection::Static` (the default) stays byte-for-byte today's
//! behavior.

use crate::engine::graph::editor::undo_engine::UndoEngine;
use crate::engine::named_range::{NameScope, NamedDefinition};
use crate::engine::{CycleConfig, CycleDetection, CyclePolicy, Engine, EvalConfig};
use crate::reference::{CellRef, Coord, RangeRef};
use crate::test_workbook::TestWorkbook;
use formualizer_common::{ExcelErrorKind, LiteralValue, PackedSheetCell};
use formualizer_parse::parser::parse;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};

fn runtime_cycle() -> CycleConfig {
    CycleConfig {
        detection: CycleDetection::Runtime,
        policy: CyclePolicy::Error,
    }
}

fn runtime_cfg() -> EvalConfig {
    EvalConfig::default()
        .with_cycle(runtime_cycle())
        .with_virtual_dep_telemetry(true)
}

fn runtime_engine() -> Engine<TestWorkbook> {
    Engine::new(TestWorkbook::new(), runtime_cfg())
}

fn static_engine() -> Engine<TestWorkbook> {
    Engine::new(TestWorkbook::new(), EvalConfig::default())
}

fn set_formula(engine: &mut Engine<TestWorkbook>, sheet: &str, row: u32, col: u32, f: &str) {
    engine
        .set_cell_formula(sheet, row, col, parse(f).expect("parse"))
        .expect("set formula");
}

fn set_value(engine: &mut Engine<TestWorkbook>, sheet: &str, row: u32, col: u32, v: LiteralValue) {
    engine
        .set_cell_value(sheet, row, col, v)
        .expect("set value");
}

fn num(engine: &Engine<TestWorkbook>, sheet: &str, row: u32, col: u32) -> f64 {
    match engine.get_cell_value(sheet, row, col) {
        Some(LiteralValue::Number(n)) => n,
        Some(LiteralValue::Int(i)) => i as f64,
        other => panic!("expected number at {sheet} r{row}c{col}, got {other:?}"),
    }
}

fn is_circ(engine: &Engine<TestWorkbook>, sheet: &str, row: u32, col: u32) -> bool {
    matches!(
        engine.get_cell_value(sheet, row, col),
        Some(LiteralValue::Error(e)) if e.kind == ExcelErrorKind::Circ
    )
}

/// Build the discussion-#99 guarded pair: A1 guard, A2/A3 the static SCC.
fn build_99_pair(engine: &mut Engine<TestWorkbook>, guard: bool) {
    set_value(engine, "Sheet1", 1, 1, LiteralValue::Boolean(guard));
    set_formula(engine, "Sheet1", 2, 1, "=IF(A1,555,A3)");
    set_formula(engine, "Sheet1", 3, 1, "=IF(A1,A2,999)");
}

/* ───────────────────────── 7.1 self-reference ───────────────────────── */

/// Engine rule that PRE-EMPTS spec §7.1's eval-time `#CIRC!`: a direct
/// self-reference (`=A1+1` in A1, or an expanded range containing the cell)
/// is rejected when the formula is SET ("Self-reference detected"). Runtime
/// mode must not change that edit-time rule. Eval-time self-loop handling is
/// still exercised through dynamic refs (see the INDIRECT tests) and the
/// `live_graph` unit tests.
#[test]
fn direct_self_reference_rejected_at_ingest_in_both_modes() {
    for cfg in [EvalConfig::default(), runtime_cfg()] {
        let mut engine = Engine::new(TestWorkbook::new(), cfg);
        let err = engine
            .set_cell_formula("Sheet1", 1, 1, parse("=A1+1").unwrap())
            .unwrap_err();
        assert_eq!(err.kind, ExcelErrorKind::Circ);
        // Dense range covering the cell expands to direct deps → same rule.
        let err = engine
            .set_cell_formula("Sheet1", 5, 1, parse("=SUM(A1:A10)").unwrap())
            .unwrap_err();
        assert_eq!(err.kind, ExcelErrorKind::Circ);
    }
}

#[test]
fn live_two_cycle_is_circ_via_evaluate_all_and_evaluate_cell() {
    let mut engine = runtime_engine();
    set_formula(&mut engine, "Sheet1", 1, 1, "=B1+1");
    set_formula(&mut engine, "Sheet1", 1, 2, "=A1+1");
    let res = engine.evaluate_all().unwrap();
    assert!(is_circ(&engine, "Sheet1", 1, 1));
    assert!(is_circ(&engine, "Sheet1", 1, 2));
    assert_eq!(res.cycle_errors, 1);
    let t = engine.last_cycle_telemetry();
    assert_eq!(t.static_sccs, 1);
    assert_eq!(t.phantom_sccs, 0);
    assert_eq!(t.live_cycles_witnessed, 1);
    assert_eq!(t.circ_cells_stamped, 2);

    let mut engine = runtime_engine();
    set_formula(&mut engine, "Sheet1", 1, 1, "=B1+1");
    set_formula(&mut engine, "Sheet1", 1, 2, "=A1+1");
    let v = engine.evaluate_cell("Sheet1", 1, 1).unwrap();
    assert!(
        matches!(v, Some(LiteralValue::Error(ref e)) if e.kind == ExcelErrorKind::Circ),
        "got {v:?}"
    );
}

/* ──────────────── 7.2 #99 guarded pair — both polarities ─────────────── */

#[test]
fn guarded_pair_99_evaluates_to_values_under_runtime() {
    let mut engine = runtime_engine();
    build_99_pair(&mut engine, true);
    let res = engine.evaluate_all().unwrap();
    assert_eq!(num(&engine, "Sheet1", 2, 1), 555.0);
    assert_eq!(num(&engine, "Sheet1", 3, 1), 555.0);
    assert_eq!(res.cycle_errors, 0, "phantom SCC must not count as a cycle");
    let t = engine.last_cycle_telemetry();
    assert_eq!(t.static_sccs, 1);
    assert_eq!(t.phantom_sccs, 1);
    assert_eq!(t.live_cycles_witnessed, 0);
    assert_eq!(t.circ_cells_stamped, 0);
    // A2 is ordered first and A3 reads it fresh: exactly one pass.
    assert_eq!(t.settle_passes_total, 1);
}

#[test]
fn guarded_pair_99_opposite_polarity_settles_to_values() {
    // Same pair but the live edge points at the *later*-ordered member:
    // A2 = IF(A1, A3, 999), A3 = IF(A1, 555, A2). With A1=TRUE the live
    // edge is A2→A3 and A2 runs first, so one settle re-eval is needed.
    let mut engine = runtime_engine();
    set_value(&mut engine, "Sheet1", 1, 1, LiteralValue::Boolean(true));
    set_formula(&mut engine, "Sheet1", 2, 1, "=IF(A1,A3,999)");
    set_formula(&mut engine, "Sheet1", 3, 1, "=IF(A1,555,A2)");
    engine.evaluate_all().unwrap();
    assert_eq!(num(&engine, "Sheet1", 2, 1), 555.0);
    assert_eq!(num(&engine, "Sheet1", 3, 1), 555.0);
    let t = engine.last_cycle_telemetry();
    assert_eq!(t.phantom_sccs, 1);
    assert_eq!(t.settle_passes_total, 2, "polarity flip costs one settle");
}

#[test]
fn guarded_pair_99_guard_flip_between_recalcs_reverses_values() {
    let mut engine = runtime_engine();
    build_99_pair(&mut engine, true);
    engine.evaluate_all().unwrap();
    assert_eq!(num(&engine, "Sheet1", 2, 1), 555.0);
    assert_eq!(num(&engine, "Sheet1", 3, 1), 555.0);

    // Flip the guard: live edges reverse, values follow.
    set_value(&mut engine, "Sheet1", 1, 1, LiteralValue::Boolean(false));
    engine.evaluate_all().unwrap();
    assert_eq!(num(&engine, "Sheet1", 2, 1), 999.0);
    assert_eq!(num(&engine, "Sheet1", 3, 1), 999.0);
    assert_eq!(engine.last_cycle_telemetry().phantom_sccs, 1);

    // And back.
    set_value(&mut engine, "Sheet1", 1, 1, LiteralValue::Boolean(true));
    engine.evaluate_all().unwrap();
    assert_eq!(num(&engine, "Sheet1", 2, 1), 555.0);
    assert_eq!(num(&engine, "Sheet1", 3, 1), 555.0);
}

#[test]
fn guarded_pair_99_via_evaluate_cell_both_polarities() {
    // Demand path (evaluate_until under the hood): the demand closure pulls
    // the whole SCC via static deps and evaluates it as one task.
    let mut engine = runtime_engine();
    build_99_pair(&mut engine, true);
    let v = engine.evaluate_cell("Sheet1", 2, 1).unwrap();
    assert_eq!(v, Some(LiteralValue::Number(555.0)));
    assert_eq!(num(&engine, "Sheet1", 3, 1), 555.0);

    let mut engine = runtime_engine();
    build_99_pair(&mut engine, false);
    let v = engine.evaluate_cell("Sheet1", 3, 1).unwrap();
    assert_eq!(v, Some(LiteralValue::Number(999.0)));
    assert_eq!(num(&engine, "Sheet1", 2, 1), 999.0);
}

#[test]
fn guarded_chains_three_and_five_cells_mixed_polarity() {
    // 3-cell guarded ring: every static edge exists, live edges form a chain.
    let mut engine = runtime_engine();
    set_value(&mut engine, "Sheet1", 1, 7, LiteralValue::Boolean(true)); // G1
    set_formula(&mut engine, "Sheet1", 1, 1, "=IF(G1,100,A2)");
    set_formula(&mut engine, "Sheet1", 2, 1, "=IF(G1,A1,A3)");
    set_formula(&mut engine, "Sheet1", 3, 1, "=IF(G1,A2,100)");
    engine.evaluate_all().unwrap();
    for r in 1..=3 {
        assert_eq!(num(&engine, "Sheet1", r, 1), 100.0, "row {r}");
    }
    assert_eq!(engine.last_cycle_telemetry().phantom_sccs, 1);

    // Flip: live edges reverse direction (reads now go down the column,
    // against member order → settling required).
    set_value(&mut engine, "Sheet1", 1, 7, LiteralValue::Boolean(false));
    engine.evaluate_all().unwrap();
    for r in 1..=3 {
        assert_eq!(num(&engine, "Sheet1", r, 1), 100.0, "row {r} after flip");
    }

    // 5-cell ring with mixed polarity guards.
    let mut engine = runtime_engine();
    set_value(&mut engine, "Sheet1", 1, 7, LiteralValue::Boolean(true));
    set_formula(&mut engine, "Sheet1", 1, 1, "=IF(G1,42,B5)");
    set_formula(&mut engine, "Sheet1", 2, 1, "=IF(G1,A1,B1)"); // reads up (fresh)
    set_formula(&mut engine, "Sheet1", 3, 1, "=IF(NOT(G1),B1,A2)"); // reads up (fresh)
    set_formula(&mut engine, "Sheet1", 4, 1, "=IF(G1,A5,A3)"); // reads DOWN (stale)
    set_formula(&mut engine, "Sheet1", 5, 1, "=IF(G1,A3,A4)"); // reads up
    // Static ring closure: B5 referenced by A1's untaken branch.
    set_formula(&mut engine, "Sheet1", 5, 2, "=A1");
    engine.evaluate_all().unwrap();
    for r in 1..=5 {
        assert_eq!(num(&engine, "Sheet1", r, 1), 42.0, "row {r}");
    }
}

/* ─────────────── 7.3 guard inside the cycle (always live) ────────────── */

#[test]
fn guard_reading_cycle_member_is_a_live_cycle() {
    // Spec §7.3's literal example (`=IF(A1, A2+1, 5)` in A2) contains a
    // direct self-reference and is rejected at ingest; the same semantics —
    // a guard read that is itself a live edge into the cycle — is built with
    // two cells: A1's GUARD always reads A2, and A2 always reads A1, so the
    // live subgraph is cyclic in every guard state.
    for guard_seed in [0.0, 9.0] {
        let mut engine = runtime_engine();
        set_value(
            &mut engine,
            "Sheet1",
            1,
            2,
            LiteralValue::Number(guard_seed),
        );
        set_formula(&mut engine, "Sheet1", 1, 1, "=IF(A2>0,A2+1,5)");
        set_formula(&mut engine, "Sheet1", 2, 1, "=A1+B1");
        engine.evaluate_all().unwrap();
        assert!(is_circ(&engine, "Sheet1", 1, 1), "seed {guard_seed}");
        assert!(is_circ(&engine, "Sheet1", 2, 1), "seed {guard_seed}");
        assert_eq!(engine.last_cycle_telemetry().live_cycles_witnessed, 1);
    }
}

/* ─────────────── 7.4 arithmetic routing — always live ────────────────── */

#[test]
fn arithmetic_routing_stays_circ() {
    // Both operands of `+`/`*` always evaluate: the reads genuinely occur,
    // so the cycle is live regardless of the mixing weight g (A1).
    let mut engine = runtime_engine();
    set_value(&mut engine, "Sheet1", 1, 1, LiteralValue::Number(0.0)); // g
    set_formula(&mut engine, "Sheet1", 1, 2, "=A1*99+(1-A1)*C1"); // B1
    set_formula(&mut engine, "Sheet1", 1, 3, "=A1*B1+(1-A1)*7"); // C1
    engine.evaluate_all().unwrap();
    assert!(is_circ(&engine, "Sheet1", 1, 2));
    assert!(is_circ(&engine, "Sheet1", 1, 3));
}

/* ─────────────────────── 7.8 range self-inclusion ────────────────────── */

#[test]
fn range_mediated_live_cycle_is_circ() {
    // A5 ranges over B1:B10 which contains B5 = A5: the rect read records a
    // live edge into B5 and the cycle is witnessed. (Direct self-inclusion —
    // `=SUM(A1:A10)` in A5 — is rejected at ingest; see the §7.1 test.)
    let mut engine = runtime_engine();
    for r in 1..=10u32 {
        if r != 5 {
            set_value(&mut engine, "Sheet1", r, 2, LiteralValue::Number(r as f64));
        }
    }
    set_formula(&mut engine, "Sheet1", 5, 1, "=SUM(B1:B10)");
    set_formula(&mut engine, "Sheet1", 5, 2, "=A5");
    engine.evaluate_all().unwrap();
    assert!(is_circ(&engine, "Sheet1", 5, 1));
    assert!(is_circ(&engine, "Sheet1", 5, 2));
    assert_eq!(engine.last_cycle_telemetry().live_cycles_witnessed, 1);
}

#[test]
fn range_mediated_guarded_cycle_is_phantom_until_guard_flips() {
    // Same shape but the range read sits in a guarded branch: phantom while
    // the guard holds (Static would stamp #CIRC — documented diff), live
    // cycle when it flips.
    let mut engine = runtime_engine();
    set_value(&mut engine, "Sheet1", 1, 7, LiteralValue::Boolean(true)); // G1
    for r in 1..=10u32 {
        if r != 5 {
            set_value(&mut engine, "Sheet1", r, 2, LiteralValue::Number(1.0));
        }
    }
    set_formula(&mut engine, "Sheet1", 5, 1, "=IF(G1,5,SUM(B1:B10))");
    set_formula(&mut engine, "Sheet1", 5, 2, "=A5");
    engine.evaluate_all().unwrap();
    assert_eq!(num(&engine, "Sheet1", 5, 1), 5.0);
    assert_eq!(num(&engine, "Sheet1", 5, 2), 5.0);
    assert_eq!(engine.last_cycle_telemetry().phantom_sccs, 1);

    set_value(&mut engine, "Sheet1", 1, 7, LiteralValue::Boolean(false));
    engine.evaluate_all().unwrap();
    assert!(is_circ(&engine, "Sheet1", 5, 1));
    assert!(is_circ(&engine, "Sheet1", 5, 2));
}

#[test]
fn named_range_covering_the_cell_rejected_at_ingest_in_both_modes() {
    // A name whose region covers the formula's own cell is rejected when the
    // formula is set ("Circular reference through named range") — existing
    // engine rule, identical under Runtime.
    for cfg in [EvalConfig::default(), runtime_cfg()] {
        let mut engine = Engine::new(TestWorkbook::new(), cfg);
        let sheet_id = engine.sheet_id("Sheet1").unwrap();
        let nr = RangeRef::new(
            CellRef::new(sheet_id, Coord::from_excel(1, 1, true, true)),
            CellRef::new(sheet_id, Coord::from_excel(10, 1, true, true)),
        );
        engine
            .define_name("COVER", NamedDefinition::Range(nr), NameScope::Workbook)
            .unwrap();
        let err = engine
            .set_cell_formula("Sheet1", 5, 1, parse("=SUM(COVER)").unwrap())
            .unwrap_err();
        assert_eq!(err.kind, ExcelErrorKind::Circ);
    }
}

/// Spec §7.8 stripe-path self-inclusion (#120): whole-column `=SUM(B:B)` in
/// B1 references column B, which contains B1 itself. At ingest the stripe
/// region is detected to cover the formula's own cell, so a SELF-LOOP edge is
/// recorded; Tarjan then sees a single-vertex SCC with a self-loop and
/// `separate_cycles` classifies it as a cycle. Both modes resolve to `#CIRC!`
/// under `CyclePolicy::Error` and Runtime still matches Static exactly.
///
/// This pin previously documented the *incorrect* pre-#120 value (silent
/// `9.0`, 0 cycle errors); it was consciously updated when the detection gap
/// was closed.
#[test]
fn whole_column_self_inclusion_gap_runtime_matches_static() {
    let run = |cfg: EvalConfig| {
        let mut engine = Engine::new(TestWorkbook::new(), cfg);
        for r in 2..=4u32 {
            set_value(&mut engine, "Sheet1", r, 2, LiteralValue::Number(r as f64));
        }
        set_formula(&mut engine, "Sheet1", 1, 2, "=SUM(B:B)");
        let res = engine.evaluate_all().unwrap();
        (engine.get_cell_value("Sheet1", 1, 2), res.cycle_errors)
    };
    let static_out = run(EvalConfig::default());
    let runtime_out = run(runtime_cfg());
    assert_eq!(static_out, runtime_out);
    // Whole-column self-inclusion is now a detected cycle in both modes.
    assert!(
        matches!(
            static_out.0,
            Some(LiteralValue::Error(ref e)) if e.kind == ExcelErrorKind::Circ
        ),
        "expected #CIRC, got {:?}",
        static_out.0
    );
    assert_eq!(static_out.1, 1);
}

/// #120: whole-column self-inclusion `=SUM(B:B)` placed in column B is a
/// circular reference (Excel flags it). The stripe region covers the
/// formula's own cell, so a self-loop is recorded at ingest and the cell
/// resolves to `#CIRC!` under both detection modes.
#[test]
fn whole_column_in_column_self_inclusion_is_circ_both_modes() {
    for cfg in [EvalConfig::default(), runtime_cfg()] {
        let mut engine = Engine::new(TestWorkbook::new(), cfg);
        for r in 2..=4u32 {
            set_value(&mut engine, "Sheet1", r, 2, LiteralValue::Number(r as f64));
        }
        // B1 = SUM(B:B) ⇒ B1 ∈ column B ⇒ self-loop.
        set_formula(&mut engine, "Sheet1", 1, 2, "=SUM(B:B)");
        engine.evaluate_all().unwrap();
        assert!(
            is_circ(&engine, "Sheet1", 1, 2),
            "B1=SUM(B:B) must be #CIRC, got {:?}",
            engine.get_cell_value("Sheet1", 1, 2)
        );
    }
}

/// #120 symmetric whole-row case: `=SUM(2:2)` placed in row 2 is circular.
#[test]
fn whole_row_in_row_self_inclusion_is_circ_both_modes() {
    for cfg in [EvalConfig::default(), runtime_cfg()] {
        let mut engine = Engine::new(TestWorkbook::new(), cfg);
        for c in 3..=5u32 {
            set_value(&mut engine, "Sheet1", 2, c, LiteralValue::Number(c as f64));
        }
        // B2 = SUM(2:2) ⇒ B2 ∈ row 2 ⇒ self-loop.
        set_formula(&mut engine, "Sheet1", 2, 2, "=SUM(2:2)");
        engine.evaluate_all().unwrap();
        assert!(
            is_circ(&engine, "Sheet1", 2, 2),
            "B2=SUM(2:2) must be #CIRC, got {:?}",
            engine.get_cell_value("Sheet1", 2, 2)
        );
    }
}

/// #120 large bounded range over the expansion limit: `=SUM(B1:B100000)` in
/// B5 is stripe-compressed (not expanded to explicit cell edges), so the
/// pre-#120 ingest self-reference check never saw B5. The stripe region now
/// covers B5 ⇒ self-loop ⇒ `#CIRC!`.
#[test]
fn large_bounded_range_self_inclusion_is_circ_both_modes() {
    for cfg in [EvalConfig::default(), runtime_cfg()] {
        let mut engine = Engine::new(TestWorkbook::new(), cfg);
        for r in 1..=4u32 {
            set_value(&mut engine, "Sheet1", r, 2, LiteralValue::Number(r as f64));
        }
        set_formula(&mut engine, "Sheet1", 5, 2, "=SUM(B1:B100000)");
        engine.evaluate_all().unwrap();
        assert!(
            is_circ(&engine, "Sheet1", 5, 2),
            "B5=SUM(B1:B100000) must be #CIRC, got {:?}",
            engine.get_cell_value("Sheet1", 5, 2)
        );
    }
}

/// #120 NEGATIVE control: a whole-column reference consumed from OUTSIDE the
/// column stays acyclic and computes normally.
#[test]
fn whole_column_referenced_from_outside_stays_acyclic() {
    for cfg in [EvalConfig::default(), runtime_cfg()] {
        let mut engine = Engine::new(TestWorkbook::new(), cfg);
        for r in 1..=3u32 {
            set_value(&mut engine, "Sheet1", r, 2, LiteralValue::Number(r as f64));
        }
        // C1 = SUM(B:B): column C is not referenced ⇒ no self-loop.
        set_formula(&mut engine, "Sheet1", 1, 3, "=SUM(B:B)");
        let res = engine.evaluate_all().unwrap();
        assert_eq!(num(&engine, "Sheet1", 1, 3), 6.0);
        assert_eq!(res.cycle_errors, 0);
    }
}

/// #120 NEGATIVE control: a large bounded range NOT intersecting the
/// formula's own cell is unchanged (computes, no cycle).
#[test]
fn large_bounded_range_non_intersecting_stays_acyclic() {
    for cfg in [EvalConfig::default(), runtime_cfg()] {
        let mut engine = Engine::new(TestWorkbook::new(), cfg);
        for r in 1..=4u32 {
            set_value(&mut engine, "Sheet1", r, 1, LiteralValue::Number(r as f64));
        }
        // B5 = SUM(A1:A100000): own cell B5 is in column B, range is column A.
        set_formula(&mut engine, "Sheet1", 5, 2, "=SUM(A1:A100000)");
        let res = engine.evaluate_all().unwrap();
        assert_eq!(num(&engine, "Sheet1", 5, 2), 10.0);
        assert_eq!(res.cycle_errors, 0);
    }
}

/* ──────────────────── 7.9 spill anchor inside an SCC ─────────────────── */

#[test]
fn spill_anchor_in_scc_is_circ_and_region_freed() {
    let mut engine = runtime_engine();
    // Prime: B1 spills SEQUENCE(C1) with C1 = 3 → B1:B3.
    set_value(&mut engine, "Sheet1", 1, 3, LiteralValue::Number(3.0));
    set_formula(&mut engine, "Sheet1", 1, 2, "=SEQUENCE(C1)");
    engine.evaluate_all().unwrap();
    assert_eq!(num(&engine, "Sheet1", 2, 2), 2.0);
    assert_eq!(num(&engine, "Sheet1", 3, 2), 3.0);

    // Introduce the cycle: C1 = B1 + 1 ⇒ static SCC {B1, C1}.
    set_formula(&mut engine, "Sheet1", 1, 3, "=B1+1");
    engine.evaluate_all().unwrap();

    // Anchor pre-stamped #CIRC with spill teardown (spec §7.9, #115).
    assert!(is_circ(&engine, "Sheet1", 1, 2));
    for r in 2..=3 {
        assert!(
            matches!(
                engine.get_cell_value("Sheet1", r, 2),
                None | Some(LiteralValue::Empty)
            ),
            "spilled B{r} must be cleared, got {:?}",
            engine.get_cell_value("Sheet1", r, 2)
        );
    }
    // C1 reads the stamped anchor: #CIRC propagates through arithmetic.
    assert!(is_circ(&engine, "Sheet1", 1, 3));

    // Region freed: a new spill into the former region succeeds.
    set_formula(&mut engine, "Sheet1", 2, 2, "=SEQUENCE(2)");
    engine.evaluate_all().unwrap();
    assert_eq!(num(&engine, "Sheet1", 2, 2), 1.0);
    assert_eq!(num(&engine, "Sheet1", 3, 2), 2.0);
}

#[test]
fn array_result_first_produced_inside_scc_is_stamped() {
    // A member that would *become* a spill anchor during the SCC task gets
    // the conservative §7.9 verdict instead of spilling.
    let mut engine = runtime_engine();
    set_formula(&mut engine, "Sheet1", 1, 1, "=SEQUENCE(2)+0*A2");
    set_formula(&mut engine, "Sheet1", 2, 1, "=A1");
    engine.evaluate_all().unwrap();
    assert!(is_circ(&engine, "Sheet1", 1, 1));
    assert!(
        is_circ(&engine, "Sheet1", 2, 1),
        "readers see propagated #CIRC"
    );
}

/* ───────────── 7.13 cross-sheet and named-formula members ────────────── */

#[test]
fn cross_sheet_phantom_scc_produces_values() {
    let mut engine = runtime_engine();
    engine.add_sheet("Sheet2").unwrap();
    set_formula(&mut engine, "Sheet1", 1, 1, "=IF(TRUE,5,Sheet2!A1)");
    set_formula(&mut engine, "Sheet2", 1, 1, "=Sheet1!A1+1");
    engine.evaluate_all().unwrap();
    assert_eq!(num(&engine, "Sheet1", 1, 1), 5.0);
    assert_eq!(num(&engine, "Sheet2", 1, 1), 6.0);
    assert_eq!(engine.last_cycle_telemetry().phantom_sccs, 1);
}

/// Entering a cell formula that reads a name already depending on that cell
/// is rejected at ingest in both modes (existing rule).
#[test]
fn named_formula_cycle_rejected_at_ingest_in_both_modes() {
    for cfg in [EvalConfig::default(), runtime_cfg()] {
        let mut engine = Engine::new(TestWorkbook::new(), cfg);
        engine
            .define_name(
                "N",
                NamedDefinition::Formula {
                    ast: parse("=A1+1").unwrap(),
                    dependencies: Vec::new(),
                    range_deps: Vec::new(),
                },
                NameScope::Workbook,
            )
            .unwrap();
        let err = engine
            .set_cell_formula("Sheet1", 1, 1, parse("=N").unwrap())
            .unwrap_err();
        assert_eq!(err.kind, ExcelErrorKind::Circ);
    }
}

/// Direct SCC-task test for name-vertex members (spec §7.13): the scheduler
/// cannot currently produce a {cell, name} SCC (no static name-cycle edges),
/// so drive `evaluate_scc_unit` with the membership directly. Pass 1 records
/// the cell's read OF the name (by folded key) and the name's read of the
/// cell — a witnessed live cycle stamping both.
#[test]
fn named_formula_member_live_cycle_is_circ_in_scc_task() {
    let mut engine = runtime_engine();
    engine
        .define_name(
            "N",
            NamedDefinition::Literal(LiteralValue::Number(1.0)),
            NameScope::Workbook,
        )
        .unwrap();
    set_formula(&mut engine, "Sheet1", 1, 1, "=N");
    engine.evaluate_all().unwrap();
    // Re-point the name at a formula reading A1 (allowed via update path).
    engine
        .graph
        .update_name(
            "N",
            NamedDefinition::Formula {
                ast: parse("=A1+1").unwrap(),
                dependencies: Vec::new(),
                range_deps: Vec::new(),
            },
            NameScope::Workbook,
        )
        .unwrap();

    let sheet_id = engine.sheet_id("Sheet1").unwrap();
    let a1 = *engine
        .graph
        .get_vertex_id_for_address(&CellRef::new(sheet_id, Coord::from_excel(1, 1, true, true)))
        .unwrap();
    let n = engine
        .graph
        .resolve_name_entry("N", sheet_id)
        .expect("name entry")
        .vertex;

    let stamped = engine.evaluate_scc_unit(&[a1, n], None, None).unwrap();
    assert_eq!(stamped, 2, "both the cell and the name member are stamped");
    assert!(is_circ(&engine, "Sheet1", 1, 1));
    assert!(matches!(
        engine.graph.get_value(n),
        Some(LiteralValue::Error(ref e)) if e.kind == ExcelErrorKind::Circ
    ));
    let t = engine.last_cycle_telemetry();
    assert_eq!(t.live_cycles_witnessed, 1);
    assert_eq!(t.circ_cells_stamped, 2);
}

/// Phantom counterpart: the cell's guarded branch never reads the name, so
/// the SCC task produces values for both members.
#[test]
fn named_formula_member_phantom_produces_values_in_scc_task() {
    let mut engine = runtime_engine();
    engine
        .define_name(
            "N",
            NamedDefinition::Literal(LiteralValue::Number(1.0)),
            NameScope::Workbook,
        )
        .unwrap();
    set_formula(&mut engine, "Sheet1", 1, 1, "=IF(TRUE,2,N)");
    engine.evaluate_all().unwrap();
    engine
        .graph
        .update_name(
            "N",
            NamedDefinition::Formula {
                ast: parse("=A1+1").unwrap(),
                dependencies: Vec::new(),
                range_deps: Vec::new(),
            },
            NameScope::Workbook,
        )
        .unwrap();

    let sheet_id = engine.sheet_id("Sheet1").unwrap();
    let a1 = *engine
        .graph
        .get_vertex_id_for_address(&CellRef::new(sheet_id, Coord::from_excel(1, 1, true, true)))
        .unwrap();
    let n = engine
        .graph
        .resolve_name_entry("N", sheet_id)
        .expect("name entry")
        .vertex;

    let stamped = engine.evaluate_scc_unit(&[a1, n], None, None).unwrap();
    assert_eq!(stamped, 0);
    assert_eq!(num(&engine, "Sheet1", 1, 1), 2.0);
    assert_eq!(engine.graph.get_value(n), Some(LiteralValue::Number(3.0)));
    assert_eq!(engine.last_cycle_telemetry().phantom_sccs, 1);
}

/* ───────────── 7.14 user value over a formula member ─────────────────── */

#[test]
fn user_value_over_formula_removes_it_from_the_scc() {
    let mut engine = runtime_engine();
    build_99_pair(&mut engine, true);
    engine.evaluate_all().unwrap();
    assert_eq!(engine.last_cycle_telemetry().static_sccs, 1);

    // Overwrite A3 with a literal: its formula is removed (engine rule), so
    // no static SCC remains.
    set_value(&mut engine, "Sheet1", 3, 1, LiteralValue::Number(777.0));
    let res = engine.evaluate_all().unwrap();
    assert_eq!(res.cycle_errors, 0);
    assert_eq!(engine.last_cycle_telemetry().static_sccs, 0);
    assert_eq!(num(&engine, "Sheet1", 2, 1), 555.0);
    assert_eq!(num(&engine, "Sheet1", 3, 1), 777.0);
}

/* ─────────────────────────── blast radius ────────────────────────────── */

#[test]
fn blast_radius_only_live_cycle_members_are_stamped() {
    // 10-member static ring C1..C10 (each row r references row r+1 in an
    // untaken branch; C10 closes the ring to C1). Only C5↔C6 is live.
    let mut engine = runtime_engine();
    for r in 1..=10u32 {
        let f = match r {
            5 => "=IF(TRUE,C6,C6)".to_string(), // live edge C5→C6 (both arms)
            6 => "=IF(TRUE,C5,C7)".to_string(), // live edge C6→C5
            10 => "=IF(TRUE,100,C1)".to_string(),
            _ => format!("=IF(TRUE,{},C{})", r * 10, r + 1),
        };
        set_formula(&mut engine, "Sheet1", r, 3, &f);
    }
    let res = engine.evaluate_all().unwrap();

    assert!(is_circ(&engine, "Sheet1", 5, 3), "C5 on the live cycle");
    assert!(is_circ(&engine, "Sheet1", 6, 3), "C6 on the live cycle");
    for r in [1u32, 2, 3, 4, 7, 8, 9] {
        assert_eq!(num(&engine, "Sheet1", r, 3), (r * 10) as f64, "C{r}");
    }
    assert_eq!(num(&engine, "Sheet1", 10, 3), 100.0);

    assert_eq!(res.cycle_errors, 1);
    let t = engine.last_cycle_telemetry();
    assert_eq!(t.static_sccs, 1);
    assert_eq!(t.live_cycles_witnessed, 1);
    assert_eq!(t.circ_cells_stamped, 2, "exactly the live-cycle members");
    assert_eq!(t.phantom_sccs, 0);
}

/* ─────────────────────────── settle mechanics ────────────────────────── */

#[test]
fn engineered_stale_reader_settles_to_exact_values() {
    // A1 (ordered first) live-reads A2 (ordered later): pass 1 sees A2's
    // pre-task value, the settle pass fixes it.
    let mut engine = runtime_engine();
    set_formula(&mut engine, "Sheet1", 1, 1, "=IF(TRUE,A2,0)");
    set_formula(&mut engine, "Sheet1", 2, 1, "=IF(TRUE,7,A1)");
    engine.evaluate_all().unwrap();
    assert_eq!(num(&engine, "Sheet1", 1, 1), 7.0);
    assert_eq!(num(&engine, "Sheet1", 2, 1), 7.0);
    let t = engine.last_cycle_telemetry();
    assert_eq!(t.phantom_sccs, 1);
    assert_eq!(t.settle_passes_total, 2);
    assert_eq!(t.max_passes_single_scc, 2);
}

#[test]
fn branch_flip_during_settle_creating_live_cycle_is_circ() {
    // Pass 1 is acyclic; C1's settle re-eval flips its branch onto C3,
    // closing a live cycle C1↔C3 that only classification-after-settle sees.
    let mut engine = runtime_engine();
    set_formula(&mut engine, "Sheet1", 1, 1, "=IF(A2=999,A3,7)");
    set_formula(&mut engine, "Sheet1", 2, 1, "=IF(TRUE,999,A1)");
    set_formula(&mut engine, "Sheet1", 3, 1, "=IF(TRUE,A1,8)");
    engine.evaluate_all().unwrap();
    assert!(is_circ(&engine, "Sheet1", 1, 1), "A1 joins the live cycle");
    assert!(is_circ(&engine, "Sheet1", 3, 1), "A3 joins the live cycle");
    assert_eq!(num(&engine, "Sheet1", 2, 1), 999.0, "A2 keeps its value");
    let t = engine.last_cycle_telemetry();
    assert_eq!(t.live_cycles_witnessed, 1);
    assert_eq!(t.circ_cells_stamped, 2);
    assert_eq!(t.capped_sccs, 0);
}

/* ───────────────────────────── side effects ──────────────────────────── */

#[test]
fn one_delta_per_member_per_recalc() {
    let mut engine = runtime_engine();
    build_99_pair(&mut engine, true);
    let (_res, delta) = engine.evaluate_all_with_delta().unwrap();
    let sheet_id = engine.sheet_id("Sheet1").unwrap();
    let mut expected = vec![
        PackedSheetCell::try_new(sheet_id, 1, 0).unwrap(), // A2 (0-based r1c0)
        PackedSheetCell::try_new(sheet_id, 2, 0).unwrap(), // A3
    ];
    expected.sort_unstable();
    assert_eq!(delta.changed_cells, expected);

    // No changes → no deltas (values persist, the SCC isn't re-stamped).
    let (_res, delta) = engine.evaluate_all_with_delta().unwrap();
    assert!(
        delta.changed_cells.is_empty(),
        "got {:?}",
        delta.changed_cells
    );

    // Guard flip: exactly one delta per member again.
    set_value(&mut engine, "Sheet1", 1, 1, LiteralValue::Boolean(false));
    let (_res, delta) = engine.evaluate_all_with_delta().unwrap();
    assert_eq!(delta.changed_cells, expected);
}

#[test]
fn evaluation_writes_do_not_hit_the_changelog() {
    // G11 confirm: `evaluate_all_logged` records only spill events for
    // computed results; Runtime SCC commits must not add anything either.
    use crate::engine::ChangeLog;
    use crate::engine::graph::editor::change_log::ChangeEvent;

    let mut engine = runtime_engine();
    build_99_pair(&mut engine, true);
    let mut log = ChangeLog::new();
    engine.evaluate_all_logged(&mut log).unwrap();
    assert_eq!(num(&engine, "Sheet1", 2, 1), 555.0);
    for ev in log.events() {
        assert!(
            matches!(
                ev,
                ChangeEvent::CompoundStart { .. } | ChangeEvent::CompoundEnd { .. }
            ),
            "unexpected changelog event from SCC evaluation: {ev:?}"
        );
    }
}

#[test]
fn undo_of_the_triggering_edit_restores_pre_recalc_values() {
    let mut engine = runtime_engine();
    let mut undo = UndoEngine::new();

    let (_v, journal) = engine
        .action_atomic_journal("seed".to_string(), |tx| {
            tx.set_cell_value("Sheet1", 1, 1, LiteralValue::Boolean(true))?;
            tx.set_cell_formula("Sheet1", 2, 1, parse("=IF(A1,555,A3)").unwrap())?;
            tx.set_cell_formula("Sheet1", 3, 1, parse("=IF(A1,A2,999)").unwrap())?;
            Ok(())
        })
        .unwrap();
    undo.push_action(journal);
    engine.evaluate_all().unwrap();
    assert_eq!(num(&engine, "Sheet1", 2, 1), 555.0);

    let (_v, journal) = engine
        .action_atomic_journal("flip".to_string(), |tx| {
            tx.set_cell_value("Sheet1", 1, 1, LiteralValue::Boolean(false))?;
            Ok(())
        })
        .unwrap();
    undo.push_action(journal);
    engine.evaluate_all().unwrap();
    assert_eq!(num(&engine, "Sheet1", 2, 1), 999.0);
    assert_eq!(num(&engine, "Sheet1", 3, 1), 999.0);

    // Undo the flip and recalc: pre-flip values come back.
    engine.undo_action(&mut undo).unwrap();
    engine.evaluate_all().unwrap();
    assert_eq!(num(&engine, "Sheet1", 2, 1), 555.0);
    assert_eq!(num(&engine, "Sheet1", 3, 1), 555.0);
}

#[test]
fn cancellation_is_honored_at_settle_pass_boundaries() {
    use crate::args::ArgSchema;
    use crate::function::{FnCaps, Function};
    use crate::traits::{ArgumentHandle, FunctionContext};

    // TRIP() sets the cancel flag as a side effect of pass 1; the SCC task's
    // per-settle-pass check must observe it before re-evaluating.
    #[derive(Debug)]
    struct TripFn(Arc<AtomicBool>);
    impl Function for TripFn {
        fn caps(&self) -> FnCaps {
            FnCaps::empty()
        }
        fn name(&self) -> &'static str {
            "TRIPCANCEL"
        }
        fn arg_schema(&self) -> &'static [ArgSchema] {
            &[]
        }
        fn eval<'a, 'b, 'c>(
            &self,
            _args: &'c [ArgumentHandle<'a, 'b>],
            _ctx: &dyn FunctionContext<'b>,
        ) -> Result<crate::traits::CalcValue<'b>, formualizer_common::ExcelError> {
            self.0.store(true, Ordering::Relaxed);
            Ok(crate::traits::CalcValue::Scalar(LiteralValue::Int(0)))
        }
    }

    let flag = Arc::new(AtomicBool::new(false));
    let wb = TestWorkbook::new().with_function(Arc::new(TripFn(flag.clone())));
    let mut engine = Engine::new(wb, runtime_cfg());
    // Stale-reader shape: A1 needs a settle pass, A2's pass-1 evaluation
    // trips the flag.
    set_formula(&mut engine, "Sheet1", 1, 1, "=IF(TRUE,A2,0)");
    set_formula(&mut engine, "Sheet1", 2, 1, "=IF(TRUE,7+TRIPCANCEL(),A1)");
    let err = engine.evaluate_all_cancellable(flag).unwrap_err();
    assert_eq!(err.kind, ExcelErrorKind::Cancelled);
    assert!(
        err.message.as_deref().unwrap_or("").contains("SCC"),
        "cancellation must come from the SCC pass boundary, got {err:?}"
    );
}

/* ───────────────────── G12: INDIRECT inside an SCC ───────────────────── */

#[test]
fn indirect_cycle_through_replan_then_target_change_breaks_it() {
    // A1 = INDIRECT(D1)+1 with D1 → "B1" and B1 = A1+1: the virtual edge
    // closes a 2-vertex SCC (tarjan_scc_with_virtual); both members are
    // live (arithmetic) → #CIRC. (A single-vertex virtual SELF-edge does not
    // form a cycle unit today — pre-existing scheduler behavior, identical
    // under Static — so the pair shape is the canonical G12 case.)
    let mut engine = runtime_engine();
    set_value(
        &mut engine,
        "Sheet1",
        1,
        4,
        LiteralValue::Text("B1".to_string()),
    );
    set_formula(&mut engine, "Sheet1", 1, 1, "=INDIRECT(D1)+1");
    set_formula(&mut engine, "Sheet1", 1, 2, "=A1+1");
    engine.evaluate_all().unwrap();
    assert!(is_circ(&engine, "Sheet1", 1, 1));
    assert!(is_circ(&engine, "Sheet1", 1, 2));

    // Re-point the dynamic ref: the outer replan loop drops the virtual
    // edge and both cells evaluate normally.
    set_value(&mut engine, "Sheet1", 3, 1, LiteralValue::Number(10.0)); // A3
    set_value(
        &mut engine,
        "Sheet1",
        1,
        4,
        LiteralValue::Text("A3".to_string()),
    );
    engine.evaluate_all().unwrap();
    assert_eq!(num(&engine, "Sheet1", 1, 1), 11.0);
    assert_eq!(num(&engine, "Sheet1", 1, 2), 12.0);
}

#[test]
fn indirect_in_untaken_branch_is_phantom() {
    // The virtual-dep builder registers INDIRECT targets statically, so the
    // self-edge exists in the schedule; at runtime the branch never executes
    // and the SCC is phantom.
    let mut engine = runtime_engine();
    set_value(
        &mut engine,
        "Sheet1",
        1,
        4,
        LiteralValue::Text("A1".to_string()),
    );
    set_formula(&mut engine, "Sheet1", 1, 1, "=IF(TRUE,1,INDIRECT(D1))");
    engine.evaluate_all().unwrap();
    assert_eq!(num(&engine, "Sheet1", 1, 1), 1.0);
}

/* ───────────────── recalc-plan dirty quirk under Runtime ─────────────── */

#[test]
fn recalc_plan_skips_clean_cycles_and_evaluates_dirty_ones_whole() {
    let mut engine = runtime_engine();
    build_99_pair(&mut engine, true);
    set_formula(&mut engine, "Sheet1", 1, 5, "=1+1"); // unrelated formula E1

    let plan = engine.build_recalc_plan().unwrap();
    engine.evaluate_recalc_plan(&plan).unwrap();
    assert_eq!(num(&engine, "Sheet1", 2, 1), 555.0);
    assert_eq!(engine.last_cycle_telemetry().static_sccs, 1);

    // Dirty only the unrelated formula: the clean SCC must be skipped
    // entirely (values stand, no task runs).
    set_formula(&mut engine, "Sheet1", 1, 5, "=2+2");
    engine.evaluate_recalc_plan(&plan).unwrap();
    assert_eq!(engine.last_cycle_telemetry().static_sccs, 0);
    assert_eq!(num(&engine, "Sheet1", 2, 1), 555.0);
    assert_eq!(num(&engine, "Sheet1", 3, 1), 555.0);

    // Dirty a cycle member (via its guard): the whole SCC evaluates.
    set_value(&mut engine, "Sheet1", 1, 1, LiteralValue::Boolean(false));
    engine.evaluate_recalc_plan(&plan).unwrap();
    assert_eq!(engine.last_cycle_telemetry().static_sccs, 1);
    assert_eq!(num(&engine, "Sheet1", 2, 1), 999.0);
    assert_eq!(num(&engine, "Sheet1", 3, 1), 999.0);
}

/* ──────────────────────── compat & determinism ───────────────────────── */

#[test]
fn static_default_keeps_stamping_the_99_pair() {
    // Golden dual-mode pin: the only documented diff (spec §8) is values vs
    // #CIRC for the guarded phantom pair.
    let mut engine = static_engine();
    build_99_pair(&mut engine, true);
    let res = engine.evaluate_all().unwrap();
    assert!(is_circ(&engine, "Sheet1", 2, 1));
    assert!(is_circ(&engine, "Sheet1", 3, 1));
    assert_eq!(res.cycle_errors, 1);
    // Telemetry stays default-zero in Static mode.
    assert_eq!(
        engine.last_cycle_telemetry(),
        &crate::engine::CycleTelemetry::default()
    );
}

#[test]
fn dual_mode_corpus_documented_diffs_only() {
    // For each scenario: build identically under both modes; live cycles
    // must be #CIRC in both, phantoms differ exactly as documented.
    type Builder = fn(&mut Engine<TestWorkbook>);
    type Scenario = (Builder, &'static [(u32, u32)], &'static [(u32, u32, f64)]);
    let scenarios: Vec<Scenario> = vec![
        // (build, circ-in-both, runtime-values)
        (
            |e| {
                set_value(e, "Sheet1", 1, 7, LiteralValue::Boolean(true));
                set_value(e, "Sheet1", 1, 2, LiteralValue::Number(1.0));
                set_formula(e, "Sheet1", 5, 1, "=IF(G1,5,SUM(B1:B10))");
                set_formula(e, "Sheet1", 5, 2, "=A5");
            },
            &[][..],
            &[(5, 1, 5.0), (5, 2, 5.0)][..],
        ),
        (
            |e| build_99_pair(e, true),
            &[][..],
            &[(2, 1, 555.0), (3, 1, 555.0)][..],
        ),
        (
            |e| {
                set_formula(e, "Sheet1", 1, 1, "=B1+1");
                set_formula(e, "Sheet1", 1, 2, "=A1+1");
            },
            &[(1, 1), (1, 2)][..],
            &[][..],
        ),
    ];

    for (build, circ_both, runtime_values) in scenarios {
        let mut st = static_engine();
        build(&mut st);
        st.evaluate_all().unwrap();
        let mut rt = runtime_engine();
        build(&mut rt);
        rt.evaluate_all().unwrap();

        for &(r, c) in circ_both {
            assert!(is_circ(&st, "Sheet1", r, c), "static r{r}c{c}");
            assert!(is_circ(&rt, "Sheet1", r, c), "runtime r{r}c{c}");
        }
        for &(r, c, v) in runtime_values {
            assert!(
                is_circ(&st, "Sheet1", r, c),
                "static stamps phantoms r{r}c{c}"
            );
            assert_eq!(num(&rt, "Sheet1", r, c), v, "runtime value r{r}c{c}");
        }
    }
}

#[test]
fn deterministic_across_thread_counts_and_repeats() {
    fn build_and_run(threads: usize) -> (Vec<Option<LiteralValue>>, crate::engine::CycleTelemetry) {
        let cfg = EvalConfig {
            max_threads: Some(threads),
            enable_parallel: threads > 1,
            ..runtime_cfg()
        };
        let mut engine = Engine::new(TestWorkbook::new(), cfg);
        // Mixed workbook: phantom pair + live pair + blast-radius ring +
        // downstream readers.
        build_99_pair(&mut engine, true);
        set_formula(&mut engine, "Sheet1", 1, 2, "=B2+1");
        set_formula(&mut engine, "Sheet1", 2, 2, "=B1+1");
        for r in 1..=10u32 {
            let f = match r {
                5 => "=IF(TRUE,C6,C6)".to_string(),
                6 => "=IF(TRUE,C5,C7)".to_string(),
                10 => "=IF(TRUE,100,C1)".to_string(),
                _ => format!("=IF(TRUE,{},C{})", r * 10, r + 1),
            };
            set_formula(&mut engine, "Sheet1", r, 3, &f);
        }
        set_formula(&mut engine, "Sheet1", 1, 4, "=A2+C1"); // downstream
        engine.evaluate_all().unwrap();

        let mut values = Vec::new();
        for r in 1..=10u32 {
            for c in 1..=4u32 {
                values.push(engine.get_cell_value("Sheet1", r, c));
            }
        }
        let mut telemetry = engine.last_cycle_telemetry().clone();
        telemetry.elapsed_ms = 0; // wall clock is the only nondeterministic field
        (values, telemetry)
    }

    let baseline = build_and_run(1);
    for threads in [1usize, 2, 8] {
        for run in 0..2 {
            let out = build_and_run(threads);
            assert_eq!(out, baseline, "threads={threads} run={run}");
        }
    }
}

/* ──────────────── journaled-effects site under Runtime ───────────────── */

#[test]
fn evaluate_all_logged_handles_runtime_cycles_directly() {
    use crate::engine::ChangeLog;

    // Phantom and live cycles through the ChangeLog-threaded path produce
    // identical results to plain evaluate_all (the journal only ever records
    // spill events, which SCC tasks never produce).
    let mut engine = runtime_engine();
    build_99_pair(&mut engine, true);
    set_formula(&mut engine, "Sheet1", 1, 2, "=B2+1");
    set_formula(&mut engine, "Sheet1", 2, 2, "=B1+1");
    let mut log = ChangeLog::new();
    let res = engine.evaluate_all_logged(&mut log).unwrap();
    assert_eq!(num(&engine, "Sheet1", 2, 1), 555.0);
    assert_eq!(num(&engine, "Sheet1", 3, 1), 555.0);
    assert!(is_circ(&engine, "Sheet1", 1, 2));
    assert!(is_circ(&engine, "Sheet1", 2, 2));
    assert_eq!(res.cycle_errors, 1);
}