aidaemon-work 0.22.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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
use std::sync::Arc;

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

use crate::traits::{
    HandoffArtifact, ManageGoalTasksStore, Task, TaskAttemptPatch, TaskHandoff, ToolCallContext,
    ToolCallSemantics, ToolCapabilities, ToolMutationEffects, ToolRole,
    semantics_for_exact_read_actions,
};
use aidaemon_tools::OutcomeTool as Tool;

/// Tool for task leads to manage tasks within their assigned goal.
pub struct ManageGoalTasksTool {
    goal_id: String,
    state: Arc<dyn ManageGoalTasksStore>,
    goal_run_id: Option<String>,
}

impl ManageGoalTasksTool {
    pub fn new(goal_id: String, state: Arc<dyn ManageGoalTasksStore>) -> Self {
        Self {
            goal_id,
            state,
            goal_run_id: None,
        }
    }

    pub fn with_goal_run_id(mut self, goal_run_id: Option<String>) -> Self {
        self.goal_run_id = goal_run_id;
        self
    }

    fn task_in_scope(&self, task: &Task) -> bool {
        task.goal_id == self.goal_id
    }

    async fn scoped_tasks(&self) -> anyhow::Result<Vec<Task>> {
        let tasks = if let Some(run_id) = self.goal_run_id.as_deref() {
            self.state.get_tasks_for_goal_run(run_id).await?
        } else {
            self.state.get_tasks_for_goal(&self.goal_id).await?
        };
        let root_task_ids = self
            .state
            .get_goal_runs(&self.goal_id)
            .await?
            .into_iter()
            .filter(|run| {
                self.goal_run_id
                    .as_deref()
                    .is_none_or(|run_id| run.id == run_id)
            })
            .filter_map(|run| run.root_task_id)
            .collect::<std::collections::HashSet<_>>();
        Ok(tasks
            .into_iter()
            .filter(|task| self.task_in_scope(task) && !root_task_ids.contains(&task.id))
            .collect())
    }

    async fn task_id_in_scope(&self, task_id: &str) -> anyhow::Result<bool> {
        Ok(self
            .scoped_tasks()
            .await?
            .iter()
            .any(|task| task.id == task_id))
    }

    fn out_of_scope_message(&self, task_id: &str) -> String {
        format!(
            "Task {task_id} belongs to an earlier run and cannot be changed by the current run. Use manage_goal_tasks(action=\"list_tasks\") for current task IDs."
        )
    }

    async fn task_not_found_message(&self, task_id: &str) -> String {
        let list = self
            .list_tasks()
            .await
            .unwrap_or_else(|_| "(failed to list tasks)".to_string());
        format!(
            "Task not found: {}.\n\n\
             Use manage_goal_tasks(action=\"list_tasks\") to see valid task IDs, then retry with an existing task_id.\n\n\
             {}",
            task_id, list
        )
    }

    /// Resolve a potentially short task ID to the full UUID by prefix-matching
    /// against all tasks in the current goal. Returns the full ID if exactly one
    /// task matches the prefix, otherwise returns the original string unchanged.
    async fn resolve_task_id(&self, task_id: &str) -> String {
        // If it's already a full UUID (36 chars with dashes), skip resolution
        if task_id.len() >= 36 {
            return task_id.to_string();
        }
        // Try exact lookup first
        if let Ok(Some(task)) = self.state.get_task(task_id).await
            && self.task_in_scope(&task)
            && self.task_id_in_scope(task_id).await.unwrap_or(false)
        {
            return task_id.to_string();
        }
        // Prefix-match against all tasks in this goal
        if let Ok(tasks) = self.scoped_tasks().await {
            let matches: Vec<&Task> = tasks.iter().filter(|t| t.id.starts_with(task_id)).collect();
            if matches.len() == 1 {
                return matches[0].id.clone();
            }
        }
        task_id.to_string()
    }

    fn truncate_result_for_tool_output(text: &str, max_chars: usize) -> String {
        let truncated: String = text.chars().take(max_chars).collect();
        if text.chars().count() > max_chars {
            format!("{truncated}...")
        } else {
            truncated
        }
    }

    async fn build_completed_task_result_excerpt(&self) -> anyhow::Result<Option<String>> {
        let tasks = self.scoped_tasks().await?;
        if tasks.is_empty() {
            return Ok(None);
        }

        let mut successful: Vec<&Task> = tasks
            .iter()
            .filter(|t| t.completed_successfully())
            .filter(|t| t.result.as_deref().is_some_and(|r| !r.trim().is_empty()))
            .collect();
        if successful.is_empty() {
            return Ok(None);
        }

        successful.sort_by(|a, b| {
            let a_key = a.completed_at.as_deref().unwrap_or(a.created_at.as_str());
            let b_key = b.completed_at.as_deref().unwrap_or(b.created_at.as_str());
            a_key
                .cmp(b_key)
                .then_with(|| a.task_order.cmp(&b.task_order))
                .then_with(|| a.id.cmp(&b.id))
        });

        let successful_count = successful.len();
        let total_count = tasks.len();
        const MAX_INCLUDED_RESULTS: usize = 3;
        const MAX_RESULT_CHARS_PER_TASK: usize = 700;
        let mut selected: Vec<&Task> = successful
            .iter()
            .rev()
            .take(MAX_INCLUDED_RESULTS)
            .copied()
            .collect();
        selected.reverse();

        let mut excerpt = String::new();
        if successful_count > 1 {
            excerpt.push_str(&format!(
                "{successful_count}/{total_count} tasks completed.\n\n"
            ));
        }
        if selected.len() == 1 {
            let task = selected[0];
            excerpt.push_str("Task result:\n");
            excerpt.push_str(&format!(
                "**{}**\n{}",
                task.description,
                Self::truncate_result_for_tool_output(
                    task.result.as_deref().unwrap_or(""),
                    MAX_RESULT_CHARS_PER_TASK
                )
            ));
        } else {
            excerpt.push_str("Recent task results:\n\n");
            for (idx, task) in selected.iter().enumerate() {
                if idx > 0 {
                    excerpt.push_str("\n\n");
                }
                excerpt.push_str(&format!(
                    "**{}**\n{}",
                    task.description,
                    Self::truncate_result_for_tool_output(
                        task.result.as_deref().unwrap_or(""),
                        MAX_RESULT_CHARS_PER_TASK
                    )
                ));
            }

            let omitted = successful_count.saturating_sub(selected.len());
            if omitted > 0 {
                let suffix = if omitted == 1 { "" } else { "s" };
                excerpt.push_str(&format!(
                    "\n\n(+{} earlier completed task result{} omitted)",
                    omitted, suffix
                ));
            }
        }
        Ok(Some(excerpt))
    }
}

pub use crate::goals::{is_terminal_task_status, tasks_satisfy_goal_completion};

#[derive(Clone, Deserialize)]
struct ManageGoalTasksArgs {
    action: String,
    /// `complete_goal` for a scheduled run: it found nothing changed since
    /// the automation's previous run.
    #[serde(default)]
    nothing_new: Option<bool>,
    #[serde(default)]
    task_id: Option<String>,
    #[serde(default)]
    description: Option<String>,
    #[serde(default)]
    priority: Option<String>,
    #[serde(default)]
    task_order: Option<i32>,
    #[serde(default)]
    parallel_group: Option<String>,
    #[serde(default)]
    depends_on: Option<Vec<String>>,
    #[serde(default)]
    idempotent: Option<bool>,
    #[serde(default)]
    status: Option<String>,
    #[serde(default)]
    result: Option<String>,
    #[serde(default)]
    error: Option<String>,
    #[serde(default)]
    summary: Option<String>,
    #[serde(default)]
    agent_id: Option<String>,
    #[serde(default)]
    worker_profile: Option<String>,
    #[serde(default)]
    workspace_policy: Option<String>,
    #[serde(default)]
    artifacts: Option<Vec<String>>,
    #[serde(default)]
    verification: Option<Vec<String>>,
    #[serde(default)]
    remaining_risk: Option<String>,
    #[serde(default)]
    next_step: Option<String>,
    /// The dispatcher's mandate fence (from the call's facts, never an
    /// argument).
    #[serde(skip)]
    fence_mandate_id: Option<String>,
    #[serde(skip)]
    fence_mandate_version: Option<i64>,
    #[serde(skip)]
    fence_goal_run_id: Option<String>,
    #[serde(skip)]
    fence_task_attempt_id: Option<String>,
}

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

    fn description(&self) -> &str {
        "Manage tasks within your assigned goal. Use create_task to break work into steps, \
         claim_task before spawning an executor, list_tasks to check progress, update_task to record results."
    }

    fn schema(&self) -> Value {
        json!({
            "name": "manage_goal_tasks",
            "description": self.description(),
            "parameters": {
                "type": "object",
                "properties": {
                    "action": {
                        "type": "string",
                        "enum": ["create_task", "list_tasks", "update_task", "claim_task", "retry_task", "resolve_blocker", "complete_goal", "fail_goal"]
                    },
                    "task_id": {
                        "type": "string"
                    },
                    "description": {
                        "type": "string"
                    },
                    "priority": {
                        "type": "string",
                        "enum": ["low", "medium", "high"]
                    },
                    "task_order": {
                        "type": "integer"
                    },
                    "parallel_group": {
                        "type": "string"
                    },
                    "depends_on": {
                        "type": "array",
                        "items": { "type": "string" }
                    },
                    "idempotent": {
                        "type": "boolean"
                    },
                    "status": {
                        "type": "string",
                        "enum": ["pending", "running", "completed", "failed", "blocked", "skipped", "superseded"],
                        "description": "New status (for update_task). Use superseded only when another named task replaced this task's required work."
                    },
                    "result": {
                        "type": "string"
                    },
                    "error": {
                        "type": "string"
                    },
                    "summary": {
                        "type": "string"
                    },
                    "nothing_new": {
                        "type": "boolean",
                        "description": "complete_goal for a scheduled run: true when this run found nothing changed since the automation's previous run, so there is nothing new to tell the owner."
                    },
                    "agent_id": {
                        "type": "string"
                    },
                    "worker_profile": {
                        "type": "string",
                        "description": "Worker profile ID."
                    },
                    "workspace_policy": {
                        "type": "string",
                        "enum": ["shared", "isolated", "worktree"],
                        "description": "shared (the scope given, or the whole workspace for cross-project work), isolated, or Git worktree."
                    },
                    "artifacts": {
                        "type": "array",
                        "items": { "type": "string" },
                        "description": "Produced paths, URLs, commits, or messages."
                    },
                    "verification": {
                        "type": "array",
                        "items": { "type": "string" },
                        "description": "Checks and outcomes."
                    },
                    "remaining_risk": {
                        "type": "string",
                        "description": "Residual risk."
                    },
                    "next_step": {
                        "type": "string",
                        "description": "Next action."
                    }
                },
                "required": ["action"],
                "additionalProperties": false
            }
        })
    }

    async fn execute(
        &self,
        arguments: &str,
        facts: &crate::traits::ToolCallContext,
    ) -> anyhow::Result<crate::traits::ToolCallOutcome> {
        let facts = facts.clone();

        self.run(arguments, &facts)
            .await
            .map(crate::traits::ToolCallOutcome::from_output)
    }
}

impl ManageGoalTasksTool {
    /// Run one call with the dispatcher's `facts`.
    pub(crate) async fn run(
        &self,
        arguments: &str,
        facts: &ToolCallContext,
    ) -> anyhow::Result<String> {
        let mut args: ManageGoalTasksArgs = serde_json::from_str(arguments)?;
        // The mandate fence comes from the dispatcher, never the arguments.
        args.fence_mandate_id = facts.mandate_id.clone();
        args.fence_mandate_version = facts.mandate_version;
        args.fence_goal_run_id = facts.goal_run_id.clone();
        args.fence_task_attempt_id = facts.task_attempt_id.clone();

        let mandate_context = args.fence_mandate_id.is_some()
            || self
                .state
                .get_mandate_for_goal(&self.goal_id)
                .await?
                .is_some();
        if mandate_context
            && !matches!(
                args.action.as_str(),
                "create_task" | "claim_task" | "list_tasks"
            )
        {
            return Ok(format!(
                "Mandate task control rejects '{}': only exact-run create_task/claim_task and read-only list_tasks are supported; executor outcomes are finalized through their own leases.",
                args.action
            ));
        }

        match args.action.as_str() {
            "create_task" => self.create_task(&args).await,
            "list_tasks" => self.list_tasks().await,
            "update_task" => self.update_task(&args).await,
            "claim_task" => self.claim_task(&args).await,
            "retry_task" => self.retry_task(&args).await,
            "resolve_blocker" => self.resolve_blocker(&args).await,
            "complete_goal" => self.complete_goal(&args).await,
            "fail_goal" => self.fail_goal(&args).await,
            other => Ok(format!(
                "Unknown action: {}. Use: create_task, list_tasks, update_task, claim_task, retry_task, resolve_blocker, complete_goal, fail_goal",
                other
            )),
        }
    }
}

impl crate::traits::ToolSemantics for ManageGoalTasksTool {
    fn dispatch_facts(&self) -> crate::traits::ToolDispatchFacts {
        crate::traits::ToolDispatchFacts {
            offering: aidaemon_tools::semantics::Offering::Scoped,
            task_lead_delegation: true,
            same_tool_streak: crate::traits::SameToolStreak::Versatile,
            goal_context: crate::traits::GoalContext::MandateRun,
            evidence: aidaemon_tools::semantics::EvidenceSource::GoalRecords,
            semantic_scope: Some(aidaemon_tools::semantics::ToolSemanticScope::GoalState),
            ..Default::default()
        }
    }

    fn tool_role(&self) -> ToolRole {
        ToolRole::Management
    }

    fn call_semantics(&self, arguments: &str) -> ToolCallSemantics {
        semantics_for_exact_read_actions(arguments, &["list_tasks"], ToolMutationEffects::NONE)
    }

    fn capabilities(&self) -> ToolCapabilities {
        ToolCapabilities {
            read_only: false,
            external_side_effect: false,
            needs_approval: false,
            idempotent: false,
            high_impact_write: false,
        }
    }

    /// `list_tasks`, `claim_task` and an `update_task` that does not put a
    /// task back to `pending` only write the daemon's own task records.
    /// Creating, retrying or unblocking a task queues work, and completing
    /// or failing a goal can alert the owner, so those stay undeclared.
    fn call_effects(&self, arguments: &str) -> aidaemon_core::CallEffects {
        let args = serde_json::from_str::<Value>(arguments).ok();
        let field = |key: &str| {
            args.as_ref()
                .and_then(|args| args.get(key))
                .and_then(Value::as_str)
        };
        let internal = match field("action") {
            Some("list_tasks" | "claim_task") => true,
            Some("update_task") => field("status") != Some("pending"),
            _ => false,
        };
        if internal {
            aidaemon_core::CallEffects::none()
        } else {
            aidaemon_core::CallEffects::unknown()
        }
    }
}

/// Check if all dependencies for a task are completed. Returns Ok(()) if all are met,
/// or an error message string describing which dependencies are unmet.
async fn check_dependencies_met(
    state: &dyn ManageGoalTasksStore,
    task: &Task,
) -> Result<(), String> {
    let tasks = state
        .get_tasks_for_goal(&task.goal_id)
        .await
        .map_err(|error| format!("could not read task dependency graph: {error}"))?;
    let graph = crate::traits::task_execution_graph(&tasks)?;
    if let Some(dependency) = graph.unresolved_dependencies(&task.id).first() {
        return Err(format!(
            "dependency {} is not completed or otherwise accepted (state: {:?})",
            dependency.id, dependency.state
        ));
    }
    Ok(())
}

/// Compress older task result entries to save space.
/// Keeps last 10 entries in full detail, compresses older ones to one-line summaries.
fn compress_old_entries(entries: &mut [Value]) {
    if entries.len() <= 10 {
        return;
    }
    let keep_full = entries.len() - 10;
    for entry in entries.iter_mut().take(keep_full) {
        if let Some(obj) = entry.as_object() {
            let task_id = obj.get("task_id").and_then(|v| v.as_str()).unwrap_or("?");
            let desc = obj
                .get("description")
                .and_then(|v| v.as_str())
                .unwrap_or("?");
            let short_id = &task_id[..task_id.len().min(8)];
            *entry = json!(format!("{}: {} (completed)", short_id, desc));
        }
    }
}

/// Validate that adding a new task with the given dependencies won't create a cycle.
/// Uses the shared typed execution graph so scheduling and validation enforce
/// the same dependency and cycle invariants.
fn validate_no_cycles(
    existing: &[Task],
    new_task_id: &str,
    new_deps: &[String],
) -> Result<(), String> {
    let Some(template) = existing.first() else {
        return if new_deps.is_empty() {
            Ok(())
        } else {
            Err("Dependencies cannot be added before their tasks exist".to_string())
        };
    };
    let mut tasks = existing.to_vec();
    let mut candidate = template.clone();
    candidate.id = new_task_id.to_string();
    candidate.status = "pending".to_string();
    candidate.depends_on = Some(
        serde_json::to_string(new_deps)
            .map_err(|error| format!("Could not encode dependencies: {error}"))?,
    );
    tasks.push(candidate);
    crate::traits::task_execution_graph(&tasks).map(|_| ())
}

impl ManageGoalTasksTool {
    async fn create_task(&self, args: &ManageGoalTasksArgs) -> anyhow::Result<String> {
        let persisted_mandate = self.state.get_mandate_for_goal(&self.goal_id).await?;
        let mandate_context = args.fence_mandate_id.is_some() || persisted_mandate.is_some();
        let mandate_fence = if mandate_context {
            let mandate_id = args.fence_mandate_id.as_deref().ok_or_else(|| {
                anyhow::anyhow!("mandate task creation is missing its dispatcher-owned mandate id")
            })?;
            let mandate_version = args.fence_mandate_version.ok_or_else(|| {
                anyhow::anyhow!(
                    "mandate task creation is missing its dispatcher-owned policy version"
                )
            })?;
            let goal_run_id = args.fence_goal_run_id.as_deref().ok_or_else(|| {
                anyhow::anyhow!("mandate task creation is missing its dispatcher-owned goal run")
            })?;
            let task_attempt_id = args.fence_task_attempt_id.as_deref().ok_or_else(|| {
                anyhow::anyhow!(
                    "mandate task creation is missing its dispatcher-owned task attempt"
                )
            })?;
            anyhow::ensure!(
                self.goal_run_id.as_deref() == Some(goal_run_id),
                "mandate task creation run does not match the task-lead binding"
            );
            if let Some(mandate) = persisted_mandate.as_ref() {
                anyhow::ensure!(
                    mandate.id == mandate_id && mandate.version == mandate_version,
                    "mandate task creation policy binding is stale"
                );
            }
            Some((mandate_id, mandate_version, goal_run_id, task_attempt_id))
        } else {
            None
        };
        // Mandate work placement is runtime-owned: the fenced dispatcher picks
        // the worker and workspace. The schema still offers these hints, so a
        // task lead that fills them in must not have its committed ACT
        // rejected; the hints are dropped instead of honored.
        let without_hints;
        let args = if mandate_fence.is_some() {
            without_hints = ManageGoalTasksArgs {
                workspace_policy: None,
                worker_profile: None,
                ..args.clone()
            };
            &without_hints
        } else {
            args
        };

        let description = args
            .description
            .as_deref()
            .ok_or_else(|| anyhow::anyhow!("create_task requires 'description'"))?;
        if let Some(profile_id) = args.worker_profile.as_deref()
            && self.state.get_worker_profile(profile_id).await?.is_none()
        {
            return Ok(format!(
                "Cannot create task: worker profile {profile_id} not found"
            ));
        }
        if let Some(policy) = args.workspace_policy.as_deref() {
            anyhow::ensure!(
                matches!(policy, "shared" | "isolated" | "worktree"),
                "workspace_policy must be shared, isolated, or worktree"
            );
        }

        let now = chrono::Utc::now().to_rfc3339();
        let task_id = uuid::Uuid::new_v4().to_string();

        // Validate dependencies don't create cycles
        if let Some(ref dep_ids) = args.depends_on
            && !dep_ids.is_empty()
        {
            let existing = self.scoped_tasks().await?;
            if let Err(reason) = validate_no_cycles(&existing, &task_id, dep_ids) {
                return Ok(format!("Cannot create task: {}", reason));
            }
        }

        let task = Task {
            id: task_id,
            goal_id: self.goal_id.clone(),
            description: description.to_string(),
            status: "pending".to_string(),
            priority: args
                .priority
                .clone()
                .unwrap_or_else(|| "medium".to_string()),
            task_order: args.task_order.unwrap_or(0),
            parallel_group: args.parallel_group.clone(),
            depends_on: args
                .depends_on
                .as_ref()
                .map(|v| serde_json::to_string(v).unwrap_or_default()),
            agent_id: None,
            context: None,
            result: None,
            error: None,
            blocker: None,
            idempotent: if mandate_fence.is_some() {
                false
            } else {
                args.idempotent.unwrap_or(false)
            },
            retry_count: 0,
            max_retries: if mandate_fence.is_some() { 0 } else { 3 },
            created_at: now,
            started_at: None,
            completed_at: None,
        };

        if let Some((mandate_id, mandate_version, goal_run_id, task_attempt_id)) = mandate_fence {
            const MAX_MANDATE_ACTION_TASKS_PER_RUN: i64 = 16;
            if !self
                .state
                .create_mandate_task_from_attempt(
                    &task,
                    mandate_id,
                    mandate_version,
                    goal_run_id,
                    task_attempt_id,
                    MAX_MANDATE_ACTION_TASKS_PER_RUN,
                )
                .await?
            {
                return Ok(
                    "Cannot create mandate task: authority/run/attempt changed or the per-run task cap was reached. Reconsider in a fresh cycle."
                        .to_string(),
                );
            }
        } else {
            self.state.create_task(&task).await?;
        }
        if mandate_fence.is_none() {
            if let Some(profile_id) = args.worker_profile.as_deref()
                && !self
                    .state
                    .assign_task_worker_profile(&task.id, profile_id, "task-lead", None)
                    .await?
            {
                anyhow::bail!("worker profile '{}' is unavailable", profile_id);
            }
            if let Some(policy) = args.workspace_policy.as_deref()
                && !self
                    .state
                    .set_task_workspace_policy(&task.id, policy)
                    .await?
            {
                anyhow::bail!("workspace policy could not be applied to the new task");
            }
        }
        info!(goal_id = %self.goal_id, task_id = %task.id, "Created task");

        Ok(format!(
            "Created task {} (order: {}, priority: {}): {}",
            task.id, task.task_order, task.priority, task.description
        ))
    }

    async fn list_tasks(&self) -> anyhow::Result<String> {
        let tasks = self.scoped_tasks().await?;

        if tasks.is_empty() {
            return Ok(format!("No tasks for goal {}", self.goal_id));
        }

        let mut output = format!("Tasks for goal {} ({} total):\n", self.goal_id, tasks.len());
        for task in &tasks {
            let short_id = &task.id[..8.min(task.id.len())];

            // Build detail parts
            let mut details = vec![
                format!("order: {}", task.task_order),
                format!("status: {}", task.status),
            ];
            if let Some(ref pg) = task.parallel_group {
                details.push(format!("group: {}", pg));
            }
            if let Some(ref deps) = task.depends_on
                && let Ok(dep_ids) = serde_json::from_str::<Vec<String>>(deps)
                && !dep_ids.is_empty()
            {
                let short_deps: Vec<String> = dep_ids
                    .iter()
                    .map(|d| d[..8.min(d.len())].to_string())
                    .collect();
                details.push(format!("deps: [{}]", short_deps.join(", ")));
            }
            if let Some(ref aid) = task.agent_id {
                details.push(format!("agent: {}", aid));
            }
            if task.idempotent && task.max_retries > 0 {
                details.push(format!(
                    "retries: {}/{}",
                    task.retry_count, task.max_retries
                ));
            }
            if let Ok(journal) = self.state.get_task_journal(&task.id, 12).await
                && let Some(entry) = journal.iter().find(|entry| entry.actor_type == "human")
            {
                let end = aidaemon_tools::text::floor_char_boundary(&entry.body, 120);
                details.push(format!(
                    "latest human {}: {}",
                    entry.entry_type,
                    &entry.body[..end]
                ));
            }

            let result_suffix = task
                .result
                .as_deref()
                .map(|r| {
                    let end = aidaemon_tools::text::floor_char_boundary(r, 200);
                    format!(" → {}", &r[..end])
                })
                .unwrap_or_default();

            output.push_str(&format!(
                "- [{}] {} ({}){}\n",
                short_id,
                task.description,
                details.join(", "),
                result_suffix,
            ));
        }

        Ok(output)
    }

    async fn update_task(&self, args: &ManageGoalTasksArgs) -> anyhow::Result<String> {
        let raw_task_id = args
            .task_id
            .as_deref()
            .ok_or_else(|| anyhow::anyhow!("update_task requires 'task_id'"))?;
        let task_id = self.resolve_task_id(raw_task_id).await;
        let task_id = task_id.as_str();

        let Some(mut task) = self.state.get_task(task_id).await? else {
            return Ok(self.task_not_found_message(task_id).await);
        };
        let original_status = task.status.clone();

        if task.goal_id != self.goal_id {
            anyhow::bail!("Task {} does not belong to goal {}", task_id, self.goal_id);
        }
        if !self.task_id_in_scope(task_id).await? {
            return Ok(self.out_of_scope_message(task_id));
        }

        if original_status == "blocked"
            && args
                .status
                .as_deref()
                .is_some_and(|status| status != "blocked")
        {
            return Ok(format!(
                "Blocked: task {} is waiting on a recorded blocker and cannot transition directly to another status. Use resolve_blocker with the concrete resolution, or retry_task, before completing it.",
                task_id
            ));
        }

        // Dependency enforcement: prevent moving to "running" or "claimed" if deps unmet
        if let Some(ref new_status) = args.status
            && (new_status == "running" || new_status == "claimed")
            && let Err(reason) = check_dependencies_met(self.state.as_ref(), &task).await
        {
            return Ok(format!(
                "Cannot set task {} to {}: {}",
                task_id, new_status, reason
            ));
        }

        if let Some(status) = &args.status {
            task.status = status.clone();
            if matches!(
                status.as_str(),
                "completed" | "failed" | "blocked" | "skipped" | "superseded"
            ) {
                task.completed_at = Some(chrono::Utc::now().to_rfc3339());
            }
            if status == "running" {
                task.started_at = Some(chrono::Utc::now().to_rfc3339());
            }
        }
        if let Some(result) = &args.result {
            task.result = Some(result.clone());
        }
        // A completed task's outcome is success. Error text left by a prior
        // attempt (e.g. a failed specialist handoff the lead then finished
        // directly), or supplied alongside the completion, is attempt history
        // rather than an open failure. Keeping it in `error` made the task
        // read as "completed but failed" and vetoed the whole run, so it is
        // carried as handoff risk instead.
        let attempt_history = if task.status == "completed" {
            let note = args
                .error
                .clone()
                .or_else(|| task.error.take())
                .filter(|note| !note.trim().is_empty());
            task.error = None;
            note
        } else {
            if let Some(error) = &args.error {
                task.error = (!error.trim().is_empty()).then(|| error.clone());
            }
            None
        };
        let remaining_risk = match (args.remaining_risk.clone(), attempt_history) {
            (Some(risk), Some(note)) => Some(format!("{risk}\nPrior attempt: {note}")),
            (Some(risk), None) => Some(risk),
            (None, Some(note)) => Some(format!("Prior attempt: {note}")),
            (None, None) => None,
        };

        let fenced_status = matches!(
            task.status.as_str(),
            "running" | "completed" | "failed" | "blocked" | "cancelled"
        );
        let mut attempt = self.state.get_current_task_attempt(task_id).await?;
        if attempt.is_none() && original_status == "pending" && fenced_status {
            attempt = self
                .state
                .claim_task_with_lease(
                    task_id,
                    args.agent_id.as_deref().unwrap_or("task-lead"),
                    args.worker_profile.as_deref().or(Some("profile-task-lead")),
                    180,
                )
                .await?;
        }
        if let Some(attempt) = attempt {
            if !fenced_status {
                return Ok(
                    "A claimed task cannot be skipped or superseded by an unfenced update; finish or cancel its current attempt first."
                        .to_string(),
                );
            }
            let terminal = matches!(
                task.status.as_str(),
                "completed" | "failed" | "blocked" | "cancelled"
            );
            let handoff = terminal.then(|| TaskHandoff {
                id: uuid::Uuid::new_v4().to_string(),
                task_id: task_id.to_string(),
                attempt_id: attempt.id.clone(),
                summary: args
                    .summary
                    .clone()
                    .or_else(|| args.result.clone())
                    .or_else(|| args.error.clone())
                    .unwrap_or_else(|| format!("Task {}", task.status)),
                artifacts: args
                    .artifacts
                    .clone()
                    .unwrap_or_default()
                    .into_iter()
                    .map(|reference| HandoffArtifact {
                        kind: if reference.starts_with("http://")
                            || reference.starts_with("https://")
                        {
                            "url".to_string()
                        } else {
                            "path".to_string()
                        },
                        reference,
                        digest: None,
                        metadata: None,
                    })
                    .collect(),
                verification: args.verification.clone().unwrap_or_default(),
                remaining_risk: remaining_risk.clone(),
                next_step: args.next_step.clone(),
                created_at: chrono::Utc::now().to_rfc3339(),
            });
            let patch = TaskAttemptPatch {
                status: task.status.clone(),
                result: task.result.clone(),
                error: task.error.clone(),
                blocker: (task.status == "blocked").then(|| {
                    args.error
                        .clone()
                        .or_else(|| args.remaining_risk.clone())
                        .or_else(|| args.result.clone())
                        .unwrap_or_else(|| "Task is blocked.".to_string())
                }),
                context: task.context.clone(),
                handoff,
            };
            if !self
                .state
                .patch_task_from_attempt(&attempt.id, &attempt.lease_token, &patch)
                .await?
            {
                return Ok(
                    "Update rejected because the task lease is no longer current.".to_string(),
                );
            }
            task = self
                .state
                .get_task(task_id)
                .await?
                .ok_or_else(|| anyhow::anyhow!("Task disappeared after update"))?;
        } else {
            self.state.update_task(&task).await?;
        }
        info!(goal_id = %self.goal_id, task_id, status = %task.status, "Updated task");

        // Accumulate context when a task completes with a result
        if task.status == "completed"
            && task.result.is_some()
            && let Err(e) = self.accumulate_goal_context(&task).await
        {
            tracing::warn!(goal_id = %self.goal_id, "Failed to accumulate goal context: {}", e);
        }

        Ok(format!(
            "Updated task {} → status: {}",
            task_id, task.status
        ))
    }

    async fn claim_task(&self, args: &ManageGoalTasksArgs) -> anyhow::Result<String> {
        let raw_task_id = args
            .task_id
            .as_deref()
            .ok_or_else(|| anyhow::anyhow!("claim_task requires 'task_id'"))?;
        let task_id = self.resolve_task_id(raw_task_id).await;
        let task_id = task_id.as_str();

        let Some(task) = self.state.get_task(task_id).await? else {
            return Ok(self.task_not_found_message(task_id).await);
        };

        if task.goal_id != self.goal_id {
            anyhow::bail!("Task {} does not belong to goal {}", task_id, self.goal_id);
        }
        if !self.task_id_in_scope(task_id).await? {
            return Ok(self.out_of_scope_message(task_id));
        }

        // Give the task lead a precise explanation while retaining the same
        // dependency check inside the atomic lease claim for race safety.
        if let Err(reason) = check_dependencies_met(self.state.as_ref(), &task).await {
            return Ok(format!("Cannot claim task {}: {}", task_id, reason));
        }

        let agent_id = args.agent_id.as_deref().unwrap_or("executor");
        if args.fence_mandate_id.is_some()
            || self
                .state
                .get_mandate_for_goal(&self.goal_id)
                .await?
                .is_some()
        {
            let mandate_id = args.fence_mandate_id.as_deref().ok_or_else(|| {
                anyhow::anyhow!("mandate task claim is missing its dispatcher-owned mandate id")
            })?;
            let mandate_version = args.fence_mandate_version.ok_or_else(|| {
                anyhow::anyhow!("mandate task claim is missing its dispatcher-owned policy version")
            })?;
            let goal_run_id = args.fence_goal_run_id.as_deref().ok_or_else(|| {
                anyhow::anyhow!("mandate task claim is missing its dispatcher-owned goal run")
            })?;
            let root_attempt_id = args.fence_task_attempt_id.as_deref().ok_or_else(|| {
                anyhow::anyhow!("mandate task claim is missing its dispatcher-owned root attempt")
            })?;
            anyhow::ensure!(
                self.goal_run_id.as_deref() == Some(goal_run_id),
                "mandate task claim run does not match the task-lead binding"
            );
            // worker_profile is a runtime-owned placement hint here; the
            // claim below never binds one, so a supplied value is ignored.
            return match self
                .state
                .claim_mandate_task_from_attempt(
                    task_id,
                    agent_id,
                    mandate_id,
                    mandate_version,
                    goal_run_id,
                    root_attempt_id,
                    180,
                )
                .await?
            {
                Some(attempt) => Ok(format!(
                    "Claimed mandate task {} for agent {} with attempt {} (no worker-profile or workspace binding)",
                    task_id, agent_id, attempt.id
                )),
                None => Ok(format!(
                    "Failed to claim mandate task {} — authority/run/attempt changed, dependencies are unmet, or another worker owns it",
                    task_id
                )),
            };
        }
        match self
            .state
            .claim_task_with_lease(task_id, agent_id, args.worker_profile.as_deref(), 180)
            .await?
        {
            Some(attempt) => {
                info!(
                    goal_id = %self.goal_id,
                    task_id,
                    agent_id,
                    attempt_id = %attempt.id,
                    "Claimed task"
                );
                Ok(format!(
                    "Claimed task {} for agent {} with attempt {} and profile {}",
                    task_id,
                    agent_id,
                    attempt.id,
                    attempt.worker_profile_id.as_deref().unwrap_or("default")
                ))
            }
            None => Ok(format!(
                "Failed to claim task {} — dependencies may be unmet, the profile may be at capacity, or another worker owns it",
                task_id
            )),
        }
    }

    async fn retry_task(&self, args: &ManageGoalTasksArgs) -> anyhow::Result<String> {
        let raw_task_id = args
            .task_id
            .as_deref()
            .ok_or_else(|| anyhow::anyhow!("retry_task requires 'task_id'"))?;
        let task_id = self.resolve_task_id(raw_task_id).await;
        let task_id = task_id.as_str();

        let Some(task) = self.state.get_task(task_id).await? else {
            return Ok(self.task_not_found_message(task_id).await);
        };

        if task.goal_id != self.goal_id {
            anyhow::bail!("Task {} does not belong to goal {}", task_id, self.goal_id);
        }
        if !self.task_id_in_scope(task_id).await? {
            return Ok(self.out_of_scope_message(task_id));
        }
        if task.status != "failed" && task.status != "blocked" {
            return Ok(format!(
                "Cannot retry task {} — status is '{}'",
                task_id, task.status
            ));
        }
        if !task.idempotent {
            return Ok(format!(
                "Cannot retry task {} — not marked as idempotent",
                task_id
            ));
        }
        if task.retry_count >= task.max_retries {
            return Ok(format!(
                "Cannot retry task {} — max retries reached ({}/{})",
                task_id, task.retry_count, task.max_retries
            ));
        }

        if !self
            .state
            .retry_work_task(task_id, "task-lead", None)
            .await?
        {
            return Ok(format!(
                "Cannot retry task {} — it may still have a live attempt",
                task_id
            ));
        }
        let retry_count = task.retry_count + 1;

        info!(
            goal_id = %self.goal_id,
            task_id,
            retry_count,
            max_retries = task.max_retries,
            "Retried task"
        );

        Ok(format!(
            "Task {} reset to pending for retry ({}/{})",
            task_id, retry_count, task.max_retries
        ))
    }

    async fn complete_goal(&self, args: &ManageGoalTasksArgs) -> anyhow::Result<String> {
        let summary = args
            .summary
            .as_deref()
            .unwrap_or("Goal completed successfully");

        let tasks = self.scoped_tasks().await?;
        if tasks.is_empty() {
            return Ok(
                "Blocked: do not call manage_goal_tasks(action=\"complete_goal\") before creating and completing concrete tasks for this goal. Create the task plan first, then complete the goal only after the task list is actually done."
                    .to_string(),
            );
        }
        let mut goal = self
            .state
            .get_goal(&self.goal_id)
            .await?
            .ok_or_else(|| anyhow::anyhow!("Goal not found: {}", self.goal_id))?;

        // Identify genuinely active work first. A dead task never transitions to
        // completed on its own; strict success handling for finite/current runs
        // follows below, while legacy recurring calls may ignore historical dead
        // rows so one old run cannot wedge every future schedule fire.
        // (Observed live on the daily-tweet goal 9a744834: watchdog-interrupted
        // tasks from prior fires blocked complete_goal, so every new fire looped
        // trying to "resolve" them, got interrupted in turn, and piled up 178 dead
        // tasks.) This mirrors heartbeat's own `terminal_failed` classification.
        if let Some(task) = tasks
            .iter()
            .find(|task| !is_terminal_task_status(&task.status))
        {
            return Ok(format!(
                "Blocked: do not call manage_goal_tasks(action=\"complete_goal\") while tasks are still incomplete. '{}' is still {}. Finish or explicitly resolve every active task first, or use fail_goal if the goal cannot be completed.",
                task.description, task.status
            ));
        }

        // A finite goal, and every explicitly scoped run, may complete only
        // from successful terminal task state. Failed/blocked/interrupted work
        // is terminal for scheduling purposes but is not completion evidence.
        // Legacy recurring calls without a run id retain their historical-task
        // tolerance so an old dead task cannot wedge every future schedule fire.
        let strict_run_completion = goal.goal_type == "finite" || self.goal_run_id.is_some();
        if strict_run_completion && !tasks_satisfy_goal_completion(&tasks) {
            let task = tasks
                .iter()
                .find(|task| !task.satisfies_run_completion())
                .expect("an unsatisfied task must exist");
            return Ok(format!(
                "Blocked: goal completion requires successful task state. '{}' ended as {}. Retry or replace that task successfully, or use fail_goal.",
                task.description, task.status
            ));
        }

        // A continuous (recurring) goal is open-ended: a successful run does not
        // complete the goal. Keep it active for the next cycle and clear the
        // failure streak. Only finite goals are marked completed.
        let is_continuous = goal.goal_type == "continuous";
        if is_continuous {
            goal.dispatch_failures = 0;
            if let Some(raw_context) = goal.context.as_deref()
                && let Ok(mut context) = serde_json::from_str::<Value>(raw_context)
                && let Some(object) = context.as_object_mut()
            {
                object.remove("failure_summary");
                goal.context = if object.is_empty() {
                    None
                } else {
                    Some(context.to_string())
                };
            }
        } else {
            goal.status = "completed".to_string();
            goal.completed_at = Some(chrono::Utc::now().to_rfc3339());
        }
        goal.updated_at = chrono::Utc::now().to_rfc3339();

        // Finish the run before touching the goal: if the store refuses the
        // run, the goal must not already claim a successful cycle.
        let run_id = match self.goal_run_id.clone() {
            Some(run_id) => Some(run_id),
            None => self
                .state
                .get_current_goal_run(&self.goal_id)
                .await?
                .map(|run| run.id),
        };
        if let Some(run_id) = run_id.as_deref() {
            let _ = self
                .state
                .finish_goal_run(run_id, "completed", Some(summary))
                .await?;
        }
        // "Nothing new" is this run's typed verdict, kept with the run it
        // belongs to: an automation set to tell only changes stays quiet.
        if is_continuous
            && args.nothing_new == Some(true)
            && let Some(run_id) = run_id.as_deref()
        {
            let mut context = goal
                .context
                .as_deref()
                .and_then(|raw| serde_json::from_str::<Value>(raw).ok())
                .filter(Value::is_object)
                .unwrap_or_else(|| serde_json::json!({}));
            context[crate::automations::NOTHING_NEW_RUN_KEY] = Value::String(run_id.to_string());
            goal.context = Some(context.to_string());
        }
        self.state.update_goal(&goal).await?;
        info!(
            goal_id = %self.goal_id,
            is_continuous,
            nothing_new = args.nothing_new.unwrap_or(false),
            "Goal run completed"
        );

        let mut response = if is_continuous {
            format!(
                "Recurring goal {} run completed (goal stays active for the next cycle): {}",
                self.goal_id, summary
            )
        } else {
            format!("Goal {} completed: {}", self.goal_id, summary)
        };
        if let Some(excerpt) = self.build_completed_task_result_excerpt().await? {
            response.push_str("\n\n");
            response.push_str(&excerpt);
        }
        Ok(response)
    }

    /// Append a completed task's summary to the goal's context JSON,
    /// so later executors and the task lead can see what was accomplished.
    async fn accumulate_goal_context(&self, task: &Task) -> anyhow::Result<()> {
        let mut goal = self
            .state
            .get_goal(&self.goal_id)
            .await?
            .ok_or_else(|| anyhow::anyhow!("Goal not found"))?;

        // Parse existing context or create new
        let mut ctx: serde_json::Value = goal
            .context
            .as_deref()
            .and_then(|s| serde_json::from_str(s).ok())
            .unwrap_or_else(|| json!({"task_results": []}));

        let result_summary = task
            .result
            .as_deref()
            .map(|r| {
                let end = aidaemon_tools::text::floor_char_boundary(r, 500);
                &r[..end]
            })
            .unwrap_or("");

        let entry = json!({
            "task_id": task.id,
            "description": task.description,
            "result_summary": result_summary,
            "completed_at": task.completed_at,
        });

        if let Some(arr) = ctx.get_mut("task_results").and_then(|v| v.as_array_mut()) {
            arr.push(entry);
        }

        // Compress if context > 32KB: older entries get one-line summaries
        let serialized = serde_json::to_string(&ctx)?;
        if serialized.len() > 32_000
            && let Some(arr) = ctx.get_mut("task_results").and_then(|v| v.as_array_mut())
        {
            compress_old_entries(arr);
        }

        goal.context = Some(serde_json::to_string(&ctx)?);
        goal.updated_at = chrono::Utc::now().to_rfc3339();
        self.state.update_goal(&goal).await?;
        Ok(())
    }

    async fn resolve_blocker(&self, args: &ManageGoalTasksArgs) -> anyhow::Result<String> {
        let raw_task_id = args
            .task_id
            .as_deref()
            .ok_or_else(|| anyhow::anyhow!("resolve_blocker requires 'task_id'"))?;
        let task_id = self.resolve_task_id(raw_task_id).await;
        let task_id = task_id.as_str();

        let Some(task) = self.state.get_task(task_id).await? else {
            return Ok(self.task_not_found_message(task_id).await);
        };

        if task.goal_id != self.goal_id {
            anyhow::bail!("Task {} does not belong to goal {}", task_id, self.goal_id);
        }
        if !self.task_id_in_scope(task_id).await? {
            return Ok(self.out_of_scope_message(task_id));
        }
        if task.status != "blocked" {
            return Ok(format!(
                "Task {} is not blocked (status: {})",
                task_id, task.status
            ));
        }

        let Some(resolution) = args
            .result
            .as_deref()
            .or(args.summary.as_deref())
            .or(args.error.as_deref())
            .filter(|value| !value.trim().is_empty())
        else {
            return Ok(
                "resolve_blocker requires the concrete unblock message in result or summary."
                    .to_string(),
            );
        };
        if !self
            .state
            .unblock_task(task_id, resolution, "task-lead", None)
            .await?
        {
            return Ok(format!("Task {} could not be unblocked.", task_id));
        }
        info!(goal_id = %self.goal_id, task_id, "Blocker resolved; task reset to pending");

        Ok(format!(
            "Blocker resolved for task {}. Task reset to pending.",
            task_id
        ))
    }

    async fn fail_goal(&self, args: &ManageGoalTasksArgs) -> anyhow::Result<String> {
        let mut goal = self
            .state
            .get_goal(&self.goal_id)
            .await?
            .ok_or_else(|| anyhow::anyhow!("Goal not found: {}", self.goal_id))?;
        let summary = args.summary.as_deref().unwrap_or("Goal failed");

        let mut context = goal
            .context
            .as_deref()
            .and_then(|ctx| serde_json::from_str::<Value>(ctx).ok())
            .unwrap_or_else(|| json!({}));
        if !context.is_object() {
            context = json!({
                "prior_context_raw": goal.context.clone().unwrap_or_default(),
            });
        }
        if let Some(obj) = context.as_object_mut() {
            obj.insert("failure_summary".to_string(), json!(summary));
        }

        // A continuous (recurring) goal is never terminally failed by a single
        // run: keep it active so its schedule retries next cycle, and record the
        // failure (dispatch_failures) for repeated-failure detection. Only finite
        // goals are marked "failed".
        let is_continuous = goal.goal_type == "continuous";
        if is_continuous {
            goal.dispatch_failures += 1;
        } else {
            goal.status = "failed".to_string();
        }
        goal.context = Some(context.to_string());
        goal.updated_at = chrono::Utc::now().to_rfc3339();

        self.state.update_goal(&goal).await?;
        if is_continuous {
            info!(
                goal_id = %self.goal_id,
                failures = goal.dispatch_failures,
                "Continuous goal run failed; goal kept active for next cycle"
            );
        } else {
            info!(goal_id = %self.goal_id, "Goal failed");
        }

        // Surface a recurring goal that keeps failing, so it doesn't retry
        // forever in silence. Alert on every Nth consecutive failure (the streak
        // is cleared by a successful run in complete_goal).
        // A scheduled run's failures are counted when its run closes, and
        // that pauses the schedule with its own notice: a second alert saying
        // it "will keep retrying" would contradict it.
        const REPEATED_FAILURE_ALERT_THRESHOLD: i32 = 3;
        let scheduled_run = self
            .state
            .get_current_goal_run(&self.goal_id)
            .await
            .ok()
            .flatten()
            .is_some_and(|run| run.trigger_type == "scheduled");
        if is_continuous
            && !scheduled_run
            && goal.dispatch_failures >= REPEATED_FAILURE_ALERT_THRESHOLD
            && goal.dispatch_failures % REPEATED_FAILURE_ALERT_THRESHOLD == 0
        {
            let goal_label = aidaemon_tools::sanitize::short_goal_label(&goal.description);
            let msg = format!(
                "Heads up: your recurring goal \"{}\" has failed {} runs in a row. It is still \
                 scheduled and will keep retrying, but it likely needs attention. Latest failure: {}",
                goal_label, goal.dispatch_failures, summary
            );
            let entry = crate::traits::NotificationEntry::new(
                &goal.id,
                &goal.session_id,
                "evergreen_alert",
                &msg,
            );
            let _ = self.state.enqueue_notification(&entry).await;
            info!(
                goal_id = %self.goal_id,
                failures = goal.dispatch_failures,
                "Alerted user: recurring goal failing repeatedly"
            );
        }

        // Cancel remaining pending/claimed tasks so they don't get re-dispatched
        let tasks = self.scoped_tasks().await.unwrap_or_default();
        let mut cancelled = 0;
        for task in &tasks {
            if (task.status == "pending" || task.status == "claimed")
                && self
                    .state
                    .cancel_work_task(&task.id, "task-lead", None)
                    .await
                    .unwrap_or(false)
            {
                cancelled += 1;
            }
        }
        if cancelled > 0 {
            info!(goal_id = %self.goal_id, cancelled, "Cancelled pending tasks for failed goal");
        }
        let run_id = match self.goal_run_id.clone() {
            Some(run_id) => Some(run_id),
            None => self
                .state
                .get_current_goal_run(&self.goal_id)
                .await?
                .map(|run| run.id),
        };
        if let Some(run_id) = run_id {
            let _ = self
                .state
                .finish_goal_run(&run_id, "failed", Some(summary))
                .await?;
        }

        if is_continuous {
            Ok(format!(
                "Recurring goal {} run failed (cancelled {} pending tasks); the goal stays active and will retry next cycle: {}",
                self.goal_id, cancelled, summary
            ))
        } else {
            Ok(format!(
                "Goal {} failed (cancelled {} pending tasks): {}",
                self.goal_id, cancelled, summary
            ))
        }
    }
}

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

    #[test]
    fn test_validate_no_cycles_simple() {
        let task_a = Task {
            id: "a".to_string(),
            goal_id: "g".to_string(),
            description: "A".to_string(),
            status: "pending".to_string(),
            priority: "medium".to_string(),
            task_order: 0,
            parallel_group: None,
            depends_on: None,
            agent_id: None,
            context: None,
            result: None,
            error: None,
            blocker: None,
            idempotent: false,
            retry_count: 0,
            max_retries: 3,
            created_at: String::new(),
            started_at: None,
            completed_at: None,
        };

        // B depends on A — no cycle
        assert!(
            super::validate_no_cycles(std::slice::from_ref(&task_a), "b", &["a".to_string()])
                .is_ok()
        );
    }

    #[test]
    fn test_validate_no_cycles_self_reference() {
        // Task tries to depend on itself
        assert!(super::validate_no_cycles(&[], "a", &["a".to_string()]).is_err());
    }

    #[test]
    fn test_validate_no_cycles_circular() {
        // A depends on B, B (new) depends on A → cycle
        let task_a = Task {
            id: "a".to_string(),
            goal_id: "g".to_string(),
            description: "A".to_string(),
            status: "pending".to_string(),
            priority: "medium".to_string(),
            task_order: 0,
            parallel_group: None,
            depends_on: Some(serde_json::to_string(&vec!["b"]).unwrap()),
            agent_id: None,
            context: None,
            result: None,
            error: None,
            blocker: None,
            idempotent: false,
            retry_count: 0,
            max_retries: 3,
            created_at: String::new(),
            started_at: None,
            completed_at: None,
        };

        let task_b = Task {
            id: "b".to_string(),
            goal_id: "g".to_string(),
            description: "B".to_string(),
            status: "pending".to_string(),
            priority: "medium".to_string(),
            task_order: 0,
            parallel_group: None,
            depends_on: None,
            agent_id: None,
            context: None,
            result: None,
            error: None,
            blocker: None,
            idempotent: false,
            retry_count: 0,
            max_retries: 3,
            created_at: String::new(),
            started_at: None,
            completed_at: None,
        };

        // C depends on A, but A already depends on B → valid (A→B, C→A)
        assert!(
            super::validate_no_cycles(&[task_a.clone(), task_b.clone()], "c", &["a".to_string()])
                .is_ok()
        );

        // New task "b2" depends on A, but A depends on B, and B already exists → no cycle
        // But if we create a NEW "b" that depends on "a" when "a" depends on "b" → cycle
        // Let's test: existing has A depends on C, C exists. New task C depends on A → cycle
        let task_a_dep_c = Task {
            id: "a".to_string(),
            depends_on: Some(serde_json::to_string(&vec!["c"]).unwrap()),
            ..task_a.clone()
        };
        assert!(super::validate_no_cycles(&[task_a_dep_c], "c", &["a".to_string()]).is_err());
    }

    #[test]
    fn test_validate_nonexistent_dependency() {
        assert!(super::validate_no_cycles(&[], "a", &["nonexistent".to_string()]).is_err());
    }

    #[test]
    fn test_compress_old_entries() {
        let mut entries: Vec<Value> = (0..15)
            .map(|i| {
                json!({
                    "task_id": format!("task-{:04}", i),
                    "description": format!("Task number {}", i),
                    "result_summary": "Done",
                    "completed_at": "2025-01-01T00:00:00Z",
                })
            })
            .collect();

        super::compress_old_entries(&mut entries);

        // First 5 (15-10) should be compressed to strings
        for entry in entries.iter().take(5) {
            assert!(
                entry.is_string(),
                "Old entries should be compressed to strings"
            );
            let s = entry.as_str().unwrap();
            assert!(
                s.contains("(completed)"),
                "Compressed entry should say completed: {}",
                s
            );
        }

        // Last 10 should remain as objects
        for entry in entries.iter().skip(5) {
            assert!(entry.is_object(), "Recent entries should remain as objects");
        }
    }
}