jflow-core 0.1.0

Shared types, configuration, and application state for the JANUS trading engine (signals, config, unified metrics, inter-module channels).
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
//! Service lifecycle state machine for the Janus Supervisor.
//!
//! Each service managed by the [`JanusSupervisor`](super::JanusSupervisor)
//! progresses through a well-defined set of states:
//!
//! ```text
//!   ┌──────────┐
//!   │ Starting │──────────────────────────┐
//!   └────┬─────┘                          │
//!        │ run() entered                  │ init error
//!        ▼                                ▼
//!   ┌──────────┐                    ┌────────────┐
//!   │ Running  │───── error ──────▶│ BackingOff │
//!   └────┬─────┘                    └──────┬─────┘
//!        │                                 │
//!        │ cancel / Ok(())                 │ retry
//!        │                                 │
//!        │    ┌────────────────────────────┘
//!        ▼    ▼
//!   ┌──────────┐         ┌────────────┐
//!   │ Stopping │────────▶│ Terminated │
//!   └──────────┘         └────────────┘
//! ```
//!
//! The state machine enforces deterministic behaviour:
//!
//! - **Starting**: The service is initializing resources (connections, channels).
//! - **Running**: The service's `run()` loop is active.
//! - **BackingOff**: The service failed and is waiting for the exponential
//!   backoff timer before the supervisor retries.
//! - **Stopping**: A cancellation signal was received; the service is
//!   finalizing (flushing WAL, closing connections).
//! - **Terminated**: The service has exited cleanly (or the circuit breaker
//!   tripped and the supervisor gave up). Terminal state.
//!
//! The `BackingOff` state prevents the supervisor from tight-looping on a
//! persistent failure, which would burn CPU and flood logs.

use std::fmt;
use std::time::{Duration, Instant};

// ---------------------------------------------------------------------------
// ServicePhase — the raw enum
// ---------------------------------------------------------------------------

/// Lifecycle phase of a supervised service.
///
/// This is a plain enum without associated data; the richer context (timing,
/// error info, attempt counts) lives in [`ServiceLifecycle`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ServicePhase {
    /// The service is initializing (connecting to databases, setting up
    /// channels, etc.).
    Starting,

    /// The service's main `run()` loop is executing.
    Running,

    /// The service failed and is waiting for the backoff timer to expire
    /// before the supervisor attempts a restart.
    BackingOff,

    /// A shutdown signal was received; the service is performing cleanup.
    Stopping,

    /// Terminal state — the service has exited. No further transitions are
    /// valid.
    Terminated,
}

impl fmt::Display for ServicePhase {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Starting => write!(f, "starting"),
            Self::Running => write!(f, "running"),
            Self::BackingOff => write!(f, "backing_off"),
            Self::Stopping => write!(f, "stopping"),
            Self::Terminated => write!(f, "terminated"),
        }
    }
}

impl ServicePhase {
    /// Returns `true` if the service is in a terminal state and will not
    /// transition again.
    pub fn is_terminal(&self) -> bool {
        matches!(self, Self::Terminated)
    }

    /// Returns `true` if the service is considered "alive" (starting,
    /// running, or backing off for a retry).
    pub fn is_alive(&self) -> bool {
        matches!(self, Self::Starting | Self::Running | Self::BackingOff)
    }
}

// ---------------------------------------------------------------------------
// TerminationReason
// ---------------------------------------------------------------------------

/// Why a service reached the `Terminated` phase.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TerminationReason {
    /// The service's `run()` returned `Ok(())` — clean completion.
    Completed,

    /// The supervisor's cancellation token was triggered (graceful shutdown).
    Cancelled,

    /// The circuit breaker tripped after too many consecutive failures.
    CircuitBreakerOpen {
        /// Number of failures observed within the circuit-breaker window.
        failures: u32,
        /// The configured maximum before tripping.
        max_retries: u32,
    },

    /// The service encountered an unrecoverable error and its restart
    /// policy is [`RestartPolicy::Never`](super::RestartPolicy::Never).
    Unrecoverable(String),
}

impl fmt::Display for TerminationReason {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Completed => write!(f, "completed"),
            Self::Cancelled => write!(f, "cancelled"),
            Self::CircuitBreakerOpen {
                failures,
                max_retries,
            } => write!(
                f,
                "circuit breaker open ({failures}/{max_retries} failures)"
            ),
            Self::Unrecoverable(msg) => write!(f, "unrecoverable: {msg}"),
        }
    }
}

// ---------------------------------------------------------------------------
// TransitionError
// ---------------------------------------------------------------------------

/// Error returned when an invalid state transition is attempted.
#[derive(Debug, Clone, thiserror::Error)]
#[error("invalid lifecycle transition: {from} → {to}")]
pub struct TransitionError {
    pub from: ServicePhase,
    pub to: ServicePhase,
}

// ---------------------------------------------------------------------------
// ServiceLifecycle — the state machine
// ---------------------------------------------------------------------------

/// Full lifecycle tracker for a single supervised service.
///
/// Wraps the [`ServicePhase`] enum with timing data, counters, and
/// transition validation logic. The supervisor holds one of these per
/// managed service.
#[derive(Debug, Clone)]
pub struct ServiceLifecycle {
    /// Current phase.
    phase: ServicePhase,

    /// Name of the service (for logging/metrics).
    service_name: String,

    /// When the service first entered `Starting`.
    created_at: Instant,

    /// When the current phase was entered.
    phase_entered_at: Instant,

    /// Total number of times the service has been (re)started.
    start_count: u32,

    /// Total number of failures observed over the service's lifetime.
    total_failures: u32,

    /// The last error message if the service failed.
    last_error: Option<String>,

    /// Why the service terminated (only set in `Terminated` phase).
    termination_reason: Option<TerminationReason>,

    /// Cumulative time spent in the `Running` phase.
    cumulative_running: Duration,

    /// Snapshot of `phase_entered_at` when we last entered `Running`,
    /// used to accumulate `cumulative_running` on exit.
    running_since: Option<Instant>,
}

impl ServiceLifecycle {
    /// Create a new lifecycle tracker in the `Starting` phase.
    pub fn new(service_name: impl Into<String>) -> Self {
        let now = Instant::now();
        Self {
            phase: ServicePhase::Starting,
            service_name: service_name.into(),
            created_at: now,
            phase_entered_at: now,
            start_count: 1,
            total_failures: 0,
            last_error: None,
            termination_reason: None,
            cumulative_running: Duration::ZERO,
            running_since: None,
        }
    }

    // ── Accessors ─────────────────────────────────────────────────────

    /// Current lifecycle phase.
    pub fn phase(&self) -> ServicePhase {
        self.phase
    }

    /// Service name.
    pub fn service_name(&self) -> &str {
        &self.service_name
    }

    /// How long the service has existed (since first `Starting`).
    pub fn age(&self) -> Duration {
        self.created_at.elapsed()
    }

    /// How long the service has been in its current phase.
    pub fn time_in_current_phase(&self) -> Duration {
        self.phase_entered_at.elapsed()
    }

    /// Total number of times the service has been started.
    pub fn start_count(&self) -> u32 {
        self.start_count
    }

    /// Total failures over the service's lifetime.
    pub fn total_failures(&self) -> u32 {
        self.total_failures
    }

    /// The last error message, if any.
    pub fn last_error(&self) -> Option<&str> {
        self.last_error.as_deref()
    }

    /// Why the service terminated (only `Some` when phase is `Terminated`).
    pub fn termination_reason(&self) -> Option<&TerminationReason> {
        self.termination_reason.as_ref()
    }

    /// Cumulative wall-clock time spent in the `Running` phase.
    ///
    /// If the service is currently running, includes time up to *now*.
    pub fn cumulative_running_time(&self) -> Duration {
        let extra = self
            .running_since
            .map(|since| since.elapsed())
            .unwrap_or(Duration::ZERO);
        self.cumulative_running + extra
    }

    // ── Transitions ───────────────────────────────────────────────────

    /// Transition from `Starting` → `Running`.
    ///
    /// Called when the service's `run()` method is entered.
    pub fn transition_to_running(&mut self) -> Result<(), TransitionError> {
        self.validate_transition(ServicePhase::Running)?;
        self.set_phase(ServicePhase::Running);
        self.running_since = Some(Instant::now());
        tracing::info!(
            service = %self.service_name,
            start_count = self.start_count,
            "service entered Running phase"
        );
        Ok(())
    }

    /// Transition from `Running` → `BackingOff`.
    ///
    /// Called when the service's `run()` returns an `Err`.
    pub fn transition_to_backing_off(
        &mut self,
        error: &str,
        backoff_duration: Duration,
    ) -> Result<(), TransitionError> {
        self.validate_transition(ServicePhase::BackingOff)?;
        self.accumulate_running_time();
        self.total_failures += 1;
        self.last_error = Some(error.to_string());
        self.set_phase(ServicePhase::BackingOff);
        tracing::warn!(
            service = %self.service_name,
            error = %error,
            attempt = self.total_failures,
            backoff_ms = backoff_duration.as_millis() as u64,
            "service failed, entering BackingOff phase"
        );
        Ok(())
    }

    /// Transition from `BackingOff` → `Starting` (retry).
    ///
    /// Called when the backoff timer has expired and the supervisor is
    /// about to restart the service.
    pub fn transition_to_restarting(&mut self) -> Result<(), TransitionError> {
        // BackingOff → Starting is a restart
        self.validate_transition(ServicePhase::Starting)?;
        self.start_count += 1;
        self.set_phase(ServicePhase::Starting);
        tracing::info!(
            service = %self.service_name,
            start_count = self.start_count,
            "service restarting (entering Starting phase)"
        );
        Ok(())
    }

    /// Transition to `Stopping` from any alive phase.
    ///
    /// Called when the cancellation token is triggered.
    pub fn transition_to_stopping(&mut self) -> Result<(), TransitionError> {
        self.validate_transition(ServicePhase::Stopping)?;
        self.accumulate_running_time();
        self.set_phase(ServicePhase::Stopping);
        tracing::info!(
            service = %self.service_name,
            "service entering Stopping phase"
        );
        Ok(())
    }

    /// Transition to `Terminated` from `Stopping`, `Running`, `Starting`,
    /// or `BackingOff`.
    ///
    /// This is the terminal state; no further transitions are allowed.
    pub fn transition_to_terminated(
        &mut self,
        reason: TerminationReason,
    ) -> Result<(), TransitionError> {
        self.validate_transition(ServicePhase::Terminated)?;
        self.accumulate_running_time();
        self.termination_reason = Some(reason.clone());
        self.set_phase(ServicePhase::Terminated);
        tracing::info!(
            service = %self.service_name,
            reason = %reason,
            total_starts = self.start_count,
            total_failures = self.total_failures,
            cumulative_running_secs = self.cumulative_running.as_secs_f64(),
            "service terminated"
        );
        Ok(())
    }

    // ── Internal helpers ──────────────────────────────────────────────

    /// Validate that transitioning from the current phase to `target` is
    /// legal.
    fn validate_transition(&self, target: ServicePhase) -> Result<(), TransitionError> {
        let valid = match (self.phase, target) {
            // Starting can go to Running or Terminated (init failure) or Stopping
            (ServicePhase::Starting, ServicePhase::Running) => true,
            (ServicePhase::Starting, ServicePhase::Terminated) => true,
            (ServicePhase::Starting, ServicePhase::Stopping) => true,
            // Also allow Starting → BackingOff for init errors that are retryable
            (ServicePhase::Starting, ServicePhase::BackingOff) => true,

            // Running can go to BackingOff (failure), Stopping, or Terminated
            (ServicePhase::Running, ServicePhase::BackingOff) => true,
            (ServicePhase::Running, ServicePhase::Stopping) => true,
            (ServicePhase::Running, ServicePhase::Terminated) => true,

            // BackingOff can go to Starting (retry), Stopping, or Terminated
            (ServicePhase::BackingOff, ServicePhase::Starting) => true,
            (ServicePhase::BackingOff, ServicePhase::Stopping) => true,
            (ServicePhase::BackingOff, ServicePhase::Terminated) => true,

            // Stopping can only go to Terminated
            (ServicePhase::Stopping, ServicePhase::Terminated) => true,

            // Terminated is terminal — nothing is valid
            (ServicePhase::Terminated, _) => false,

            // Everything else is invalid
            _ => false,
        };

        if valid {
            Ok(())
        } else {
            Err(TransitionError {
                from: self.phase,
                to: target,
            })
        }
    }

    /// Set the phase and update `phase_entered_at`.
    fn set_phase(&mut self, phase: ServicePhase) {
        self.phase = phase;
        self.phase_entered_at = Instant::now();
    }

    /// If we were in Running, accumulate the elapsed running time and
    /// clear the `running_since` marker.
    fn accumulate_running_time(&mut self) {
        if let Some(since) = self.running_since.take() {
            self.cumulative_running += since.elapsed();
        }
    }
}

impl fmt::Display for ServiceLifecycle {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}[{}] starts={} failures={} running={:.1}s",
            self.service_name,
            self.phase,
            self.start_count,
            self.total_failures,
            self.cumulative_running_time().as_secs_f64(),
        )
    }
}

// ---------------------------------------------------------------------------
// Serializable snapshot for API / metrics
// ---------------------------------------------------------------------------

/// A point-in-time snapshot of a service's lifecycle, suitable for
/// serialization into JSON (e.g., for the `/api/health` endpoint).
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ServiceLifecycleSnapshot {
    pub service_name: String,
    pub phase: ServicePhase,
    pub start_count: u32,
    pub total_failures: u32,
    pub last_error: Option<String>,
    pub cumulative_running_secs: f64,
    pub age_secs: f64,
    pub time_in_phase_secs: f64,
    pub termination_reason: Option<String>,
}

impl From<&ServiceLifecycle> for ServiceLifecycleSnapshot {
    fn from(lc: &ServiceLifecycle) -> Self {
        Self {
            service_name: lc.service_name.clone(),
            phase: lc.phase,
            start_count: lc.start_count,
            total_failures: lc.total_failures,
            last_error: lc.last_error.clone(),
            cumulative_running_secs: lc.cumulative_running_time().as_secs_f64(),
            age_secs: lc.age().as_secs_f64(),
            time_in_phase_secs: lc.time_in_current_phase().as_secs_f64(),
            termination_reason: lc.termination_reason.as_ref().map(|r| r.to_string()),
        }
    }
}

// ===========================================================================
// Tests
// ===========================================================================

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

    #[test]
    fn test_new_lifecycle_starts_in_starting() {
        let lc = ServiceLifecycle::new("test-svc");
        assert_eq!(lc.phase(), ServicePhase::Starting);
        assert_eq!(lc.start_count(), 1);
        assert_eq!(lc.total_failures(), 0);
        assert!(lc.last_error().is_none());
        assert!(lc.termination_reason().is_none());
    }

    #[test]
    fn test_service_name() {
        let lc = ServiceLifecycle::new("data-service");
        assert_eq!(lc.service_name(), "data-service");
    }

    #[test]
    fn test_happy_path_starting_to_running_to_stopping_to_terminated() {
        let mut lc = ServiceLifecycle::new("happy");

        lc.transition_to_running().unwrap();
        assert_eq!(lc.phase(), ServicePhase::Running);

        lc.transition_to_stopping().unwrap();
        assert_eq!(lc.phase(), ServicePhase::Stopping);

        lc.transition_to_terminated(TerminationReason::Cancelled)
            .unwrap();
        assert_eq!(lc.phase(), ServicePhase::Terminated);
        assert_eq!(lc.termination_reason(), Some(&TerminationReason::Cancelled));
    }

    #[test]
    fn test_failure_and_restart_cycle() {
        let mut lc = ServiceLifecycle::new("flaky");

        // Start → Run → Fail (BackingOff) → Restart (Starting) → Run
        lc.transition_to_running().unwrap();
        assert_eq!(lc.start_count(), 1);

        lc.transition_to_backing_off("connection refused", Duration::from_millis(200))
            .unwrap();
        assert_eq!(lc.phase(), ServicePhase::BackingOff);
        assert_eq!(lc.total_failures(), 1);
        assert_eq!(lc.last_error(), Some("connection refused"));

        lc.transition_to_restarting().unwrap();
        assert_eq!(lc.phase(), ServicePhase::Starting);
        assert_eq!(lc.start_count(), 2);

        lc.transition_to_running().unwrap();
        assert_eq!(lc.phase(), ServicePhase::Running);
    }

    #[test]
    fn test_circuit_breaker_termination() {
        let mut lc = ServiceLifecycle::new("breaker");

        lc.transition_to_running().unwrap();
        lc.transition_to_backing_off("error 1", Duration::from_millis(100))
            .unwrap();

        lc.transition_to_terminated(TerminationReason::CircuitBreakerOpen {
            failures: 10,
            max_retries: 10,
        })
        .unwrap();

        assert_eq!(lc.phase(), ServicePhase::Terminated);
        assert!(matches!(
            lc.termination_reason(),
            Some(TerminationReason::CircuitBreakerOpen { .. })
        ));
    }

    #[test]
    fn test_completed_termination_from_running() {
        let mut lc = ServiceLifecycle::new("one-shot");

        lc.transition_to_running().unwrap();
        lc.transition_to_terminated(TerminationReason::Completed)
            .unwrap();

        assert_eq!(lc.phase(), ServicePhase::Terminated);
        assert_eq!(lc.termination_reason(), Some(&TerminationReason::Completed));
    }

    #[test]
    fn test_invalid_transition_terminated_to_anything() {
        let mut lc = ServiceLifecycle::new("dead");

        lc.transition_to_running().unwrap();
        lc.transition_to_terminated(TerminationReason::Completed)
            .unwrap();

        // Any further transition should fail
        assert!(lc.transition_to_running().is_err());
        assert!(lc.transition_to_stopping().is_err());
        assert!(
            lc.transition_to_terminated(TerminationReason::Cancelled)
                .is_err()
        );
        assert!(lc.transition_to_restarting().is_err());
    }

    #[test]
    fn test_invalid_transition_running_to_starting() {
        let mut lc = ServiceLifecycle::new("bad");

        lc.transition_to_running().unwrap();

        // Running → Starting is not valid (must go through BackingOff first)
        let err = lc.transition_to_restarting().unwrap_err();
        assert_eq!(err.from, ServicePhase::Running);
        assert_eq!(err.to, ServicePhase::Starting);
    }

    #[test]
    fn test_stopping_from_backing_off() {
        let mut lc = ServiceLifecycle::new("interrupted");

        lc.transition_to_running().unwrap();
        lc.transition_to_backing_off("timeout", Duration::from_secs(5))
            .unwrap();

        // Shutdown arrives while backing off
        lc.transition_to_stopping().unwrap();
        assert_eq!(lc.phase(), ServicePhase::Stopping);

        lc.transition_to_terminated(TerminationReason::Cancelled)
            .unwrap();
        assert_eq!(lc.phase(), ServicePhase::Terminated);
    }

    #[test]
    fn test_starting_directly_to_terminated() {
        let mut lc = ServiceLifecycle::new("init-fail");

        // If init fails catastrophically, we can go straight to Terminated
        lc.transition_to_terminated(TerminationReason::Unrecoverable(
            "missing config".to_string(),
        ))
        .unwrap();
        assert_eq!(lc.phase(), ServicePhase::Terminated);
    }

    #[test]
    fn test_starting_to_backing_off() {
        let mut lc = ServiceLifecycle::new("init-retry");

        // Init fails but is retryable
        lc.transition_to_backing_off("db connect timeout", Duration::from_millis(500))
            .unwrap();
        assert_eq!(lc.phase(), ServicePhase::BackingOff);
        assert_eq!(lc.total_failures(), 1);
    }

    #[test]
    fn test_phase_display() {
        assert_eq!(ServicePhase::Starting.to_string(), "starting");
        assert_eq!(ServicePhase::Running.to_string(), "running");
        assert_eq!(ServicePhase::BackingOff.to_string(), "backing_off");
        assert_eq!(ServicePhase::Stopping.to_string(), "stopping");
        assert_eq!(ServicePhase::Terminated.to_string(), "terminated");
    }

    #[test]
    fn test_phase_is_terminal() {
        assert!(!ServicePhase::Starting.is_terminal());
        assert!(!ServicePhase::Running.is_terminal());
        assert!(!ServicePhase::BackingOff.is_terminal());
        assert!(!ServicePhase::Stopping.is_terminal());
        assert!(ServicePhase::Terminated.is_terminal());
    }

    #[test]
    fn test_phase_is_alive() {
        assert!(ServicePhase::Starting.is_alive());
        assert!(ServicePhase::Running.is_alive());
        assert!(ServicePhase::BackingOff.is_alive());
        assert!(!ServicePhase::Stopping.is_alive());
        assert!(!ServicePhase::Terminated.is_alive());
    }

    #[test]
    fn test_lifecycle_display() {
        let lc = ServiceLifecycle::new("display-test");
        let display = format!("{lc}");
        assert!(display.contains("display-test"));
        assert!(display.contains("starting"));
        assert!(display.contains("starts=1"));
        assert!(display.contains("failures=0"));
    }

    #[test]
    fn test_snapshot_from_lifecycle() {
        let mut lc = ServiceLifecycle::new("snapshot-svc");
        lc.transition_to_running().unwrap();
        lc.transition_to_backing_off("oops", Duration::from_millis(100))
            .unwrap();

        let snap = ServiceLifecycleSnapshot::from(&lc);
        assert_eq!(snap.service_name, "snapshot-svc");
        assert_eq!(snap.phase, ServicePhase::BackingOff);
        assert_eq!(snap.start_count, 1);
        assert_eq!(snap.total_failures, 1);
        assert_eq!(snap.last_error.as_deref(), Some("oops"));
        assert!(snap.termination_reason.is_none());
        assert!(snap.age_secs >= 0.0);
    }

    #[test]
    fn test_termination_reason_display() {
        assert_eq!(TerminationReason::Completed.to_string(), "completed");
        assert_eq!(TerminationReason::Cancelled.to_string(), "cancelled");
        assert_eq!(
            TerminationReason::CircuitBreakerOpen {
                failures: 5,
                max_retries: 5
            }
            .to_string(),
            "circuit breaker open (5/5 failures)"
        );
        assert_eq!(
            TerminationReason::Unrecoverable("bad config".into()).to_string(),
            "unrecoverable: bad config"
        );
    }

    #[test]
    fn test_transition_error_display() {
        let err = TransitionError {
            from: ServicePhase::Terminated,
            to: ServicePhase::Running,
        };
        assert_eq!(
            err.to_string(),
            "invalid lifecycle transition: terminated → running"
        );
    }

    #[test]
    fn test_multiple_failure_cycles_accumulate() {
        let mut lc = ServiceLifecycle::new("multi-fail");

        for i in 1..=5 {
            if lc.phase() == ServicePhase::Starting && i > 1 {
                // After restarting
            }
            lc.transition_to_running().unwrap();
            lc.transition_to_backing_off(
                &format!("error {i}"),
                Duration::from_millis(100 * i as u64),
            )
            .unwrap();
            if i < 5 {
                lc.transition_to_restarting().unwrap();
            }
        }

        assert_eq!(lc.total_failures(), 5);
        assert_eq!(lc.start_count(), 5);
        assert_eq!(lc.last_error(), Some("error 5"));
    }

    #[test]
    fn test_stopping_from_starting() {
        let mut lc = ServiceLifecycle::new("early-stop");

        // Shutdown arrives before the service even starts running
        lc.transition_to_stopping().unwrap();
        assert_eq!(lc.phase(), ServicePhase::Stopping);

        lc.transition_to_terminated(TerminationReason::Cancelled)
            .unwrap();
    }
}