dirge-agent 0.19.15

Minimalistic coding agent written in Rust, optimized for memory footprint and performance
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//! `task_status` — poll a background [`task`](crate::agent::tools::task) by id
//! (optionally waiting), reporting its [`TaskState`]. The read side of the
//! background-subagent surface.
//!
//! See the canonical map of the four work-tracking concepts in
//! [`crate::agent::plan`].

use rig::completion::ToolDefinition;
use rig::tool::Tool;
use serde::Deserialize;
use std::time::Duration;

use crate::agent::tools::background::{BackgroundStore, TaskState};
use crate::agent::tools::{AskSender, PermCheck, ToolError, check_perm};

pub struct TaskStatusTool {
    bg_store: BackgroundStore,
    permission: Option<PermCheck>,
    ask_tx: Option<AskSender>,
}

impl TaskStatusTool {
    pub fn new(bg_store: BackgroundStore) -> Self {
        Self {
            bg_store,
            permission: None,
            ask_tx: None,
        }
    }

    pub fn with_permission(
        mut self,
        permission: Option<PermCheck>,
        ask_tx: Option<AskSender>,
    ) -> Self {
        self.permission = permission;
        self.ask_tx = ask_tx;
        self
    }
}

#[derive(Deserialize)]
pub struct TaskStatusArgs {
    pub task_id: String,
    #[serde(default)]
    pub wait: Option<bool>,
}

impl Tool for TaskStatusTool {
    const NAME: &'static str = "task_status";

    type Error = ToolError;
    type Args = TaskStatusArgs;
    type Output = String;

    async fn definition(&self, _prompt: String) -> ToolDefinition {
        ToolDefinition {
            name: "task_status".to_string(),
            description: "Look up the state of a background task by id. You usually do NOT need this — completion notifications arrive automatically as a <system-reminder> on your next turn. Use task_status only when you need to re-check a task's status mid-turn or look up a task whose notification you've already consumed. Returns running / completed / failed plus the result text. Set wait=true to block until the task transitions out of running (rarely useful — prefer letting the notification arrive).".to_string(),
            parameters: serde_json::json!({
                "type": "object",
                "properties": {
                    "task_id": {
                        "type": "string",
                        "description": "The task ID returned by the task tool with background=true"
                    },
                    "wait": {
                        "type": "boolean",
                        "description": "Block until the task completes (default: false)"
                    }
                },
                "required": ["task_id"]
            }),
        }
    }

    async fn call(&self, args: TaskStatusArgs) -> Result<String, ToolError> {
        // Same permission gate as `task` — status polling can side-
        // channel sensitive subagent results, and the wait=true path
        // can hold the parent turn open for up to 10 minutes.
        check_perm(&self.permission, &self.ask_tx, "task_status", &args.task_id).await?;
        let wait = args.wait.unwrap_or(false);

        if let Some(generation) = self
            .bg_store
            .task_is_awaiting_coordinator_delivery(&args.task_id)
        {
            return Ok(format!(
                "Task {} belongs to active coordinator batch {}; its result will be delivered at batch reconciliation.",
                args.task_id, generation
            ));
        }

        if wait {
            // Hard cap so a stuck subagent (e.g. LLM hang) can't hold the
            // parent turn open forever. The agent should rely on the
            // automatic <system-reminder> on the next turn instead of
            // long waits.
            const MAX_WAIT: Duration = Duration::from_secs(600);
            // tokio::time::Instant respects paused timers in tests; using
            // std::time::Instant here would hang under tokio::time::pause().
            let started = tokio::time::Instant::now();
            loop {
                let state = self.bg_store.get(&args.task_id);
                match state {
                    Some(task) => match &task.state {
                        TaskState::Running => {
                            if started.elapsed() >= MAX_WAIT {
                                return Ok(format!(
                                    "task_id: {}\nstate: running\n\nwait timeout reached ({}s) — the task is still running. The notification will arrive automatically when it finishes.",
                                    args.task_id,
                                    MAX_WAIT.as_secs()
                                ));
                            }
                            tokio::time::sleep(Duration::from_millis(300)).await;
                            continue;
                        }
                        TaskState::Completed(text) => {
                            return Ok(format!(
                                "task_id: {}\nstate: completed\n\n{}",
                                args.task_id, text
                            ));
                        }
                        TaskState::Failed(err) => {
                            return Ok(format!(
                                "task_id: {}\nstate: failed\n\nerror: {}",
                                args.task_id, err
                            ));
                        }
                        TaskState::Cancelled(reason) => {
                            return Ok(format!(
                                "task_id: {}\nstate: cancelled\n\nreason: {}",
                                args.task_id, reason
                            ));
                        }
                    },
                    None => {
                        return Err(ToolError::Msg(format!(
                            "task not found: {} (either it never existed, or it was evicted from the background store after 32 newer tasks)",
                            args.task_id
                        )));
                    }
                }
            }
        } else {
            match self.bg_store.get(&args.task_id) {
                Some(task) => match &task.state {
                    TaskState::Running => Ok(format!("task_id: {}\nstate: running", args.task_id)),
                    TaskState::Completed(text) => Ok(format!(
                        "task_id: {}\nstate: completed\n\n{}",
                        args.task_id, text
                    )),
                    TaskState::Failed(err) => Ok(format!(
                        "task_id: {}\nstate: failed\n\nerror: {}",
                        args.task_id, err
                    )),
                    TaskState::Cancelled(reason) => Ok(format!(
                        "task_id: {}\nstate: cancelled\n\nreason: {}",
                        args.task_id, reason
                    )),
                },
                None => Err(ToolError::Msg(format!(
                    "task not found: {} (either it never existed, or it was evicted from the background store after 32 newer tasks)",
                    args.task_id
                ))),
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_task_status_not_found() {
        let store = BackgroundStore::new();
        let tool = TaskStatusTool::new(store);
        let result = tool
            .call(TaskStatusArgs {
                task_id: "nonexistent".to_string(),
                wait: None,
            })
            .await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("not found"));
    }

    #[tokio::test]
    async fn test_task_status_running() {
        let store = BackgroundStore::new();
        store.insert("test-task".to_string());
        let tool = TaskStatusTool::new(store);
        let result = tool
            .call(TaskStatusArgs {
                task_id: "test-task".to_string(),
                wait: None,
            })
            .await
            .unwrap();
        assert!(result.contains("state: running"));
    }

    #[tokio::test]
    async fn test_task_status_completed() {
        let store = BackgroundStore::new();
        store.insert("test-task".to_string());
        store.notify("test-task", TaskState::Completed("result text".to_string()));
        let tool = TaskStatusTool::new(store);
        let result = tool
            .call(TaskStatusArgs {
                task_id: "test-task".to_string(),
                wait: None,
            })
            .await
            .unwrap();
        assert!(result.contains("state: completed"));
        assert!(result.contains("result text"));
    }

    #[tokio::test]
    async fn coordinator_batch_hides_terminal_task_payload_until_delivery() {
        let store = BackgroundStore::new();
        store.enable_coordinator(crate::config::SubagentDispatchStrategy::Full);
        store.insert_coordinated("first".to_string());
        store.insert_coordinated("second".to_string());
        store.notify("first", TaskState::Completed("secret result".to_string()));

        let result = TaskStatusTool::new(store)
            .call(TaskStatusArgs {
                task_id: "first".to_string(),
                wait: Some(true),
            })
            .await
            .unwrap();
        assert!(result.contains("coordinator batch"));
        assert!(!result.contains("secret result"));
    }

    #[tokio::test]
    async fn test_task_status_failed() {
        let store = BackgroundStore::new();
        store.insert("test-task".to_string());
        store.notify("test-task", TaskState::Failed("error message".to_string()));
        let tool = TaskStatusTool::new(store);
        let result = tool
            .call(TaskStatusArgs {
                task_id: "test-task".to_string(),
                wait: None,
            })
            .await
            .unwrap();
        assert!(result.contains("state: failed"));
        assert!(result.contains("error message"));
    }

    #[tokio::test]
    async fn test_task_status_wait_completed() {
        let store = BackgroundStore::new();
        store.insert("test-task".to_string());

        // Update to completed after a short delay
        let store_clone = store.clone();
        tokio::spawn(async move {
            tokio::time::sleep(Duration::from_millis(100)).await;
            store_clone.notify("test-task", TaskState::Completed("done".to_string()));
        });

        let tool = TaskStatusTool::new(store);
        let result = tool
            .call(TaskStatusArgs {
                task_id: "test-task".to_string(),
                wait: Some(true),
            })
            .await
            .unwrap();
        assert!(result.contains("state: completed"));
        assert!(result.contains("done"));
    }

    #[tokio::test]
    async fn test_definition_has_correct_name() {
        let store = BackgroundStore::new();
        let tool = TaskStatusTool::new(store);
        let def = tool.definition(String::new()).await;
        assert_eq!(def.name, "task_status");
    }

    // Regression: the description must steer the agent away from polling
    // (notifications now arrive automatically). A future "improvement" that
    // re-introduces "poll until completion" language would silently regress
    // the push-notification UX.
    #[tokio::test]
    async fn definition_discourages_polling() {
        let store = BackgroundStore::new();
        let tool = TaskStatusTool::new(store);
        let def = tool.definition(String::new()).await;
        let desc = def.description.to_lowercase();
        assert!(
            desc.contains("system-reminder") || desc.contains("automatically"),
            "task_status description must reference automatic notification: {}",
            def.description
        );
        assert!(
            desc.contains("do not") || desc.contains("usually") || desc.contains("rarely"),
            "task_status description must discourage routine polling: {}",
            def.description
        );
    }

    // task_status is read-only: repeated lookups return the same payload.
    // Completed tasks are evicted by the store's LRU cap, not by reads, so
    // an agent can re-fetch its own results idempotently. Notification
    // delivery (Phase 3) happens out-of-band via drain_notifications().
    #[tokio::test]
    async fn status_lookup_is_idempotent() {
        let store = BackgroundStore::new();
        store.insert("t1".into());
        store.notify("t1", TaskState::Completed("payload".into()));

        let tool = TaskStatusTool::new(store);
        for _ in 0..3 {
            let result = tool
                .call(TaskStatusArgs {
                    task_id: "t1".into(),
                    wait: None,
                })
                .await
                .unwrap();
            assert!(result.contains("state: completed"));
            assert!(result.contains("payload"));
        }
    }

    // wait=true must also return on failure (not just on completion), and the
    // error text must be surfaced.
    #[tokio::test]
    async fn wait_returns_on_failure() {
        let store = BackgroundStore::new();
        store.insert("t1".into());

        let store_clone = store.clone();
        tokio::spawn(async move {
            tokio::time::sleep(Duration::from_millis(50)).await;
            store_clone.notify("t1", TaskState::Failed("kaboom".into()));
        });

        let tool = TaskStatusTool::new(store);
        let result = tool
            .call(TaskStatusArgs {
                task_id: "t1".into(),
                wait: Some(true),
            })
            .await
            .unwrap();
        assert!(result.contains("state: failed"));
        assert!(result.contains("kaboom"));
    }

    // wait=true must surface a not-found error promptly rather than loop on
    // an absent task.
    #[tokio::test]
    async fn wait_on_missing_task_errors_promptly() {
        let store = BackgroundStore::new();
        let tool = TaskStatusTool::new(store);

        // Bound the call so a regression to infinite-loop fails the test.
        let result = tokio::time::timeout(
            Duration::from_secs(1),
            tool.call(TaskStatusArgs {
                task_id: "never-existed".into(),
                wait: Some(true),
            }),
        )
        .await
        .expect("must not loop on missing task");

        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("not found"));
    }

    // Regression M6: wait=true used to loop forever on a never-completing
    // task. The hard cap (MAX_WAIT, 600s) now bails with a "still running"
    // message after the timeout elapses. We verify the behavior by running
    // a contrived in-process test with tokio::time::pause + advance to fast-
    // forward through the 600 seconds without actually waiting.
    #[tokio::test(start_paused = true)]
    async fn wait_returns_timeout_message_when_task_stays_running() {
        let store = BackgroundStore::new();
        store.insert("forever".into());
        let tool = TaskStatusTool::new(store);

        // Drive the future with a parallel timer that advances past the cap.
        let task_call = tokio::spawn(async move {
            tool.call(TaskStatusArgs {
                task_id: "forever".into(),
                wait: Some(true),
            })
            .await
        });

        // Advance virtual time past MAX_WAIT (600s). tokio's paused timer
        // requires us to yield between sleeps for the loop to make progress.
        for _ in 0..2100 {
            tokio::time::advance(Duration::from_millis(300)).await;
            tokio::task::yield_now().await;
        }

        let result = task_call.await.unwrap().unwrap();
        assert!(result.contains("state: running"), "got: {result}");
        assert!(
            result.contains("timeout") || result.contains("running"),
            "got: {result}"
        );
    }
}