Execute a task in the background. Returns immediately with the task ID; the same id is used in the emitted `SubagentStart`/`SubagentEnd` events so callers can correlate. Pre-emits `SubagentStart` synchronously when an event channel is available so a caller that queries the subagent task tracker right after this call observes the task in `Running` state without a race window.
(
self: Arc<Self>,
params: TaskParams,
event_tx: Option<broadcast::Sender<AgentEvent>>,
parent_session_id: Option<String>,
)
| 457 | /// caller that queries the subagent task tracker right after this call |
| 458 | /// observes the task in `Running` state without a race window. |
| 459 | pub fn execute_background( |
| 460 | self: Arc<Self>, |
| 461 | params: TaskParams, |
| 462 | event_tx: Option<broadcast::Sender<AgentEvent>>, |
| 463 | parent_session_id: Option<String>, |
| 464 | ) -> String { |
| 465 | let task_id = format!("task-{}", uuid::Uuid::new_v4()); |
| 466 | let session_id = format!("task-run-{}", task_id); |
| 467 | |
| 468 | if let Some(ref tx) = event_tx { |
| 469 | let _ = tx.send(AgentEvent::SubagentStart { |
| 470 | task_id: task_id.clone(), |
| 471 | session_id, |
| 472 | parent_session_id: parent_session_id.clone().unwrap_or_default(), |
| 473 | agent: params.agent.clone(), |
| 474 | description: params.description.clone(), |
| 475 | }); |
| 476 | } |
| 477 | |
| 478 | let task_id_for_spawn = task_id.clone(); |
| 479 | let task_id_for_log = task_id.clone(); |
| 480 | tokio::spawn(async move { |
| 481 | if let Err(e) = self |
| 482 | .execute_with_task_id( |
| 483 | task_id_for_spawn, |
| 484 | params, |
| 485 | event_tx, |
| 486 | parent_session_id.as_deref(), |
| 487 | false, |
| 488 | ) |
| 489 | .await |
| 490 | { |
| 491 | tracing::error!("Background task {} failed: {}", task_id_for_log, e); |
| 492 | } |
| 493 | }); |
| 494 | |
| 495 | task_id |
| 496 | } |
| 497 | |
| 498 | /// Execute multiple tasks in parallel. |
| 499 | /// |
no test coverage detected