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
use aca::cli::{
    Args, BatchConfig, ConfigDiscovery, ExecutionMode, InteractiveConfig, TaskInput, TaskLoader,
    args::ResumeConfig,
};
use aca::env;
use aca::session::persistence::PersistenceConfig;
use aca::session::recovery::RecoveryConfig;
use aca::session::{SessionInitOptions, SessionManager, SessionManagerConfig};
use aca::task::ExecutionPlan;
use aca::task::TaskStatus;
use aca::task::manager::TaskManagerConfig;
use aca::{AgentConfig, AgentSystem};
use std::io::{self, Write};
use std::path::Path;
use tracing::{error, info};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Initialize logging
    tracing_subscriber::fmt()
        .with_env_filter("automatic_coding_agent=info")
        .init();

    info!("Starting Automatic Coding Agent");

    // Parse command line arguments
    let args = Args::parse();

    // Execute based on mode
    let mode = match args.mode() {
        Ok(mode) => mode,
        Err(e) => {
            eprintln!("Error: {}", e);
            std::process::exit(1);
        }
    };

    match mode {
        ExecutionMode::Batch(config) => run_batch_mode(config).await,
        ExecutionMode::Interactive(config) => run_interactive_mode(config).await,
        ExecutionMode::Resume(config) => run_resume_mode(config).await,
        ExecutionMode::ListCheckpoints { all_sessions } => {
            list_available_checkpoints(all_sessions).await
        }
        ExecutionMode::CreateCheckpoint(description) => create_manual_checkpoint(description).await,
        ExecutionMode::ShowConfig => {
            ConfigDiscovery::show_discovery_info();
            Ok(())
        }
    }
}

async fn run_batch_mode(config: BatchConfig) -> Result<(), Box<dyn std::error::Error>> {
    info!(
        "Running in batch mode with task input: {:?}",
        config.task_input
    );

    // Discover and load configuration
    let agent_config = if let Some(ref config_override) = config.config_override {
        info!("Loading configuration override from: {:?}", config_override);
        AgentConfig::from_toml_file(config_override)?
    } else {
        info!("Discovering default configuration...");
        let default_config = ConfigDiscovery::discover_config()?;
        default_config.to_agent_config(config.workspace_override.clone())
    };

    // Convert task input to execution plan
    let execution_plan = match &config.task_input {
        TaskInput::ConfigWithTasks(path) => {
            // This is the structured TOML configuration with tasks - handle it differently
            info!(
                "Loading structured TOML configuration with tasks: {:?}",
                path
            );
            return run_structured_config_mode(path.clone(), config).await;
        }
        TaskInput::ExecutionPlan(path) => {
            info!("Loading execution plan from: {:?}", path);
            TaskLoader::load_execution_plan(path)?
        }
        _ => {
            info!("Converting task input to execution plan...");

            // Determine whether to use intelligent parser
            let use_intelligent = if config.force_naive_parser {
                false
            } else if config.use_intelligent_parser {
                true
            } else {
                // Auto-detect: use intelligent parser for task lists
                matches!(config.task_input, TaskInput::TaskList(_))
            };

            if use_intelligent {
                info!("Using intelligent LLM-based task parser");
                TaskLoader::task_input_to_execution_plan_with_options(
                    &config.task_input,
                    true,
                    config.context_hints.clone(),
                    config.provider_override.clone(),
                    config.model_override.clone(),
                )
                .await?
            } else {
                TaskLoader::task_input_to_execution_plan(&config.task_input)?
            }
        }
    };

    if config.verbose {
        println!("📁 Created execution plan: {}", execution_plan.summary());
        if let Some(ref name) = execution_plan.metadata.name {
            println!("  📋 Plan: {}", name);
        }
        if let Some(ref description) = execution_plan.metadata.description {
            println!("  📝 Description: {}", description);
        }
        if execution_plan.has_setup_commands() {
            println!(
                "  ⚙️  Setup commands: {}",
                execution_plan.setup_command_count()
            );
        }
        if execution_plan.has_tasks() {
            println!("  🎯 Tasks: {}", execution_plan.task_count());
            for (i, task_spec) in execution_plan.task_specs.iter().enumerate() {
                let title = if task_spec.title.len() > 80 {
                    format!("{}...", &task_spec.title[..77])
                } else {
                    task_spec.title.clone()
                };
                println!("      {}. {}", i + 1, title);
            }
        }
    }

    // Dump execution plan if requested
    if let Some(ref dump_path) = config.dump_plan {
        dump_execution_plan(&execution_plan, dump_path)?;
        println!("📄 Execution plan dumped to: {}", dump_path.display());
        if config.dry_run {
            return Ok(());
        }
    }

    if config.dry_run {
        println!("🔍 Dry run mode - execution plan would be processed but won't actually run");
        return Ok(());
    }

    // Initialize agent system
    info!("Initializing agent system for batch execution...");
    let agent_config = agent_config.with_subprocess_output(config.verbose);
    let agent = AgentSystem::new(agent_config).await?;

    info!("Agent system initialized successfully!");

    // Execute the plan using the unified execution path
    info!("Executing plan with unified agent system...");
    let task_ids = agent.execute_plan(execution_plan).await?;

    if config.verbose {
        if !task_ids.is_empty() {
            println!(
                "✅ All {} tasks in plan completed successfully!",
                task_ids.len()
            );
            if task_ids.len() <= 5 {
                // Show task IDs for small numbers of tasks
                for (i, task_id) in task_ids.iter().enumerate() {
                    println!("    {}. {}", i + 1, task_id);
                }
            }
        } else {
            println!("ℹ️  No tasks were executed (setup-only plan)");
        }
    }

    // Graceful shutdown
    info!("Shutting down agent system...");
    agent.shutdown().await?;

    Ok(())
}

async fn run_structured_config_mode(
    config_path: std::path::PathBuf,
    config: BatchConfig,
) -> Result<(), Box<dyn std::error::Error>> {
    // This handles the structured TOML format with embedded tasks and configuration
    info!("Running structured TOML configuration mode");

    // Load the agent config from TOML file
    let agent_config = AgentConfig::from_toml_file(config_path)?;

    // Convert the agent config to execution plan
    info!("Converting structured configuration to execution plan...");
    let execution_plan = AgentSystem::agent_config_to_execution_plan(&agent_config);

    if config.verbose {
        println!(
            "📁 Created execution plan from structured config: {}",
            execution_plan.summary()
        );
        if let Some(ref name) = execution_plan.metadata.name {
            println!("  📋 Plan: {}", name);
        }
        if let Some(ref description) = execution_plan.metadata.description {
            println!("  📝 Description: {}", description);
        }
        if execution_plan.has_setup_commands() {
            println!(
                "  ⚙️  Setup commands: {}",
                execution_plan.setup_command_count()
            );
            for (i, setup_cmd) in execution_plan.setup_commands.iter().enumerate() {
                println!(
                    "      {}. {} ({})",
                    i + 1,
                    setup_cmd.name,
                    setup_cmd.command
                );
            }
        }
    }

    if config.dry_run {
        println!(
            "🔍 Dry run mode - structured execution plan would be processed but won't actually run"
        );
        return Ok(());
    }

    // Initialize agent system
    info!("Initializing agent system for structured batch execution...");
    let agent_config = agent_config.with_subprocess_output(config.verbose);
    let agent = AgentSystem::new(agent_config).await?;

    info!("Agent system initialized successfully!");

    // Execute the plan using the unified execution path
    info!("Executing structured configuration plan...");
    let task_ids = agent.execute_plan(execution_plan).await?;

    if config.verbose {
        if !task_ids.is_empty() {
            println!(
                "✅ All {} tasks in structured plan completed successfully!",
                task_ids.len()
            );
        } else {
            println!("✅ Structured configuration setup completed successfully!");
        }
    }

    // Graceful shutdown
    info!("Shutting down agent system...");
    agent.shutdown().await?;

    Ok(())
}

async fn run_interactive_mode(config: InteractiveConfig) -> Result<(), Box<dyn std::error::Error>> {
    info!("Running in interactive mode");

    // Discover configuration for interactive mode
    let default_config = ConfigDiscovery::discover_config()?;
    let agent_config = default_config.to_agent_config(config.workspace.clone());

    // Initialize the agent system
    info!("Initializing agent system...");
    let agent_config = agent_config.with_subprocess_output(config.verbose);
    let agent = AgentSystem::new(agent_config).await?;

    info!("Agent system initialized successfully!");

    if config.verbose {
        println!("🤖 Interactive mode started. Type 'help' for commands.");
    }

    // Interactive CLI loop (preserved from original)
    loop {
        print!("\n> Enter a task description (or 'quit' to exit): ");
        io::stdout().flush()?;

        let mut input = String::new();
        io::stdin().read_line(&mut input)?;
        let input = input.trim();

        if input == "quit" || input == "exit" {
            break;
        }

        if input == "status" {
            show_system_status(&agent).await?;
            continue;
        }

        if input == "help" {
            show_interactive_help();
            continue;
        }

        if input.is_empty() {
            continue;
        }

        // Create and process the task
        info!("Creating task: {}", input);

        match agent.create_and_process_task("User Task", input).await {
            Ok(task_id) => {
                info!("Task completed successfully! Task ID: {}", task_id);
                println!("✅ Task completed: {}", task_id);
            }
            Err(e) => {
                error!("Task failed: {}", e);
                println!("❌ Task failed: {}", e);
            }
        }
    }

    // Graceful shutdown
    info!("Shutting down agent system...");
    agent.shutdown().await?;

    println!("Goodbye!");
    Ok(())
}

fn show_interactive_help() {
    println!("📖 Interactive Mode Commands:");
    println!("  status  - Show system status");
    println!("  help    - Show this help message");
    println!("  quit    - Exit the application");
    println!("  exit    - Exit the application");
    println!("\n💡 Enter any other text to create and execute a task.");
}

async fn show_system_status(agent: &AgentSystem) -> Result<(), Box<dyn std::error::Error>> {
    let status = agent.get_system_status().await?;

    println!("\n📊 System Status:");
    println!(
        "  Health: {}",
        if status.is_healthy {
            "✅ Healthy"
        } else {
            "❌ Unhealthy"
        }
    );
    println!("  Tasks: {} total", status.task_stats.total_tasks);
    println!(
        "  Claude: {} available tokens, {} requests",
        status.claude_status.rate_limiter.available_tokens,
        status.claude_status.rate_limiter.available_requests
    );
    println!(
        "  Sessions: {} active, {} idle",
        status.claude_status.session_stats.active_sessions,
        status.claude_status.session_stats.idle_sessions
    );

    Ok(())
}

async fn run_resume_mode(config: ResumeConfig) -> Result<(), Box<dyn std::error::Error>> {
    info!("Running in resume mode");

    let workspace = config
        .workspace_override
        .unwrap_or_else(|| std::env::current_dir().unwrap());

    // Check if .aca directory structure exists
    let _aca_dir = env::aca_dir_path(&workspace);
    let sessions_dir = env::sessions_dir_path(&workspace);

    if !sessions_dir.exists() {
        eprintln!(
            "Error: No session data found in directory: {}",
            workspace.display()
        );
        eprintln!("Make sure you're in the correct workspace directory.");
        std::process::exit(1);
    }

    // Determine which checkpoint to restore from
    let checkpoint_id = if config.continue_latest {
        match find_latest_checkpoint(&workspace).await {
            Ok(id) => id,
            Err(e) => {
                eprintln!("Error: Failed to find latest checkpoint: {}", e);
                std::process::exit(1);
            }
        }
    } else if let Some(id) = config.checkpoint_id {
        id
    } else {
        eprintln!("Error: Must specify --resume <checkpoint-id> or --continue");
        std::process::exit(1);
    };

    if config.verbose {
        println!("🔄 Resuming from checkpoint: {}", checkpoint_id);
    }

    // Discover and load configuration
    let default_config = ConfigDiscovery::discover_config()?;
    let agent_config = default_config.to_agent_config(Some(workspace.clone()));

    // Initialize agent system with restore
    info!("Initializing agent system with checkpoint restore...");
    // TODO: We need to modify AgentSystem to support session restore
    // For now, we'll use the regular initialization and these variables are placeholders for future use
    let _session_config = SessionManagerConfig::default();
    let _init_options = SessionInitOptions {
        name: "Resumed Session".to_string(),
        description: Some("Session resumed from checkpoint".to_string()),
        workspace_root: workspace.clone(),
        task_manager_config: TaskManagerConfig::default(),
        persistence_config: PersistenceConfig::default(),
        recovery_config: RecoveryConfig::default(),
        enable_auto_save: true,
        restore_from_checkpoint: Some(checkpoint_id.clone()),
    };

    let agent = AgentSystem::new(agent_config).await?;

    if config.verbose {
        println!("✅ Successfully resumed from checkpoint: {}", checkpoint_id);
        println!("🤖 Agent system ready. Checking for incomplete tasks...");
    }

    // Check for incomplete tasks and continue processing
    let incomplete_tasks = find_incomplete_tasks(&agent).await?;

    if !incomplete_tasks.is_empty() {
        if config.verbose {
            println!(
                "🔄 Found {} incomplete tasks. Continuing processing...",
                incomplete_tasks.len()
            );
        }

        let mut successful_tasks = 0;
        let total_tasks = incomplete_tasks.len();

        for (task_num, task_id) in incomplete_tasks.iter().enumerate() {
            if config.verbose {
                println!(
                    "🔄 Processing incomplete task {}/{}: {}",
                    task_num + 1,
                    total_tasks,
                    task_id
                );
            }

            match agent.process_task(*task_id).await {
                Ok(()) => {
                    info!(
                        "Resumed task {}/{} completed successfully! Task ID: {}",
                        task_num + 1,
                        total_tasks,
                        task_id
                    );
                    if config.verbose {
                        println!(
                            "✅ Task {}/{} completed: {}",
                            task_num + 1,
                            total_tasks,
                            task_id
                        );
                    }
                    successful_tasks += 1;
                }
                Err(e) => {
                    error!("Failed to process resumed task {}: {}", task_id, e);
                    if config.verbose {
                        println!("❌ Task {}/{} failed: {}", task_num + 1, total_tasks, e);
                    }
                }
            }
        }

        if successful_tasks == total_tasks {
            println!(
                "✅ All {} resumed tasks completed successfully!",
                total_tasks
            );
        } else {
            println!(
                "⚠️  {}/{} resumed tasks completed successfully",
                successful_tasks, total_tasks
            );
        }
    } else if config.verbose {
        println!("ℹ️  No incomplete tasks found. Session restored successfully.");
    }

    // Graceful shutdown
    info!("Shutting down agent system...");
    agent.shutdown().await?;

    Ok(())
}

async fn list_available_checkpoints(_all_sessions: bool) -> Result<(), Box<dyn std::error::Error>> {
    let workspace = std::env::current_dir()?;
    let _aca_dir = env::aca_dir_path(&workspace);
    let sessions_dir = env::sessions_dir_path(&workspace);

    // Check if .aca directory structure exists
    if !sessions_dir.exists() {
        println!("No session data found in current directory.");
        println!("Make sure you're in a workspace that has been used with aca.");
        return Ok(());
    }

    // Find the most recent session directory
    let mut latest_session_dir = None;
    let mut latest_time = None;

    if let Ok(entries) = std::fs::read_dir(&sessions_dir) {
        for entry in entries.flatten() {
            let path = entry.path();
            if path.is_dir()
                && let Ok(metadata) = entry.metadata()
                && let Ok(modified) = metadata.modified()
                && (latest_time.is_none() || Some(modified) > latest_time)
            {
                latest_time = Some(modified);
                latest_session_dir = Some(path);
            }
        }
    }

    let Some(_session_dir) = latest_session_dir else {
        println!("No session directories found in .aca/sessions/");
        return Ok(());
    };

    // Create a temporary session manager to list all checkpoints across sessions
    let session_config = SessionManagerConfig::default();
    let init_options = SessionInitOptions {
        name: "Temporary Session for Listing".to_string(),
        description: Some("Temporary session for listing checkpoints".to_string()),
        workspace_root: workspace.clone(),
        task_manager_config: TaskManagerConfig::default(),
        persistence_config: PersistenceConfig::default(),
        recovery_config: RecoveryConfig::default(),
        enable_auto_save: false,
        restore_from_checkpoint: None,
    };

    let temp_session =
        match SessionManager::new(workspace.clone(), session_config, init_options).await {
            Ok(session) => session,
            Err(e) => {
                eprintln!("Error: Failed to create session for listing: {}", e);
                std::process::exit(1);
            }
        };

    // For CLI usage, default to showing all checkpoints across sessions
    // The --all-sessions flag is currently redundant but kept for explicit behavior
    let show_all = true; // CLI users expect to see all checkpoints by default
    let checkpoints = temp_session.list_checkpoints(show_all).await?;

    if checkpoints.is_empty() {
        println!("No checkpoints available in current workspace.");
    } else {
        println!("Available checkpoints in {}:", workspace.display());
        println!();
        for checkpoint in checkpoints {
            println!(
                "📌 {} ({})",
                checkpoint.id,
                checkpoint.created_at.format("%Y-%m-%d %H:%M:%S")
            );
            println!("   Description: {}", checkpoint.description);
            if checkpoint.task_count > 0 {
                println!("   Tasks: {} total", checkpoint.task_count);
            }
            println!();
        }
        println!("Use --resume <checkpoint-id> to restore from a specific checkpoint");
        println!("Use --continue to resume from the latest checkpoint");
    }

    Ok(())
}

async fn create_manual_checkpoint(description: String) -> Result<(), Box<dyn std::error::Error>> {
    let workspace = std::env::current_dir()?;
    let _aca_dir = env::aca_dir_path(&workspace);
    let sessions_dir = env::sessions_dir_path(&workspace);

    // Check if .aca directory structure exists
    if !sessions_dir.exists() {
        eprintln!("Error: No active session found in current directory.");
        eprintln!("Start a task first to create a session, then you can create checkpoints.");
        std::process::exit(1);
    }

    // Create checkpoint using a temporary session manager that operates on the latest session
    let session_config = SessionManagerConfig::default();
    let init_options = SessionInitOptions {
        name: "Temporary Session for Manual Checkpoint".to_string(),
        description: Some("Temporary session for creating manual checkpoint".to_string()),
        workspace_root: workspace.clone(),
        task_manager_config: TaskManagerConfig::default(),
        persistence_config: PersistenceConfig::default(),
        recovery_config: RecoveryConfig::default(),
        enable_auto_save: false,
        restore_from_checkpoint: None,
    };

    let temp_session =
        match SessionManager::new(workspace.clone(), session_config, init_options).await {
            Ok(session) => session,
            Err(e) => {
                eprintln!("Error: Failed to create session for checkpoint: {}", e);
                std::process::exit(1);
            }
        };

    let checkpoint = match temp_session
        .create_checkpoint_in_latest_session_of_workspace(description.clone())
        .await
    {
        Ok(checkpoint) => checkpoint,
        Err(e) => {
            eprintln!("Error: Failed to create checkpoint: {}", e);
            std::process::exit(1);
        }
    };

    println!("✅ Checkpoint created: {}", checkpoint.id);
    println!("   Description: {}", description);
    println!(
        "   Created: {}",
        checkpoint.created_at.format("%Y-%m-%d %H:%M:%S")
    );

    Ok(())
}

async fn find_latest_checkpoint(
    session_dir: &std::path::Path,
) -> Result<String, Box<dyn std::error::Error>> {
    let session_config = SessionManagerConfig::default();
    let init_options = SessionInitOptions {
        name: "Temporary Session".to_string(),
        description: Some("Temporary session for finding latest checkpoint".to_string()),
        workspace_root: session_dir.to_path_buf(),
        task_manager_config: TaskManagerConfig::default(),
        persistence_config: PersistenceConfig::default(),
        recovery_config: RecoveryConfig::default(),
        enable_auto_save: false,
        restore_from_checkpoint: None,
    };
    let temp_session =
        SessionManager::new(session_dir.to_path_buf(), session_config, init_options).await?;
    let checkpoints = temp_session.list_checkpoints(true).await?;

    if checkpoints.is_empty() {
        return Err("No checkpoints available".into());
    }

    // Find the most recent checkpoint
    let latest = checkpoints.iter().max_by_key(|c| c.created_at).unwrap();

    Ok(latest.id.clone())
}

/// Dump execution plan to JSON or TOML file
fn dump_execution_plan(
    plan: &ExecutionPlan,
    path: &Path,
) -> Result<(), Box<dyn std::error::Error>> {
    let extension = path.extension().and_then(|s| s.to_str()).unwrap_or("json");

    match extension {
        "json" => {
            let json = serde_json::to_string_pretty(plan)?;
            std::fs::write(path, json)?;
        }
        "toml" => {
            let toml = toml::to_string_pretty(plan)?;
            std::fs::write(path, toml)?;
        }
        _ => {
            return Err(format!(
                "Unsupported format: {}. Use .json or .toml extension",
                extension
            )
            .into());
        }
    }

    Ok(())
}

/// Find incomplete tasks that should be continued when resuming
async fn find_incomplete_tasks(
    agent: &AgentSystem,
) -> Result<Vec<uuid::Uuid>, Box<dyn std::error::Error>> {
    let task_manager = agent.task_manager();

    // Look for tasks that are in progress or eligible to be processed
    let in_progress_tasks = task_manager
        .get_tasks_by_status(|status| matches!(status, TaskStatus::InProgress { .. }))
        .await?;

    let eligible_tasks = task_manager.get_eligible_tasks().await?;

    // Combine both sets, prioritizing in-progress tasks
    let mut incomplete_tasks = Vec::new();

    // Track counts before moving
    let in_progress_count = in_progress_tasks.len();
    let eligible_count = eligible_tasks.len();

    // Add in-progress tasks first (highest priority for resume)
    incomplete_tasks.extend(in_progress_tasks);

    // Add eligible tasks that aren't already in progress
    for task_id in &eligible_tasks {
        if !incomplete_tasks.contains(task_id) {
            incomplete_tasks.push(*task_id);
        }
    }

    info!(
        "Found {} incomplete tasks for resume: {} in-progress, {} eligible",
        incomplete_tasks.len(),
        in_progress_count,
        eligible_count
    );

    Ok(incomplete_tasks)
}