meerkat-runtime 0.4.12

v9 runtime control-plane for Meerkat agent lifecycle
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
//! §15 InputStateMachine — enforces all mandatory lifecycle transitions.
//!
//! Hard rules:
//! - AppliedPendingConsumption → Queued is REJECTED
//! - Terminal states reject ALL transitions
//! - Every transition is recorded in history

use chrono::Utc;

use crate::input_state::{
    InputAbandonReason, InputLifecycleState, InputState, InputStateHistoryEntry,
    InputTerminalOutcome,
};
use meerkat_core::lifecycle::InputId;

/// Errors from the input state machine.
#[derive(Debug, Clone, thiserror::Error)]
#[non_exhaustive]
pub enum InputStateMachineError {
    /// The transition is not valid from the current state.
    #[error("Invalid transition: {from:?} -> {to:?}")]
    InvalidTransition {
        from: InputLifecycleState,
        to: InputLifecycleState,
    },
    /// The input is already in a terminal state.
    #[error("Input {input_id} is in terminal state {state:?}")]
    TerminalState {
        input_id: InputId,
        state: InputLifecycleState,
    },
}

/// Validates and applies lifecycle state transitions on InputState.
pub struct InputStateMachine;

impl InputStateMachine {
    /// Check if a transition from `from` to `to` is valid per §15.
    pub fn is_valid_transition(from: InputLifecycleState, to: InputLifecycleState) -> bool {
        use InputLifecycleState::{
            Abandoned, Accepted, Applied, AppliedPendingConsumption, Coalesced, Consumed, Queued,
            Staged, Superseded,
        };

        // Terminal states reject all transitions
        if from.is_terminal() {
            return false;
        }

        matches!(
            (from, to),
            // Accepted → Queued, Consumed (Ignore+OnAccept), Superseded, Coalesced, Abandoned
            (Accepted, Queued | Consumed | Superseded | Coalesced | Abandoned)
            // Queued → Staged, Superseded, Coalesced, Abandoned
            | (Queued, Staged | Superseded | Coalesced | Abandoned)
            // Staged → Queued (rollback on run failure), Applied, Superseded, Abandoned
            | (Staged, Queued | Applied | Superseded | Abandoned)
            // Applied → AppliedPendingConsumption, Abandoned
            | (Applied, AppliedPendingConsumption | Abandoned)
            // AppliedPendingConsumption → Consumed, Abandoned
            // NOTE: AppliedPendingConsumption → Queued is REJECTED (§15 hard rule)
            | (AppliedPendingConsumption, Consumed | Abandoned)
        )
    }

    /// Transition the input state, recording history.
    ///
    /// Returns an error if the transition is invalid or the state is terminal.
    pub fn transition(
        state: &mut InputState,
        to: InputLifecycleState,
        reason: Option<String>,
    ) -> Result<(), InputStateMachineError> {
        let from = state.current_state;

        // Check terminal first for better error message
        if from.is_terminal() {
            return Err(InputStateMachineError::TerminalState {
                input_id: state.input_id.clone(),
                state: from,
            });
        }

        if !Self::is_valid_transition(from, to) {
            return Err(InputStateMachineError::InvalidTransition { from, to });
        }

        let now = Utc::now();

        // Record history
        state.history.push(InputStateHistoryEntry {
            timestamp: now,
            from,
            to,
            reason,
        });

        // Update state
        state.current_state = to;
        state.updated_at = now;

        // Set terminal outcome if transitioning to terminal
        if to.is_terminal() && state.terminal_outcome.is_none() {
            state.terminal_outcome = Some(match to {
                InputLifecycleState::Consumed => InputTerminalOutcome::Consumed,
                InputLifecycleState::Abandoned => InputTerminalOutcome::Abandoned {
                    reason: InputAbandonReason::Cancelled,
                },
                // Superseded and Coalesced terminal outcomes are set by the caller
                // via set_terminal_outcome() since they need additional data
                _ => return Ok(()),
            });
        }

        Ok(())
    }

    /// Set the terminal outcome explicitly (for Superseded/Coalesced which need extra data).
    pub fn set_terminal_outcome(state: &mut InputState, outcome: InputTerminalOutcome) {
        state.terminal_outcome = Some(outcome);
    }

    /// Transition to Abandoned with a specific reason.
    pub fn abandon(
        state: &mut InputState,
        reason: InputAbandonReason,
    ) -> Result<(), InputStateMachineError> {
        Self::transition(
            state,
            InputLifecycleState::Abandoned,
            Some(format!("abandoned: {reason:?}")),
        )?;
        state.terminal_outcome = Some(InputTerminalOutcome::Abandoned { reason });
        Ok(())
    }
}

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

    fn new_state() -> InputState {
        InputState::new_accepted(InputId::new())
    }

    // ---- Happy path transitions ----

    #[test]
    fn accepted_to_queued() {
        let mut state = new_state();
        assert!(
            InputStateMachine::transition(
                &mut state,
                InputLifecycleState::Queued,
                Some("policy resolved".into()),
            )
            .is_ok()
        );
        assert_eq!(state.current_state, InputLifecycleState::Queued);
        assert_eq!(state.history.len(), 1);
        assert_eq!(state.history[0].from, InputLifecycleState::Accepted);
        assert_eq!(state.history[0].to, InputLifecycleState::Queued);
    }

    #[test]
    fn queued_to_staged() {
        let mut state = new_state();
        InputStateMachine::transition(&mut state, InputLifecycleState::Queued, None).unwrap();
        assert!(
            InputStateMachine::transition(&mut state, InputLifecycleState::Staged, None,).is_ok()
        );
        assert_eq!(state.current_state, InputLifecycleState::Staged);
    }

    #[test]
    fn staged_to_applied() {
        let mut state = new_state();
        InputStateMachine::transition(&mut state, InputLifecycleState::Queued, None).unwrap();
        InputStateMachine::transition(&mut state, InputLifecycleState::Staged, None).unwrap();
        assert!(
            InputStateMachine::transition(&mut state, InputLifecycleState::Applied, None,).is_ok()
        );
        assert_eq!(state.current_state, InputLifecycleState::Applied);
    }

    #[test]
    fn applied_to_applied_pending_consumption() {
        let mut state = new_state();
        InputStateMachine::transition(&mut state, InputLifecycleState::Queued, None).unwrap();
        InputStateMachine::transition(&mut state, InputLifecycleState::Staged, None).unwrap();
        InputStateMachine::transition(&mut state, InputLifecycleState::Applied, None).unwrap();
        assert!(
            InputStateMachine::transition(
                &mut state,
                InputLifecycleState::AppliedPendingConsumption,
                None,
            )
            .is_ok()
        );
        assert_eq!(
            state.current_state,
            InputLifecycleState::AppliedPendingConsumption
        );
    }

    #[test]
    fn applied_pending_to_consumed() {
        let mut state = new_state();
        InputStateMachine::transition(&mut state, InputLifecycleState::Queued, None).unwrap();
        InputStateMachine::transition(&mut state, InputLifecycleState::Staged, None).unwrap();
        InputStateMachine::transition(&mut state, InputLifecycleState::Applied, None).unwrap();
        InputStateMachine::transition(
            &mut state,
            InputLifecycleState::AppliedPendingConsumption,
            None,
        )
        .unwrap();
        assert!(
            InputStateMachine::transition(&mut state, InputLifecycleState::Consumed, None,).is_ok()
        );
        assert!(state.is_terminal());
        assert!(matches!(
            state.terminal_outcome,
            Some(InputTerminalOutcome::Consumed)
        ));
    }

    #[test]
    fn full_happy_path_history() {
        let mut state = new_state();
        InputStateMachine::transition(&mut state, InputLifecycleState::Queued, None).unwrap();
        InputStateMachine::transition(&mut state, InputLifecycleState::Staged, None).unwrap();
        InputStateMachine::transition(&mut state, InputLifecycleState::Applied, None).unwrap();
        InputStateMachine::transition(
            &mut state,
            InputLifecycleState::AppliedPendingConsumption,
            None,
        )
        .unwrap();
        InputStateMachine::transition(&mut state, InputLifecycleState::Consumed, None).unwrap();
        assert_eq!(state.history.len(), 5);
    }

    // ---- Staged rollback on run failure ----

    #[test]
    fn staged_to_queued_rollback() {
        let mut state = new_state();
        InputStateMachine::transition(&mut state, InputLifecycleState::Queued, None).unwrap();
        InputStateMachine::transition(&mut state, InputLifecycleState::Staged, None).unwrap();
        assert!(
            InputStateMachine::transition(
                &mut state,
                InputLifecycleState::Queued,
                Some("run failed, rollback".into()),
            )
            .is_ok()
        );
        assert_eq!(state.current_state, InputLifecycleState::Queued);
    }

    // ---- §15 HARD RULE: AppliedPendingConsumption → Queued rejected ----

    #[test]
    fn applied_pending_to_queued_rejected() {
        let mut state = new_state();
        InputStateMachine::transition(&mut state, InputLifecycleState::Queued, None).unwrap();
        InputStateMachine::transition(&mut state, InputLifecycleState::Staged, None).unwrap();
        InputStateMachine::transition(&mut state, InputLifecycleState::Applied, None).unwrap();
        InputStateMachine::transition(
            &mut state,
            InputLifecycleState::AppliedPendingConsumption,
            None,
        )
        .unwrap();

        let result = InputStateMachine::transition(&mut state, InputLifecycleState::Queued, None);
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            InputStateMachineError::InvalidTransition { .. }
        ));
        // State unchanged
        assert_eq!(
            state.current_state,
            InputLifecycleState::AppliedPendingConsumption
        );
    }

    // ---- Terminal states reject all transitions ----

    #[test]
    fn consumed_rejects_all() {
        let mut state = new_state();
        InputStateMachine::transition(&mut state, InputLifecycleState::Queued, None).unwrap();
        InputStateMachine::transition(&mut state, InputLifecycleState::Staged, None).unwrap();
        InputStateMachine::transition(&mut state, InputLifecycleState::Applied, None).unwrap();
        InputStateMachine::transition(
            &mut state,
            InputLifecycleState::AppliedPendingConsumption,
            None,
        )
        .unwrap();
        InputStateMachine::transition(&mut state, InputLifecycleState::Consumed, None).unwrap();

        for target in [
            InputLifecycleState::Accepted,
            InputLifecycleState::Queued,
            InputLifecycleState::Staged,
            InputLifecycleState::Applied,
            InputLifecycleState::Consumed,
        ] {
            let result = InputStateMachine::transition(&mut state, target, None);
            assert!(result.is_err());
            assert!(matches!(
                result.unwrap_err(),
                InputStateMachineError::TerminalState { .. }
            ));
        }
    }

    #[test]
    fn superseded_rejects_all() {
        let mut state = new_state();
        InputStateMachine::transition(&mut state, InputLifecycleState::Superseded, None).unwrap();
        assert!(state.is_terminal());

        let result = InputStateMachine::transition(&mut state, InputLifecycleState::Queued, None);
        assert!(result.is_err());
    }

    #[test]
    fn coalesced_rejects_all() {
        let mut state = new_state();
        InputStateMachine::transition(&mut state, InputLifecycleState::Coalesced, None).unwrap();
        assert!(state.is_terminal());

        let result = InputStateMachine::transition(&mut state, InputLifecycleState::Queued, None);
        assert!(result.is_err());
    }

    #[test]
    fn abandoned_rejects_all() {
        let mut state = new_state();
        InputStateMachine::abandon(&mut state, InputAbandonReason::Retired).unwrap();
        assert!(state.is_terminal());

        let result = InputStateMachine::transition(&mut state, InputLifecycleState::Queued, None);
        assert!(result.is_err());
    }

    // ---- Abandon from various states ----

    #[test]
    fn abandon_from_accepted() {
        let mut state = new_state();
        assert!(InputStateMachine::abandon(&mut state, InputAbandonReason::Retired).is_ok());
        assert!(matches!(
            state.terminal_outcome,
            Some(InputTerminalOutcome::Abandoned {
                reason: InputAbandonReason::Retired,
            })
        ));
    }

    #[test]
    fn abandon_from_queued() {
        let mut state = new_state();
        InputStateMachine::transition(&mut state, InputLifecycleState::Queued, None).unwrap();
        assert!(InputStateMachine::abandon(&mut state, InputAbandonReason::Reset).is_ok());
    }

    #[test]
    fn abandon_from_staged() {
        let mut state = new_state();
        InputStateMachine::transition(&mut state, InputLifecycleState::Queued, None).unwrap();
        InputStateMachine::transition(&mut state, InputLifecycleState::Staged, None).unwrap();
        assert!(InputStateMachine::abandon(&mut state, InputAbandonReason::Destroyed).is_ok());
    }

    #[test]
    fn abandon_from_applied() {
        let mut state = new_state();
        InputStateMachine::transition(&mut state, InputLifecycleState::Queued, None).unwrap();
        InputStateMachine::transition(&mut state, InputLifecycleState::Staged, None).unwrap();
        InputStateMachine::transition(&mut state, InputLifecycleState::Applied, None).unwrap();
        assert!(InputStateMachine::abandon(&mut state, InputAbandonReason::Cancelled).is_ok());
    }

    #[test]
    fn abandon_from_applied_pending() {
        let mut state = new_state();
        InputStateMachine::transition(&mut state, InputLifecycleState::Queued, None).unwrap();
        InputStateMachine::transition(&mut state, InputLifecycleState::Staged, None).unwrap();
        InputStateMachine::transition(&mut state, InputLifecycleState::Applied, None).unwrap();
        InputStateMachine::transition(
            &mut state,
            InputLifecycleState::AppliedPendingConsumption,
            None,
        )
        .unwrap();
        assert!(InputStateMachine::abandon(&mut state, InputAbandonReason::Retired).is_ok());
    }

    // ---- Accepted direct to Consumed (Ignore + OnAccept) ----

    #[test]
    fn accepted_to_consumed_ignore_on_accept() {
        let mut state = new_state();
        assert!(
            InputStateMachine::transition(
                &mut state,
                InputLifecycleState::Consumed,
                Some("Ignore + OnAccept".into()),
            )
            .is_ok()
        );
        assert!(state.is_terminal());
    }

    // ---- Invalid transitions ----

    #[test]
    fn accepted_to_staged_invalid() {
        let mut state = new_state();
        let result = InputStateMachine::transition(&mut state, InputLifecycleState::Staged, None);
        assert!(result.is_err());
    }

    #[test]
    fn accepted_to_applied_invalid() {
        let mut state = new_state();
        let result = InputStateMachine::transition(&mut state, InputLifecycleState::Applied, None);
        assert!(result.is_err());
    }

    #[test]
    fn queued_to_applied_invalid() {
        let mut state = new_state();
        InputStateMachine::transition(&mut state, InputLifecycleState::Queued, None).unwrap();
        let result = InputStateMachine::transition(&mut state, InputLifecycleState::Applied, None);
        assert!(result.is_err());
    }

    #[test]
    fn queued_to_consumed_invalid() {
        let mut state = new_state();
        InputStateMachine::transition(&mut state, InputLifecycleState::Queued, None).unwrap();
        let result = InputStateMachine::transition(&mut state, InputLifecycleState::Consumed, None);
        assert!(result.is_err());
    }

    // ---- Set terminal outcome ----

    #[test]
    fn set_terminal_outcome_superseded() {
        let mut state = new_state();
        let superseder = InputId::new();
        InputStateMachine::transition(&mut state, InputLifecycleState::Superseded, None).unwrap();
        InputStateMachine::set_terminal_outcome(
            &mut state,
            InputTerminalOutcome::Superseded {
                superseded_by: superseder,
            },
        );
        assert!(matches!(
            state.terminal_outcome,
            Some(InputTerminalOutcome::Superseded { .. })
        ));
    }

    // ---- History recording ----

    #[test]
    fn history_records_reason() {
        let mut state = new_state();
        InputStateMachine::transition(
            &mut state,
            InputLifecycleState::Queued,
            Some("test reason".into()),
        )
        .unwrap();
        assert_eq!(state.history[0].reason.as_deref(), Some("test reason"));
    }

    #[test]
    fn history_records_timestamps() {
        let mut state = new_state();
        let before = Utc::now();
        InputStateMachine::transition(&mut state, InputLifecycleState::Queued, None).unwrap();
        let after = Utc::now();
        assert!(state.history[0].timestamp >= before);
        assert!(state.history[0].timestamp <= after);
    }
}