brainwires-agents 0.10.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
//! Agent Pool - Manages a pool of background task agents
//!
//! [`AgentPool`] handles the lifecycle of [`TaskAgent`]s: spawning, monitoring,
//! stopping, and awaiting results. All agents in the pool share the same
//! [`Provider`], tool executor, communication hub, and file lock manager.
//!
//! ## Usage
//!
//! ```rust,ignore
//! use std::sync::Arc;
//! use brainwires_agents::{AgentPool, TaskAgentConfig};
//! use brainwires_core::Task;
//!
//! let pool = AgentPool::new(
//!     10,
//!     Arc::clone(&provider),
//!     Arc::clone(&tool_executor),
//!     Arc::clone(&hub),
//!     Arc::clone(&lock_manager),
//!     "/my/project".to_string(),
//! );
//!
//! let agent_id = pool.spawn_agent(
//!     Task::new("t-1", "Implement feature X"),
//!     None,
//! ).await?;
//!
//! let result = pool.await_completion(&agent_id).await?;
//! ```

use std::collections::HashMap;
use std::sync::Arc;

use anyhow::{Result, anyhow};
use tokio::sync::RwLock;
use tokio::task::JoinHandle;

use brainwires_core::{Provider, Task};
use brainwires_tools::ToolExecutor;

use crate::communication::CommunicationHub;
use crate::context::AgentContext;
use crate::file_locks::FileLockManager;
use crate::task_agent::{
    TaskAgent, TaskAgentConfig, TaskAgentResult, TaskAgentStatus, spawn_task_agent,
};

// ── Internal handle ────────────────────────────────────────────────────────

struct AgentHandle {
    agent: Arc<TaskAgent>,
    join_handle: JoinHandle<Result<TaskAgentResult>>,
}

// ── Public API ─────────────────────────────────────────────────────────────

/// Manages a pool of background [`TaskAgent`]s.
///
/// All agents share the same provider, tool executor, communication hub,
/// file lock manager, and working directory. Each agent gets its own
/// conversation history and working set.
pub struct AgentPool {
    max_agents: usize,
    agents: Arc<RwLock<HashMap<String, AgentHandle>>>,
    communication_hub: Arc<CommunicationHub>,
    file_lock_manager: Arc<FileLockManager>,
    provider: Arc<dyn Provider>,
    tool_executor: Arc<dyn ToolExecutor>,
    working_directory: String,
}

impl AgentPool {
    /// Create a new agent pool.
    ///
    /// # Parameters
    /// - `max_agents`: maximum number of concurrently running agents.
    /// - `provider`: AI provider shared by all agents.
    /// - `tool_executor`: tool executor shared by all agents.
    /// - `communication_hub`: inter-agent message bus.
    /// - `file_lock_manager`: file coordination across agents.
    /// - `working_directory`: default working directory for spawned agents.
    pub fn new(
        max_agents: usize,
        provider: Arc<dyn Provider>,
        tool_executor: Arc<dyn ToolExecutor>,
        communication_hub: Arc<CommunicationHub>,
        file_lock_manager: Arc<FileLockManager>,
        working_directory: impl Into<String>,
    ) -> Self {
        Self {
            max_agents,
            agents: Arc::new(RwLock::new(HashMap::new())),
            communication_hub,
            file_lock_manager,
            provider,
            tool_executor,
            working_directory: working_directory.into(),
        }
    }

    /// Spawn a new task agent and start it on a Tokio background task.
    ///
    /// Returns the agent ID. Use [`await_completion`][Self::await_completion]
    /// to wait for the result.
    ///
    /// Returns an error if the pool is already at capacity.
    pub async fn spawn_agent(&self, task: Task, config: Option<TaskAgentConfig>) -> Result<String> {
        {
            let agents = self.agents.read().await;
            if agents.len() >= self.max_agents {
                return Err(anyhow!(
                    "Agent pool is full ({}/{})",
                    agents.len(),
                    self.max_agents
                ));
            }
        }

        let agent_id = format!("agent-{}", uuid::Uuid::new_v4());
        let config = config.unwrap_or_default();

        let context = Arc::new(AgentContext::new(
            self.working_directory.clone(),
            Arc::clone(&self.tool_executor),
            Arc::clone(&self.communication_hub),
            Arc::clone(&self.file_lock_manager),
        ));

        let agent = Arc::new(TaskAgent::new(
            agent_id.clone(),
            task,
            Arc::clone(&self.provider),
            context,
            config,
        ));

        let handle = spawn_task_agent(Arc::clone(&agent));

        self.agents.write().await.insert(
            agent_id.clone(),
            AgentHandle {
                agent,
                join_handle: handle,
            },
        );

        tracing::info!(agent_id = %agent_id, "spawned agent");
        Ok(agent_id)
    }

    /// Spawn a new task agent with a custom [`AgentContext`].
    ///
    /// Unlike [`spawn_agent`][Self::spawn_agent] which uses the pool's default
    /// working directory, this method accepts a pre-built context. This is
    /// useful for workers that run in isolated worktrees with per-agent
    /// working directories.
    ///
    /// Returns the agent ID.
    pub async fn spawn_agent_with_context(
        &self,
        task: Task,
        context: Arc<AgentContext>,
        config: Option<TaskAgentConfig>,
    ) -> Result<String> {
        {
            let agents = self.agents.read().await;
            if agents.len() >= self.max_agents {
                return Err(anyhow!(
                    "Agent pool is full ({}/{})",
                    agents.len(),
                    self.max_agents
                ));
            }
        }

        let agent_id = format!("agent-{}", uuid::Uuid::new_v4());
        let config = config.unwrap_or_default();

        let agent = Arc::new(TaskAgent::new(
            agent_id.clone(),
            task,
            Arc::clone(&self.provider),
            context,
            config,
        ));

        let handle = spawn_task_agent(Arc::clone(&agent));

        self.agents.write().await.insert(
            agent_id.clone(),
            AgentHandle {
                agent,
                join_handle: handle,
            },
        );

        tracing::info!(agent_id = %agent_id, "spawned agent with custom context");
        Ok(agent_id)
    }

    /// Get the current status of an agent.
    ///
    /// Returns `None` if the agent is not in the pool.
    pub async fn get_status(&self, agent_id: &str) -> Option<TaskAgentStatus> {
        let agents = self.agents.read().await;
        let handle = agents.get(agent_id)?;
        Some(handle.agent.status().await)
    }

    /// Get a snapshot of the task assigned to an agent.
    pub async fn get_task(&self, agent_id: &str) -> Option<Task> {
        let agents = self.agents.read().await;
        let handle = agents.get(agent_id)?;
        Some(handle.agent.task().await)
    }

    /// Abort an agent and remove it from the pool.
    ///
    /// File locks held by the agent are released immediately.
    pub async fn stop_agent(&self, agent_id: &str) -> Result<()> {
        let handle = self
            .agents
            .write()
            .await
            .remove(agent_id)
            .ok_or_else(|| anyhow!("Agent {} not found", agent_id))?;

        handle.join_handle.abort();
        self.file_lock_manager.release_all_locks(agent_id).await;
        tracing::info!(agent_id = %agent_id, "stopped agent");
        Ok(())
    }

    /// Wait for an agent to finish and return its result.
    ///
    /// The agent is removed from the pool once it completes.
    pub async fn await_completion(&self, agent_id: &str) -> Result<TaskAgentResult> {
        let handle = self.agents.write().await.remove(agent_id);

        match handle {
            Some(h) => match h.join_handle.await {
                Ok(result) => result,
                Err(e) => Err(anyhow!("Agent task panicked: {}", e)),
            },
            None => Err(anyhow!("Agent {} not found", agent_id)),
        }
    }

    /// List all agents currently in the pool with their status.
    pub async fn list_active(&self) -> Vec<(String, TaskAgentStatus)> {
        let agents = self.agents.read().await;
        let mut out = Vec::with_capacity(agents.len());
        for (id, handle) in agents.iter() {
            out.push((id.clone(), handle.agent.status().await));
        }
        out
    }

    /// Number of agents currently in the pool (running or pending cleanup).
    pub async fn active_count(&self) -> usize {
        self.agents.read().await.len()
    }

    /// Returns `true` if the agent is still running (join handle not finished).
    pub async fn is_running(&self, agent_id: &str) -> bool {
        let agents = self.agents.read().await;
        agents
            .get(agent_id)
            .map(|h| !h.join_handle.is_finished())
            .unwrap_or(false)
    }

    /// Remove all finished agents from the pool and return their results.
    pub async fn cleanup_completed(&self) -> Vec<(String, Result<TaskAgentResult>)> {
        let finished: Vec<String> = {
            let agents = self.agents.read().await;
            agents
                .iter()
                .filter(|(_, h)| h.join_handle.is_finished())
                .map(|(id, _)| id.clone())
                .collect()
        };

        let mut results = Vec::new();
        let mut agents = self.agents.write().await;
        for id in finished {
            if let Some(handle) = agents.remove(&id) {
                let result = match handle.join_handle.await {
                    Ok(r) => r,
                    Err(e) => Err(anyhow!("Agent task panicked: {}", e)),
                };
                results.push((id, result));
            }
        }
        results
    }

    /// Wait for every agent in the pool to finish.
    pub async fn await_all(&self) -> Vec<(String, Result<TaskAgentResult>)> {
        let ids: Vec<String> = self.agents.read().await.keys().cloned().collect();
        let mut results = Vec::new();
        for id in ids {
            results.push((id.clone(), self.await_completion(&id).await));
        }
        results
    }

    /// Abort all agents and clear the pool.
    pub async fn shutdown(&self) {
        let mut agents = self.agents.write().await;
        for (agent_id, handle) in agents.drain() {
            handle.join_handle.abort();
            self.file_lock_manager.release_all_locks(&agent_id).await;
        }
        tracing::info!("agent pool shut down");
    }

    /// Get a statistical snapshot of the pool.
    pub async fn stats(&self) -> AgentPoolStats {
        let agents = self.agents.read().await;
        let mut running = 0usize;
        let mut completed = 0usize;

        for (_, handle) in agents.iter() {
            if handle.join_handle.is_finished() {
                completed += 1;
            } else {
                running += 1;
            }
        }

        AgentPoolStats {
            max_agents: self.max_agents,
            total_agents: agents.len(),
            running,
            completed,
            failed: 0, // Not distinguishable without awaiting the handle.
        }
    }

    /// Get the shared file lock manager.
    pub fn file_lock_manager(&self) -> Arc<FileLockManager> {
        Arc::clone(&self.file_lock_manager)
    }

    /// Get the shared communication hub.
    pub fn communication_hub(&self) -> Arc<CommunicationHub> {
        Arc::clone(&self.communication_hub)
    }
}

/// Statistics about the agent pool.
#[derive(Debug, Clone)]
pub struct AgentPoolStats {
    /// Maximum concurrent agents allowed.
    pub max_agents: usize,
    /// Total agents currently tracked (running + awaiting cleanup).
    pub total_agents: usize,
    /// Agents that are currently running.
    pub running: usize,
    /// Agents that have finished but not yet cleaned up.
    pub completed: usize,
    /// Agents that are known to have failed (requires awaiting the handle).
    pub failed: usize,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::communication::CommunicationHub;
    use crate::file_locks::FileLockManager;
    use async_trait::async_trait;
    use brainwires_core::{
        ChatOptions, ChatResponse, Message, StreamChunk, Tool, ToolContext, ToolResult, ToolUse,
        Usage,
    };
    use brainwires_tools::ToolExecutor;
    use futures::stream::BoxStream;

    struct MockProvider(ChatResponse);

    impl MockProvider {
        fn done(text: &str) -> Self {
            Self(ChatResponse {
                message: Message::assistant(text),
                finish_reason: Some("stop".to_string()),
                usage: Usage::default(),
            })
        }
    }

    #[async_trait]
    impl Provider for MockProvider {
        fn name(&self) -> &str {
            "mock"
        }

        async fn chat(
            &self,
            _: &[Message],
            _: Option<&[Tool]>,
            _: &ChatOptions,
        ) -> Result<ChatResponse> {
            Ok(self.0.clone())
        }

        fn stream_chat<'a>(
            &'a self,
            _: &'a [Message],
            _: Option<&'a [Tool]>,
            _: &'a ChatOptions,
        ) -> BoxStream<'a, Result<StreamChunk>> {
            Box::pin(futures::stream::empty())
        }
    }

    struct NoOpExecutor;

    #[async_trait]
    impl ToolExecutor for NoOpExecutor {
        async fn execute(&self, tu: &ToolUse, _: &ToolContext) -> Result<ToolResult> {
            Ok(ToolResult::success(tu.id.clone(), "ok".to_string()))
        }

        fn available_tools(&self) -> Vec<Tool> {
            vec![]
        }
    }

    fn make_pool(max: usize) -> AgentPool {
        AgentPool::new(
            max,
            Arc::new(MockProvider::done("Done")),
            Arc::new(NoOpExecutor),
            Arc::new(CommunicationHub::new()),
            Arc::new(FileLockManager::new()),
            "/tmp",
        )
    }

    #[tokio::test]
    async fn test_pool_creation() {
        let pool = make_pool(5);
        assert_eq!(pool.active_count().await, 0);
    }

    #[tokio::test]
    async fn test_spawn_and_count() {
        let pool = make_pool(5);
        let _ = pool
            .spawn_agent(
                Task::new("t-1", "Test"),
                Some(TaskAgentConfig {
                    validation_config: None,
                    ..Default::default()
                }),
            )
            .await
            .unwrap();
        assert_eq!(pool.active_count().await, 1);
    }

    #[tokio::test]
    async fn test_max_agents_limit() {
        let pool = make_pool(2);
        let cfg = || {
            Some(TaskAgentConfig {
                validation_config: None,
                ..Default::default()
            })
        };

        pool.spawn_agent(Task::new("t-1", "T1"), cfg())
            .await
            .unwrap();
        pool.spawn_agent(Task::new("t-2", "T2"), cfg())
            .await
            .unwrap();

        let err = pool.spawn_agent(Task::new("t-3", "T3"), cfg()).await;
        assert!(err.is_err());
        assert!(err.unwrap_err().to_string().contains("full"));
    }

    #[tokio::test]
    async fn test_await_completion() {
        let pool = make_pool(5);
        let id = pool
            .spawn_agent(
                Task::new("t-1", "Finish me"),
                Some(TaskAgentConfig {
                    validation_config: None,
                    ..Default::default()
                }),
            )
            .await
            .unwrap();

        let result = pool.await_completion(&id).await.unwrap();
        assert!(result.success);
        assert_eq!(result.task_id, "t-1");
    }

    #[tokio::test]
    async fn test_stop_agent() {
        let pool = make_pool(5);
        let id = pool.spawn_agent(Task::new("t-1", "T"), None).await.unwrap();

        pool.stop_agent(&id).await.unwrap();
        assert_eq!(pool.active_count().await, 0);
    }

    #[tokio::test]
    async fn test_shutdown() {
        let pool = make_pool(5);
        pool.spawn_agent(Task::new("t-1", "T1"), None)
            .await
            .unwrap();
        pool.spawn_agent(Task::new("t-2", "T2"), None)
            .await
            .unwrap();

        pool.shutdown().await;
        assert_eq!(pool.active_count().await, 0);
    }

    #[tokio::test]
    async fn test_stats() {
        let pool = make_pool(10);
        let stats = pool.stats().await;
        assert_eq!(stats.max_agents, 10);
        assert_eq!(stats.total_agents, 0);
    }

    #[tokio::test]
    async fn test_list_active() {
        let pool = make_pool(5);
        pool.spawn_agent(Task::new("t-1", "T1"), None)
            .await
            .unwrap();
        pool.spawn_agent(Task::new("t-2", "T2"), None)
            .await
            .unwrap();

        let active = pool.list_active().await;
        assert_eq!(active.len(), 2);
    }
}