Skip to main content

ralph_core/event_loop/
loop_state.rs

1//! Loop state tracking for the event loop.
2//!
3//! This module contains the `LoopState` struct that tracks the current
4//! state of the orchestration loop including iteration count, failures,
5//! timing, and hat activation tracking.
6
7use ralph_proto::HatId;
8use std::collections::{HashMap, HashSet};
9use std::time::{Duration, Instant};
10
11/// Current state of the event loop.
12#[derive(Debug)]
13pub struct LoopState {
14    /// Current iteration number (1-indexed).
15    pub iteration: u32,
16    /// Number of consecutive failures.
17    pub consecutive_failures: u32,
18    /// Cumulative cost in USD (if tracked).
19    pub cumulative_cost: f64,
20    /// When the loop started.
21    pub started_at: Instant,
22    /// The last hat that executed.
23    pub last_hat: Option<HatId>,
24    /// Consecutive blocked events from the same hat.
25    pub consecutive_blocked: u32,
26    /// Hat that emitted the last blocked event.
27    pub last_blocked_hat: Option<HatId>,
28    /// Per-task block counts for task-level thrashing detection.
29    pub task_block_counts: HashMap<String, u32>,
30    /// Tasks that have been abandoned after 3+ blocks.
31    pub abandoned_tasks: Vec<String>,
32    /// Count of times planner dispatched an already-abandoned task.
33    pub abandoned_task_redispatches: u32,
34    /// Consecutive malformed JSONL lines encountered (for validation backpressure).
35    pub consecutive_malformed_events: u32,
36    /// Whether a completion event has been observed in JSONL.
37    pub completion_requested: bool,
38
39    /// Per-hat activation counts (used for max_activations).
40    pub hat_activation_counts: HashMap<HatId, u32>,
41
42    /// Hats for which `<hat_id>.exhausted` has been emitted.
43    pub exhausted_hats: HashSet<HatId>,
44
45    /// When the last Telegram check-in message was sent.
46    /// `None` means no check-in has been sent yet.
47    pub last_checkin_at: Option<Instant>,
48}
49
50impl Default for LoopState {
51    fn default() -> Self {
52        Self {
53            iteration: 0,
54            consecutive_failures: 0,
55            cumulative_cost: 0.0,
56            started_at: Instant::now(),
57            last_hat: None,
58            consecutive_blocked: 0,
59            last_blocked_hat: None,
60            task_block_counts: HashMap::new(),
61            abandoned_tasks: Vec::new(),
62            abandoned_task_redispatches: 0,
63            consecutive_malformed_events: 0,
64            completion_requested: false,
65            hat_activation_counts: HashMap::new(),
66            exhausted_hats: HashSet::new(),
67            last_checkin_at: None,
68        }
69    }
70}
71
72impl LoopState {
73    /// Creates a new loop state.
74    pub fn new() -> Self {
75        Self::default()
76    }
77
78    /// Returns the elapsed time since the loop started.
79    pub fn elapsed(&self) -> Duration {
80        self.started_at.elapsed()
81    }
82}