pub struct BackgroundTaskManager { /* private fields */ }Expand description
Thread-scoped handle table for background tasks.
Spawns, tracks, cancels, and queries background tasks.
Task metadata (status, error, result, timestamps) is stored in the
StateStore as the single source of truth. This struct only holds
runtime handles (cancel, join, inbox).
Implementations§
Source§impl BackgroundTaskManager
impl BackgroundTaskManager
pub fn new() -> BackgroundTaskManager
Sourcepub fn set_owner_inbox(&self, inbox: InboxSender)
pub fn set_owner_inbox(&self, inbox: InboxSender)
Set the inbox sender that background tasks receive for pushing messages to the owner thread.
Sourcepub fn set_store(&self, store: StateStore)
pub fn set_store(&self, store: StateStore)
Provide the state store for metadata persistence.
Called once during plugin registration or run start. Subsequent calls are silently ignored (OnceLock semantics).
Sourcepub async fn spawn<F, Fut>(
self: &Arc<BackgroundTaskManager>,
owner_thread_id: &str,
task_type: &str,
name: Option<&str>,
description: &str,
parent_context: TaskParentContext,
task_fn: F,
) -> Result<String, SpawnError>where
F: FnOnce(TaskContext) -> Fut + Send + 'static,
Fut: Future<Output = TaskResult> + Send + 'static,
pub async fn spawn<F, Fut>(
self: &Arc<BackgroundTaskManager>,
owner_thread_id: &str,
task_type: &str,
name: Option<&str>,
description: &str,
parent_context: TaskParentContext,
task_fn: F,
) -> Result<String, SpawnError>where
F: FnOnce(TaskContext) -> Fut + Send + 'static,
Fut: Future<Output = TaskResult> + Send + 'static,
Spawn a background task.
The task_fn receives a TaskContext and returns a TaskResult.
Spawn a background task.
name is an optional short identifier for addressing (e.g. “researcher”).
If provided, it must be unique among running tasks on this thread and
must not be a reserved name (“parent”, “self”, “all”, “broadcast”).
Sourcepub async fn cancel_tree(&self, task_id: &str) -> usize
pub async fn cancel_tree(&self, task_id: &str) -> usize
Cancel a task and every known descendant task in the same manager.
Descendants are discovered through TaskParentContext.task_id.
Returns the number of live tasks whose cancellation token was signalled.
Sourcepub async fn cancel_all(&self, owner_thread_id: &str) -> usize
pub async fn cancel_all(&self, owner_thread_id: &str) -> usize
Cancel all running tasks for a given thread. Returns the number of tasks cancelled.
Sourcepub async fn list(&self, owner_thread_id: &str) -> Vec<TaskSummary>
pub async fn list(&self, owner_thread_id: &str) -> Vec<TaskSummary>
List all tasks for a given owner thread.
Sourcepub async fn get(&self, task_id: &str) -> Option<TaskSummary>
pub async fn get(&self, task_id: &str) -> Option<TaskSummary>
Get the summary of a specific task.
Sourcepub async fn has_running(&self, owner_thread_id: &str) -> bool
pub async fn has_running(&self, owner_thread_id: &str) -> bool
Returns true if any task for the given thread is still running.
Sourcepub async fn spawn_agent<F, Fut>(
self: &Arc<BackgroundTaskManager>,
owner_thread_id: &str,
name: Option<&str>,
description: &str,
parent_context: TaskParentContext,
task_fn: F,
) -> Result<String, SpawnError>where
F: FnOnce(CancellationToken, InboxSender, InboxReceiver) -> Fut + Send + 'static,
Fut: Future<Output = TaskResult> + Send + 'static,
pub async fn spawn_agent<F, Fut>(
self: &Arc<BackgroundTaskManager>,
owner_thread_id: &str,
name: Option<&str>,
description: &str,
parent_context: TaskParentContext,
task_fn: F,
) -> Result<String, SpawnError>where
F: FnOnce(CancellationToken, InboxSender, InboxReceiver) -> Fut + Send + 'static,
Fut: Future<Output = TaskResult> + Send + 'static,
Spawn a sub-agent as a background task with its own inbox.
name is an optional short identifier for addressing via send_message.
Sourcepub async fn spawn_agent_with_context<F, Fut>(
self: &Arc<BackgroundTaskManager>,
owner_thread_id: &str,
name: Option<&str>,
description: &str,
parent_context: TaskParentContext,
task_fn: F,
) -> Result<String, SpawnError>where
F: FnOnce(AgentTaskContext) -> Fut + Send + 'static,
Fut: Future<Output = TaskResult> + Send + 'static,
pub async fn spawn_agent_with_context<F, Fut>(
self: &Arc<BackgroundTaskManager>,
owner_thread_id: &str,
name: Option<&str>,
description: &str,
parent_context: TaskParentContext,
task_fn: F,
) -> Result<String, SpawnError>where
F: FnOnce(AgentTaskContext) -> Fut + Send + 'static,
Fut: Future<Output = TaskResult> + Send + 'static,
Spawn a sub-agent as a background task while exposing the spawned task ID to the closure for lineage-aware coordination.
Sourcepub async fn send_task_inbox_message(
&self,
task_id: &str,
owner_thread_id: &str,
sender_agent_id: &str,
content: &str,
) -> Result<(), SendError>
pub async fn send_task_inbox_message( &self, task_id: &str, owner_thread_id: &str, sender_agent_id: &str, content: &str, ) -> Result<(), SendError>
Send a message to a child task’s live inbox.
This is the internal low-latency transport for parent→child
communication within the same process. For cross-agent or durable
messaging, use the mailbox-based send_message tool instead.