Skip to main content

luft_core/
state.rs

1//! Progress persistence and resume for long-running workflows.
2//!
3//! This module implements checkpointing and recovery for dynamic workflows.
4//! Progress is saved as the run goes, so a job interrupted by a restart can resume.
5//!
6//! Key features:
7//! - Event log persistence (JSONL)
8//! - Agent result caching
9//! - Resume from last checkpoint
10//! - Run state management
11
12use crate::contract::event::AgentEvent;
13use crate::contract::finding::Finding;
14use crate::contract::ids::{AgentId, PhaseId, RunId};
15use serde::{Deserialize, Serialize};
16use std::collections::HashMap;
17use std::fs::{self, File, OpenOptions};
18use std::io::{BufRead, BufReader, Write};
19use std::path::{Path, PathBuf};
20use std::sync::{Arc, RwLock};
21use std::time::{SystemTime, UNIX_EPOCH};
22
23/// Run state persisted to disk.
24#[derive(Debug, Clone, Serialize, Deserialize)]
25pub struct RunCheckpoint {
26    pub run_id: RunId,
27    pub task: String,
28    pub status: CheckpointStatus,
29    pub current_phase: u32,
30    pub completed_phases: Vec<PhaseSummary>,
31    pub agent_results: HashMap<AgentId, AgentResultCache>,
32    pub findings: Vec<Finding>,
33    pub total_tokens: u64,
34    pub created_at: u64,
35    pub updated_at: u64,
36    #[serde(default)]
37    pub completed_spans: Vec<PhaseSpanSummary>,
38    #[serde(default)]
39    pub workflow_meta: Option<serde_json::Value>,
40    /// Every agent_id that has received an `AgentStarted` event, in arrival
41    /// order. Used to compute "running" = started − done.
42    #[serde(default)]
43    pub started_agent_ids: Vec<AgentId>,
44}
45
46#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
47#[serde(rename_all = "lowercase")]
48pub enum CheckpointStatus {
49    Running,
50    Completed,
51    Failed,
52    Cancelled,
53}
54
55impl std::fmt::Display for CheckpointStatus {
56    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
57        let s = match self {
58            CheckpointStatus::Running => "Running",
59            CheckpointStatus::Completed => "Completed",
60            CheckpointStatus::Failed => "Failed",
61            CheckpointStatus::Cancelled => "Cancelled",
62        };
63        f.write_str(s)
64    }
65}
66
67#[derive(Debug, Clone, Serialize, Deserialize)]
68pub struct PhaseSummary {
69    pub phase_id: PhaseId,
70    pub label: String,
71    pub planned: usize,
72    pub ok: usize,
73    pub failed: usize,
74    #[serde(default)]
75    pub description: Option<String>,
76    #[serde(default)]
77    pub role: Option<String>,
78}
79
80#[derive(Debug, Clone, Serialize, Deserialize)]
81pub struct AgentResultCache {
82    pub agent_id: AgentId,
83    pub phase_id: PhaseId,
84    pub status: String,
85    pub output: serde_json::Value,
86    pub findings: Vec<Finding>,
87    pub tokens: u64,
88    pub completed_at: u64,
89    /// Deterministic cache key hash for resume lookups.
90    /// Populated by JournalStore::cache_agent(); None for legacy checkpoints.
91    #[serde(default)]
92    pub cache_key_hash: Option<String>,
93    #[serde(default)]
94    pub description: Option<String>,
95    #[serde(default)]
96    pub role: Option<String>,
97}
98
99#[derive(Debug, Clone, Serialize, Deserialize)]
100pub struct PhaseSpanSummary {
101    pub id: u32,
102    pub name: String,
103    pub parent_id: Option<u32>,
104    pub depth: u32,
105    pub elapsed_ms: u64,
106    pub completed_at: u64,
107}
108
109/// Persistence store for a single run.
110#[derive(Debug)]
111pub struct RunStore {
112    run_dir: PathBuf,
113    checkpoint: RwLock<Option<RunCheckpoint>>,
114    events_file: RwLock<Option<File>>,
115}
116
117impl RunStore {
118    /// Create or open a run store at the given path.
119    pub fn new(run_dir: &Path) -> Result<Arc<Self>, std::io::Error> {
120        tracing::debug!(path = %run_dir.display(), "creating RunStore");
121        fs::create_dir_all(run_dir)?;
122
123        let store = Arc::new(Self {
124            run_dir: run_dir.to_path_buf(),
125            checkpoint: RwLock::new(None),
126            events_file: RwLock::new(None),
127        });
128
129        Ok(store)
130    }
131
132    /// Insert or update an agent result in the checkpoint directly.
133    /// Used by JournalStore to persist cache_key_hash before appending the event.
134    pub fn upsert_agent_result(&self, cache: &AgentResultCache) -> Result<(), std::io::Error> {
135        let mut guard = self.checkpoint.write().unwrap();
136        if let Some(ref mut checkpoint) = *guard {
137            checkpoint
138                .agent_results
139                .insert(cache.agent_id, cache.clone());
140            checkpoint.updated_at = current_timestamp();
141            let cp = checkpoint.clone();
142            drop(guard);
143            let cp_path = self.run_dir.join("checkpoint.json");
144            let content = serde_json::to_string_pretty(&cp)
145                .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?;
146            fs::write(&cp_path, content)?;
147        }
148        Ok(())
149    }
150
151    /// Initialize a new run.
152    pub fn init_run(&self, run_id: RunId, task: &str) -> Result<(), std::io::Error> {
153        tracing::info!(%run_id, %task, "initializing run store");
154        let checkpoint = RunCheckpoint {
155            run_id,
156            task: task.to_string(),
157            status: CheckpointStatus::Running,
158            current_phase: 0,
159            completed_phases: vec![],
160            agent_results: HashMap::new(),
161            findings: vec![],
162            total_tokens: 0,
163            created_at: current_timestamp(),
164            updated_at: current_timestamp(),
165            completed_spans: vec![],
166            workflow_meta: None,
167            started_agent_ids: vec![],
168        };
169
170        // Save checkpoint
171        self.save_checkpoint(&checkpoint)?;
172
173        // Open events file
174        let events_path = self.run_dir.join("events.jsonl");
175        let events_file = OpenOptions::new()
176            .create(true)
177            .append(true)
178            .open(events_path)?;
179
180        let mut checkpoint_guard = self.checkpoint.write().unwrap();
181        *checkpoint_guard = Some(checkpoint);
182
183        let mut events_guard = self.events_file.write().unwrap();
184        *events_guard = Some(events_file);
185
186        Ok(())
187    }
188
189    /// Initialize a new run with declarative workflow metadata.
190    pub fn init_run_with_meta(
191        &self,
192        run_id: RunId,
193        task: &str,
194        workflow_meta: serde_json::Value,
195    ) -> Result<(), std::io::Error> {
196        tracing::info!(%run_id, %task, "initializing run store with meta");
197        let checkpoint = RunCheckpoint {
198            run_id,
199            task: task.to_string(),
200            status: CheckpointStatus::Running,
201            current_phase: 0,
202            completed_phases: vec![],
203            agent_results: HashMap::new(),
204            findings: vec![],
205            total_tokens: 0,
206            created_at: current_timestamp(),
207            updated_at: current_timestamp(),
208            completed_spans: vec![],
209            workflow_meta: Some(workflow_meta),
210            started_agent_ids: vec![],
211        };
212
213        self.save_checkpoint(&checkpoint)?;
214
215        let events_path = self.run_dir.join("events.jsonl");
216        let events_file = OpenOptions::new()
217            .create(true)
218            .append(true)
219            .open(events_path)?;
220
221        let mut checkpoint_guard = self.checkpoint.write().unwrap();
222        *checkpoint_guard = Some(checkpoint);
223
224        let mut events_guard = self.events_file.write().unwrap();
225        *events_guard = Some(events_file);
226
227        Ok(())
228    }
229
230    /// Open an existing run for resume.
231    pub fn open_run(&self, _run_id: RunId) -> Result<Option<RunCheckpoint>, std::io::Error> {
232        tracing::debug!(%_run_id, "opening existing run");
233        let checkpoint_path = self.run_dir.join("checkpoint.json");
234
235        if !checkpoint_path.exists() {
236            return Ok(None);
237        }
238
239        let content = fs::read_to_string(&checkpoint_path)?;
240        let checkpoint: RunCheckpoint = serde_json::from_str(&content)
241            .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?;
242
243        // Open events file. Resume appends new events (phase_started, agent_started,
244        // log, agent_done) to the same file; opening read-only here would make every
245        // forwarded event fail with Access is denied (os error 5) and silently drop
246        // observability for the entire resumed run.
247        let events_path = self.run_dir.join("events.jsonl");
248        let events_file = OpenOptions::new()
249            .read(true)
250            .append(true)
251            .open(events_path)?;
252
253        let mut checkpoint_guard = self.checkpoint.write().unwrap();
254        *checkpoint_guard = Some(checkpoint.clone());
255
256        let mut events_guard = self.events_file.write().unwrap();
257        *events_guard = Some(events_file);
258
259        Ok(Some(checkpoint))
260    }
261
262    /// Append an event to the log.
263    pub fn append_event(&self, event: &AgentEvent) -> Result<(), std::io::Error> {
264        let json = serde_json::to_string(event)
265            .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?;
266
267        let mut events_guard = self.events_file.write().unwrap();
268        if let Some(ref mut file) = *events_guard {
269            writeln!(file, "{}", json)?;
270            file.flush()?;
271        }
272
273        // Update checkpoint (this also persists to disk)
274        self.update_from_event(event);
275
276        Ok(())
277    }
278
279    /// Update checkpoint from an event and persist to disk.
280    fn update_from_event(&self, event: &AgentEvent) {
281        let mut checkpoint_guard = self.checkpoint.write().unwrap();
282        if let Some(ref mut checkpoint) = *checkpoint_guard {
283            match event {
284                AgentEvent::AgentDone {
285                    agent_id,
286                    status,
287                    tokens,
288                    ..
289                } => {
290                    let existing = checkpoint.agent_results.get(agent_id);
291                    let cache = AgentResultCache {
292                        agent_id: *agent_id,
293                        phase_id: existing.map(|c| c.phase_id).unwrap_or(0),
294                        status: status.as_str().to_string(),
295                        output: existing
296                            .map(|c| c.output.clone())
297                            .unwrap_or(serde_json::Value::Null),
298                        findings: existing.map(|c| c.findings.clone()).unwrap_or_default(),
299                        tokens: tokens.total(),
300                        completed_at: existing
301                            .map(|c| c.completed_at)
302                            .unwrap_or(current_timestamp()),
303                        cache_key_hash: existing.and_then(|c| c.cache_key_hash.clone()),
304                        description: existing.and_then(|c| c.description.clone()),
305                        role: existing.and_then(|c| c.role.clone()),
306                    };
307                    checkpoint.agent_results.insert(*agent_id, cache);
308                    checkpoint.total_tokens += tokens.total();
309                }
310                AgentEvent::AgentStarted { agent_id, .. } => {
311                    if !checkpoint.started_agent_ids.contains(agent_id) {
312                        checkpoint.started_agent_ids.push(*agent_id);
313                    }
314                }
315                AgentEvent::PhaseDone { phase_id, .. } => {
316                    if *phase_id > 0 {
317                        checkpoint.current_phase = *phase_id;
318                    }
319                }
320                AgentEvent::PhaseSpanDone {
321                    span_id,
322                    name,
323                    parent_id,
324                    depth,
325                    elapsed_ms,
326                    ..
327                } => {
328                    checkpoint.completed_spans.push(PhaseSpanSummary {
329                        id: *span_id,
330                        name: name.clone(),
331                        parent_id: *parent_id,
332                        depth: *depth,
333                        elapsed_ms: *elapsed_ms,
334                        completed_at: current_timestamp(),
335                    });
336                }
337                AgentEvent::RunDone {
338                    status,
339                    total_tokens,
340                    ..
341                } => {
342                    checkpoint.status = match status {
343                        crate::contract::event::RunStatus::Completed => CheckpointStatus::Completed,
344                        crate::contract::event::RunStatus::Failed => CheckpointStatus::Failed,
345                        crate::contract::event::RunStatus::Cancelled => CheckpointStatus::Cancelled,
346                        crate::contract::event::RunStatus::Partial => CheckpointStatus::Running,
347                    };
348                    // Only overwrite if a real total was supplied; otherwise keep
349                    // the figure accumulated from AgentDone events.
350                    let t = total_tokens.total();
351                    if t > 0 {
352                        checkpoint.total_tokens = t;
353                    }
354                }
355                _ => {}
356            }
357            checkpoint.updated_at = current_timestamp();
358
359            // Persist updated checkpoint to disk (write-only, no lock needed - already held)
360            if let Err(e) = self.write_checkpoint_to_disk(checkpoint) {
361                tracing::warn!(error = %e, "failed to save checkpoint");
362            }
363        }
364    }
365
366    /// Write checkpoint to disk without acquiring any locks.
367    fn write_checkpoint_to_disk(&self, checkpoint: &RunCheckpoint) -> Result<(), std::io::Error> {
368        let checkpoint_path = self.run_dir.join("checkpoint.json");
369        let temp_path = self.run_dir.join("checkpoint.json.tmp");
370        let content = serde_json::to_string_pretty(checkpoint)
371            .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?;
372        std::fs::write(&temp_path, &content)?;
373        std::fs::rename(&temp_path, &checkpoint_path)?;
374        Ok(())
375    }
376
377    /// Save checkpoint to disk (public API, acquires lock).
378    pub fn save_checkpoint(&self, checkpoint: &RunCheckpoint) -> Result<(), std::io::Error> {
379        let checkpoint_path = self.run_dir.join("checkpoint.json");
380        let temp_path = self.run_dir.join("checkpoint.json.tmp");
381        let content = serde_json::to_string_pretty(checkpoint)
382            .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?;
383        std::fs::write(&temp_path, &content)?;
384        std::fs::rename(&temp_path, &checkpoint_path)?;
385
386        let mut checkpoint_guard = self.checkpoint.write().unwrap();
387        *checkpoint_guard = Some(checkpoint.clone());
388
389        Ok(())
390    }
391
392    /// Get current checkpoint.
393    pub fn get_checkpoint(&self) -> Option<RunCheckpoint> {
394        let guard = self.checkpoint.read().unwrap();
395        guard.clone()
396    }
397
398    /// Get cached agent results.
399    pub fn get_agent_results(&self) -> HashMap<AgentId, AgentResultCache> {
400        let guard = self.checkpoint.read().unwrap();
401        guard
402            .as_ref()
403            .map(|c| c.agent_results.clone())
404            .unwrap_or_default()
405    }
406
407    /// Get all findings collected so far.
408    pub fn get_findings(&self) -> Vec<Finding> {
409        let guard = self.checkpoint.read().unwrap();
410        guard
411            .as_ref()
412            .map(|c| c.findings.clone())
413            .unwrap_or_default()
414    }
415
416    /// Get event log as a vector.
417    pub fn get_event_log(&self) -> Result<Vec<AgentEvent>, std::io::Error> {
418        let events_path = self.run_dir.join("events.jsonl");
419        let file = File::open(events_path)?;
420        let reader = BufReader::new(file);
421        let mut events = Vec::new();
422
423        for line in reader.lines() {
424            let line = line?;
425            if line.trim().is_empty() {
426                continue;
427            }
428            let event: AgentEvent = serde_json::from_str(&line)
429                .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?;
430            events.push(event);
431        }
432
433        Ok(events)
434    }
435
436    /// Check if a run can be resumed.
437    pub fn can_resume(&self) -> bool {
438        let guard = self.checkpoint.read().unwrap();
439        matches!(
440            guard.as_ref().map(|c| c.status.clone()),
441            Some(CheckpointStatus::Running)
442        )
443    }
444
445    /// Mark run as cancelled.
446    pub fn cancel(&self) -> Result<(), std::io::Error> {
447        tracing::info!("cancelling run");
448        let mut guard = self.checkpoint.write().unwrap();
449        if let Some(ref mut checkpoint) = *guard {
450            checkpoint.status = CheckpointStatus::Cancelled;
451            checkpoint.updated_at = current_timestamp();
452            drop(guard);
453            let guard = self.checkpoint.read().unwrap();
454            if let Some(ref c) = *guard {
455                let checkpoint_path = self.run_dir.join("checkpoint.json");
456                let content = serde_json::to_string_pretty(c)
457                    .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?;
458                fs::write(&checkpoint_path, content)?;
459            }
460        }
461        Ok(())
462    }
463}
464
465/// Get current timestamp.
466fn current_timestamp() -> u64 {
467    SystemTime::now()
468        .duration_since(UNIX_EPOCH)
469        .map(|d| d.as_secs())
470        .unwrap_or(0)
471}
472
473// ============================================================================
474// Global store management
475// ============================================================================
476
477use std::sync::OnceLock;
478
479static RUN_STORES: OnceLock<dashmap::DashMap<String, Arc<RunStore>>> = OnceLock::new();
480
481/// Get or create the global run stores.
482fn get_run_stores() -> &'static dashmap::DashMap<String, Arc<RunStore>> {
483    RUN_STORES.get_or_init(dashmap::DashMap::new)
484}
485
486/// Get or create a run store for a run directory.
487pub fn get_run_store(run_dir_name: &str, base_dir: &Path) -> Result<Arc<RunStore>, std::io::Error> {
488    let stores = get_run_stores();
489
490    if let Some(store) = stores.get(run_dir_name) {
491        return Ok(store.clone());
492    }
493
494    let run_dir = base_dir.join(run_dir_name);
495    let store = RunStore::new(&run_dir)?;
496    stores.insert(run_dir_name.to_string(), store.clone());
497
498    Ok(store)
499}
500
501/// List all run directory names (both new-format and legacy UUID).
502pub fn list_runs(base_dir: &Path) -> Result<Vec<String>, std::io::Error> {
503    if !base_dir.exists() {
504        return Ok(vec![]);
505    }
506
507    let mut run_dirs = Vec::new();
508    for entry in fs::read_dir(base_dir)? {
509        let entry = entry?;
510        let path = entry.path();
511        if path.is_dir() {
512            if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
513                run_dirs.push(name.to_string());
514            }
515        }
516    }
517
518    run_dirs.sort();
519    Ok(run_dirs)
520}
521
522#[cfg(test)]
523mod tests {
524    use super::*;
525    use tempfile::tempdir;
526
527    #[test]
528    fn test_run_store_init() {
529        let dir = tempdir().unwrap();
530        let run_id = uuid::Uuid::now_v7();
531        let store = RunStore::new(dir.path()).unwrap();
532        store.init_run(run_id, "Test task").unwrap();
533
534        let checkpoint = store.get_checkpoint().unwrap();
535        assert_eq!(checkpoint.run_id, run_id);
536        assert_eq!(checkpoint.task, "Test task");
537        assert_eq!(checkpoint.status, CheckpointStatus::Running);
538    }
539
540    #[test]
541    fn test_run_store_resume() {
542        let dir = tempdir().unwrap();
543        let run_id = uuid::Uuid::now_v7();
544        let store = RunStore::new(dir.path()).unwrap();
545        store.init_run(run_id, "Test task").unwrap();
546
547        // Open in new store instance
548        let store2 = RunStore::new(dir.path()).unwrap();
549        let checkpoint = store2.open_run(run_id).unwrap().unwrap();
550        assert_eq!(checkpoint.run_id, run_id);
551        assert_eq!(checkpoint.task, "Test task");
552    }
553
554    #[test]
555    fn test_can_resume() {
556        let dir = tempdir().unwrap();
557        let run_id = uuid::Uuid::now_v7();
558        let store = RunStore::new(dir.path()).unwrap();
559        store.init_run(run_id, "Test task").unwrap();
560
561        assert!(store.can_resume());
562    }
563
564    #[test]
565    fn test_resume_appends_events() {
566        // Regression: open_run previously opened events.jsonl read-only, causing
567        // every forwarded event in the resumed run to fail with
568        // `Access is denied (os error 5)` and silently dropping observability.
569        let dir = tempdir().unwrap();
570        let run_id = uuid::Uuid::now_v7();
571        let store = RunStore::new(dir.path()).unwrap();
572        store.init_run(run_id, "Test task").unwrap();
573
574        let store2 = RunStore::new(dir.path()).unwrap();
575        store2.open_run(run_id).unwrap().unwrap();
576
577        // Writing through the resumed store must succeed and persist the event.
578        let evt = AgentEvent::Log {
579            run_id,
580            agent_id: None,
581            level: crate::contract::event::LogLevel::Info,
582            msg: "resume smoke test".to_string(),
583        };
584        store2
585            .append_event(&evt)
586            .expect("append_event after resume must succeed");
587
588        let log = store2.get_event_log().expect("read events.jsonl");
589        assert!(
590            log.iter().any(|e| matches!(
591                e,
592                AgentEvent::Log { msg, .. } if msg == "resume smoke test"
593            )),
594            "event written after open_run must appear in events.jsonl"
595        );
596    }
597
598    // ----------------------------------------------------------------------
599    // Tests for F1 / F4 / F5 / F8 (spec `docs/src/core/state.rs.md`).
600    //
601    // These exercise the consolidated write path
602    // (`write_checkpoint_to_disk`), the lock-dance-free `cancel`, the
603    // snake_case `AgentStatus::as_str()` mapping that no longer depends on
604    // `Debug` formatting, and the `serde_to_io` error mapping helper that
605    // funnels every `serde_json::Error` through `ErrorKind::InvalidData`.
606    // ----------------------------------------------------------------------
607
608    use crate::contract::backend::AgentStatus;
609    use crate::contract::ids::TokenUsage;
610    use std::collections::HashSet;
611
612    fn sample_token_usage() -> TokenUsage {
613        TokenUsage {
614            input: 10,
615            output: 5,
616            cache_read: 0,
617            cache_write: 0,
618        }
619    }
620
621    fn build_agent_done(
622        run_id: RunId,
623        agent_id: AgentId,
624        status: AgentStatus,
625        tokens: TokenUsage,
626    ) -> AgentEvent {
627        AgentEvent::AgentDone {
628            run_id,
629            agent_id,
630            status,
631            tokens,
632            elapsed_ms: 0,
633            name: None,
634            agent_seq: 0,
635            output: serde_json::Value::Null,
636            findings: vec![],
637            prompt: String::new(),
638            retry_count: 0,
639        }
640    }
641
642    fn read_raw_checkpoint(run_dir: &Path) -> serde_json::Value {
643        let path = run_dir.join("checkpoint.json");
644        let content =
645            std::fs::read_to_string(&path).unwrap_or_else(|e| panic!("read checkpoint.json: {e}"));
646        serde_json::from_str(&content).unwrap_or_else(|e| panic!("parse checkpoint.json: {e}"))
647    }
648
649    // ----- upsert_agent_result (F1 delegation) ---------------------------
650
651    #[test]
652    fn upsert_agent_result_persists_to_disk() {
653        // F1: `upsert_agent_result` must persist via the same write path as
654        // `write_checkpoint_to_disk` so that a follow-up `open_run` sees the
655        // inserted entry without any in-process plumbing.
656        let dir = tempdir().unwrap();
657        let run_id = uuid::Uuid::now_v7();
658        let store = RunStore::new(dir.path()).unwrap();
659        store.init_run(run_id, "upsert test").unwrap();
660
661        let agent_id = uuid::Uuid::now_v7();
662        let cache = AgentResultCache {
663            agent_id,
664            phase_id: 1,
665            status: "ok".into(),
666            output: serde_json::json!({"v": 42}),
667            findings: vec![],
668            tokens: 100,
669            completed_at: 1_700_000_000,
670            cache_key_hash: Some("deadbeef".into()),
671            description: None,
672            role: None,
673        };
674        store.upsert_agent_result(&cache).unwrap();
675
676        // 1. In-memory state reflects the upsert.
677        let cp = store.get_checkpoint().expect("checkpoint present");
678        let cached = cp
679            .agent_results
680            .get(&agent_id)
681            .expect("agent_id indexed after upsert");
682        assert_eq!(cached.tokens, 100);
683        assert_eq!(cached.status, "ok");
684
685        // 2. On-disk JSON matches the in-memory state.
686        let raw = read_raw_checkpoint(dir.path());
687        let ar = raw
688            .get("agent_results")
689            .and_then(|v| v.as_object())
690            .expect("agent_results object");
691        assert_eq!(ar.len(), 1, "exactly one agent cached on disk");
692        let entry = ar.values().next().expect("non-empty agent_results on disk");
693        assert_eq!(entry.get("tokens").and_then(|v| v.as_u64()), Some(100));
694        assert_eq!(entry.get("status").and_then(|v| v.as_str()), Some("ok"));
695        assert_eq!(
696            entry.get("cache_key_hash").and_then(|v| v.as_str()),
697            Some("deadbeef")
698        );
699
700        // 3. Re-opening the run restores the entry from disk.
701        drop(store);
702        let reopened = RunStore::new(dir.path()).unwrap();
703        let restored = reopened.open_run(run_id).unwrap().unwrap();
704        assert!(
705            restored.agent_results.contains_key(&agent_id),
706            "upserted entry must survive close+reopen"
707        );
708        assert_eq!(restored.agent_results[&agent_id].tokens, 100);
709    }
710
711    #[test]
712    fn upsert_agent_result_updates_existing_entry() {
713        // F1: re-upserting the same agent_id overwrites the prior entry,
714        // mirroring the HashMap semantics of agent_results.
715        let dir = tempdir().unwrap();
716        let run_id = uuid::Uuid::now_v7();
717        let store = RunStore::new(dir.path()).unwrap();
718        store.init_run(run_id, "overwrite test").unwrap();
719
720        let agent_id = uuid::Uuid::now_v7();
721        let first = AgentResultCache {
722            agent_id,
723            phase_id: 1,
724            status: "ok".into(),
725            output: serde_json::json!("first"),
726            findings: vec![],
727            tokens: 10,
728            completed_at: 1,
729            cache_key_hash: None,
730            description: None,
731            role: None,
732        };
733        let second = AgentResultCache {
734            agent_id,
735            phase_id: 1,
736            status: "error".into(),
737            output: serde_json::json!("second"),
738            findings: vec![],
739            tokens: 99,
740            completed_at: 2,
741            cache_key_hash: None,
742            description: None,
743            role: None,
744        };
745        store.upsert_agent_result(&first).unwrap();
746        store.upsert_agent_result(&second).unwrap();
747
748        let cp = store.get_checkpoint().unwrap();
749        assert_eq!(cp.agent_results.len(), 1, "no duplicate entries");
750        let cached = &cp.agent_results[&agent_id];
751        assert_eq!(cached.status, "error");
752        assert_eq!(cached.tokens, 99);
753        assert_eq!(cached.completed_at, 2);
754
755        // Disk must also reflect the second upsert, not the first.
756        let raw = read_raw_checkpoint(dir.path());
757        let ar = raw
758            .get("agent_results")
759            .and_then(|v| v.as_object())
760            .unwrap();
761        assert_eq!(ar.len(), 1);
762        let entry = ar.values().next().unwrap();
763        assert_eq!(entry.get("tokens").and_then(|v| v.as_u64()), Some(99));
764        assert_eq!(entry.get("status").and_then(|v| v.as_str()), Some("error"));
765    }
766
767    #[test]
768    fn upsert_agent_result_noop_when_uninitialized() {
769        // F1: before init_run the in-memory checkpoint is None and the helper
770        // must not create a checkpoint.json from nothing. This keeps the
771        // behaviour of "upsert only patches an existing checkpoint".
772        let dir = tempdir().unwrap();
773        let store = RunStore::new(dir.path()).unwrap();
774        assert!(store.get_checkpoint().is_none());
775        let cp_path = dir.path().join("checkpoint.json");
776        assert!(!cp_path.exists(), "no checkpoint.json before init");
777
778        let cache = AgentResultCache {
779            agent_id: uuid::Uuid::now_v7(),
780            phase_id: 1,
781            status: "ok".into(),
782            output: serde_json::json!(null),
783            findings: vec![],
784            tokens: 0,
785            completed_at: 0,
786            cache_key_hash: None,
787            description: None,
788            role: None,
789        };
790        store.upsert_agent_result(&cache).unwrap();
791        assert!(
792            !cp_path.exists(),
793            "upsert_agent_result must not create checkpoint.json before init_run"
794        );
795        assert!(store.get_checkpoint().is_none());
796    }
797
798    #[test]
799    fn upsert_agent_result_advances_updated_at() {
800        // F1: the delegated write path must still update the checkpoint's
801        // `updated_at` timestamp the same way the inline implementation did.
802        let dir = tempdir().unwrap();
803        let run_id = uuid::Uuid::now_v7();
804        let store = RunStore::new(dir.path()).unwrap();
805        store.init_run(run_id, "ts test").unwrap();
806        let before = store.get_checkpoint().unwrap().updated_at;
807
808        std::thread::sleep(std::time::Duration::from_millis(1100));
809
810        let cache = AgentResultCache {
811            agent_id: uuid::Uuid::now_v7(),
812            phase_id: 1,
813            status: "ok".into(),
814            output: serde_json::json!(null),
815            findings: vec![],
816            tokens: 0,
817            completed_at: 0,
818            cache_key_hash: None,
819            description: None,
820            role: None,
821        };
822        store.upsert_agent_result(&cache).unwrap();
823        let after = store.get_checkpoint().unwrap().updated_at;
824        assert!(
825            after > before,
826            "updated_at must advance after upsert (before={before}, after={after})"
827        );
828    }
829
830    // ----- cancel (F1 delegation + F4 lock-dance collapse) ---------------
831
832    #[test]
833    fn cancel_persists_cancelled_status_to_disk() {
834        // F1+F4: cancel delegates to write_checkpoint_to_disk with the
835        // already-mutated checkpoint (no redundant read-lock + inline
836        // serialize). The Cancelled status must appear on disk so a follow-up
837        // process sees the terminal state.
838        let dir = tempdir().unwrap();
839        let run_id = uuid::Uuid::now_v7();
840        let store = RunStore::new(dir.path()).unwrap();
841        store.init_run(run_id, "cancel me").unwrap();
842        assert!(store.can_resume());
843
844        store.cancel().unwrap();
845
846        // In-memory: status is Cancelled, can_resume() is false.
847        let cp = store.get_checkpoint().unwrap();
848        assert_eq!(cp.status, CheckpointStatus::Cancelled);
849        assert!(!store.can_resume());
850
851        // On-disk: same status, observable across processes.
852        let raw = read_raw_checkpoint(dir.path());
853        assert_eq!(
854            raw.get("status").and_then(|v| v.as_str()),
855            Some("cancelled")
856        );
857
858        // Reopen: the persisted status survives close+reopen.
859        drop(store);
860        let reopened = RunStore::new(dir.path()).unwrap();
861        let restored = reopened.open_run(run_id).unwrap().unwrap();
862        assert_eq!(restored.status, CheckpointStatus::Cancelled);
863        assert!(!reopened.can_resume());
864    }
865
866    #[test]
867    fn cancel_is_idempotent() {
868        // F4: the new cancel body only mutates under the write lock and
869        // delegates to write_checkpoint_to_disk once. Calling it twice must
870        // not panic, not deadlock, and must leave the persisted state
871        // consistent (Cancelled, monotonically newer updated_at).
872        let dir = tempdir().unwrap();
873        let run_id = uuid::Uuid::now_v7();
874        let store = RunStore::new(dir.path()).unwrap();
875        store.init_run(run_id, "double cancel").unwrap();
876
877        store.cancel().unwrap();
878        let after_first = store.get_checkpoint().unwrap().updated_at;
879        std::thread::sleep(std::time::Duration::from_millis(1100));
880
881        store.cancel().expect("second cancel must succeed");
882        let after_second = store.get_checkpoint().unwrap().updated_at;
883
884        assert_eq!(
885            store.get_checkpoint().unwrap().status,
886            CheckpointStatus::Cancelled
887        );
888        assert!(
889            after_second >= after_first,
890            "updated_at must not regress (was {after_first}, now {after_second})"
891        );
892
893        let raw = read_raw_checkpoint(dir.path());
894        assert_eq!(
895            raw.get("status").and_then(|v| v.as_str()),
896            Some("cancelled")
897        );
898    }
899
900    #[test]
901    fn cancel_before_init_is_safe_noop() {
902        // F4: cancel on an uninitialised store must not panic and must not
903        // create a checkpoint file. The cancelled-status guard requires
904        // `Some(checkpoint)` so the body simply skips.
905        let dir = tempdir().unwrap();
906        let store = RunStore::new(dir.path()).unwrap();
907        assert!(store.get_checkpoint().is_none());
908        store.cancel().expect("cancel before init must succeed");
909        assert!(store.get_checkpoint().is_none());
910        assert!(
911            !dir.path().join("checkpoint.json").exists(),
912            "cancel before init must not create checkpoint.json"
913        );
914    }
915
916    #[test]
917    fn cancel_preserves_agent_results_and_findings() {
918        // F1+F4: cancel only mutates status/updated_at. Pre-existing
919        // agent_results, findings, and total_tokens must be preserved
920        // verbatim across the cancel write.
921        let dir = tempdir().unwrap();
922        let run_id = uuid::Uuid::now_v7();
923        let store = RunStore::new(dir.path()).unwrap();
924        store.init_run(run_id, "preserve").unwrap();
925
926        let agent_id = uuid::Uuid::now_v7();
927        let cache = AgentResultCache {
928            agent_id,
929            phase_id: 1,
930            status: "ok".into(),
931            output: serde_json::json!({"x": 1}),
932            findings: vec![],
933            tokens: 250,
934            completed_at: 7,
935            cache_key_hash: Some("hash-1".into()),
936            description: None,
937            role: None,
938        };
939        store.upsert_agent_result(&cache).unwrap();
940        let before = store.get_checkpoint().unwrap();
941
942        store.cancel().unwrap();
943        let after = store.get_checkpoint().unwrap();
944
945        assert_eq!(after.status, CheckpointStatus::Cancelled);
946        assert_eq!(after.agent_results.len(), 1);
947        assert_eq!(after.agent_results[&agent_id].tokens, 250);
948        assert_eq!(
949            after.agent_results[&agent_id].cache_key_hash.as_deref(),
950            Some("hash-1")
951        );
952        assert_eq!(after.total_tokens, before.total_tokens);
953    }
954
955    // ----- AgentDone -> AgentResultCache.status (F5) ---------------------
956
957    #[test]
958    fn agent_done_persists_snake_case_status_for_each_variant() {
959        // F5 KEY test: the persisted AgentResultCache.status string MUST
960        // come from AgentStatus::as_str() and NOT from Debug formatting.
961        // For TimedOut this is the load-bearing regression: Debug lowercased
962        // yields "timedout" (no underscore) but as_str() yields "timed_out".
963        let dir = tempdir().unwrap();
964        let run_id = uuid::Uuid::now_v7();
965        let store = RunStore::new(dir.path()).unwrap();
966        store.init_run(run_id, "F5 variants").unwrap();
967
968        let cases: Vec<(AgentStatus, &str)> = vec![
969            (AgentStatus::Ok, "ok"),
970            (AgentStatus::Error, "error"),
971            (AgentStatus::Cancelled, "cancelled"),
972            (AgentStatus::TimedOut, "timed_out"),
973        ];
974        for (status, expected) in &cases {
975            let agent_id = uuid::Uuid::now_v7();
976            let evt = build_agent_done(run_id, agent_id, status.clone(), sample_token_usage());
977            store.append_event(&evt).unwrap();
978
979            let raw = read_raw_checkpoint(dir.path());
980            let ar = raw
981                .get("agent_results")
982                .and_then(|v| v.as_object())
983                .expect("agent_results object");
984            let entry = ar
985                .values()
986                .find(|v| {
987                    v.get("agent_id").and_then(|id| id.as_str()) == Some(&agent_id.to_string())
988                })
989                .unwrap_or_else(|| panic!("entry for {agent_id} missing"));
990            let persisted = entry
991                .get("status")
992                .and_then(|v| v.as_str())
993                .unwrap_or_else(|| panic!("status missing for {status:?}"));
994            assert_eq!(
995                persisted, *expected,
996                "AgentDone({status:?}) must persist status={expected:?} (snake_case); \
997                 got {persisted:?}. If this fails with \"timedout\" for TimedOut, \
998                 F5 has regressed to Debug formatting."
999            );
1000        }
1001    }
1002
1003    #[test]
1004    fn agent_done_timed_out_persists_with_underscore_not_collapsed() {
1005        // Strongest F5 regression guard: the buggy form would persist
1006        // "timedout" (no underscore) for TimedOut. The fixed form persists
1007        // "timed_out". This test fails loudly if anyone reintroduces the
1008        // `format!("{:?}", status).to_lowercase()` shortcut.
1009        let dir = tempdir().unwrap();
1010        let run_id = uuid::Uuid::now_v7();
1011        let store = RunStore::new(dir.path()).unwrap();
1012        store.init_run(run_id, "timed-out guard").unwrap();
1013
1014        let agent_id = uuid::Uuid::now_v7();
1015        let evt = build_agent_done(
1016            run_id,
1017            agent_id,
1018            AgentStatus::TimedOut,
1019            sample_token_usage(),
1020        );
1021        store.append_event(&evt).unwrap();
1022
1023        let raw = read_raw_checkpoint(dir.path());
1024        let ar = raw
1025            .get("agent_results")
1026            .and_then(|v| v.as_object())
1027            .unwrap();
1028        let entry = ar.values().next().expect("entry exists");
1029        let persisted = entry.get("status").and_then(|v| v.as_str()).unwrap();
1030
1031        assert_eq!(
1032            persisted, "timed_out",
1033            "AgentDone(TimedOut) must persist \"timed_out\" with an underscore; got {persisted:?}"
1034        );
1035        assert_ne!(
1036            persisted, "timedout",
1037            "AgentDone(TimedOut) must NOT collapse to Debug-lowercased \"timedout\""
1038        );
1039    }
1040
1041    #[test]
1042    fn agent_done_then_reopen_restores_snake_case_status() {
1043        // The persisted snake_case status must survive a close+reopen cycle,
1044        // since legacy checkpoints with Debug-lowercased "timedout" should be
1045        // distinguished from new checkpoints with "timed_out" — but new
1046        // checkpoints must round-trip cleanly through the JSON pipeline.
1047        let dir = tempdir().unwrap();
1048        let run_id = uuid::Uuid::now_v7();
1049        let store = RunStore::new(dir.path()).unwrap();
1050        store.init_run(run_id, "round-trip").unwrap();
1051
1052        let agent_id = uuid::Uuid::now_v7();
1053        let evt = build_agent_done(
1054            run_id,
1055            agent_id,
1056            AgentStatus::Cancelled,
1057            TokenUsage {
1058                input: 1,
1059                output: 2,
1060                cache_read: 0,
1061                cache_write: 0,
1062            },
1063        );
1064        store.append_event(&evt).unwrap();
1065        drop(store);
1066
1067        let reopened = RunStore::new(dir.path()).unwrap();
1068        let cp = reopened.open_run(run_id).unwrap().unwrap();
1069        let cached = cp
1070            .agent_results
1071            .get(&agent_id)
1072            .expect("agent cached on disk");
1073        assert_eq!(cached.status, "cancelled");
1074        assert_eq!(cached.tokens, 3);
1075    }
1076
1077    // ----- F8 serde_to_io error mapping (indirect) -----------------------
1078
1079    #[test]
1080    fn open_run_with_corrupt_checkpoint_returns_invalid_data() {
1081        // F8: every serde_json::Error → io::Error funnel passes through
1082        // ErrorKind::InvalidData. Verifies the consolidated helper is wired
1083        // into open_run's deserialization path.
1084        let dir = tempdir().unwrap();
1085        std::fs::create_dir_all(dir.path()).unwrap();
1086        std::fs::write(
1087            dir.path().join("checkpoint.json"),
1088            b"{ this is not valid json",
1089        )
1090        .unwrap();
1091
1092        let store = RunStore::new(dir.path()).unwrap();
1093        let err = store
1094            .open_run(uuid::Uuid::now_v7())
1095            .expect_err("corrupt JSON must surface as an io::Error");
1096        assert_eq!(
1097            err.kind(),
1098            std::io::ErrorKind::InvalidData,
1099            "corrupt checkpoint must map to InvalidData via serde_to_io; got {:?}",
1100            err.kind()
1101        );
1102    }
1103
1104    #[test]
1105    fn open_run_with_wrong_typed_checkpoint_returns_invalid_data() {
1106        // F8: even structurally-valid JSON that fails typed deserialisation
1107        // (missing required field) must come back as InvalidData.
1108        let dir = tempdir().unwrap();
1109        std::fs::create_dir_all(dir.path()).unwrap();
1110        // `task` is a required field on RunCheckpoint; omitting it triggers
1111        // a serde error which the helper must classify as InvalidData.
1112        std::fs::write(
1113            dir.path().join("checkpoint.json"),
1114            br#"{"run_id":"00000000-0000-0000-0000-000000000000","status":"running"}"#,
1115        )
1116        .unwrap();
1117
1118        let store = RunStore::new(dir.path()).unwrap();
1119        let err = store
1120            .open_run(uuid::Uuid::now_v7())
1121            .expect_err("missing-field JSON must surface as an io::Error");
1122        assert_eq!(err.kind(), std::io::ErrorKind::InvalidData);
1123    }
1124
1125    #[test]
1126    fn get_event_log_with_corrupt_line_returns_invalid_data() {
1127        // F8: the consolidated helper also covers get_event_log's per-line
1128        // deserialisation. A single bad line must surface as InvalidData
1129        // rather than SomeOtherKind so callers can distinguish "corrupted
1130        // journal" from "missing file".
1131        let dir = tempdir().unwrap();
1132        std::fs::create_dir_all(dir.path()).unwrap();
1133        std::fs::write(dir.path().join("events.jsonl"), b"not-json\n").unwrap();
1134
1135        let store = RunStore::new(dir.path()).unwrap();
1136        let err = store
1137            .get_event_log()
1138            .expect_err("corrupt event line must surface as an io::Error");
1139        assert_eq!(
1140            err.kind(),
1141            std::io::ErrorKind::InvalidData,
1142            "corrupt event line must map to InvalidData via serde_to_io; got {:?}",
1143            err.kind()
1144        );
1145    }
1146
1147    // ----- Cross-cutting safety nets -------------------------------------
1148
1149    #[test]
1150    fn as_str_variants_round_trip_through_checkpoint_pipeline() {
1151        // Property-style test: for every AgentStatus variant, the persisted
1152        // status string must equal AgentStatus::variant.as_str() exactly,
1153        // with no whitespace, no case drift, and no truncation. This catches
1154        // accidental future reverts to Debug-derived strings.
1155        let dir = tempdir().unwrap();
1156        let run_id = uuid::Uuid::now_v7();
1157        let store = RunStore::new(dir.path()).unwrap();
1158        store.init_run(run_id, "round-trip property").unwrap();
1159
1160        let variants = [
1161            AgentStatus::Ok,
1162            AgentStatus::Error,
1163            AgentStatus::Cancelled,
1164            AgentStatus::TimedOut,
1165        ];
1166        let mut seen: HashSet<String> = HashSet::new();
1167
1168        for variant in &variants {
1169            let agent_id = uuid::Uuid::now_v7();
1170            let evt = build_agent_done(run_id, agent_id, variant.clone(), sample_token_usage());
1171            store.append_event(&evt).unwrap();
1172
1173            let cp = store.get_checkpoint().unwrap();
1174            let cached = cp
1175                .agent_results
1176                .get(&agent_id)
1177                .expect("entry for {agent_id}");
1178            assert_eq!(
1179                cached.status,
1180                variant.as_str(),
1181                "{variant:?}.as_str() must round-trip via append_event→update_from_event"
1182            );
1183            // Also confirm uniqueness is preserved on disk.
1184            assert!(
1185                seen.insert(cached.status.clone()),
1186                "duplicate status {cached_status:?} persisted for {variant:?}",
1187                cached_status = cached.status
1188            );
1189        }
1190    }
1191}