brainwires-agents 0.7.0

Agent orchestration, coordination, and lifecycle management for the Brainwires Agent Framework
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
//! Plan Executor Agent - Executes plans by orchestrating task execution
//!
//! Runs through a plan's tasks, respecting dependencies and approval modes.
//! Integrates with completion detection to auto-progress tasks.

use anyhow::Result;
use std::sync::Arc;
use tokio::sync::RwLock;

use brainwires_core::{PlanMetadata, Task};

use crate::task_manager::TaskManager;

/// Approval mode for plan execution
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ExecutionApprovalMode {
    /// Suggest mode - ask user before each task (safest)
    Suggest,
    /// Auto-edit mode - auto-approve file edits, ask for shell commands
    AutoEdit,
    /// Full-auto mode - auto-approve everything (default for plan execution)
    #[default]
    FullAuto,
}

impl std::fmt::Display for ExecutionApprovalMode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Suggest => write!(f, "suggest"),
            Self::AutoEdit => write!(f, "auto-edit"),
            Self::FullAuto => write!(f, "full-auto"),
        }
    }
}

impl std::str::FromStr for ExecutionApprovalMode {
    type Err = String;

    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "suggest" => Ok(Self::Suggest),
            "auto-edit" | "autoedit" => Ok(Self::AutoEdit),
            "full-auto" | "fullauto" | "auto" => Ok(Self::FullAuto),
            _ => Err(format!("Unknown approval mode: {}", s)),
        }
    }
}

/// Status of plan execution
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PlanExecutionStatus {
    /// Not started
    Idle,
    /// Currently executing
    Running,
    /// Waiting for user approval
    WaitingForApproval(String),
    /// Paused by user
    Paused,
    /// Completed successfully
    Completed,
    /// Failed with error
    Failed(String),
}

impl std::fmt::Display for PlanExecutionStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Idle => write!(f, "Idle"),
            Self::Running => write!(f, "Running"),
            Self::WaitingForApproval(task) => write!(f, "Waiting for approval: {}", task),
            Self::Paused => write!(f, "Paused"),
            Self::Completed => write!(f, "Completed"),
            Self::Failed(err) => write!(f, "Failed: {}", err),
        }
    }
}

/// Configuration for plan execution
#[derive(Debug, Clone)]
pub struct PlanExecutionConfig {
    /// Approval mode
    pub approval_mode: ExecutionApprovalMode,
    /// Maximum iterations per task
    pub max_iterations_per_task: u32,
    /// Whether to auto-start next task after completion
    pub auto_advance: bool,
    /// Stop on first error
    pub stop_on_error: bool,
}

impl Default for PlanExecutionConfig {
    fn default() -> Self {
        Self {
            approval_mode: ExecutionApprovalMode::FullAuto,
            max_iterations_per_task: 15,
            auto_advance: true,
            stop_on_error: true,
        }
    }
}

/// Plan Executor Agent - coordinates execution of a plan's tasks
pub struct PlanExecutorAgent {
    /// The plan being executed
    plan: Arc<RwLock<PlanMetadata>>,
    /// Task manager
    task_manager: Arc<RwLock<TaskManager>>,
    /// Execution configuration
    config: PlanExecutionConfig,
    /// Current execution status
    status: Arc<RwLock<PlanExecutionStatus>>,
    /// Current task being executed (if any)
    current_task_id: Arc<RwLock<Option<String>>>,
}

impl PlanExecutorAgent {
    /// Create a new plan executor
    pub fn new(
        plan: PlanMetadata,
        task_manager: Arc<RwLock<TaskManager>>,
        config: PlanExecutionConfig,
    ) -> Self {
        Self {
            plan: Arc::new(RwLock::new(plan)),
            task_manager,
            config,
            status: Arc::new(RwLock::new(PlanExecutionStatus::Idle)),
            current_task_id: Arc::new(RwLock::new(None)),
        }
    }

    /// Get the plan
    #[tracing::instrument(name = "agent.plan.get", skip(self))]
    pub async fn plan(&self) -> PlanMetadata {
        self.plan.read().await.clone()
    }

    /// Get the execution status
    pub async fn status(&self) -> PlanExecutionStatus {
        self.status.read().await.clone()
    }

    /// Get the current task ID
    pub async fn current_task_id(&self) -> Option<String> {
        self.current_task_id.read().await.clone()
    }

    /// Get the approval mode
    pub fn approval_mode(&self) -> ExecutionApprovalMode {
        self.config.approval_mode
    }

    /// Set the approval mode
    pub fn set_approval_mode(&mut self, mode: ExecutionApprovalMode) {
        self.config.approval_mode = mode;
    }

    /// Check if a task needs approval based on current mode
    pub fn needs_approval(&self, _task: &Task) -> bool {
        match self.config.approval_mode {
            ExecutionApprovalMode::Suggest => true,   // Always ask
            ExecutionApprovalMode::AutoEdit => false, // Auto-approve (shell commands need separate handling)
            ExecutionApprovalMode::FullAuto => false, // Never ask
        }
    }

    /// Get the next task to execute
    #[tracing::instrument(name = "agent.plan.next_task", skip(self))]
    pub async fn get_next_task(&self) -> Option<Task> {
        let task_mgr = self.task_manager.read().await;
        let ready_tasks = task_mgr.get_ready_tasks().await;
        ready_tasks.into_iter().next()
    }

    /// Start executing a specific task
    #[tracing::instrument(name = "agent.plan.start_task", skip(self))]
    pub async fn start_task(&self, task_id: &str) -> Result<()> {
        let task_mgr = self.task_manager.write().await;

        // Check if task can start
        match task_mgr.can_start(task_id).await {
            Ok(true) => {}
            Ok(false) => {
                anyhow::bail!(
                    "Task '{}' cannot be started (may already be completed)",
                    task_id
                );
            }
            Err(blocking_tasks) => {
                anyhow::bail!(
                    "Task '{}' is blocked by incomplete dependencies: {}",
                    task_id,
                    blocking_tasks.join(", ")
                );
            }
        }

        // Start the task
        task_mgr.start_task(task_id).await?;

        // Update current task
        *self.current_task_id.write().await = Some(task_id.to_string());

        // Update status
        *self.status.write().await = PlanExecutionStatus::Running;

        Ok(())
    }

    /// Complete the current task
    #[tracing::instrument(name = "agent.plan.complete_task", skip(self, summary))]
    pub async fn complete_current_task(&self, summary: String) -> Result<Option<Task>> {
        let task_id = {
            let current = self.current_task_id.read().await;
            current.clone()
        };

        if let Some(task_id) = task_id {
            let task_mgr = self.task_manager.write().await;
            task_mgr.complete_task(&task_id, summary).await?;

            // Clear current task
            *self.current_task_id.write().await = None;

            // Check if plan is complete
            let stats = task_mgr.get_stats().await;
            if stats.completed == stats.total {
                *self.status.write().await = PlanExecutionStatus::Completed;
            }

            // Get and return the next task if auto-advance is enabled
            if self.config.auto_advance {
                let ready_tasks = task_mgr.get_ready_tasks().await;
                return Ok(ready_tasks.into_iter().next());
            }
        }

        Ok(None)
    }

    /// Skip the current task
    pub async fn skip_current_task(&self, reason: Option<String>) -> Result<Option<Task>> {
        let task_id = {
            let current = self.current_task_id.read().await;
            current.clone()
        };

        if let Some(task_id) = task_id {
            let task_mgr = self.task_manager.write().await;
            task_mgr.skip_task(&task_id, reason).await?;

            // Clear current task
            *self.current_task_id.write().await = None;

            // Get next task if auto-advance
            if self.config.auto_advance {
                let ready_tasks = task_mgr.get_ready_tasks().await;
                return Ok(ready_tasks.into_iter().next());
            }
        }

        Ok(None)
    }

    /// Fail the current task
    pub async fn fail_current_task(&self, error: String) -> Result<()> {
        let task_id = {
            let current = self.current_task_id.read().await;
            current.clone()
        };

        if let Some(task_id) = task_id {
            let task_mgr = self.task_manager.write().await;
            task_mgr.fail_task(&task_id, error.clone()).await?;

            // Clear current task
            *self.current_task_id.write().await = None;

            if self.config.stop_on_error {
                *self.status.write().await = PlanExecutionStatus::Failed(error);
            }
        }

        Ok(())
    }

    /// Pause execution
    pub async fn pause(&self) {
        *self.status.write().await = PlanExecutionStatus::Paused;
    }

    /// Resume execution
    pub async fn resume(&self) -> Option<Task> {
        *self.status.write().await = PlanExecutionStatus::Running;

        // Return current task or get next ready task
        let current = self.current_task_id.read().await.clone();
        if current.is_some() {
            let task_mgr = self.task_manager.read().await;
            if let Some(id) = current {
                return task_mgr.get_task(&id).await;
            }
        }

        self.get_next_task().await
    }

    /// Request approval for a task (in Suggest mode)
    pub async fn request_approval(&self, task: &Task) {
        *self.status.write().await =
            PlanExecutionStatus::WaitingForApproval(task.description.clone());
    }

    /// Approve and start a task
    pub async fn approve_and_start(&self, task_id: &str) -> Result<()> {
        self.start_task(task_id).await
    }

    /// Get execution progress
    pub async fn get_progress(&self) -> ExecutionProgress {
        let task_mgr = self.task_manager.read().await;
        let stats = task_mgr.get_stats().await;
        let time_stats = task_mgr.get_time_stats().await;

        ExecutionProgress {
            total_tasks: stats.total,
            completed_tasks: stats.completed,
            in_progress_tasks: stats.in_progress,
            pending_tasks: stats.pending,
            blocked_tasks: stats.blocked,
            skipped_tasks: stats.skipped,
            failed_tasks: stats.failed,
            total_duration_secs: time_stats.total_duration_secs,
            average_task_duration_secs: time_stats.average_duration_secs,
            estimated_remaining_secs: task_mgr.estimate_remaining_time().await,
        }
    }

    /// Format progress as a string
    pub async fn format_progress(&self) -> String {
        let progress = self.get_progress().await;
        let status = self.status().await;

        let mut output = format!(
            "Plan Execution Status: {}\n\
             Progress: {}/{} tasks completed\n",
            status, progress.completed_tasks, progress.total_tasks
        );

        if progress.in_progress_tasks > 0 {
            output.push_str(&format!("  In Progress: {}\n", progress.in_progress_tasks));
        }
        if progress.blocked_tasks > 0 {
            output.push_str(&format!("  Blocked: {}\n", progress.blocked_tasks));
        }
        if progress.skipped_tasks > 0 {
            output.push_str(&format!("  Skipped: {}\n", progress.skipped_tasks));
        }
        if progress.failed_tasks > 0 {
            output.push_str(&format!("  Failed: {}\n", progress.failed_tasks));
        }

        if progress.total_duration_secs > 0 {
            output.push_str(&format!(
                "Time: {} elapsed",
                format_duration(progress.total_duration_secs)
            ));

            if let Some(remaining) = progress.estimated_remaining_secs {
                output.push_str(&format!(", ~{} remaining", format_duration(remaining)));
            }
            output.push('\n');
        }

        output
    }
}

/// Execution progress information
#[derive(Debug, Clone)]
pub struct ExecutionProgress {
    /// Total number of tasks in the plan.
    pub total_tasks: usize,
    /// Number of completed tasks.
    pub completed_tasks: usize,
    /// Number of tasks currently in progress.
    pub in_progress_tasks: usize,
    /// Number of pending tasks.
    pub pending_tasks: usize,
    /// Number of blocked tasks.
    pub blocked_tasks: usize,
    /// Number of skipped tasks.
    pub skipped_tasks: usize,
    /// Number of failed tasks.
    pub failed_tasks: usize,
    /// Total elapsed duration in seconds.
    pub total_duration_secs: i64,
    /// Average task duration in seconds.
    pub average_task_duration_secs: Option<i64>,
    /// Estimated remaining time in seconds.
    pub estimated_remaining_secs: Option<i64>,
}

/// Format duration in human readable form
fn format_duration(secs: i64) -> String {
    if secs < 60 {
        format!("{}s", secs)
    } else if secs < 3600 {
        format!("{}m {}s", secs / 60, secs % 60)
    } else {
        format!("{}h {}m", secs / 3600, (secs % 3600) / 60)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use brainwires_core::{PlanStatus, TaskPriority, TaskStatus};

    fn create_test_plan() -> PlanMetadata {
        PlanMetadata {
            plan_id: "test-plan-1".to_string(),
            conversation_id: "conv-1".to_string(),
            title: "Test Plan".to_string(),
            task_description: "Test the plan executor".to_string(),
            plan_content: "1. First task\n2. Second task".to_string(),
            model_id: None,
            status: PlanStatus::Active,
            executed: false,
            iterations_used: 0,
            created_at: 0,
            updated_at: 0,
            file_path: None,
            embedding: None,
            // Branching fields
            parent_plan_id: None,
            child_plan_ids: Vec::new(),
            branch_name: None,
            merged: false,
            depth: 0,
        }
    }

    async fn create_test_task_manager() -> Arc<RwLock<TaskManager>> {
        let task_mgr = TaskManager::new();

        // Add test tasks
        task_mgr
            .create_task("First task".to_string(), None, TaskPriority::Normal)
            .await
            .unwrap();
        task_mgr
            .create_task("Second task".to_string(), None, TaskPriority::Normal)
            .await
            .unwrap();

        Arc::new(RwLock::new(task_mgr))
    }

    #[tokio::test]
    async fn test_executor_creation() {
        let plan = create_test_plan();
        let task_mgr = create_test_task_manager().await;
        let config = PlanExecutionConfig::default();

        let executor = PlanExecutorAgent::new(plan, task_mgr, config);

        assert_eq!(executor.status().await, PlanExecutionStatus::Idle);
        assert!(executor.current_task_id().await.is_none());
    }

    #[tokio::test]
    async fn test_approval_modes() {
        let plan = create_test_plan();
        let task_mgr = create_test_task_manager().await;
        let config = PlanExecutionConfig::default();

        let mut executor = PlanExecutorAgent::new(plan, task_mgr, config);

        // Default is FullAuto
        assert_eq!(executor.approval_mode(), ExecutionApprovalMode::FullAuto);

        // Change mode
        executor.set_approval_mode(ExecutionApprovalMode::Suggest);
        assert_eq!(executor.approval_mode(), ExecutionApprovalMode::Suggest);
    }

    #[tokio::test]
    async fn test_get_next_task() {
        let plan = create_test_plan();
        let task_mgr = create_test_task_manager().await;
        let config = PlanExecutionConfig::default();

        let executor = PlanExecutorAgent::new(plan, task_mgr, config);

        let next = executor.get_next_task().await;
        assert!(next.is_some());
        // Don't check specific task - order is non-deterministic
        let desc = next.unwrap().description;
        assert!(desc == "First task" || desc == "Second task");
    }

    #[tokio::test]
    async fn test_start_task() {
        let plan = create_test_plan();
        let task_mgr = create_test_task_manager().await;
        let config = PlanExecutionConfig::default();

        // Get the first task ID
        let task_id = {
            let mgr = task_mgr.read().await;
            let tasks = mgr.get_all_tasks().await;
            tasks[0].id.clone()
        };

        let executor = PlanExecutorAgent::new(plan, task_mgr.clone(), config);

        // Start the task
        executor.start_task(&task_id).await.unwrap();

        assert_eq!(executor.status().await, PlanExecutionStatus::Running);
        assert_eq!(executor.current_task_id().await, Some(task_id.clone()));

        // Verify task status in manager
        let mgr = task_mgr.read().await;
        let task = mgr.get_task(&task_id).await.unwrap();
        assert_eq!(task.status, TaskStatus::InProgress);
    }

    #[tokio::test]
    async fn test_complete_task() {
        let plan = create_test_plan();
        let task_mgr = create_test_task_manager().await;
        let config = PlanExecutionConfig::default();

        let task_id = {
            let mgr = task_mgr.read().await;
            let tasks = mgr.get_all_tasks().await;
            tasks[0].id.clone()
        };

        let executor = PlanExecutorAgent::new(plan, task_mgr.clone(), config);

        // Start and complete task
        executor.start_task(&task_id).await.unwrap();
        let next = executor
            .complete_current_task("Done".to_string())
            .await
            .unwrap();

        // Should get next task due to auto-advance
        assert!(next.is_some());
        // Don't check specific task - the other task should be returned
        let next_desc = next.unwrap().description;
        let started_desc = {
            let mgr = task_mgr.read().await;
            mgr.get_task(&task_id).await.unwrap().description.clone()
        };
        // Next task should be different from the one we completed
        assert_ne!(next_desc, started_desc);

        // Current task should be cleared
        assert!(executor.current_task_id().await.is_none());
    }

    #[tokio::test]
    async fn test_pause_resume() {
        let plan = create_test_plan();
        let task_mgr = create_test_task_manager().await;
        let config = PlanExecutionConfig::default();

        let executor = PlanExecutorAgent::new(plan, task_mgr, config);

        executor.pause().await;
        assert_eq!(executor.status().await, PlanExecutionStatus::Paused);

        let next = executor.resume().await;
        assert_eq!(executor.status().await, PlanExecutionStatus::Running);
        assert!(next.is_some());
    }

    #[tokio::test]
    async fn test_progress() {
        let plan = create_test_plan();
        let task_mgr = create_test_task_manager().await;
        let config = PlanExecutionConfig::default();

        let task_id = {
            let mgr = task_mgr.read().await;
            let tasks = mgr.get_all_tasks().await;
            tasks[0].id.clone()
        };

        let executor = PlanExecutorAgent::new(plan, task_mgr, config);

        // Start task
        executor.start_task(&task_id).await.unwrap();

        let progress = executor.get_progress().await;
        assert_eq!(progress.total_tasks, 2);
        assert_eq!(progress.in_progress_tasks, 1);
        assert_eq!(progress.pending_tasks, 1);
        assert_eq!(progress.completed_tasks, 0);
    }

    #[test]
    fn test_approval_mode_parsing() {
        assert_eq!(
            "suggest".parse::<ExecutionApprovalMode>().unwrap(),
            ExecutionApprovalMode::Suggest
        );
        assert_eq!(
            "auto-edit".parse::<ExecutionApprovalMode>().unwrap(),
            ExecutionApprovalMode::AutoEdit
        );
        assert_eq!(
            "full-auto".parse::<ExecutionApprovalMode>().unwrap(),
            ExecutionApprovalMode::FullAuto
        );
        assert_eq!(
            "auto".parse::<ExecutionApprovalMode>().unwrap(),
            ExecutionApprovalMode::FullAuto
        );
    }

    #[test]
    fn test_format_duration() {
        assert_eq!(format_duration(30), "30s");
        assert_eq!(format_duration(90), "1m 30s");
        assert_eq!(format_duration(3661), "1h 1m");
    }
}