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
//! Formal effects pipeline for evaluation (ticket 603).
//!
//! An `Effect` is an explicit, inspectable side-effect produced by formula
//! evaluation. Effects are *planned* deterministically from
//! `(computed_value, workbook_state)` and *applied* sequentially.
//!
//! This separation enables:
//! - Parallel computation with sequential apply
//! - ChangeLog integration at the effect layer
//! - Deterministic replay without re-evaluation
use crate::engine::vertex::VertexId;
use crate::reference::CellRef;
use formualizer_common::LiteralValue;
use std::sync::{Arc, Mutex};
/// Opaque, one-use read evidence paired with its final planned anchor value.
/// Only the dynamic-read planner creates these; ordinary and synthetic writes
/// cannot borrow pending evidence by vertex ID.
#[doc(hidden)]
#[derive(Debug)]
pub struct RecordedResult {
vertex: VertexId,
value: LiteralValue,
pass: Arc<()>,
reads: Mutex<Option<Vec<crate::engine::authority::host::ObservedRect>>>,
}
impl RecordedResult {
pub(crate) fn new(
vertex: VertexId,
value: LiteralValue,
pass: Arc<()>,
reads: Vec<crate::engine::authority::host::ObservedRect>,
) -> Self {
Self {
vertex,
value,
pass,
reads: Mutex::new(Some(reads)),
}
}
pub(crate) fn vertex(&self) -> VertexId {
self.vertex
}
pub(crate) fn value(&self) -> &LiteralValue {
&self.value
}
pub(crate) fn belongs_to_pass(&self, pass: &Arc<()>) -> bool {
Arc::ptr_eq(&self.pass, pass)
}
pub(crate) fn take_reads(&self) -> Option<Vec<crate::engine::authority::host::ObservedRect>> {
self.reads.lock().unwrap().take()
}
}
/// An explicit, inspectable side-effect produced by formula evaluation.
#[derive(Debug, Clone)]
pub enum Effect {
/// Write a scalar value (or error) to a formula/named vertex.
WriteCell {
vertex_id: VertexId,
value: LiteralValue,
},
/// Write the designated evaluated result, carrying its own read proof.
/// Clones share one-use evidence and cannot change the paired value.
#[doc(hidden)]
WriteRecordedCell { result: Arc<RecordedResult> },
/// Commit a dynamic array spill to the grid.
SpillCommit {
anchor_vertex: VertexId,
anchor_cell: CellRef,
target_cells: Vec<CellRef>,
values: Vec<Vec<LiteralValue>>,
},
/// Clear a previous spill region (before resize or scalar downgrade).
SpillClear { anchor_vertex: VertexId },
}
/// A batch of effects from evaluating a single layer.
/// Each entry pairs a vertex with its planned effects.
pub type EffectBatch = Vec<(VertexId, Vec<Effect>)>;