Skip to main content

car_multi/patterns/
supervisor.rs

1//! Supervisor — one agent monitors and controls workers.
2//!
3//! Each round: workers execute in parallel → supervisor reviews → feedback or approve.
4//! Workers get the supervisor's feedback in the next round.
5//! Stops when supervisor says "APPROVED" or max rounds reached.
6
7use crate::error::MultiError;
8use crate::mailbox::Mailbox;
9use crate::runner::AgentRunner;
10use crate::shared::SharedInfra;
11use crate::types::{AgentOutput, AgentSpec};
12use crate::patterns::swarm::{Swarm, SwarmMode};
13use serde::{Deserialize, Serialize};
14use std::sync::Arc;
15use tracing::instrument;
16
17#[derive(Debug, Clone, Serialize, Deserialize)]
18pub struct SupervisorResult {
19    pub task: String,
20    pub rounds: Vec<Vec<AgentOutput>>,
21    pub supervisor_feedback: Vec<String>,
22    pub final_answer: String,
23    pub approved: bool,
24}
25
26impl SupervisorResult {
27    pub fn total_rounds(&self) -> usize {
28        self.rounds.len()
29    }
30}
31
32pub struct Supervisor {
33    pub workers: Vec<AgentSpec>,
34    pub supervisor: AgentSpec,
35    pub max_rounds: u32,
36}
37
38impl Supervisor {
39    pub fn new(workers: Vec<AgentSpec>, supervisor: AgentSpec) -> Self {
40        Self {
41            workers,
42            supervisor,
43            max_rounds: 3,
44        }
45    }
46
47    pub fn with_max_rounds(mut self, max_rounds: u32) -> Self {
48        self.max_rounds = max_rounds;
49        self
50    }
51
52    #[instrument(name = "multi.supervisor", skip_all)]
53    pub async fn run(
54        &self,
55        task: &str,
56        runner: &Arc<dyn AgentRunner>,
57        infra: &SharedInfra,
58    ) -> Result<SupervisorResult, MultiError> {
59        let mut result = SupervisorResult {
60            task: task.to_string(),
61            rounds: Vec::new(),
62            supervisor_feedback: Vec::new(),
63            final_answer: String::new(),
64            approved: false,
65        };
66
67        let mut current_task = task.to_string();
68
69        for round_num in 0..self.max_rounds {
70            // Workers execute in parallel
71            let swarm = Swarm::new(self.workers.clone(), SwarmMode::Parallel);
72            let swarm_result = swarm.run(&current_task, runner, infra).await?;
73            result.rounds.push(swarm_result.outputs.clone());
74
75            // Supervisor reviews
76            let worker_summaries: Vec<String> = swarm_result
77                .outputs
78                .iter()
79                .filter(|o| o.succeeded())
80                .map(|o| format!("- {}: {}", o.name, truncate(&o.answer, 300)))
81                .collect();
82
83            let review_task = format!(
84                "Original task: {}\n\nRound {} results:\n{}\n\n\
85                 Review these results. If they are satisfactory, respond with \
86                 APPROVED followed by a final summary. Otherwise, provide specific \
87                 feedback for improvement.",
88                task,
89                round_num + 1,
90                worker_summaries.join("\n")
91            );
92
93            let mailbox = Mailbox::default();
94            let rt = infra.make_runtime();
95            let feedback_output = runner.run(&self.supervisor, &review_task, &rt, &mailbox).await?;
96            let feedback = feedback_output.answer;
97            result.supervisor_feedback.push(feedback.clone());
98
99            if feedback.to_uppercase().contains("APPROVED") {
100                // Extract answer after APPROVED marker
101                let answer = strip_approved_prefix(&feedback);
102                result.final_answer = answer;
103                result.approved = true;
104                return Ok(result);
105            }
106
107            // Feed supervisor's feedback back to workers
108            current_task = format!(
109                "{}\n\nSupervisor feedback from round {}:\n{}",
110                task,
111                round_num + 1,
112                feedback
113            );
114        }
115
116        // Max rounds reached
117        result.final_answer = format!(
118            "[max supervision rounds reached] {}",
119            result.supervisor_feedback.last().unwrap_or(&String::new())
120        );
121        Ok(result)
122    }
123}
124
125fn strip_approved_prefix(s: &str) -> String {
126    let upper = s.to_uppercase();
127    for prefix in &["APPROVED:", "APPROVED.", "APPROVED\n", "APPROVED "] {
128        if upper.starts_with(prefix) {
129            return s[prefix.len()..].trim().to_string();
130        }
131    }
132    s.to_string()
133}
134
135fn truncate(s: &str, max_len: usize) -> &str {
136    if s.len() <= max_len {
137        return s;
138    }
139    let mut end = max_len;
140    while end > 0 && !s.is_char_boundary(end) {
141        end -= 1;
142    }
143    &s[..end]
144}
145
146#[cfg(test)]
147mod tests {
148    use super::*;
149    use crate::types::{AgentOutput, AgentSpec};
150    use car_engine::Runtime;
151    use std::sync::atomic::{AtomicU32, Ordering};
152
153    struct ApprovingRunner {
154        call_count: AtomicU32,
155    }
156
157    #[async_trait::async_trait]
158    impl AgentRunner for ApprovingRunner {
159        async fn run(
160            &self,
161            spec: &AgentSpec,
162            _task: &str,
163            _runtime: &Runtime,
164            _mailbox: &Mailbox,
165        ) -> Result<AgentOutput, MultiError> {
166            let _n = self.call_count.fetch_add(1, Ordering::SeqCst);
167            // Supervisor (runs after workers) approves on first review
168            let answer = if spec.name == "supervisor" {
169                "APPROVED: Everything looks good.".to_string()
170            } else {
171                format!("work from {}", spec.name)
172            };
173            Ok(AgentOutput {
174                name: spec.name.clone(),
175                answer,
176                turns: 1,
177                tool_calls: 0,
178                duration_ms: 5.0,
179                error: None,
180                outcome: None,
181                tokens: None,
182            })
183        }
184    }
185
186    #[tokio::test]
187    async fn test_supervisor_approves_round_1() {
188        let workers = vec![
189            AgentSpec::new("coder", "Write code"),
190            AgentSpec::new("tester", "Write tests"),
191        ];
192        let supervisor_spec = AgentSpec::new("supervisor", "Review and coordinate");
193
194        let runner: Arc<dyn AgentRunner> = Arc::new(ApprovingRunner {
195            call_count: AtomicU32::new(0),
196        });
197        let infra = SharedInfra::new();
198
199        let result = Supervisor::new(workers, supervisor_spec)
200            .run("build fibonacci", &runner, &infra)
201            .await
202            .unwrap();
203
204        assert!(result.approved);
205        assert_eq!(result.total_rounds(), 1);
206        assert_eq!(result.final_answer, "Everything looks good.");
207    }
208}