Skip to main content

BackgroundTasks

Struct BackgroundTasks 

Source
pub struct BackgroundTasks { /* private fields */ }
Expand description

Session-level background task table. Clone is cheap (inner Arc) — DefaultSession holds one copy, cloned to tools via ToolContext.

Implementations§

Source§

impl BackgroundTasks

Source

pub fn new( session_cancel: CancellationToken, progress_config: BackgroundProgressConfig, ) -> Self

Constructs a new instance with a session-level cancellation token and a progress-view configuration. session_cancel is owned by the session and is cancelled when the session terminates.

Source

pub async fn wait_for_completion(&self)

Wait for a “task completion enqueued” event. The session driver uses this to drive proactive continuation.

Uses Notify: the driver first calls notified() to obtain a future, then checks the queue, then awaits — avoiding missed notifications that arrive between checks (Notify’s permit semantics guarantee that already-fired notifies are not lost).

Source

pub fn has_completed(&self) -> bool

Whether there are completed results waiting to be collected. The driver checks this after waking up to decide whether to start a turn.

Source

pub fn spawn<F, Fut>(&self, label: String, make_fut: F) -> String
where F: FnOnce(CancellationToken, TaskHandle) -> Fut, Fut: Future<Output = BackgroundResult> + Send + 'static,

Spawns a background task and returns its ID immediately.

make_fut receives two handles: a CancellationToken specific to this task (a child of the session-level token, which the task body should use to observe cancellation) and a TaskHandle (the task body shares its history Arc into the table via TaskHandle::attach_history, allowing the control plane to peek at the message chunks submitted to the LLM). On completion, the result is placed in the completed queue and the corresponding entry in the tasks table is marked as terminal (the entry is retained for later inspection).

The closure form that “receives token/handle and then creates the future” is used because both must be minted inside spawn, and the future needs to capture them — accepting a future directly would not allow obtaining a token whose lifetime is independent of the turn.

Source

pub fn drain_completed(&self) -> Vec<BackgroundOutcome>

Drain all completed results (clears the queue). Called by run_turn before starting a turn to passively collect results.

Source

pub fn running_count(&self) -> usize

Number of currently running tasks. Used for diagnostics / control plane.

Source

pub fn list(&self) -> Vec<TaskSnapshot>

Returns a snapshot of all tasks (running + recently finished), without reading history (recent is empty, block_count is 0). Sorted by task ID in ascending order. Used by inspect_background_task when called without arguments.

Source

pub fn peek( &self, id: &str, recent_blocks: Option<usize>, ) -> Option<TaskSnapshot>

Take a snapshot of a single task, including the most recent recent_blocks message blocks submitted to the LLM (None uses the config default). Returns None if the task does not exist (never spawned or already evicted); blocks are empty if the task does not expose history.

Implementation: clone the task’s history Arc while holding the table lock, then release the table lock before snapshotting (snapshotting uses the history’s own lock). This avoids performing a potentially expensive deep copy of history while holding the table lock, which would block spawn/finish.

Source

pub fn cancel_task(&self, id: &str) -> Option<bool>

Cancel a single task early: cancels only its dedicated child token, without affecting other tasks.

Returns Some(true) if a running task was found and cancellation was requested; Some(false) if the task exists but is already in a terminal state (no-op); None if no such id exists. Cancellation is cooperative — the task body must observe its cancel token and exit; the status transitions to Canceled only when the task actually finishes.

Source

pub fn cancel_all(&self)

Cancels all background tasks (called when the session ends). Idempotent.

Trait Implementations§

Source§

impl Clone for BackgroundTasks

Source§

fn clone(&self) -> BackgroundTasks

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoMaybeUndefined<T> for T

Source§

impl<T> IntoOption<T> for T

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more