ares-agent 0.10.0

Agent orchestration for ARES
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
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
//! Skill execution engine — turns a Skill definition into an agent run.

use ares_store::run_history::{LogLlmCallRequest, LogToolCallRequest, RunHistoryStore};
use ares_store::skills::SkillStore;
use ares_store::tenant_allowlist::TenantAllowlistStore;
use ares_llm::{
    CapabilityRequirements, Llm, LLMResponse, MicroEngine, MicroOutcome, MicroTask,
    TenantModelPolicy,
};
use ares_tools::Tools;
use ares_types::AppError;
use sqlx::PgPool;
use std::collections::HashMap;
use std::sync::Mutex;
use std::sync::atomic::{AtomicBool, Ordering};

use crate::EmergencyStop;
use std::sync::Arc;


async fn resolve_model_tier(
    tenant_id: &str,
    tier_name: &str,
    pool: &PgPool,
) -> Option<(String, String)> {
    let store = ares_store::tenant_model_tiers::TenantModelTierStore::new(pool);
    match store.get(tenant_id, tier_name).await {
        Ok(Some(tier)) => Some((tier.provider_name, tier.model_name)),
        _ => None,
    }
}


fn estimated_cost_usd(prompt_tokens: i64, completion_tokens: i64) -> rust_decimal::Decimal {
    rust_decimal::Decimal::new((prompt_tokens + completion_tokens) * 2, 6)
}

const MAX_SKILL_CALL_DEPTH: usize = 8;
const RUN_HISTORY_STATUS_SUCCESS: &str = "success";
/// Stable marker prefixed to delegated-subtask cancellation aborts so
/// callers can classify them without parsing prose.
pub const SUBTASK_CANCELLED_MARKER: &str = "subtask_cancelled";

/// Sticky cancel token for one delegated subtask.
///
/// Once flipped the token stays cancelled — there is no un-cancel — so a
/// trigger racing a starting subtask still aborts it at its first call
/// boundary. Tokens are keyed `"{run_id}/{skill_id}"` in the engine's
/// registry; [`SkillEngine::cancel_subtask`] is the external trigger and
/// every execution loop checks its governing token before starting the
/// next LLM round or tool iteration.
#[derive(Debug, Default)]
pub struct SubtaskCancelToken {
    cancelled: AtomicBool,
}

impl SubtaskCancelToken {
    pub fn new() -> Self {
        Self::default()
    }

    /// Flip the token. Every later [`Self::check`] keeps failing.
    pub fn cancel(&self) {
        self.cancelled.store(true, Ordering::SeqCst);
    }

    pub fn is_cancelled(&self) -> bool {
        self.cancelled.load(Ordering::SeqCst)
    }

    /// Call-boundary gate: `Ok` while the subtask is active, one stable
    /// [`SUBTASK_CANCELLED_MARKER`] error once it has been cancelled.
    pub fn check(&self) -> Result<(), String> {
        if self.is_cancelled() {
            Err(format!(
                "{SUBTASK_CANCELLED_MARKER}: delegated subtask was cancelled"
            ))
        } else {
            Ok(())
        }
    }
}

/// Fixed cache-friendly preamble for delegated-result self-critique rounds.
///
/// Kept byte-stable across calls and rounds so provider-side prompt caches
/// hit on the template; only the per-call tail (skill id, input, current
/// result) varies. Each round asks whether the answer addressed the task and
/// carries obvious errors or omissions; the model either outputs the
/// corrected final answer or returns the original verbatim.
const SELF_CHECK_TEMPLATE: &str = "You are double-checking the final answer of a delegated \
                                   sub-workflow step.\n\
                                   Ask yourself: (a) did the answer address the requested task? \
                                   (b) are there obvious errors or omissions?\n\
                                   If corrections are needed, output ONLY the corrected final \
                                   answer. Otherwise return the original answer verbatim.\n";

/// Opt-in ambient enrichment for assistant completions.
///
/// When enabled, every LLM step completion fires two parallel micro calls
/// (intent classification + keyword tags) over the micro-engine, and the
/// outcomes are attached as session metadata on the existing skill-step
/// record path (`run_llm_calls.response_payload["ambient_enrichment"]`).
/// Enrichment never delays or fails the completion itself: failures are
/// logged and silently skipped.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Default)]
pub struct AmbientEnrichmentConfig {
    /// Master switch. `false` (the default) issues no enrichment calls.
    pub enabled: bool,
}

/// Fixed system prompt for the intent-classification micro call.
const AMBIENT_INTENT_SYSTEM: &str = "You classify the user intent of an assistant answer. \
Reply with ONLY a JSON object of the form \
{\"intent\": \"question|command|summary|analysis|other\", \"confidence\": <number 0-1>}.";

/// Fixed system prompt for the keyword-tagging micro call.
const AMBIENT_TAGS_SYSTEM: &str = "You extract keyword tags from an assistant answer. \
Reply with ONLY a JSON object of the form {\"tags\": [\"tag\", ...]} with at most 5 tags.";

/// Run one ambient micro call. A transport failure after all retries yields
/// `None` — ambient enrichment is best-effort by contract.
async fn run_ambient_micro_call(
    engine: &MicroEngine,
    system: &str,
    input: String,
) -> Option<MicroOutcome> {
    let task = MicroTask {
        name: "ambient",
        system: system.to_string(),
        input,
        max_tokens: 128,
    };
    match engine.run(&task).await {
        Ok(outcome) => Some(outcome),
        Err(err) => {
            tracing::debug!(error = %err, "ambient enrichment call failed; skipping");
            None
        }
    }
}

/// Fold one micro outcome's parsed JSON fields into `target`.
fn merge_micro_outcome(outcome: &Option<MicroOutcome>, target: &mut serde_json::Value) {
    let Some(outcome) = outcome else { return };
    let Some(json) = &outcome.json else { return };
    let Some(object) = json.as_object() else { return };
    let Some(target_object) = target.as_object_mut() else { return };
    for (key, value) in object {
        target_object.insert(key.clone(), value.clone());
    }
}

/// Cap the completion text fed to the ambient micro calls.
fn truncate_ambient_input(text: &str) -> String {
    const AMBIENT_INPUT_MAX_CHARS: usize = 4_000;
    text.chars().take(AMBIENT_INPUT_MAX_CHARS).collect()
}

/// Fold ambient session metadata into a skill-step record's response
/// payload. Null metadata (disabled or fully failed enrichment) adds no key,
/// so the record stays byte-compatible with the pre-enrichment shape.
fn fold_ambient_into_response_payload(
    mut payload: serde_json::Value,
    ambient_metadata: serde_json::Value,
) -> serde_json::Value {
    if !ambient_metadata.is_null() {
        if let Some(object) = payload.as_object_mut() {
            object.insert("ambient_enrichment".to_string(), ambient_metadata);
        }
    }
    payload
}

fn default_step_input() -> serde_json::Value {
    serde_json::Value::Null
}

pub fn skill_result_token_counts(result: &serde_json::Value) -> (i64, i64) {
    fn walk(value: &serde_json::Value, input_tokens: &mut i64, output_tokens: &mut i64) {
        match value {
            serde_json::Value::Object(object) => {
                if let Some(usage) = object.get("usage").and_then(|usage| usage.as_object()) {
                    *input_tokens += usage
                        .get("prompt_tokens")
                        .and_then(|value| value.as_i64())
                        .unwrap_or(0);
                    *output_tokens += usage
                        .get("completion_tokens")
                        .and_then(|value| value.as_i64())
                        .unwrap_or(0);
                }
                for child in object.values() {
                    walk(child, input_tokens, output_tokens);
                }
            }
            serde_json::Value::Array(items) => {
                for item in items {
                    walk(item, input_tokens, output_tokens);
                }
            }
            _ => {}
        }
    }

    let mut input_tokens = 0;
    let mut output_tokens = 0;
    walk(result, &mut input_tokens, &mut output_tokens);
    (input_tokens, output_tokens)
}

fn validate_skill_call_depth(depth: usize) -> Result<(), String> {
    if depth > MAX_SKILL_CALL_DEPTH {
        return Err(format!(
            "Skill call depth exceeded maximum of {}",
            MAX_SKILL_CALL_DEPTH
        ));
    }
    Ok(())
}

/// One step inside a skill workflow.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
#[allow(dead_code)]
pub enum SkillStep {
    /// Call a tool by name with JSON arguments.
    ToolCall {
        #[serde(alias = "tool", alias = "name")]
        tool_name: String,
        #[serde(default = "default_step_input", alias = "arguments", alias = "input")]
        args: serde_json::Value,
    },
    /// Call an LLM with a prompt and a model tier.
    LlmCall {
        prompt: String,
        #[serde(alias = "model")]
        model_tier: String,
    },
    /// Execute another skill with optional JSON input.
    SkillCall {
        #[serde(alias = "skill", alias = "id")]
        skill_id: String,
        #[serde(default = "default_step_input", alias = "args", alias = "arguments")]
        input: serde_json::Value,
    },
    /// Conditional branch evaluated against execution context.
    Condition {
        expression: String,
        #[serde(default)]
        then_steps: Vec<SkillStep>,
    },
}

/// Engine that loads a [`Skill`] from the DB and executes its steps.
pub struct SkillEngine {
    pool: PgPool,
    tools: Arc<Tools>,
    llm: Arc<Llm>,
    ambient: AmbientEnrichmentConfig,
    self_check_rounds: Option<u32>,
    /// Sticky per-delegated-subtask cancel tokens keyed
    /// `"{run_id}/{skill_id}"`. Entries appear when an external trigger or
    /// registration names a subtask; execution only ever reads them.
    cancels: Mutex<HashMap<String, Arc<SubtaskCancelToken>>>,
}

impl SkillEngine {
    /// Create a new engine with Tools and Llm (no Overlay).
    ///
    /// Ambient enrichment defaults to off; enable it with
    /// [`SkillEngine::with_ambient_enrichment`].
    pub fn new(
        pool: PgPool,
        tools: Arc<Tools>,
        llm: Arc<Llm>,
    ) -> Self {
        Self {
            pool,
            tools,
            llm,
            ambient: AmbientEnrichmentConfig::default(),
            self_check_rounds: None,
            cancels: Mutex::new(HashMap::new()),
        }
    }

    /// Opt in to ambient enrichment of assistant completions (default off).
    ///
    /// When enabled, each LLM step fires parallel intent-classify and
    /// keyword-tag micro calls after the completion and attaches the parsed
    /// outcomes as session metadata on the existing skill-step record.
    pub fn with_ambient_enrichment(mut self, config: AmbientEnrichmentConfig) -> Self {
        self.ambient = config;
        self
    }

    /// Opt in to delegated-result self-critique rounds (default off).
    ///
    /// When set to `Some(n)` with `n >= 1`, each nested `SkillCall` result
    /// passes through up to `n` self-check rounds — one LLM call per round
    /// over a cache-stable template — before the result integrates. An LLM
    /// failure mid-loop keeps the last good answer silently.
    pub fn with_self_check_rounds(mut self, rounds: Option<u32>) -> Self {
        self.self_check_rounds = rounds.filter(|r| *r >= 1);
        self
    }
    /// Register (or fetch) the sticky cancel token for one delegated
    /// subtask. Idempotent: repeated registrations return the same token.
    pub fn register_subtask_cancel(&self, subtask_id: &str) -> Arc<SubtaskCancelToken> {
        self.cancels
            .lock()
            .expect("skill subtask cancel registry poisoned")
            .entry(subtask_id.to_string())
            .or_default()
            .clone()
    }

    /// External trigger path: flip the sticky cancel token for
    /// `subtask_id` (e.g. `"{run_id}/{skill_id}"`). Works before the
    /// subtask starts — a later registration observes the same flipped
    /// token — so a trigger racing delegation still aborts it at its first
    /// call boundary. Returns `true` on the first flip of that token,
    /// `false` when it was already cancelled.
    pub fn cancel_subtask(&self, subtask_id: &str) -> bool {
        let mut registry = self
            .cancels
            .lock()
            .expect("skill subtask cancel registry poisoned");
        let token = registry.entry(subtask_id.to_string()).or_default();
        !token.cancelled.swap(true, Ordering::SeqCst)
    }

    fn registered_cancel_token(&self, subtask_id: &str) -> Option<Arc<SubtaskCancelToken>> {
        self.cancels
            .lock()
            .expect("skill subtask cancel registry poisoned")
            .get(subtask_id)
            .cloned()
    }

    /// Delegate one nested `SkillCall`, gated by its per-subtask cancel
    /// token at every call boundary. A cancelled child aborts cleanly with
    /// the stable [`SUBTASK_CANCELLED_MARKER`] error; nothing it produced
    /// integrates into the parent context because the error unwinds before
    /// any context write. The pre-delegation gate catches triggers that
    /// landed before the child started; the child re-checks its own key at
    /// each of its own boundaries.
    async fn delegate_skill_call(
        &self,
        skill_id: &str,
        input: serde_json::Value,
        tenant_id: &str,
        run_id: &str,
        ctx: &Arc<cordis::Context>,
        depth: usize,
    ) -> Result<serde_json::Value, String> {
        ensure_execution_active(self, ctx, &format!("{run_id}/{skill_id}"))?;
        let check_input = self.self_check_rounds.map(|_| input.clone());
        let result = Box::pin(self.execute_skill_at_depth(
            skill_id,
            tenant_id,
            input,
            run_id,
            ctx,
            depth + 1,
        ))
        .await?;
        Ok(match check_input {
            Some(check_input) => {
                self.self_check_nested_result(ctx, skill_id, &check_input, result)
                    .await
            }
            None => result,
        })
    }

    fn scoped_tool_context(
        &self,
        ctx: &Arc<cordis::Context>,
        tenant_id: &str,
    ) -> Arc<cordis::Context> {
        ctx.isolate::<Tools>(tenant_id.to_string())
    }

    fn resolve_tenant_tool(
        &self,
        ctx: &Arc<cordis::Context>,
        tenant_id: &str,
        name: &str,
    ) -> Option<Arc<dyn ares_tools::Tool>> {
        let scoped = self.scoped_tool_context(ctx, tenant_id);
        let tools = scoped.get::<Tools>().unwrap_or_else(|| Arc::clone(&self.tools));
        tools.resolve(&scoped, name)
    }

    async fn execute_tenant_tool(
        &self,
        ctx: &Arc<cordis::Context>,
        tenant_id: &str,
        name: &str,
        args: serde_json::Value,
    ) -> Result<serde_json::Value, String> {
        let scoped = self.scoped_tool_context(ctx, tenant_id);
        let tools = scoped.get::<Tools>().unwrap_or_else(|| Arc::clone(&self.tools));
        tools
            .execute(&scoped, name, args)
            .await
            .map_err(|e| match e {
                AppError::NotFound(_) => format!("Tool {name} not found"),
                e => format!("Tool {name} execution error: {e}"),
            })
    }

    /// Answer text a self-check round critiques: the `content` field of a
    /// standard step-context result, or a bare string result itself.
    /// Anything else carries no answer to check.
    fn self_check_answer(result: &serde_json::Value) -> Option<&str> {
        match result {
            serde_json::Value::String(text) => Some(text.as_str()),
            serde_json::Value::Object(_) => result.get("content").and_then(|c| c.as_str()),
            _ => None,
        }
    }

    /// Fold a corrected answer back into the result: structured results keep
    /// their shape (only `content` moves); bare strings are replaced whole.
    fn apply_self_check_answer(
        mut current: serde_json::Value,
        answer: &str,
    ) -> serde_json::Value {
        if current.is_object() && current.get("content").is_some() {
            current["content"] = serde_json::Value::String(answer.to_string());
            current
        } else {
            serde_json::Value::String(answer.to_string())
        }
    }

    /// Run up to `rounds` self-critique-and-fix rounds over a delegated skill
    /// result BEFORE any downstream gate sees it.
    ///
    /// Each round is exactly one LLM call whose prompt is the byte-stable
    /// [`SELF_CHECK_TEMPLATE`] followed by the delegated skill id, requested
    /// input, and current answer — identical request shape across rounds so
    /// provider-side prefix caches hit. A verbatim reply means the model found
    /// no corrections and ends the loop; an LLM error or empty answer keeps
    /// the last good answer silently and stops further rounds.
    async fn self_check_nested_result(
        &self,
        ctx: &Arc<cordis::Context>,
        delegated_skill_id: &str,
        delegated_input: &serde_json::Value,
        result: serde_json::Value,
    ) -> serde_json::Value {
        let Some(rounds) = self.self_check_rounds else {
            return result;
        };
        let Some(original_answer) = Self::self_check_answer(&result).map(str::to_string) else {
            return result;
        };
        let mut answer = original_answer.clone();
        for _ in 0..rounds {
            let prompt = format!(
                "{SELF_CHECK_TEMPLATE}Delegated sub-workflow: {delegated_skill_id}\n\
                 Requested input: {delegated_input}\n\
                 Current answer to check: {answer}",
            );
            match self.llm.complete(ctx, &prompt).await {
                Ok(next) if next.trim().is_empty() => {
                    tracing::debug!(
                        "self-check round returned an empty answer; keeping last good answer"
                    );
                    break;
                }
                // Verbatim reply: the model judged the answer sound.
                Ok(next) if next == answer => break,
                Ok(next) => answer = next,
                Err(err) => {
                    tracing::debug!(
                        error = %err,
                        "self-check LLM call failed; keeping last good answer"
                    );
                    break;
                }
            }
        }
        if answer == original_answer {
            return result;
        }
        Self::apply_self_check_answer(result, &answer)
    }

    /// Run the completion and, when ambient enrichment is enabled, fire the
    /// intent/tag micro calls in parallel afterwards.
    ///
    /// Returns the completion plus its session metadata (`Null` when
    /// disabled). Enrichment failures are logged and skipped — they never
    /// delay or fail the completion result.
    #[allow(clippy::too_many_arguments)]
    async fn complete_llm_step_with_metadata(
        &self,
        ctx: &Arc<cordis::Context>,
        messages: &[(String, String)],
        model_name: &str,
        run_id: Option<&str>,
        tenant_id: Option<&str>,
        step_index: Option<i32>,
        provider_name: Option<&str>,
    ) -> (Result<LLMResponse, String>, serde_json::Value) {
        // Preserve the existing skill behavior: concatenate message contents in order.
        let prompt = messages
            .iter()
            .map(|(_, content)| content.as_str())
            .collect::<Vec<_>>()
            .join("\n");
        let request_ctx = if model_name.is_empty() || ctx.get::<ares_llm::ModelOverride>().is_some() {
            Arc::clone(ctx)
        } else {
            ctx.with_intercept(ares_llm::ModelOverride {
                model: model_name.to_string(),
            })
        };
        let content = self
            .llm
            .complete(&request_ctx, &prompt)
            .await;
        let response = content.map_err(|e| format!("LLM generation failed: {e}")).map(
            |content| LLMResponse {
                content,
                tool_calls: Vec::new(),
                finish_reason: "stop".to_string(),
                usage: None,
            },
        );
        let metadata = match (&self.ambient, &response) {
            (AmbientEnrichmentConfig { enabled: true }, Ok(response)) => {
                self.run_ambient_enrichment(
                    ctx,
                    &response.content,
                    run_id,
                    tenant_id,
                    step_index,
                    provider_name.unwrap_or("default"),
                    model_name,
                )
                .await
            }
            _ => serde_json::Value::Null,
        };
        (response, metadata)
    }

    /// Fire the intent-classify and keyword-tag micro calls in parallel over
    /// the completion text and fold their parsed JSON into one session
    /// metadata object on the existing record path.
    ///
    /// Best-effort by contract: any micro-call failure is logged and that
    /// section is silently omitted.
    #[allow(clippy::too_many_arguments)]
    async fn run_ambient_enrichment(
        &self,
        ctx: &Arc<cordis::Context>,
        completion_text: &str,
        run_id: Option<&str>,
        tenant_id: Option<&str>,
        step_index: Option<i32>,
        provider_name: &str,
        model_name: &str,
    ) -> serde_json::Value {
        let client = match self.llm.get_client(ctx, CapabilityRequirements::default()).await {
            Ok(client) => client,
            Err(err) => {
                tracing::debug!(error = %err, "ambient enrichment client unavailable; skipping");
                return serde_json::Value::Null;
            }
        };
        let engine = MicroEngine::with_client(client);
        let input = truncate_ambient_input(completion_text);

        let intent = run_ambient_micro_call(&engine, AMBIENT_INTENT_SYSTEM, input.clone());
        let tags = run_ambient_micro_call(&engine, AMBIENT_TAGS_SYSTEM, input);
        let (intent, tags) = tokio::join!(intent, tags);

        let mut meta = serde_json::json!({
            "intent": {},
            "tags": {},
        });
        merge_micro_outcome(&intent, &mut meta["intent"]);
        merge_micro_outcome(&tags, &mut meta["tags"]);
        if let Some(run_id) = run_id {
            meta["run_id"] = serde_json::Value::String(run_id.to_string());
        }
        if let Some(tenant_id) = tenant_id {
            meta["tenant_id"] = serde_json::Value::String(tenant_id.to_string());
        }
        if let Some(step_index) = step_index {
            meta["step_index"] = serde_json::json!(step_index);
        }
        if !meta["intent"].as_object().is_some_and(|o| !o.is_empty())
            && !meta["tags"].as_object().is_some_and(|o| !o.is_empty())
        {
            return serde_json::Value::Null;
        }
        meta["provider"] = serde_json::Value::String(provider_name.to_string());
        meta["model"] = serde_json::Value::String(model_name.to_string());
        meta
    }

    /// Execute a skill by id for a tenant.
    ///
    /// Steps are run sequentially. Tool calls and LLM calls are executed
    /// for real and logged to `run_history` with their results.
    pub async fn execute_skill(
        &self,
        skill_id: &str,
        tenant_id: &str,
        input: serde_json::Value,
        run_id: &str,
        ctx: &Arc<cordis::Context>,
    ) -> Result<serde_json::Value, String> {
        self.execute_skill_at_depth(skill_id, tenant_id, input, run_id, ctx, 0)
            .await
    }

    async fn execute_skill_at_depth(
        &self,
        skill_id: &str,
        tenant_id: &str,
        input: serde_json::Value,
        run_id: &str,
        ctx: &Arc<cordis::Context>,
        depth: usize,
    ) -> Result<serde_json::Value, String> {
        validate_skill_call_depth(depth)?;
        let subtask_key = format!("{run_id}/{skill_id}");

        // 1. Load the skill definition
        let skill_store = SkillStore::new(&self.pool);
        let skill = skill_store
            .get_skill_for_tenant(skill_id, tenant_id)
            .await
            .map_err(|e| e.to_string())?
            .ok_or_else(|| "Skill not found".to_string())?;

        // 2. Parse steps JSONB into SkillStep vec
        let steps: Vec<SkillStep> = serde_json::from_value(skill.steps)
            .map_err(|e| format!("Invalid skill steps: {}", e))?;

        // 3. Execute each step sequentially
        let mut context = serde_json::json!({"input": input});
        let mut step_index: i32 = 0;

        for step in steps {
            // Call boundary: never start the next LLM round or tool
            // iteration once cancellation latched. The governing token is
            // re-read from the registry at every boundary so an external
            // trigger racing the run is honored immediately.
            ensure_execution_active(self, ctx, &subtask_key)?;
            match step {
                SkillStep::ToolCall { tool_name, args } => {
                    tracing::info!("Step {}: tool_call {}", step_index, tool_name);
                    ensure_tenant_tool_allowed(&self.pool, tenant_id, &tool_name).await?;
                    let start = std::time::Instant::now();

                    // Execute via request-context Tools (tenant isolate → runtime → static).
                    let result = self
                        .execute_tenant_tool(ctx, tenant_id, &tool_name, args.clone())
                        .await?;

                    let latency_ms = start.elapsed().as_millis() as i64;

                    // Store result in context
                    context[&format!("step_{}", step_index)] =
                        successful_step_context(result.clone());

                    // Log the completed tool call
                    self.log_tool_call_result(
                        run_id,
                        tenant_id,
                        step_index,
                        &tool_name,
                        args,
                        Some(result),
                        latency_ms,
                    )
                    .await?;
                }
                SkillStep::LlmCall { prompt, model_tier } => {
                    tracing::info!("Step {}: llm_call (tier: {})", step_index, model_tier);
                    let start = std::time::Instant::now();

                    // Resolve model tier to concrete model
                    let (provider_name, model_name) =
                        resolve_model_tier(tenant_id, &model_tier, &self.pool)
                            .await
                            .unwrap_or_else(|| ("default".to_string(), model_tier.clone()));
                    ensure_tenant_model_allowed(&self.pool, tenant_id, &model_name).await?;

                    // Build messages and call Llm through the request context.
                    let messages = vec![("user".to_string(), prompt.clone())];
                    self.enforce_token_budget_before_llm_call(tenant_id).await?;
                    let (response, ambient_metadata) = self
                        .complete_llm_step_with_metadata(
                            ctx,
                            &messages,
                            &model_name,
                            Some(run_id),
                            Some(tenant_id),
                            Some(step_index),
                            Some(provider_name.as_str()),
                        )
                        .await;
                    let response = response?;
                    self.record_llm_token_budget_usage(tenant_id, run_id, &model_name, &response)
                        .await?;

                    let latency_ms = start.elapsed().as_millis() as i64;

                    // Store result
                    let result = serde_json::json!({
                        "content": response.content,
                        "usage": response.usage,
                    });
                    context[&format!("step_{}", step_index)] =
                        successful_step_context(result.clone());

                    // Log
                    self.log_llm_call_with_metadata(
                        run_id,
                        tenant_id,
                        step_index,
                        &provider_name,
                        &model_name,
                        response,
                        latency_ms,
                        ambient_metadata,
                    )
                    .await?;
                }
                SkillStep::SkillCall { skill_id, input } => {
                    tracing::info!("Step {}: skill_call {}", step_index, skill_id);
                    let result = Box::pin(
                        self.delegate_skill_call(&skill_id, input, tenant_id, run_id, ctx, depth),
                    )
                    .await?;
                    context[&format!("step_{}", step_index)] = successful_step_context(result);
                }
                SkillStep::Condition {
                    expression,
                    then_steps,
                } => {
                    tracing::info!("Step {}: condition {}", step_index, expression);
                    if let Some(ready_steps) = ready_then_steps(&expression, &then_steps, &context)
                    {
                        // Recursively execute then_steps
                        for (sub_idx, sub_step) in ready_steps.iter().enumerate() {
                            let sub_step_index = step_index + 1 + sub_idx as i32;
                            self.execute_sub_step(
                                sub_step,
                                ctx,
                                tenant_id,
                                run_id,
                                sub_step_index,
                                &mut context,
                                depth,
                                &subtask_key,
                            )
                            .await?;
                        }
                    }
                }
            }
            step_index += 1;
        }

        Ok(context)
    }

    /// Execute a single sub-step (used for conditional branches).
    async fn execute_sub_step(
        &self,
        step: &SkillStep,
        ctx: &Arc<cordis::Context>,
        tenant_id: &str,
        run_id: &str,
        step_index: i32,
        context: &mut serde_json::Value,
        depth: usize,
        cancel_key: &str,
    ) -> Result<(), String> {
        ensure_execution_active(self, ctx, cancel_key)?;
        match step {
            SkillStep::ToolCall { tool_name, args } => {
                tracing::info!("Sub-step {}: tool_call {}", step_index, tool_name);
                ensure_tenant_tool_allowed(&self.pool, tenant_id, tool_name).await?;
                let start = std::time::Instant::now();

                let result = self
                    .execute_tenant_tool(ctx, tenant_id, tool_name, args.clone())
                    .await?;

                let latency_ms = start.elapsed().as_millis() as i64;
                context[&format!("step_{}", step_index)] = successful_step_context(result.clone());

                self.log_tool_call_result(
                    run_id,
                    tenant_id,
                    step_index,
                    tool_name,
                    args.clone(),
                    Some(result),
                    latency_ms,
                )
                .await?;
            }
            SkillStep::LlmCall { prompt, model_tier } => {
                tracing::info!("Sub-step {}: llm_call (tier: {})", step_index, model_tier);
                let start = std::time::Instant::now();

                let (provider_name, model_name) =
                    resolve_model_tier(tenant_id, model_tier, &self.pool)
                        .await
                        .unwrap_or_else(|| ("default".to_string(), model_tier.clone()));
                ensure_tenant_model_allowed(&self.pool, tenant_id, &model_name).await?;

                let messages = vec![("user".to_string(), prompt.clone())];
                self.enforce_token_budget_before_llm_call(tenant_id).await?;
                let (response, ambient_metadata) = self
                    .complete_llm_step_with_metadata(
                        ctx,
                        &messages,
                        &model_name,
                        Some(run_id),
                        Some(tenant_id),
                        Some(step_index),
                        Some(provider_name.as_str()),
                    )
                    .await;
                let response = response?;
                self.record_llm_token_budget_usage(tenant_id, run_id, &model_name, &response)
                    .await?;

                let latency_ms = start.elapsed().as_millis() as i64;
                let result = serde_json::json!({
                    "content": response.content,
                    "usage": response.usage,
                });
                context[&format!("step_{}", step_index)] = successful_step_context(result.clone());

                self.log_llm_call_with_metadata(
                    run_id,
                    tenant_id,
                    step_index,
                    &provider_name,
                    &model_name,
                    response,
                    latency_ms,
                    ambient_metadata,
                )
                .await?;
            }
            SkillStep::SkillCall { skill_id, input } => {
                tracing::info!("Sub-step {}: skill_call {}", step_index, skill_id);
                let result = Box::pin(
                    self.delegate_skill_call(skill_id, input.clone(), tenant_id, run_id, ctx, depth),
                )
                .await?;
                context[&format!("step_{}", step_index)] = successful_step_context(result);
            }
            SkillStep::Condition {
                expression,
                then_steps,
            } => {
                tracing::info!("Sub-step {}: condition {}", step_index, expression);
                if let Some(ready_steps) = ready_then_steps(expression, then_steps, context) {
                    for (sub_idx, sub_step) in ready_steps.iter().enumerate() {
                        let sub_step_index = step_index + 1 + sub_idx as i32;
                        Box::pin(self.execute_sub_step(
                            sub_step,
                            ctx,
                            tenant_id,
                            run_id,
                            sub_step_index,
                            context,
                            depth,
                            cancel_key,
                        ))
                        .await?;
                    }
                }
            }
        }
        Ok(())
    }

    async fn enforce_token_budget_before_llm_call(&self, tenant_id: &str) -> Result<(), String> {
        let store = ares_store::token_budgets::TokenBudgetStore::new(&self.pool);
        let status = store
            .check_budget(tenant_id)
            .await
            .map_err(|e| e.to_string())?;
        if status.would_exceed {
            return Err(format!(
                "Tenant {} token budget exceeded ({} / {})",
                tenant_id, status.tokens_used, status.token_limit
            ));
        }
        Ok(())
    }

    async fn record_llm_token_budget_usage(
        &self,
        tenant_id: &str,
        run_id: &str,
        model: &str,
        response: &LLMResponse,
    ) -> Result<(), String> {
        let Some(usage) = response.usage.as_ref() else {
            return Ok(());
        };
        let store = ares_store::token_budgets::TokenBudgetStore::new(&self.pool);
        store
            .record_usage(
                tenant_id,
                Some(run_id),
                "skill",
                model,
                usage.prompt_tokens as i64,
                usage.completion_tokens as i64,
            )
            .await
            .map_err(|e| e.to_string())
    }

    async fn log_tool_call_result(
        &self,
        run_id: &str,
        tenant_id: &str,
        step_index: i32,
        tool_name: &str,
        args: serde_json::Value,
        result: Option<serde_json::Value>,
        latency_ms: i64,
    ) -> Result<(), String> {
        let store = RunHistoryStore::new(&self.pool);
        let req = LogToolCallRequest {
            id: uuid::Uuid::new_v4().to_string(),
            run_id: run_id.to_string(),
            tenant_id: tenant_id.to_string(),
            agent_name: "skill_executor".to_string(),
            step_index,
            tool_name: tool_name.to_string(),
            tool_type: "skill_step".to_string(),
            arguments: args,
            result,
            latency_ms,
            status: RUN_HISTORY_STATUS_SUCCESS.to_string(),
            error_message: None,
            created_at: chrono::Utc::now().timestamp(),
        };
        store
            .insert_tool_call(&req)
            .await
            .map_err(|e| e.to_string())?;
        Ok(())
    }

    /// Log one LLM step on the existing `run_llm_calls` record path, with
    /// ambient-enrichment session metadata folded into `response_payload`
    /// when present.
    #[allow(clippy::too_many_arguments)]
    async fn log_llm_call_with_metadata(
        &self,
        run_id: &str,
        tenant_id: &str,
        step_index: i32,
        provider: &str,
        model: &str,
        response: LLMResponse,
        latency_ms: i64,
        ambient_metadata: serde_json::Value,
    ) -> Result<(), String> {
        let store = RunHistoryStore::new(&self.pool);
        let usage = response.usage.unwrap_or_default();
        let estimated_cost_usd = estimated_cost_usd(
            usage.prompt_tokens as i64,
            usage.completion_tokens as i64,
        );
        let req = LogLlmCallRequest {
            id: uuid::Uuid::new_v4().to_string(),
            run_id: run_id.to_string(),
            tenant_id: tenant_id.to_string(),
            agent_name: "skill_executor".to_string(),
            step_index,
            provider: provider.to_string(),
            model: model.to_string(),
            prompt_tokens: usage.prompt_tokens as i64,
            completion_tokens: usage.completion_tokens as i64,
            total_tokens: usage.total_tokens as i64,
            estimated_cost_usd,
            latency_ms,
            cached_tokens: usage.cached_tokens,
            total_time_ms: Some(latency_ms),
            status: RUN_HISTORY_STATUS_SUCCESS.to_string(),
            error_message: None,
            request_payload: None,
            response_payload: Some(fold_ambient_into_response_payload(
                serde_json::json!({
                    "content": response.content,
                    "finish_reason": response.finish_reason,
                }),
                ambient_metadata,
            )),
            created_at: chrono::Utc::now().timestamp(),
        };
        store
            .insert_llm_call(&req)
            .await
            .map_err(|e| e.to_string())?;
        Ok(())
    }
}

fn runtime_tool_error_allows_static_fallback(error: &AppError) -> bool {
    matches!(error, AppError::NotFound(_))
}

/// Call-boundary gate shared by every skill-execution loop: refuse to start
/// the next unit of work when the global emergency stop has latched or the
/// governing per-subtask cancel token flipped. Both abort with the stable
/// [`SUBTASK_CANCELLED_MARKER`] so callers classify them identically.
fn ensure_execution_active(
    engine: &SkillEngine,
    ctx: &Arc<cordis::Context>,
    subtask_key: &str,
) -> Result<(), String> {
    if let Some(stop) = ctx.get::<EmergencyStop>() {
        if stop.is_active() {
            return Err(format!(
                "{SUBTASK_CANCELLED_MARKER}: emergency stop is active"
            ));
        }
    }
    // Fresh registry read on every boundary: an external trigger racing the
    // run is honored at the very next LLM round or tool iteration.
    if let Some(token) = engine.registered_cancel_token(subtask_key) {
        token.check()?;
    }
    Ok(())
}

async fn ensure_tenant_tool_allowed(
    pool: &PgPool,
    tenant_id: &str,
    tool_name: &str,
) -> Result<(), String> {
    let store = TenantAllowlistStore::new(pool);
    match store.is_tool_allowed(tenant_id, tool_name).await {
        Ok(true) => Ok(()),
        Ok(false) => Err(format!(
            "Tool '{}' is not allowed for tenant '{}'",
            tool_name, tenant_id
        )),
        Err(e) => Err(format!("Failed to check tool allowlist: {}", e)),
    }
}

async fn ensure_tenant_model_allowed(
    pool: &PgPool,
    tenant_id: &str,
    model_name: &str,
) -> Result<(), String> {
    let store = TenantAllowlistStore::new(pool);
    match store.is_model_allowed(tenant_id, model_name).await {
        Ok(true) => Ok(()),
        Ok(false) => Err(TenantModelPolicy::denial_message(tenant_id, model_name)),
        Err(e) => Err(format!("Failed to check model allowlist: {}", e)),
    }
}

fn successful_step_context(result: serde_json::Value) -> serde_json::Value {
    match result {
        serde_json::Value::Object(mut fields) => {
            fields.entry("status").or_insert_with(|| {
                serde_json::Value::String(RUN_HISTORY_STATUS_SUCCESS.to_string())
            });
            serde_json::Value::Object(fields)
        }
        value => serde_json::json!({
            "status": RUN_HISTORY_STATUS_SUCCESS,
            "content": value,
        }),
    }
}

fn ready_then_steps<'a>(
    expression: &str,
    then_steps: &'a [SkillStep],
    context: &serde_json::Value,
) -> Option<&'a [SkillStep]> {
    evaluate_condition(expression, context).then_some(then_steps)
}

/// Evaluate a simple condition expression against the execution context.
///
/// Supported forms:
/// - `step_N.status == 'success'`
/// - `step_N.status != 'success'`
/// - `step_N.result == 'value'` (string comparison against step result["content"])
/// - `step_N.result != 'value'`
fn evaluate_condition(expression: &str, context: &serde_json::Value) -> bool {
    let expr = expression.trim();

    // Parse "left op right" where right is quoted
    let (left, op, right) = {
        let parts: Vec<&str> = expr.splitn(2, "==").collect();
        if parts.len() == 2 {
            (parts[0].trim(), "==", parts[1].trim())
        } else {
            let parts: Vec<&str> = expr.splitn(2, "!=").collect();
            if parts.len() == 2 {
                (parts[0].trim(), "!=", parts[1].trim())
            } else {
                return false;
            }
        }
    };

    let right_val = right.trim_matches('\'').trim_matches('"');

    // Parse left side like "step_0.status" or "step_0.result"
    let left_parts: Vec<&str> = left.split('.').collect();
    if left_parts.len() != 2 {
        return false;
    }
    let step_key = left_parts[0];
    let field = left_parts[1];

    let step_value = match context.get(step_key) {
        Some(v) => v,
        None => return false,
    };

    let actual = match field {
        "status" => step_value
            .get("status")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_string(),
        "result" => step_value
            .get("content")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_string(),
        _ => return false,
    };

    match op {
        "==" => actual == right_val,
        "!=" => actual != right_val,
        _ => false,
    }
}

impl cordis::Service for SkillEngine {
    fn name(&self) -> &'static str {
        "skill_engine"
    }
    fn init(
        &self,
        _ctx: &std::sync::Arc<cordis::Context>,
    ) -> cordis::ServiceInitFuture<'_> {
        Box::pin(async { Ok(None) })
    }
    fn check(&self) -> bool {
        true
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use ares_llm::{ClientPool, LLMClient, ProviderRegistry};
    use ares_tools::Tool;
    use cordis::{Context, EventsService};
    use serde_json::json;
    use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
    use std::sync::Mutex;

    struct EventProbeTool(Arc<AtomicBool>);

    #[async_trait::async_trait]
    impl Tool for EventProbeTool {
        fn name(&self) -> &str {
            "event-probe"
        }

        fn description(&self) -> &str {
            "tool used to prove tools.execute dispatch"
        }

        fn parameters_schema(&self) -> serde_json::Value {
            json!({"type": "object"})
        }

        async fn execute(&self, _args: serde_json::Value) -> ares_types::Result<serde_json::Value> {
            self.0.store(true, Ordering::SeqCst);
            Ok(json!({"reached": true}))
        }
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn skill_llm_call_uses_complete_waterfall() {
        let mut registry = ProviderRegistry::new();
        registry.register_provider(
            "local",
            ares_llm::ProviderConfig::Ollama {
                api_key_env: "SKILL_ENGINE_TEST_KEY".to_string(),
                base_url: "http://127.0.0.1:9".to_string(),
                default_model: "test-model".to_string(),
            },
        );
        registry.register_model(
            "test-model",
            ares_llm::ModelConfig {
                provider: "local".to_string(),
                model: "test-model".to_string(),
                temperature: 0.0,
                max_tokens: 32,
            },
        );
        let llm = Arc::new(Llm::new(
            Arc::new(registry),
            Arc::new(ClientPool::with_defaults()),
            None,
        ));
        let ctx = Context::new_root();
        let events = ctx.provide(EventsService::new());
        events.on_waterfall("llm.complete".into(), |_payload, _next| async move {
            Ok(json!({"content": "cached"}))
        });
        let engine = SkillEngine::new(
            PgPool::connect_lazy("postgres://localhost/ares_test").expect("lazy pool"),
            Arc::new(Tools::from_static(Vec::<Arc<dyn Tool>>::new())),
            llm,
        );

        let (response, ambient) = engine
            .complete_llm_step_with_metadata(
                &ctx,
                &[("user".to_string(), "ignored if provider is reached".to_string())],
                "test-model",
                None,
                None,
                None,
                None,
            )
            .await;
        let response = response.expect("llm.complete waterfall");
        assert!(ambient.is_null());
        assert_eq!(response.content, "cached");
        assert!(response.tool_calls.is_empty());
        assert_eq!(response.finish_reason, "stop");
        assert!(response.usage.is_none());
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn skill_tool_call_uses_request_context_tools_execute_waterfall() {
        let reached = Arc::new(AtomicBool::new(false));
        let ctx = Context::new_root();
        let events = ctx.provide(EventsService::new());
        let _ = &reached;
        events.on_waterfall("tools.execute".into(), |_payload, _next| async move {
            Ok(json!({"result": {"cached": true}}))
        });

        let llm = Arc::new(Llm::new(
            Arc::new(ProviderRegistry::new()),
            Arc::new(ClientPool::with_defaults()),
            None,
        ));
        // Scope lookup misses (no per-tenant Tools on ctx) fall back to the
        // engine's own registry — which holds the event probe.
        let engine_tools = Tools::from_static([
            Arc::new(EventProbeTool(Arc::clone(&reached))) as Arc<dyn Tool>,
        ]);
        let engine = SkillEngine::new(
            PgPool::connect_lazy("postgres://localhost/ares_test").expect("lazy pool"),
            Arc::new(engine_tools),
            llm,
        );
        let result = engine
            .execute_tenant_tool(&ctx, "acme", "event-probe", json!({"x": 1}))
            .await
            .expect("tools.execute waterfall");

        assert_eq!(result, json!({"cached": true}));
        assert!(!reached.load(Ordering::SeqCst));
    }

    fn test_db_url() -> Option<String> {
        std::env::var("TEST_DATABASE_URL")
            .ok()
            .or_else(|| std::env::var("DATABASE_URL").ok())
    }

    async fn try_test_pool() -> Option<PgPool> {
        let pool = PgPool::connect(&test_db_url()?).await.ok()?;
        Some(pool)
    }

    fn collect_ready_skill_calls<'a>(
        steps: &'a [SkillStep],
        context: &serde_json::Value,
        ready_skill_ids: &mut Vec<&'a str>,
    ) {
        for step in steps {
            match step {
                SkillStep::SkillCall { skill_id, .. } => ready_skill_ids.push(skill_id.as_str()),
                SkillStep::Condition {
                    expression,
                    then_steps,
                } => {
                    if let Some(ready_steps) = ready_then_steps(expression, then_steps, context) {
                        collect_ready_skill_calls(ready_steps, context, ready_skill_ids);
                    }
                }
                SkillStep::ToolCall { .. } | SkillStep::LlmCall { .. } => {}
            }
        }
    }

    #[test]
    fn runtime_tool_error_falls_back_only_when_tool_missing() {
        assert!(runtime_tool_error_allows_static_fallback(
            &AppError::NotFound("Runtime tool not found: calendar".to_string())
        ));
        assert!(!runtime_tool_error_allows_static_fallback(
            &AppError::External("runtime HTTP tool failed".to_string())
        ));
        assert!(!runtime_tool_error_allows_static_fallback(
            &AppError::Configuration("invalid runtime tool config".to_string())
        ));
        assert!(!runtime_tool_error_allows_static_fallback(
            &AppError::Unavailable("runtime tool disabled".to_string())
        ));
    }

    #[tokio::test]
    async fn ensure_tenant_tool_allowed_defaults_to_deny() {
        let Some(pool) = try_test_pool().await else {
            return;
        };
        let tenant_id = format!("tenant-{}", uuid::Uuid::new_v4());
        let err = ensure_tenant_tool_allowed(&pool, &tenant_id, "calendar")
            .await
            .expect_err("missing allowlist row should deny");
        assert!(err.contains("calendar"));
    }

    #[tokio::test]
    async fn ensure_tenant_tool_allowed_accepts_enabled_row() {
        let Some(pool) = try_test_pool().await else {
            return;
        };
        let tenant_id = format!("tenant-{}", uuid::Uuid::new_v4());
        let store = TenantAllowlistStore::new(&pool);
        store
            .allow_tool(&tenant_id, "calendar")
            .await
            .expect("allow tool");
        ensure_tenant_tool_allowed(&pool, &tenant_id, "calendar")
            .await
            .expect("enabled allowlist row should permit tool");
        let _ = store.deny_tool(&tenant_id, "calendar").await;
    }

    #[tokio::test]
    async fn ensure_tenant_model_allowed_defaults_to_deny() {
        let Some(pool) = try_test_pool().await else {
            return;
        };
        let tenant_id = format!("tenant-{}", uuid::Uuid::new_v4());
        let err = ensure_tenant_model_allowed(&pool, &tenant_id, "gpt-4o")
            .await
            .expect_err("missing model allowlist row should deny");
        assert!(err.contains("gpt-4o"));
    }

    #[tokio::test]
    async fn ensure_tenant_model_allowed_accepts_enabled_row() {
        let Some(pool) = try_test_pool().await else {
            return;
        };
        let tenant_id = format!("tenant-{}", uuid::Uuid::new_v4());
        let store = TenantAllowlistStore::new(&pool);
        store
            .allow_model(&tenant_id, "gpt-4o")
            .await
            .expect("allow model");
        ensure_tenant_model_allowed(&pool, &tenant_id, "gpt-4o")
            .await
            .expect("enabled allowlist row should permit model");
        let _ = store.deny_model(&tenant_id, "gpt-4o").await;
    }

    #[test]
    fn skill_llm_cost_estimate_uses_token_counts() {
        assert!(estimated_cost_usd(10, 5) > rust_decimal::Decimal::ZERO);
        assert_eq!(
            estimated_cost_usd(0, 0),
            rust_decimal::Decimal::ZERO
        );
    }

    #[test]
    fn skill_result_token_counts_sums_nested_llm_usage() {
        let result = serde_json::json!({
            "step_0": {
                "content": "top",
                "usage": {"prompt_tokens": 10, "completion_tokens": 4, "total_tokens": 14}
            },
            "step_1": {
                "nested": {
                    "usage": {"prompt_tokens": 3, "completion_tokens": 2, "total_tokens": 5}
                }
            }
        });

        assert_eq!(skill_result_token_counts(&result), (13, 6));
    }

    #[test]
    fn skill_run_history_status_matches_store_validation() {
        assert_eq!(RUN_HISTORY_STATUS_SUCCESS, "success");
    }

    #[test]
    fn skill_step_deserializes_internally_tagged_nested_chain() {
        let steps: Vec<SkillStep> = serde_json::from_value(json!([
            {
                "type": "condition",
                "expression": "step_0.status == 'success'",
                "then_steps": [
                    {
                        "type": "condition",
                        "expression": "step_1.result == 'ready'",
                        "then_steps": [
                            {
                                "type": "skill_call",
                                "skill": "child-skill",
                                "args": {"source": "nested-then"}
                            }
                        ]
                    }
                ]
            }
        ]))
        .expect("nested internally tagged skill steps should deserialize");

        let SkillStep::Condition { then_steps, .. } = &steps[0] else {
            panic!("top-level step should be a condition");
        };
        let SkillStep::Condition { then_steps, .. } = &then_steps[0] else {
            panic!("then_steps should contain the nested condition");
        };
        let SkillStep::SkillCall { skill_id, input } = &then_steps[0] else {
            panic!("nested condition should contain a skill call");
        };
        assert_eq!(skill_id, "child-skill");
        assert_eq!(input, &json!({"source": "nested-then"}));
    }

    #[test]
    fn successful_step_context_adds_status_without_losing_content() {
        let context = successful_step_context(json!({"content": "ready"}));
        assert_eq!(context["status"], RUN_HISTORY_STATUS_SUCCESS);
        assert_eq!(context["content"], "ready");
    }

    #[test]
    fn successful_step_context_preserves_existing_status() {
        let context = successful_step_context(json!({"status": "custom", "content": "ready"}));
        assert_eq!(context["status"], "custom");
        assert_eq!(context["content"], "ready");
    }

    #[test]
    fn status_conditions_match_successful_step_context() {
        let ready_steps = vec![SkillStep::SkillCall {
            skill_id: "child-skill".to_string(),
            input: json!({}),
        }];
        let context = json!({
            "step_0": successful_step_context(json!({"content": "ready"}))
        });
        assert!(ready_then_steps("step_0.status == 'success'", &ready_steps, &context).is_some());
    }

    #[test]
    fn nested_skill_call_is_ready_only_when_each_condition_matches() {
        let steps: Vec<SkillStep> = serde_json::from_value(json!([
            {
                "type": "condition",
                "expression": "step_0.status == 'success'",
                "then_steps": [
                    {
                        "type": "condition",
                        "expression": "step_1.result == 'ready'",
                        "then_steps": [
                            {"type": "skill_call", "skill_id": "child-skill"}
                        ]
                    }
                ]
            }
        ]))
        .expect("nested chain should deserialize");

        let mut ready = Vec::new();
        collect_ready_skill_calls(
            &steps,
            &json!({
                "step_0": {"status": "failed"},
                "step_1": {"content": "ready"}
            }),
            &mut ready,
        );
        assert!(
            ready.is_empty(),
            "nested skill_call metadata must not be considered executable when the outer condition fails"
        );

        collect_ready_skill_calls(
            &steps,
            &json!({
                "step_0": {"status": "success"},
                "step_1": {"content": "not-ready"}
            }),
            &mut ready,
        );
        assert!(
            ready.is_empty(),
            "outer condition readiness alone must not bypass the inner condition"
        );

        collect_ready_skill_calls(
            &steps,
            &json!({
                "step_0": {"status": "success"},
                "step_1": {"content": "ready"}
            }),
            &mut ready,
        );
        assert_eq!(ready, vec!["child-skill"]);
    }

    #[test]
    fn aliases_cover_existing_tool_and_input_shapes() {
        let steps: Vec<SkillStep> = serde_json::from_value(json!([
            {"type": "tool_call", "tool": "lookup", "input": {"q": "x"}},
            {"type": "llm_call", "prompt": "summarize", "model": "fast"},
            {"type": "skill_call", "id": "follow-up", "arguments": {"from": "alias"}}
        ]))
        .expect("aliased skill step fields should deserialize");

        let SkillStep::ToolCall { tool_name, args } = &steps[0] else {
            panic!("first step should be a tool call");
        };
        assert_eq!(tool_name, "lookup");
        assert_eq!(args, &json!({"q": "x"}));

        let SkillStep::LlmCall { model_tier, .. } = &steps[1] else {
            panic!("second step should be an LLM call");
        };
        assert_eq!(model_tier, "fast");

        let SkillStep::SkillCall { skill_id, input } = &steps[2] else {
            panic!("third step should be a skill call");
        };
        assert_eq!(skill_id, "follow-up");
        assert_eq!(input, &json!({"from": "alias"}));
    }

    #[test]
    fn skill_call_depth_guard_allows_boundary_and_rejects_next_level() {
        assert!(validate_skill_call_depth(MAX_SKILL_CALL_DEPTH).is_ok());
        let err = validate_skill_call_depth(MAX_SKILL_CALL_DEPTH + 1)
            .expect_err("depth above the maximum should be rejected");
        assert!(err.contains("Skill call depth exceeded maximum"));
    }

    /// Mock micro client: counts every `generate_with_system` call, answers
    /// with a scripted result (or error), and records the prompts it saw.
    struct AmbientMockClient {
        behavior: Mutex<AmbientBehavior>,
        calls: AtomicUsize,
    }

    enum AmbientBehavior {
        Respond(Box<dyn Fn(&str) -> String + Send + Sync>),
        Fail(String),
    }

    impl AmbientMockClient {
        fn respond<F>(make_answer: F) -> Self
        where
            F: Fn(&str) -> String + Send + Sync + 'static,
        {
            Self {
                behavior: Mutex::new(AmbientBehavior::Respond(Box::new(make_answer))),
                calls: AtomicUsize::new(0),
            }
        }

        fn failing(message: &str) -> Self {
            Self {
                behavior: Mutex::new(AmbientBehavior::Fail(message.to_string())),
                calls: AtomicUsize::new(0),
            }
        }

        fn call_count(&self) -> usize {
            self.calls.load(Ordering::SeqCst)
        }
    }

    #[async_trait::async_trait]
    impl LLMClient for AmbientMockClient {
        async fn generate(&self, prompt: &str) -> ares_types::Result<String> {
            // The completion waterfall calls this; answer with a
            // deterministic echo so tests never touch a real provider.
            Ok(format!("classification-of-{}", prompt))
        }

        async fn generate_with_system(
            &self,
            system: &str,
            prompt: &str,
        ) -> ares_types::Result<String> {
            self.calls.fetch_add(1, Ordering::SeqCst);
            let Ok(guard) = self.behavior.lock() else {
                return Err(AppError::Internal("ambient mock poisoned".into()));
            };
            match &*guard {
                AmbientBehavior::Respond(make_answer) => {
                    // Echo the distinguishing prefix of the system prompt so
                    // tests can tell which micro task answered.
                    let which = if system.contains("classify") {
                        "intent"
                    } else {
                        "tags"
                    };
                    Ok(make_answer(which))
                }
                AmbientBehavior::Fail(message) => {
                    Err(AppError::Internal(message.clone()))
                }
            }
        }

        async fn generate_with_history(
            &self,
            _messages: &[(String, String)],
        ) -> ares_types::Result<LLMResponse> {
            Err(AppError::Internal("unused".into()))
        }

        async fn generate_with_tools(
            &self,
            _prompt: &str,
            _tools: &[ares_types::types::ToolDefinition],
        ) -> ares_types::Result<LLMResponse> {
            Err(AppError::Internal("unused".into()))
        }

        async fn generate_with_tools_and_history(
            &self,
            _messages: &[ares_llm::ConversationMessage],
            _tools: &[ares_types::types::ToolDefinition],
        ) -> ares_types::Result<LLMResponse> {
            Err(AppError::Internal("unused".into()))
        }

        async fn stream(
            &self,
            _prompt: &str,
        ) -> ares_types::Result<Box<dyn futures::Stream<Item = ares_types::Result<String>> + Send + Unpin>>
        {
            Err(AppError::Internal("unused".into()))
        }

        async fn stream_with_system(
            &self,
            _system: &str,
            _prompt: &str,
        ) -> ares_types::Result<Box<dyn futures::Stream<Item = ares_types::Result<String>> + Send + Unpin>>
        {
            Err(AppError::Internal("unused".into()))
        }

        async fn stream_with_history(
            &self,
            _messages: &[(String, String)],
        ) -> ares_types::Result<Box<dyn futures::Stream<Item = ares_types::Result<String>> + Send + Unpin>>
        {
            Err(AppError::Internal("unused".into()))
        }

        fn model_name(&self) -> &str {
            "ambient-mock"
        }
    }

    /// Ambient mock pinned as the Llm test client — the SAME mock serves the
    /// completion (`generate` echoes the prompt) and the enrichment micro
    /// calls (`generate_with_system` answers per system-prompt kind).
    fn ambient_engine(
        mock: Arc<AmbientMockClient>,
        ambient: AmbientEnrichmentConfig,
    ) -> SkillEngine {
        SkillEngine::new(
            PgPool::connect_lazy("postgres://localhost/ares_test").expect("lazy pool"),
            Arc::new(Tools::from_static(Vec::<Arc<dyn Tool>>::new())),
            // Pin the completion AND enrichment paths to the same mock so no
            // real provider is ever reached.
            Arc::new(Llm::from_client(mock as Arc<dyn LLMClient>)),
        )
        .with_ambient_enrichment(ambient)
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn enrichment_off_no_calls() {
        let ctx = Context::new_root();
        let events = ctx.provide(EventsService::new());
        events.on_waterfall("llm.complete".into(), |payload, next| async move {
            next(payload).await
        });

        let calls = Arc::new(AtomicUsize::new(0));
        let seen = Arc::clone(&calls);
        let mock = Arc::new(AmbientMockClient::respond(move |_| {
            seen.fetch_add(1, Ordering::SeqCst);
            "{}".to_string()
        }));
        let engine = ambient_engine(mock, AmbientEnrichmentConfig { enabled: false });

        let (response, metadata) = engine
            .complete_llm_step_with_metadata(
                &ctx,
                &[("user".to_string(), "hello there".to_string())],
                "",
                Some("run-1"),
                Some("acme"),
                Some(3),
                Some("local"),
            )
            .await;

        assert_eq!(
            response.expect("completion ok").content,
            "classification-of-hello there"
        );
        assert!(
            metadata.is_null(),
            "no session metadata should be attached when disabled"
        );
        assert_eq!(
            calls.load(Ordering::SeqCst),
            0,
            "ambient micro client must not be called when disabled"
        );
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn enrichment_on_attaches_metadata() {
        let ctx = Context::new_root();
        let events = ctx.provide(EventsService::new());
        events.on_waterfall("llm.complete".into(), |payload, next| async move {
            next(payload).await
        });

        let calls = Arc::new(AtomicUsize::new(0));
        let seen = Arc::clone(&calls);
        let mock = Arc::new(AmbientMockClient::respond(move |which| {
            seen.fetch_add(1, Ordering::SeqCst);
            if which == "intent" {
                r#"{"intent": "summary", "confidence": 0.87}"#.to_string()
            } else {
                r#"{"tags": ["alpha", "beta"]}"#.to_string()
            }
        }));
        let engine = ambient_engine(mock, AmbientEnrichmentConfig { enabled: true });

        let (response, metadata) = engine
            .complete_llm_step_with_metadata(
                &ctx,
                &[("user".to_string(), "hello there".to_string())],
                "",
                Some("run-1"),
                Some("acme"),
                Some(3),
                Some("local"),
            )
            .await;

        assert_eq!(
            response.expect("completion ok").content,
            "classification-of-hello there"
        );
        assert!(!metadata.is_null(), "session metadata should be attached");
        assert_eq!(metadata["intent"]["intent"], json!("summary"));
        assert_eq!(metadata["intent"]["confidence"], json!(0.87));
        assert_eq!(metadata["tags"]["tags"], json!(["alpha", "beta"]));
        assert_eq!(metadata["run_id"], json!("run-1"));
        assert_eq!(metadata["tenant_id"], json!("acme"));
        assert_eq!(metadata["step_index"], json!(3));
        assert_eq!(
            calls.load(Ordering::SeqCst),
            2,
            "both intent and tag micro calls must fire exactly once"
        );

        // The record path folds non-null session metadata under the
        // `response_payload` key "ambient_enrichment"; null stays absent.
        let folded = fold_ambient_into_response_payload(
            serde_json::json!({
                "content": "answer",
                "finish_reason": "stop",
            }),
            metadata.clone(),
        );
        let ambient = folded
            .get("ambient_enrichment")
            .expect("metadata folded into response_payload");
        assert_eq!(ambient["intent"]["intent"], json!("summary"));
        assert_eq!(ambient["tags"]["tags"], json!(["alpha", "beta"]));
        assert_eq!(folded["content"], json!("answer"));

        let unfolded = fold_ambient_into_response_payload(
            serde_json::json!({"content": "answer"}),
            serde_json::Value::Null,
        );
        assert!(
            unfolded.get("ambient_enrichment").is_none(),
            "null metadata must not add an ambient key"
        );
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn enrichment_failure_silent() {
        let ctx = Context::new_root();
        let events = ctx.provide(EventsService::new());
        events.on_waterfall("llm.complete".into(), |payload, next| async move {
            next(payload).await
        });

        let mock = Arc::new(AmbientMockClient::failing("provider down"));
        let engine = ambient_engine(mock, AmbientEnrichmentConfig { enabled: true });

        let (response, metadata) = engine
            .complete_llm_step_with_metadata(
                &ctx,
                &[("user".to_string(), "hello there".to_string())],
                "",
                Some("run-2"),
                Some("acme"),
                Some(1),
                Some("local"),
            )
            .await;

        assert_eq!(
            response.expect("completion must succeed despite enrichment failure").content,
            "classification-of-hello there"
        );
        assert!(
            metadata.is_null(),
            "failed enrichment is silently skipped, not surfaced"
        );

        // The record still lands on the existing path; folding the null
        // metadata leaves no ambient_enrichment key behind.
        let folded = fold_ambient_into_response_payload(
            serde_json::json!({
                "content": "classification-of-hello there",
                "finish_reason": "stop",
            }),
            metadata,
        );
        assert!(
            folded.get("ambient_enrichment").is_none(),
            "no ambient key when enrichment failed"
        );
        assert_eq!(folded["content"], json!("classification-of-hello there"));
    }

    /// Scripted mock for self-check rounds: `generate` pops the next scripted
    /// outcome per call and records every prompt it saw.
    struct SelfCheckMockClient {
        replies: Mutex<std::collections::VecDeque<Result<String, String>>>,
        prompts: Mutex<Vec<String>>,
    }

    impl SelfCheckMockClient {
        fn scripted(replies: Vec<Result<String, String>>) -> Arc<Self> {
            Arc::new(Self {
                replies: Mutex::new(replies.into()),
                prompts: Mutex::new(Vec::new()),
            })
        }

        fn recorded_prompts(&self) -> Vec<String> {
            self.prompts.lock().expect("prompts").clone()
        }

        fn record(&self, prompt: &str) {
            self.prompts
                .lock()
                .expect("prompts")
                .push(prompt.to_string());
        }

        fn pop_reply(&self) -> Result<String, AppError> {
            let reply = self
                .replies
                .lock()
                .expect("replies")
                .pop_front()
                .unwrap_or_else(|| Ok(String::new()));
            reply.map_err(AppError::Internal)
        }
    }

    #[async_trait::async_trait]
    impl LLMClient for SelfCheckMockClient {
        async fn generate(&self, prompt: &str) -> ares_types::Result<String> {
            self.record(prompt);
            self.pop_reply()
        }

        async fn generate_with_system(
            &self,
            _system: &str,
            _prompt: &str,
        ) -> ares_types::Result<String> {
            Err(AppError::Internal("unused".into()))
        }

        async fn generate_with_history(
            &self,
            _messages: &[(String, String)],
        ) -> ares_types::Result<LLMResponse> {
            Err(AppError::Internal("unused".into()))
        }

        async fn generate_with_tools(
            &self,
            _prompt: &str,
            _tools: &[ares_types::types::ToolDefinition],
        ) -> ares_types::Result<LLMResponse> {
            Err(AppError::Internal("unused".into()))
        }

        async fn generate_with_tools_and_history(
            &self,
            _messages: &[ares_llm::ConversationMessage],
            _tools: &[ares_types::types::ToolDefinition],
        ) -> ares_types::Result<LLMResponse> {
            Err(AppError::Internal("unused".into()))
        }

        async fn stream(
            &self,
            _prompt: &str,
        ) -> ares_types::Result<
            Box<dyn futures::Stream<Item = ares_types::Result<String>> + Send + Unpin>,
        > {
            Err(AppError::Internal("unused".into()))
        }

        async fn stream_with_system(
            &self,
            _system: &str,
            _prompt: &str,
        ) -> ares_types::Result<
            Box<dyn futures::Stream<Item = ares_types::Result<String>> + Send + Unpin>,
        > {
            Err(AppError::Internal("unused".into()))
        }

        async fn stream_with_history(
            &self,
            _messages: &[(String, String)],
        ) -> ares_types::Result<
            Box<dyn futures::Stream<Item = ares_types::Result<String>> + Send + Unpin>,
        > {
            Err(AppError::Internal("unused".into()))
        }

        fn model_name(&self) -> &str {
            "self-check-mock"
        }
    }

    /// Engine pinned to the scripted self-check mock; the lazy pool is never
    /// touched because only `self_check_nested_result` runs.
    fn self_check_engine(mock: Arc<SelfCheckMockClient>, rounds: Option<u32>) -> SkillEngine {
        SkillEngine::new(
            PgPool::connect_lazy("postgres://localhost/ares_test").expect("lazy pool"),
            Arc::new(Tools::from_static(Vec::<Arc<dyn Tool>>::new())),
            Arc::new(Llm::from_client(mock as Arc<dyn LLMClient>)),
        )
        .with_self_check_rounds(rounds)
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn self_check_off_passthrough_identical() {
        // No LLM service traffic at all: the mock is never consulted.
        let ctx = Context::new_root();

        let result = json!({
            "status": "success",
            "content": "flawed answer",
            "usage": {"prompt_tokens": 1},
        });
        let engine = self_check_engine(SelfCheckMockClient::scripted(vec![]), None);
        let out = engine
            .self_check_nested_result(&ctx, "child-skill", &json!({"q": "capital of France"}), result.clone())
            .await;
        assert_eq!(out, result, "off must return the delegated result unchanged");

        // Explicit zero behaves identically to off.
        let engine_zero = self_check_engine(SelfCheckMockClient::scripted(vec![]), Some(0));
        let out_zero = engine_zero
            .self_check_nested_result(&ctx, "child-skill", &json!({"q": "x"}), result.clone())
            .await;
        assert_eq!(out_zero, result);
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn one_round_fixes_flawed_answer() {
        let ctx = Context::new_root();
        let mock = SelfCheckMockClient::scripted(vec![Ok("The capital of France is Paris.".into())]);
        let engine = self_check_engine(Arc::clone(&mock), Some(1));

        let flawed = json!({"status": "success", "content": "The capital of France is Lyon."});
        let out = engine
            .self_check_nested_result(&ctx, "geo-skill", &json!({"task": "capital of France"}), flawed)
            .await;

        assert_eq!(out["content"], json!("The capital of France is Paris."));
        assert_eq!(out["status"], json!("success"), "non-content fields stay intact");

        let prompts = mock.recorded_prompts();
        assert_eq!(prompts.len(), 1, "exactly one round means exactly one LLM call");
        assert!(
            prompts[0].starts_with(SELF_CHECK_TEMPLATE),
            "round prompt must lead with the cache-stable template"
        );
        assert!(prompts[0].contains("geo-skill"));
        assert!(prompts[0].contains("The capital of France is Lyon."));

        // Bare string results are replaced whole.
        let bare = engine
            .self_check_nested_result(&ctx, "s", &json!({}), json!("stale text"))
            .await;
        assert_eq!(bare, json!("stale text"), "verbatim reply keeps the original");
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn llm_failure_keeps_last_good_answer() {
        let ctx = Context::new_root();
        // Round 1 fixes the flaw; round 2's call errors out.
        let mock = SelfCheckMockClient::scripted(vec![
            Ok("corrected answer".into()),
            Err("provider down".into()),
        ]);
        let engine = self_check_engine(Arc::clone(&mock), Some(2));

        let flawed = json!({"status": "success", "content": "broken draft"});
        let out = engine
            .self_check_nested_result(&ctx, "child", &json!({}), flawed.clone())
            .await;

        assert_eq!(
            out["content"],
            json!("corrected answer"),
            "failure mid-loop must keep the last good answer"
        );
        assert_eq!(mock.recorded_prompts().len(), 2, "both rounds attempted");

        // A failure on the FIRST round degrades to the untouched input.
        let first_fails =
            SelfCheckMockClient::scripted(vec![Err("down".into())]);
        let degraded = self_check_engine(first_fails.clone(), Some(3))
            .self_check_nested_result(&ctx, "child", &json!({}), flawed.clone())
            .await;
        assert_eq!(degraded, flawed, "first-round failure passes the original through");
        assert_eq!(first_fails.recorded_prompts().len(), 1);
    }
    // -------------------------------------------------------------------
    // Per-subtask cancel tokens
    // -------------------------------------------------------------------

    /// Fixed run id for cancellation tests; subtask keys are
    /// `"{CANCEL_TEST_RUN_ID}/{skill_id}"`.
    const CANCEL_TEST_RUN_ID: &str = "run-cancel-test";

    /// Shared fixture for the cancellation tests: a real test-pool engine
    /// whose `llm.complete` waterfall records every prompt and can flip
    /// cancel tokens at exact round boundaries.
    struct CancelFixture {
        engine: Arc<SkillEngine>,
        ctx: Arc<Context>,
        pool: PgPool,
        tenant_id: String,
        seen_prompts: Arc<Mutex<Vec<String>>>,
    }

    impl CancelFixture {
        async fn build() -> Option<Self> {
            let pool = try_test_pool().await?;
            let tenant_id = format!("tenant-{}", uuid::Uuid::new_v4());
            TenantAllowlistStore::new(&pool)
                .allow_model(&tenant_id, "test-model")
                .await
                .expect("allow model for tenant");

            // get_client resolves before the waterfall short-circuits, so a
            // fully-registered unreachable provider keeps the hook in charge.
            let mut registry = ProviderRegistry::new();
            registry.register_provider(
                "local",
                ares_llm::ProviderConfig::Ollama {
                    api_key_env: "SKILL_ENGINE_CANCEL_TEST_KEY".to_string(),
                    base_url: "http://127.0.0.1:9".to_string(),
                    default_model: "test-model".to_string(),
                },
            );
            registry.register_model(
                "test-model",
                ares_llm::ModelConfig {
                    provider: "local".to_string(),
                    model: "test-model".to_string(),
                    temperature: 0.0,
                    max_tokens: 32,
                },
            );
            let llm = Arc::new(Llm::new(
                Arc::new(registry),
                Arc::new(ClientPool::with_defaults()),
                None,
            ));
            let engine = Arc::new(SkillEngine::new(
                pool.clone(),
                Arc::new(Tools::from_static(Vec::<Arc<dyn Tool>>::new())),
                llm,
            ));
            let ctx = Context::new_root();
            ctx.provide(EventsService::new());

            Some(Self {
                engine,
                ctx,
                pool,
                tenant_id,
                seen_prompts: Arc::new(Mutex::new(Vec::new())),
            })
        }

        /// Install the recording waterfall. `on_prompt` runs after each LLM
        /// round is recorded — flip a token there to cancel between rounds.
        fn install_prompt_hook(
            &self,
            on_prompt: impl Fn(&str, &SkillEngine) + Send + Sync + Clone + 'static,
        ) {
            let events = self.ctx.get::<EventsService>().expect("events service");
            let prompts_for_handler = Arc::clone(&self.seen_prompts);
            let engine_for_handler = Arc::clone(&self.engine);
            events.on_waterfall("llm.complete".into(), move |payload, _next| {
                let prompts = Arc::clone(&prompts_for_handler);
                let engine = Arc::clone(&engine_for_handler);
                let on_prompt = on_prompt.clone();
                async move {
                    let prompt = payload
                        .get("prompt")
                        .and_then(|v| v.as_str())
                        .unwrap_or("")
                        .to_string();
                    prompts.lock().expect("prompt log").push(prompt.clone());
                    on_prompt(&prompt, &engine);
                    Ok(json!({"content": format!("answer-for-{prompt}")}))
                }
            });
        }

        /// Insert a skill owned by the fixture tenant; returns its id.
        async fn create_skill(&self, label: &str, steps: serde_json::Value) -> String {
            let label = format!("{label}-{}", uuid::Uuid::new_v4());
            let created = SkillStore::new(&self.pool)
                .create_skill(&ares_store::skills::CreateSkillRequest {
                    tenant_id: self.tenant_id.clone(),
                    name: label.clone(),
                    display_name: label.clone(),
                    description: None,
                    skill_type: "workflow".to_string(),
                    steps,
                    input_schema: None,
                    output_schema: None,
                    tools: None,
                    is_public: false,
                    created_by: None,
                })
                .await
                .expect("create skill");
            created.id
        }

        async fn delete_skill(&self, skill_id: &str) {
            let _ = SkillStore::new(&self.pool)
                .delete_skill_for_tenant(&self.tenant_id, skill_id)
                .await;
        }
        /// Insert the `agent_runs` row `run_llm_calls.run_id` requires.
        async fn seed_run_row(&self, run_id: &str) {
            sqlx::query(
                "INSERT INTO tenants (id, name, tier, created_at, updated_at) \
                 VALUES ($1, $2, 'free', 1, 1) ON CONFLICT (id) DO NOTHING",
            )
            .bind(&self.tenant_id)
            .bind(&self.tenant_id)
            .execute(&self.pool)
            .await
            .expect("seed tenant row");
            sqlx::query(
                "INSERT INTO agent_runs (id, tenant_id, agent_name, status, \
                     input_tokens, output_tokens, duration_ms, created_at) \
                 VALUES ($1, $2, 'skill_cancel_test', 'completed', 0, 0, 0, 1) \
                 ON CONFLICT (id) DO NOTHING",
            )
            .bind(run_id)
            .bind(&self.tenant_id)
            .execute(&self.pool)
            .await
            .expect("seed agent run row");
        }

        fn prompts(&self) -> Vec<String> {
            self.seen_prompts.lock().expect("prompt log").clone()
        }
    }

    fn two_llm_rounds(first: &str, second: &str) -> serde_json::Value {
        json!([
            {"type": "llm_call", "prompt": first, "model_tier": "test-model"},
            {"type": "llm_call", "prompt": second, "model_tier": "test-model"},
        ])
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn cancel_token_aborts_subtask_between_rounds() {
        let Some(fixture) = CancelFixture::build().await else {
            return;
        };
        let skill_id = fixture
            .create_skill(
                "cancel-between-rounds",
                two_llm_rounds("cancel-round-0", "cancel-round-1"),
            )
            .await;
        let subtask_id = format!("{}/{}", CANCEL_TEST_RUN_ID, skill_id);
        let hook_subtask_id = subtask_id.clone();
        fixture.install_prompt_hook(move |prompt, engine| {
            if prompt.contains("round-0") {
                // External trigger racing the run: flips between rounds.
                engine.cancel_subtask(&hook_subtask_id);
            }
        });

        fixture.seed_run_row(CANCEL_TEST_RUN_ID).await;
        let err = fixture
            .engine
            .execute_skill(
                &skill_id,
                &fixture.tenant_id,
                json!({}),
                CANCEL_TEST_RUN_ID,
                &fixture.ctx,
            )
            .await
            .expect_err("cancelled execution must abort");

        assert!(
            err.contains(SUBTASK_CANCELLED_MARKER),
            "abort must carry the stable marker, got {err}"
        );
        assert_eq!(
            fixture.prompts(),
            vec!["cancel-round-0".to_string()],
            "execution must abort between rounds, not mid-call or after"
        );
        assert!(
            !fixture.engine.cancel_subtask(&subtask_id),
            "sticky token flips exactly once"
        );

        fixture.delete_skill(&skill_id).await;
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn cancelled_result_not_integrated() {
        let Some(fixture) = CancelFixture::build().await else {
            return;
        };
        let child_id = fixture
            .create_skill(
                "cancel-child",
                two_llm_rounds("child-round-0", "child-round-1"),
            )
            .await;
        let parent_steps = json!([
            {"type": "llm_call", "prompt": "parent-zero", "model_tier": "test-model"},
            {"type": "skill_call", "skill_id": child_id, "input": {}},
            {"type": "llm_call", "prompt": "parent-tail", "model_tier": "test-model"},
        ]);
        let parent_id = fixture.create_skill("cancel-parent", parent_steps).await;
        let hook_child_key = format!("{}/{}", CANCEL_TEST_RUN_ID, child_id);
        fixture.install_prompt_hook(move |prompt, engine| {
            if prompt.contains("child-round-0") {
                // The child produced one partial round; abort it before its
                // result could integrate into the parent context.
                engine.cancel_subtask(&hook_child_key);
            }
        });

        fixture.seed_run_row(CANCEL_TEST_RUN_ID).await;
        let err = fixture
            .engine
            .execute_skill(
                &parent_id,
                &fixture.tenant_id,
                json!({}),
                CANCEL_TEST_RUN_ID,
                &fixture.ctx,
            )
            .await
            .expect_err("cancelled delegation must unwind the parent run");

        assert!(
            err.contains(SUBTASK_CANCELLED_MARKER),
            "unwind must carry the stable marker, got {err}"
        );
        assert_eq!(
            fixture.prompts(),
            vec![
                "parent-zero".to_string(),
                "child-round-0".to_string(),
            ],
            "parent tail and child round 1 must never start after cancellation"
        );
        assert!(
            !err.contains("answer-for-child-round-0"),
            "partial child output must not leak into the reported result"
        );

        fixture.delete_skill(&parent_id).await;
        fixture.delete_skill(&child_id).await;
    }
}