aidaemon-work 0.27.2

Goals, mandates, tasks and work coordination with a SQLite store; internal to aidaemon, no stability promise
Documentation
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
//! `/work` in chat: the owner's view of durable work (lanes, tasks, journal
//! comments, retries, workspaces, worker profiles and projects), over the
//! [`WorkCoordinationStore`]. A host adds it to the shared chat commands.

use std::sync::Arc;

use aidaemon_channels::slash::{SlashCall, SlashCommand};
use aidaemon_tools::semantics::UserRole;
use async_trait::async_trait;

use crate::goals::{TaskJournalEntry, WorkTaskSummary};
use crate::stores::WorkCoordinationStore;

/// The `/work` command.
pub struct WorkCommand {
    state: Arc<dyn WorkCoordinationStore>,
}

impl WorkCommand {
    pub fn new(state: Arc<dyn WorkCoordinationStore>) -> Self {
        Self { state }
    }
}

#[async_trait]
impl SlashCommand for WorkCommand {
    fn handles(&self, cmd: &str) -> bool {
        cmd == "/work"
    }

    async fn run(&self, _cmd: &str, args: &str, call: &SlashCall<'_>) -> String {
        self.handle_work(args, call.session_id, call.user_role)
            .await
    }
}

impl WorkCommand {
    async fn handle_work(&self, args: &str, session_id: &str, user_role: UserRole) -> String {
        if user_role != UserRole::Owner {
            return "Only the owner can view or manage durable work.".to_string();
        }

        let mut parts = args.split_whitespace();
        let action = parts.next().unwrap_or("overview").to_ascii_lowercase();
        let project_id = match self.state.get_session_work_project(session_id).await {
            Ok(project_id) => project_id,
            Err(error) => return format!("Could not resolve the active project: {error}"),
        };

        match action.as_str() {
            "overview" | "all" => self.render_work_overview(&project_id).await,
            "waiting" | "ready" | "running" | "in_progress" | "blocked" | "attention"
            | "needs_attention" | "done" => {
                let lane = match action.as_str() {
                    "running" => "in_progress",
                    "attention" => "needs_attention",
                    value => value,
                };
                self.render_work_lane(&project_id, lane).await
            }
            "show" => {
                let Some(reference) = parts.next() else {
                    return "Usage: /work show <task-id>".to_string();
                };
                let task = match self.resolve_work_task(&project_id, reference).await {
                    Ok(task) => task,
                    Err(message) => return message,
                };
                self.render_work_task(&task).await
            }
            "comment" | "unblock" => {
                let Some(reference) = parts.next() else {
                    return format!("Usage: /work {action} <task-id> <message>");
                };
                let message = parts.collect::<Vec<_>>().join(" ");
                if message.trim().is_empty() {
                    return format!("Usage: /work {action} <task-id> <message>");
                }
                let task = match self.resolve_work_task(&project_id, reference).await {
                    Ok(task) => task,
                    Err(message) => return message,
                };
                if action == "unblock" {
                    return match self
                        .state
                        .unblock_task(&task.task_id, &message, "owner", Some(session_id))
                        .await
                    {
                        Ok(true) => format!(
                            "Task {} was unblocked and returned to the ready queue.",
                            short_id(&task.task_id)
                        ),
                        Ok(false) => "That task is not currently blocked.".to_string(),
                        Err(error) => format!("Could not unblock task: {error}"),
                    };
                }
                let entry = TaskJournalEntry::new(
                    &task.project_id,
                    &task.goal_id,
                    &task.goal_run_id,
                    "comment",
                    "human",
                    "owner",
                    &message,
                )
                .with_task(Some(&task.task_id), None)
                .with_source_channel(Some(session_id));
                match self.state.append_task_journal(&entry).await {
                    Ok(()) => format!("Comment added to task {}.", short_id(&task.task_id)),
                    Err(error) => format!("Could not add comment: {error}"),
                }
            }
            "retry" | "cancel" => {
                let Some(reference) = parts.next() else {
                    return format!("Usage: /work {action} <task-id>");
                };
                let task = match self.resolve_work_task(&project_id, reference).await {
                    Ok(task) => task,
                    Err(message) => return message,
                };
                let result = if action == "retry" {
                    self.state
                        .retry_work_task(&task.task_id, "owner", Some(session_id))
                        .await
                } else {
                    self.state
                        .cancel_work_task(&task.task_id, "owner", Some(session_id))
                        .await
                };
                match result {
                    Ok(true) if action == "retry" => {
                        format!("Task {} was queued for retry.", short_id(&task.task_id))
                    }
                    Ok(true) => format!("Task {} was cancelled.", short_id(&task.task_id)),
                    Ok(false) if action == "retry" => {
                        format!("Task {} cannot be retried.", short_id(&task.task_id))
                    }
                    Ok(false) => format!("Task {} cannot be cancelled.", short_id(&task.task_id)),
                    Err(error) => format!("Could not {action} task: {error}"),
                }
            }
            "assign" => {
                let (Some(reference), Some(profile_id)) = (parts.next(), parts.next()) else {
                    return "Usage: /work assign <task-id> <profile-id>".to_string();
                };
                let task = match self.resolve_work_task(&project_id, reference).await {
                    Ok(task) => task,
                    Err(message) => return message,
                };
                match self
                    .state
                    .assign_task_worker_profile(
                        &task.task_id,
                        profile_id,
                        "owner",
                        Some(session_id),
                    )
                    .await
                {
                    Ok(true) => format!(
                        "Task {} assigned to profile {}.",
                        short_id(&task.task_id),
                        profile_id
                    ),
                    Ok(false) => "Task or worker profile was unavailable.".to_string(),
                    Err(error) => format!("Could not assign worker profile: {error}"),
                }
            }
            "workspace" => {
                let Some(reference) = parts.next() else {
                    return "Usage: /work workspace <task-id> [shared|isolated|worktree]"
                        .to_string();
                };
                let task = match self.resolve_work_task(&project_id, reference).await {
                    Ok(task) => task,
                    Err(message) => return message,
                };
                if let Some(policy) = parts.next() {
                    return match self
                        .state
                        .set_task_workspace_policy(&task.task_id, policy)
                        .await
                    {
                        Ok(true) => format!(
                            "Task {} workspace policy set to {}.",
                            short_id(&task.task_id),
                            policy
                        ),
                        Ok(false) => {
                            "Workspace policy can only change before execution.".to_string()
                        }
                        Err(error) => format!("Could not set workspace policy: {error}"),
                    };
                }
                match self.state.get_task_workspace(&task.task_id).await {
                    Ok(Some(workspace)) => format!(
                        "Task {} workspace\nPolicy: {}\nStatus: {}\nPath: {}\nBranch: {}",
                        short_id(&task.task_id),
                        workspace.policy,
                        workspace.status,
                        workspace.root_path,
                        workspace.branch_name.as_deref().unwrap_or("—")
                    ),
                    Ok(None) => match self.state.get_task_workspace_policy(&task.task_id).await {
                        Ok(policy) => format!(
                            "Task {} has policy {}; no workspace has been created yet.",
                            short_id(&task.task_id),
                            policy
                        ),
                        Err(error) => format!("Could not inspect workspace: {error}"),
                    },
                    Err(error) => format!("Could not inspect workspace: {error}"),
                }
            }
            "profiles" => match self.state.list_worker_profiles(Some(&project_id)).await {
                Ok(profiles) if profiles.is_empty() => "No enabled worker profiles.".to_string(),
                Ok(profiles) => {
                    let lines = profiles
                        .into_iter()
                        .map(|profile| {
                            format!(
                                "• {} — {} (max {}, {})",
                                profile.id,
                                profile.name,
                                profile.max_concurrency,
                                profile.workspace_policy
                            )
                        })
                        .collect::<Vec<_>>();
                    format!("Worker profiles\n{}", lines.join("\n"))
                }
                Err(error) => format!("Could not list worker profiles: {error}"),
            },
            "projects" => match self.state.list_work_projects().await {
                Ok(projects) => {
                    let lines = projects
                        .into_iter()
                        .map(|project| {
                            let marker = if project.id == project_id {
                                " (active)"
                            } else {
                                ""
                            };
                            format!("• {} — {}{}", project.id, project.name, marker)
                        })
                        .collect::<Vec<_>>();
                    format!("Projects\n{}", lines.join("\n"))
                }
                Err(error) => format!("Could not list projects: {error}"),
            },
            "project" => {
                let Some(reference_head) = parts.next() else {
                    return format!("Active project: {project_id}\nUsage: /work project <id>");
                };
                if reference_head.eq_ignore_ascii_case("create") {
                    let name = parts.collect::<Vec<_>>().join(" ");
                    if name.trim().is_empty() {
                        return "Usage: /work project create <name>".to_string();
                    }
                    return match self.state.create_work_project(&name, None).await {
                        Ok(project) => match self
                            .state
                            .set_session_work_project(session_id, &project.id)
                            .await
                        {
                            Ok(true) => format!(
                                "Created and selected project {} ({})",
                                project.name, project.id
                            ),
                            Ok(false) => {
                                "Project was created but could not be selected.".to_string()
                            }
                            Err(error) => {
                                format!("Project was created but selection failed: {error}")
                            }
                        },
                        Err(error) => format!("Could not create project: {error}"),
                    };
                }
                let reference = std::iter::once(reference_head)
                    .chain(parts)
                    .collect::<Vec<_>>()
                    .join(" ");
                let projects = match self.state.list_work_projects().await {
                    Ok(projects) => projects,
                    Err(error) => return format!("Could not list projects: {error}"),
                };
                let matches = projects
                    .into_iter()
                    .filter(|project| {
                        project.id == reference || project.name.eq_ignore_ascii_case(&reference)
                    })
                    .collect::<Vec<_>>();
                let Some(project) = matches.first() else {
                    return "Project not found. Use /work projects to list choices.".to_string();
                };
                match self
                    .state
                    .set_session_work_project(session_id, &project.id)
                    .await
                {
                    Ok(true) => format!("Active project set to {}.", project.name),
                    Ok(false) => "Project not found.".to_string(),
                    Err(error) => format!("Could not select project: {error}"),
                }
            }
            _ => work_usage(),
        }
    }

    async fn render_work_overview(&self, project_id: &str) -> String {
        match self.state.list_work_goals(project_id, false, 25).await {
            Ok(goals) if goals.is_empty() => {
                format!("No active work in project {project_id}.")
            }
            Ok(goals) => {
                let lines = goals
                    .into_iter()
                    .map(|goal| {
                        format!(
                            "• {} {} — ready {} · running {} · blocked {} · attention {} · done {}",
                            short_id(&goal.goal_id),
                            truncate_chars(&goal.description, 72),
                            goal.ready,
                            goal.in_progress,
                            goal.blocked,
                            goal.needs_attention,
                            goal.done
                        )
                    })
                    .collect::<Vec<_>>();
                format!(
                    "Active work · project {project_id}\n{}\n\nUse /work ready, /work blocked, or /work show <task-id>.",
                    lines.join("\n")
                )
            }
            Err(error) => format!("Could not load work: {error}"),
        }
    }

    async fn render_work_lane(&self, project_id: &str, lane: &str) -> String {
        match self.state.list_work_tasks(project_id, Some(lane), 50).await {
            Ok(tasks) if tasks.is_empty() => format!("No tasks in the {lane} lane."),
            Ok(tasks) => {
                let lines = tasks
                    .into_iter()
                    .map(|task| {
                        let owner = task
                            .worker_profile
                            .as_deref()
                            .map(|profile| format!(" · {profile}"))
                            .unwrap_or_default();
                        format!(
                            "• {} [{}] {}{}",
                            short_id(&task.task_id),
                            task.priority,
                            truncate_chars(&task.description, 90),
                            owner
                        )
                    })
                    .collect::<Vec<_>>();
                format!("{lane}\n{}", lines.join("\n"))
            }
            Err(error) => format!("Could not load work: {error}"),
        }
    }

    async fn resolve_work_task(
        &self,
        project_id: &str,
        reference: &str,
    ) -> Result<WorkTaskSummary, String> {
        let tasks = self
            .state
            .list_work_tasks(project_id, None, 500)
            .await
            .map_err(|error| format!("Could not load work: {error}"))?;
        let mut matches = tasks
            .into_iter()
            .filter(|task| task.task_id == reference || task.task_id.starts_with(reference))
            .collect::<Vec<_>>();
        match matches.len() {
            0 => Err("Task not found in the active project run.".to_string()),
            1 => Ok(matches.remove(0)),
            _ => Err("Task reference is ambiguous; provide more ID characters.".to_string()),
        }
    }

    async fn render_work_task(&self, task: &WorkTaskSummary) -> String {
        let journal = self
            .state
            .get_task_journal(&task.task_id, 8)
            .await
            .unwrap_or_default();
        let handoff = self
            .state
            .get_latest_task_handoff(&task.task_id)
            .await
            .ok()
            .flatten();
        let workspace = self
            .state
            .get_task_workspace(&task.task_id)
            .await
            .ok()
            .flatten();
        let mut reply = format!(
            "Task {}\n{}\nLane: {} · status: {} · priority: {}\nGoal: {}\nWorker: {}",
            short_id(&task.task_id),
            task.description,
            task.lane,
            task.status,
            task.priority,
            truncate_chars(&task.goal_description, 100),
            task.worker_profile.as_deref().unwrap_or("unassigned")
        );
        if let Some(blocker) = &task.blocker {
            reply.push_str(&format!("\nBlocker: {blocker}"));
        }
        if let Some(handoff) = handoff {
            reply.push_str(&format!("\n\nLatest handoff: {}", handoff.summary));
            if !handoff.verification.is_empty() {
                reply.push_str(&format!("\nVerified: {}", handoff.verification.join("; ")));
            }
            if let Some(risk) = handoff.remaining_risk {
                reply.push_str(&format!("\nRemaining risk: {risk}"));
            }
            if let Some(next) = handoff.next_step {
                reply.push_str(&format!("\nNext step: {next}"));
            }
        }
        if let Some(workspace) = workspace {
            reply.push_str(&format!(
                "\n\nWorkspace: {} ({})",
                workspace.root_path, workspace.status
            ));
        }
        if !journal.is_empty() {
            let lines = journal
                .into_iter()
                .map(|entry| {
                    format!(
                        "• {} · {}: {}",
                        entry.created_at,
                        entry.entry_type,
                        truncate_chars(&entry.body, 120)
                    )
                })
                .collect::<Vec<_>>();
            reply.push_str(&format!("\n\nRecent journal\n{}", lines.join("\n")));
        }
        reply
    }
}

fn short_id(id: &str) -> &str {
    let end = id
        .char_indices()
        .nth(8)
        .map(|(index, _)| index)
        .unwrap_or(id.len());
    &id[..end]
}

use aidaemon_tools::text::truncate_ellipsis as truncate_chars;

fn work_usage() -> String {
    "Usage:\n\
     /work — active overview\n\
     /work ready|running|blocked|attention|done\n\
     /work show <task-id>\n\
     /work comment <task-id> <message>\n\
     /work unblock <task-id> <resolution>\n\
     /work retry|cancel <task-id>\n\
     /work assign <task-id> <profile-id>\n\
     /work workspace <task-id> [shared|isolated|worktree]\n\
     /work profiles|projects\n\
     /work project <id>\n\
     /work project create <name>"
        .to_string()
}