1use serde::{Deserialize, Serialize};
10
11#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
13#[cfg_attr(feature = "schema-export", derive(schemars::JsonSchema))]
14#[serde(rename_all = "snake_case")]
15pub enum GoalState {
16 Active,
18 Paused,
20 Complete,
22 Blocked,
24 BudgetLimited,
26 #[serde(other)]
28 Unknown,
29}
30
31impl GoalState {
32 #[must_use]
34 pub const fn as_str(self) -> &'static str {
35 match self {
36 Self::Active => "active",
37 Self::Paused => "paused",
38 Self::Complete => "complete",
39 Self::Blocked => "blocked",
40 Self::BudgetLimited => "budget_limited",
41 Self::Unknown => "unknown",
42 }
43 }
44}
45
46#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
48#[cfg_attr(feature = "schema-export", derive(schemars::JsonSchema))]
49pub struct GoalSnapshot {
50 pub goal_id: String,
52 pub state: GoalState,
54 pub objective: String,
56 pub elapsed_ms: u64,
58 pub tokens_used: i64,
60 #[serde(default, skip_serializing_if = "Option::is_none")]
62 pub token_budget: Option<i64>,
63 #[serde(default, skip_serializing_if = "Option::is_none")]
65 pub pause_message: Option<String>,
66}
67
68#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
70#[cfg_attr(feature = "schema-export", derive(schemars::JsonSchema))]
71pub struct GoalUpdatedEvent {
72 #[serde(default, skip_serializing_if = "Option::is_none")]
74 pub goal: Option<GoalSnapshot>,
75}
76
77impl GoalSnapshot {
78 #[must_use]
80 pub fn elapsed_label(&self) -> String {
81 Self::elapsed_label_for(self.elapsed_ms)
82 }
83
84 #[must_use]
87 pub fn elapsed_label_for(elapsed_ms: u64) -> String {
88 let secs = elapsed_ms / 1000;
89 if secs < 60 {
90 return format!("{secs}s");
91 }
92 let mins = secs / 60;
93 if mins < 60 {
94 return format!("{mins}m {}s", secs % 60);
95 }
96 let hours = mins / 60;
97 if hours < 24 {
98 return format!("{hours}h {}m", mins % 60);
99 }
100 format!("{}d {}h", hours / 24, hours % 24)
101 }
102
103 #[must_use]
106 pub fn short_objective(&self, max_chars: usize) -> String {
107 if self.objective.chars().count() <= max_chars {
108 return self.objective.clone();
109 }
110 let head: String = self.objective.chars().take(max_chars).collect();
111 format!("{}…", head.trim_end())
112 }
113
114 #[must_use]
116 pub fn tokens_label(&self) -> String {
117 match self.token_budget {
118 Some(limit) => format!("{}/{} tokens", self.tokens_used, limit),
119 None => format!("{} tokens", self.tokens_used),
120 }
121 }
122}
123
124#[cfg(test)]
125mod tests {
126 use super::*;
127 use crate::ThreadEvent;
128
129 fn snapshot() -> GoalSnapshot {
130 GoalSnapshot {
131 goal_id: "goal-1".to_string(),
132 state: GoalState::Active,
133 objective: "Ship it".to_string(),
134 elapsed_ms: 11_000,
135 tokens_used: 250,
136 token_budget: Some(100_000),
137 pause_message: None,
138 }
139 }
140
141 #[test]
142 fn goal_updated_event_round_trips_some_and_none() {
143 let event = ThreadEvent::GoalUpdated(Box::new(GoalUpdatedEvent { goal: Some(snapshot()) }));
144 let json = serde_json::to_string(&event).unwrap();
145 assert!(json.contains("\"type\":\"goal.updated\""), "unexpected tag: {json}");
146 let restored: ThreadEvent = serde_json::from_str(&json).unwrap();
147 assert_eq!(restored, event);
148
149 let cleared = ThreadEvent::GoalUpdated(Box::new(GoalUpdatedEvent { goal: None }));
150 let json = serde_json::to_string(&cleared).unwrap();
151 assert!(!json.contains("pause_message"), "cleared payload must stay minimal: {json}");
152 let restored: ThreadEvent = serde_json::from_str(&json).unwrap();
153 assert_eq!(restored, cleared);
154 }
155
156 #[test]
157 fn unknown_goal_state_decodes_to_unknown() {
158 let state: GoalState = serde_json::from_str("\"quarantined\"").unwrap();
159 assert_eq!(state, GoalState::Unknown);
160 assert_eq!(GoalState::Paused.as_str(), "paused");
161 }
162
163 #[test]
164 fn elapsed_label_for_matches_the_method() {
165 for ms in [0, 999, 1000, 59_000, 60_000, 1_031_000, 3_600_000, 90_000_000] {
166 let snapshot = GoalSnapshot { elapsed_ms: ms, ..snapshot() };
167 assert_eq!(GoalSnapshot::elapsed_label_for(ms), snapshot.elapsed_label(), "{ms}ms");
168 }
169 }
170
171 #[test]
172 fn elapsed_label_matches_both_sides_of_each_boundary() {
173 for (ms, expected) in [
174 (0, "0s"),
175 (59_000, "59s"),
176 (60_000, "1m 0s"),
177 (1_031_000, "17m 11s"),
178 (3_599_000, "59m 59s"),
179 (3_600_000, "1h 0m"),
180 (86_399_000, "23h 59m"),
181 (86_400_000, "1d 0h"),
182 (90_000_000, "1d 1h"),
183 ] {
184 let snapshot = GoalSnapshot { elapsed_ms: ms, ..snapshot() };
185 assert_eq!(snapshot.elapsed_label(), expected, "{ms}ms");
186 }
187 }
188
189 #[test]
190 fn short_objective_truncates_on_char_boundaries() {
191 let short = GoalSnapshot { objective: "Ship it".into(), ..snapshot() };
192 assert_eq!(short.short_objective(60), "Ship it");
193 assert_eq!(short.short_objective(7), "Ship it");
194
195 let accented = GoalSnapshot { objective: "héllo wörld".into(), ..snapshot() };
196 assert_eq!(accented.short_objective(5), "héllo…");
197
198 let emoji = GoalSnapshot { objective: "a😀b".into(), ..snapshot() };
199 assert_eq!(emoji.short_objective(2), "a😀…");
200 assert_eq!(emoji.short_objective(3), "a😀b");
201 }
202
203 #[test]
204 fn tokens_label_shows_budget_only_when_set() {
205 let unbounded = GoalSnapshot { tokens_used: 250, token_budget: None, ..snapshot() };
206 assert_eq!(unbounded.tokens_label(), "250 tokens");
207 let bounded = GoalSnapshot {
208 tokens_used: 250,
209 token_budget: Some(100_000),
210 ..snapshot()
211 };
212 assert_eq!(bounded.tokens_label(), "250/100000 tokens");
213 }
214}