synaps_cli/tools/subagent/
resume.rs1use serde_json::{json, Value};
8use std::sync::atomic::Ordering;
9use std::sync::{Arc, RwLock};
10use std::time::Duration;
11use tokio::sync::{mpsc, oneshot};
12
13use crate::{Result, RuntimeError, LlmEvent, SessionEvent, AgentEvent};
14use super::super::{Tool, ToolContext, NEXT_SUBAGENT_ID};
15use crate::runtime::subagent::{SubagentHandle, SubagentResult, SubagentStatus, SubagentState};
16
17pub struct SubagentResumeTool;
18
19#[async_trait::async_trait]
20impl Tool for SubagentResumeTool {
21 fn name(&self) -> &str { "subagent_resume" }
22
23 fn description(&self) -> &str {
24 "Resume a finished or timed-out reactive subagent with new instructions. \
25 The previous subagent's conversation state is prepended as context so the \
26 new run has full history. Returns a new handle_id — the original handle \
27 remains readable for comparison. Only works on subagents in \
28 finished/timed_out/failed state."
29 }
30
31 fn parameters(&self) -> Value {
32 json!({
33 "type": "object",
34 "properties": {
35 "handle_id": {
36 "type": "string",
37 "description": "Handle ID of the completed subagent to resume (e.g. \"sa_3\")."
38 },
39 "instructions": {
40 "type": "string",
41 "description": "New task or context to prepend to the resumed subagent. \
42 Injected before the prior conversation history."
43 }
44 },
45 "required": ["handle_id", "instructions"]
46 })
47 }
48
49 async fn execute(&self, params: Value, ctx: ToolContext) -> Result<String> {
50 let prior_handle_id = params["handle_id"].as_str()
51 .ok_or_else(|| RuntimeError::Tool("Missing 'handle_id' parameter".to_string()))?
52 .to_string();
53
54 let instructions = params["instructions"].as_str()
55 .ok_or_else(|| RuntimeError::Tool("Missing 'instructions' parameter".to_string()))?
56 .to_string();
57
58 let registry = ctx.capabilities.subagent_registry.as_ref()
59 .ok_or_else(|| RuntimeError::Tool(
60 "SubagentRegistry not available on this ToolContext".to_string()
61 ))?;
62
63 let (agent_name, model, prior_context, prior_system_prompt, prior_timeout) = {
65 let reg = registry.lock().unwrap();
66 let handle = reg.get(&prior_handle_id)
67 .ok_or_else(|| RuntimeError::Tool(
68 format!("No subagent found with handle_id '{}'", prior_handle_id)
69 ))?;
70
71 if handle.status() == SubagentStatus::Running {
72 return Err(RuntimeError::Tool(format!(
73 "Subagent '{}' is still running. Call subagent_collect first, \
74 or wait until it finishes.",
75 prior_handle_id
76 )));
77 }
78
79 let prior = {
80 let state = handle.conversation_state();
81 if state.is_empty() {
82 handle.partial_output()
83 } else {
84 serde_json::to_string(&state).unwrap_or_else(|_| handle.partial_output())
85 }
86 };
87
88 (handle.agent_name.clone(), handle.model.clone(), prior, handle.system_prompt.clone(), handle.timeout_secs)
89 };
90
91 let resumed_task = format!(
93 "{instructions}\n\n\
94 ---\n\
95 [Prior conversation context from handle {prior_handle_id}]\n\
96 {prior_context}"
97 );
98
99 let system_prompt = prior_system_prompt;
101 let timeout_secs = prior_timeout;
102 let label = agent_name.clone();
103 let task_preview: String = resumed_task.chars().take(80).collect();
104 let task_full = resumed_task.clone();
105 let subagent_id = NEXT_SUBAGENT_ID.fetch_add(1, Ordering::Relaxed);
106 let handle_id = format!("sa_{}", subagent_id);
107
108 tracing::info!(
109 "subagent_resume: dispatching '{}' (id={}, resumed_from={}) model={}",
110 label, handle_id, prior_handle_id, model
111 );
112
113 let state = Arc::new(RwLock::new(SubagentState::new()));
114
115 let (steer_tx, steer_rx) = mpsc::unbounded_channel::<String>();
116 let (shutdown_tx, shutdown_rx) = oneshot::channel::<()>();
117 let (result_tx, result_rx) = oneshot::channel::<SubagentResult>();
118
119 if let Some(ref tx) = ctx.channels.tx_events {
120 let _ = tx.send(crate::StreamEvent::Agent(AgentEvent::SubagentStart {
121 subagent_id,
122 agent_name: label.clone(),
123 task_preview: task_preview.clone(),
124 }));
125 }
126
127 let state_t = Arc::clone(&state);
128 let task_full_a = task_full.clone();
129 let label_inner = label.clone();
130 let model_inner = model.clone();
131 let tx_events_inner = ctx.channels.tx_events.clone();
132 let start_time = std::time::Instant::now();
133
134 let system_prompt_for_handle = system_prompt.clone();
135 let thread_handle = std::thread::spawn(move || {
136 let panic_result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
137 let rt = match tokio::runtime::Builder::new_current_thread()
138 .enable_all()
139 .build()
140 {
141 Ok(rt) => rt,
142 Err(e) => {
143 state_t.write().unwrap().status =
144 SubagentStatus::Failed(format!("tokio runtime: {}", e));
145 return;
146 }
147 };
148
149 let state_a = Arc::clone(&state_t);
150 let label_a = label_inner.clone();
151 let model_a = model_inner.clone();
152 let tx_events_a = tx_events_inner.clone();
153 let task_for_timeout = task_full_a.clone();
154 let task_for_complete = task_full_a.clone();
155 let task_for_stream = task_full_a;
156
157 let outcome: std::result::Result<SubagentResult, String> = rt.block_on(async move {
158 use futures::StreamExt;
159
160 let mut runtime = match crate::Runtime::new().await {
161 Ok(r) => r,
162 Err(e) => return Err(format!("Failed to create subagent runtime: {}", e)),
163 };
164
165 runtime.set_system_prompt(system_prompt);
166 runtime.set_model(model_a.clone());
167 runtime.set_tools(crate::ToolRegistry::without_subagent());
168
169 let cancel = crate::CancellationToken::new();
170 let cancel_inner = cancel.clone();
171 tokio::spawn(async move {
172 let _ = shutdown_rx.await;
173 cancel_inner.cancel();
174 });
175
176 let mut stream = runtime.run_stream_with_messages(
177 vec![serde_json::json!({"role": "user", "content": task_for_stream})],
178 cancel,
179 Some(steer_rx),
180 None,
181 false,
182 ).await;
183
184 let mut tool_count = 0u32;
185 let mut total_input_tokens = 0u64;
186 let mut total_output_tokens = 0u64;
187 let mut total_cache_read = 0u64;
188 let mut total_cache_creation = 0u64;
189
190 let timeout_fut = tokio::time::sleep(Duration::from_secs(timeout_secs));
191 tokio::pin!(timeout_fut);
192
193 loop {
194 tokio::select! {
195 event = stream.next() => {
196 let Some(event) = event else { break };
197 match event {
198 crate::StreamEvent::Llm(LlmEvent::Thinking(_)) => {
199 if let Some(ref tx) = tx_events_a {
200 let _ = tx.send(crate::StreamEvent::Agent(AgentEvent::SubagentUpdate {
201 subagent_id,
202 agent_name: label_a.clone(),
203 status: "💭 thinking...".to_string(),
204 }));
205 }
206 }
207 crate::StreamEvent::Llm(LlmEvent::Text(text)) => {
208 state_a.write().unwrap().partial_text.push_str(&text);
209 }
210 crate::StreamEvent::Llm(LlmEvent::ToolUseStart { tool_name: name, .. }) => {
211 tool_count += 1;
212 if let Some(ref tx) = tx_events_a {
213 let _ = tx.send(crate::StreamEvent::Agent(AgentEvent::SubagentUpdate {
214 subagent_id,
215 agent_name: label_a.clone(),
216 status: format!("⚙ {} (tool #{})", name, tool_count),
217 }));
218 }
219 }
220 crate::StreamEvent::Llm(LlmEvent::ToolUse { tool_name, input, .. }) => {
221 let input_str = input.to_string();
222 let input_preview: String = input_str.chars().take(200).collect();
223 state_a.write().unwrap().tool_log
224 .push(format!("[tool_use]: {} — {}", tool_name, input_preview));
225 let detail = match tool_name.as_str() {
226 "bash" => {
227 let cmd = input["command"].as_str().unwrap_or("");
228 let preview: String = cmd.chars().take(60).collect();
229 format!("$ {}", preview)
230 }
231 "read" => format!("reading {}", input["path"].as_str().unwrap_or("?").rsplit('/').next().unwrap_or("?")),
232 "write" => format!("writing {}", input["path"].as_str().unwrap_or("?").rsplit('/').next().unwrap_or("?")),
233 "edit" => format!("editing {}", input["path"].as_str().unwrap_or("?").rsplit('/').next().unwrap_or("?")),
234 "grep" => format!("grep /{}/", input["pattern"].as_str().unwrap_or("?").chars().take(30).collect::<String>()),
235 "find" => format!("find {}", input["pattern"].as_str().unwrap_or("?")),
236 "ls" => format!("ls {}", input["path"].as_str().unwrap_or(".").rsplit('/').next().unwrap_or(".")),
237 other => {
238 if other.starts_with("ext__") {
239 other.splitn(3, "__").last().unwrap_or(other).to_string()
240 } else {
241 other.to_string()
242 }
243 }
244 };
245 if let Some(ref tx) = tx_events_a {
246 let _ = tx.send(crate::StreamEvent::Agent(AgentEvent::SubagentUpdate {
247 subagent_id,
248 agent_name: label_a.clone(),
249 status: detail,
250 }));
251 }
252 }
253 crate::StreamEvent::Llm(LlmEvent::ToolResult { result, .. }) => {
254 let preview: String = result.chars().take(300).collect();
255 state_a.write().unwrap().tool_log
256 .push(format!("[tool_result]: {}", preview));
257 }
258 crate::StreamEvent::Session(SessionEvent::Usage {
259 input_tokens, output_tokens,
260 cache_read_input_tokens, cache_creation_input_tokens,
261 model: _,
262 }) => {
263 total_input_tokens += input_tokens;
264 total_output_tokens += output_tokens;
265 total_cache_read += cache_read_input_tokens;
266 total_cache_creation += cache_creation_input_tokens;
267 }
268 crate::StreamEvent::Session(SessionEvent::Error(e)) => return Err(e),
269 crate::StreamEvent::Session(SessionEvent::Done) => break,
270 _ => {}
271 }
272 }
273 _ = &mut timeout_fut => {
274 let (partial, log) = {
275 let mut s = state_a.write().unwrap();
276 s.status = SubagentStatus::TimedOut;
277 s.conversation_state = vec![
278 serde_json::json!({"role": "user", "content": task_for_timeout.clone()}),
279 serde_json::json!({"role": "assistant", "content": &s.partial_text}),
280 ];
281 (s.partial_text.clone(), s.tool_log.clone())
282 };
283 let mut text = format!("[TIMED OUT after {}s — partial results below]\n\n", timeout_secs);
284 if !log.is_empty() {
285 text.push_str(&log.join("\n"));
286 text.push('\n');
287 }
288 if !partial.is_empty() {
289 text.push_str("\n[partial response]:\n");
290 text.push_str(&partial);
291 }
292 return Ok(SubagentResult {
293 text,
294 model: model_a.clone(),
295 input_tokens: total_input_tokens,
296 output_tokens: total_output_tokens,
297 cache_read: total_cache_read,
298 cache_creation: total_cache_creation,
299 tool_count,
300 });
301 }
302 }
303 }
304
305 Ok(SubagentResult {
306 text: state_a.write().unwrap().partial_text.clone(),
307 model: model_a.clone(),
308 input_tokens: total_input_tokens,
309 output_tokens: total_output_tokens,
310 cache_read: total_cache_read,
311 cache_creation: total_cache_creation,
312 tool_count,
313 })
314 });
315
316 match outcome {
317 Ok(sa_result) => {
318 {
319 let mut s = state_t.write().unwrap();
320 if matches!(s.status, SubagentStatus::Running) {
321 s.status = SubagentStatus::Completed;
322 s.conversation_state = vec![
323 serde_json::json!({"role": "user", "content": task_for_complete.clone()}),
324 serde_json::json!({"role": "assistant", "content": sa_result.text.clone()}),
325 ];
326 }
327 }
328 let elapsed = start_time.elapsed().as_secs_f64();
329 let preview: String = sa_result.text.chars().take(120).collect();
330 if let Some(ref tx) = tx_events_inner {
331 let _ = tx.send(crate::StreamEvent::Agent(AgentEvent::SubagentDone {
332 subagent_id,
333 agent_name: label_inner.clone(),
334 result_preview: preview,
335 duration_secs: elapsed,
336 }));
337 }
338 let _ = result_tx.send(sa_result);
339 }
340 Err(e) => {
341 state_t.write().unwrap().status = SubagentStatus::Failed(e.clone());
342 let elapsed = start_time.elapsed().as_secs_f64();
343 if let Some(ref tx) = tx_events_inner {
344 let _ = tx.send(crate::StreamEvent::Agent(AgentEvent::SubagentDone {
345 subagent_id,
346 agent_name: label_inner.clone(),
347 result_preview: format!("ERROR: {}", e),
348 duration_secs: elapsed,
349 }));
350 }
351 }
352 }
353 }));
354
355 if let Err(panic_info) = panic_result {
356 let msg = if let Some(s) = panic_info.downcast_ref::<&str>() {
357 s.to_string()
358 } else if let Some(s) = panic_info.downcast_ref::<String>() {
359 s.clone()
360 } else {
361 "unknown panic".to_string()
362 };
363 tracing::error!("Resumed subagent thread panicked: {}", msg);
364 state_t.write().unwrap().status = SubagentStatus::Failed(format!("panic: {}", msg));
365 }
366 });
367
368 let handle = SubagentHandle::new(
369 handle_id.clone(),
370 label.clone(),
371 task_preview,
372 model,
373 system_prompt_for_handle,
374 timeout_secs,
375 state,
376 Some(steer_tx),
377 Some(shutdown_tx),
378 Some(result_rx),
379 );
380
381 {
382 let mut reg = registry.lock().unwrap();
383 reg.register(handle);
384 if let Some(h) = reg.get_mut(&handle_id) {
385 h.set_thread_handle(thread_handle);
386 }
387 }
388
389 Ok(json!({
390 "handle_id": handle_id,
391 "resumed_from": prior_handle_id,
392 "agent_name": label,
393 "status": "running"
394 }).to_string())
395 }
396}