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
use crate::task::tree::*;
use crate::task::types::*;
use chrono::{DateTime, Duration, Utc};
use rand::prelude::*;
use rand::rng;
use serde::{Deserialize, Serialize};
use tracing::{debug, info};

/// Task scheduler with intelligent prioritization
pub struct TaskScheduler {
    scoring_weights: ScoringWeights,
    resource_monitor: ResourceMonitor,
    context_cache: ContextCache,
    config: SchedulerConfig,
}

/// Weights for task scoring algorithm
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ScoringWeights {
    pub priority_weight: f64,
    pub dependency_weight: f64,
    pub context_similarity_weight: f64,
    pub resource_availability_weight: f64,
    pub failure_penalty_weight: f64,
    pub age_bonus_weight: f64,
    pub complexity_weight: f64,
}

/// Resource monitoring for task execution
#[derive(Clone, Debug)]
pub struct ResourceMonitor {
    pub max_concurrent_tasks: u32,
    pub memory_limit_mb: u64,
    pub cpu_limit_percent: f64,
    pub current_usage: ResourceUsage,
}

/// Context cache for optimization
#[derive(Clone, Debug)]
pub struct ContextCache {
    recent_files: Vec<std::path::PathBuf>,
    active_repositories: Vec<String>,
    #[allow(dead_code)]
    claude_context_window: Vec<String>,
    last_updated: DateTime<Utc>,
}

/// Scheduler configuration
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SchedulerConfig {
    pub max_concurrent_tasks: u32,
    pub selection_randomization: f64, // 0.0 = pure scoring, 1.0 = random
    pub context_window_size: usize,
    pub resource_check_enabled: bool,
    pub dependency_lookahead: u32, // How many levels to look ahead for dependencies
}

/// Task selection result
#[derive(Debug, Clone)]
pub struct TaskSelection {
    pub task_id: TaskId,
    pub score: f64,
    pub selection_reason: String,
    pub estimated_resources: ResourceRequirement,
}

/// Resource requirements for a task
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResourceRequirement {
    pub memory_mb: u64,
    pub cpu_percent: f64,
    pub estimated_duration: Duration,
    pub concurrent_task_limit: u32,
}

impl TaskScheduler {
    /// Create a new task scheduler
    pub fn new(config: SchedulerConfig) -> Self {
        Self {
            scoring_weights: ScoringWeights::default(),
            resource_monitor: ResourceMonitor::new(),
            context_cache: ContextCache::new(),
            config,
        }
    }

    /// Create scheduler with custom weights
    pub fn with_weights(mut self, weights: ScoringWeights) -> Self {
        self.scoring_weights = weights;
        self
    }

    /// Select the next task to execute
    pub async fn select_next_task(&self, tree: &TaskTree) -> Option<TaskSelection> {
        let eligible_tasks = self.get_eligible_tasks(tree).await;

        if eligible_tasks.is_empty() {
            debug!("No eligible tasks found");
            return None;
        }

        let scored_tasks = eligible_tasks
            .iter()
            .map(|&task_id| {
                let score = self.calculate_task_score(task_id, tree);
                let task = tree.get_task(task_id).unwrap();
                let estimated_resources = self.estimate_task_resources(task);

                TaskSelection {
                    task_id,
                    score,
                    selection_reason: self.build_selection_reason(task_id, score, tree),
                    estimated_resources,
                }
            })
            .collect::<Vec<_>>();

        // Select task using weighted random selection or pure scoring
        let selection = if self.config.selection_randomization > 0.0 {
            self.weighted_random_selection(scored_tasks).await
        } else {
            scored_tasks.into_iter().max_by(|a, b| {
                a.score
                    .partial_cmp(&b.score)
                    .unwrap_or(std::cmp::Ordering::Equal)
            })
        };

        if let Some(ref selection) = selection {
            info!(
                "Selected task {} with score {:.2}: {}",
                selection.task_id, selection.score, selection.selection_reason
            );
        }

        selection
    }

    /// Get tasks eligible for execution
    async fn get_eligible_tasks(&self, tree: &TaskTree) -> Vec<TaskId> {
        let mut eligible = Vec::new();

        for &task_id in &tree.get_all_task_ids() {
            if self.is_task_eligible(task_id, tree).await {
                eligible.push(task_id);
            }
        }

        eligible
    }

    /// Check if a task is eligible for execution
    async fn is_task_eligible(&self, task_id: TaskId, tree: &TaskTree) -> bool {
        let task = match tree.get_task(task_id) {
            Ok(task) => task,
            Err(_) => return false,
        };

        // Check basic status eligibility
        if !task.is_runnable() {
            return false;
        }

        // Check all dependencies are satisfied
        if let Ok(deps_satisfied) = tree.are_dependencies_satisfied(task_id) {
            if !deps_satisfied {
                return false;
            }
        } else {
            return false;
        }

        // Check resource availability if enabled
        if self.config.resource_check_enabled && !self.resource_monitor.can_execute_task(task).await
        {
            return false;
        }

        // Check for exclusive dependencies
        if self.has_conflicting_active_tasks(task, tree).await {
            return false;
        }

        true
    }

    /// Calculate comprehensive task score
    fn calculate_task_score(&self, task_id: TaskId, tree: &TaskTree) -> f64 {
        let task = tree.get_task(task_id).unwrap();

        let priority_score = self.calculate_priority_score(task);
        let dependency_score = self.calculate_dependency_score(task, tree);
        let context_score = self.calculate_context_score(task);
        let resource_score = self.calculate_resource_score(task);
        let history_score = self.calculate_history_score(task);
        let age_score = self.calculate_age_score(task);
        let complexity_score = self.calculate_complexity_score(task);

        let weights = &self.scoring_weights;

        let total_score = priority_score * weights.priority_weight
            + dependency_score * weights.dependency_weight
            + context_score * weights.context_similarity_weight
            + resource_score * weights.resource_availability_weight
            + history_score * weights.failure_penalty_weight
            + age_score * weights.age_bonus_weight
            + complexity_score * weights.complexity_weight;

        // Normalize score to 0-100 range
        total_score.clamp(0.0, 100.0)
    }

    /// Calculate priority-based score
    fn calculate_priority_score(&self, task: &Task) -> f64 {
        task.priority_value() as f64
    }

    /// Calculate dependency-based score (higher for tasks that unblock others)
    fn calculate_dependency_score(&self, task: &Task, tree: &TaskTree) -> f64 {
        let mut score = 0.0;

        // Count how many tasks depend on this one
        let dependent_count = tree
            .tasks
            .values()
            .filter(|t| t.dependencies.contains(&task.id))
            .count() as f64;

        score += dependent_count * 2.0; // Each dependent task adds 2 points

        // Bonus for tasks that are critical path items
        if self.is_on_critical_path(task, tree) {
            score += 5.0;
        }

        score
    }

    /// Calculate context similarity score
    fn calculate_context_score(&self, task: &Task) -> f64 {
        let task_files: std::collections::HashSet<_> =
            task.metadata.file_refs.iter().map(|f| &f.path).collect();

        let recent_files: std::collections::HashSet<_> =
            self.context_cache.recent_files.iter().collect();

        if task_files.is_empty() || recent_files.is_empty() {
            return 0.0;
        }

        let intersection = task_files.intersection(&recent_files).count() as f64;
        let union = task_files.union(&recent_files).count() as f64;

        if union > 0.0 {
            (intersection / union) * 10.0 // Scale to 0-10 range
        } else {
            0.0
        }
    }

    /// Calculate resource availability score
    fn calculate_resource_score(&self, task: &Task) -> f64 {
        let requirements = self.estimate_task_resources(task);

        // Score based on how well current resources match requirements
        let memory_ratio = self.resource_monitor.current_usage.max_memory_mb as f64
            / (requirements.memory_mb as f64).max(1.0);
        let cpu_ratio = self.resource_monitor.current_usage.cpu_time_seconds
            / requirements.cpu_percent.max(0.1);

        // Lower resource usage = higher score
        let resource_efficiency = (2.0 - memory_ratio.min(2.0)) + (2.0 - cpu_ratio.min(2.0));
        resource_efficiency * 2.5 // Scale to 0-10 range
    }

    /// Calculate history-based score (penalty for previous failures)
    fn calculate_history_score(&self, task: &Task) -> f64 {
        let mut score = 0.0;

        let failure_count = task
            .execution_history
            .iter()
            .filter(|record| matches!(record.status, TaskStatus::Failed { .. }))
            .count() as f64;

        // Penalty for failures, but with diminishing returns
        score -= (failure_count * 2.0).min(8.0);

        // Bonus for successful completions in similar tasks
        let success_count = task
            .execution_history
            .iter()
            .filter(|record| matches!(record.status, TaskStatus::Completed { .. }))
            .count() as f64;

        score += success_count * 1.0;

        score
    }

    /// Calculate age-based score (older tasks get bonus)
    fn calculate_age_score(&self, task: &Task) -> f64 {
        let age_hours = task.age().num_hours() as f64;

        // Logarithmic bonus for age to prevent starvation
        if age_hours > 0.0 {
            (age_hours + 1.0).ln() * 2.0
        } else {
            0.0
        }
    }

    /// Calculate complexity-based score
    fn calculate_complexity_score(&self, task: &Task) -> f64 {
        match &task.metadata.estimated_complexity {
            Some(complexity) => {
                // Prefer moderate complexity tasks - not too simple, not too complex
                match complexity {
                    ComplexityLevel::Trivial => 2.0,
                    ComplexityLevel::Simple => 4.0,
                    ComplexityLevel::Moderate => 5.0, // Optimal
                    ComplexityLevel::Complex => 3.0,
                    ComplexityLevel::Epic => 1.0,
                }
            }
            None => 3.0, // Default score for unknown complexity
        }
    }

    /// Check if task is on the critical path
    fn is_on_critical_path(&self, task: &Task, tree: &TaskTree) -> bool {
        // Simplified critical path detection
        // In practice, this would use more sophisticated graph analysis
        let dependent_count = tree
            .tasks
            .values()
            .filter(|t| t.dependencies.contains(&task.id))
            .count();

        dependent_count > 2 || task.metadata.priority == TaskPriority::Critical
    }

    /// Estimate resource requirements for a task
    fn estimate_task_resources(&self, task: &Task) -> ResourceRequirement {
        let base_memory = 256; // MB
        let base_cpu = 10.0; // percent

        let complexity_multiplier = match task.metadata.estimated_complexity {
            Some(ComplexityLevel::Trivial) => 0.5,
            Some(ComplexityLevel::Simple) => 1.0,
            Some(ComplexityLevel::Moderate) => 2.0,
            Some(ComplexityLevel::Complex) => 4.0,
            Some(ComplexityLevel::Epic) => 8.0,
            None => 1.5,
        };

        let duration = task.metadata.estimated_duration.unwrap_or_else(|| {
            match task.metadata.estimated_complexity {
                Some(ref complexity) => complexity.estimated_duration(),
                None => Duration::minutes(30),
            }
        });

        ResourceRequirement {
            memory_mb: (base_memory as f64 * complexity_multiplier) as u64,
            cpu_percent: base_cpu * complexity_multiplier,
            estimated_duration: duration,
            concurrent_task_limit: match task.metadata.estimated_complexity {
                Some(ComplexityLevel::Epic) | Some(ComplexityLevel::Complex) => 1,
                _ => 3,
            },
        }
    }

    /// Check for conflicting active tasks
    async fn has_conflicting_active_tasks(&self, _task: &Task, _tree: &TaskTree) -> bool {
        // Placeholder for conflict detection
        // Would check for resource conflicts, file locks, etc.
        false
    }

    /// Weighted random selection from scored tasks
    async fn weighted_random_selection(
        &self,
        mut selections: Vec<TaskSelection>,
    ) -> Option<TaskSelection> {
        if selections.is_empty() {
            return None;
        }

        // Sort by score for debugging
        selections.sort_by(|a, b| {
            b.score
                .partial_cmp(&a.score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        let randomization = self.config.selection_randomization;

        if randomization <= 0.0 {
            // Pure scoring - return highest scored task
            return selections.into_iter().next();
        }

        if randomization >= 1.0 {
            // Pure random - ignore scores
            let mut rng = rng();
            return selections.choose(&mut rng).cloned();
        }

        // Weighted selection based on scores
        let total_score: f64 = selections.iter().map(|s| s.score.max(0.1)).sum();
        let mut rng = rng();
        let random_value = rng.random_range(0.0..total_score);

        let mut cumulative_score = 0.0;
        for selection in selections {
            cumulative_score += selection.score.max(0.1);
            if cumulative_score >= random_value {
                return Some(selection);
            }
        }

        None
    }

    /// Build a human-readable explanation for task selection
    fn build_selection_reason(&self, task_id: TaskId, score: f64, tree: &TaskTree) -> String {
        let task = tree.get_task(task_id).unwrap();

        let mut reasons = Vec::new();

        if task.priority_value() >= 8 {
            reasons.push(format!(
                "high priority ({})",
                task.metadata.priority.clone() as u8
            ));
        }

        let dependent_count = tree
            .tasks
            .values()
            .filter(|t| t.dependencies.contains(&task.id))
            .count();

        if dependent_count > 0 {
            reasons.push(format!("blocks {} other tasks", dependent_count));
        }

        let age_hours = task.age().num_hours();
        if age_hours > 24 {
            reasons.push(format!("aged {} hours", age_hours));
        }

        if let Some(complexity) = &task.metadata.estimated_complexity
            && matches!(
                complexity,
                ComplexityLevel::Trivial | ComplexityLevel::Simple
            )
        {
            reasons.push("quick win".to_string());
        }

        if reasons.is_empty() {
            format!("score {:.1}", score)
        } else {
            reasons.join(", ")
        }
    }

    /// Update context cache with recent activity
    pub fn update_context(&mut self, files: Vec<std::path::PathBuf>, repositories: Vec<String>) {
        self.context_cache.recent_files = files;
        self.context_cache.active_repositories = repositories;
        self.context_cache.last_updated = Utc::now();

        // Limit cache size
        if self.context_cache.recent_files.len() > self.config.context_window_size {
            self.context_cache
                .recent_files
                .truncate(self.config.context_window_size);
        }
    }
}

impl Default for ScoringWeights {
    fn default() -> Self {
        Self {
            priority_weight: 10.0,
            dependency_weight: 8.0,
            context_similarity_weight: 5.0,
            resource_availability_weight: 3.0,
            failure_penalty_weight: -2.0,
            age_bonus_weight: 2.0,
            complexity_weight: 1.0,
        }
    }
}

impl Default for SchedulerConfig {
    fn default() -> Self {
        Self {
            max_concurrent_tasks: 3,
            selection_randomization: 0.1, // Slight randomization
            context_window_size: 50,
            resource_check_enabled: true,
            dependency_lookahead: 3,
        }
    }
}

impl Default for ResourceMonitor {
    fn default() -> Self {
        Self::new()
    }
}

impl ResourceMonitor {
    pub fn new() -> Self {
        Self {
            max_concurrent_tasks: 3,
            memory_limit_mb: 8192,
            cpu_limit_percent: 80.0,
            current_usage: ResourceUsage {
                max_memory_mb: 0,
                cpu_time_seconds: 0.0,
                disk_io_mb: 0,
                network_requests: 0,
            },
        }
    }

    pub async fn can_execute_task(&self, task: &Task) -> bool {
        // Simplified resource check
        let _requirements = self.estimate_requirements(task);

        // In practice, would check current system usage
        true
    }

    fn estimate_requirements(&self, task: &Task) -> ResourceRequirement {
        let scheduler = TaskScheduler::new(SchedulerConfig::default());
        scheduler.estimate_task_resources(task)
    }
}

impl Default for ContextCache {
    fn default() -> Self {
        Self::new()
    }
}

impl ContextCache {
    pub fn new() -> Self {
        Self {
            recent_files: Vec::new(),
            active_repositories: Vec::new(),
            claude_context_window: Vec::new(),
            last_updated: Utc::now(),
        }
    }
}