spanfold 0.1.1

Temporal state-window recording, comparison, and audit primitives 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
use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::{TemporalAxis, TemporalPoint};

/// Stable identity for one tracked source lane.
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct LaneKey {
    /// Source or lane identifier.
    pub lane: String,
    /// Optional partition identifier.
    pub partition: Option<String>,
}

impl LaneKey {
    /// Creates an unpartitioned lane key.
    #[must_use]
    pub fn new(lane: impl Into<String>) -> Self {
        Self {
            lane: lane.into(),
            partition: None,
        }
    }

    /// Creates a partition-aware lane key.
    #[must_use]
    pub fn with_partition(lane: impl Into<String>, partition: impl Into<String>) -> Self {
        Self {
            lane: lane.into(),
            partition: Some(partition.into()),
        }
    }
}

impl From<&str> for LaneKey {
    fn from(value: &str) -> Self {
        Self::new(value)
    }
}

impl From<String> for LaneKey {
    fn from(value: String) -> Self {
        Self::new(value)
    }
}

/// A deterministic lane liveness state change.
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct LaneLivenessSignal {
    /// Source or lane identifier.
    pub lane: String,
    /// Optional partition identifier.
    pub partition: Option<String>,
    /// Whether this signal represents silence instead of recovery/alive state.
    #[serde(rename = "isSilent")]
    pub is_silent: bool,
    /// Point where the liveness transition occurred.
    #[serde(rename = "occurredAt")]
    pub occurred_at: TemporalPoint,
    /// Point where the lane was evaluated.
    #[serde(rename = "evaluatedAt")]
    pub evaluated_at: TemporalPoint,
    /// Silence threshold magnitude on the same axis as the tracked points.
    #[serde(rename = "silenceThresholdMagnitude")]
    pub silence_threshold_magnitude: i64,
}

impl<'de> Deserialize<'de> for LaneLivenessSignal {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        #[derive(Deserialize)]
        #[serde(deny_unknown_fields)]
        struct RawSignal {
            lane: String,
            partition: Option<String>,
            #[serde(rename = "isSilent")]
            is_silent: bool,
            #[serde(rename = "occurredAt")]
            occurred_at: TemporalPoint,
            #[serde(rename = "evaluatedAt")]
            evaluated_at: TemporalPoint,
            #[serde(rename = "silenceThresholdMagnitude")]
            silence_threshold_magnitude: i64,
        }

        let raw = RawSignal::deserialize(deserializer)?;
        if raw.lane.trim().is_empty() {
            return Err(serde::de::Error::custom("lane cannot be empty"));
        }
        if raw.silence_threshold_magnitude <= 0 {
            return Err(serde::de::Error::custom(
                "silence threshold must be greater than zero",
            ));
        }
        if !raw.occurred_at.is_compatible_with(&raw.evaluated_at)
            || !matches!(
                raw.occurred_at.try_cmp(&raw.evaluated_at),
                Ok(std::cmp::Ordering::Less | std::cmp::Ordering::Equal)
            )
        {
            return Err(serde::de::Error::custom(
                "liveness signal points must share a domain and occurredAt must not be after evaluatedAt",
            ));
        }
        Ok(Self {
            lane: raw.lane,
            partition: raw.partition,
            is_silent: raw.is_silent,
            occurred_at: raw.occurred_at,
            evaluated_at: raw.evaluated_at,
            silence_threshold_magnitude: raw.silence_threshold_magnitude,
        })
    }
}

impl LaneLivenessSignal {
    fn new(
        key: &LaneKey,
        is_silent: bool,
        occurred_at: TemporalPoint,
        evaluated_at: TemporalPoint,
        silence_threshold_magnitude: i64,
    ) -> Self {
        Self {
            lane: key.lane.clone(),
            partition: key.partition.clone(),
            is_silent,
            occurred_at,
            evaluated_at,
            silence_threshold_magnitude,
        }
    }
}

#[derive(Clone, Debug)]
struct LaneState {
    key: LaneKey,
    started_at: TemporalPoint,
    last_observed_at: Option<TemporalPoint>,
    has_reported_state: bool,
    is_silent: bool,
}

impl LaneState {
    fn new(key: LaneKey, started_at: TemporalPoint) -> Self {
        Self {
            key,
            started_at,
            last_observed_at: None,
            has_reported_state: false,
            is_silent: false,
        }
    }
}

/// Liveness tracker construction and evaluation errors.
#[derive(Clone, Debug, Error, Eq, PartialEq)]
pub enum LaneLivenessError {
    /// At least one lane must be tracked.
    #[error("at least one lane must be tracked")]
    EmptyLanes,
    /// Silence thresholds must be positive.
    #[error("silence threshold must be greater than zero: {threshold}")]
    NonPositiveThreshold {
        /// Invalid threshold magnitude.
        threshold: i64,
    },
    /// Tracked lane identities must be unique.
    #[error("tracked lanes must be unique: {lane:?}")]
    DuplicateLane {
        /// Duplicate lane key.
        lane: LaneKey,
    },
    /// Observations/checks must use the same temporal axis as the tracker.
    #[error("liveness point axis mismatch: expected={expected:?}, actual={actual:?}")]
    AxisMismatch {
        /// Expected temporal axis.
        expected: TemporalAxis,
        /// Actual temporal axis.
        actual: TemporalAxis,
    },
    /// Observations cannot precede tracker start.
    #[error("observation cannot be earlier than tracker start")]
    ObservationBeforeStart,
    /// Per-lane observations must be monotonic.
    #[error("observation cannot be earlier than the lane's previous observation: {lane:?}")]
    ObservationMovedBackwards {
        /// Lane that moved backwards.
        lane: LaneKey,
    },
    /// Check horizons cannot precede tracker start.
    #[error("liveness horizon cannot be earlier than tracker start")]
    HorizonBeforeStart,
    /// Check horizons must be monotonic.
    #[error("liveness horizon cannot move backwards")]
    HorizonMovedBackwards,
    /// Observed lane is not tracked.
    #[error("lane is not tracked by this liveness tracker: {lane:?}")]
    UnknownLane {
        /// Unknown lane key.
        lane: LaneKey,
    },
    /// Timestamp points use different clocks.
    #[error("liveness point clock mismatch")]
    ClockMismatch,
    /// Observations cannot arrive before the last evaluation horizon.
    #[error("observation cannot precede the last liveness check")]
    ObservationBeforeLastCheck,
    /// Silence threshold arithmetic overflowed.
    #[error("liveness silence horizon overflowed")]
    ArithmeticOverflow,
}

/// Deterministic heartbeat/silence tracker for a fixed set of lanes.
///
/// The tracker owns no timers, background tasks, persistence, or IO. Call
/// [`observe`](Self::observe) when a lane reports and [`check`](Self::check) at
/// explicit horizons. Returned signals can be ingested into a normal Spanfold
/// pipeline to record silence windows.
#[derive(Clone, Debug)]
pub struct LaneLivenessTracker {
    started_at: TemporalPoint,
    last_check_at: TemporalPoint,
    silence_threshold_magnitude: i64,
    lanes: BTreeMap<LaneKey, LaneState>,
}

impl LaneLivenessTracker {
    /// Creates a tracker for explicit lane keys.
    pub fn new<I>(
        started_at: TemporalPoint,
        silence_threshold_magnitude: i64,
        lanes: I,
    ) -> Result<Self, LaneLivenessError>
    where
        I: IntoIterator<Item = LaneKey>,
    {
        if silence_threshold_magnitude <= 0 {
            return Err(LaneLivenessError::NonPositiveThreshold {
                threshold: silence_threshold_magnitude,
            });
        }

        let mut states = BTreeMap::new();
        for lane in lanes {
            if states.contains_key(&lane) {
                return Err(LaneLivenessError::DuplicateLane { lane });
            }
            states.insert(lane.clone(), LaneState::new(lane, started_at.clone()));
        }

        if states.is_empty() {
            return Err(LaneLivenessError::EmptyLanes);
        }

        Ok(Self {
            started_at: started_at.clone(),
            last_check_at: started_at,
            silence_threshold_magnitude,
            lanes: states,
        })
    }

    /// Creates a tracker for unpartitioned lanes.
    pub fn for_lanes<I, S>(
        started_at: TemporalPoint,
        silence_threshold_magnitude: i64,
        lanes: I,
    ) -> Result<Self, LaneLivenessError>
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        Self::new(
            started_at,
            silence_threshold_magnitude,
            lanes.into_iter().map(|lane| LaneKey::new(lane.into())),
        )
    }

    /// Returns the tracker start point.
    #[must_use]
    pub fn started_at(&self) -> TemporalPoint {
        self.started_at.clone()
    }

    /// Returns the configured silence threshold magnitude.
    #[must_use]
    pub const fn silence_threshold_magnitude(&self) -> i64 {
        self.silence_threshold_magnitude
    }

    /// Records an observation for an unpartitioned lane.
    pub fn observe(
        &mut self,
        lane: impl Into<String>,
        observed_at: TemporalPoint,
    ) -> Result<Vec<LaneLivenessSignal>, LaneLivenessError> {
        self.observe_key(LaneKey::new(lane.into()), observed_at)
    }

    /// Records an observation for a partitioned lane.
    pub fn observe_partition(
        &mut self,
        lane: impl Into<String>,
        partition: impl Into<String>,
        observed_at: TemporalPoint,
    ) -> Result<Vec<LaneLivenessSignal>, LaneLivenessError> {
        self.observe_key(
            LaneKey::with_partition(lane.into(), partition.into()),
            observed_at,
        )
    }

    /// Records an observation for an explicit lane key.
    pub fn observe_key(
        &mut self,
        lane: LaneKey,
        observed_at: TemporalPoint,
    ) -> Result<Vec<LaneLivenessSignal>, LaneLivenessError> {
        self.ensure_compatible(&observed_at)?;
        if matches!(
            observed_at.try_cmp(&self.started_at),
            Ok(std::cmp::Ordering::Less)
        ) {
            return Err(LaneLivenessError::ObservationBeforeStart);
        }
        if matches!(
            observed_at.try_cmp(&self.last_check_at),
            Ok(std::cmp::Ordering::Less)
        ) {
            return Err(LaneLivenessError::ObservationBeforeLastCheck);
        }

        let Some(state) = self.lanes.get_mut(&lane) else {
            return Err(LaneLivenessError::UnknownLane { lane });
        };
        if state
            .last_observed_at
            .as_ref()
            .is_some_and(|last_observed_at| observed_at.magnitude() < last_observed_at.magnitude())
        {
            return Err(LaneLivenessError::ObservationMovedBackwards { lane });
        }

        state.last_observed_at = Some(observed_at.clone());
        if !state.has_reported_state || state.is_silent {
            state.has_reported_state = true;
            state.is_silent = false;
            return Ok(vec![LaneLivenessSignal::new(
                &state.key,
                false,
                observed_at.clone(),
                observed_at,
                self.silence_threshold_magnitude,
            )]);
        }

        Ok(Vec::new())
    }

    /// Evaluates all tracked lanes at an explicit horizon.
    pub fn check(
        &mut self,
        horizon: TemporalPoint,
    ) -> Result<Vec<LaneLivenessSignal>, LaneLivenessError> {
        self.ensure_compatible(&horizon)?;
        if matches!(
            horizon.try_cmp(&self.started_at),
            Ok(std::cmp::Ordering::Less)
        ) {
            return Err(LaneLivenessError::HorizonBeforeStart);
        }
        if matches!(
            horizon.try_cmp(&self.last_check_at),
            Ok(std::cmp::Ordering::Less)
        ) {
            return Err(LaneLivenessError::HorizonMovedBackwards);
        }

        self.last_check_at = horizon.clone();
        let threshold = self.silence_threshold_magnitude;
        let mut signals = Vec::new();
        for state in self.lanes.values_mut() {
            let silence_started_at = add_magnitude(
                state
                    .last_observed_at
                    .clone()
                    .unwrap_or_else(|| state.started_at.clone()),
                threshold,
            )?;
            if state.is_silent
                || matches!(
                    horizon.try_cmp(&silence_started_at),
                    Ok(std::cmp::Ordering::Less)
                )
            {
                continue;
            }

            state.has_reported_state = true;
            state.is_silent = true;
            signals.push(LaneLivenessSignal::new(
                &state.key,
                true,
                silence_started_at,
                horizon.clone(),
                threshold,
            ));
        }
        Ok(signals)
    }

    fn ensure_compatible(&self, point: &TemporalPoint) -> Result<(), LaneLivenessError> {
        let expected = self.started_at.axis();
        let actual = point.axis();
        if expected != actual {
            return Err(LaneLivenessError::AxisMismatch { expected, actual });
        }
        if !self.started_at.is_compatible_with(point) {
            return Err(LaneLivenessError::ClockMismatch);
        }
        Ok(())
    }
}

fn add_magnitude(point: TemporalPoint, magnitude: i64) -> Result<TemporalPoint, LaneLivenessError> {
    let value = point
        .magnitude()
        .checked_add(magnitude)
        .ok_or(LaneLivenessError::ArithmeticOverflow)?;
    match point.axis() {
        TemporalAxis::ProcessingPosition => Ok(TemporalPoint::position(value)),
        TemporalAxis::Timestamp => Ok(match point.clock() {
            Some(clock) => TemporalPoint::timestamp_ticks_with_clock(value, clock),
            None => TemporalPoint::timestamp_ticks(value),
        }),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn first_observation_emits_alive_once() {
        let started_at = TemporalPoint::timestamp_ticks(100);
        let mut tracker =
            LaneLivenessTracker::for_lanes(started_at.clone(), 30, ["lane-a"]).expect("tracker");

        let first = tracker
            .observe("lane-a", TemporalPoint::timestamp_ticks(105))
            .expect("first observation");
        let second = tracker
            .observe("lane-a", TemporalPoint::timestamp_ticks(110))
            .expect("second observation");

        assert_eq!(first.len(), 1);
        assert_eq!(first[0].lane, "lane-a");
        assert!(!first[0].is_silent);
        assert_eq!(first[0].occurred_at, TemporalPoint::timestamp_ticks(105));
        assert!(second.is_empty());
    }

    #[test]
    fn check_emits_silence_once_when_lane_expires() {
        let started_at = TemporalPoint::timestamp_ticks(100);
        let mut tracker =
            LaneLivenessTracker::for_lanes(started_at.clone(), 30, ["lane-a"]).expect("tracker");
        tracker
            .observe("lane-a", TemporalPoint::timestamp_ticks(105))
            .expect("observation");

        let early = tracker
            .check(TemporalPoint::timestamp_ticks(134))
            .expect("early check");
        let expired = tracker
            .check(TemporalPoint::timestamp_ticks(140))
            .expect("expired check");
        let repeated = tracker
            .check(TemporalPoint::timestamp_ticks(150))
            .expect("repeated check");

        assert!(early.is_empty());
        assert_eq!(expired.len(), 1);
        assert!(expired[0].is_silent);
        assert_eq!(expired[0].occurred_at, TemporalPoint::timestamp_ticks(135));
        assert_eq!(expired[0].evaluated_at, TemporalPoint::timestamp_ticks(140));
        assert!(repeated.is_empty());
    }

    #[test]
    fn observation_after_silence_emits_recovery() {
        let started_at = TemporalPoint::timestamp_ticks(100);
        let mut tracker =
            LaneLivenessTracker::for_lanes(started_at.clone(), 30, ["lane-a"]).expect("tracker");

        tracker
            .observe("lane-a", started_at)
            .expect("first observation");
        tracker
            .check(TemporalPoint::timestamp_ticks(131))
            .expect("silence check");
        let recovery = tracker
            .observe("lane-a", TemporalPoint::timestamp_ticks(145))
            .expect("recovery");

        assert_eq!(recovery.len(), 1);
        assert!(!recovery[0].is_silent);
        assert_eq!(recovery[0].occurred_at, TemporalPoint::timestamp_ticks(145));
    }

    #[test]
    fn check_can_emit_silence_for_lane_that_never_reported() {
        let started_at = TemporalPoint::timestamp_ticks(100);
        let mut tracker =
            LaneLivenessTracker::for_lanes(started_at.clone(), 30, ["lane-a"]).expect("tracker");

        let signal = tracker
            .check(TemporalPoint::timestamp_ticks(140))
            .expect("check")
            .remove(0);

        assert!(signal.is_silent);
        assert_eq!(signal.occurred_at, TemporalPoint::timestamp_ticks(130));
        assert_eq!(signal.evaluated_at, TemporalPoint::timestamp_ticks(140));
    }

    #[test]
    fn tracker_rejects_unknown_lane() {
        let started_at = TemporalPoint::timestamp_ticks(100);
        let mut tracker =
            LaneLivenessTracker::for_lanes(started_at.clone(), 30, ["lane-a"]).expect("tracker");

        let error = tracker
            .observe("lane-b", started_at)
            .expect_err("unknown lane should fail");

        assert!(matches!(error, LaneLivenessError::UnknownLane { .. }));
    }

    #[test]
    fn partitioned_lanes_are_tracked_independently() {
        let started_at = TemporalPoint::position(0);
        let mut tracker = LaneLivenessTracker::new(
            started_at,
            10,
            [
                LaneKey::with_partition("provider-a", "partition-1"),
                LaneKey::with_partition("provider-a", "partition-2"),
            ],
        )
        .expect("tracker");

        tracker
            .observe_partition("provider-a", "partition-1", TemporalPoint::position(1))
            .expect("partition-1 observation");
        let signals = tracker.check(TemporalPoint::position(10)).expect("check");

        assert_eq!(signals.len(), 1);
        assert_eq!(signals[0].partition.as_deref(), Some("partition-2"));
    }
}