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    /// An empty tree.
17    #[must_use]
18    pub fn new() -> Self { Self::default() }
19
20    /// Update with a new snapshot (replace semantics).
21    pub fn update(&mut self, tasks: Vec<BackgroundTask>) {
22        self.tasks = tasks;
23    }
24
25    /// The latest snapshot.
26    #[must_use]
27    pub fn tasks(&self) -> &[BackgroundTask] { &self.tasks }
28
29    /// Find a task by ID.
30    #[must_use]
31    pub fn get(&self, id: &str) -> Option<&BackgroundTask> {
32        self.tasks.iter().find(|t| t.task_id == id)
33    }
34
35    /// All direct children of a parent task.
36    #[must_use]
37    pub fn children(&self, parent_id: &str) -> Vec<&BackgroundTask> {
38        self.tasks.iter()
39            .filter(|t| t.parent_task_id.as_deref() == Some(parent_id))
40            .collect()
41    }
42}