cellrune 0.1.18

Bounded XLSX/XLSM reading, deterministic calculation, editing, and writing for Rust
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
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
use std::collections::{BTreeSet, VecDeque};
use std::sync::Arc;

use super::eval::{CompiledWorkbook, clone_set_cancellable};
use super::formula_rewrite::{FormulaRewriteBudget, FormulaRewriteError, FormulaRewriteLimits};
use super::pipeline::{calculate_and_compile, calculate_from_compiled};
use crate::draft::{
    BatchExecutionError, TableMaterializationBudget, TableMaterializationError,
    rewrite_limit_detail,
};
use crate::{
    CalculationCellId, CalculationOptions, CalculationSnapshot, EditBatch, EditReceipt,
    ValidationError, WorkbookDraft, WorkbookSnapshot,
};

mod delta;
mod error;
mod impact;
mod limits;
mod targeted;
mod transaction;
pub use targeted::{CompletedTargetCalculation, PreparedTargetCalculation};

pub use delta::{CalculationDelta, CalculationDeltaCell, CalculationDeltaPage};
use delta::{DeltaMetadata, build_delta, build_empty_delta, build_incremental_delta};
use error::stale_calculation_cursor_detail;
pub use error::{SessionError, SessionErrorCode};
use impact::{affected_formulas, formula_cells, formula_cells_from_workbook};
pub use limits::{CancellationToken, SessionLimits};
pub use transaction::{
    CompletedWorkbookTransaction, InstallDeltaBasisReason, PreparedWorkbookTransaction,
    TransactionAffectedFormula, TransactionDetailItem, TransactionDetailSection,
    TransactionImpactCause, TransactionImpactCoverage, TransactionImpactPage,
    TransactionInstallResultChange, TransactionIssueChange, TransactionIssueChangeKind,
    TransactionPageCursor, TransactionResultChange, WorkbookTransactionReceipt,
    WorkbookTransactionReport,
};

/// Caller-selected recalculation policy.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum RecalculationMode {
    /// Select incremental calculation when its complete impact boundary is known, otherwise full.
    #[default]
    Auto,
    /// Require a safe incremental pass and return an error instead of falling back.
    Incremental,
    /// Evaluate every formula cell.
    Full,
}

/// Actual schedule used by one installed calculation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum CalculationExecutionMode {
    /// Only dirty formula cells were evaluated.
    Incremental,
    /// Every schedulable formula cell was evaluated.
    Full,
}

/// Deterministic explanation for the selected calculation schedule.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum CalculationDecisionReason {
    /// No complete calculation state existed.
    InitialCalculation,
    /// The caller explicitly requested a full pass.
    FullRequested,
    /// The caller explicitly requested an incremental pass.
    IncrementalRequested,
    /// Auto mode selected a safe dirty-only pass.
    DirtySubset,
    /// No formula was dirty, so no evaluator work was required.
    NoDirtyFormulas,
    /// Formula, name, or sheet topology changed.
    TopologyChanged,
    /// Calculation options or workbook interpretation changed.
    OptionsChanged,
    /// Dynamic reference or undeclared spill topology requires a conservative full pass.
    DynamicTopology,
    /// Dirty formulas cover the complete compiled formula set.
    DirtySetCoversWorkbook,
}

/// Stateful workbook editor and persistent calculation engine.
#[derive(Debug)]
pub struct WorkbookCalculationSession {
    draft: WorkbookDraft,
    compiled: Option<Arc<CompiledWorkbook>>,
    calculation: Option<Arc<CalculationSnapshot>>,
    calculation_options: Option<CalculationOptions>,
    dirty: BTreeSet<CalculationCellId>,
    calculation_changes_pending: bool,
    requires_full_rebuild: bool,
    limits: SessionLimits,
    next_cursor: u64,
    history: VecDeque<CalculationDelta>,
}

impl WorkbookCalculationSession {
    /// Creates a session around an existing mutable workbook draft.
    pub fn new(draft: WorkbookDraft) -> Self {
        Self::with_limits(draft, SessionLimits::default())
    }

    /// Creates a session with explicit state and response limits.
    pub fn with_limits(draft: WorkbookDraft, limits: SessionLimits) -> Self {
        Self {
            draft,
            compiled: None,
            calculation: None,
            calculation_options: None,
            dirty: BTreeSet::new(),
            calculation_changes_pending: false,
            requires_full_rebuild: true,
            limits,
            next_cursor: 1,
            history: VecDeque::new(),
        }
    }

    /// Creates a new workbook session containing `Sheet1`.
    pub fn create() -> Self {
        Self::new(WorkbookDraft::new())
    }

    /// Returns the current immutable workbook snapshot.
    pub fn workbook(&self) -> &WorkbookSnapshot {
        self.draft.workbook()
    }

    /// Returns the mutable draft used by verified writers.
    pub const fn draft(&self) -> &WorkbookDraft {
        &self.draft
    }

    /// Returns the installed complete calculation, when available.
    pub fn calculation(&self) -> Option<&CalculationSnapshot> {
        self.calculation.as_deref()
    }

    /// Returns the configured session limits.
    pub const fn limits(&self) -> SessionLimits {
        self.limits
    }

    /// Applies one atomic batch after checking the caller's expected revision.
    ///
    /// # Errors
    ///
    /// Returns a stable session error for an outdated revision, empty or excessive batch, or a
    /// workbook validation error if any operation or final invariant fails.
    pub fn apply_changes(
        &mut self,
        expected_revision: u64,
        batch: EditBatch,
    ) -> Result<EditReceipt, ApplyChangesError> {
        let prepared = self.prepare_changes(expected_revision, batch)?;
        self.install_changes(prepared)
    }

    /// Stages one atomic batch without changing the live session.
    ///
    /// The returned batch can be inspected by a communication layer before installation, for
    /// example to enforce a response budget without reporting failure after an edit committed.
    ///
    /// # Errors
    ///
    /// Returns a stable session error for an outdated revision, empty or excessive batch, or a
    /// workbook validation error if any operation or final invariant fails.
    pub fn prepare_changes(
        &self,
        expected_revision: u64,
        batch: EditBatch,
    ) -> Result<PreparedEditBatch, ApplyChangesError> {
        self.prepare_changes_with_cancellation(expected_revision, batch, &CancellationToken::new())
    }

    /// Stages one atomic edit batch with cooperative cancellation.
    ///
    /// Cancellation or resource exhaustion leaves the live session and semantic revision
    /// unchanged.
    ///
    /// # Errors
    ///
    /// Returns the same validation and revision errors as [`Self::prepare_changes`], plus
    /// [`SessionErrorCode::Cancelled`], [`SessionErrorCode::RewriteLimitExceeded`], or
    /// [`SessionErrorCode::TableMaterializationLimitExceeded`].
    pub fn prepare_changes_cancellable(
        &self,
        expected_revision: u64,
        batch: EditBatch,
        cancellation: &CancellationToken,
    ) -> Result<PreparedEditBatch, ApplyChangesError> {
        self.prepare_changes_with_cancellation(expected_revision, batch, cancellation)
    }

    fn prepare_changes_with_cancellation(
        &self,
        expected_revision: u64,
        batch: EditBatch,
        cancellation: &CancellationToken,
    ) -> Result<PreparedEditBatch, ApplyChangesError> {
        let current_revision = self.workbook().semantic_revision();
        if expected_revision != current_revision {
            return Err(SessionError::new(
                SessionErrorCode::RevisionMismatch,
                Some(format!(
                    "expected={expected_revision}, current={current_revision}"
                )),
            )
            .into());
        }
        if batch.is_empty() {
            return Err(SessionError::new(SessionErrorCode::EmptyBatch, None).into());
        }
        if batch.len() > self.limits.max_batch_operations {
            return Err(SessionError::new(
                SessionErrorCode::BatchLimitExceeded,
                Some(format!(
                    "operations={}, limit={}",
                    batch.len(),
                    self.limits.max_batch_operations
                )),
            )
            .into());
        }
        if cancellation.is_cancelled() {
            return Err(SessionError::new(SessionErrorCode::Cancelled, None).into());
        }
        let cancelled = || cancellation.is_cancelled();
        let mut draft = self
            .draft
            .clone_cancellable(&cancelled)
            .map_err(|()| SessionError::new(SessionErrorCode::Cancelled, None))?;
        let rewrite_limits = FormulaRewriteLimits {
            max_formulas: self.limits.max_rewrite_formulas,
            max_source_bytes: self.limits.max_rewrite_source_bytes,
            max_ast_nodes: self.limits.max_rewrite_ast_nodes,
            max_source_edits: self.limits.max_rewrite_source_edits,
        };
        let mut rewrite_budget = FormulaRewriteBudget::new(rewrite_limits, &cancelled);
        let mut materialization_budget =
            TableMaterializationBudget::new(self.limits.max_table_materialized_cells, &cancelled);
        let receipt = draft
            .apply_changes_controlled(batch, &mut rewrite_budget, &mut materialization_budget)
            .map_err(map_batch_execution_error)?;
        if cancellation.is_cancelled() {
            return Err(SessionError::new(SessionErrorCode::Cancelled, None).into());
        }
        let topology_or_metadata_changed =
            receipt.topology_changed() || receipt.calculation_metadata_changed();
        let next_requires_full_rebuild = self.requires_full_rebuild || topology_or_metadata_changed;
        let replacement_dirty_state =
            if !topology_or_metadata_changed && let Some(compiled) = &self.compiled {
                affected_formulas(
                    draft.workbook(),
                    compiled,
                    self.calculation.as_deref(),
                    receipt.calculation_changed_cells(),
                    &self.dirty,
                    &cancelled,
                )
                .map_err(|()| SessionError::new(SessionErrorCode::Cancelled, None))?
            } else {
                clone_set_cancellable(&self.dirty, &cancelled)
                    .map_err(|()| SessionError::new(SessionErrorCode::Cancelled, None))?
            };
        let next_calculation_changes_pending =
            self.calculation_changes_pending || !receipt.calculation_changed_cells().is_empty();
        Ok(PreparedEditBatch {
            base_revision: current_revision,
            base_cursor: self.next_cursor,
            draft,
            receipt,
            replacement_dirty_state,
            next_requires_full_rebuild,
            next_calculation_changes_pending,
        })
    }

    /// Installs a previously staged edit batch if its source revision and calculation
    /// generation are still current.
    ///
    /// # Errors
    ///
    /// Returns [`SessionErrorCode::RevisionMismatch`] without changing the session when another
    /// edit was installed after the batch was prepared, or [`SessionErrorCode::StaleResult`]
    /// when a calculation was installed after the batch was prepared.
    pub fn install_changes(
        &mut self,
        prepared: PreparedEditBatch,
    ) -> Result<EditReceipt, ApplyChangesError> {
        let current_revision = self.workbook().semantic_revision();
        if current_revision != prepared.base_revision {
            return Err(SessionError::new(
                SessionErrorCode::RevisionMismatch,
                Some(format!(
                    "expected={}, current={current_revision}",
                    prepared.base_revision
                )),
            )
            .into());
        }
        if prepared.base_cursor != self.next_cursor {
            return Err(SessionError::new(
                SessionErrorCode::StaleResult,
                Some(stale_calculation_cursor_detail(
                    prepared.base_cursor,
                    self.next_cursor,
                )),
            )
            .into());
        }
        self.requires_full_rebuild = prepared.next_requires_full_rebuild;
        self.dirty = prepared.replacement_dirty_state;
        self.calculation_changes_pending = prepared.next_calculation_changes_pending;
        self.draft = prepared.draft;
        Ok(prepared.receipt)
    }

    /// Captures an immutable calculation job that can run without holding the session lock.
    ///
    /// # Errors
    ///
    /// Returns a stable error when forced incremental calculation lacks a complete safe state.
    pub fn prepare_recalculation(
        &self,
        mode: RecalculationMode,
        options: CalculationOptions,
        cancellation: CancellationToken,
    ) -> Result<PreparedCalculation, SessionError> {
        let cancelled = || cancellation.is_cancelled();
        if cancelled() {
            return Err(SessionError::new(SessionErrorCode::Cancelled, None));
        }
        let current_revision = self.workbook().semantic_revision();
        let previous_revision = self
            .calculation
            .as_ref()
            .map_or(current_revision, |calculation| {
                calculation.source_revision()
            });
        if let Some(previous) = &self.calculation
            && previous.source_revision() > current_revision
        {
            return Err(SessionError::new(
                SessionErrorCode::StateRevisionMismatch,
                Some(format!(
                    "calculation={}, workbook={current_revision}",
                    previous.source_revision()
                )),
            ));
        }

        let options_changed = self
            .calculation_options
            .is_some_and(|previous| previous != options);
        let no_state = self.calculation.is_none() || self.compiled.is_none();
        let compiled_limit_changed = self
            .compiled
            .as_ref()
            .is_some_and(|compiled| compiled.limits() != options.limits());
        let table_topology_changed = match &self.compiled {
            Some(compiled) => !compiled
                .table_topology_matches(self.workbook(), &cancelled)
                .map_err(|()| SessionError::new(SessionErrorCode::Cancelled, None))?,
            None => false,
        };
        let unsafe_dynamic = self
            .compiled
            .as_ref()
            .is_some_and(|compiled| !compiled.incremental_safe())
            && self.calculation_changes_pending;
        let compile_required = no_state
            || self.requires_full_rebuild
            || compiled_limit_changed
            || table_topology_changed
            || unsafe_dynamic;

        let (execution_mode, reason) = match mode {
            RecalculationMode::Full => (
                CalculationExecutionMode::Full,
                CalculationDecisionReason::FullRequested,
            ),
            RecalculationMode::Incremental if no_state => {
                return Err(SessionError::new(
                    SessionErrorCode::CalculationUninitialized,
                    None,
                ));
            }
            RecalculationMode::Incremental
                if compile_required || options_changed || unsafe_dynamic =>
            {
                return Err(SessionError::new(
                    SessionErrorCode::IncrementalUnsafe,
                    Some(incremental_unsafe_detail(
                        self.requires_full_rebuild || table_topology_changed,
                        options_changed || compiled_limit_changed,
                        unsafe_dynamic,
                    )),
                ));
            }
            RecalculationMode::Incremental => (
                CalculationExecutionMode::Incremental,
                if self.dirty.is_empty() {
                    CalculationDecisionReason::NoDirtyFormulas
                } else {
                    CalculationDecisionReason::IncrementalRequested
                },
            ),
            RecalculationMode::Auto if no_state => (
                CalculationExecutionMode::Full,
                CalculationDecisionReason::InitialCalculation,
            ),
            RecalculationMode::Auto if self.requires_full_rebuild || table_topology_changed => (
                CalculationExecutionMode::Full,
                CalculationDecisionReason::TopologyChanged,
            ),
            RecalculationMode::Auto if options_changed || compiled_limit_changed => (
                CalculationExecutionMode::Full,
                CalculationDecisionReason::OptionsChanged,
            ),
            RecalculationMode::Auto if unsafe_dynamic => (
                CalculationExecutionMode::Full,
                CalculationDecisionReason::DynamicTopology,
            ),
            RecalculationMode::Auto
                if self.compiled.as_ref().is_some_and(|compiled| {
                    !self.dirty.is_empty() && self.dirty.len() >= compiled.formula_count()
                }) =>
            {
                (
                    CalculationExecutionMode::Full,
                    CalculationDecisionReason::DirtySetCoversWorkbook,
                )
            }
            RecalculationMode::Auto if self.dirty.is_empty() => (
                CalculationExecutionMode::Incremental,
                CalculationDecisionReason::NoDirtyFormulas,
            ),
            RecalculationMode::Auto => (
                CalculationExecutionMode::Incremental,
                CalculationDecisionReason::DirtySubset,
            ),
        };

        let dirty = if execution_mode == CalculationExecutionMode::Full {
            match self.compiled.as_deref() {
                Some(compiled) if !compile_required => {
                    formula_cells(self.workbook(), compiled, &cancelled)
                }
                _ => formula_cells_from_workbook(self.workbook(), &cancelled),
            }
        } else {
            clone_set_cancellable(&self.dirty, &cancelled)
        }
        .map_err(|()| SessionError::new(SessionErrorCode::Cancelled, None))?;
        let workbook = self.draft.shared_workbook();
        let compiled = self.compiled.as_ref().map(Arc::clone);
        let previous = self.calculation.as_ref().map(Arc::clone);
        if cancelled() {
            return Err(SessionError::new(SessionErrorCode::Cancelled, None));
        }
        Ok(PreparedCalculation {
            workbook,
            expected_revision: current_revision,
            base_cursor: self.next_cursor,
            base_revision: previous_revision,
            options,
            execution_mode,
            reason,
            compile_required,
            compiled,
            previous,
            dirty,
            cancellation,
            limits: self.limits,
        })
    }

    /// Installs a completed calculation only if its workbook and calculation state are current.
    ///
    /// # Errors
    ///
    /// Returns [`SessionErrorCode::StaleResult`] or [`SessionErrorCode::Cancelled`] without
    /// changing the installed state.
    pub fn install(
        &mut self,
        completed: CompletedCalculation,
    ) -> Result<CalculationDelta, SessionError> {
        let delta = self.preview_install(&completed)?;
        let history_delta = delta
            .clone_cancellable(&|| completed.cancellation.is_cancelled())
            .map_err(|()| SessionError::new(SessionErrorCode::Cancelled, None))?;
        if completed.cancellation.is_cancelled() {
            return Err(SessionError::new(SessionErrorCode::Cancelled, None));
        }
        self.next_cursor = delta.cursor().checked_add(1).ok_or_else(|| {
            SessionError::new(
                SessionErrorCode::DeltaLimitExceeded,
                Some("delta cursor exhausted".to_owned()),
            )
        })?;
        self.compiled = Some(completed.compiled);
        self.calculation = Some(completed.calculation);
        self.calculation_options = Some(completed.options);
        self.dirty.clear();
        self.calculation_changes_pending = false;
        self.requires_full_rebuild = false;
        self.history.push_back(history_delta);
        while self.history.len() > self.limits.max_retained_deltas {
            self.history.pop_front();
        }
        Ok(delta)
    }

    /// Returns the exact delta that installing a completed calculation would commit.
    ///
    /// This validates cancellation, revision, and cursor state without changing the session.
    ///
    /// # Errors
    ///
    /// Returns [`SessionErrorCode::Cancelled`], [`SessionErrorCode::StaleResult`], or
    /// [`SessionErrorCode::DeltaLimitExceeded`] without changing installed state.
    pub fn preview_install(
        &self,
        completed: &CompletedCalculation,
    ) -> Result<CalculationDelta, SessionError> {
        if completed.cancellation.is_cancelled() {
            return Err(SessionError::new(SessionErrorCode::Cancelled, None));
        }
        let current_revision = self.workbook().semantic_revision();
        if completed.expected_revision != current_revision {
            return Err(SessionError::new(
                SessionErrorCode::StaleResult,
                Some(format!(
                    "calculated={}, current={current_revision}",
                    completed.expected_revision
                )),
            ));
        }
        if completed.base_cursor != self.next_cursor {
            return Err(SessionError::new(
                SessionErrorCode::StaleResult,
                Some(stale_calculation_cursor_detail(
                    completed.base_cursor,
                    self.next_cursor,
                )),
            ));
        }
        self.next_cursor.checked_add(1).ok_or_else(|| {
            SessionError::new(
                SessionErrorCode::DeltaLimitExceeded,
                Some("delta cursor exhausted".to_owned()),
            )
        })?;
        let mut delta = completed
            .delta
            .clone_cancellable(&|| completed.cancellation.is_cancelled())
            .map_err(|()| SessionError::new(SessionErrorCode::Cancelled, None))?;
        if completed.cancellation.is_cancelled() {
            return Err(SessionError::new(SessionErrorCode::Cancelled, None));
        }
        delta.cursor = self.next_cursor;
        Ok(delta)
    }

    /// Prepares, runs, and installs one calculation synchronously.
    ///
    /// # Errors
    ///
    /// Returns a stable session error for unsafe incremental mode, cancellation, resource limits,
    /// or a stale installation.
    pub fn recalculate(
        &mut self,
        mode: RecalculationMode,
        options: CalculationOptions,
        cancellation: CancellationToken,
    ) -> Result<CalculationDelta, SessionError> {
        let prepared = self.prepare_recalculation(mode, options, cancellation)?;
        let completed = prepared.run()?;
        self.install(completed)
    }

    /// Returns complete installed deltas after an exclusive cursor.
    ///
    /// A zero cursor starts at the oldest retained delta. A zero limit selects the configured page
    /// maximum.
    ///
    /// # Errors
    ///
    /// Returns a stable error for an expired cursor or excessive page size.
    pub fn changes_since(
        &self,
        cursor: u64,
        limit: usize,
    ) -> Result<CalculationDeltaPage, SessionError> {
        let limit = if limit == 0 {
            self.limits.max_delta_page
        } else {
            limit
        };
        if limit > self.limits.max_delta_page {
            return Err(SessionError::new(
                SessionErrorCode::PageLimitExceeded,
                Some(format!(
                    "requested={limit}, limit={}",
                    self.limits.max_delta_page
                )),
            ));
        }
        if let Some(oldest) = self.history.front()
            && cursor != 0
            && cursor < oldest.cursor().saturating_sub(1)
        {
            return Err(SessionError::new(
                SessionErrorCode::CursorExpired,
                Some(format!("cursor={cursor}, oldest={}", oldest.cursor())),
            ));
        }
        let available = self
            .history
            .iter()
            .filter(|delta| delta.cursor() > cursor)
            .collect::<Vec<_>>();
        let deltas = available
            .iter()
            .take(limit)
            .map(|delta| (*delta).clone())
            .collect::<Vec<_>>();
        let next_cursor = (available.len() > deltas.len())
            .then(|| deltas.last().map_or(cursor, CalculationDelta::cursor));
        Ok(CalculationDeltaPage::new(cursor, next_cursor, deltas))
    }
}

impl Default for WorkbookCalculationSession {
    fn default() -> Self {
        Self::create()
    }
}

/// An atomic workbook edit batch staged for guarded installation.
#[derive(Debug)]
pub struct PreparedEditBatch {
    base_revision: u64,
    base_cursor: u64,
    draft: WorkbookDraft,
    receipt: EditReceipt,
    replacement_dirty_state: BTreeSet<CalculationCellId>,
    next_requires_full_rebuild: bool,
    next_calculation_changes_pending: bool,
}

impl PreparedEditBatch {
    /// Returns the workbook state that would be installed.
    pub fn workbook(&self) -> &WorkbookSnapshot {
        self.draft.workbook()
    }

    /// Returns the exact receipt that installation would commit.
    pub const fn receipt(&self) -> &EditReceipt {
        &self.receipt
    }
}

/// An immutable calculation job safe to execute outside a session lock.
#[derive(Debug)]
pub struct PreparedCalculation {
    workbook: Arc<WorkbookSnapshot>,
    expected_revision: u64,
    base_cursor: u64,
    base_revision: u64,
    options: CalculationOptions,
    execution_mode: CalculationExecutionMode,
    reason: CalculationDecisionReason,
    compile_required: bool,
    compiled: Option<Arc<CompiledWorkbook>>,
    previous: Option<Arc<CalculationSnapshot>>,
    dirty: BTreeSet<CalculationCellId>,
    cancellation: CancellationToken,
    limits: SessionLimits,
}

impl PreparedCalculation {
    /// Returns the workbook revision captured by this job.
    pub const fn expected_revision(&self) -> u64 {
        self.expected_revision
    }

    /// Returns the selected execution mode.
    pub const fn execution_mode(&self) -> CalculationExecutionMode {
        self.execution_mode
    }

    pub(super) const fn decision_reason(&self) -> CalculationDecisionReason {
        self.reason
    }

    pub(super) fn dirty_cells(&self) -> &BTreeSet<CalculationCellId> {
        &self.dirty
    }

    pub(super) fn execute(&self) -> Result<CalculationRunResult, SessionError> {
        if self.cancellation.is_cancelled() {
            return Err(SessionError::new(SessionErrorCode::Cancelled, None));
        }
        let planned_evaluations = self.dirty.len();
        if planned_evaluations > self.limits.max_evaluated_cells {
            return Err(SessionError::new(
                SessionErrorCode::EvaluationLimitExceeded,
                Some(format!(
                    "planned={planned_evaluations}, limit={}",
                    self.limits.max_evaluated_cells
                )),
            ));
        }
        let dirty =
            (self.execution_mode == CalculationExecutionMode::Incremental).then_some(&self.dirty);
        if !self.compile_required && self.reason == CalculationDecisionReason::NoDirtyFormulas {
            let previous = self.previous.as_ref().ok_or_else(|| {
                SessionError::new(SessionErrorCode::CalculationUninitialized, None)
            })?;
            let compiled = self.compiled.as_ref().map(Arc::clone).ok_or_else(|| {
                SessionError::new(SessionErrorCode::CalculationUninitialized, None)
            })?;
            let calculation = Arc::new(
                previous
                    .rebase_source_cancellable(&self.workbook, self.options, &|| {
                        self.cancellation.is_cancelled()
                    })
                    .map_err(|()| SessionError::new(SessionErrorCode::Cancelled, None))?,
            );
            return Ok(CalculationRunResult {
                compiled,
                calculation,
                evaluated_count: 0,
                evaluated_cells: BTreeSet::new(),
                parsed_formula_count: 0,
                function_iterations: 0,
                reference_cells: 0,
            });
        }
        let (calculation, compiled, work, parsed_formula_count) = if self.compile_required {
            let (calculation, compiled, work) =
                calculate_and_compile(&self.workbook, self.options, || {
                    self.cancellation.is_cancelled()
                })
                .map_err(|()| SessionError::new(SessionErrorCode::Cancelled, None))?;
            let parsed = compiled.formula_count();
            (Arc::new(calculation), Arc::new(compiled), work, parsed)
        } else {
            let compiled = self.compiled.as_ref().ok_or_else(|| {
                SessionError::new(SessionErrorCode::CalculationUninitialized, None)
            })?;
            let previous = (self.execution_mode == CalculationExecutionMode::Incremental)
                .then_some(self.previous.as_deref())
                .flatten();
            let (calculation, work) = calculate_from_compiled(
                &self.workbook,
                self.options,
                compiled,
                previous,
                dirty,
                || self.cancellation.is_cancelled(),
            )
            .map_err(|()| SessionError::new(SessionErrorCode::Cancelled, None))?;
            let compiled = self.compiled.as_ref().map(Arc::clone).ok_or_else(|| {
                SessionError::new(SessionErrorCode::CalculationUninitialized, None)
            })?;
            (Arc::new(calculation), compiled, work, 0)
        };
        if self.cancellation.is_cancelled() {
            return Err(SessionError::new(SessionErrorCode::Cancelled, None));
        }
        if work.evaluated_cells.len() > self.limits.max_evaluated_cells {
            return Err(SessionError::new(
                SessionErrorCode::EvaluationLimitExceeded,
                Some(format!(
                    "evaluated={}, limit={}",
                    work.evaluated_cells.len(),
                    self.limits.max_evaluated_cells
                )),
            ));
        }
        Ok(CalculationRunResult {
            compiled,
            calculation,
            evaluated_count: work.evaluated_cells.len(),
            evaluated_cells: work.evaluated_cells,
            parsed_formula_count,
            function_iterations: work.function_iterations,
            reference_cells: work.reference_cells,
        })
    }

    /// Executes the job without mutating the source session.
    ///
    /// # Errors
    ///
    /// Returns a stable cancellation or session resource-limit error.
    pub fn run(self) -> Result<CompletedCalculation, SessionError> {
        let execution = self.execute()?;
        let delta = if self.execution_mode == CalculationExecutionMode::Incremental {
            if self.reason == CalculationDecisionReason::NoDirtyFormulas {
                build_empty_delta(DeltaMetadata {
                    base_revision: self.base_revision,
                    result_revision: self.expected_revision,
                    mode: self.execution_mode,
                    reason: self.reason,
                    dirty_count: 0,
                    evaluated_count: 0,
                    parsed_formula_count: 0,
                })
            } else {
                let previous = self.previous.as_deref().ok_or_else(|| {
                    SessionError::new(SessionErrorCode::CalculationUninitialized, None)
                })?;
                build_incremental_delta(
                    previous,
                    &execution.calculation,
                    &self.dirty,
                    DeltaMetadata {
                        base_revision: self.base_revision,
                        result_revision: self.expected_revision,
                        mode: self.execution_mode,
                        reason: self.reason,
                        dirty_count: self.dirty.len(),
                        evaluated_count: execution.evaluated_count,
                        parsed_formula_count: 0,
                    },
                    self.limits.max_delta_cells,
                    &|| self.cancellation.is_cancelled(),
                )?
            }
        } else {
            build_delta(
                self.previous.as_deref(),
                &execution.calculation,
                DeltaMetadata {
                    base_revision: self.base_revision,
                    result_revision: self.expected_revision,
                    mode: self.execution_mode,
                    reason: self.reason,
                    dirty_count: self.dirty.len(),
                    evaluated_count: execution.evaluated_count,
                    parsed_formula_count: execution.parsed_formula_count,
                },
                self.limits.max_delta_cells,
                &|| self.cancellation.is_cancelled(),
            )?
        };
        if self.cancellation.is_cancelled() {
            return Err(SessionError::new(SessionErrorCode::Cancelled, None));
        }
        Ok(CompletedCalculation {
            expected_revision: self.expected_revision,
            base_cursor: self.base_cursor,
            options: self.options,
            compiled: execution.compiled,
            calculation: execution.calculation,
            delta,
            cancellation: self.cancellation,
        })
    }
}

#[derive(Debug)]
pub(super) struct CalculationRunResult {
    pub(super) compiled: Arc<CompiledWorkbook>,
    pub(super) calculation: Arc<CalculationSnapshot>,
    pub(super) evaluated_count: usize,
    pub(super) evaluated_cells: BTreeSet<CalculationCellId>,
    pub(super) parsed_formula_count: usize,
    pub(super) function_iterations: u64,
    pub(super) reference_cells: u64,
}

/// A calculated but not yet installed session result.
#[derive(Debug)]
pub struct CompletedCalculation {
    expected_revision: u64,
    base_cursor: u64,
    options: CalculationOptions,
    compiled: Arc<CompiledWorkbook>,
    calculation: Arc<CalculationSnapshot>,
    delta: CalculationDelta,
    cancellation: CancellationToken,
}

/// Error boundary for atomic edit validation and state conflicts.
#[derive(Debug)]
pub enum ApplyChangesError {
    /// The session state rejected the request before workbook mutation.
    Session(SessionError),
    /// A workbook invariant rejected the staged batch.
    Validation(ValidationError),
}

impl From<SessionError> for ApplyChangesError {
    fn from(error: SessionError) -> Self {
        Self::Session(error)
    }
}

impl From<ValidationError> for ApplyChangesError {
    fn from(error: ValidationError) -> Self {
        Self::Validation(error)
    }
}

fn map_batch_execution_error(error: BatchExecutionError) -> ApplyChangesError {
    match error {
        BatchExecutionError::Rewrite(FormulaRewriteError::Cancelled) => {
            SessionError::new(SessionErrorCode::Cancelled, None).into()
        }
        BatchExecutionError::Rewrite(FormulaRewriteError::LimitExceeded {
            kind,
            limit,
            actual,
        }) => SessionError::new(
            SessionErrorCode::RewriteLimitExceeded,
            Some(rewrite_limit_detail(kind, limit, actual)),
        )
        .into(),
        BatchExecutionError::Materialization(TableMaterializationError::Cancelled) => {
            SessionError::new(SessionErrorCode::Cancelled, None).into()
        }
        BatchExecutionError::Materialization(TableMaterializationError::LimitExceeded {
            limit,
            actual,
        }) => SessionError::new(
            SessionErrorCode::TableMaterializationLimitExceeded,
            Some(format!("cells={actual}, limit={limit}")),
        )
        .into(),
        other => other.into_validation().into(),
    }
}

impl std::fmt::Display for ApplyChangesError {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Session(error) => error.fmt(formatter),
            Self::Validation(error) => error.fmt(formatter),
        }
    }
}

impl std::error::Error for ApplyChangesError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Session(error) => Some(error),
            Self::Validation(error) => Some(error),
        }
    }
}

fn incremental_unsafe_detail(
    topology_changed: bool,
    options_changed: bool,
    dynamic_topology: bool,
) -> String {
    if topology_changed {
        "formula, name, sheet, or calculation topology changed".to_owned()
    } else if options_changed {
        "calculation options changed".to_owned()
    } else if dynamic_topology {
        "dynamic dependency or spill topology requires full recalculation".to_owned()
    } else {
        "incremental state is unavailable".to_owned()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{CellAddress, CellValue, FiniteNumber, FormulaText, SheetId, WorkbookChange};

    fn address(value: &str) -> CellAddress {
        CellAddress::from_a1(value).expect("valid test address")
    }

    fn number(value: f64) -> CellValue {
        CellValue::Number(FiniteNumber::new(value).expect("finite test number"))
    }

    #[test]
    fn stable_incremental_work_reuses_compiled_indexes_and_snapshot_chunks() {
        let sheet = SheetId::new(1).expect("valid default sheet ID");
        let mut session = WorkbookCalculationSession::create();
        session
            .apply_changes(
                0,
                EditBatch::new([
                    WorkbookChange::set_cell_value(sheet, address("A1"), number(1.0)),
                    WorkbookChange::set_cell_formula(
                        sheet,
                        address("B1"),
                        FormulaText::from_xlsx("A1+1").expect("valid test formula"),
                    ),
                    WorkbookChange::set_cell_value(sheet, address("A2"), number(1.0)),
                    WorkbookChange::set_cell_formula(
                        sheet,
                        address("B2"),
                        FormulaText::from_xlsx("A2+1").expect("valid test formula"),
                    ),
                ]),
            )
            .expect("initial test workbook");
        session
            .recalculate(
                RecalculationMode::Auto,
                CalculationOptions::default(),
                CancellationToken::new(),
            )
            .expect("initial calculation");

        let no_dirty = session
            .recalculate(
                RecalculationMode::Auto,
                CalculationOptions::default(),
                CancellationToken::new(),
            )
            .expect("no-dirty calculation");
        assert_eq!(no_dirty.evaluated_count(), 0);

        session
            .apply_changes(
                session.workbook().semantic_revision(),
                EditBatch::new([WorkbookChange::set_cell_value(
                    sheet,
                    address("A1"),
                    number(2.0),
                )]),
            )
            .expect("one-cell test edit");
        let one_dirty = session
            .recalculate(
                RecalculationMode::Auto,
                CalculationOptions::default(),
                CancellationToken::new(),
            )
            .expect("one-dirty calculation");
        assert_eq!(one_dirty.evaluated_count(), 1);
    }

    #[test]
    fn range_impact_index_visits_only_the_matching_sheet_local_intervals() {
        let sheet = SheetId::new(1).expect("valid default sheet ID");
        let mut changes = Vec::with_capacity(10_400);
        for column in 1..=10 {
            for row in 1..=1_000 {
                changes.push(WorkbookChange::set_cell_value(
                    sheet,
                    CellAddress::from_indices(row, column).expect("valid range input"),
                    number(1.0),
                ));
            }
            let column_name =
                char::from_u32(u32::from(b'A') + column - 1).expect("generated A:J column");
            for row in 1_002..=1_041 {
                changes.push(WorkbookChange::set_cell_formula(
                    sheet,
                    CellAddress::from_indices(row, column).expect("valid range formula"),
                    FormulaText::from_xlsx(format!("SUM({column_name}1:{column_name}1000)"))
                        .expect("valid generated range formula"),
                ));
            }
        }
        let mut session = WorkbookCalculationSession::create();
        session
            .apply_changes(0, EditBatch::new(changes))
            .expect("range-index test workbook");
        session
            .recalculate(
                RecalculationMode::Auto,
                CalculationOptions::default(),
                CancellationToken::new(),
            )
            .expect("initial range calculation");

        session
            .apply_changes(
                session.workbook().semantic_revision(),
                EditBatch::new([WorkbookChange::set_cell_value(
                    sheet,
                    address("A500"),
                    number(2.0),
                )]),
            )
            .expect("one range input edit");
        let delta = session
            .recalculate(
                RecalculationMode::Auto,
                CalculationOptions::default(),
                CancellationToken::new(),
            )
            .expect("range-index incremental calculation");
        assert_eq!(delta.evaluated_count(), 40);
    }

    #[test]
    fn cancelled_impact_preparation_leaves_session_unchanged() {
        let sheet = SheetId::new(1).expect("valid default sheet ID");
        let mut session = WorkbookCalculationSession::create();
        session
            .apply_changes(
                0,
                EditBatch::new([
                    WorkbookChange::set_cell_value(sheet, address("A1"), number(1.0)),
                    WorkbookChange::set_cell_formula(
                        sheet,
                        address("B1"),
                        FormulaText::from_xlsx("A1+1").expect("valid test formula"),
                    ),
                ]),
            )
            .expect("initial workbook");
        session
            .recalculate(
                RecalculationMode::Auto,
                CalculationOptions::default(),
                CancellationToken::new(),
            )
            .expect("initial calculation");

        let before_dirty = session.dirty.clone();
        let before_requires_full_rebuild = session.requires_full_rebuild;
        let before_pending = session.calculation_changes_pending;
        let before_revision = session.workbook().semantic_revision();
        let before_calculation = session.calculation.clone();

        let cancellation = CancellationToken::new();
        cancellation.cancel();

        let error = session
            .prepare_changes_cancellable(
                before_revision,
                EditBatch::new([WorkbookChange::set_cell_value(
                    sheet,
                    address("A1"),
                    number(2.0),
                )]),
                &cancellation,
            )
            .expect_err("pre-cancelled preparation must fail");

        let code = match error {
            ApplyChangesError::Session(error) => error.code(),
            ApplyChangesError::Validation(_) => panic!("expected a session cancellation error"),
        };
        assert_eq!(code, SessionErrorCode::Cancelled);

        assert_eq!(session.dirty, before_dirty);
        assert_eq!(session.requires_full_rebuild, before_requires_full_rebuild);
        assert_eq!(session.calculation_changes_pending, before_pending);
        assert_eq!(session.workbook().semantic_revision(), before_revision);
        match (&before_calculation, &session.calculation) {
            (Some(before), Some(after)) => assert!(Arc::ptr_eq(before, after)),
            (None, None) => {}
            _ => panic!("calculation state must be unchanged"),
        }
    }

    #[test]
    fn impact_preparation_observes_mid_flight_cancellation() {
        use std::cell::Cell;
        use std::rc::Rc;

        let sheet = SheetId::new(1).expect("valid default sheet ID");
        const CHAIN_LEN: u32 = 1_025;
        let mut changes = Vec::with_capacity(CHAIN_LEN as usize);
        changes.push(WorkbookChange::set_cell_value(
            sheet,
            CellAddress::from_indices(1, 1).expect("chain input cell"),
            number(1.0),
        ));
        for row in 2..=CHAIN_LEN {
            changes.push(WorkbookChange::set_cell_formula(
                sheet,
                CellAddress::from_indices(row, 1).expect("chain formula cell"),
                FormulaText::from_xlsx(format!("A{}+1", row - 1))
                    .expect("valid generated chain formula"),
            ));
        }
        let limits = SessionLimits::new(2_000, 2_000, 2_000, 256, 100).expect("test fanout limits");
        let mut session = WorkbookCalculationSession::with_limits(WorkbookDraft::new(), limits);
        session
            .apply_changes(0, EditBatch::new(changes))
            .expect("chain workbook");
        session
            .recalculate(
                RecalculationMode::Auto,
                CalculationOptions::default(),
                CancellationToken::new(),
            )
            .expect("initial chain calculation");

        let compiled = session.compiled.clone().expect("compiled workbook");
        let workbook = session.workbook();
        let previous = session.calculation.as_deref();
        let changed = [CalculationCellId::new(sheet, address("A1"))];

        let polls = Rc::new(Cell::new(0_u32));
        let cancelled = {
            let polls = Rc::clone(&polls);
            move || {
                let count = polls.get() + 1;
                polls.set(count);
                count >= 2
            }
        };

        let result = affected_formulas(
            workbook,
            compiled.as_ref(),
            previous,
            &changed,
            &BTreeSet::new(),
            &cancelled,
        );

        assert!(
            result.is_err(),
            "cancellation on the second poll must abort impact preparation"
        );
        assert_eq!(polls.get(), 2);
    }

    #[test]
    fn direct_fanout_observes_mid_flight_cancellation() {
        use std::cell::Cell;
        use std::rc::Rc;

        let sheet = SheetId::new(1).expect("valid default sheet ID");
        const FORMULAS: u32 = 1_024;
        let mut changes = Vec::with_capacity(FORMULAS as usize + 1);
        changes.push(WorkbookChange::set_cell_value(
            sheet,
            address("A1"),
            number(1.0),
        ));
        for row in 2..=FORMULAS + 1 {
            changes.push(WorkbookChange::set_cell_formula(
                sheet,
                CellAddress::from_indices(row, 2).expect("fanout formula cell"),
                FormulaText::from_xlsx("$A$1+1").expect("valid fanout formula"),
            ));
        }
        let limits = SessionLimits::new(2_000, 2_000, 2_000, 256, 100).expect("test fanout limits");
        let mut session = WorkbookCalculationSession::with_limits(WorkbookDraft::new(), limits);
        session
            .apply_changes(0, EditBatch::new(changes))
            .expect("direct fanout workbook");
        session
            .recalculate(
                RecalculationMode::Full,
                CalculationOptions::default(),
                CancellationToken::new(),
            )
            .expect("initial direct fanout calculation");

        let compiled = session.compiled.clone().expect("compiled workbook");
        let changed = [CalculationCellId::new(sheet, address("A1"))];
        let polls = Rc::new(Cell::new(0_u32));
        let cancelled = {
            let polls = Rc::clone(&polls);
            move || {
                let next = polls.get() + 1;
                polls.set(next);
                next >= 3
            }
        };
        let result = affected_formulas(
            session.workbook(),
            compiled.as_ref(),
            session.calculation.as_deref(),
            &changed,
            &BTreeSet::new(),
            &cancelled,
        );
        assert!(result.is_err());
        assert_eq!(polls.get(), 3);
    }

    #[test]
    fn staged_edit_rejects_an_intervening_calculation_generation() {
        let sheet = SheetId::new(1).expect("valid default sheet ID");
        let mut session = WorkbookCalculationSession::create();
        session
            .apply_changes(
                0,
                EditBatch::new([
                    WorkbookChange::set_cell_value(sheet, address("A1"), number(1.0)),
                    WorkbookChange::set_cell_formula(
                        sheet,
                        address("B1"),
                        FormulaText::from_xlsx("A1+1").expect("valid test formula"),
                    ),
                ]),
            )
            .expect("initial workbook");
        session
            .recalculate(
                RecalculationMode::Auto,
                CalculationOptions::default(),
                CancellationToken::new(),
            )
            .expect("initial calculation");

        let revision = session.workbook().semantic_revision();
        let prepared = session
            .prepare_changes(
                revision,
                EditBatch::new([WorkbookChange::set_cell_value(
                    sheet,
                    address("A1"),
                    number(2.0),
                )]),
            )
            .expect("staged edit");

        session
            .recalculate(
                RecalculationMode::Auto,
                CalculationOptions::default(),
                CancellationToken::new(),
            )
            .expect("intervening calculation");

        let error = session
            .install_changes(prepared)
            .expect_err("install must reject a stale calculation generation");
        let ApplyChangesError::Session(error) = error else {
            panic!("expected a session error");
        };
        assert_eq!(error.code(), SessionErrorCode::StaleResult);
    }
}