1use chrono::{DateTime, Utc};
4use serde::{Deserialize, Serialize};
5
6use crate::error::TaskStoreError;
7
8#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
10pub struct Plan {
11 pub name: String,
12 pub description: Option<String>,
13 pub status: PlanStatus,
14 pub created_at: DateTime<Utc>,
15 pub created_by: String,
16 pub archived_at: Option<DateTime<Utc>>,
17 #[serde(default, skip_serializing_if = "Option::is_none")]
22 pub summary: Option<String>,
23 #[serde(default, skip_serializing_if = "Option::is_none")]
25 pub closed_at: Option<DateTime<Utc>>,
26 #[serde(default, skip_serializing_if = "Option::is_none")]
28 pub closed_by: Option<String>,
29}
30
31pub const MAX_NOTES_BYTES: usize = 16 * 1024;
39
40#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
42pub struct Task {
43 pub id: TaskId,
44 pub title: String,
45 #[serde(default, skip_serializing_if = "Option::is_none")]
50 pub notes: Option<String>,
51 pub status: TaskStatus,
52 pub priority: Priority,
53 pub parent_id: Option<TaskId>,
54 #[serde(default)]
55 pub blocked_by: Vec<TaskId>,
56 pub created_at: DateTime<Utc>,
57 pub created_by: String,
58 pub started_at: Option<DateTime<Utc>>,
59 pub started_by: Option<String>,
60 pub completed_at: Option<DateTime<Utc>>,
61 pub completed_by: Option<String>,
62 pub proof: Option<Proof>,
63 pub abandoned_at: Option<DateTime<Utc>>,
64 pub abandoned_reason: Option<String>,
65 #[serde(default, skip_serializing_if = "Option::is_none")]
66 pub assigned_to: Option<String>,
67 #[serde(default, skip_serializing_if = "Option::is_none")]
73 pub payload: Option<serde_json::Value>,
74 #[serde(default, skip_serializing_if = "Option::is_none")]
79 pub parent_change: Option<String>,
80 #[serde(default, skip_serializing_if = "Option::is_none")]
84 pub on_complete: Option<OnCompleteHook>,
85}
86
87#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
92#[serde(tag = "kind", rename_all = "snake_case")]
93pub enum OnCompleteHook {
94 PromoteChange,
99 Named { name: String },
103}
104
105#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
106#[serde(rename_all = "snake_case")]
107pub enum PlanStatus {
108 Active,
109 Completed,
110 Archived,
111}
112
113#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
114#[serde(rename_all = "snake_case")]
115pub enum TaskStatus {
116 Pending,
117 InProgress,
118 Done,
119 Abandoned,
120}
121
122impl TaskStatus {
123 pub fn is_terminal(self) -> bool {
124 matches!(self, TaskStatus::Done | TaskStatus::Abandoned)
125 }
126}
127
128#[derive(
129 Debug, Clone, Copy, Default, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize,
130)]
131#[serde(rename_all = "snake_case")]
132pub enum Priority {
133 Low,
134 #[default]
135 Medium,
136 High,
137 Critical,
138}
139
140#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
142pub struct TaskId(pub String);
143
144impl TaskId {
145 pub fn new(n: u32) -> Self {
147 Self(format!("t-{:03}", n))
148 }
149
150 pub fn number(&self) -> Result<u32, TaskStoreError> {
152 self.0
153 .strip_prefix("t-")
154 .and_then(|s| s.parse::<u32>().ok())
155 .ok_or_else(|| TaskStoreError::InvalidTaskId(self.0.clone()))
156 }
157
158 pub fn as_str(&self) -> &str {
160 &self.0
161 }
162}
163
164impl std::fmt::Display for TaskId {
165 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
166 f.write_str(&self.0)
167 }
168}
169
170#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
172pub struct Proof {
173 pub kind: ProofKind,
174 pub value: String,
175 pub note: Option<String>,
176}
177
178impl Proof {
179 pub fn commit(sha: impl Into<String>) -> Self {
180 Self {
181 kind: ProofKind::Commit,
182 value: sha.into(),
183 note: None,
184 }
185 }
186
187 pub fn file(path: impl Into<String>) -> Self {
188 Self {
189 kind: ProofKind::File,
190 value: path.into(),
191 note: None,
192 }
193 }
194
195 pub fn test(name: impl Into<String>) -> Self {
196 Self {
197 kind: ProofKind::Test,
198 value: name.into(),
199 note: None,
200 }
201 }
202
203 pub fn text(value: impl Into<String>) -> Self {
204 Self {
205 kind: ProofKind::Text,
206 value: value.into(),
207 note: None,
208 }
209 }
210
211 pub fn with_note(mut self, note: impl Into<String>) -> Self {
212 self.note = Some(note.into());
213 self
214 }
215}
216
217#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
218#[serde(rename_all = "snake_case")]
219pub enum ProofKind {
220 Commit,
222 File,
224 Test,
226 Text,
228}
229
230#[cfg(test)]
231mod tests {
232 use super::*;
233
234 #[test]
235 fn task_id_roundtrip() {
236 let id = TaskId::new(42);
237 assert_eq!(id.as_str(), "t-042");
238 assert_eq!(id.number().unwrap(), 42);
239 }
240
241 #[test]
242 fn task_id_padding() {
243 assert_eq!(TaskId::new(1).as_str(), "t-001");
244 assert_eq!(TaskId::new(999).as_str(), "t-999");
245 assert_eq!(TaskId::new(1000).as_str(), "t-1000");
246 }
247
248 #[test]
249 fn task_id_parse_invalid() {
250 assert!(TaskId("nope".to_string()).number().is_err());
251 assert!(TaskId("t-abc".to_string()).number().is_err());
252 }
253
254 #[test]
255 fn priority_ordering() {
256 assert!(Priority::Critical > Priority::High);
257 assert!(Priority::High > Priority::Medium);
258 assert!(Priority::Medium > Priority::Low);
259 }
260
261 #[test]
262 fn task_status_terminal() {
263 assert!(TaskStatus::Done.is_terminal());
264 assert!(TaskStatus::Abandoned.is_terminal());
265 assert!(!TaskStatus::Pending.is_terminal());
266 assert!(!TaskStatus::InProgress.is_terminal());
267 }
268
269 #[test]
270 fn plan_serde_roundtrip() {
271 let plan = Plan {
272 name: "website-v2".to_string(),
273 description: Some("Brand pivot".to_string()),
274 status: PlanStatus::Active,
275 created_at: Utc::now(),
276 created_by: "claude-code".to_string(),
277 archived_at: None,
278 summary: None,
279 closed_at: None,
280 closed_by: None,
281 };
282 let json = serde_json::to_value(&plan).unwrap();
283 let back: Plan = serde_json::from_value(json).unwrap();
284 assert_eq!(plan, back);
285 }
286
287 #[test]
288 fn task_serde_roundtrip() {
289 let task = Task {
290 id: TaskId::new(1),
291 title: "Rewrite hero".to_string(),
292 notes: Some("Keep the old copy.\nLink the brand doc.".to_string()),
293 status: TaskStatus::Pending,
294 priority: Priority::High,
295 parent_id: None,
296 blocked_by: vec![TaskId::new(2)],
297 created_at: Utc::now(),
298 created_by: "claude-code".to_string(),
299 started_at: None,
300 started_by: None,
301 completed_at: None,
302 completed_by: None,
303 proof: None,
304 abandoned_at: None,
305 abandoned_reason: None,
306 assigned_to: Some("codex".to_string()),
307 payload: None,
308 parent_change: None,
309 on_complete: None,
310 };
311 let json = serde_json::to_value(&task).unwrap();
312 let back: Task = serde_json::from_value(json).unwrap();
313 assert_eq!(task, back);
314 }
315
316 #[test]
317 fn a_task_stored_before_notes_reads_with_none_and_writes_back_unchanged() {
318 let stored = serde_json::json!({
319 "id": "t-001", "title": "Rewrite hero", "status": "pending",
320 "priority": "high", "parent_id": null, "blocked_by": [],
321 "created_at": "2026-10-01T00:00:00Z", "created_by": "claude-code",
322 "started_at": null, "started_by": null, "completed_at": null,
323 "completed_by": null, "proof": null, "abandoned_at": null,
324 "abandoned_reason": null,
325 });
326 let task: Task = serde_json::from_value(stored.clone()).unwrap();
327 assert_eq!(task.notes, None);
328 assert_eq!(serde_json::to_value(&task).unwrap(), stored);
331 }
332
333 #[test]
334 fn proof_constructors() {
335 let p = Proof::commit("abc123").with_note("verified");
336 assert_eq!(p.kind, ProofKind::Commit);
337 assert_eq!(p.value, "abc123");
338 assert_eq!(p.note.as_deref(), Some("verified"));
339 }
340}