aidaemon-work 0.26.1

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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
use std::collections::HashMap;
use std::sync::Arc;

use async_trait::async_trait;
use serde::Deserialize;
use serde_json::{Value, json};

use crate::traits::{GoalTraceStore, TaskActivity, ToolCapabilities};
use aidaemon_tools::OutcomeTool as Tool;

pub struct GoalTraceTool {
    state: Arc<dyn GoalTraceStore>,
}

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

    async fn resolve_goal_id(&self, input_id: &str) -> anyhow::Result<String> {
        let trimmed = input_id.trim();
        if trimmed.is_empty() {
            anyhow::bail!("Empty goal ID");
        }
        if self.state.get_goal(trimmed).await?.is_some() {
            return Ok(trimmed.to_string());
        }

        if let Some(mandate) = self.state.get_mandate(trimmed).await? {
            return Ok(mandate.goal_id);
        }
        let mandate_matches = self
            .state
            .list_mandates(None, true)
            .await?
            .into_iter()
            .filter(|mandate| {
                trimmed.chars().count() >= super::goal_ids::SHORT_GOAL_ID_CHARS
                    && mandate.id.starts_with(trimmed)
            })
            .collect::<Vec<_>>();
        match mandate_matches.as_slice() {
            [mandate] => return Ok(mandate.goal_id.clone()),
            [] => {}
            _ => {
                let preview = mandate_matches
                    .iter()
                    .take(5)
                    .map(|mandate| format!("{} ({})", Self::short_id(&mandate.id), mandate.status))
                    .collect::<Vec<_>>()
                    .join(", ");
                anyhow::bail!(
                    "Mandate ID prefix '{}' is ambiguous ({} matches): {}. Use the full mandate_id.",
                    trimmed,
                    mandate_matches.len(),
                    preview
                );
            }
        }

        let mut candidates = self.state.get_active_goals().await?;
        for g in self.state.get_scheduled_goals().await? {
            if !candidates.iter().any(|x| x.id == g.id) {
                candidates.push(g);
            }
        }
        super::goal_ids::pick_goal(trimmed, &candidates, "Goal")
    }

    fn short_id(id: &str) -> String {
        super::goal_ids::short_goal_id(id)
    }

    fn format_duration(started_at: Option<&str>, completed_at: Option<&str>) -> String {
        aidaemon_tools::duration_format::compact_elapsed_timestamps(
            started_at,
            completed_at,
            aidaemon_tools::duration_format::ZeroUnitStyle::Keep,
        )
    }

    async fn recent_goal_summary(&self, limit: usize) -> anyhow::Result<String> {
        let mut goals = self.state.get_active_goals().await?;
        for g in self.state.get_scheduled_goals().await? {
            if !goals.iter().any(|existing| existing.id == g.id) {
                goals.push(g);
            }
        }

        goals.sort_by(|a, b| b.updated_at.cmp(&a.updated_at));
        goals.truncate(limit.clamp(1, 20));

        if goals.is_empty() {
            return Ok(
                "No recent goals found. For simple chat recall questions, use conversation history instead of goal_trace."
                    .to_string(),
            );
        }

        let mut out = String::from(
            "**Goal Trace (Recent Goals)**\n\n\
             No `goal_id` was provided, so here are the most recent active/scheduled goals:\n",
        );
        for goal in goals {
            out.push_str(&format!(
                "- {} ({}) updated {}\n  id: {}",
                aidaemon_tools::text::truncate_str(&goal.description, 100),
                goal.status,
                goal.updated_at,
                goal.id
            ));
            out.push('\n');
        }
        out.push_str(
            "\nTip: call `goal_trace` again with `goal_id` (full or unique prefix) for full task/tool timeline details.",
        );
        Ok(out)
    }

    async fn gather_goal_activities(
        &self,
        goal_id: &str,
    ) -> anyhow::Result<Vec<(String, TaskActivity)>> {
        let mut rows = Vec::new();
        let tasks = self.state.get_tasks_for_goal(goal_id).await?;
        for t in &tasks {
            let activities = self.state.get_task_activities(&t.id).await?;
            for a in activities {
                rows.push((t.id.clone(), a));
            }
        }
        rows.sort_by(|a, b| a.1.created_at.cmp(&b.1.created_at));
        Ok(rows)
    }

    async fn goal_trace(
        &self,
        goal_id_input: &str,
        max_tasks: usize,
        max_activities_per_task: usize,
    ) -> anyhow::Result<String> {
        let resolved_goal_id = match self.resolve_goal_id(goal_id_input).await {
            Ok(id) => id,
            Err(e) => return Ok(e.to_string()),
        };
        let Some(goal) = self.state.get_goal(&resolved_goal_id).await? else {
            return Ok(format!("Goal not found: {}", resolved_goal_id));
        };

        let mut tasks = self.state.get_tasks_for_goal(&goal.id).await?;
        tasks.sort_by(|a, b| a.created_at.cmp(&b.created_at));

        let mut status_counts: HashMap<String, usize> = HashMap::new();
        let mut retries_total = 0i32;
        let mut tokens_total = 0i64;
        for t in &tasks {
            *status_counts.entry(t.status.clone()).or_insert(0) += 1;
            retries_total += t.retry_count;
            let activities = self.state.get_task_activities(&t.id).await?;
            tokens_total += activities.iter().filter_map(|a| a.tokens_used).sum::<i64>();
        }

        let mut out = format!(
            "**Goal Trace**\n\n- Goal: {}\n- Goal ID: {}\n- Status: {}\n- Type: {}\n- Created: {}\n- Updated: {}\n- Tasks: {}\n- Retries used: {}\n- Activity tokens (logged): {}\n",
            goal.description,
            goal.id,
            goal.status,
            goal.goal_type,
            goal.created_at,
            goal.updated_at,
            tasks.len(),
            retries_total,
            tokens_total
        );

        if !status_counts.is_empty() {
            let mut keys: Vec<_> = status_counts.keys().cloned().collect();
            keys.sort();
            let status_str = keys
                .iter()
                .map(|k| format!("{} {}", k, status_counts[k]))
                .collect::<Vec<_>>()
                .join(", ");
            out.push_str(&format!("- Task status mix: {}\n", status_str));
        }

        // Shared authoritative lifecycle readout: the same schedule/run/
        // recovery/control answer every other objective surface projects, so
        // an audit consulting the trace lane sees identical typed state.
        let mandate = self.state.get_mandate_for_goal(&goal.id).await?;
        {
            let schedules = self.state.get_schedules_for_goal(&goal.id).await?;
            let runs = self.state.get_goal_runs(&goal.id).await?;
            let recovery = self.state.get_scheduled_recovery_state(&goal.id).await?;
            if !schedules.is_empty() || !runs.is_empty() || recovery.is_some() || mandate.is_some()
            {
                let measurement_count = match mandate.as_ref() {
                    Some(mandate) => self
                        .state
                        .list_mandate_objective_measurements(&mandate.id, 500)
                        .await?
                        .len(),
                    None => 0,
                };
                let enumerated_from = if mandate.is_some() && schedules.is_empty() {
                    crate::tools::objective_status::ObjectiveCollection::MandateControllers
                } else {
                    crate::tools::objective_status::ObjectiveCollection::ScheduledGoals
                };
                let row = crate::tools::objective_status::objective_portfolio_row(
                    &goal.id,
                    enumerated_from,
                    &schedules,
                    &runs,
                    recovery.as_ref(),
                    mandate.as_ref(),
                    measurement_count,
                )?;
                out.push_str(&format!(
                    "- Objective status: {}\n",
                    crate::tools::objective_status::render_objective_status_line(&row.status)
                ));
            }
        }

        if let Some(mandate) = mandate {
            let today = chrono::Utc::now().date_naive().to_string();
            let used_today = if goal.tokens_used_day == today {
                goal.tokens_used_today.max(0)
            } else {
                0
            };
            let remaining = goal
                .budget_daily
                .map(|daily| daily.saturating_sub(used_today).max(0));
            let can_fund_cycle = match (remaining, goal.budget_per_check) {
                (Some(remaining), Some(per_cycle)) => remaining >= per_cycle.max(0),
                _ => true,
            };
            let admission = if can_fund_cycle {
                "ready".to_string()
            } else {
                let reset = chrono::DateTime::<chrono::Utc>::from_naive_utc_and_offset(
                    chrono::Utc::now()
                        .date_naive()
                        .succ_opt()
                        .expect("the next UTC date is representable")
                        .and_hms_opt(0, 0, 0)
                        .expect("UTC midnight is representable"),
                    chrono::Utc,
                );
                format!("budget-blocked until {}", reset.to_rfc3339())
            };
            let latest_run = self.state.get_goal_runs(&goal.id).await?.into_iter().next();
            let latest_decision = self
                .state
                .list_mandate_decisions(&mandate.id, 1)
                .await?
                .into_iter()
                .next();
            out.push_str(&format!(
                "\n**Mandate Controller**\n\n- Mandate: {} [{} v{}]\n- Next review: {}\n- Review admission: {}\n- Token budget: {:?}/cycle, {:?}/UTC day, {} used today, {:?} remaining\n- Latest run: {}\n- Latest explicit decision: {}\n",
                mandate.id,
                mandate.status,
                mandate.version,
                mandate.scheduled_next_review().unwrap_or("none, not active"),
                admission,
                goal.budget_per_check,
                goal.budget_daily,
                used_today,
                remaining,
                latest_run.as_ref().map_or_else(
                    || "none".to_string(),
                    |run| format!("{} ({})", run.id, run.status)
                ),
                latest_decision.as_ref().map_or_else(
                    || "none".to_string(),
                    |decision| format!("{} ({})", decision.outcome.as_str(), decision.created_at)
                ),
            ));
        }

        out.push_str("\n**Task Timeline**");
        for t in tasks.iter().take(max_tasks.clamp(1, 100)) {
            let activities = self.state.get_task_activities(&t.id).await?;
            let mut tool_chain = Vec::new();
            for a in activities.iter().take(max_activities_per_task.clamp(1, 20)) {
                if let Some(tool) = &a.tool_name {
                    let ok = a
                        .success
                        .map(|v| if v { "ok" } else { "err" })
                        .unwrap_or("n/a");
                    tool_chain.push(format!("{}({})", tool, ok));
                }
            }
            let tool_seq = if tool_chain.is_empty() {
                "-".to_string()
            } else {
                tool_chain.join(" -> ")
            };

            out.push_str(&format!(
                "\n- **{}** status={} retry={}/{} created={} duration={} agent={} tools={}",
                t.id,
                t.status,
                t.retry_count,
                t.max_retries,
                t.created_at,
                Self::format_duration(t.started_at.as_deref(), t.completed_at.as_deref()),
                t.agent_id
                    .as_deref()
                    .map(Self::short_id)
                    .unwrap_or_else(|| "-".to_string()),
                tool_seq
            ));

            if t.status == "failed" {
                if let Some(err) = &t.error {
                    out.push_str(&format!(
                        "\n  error: {}",
                        aidaemon_tools::text::truncate_str(err, 180)
                    ));
                }
            } else if t.status == "blocked"
                && let Some(blocker) = &t.blocker
            {
                out.push_str(&format!(
                    "\n  blocker: {}",
                    aidaemon_tools::text::truncate_str(blocker, 180)
                ));
            }
        }

        Ok(out)
    }

    async fn tool_trace(
        &self,
        goal_id_input: Option<&str>,
        task_id: Option<&str>,
        tool_name: Option<&str>,
        limit: usize,
    ) -> anyhow::Result<String> {
        let mut rows: Vec<(String, TaskActivity)> = if let Some(task_id) = task_id {
            let activities = self.state.get_task_activities(task_id).await?;
            activities
                .into_iter()
                .map(|a| (task_id.to_string(), a))
                .collect::<Vec<_>>()
        } else {
            let goal_input = goal_id_input
                .ok_or_else(|| anyhow::anyhow!("'goal_id' or 'task_id' is required"))?;
            let resolved_goal_id = match self.resolve_goal_id(goal_input).await {
                Ok(id) => id,
                Err(e) => return Ok(e.to_string()),
            };
            self.gather_goal_activities(&resolved_goal_id).await?
        };

        if let Some(filter_tool) = tool_name {
            let needle = filter_tool.to_ascii_lowercase();
            rows.retain(|(_, a)| {
                a.tool_name
                    .as_deref()
                    .unwrap_or("")
                    .to_ascii_lowercase()
                    .contains(&needle)
            });
        }

        if rows.is_empty() {
            return Ok("No matching tool activity found.".to_string());
        }

        rows.sort_by(|a, b| b.1.created_at.cmp(&a.1.created_at));
        rows.truncate(limit.clamp(1, 200));

        let mut by_tool: HashMap<String, (usize, usize, usize, i64)> = HashMap::new();
        for (_, a) in &rows {
            let key = a.tool_name.clone().unwrap_or_else(|| "-".to_string());
            let entry = by_tool.entry(key).or_insert((0, 0, 0, 0));
            entry.0 += 1;
            match a.success {
                Some(true) => entry.1 += 1,
                Some(false) => entry.2 += 1,
                None => {}
            }
            entry.3 += a.tokens_used.unwrap_or(0);
        }

        let mut tools: Vec<_> = by_tool.keys().cloned().collect();
        tools.sort();
        let mut out = format!("**Tool Trace**\n\n- Events: {}\n", rows.len());
        if let Some(tn) = tool_name {
            out.push_str(&format!("- Filter tool: {}\n", tn));
        }
        out.push_str("\n**By Tool**");
        for t in &tools {
            let (calls, ok, err, tokens) = by_tool[t];
            out.push_str(&format!(
                "\n- {}: calls {}, ok {}, err {}, tokens {}",
                t, calls, ok, err, tokens
            ));
        }

        out.push_str("\n\n**Recent Events**");
        for (task_id, a) in &rows {
            let tool = a.tool_name.as_deref().unwrap_or("-");
            let ok = a
                .success
                .map(|v| if v { "ok" } else { "err" })
                .unwrap_or("n/a");
            let result = a
                .result
                .as_deref()
                .map(|r| aidaemon_tools::text::truncate_str(r, 120))
                .unwrap_or_default();
            if result.is_empty() {
                out.push_str(&format!(
                    "\n- {} task={} {} tool={} [{}] tokens={}",
                    a.created_at,
                    Self::short_id(task_id),
                    a.activity_type,
                    tool,
                    ok,
                    a.tokens_used.unwrap_or(0)
                ));
            } else {
                out.push_str(&format!(
                    "\n- {} task={} {} tool={} [{}] tokens={} => {}",
                    a.created_at,
                    Self::short_id(task_id),
                    a.activity_type,
                    tool,
                    ok,
                    a.tokens_used.unwrap_or(0),
                    result
                ));
            }
        }

        Ok(out)
    }
}

#[derive(Deserialize)]
struct GoalTraceArgs {
    action: String,
    #[serde(default, alias = "goal_id_v3")]
    goal_id: Option<String>,
    #[serde(default)]
    task_id: Option<String>,
    #[serde(default)]
    tool_name: Option<String>,
    #[serde(default)]
    limit: Option<usize>,
    #[serde(default)]
    max_tasks: Option<usize>,
    #[serde(default)]
    max_activities_per_task: Option<usize>,
}

/// The tool's one description: the schema the model reads and
/// `description()` both use it.
const TOOL_DESCRIPTION: &str =
    "Goal/task traces and tool-call timelines. Omit goal_id for recent-goal list.";

fn goal_trace_schema() -> Value {
    json!({
        "name": "goal_trace",
        "description": TOOL_DESCRIPTION,
        "parameters": {
            "type": "object",
            "properties": {
                "action": {
                    "type": "string",
                    "enum": ["goal_trace", "tool_trace"]
                },
                "goal_id": {
                    "type": "string",
                    "description": "Goal or mandate ID. Required for goal_trace; optional for tool_trace with task_id"
                },
                "task_id": {
                    "type": "string",
                    "description": "Task ID (tool_trace)"
                },
                "tool_name": {
                    "type": "string",
                    "description": "Tool filter (tool_trace)"
                },
                "limit": {
                    "type": "integer",
                    "description": "Max events; default 30, max 200"
                },
                "max_tasks": {
                    "type": "integer",
                    "description": "Max tasks; default 20, max 100"
                },
                "max_activities_per_task": {
                    "type": "integer",
                    "description": "Max per task; default 6, max 20"
                }
            },
            "required": ["action"],
            "additionalProperties": false
        }
    })
}

#[async_trait]
impl Tool for GoalTraceTool {
    fn name(&self) -> &str {
        "goal_trace"
    }

    fn description(&self) -> &str {
        TOOL_DESCRIPTION
    }

    fn schema(&self) -> Value {
        goal_trace_schema()
    }

    async fn execute(
        &self,
        arguments: &str,
        _facts: &crate::traits::ToolCallContext,
    ) -> anyhow::Result<crate::traits::ToolCallOutcome> {
        let output: anyhow::Result<String> = async move {
            let args: GoalTraceArgs = serde_json::from_str(arguments)?;
            match args.action.as_str() {
                "goal_trace" => {
                    if let Some(goal_id) = args.goal_id.as_deref() {
                        self.goal_trace(
                            goal_id,
                            args.max_tasks.unwrap_or(20),
                            args.max_activities_per_task.unwrap_or(6),
                        )
                        .await
                    } else {
                        self.recent_goal_summary(8).await
                    }
                }
                "tool_trace" => {
                    if args.goal_id.is_none() && args.task_id.is_none() {
                        self.recent_goal_summary(8).await
                    } else {
                        self.tool_trace(
                            args.goal_id.as_deref(),
                            args.task_id.as_deref(),
                            args.tool_name.as_deref(),
                            args.limit.unwrap_or(30),
                        )
                        .await
                    }
                }
                other => Ok(format!(
                    "Unknown action: '{}'. Use goal_trace or tool_trace.",
                    other
                )),
            }
        }
        .await;
        output.map(crate::traits::ToolCallOutcome::from_output)
    }
}

impl crate::traits::ToolSemantics for GoalTraceTool {
    fn dispatch_facts(&self) -> aidaemon_tools::semantics::ToolDispatchFacts {
        aidaemon_tools::semantics::ToolDispatchFacts {
            evidence: aidaemon_tools::semantics::EvidenceSource::GoalTrace,
            semantic_scope: Some(aidaemon_tools::semantics::ToolSemanticScope::GoalState),
            ..Default::default()
        }
    }

    /// Goal traces replay past tool results and messages, whoever wrote them.
    fn output_trust(
        &self,
        _arguments: &str,
        _roots: &crate::traits::TrustRoots<'_>,
    ) -> aidaemon_core::SourceTrust {
        aidaemon_core::SourceTrust::Untrusted
    }

    fn capabilities(&self) -> ToolCapabilities {
        ToolCapabilities::READ_ONLY
    }
}