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
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2025 ReifyDB
//! Tumbling-aggregator authoring surface.
//!
//! Most stateful operators share an
//! identical event-handling shape. They have repeatedly been written from
//! scratch on top of [`crate::operator::FFIOperator`], and have repeatedly
//! shipped the same families of bugs:
//!
//! 1. **Per-slot replacement on Update.** `Update` is treated like another `Insert` and the slot's contribution is
//! added twice.
//! 2. **`DiffType::Remove` silently dropped.** No match arm exists for Remove, so the state retains rows the source
//! already deleted.
//! 3. **Per-slot extrema** (open/close/high/low) computed as a running min/max instead of as min/max over the *current*
//! per-slot map, so a same-slot Update with a lower price never lowers the high.
//! 4. **Window boundary off-by-one.** Inconsistent `[start, end)` vs `[start, end]` semantics across operators.
//!
//! This module exists to make those bugs *unrepresentable in user code*.
//! An operator implements [`TumblingOperator`] -- a small set of pure
//! functions over a typed slot input and contribution. The shared driver
//! (slice 2 of the rollout) handles diff routing, per-slot replacement,
//! Remove, and window boundary math, exhaustively and in one place.
//!
//! For overlapping-window operators (rolling buffers of the last N
//! tumbling outputs) see the sibling [`RollingOperator`] trait; both
//! traits live under the `windowed` namespace because they share the
//! `Slot` / `WindowSpan` machinery in [`span`].
//!
//! # Contract
//!
//! - The slot map for a window is keyed by [`TumblingOperator::SlotKey`]. Insert and Update of an input row whose
//! extracted slot key is `k` *replace* the contribution at `k`, never add to it. This is the per-slot replacement
//! contract documented on `chaindex::testing::oracle::naive_ohlcv` and matches the way the existing chaindex chaos
//! oracles model events.
//! - [`TumblingOperator::fold_into_slot`] is a pure function of `(prev, input)`. It is called with `prev = None` on
//! first observation of a slot and `prev = Some(existing)` if a row at the same slot was seen earlier in the same
//! input batch (e.g. multiple rows in a single `BorrowedChange` map to the same slot). Implementations that simply
//! want last-write-wins should ignore `prev`.
//! - [`TumblingOperator::combine`] reduces the entire per-slot map of a window into the operator's public output. **It
//! must be a pure function of the map's contents, independent of iteration order.** This is the invariant that
//! prevents bug class 3 above: extrema must be computed by folding `slots.values()`, never by maintaining a running
//! max/min that mutates as rows arrive.
//! - [`TumblingOperator::window_for`] returns a [`WindowSpan`] with `[start, end)` semantics over the operator's chosen
//! slot coordinate (`u64` timestamps, Solana slot numbers, `DateTime` newtypes, etc.). See [`span`] for the canonical
//! helper and [`span::Slot`] for the coordinate trait.
use ;
use ;
use Value;
use ;
use crate::;
/// A typed view extracted from one input row, sufficient to drive the
/// aggregation. Implementations are typically small `Copy` structs
/// (e.g. `{ price: f64, size: f64 }` for VWAP).
/// The per-slot reduced value persisted inside a window's slot map.
/// Must be cheap to clone and serde-roundtrippable so the driver can
/// persist the full per-window map through [`crate::state::cache::StateCache`].
/// The output emitted for a window once its slot map has any contributions.
/// Implementations are typically the typed row struct generated by chaindex's
/// `row!()` macro; the contract suite here only requires `PartialEq + Debug`
/// so order-independence and roundtrip equality can be checked.
/// The core authoring trait. Implementors describe their aggregation as four
/// pure functions; the driver handles all FFI plumbing, diff routing, and
/// state persistence.
/// The persisted state for one window: a sorted map from slot key to its
/// reduced contribution. Always serde-roundtripped through `StateCache`.
pub type WindowSlots<A> = ;
/// FFI registration surface for a [`TumblingOperator`].
///
/// `TumblingOperator` is the *pure* aggregation surface (the four functions
/// the contract harness checks). `FFITumblingOperator` adds the bits the FFI
/// driver needs to register the operator with the runtime: per-operator
/// metadata constants, a config-driven constructor, and a row-key encoder
/// so the driver can allocate `RowNumber` per `(group, window_start)`.
///
/// Authors implement BOTH traits in adjacent `impl` blocks. The contract
/// harness in `crate::testing::windowed` only sees the pure aggregator, so
/// test fixtures don't need FFI metadata; production operators registered
/// through [`TumblingDriver`] must impl both.