engenho-substrate 0.53.141

engenho-substrate — the substrate leaf: every module a shipped engenho crate references (derivations and caches, receipts, quorum, ledgers, verifiers, rollout gates, freshness, owned tasks), with engenho-substrate-core re-exported whole so every engenho_substrate:: path keeps resolving.
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
//! replay — typed cursor over event streams + máquina state reconstruction.
//!
//! Per the research brief — seventh inventive primitive. Composes
//! máquina + linhagem-aberta: an event stream can be replayed
//! through a `StateMachine` to deterministically reconstruct any
//! historical state — operators debug by bisecting cursors.
//!
//! ## Surface
//!
//!   - `ReplayCursor<E>` — typed cursor (position + bounds + skip /
//!     rewind / peek / reset)
//!   - `replay_into<M>` — drives a `MachineRunner<M>` through an
//!     event stream; returns count consumed or first machine error
//!   - `replay_bisect` — split-then-step pattern for finding the
//!     event that triggered a specific state predicate
//!
//! Two key properties:
//!   - Cursor positions form a deterministic sequence given the
//!     event Vec; no hidden state mutation
//!   - Replay through `MachineRunner` is deterministic by máquina's
//!     own determinism contract — same events, same `FrozenClock`,
//!     byte-identical state and history

use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};

use crate::maquina::{MachineError, MachineRunner, StateMachine};

/// Typed cursor over an event stream.
///
/// Lock-free; `next` uses `fetch_add` so multi-producer scenarios
/// each get unique positions (one-shot delivery semantics).
pub struct ReplayCursor<E: Clone> {
    events: Arc<[E]>,
    pos: AtomicUsize,
    name: &'static str,
}

impl<E: Clone> ReplayCursor<E> {
    /// New cursor over the given event sequence. Position starts at 0.
    pub fn new<I: IntoIterator<Item = E>>(name: &'static str, events: I) -> Self {
        let v: Vec<E> = events.into_iter().collect();
        Self {
            events: v.into(),
            pos: AtomicUsize::new(0),
            name,
        }
    }

    /// Advance and return the next event, or `None` if done. Atomic.
    pub fn next(&self) -> Option<E> {
        let p = self.pos.fetch_add(1, Ordering::AcqRel);
        if p >= self.events.len() {
            // Saturate back to len so position() reports accurately.
            self.pos.store(self.events.len(), Ordering::Release);
            None
        } else {
            Some(self.events[p].clone())
        }
    }

    /// Peek at the next event without advancing.
    pub fn peek(&self) -> Option<E> {
        let p = self.pos.load(Ordering::Acquire);
        self.events.get(p).cloned()
    }

    /// Reset to position 0.
    pub fn reset(&self) {
        self.pos.store(0, Ordering::Release);
    }

    /// Current position (0..=len).
    pub fn position(&self) -> usize {
        self.pos.load(Ordering::Acquire).min(self.events.len())
    }

    /// Total event count.
    #[must_use]
    pub fn len(&self) -> usize {
        self.events.len()
    }

    /// Remaining events from the current position.
    pub fn remaining(&self) -> usize {
        self.len().saturating_sub(self.position())
    }

    /// True if cursor is exhausted.
    pub fn is_done(&self) -> bool {
        self.remaining() == 0
    }

    /// True if cursor has not been advanced.
    pub fn is_empty(&self) -> bool {
        self.events.is_empty()
    }

    /// Skip forward up to `n` positions. Returns the actual count skipped
    /// (clamped to remaining).
    pub fn skip(&self, n: usize) -> usize {
        let curr = self.position();
        let target = (curr + n).min(self.events.len());
        let delta = target - curr;
        self.pos.store(target, Ordering::Release);
        delta
    }

    /// Rewind up to `n` positions. Returns the actual count rewound
    /// (clamped to current position).
    pub fn rewind(&self, n: usize) -> usize {
        let curr = self.position();
        let target = curr.saturating_sub(n);
        let delta = curr - target;
        self.pos.store(target, Ordering::Release);
        delta
    }
}

crate::impl_named_field_generic!(ReplayCursor, E: Clone);

/// Observable snapshot of a `ReplayCursor<E>` — cursor progress
/// metadata (name + position + len + remaining + is_done). Pattern
/// #2 (SSC v0.99): ReplayCursor's progress becomes plottable on
/// any mirante dashboard without per-consumer adapter.
///
/// Note: the snapshot does NOT include the cursor's E-typed events
/// (could be large + would conflict with the generic bound). Just
/// the metadata that operators need for "how far through replay
/// are we" telemetry.
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ReplayCursorSnapshot {
    /// Cursor name (telemetry identifier).
    pub name: &'static str,
    /// Current 0-based position (0..=len).
    pub position: usize,
    /// Total event count.
    pub len: usize,
    /// Remaining events to consume from current position.
    pub remaining: usize,
    /// True when position == len (no more events).
    pub is_done: bool,
}

impl<E: Clone + Send + Sync + 'static> crate::mirante::Observable for ReplayCursor<E> {
    type Snapshot = ReplayCursorSnapshot;

    fn snapshot(&self) -> Self::Snapshot {
        ReplayCursorSnapshot {
            name: <Self as crate::named::Named>::name(self),
            position: self.position(),
            len: self.len(),
            remaining: self.remaining(),
            is_done: self.is_done(),
        }
    }
}

/// Drive a `MachineRunner<M>` through every remaining event in
/// `cursor`. Stops on first machine error OR when cursor is
/// exhausted; returns the number of events actually applied.
///
/// # Errors
/// Returns `MachineError<M::Err>` from the first step that fails;
/// remaining events are NOT consumed (cursor stops at the failing
/// position).
pub fn replay_into<M: StateMachine>(
    runner: &mut MachineRunner<M>,
    cursor: &ReplayCursor<M::Event>,
) -> Result<usize, MachineError<M::Err>>
where
    M::Event: Clone,
{
    let mut applied = 0;
    while let Some(event) = cursor.peek() {
        runner.step(event)?;
        // Only advance after the step succeeds, so failures leave the
        // cursor on the offending event for inspection.
        cursor.next();
        applied += 1;
    }
    Ok(applied)
}

/// Bisect: replay through `cursor` until `predicate(state)` becomes
/// true. Returns the (position, `transition_count`) at which the
/// predicate fired, or `None` if it never did.
///
/// # Errors
/// Returns `MachineError<M::Err>` if any intermediate step fails.
pub fn replay_until<M: StateMachine>(
    runner: &mut MachineRunner<M>,
    cursor: &ReplayCursor<M::Event>,
    predicate: impl Fn(&M::State) -> bool,
) -> Result<Option<usize>, MachineError<M::Err>>
where
    M::Event: Clone,
{
    if predicate(runner.state()) {
        return Ok(Some(0));
    }
    let mut steps = 0;
    while let Some(event) = cursor.peek() {
        runner.step(event)?;
        cursor.next();
        steps += 1;
        if predicate(runner.state()) {
            return Ok(Some(steps));
        }
    }
    Ok(None)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::impl_error_kind;
    use crate::maquina::{MachineRunner, StateMachine};
    use crate::named::Named;
    use crate::relogio::FrozenClock;
    use serde::{Deserialize, Serialize};
    use std::sync::Arc;
    use thiserror::Error;

    // ── Cursor primitive tests ──────────────────────────────────

    #[test]
    fn empty_cursor_returns_none() {
        let c: ReplayCursor<u32> = ReplayCursor::new("test", vec![]);
        assert_eq!(c.next(), None);
        assert!(c.is_done());
        assert!(c.is_empty());
        assert_eq!(c.len(), 0);
        assert_eq!(c.remaining(), 0);
    }

    #[test]
    fn next_advances_position() {
        let c = ReplayCursor::new("test", vec![1, 2, 3]);
        assert_eq!(c.position(), 0);
        assert_eq!(c.next(), Some(1));
        assert_eq!(c.position(), 1);
        assert_eq!(c.next(), Some(2));
        assert_eq!(c.next(), Some(3));
        assert_eq!(c.next(), None);
        assert_eq!(c.position(), 3);
    }

    #[test]
    fn peek_does_not_advance() {
        let c = ReplayCursor::new("test", vec![1, 2, 3]);
        assert_eq!(c.peek(), Some(1));
        assert_eq!(c.peek(), Some(1));
        assert_eq!(c.position(), 0);
        let _ = c.next();
        assert_eq!(c.peek(), Some(2));
    }

    #[test]
    fn reset_returns_to_zero() {
        let c = ReplayCursor::new("test", vec![1, 2, 3]);
        let _ = c.next();
        let _ = c.next();
        c.reset();
        assert_eq!(c.position(), 0);
        assert_eq!(c.next(), Some(1));
    }

    #[test]
    fn skip_clamps_to_len() {
        let c = ReplayCursor::new("test", vec![1, 2, 3]);
        let skipped = c.skip(10);
        assert_eq!(skipped, 3);
        assert_eq!(c.position(), 3);
        assert!(c.is_done());
    }

    #[test]
    fn rewind_clamps_to_zero() {
        let c = ReplayCursor::new("test", vec![1, 2, 3]);
        c.skip(2);
        let rewound = c.rewind(5);
        assert_eq!(rewound, 2);
        assert_eq!(c.position(), 0);
    }

    #[test]
    fn remaining_decreases_with_next() {
        let c = ReplayCursor::new("test", vec![10, 20, 30, 40]);
        assert_eq!(c.remaining(), 4);
        let _ = c.next();
        assert_eq!(c.remaining(), 3);
        c.skip(2);
        assert_eq!(c.remaining(), 1);
        let _ = c.next();
        assert_eq!(c.remaining(), 0);
    }

    #[test]
    fn named_works() {
        let c: ReplayCursor<u32> = ReplayCursor::new("ev-stream", vec![]);
        assert_eq!(c.name(), "ev-stream");
    }

    // ── replay_into with a Sum machine ──────────────────────────

    #[derive(Default)]
    struct SumMachine;
    crate::define_named!(SumMachine, "sum");

    #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
    enum SumState {
        Active(i64),
        Capped,
    }

    #[derive(Clone, Debug, Serialize, Deserialize)]
    enum SumEvent {
        Add(i64),
    }

    #[derive(Debug, Clone, Error)]
    enum SumErr {
        #[error("overflow")]
        Overflow,
    }
    impl_error_kind! {
        SumErr {
            Overflow => "overflow",
        }
    }

    impl StateMachine for SumMachine {
        type State = SumState;
        type Event = SumEvent;
        type Effect = i64;
        type Err = SumErr;

        fn initial() -> SumState {
            SumState::Active(0)
        }

        fn step(state: &SumState, event: &SumEvent) -> Result<(SumState, i64), SumErr> {
            match (state, event) {
                (SumState::Active(n), SumEvent::Add(by)) => {
                    let next = n.checked_add(*by).ok_or(SumErr::Overflow)?;
                    Ok((SumState::Active(next), next))
                }
                (SumState::Capped, _) => Err(SumErr::Overflow),
            }
        }

        fn is_terminal(state: &SumState) -> bool {
            matches!(state, SumState::Capped)
        }
    }

    #[test]
    fn replay_into_consumes_all_events() {
        let mut runner = MachineRunner::<SumMachine>::new(Arc::new(FrozenClock::at(0)));
        let cursor = ReplayCursor::new(
            "test",
            vec![SumEvent::Add(1), SumEvent::Add(2), SumEvent::Add(3)],
        );
        let applied = replay_into(&mut runner, &cursor).unwrap();
        assert_eq!(applied, 3);
        assert_eq!(runner.state(), &SumState::Active(6));
        assert!(cursor.is_done());
    }

    #[test]
    fn replay_into_stops_on_machine_error() {
        let mut runner = MachineRunner::<SumMachine>::new(Arc::new(FrozenClock::at(0)));
        runner.step(SumEvent::Add(i64::MAX - 1)).unwrap();
        let cursor = ReplayCursor::new(
            "test",
            vec![SumEvent::Add(1), SumEvent::Add(100), SumEvent::Add(200)],
        );
        let err = replay_into(&mut runner, &cursor).unwrap_err();
        match err {
            MachineError::Step(SumErr::Overflow) => {}
            _ => panic!("expected Overflow"),
        }
        // Cursor stopped at the offending event (Add(100)); Add(1) consumed first.
        assert_eq!(cursor.position(), 1);
    }

    #[test]
    fn replay_until_finds_predicate() {
        let mut runner = MachineRunner::<SumMachine>::new(Arc::new(FrozenClock::at(0)));
        let cursor = ReplayCursor::new(
            "test",
            vec![
                SumEvent::Add(1),
                SumEvent::Add(2),
                SumEvent::Add(3),
                SumEvent::Add(4),
                SumEvent::Add(5),
            ],
        );
        // Stop when sum reaches 6.
        let found = replay_until(
            &mut runner,
            &cursor,
            |s| matches!(s, SumState::Active(n) if *n >= 6),
        )
        .unwrap();
        assert_eq!(found, Some(3)); // 1+2+3 = 6 at step 3
        assert_eq!(runner.state(), &SumState::Active(6));
        assert_eq!(cursor.position(), 3);
    }

    #[test]
    fn replay_until_returns_none_when_predicate_never_fires() {
        let mut runner = MachineRunner::<SumMachine>::new(Arc::new(FrozenClock::at(0)));
        let cursor = ReplayCursor::new("test", vec![SumEvent::Add(1), SumEvent::Add(2)]);
        let found = replay_until(
            &mut runner,
            &cursor,
            |s| matches!(s, SumState::Active(n) if *n >= 1000),
        )
        .unwrap();
        assert_eq!(found, None);
        // Cursor exhausted.
        assert!(cursor.is_done());
    }

    #[test]
    fn replay_until_zero_steps_when_predicate_already_true() {
        let mut runner = MachineRunner::<SumMachine>::new(Arc::new(FrozenClock::at(0)));
        let cursor = ReplayCursor::new("test", vec![SumEvent::Add(1)]);
        let found = replay_until(&mut runner, &cursor, |_| true).unwrap();
        assert_eq!(found, Some(0));
        // Cursor unaffected.
        assert_eq!(cursor.position(), 0);
    }
}