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
//! Multi-agent orchestration pool.
//!
//! Manages concurrent agent instances with message passing
//! and fan-out/fan-in patterns. Each agent runs its own
//! perceive-reason-act loop in a separate tokio task.
//!
//! # Toyota Production System Principles
//!
//! - **Heijunka**: Load-level work across agents
//! - **Jidoka**: Each agent has its own `LoopGuard`
//! - **Muda**: Bounded concurrency prevents resource waste
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::mpsc;
use tokio::task::JoinSet;
use tracing::{debug, info, warn};
use super::driver::{LlmDriver, StreamEvent};
use super::manifest::AgentManifest;
use super::memory::{InMemorySubstrate, MemorySubstrate};
use super::result::{AgentError, AgentLoopResult};
use super::tool::ToolRegistry;
/// Unique identifier for a spawned agent.
pub type AgentId = u64;
/// Message sent between agents in the pool.
#[derive(Debug, Clone)]
pub struct AgentMessage {
/// Source agent ID (0 = external/supervisor).
pub from: AgentId,
/// Target agent ID.
pub to: AgentId,
/// Message payload.
pub content: String,
}
/// Configuration for a spawned agent.
pub struct SpawnConfig {
/// Agent manifest.
pub manifest: AgentManifest,
/// Query to execute.
pub query: String,
}
/// Routes messages between agents in a pool.
///
/// Each agent gets an inbox (bounded `mpsc` channel). The router
/// holds senders keyed by `AgentId`, so any agent can send to any
/// other agent via the shared router reference.
#[derive(Clone)]
pub struct MessageRouter {
inboxes: Arc<std::sync::RwLock<HashMap<AgentId, mpsc::Sender<AgentMessage>>>>,
inbox_capacity: usize,
}
impl MessageRouter {
/// Create a new message router.
pub fn new(inbox_capacity: usize) -> Self {
Self { inboxes: Arc::new(std::sync::RwLock::new(HashMap::new())), inbox_capacity }
}
/// Register an agent inbox, returning the receiver.
pub fn register(&self, agent_id: AgentId) -> mpsc::Receiver<AgentMessage> {
let (tx, rx) = mpsc::channel(self.inbox_capacity);
let mut inboxes = self.inboxes.write().expect("message router lock");
inboxes.insert(agent_id, tx);
rx
}
/// Unregister an agent (removes its inbox sender).
pub fn unregister(&self, agent_id: AgentId) {
let mut inboxes = self.inboxes.write().expect("message router lock");
inboxes.remove(&agent_id);
}
/// Send a message to a target agent.
///
/// Returns `Err` if target agent is not registered or inbox
/// is full (bounded channel protects against backpressure).
pub async fn send(&self, msg: AgentMessage) -> Result<(), String> {
let tx = {
let inboxes = self.inboxes.read().expect("message router lock");
inboxes
.get(&msg.to)
.cloned()
.ok_or_else(|| format!("agent {} not registered", msg.to))?
};
tx.send(msg).await.map_err(|e| format!("inbox closed: {e}"))
}
/// Number of registered agents.
pub fn agent_count(&self) -> usize {
let inboxes = self.inboxes.read().expect("message router lock");
inboxes.len()
}
}
/// Function that builds a `ToolRegistry` from a manifest.
pub type ToolBuilder = Arc<dyn Fn(&AgentManifest) -> ToolRegistry + Send + Sync>;
/// Multi-agent orchestration pool.
///
/// Manages concurrent agent instances, each running its own
/// perceive-reason-act loop. Supports fan-out (spawn many) and
/// fan-in (collect results) patterns.
///
/// ```rust,ignore
/// let mut pool = AgentPool::new(driver, 4);
/// pool.spawn(config1).await?;
/// pool.spawn(config2).await?;
/// let results = pool.join_all().await;
/// ```
pub struct AgentPool {
driver: Arc<dyn LlmDriver>,
memory: Arc<dyn MemorySubstrate>,
next_id: AgentId,
max_concurrent: usize,
join_set: JoinSet<(AgentId, String, Result<AgentLoopResult, String>)>,
stream_tx: Option<mpsc::Sender<StreamEvent>>,
router: MessageRouter,
tool_builder: Option<ToolBuilder>,
}
impl AgentPool {
/// Create a new agent pool with bounded concurrency.
pub fn new(driver: Arc<dyn LlmDriver>, max_concurrent: usize) -> Self {
Self {
driver,
memory: Arc::new(InMemorySubstrate::new()),
next_id: 1,
max_concurrent,
join_set: JoinSet::new(),
stream_tx: None,
router: MessageRouter::new(32),
tool_builder: None,
}
}
/// Access the message router for inter-agent messaging.
pub fn router(&self) -> &MessageRouter {
&self.router
}
/// Set a shared memory substrate for all agents.
#[must_use]
pub fn with_memory(mut self, memory: Arc<dyn MemorySubstrate>) -> Self {
self.memory = memory;
self
}
/// Set a stream event channel for pool-level events.
#[must_use]
pub fn with_stream(mut self, tx: mpsc::Sender<StreamEvent>) -> Self {
self.stream_tx = Some(tx);
self
}
/// Set a tool builder for spawned agents.
///
/// When set, each spawned agent gets tools built from its
/// manifest rather than an empty registry.
#[must_use]
pub fn with_tool_builder(mut self, builder: ToolBuilder) -> Self {
self.tool_builder = Some(builder);
self
}
/// Number of currently active agents.
pub fn active_count(&self) -> usize {
self.join_set.len()
}
/// Maximum concurrent agents allowed.
pub fn max_concurrent(&self) -> usize {
self.max_concurrent
}
/// Spawn a new agent in the pool.
///
/// Returns the `AgentId` assigned to this agent.
/// Returns error if pool is at capacity.
pub fn spawn(&mut self, config: SpawnConfig) -> Result<AgentId, AgentError> {
if self.join_set.len() >= self.max_concurrent {
return Err(AgentError::CircuitBreak(format!(
"agent pool at capacity ({}/{})",
self.join_set.len(),
self.max_concurrent
)));
}
let id = self.next_id;
self.next_id += 1;
let name = config.manifest.name.clone();
let driver = Arc::clone(&self.driver);
let memory = Arc::clone(&self.memory);
let stream_tx = self.stream_tx.clone();
// Register agent inbox for inter-agent messaging
let _inbox_rx = self.router.register(id);
let router = self.router.clone();
info!(
agent_id = id,
name = %name,
query_len = config.query.len(),
"spawning agent"
);
let tool_builder = self.tool_builder.clone();
self.join_set.spawn(async move {
let tools = match tool_builder {
Some(builder) => builder(&config.manifest),
None => ToolRegistry::new(),
};
let result = super::runtime::run_agent_loop(
&config.manifest,
&config.query,
driver.as_ref(),
&tools,
memory.as_ref(),
stream_tx,
)
.await;
// Unregister agent from router on completion
router.unregister(id);
// Map error to String to avoid Clone requirement
let mapped = result.map_err(|e| e.to_string());
(id, name, mapped)
});
Ok(id)
}
/// Fan-out: spawn multiple agents concurrently.
///
/// Returns a list of `AgentId`s for the spawned agents.
pub fn fan_out(&mut self, configs: Vec<SpawnConfig>) -> Result<Vec<AgentId>, AgentError> {
let mut ids = Vec::with_capacity(configs.len());
for config in configs {
ids.push(self.spawn(config)?);
}
Ok(ids)
}
/// Fan-in: wait for all active agents to complete.
///
/// Returns results keyed by `AgentId`. Agents that error
/// are included with their error string.
pub async fn join_all(&mut self) -> HashMap<AgentId, Result<AgentLoopResult, String>> {
let mut results = HashMap::new();
while let Some(outcome) = self.join_set.join_next().await {
match outcome {
Ok((id, name, result)) => {
debug!(
agent_id = id,
name = %name,
ok = result.is_ok(),
"agent completed"
);
results.insert(id, result);
}
Err(e) => {
warn!(error = %e, "agent task panicked");
}
}
}
results
}
/// Wait for the next agent to complete.
///
/// Returns `None` if no agents are active.
pub async fn join_next(&mut self) -> Option<(AgentId, Result<AgentLoopResult, String>)> {
match self.join_set.join_next().await {
Some(Ok((id, _name, result))) => Some((id, result)),
Some(Err(e)) => {
warn!(error = %e, "agent task panicked");
None
}
None => None,
}
}
/// Abort all running agents.
pub fn abort_all(&mut self) {
self.join_set.abort_all();
info!("all agents aborted");
}
}
#[cfg(test)]
#[path = "pool_tests.rs"]
mod tests;