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
//! Subagent: an isolated agent loop spawned from a parent.
//!
//! Per DESIGN.md §8, a subagent has:
//! - independent `Context`
//! - restricted tool / sensor / guide set
//! - its own iteration budget
//!
//! A subagent reports back via [`SubagentStatus`] (the Superpowers
//! convention: Done / DoneWithConcerns / Blocked / NeedsContext).
use crate::{AgentLoop, Outcome};
use harness_core::{Guide, HarnessError, Model, Sensor, SubagentStatus, Task, Tool, World};
use std::sync::Arc;
/// What a subagent needs to run.
pub struct SubagentSpec {
pub name: String,
pub task: Task,
pub tools: Vec<Arc<dyn Tool>>,
pub guides: Vec<Arc<dyn Guide>>,
pub sensors: Vec<Arc<dyn Sensor>>,
pub max_iters: u32,
}
impl SubagentSpec {
pub fn new(name: impl Into<String>, task: Task) -> Self {
Self {
name: name.into(),
task,
tools: Vec::new(),
guides: Vec::new(),
sensors: Vec::new(),
max_iters: 12,
}
}
pub fn with_tool(mut self, t: Arc<dyn Tool>) -> Self {
self.tools.push(t);
self
}
pub fn with_guide(mut self, g: Arc<dyn Guide>) -> Self {
self.guides.push(g);
self
}
pub fn with_sensor(mut self, s: Arc<dyn Sensor>) -> Self {
self.sensors.push(s);
self
}
pub fn with_max_iters(mut self, n: u32) -> Self {
self.max_iters = n;
self
}
}
/// A subagent's structured report back to the parent.
#[derive(Debug, Clone)]
pub struct SubagentReport {
pub name: String,
pub status: SubagentStatus,
pub text: Option<String>,
pub iters: u32,
/// Token usage accumulated by the subagent's loop. Lets callers
/// (e.g. `harness-loop-engine`'s `TokenBudget`) account for spend
/// across maker/checker turns.
pub usage: harness_core::Usage,
}
/// Bind a `Model` to a `SubagentSpec` and run it.
pub struct Subagent<M: Model> {
pub spec: SubagentSpec,
pub loop_: AgentLoop<M>,
}
impl<M: Model> Subagent<M> {
pub fn new(model: M, spec: SubagentSpec) -> Self {
let mut loop_ = AgentLoop::new(model);
for t in &spec.tools {
loop_ = loop_.with_tool(t.clone());
}
for g in &spec.guides {
loop_ = loop_.with_guide(g.clone());
}
for s in &spec.sensors {
loop_ = loop_.with_sensor(s.clone());
}
Self { spec, loop_ }
}
pub async fn run(self, world: &mut World) -> Result<SubagentReport, HarnessError> {
let name = self.spec.name.clone();
let max = self.spec.max_iters;
let task = self.spec.task.clone();
let outcome = self.loop_.run_with_max_iters(task, world, max).await?;
let report = match outcome {
Outcome::Done {
text, iters, usage, ..
} => SubagentReport {
name,
status: SubagentStatus::Done,
text,
iters,
usage,
},
// Running out of iterations is not the same as having nothing to
// show. Measured on a real run: a code-audit subagent produced a
// complete, correct answer on its last iteration, was reported
// `Blocked`, and the orchestrator dead-lettered it — filing the
// finished work as an error message and cancelling everything
// downstream. Work that exists is reported as work, with the caveat
// that it was cut short; only an empty hand is `Blocked`.
// A cancel is reported like an exhausted budget: the work done so far still counts.
Outcome::BudgetExhausted {
iters,
last_text,
usage,
..
}
| Outcome::Cancelled {
iters,
last_text,
usage,
..
} => {
let empty_handed = last_text.as_deref().map(str::trim).unwrap_or("").is_empty();
SubagentReport {
name,
status: if empty_handed {
SubagentStatus::Blocked
} else {
SubagentStatus::DoneWithConcerns
},
text: last_text,
iters,
usage,
}
}
// Stuck is different: the loop caught it repeating itself, so
// whatever text is there is the thing it kept saying, not an answer.
Outcome::Stuck {
iters,
last_text,
usage,
..
} => SubagentReport {
name,
status: SubagentStatus::Blocked,
text: last_text,
iters,
usage,
},
};
tracing::info!(
subagent = %report.name,
status = ?report.status,
iters = report.iters,
"subagent completed"
);
Ok(report)
}
}