pub struct TaskState {
pub id: TaskId,
pub description: String,
pub team: String,
pub name: String,
pub started_at: DateTime<Utc>,
pub output_tx: UnboundedSender<String>,
pub final_result: Mutex<Option<Result<String, String>>>,
pub completed_notify: Arc<Notify>,
/* private fields */
}Expand description
Full state of a single background task.
Fields§
§id: TaskIdStable opaque task ID. Use this for task_get, task_stop, etc.
description: StringHuman-readable description (e.g. the goal the agent was given).
team: StringTeam this task belongs to, if any. Empty string means “no team”.
name: StringTeammate name within the team (empty if not a teammate).
started_at: DateTime<Utc>When the task was started.
output_tx: UnboundedSender<String>Channel sender for the inner agent to push more output.
final_result: Mutex<Option<Result<String, String>>>Set when a final result (Ok or Err) is captured. Read by
task_get / task_output and reported as the task’s terminal
status. Held in an Option so the registry can record the
outcome even after the JoinHandle finishes.
completed_notify: Arc<Notify>Fired once when the task reaches any terminal state (Completed /
Failed / Stopped). Uses notify_one() so a completion that happens
while a waiter is mid-turn is not lost — the next .notified() poll
consumes the stored permit. Mirrors the BackgroundJobManager pattern
in src/tools/run_background.rs. Used by task_output(block=true)
to wait event-driven instead of busy-polling.
Implementations§
Source§impl TaskState
impl TaskState
Sourcepub fn new(
description: impl Into<String>,
team: impl Into<String>,
name: impl Into<String>,
) -> (Self, TaskId)
pub fn new( description: impl Into<String>, team: impl Into<String>, name: impl Into<String>, ) -> (Self, TaskId)
Create a new in-memory task state. The caller must call
set_handle to attach the JoinHandle once the task is spawned.
Sourcepub async fn set_handle(&self, handle: JoinHandle<()>)
pub async fn set_handle(&self, handle: JoinHandle<()>)
Attach the JoinHandle for cancellation / awaiting.
Sourcepub async fn status(&self) -> TaskStatus
pub async fn status(&self) -> TaskStatus
Read the current status (cloned).
Sourcepub async fn append_output(&self, line: String)
pub async fn append_output(&self, line: String)
Append a single output line to the buffer.
Sourcepub async fn output_snapshot(&self) -> Vec<String>
pub async fn output_snapshot(&self) -> Vec<String>
Read a snapshot of the output buffer.
Sourcepub async fn drain_output(&self) -> usize
pub async fn drain_output(&self) -> usize
Drain any pending output from the inner channel into self.output.
Returns the number of new lines drained.
Sourcepub async fn stop(&self) -> bool
pub async fn stop(&self) -> bool
Cancel the task. Returns true if a running handle was found
and aborted.
Sourcepub async fn mark_completed(&self, output: String)
pub async fn mark_completed(&self, output: String)
Mark the task as completed with the given output text.
Sourcepub async fn mark_failed(&self, err: String)
pub async fn mark_failed(&self, err: String)
Mark the task as failed.
Sourcepub fn completed_notify(&self) -> Arc<Notify> ⓘ
pub fn completed_notify(&self) -> Arc<Notify> ⓘ
Return a clone of the completion-notify handle.
Callers can select! on completed_notify().notified() to wake
when the task enters any terminal state (Completed / Failed /
Stopped), instead of polling status(). Mirrors
BackgroundJobManager::completed_notify.