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
use harn_vm::agent_events::WorkerEvent;
use harn_vm::llm::{take_agent_trace, AgentTraceEvent};
use serde_json::json;
use super::state::Debugger;
use crate::protocol::*;
impl Debugger {
/// End any in-flight progress event. Called whenever the VM stops
/// (breakpoint, pause, terminate, error) so the IDE clears its
/// "Running..." indicator.
pub(crate) fn end_progress(&mut self, responses: &mut Vec<DapResponse>) {
if let Some(id) = self.active_progress_id.take() {
let seq = self.next_seq();
responses.push(DapResponse::event(
seq,
"progressEnd",
Some(json!({ "progressId": id })),
));
}
self.steps_since_progress_update = 0;
}
/// Emit a progressUpdate roughly every 256 steps so the IDE sees
/// liveness ticks during long runs and can extend its own timeouts.
/// Cheap when there's no active progress (early return).
pub(crate) fn maybe_progress_update(&mut self, responses: &mut Vec<DapResponse>) {
if self.active_progress_id.is_none() {
return;
}
self.steps_since_progress_update = self.steps_since_progress_update.wrapping_add(1);
if self.steps_since_progress_update & 0xFF != 0 {
return;
}
let id = self.active_progress_id.clone().unwrap();
let line = self.current_line;
let seq = self.next_seq();
responses.push(DapResponse::event(
seq,
"progressUpdate",
Some(json!({
"progressId": id,
"message": format!("line {}", line),
})),
));
}
/// Drain agent trace events the VM has accumulated and serialize the
/// LLM-call entries as DAP `output` events with `category: "telemetry"`.
/// Other event kinds (tool execution, phase change, etc.) are skipped
/// for now -- the IDE consumes only LLM telemetry. Keeping this here
/// rather than in harn-vm preserves the rule that DAP wire-format
/// concerns belong in harn-dap.
pub(crate) fn drain_telemetry_events(&mut self) -> Vec<DapResponse> {
let events = take_agent_trace();
if events.is_empty() {
return Vec::new();
}
let mut out = Vec::new();
for event in events {
if let AgentTraceEvent::LlmCall {
call_id,
model,
input_tokens,
output_tokens,
cache_tokens,
duration_ms,
iteration,
} = event
{
let payload = json!({
"call_id": call_id,
"model": model,
"prompt_tokens": input_tokens,
"completion_tokens": output_tokens,
"cache_tokens": cache_tokens,
"total_ms": duration_ms,
"iteration": iteration,
});
let body_str = serde_json::to_string(&payload).unwrap_or_default();
let seq = self.next_seq();
out.push(DapResponse::event(
seq,
"output",
Some(json!({
"category": "telemetry",
"output": body_str,
})),
));
}
}
out
}
/// Issue #1868 — drain queued subagent observations and translate
/// them into DAP `thread`/`stopped`/`continued` events. Called
/// between VM steps from `step_running_vm` so subagent lifecycle
/// surfaces on the IDE in the same frame the worker emitted it.
///
/// Event mapping (see also the table in `subagents.rs`):
/// - `WorkerSpawned` → `thread { reason: "started" }`
/// - `WorkerSuspended` → `stopped { reason: "suspend",
/// description: <reason>, threadId: <subagent>,
/// allThreadsStopped: false }`
/// - `WorkerWaitingForInput` → `stopped { reason: "pause", ... }`
/// - `WorkerResumed` → `continued { allThreadsContinued: false }`
/// - `WorkerCompleted` / `WorkerCancelled` → `thread { reason: "exited" }`
/// - `WorkerFailed` → `stopped { reason: "exception" }` then
/// `thread { reason: "exited" }`
/// - `WorkerProgressed` → no DAP emission (too noisy)
///
/// When the `break-on-resume` exception filter is active, a
/// `WorkerResumed` additionally emits a `stopped { reason:
/// "pause", threadId: main }` so the user can step through the
/// resume continuation on the main thread.
pub(crate) fn drain_subagent_events(&mut self) -> Vec<DapResponse> {
let observations = self.subagent_tracker.drain();
if observations.is_empty() {
return Vec::new();
}
let mut out = Vec::new();
for obs in observations {
// Upsert the thread first so every emitted event references
// a stable `threadId`. Idempotent for the second+ event on
// the same worker.
let record = self.subagent_tracker.upsert_thread(
&obs.worker_id,
&obs.worker_name,
obs.parent_worker_id.as_deref(),
&obs.status,
);
let thread_id = record.thread_id as i64;
// Spawn → emit `thread started` once. Subsequent events for
// the same worker still upsert (above) but don't re-emit
// the start.
match obs.event {
WorkerEvent::WorkerSpawned => {
let seq = self.next_seq();
out.push(DapResponse::event(
seq,
"thread",
Some(json!({
"reason": "started",
"threadId": thread_id,
})),
));
}
WorkerEvent::WorkerProgressed => {
// Intentionally silent — progress milestones map
// poorly to DAP runtime states and would flood the
// IDE with no-op stopped/continued churn.
}
WorkerEvent::WorkerWaitingForInput => {
self.subagent_tracker
.mark_suspended(&obs.worker_id, Some("awaiting input"));
let seq = self.next_seq();
out.push(DapResponse::event(
seq,
"stopped",
Some(json!({
"reason": "pause",
"description": "awaiting input",
"threadId": thread_id,
"allThreadsStopped": false,
"preserveFocusHint": true,
})),
));
}
WorkerEvent::WorkerSuspended => {
self.subagent_tracker
.mark_suspended(&obs.worker_id, obs.suspend_reason.as_deref());
let description = obs
.suspend_reason
.clone()
.unwrap_or_else(|| "suspended".to_string());
let seq = self.next_seq();
out.push(DapResponse::event(
seq,
"stopped",
Some(json!({
"reason": "suspend",
"description": description,
"threadId": thread_id,
"allThreadsStopped": false,
"preserveFocusHint": !self.break_on_subagent_suspend,
})),
));
}
WorkerEvent::WorkerResumed => {
self.subagent_tracker.mark_resumed(&obs.worker_id);
let seq = self.next_seq();
out.push(DapResponse::event(
seq,
"continued",
Some(json!({
"threadId": thread_id,
"allThreadsContinued": false,
})),
));
if self.break_on_subagent_resume {
// Force a stop on the main debugger thread so
// the user can step through the resume
// continuation. The next step_running_vm tick
// honours pending_pause and emits a proper
// `stopped(pause)` event for the main thread.
self.pending_pause = true;
}
}
WorkerEvent::WorkerCompleted | WorkerEvent::WorkerCancelled => {
self.subagent_tracker.mark_exited(&obs.worker_id);
let seq = self.next_seq();
out.push(DapResponse::event(
seq,
"thread",
Some(json!({
"reason": "exited",
"threadId": thread_id,
})),
));
}
WorkerEvent::WorkerFailed => {
self.subagent_tracker.mark_exited(&obs.worker_id);
let description = obs
.suspend_reason
.clone()
.unwrap_or_else(|| "worker failed".to_string());
let seq = self.next_seq();
out.push(DapResponse::event(
seq,
"stopped",
Some(json!({
"reason": "exception",
"description": description,
"threadId": thread_id,
"allThreadsStopped": false,
"preserveFocusHint": true,
})),
));
let seq = self.next_seq();
out.push(DapResponse::event(
seq,
"thread",
Some(json!({
"reason": "exited",
"threadId": thread_id,
})),
));
}
}
}
out
}
/// Drain any new VM stdout into `output` DAP events. Used by the
/// stop/terminate paths so the IDE doesn't lose trailing __io_print()s.
pub(crate) fn flush_output_into(&mut self, responses: &mut Vec<DapResponse>) {
let output = self.vm.as_ref().unwrap().output().to_string();
if !output.is_empty() && output != self.output {
let new_output = output[self.output.len()..].to_string();
if !new_output.is_empty() {
let seq = self.next_seq();
responses.push(DapResponse::event(
seq,
"output",
Some(json!({
"category": "stdout",
"output": new_output,
})),
));
}
self.output = output;
}
}
}