cuenv_core/hooks/
state.rs

1//! State management for hook execution tracking
2
3use crate::hooks::types::{ExecutionStatus, HookResult};
4use crate::{Error, Result};
5use chrono::{DateTime, Utc};
6use fs4::tokio::AsyncFileExt;
7use serde::{Deserialize, Serialize};
8use std::collections::HashMap;
9use std::path::{Path, PathBuf};
10use tokio::fs;
11use tokio::fs::OpenOptions;
12use tokio::io::{AsyncReadExt, AsyncWriteExt};
13use tracing::{debug, error, info, warn};
14
15/// Manages persistent state for hook execution sessions
16#[derive(Debug, Clone)]
17pub struct StateManager {
18    state_dir: PathBuf,
19}
20
21impl StateManager {
22    /// Create a new state manager with the specified state directory
23    pub fn new(state_dir: PathBuf) -> Self {
24        Self { state_dir }
25    }
26
27    /// Get the default state directory (~/.cuenv/state)
28    pub fn default_state_dir() -> Result<PathBuf> {
29        // Check for CUENV_STATE_DIR environment variable first
30        if let Ok(state_dir) = std::env::var("CUENV_STATE_DIR") {
31            return Ok(PathBuf::from(state_dir));
32        }
33
34        let home = dirs::home_dir()
35            .ok_or_else(|| Error::configuration("Could not determine home directory"))?;
36        Ok(home.join(".cuenv").join("state"))
37    }
38
39    /// Create a state manager using the default state directory
40    pub fn with_default_dir() -> Result<Self> {
41        Ok(Self::new(Self::default_state_dir()?))
42    }
43
44    /// Get the state directory path
45    pub fn get_state_dir(&self) -> &Path {
46        &self.state_dir
47    }
48
49    /// Ensure the state directory exists
50    pub async fn ensure_state_dir(&self) -> Result<()> {
51        if !self.state_dir.exists() {
52            fs::create_dir_all(&self.state_dir)
53                .await
54                .map_err(|e| Error::Io {
55                    source: e,
56                    path: Some(self.state_dir.clone().into_boxed_path()),
57                    operation: "create_dir_all".to_string(),
58                })?;
59            debug!("Created state directory: {}", self.state_dir.display());
60        }
61        Ok(())
62    }
63
64    /// Generate a state file path for a given directory hash
65    fn state_file_path(&self, instance_hash: &str) -> PathBuf {
66        self.state_dir.join(format!("{}.json", instance_hash))
67    }
68
69    /// Get the state file path for a given directory hash (public for PID files)
70    pub fn get_state_file_path(&self, instance_hash: &str) -> PathBuf {
71        self.state_dir.join(format!("{}.json", instance_hash))
72    }
73
74    /// Save execution state to disk with atomic write and locking
75    pub async fn save_state(&self, state: &HookExecutionState) -> Result<()> {
76        self.ensure_state_dir().await?;
77
78        let state_file = self.state_file_path(&state.instance_hash);
79        let json = serde_json::to_string_pretty(state)
80            .map_err(|e| Error::configuration(format!("Failed to serialize state: {e}")))?;
81
82        // Write to a temporary file first, then rename atomically
83        let temp_path = state_file.with_extension("tmp");
84
85        // Open temp file with exclusive lock for writing
86        let mut file = OpenOptions::new()
87            .write(true)
88            .create(true)
89            .truncate(true)
90            .open(&temp_path)
91            .await
92            .map_err(|e| Error::Io {
93                source: e,
94                path: Some(temp_path.clone().into_boxed_path()),
95                operation: "open".to_string(),
96            })?;
97
98        // Acquire exclusive lock (only one writer allowed)
99        file.lock_exclusive().map_err(|e| {
100            Error::configuration(format!(
101                "Failed to acquire exclusive lock on state temp file: {}",
102                e
103            ))
104        })?;
105
106        file.write_all(json.as_bytes())
107            .await
108            .map_err(|e| Error::Io {
109                source: e,
110                path: Some(temp_path.clone().into_boxed_path()),
111                operation: "write_all".to_string(),
112            })?;
113
114        file.sync_all().await.map_err(|e| Error::Io {
115            source: e,
116            path: Some(temp_path.clone().into_boxed_path()),
117            operation: "sync_all".to_string(),
118        })?;
119
120        // Unlock happens automatically when file is dropped
121        drop(file);
122
123        // Atomically rename temp file to final location
124        fs::rename(&temp_path, &state_file)
125            .await
126            .map_err(|e| Error::Io {
127                source: e,
128                path: Some(state_file.clone().into_boxed_path()),
129                operation: "rename".to_string(),
130            })?;
131
132        debug!(
133            "Saved execution state for directory hash: {}",
134            state.instance_hash
135        );
136        Ok(())
137    }
138
139    /// Load execution state from disk with shared locking
140    pub async fn load_state(&self, instance_hash: &str) -> Result<Option<HookExecutionState>> {
141        let state_file = self.state_file_path(instance_hash);
142
143        if !state_file.exists() {
144            return Ok(None);
145        }
146
147        // Open file with shared lock for reading
148        let mut file = match OpenOptions::new().read(true).open(&state_file).await {
149            Ok(f) => f,
150            Err(e) => {
151                // File might have been deleted between exists check and open
152                if e.kind() == std::io::ErrorKind::NotFound {
153                    return Ok(None);
154                }
155                return Err(Error::Io {
156                    source: e,
157                    path: Some(state_file.clone().into_boxed_path()),
158                    operation: "open".to_string(),
159                });
160            }
161        };
162
163        // Acquire shared lock (multiple readers allowed)
164        file.lock_shared().map_err(|e| {
165            Error::configuration(format!(
166                "Failed to acquire shared lock on state file: {}",
167                e
168            ))
169        })?;
170
171        let mut contents = String::new();
172        file.read_to_string(&mut contents)
173            .await
174            .map_err(|e| Error::Io {
175                source: e,
176                path: Some(state_file.clone().into_boxed_path()),
177                operation: "read_to_string".to_string(),
178            })?;
179
180        // Unlock happens automatically when file is dropped
181        drop(file);
182
183        let state: HookExecutionState = serde_json::from_str(&contents)
184            .map_err(|e| Error::configuration(format!("Failed to deserialize state: {e}")))?;
185
186        debug!(
187            "Loaded execution state for directory hash: {}",
188            instance_hash
189        );
190        Ok(Some(state))
191    }
192
193    /// Remove state file for a directory
194    pub async fn remove_state(&self, instance_hash: &str) -> Result<()> {
195        let state_file = self.state_file_path(instance_hash);
196
197        if state_file.exists() {
198            fs::remove_file(&state_file).await.map_err(|e| Error::Io {
199                source: e,
200                path: Some(state_file.into_boxed_path()),
201                operation: "remove_file".to_string(),
202            })?;
203            debug!(
204                "Removed execution state for directory hash: {}",
205                instance_hash
206            );
207        }
208
209        Ok(())
210    }
211
212    /// List all active execution states
213    pub async fn list_active_states(&self) -> Result<Vec<HookExecutionState>> {
214        if !self.state_dir.exists() {
215            return Ok(Vec::new());
216        }
217
218        let mut states = Vec::new();
219        let mut dir = fs::read_dir(&self.state_dir).await.map_err(|e| Error::Io {
220            source: e,
221            path: Some(self.state_dir.clone().into_boxed_path()),
222            operation: "read_dir".to_string(),
223        })?;
224
225        while let Some(entry) = dir.next_entry().await.map_err(|e| Error::Io {
226            source: e,
227            path: Some(self.state_dir.clone().into_boxed_path()),
228            operation: "next_entry".to_string(),
229        })? {
230            let path = entry.path();
231            if path.extension().and_then(|s| s.to_str()) == Some("json")
232                && let Some(stem) = path.file_stem().and_then(|s| s.to_str())
233                && let Ok(Some(state)) = self.load_state(stem).await
234            {
235                states.push(state);
236            }
237        }
238
239        Ok(states)
240    }
241
242    /// Clean up the entire state directory
243    pub async fn cleanup_state_directory(&self) -> Result<usize> {
244        if !self.state_dir.exists() {
245            return Ok(0);
246        }
247
248        let mut cleaned_count = 0;
249        let mut dir = fs::read_dir(&self.state_dir).await.map_err(|e| Error::Io {
250            source: e,
251            path: Some(self.state_dir.clone().into_boxed_path()),
252            operation: "read_dir".to_string(),
253        })?;
254
255        while let Some(entry) = dir.next_entry().await.map_err(|e| Error::Io {
256            source: e,
257            path: Some(self.state_dir.clone().into_boxed_path()),
258            operation: "next_entry".to_string(),
259        })? {
260            let path = entry.path();
261
262            // Only clean up JSON state files
263            if path.extension().and_then(|s| s.to_str()) == Some("json") {
264                // Try to load and check if it's a completed state
265                if let Some(stem) = path.file_stem().and_then(|s| s.to_str()) {
266                    match self.load_state(stem).await {
267                        Ok(Some(state)) if state.is_complete() => {
268                            // Remove completed states
269                            if let Err(e) = fs::remove_file(&path).await {
270                                warn!("Failed to remove state file {}: {}", path.display(), e);
271                            } else {
272                                cleaned_count += 1;
273                                debug!("Cleaned up state file: {}", path.display());
274                            }
275                        }
276                        Ok(Some(_)) => {
277                            // Keep running states
278                            debug!("Keeping active state file: {}", path.display());
279                        }
280                        Ok(None) => {}
281                        Err(e) => {
282                            // If we can't parse it, it might be corrupted - remove it
283                            warn!("Failed to parse state file {}: {}", path.display(), e);
284                            if let Err(rm_err) = fs::remove_file(&path).await {
285                                error!(
286                                    "Failed to remove corrupted state file {}: {}",
287                                    path.display(),
288                                    rm_err
289                                );
290                            } else {
291                                cleaned_count += 1;
292                                info!("Removed corrupted state file: {}", path.display());
293                            }
294                        }
295                    }
296                }
297            }
298        }
299
300        if cleaned_count > 0 {
301            info!("Cleaned up {} state files from directory", cleaned_count);
302        }
303
304        Ok(cleaned_count)
305    }
306
307    /// Clean up orphaned state files (states without corresponding processes)
308    pub async fn cleanup_orphaned_states(&self, max_age: chrono::Duration) -> Result<usize> {
309        let cutoff = Utc::now() - max_age;
310        let mut cleaned_count = 0;
311
312        for state in self.list_active_states().await? {
313            // Remove states that are stuck in running but are too old
314            if state.status == ExecutionStatus::Running && state.started_at < cutoff {
315                warn!(
316                    "Found orphaned running state for {} (started {}), removing",
317                    state.directory_path.display(),
318                    state.started_at
319                );
320                self.remove_state(&state.instance_hash).await?;
321                cleaned_count += 1;
322            }
323        }
324
325        if cleaned_count > 0 {
326            info!("Cleaned up {} orphaned state files", cleaned_count);
327        }
328
329        Ok(cleaned_count)
330    }
331}
332
333/// Represents the state of hook execution for a specific directory
334#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
335pub struct HookExecutionState {
336    /// Hash combining directory path and config (instance identifier)
337    pub instance_hash: String,
338    /// Path to the directory being processed
339    pub directory_path: PathBuf,
340    /// Hash of the configuration that was approved
341    pub config_hash: String,
342    /// Current status of execution
343    pub status: ExecutionStatus,
344    /// Total number of hooks to execute
345    pub total_hooks: usize,
346    /// Number of hooks completed so far
347    pub completed_hooks: usize,
348    /// Index of currently executing hook (if any)
349    pub current_hook_index: Option<usize>,
350    /// The list of hooks being executed (for display purposes)
351    #[serde(default)]
352    pub hooks: Vec<crate::hooks::types::Hook>,
353    /// Results of completed hooks
354    pub hook_results: HashMap<usize, HookResult>,
355    /// Timestamp when execution started
356    pub started_at: DateTime<Utc>,
357    /// Timestamp when execution finished (if completed)
358    pub finished_at: Option<DateTime<Utc>>,
359    /// Timestamp when the current hook started (if running)
360    pub current_hook_started_at: Option<DateTime<Utc>>,
361    /// Timestamp until which completed state should be displayed
362    pub completed_display_until: Option<DateTime<Utc>>,
363    /// Error message if execution failed
364    pub error_message: Option<String>,
365    /// Environment variables captured from source hooks
366    pub environment_vars: HashMap<String, String>,
367    /// Previous environment variables (for diff/unset support)
368    pub previous_env: Option<HashMap<String, String>>,
369}
370
371impl HookExecutionState {
372    /// Create a new execution state
373    pub fn new(
374        directory_path: PathBuf,
375        instance_hash: String,
376        config_hash: String,
377        hooks: Vec<crate::hooks::types::Hook>,
378    ) -> Self {
379        let total_hooks = hooks.len();
380        Self {
381            instance_hash,
382            directory_path,
383            config_hash,
384            status: ExecutionStatus::Running,
385            total_hooks,
386            completed_hooks: 0,
387            current_hook_index: None,
388            hooks,
389            hook_results: HashMap::new(),
390            started_at: Utc::now(),
391            finished_at: None,
392            current_hook_started_at: None,
393            completed_display_until: None,
394            error_message: None,
395            environment_vars: HashMap::new(),
396            previous_env: None,
397        }
398    }
399
400    /// Mark a hook as currently executing
401    pub fn mark_hook_running(&mut self, hook_index: usize) {
402        self.current_hook_index = Some(hook_index);
403        self.current_hook_started_at = Some(Utc::now());
404        info!(
405            "Started executing hook {} of {}",
406            hook_index + 1,
407            self.total_hooks
408        );
409    }
410
411    /// Record the result of a hook execution
412    pub fn record_hook_result(&mut self, hook_index: usize, result: HookResult) {
413        self.hook_results.insert(hook_index, result.clone());
414        self.completed_hooks += 1;
415        self.current_hook_index = None;
416        self.current_hook_started_at = None;
417
418        if result.success {
419            info!(
420                "Hook {} of {} completed successfully",
421                hook_index + 1,
422                self.total_hooks
423            );
424        } else {
425            error!(
426                "Hook {} of {} failed: {:?}",
427                hook_index + 1,
428                self.total_hooks,
429                result.error
430            );
431            self.status = ExecutionStatus::Failed;
432            self.error_message = result.error.clone();
433            self.finished_at = Some(Utc::now());
434            // Keep failed state visible for 2 seconds (enough for at least one starship poll)
435            self.completed_display_until = Some(Utc::now() + chrono::Duration::seconds(2));
436            return;
437        }
438
439        // Check if all hooks are complete
440        if self.completed_hooks == self.total_hooks {
441            self.status = ExecutionStatus::Completed;
442            let now = Utc::now();
443            self.finished_at = Some(now);
444            // Keep completed state visible for 2 seconds (enough for at least one starship poll)
445            self.completed_display_until = Some(now + chrono::Duration::seconds(2));
446            info!("All {} hooks completed successfully", self.total_hooks);
447        }
448    }
449
450    /// Mark execution as cancelled
451    pub fn mark_cancelled(&mut self, reason: Option<String>) {
452        self.status = ExecutionStatus::Cancelled;
453        self.finished_at = Some(Utc::now());
454        self.error_message = reason;
455        self.current_hook_index = None;
456    }
457
458    /// Check if execution is complete (success, failure, or cancelled)
459    pub fn is_complete(&self) -> bool {
460        matches!(
461            self.status,
462            ExecutionStatus::Completed | ExecutionStatus::Failed | ExecutionStatus::Cancelled
463        )
464    }
465
466    /// Get a human-readable progress display
467    pub fn progress_display(&self) -> String {
468        match &self.status {
469            ExecutionStatus::Running => {
470                if let Some(current) = self.current_hook_index {
471                    format!(
472                        "Executing hook {} of {} ({})",
473                        current + 1,
474                        self.total_hooks,
475                        self.status
476                    )
477                } else {
478                    format!(
479                        "{} of {} hooks completed",
480                        self.completed_hooks, self.total_hooks
481                    )
482                }
483            }
484            ExecutionStatus::Completed => "All hooks completed successfully".to_string(),
485            ExecutionStatus::Failed => {
486                if let Some(error) = &self.error_message {
487                    format!("Hook execution failed: {}", error)
488                } else {
489                    "Hook execution failed".to_string()
490                }
491            }
492            ExecutionStatus::Cancelled => {
493                if let Some(reason) = &self.error_message {
494                    format!("Hook execution cancelled: {}", reason)
495                } else {
496                    "Hook execution cancelled".to_string()
497                }
498            }
499        }
500    }
501
502    /// Get execution duration
503    pub fn duration(&self) -> chrono::Duration {
504        let end = self.finished_at.unwrap_or_else(Utc::now);
505        end - self.started_at
506    }
507
508    /// Get current hook duration (if a hook is currently running)
509    pub fn current_hook_duration(&self) -> Option<chrono::Duration> {
510        self.current_hook_started_at
511            .map(|started| Utc::now() - started)
512    }
513
514    /// Get the currently executing hook
515    pub fn current_hook(&self) -> Option<&crate::hooks::types::Hook> {
516        self.current_hook_index.and_then(|idx| self.hooks.get(idx))
517    }
518
519    /// Format duration in human-readable format (e.g., "2.3s", "1m 15s", "2h 5m")
520    pub fn format_duration(duration: chrono::Duration) -> String {
521        let total_secs = duration.num_seconds();
522
523        if total_secs < 60 {
524            // Less than 1 minute: show as decimal seconds
525            let millis = duration.num_milliseconds();
526            format!("{:.1}s", millis as f64 / 1000.0)
527        } else if total_secs < 3600 {
528            // Less than 1 hour: show minutes and seconds
529            let mins = total_secs / 60;
530            let secs = total_secs % 60;
531            if secs == 0 {
532                format!("{}m", mins)
533            } else {
534                format!("{}m {}s", mins, secs)
535            }
536        } else {
537            // 1 hour or more: show hours and minutes
538            let hours = total_secs / 3600;
539            let mins = (total_secs % 3600) / 60;
540            if mins == 0 {
541                format!("{}h", hours)
542            } else {
543                format!("{}h {}m", hours, mins)
544            }
545        }
546    }
547
548    /// Get a short description of the current or next hook for display
549    pub fn current_hook_display(&self) -> Option<String> {
550        // If there's a current hook index, use that
551        let hook = if let Some(hook) = self.current_hook() {
552            Some(hook)
553        } else if self.status == ExecutionStatus::Running && self.completed_hooks < self.total_hooks
554        {
555            // If we're running but no current hook index yet, show the next hook to execute
556            self.hooks.get(self.completed_hooks)
557        } else {
558            None
559        };
560
561        hook.map(|h| {
562            // Extract just the command name (first part before any path separators)
563            let cmd_name = h.command.split('/').next_back().unwrap_or(&h.command);
564
565            // Format: just the command name (no args, to keep it concise)
566            format!("`{}`", cmd_name)
567        })
568    }
569
570    /// Check if the completed state should still be displayed
571    pub fn should_display_completed(&self) -> bool {
572        if let Some(display_until) = self.completed_display_until {
573            Utc::now() < display_until
574        } else {
575            false
576        }
577    }
578}
579
580/// Compute a hash for a unique execution instance (directory + config)
581pub fn compute_instance_hash(path: &Path, config_hash: &str) -> String {
582    use sha2::{Digest, Sha256};
583    let mut hasher = Sha256::new();
584    hasher.update(path.to_string_lossy().as_bytes());
585    hasher.update(b":");
586    hasher.update(config_hash.as_bytes());
587    // Include cuenv version in hash to invalidate cache on upgrades
588    // This is important when internal logic (like environment capturing) changes
589    hasher.update(b":");
590    hasher.update(crate::VERSION.as_bytes());
591    format!("{:x}", hasher.finalize())[..16].to_string()
592}
593
594#[cfg(test)]
595mod tests {
596    use super::*;
597    use crate::hooks::types::{Hook, HookResult};
598    use std::collections::HashMap;
599    use std::os::unix::process::ExitStatusExt;
600    use std::sync::Arc;
601    use std::time::Duration;
602    use tempfile::TempDir;
603
604    #[test]
605    fn test_compute_instance_hash() {
606        let path = Path::new("/test/path");
607        let config_hash = "test_config";
608        let hash = compute_instance_hash(path, config_hash);
609        assert_eq!(hash.len(), 16);
610
611        // Same path and config should produce same hash
612        let hash2 = compute_instance_hash(path, config_hash);
613        assert_eq!(hash, hash2);
614
615        // Different path should produce different hash
616        let different_path = Path::new("/other/path");
617        let different_hash = compute_instance_hash(different_path, config_hash);
618        assert_ne!(hash, different_hash);
619
620        // Same path but different config should produce different hash
621        let different_config_hash = compute_instance_hash(path, "different_config");
622        assert_ne!(hash, different_config_hash);
623    }
624
625    #[tokio::test]
626    async fn test_state_manager_operations() {
627        let temp_dir = TempDir::new().unwrap();
628        let state_manager = StateManager::new(temp_dir.path().to_path_buf());
629
630        let directory_path = PathBuf::from("/test/dir");
631        let config_hash = "test_config_hash".to_string();
632        let instance_hash = compute_instance_hash(&directory_path, &config_hash);
633
634        let hooks = vec![
635            Hook {
636                order: 100,
637                propagate: false,
638                command: "echo".to_string(),
639                args: vec!["test1".to_string()],
640                dir: None,
641                inputs: vec![],
642                source: None,
643            },
644            Hook {
645                order: 100,
646                propagate: false,
647                command: "echo".to_string(),
648                args: vec!["test2".to_string()],
649                dir: None,
650                inputs: vec![],
651                source: None,
652            },
653        ];
654
655        let mut state =
656            HookExecutionState::new(directory_path, instance_hash.clone(), config_hash, hooks);
657
658        // Save initial state
659        state_manager.save_state(&state).await.unwrap();
660
661        // Load state back
662        let loaded_state = state_manager
663            .load_state(&instance_hash)
664            .await
665            .unwrap()
666            .unwrap();
667        assert_eq!(loaded_state.instance_hash, state.instance_hash);
668        assert_eq!(loaded_state.total_hooks, 2);
669        assert_eq!(loaded_state.status, ExecutionStatus::Running);
670
671        // Update state with hook result
672        let hook = Hook {
673            order: 100,
674            propagate: false,
675            command: "echo".to_string(),
676            args: vec!["test".to_string()],
677            dir: None,
678            inputs: Vec::new(),
679            source: Some(false),
680        };
681
682        let result = HookResult::success(
683            hook,
684            std::process::ExitStatus::from_raw(0),
685            "test\n".to_string(),
686            "".to_string(),
687            100,
688        );
689
690        state.record_hook_result(0, result);
691        state_manager.save_state(&state).await.unwrap();
692
693        // Load updated state
694        let updated_state = state_manager
695            .load_state(&instance_hash)
696            .await
697            .unwrap()
698            .unwrap();
699        assert_eq!(updated_state.completed_hooks, 1);
700        assert_eq!(updated_state.hook_results.len(), 1);
701
702        // Remove state
703        state_manager.remove_state(&instance_hash).await.unwrap();
704        let removed_state = state_manager.load_state(&instance_hash).await.unwrap();
705        assert!(removed_state.is_none());
706    }
707
708    #[test]
709    fn test_hook_execution_state() {
710        let directory_path = PathBuf::from("/test/dir");
711        let instance_hash = "test_hash".to_string();
712        let config_hash = "config_hash".to_string();
713        let hooks = vec![
714            Hook {
715                order: 100,
716                propagate: false,
717                command: "echo".to_string(),
718                args: vec!["test1".to_string()],
719                dir: None,
720                inputs: vec![],
721                source: None,
722            },
723            Hook {
724                order: 100,
725                propagate: false,
726                command: "echo".to_string(),
727                args: vec!["test2".to_string()],
728                dir: None,
729                inputs: vec![],
730                source: None,
731            },
732            Hook {
733                order: 100,
734                propagate: false,
735                command: "echo".to_string(),
736                args: vec!["test3".to_string()],
737                dir: None,
738                inputs: vec![],
739                source: None,
740            },
741        ];
742        let mut state = HookExecutionState::new(directory_path, instance_hash, config_hash, hooks);
743
744        // Initial state
745        assert_eq!(state.status, ExecutionStatus::Running);
746        assert_eq!(state.total_hooks, 3);
747        assert_eq!(state.completed_hooks, 0);
748        assert!(!state.is_complete());
749
750        // Mark hook as running
751        state.mark_hook_running(0);
752        assert_eq!(state.current_hook_index, Some(0));
753
754        // Record successful hook result
755        let hook = Hook {
756            order: 100,
757            propagate: false,
758            command: "echo".to_string(),
759            args: vec![],
760            dir: None,
761            inputs: Vec::new(),
762            source: Some(false),
763        };
764
765        let result = HookResult::success(
766            hook.clone(),
767            std::process::ExitStatus::from_raw(0),
768            "".to_string(),
769            "".to_string(),
770            100,
771        );
772
773        state.record_hook_result(0, result);
774        assert_eq!(state.completed_hooks, 1);
775        assert_eq!(state.current_hook_index, None);
776        assert_eq!(state.status, ExecutionStatus::Running);
777        assert!(!state.is_complete());
778
779        // Record failed hook result
780        let failed_result = HookResult::failure(
781            hook,
782            Some(std::process::ExitStatus::from_raw(256)),
783            "".to_string(),
784            "error".to_string(),
785            50,
786            "Command failed".to_string(),
787        );
788
789        state.record_hook_result(1, failed_result);
790        assert_eq!(state.completed_hooks, 2);
791        assert_eq!(state.status, ExecutionStatus::Failed);
792        assert!(state.is_complete());
793        assert!(state.error_message.is_some());
794
795        // Test cancellation
796        let mut cancelled_state = HookExecutionState::new(
797            PathBuf::from("/test"),
798            "hash".to_string(),
799            "config".to_string(),
800            vec![Hook {
801                order: 100,
802                propagate: false,
803                command: "echo".to_string(),
804                args: vec![],
805                dir: None,
806                inputs: vec![],
807                source: None,
808            }],
809        );
810        cancelled_state.mark_cancelled(Some("User cancelled".to_string()));
811        assert_eq!(cancelled_state.status, ExecutionStatus::Cancelled);
812        assert!(cancelled_state.is_complete());
813    }
814
815    #[test]
816    fn test_progress_display() {
817        let directory_path = PathBuf::from("/test/dir");
818        let instance_hash = "test_hash".to_string();
819        let config_hash = "config_hash".to_string();
820        let hooks = vec![
821            Hook {
822                order: 100,
823                propagate: false,
824                command: "echo".to_string(),
825                args: vec!["test1".to_string()],
826                dir: None,
827                inputs: vec![],
828                source: None,
829            },
830            Hook {
831                order: 100,
832                propagate: false,
833                command: "echo".to_string(),
834                args: vec!["test2".to_string()],
835                dir: None,
836                inputs: vec![],
837                source: None,
838            },
839        ];
840        let mut state = HookExecutionState::new(directory_path, instance_hash, config_hash, hooks);
841
842        // Running state
843        let display = state.progress_display();
844        assert!(display.contains("0 of 2"));
845
846        // Running with current hook
847        state.mark_hook_running(0);
848        let display = state.progress_display();
849        assert!(display.contains("Executing hook 1 of 2"));
850
851        // Completed state
852        state.status = ExecutionStatus::Completed;
853        state.current_hook_index = None;
854        let display = state.progress_display();
855        assert_eq!(display, "All hooks completed successfully");
856
857        // Failed state
858        state.status = ExecutionStatus::Failed;
859        state.error_message = Some("Test error".to_string());
860        let display = state.progress_display();
861        assert!(display.contains("Hook execution failed: Test error"));
862    }
863
864    #[tokio::test]
865    async fn test_state_directory_cleanup() {
866        let temp_dir = TempDir::new().unwrap();
867        let state_manager = StateManager::new(temp_dir.path().to_path_buf());
868
869        // Create multiple states with different statuses
870        let completed_state = HookExecutionState {
871            instance_hash: "completed_hash".to_string(),
872            directory_path: PathBuf::from("/completed"),
873            config_hash: "config1".to_string(),
874            status: ExecutionStatus::Completed,
875            total_hooks: 1,
876            completed_hooks: 1,
877            current_hook_index: None,
878            hooks: vec![],
879            hook_results: HashMap::new(),
880            environment_vars: HashMap::new(),
881            started_at: Utc::now() - chrono::Duration::hours(1),
882            finished_at: Some(Utc::now() - chrono::Duration::minutes(30)),
883            current_hook_started_at: None,
884            completed_display_until: None,
885            error_message: None,
886            previous_env: None,
887        };
888
889        let running_state = HookExecutionState {
890            instance_hash: "running_hash".to_string(),
891            directory_path: PathBuf::from("/running"),
892            config_hash: "config2".to_string(),
893            status: ExecutionStatus::Running,
894            total_hooks: 2,
895            completed_hooks: 1,
896            current_hook_index: Some(1),
897            hooks: vec![],
898            hook_results: HashMap::new(),
899            environment_vars: HashMap::new(),
900            started_at: Utc::now() - chrono::Duration::minutes(5),
901            finished_at: None,
902            current_hook_started_at: None,
903            completed_display_until: None,
904            error_message: None,
905            previous_env: None,
906        };
907
908        let failed_state = HookExecutionState {
909            instance_hash: "failed_hash".to_string(),
910            directory_path: PathBuf::from("/failed"),
911            config_hash: "config3".to_string(),
912            status: ExecutionStatus::Failed,
913            total_hooks: 1,
914            completed_hooks: 0,
915            current_hook_index: None,
916            hooks: vec![],
917            hook_results: HashMap::new(),
918            environment_vars: HashMap::new(),
919            started_at: Utc::now() - chrono::Duration::hours(2),
920            finished_at: Some(Utc::now() - chrono::Duration::hours(1)),
921            current_hook_started_at: None,
922            completed_display_until: None,
923            error_message: Some("Test failure".to_string()),
924            previous_env: None,
925        };
926
927        // Save all states
928        state_manager.save_state(&completed_state).await.unwrap();
929        state_manager.save_state(&running_state).await.unwrap();
930        state_manager.save_state(&failed_state).await.unwrap();
931
932        // Verify all states exist
933        let states = state_manager.list_active_states().await.unwrap();
934        assert_eq!(states.len(), 3);
935
936        // Clean up completed states
937        let cleaned = state_manager.cleanup_state_directory().await.unwrap();
938        assert_eq!(cleaned, 2); // Should clean up completed and failed states
939
940        // Verify only running state remains
941        let remaining_states = state_manager.list_active_states().await.unwrap();
942        assert_eq!(remaining_states.len(), 1);
943        assert_eq!(remaining_states[0].instance_hash, "running_hash");
944    }
945
946    #[tokio::test]
947    async fn test_cleanup_orphaned_states() {
948        let temp_dir = TempDir::new().unwrap();
949        let state_manager = StateManager::new(temp_dir.path().to_path_buf());
950
951        // Create an old running state (orphaned)
952        let orphaned_state = HookExecutionState {
953            instance_hash: "orphaned_hash".to_string(),
954            directory_path: PathBuf::from("/orphaned"),
955            config_hash: "config".to_string(),
956            status: ExecutionStatus::Running,
957            total_hooks: 1,
958            completed_hooks: 0,
959            current_hook_index: Some(0),
960            hooks: vec![],
961            hook_results: HashMap::new(),
962            environment_vars: HashMap::new(),
963            started_at: Utc::now() - chrono::Duration::hours(3),
964            finished_at: None,
965            current_hook_started_at: None,
966            completed_display_until: None,
967            error_message: None,
968            previous_env: None,
969        };
970
971        // Create a recent running state (not orphaned)
972        let recent_state = HookExecutionState {
973            instance_hash: "recent_hash".to_string(),
974            directory_path: PathBuf::from("/recent"),
975            config_hash: "config".to_string(),
976            status: ExecutionStatus::Running,
977            total_hooks: 1,
978            completed_hooks: 0,
979            current_hook_index: Some(0),
980            hooks: vec![],
981            hook_results: HashMap::new(),
982            environment_vars: HashMap::new(),
983            started_at: Utc::now() - chrono::Duration::minutes(5),
984            finished_at: None,
985            current_hook_started_at: None,
986            completed_display_until: None,
987            error_message: None,
988            previous_env: None,
989        };
990
991        // Save both states
992        state_manager.save_state(&orphaned_state).await.unwrap();
993        state_manager.save_state(&recent_state).await.unwrap();
994
995        // Clean up orphaned states older than 1 hour
996        let cleaned = state_manager
997            .cleanup_orphaned_states(chrono::Duration::hours(1))
998            .await
999            .unwrap();
1000        assert_eq!(cleaned, 1); // Should clean up only the orphaned state
1001
1002        // Verify only recent state remains
1003        let remaining_states = state_manager.list_active_states().await.unwrap();
1004        assert_eq!(remaining_states.len(), 1);
1005        assert_eq!(remaining_states[0].instance_hash, "recent_hash");
1006    }
1007
1008    #[tokio::test]
1009    async fn test_corrupted_state_file_handling() {
1010        let temp_dir = TempDir::new().unwrap();
1011        let state_dir = temp_dir.path().join("state");
1012        let state_manager = StateManager::new(state_dir.clone());
1013
1014        // Ensure state directory exists
1015        state_manager.ensure_state_dir().await.unwrap();
1016
1017        // Write corrupted JSON to a state file
1018        let corrupted_file = state_dir.join("corrupted.json");
1019        tokio::fs::write(&corrupted_file, "{invalid json}")
1020            .await
1021            .unwrap();
1022
1023        // List active states should handle the corrupted file gracefully
1024        let states = state_manager.list_active_states().await.unwrap();
1025        assert_eq!(states.len(), 0); // Corrupted file should be skipped
1026
1027        // Cleanup should remove the corrupted file
1028        let cleaned = state_manager.cleanup_state_directory().await.unwrap();
1029        assert_eq!(cleaned, 1);
1030
1031        // Verify the corrupted file is gone
1032        assert!(!corrupted_file.exists());
1033    }
1034
1035    #[tokio::test]
1036    async fn test_concurrent_state_modifications() {
1037        use tokio::task;
1038
1039        let temp_dir = TempDir::new().unwrap();
1040        let state_manager = Arc::new(StateManager::new(temp_dir.path().to_path_buf()));
1041
1042        // Create initial state
1043        let initial_state = HookExecutionState {
1044            instance_hash: "concurrent_hash".to_string(),
1045            directory_path: PathBuf::from("/concurrent"),
1046            config_hash: "config".to_string(),
1047            status: ExecutionStatus::Running,
1048            total_hooks: 10,
1049            completed_hooks: 0,
1050            current_hook_index: Some(0),
1051            hooks: vec![],
1052            hook_results: HashMap::new(),
1053            environment_vars: HashMap::new(),
1054            started_at: Utc::now(),
1055            finished_at: None,
1056            current_hook_started_at: None,
1057            completed_display_until: None,
1058            error_message: None,
1059            previous_env: None,
1060        };
1061
1062        state_manager.save_state(&initial_state).await.unwrap();
1063
1064        // Spawn multiple tasks that concurrently modify the state
1065        let mut handles = vec![];
1066
1067        for i in 0..5 {
1068            let sm = state_manager.clone();
1069            let path = initial_state.directory_path.clone();
1070
1071            let handle = task::spawn(async move {
1072                // Load state - it might have been modified by another task
1073                let instance_hash = compute_instance_hash(&path, "concurrent_config");
1074
1075                // Simulate some work
1076                tokio::time::sleep(Duration::from_millis(10)).await;
1077
1078                // Load state, modify, and save (handle potential concurrent modifications)
1079                if let Ok(Some(mut state)) = sm.load_state(&instance_hash).await {
1080                    state.completed_hooks += 1;
1081                    state.current_hook_index = Some(i + 1);
1082
1083                    // Save state - ignore errors from concurrent saves
1084                    let _ = sm.save_state(&state).await;
1085                }
1086            });
1087
1088            handles.push(handle);
1089        }
1090
1091        // Wait for all tasks to complete
1092        for handle in handles {
1093            handle.await.unwrap();
1094        }
1095
1096        // Verify final state - due to concurrent writes, the exact values may vary
1097        // but the state should be loadable and valid
1098        let final_state = state_manager
1099            .load_state(&initial_state.instance_hash)
1100            .await
1101            .unwrap();
1102
1103        // The state might exist or not depending on timing of concurrent operations
1104        if let Some(state) = final_state {
1105            assert_eq!(state.instance_hash, "concurrent_hash");
1106            // Completed hooks will be 0 if all concurrent writes failed, or > 0 if some succeeded
1107        }
1108    }
1109
1110    #[tokio::test]
1111    async fn test_state_with_unicode_and_special_chars() {
1112        let temp_dir = TempDir::new().unwrap();
1113        let state_manager = StateManager::new(temp_dir.path().to_path_buf());
1114
1115        // Create state with unicode and special characters
1116        let mut unicode_state = HookExecutionState {
1117            instance_hash: "unicode_hash".to_string(),
1118            directory_path: PathBuf::from("/测试/目录/🚀"),
1119            config_hash: "config_ñ_é_ü".to_string(),
1120            status: ExecutionStatus::Failed,
1121            total_hooks: 1,
1122            completed_hooks: 1,
1123            current_hook_index: None,
1124            hooks: vec![],
1125            hook_results: HashMap::new(),
1126            environment_vars: HashMap::new(),
1127            started_at: Utc::now(),
1128            finished_at: Some(Utc::now()),
1129            current_hook_started_at: None,
1130            completed_display_until: None,
1131            error_message: Some("Error: 错误信息 with émojis 🔥💥".to_string()),
1132            previous_env: None,
1133        };
1134
1135        // Add hook result with unicode output
1136        let unicode_hook = Hook {
1137            order: 100,
1138            propagate: false,
1139            command: "echo".to_string(),
1140            args: vec![],
1141            dir: None,
1142            inputs: vec![],
1143            source: None,
1144        };
1145        let unicode_result = HookResult {
1146            hook: unicode_hook,
1147            success: false,
1148            exit_status: Some(1),
1149            stdout: "输出: Hello 世界! 🌍".to_string(),
1150            stderr: "错误: ñoño error ⚠️".to_string(),
1151            duration_ms: 100,
1152            error: Some("失败了 😢".to_string()),
1153        };
1154        unicode_state.hook_results.insert(0, unicode_result);
1155
1156        // Save and load the state
1157        state_manager.save_state(&unicode_state).await.unwrap();
1158
1159        let loaded = state_manager
1160            .load_state(&unicode_state.instance_hash)
1161            .await
1162            .unwrap()
1163            .unwrap();
1164
1165        // Verify all unicode content is preserved
1166        assert_eq!(loaded.config_hash, "config_ñ_é_ü");
1167        assert_eq!(
1168            loaded.error_message,
1169            Some("Error: 错误信息 with émojis 🔥💥".to_string())
1170        );
1171
1172        let hook_result = loaded.hook_results.get(&0).unwrap();
1173        assert_eq!(hook_result.stdout, "输出: Hello 世界! 🌍");
1174        assert_eq!(hook_result.stderr, "错误: ñoño error ⚠️");
1175        assert_eq!(hook_result.error, Some("失败了 😢".to_string()));
1176    }
1177
1178    #[tokio::test]
1179    async fn test_state_directory_with_many_states() {
1180        let temp_dir = TempDir::new().unwrap();
1181        let state_manager = StateManager::new(temp_dir.path().to_path_buf());
1182
1183        // Create many states to test scalability
1184        for i in 0..50 {
1185            let state = HookExecutionState {
1186                instance_hash: format!("hash_{}", i),
1187                directory_path: PathBuf::from(format!("/dir/{}", i)),
1188                config_hash: format!("config_{}", i),
1189                status: if i % 3 == 0 {
1190                    ExecutionStatus::Completed
1191                } else if i % 3 == 1 {
1192                    ExecutionStatus::Running
1193                } else {
1194                    ExecutionStatus::Failed
1195                },
1196                total_hooks: 1,
1197                completed_hooks: if i % 3 == 0 { 1 } else { 0 },
1198                current_hook_index: if i % 3 == 1 { Some(0) } else { None },
1199                hooks: vec![],
1200                hook_results: HashMap::new(),
1201                environment_vars: HashMap::new(),
1202                started_at: Utc::now() - chrono::Duration::hours(i as i64),
1203                finished_at: if i % 3 != 1 {
1204                    Some(Utc::now() - chrono::Duration::hours(i as i64 - 1))
1205                } else {
1206                    None
1207                },
1208                current_hook_started_at: None,
1209                completed_display_until: None,
1210                error_message: if i % 3 == 2 {
1211                    Some(format!("Error {}", i))
1212                } else {
1213                    None
1214                },
1215                previous_env: None,
1216            };
1217            state_manager.save_state(&state).await.unwrap();
1218        }
1219
1220        // List all states
1221        let listed = state_manager.list_active_states().await.unwrap();
1222        assert_eq!(listed.len(), 50);
1223
1224        // Clean up old completed states (older than 24 hours)
1225        let cleaned = state_manager
1226            .cleanup_orphaned_states(chrono::Duration::hours(24))
1227            .await
1228            .unwrap();
1229
1230        // Should clean up states older than 24 hours
1231        assert!(cleaned > 0);
1232    }
1233}