walrus_daemon/hook/system/task/mod.rs
1//! Task registry — in-memory tracking of agent work units.
2//!
3//! [`TaskRegistry`] stores [`Task`] records with concurrency control,
4//! parent/sub-task hierarchy, and inbox-based blocking for user approval.
5
6use compact_str::CompactString;
7use tokio::sync::{oneshot, watch};
8use tokio::task::AbortHandle;
9use tokio::time::Instant;
10
11pub use registry::TaskRegistry;
12
13mod registry;
14pub(crate) mod tool;
15
16/// Task execution status.
17#[derive(Debug, Clone, Copy, PartialEq, Eq)]
18pub enum TaskStatus {
19 /// Waiting for a concurrency slot.
20 Queued,
21 /// Actively running.
22 InProgress,
23 /// Blocked waiting for user approval.
24 Blocked,
25 /// Completed successfully.
26 Finished,
27 /// Completed with error.
28 Failed,
29}
30
31impl std::fmt::Display for TaskStatus {
32 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33 match self {
34 Self::Queued => write!(f, "queued"),
35 Self::InProgress => write!(f, "in_progress"),
36 Self::Blocked => write!(f, "blocked"),
37 Self::Finished => write!(f, "finished"),
38 Self::Failed => write!(f, "failed"),
39 }
40 }
41}
42
43/// Pending user approval item — blocks task until resolved.
44pub struct InboxItem {
45 /// Description of what needs approval (tool name + args summary).
46 pub question: String,
47 /// Channel to send the user's response through.
48 pub reply: oneshot::Sender<String>,
49}
50
51impl std::fmt::Debug for InboxItem {
52 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
53 f.debug_struct("InboxItem")
54 .field("question", &self.question)
55 .finish()
56 }
57}
58
59/// A tracked unit of agent work.
60pub struct Task {
61 /// Unique task identifier.
62 pub id: u64,
63 /// Parent task ID for sub-task hierarchy.
64 pub parent_id: Option<u64>,
65 /// Session allocated for this task's execution.
66 pub session_id: Option<u64>,
67 /// Agent assigned to this task.
68 pub agent: CompactString,
69 /// Current execution status.
70 pub status: TaskStatus,
71 /// Origin of this task ("user" or agent name).
72 pub created_by: CompactString,
73 /// Human-readable task description / message.
74 pub description: String,
75 /// Final result content (set on Finished).
76 pub result: Option<String>,
77 /// Error message (set on Failed).
78 pub error: Option<String>,
79 /// Pending approval item (set when status is Blocked).
80 pub blocked_on: Option<InboxItem>,
81 /// Cumulative prompt tokens used.
82 pub prompt_tokens: u64,
83 /// Cumulative completion tokens used.
84 pub completion_tokens: u64,
85 /// When this task was created.
86 pub created_at: Instant,
87 /// Handle to abort the spawned execution task.
88 pub abort_handle: Option<AbortHandle>,
89 /// Watch channel for status change notifications (used by await_tasks).
90 pub status_tx: watch::Sender<TaskStatus>,
91}