1use crate::{AgentLoop, Outcome};
12use harness_core::{Guide, HarnessError, Model, Sensor, SubagentStatus, Task, Tool, World};
13use std::sync::Arc;
14
15pub struct SubagentSpec {
17 pub name: String,
18 pub task: Task,
19 pub tools: Vec<Arc<dyn Tool>>,
20 pub guides: Vec<Arc<dyn Guide>>,
21 pub sensors: Vec<Arc<dyn Sensor>>,
22 pub max_iters: u32,
23}
24
25impl SubagentSpec {
26 pub fn new(name: impl Into<String>, task: Task) -> Self {
27 Self {
28 name: name.into(),
29 task,
30 tools: Vec::new(),
31 guides: Vec::new(),
32 sensors: Vec::new(),
33 max_iters: 12,
34 }
35 }
36
37 pub fn with_tool(mut self, t: Arc<dyn Tool>) -> Self {
38 self.tools.push(t);
39 self
40 }
41
42 pub fn with_guide(mut self, g: Arc<dyn Guide>) -> Self {
43 self.guides.push(g);
44 self
45 }
46
47 pub fn with_sensor(mut self, s: Arc<dyn Sensor>) -> Self {
48 self.sensors.push(s);
49 self
50 }
51
52 pub fn with_max_iters(mut self, n: u32) -> Self {
53 self.max_iters = n;
54 self
55 }
56}
57
58#[derive(Debug, Clone)]
60pub struct SubagentReport {
61 pub name: String,
62 pub status: SubagentStatus,
63 pub text: Option<String>,
64 pub iters: u32,
65}
66
67pub struct Subagent<M: Model> {
69 pub spec: SubagentSpec,
70 pub loop_: AgentLoop<M>,
71}
72
73impl<M: Model> Subagent<M> {
74 pub fn new(model: M, spec: SubagentSpec) -> Self {
75 let mut loop_ = AgentLoop::new(model);
76 for t in &spec.tools {
77 loop_ = loop_.with_tool(t.clone());
78 }
79 for g in &spec.guides {
80 loop_ = loop_.with_guide(g.clone());
81 }
82 for s in &spec.sensors {
83 loop_ = loop_.with_sensor(s.clone());
84 }
85 Self { spec, loop_ }
86 }
87
88 pub async fn run(self, world: &mut World) -> Result<SubagentReport, HarnessError> {
89 let name = self.spec.name.clone();
90 let max = self.spec.max_iters;
91 let task = self.spec.task.clone();
92 let outcome = self.loop_.run_with_max_iters(task, world, max).await?;
93 let report = match outcome {
94 Outcome::Done { text, iters, .. } => SubagentReport {
95 name,
96 status: SubagentStatus::Done,
97 text,
98 iters,
99 },
100 Outcome::BudgetExhausted { iters, .. } => SubagentReport {
101 name,
102 status: SubagentStatus::Blocked,
103 text: None,
104 iters,
105 },
106 };
107 tracing::info!(
108 subagent = %report.name,
109 status = ?report.status,
110 iters = report.iters,
111 "subagent completed"
112 );
113 Ok(report)
114 }
115}