bones-core 0.24.4

Core data structures, CRDT event model, and projection engine for bones
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
//! Epoch+Phase state CRDT for work item lifecycle.
//!
//! The lifecycle state (Open/Doing/Done/Archived) uses a non-standard
//! (epoch, phase) CRDT that handles concurrent close/reopen correctly:
//! if agent A closes an item while agent B reopens it, the higher epoch wins.
//!
//! # Phase Ranking
//!
//! Phases have a total ordering:
//!   Open(0) < Doing(1) < Done(2) < Archived(3)
//!
//! # Merge Rules
//!
//! Given two `EpochPhaseState` values `a` and `b`:
//!   1. If `a.epoch != b.epoch`: the one with higher epoch wins entirely.
//!   2. If `a.epoch == b.epoch`: the one with higher phase rank wins.
//!
//! This satisfies the semilattice laws (commutative, associative, idempotent).
//!
//! # Reopen Semantics
//!
//! To reopen an item, increment the epoch and set phase to Open.
//! This guarantees the reopen wins against any concurrent operations
//! in the previous epoch.

use serde::{Deserialize, Serialize};
use std::fmt;

// ---------------------------------------------------------------------------
// Phase enum
// ---------------------------------------------------------------------------

/// Work item lifecycle phase with total ordering by rank.
///
/// The discriminant values define the rank for merge comparison.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[repr(u8)]
pub enum Phase {
    Open = 0,
    Doing = 1,
    Done = 2,
    Archived = 3,
}

impl Phase {
    /// Return the numeric rank of this phase.
    #[must_use]
    pub const fn rank(self) -> u8 {
        self as u8
    }

    /// Return the phase name as a string slice.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Open => "open",
            Self::Doing => "doing",
            Self::Done => "done",
            Self::Archived => "archived",
        }
    }

    /// All phases in rank order.
    pub const ALL: [Self; 4] = [Self::Open, Self::Doing, Self::Done, Self::Archived];
}

impl PartialOrd for Phase {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Phase {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.rank().cmp(&other.rank())
    }
}

impl fmt::Display for Phase {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

impl std::str::FromStr for Phase {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "open" => Ok(Self::Open),
            "doing" => Ok(Self::Doing),
            "done" => Ok(Self::Done),
            "archived" => Ok(Self::Archived),
            _ => Err(format!("unknown phase: {s}")),
        }
    }
}

// ---------------------------------------------------------------------------
// EpochPhaseState
// ---------------------------------------------------------------------------

/// CRDT state combining an epoch counter with a lifecycle phase.
///
/// The epoch increases on reopen operations, ensuring that reopens
/// always win against concurrent operations in earlier epochs.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct EpochPhaseState {
    /// Monotonically increasing epoch counter. Incremented on reopen.
    pub epoch: u64,
    /// Current lifecycle phase within this epoch.
    pub phase: Phase,
}

impl EpochPhaseState {
    /// Create a new state at epoch 0, phase Open.
    #[must_use]
    pub const fn new() -> Self {
        Self {
            epoch: 0,
            phase: Phase::Open,
        }
    }

    /// Create a state with specific epoch and phase.
    #[must_use]
    pub const fn with(epoch: u64, phase: Phase) -> Self {
        Self { epoch, phase }
    }

    /// Advance to a new phase within the current epoch.
    ///
    /// Returns `Err` if the target phase has a lower rank than the current
    /// phase (within the same epoch, phases only move forward).
    ///
    /// # Errors
    ///
    /// Returns [`StateError::InvalidTransition`] if the target phase has
    /// a rank equal to or lower than the current phase.
    pub fn advance(&mut self, target: Phase) -> Result<(), StateError> {
        if target <= self.phase {
            return Err(StateError::InvalidTransition {
                from: self.phase,
                to: target,
                epoch: self.epoch,
            });
        }
        self.phase = target;
        Ok(())
    }

    /// Reopen the item: increment epoch and set phase to Open.
    ///
    /// This is valid from any phase. The epoch increment ensures
    /// this reopen wins against concurrent operations in the old epoch.
    pub const fn reopen(&mut self) {
        self.epoch += 1;
        self.phase = Phase::Open;
    }

    /// Merge two states, producing the semilattice join (least upper bound).
    ///
    /// Rules:
    /// 1. Higher epoch wins entirely.
    /// 2. Same epoch: higher phase rank wins.
    pub fn merge(&mut self, other: &Self) {
        match self.epoch.cmp(&other.epoch) {
            std::cmp::Ordering::Less => {
                // Other has higher epoch — take it entirely
                self.epoch = other.epoch;
                self.phase = other.phase;
            }
            std::cmp::Ordering::Equal => {
                // Same epoch — take higher phase
                if other.phase > self.phase {
                    self.phase = other.phase;
                }
            }
            std::cmp::Ordering::Greater => {
                // We have higher epoch — keep ours
            }
        }
    }
}

impl Default for EpochPhaseState {
    fn default() -> Self {
        Self::new()
    }
}

impl fmt::Display for EpochPhaseState {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "epoch={} phase={}", self.epoch, self.phase)
    }
}

// ---------------------------------------------------------------------------
// Error type
// ---------------------------------------------------------------------------

/// Error for invalid state transitions.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum StateError {
    #[error("invalid transition from {from} to {to} in epoch {epoch}")]
    InvalidTransition { from: Phase, to: Phase, epoch: u64 },
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    // === Phase ordering ===

    #[test]
    fn phase_ranking() {
        assert!(Phase::Open < Phase::Doing);
        assert!(Phase::Doing < Phase::Done);
        assert!(Phase::Done < Phase::Archived);
    }

    #[test]
    fn phase_rank_values() {
        assert_eq!(Phase::Open.rank(), 0);
        assert_eq!(Phase::Doing.rank(), 1);
        assert_eq!(Phase::Done.rank(), 2);
        assert_eq!(Phase::Archived.rank(), 3);
    }

    #[test]
    fn phase_display_and_parse() {
        for phase in Phase::ALL {
            let s = phase.to_string();
            let parsed: Phase = s.parse().unwrap();
            assert_eq!(phase, parsed);
        }
    }

    // === EpochPhaseState basics ===

    #[test]
    fn new_state_is_epoch_0_open() {
        let s = EpochPhaseState::new();
        assert_eq!(s.epoch, 0);
        assert_eq!(s.phase, Phase::Open);
    }

    #[test]
    fn advance_forward() {
        let mut s = EpochPhaseState::new();
        s.advance(Phase::Doing).unwrap();
        assert_eq!(s.phase, Phase::Doing);
        s.advance(Phase::Done).unwrap();
        assert_eq!(s.phase, Phase::Done);
        s.advance(Phase::Archived).unwrap();
        assert_eq!(s.phase, Phase::Archived);
    }

    #[test]
    fn advance_backward_fails() {
        let mut s = EpochPhaseState::with(0, Phase::Done);
        let err = s.advance(Phase::Doing).unwrap_err();
        assert!(matches!(err, StateError::InvalidTransition { .. }));
    }

    #[test]
    fn advance_same_phase_fails() {
        let mut s = EpochPhaseState::with(0, Phase::Doing);
        let err = s.advance(Phase::Doing).unwrap_err();
        assert!(matches!(err, StateError::InvalidTransition { .. }));
    }

    // === Reopen ===

    #[test]
    fn reopen_increments_epoch() {
        let mut s = EpochPhaseState::with(0, Phase::Done);
        s.reopen();
        assert_eq!(s.epoch, 1);
        assert_eq!(s.phase, Phase::Open);
    }

    #[test]
    fn reopen_from_archived() {
        let mut s = EpochPhaseState::with(0, Phase::Archived);
        s.reopen();
        assert_eq!(s.epoch, 1);
        assert_eq!(s.phase, Phase::Open);
    }

    #[test]
    fn multiple_reopens_monotonic_epochs() {
        let mut s = EpochPhaseState::new();
        for expected_epoch in 1..=5 {
            s.advance(Phase::Done).unwrap_or(()); // may fail if already past Done
            s.reopen();
            assert_eq!(s.epoch, expected_epoch);
            assert_eq!(s.phase, Phase::Open);
        }
    }

    // === Merge: same epoch ===

    #[test]
    fn merge_same_epoch_higher_phase_wins() {
        let mut a = EpochPhaseState::with(0, Phase::Open);
        let b = EpochPhaseState::with(0, Phase::Done);
        a.merge(&b);
        assert_eq!(a.phase, Phase::Done);
        assert_eq!(a.epoch, 0);
    }

    #[test]
    fn merge_same_epoch_lower_phase_no_change() {
        let mut a = EpochPhaseState::with(0, Phase::Done);
        let b = EpochPhaseState::with(0, Phase::Open);
        a.merge(&b);
        assert_eq!(a.phase, Phase::Done);
    }

    #[test]
    fn merge_same_epoch_same_phase_idempotent() {
        let mut a = EpochPhaseState::with(0, Phase::Doing);
        let b = EpochPhaseState::with(0, Phase::Doing);
        a.merge(&b);
        assert_eq!(a.phase, Phase::Doing);
        assert_eq!(a.epoch, 0);
    }

    // === Merge: different epochs ===

    #[test]
    fn merge_higher_epoch_wins() {
        let mut a = EpochPhaseState::with(1, Phase::Open);
        let b = EpochPhaseState::with(2, Phase::Doing);
        a.merge(&b);
        assert_eq!(a.epoch, 2);
        assert_eq!(a.phase, Phase::Doing);
    }

    #[test]
    fn merge_lower_epoch_no_change() {
        let mut a = EpochPhaseState::with(3, Phase::Doing);
        let b = EpochPhaseState::with(1, Phase::Archived);
        a.merge(&b);
        assert_eq!(a.epoch, 3);
        assert_eq!(a.phase, Phase::Doing);
    }

    // === Concurrent close/reopen ===

    #[test]
    fn concurrent_close_and_reopen_reopen_wins() {
        // Agent A closes: epoch 0, Done
        let close = EpochPhaseState::with(0, Phase::Done);
        // Agent B reopens: epoch 1, Open
        let reopen = EpochPhaseState::with(1, Phase::Open);

        // Merge order 1: close into reopen
        let mut m1 = close.clone();
        m1.merge(&reopen);
        assert_eq!(m1.epoch, 1);
        assert_eq!(m1.phase, Phase::Open);

        // Merge order 2: reopen into close
        let mut m2 = reopen.clone();
        m2.merge(&close);
        assert_eq!(m2.epoch, 1);
        assert_eq!(m2.phase, Phase::Open);

        // Both orderings produce same result
        assert_eq!(m1, m2);
    }

    // === Semilattice properties ===

    #[test]
    fn semilattice_commutative() {
        let cases = vec![
            (
                EpochPhaseState::with(0, Phase::Open),
                EpochPhaseState::with(0, Phase::Done),
            ),
            (
                EpochPhaseState::with(1, Phase::Doing),
                EpochPhaseState::with(0, Phase::Archived),
            ),
            (
                EpochPhaseState::with(2, Phase::Open),
                EpochPhaseState::with(2, Phase::Doing),
            ),
        ];
        for (a, b) in cases {
            let mut ab = a.clone();
            ab.merge(&b);
            let mut ba = b.clone();
            ba.merge(&a);
            assert_eq!(ab, ba, "commutative failed for {a:?} and {b:?}");
        }
    }

    #[test]
    fn semilattice_associative() {
        let a = EpochPhaseState::with(1, Phase::Open);
        let b = EpochPhaseState::with(0, Phase::Done);
        let c = EpochPhaseState::with(1, Phase::Doing);

        // (a merge b) merge c
        let mut left = a.clone();
        left.merge(&b);
        left.merge(&c);

        // a merge (b merge c)
        let mut bc = b.clone();
        bc.merge(&c);
        let mut right = a.clone();
        right.merge(&bc);

        assert_eq!(left, right);
    }

    #[test]
    fn semilattice_idempotent() {
        let a = EpochPhaseState::with(2, Phase::Doing);
        let mut m = a.clone();
        m.merge(&a);
        assert_eq!(m, a);
    }

    // === Edge cases ===

    #[test]
    fn merge_default_with_default() {
        let mut a = EpochPhaseState::default();
        let b = EpochPhaseState::default();
        a.merge(&b);
        assert_eq!(a, EpochPhaseState::new());
    }

    #[test]
    fn display() {
        let s = EpochPhaseState::with(3, Phase::Done);
        assert_eq!(s.to_string(), "epoch=3 phase=done");
    }

    #[test]
    fn serde_roundtrip() {
        let s = EpochPhaseState::with(5, Phase::Archived);
        let json = serde_json::to_string(&s).unwrap();
        let deserialized: EpochPhaseState = serde_json::from_str(&json).unwrap();
        assert_eq!(s, deserialized);
    }

    #[test]
    fn phase_serde_roundtrip() {
        for phase in Phase::ALL {
            let json = serde_json::to_string(&phase).unwrap();
            let deserialized: Phase = serde_json::from_str(&json).unwrap();
            assert_eq!(phase, deserialized);
        }
    }
}