echo_agent 0.1.4

Production-grade AI Agent framework for Rust — ReAct engine, multi-agent, memory, streaming, MCP, IM channels, workflows
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
//! Subagent executor — unified dispatch engine for Sync / Fork / Teammate modes
//!
//! The executor receives a [`DispatchRequest`] and routes it to the appropriate
//! execution strategy based on the definition's [`ExecutionMode`].

use crate::error::{AgentError, ReactError, Result};
use echo_core::agent::CancellationToken;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::Semaphore;
use tracing::info;

use super::context::SubagentContext;
use super::events::SubagentEvent;
use super::hooks::{SubagentHookContext, SubagentHookRegistry};
use super::registry::SubagentRegistry;
use super::types::{ExecutionMode, SubagentResult};

// ── Dispatch Request ──────────────────────────────────────────────────────────

/// A request to dispatch a task to a subagent.
#[derive(Debug, Clone)]
pub struct DispatchRequest {
    /// Target subagent name.
    pub agent_name: String,
    /// Task description.
    pub task: String,
    /// Override the definition's default execution mode.
    pub mode_override: Option<ExecutionMode>,
    /// Cancellation token (propagated from parent).
    pub cancel: CancellationToken,
    /// Parent agent name (for logging and topology).
    pub parent_agent: String,
    /// Parent context for inheritance (Fork mode).
    pub parent_context: Option<SubagentContext>,
    /// Current delegation depth (prevents infinite delegation chains).
    pub delegate_depth: u32,
}

/// Maximum delegation depth to prevent infinite chains.
const MAX_DELEGATE_DEPTH: u32 = 3;

// ── Teammate Handle ───────────────────────────────────────────────────────────

/// Handle to a running teammate agent.
///
/// Used to poll for results or cancel execution.
#[derive(Debug)]
pub struct TeammateHandle {
    /// Unique handle ID.
    pub id: String,
    /// The teammate agent name.
    pub agent_name: String,
    /// Cancellation token for this teammate.
    pub cancel: CancellationToken,
    /// Join handle for the spawned task.
    join_handle: tokio::task::JoinHandle<Result<SubagentResult>>,
}

impl TeammateHandle {
    /// Check if the teammate has completed.
    pub fn is_finished(&self) -> bool {
        self.join_handle.is_finished()
    }

    /// Await the teammate's result.
    pub async fn join(self) -> Result<SubagentResult> {
        self.join_handle
            .await
            .map_err(|e| ReactError::Other(format!("Teammate join error: {}", e)))?
    }
}

// ── Executor Config ───────────────────────────────────────────────────────────

/// Configuration for the subagent executor.
#[derive(Debug, Clone)]
pub struct SubagentExecutorConfig {
    /// Maximum concurrent Fork dispatches.
    pub max_concurrent_forks: usize,
    /// Default timeout for Fork/Teammate dispatches (seconds). 0 = no timeout.
    pub default_timeout_secs: u64,
    /// Enable hooks.
    pub enable_hooks: bool,
}

impl Default for SubagentExecutorConfig {
    fn default() -> Self {
        Self {
            max_concurrent_forks: 5,
            default_timeout_secs: 300,
            enable_hooks: true,
        }
    }
}

// ── Subagent Executor ─────────────────────────────────────────────────────────

/// Unified dispatch engine for all subagent execution modes.
pub struct SubagentExecutor {
    registry: Arc<SubagentRegistry>,
    hooks: Arc<SubagentHookRegistry>,
    config: SubagentExecutorConfig,
    semaphore: Arc<Semaphore>,
}

impl SubagentExecutor {
    /// Create a new executor backed by the given registry.
    pub fn new(registry: Arc<SubagentRegistry>, config: SubagentExecutorConfig) -> Self {
        let semaphore = Arc::new(Semaphore::new(config.max_concurrent_forks));
        Self {
            registry,
            hooks: Arc::new(SubagentHookRegistry::new()),
            config,
            semaphore,
        }
    }

    /// Create with a specific hook registry.
    pub fn with_hooks(
        registry: Arc<SubagentRegistry>,
        config: SubagentExecutorConfig,
        hooks: SubagentHookRegistry,
    ) -> Self {
        let semaphore = Arc::new(Semaphore::new(config.max_concurrent_forks));
        Self {
            registry,
            hooks: Arc::new(hooks),
            config,
            semaphore,
        }
    }

    /// Get the hook registry (for external registration).
    pub fn hooks(&self) -> &SubagentHookRegistry {
        &self.hooks
    }

    /// Main dispatch entry point.
    ///
    /// Routes to the appropriate mode based on the definition or override.
    /// Uses a loop for retry/delegation instead of recursion to prevent stack overflow.
    pub async fn dispatch(&self, mut req: DispatchRequest) -> Result<SubagentResult> {
        let mut retry_count: u32 = 0;
        let max_retries: u32 = 3; // Prevent infinite retry loops

        loop {
            // Guard against infinite delegation chains
            if req.delegate_depth > MAX_DELEGATE_DEPTH {
                return Err(ReactError::Other(format!(
                    "Delegation depth exceeded (max {}): agent '{}'",
                    MAX_DELEGATE_DEPTH, req.agent_name
                )));
            }

            // Guard against excessive retries
            if retry_count > max_retries {
                return Err(ReactError::Agent(AgentError::ContextLimitExceeded(
                    format!(
                        "Max retry count exceeded ({}): agent '{}'",
                        max_retries, req.agent_name
                    ),
                )));
            }

            // Look up definition
            let registered = self.registry.get(&req.agent_name).await.ok_or_else(|| {
                ReactError::Other(format!("Subagent '{}' not found", req.agent_name))
            })?;

            let mode = req
                .mode_override
                .as_ref()
                .unwrap_or(&registered.definition.execution_mode)
                .clone();

            // Build hook context
            let hook_ctx = SubagentHookContext {
                parent_agent: req.parent_agent.clone(),
                subagent_name: req.agent_name.clone(),
                execution_mode: mode.clone(),
                task: req.task.clone(),
                attempt: 1 + retry_count,
            };

            // Emit event and call before_dispatch hook
            self.registry
                .event_bus()
                .emit(SubagentEvent::DispatchStarted {
                    parent: req.parent_agent.clone(),
                    agent: req.agent_name.clone(),
                    mode: mode.clone(),
                    task: req.task.clone(),
                });

            if self.config.enable_hooks {
                self.hooks.before_dispatch(&hook_ctx).await;
            }

            // Snapshot fields needed in error path before `req` is moved
            let req_agent_name = req.agent_name.clone();
            let req_parent_agent = req.parent_agent.clone();
            let delegate_depth = req.delegate_depth;

            // Dispatch based on mode
            let start = Instant::now();
            let result = match mode {
                ExecutionMode::Sync => self.dispatch_sync(&req).await,
                ExecutionMode::Fork => self.dispatch_fork(&req).await,
                ExecutionMode::Teammate => {
                    // Teammate mode: spawn independently, then await result
                    match self.dispatch_teammate(req.clone()).await {
                        Ok(handle) => handle.join().await,
                        Err(e) => Err(e),
                    }
                }
            };

            let duration = start.elapsed();

            match result {
                Ok(mut sub_result) => {
                    sub_result.duration = duration;
                    sub_result.mode = mode.clone();

                    self.registry
                        .event_bus()
                        .emit(SubagentEvent::DispatchCompleted {
                            parent: req_parent_agent,
                            agent: req_agent_name,
                            duration_ms: duration.as_millis() as u64,
                        });

                    if self.config.enable_hooks {
                        self.hooks.after_dispatch(&hook_ctx, &sub_result).await;
                    }

                    return Ok(sub_result);
                }
                Err(e) => {
                    let error_str = e.to_string();

                    self.registry
                        .event_bus()
                        .emit(SubagentEvent::DispatchFailed {
                            parent: req_parent_agent,
                            agent: req_agent_name,
                            error: error_str.clone(),
                        });

                    if self.config.enable_hooks {
                        let decision = self.hooks.on_failure(&hook_ctx, &error_str).await;
                        match decision {
                            super::hooks::SubagentRetryDecision::Delegate { alternative_agent } => {
                                info!(
                                    from = %hook_ctx.subagent_name,
                                    to = %alternative_agent,
                                    depth = delegate_depth + 1,
                                    "Delegating to alternative subagent"
                                );
                                retry_count += 1;
                                req = DispatchRequest {
                                    agent_name: alternative_agent,
                                    task: hook_ctx.task.clone(),
                                    mode_override: Some(hook_ctx.execution_mode.clone()),
                                    cancel: CancellationToken::new(),
                                    parent_agent: hook_ctx.parent_agent.clone(),
                                    parent_context: None,
                                    delegate_depth: delegate_depth + 1,
                                };
                                // Loop instead of recursing
                                continue;
                            }
                            super::hooks::SubagentRetryDecision::Retry { delay_secs } => {
                                info!(
                                    delay_secs,
                                    attempt = retry_count + 1,
                                    "Retrying subagent dispatch"
                                );
                                tokio::time::sleep(Duration::from_secs(delay_secs)).await;
                                retry_count += 1;
                                req = DispatchRequest {
                                    agent_name: hook_ctx.subagent_name.clone(),
                                    task: hook_ctx.task.clone(),
                                    mode_override: Some(hook_ctx.execution_mode.clone()),
                                    cancel: CancellationToken::new(),
                                    parent_agent: hook_ctx.parent_agent.clone(),
                                    parent_context: None,
                                    delegate_depth,
                                };
                                // Loop instead of recursing
                                continue;
                            }
                            super::hooks::SubagentRetryDecision::Fail => {}
                        }
                    }

                    return Err(e);
                }
            }
        }
    }

    /// Dispatch a teammate, returning a handle for async polling.
    pub async fn dispatch_teammate(&self, req: DispatchRequest) -> Result<TeammateHandle> {
        let registered =
            self.registry.get(&req.agent_name).await.ok_or_else(|| {
                ReactError::Other(format!("Subagent '{}' not found", req.agent_name))
            })?;

        // get_agent() auto-instantiates from factory if needed
        let agent_arc = self
            .registry
            .get_agent(&req.agent_name)
            .await
            .ok_or_else(|| {
                ReactError::Other(format!(
                    "Cannot get agent instance for '{}'",
                    req.agent_name
                ))
            })?;

        let child_token = req.cancel.child_token();
        let task = req.task.clone();
        let agent_name = req.agent_name.clone();
        let timeout_secs = if registered.definition.timeout_secs > 0 {
            registered.definition.timeout_secs
        } else {
            self.config.default_timeout_secs
        };

        let handle_id = format!("tm_{}", uuid::Uuid::new_v4().as_simple());

        let join_handle = tokio::spawn(async move {
            let _permit = child_token.clone();
            let start = Instant::now();

            let agent = agent_arc.lock().await;

            // Check cancellation before execution
            if child_token.is_cancelled() {
                return Ok(SubagentResult {
                    agent_name: agent_name.clone(),
                    output: "Cancelled before execution".into(),
                    duration: start.elapsed(),
                    iterations: 0,
                    tokens_used: None,
                    was_truncated: false,
                    mode: ExecutionMode::Teammate,
                });
            }

            let execute_future = agent.execute(&task);

            let result = if timeout_secs > 0 {
                // Race between timeout, cancellation, and execution
                tokio::select! {
                    biased; // Check cancellation first
                    _ = child_token.cancelled() => {
                        Err(ReactError::Agent(AgentError::Interrupted))
                    }
                    _ = tokio::time::sleep(Duration::from_secs(timeout_secs)) => {
                        Err(ReactError::Other(format!(
                            "Teammate '{}' timed out after {}s",
                            agent_name, timeout_secs
                        )))
                    }
                    r = execute_future => r,
                }
            } else {
                // Race between cancellation and execution
                tokio::select! {
                    biased;
                    _ = child_token.cancelled() => {
                        Err(ReactError::Agent(AgentError::Interrupted))
                    }
                    r = execute_future => r,
                }
            };

            match result {
                Ok(output) => Ok(SubagentResult {
                    agent_name,
                    output,
                    duration: start.elapsed(),
                    iterations: 1,
                    tokens_used: None,
                    was_truncated: false,
                    mode: ExecutionMode::Teammate,
                }),
                Err(e) => Err(e),
            }
        });

        Ok(TeammateHandle {
            id: handle_id,
            agent_name: req.agent_name.clone(),
            cancel: req.cancel.clone(),
            join_handle,
        })
    }

    // ── Internal dispatch methods ──────────────────────────────────────────

    /// Enhance the task description with inherited parent context.
    ///
    /// Prepends inherited system prompt and conversation history to the task,
    /// giving the subagent awareness of the parent's state.
    fn enhance_task(task: &str, parent_ctx: Option<&super::context::SubagentContext>) -> String {
        let Some(ctx) = parent_ctx else {
            return task.to_string();
        };

        let mut parts = Vec::new();
        if !ctx.system_prompt.is_empty() {
            parts.push(format!("[Inherited System Context]\n{}", ctx.system_prompt));
        }
        if !ctx.messages.is_empty() {
            let history: Vec<String> = ctx
                .messages
                .iter()
                .filter_map(|m| m.content.as_text().map(|c| format!("[{}] {}", m.role, c)))
                .collect();
            if !history.is_empty() {
                parts.push(format!(
                    "[Inherited Conversation History]\n{}",
                    history.join("\n")
                ));
            }
        }
        if parts.is_empty() {
            task.to_string()
        } else {
            format!("{}\n\n---\n\n{}", parts.join("\n\n"), task)
        }
    }

    /// Sync mode: lock the agent, execute, return.
    async fn dispatch_sync(&self, req: &DispatchRequest) -> Result<SubagentResult> {
        let agent_arc = self
            .registry
            .get_agent(&req.agent_name)
            .await
            .ok_or_else(|| {
                ReactError::Other(format!(
                    "Subagent '{}' not found or not instantiated",
                    req.agent_name
                ))
            })?;

        let start = Instant::now();
        let agent = agent_arc.lock().await;

        // Check cancellation
        if req.cancel.is_cancelled() {
            return Ok(SubagentResult {
                agent_name: req.agent_name.clone(),
                output: "Cancelled before execution".into(),
                duration: start.elapsed(),
                iterations: 0,
                tokens_used: None,
                was_truncated: false,
                mode: ExecutionMode::Sync,
            });
        }

        let output = agent
            .execute(&Self::enhance_task(&req.task, req.parent_context.as_ref()))
            .await?;

        Ok(SubagentResult {
            agent_name: req.agent_name.clone(),
            output,
            duration: start.elapsed(),
            iterations: 1,
            tokens_used: None,
            was_truncated: false,
            mode: ExecutionMode::Sync,
        })
    }

    /// Fork mode: acquire semaphore, spawn task, await with timeout.
    async fn dispatch_fork(&self, req: &DispatchRequest) -> Result<SubagentResult> {
        let permit = self
            .semaphore
            .clone()
            .acquire_owned()
            .await
            .map_err(|e| ReactError::Other(format!("Semaphore error: {}", e)))?;

        let registered =
            self.registry.get(&req.agent_name).await.ok_or_else(|| {
                ReactError::Other(format!("Subagent '{}' not found", req.agent_name))
            })?;

        let agent_arc = self
            .registry
            .get_agent(&req.agent_name)
            .await
            .ok_or_else(|| {
                ReactError::Other(format!(
                    "Cannot get agent instance for '{}'",
                    req.agent_name
                ))
            })?;

        let timeout_secs = if registered.definition.timeout_secs > 0 {
            registered.definition.timeout_secs
        } else {
            self.config.default_timeout_secs
        };

        let task = req.task.clone();
        let agent_name = req.agent_name.clone();
        let cancel = req.cancel.clone();
        let enhanced_task = Self::enhance_task(&task, req.parent_context.as_ref());

        let result = tokio::spawn(async move {
            let _permit = permit;
            let start = Instant::now();

            // Check cancellation
            if cancel.is_cancelled() {
                return Ok(SubagentResult {
                    agent_name: agent_name.clone(),
                    output: "Cancelled before execution".into(),
                    duration: start.elapsed(),
                    iterations: 0,
                    tokens_used: None,
                    was_truncated: false,
                    mode: ExecutionMode::Fork,
                });
            }

            let agent = agent_arc.lock().await;

            let result = if timeout_secs > 0 {
                match tokio::time::timeout(
                    Duration::from_secs(timeout_secs),
                    agent.execute(&enhanced_task),
                )
                .await
                {
                    Ok(r) => r,
                    Err(_) => Err(ReactError::Other(format!(
                        "Fork subagent '{}' timed out after {}s",
                        agent_name, timeout_secs
                    ))),
                }
            } else {
                agent.execute(&enhanced_task).await
            };

            result.map(|output| SubagentResult {
                agent_name,
                output,
                duration: start.elapsed(),
                iterations: 1,
                tokens_used: None,
                was_truncated: false,
                mode: ExecutionMode::Fork,
            })
        })
        .await
        .map_err(|e| ReactError::Other(format!("Fork task join error: {}", e)))??;

        Ok(result)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::testing::MockAgent;

    async fn make_executor() -> (Arc<SubagentRegistry>, SubagentExecutor) {
        let registry = Arc::new(SubagentRegistry::new());
        let executor = SubagentExecutor::new(registry.clone(), SubagentExecutorConfig::default());
        (registry, executor)
    }

    #[tokio::test]
    async fn test_dispatch_sync() {
        let (registry, executor) = make_executor().await;

        let agent = MockAgent::new("worker").with_response("done");
        let def = super::super::types::SubagentDefinition::new("worker", "Worker");
        registry.register(def, Box::new(agent)).await;

        let req = DispatchRequest {
            agent_name: "worker".into(),
            task: "do work".into(),
            mode_override: None,
            cancel: CancellationToken::new(),
            parent_agent: "parent".into(),
            parent_context: None,
            delegate_depth: 0,
        };

        let result = executor.dispatch(req).await.unwrap();
        assert_eq!(result.output, "done");
        assert_eq!(result.mode, ExecutionMode::Sync);
    }

    #[tokio::test]
    async fn test_dispatch_not_found() {
        let (_registry, executor) = make_executor().await;

        let req = DispatchRequest {
            agent_name: "missing".into(),
            task: "task".into(),
            mode_override: None,
            cancel: CancellationToken::new(),
            parent_agent: "parent".into(),
            parent_context: None,
            delegate_depth: 0,
        };

        let err = executor.dispatch(req).await.unwrap_err();
        assert!(err.to_string().contains("not found"));
    }

    #[tokio::test]
    async fn test_dispatch_cancelled() {
        let (registry, executor) = make_executor().await;

        let agent = MockAgent::new("c").with_response("ok");
        let def = super::super::types::SubagentDefinition::new("c", "C");
        registry.register(def, Box::new(agent)).await;

        let cancel = CancellationToken::new();
        cancel.cancel();

        let req = DispatchRequest {
            agent_name: "c".into(),
            task: "task".into(),
            mode_override: None,
            cancel,
            parent_agent: "parent".into(),
            parent_context: None,
            delegate_depth: 0,
        };

        let result = executor.dispatch(req).await.unwrap();
        assert!(result.output.contains("Cancelled"));
    }

    #[tokio::test]
    async fn test_dispatch_mode_override() {
        let (registry, executor) = make_executor().await;

        let agent = MockAgent::new("forker").with_response("forked");
        let mut def = super::super::types::SubagentDefinition::new("forker", "Fork agent");
        def.execution_mode = ExecutionMode::Sync; // Default is Sync
        registry.register(def, Box::new(agent)).await;

        let req = DispatchRequest {
            agent_name: "forker".into(),
            task: "task".into(),
            mode_override: Some(ExecutionMode::Fork),
            cancel: CancellationToken::new(),
            parent_agent: "parent".into(),
            parent_context: None,
            delegate_depth: 0,
        };

        let result = executor.dispatch(req).await.unwrap();
        assert_eq!(result.output, "forked");
        assert_eq!(result.mode, ExecutionMode::Fork);
    }

    #[tokio::test]
    async fn test_teammate_dispatch() {
        let (registry, executor) = make_executor().await;

        let agent = MockAgent::new("tm").with_response("team result");
        let mut def = super::super::types::SubagentDefinition::new("tm", "Teammate");
        def.execution_mode = ExecutionMode::Teammate;
        registry.register(def, Box::new(agent)).await;

        let req = DispatchRequest {
            agent_name: "tm".into(),
            task: "team task".into(),
            mode_override: None,
            cancel: CancellationToken::new(),
            parent_agent: "leader".into(),
            parent_context: None,
            delegate_depth: 0,
        };

        let handle = executor.dispatch_teammate(req).await.unwrap();
        assert_eq!(handle.agent_name, "tm");

        let result = handle.join().await.unwrap();
        assert_eq!(result.output, "team result");
    }
}