aca 0.3.1

A Rust-based agentic tool that automates coding tasks using Claude Code and OpenAI Codex CLI integrations
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
//! # High-Level System Integration and Orchestration
//!
//! Combines all subsystems into a cohesive agent architecture with
//! coordinated task processing and system-wide status monitoring.
//!
//! ## Core Components
//!
//! - **[`AgentSystem`]**: Main orchestrator coordinating all subsystems
//! - **[`AgentConfig`]**: Unified configuration for all system components
//! - **[`SystemStatus`]**: Comprehensive system health and performance monitoring
//!
//! ## System Architecture
//!
//! ```text
//! ┌─────────────────────────────────────────────────┐
//! │                 AgentSystem                     │
//! │  ┌─────────────┐ ┌─────────────┐ ┌─────────────┐│
//! │  │    Task     │ │   Session   │ │   Claude    ││
//! │  │  Manager    │ │   Manager   │ │ Interface   ││
//! │  └─────────────┘ └─────────────┘ └─────────────┘│
//! └─────────────────────────────────────────────────┘
//! ```
//!
//! ## Key Features
//!
//! ### 🎯 Unified Orchestration
//! - Seamless integration of task management, session persistence, and LLM interfaces
//! - Coordinated lifecycle management across all subsystems
//! - Centralized configuration and initialization
//! - Graceful startup and shutdown procedures
//!
//! ### 🔄 End-to-End Task Processing
//! - Complete task lifecycle from creation to completion
//! - Automatic state persistence at each stage
//! - Error handling and recovery across system boundaries
//! - Progress tracking and status reporting
//!
//! ### 📊 System Monitoring
//! - Real-time health monitoring across all components
//! - Performance metrics aggregation and reporting
//! - Resource utilization tracking
//! - Unified status reporting interface
//!
//! ### 🛡️ Fault Tolerance
//! - Graceful error handling across subsystem boundaries
//! - Automatic recovery from transient failures
//! - State consistency maintenance during errors
//! - Rollback capabilities for failed operations
//!
//! ### ⚙️ Configuration Management
//! - Unified configuration interface for all subsystems
//! - Environment-based configuration loading
//! - Runtime configuration updates
//! - Validation and error reporting
//!
//! ## Example Usage
//!
//! ```rust,no_run
//! use aca::{AgentSystem, AgentConfig};
//!
//! #[tokio::main]
//! async fn main() -> anyhow::Result<()> {
//!     // Initialize with default configuration
//!     let config = AgentConfig::default();
//!     let agent = AgentSystem::new(config).await?;
//!
//!     // Create and process a task
//!     let task_id = agent.create_and_process_task(
//!         "Implement REST API",
//!         "Create a new REST API endpoint for user management"
//!     ).await?;
//!
//!     // Monitor system status
//!     let status = agent.get_system_status().await?;
//!     println!("System health: {}", status.is_healthy);
//!
//!     // Graceful shutdown
//!     agent.shutdown().await?;
//!     Ok(())
//! }
//! ```

use crate::claude::{ClaudeCodeInterface, ClaudeConfig};
use crate::session::{SessionInitOptions, SessionManager, SessionManagerConfig};
use crate::task::{
    ErrorHandler, ErrorStrategy, OutputCondition, SetupCommand, SetupResult, TaskManager,
    TaskManagerConfig, TaskSpec, TaskStatus,
};
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::process::Stdio;
use std::sync::Arc;
use std::time::Instant;
use tokio::process::Command;
use tracing::{info, warn};
use uuid::Uuid;

/// Integrated agent system that combines task management, session persistence, and Claude integration
pub struct AgentSystem {
    task_manager: Arc<TaskManager>,
    session_manager: Arc<SessionManager>,
    claude_interface: Arc<ClaudeCodeInterface>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgentConfig {
    pub workspace_path: std::path::PathBuf,
    pub session_config: SessionManagerConfig,
    pub task_config: TaskManagerConfig,
    pub claude_config: ClaudeConfig,
    pub setup_commands: Vec<SetupCommand>,
}

impl AgentConfig {
    /// Enable subprocess output streaming (typically from --verbose flag)
    pub fn with_subprocess_output(mut self, show_output: bool) -> Self {
        tracing::info!("Setting show_subprocess_output to: {}", show_output);
        self.claude_config.show_subprocess_output = show_output;
        self
    }
}

impl AgentSystem {
    pub async fn new(config: AgentConfig) -> Result<Self> {
        // Execute setup commands first, before any other initialization
        if !config.setup_commands.is_empty() {
            info!("Executing setup commands before system initialization...");
            Self::execute_setup_commands(&config.setup_commands).await?;
        }

        // Extract workspace path before moving config
        let workspace_path = config.workspace_path.clone();

        // Initialize session manager
        let session_manager = Arc::new(
            SessionManager::new(
                config.workspace_path,
                config.session_config,
                SessionInitOptions::default(),
            )
            .await?,
        );

        // Initialize task manager
        let task_manager = Arc::new(TaskManager::new(config.task_config));

        // Initialize Claude interface
        let claude_interface = Arc::new(
            ClaudeCodeInterface::new(config.claude_config, workspace_path)
                .await
                .map_err(|e| anyhow::anyhow!("Failed to initialize Claude interface: {}", e))?,
        );

        Ok(Self {
            task_manager,
            session_manager,
            claude_interface,
        })
    }

    /// Process a single task with Claude integration and full persistence
    pub async fn process_task(&self, task_id: Uuid) -> Result<()> {
        // Get task from task manager
        let task = self.task_manager.get_task(task_id).await?;

        tracing::info!("Processing task: {} - {}", task.id, task.title);

        // Update task status to in progress
        self.task_manager
            .update_task_status(
                task_id,
                TaskStatus::InProgress {
                    started_at: chrono::Utc::now(),
                    estimated_completion: None,
                },
            )
            .await?;

        // Save current state
        self.save_session_state().await?;

        // Process with Claude (session_dir coordination will be added in follow-up)
        // TODO: Pass session_dir from session_manager.get_session_dir().await
        let result = self.claude_interface.process_task(&task, None).await;

        match result {
            Ok(completed_task) => {
                // Update task status to completed
                self.task_manager
                    .update_task_status(task_id, completed_task.status)
                    .await?;

                // Save session state
                self.save_session_state().await?;

                tracing::info!("Task completed successfully: {}", task_id);
                Ok(())
            }
            Err(e) => {
                // Mark task as failed
                self.task_manager
                    .update_task_status(
                        task_id,
                        TaskStatus::Failed {
                            failed_at: chrono::Utc::now(),
                            error: crate::task::types::TaskError::ClaudeError {
                                message: format!("Failed: {}", e),
                                error_code: None,
                                retry_possible: true,
                            },
                            retry_count: 0,
                        },
                    )
                    .await?;

                self.save_session_state().await?;

                tracing::error!("Task failed: {} - {}", task_id, e);
                Err(anyhow::anyhow!("Task processing failed: {}", e))
            }
        }
    }

    /// Create and process a new task
    pub async fn create_and_process_task(&self, title: &str, description: &str) -> Result<Uuid> {
        // Create task spec
        let task_spec = TaskSpec {
            title: title.to_string(),
            description: description.to_string(),
            dependencies: Vec::new(),
            metadata: crate::task::types::TaskMetadata {
                priority: crate::task::types::TaskPriority::Normal,
                estimated_complexity: Some(crate::task::types::ComplexityLevel::Moderate),
                estimated_duration: Some(
                    chrono::Duration::from_std(std::time::Duration::from_secs(300)).unwrap(),
                ),
                repository_refs: Vec::new(),
                file_refs: Vec::new(),
                tags: vec!["auto-generated".to_string()],
                context_requirements: crate::task::types::ContextRequirements {
                    required_files: Vec::new(),
                    required_repositories: Vec::new(),
                    build_dependencies: Vec::new(),
                    environment_vars: std::collections::HashMap::new(),
                    claude_context_keys: Vec::new(),
                },
            },
        };

        // Create task in task manager
        let task_id = self.task_manager.create_task(task_spec, None).await?;

        // Save state
        self.save_session_state().await?;

        // Process the task
        self.process_task(task_id).await?;

        Ok(task_id)
    }

    /// Execute a complete execution plan with setup commands and tasks
    pub async fn execute_plan(&self, plan: crate::task::ExecutionPlan) -> Result<Vec<uuid::Uuid>> {
        use tracing::{error, info, warn};

        info!("Executing plan: {}", plan.summary());

        // Validate the execution plan
        if let Err(validation_error) = plan.validate() {
            return Err(anyhow::anyhow!(
                "Invalid execution plan: {}",
                validation_error
            ));
        }

        let mut task_ids = Vec::new();

        // Phase 1: Execute setup commands
        if plan.has_setup_commands() {
            info!("Executing {} setup commands...", plan.setup_command_count());
            Self::execute_setup_commands(&plan.setup_commands).await?;
            info!("Setup commands completed successfully");
        }

        // Phase 2: Process tasks based on execution mode
        if plan.has_tasks() {
            info!(
                "Processing {} tasks with mode: {:?}",
                plan.task_count(),
                plan.execution_mode
            );

            let total_tasks = plan.task_count();

            match plan.execution_mode {
                crate::task::ExecutionMode::Sequential => {
                    // Execute tasks one by one
                    for (index, task_spec) in plan.task_specs.into_iter().enumerate() {
                        let task_num = index + 1;

                        info!(
                            "Processing task {}/{}: {}",
                            task_num, total_tasks, task_spec.title
                        );

                        match self.create_and_process_task_spec(task_spec).await {
                            Ok(task_id) => {
                                info!(
                                    "Task {}/{} completed successfully: {}",
                                    task_num, total_tasks, task_id
                                );
                                task_ids.push(task_id);
                            }
                            Err(e) => {
                                error!("Task {}/{} failed: {}", task_num, total_tasks, e);
                                // For sequential mode, we can choose to continue or fail
                                // For now, we'll continue but log the error
                                warn!("Continuing with remaining tasks despite failure");
                            }
                        }
                    }
                }
                crate::task::ExecutionMode::Parallel { max_concurrent: _ } => {
                    warn!(
                        "Parallel execution mode not yet fully implemented, falling back to sequential"
                    );
                    // TODO: Implement parallel execution with semaphore for max_concurrent
                    // For now, fall back to sequential execution
                    for task_spec in plan.task_specs {
                        match self.create_and_process_task_spec(task_spec).await {
                            Ok(task_id) => task_ids.push(task_id),
                            Err(e) => warn!("Task failed in parallel mode: {}", e),
                        }
                    }
                }
                crate::task::ExecutionMode::Intelligent => {
                    warn!(
                        "Intelligent execution mode not yet implemented, falling back to sequential"
                    );
                    // TODO: Implement intelligent scheduling based on task metadata
                    // For now, fall back to sequential execution
                    for task_spec in plan.task_specs {
                        match self.create_and_process_task_spec(task_spec).await {
                            Ok(task_id) => task_ids.push(task_id),
                            Err(e) => warn!("Task failed in intelligent mode: {}", e),
                        }
                    }
                }
            }
        }

        // Save session state with checkpoint after plan execution
        self.save_session_checkpoint("plan_execution_complete")
            .await?;

        info!(
            "Execution plan completed: {} tasks processed",
            task_ids.len()
        );
        Ok(task_ids)
    }

    /// Create and process a task from a TaskSpec (internal helper)
    async fn create_and_process_task_spec(
        &self,
        task_spec: crate::task::TaskSpec,
    ) -> Result<uuid::Uuid> {
        // Create task in the task manager
        let task_id = self.task_manager.create_task(task_spec, None).await?;

        // Process the task
        self.process_task(task_id).await?;

        Ok(task_id)
    }

    /// Convert AgentConfig to ExecutionPlan (for structured config mode)
    pub fn agent_config_to_execution_plan(config: &AgentConfig) -> crate::task::ExecutionPlan {
        use tracing::info;

        info!(
            "Converting AgentConfig to ExecutionPlan with {} setup commands",
            config.setup_commands.len()
        );

        let plan_name = format!(
            "Structured Config: {}",
            config
                .workspace_path
                .file_name()
                .and_then(|n| n.to_str())
                .unwrap_or("workspace")
        );

        let plan = crate::task::ExecutionPlan::new()
            .with_setup_commands(config.setup_commands.clone())
            .with_metadata(
                plan_name,
                "Execution plan generated from structured TOML configuration",
            )
            .with_tags(vec![
                "structured-config".to_string(),
                "auto-generated".to_string(),
            ])
            .with_sequential_execution();

        info!("Created execution plan: {}", plan.summary());
        plan
    }

    /// Get system status
    pub async fn get_system_status(&self) -> Result<SystemStatus> {
        let task_stats = self.task_manager.get_statistics().await?;
        let claude_status = self.claude_interface.get_interface_status().await;
        let session_stats = self.session_manager.get_session_statistics().await?;

        Ok(SystemStatus {
            task_stats: task_stats.clone(),
            claude_status: claude_status.clone(),
            session_stats,
            is_healthy: claude_status.is_healthy && task_stats.total_tasks > 0,
        })
    }

    /// Save current session state
    async fn save_session_state(&self) -> Result<()> {
        // Only save session state, don't create checkpoint
        // Checkpoints are created at major milestones (plan completion, shutdown)
        self.session_manager.save_session().await?;
        Ok(())
    }

    /// Save session state and create a checkpoint
    async fn save_session_checkpoint(&self, description: &str) -> Result<()> {
        self.session_manager.save_session().await?;
        self.session_manager
            .create_checkpoint(description.to_string())
            .await?;
        Ok(())
    }

    /// Graceful shutdown
    pub async fn shutdown(&self) -> Result<()> {
        tracing::info!("Shutting down agent system...");

        // Save final state with checkpoint
        self.save_session_checkpoint("agent_shutdown").await?;

        // Graceful shutdown of session manager (this also creates a session_shutdown checkpoint)
        self.session_manager.shutdown().await?;

        tracing::info!("Agent system shutdown complete");
        Ok(())
    }

    // ============================================================================
    // Setup Command Execution
    // ============================================================================

    /// Execute all setup commands with error handling
    async fn execute_setup_commands(commands: &[SetupCommand]) -> Result<()> {
        if commands.is_empty() {
            return Ok(());
        }

        info!("Running {} setup commands...", commands.len());

        for (index, cmd) in commands.iter().enumerate() {
            info!(
                "Executing setup command {}/{}: {}",
                index + 1,
                commands.len(),
                cmd.name
            );

            let result = Self::execute_shell_command(cmd).await?;

            if !result.success {
                info!(
                    "Setup command '{}' failed with exit code: {}",
                    cmd.name, result.exit_code
                );

                if let Some(handler) = &cmd.error_handler {
                    if let Err(e) = Self::handle_command_error(cmd, &result, handler).await {
                        if cmd.required {
                            return Err(anyhow::anyhow!(
                                "Required setup command '{}' failed: {}",
                                cmd.name,
                                e
                            ));
                        } else {
                            warn!("Optional setup command '{}' failed: {}", cmd.name, e);
                        }
                    }
                } else if cmd.required {
                    return Err(anyhow::anyhow!(
                        "Required setup command '{}' failed: {}",
                        cmd.name,
                        result.stderr
                    ));
                } else {
                    warn!(
                        "Optional setup command '{}' failed: {}",
                        cmd.name, result.stderr
                    );
                }
            } else {
                info!("Setup command '{}' completed successfully", cmd.name);
            }
        }

        info!("All setup commands completed successfully");
        Ok(())
    }

    /// Execute a single shell command
    async fn execute_shell_command(cmd: &SetupCommand) -> Result<SetupResult> {
        let start_time = Instant::now();

        // Build the command
        let mut command = Command::new(&cmd.command);
        command.args(&cmd.args);

        if let Some(working_dir) = &cmd.working_dir {
            command.current_dir(working_dir);
        }

        command.stdout(Stdio::piped()).stderr(Stdio::piped());

        // Execute with timeout
        let result = if let Some(timeout) = cmd.timeout {
            let timeout_std = timeout
                .to_std()
                .map_err(|_| anyhow::anyhow!("Invalid timeout duration: {:?}", timeout))?;
            tokio::time::timeout(timeout_std, command.output())
                .await
                .map_err(|_| anyhow::anyhow!("Command timed out after {:?}", timeout))?
        } else {
            command.output().await
        };

        let duration = start_time.elapsed();

        match result {
            Ok(output) => {
                let success = output.status.success();
                let exit_code = output.status.code().unwrap_or(-1);
                let stdout = String::from_utf8_lossy(&output.stdout).to_string();
                let stderr = String::from_utf8_lossy(&output.stderr).to_string();

                Ok(SetupResult {
                    command_id: cmd.id,
                    success,
                    exit_code,
                    stdout,
                    stderr,
                    duration: chrono::Duration::from_std(duration).unwrap_or_default(),
                })
            }
            Err(e) => Err(anyhow::anyhow!("Failed to execute command: {}", e)),
        }
    }

    /// Handle command execution errors
    async fn handle_command_error(
        cmd: &SetupCommand,
        result: &SetupResult,
        handler: &ErrorHandler,
    ) -> Result<()> {
        match &handler.strategy {
            ErrorStrategy::Skip => {
                warn!("Skipping failed command: {}", cmd.name);
                Ok(())
            }
            ErrorStrategy::Retry {
                max_attempts,
                delay,
            } => {
                info!(
                    "Retrying command '{}' (max {} attempts)",
                    cmd.name, max_attempts
                );
                Self::retry_command(cmd, *max_attempts, *delay).await
            }
            ErrorStrategy::Backup {
                condition,
                backup_command,
                backup_args,
            } => {
                if Self::should_run_backup(result, condition) {
                    info!("Running backup command for: {}", cmd.name);
                    Self::execute_backup_command(backup_command, backup_args, &cmd.working_dir)
                        .await
                } else {
                    Err(anyhow::anyhow!(
                        "Backup condition not met for: {}",
                        cmd.name
                    ))
                }
            }
        }
    }

    /// Retry a command with delay
    async fn retry_command(
        cmd: &SetupCommand,
        max_attempts: u32,
        delay: chrono::Duration,
    ) -> Result<()> {
        for attempt in 1..=max_attempts {
            info!(
                "Retry attempt {}/{} for command: {}",
                attempt, max_attempts, cmd.name
            );

            if let Ok(delay_std) = delay.to_std() {
                tokio::time::sleep(delay_std).await;
            }

            let result = Self::execute_shell_command(cmd).await?;
            if result.success {
                info!(
                    "Command '{}' succeeded on retry attempt {}",
                    cmd.name, attempt
                );
                return Ok(());
            }

            if attempt == max_attempts {
                return Err(anyhow::anyhow!(
                    "Command '{}' failed after {} attempts. Last error: {}",
                    cmd.name,
                    max_attempts,
                    result.stderr
                ));
            }
        }
        Ok(())
    }

    /// Execute a backup command
    async fn execute_backup_command(
        backup_command: &str,
        backup_args: &[String],
        working_dir: &Option<std::path::PathBuf>,
    ) -> Result<()> {
        let mut command = Command::new(backup_command);
        command.args(backup_args);

        if let Some(working_dir) = working_dir {
            command.current_dir(working_dir);
        }

        command.stdout(Stdio::piped()).stderr(Stdio::piped());

        let output = command.output().await?;

        if output.status.success() {
            info!("Backup command executed successfully");
            Ok(())
        } else {
            let stderr = String::from_utf8_lossy(&output.stderr);
            Err(anyhow::anyhow!("Backup command failed: {}", stderr))
        }
    }

    /// Check if backup should be triggered based on output analysis
    fn should_run_backup(result: &SetupResult, condition: &OutputCondition) -> bool {
        // Check exit code range first
        if let Some((min, max)) = condition.exit_code_range
            && (result.exit_code < min || result.exit_code > max)
        {
            return false;
        }

        // Check for required text in output
        if let Some(must_contain) = &condition.contains {
            let output = if condition.check_stdout {
                &result.stdout
            } else {
                &result.stderr
            };
            if !output.contains(must_contain) {
                return false;
            }
        }

        // Check for forbidden text in output
        if let Some(must_not_contain) = &condition.not_contains {
            let output = if condition.check_stdout {
                &result.stdout
            } else {
                &result.stderr
            };
            if output.contains(must_not_contain) {
                return false;
            }
        }

        true
    }

    // Getters for individual components
    pub fn task_manager(&self) -> Arc<TaskManager> {
        self.task_manager.clone()
    }

    pub fn session_manager(&self) -> Arc<SessionManager> {
        self.session_manager.clone()
    }

    pub fn claude_interface(&self) -> Arc<ClaudeCodeInterface> {
        self.claude_interface.clone()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::task::{ErrorHandler, OutputCondition};
    use chrono::Duration;
    use std::path::PathBuf;

    #[tokio::test]
    async fn test_setup_command_execution_success() {
        let setup_command = SetupCommand::new("test_echo", "echo")
            .with_args(vec!["hello".to_string(), "world".to_string()]);

        let result = AgentSystem::execute_shell_command(&setup_command).await;
        assert!(result.is_ok());

        let setup_result = result.unwrap();
        assert!(setup_result.success);
        assert_eq!(setup_result.exit_code, 0);
        assert!(setup_result.stdout.contains("hello world"));
    }

    #[tokio::test]
    async fn test_setup_command_execution_failure() {
        let setup_command = SetupCommand::new("test_fail", "false"); // 'false' always exits with code 1

        let result = AgentSystem::execute_shell_command(&setup_command).await;
        assert!(result.is_ok());

        let setup_result = result.unwrap();
        assert!(!setup_result.success);
        assert_eq!(setup_result.exit_code, 1);
    }

    #[tokio::test]
    async fn test_setup_command_with_working_directory() {
        let setup_command =
            SetupCommand::new("test_pwd", "pwd").with_working_dir(PathBuf::from("/tmp"));

        let result = AgentSystem::execute_shell_command(&setup_command).await;
        assert!(result.is_ok());

        let setup_result = result.unwrap();
        assert!(setup_result.success);
        assert!(setup_result.stdout.trim().ends_with("tmp"));
    }

    #[tokio::test]
    async fn test_setup_command_timeout() {
        let setup_command = SetupCommand::new("test_timeout", "sleep")
            .with_args(vec!["2".to_string()])
            .with_timeout(Duration::milliseconds(100)); // Very short timeout

        let result = AgentSystem::execute_shell_command(&setup_command).await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("timed out"));
    }

    #[tokio::test]
    async fn test_should_run_backup_with_stderr_contains() {
        let result = SetupResult {
            command_id: uuid::Uuid::new_v4(),
            success: false,
            exit_code: 1,
            stdout: String::new(),
            stderr: "command not found".to_string(),
            duration: Duration::milliseconds(100),
        };

        let condition = OutputCondition::stderr_contains("command not found");
        assert!(AgentSystem::should_run_backup(&result, &condition));

        let condition = OutputCondition::stderr_contains("different error");
        assert!(!AgentSystem::should_run_backup(&result, &condition));
    }

    #[tokio::test]
    async fn test_should_run_backup_with_exit_code_range() {
        let result = SetupResult {
            command_id: uuid::Uuid::new_v4(),
            success: false,
            exit_code: 5,
            stdout: String::new(),
            stderr: String::new(),
            duration: Duration::milliseconds(100),
        };

        let condition = OutputCondition {
            exit_code_range: Some((1, 10)),
            ..Default::default()
        };
        assert!(AgentSystem::should_run_backup(&result, &condition));

        let condition = OutputCondition {
            exit_code_range: Some((1, 3)),
            ..Default::default()
        };
        assert!(!AgentSystem::should_run_backup(&result, &condition));
    }

    #[tokio::test]
    async fn test_setup_commands_execution_with_optional_failure() {
        let setup_commands = vec![
            // This should succeed
            SetupCommand::new("success_command", "echo").with_args(vec!["success".to_string()]),
            // This should fail but is optional
            SetupCommand::new("optional_fail", "false")
                .optional()
                .with_error_handler(ErrorHandler::skip("skip_false")),
            // This should succeed after the optional failure
            SetupCommand::new("final_success", "echo").with_args(vec!["final".to_string()]),
        ];

        let result = AgentSystem::execute_setup_commands(&setup_commands).await;
        assert!(
            result.is_ok(),
            "Setup commands should succeed even with optional failures"
        );
    }

    #[tokio::test]
    async fn test_setup_commands_required_failure() {
        let setup_commands = vec![
            SetupCommand::new("required_fail", "false"), // This is required and will fail
        ];

        let result = AgentSystem::execute_setup_commands(&setup_commands).await;
        assert!(
            result.is_err(),
            "Required command failure should cause setup to fail"
        );
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Required setup command")
        );
    }

    #[tokio::test]
    async fn test_backup_command_execution() {
        let result =
            AgentSystem::execute_backup_command("echo", &["backup executed".to_string()], &None)
                .await;

        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_retry_command_eventual_success() {
        // This test would be complex to implement without mocking
        // as it requires a command that fails initially but then succeeds
        // For now, we'll test the basic retry structure exists
        let setup_command = SetupCommand::new("test_retry", "true"); // 'true' always succeeds

        let result =
            AgentSystem::retry_command(&setup_command, 2, Duration::milliseconds(10)).await;
        assert!(result.is_ok());
    }

    #[test]
    fn test_setup_command_builder_pattern() {
        let cmd = SetupCommand::new("test", "echo")
            .with_args(vec!["hello".to_string()])
            .with_working_dir(PathBuf::from("/tmp"))
            .with_timeout(Duration::seconds(30))
            .optional()
            .with_error_handler(ErrorHandler::skip("test_skip"));

        assert_eq!(cmd.name, "test");
        assert_eq!(cmd.command, "echo");
        assert_eq!(cmd.args, vec!["hello".to_string()]);
        assert_eq!(cmd.working_dir, Some(PathBuf::from("/tmp")));
        assert_eq!(cmd.timeout, Some(Duration::seconds(30)));
        assert!(!cmd.required); // optional() sets required to false
        assert!(cmd.error_handler.is_some());
    }

    #[test]
    fn test_output_condition_helpers() {
        let condition = OutputCondition::stderr_contains("error text");
        assert!(!condition.check_stdout);
        assert!(condition.check_stderr);
        assert_eq!(condition.contains, Some("error text".to_string()));

        let condition = OutputCondition::stdout_contains("success text");
        assert!(condition.check_stdout);
        assert!(!condition.check_stderr);
        assert_eq!(condition.contains, Some("success text".to_string()));

        let condition = OutputCondition::exit_code_range(1, 5);
        assert_eq!(condition.exit_code_range, Some((1, 5)));
    }

    #[test]
    fn test_error_handler_constructors() {
        let handler = ErrorHandler::skip("test_skip");
        assert_eq!(handler.name, "test_skip");
        assert!(matches!(handler.strategy, ErrorStrategy::Skip));

        let handler = ErrorHandler::retry("test_retry", 3, Duration::seconds(1));
        assert_eq!(handler.name, "test_retry");
        assert!(matches!(
            handler.strategy,
            ErrorStrategy::Retry {
                max_attempts: 3,
                ..
            }
        ));

        let handler = ErrorHandler::backup(
            "test_backup",
            OutputCondition::default(),
            "echo",
            vec!["backup".to_string()],
        );
        assert_eq!(handler.name, "test_backup");
        assert!(matches!(handler.strategy, ErrorStrategy::Backup { .. }));
    }

    #[test]
    fn test_agent_config_with_setup_commands() {
        let setup_commands = vec![
            SetupCommand::new("test1", "echo"),
            SetupCommand::new("test2", "ls"),
        ];

        let config = AgentConfig {
            setup_commands: setup_commands.clone(),
            ..Default::default()
        };

        assert_eq!(config.setup_commands.len(), 2);
        assert_eq!(config.setup_commands[0].name, "test1");
        assert_eq!(config.setup_commands[1].name, "test2");
    }

    #[test]
    fn test_agent_config_toml_serialization() {
        let config = AgentConfig::default();

        // Test serialization to TOML string
        let toml_str = config
            .to_toml_string()
            .expect("Failed to serialize to TOML");
        assert!(!toml_str.is_empty());
        assert!(toml_str.contains("workspace_path"));

        // Test deserialization from TOML string
        let deserialized =
            AgentConfig::from_toml_str(&toml_str).expect("Failed to deserialize from TOML");

        // Compare some key fields
        assert_eq!(config.workspace_path, deserialized.workspace_path);
        assert_eq!(
            config.setup_commands.len(),
            deserialized.setup_commands.len()
        );
    }

    #[test]
    fn test_agent_config_toml_file_operations() {
        use tempfile::NamedTempFile;

        let config = AgentConfig::default();

        // Create a temporary file
        let temp_file = NamedTempFile::new().expect("Failed to create temp file");
        let temp_path = temp_file.path();

        // Test saving to file
        config
            .to_toml_file(temp_path)
            .expect("Failed to save config to file");

        // Test loading from file
        let loaded_config =
            AgentConfig::from_toml_file(temp_path).expect("Failed to load config from file");

        // Verify the loaded config matches the original
        assert_eq!(config.workspace_path, loaded_config.workspace_path);
        assert_eq!(
            config.setup_commands.len(),
            loaded_config.setup_commands.len()
        );
    }
}

#[derive(Debug, Clone)]
pub struct SystemStatus {
    pub task_stats: crate::task::TaskTreeStatistics,
    pub claude_status: crate::claude::interface::ClaudeInterfaceStatus,
    pub session_stats: crate::session::SessionStatistics,
    pub is_healthy: bool,
}

impl AgentConfig {
    /// Load configuration from a TOML file
    pub fn from_toml_file<P: AsRef<std::path::Path>>(path: P) -> Result<Self> {
        let content = std::fs::read_to_string(path).context("Failed to read config file")?;
        Self::from_toml_str(&content)
    }

    /// Load configuration from a TOML string
    pub fn from_toml_str(content: &str) -> Result<Self> {
        toml::from_str(content).context("Failed to parse TOML configuration")
    }

    /// Save configuration to a TOML file
    pub fn to_toml_file<P: AsRef<std::path::Path>>(&self, path: P) -> Result<()> {
        let content = self.to_toml_string()?;
        std::fs::write(path, content).context("Failed to write config file")
    }

    /// Convert configuration to a TOML string
    pub fn to_toml_string(&self) -> Result<String> {
        toml::to_string_pretty(self).context("Failed to serialize configuration to TOML")
    }
}

impl Default for AgentConfig {
    fn default() -> Self {
        // Create a proper test/temp directory instead of using current dir
        let workspace_path = std::env::temp_dir().join("aca");

        Self {
            workspace_path,
            session_config: SessionManagerConfig::default(),
            task_config: TaskManagerConfig::default(),
            claude_config: ClaudeConfig::default(),
            setup_commands: Vec::new(), // No setup commands by default
        }
    }
}