Skip to main content

claude_code_agent/
background.rs

1//! Background task tree tracking.
2//!
3//! background_tasks_changed is a level signal (replace semantics).
4//! Feed it `AgentEvent::BackgroundTasks`; background a running task with
5//! `session.control(&BackgroundTasksRequest::one(tool_use_id))`.
6//! Source: binary background_tasks_changed + background_tasks schemas
7use crate::protocol::BackgroundTask;
8
9/// Tracks the current agent task tree from background_tasks_changed events.
10#[derive(Default)]
11pub struct TaskTree {
12    tasks: Vec<BackgroundTask>,
13}
14
15impl TaskTree {
16    pub fn new() -> Self { Self::default() }
17
18    /// Update with a new snapshot (replace semantics).
19    pub fn update(&mut self, tasks: Vec<BackgroundTask>) {
20        self.tasks = tasks;
21    }
22
23    pub fn tasks(&self) -> &[BackgroundTask] { &self.tasks }
24
25    /// Find a task by ID.
26    pub fn get(&self, id: &str) -> Option<&BackgroundTask> {
27        self.tasks.iter().find(|t| t.task_id == id)
28    }
29
30    /// All direct children of a parent task.
31    pub fn children(&self, parent_id: &str) -> Vec<&BackgroundTask> {
32        self.tasks.iter()
33            .filter(|t| t.parent_task_id.as_deref() == Some(parent_id))
34            .collect()
35    }
36}