agent86-core 1.2.3

The agent behind agent86: a safe agent runtime you can embed
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
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
//! The ReAct loop, ported from Python's `orchestration/loop.py` and
//! `orchestration/circuit.py`.
//!
//! One user turn: call the model, run the tools it asks for, feed the results
//! back, and repeat until it answers without tools. A circuit breaker bounds the
//! turn by steps, cost, wall-clock time, and consecutive tool errors. Tool
//! activity is reported inline as `[tool] ...` lines, exactly as Python streams it.
//!
//! Side-effecting calls pass through the approval gate before they run.
//!
//! Guardrails scan the goal, each answer, and each tool observation.
//!
//! A turn can be cancelled from another thread or task ([`CancelToken`]). It stops at
//! the next safe point: before a model call, while a response streams, and between
//! tool calls, never in the middle of one. The partial exchange is kept.
//!
//! Every request is fitted to the model's context window (`memory::working`).
//! Before a model call, a conversation over budget has its oldest span replaced
//! by a model-written summary (`[limits] compaction = "summarize"`), or dropped.
//!
//! `delegate` runs a sub-agent (`crate::subagent`) inside the batch's sequential
//! part, and what it spends is added to the turn.

use crate::approval::ApprovalGate;
use crate::config::EgressMode;
use crate::config::{AgentsConfig, CompactionMode, LimitsConfig};
use crate::guardrails::Guardrails;
use crate::limits::LimitKind;
use crate::memory::sizing::{
    aged_view, calibrated_budget, result_cap, shorten_middle, sized_reserve,
};
use crate::memory::working::{
    apply_progress, apply_summary, compacted_notice, compaction_failed_notice, compaction_plan,
    count_spec_tokens, count_tokens, fit_pinned, fits, python_strip, summary_request_text,
    trimmed_notice, Cut, Fitted, KEEP_RECENT, SUMMARY_MAX_TOKENS, SUMMARY_RETRY_NOTE,
    SUMMARY_SYSTEM_PROMPT,
};
use crate::observability::Recorder;
use crate::prompt::time_note;
use crate::provider::reasoning::REASONING_LOOP;
use crate::provider::{Provider, ProviderError};
use crate::pyjson::{python_float_repr, python_json_dumps};
use crate::router::Router;
use crate::session::AgentState;
use crate::tools::{ToolContext, ToolRegistry};
use crate::turn::TurnSummary;
use crate::types::{
    python_repr, AgentPhase, CompletionRequest, Message, Role, Step, ToolCall, ToolResult,
    ToolSpec, Usage,
};
use crate::vault::StreamRedactor;
use futures_util::StreamExt;
use serde_json::{json, Value};
use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};
use thiserror::Error;

/// What the harness says to a model that ran out of output tokens mid-answer.
pub const CONTINUE_PROMPT: &str = "Continue exactly where you left off; do not repeat.";

/// How many times one turn may ask for more after hitting the output cap.
pub const MAX_CONTINUATIONS: u64 = 3;

/// Tokens held back in a summary request for the chat template and the
/// estimate's error, beyond Python.
const SUMMARY_MARGIN_TOKENS: u64 = 256;

/// The least transcript a summary request is given, however small the window.
const MIN_TRANSCRIPT_TOKENS: u64 = 1_024;

/// Why [`Harness::write_summary`] wrote nothing.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum SummaryError {
    Cancelled,
    /// The provider failed; the text is what the trace records.
    Failed(String),
    /// Both attempts came back empty.
    Empty,
}

/// What [`Harness::write_summary`] produced, and what it spent doing so.
#[derive(Debug, Clone)]
pub(crate) struct WrittenSummary {
    pub outcome: Result<String, SummaryError>,
    pub usage: Usage,
    pub model: String,
}

/// A compaction to announce: the notice, and the summary when one was written.
struct Compaction {
    notice: String,
    summary: Option<String>,
}

/// What a turn keeps about its place in the history while it fits and
/// compacts it, beyond Python.
struct TurnWindow<'g> {
    /// The turn's message as the user sent it, kept verbatim through compactions.
    goal_text: &'g str,
    /// Where the turn's message is in the history.
    goal: usize,
    /// Where a continuation in progress began; compaction never reaches it.
    continuation: Option<usize>,
    /// After a failed summary, the history size at which one is tried again.
    retry_at: Option<u64>,
    /// Whether this turn has said that fitting leaves messages out.
    trim_announced: bool,
    /// How many messages the last request left out.
    trimmed: usize,
    /// How many tool results the last request sent aged (`crate::memory::sizing`).
    aged: usize,
}

/// The user-visible line announcing continuation `index`.
pub fn continuation_notice(index: u64) -> String {
    format!("\n[continuation {index}/{MAX_CONTINUATIONS}]\n")
}

/// What the harness asks, once, of a model whose final reply was empty. Beyond Python.
pub const ANSWER_PROMPT: &str =
    "Your last reply was empty. Give the user your answer now, as plain text.";

/// Said when an empty final reply is sent back for an answer.
pub const EMPTY_ANSWER_NOTICE: &str = "\n[no answer; asking the model again]\n";

/// Said when the model still gave no answer and its reasoning is shown instead.
pub const THINKING_ANSWER_NOTICE: &str = "\n[no answer; showing the model's reasoning]\n";

/// Said when the model still gave no answer and had no reasoning to show.
pub const NO_ANSWER_NOTICE: &str = "\n[the model gave no answer]\n";

/// Said when the provider stopped a reply whose reasoning kept repeating itself.
/// Beyond Python.
pub const REASONING_LOOP_NOTICE: &str =
    "\n[the model's reasoning was repeating itself; stopped it]\n";

/// What a cancelled turn says, as Python's `_cancelled`.
pub const CANCELLED_NOTICE: &str = "\n[cancelled]\n";

/// Asks the in-flight turn to stop at its next safe point, as Python's `Harness.cancel`.
///
/// Clones share one flag, so a signal handler or another task can hold one. Cancelling
/// when no turn is running is harmless: every turn clears the flag on entry.
#[derive(Clone, Default)]
pub struct CancelToken {
    inner: Arc<CancelInner>,
}

impl std::fmt::Debug for CancelToken {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("CancelToken")
            .field("cancelled", &self.is_cancelled())
            .finish()
    }
}

#[derive(Default)]
struct CancelInner {
    flag: AtomicBool,
    notify: tokio::sync::Notify,
}

impl CancelToken {
    pub fn cancel(&self) {
        self.inner.flag.store(true, Ordering::SeqCst);
        self.inner.notify.notify_waiters();
    }

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

    pub fn clear(&self) {
        self.inner.flag.store(false, Ordering::SeqCst);
    }

    /// Resolves once [`CancelToken::cancel`] has been called.
    pub async fn cancelled(&self) {
        loop {
            let notified = self.inner.notify.notified();
            tokio::pin!(notified);
            // Registered before the flag is read, so a cancel in between is not missed.
            notified.as_mut().enable();
            if self.is_cancelled() {
                return;
            }
            notified.await;
        }
    }
}

/// One thing a running turn reports. Each event carries the text that `run` and the
/// plain REPL stream for it, so every surface sees the same output.
///
/// `#[non_exhaustive]` (since 1.2.0, ADR 0004): later releases add kinds of event,
/// so a `match` outside this crate needs a `_` arm. [`TurnEvent::text`] is what any
/// event adds to the turn's output, whatever its kind.
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub enum TurnEvent<'a> {
    /// The model's text (or the ingress guardrail's refusal, which stands in for it).
    Text(&'a str),
    /// A notice from the harness (`[cancelled]`, `[guardrail] ...`, a continuation or
    /// compaction note): part of the output, but never part of the answer, so `run`
    /// can keep it off stdout.
    Notice(&'a str),
    /// A piece of the model's reasoning, already redacted. Not part of the turn's
    /// output: only the interface shows it.
    Thinking(&'a str),
    /// A tool call, announced before its batch runs; `text` is its `[tool] name(...)` line.
    ToolCall { call: &'a ToolCall, text: &'a str },
    /// A call's result, in call order. `observation` is what the model is shown, after
    /// the observation guardrail; `text` is the `[tool] name -> summary` line.
    ToolResult {
        call: &'a ToolCall,
        result: &'a ToolResult,
        observation: &'a str,
        text: &'a str,
    },
    /// Beyond Python: a model call finished. `step` is what it used; `session` is
    /// the session's usage with it added (sub-agents' spend joins when their batch
    /// ends). Adds nothing to the turn's output.
    Usage {
        step: &'a crate::types::Usage,
        session: &'a crate::types::Usage,
    },
    /// The history was compacted: `notice` is what the other surfaces stream (a
    /// notice, as before), and `summary` the summary written, `None` when it could
    /// not be and the history was trimmed instead (beyond Python: an editor shows it
    /// as ACP's `compaction_update`).
    Compacted {
        notice: &'a str,
        summary: Option<&'a str>,
    },
    /// Beyond Python (ADR 0005): one A2UI message a `render_ui` call drew,
    /// validated, for the surface to draw. Sent after the call's `ToolCall` and
    /// before its `ToolResult`, in order. Adds nothing to the turn's output.
    #[cfg(feature = "agent86ui")]
    Ui(&'a agent86_ui::AgentMessage),
}

impl<'a> TurnEvent<'a> {
    /// The text this event adds to the turn's output.
    pub fn text(&self) -> &'a str {
        match *self {
            Self::Text(text)
            | Self::Notice(text)
            | Self::ToolCall { text, .. }
            | Self::ToolResult { text, .. }
            | Self::Compacted { notice: text, .. } => text,
            Self::Thinking(_) | Self::Usage { .. } => "",
            #[cfg(feature = "agent86ui")]
            Self::Ui(_) => "",
        }
    }
}

/// A turn could not be completed.
#[derive(Debug, Clone, PartialEq, Error)]
pub enum HarnessError {
    #[error(transparent)]
    Provider(#[from] ProviderError),

    #[error("Circuit tripped: {0}")]
    CircuitTripped(String),

    #[error(
        "The '{provider}' provider ended its stream for model {} without a final completion. \
         The response was truncated; retry, or check the endpoint's streaming implementation.",
        python_repr(model)
    )]
    NoFinalCompletion { provider: String, model: String },
}

impl HarnessError {
    /// The name of the exception Python raises for this failure, which its
    /// full-screen interface shows (`error: ProviderError: ...`).
    pub fn python_type(&self) -> &'static str {
        match self {
            Self::Provider(_) => "ProviderError",
            Self::CircuitTripped(_) | Self::NoFinalCompletion { .. } => "HarnessError",
        }
    }
}

/// Bounds one turn: steps, cumulative cost, wall-clock time, and the tool-error streak.
pub struct CircuitBreaker {
    limits: LimitsConfig,
    steps: u32,
    cost_usd: f64,
    consecutive_errors: u32,
    started: Instant,
}

impl CircuitBreaker {
    pub fn new(limits: &LimitsConfig) -> Self {
        Self {
            limits: limits.clone(),
            steps: 0,
            cost_usd: 0.0,
            consecutive_errors: 0,
            started: Instant::now(),
        }
    }

    pub fn steps(&self) -> u32 {
        self.steps
    }

    /// The step budget.
    pub fn max_steps(&self) -> u32 {
        self.limits.max_steps
    }

    /// Raise (or lower) the step budget mid-run, when the user raises it.
    pub fn set_max_steps(&mut self, max_steps: u32) {
        self.limits.max_steps = max_steps;
    }

    /// The step budget is spent, so the next [`CircuitBreaker::before_step`] trips.
    pub fn out_of_steps(&self) -> bool {
        self.steps >= self.limits.max_steps
    }

    /// Fail if starting another model call would breach a bound.
    pub fn before_step(&self) -> Result<(), HarnessError> {
        let limits = &self.limits;
        if self.steps >= limits.max_steps {
            return Err(trip(format!(
                "step budget reached ({} steps)",
                limits.max_steps
            )));
        }
        if limits.max_cost_usd > 0.0 && self.cost_usd >= limits.max_cost_usd {
            return Err(trip(format!(
                "cost cap reached (${:.4} >= ${})",
                self.cost_usd,
                python_float_repr(limits.max_cost_usd)
            )));
        }
        let wall_clock = Duration::from_secs(limits.max_wall_clock_s);
        if limits.max_wall_clock_s > 0 && self.started.elapsed() >= wall_clock {
            return Err(trip(format!(
                "wall-clock limit reached ({}s)",
                limits.max_wall_clock_s
            )));
        }
        Ok(())
    }

    pub fn record_step(&mut self, usage: &Usage) {
        self.steps += 1;
        self.cost_usd += usage.cost_usd;
    }

    /// Count spending that is not a step, such as a compaction summary.
    /// What the turn has spent so far.
    pub fn cost(&self) -> f64 {
        self.cost_usd
    }

    pub fn record_cost(&mut self, cost_usd: f64) {
        self.cost_usd += cost_usd;
    }

    pub fn record_tool_result(&mut self, ok: bool) -> Result<(), HarnessError> {
        if ok {
            self.consecutive_errors = 0;
            return Ok(());
        }
        self.consecutive_errors += 1;
        if self.consecutive_errors > self.limits.max_consecutive_errors {
            return Err(trip(format!(
                "aborted after {} consecutive tool errors",
                self.consecutive_errors
            )));
        }
        Ok(())
    }
}

fn trip(reason: String) -> HarnessError {
    HarnessError::CircuitTripped(reason)
}

/// Everything one turn needs.
pub struct Harness {
    /// The session's model; the REPL's `/model` replaces it between turns. With
    /// triage routing on, each turn may use another (see [`Harness::active_provider`]).
    pub provider: Arc<dyn Provider>,
    /// Triage routing (`crate::router`); [`Router::default`] never routes.
    pub router: Router,
    /// OpenTelemetry spans (`crate::otel`); [`crate::otel::Tracer::default`] records none.
    pub tracer: crate::otel::Tracer,
    pub registry: ToolRegistry,
    pub context: ToolContext,
    pub limits: LimitsConfig,
    /// `[agents]`: whether `delegate` runs sub-agents, how deep, and how long.
    pub agents: AgentsConfig,
    pub system: Message,
    /// The configured output cap, as Python's `max_output_tokens_for`; a session
    /// override may replace it (see [`Harness::output_cap`]).
    pub max_tokens: u32,
    /// Limits overridden for the session, and the hook that offers to raise one
    /// when a turn reaches it (`crate::limits`).
    pub session_limits: crate::limits::SessionLimits,
    /// Tokens of context the model can hold (`context_window_for`); the REPL's
    /// `/model` updates it with the provider.
    pub context_window: u64,
    /// Decides whether side-effecting calls may run.
    pub gate: ApprovalGate,
    /// Ingress, egress, and observation scanning.
    pub guardrails: Guardrails,
    /// The flight recorder; [`Recorder::disabled`] writes nothing.
    pub recorder: Recorder,
    /// Live MCP servers; their section of the system message is rebuilt for every
    /// request, because servers drop, reconnect, and change their tools mid-turn.
    pub mcp: Option<std::sync::Arc<crate::mcp::McpManager>>,
    /// Long-term memory: past episodes are recalled into each turn's system
    /// message and every finished turn is recorded; `None` runs statelessly.
    pub memory: Option<crate::session::SessionStore>,
    /// Stops the running turn at its next safe point.
    pub cancel: CancelToken,
    /// Specialist sub-agents' state (beyond Python): notices for the user and the
    /// approvals of project specialties (`crate::subagent`).
    pub subagents: crate::subagent::SubagentState,
    /// Reported against estimated input tokens, per model (beyond Python;
    /// `crate::memory::sizing::Calibration`).
    pub calibration: crate::memory::sizing::Calibration,
    /// The verification gate (beyond Python; `crate::verify`): off by default.
    pub verify: crate::verify::Verify,
}

impl Harness {
    /// The system message for the next request: the prompt plus, when MCP servers
    /// are live, their section, then the turn's recall note, as Python's
    /// `_system_content`. Beyond Python, the names of the secrets the user entered
    /// come before the recall note, when there are any.
    pub(crate) fn system_message(&self, extra: Option<&str>) -> Message {
        let offered = self.delegation_offered(&self.context, 0);
        // Beyond Python: under `run --agent`, the specialty's filtered prompt.
        if let Some((persona, preloaded, _)) = self.subagents.main() {
            return self.filtered_system(&persona, &self.context, &preloaded, offered, extra);
        }
        self.system_for(&self.context, offered, extra)
    }

    /// [`Harness::system_message`] for `ctx`, an unrestricted context (the main
    /// loop's, or a plain sub-agent's fork of it). Beyond Python, when `delegate`
    /// is offered, the specialist sub-agents are listed after the MCP section.
    pub(crate) fn system_for(
        &self,
        ctx: &ToolContext,
        delegation_offered: bool,
        extra: Option<&str>,
    ) -> Message {
        let mut system = self.system.clone();
        if let Some(section) = self
            .mcp
            .as_ref()
            .map(|mcp| mcp.prompt_section())
            .filter(|section| !section.is_empty())
        {
            system.content = format!("{}\n\n{section}", system.content);
        }
        if delegation_offered {
            if let Some(section) = self.specialty_listing(ctx) {
                system.content = format!("{}\n\n{section}", system.content);
            }
        }
        if let Some(section) = ctx.secrets.prompt_section() {
            system.content = format!("{}\n\n{section}", system.content);
        }
        // Beyond Python (ADR 0006): how to ask the user, wherever someone can
        // answer and the context may ask.
        if crate::ask::reachable(ctx) && ctx.allowed_tool(crate::ask::ASK_USER) {
            #[cfg(feature = "agent86ui")]
            let drawing = ctx.ui.is_some() && ctx.allowed_tool(crate::agent86ui::RENDER_UI);
            #[cfg(not(feature = "agent86ui"))]
            let drawing = false;
            system.content = format!(
                "{}\n\n{}",
                system.content,
                crate::ask::prompt_section(drawing)
            );
        }
        // Beyond Python: agent86UI's section, only where a renderer is and the
        // context may draw.
        #[cfg(feature = "agent86ui")]
        if let Some(ui) = ctx.ui.as_deref() {
            if ctx.allowed_tool(crate::agent86ui::RENDER_UI) {
                system.content = format!("{}\n\n{}", system.content, ui.prompt_section());
            }
        }
        if let Some(extra) = extra.filter(|extra| !extra.is_empty()) {
            system.content = format!("{}\n\n{extra}", system.content);
        }
        system
    }

    /// Conversation tokens available for this request, as Python's
    /// `_context_budget`: the window minus what the request spends before any
    /// history (the system message and the tool catalogue, both of which change at
    /// runtime), minus the room the response needs.
    ///
    /// Beyond Python, the budget shrinks for a model whose reported input tokens
    /// have run above the estimate (`crate::memory::sizing::Calibration`).
    pub(crate) fn context_budget(&self, system: &Message, specs: &[ToolSpec]) -> u64 {
        self.context_budget_with(
            system,
            specs,
            self.active_context_window(),
            self.output_cap(),
            self.active_provider().model(),
        )
    }

    /// A tool result cut to its share of `budget`, unless `[limits] context_sizing`
    /// is off (`crate::memory::sizing::result_cap`).
    pub(crate) fn cap_result(&self, content: String, budget: u64) -> String {
        if !self.limits.context_sizing {
            return content;
        }
        shorten_middle(
            &content,
            result_cap(budget, self.context.policy.max_output_chars),
        )
    }

    /// `messages` as a request carries them: aged once the window fills, unless
    /// `[limits] context_sizing` is off (`crate::memory::sizing::aged_view`).
    pub(crate) fn request_view(
        &self,
        messages: &[Message],
        budget: u64,
    ) -> crate::memory::sizing::Aged {
        if self.limits.context_sizing {
            aged_view(messages, budget)
        } else {
            crate::memory::sizing::Aged {
                messages: messages.to_vec(),
                aged: 0,
            }
        }
    }

    /// [`Harness::context_budget`] for `model` with this window and output cap (a
    /// sub-agent, perhaps on a model of its own). Beyond Python, with
    /// `[limits] context_sizing` on, it is calibrated for the model
    /// (`crate::memory::sizing::calibrated_budget`) and the reserve is held to a
    /// share of the window (`sized_reserve`; the output cap already is).
    pub(crate) fn context_budget_with(
        &self,
        system: &Message,
        specs: &[ToolSpec],
        window: u64,
        output_cap: u32,
        model: &str,
    ) -> u64 {
        let overhead = count_tokens(std::slice::from_ref(system)) + count_spec_tokens(specs);
        let (ratio, reserve) = if self.limits.context_sizing {
            (
                self.calibration.ratio(model),
                sized_reserve(self.limits.context_reserve_tokens, window),
            )
        } else {
            (1.0, self.limits.context_reserve_tokens)
        };
        calibrated_budget(
            window,
            overhead,
            reserve,
            u64::from(output_cap),
            self.limits.max_context_tokens,
            ratio,
        )
    }

    /// The tools a main-loop request offers: the registry's, narrowed under
    /// `run --agent` to what the specialty may call.
    fn request_tools(&self) -> Vec<ToolSpec> {
        let mut tools = self.registry.specs();
        tools.retain(|spec| self.context.scope_allows(&spec.name));
        tools
    }

    /// Write a summary of `messages` with the compaction summarizer (its prompt,
    /// the cheap route when routing is on), as Python's summary call, with two
    /// things beyond Python: the transcript is cut to fit the summarizer's own
    /// window ([`fit_transcript`](crate::memory::working::fit_transcript)), and an
    /// empty reply (a thinking model can spend the whole cap reasoning) is asked
    /// for once more, with a larger cap and [`SUMMARY_RETRY_NOTE`]. The usage of
    /// every attempt is returned, also on failure, since it was spent.
    pub(crate) async fn write_summary(&self, messages: &[Message]) -> WrittenSummary {
        let (summarizer, window) = self.summary_model();
        let model = summarizer.model().to_owned();
        let first_cap = SUMMARY_MAX_TOKENS * 2;
        // Four times the first cap, but no more than a quarter of the window, so the
        // retry still leaves the transcript room; never less than the first cap
        // unless that is over half the window.
        let share = |divisor: u64| u32::try_from(window / divisor).unwrap_or(u32::MAX);
        let retry_cap = (SUMMARY_MAX_TOKENS * 8)
            .min(share(4))
            .max(first_cap.min(share(2)));
        let instructions = message(Role::System, SUMMARY_SYSTEM_PROMPT);
        let overhead = count_tokens(std::slice::from_ref(&instructions))
            + count_tokens(&[message(Role::User, SUMMARY_RETRY_NOTE)])
            + SUMMARY_MARGIN_TOKENS;
        let room = window
            .saturating_sub(overhead + u64::from(retry_cap))
            .max(MIN_TRANSCRIPT_TOKENS);
        let transcript =
            summary_request_text(messages, usize::try_from(room * 4).unwrap_or(usize::MAX));
        let mut usage = Usage::default();
        let mut attempt = 0;
        loop {
            let (text, cap) = if attempt == 0 {
                (transcript.clone(), first_cap)
            } else {
                (format!("{transcript}\n\n{SUMMARY_RETRY_NOTE}"), retry_cap)
            };
            let request = CompletionRequest {
                model: model.clone(),
                messages: vec![instructions.clone(), message(Role::User, &text)],
                tools: Vec::new(),
                temperature: 0.0,
                // A summary is a bounded artefact; it should not cost more than the span.
                max_tokens: Some(cap),
                stream: false,
            };
            let completion = tokio::select! {
                biased;
                // The next safe point sees the cancel and closes the turn out.
                () = self.cancel.cancelled() => {
                    return WrittenSummary { outcome: Err(SummaryError::Cancelled), usage, model };
                }
                completion = summarizer.complete(request) => completion,
            };
            let completion = match completion {
                Ok(completion) => completion,
                Err(error) => {
                    return WrittenSummary {
                        outcome: Err(SummaryError::Failed(format!("ProviderError: {error}"))),
                        usage,
                        model,
                    }
                }
            };
            usage = usage + completion.usage;
            let text = python_strip(&completion.text).to_owned();
            if !text.is_empty() {
                return WrittenSummary {
                    outcome: Ok(text),
                    usage,
                    model,
                };
            }
            if attempt > 0 {
                return WrittenSummary {
                    outcome: Err(SummaryError::Empty),
                    usage,
                    model,
                };
            }
            attempt += 1;
        }
    }

    /// Replace the oldest over-budget span with a summary, in place, before a model
    /// call, as Python's `_compact_if_needed`. Dropping the oldest turns (still
    /// available as `compaction = "drop"`) makes a long session forget its own
    /// goal; a summary keeps the goal, the decisions, and the paths found.
    ///
    /// Beyond Python, the span may reach inside the current turn
    /// ([`compaction_plan`]): the turn's message is kept verbatim with the summary
    /// appended, so a long single turn compacts too instead of losing its goal to
    /// fitting. A failed or empty summary is not tried again until the history has
    /// grown by a quarter of the budget, so a summarizer that cannot answer is not
    /// asked every step.
    ///
    /// Never fails the turn: a summary that cannot be written falls back to the drop
    /// that fitting the request does anyway, and says so.
    async fn compact_if_needed(
        &self,
        state: &mut AgentState,
        extra_system: Option<&str>,
        summary: &mut TurnSummary,
        breaker: &mut CircuitBreaker,
        window: &mut TurnWindow<'_>,
    ) -> Option<Compaction> {
        if self.limits.compaction != CompactionMode::Summarize {
            return None;
        }
        let system = self.system_message(extra_system);
        let budget = self.context_budget(&system, &self.request_tools());
        // Beyond Python: judged on what a request would carry, so a history that
        // fits once its older results are aged costs no summary.
        if fits(&self.request_view(&state.messages, budget).messages, budget) {
            return None;
        }
        let tokens = count_tokens(&state.messages);
        if window.retry_at.is_some_and(|at| tokens < at) {
            return None;
        }
        // Everything left is recent or the current turn's own message and tool
        // blocks: nothing may be compacted, so fitting trims and the provider's
        // limits take over.
        let cut = compaction_plan(
            &state.messages,
            budget,
            KEEP_RECENT,
            window.goal,
            window.continuation,
        )?;
        let sid = state.session_id.clone();
        let end = cut.cut();
        let replaced = cut.replaced();
        let prefix = &state.messages[..end];
        let dropped_tokens = count_tokens(prefix);
        let written = self.write_summary(prefix).await;
        summary.add_usage(&written.usage);
        breaker.record_cost(written.usage.cost_usd);
        let failed = |window: &mut TurnWindow<'_>, status: &str, error: Option<String>| {
            window.retry_at = Some(tokens + (budget / 4).max(1));
            let mut fields = vec![
                ("status", json!(status)),
                ("fallback", json!("drop")),
                ("dropped", json!(replaced)),
            ];
            if let Some(error) = error {
                fields.push(("error", json!(error)));
            }
            self.recorder.event(&sid, "compaction", fields);
            Some(Compaction {
                notice: compaction_failed_notice(replaced),
                summary: None,
            })
        };
        let text = match written.outcome {
            Ok(text) => text,
            Err(SummaryError::Cancelled) => return None,
            Err(SummaryError::Failed(error)) => return failed(window, "failed", Some(error)),
            Err(SummaryError::Empty) => return failed(window, "empty", None),
        };
        window.retry_at = None;

        // The originals are archived before they leave the live conversation.
        if let Some(memory) = &self.memory {
            let _ = memory.record_compaction(&sid, &text, prefix);
        }
        let before = state.messages.len();
        let kept = state.messages.split_off(end);
        state.messages = match cut {
            Cut::Earlier(_) => apply_summary(&text, kept),
            Cut::WithinTurn { .. } => apply_progress(window.goal_text, &text, kept),
        };
        let removed = before - state.messages.len();
        // Indices into the history move with it. (Python keeps the old ones, which
        // go stale after a compaction mid-turn.)
        window.goal = match cut {
            Cut::Earlier(_) => window.goal - removed,
            Cut::WithinTurn { .. } => 0,
        };
        if let Some(start) = window.continuation.as_mut() {
            *start -= removed;
        }
        summary.compactions += 1;
        let mut fields = vec![
            ("status", json!("ok")),
            ("dropped", json!(replaced)),
            ("dropped_tokens", json!(dropped_tokens)),
            (
                "summary_tokens",
                json!(count_tokens(&[message(Role::User, &text)])),
            ),
            ("kept", json!(state.messages.len())),
            ("model", json!(written.model)),
        ];
        if matches!(cut, Cut::WithinTurn { .. }) {
            fields.push(("scope", json!("turn")));
        }
        self.recorder.event(&sid, "compaction", fields);
        // Saved at once: a resume must see the compacted history, not a stale copy
        // that would re-inflate the context on the next turn.
        if let Some(memory) = &self.memory {
            let _ = memory.persist(state);
        }
        Some(Compaction {
            notice: compacted_notice(replaced),
            summary: Some(text),
        })
    }

    /// Run one user turn on `state`, passing every piece of streamed text,
    /// including tool notices, to `on_text`, and return everything streamed.
    ///
    /// The turn's messages and steps are appended to `state`, its usage added,
    /// and its summary left on `state.last_turn`. A failed turn still leaves
    /// `state` in the `error` phase with its summary, so it can be persisted.
    pub async fn run_turn(
        &self,
        state: &mut AgentState,
        goal: &str,
        on_text: impl FnMut(&str),
    ) -> Result<String, HarnessError> {
        self.run_turn_as(state, goal, goal, on_text).await
    }

    /// [`Harness::run_turn`] where the model sees `user_text` (for a prompt with
    /// `@file` mentions, the expanded text) and the trace records `display_text`,
    /// what the user typed, as Python's `display_text` argument.
    pub async fn run_turn_as(
        &self,
        state: &mut AgentState,
        user_text: &str,
        display_text: &str,
        mut on_text: impl FnMut(&str),
    ) -> Result<String, HarnessError> {
        self.run_turn_events(state, user_text, display_text, |event| {
            on_text(event.text())
        })
        .await
    }

    /// [`Harness::run_turn_as`] reporting [`TurnEvent`]s, so an interface can show
    /// tool calls without parsing the `[tool] ...` lines. Every event carries the text
    /// the other entry points stream, in the same order.
    pub async fn run_turn_events(
        &self,
        state: &mut AgentState,
        user_text: &str,
        display_text: &str,
        on_event: impl FnMut(TurnEvent<'_>),
    ) -> Result<String, HarnessError> {
        self.run_turn_with(state, user_text, display_text, None, on_event)
            .await
    }

    /// A turn with `skill` chosen by the user (`/<name> [task]`, `/skill <name>`,
    /// `run --skill`), beyond Python: the model gets the skill's instructions ahead
    /// of `task` in the user's message ([`crate::skills::skill_request`]), and the
    /// skill is active from the first step, so its `allowed-tools` apply at once.
    /// Everything else is [`Harness::run_turn_events`]; `display_text` is what the
    /// user typed, for the trace.
    pub async fn run_skill_turn(
        &self,
        state: &mut AgentState,
        skill: &Arc<crate::skills::Skill>,
        task: &str,
        display_text: &str,
        on_event: impl FnMut(TurnEvent<'_>),
    ) -> Result<String, HarnessError> {
        let user_text = crate::skills::skill_request(skill, task);
        self.run_turn_with(state, &user_text, display_text, Some(skill), on_event)
            .await
    }

    async fn run_turn_with(
        &self,
        state: &mut AgentState,
        user_text: &str,
        display_text: &str,
        skill: Option<&Arc<crate::skills::Skill>>,
        on_event: impl FnMut(TurnEvent<'_>),
    ) -> Result<String, HarnessError> {
        // A cancel requested while no turn was running must not kill this one.
        self.cancel.clear();
        // A skill activated with `use_skill` restricts tools for its turn only: the
        // restriction is lifted on entry and on every exit, so it can never keep
        // refusing tools for the rest of the session. A skill the user ran is then
        // made active for this turn alone.
        self.context.clear_skill();
        if let Some(skill) = skill {
            self.context.activate_skill(skill);
        }
        let started = Instant::now();
        // One `turn` span per user turn, parent of its model and tool spans.
        let mut span = self.tracer.start(
            "turn",
            vec![
                ("session.id", json!(state.session_id)),
                (
                    "gen_ai.request.model",
                    json!(self.active_provider().model()),
                ),
            ],
        );
        let mut summary = TurnSummary::default();
        // A secret the user pasted anyway (or an `@file` that holds one) goes no
        // further than this: not to the model, the history, nor the trace.
        let secrets = &self.context.secrets;
        let (user_text, display_text) = (secrets.redact(user_text), secrets.redact(display_text));
        let result = self
            .turn(state, &user_text, &display_text, &mut summary, on_event)
            .await;
        self.context.clear_skill();
        summary.duration_s = (started.elapsed().as_secs_f64() * 1000.0).round() / 1000.0;
        state.last_turn = Some(summary);
        if let Err(error) = &result {
            state.phase = AgentPhase::Error;
            let reason = match error {
                HarnessError::CircuitTripped(detail) => format!("circuit tripped: {detail}"),
                HarnessError::Provider(detail) => {
                    format!("provider stream failed: ProviderError: {detail}")
                }
                HarnessError::NoFinalCompletion { .. } => {
                    "provider stream ended without a final completion".to_owned()
                }
            };
            self.recorder.event(
                &state.session_id,
                "turn_end",
                vec![("status", json!("error")), ("reason", json!(reason))],
            );
        }
        // Closed with what the turn spent, on every exit, so a backend sees the cost
        // of an aborted turn too.
        span.set(turn_attributes(state, self.active_provider().model()));
        if let Err(error) = &result {
            span.fail(error.python_type(), &error.to_string());
        }
        self.tracer.end(span);
        result
    }

    async fn turn(
        &self,
        state: &mut AgentState,
        goal: &str,
        display_text: &str,
        summary: &mut TurnSummary,
        mut on_event: impl FnMut(TurnEvent<'_>),
    ) -> Result<String, HarnessError> {
        let mut output = String::new();
        let mut emit = |event: TurnEvent<'_>| {
            output.push_str(event.text());
            on_event(event);
        };

        let sid = state.session_id.clone();
        self.recorder
            .event(&sid, "turn_start", vec![("task", json!(display_text))]);
        self.verify.set_last(None);

        // Ingress guardrail on the user's input.
        let report = self.guardrails.inspect_ingress(goal);
        if report.flagged() {
            self.recorder.event(
                &sid,
                "guardrail",
                vec![
                    ("stage", json!("ingress_input")),
                    ("findings", json!(report.summary())),
                ],
            );
            if self.guardrails.should_block(&report) {
                let refusal = format!(
                    "Refused: input tripped the ingress guardrail ({}).",
                    report.summary()
                );
                state.messages.push(message(Role::User, goal));
                state.messages.push(message(Role::Assistant, &refusal));
                state.phase = AgentPhase::Error;
                self.recorder
                    .event(&sid, "turn_end", vec![("status", json!("blocked"))]);
                emit(TurnEvent::Text(&refusal));
                return Ok(output);
            }
            emit(TurnEvent::Notice(&format!(
                "[guardrail] input flagged: {}\n",
                report.summary()
            )));
        }

        // Redact mode cannot stream: text inspected only after it was shown has
        // already leaked. Each step's text is held back and replayed redacted.
        let buffering = self.guardrails.egress == EgressMode::Redact;

        // Pick this turn's model (triage routing), after the input was let through.
        self.route(goal, &sid)?;
        let provider = self.active_provider();

        // What happened last time a task like this came up.
        let recall_note = self
            .memory
            .as_ref()
            .and_then(|memory| memory.recall_note(goal));
        // Beyond Python: the time the turn started, then what was recalled.
        let time = time_note(&jiff::ZonedDateTime::now());
        let recall_note = Some(match recall_note {
            Some(recalled) => format!("{time}\n\n{recalled}"),
            None => time,
        });
        // Where this turn begins. Its message is never summarized away nor left out
        // of a request (beyond Python, whose fitting drops it first in a long turn);
        // compaction may reach past it, keeping it verbatim.
        let mut window = TurnWindow {
            goal_text: goal,
            goal: state.messages.len(),
            continuation: None,
            retry_at: None,
            trim_announced: false,
            trimmed: 0,
            aged: 0,
        };
        state.messages.push(message(Role::User, goal));
        let mut limits = self.limits.clone();
        limits.max_steps = self.max_steps();
        let mut breaker = CircuitBreaker::new(&limits);
        let mut broken_streak = BrokenStreak::default();
        // Where the first partial answer went, and the pieces collected so far.
        let mut continuation_start: Option<usize> = None;
        let mut continuation_parts: Vec<String> = Vec::new();
        // Beyond Python: an empty final reply is asked about once (`ask_again` puts
        // ANSWER_PROMPT on the next request only, never in the history), and the
        // reasoning it came with is kept in case the second try is empty too.
        let mut nudged = false;
        let mut ask_again = false;
        let mut unanswered_thinking = String::new();
        // Beyond Python: the verification gate's progress (`crate::verify`).
        let mut verify_run = crate::verify::VerifyRun::default();
        // Beyond Python: one reminder per way of going in circles (`crate::stall`).
        let mut stall = crate::stall::StallWatch::new(self.limits.stall_nudge);

        loop {
            // Never start another model call once cancelled: that is the expensive,
            // long-latency step the user is trying to escape.
            if self.cancel.is_cancelled() {
                self.cancelled(state, breaker.steps(), &mut emit);
                return Ok(output);
            }
            // Beyond Python: the interface may offer to raise a spent budget.
            if breaker.out_of_steps() {
                match self.raise_limit(LimitKind::Steps, None, &sid, breaker.max_steps(), false) {
                    Some(raised) => breaker.set_max_steps(raised),
                    // Ctrl+C at the offer stops it and cancels the turn.
                    None if self.cancel.is_cancelled() => {
                        self.cancelled(state, breaker.steps(), &mut emit);
                        return Ok(output);
                    }
                    None => {}
                }
            }
            breaker.before_step()?;

            // Compact before the request is built, so the model call sees the
            // compacted history. Once per step, never nested.
            window.continuation = continuation_start;
            if let Some(compaction) = self
                .compact_if_needed(
                    state,
                    recall_note.as_deref(),
                    summary,
                    &mut breaker,
                    &mut window,
                )
                .await
            {
                continuation_start = window.continuation;
                // A failed summary says what fitting drops; a written one starts
                // the count again.
                window.trim_announced = compaction.summary.is_none();
                // Said out loud: a conversation that quietly lost its oldest turns
                // looks, from the user's seat, like a model that forgot.
                emit(TurnEvent::Compacted {
                    notice: &compaction.notice,
                    summary: compaction.summary.as_deref(),
                });
            }

            // The request's estimated size, for calibration, and the budget the
            // step's results are capped against.
            let estimated;
            let step_budget;
            let mut completion = {
                let system = self.system_message(recall_note.as_deref());
                // Under `run --agent`, only what the specialty may call is offered.
                let tools = self.request_tools();
                let budget = self.context_budget(&system, &tools);
                step_budget = budget;
                // Beyond Python: older results go as stubs once the window fills.
                let aged = self.request_view(&state.messages, budget);
                self.announce_age(&sid, aged.aged, budget, &mut window);
                let fitted = fit_pinned(&aged.messages, budget, Some(window.goal));
                self.announce_trim(&sid, &fitted, budget, &mut window, &mut emit);
                let mut request_messages = Vec::with_capacity(fitted.messages.len() + 1);
                request_messages.push(system);
                request_messages.extend(fitted.messages);
                if std::mem::take(&mut ask_again) {
                    request_messages.push(message(Role::User, ANSWER_PROMPT));
                }
                estimated = count_tokens(&request_messages) + count_spec_tokens(&tools);
                let request = CompletionRequest {
                    model: provider.model().to_owned(),
                    messages: request_messages,
                    tools,
                    // Beyond Python: `run --agent`'s specialty, else the section's.
                    temperature: request_temperature(
                        provider.as_ref(),
                        self.subagents.main().and_then(|main| main.2),
                    ),
                    max_tokens: Some(self.output_cap()),
                    stream: true,
                };
                // GenAI semantic conventions where a name exists, `agent86.*` otherwise.
                let mut span = self.tracer.start(
                    "model_call",
                    vec![
                        ("gen_ai.system", json!(provider.name())),
                        ("gen_ai.request.model", json!(provider.model())),
                        ("agent86.step", json!(breaker.steps() + 1)),
                    ],
                );
                let mut stream = provider.stream(request);
                let mut completion = None;
                let mut buffered = String::new();
                // The model never saw a secret's value, but should it produce one
                // anyway, it is not shown.
                let mut redactor = StreamRedactor::default();
                let mut thinking_redactor = StreamRedactor::default();
                loop {
                    // A long or stalled response stops mid-flight, not after it ends.
                    // Dropping the stream drops the HTTP response.
                    let next = tokio::select! {
                        biased;
                        () = self.cancel.cancelled() => None,
                        delta = stream.next() => Some(delta),
                    };
                    let Some(Some(delta)) = next else { break };
                    let delta = match delta {
                        Ok(delta) => delta,
                        Err(error) => {
                            span.set(model_call_attributes(None));
                            span.fail("ProviderError", &error.to_string());
                            self.tracer.end(span);
                            return Err(error.into());
                        }
                    };
                    // Reasoning is shown only as it streams. Redact mode holds text
                    // back until the guardrail has seen it, so it shows none.
                    if !delta.thinking.is_empty() && !buffering {
                        let shown = thinking_redactor.push(&self.context.secrets, &delta.thinking);
                        if !shown.is_empty() {
                            emit(TurnEvent::Thinking(&shown));
                        }
                    }
                    if !delta.text.is_empty() {
                        if buffering {
                            buffered.push_str(&delta.text);
                        } else {
                            let shown = redactor.push(&self.context.secrets, &delta.text);
                            if !shown.is_empty() {
                                emit(TurnEvent::Text(&shown));
                            }
                        }
                    }
                    if delta.done && delta.completion.is_some() {
                        completion = delta.completion;
                    }
                }
                drop(stream);
                let held = thinking_redactor.finish(&self.context.secrets);
                if !held.is_empty() {
                    emit(TurnEvent::Thinking(&held));
                }
                let held = redactor.finish(&self.context.secrets);
                if !held.is_empty() {
                    emit(TurnEvent::Text(&held));
                }
                span.set(model_call_attributes(completion.as_ref()));
                self.tracer.end(span);
                if self.cancel.is_cancelled() {
                    // Whatever was held back still belongs to the user: redacted, then shown.
                    let partial = self
                        .guardrails
                        .inspect_egress(&self.context.secrets.redact(&buffered))
                        .1;
                    if !partial.is_empty() {
                        emit(TurnEvent::Text(&partial));
                    }
                    self.cancelled(state, breaker.steps(), &mut emit);
                    return Ok(output);
                }
                completion.ok_or_else(|| HarnessError::NoFinalCompletion {
                    provider: provider.name().to_owned(),
                    model: provider.model().to_owned(),
                })?
            };

            breaker.record_step(&completion.usage);
            if self.limits.context_sizing {
                self.calibration.observe(
                    provider.model(),
                    estimated,
                    completion.usage.input_tokens,
                );
            }
            summary.steps = u64::from(breaker.steps());
            summary.add_usage(&completion.usage);
            // Beyond Python: what this call used, and the session so far, for a
            // surface that shows it as it goes (an editor's `usage_update`).
            emit(TurnEvent::Usage {
                step: &completion.usage,
                session: &(state.usage + completion.usage),
            });
            let names: Vec<&str> = completion
                .tool_calls
                .iter()
                .map(|call| call.name.as_str())
                .collect();
            self.recorder.event(
                &sid,
                "model_call",
                vec![
                    ("step", json!(breaker.steps())),
                    ("model", json!(provider.model())),
                    ("input_tokens", json!(completion.usage.input_tokens)),
                    ("output_tokens", json!(completion.usage.output_tokens)),
                    ("cost_usd", json!(completion.usage.cost_usd)),
                    ("stop_reason", json!(completion.stop_reason)),
                    ("tool_calls", json!(names)),
                ],
            );

            if completion.stop_reason.as_deref() == Some(REASONING_LOOP) {
                emit(TurnEvent::Notice(REASONING_LOOP_NOTICE));
            }
            // Beyond Python: the provider ended the reply itself (Gemini's SAFETY,
            // RECITATION, ...; an OpenAI-compatible content filter; a model's
            // refusal). Said once, and not asked about again.
            let stopped = completion
                .stop_reason
                .as_deref()
                .and_then(|reason| reason.strip_prefix(crate::provider::STOPPED_PREFIX))
                .map(str::to_owned);
            if let Some(reason) = &stopped {
                emit(TurnEvent::Notice(&format!(
                    "\n[{} stopped the reply: {reason}]\n",
                    provider.name()
                )));
            }

            // Beyond Python: a reply that ends cleanly with nothing to say (a thinking
            // model can put its whole answer in its reasoning) is asked about once.
            // The empty step stays out of the history. If the second reply is empty
            // too, the reasoning stands in for the answer. A reply cut off at the cap
            // is the continuation path's, and a model out of steps is not asked again.
            let mut from_thinking = false;
            if completion.tool_calls.is_empty()
                && completion.stop_reason.as_deref() != Some("max_tokens")
                && stopped.is_none()
                && completion.text.trim().is_empty()
                && continuation_start.is_none()
            {
                if !completion.thinking.trim().is_empty() {
                    unanswered_thinking = std::mem::take(&mut completion.thinking);
                }
                if !nudged && !breaker.out_of_steps() {
                    nudged = true;
                    ask_again = true;
                    self.recorder.event(
                        &sid,
                        "empty_answer",
                        vec![
                            ("step", json!(breaker.steps())),
                            ("model", json!(provider.model())),
                            ("thinking_chars", json!(unanswered_thinking.chars().count())),
                        ],
                    );
                    record_step(
                        state,
                        Step {
                            index: state.steps.len() as u64 + 1,
                            phase: AgentPhase::Execute,
                            thought: String::new(),
                            tool_calls: Vec::new(),
                            results: Vec::new(),
                            usage: completion.usage,
                        },
                    );
                    self.checkpoint(state);
                    emit(TurnEvent::Notice(EMPTY_ANSWER_NOTICE));
                    continue;
                }
                if unanswered_thinking.trim().is_empty() {
                    emit(TurnEvent::Notice(NO_ANSWER_NOTICE));
                    // Beyond Python: LM Studio has been seen to answer a request that
                    // overflows the loaded context with nothing at all.
                    if provider.name() == crate::provider::ServerKind::LmStudio.name() {
                        emit(TurnEvent::Notice(&format!(
                            "{}\n",
                            crate::provider::hints::LMSTUDIO_EMPTY_ANSWER
                        )));
                    }
                } else {
                    completion.text = std::mem::take(&mut unanswered_thinking);
                    from_thinking = true;
                    emit(TurnEvent::Notice(THINKING_ANSWER_NOTICE));
                }
            }

            // Egress guardrail on the model's text. In redact mode the redacted copy
            // is what everything downstream sees: the output, the history, and the step.
            let answer = self.context.secrets.redact(&completion.text);
            let (egress, redacted) = self.guardrails.inspect_egress(&answer);
            let text = if buffering {
                if !redacted.is_empty() {
                    emit(TurnEvent::Text(&redacted));
                }
                redacted
            } else {
                // Reasoning standing in for the answer was never streamed as text.
                if from_thinking {
                    emit(TurnEvent::Text(&answer));
                }
                answer
            };
            if egress.flagged() {
                self.recorder.event(
                    &sid,
                    "guardrail",
                    vec![
                        ("stage", json!("egress")),
                        ("findings", json!(egress.summary())),
                    ],
                );
                emit(TurnEvent::Notice(&format!(
                    "\n[guardrail] output flagged: {}\n",
                    egress.summary()
                )));
            }
            let mut tool_calls = completion.tool_calls;
            // Before anything keeps or sees the calls, so all see the repair.
            let repairs = self.repair_broken_calls(&mut tool_calls, &sid);
            self.scan_tool_arguments(&tool_calls, &sid);
            let cut_off = completion.stop_reason.as_deref() == Some("max_tokens");
            // Beyond Python: a reply that reached the cap before writing anything (a
            // thinking model can spend all of it reasoning) has nothing to continue
            // from. The interface may offer a larger cap and ask again; the empty
            // reply is kept out of the history.
            if cut_off
                && tool_calls.is_empty()
                && text.trim().is_empty()
                && continuation_start.is_none()
            {
                let cap = self.output_cap();
                if self
                    .raise_limit(LimitKind::Output, None, &sid, cap, false)
                    .is_some()
                {
                    record_step(
                        state,
                        Step {
                            index: state.steps.len() as u64 + 1,
                            phase: AgentPhase::Execute,
                            thought: String::new(),
                            tool_calls: Vec::new(),
                            results: Vec::new(),
                            usage: completion.usage,
                        },
                    );
                    self.checkpoint(state);
                    continue;
                }
            }
            let mut step = Step {
                index: state.steps.len() as u64 + 1,
                phase: AgentPhase::Execute,
                thought: text.clone(),
                tool_calls: tool_calls.clone(),
                results: Vec::new(),
                usage: completion.usage,
            };
            state.messages.push(Message {
                tool_calls: tool_calls.clone(),
                ..message(Role::Assistant, &text)
            });

            if tool_calls.is_empty() {
                if cut_off && summary.continuations < MAX_CONTINUATIONS {
                    summary.continuations += 1;
                    continuation_start.get_or_insert(state.messages.len() - 1);
                    continuation_parts.push(text);
                    state.messages.push(message(Role::User, CONTINUE_PROMPT));
                    record_step(state, step);
                    self.checkpoint(state);
                    self.recorder.event(
                        &sid,
                        "continuation",
                        vec![
                            ("index", json!(summary.continuations)),
                            ("step", json!(breaker.steps())),
                            ("model", json!(provider.model())),
                            (
                                "chars",
                                json!(continuation_parts
                                    .last()
                                    .map_or(0, |part| part.chars().count())),
                            ),
                        ],
                    );
                    emit(TurnEvent::Notice(&continuation_notice(
                        summary.continuations,
                    )));
                    continue;
                }
                if let Some(start) = continuation_start.take() {
                    // Collapse [partial, "continue", partial, ...] into one answer.
                    continuation_parts.push(text);
                    let joined = std::mem::take(&mut continuation_parts).concat();
                    state.messages.truncate(start);
                    state.messages.push(message(Role::Assistant, &joined));
                    step.thought = joined;
                }
                // Beyond Python: with `[verify] command` set, a turn that changed the
                // workspace ends only once the check passes or its attempts run out.
                let steps_left = !breaker.out_of_steps();
                if let crate::verify::Gate::SendBack(feedback) =
                    self.verify_answer(&mut verify_run, steps_left, &sid, &mut emit)
                {
                    record_step(state, step);
                    state.messages.push(message(Role::User, &feedback));
                    self.checkpoint(state);
                    continue;
                }
                let outcome = step.thought.clone();
                record_step(state, step);
                state.phase = AgentPhase::Done;
                self.recorder.event(
                    &sid,
                    "turn_end",
                    vec![
                        ("status", json!("done")),
                        ("steps", json!(breaker.steps())),
                        ("continuations", json!(summary.continuations)),
                    ],
                );
                if let Some(memory) = &self.memory {
                    // A failed write loses one episode, never the turn.
                    let _ = memory.add_episode(&sid, goal, &outcome, &Default::default());
                }
                return Ok(output);
            }

            // A step that wants tools ends any continuation in progress.
            continuation_start = None;
            continuation_parts.clear();

            // Before the batch, never mid-execution. Python leaves these calls
            // unanswered, which providers reject on the next request; each is
            // answered here as not executed, as a cancel mid-batch does.
            if self.cancel.is_cancelled() {
                for call in &tool_calls {
                    let result = not_executed(call, "cancelled");
                    state.messages.push(Message {
                        tool_call_id: Some(call.id.clone()),
                        name: Some(call.name.clone()),
                        ..message(Role::Tool, &observation(&result))
                    });
                    step.results.push(result);
                }
                record_step(state, step);
                self.cancelled(state, breaker.steps(), &mut emit);
                return Ok(output);
            }

            // The whole batch is announced up front, then observed strictly in call order.
            for call in &tool_calls {
                let text = format!(
                    "\n[tool] {}({})\n",
                    call.name,
                    preview_arguments(&call.arguments)
                );
                emit(TurnEvent::ToolCall { call, text: &text });
            }
            let results = self.execute_batch(&tool_calls, &sid, breaker.cost()).await;
            // Beyond Python: what a sub-agent had to tell the user (a specialty's
            // model that could not be built), as notices, so `run`'s stdout keeps
            // only the answer.
            for notice in self.take_notices() {
                emit(TurnEvent::Notice(&format!("{notice}\n")));
            }
            // Questions shown as text (ADR 0006's text fallback), which end the turn.
            let mut asked_in_text: Option<String> = None;
            for (call, (result, delegated)) in tool_calls.iter().zip(results) {
                summary.tool_calls += 1;
                // What render_ui drew goes to the surface, never to the session or
                // the trace (the call's arguments already hold it).
                #[cfg(feature = "agent86ui")]
                let result = {
                    let mut result = result;
                    let (drawn, trace) = crate::agent86ui::take_drawn(&mut result.metadata);
                    for message in &drawn {
                        emit(TurnEvent::Ui(message));
                    }
                    if let Some(Value::Object(fields)) = trace {
                        // agent86's own kind (Python has none): which surfaces, how
                        // many components, and the verdict, never the tree.
                        let fields: Vec<(&str, Value)> = fields
                            .iter()
                            .map(|(key, value)| (key.as_str(), value.clone()))
                            .collect();
                        self.recorder.event(&sid, "ui_render", fields);
                    }
                    // A waiting call asks now that its messages are out, so the
                    // program has them to draw before it is asked for an answer.
                    match crate::agent86ui::take_wait(&mut result.metadata) {
                        Some(surfaces) => self.ui_wait(call, result, surfaces, &sid),
                        None => result,
                    }
                };
                // An `ask_user` call asks now (ADR 0006), after anything drawn
                // before it in the batch is out.
                let mut result = self.ask_questions(call, result, &sid, &mut emit);
                if let Some(Value::String(text)) = result.metadata.remove(crate::ask::ASK_TEXT_KEY)
                {
                    asked_in_text = Some(match asked_in_text.take() {
                        Some(earlier) => format!("{earlier}\n\n{text}"),
                        None => text,
                    });
                }
                let result = self.annotate_broken_json(call, result, &repairs, &mut broken_streak);
                // What a sub-agent spent is what this turn spent: in the summary, the
                // step, and the cost cap, but not as one of this turn's steps.
                summary.add_usage(&delegated);
                step.usage = step.usage + delegated;
                breaker.record_cost(delegated.cost_usd);
                let (content, findings) = self.guardrails.observe(observation(&result));
                // Beyond Python: no result takes more than its share of the window.
                let content = self.cap_result(content, step_budget);
                if let Some(findings) = findings {
                    self.recorder.event(
                        &sid,
                        "guardrail",
                        vec![
                            ("stage", json!("observation")),
                            ("tool", json!(call.name)),
                            ("findings", json!(findings)),
                        ],
                    );
                }
                emit(TurnEvent::ToolResult {
                    call,
                    result: &result,
                    observation: &content,
                    text: &format!("[tool] {} -> {}\n", call.name, summarize(&result)),
                });
                state.messages.push(Message {
                    tool_call_id: Some(call.id.clone()),
                    name: Some(call.name.clone()),
                    ..message(Role::Tool, &content)
                });
                let ok = result.ok;
                verify_run.saw(&call.name, ok);
                stall.saw(call, &result, &content);
                step.results.push(result);
                if let Err(tripped) = breaker.record_tool_result(ok) {
                    record_step(state, step);
                    return Err(tripped);
                }
            }
            // ADR 0006's text fallback: the questions are the agent's message, kept
            // as an ordinary assistant message after the results (so the history
            // alternates as every provider requires, and reads naturally on resume
            // and in Python), and the turn ends without another model call. No new
            // stop reason is stored; the user's reply is the next turn.
            if let Some(questions) = asked_in_text {
                emit(TurnEvent::Text(&questions));
                state.messages.push(message(Role::Assistant, &questions));
                record_step(state, step);
                state.phase = AgentPhase::Done;
                self.recorder.event(
                    &sid,
                    "turn_end",
                    vec![
                        ("status", json!("done")),
                        ("steps", json!(breaker.steps())),
                        ("continuations", json!(summary.continuations)),
                    ],
                );
                if let Some(memory) = &self.memory {
                    let _ = memory.add_episode(&sid, goal, &questions, &Default::default());
                }
                return Ok(output);
            }
            // Every call has an observation by now, including any skipped because
            // the cancel landed mid-batch, so a note can follow them.
            if let Some(found) = stall.take() {
                self.remind_of_stall(&found, &sid, &mut emit);
                state.messages.push(message(Role::User, &found.note()));
            }
            record_step(state, step);
            self.checkpoint(state);
            if self.cancel.is_cancelled() {
                self.cancelled(state, breaker.steps(), &mut emit);
                return Ok(output);
            }
        }
    }

    /// Save the session at a step boundary (beyond Python; checkpointing tier 1,
    /// `docs/checkpointing.md`), so a process that dies mid-turn keeps the steps it
    /// finished. Only called once every tool call in the history has its answer.
    /// The phase reads `execute` until the turn ends, when every exit sets `done`
    /// or `error`, so a saved state that still says `execute` is a turn cut off.
    /// A failed write loses one checkpoint, never the turn.
    fn checkpoint(&self, state: &mut AgentState) {
        state.phase = AgentPhase::Execute;
        if let Some(memory) = &self.memory {
            let _ = memory.persist(state);
        }
    }

    /// Close out a cancelled turn and say so, as Python's `_cancelled`. The partial
    /// exchange is kept: the user stopped the agent, they did not undo it. The phase
    /// is `error`; "cancelled" is carried by the trace's `status` and `reason`.
    /// Beyond Python, which trims silently: when fitting leaves messages out of a
    /// request, the trace gets a `context_trim` event each time the count changes,
    /// and the turn says so once (again after a compaction has written a summary).
    fn announce_trim(
        &self,
        sid: &str,
        fitted: &Fitted,
        budget: u64,
        window: &mut TurnWindow<'_>,
        emit: &mut impl FnMut(TurnEvent<'_>),
    ) {
        if fitted.dropped == window.trimmed {
            return;
        }
        window.trimmed = fitted.dropped;
        if fitted.dropped == 0 {
            return;
        }
        self.recorder.event(
            sid,
            "context_trim",
            vec![
                ("dropped", json!(fitted.dropped)),
                ("kept", json!(fitted.messages.len())),
                ("budget", json!(budget)),
                ("goal_pinned", json!(fitted.pinned)),
            ],
        );
        if !window.trim_announced {
            window.trim_announced = true;
            emit(TurnEvent::Notice(&trimmed_notice(fitted.dropped)));
        }
    }

    /// Beyond Python: say that the turn was going in circles and record it (a
    /// `stall` trace event, since Python has none); the loop then sends the
    /// model [`crate::stall::Stall::note`].
    fn remind_of_stall(
        &self,
        found: &crate::stall::Stall,
        sid: &str,
        emit: &mut impl FnMut(TurnEvent<'_>),
    ) {
        emit(TurnEvent::Notice(&found.notice()));
        let mut fields = vec![
            ("shape", json!(found.kind.as_str())),
            ("count", json!(found.count)),
        ];
        if let Some(tool) = &found.tool {
            fields.push(("tool", json!(tool)));
        }
        self.recorder.event(sid, "stall", fields);
    }

    /// Beyond Python: a `context_age` trace event whenever the number of tool
    /// results a request sends aged changes (`crate::memory::sizing::aged_view`).
    /// Nothing is said to the user: the session keeps every result whole.
    fn announce_age(&self, sid: &str, aged: usize, budget: u64, window: &mut TurnWindow<'_>) {
        if aged == window.aged {
            return;
        }
        window.aged = aged;
        self.recorder.event(
            sid,
            "context_age",
            vec![("aged", json!(aged)), ("budget", json!(budget))],
        );
    }

    fn cancelled(&self, state: &mut AgentState, steps: u32, emit: &mut impl FnMut(TurnEvent<'_>)) {
        state.phase = AgentPhase::Error;
        self.recorder.event(
            &state.session_id,
            "turn_end",
            vec![
                ("status", json!("cancelled")),
                ("reason", json!("cancelled")),
                ("steps", json!(steps)),
            ],
        );
        emit(TurnEvent::Notice(CANCELLED_NOTICE));
    }

    /// Run a step's tool calls and return their results in call order.
    ///
    /// Read-only calls may run concurrently with each other; side-effecting calls
    /// run sequentially, in the order the model asked for them, after the reads.
    /// Every approval is settled first, on this thread, because the gate may
    /// prompt a human.
    ///
    /// Beyond Python, adjacent delegations to specialist sub-agents among the
    /// calls left after the reads run at once (`Harness::fan_out`, `[agents]
    /// max_parallel`), so a write listed before a delegation still happens first.
    /// A batch with any specialty delegation takes this reads-first path whatever
    /// the number of reads; `spent` is what the turn has spent so far, for the
    /// fan-out's cost cap.
    async fn execute_batch(
        &self,
        calls: &[ToolCall],
        sid: &str,
        spent: f64,
    ) -> Vec<(ToolResult, Usage)> {
        let eligible = calls
            .iter()
            .filter(|call| self.parallel_eligible(call))
            .count();
        let specialty: Vec<bool> = calls
            .iter()
            .map(|call| self.is_specialty_delegation(call))
            .collect();
        let any_specialty = specialty.contains(&true);
        if !self.limits.parallel_tools || (eligible < 2 && !any_specialty) {
            // Strictly sequential: each call is resolved and run before the next.
            let mut results = Vec::with_capacity(calls.len());
            for call in calls {
                results.push(if self.cancel.is_cancelled() {
                    (not_executed(call, "cancelled"), Usage::default())
                } else {
                    self.execute_tool(call, sid).await
                });
            }
            return results;
        }

        let mut settled: Vec<Option<(ToolResult, Usage)>> = Vec::with_capacity(calls.len());
        let mut may_parallel = Vec::with_capacity(calls.len());
        for call in calls {
            let (result, parallel) = self.resolve_call(call, sid);
            settled.push(result.map(|result| (result, Usage::default())));
            may_parallel.push(parallel);
        }
        let pending = || (0..calls.len()).filter(|&index| settled[index].is_none());
        let reads: Vec<usize> = pending().filter(|&index| may_parallel[index]).collect();
        let writes: Vec<usize> = pending().filter(|&index| !may_parallel[index]).collect();

        if reads.len() > 1 {
            let collected: Vec<(usize, ToolResult)> = std::thread::scope(|scope| {
                let handles: Vec<_> = reads
                    .iter()
                    .map(|&index| {
                        (
                            index,
                            scope.spawn(move || self.dispatch(&calls[index], sid)),
                        )
                    })
                    .collect();
                handles
                    .into_iter()
                    .map(|(index, handle)| {
                        let result = handle
                            .join()
                            .unwrap_or_else(|_| not_executed(&calls[index], "the tool panicked"));
                        (index, result)
                    })
                    .collect()
            });
            for (index, result) in collected {
                settled[index] = Some((result, Usage::default()));
            }
        } else {
            for &index in &reads {
                settled[index] = Some(self.dispatch_unless_cancelled(&calls[index], sid).await);
            }
        }
        // Between calls, never mid-execution: a tool that started is allowed to finish.
        // A run of adjacent specialty delegations goes together; anything else
        // (a write, a plain delegation) runs alone and separates them.
        let mut next = 0;
        while next < writes.len() {
            let index = writes[next];
            let run_end = writes[next..]
                .iter()
                .position(|&index| !specialty[index])
                .map_or(writes.len(), |offset| next + offset);
            if specialty[index] && run_end - next > 1 {
                let group: Vec<usize> = writes[next..run_end].to_vec();
                let results = if self.cancel.is_cancelled() {
                    group
                        .iter()
                        .map(|&index| (not_executed(&calls[index], "cancelled"), Usage::default()))
                        .collect()
                } else {
                    let members: Vec<&ToolCall> =
                        group.iter().map(|&index| &calls[index]).collect();
                    self.fan_out(&members, sid, spent)
                };
                for (index, result) in group.into_iter().zip(results) {
                    settled[index] = Some(result);
                }
                next = run_end;
            } else {
                settled[index] = Some(self.dispatch_unless_cancelled(&calls[index], sid).await);
                next += 1;
            }
        }

        settled
            .into_iter()
            .zip(calls)
            .map(|(result, call)| {
                result.unwrap_or_else(|| (not_executed(call, "skipped"), Usage::default()))
            })
            .collect()
    }

    /// Could this call share a step with its siblings? Decided from the tool alone,
    /// never the gate, which may prompt and must be consulted exactly once.
    fn parallel_eligible(&self, call: &ToolCall) -> bool {
        call.invalid_arguments().is_none()
            && self
                .registry
                .get(&call.name)
                .is_some_and(|tool| !tool.side_effecting() && tool.parallel_safe())
    }

    /// Decide one call without running it: `(settled result, may run in parallel)`.
    ///
    /// Arguments the provider could not parse are answered here, before the
    /// registry, the approval gate, or any tool sees them.
    ///
    /// Beyond Python, a call answered here is recorded as a `tool_call` event
    /// too, with Python's fields, so the trace says why a step's calls never ran
    /// (Python records only the calls it dispatches).
    pub(crate) fn resolve_call(&self, call: &ToolCall, sid: &str) -> (Option<ToolResult>, bool) {
        let settled = |result: ToolResult| {
            self.record_tool_call(call, &result, sid);
            (Some(result), false)
        };
        if let Some(raw) = call.invalid_arguments() {
            return settled(invalid_arguments_result(call, raw));
        }
        let Some(tool) = self.registry.get(&call.name) else {
            // The registry's own "Unknown tool" answer.
            return settled(self.registry.dispatch(call, &self.context));
        };
        // Under `run --agent`, a call outside the specialty's tools gets the
        // registry's refusal, which says so, before the gate is consulted.
        if !self.context.scope_allows(&call.name) {
            return settled(self.registry.dispatch(call, &self.context));
        }
        let decision = self.gate.decide(tool.as_ref(), call, &self.context);
        if !decision.approved {
            return settled(not_executed(call, decision.reason));
        }
        (None, !tool.side_effecting() && tool.parallel_safe())
    }

    async fn dispatch_unless_cancelled(&self, call: &ToolCall, sid: &str) -> (ToolResult, Usage) {
        if self.cancel.is_cancelled() {
            (not_executed(call, "cancelled"), Usage::default())
        } else {
            self.dispatch_call(call, sid).await
        }
    }

    /// Run one approved call: a delegation runs its sub-agent here, on this
    /// runtime; anything else goes to the registry.
    async fn dispatch_call(&self, call: &ToolCall, sid: &str) -> (ToolResult, Usage) {
        let request = match self.delegation(call, &self.context) {
            Some(request) => request,
            None => return (self.dispatch(call, sid), Usage::default()),
        };
        let mut span = self.tool_span(call);
        let (result, usage) = match request {
            Ok(request) => self.delegate(call, request, 1, &self.context).await,
            Err(invalid) => (invalid, Usage::default()),
        };
        span.set(vec![("tool.ok", json!(result.ok))]);
        self.tracer.end(span);
        self.record_tool_call(call, &result, sid);
        (result, usage)
    }

    /// Run one approved call and record it in the flight recorder.
    pub(crate) fn dispatch(&self, call: &ToolCall, sid: &str) -> ToolResult {
        let mut span = self.tool_span(call);
        let result = self.registry.dispatch(call, &self.context);
        span.set(vec![("tool.ok", json!(result.ok))]);
        self.tracer.end(span);
        self.record_tool_call(call, &result, sid);
        result
    }

    /// A `tool_call` span for `call`, as Python's `_dispatch_call` opens.
    pub(crate) fn tool_span(&self, call: &ToolCall) -> crate::otel::Span {
        self.tracer.start(
            "tool_call",
            vec![
                ("tool.name", json!(call.name)),
                ("gen_ai.tool.name", json!(call.name)),
            ],
        )
    }

    pub(crate) fn record_tool_call(&self, call: &ToolCall, result: &ToolResult, sid: &str) {
        self.recorder.event(
            sid,
            "tool_call",
            vec![
                ("tool", json!(call.name)),
                ("ok", json!(result.ok)),
                ("arguments", Value::Object(call.arguments.clone())),
                ("error", json!(result.error)),
            ],
        );
        self.record_repair(call, result, sid);
    }

    /// Beyond Python: note a call whose JSON-string arguments the registry repaired
    /// (`tools::repair_arguments`). The call itself is recorded as the model sent it.
    pub(crate) fn record_repair(&self, call: &ToolCall, result: &ToolResult, sid: &str) {
        if let Some(repaired) = result.metadata.get(crate::tools::ARGS_REPAIRED_KEY) {
            self.recorder.event(
                sid,
                "tool_args_repaired",
                vec![("tool", json!(call.name)), ("arguments", repaired.clone())],
            );
        }
    }

    /// Beyond Python (ADR 0006): repair each call whose arguments did not parse
    /// but do once every closer that matches nothing is dropped
    /// (`broken_json::drop_unmatched_closers`). Unlike the JSON-string repair, the
    /// repaired arguments replace the raw text on the call itself, since that text
    /// is no call at all: the history, the approval gate, the tool, and replay on
    /// resume then all see the same call. A `tool_args_repaired` event records
    /// what was dropped. Returns the note each repaired call's result gets, by id.
    pub(crate) fn repair_broken_calls(
        &self,
        calls: &mut [ToolCall],
        sid: &str,
    ) -> HashMap<String, String> {
        let mut notes = HashMap::new();
        for call in calls.iter_mut() {
            let Some(raw) = call.invalid_arguments() else {
                continue;
            };
            let Some((arguments, dropped)) = crate::broken_json::drop_unmatched_closers(raw) else {
                continue;
            };
            self.recorder.event(
                sid,
                "tool_args_repaired",
                vec![
                    ("tool", json!(call.name)),
                    (
                        "dropped",
                        json!(dropped
                            .iter()
                            .map(|(at, closer)| json!({"at": at, "char": closer.to_string()}))
                            .collect::<Vec<_>>()),
                    ),
                ],
            );
            notes.insert(call.id.clone(), repair_note(&dropped));
            call.arguments = arguments;
        }
        notes
    }

    /// `result` with what the model should know about its call's JSON: the
    /// repair's note, for a call [`Harness::repair_broken_calls`] repaired, and,
    /// once a tool's arguments have failed to parse twice in a row, advice on
    /// sending less (beyond Python; ADR 0006).
    pub(crate) fn annotate_broken_json(
        &self,
        call: &ToolCall,
        result: ToolResult,
        repairs: &HashMap<String, String>,
        streak: &mut BrokenStreak,
    ) -> ToolResult {
        let mut result = result;
        if let Some(note) = repairs.get(&call.id) {
            append_note(&mut result, note);
        }
        if let Some(count) = streak.observe(call) {
            let mut advice = format!(
                "{count} calls in a row to {} have sent arguments that do not parse. Long, \
                 deeply nested arguments break most often: send less in each call.",
                call.name
            );
            if let Some(hint) = self
                .registry
                .get(&call.name)
                .and_then(|tool| tool.retry_hint().map(str::to_owned))
            {
                advice.push(' ');
                advice.push_str(&hint);
            }
            append_note(&mut result, &advice);
        }
        result
    }

    /// Egress-scan tool-call arguments, the other way a secret leaves the harness.
    /// Findings are recorded, not acted on; the approval gate stops side effects.
    fn scan_tool_arguments(&self, calls: &[ToolCall], sid: &str) {
        if self.guardrails.egress == EgressMode::Off {
            return;
        }
        for call in calls {
            let payload = python_json_dumps(&Value::Object(call.arguments.clone()), false);
            let (report, _) = self.guardrails.inspect_egress(&payload);
            if report.flagged() {
                self.recorder.event(
                    sid,
                    "guardrail",
                    vec![
                        ("stage", json!("egress_tool_args")),
                        ("tool", json!(call.name)),
                        ("findings", json!(report.summary())),
                    ],
                );
            }
        }
    }

    /// Resolve and run one call.
    async fn execute_tool(&self, call: &ToolCall, sid: &str) -> (ToolResult, Usage) {
        match self.resolve_call(call, sid) {
            (Some(result), _) => (result, Usage::default()),
            (None, _) => self.dispatch_call(call, sid).await,
        }
    }
}

/// How many calls in a row to each tool have sent arguments that did not parse,
/// in one loop's turn (beyond Python; ADR 0006).
#[derive(Debug, Default)]
pub(crate) struct BrokenStreak(HashMap<String, u32>);

impl BrokenStreak {
    /// Count `call`: the streak when its arguments did not parse and it is at
    /// least the second such call to its tool in a row, else `None`. A call
    /// that parsed (or was repaired) ends its tool's streak.
    fn observe(&mut self, call: &ToolCall) -> Option<u32> {
        let count = self.0.entry(call.name.clone()).or_default();
        if call.invalid_arguments().is_none() {
            *count = 0;
            return None;
        }
        *count += 1;
        (*count >= 2).then_some(*count)
    }
}

/// What the model is told about a call whose broken arguments were repaired.
fn repair_note(dropped: &[(usize, char)]) -> String {
    let closers: Vec<String> = dropped
        .iter()
        .map(|(at, closer)| format!("`{closer}` at character {at}"))
        .collect();
    let noun = if dropped.len() == 1 {
        "closer"
    } else {
        "closers"
    };
    format!(
        "Note: these arguments were not valid JSON. agent86 dropped the unmatched {noun} {}, \
         which matched nothing, and ran the call with the rest. Check next time that every \
         bracket closes in order.",
        closers.join(", ")
    )
}

/// Add `note` to what the model reads of `result`: after its output when the
/// call worked, after its error when it failed.
fn append_note(result: &mut ToolResult, note: &str) {
    let text = if result.ok {
        &mut result.content
    } else {
        result.error.get_or_insert_with(String::new)
    };
    if !text.is_empty() {
        text.push_str("\n\n");
    }
    text.push_str(note);
}

/// The observation for a call that was not run.
pub(crate) fn not_executed(call: &ToolCall, reason: &str) -> ToolResult {
    ToolResult {
        call_id: call.id.clone(),
        name: call.name.clone(),
        ok: false,
        content: String::new(),
        error: Some(format!("Not executed: {reason}.")),
        metadata: serde_json::Map::new(),
    }
}

/// Closing attributes for a `model_call` span, as Python's `_model_call_attributes`.
fn model_call_attributes(
    completion: Option<&crate::types::Completion>,
) -> Vec<(&'static str, Value)> {
    let Some(completion) = completion else {
        return vec![("agent86.completed", json!(false))];
    };
    let finish = completion
        .stop_reason
        .as_deref()
        .filter(|reason| !reason.is_empty())
        .map_or(Value::Null, |reason| json!([reason]));
    vec![
        ("agent86.completed", json!(true)),
        (
            "gen_ai.usage.input_tokens",
            json!(completion.usage.input_tokens),
        ),
        (
            "gen_ai.usage.output_tokens",
            json!(completion.usage.output_tokens),
        ),
        ("gen_ai.response.finish_reasons", finish),
        ("agent86.cost_usd", json!(completion.usage.cost_usd)),
        ("agent86.tool_calls", json!(completion.tool_calls.len())),
    ]
}

/// Closing attributes for the `turn` span, read off `state.last_turn`, as Python's
/// `_turn_attributes`.
fn turn_attributes(state: &AgentState, model: &str) -> Vec<(&'static str, Value)> {
    let mut attributes = vec![("gen_ai.request.model", json!(model))];
    if let Some(summary) = &state.last_turn {
        attributes.extend([
            ("gen_ai.usage.input_tokens", json!(summary.input_tokens)),
            ("gen_ai.usage.output_tokens", json!(summary.output_tokens)),
            ("agent86.cost_usd", json!(summary.cost_usd)),
            ("agent86.steps", json!(summary.steps)),
            ("agent86.tool_calls", json!(summary.tool_calls)),
            ("agent86.duration_s", json!(summary.duration_s)),
            (
                "agent86.phase",
                serde_json::to_value(state.phase).unwrap_or(Value::Null),
            ),
        ]);
    }
    attributes
}

/// Append `step` to the session and add its usage, as Python's `AgentState.record_step`.
fn record_step(state: &mut AgentState, step: Step) {
    state.usage = state.usage + step.usage;
    state.steps.push(step);
}

fn message(role: Role, content: &str) -> Message {
    Message {
        role,
        content: content.to_owned(),
        tool_calls: Vec::new(),
        tool_call_id: None,
        name: None,
    }
}

/// The observation for a tool call whose arguments were not valid JSON.
///
/// Its first line is Python's. Beyond Python, the lines after it say where the
/// JSON broke (`broken_json::describe`): the first 200 characters Python quotes
/// are usually fine, and a model shown only them guesses at what to fix.
pub fn invalid_arguments_result(call: &ToolCall, raw: &str) -> ToolResult {
    let preview = truncate_chars(raw, 200);
    let mut error = format!(
        "invalid JSON in tool arguments: {} could not be parsed. \
         Call the tool again with its arguments as a single valid JSON object.",
        python_repr(&preview)
    );
    if let Some(location) = crate::broken_json::describe(raw) {
        error.push('\n');
        error.push_str(&location);
    }
    ToolResult {
        call_id: call.id.clone(),
        name: call.name.clone(),
        ok: false,
        content: String::new(),
        error: Some(error),
        metadata: serde_json::Map::new(),
    }
}

/// The text the model observes for `result`. A failure keeps both its error and
/// any output, so the model can see what went wrong.
pub fn observation(result: &ToolResult) -> String {
    if result.ok {
        return result.content.clone();
    }
    let error = result.error.as_deref().unwrap_or_default().trim();
    let body = result.content.trim();
    match (error.is_empty(), body.is_empty()) {
        (false, false) => format!("{error}\n{body}"),
        (false, true) => error.to_owned(),
        (true, false) => body.to_owned(),
        (true, true) => "error".to_owned(),
    }
}

/// The temperature a request sends, beyond Python's fixed 0.0: the specialty's
/// own, else its model's provider section's, else 0.0. Summaries keep 0.0.
pub(crate) fn request_temperature(provider: &dyn Provider, specialty: Option<f32>) -> f32 {
    specialty.or_else(|| provider.temperature()).unwrap_or(0.0)
}

/// Tool arguments as Python's `json.dumps(..., ensure_ascii=False)`, cut at 160 characters.
pub fn preview_arguments(arguments: &serde_json::Map<String, Value>) -> String {
    let text = python_json_dumps(&Value::Object(arguments.clone()), false);
    truncate_chars(&text, 160)
}

/// The one-line summary of a result shown after `[tool] name ->`.
pub fn summarize(result: &ToolResult) -> String {
    if !result.ok {
        let error = result.error.as_deref().unwrap_or_default().trim();
        let content = result.content.trim();
        let detail = [error, content, "(failed)"]
            .into_iter()
            .find(|text| !text.is_empty())
            .unwrap_or("(failed)");
        return format!("error: {}", first_chars(first_line(detail), 160));
    }
    let content = result.content.trim();
    let head = if content.is_empty() {
        "(no output)"
    } else {
        first_line(content)
    };
    first_chars(head, 160)
}

/// The first line of `text`, splitting on every boundary Python's `splitlines()` uses.
fn first_line(text: &str) -> &str {
    let is_break = |c: char| {
        matches!(
            c,
            '\n' | '\r'
                | '\u{b}'
                | '\u{c}'
                | '\u{1c}'
                | '\u{1d}'
                | '\u{1e}'
                | '\u{85}'
                | '\u{2028}'
                | '\u{2029}'
        )
    };
    text.split(is_break).next().unwrap_or_default()
}

fn first_chars(text: &str, limit: usize) -> String {
    text.chars().take(limit).collect()
}

/// `text` if it has at most `limit` characters, else its first `limit` and `" ..."`.
fn truncate_chars(text: &str, limit: usize) -> String {
    if text.chars().count() <= limit {
        text.to_owned()
    } else {
        format!("{} ...", first_chars(text, limit))
    }
}

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

    fn limits() -> LimitsConfig {
        LimitsConfig {
            max_steps: 2,
            max_cost_usd: 1.0,
            max_wall_clock_s: 900,
            max_consecutive_errors: 1,
            max_context_tokens: 0,
            max_output_tokens: 8192,
            context_reserve_tokens: 4096,
            compaction: CompactionMode::Summarize,
            parallel_tools: true,
            tool_timeout_s: 60,
            context_sizing: true,
            stall_nudge: true,
        }
    }

    #[test]
    fn the_breaker_trips_on_steps_cost_and_error_streaks() {
        let mut breaker = CircuitBreaker::new(&limits());
        breaker.before_step().unwrap();
        breaker.record_step(&Usage::default());
        breaker.record_step(&Usage::default());
        assert_eq!(
            breaker.before_step().unwrap_err().to_string(),
            "Circuit tripped: step budget reached (2 steps)"
        );

        let mut costly = CircuitBreaker::new(&limits());
        costly.record_step(&Usage {
            cost_usd: 1.25,
            ..Usage::default()
        });
        assert_eq!(
            costly.before_step().unwrap_err().to_string(),
            "Circuit tripped: cost cap reached ($1.2500 >= $1.0)"
        );

        let mut errors = CircuitBreaker::new(&limits());
        errors.record_tool_result(false).unwrap();
        errors.record_tool_result(true).unwrap();
        errors.record_tool_result(false).unwrap();
        assert_eq!(
            errors.record_tool_result(false).unwrap_err().to_string(),
            "Circuit tripped: aborted after 2 consecutive tool errors"
        );
    }

    fn result(ok: bool, content: &str, error: Option<&str>) -> ToolResult {
        ToolResult {
            call_id: "c".to_owned(),
            name: "t".to_owned(),
            ok,
            content: content.to_owned(),
            error: error.map(str::to_owned),
            metadata: serde_json::Map::new(),
        }
    }

    #[test]
    fn observations_keep_both_error_and_output() {
        assert_eq!(observation(&result(true, "data", None)), "data");
        assert_eq!(
            observation(&result(false, " out ", Some(" bad "))),
            "bad\nout"
        );
        assert_eq!(observation(&result(false, "", Some("bad"))), "bad");
        assert_eq!(observation(&result(false, "", None)), "error");
    }

    #[test]
    fn summaries_show_the_first_line() {
        assert_eq!(summarize(&result(true, "\n first\nsecond", None)), "first");
        assert_eq!(summarize(&result(true, "  ", None)), "(no output)");
        assert_eq!(
            summarize(&result(false, "", Some("No such file: x\nmore"))),
            "error: No such file: x"
        );
        assert_eq!(summarize(&result(false, "", None)), "error: (failed)");
        assert_eq!(summarize(&result(true, &"x".repeat(200), None)).len(), 160);
    }

    #[test]
    fn previews_use_python_json_and_cut_at_160_characters() {
        let Value::Object(short) = json!({"path": "src/main.rs"}) else {
            unreachable!()
        };
        assert_eq!(preview_arguments(&short), r#"{"path": "src/main.rs"}"#);

        let Value::Object(long) = json!({"path": "a".repeat(300)}) else {
            unreachable!()
        };
        let preview = preview_arguments(&long);
        assert!(preview.ends_with(" ..."));
        assert_eq!(preview.chars().count(), 164);
    }

    #[test]
    fn invalid_json_is_reported_with_a_python_repr_preview() {
        let call = ToolCall {
            id: "c1".to_owned(),
            name: "read_file".to_owned(),
            arguments: serde_json::Map::new(),
        };
        let result = invalid_arguments_result(&call, "{\"path\": ");
        let error = result.error.as_deref().unwrap();
        // Python's line first, then where it broke (beyond Python).
        assert_eq!(
            error.lines().next(),
            Some(
                "invalid JSON in tool arguments: '{\"path\": ' could not be parsed. \
                 Call the tool again with its arguments as a single valid JSON object."
            )
        );
        assert_eq!(
            error.lines().nth(1),
            Some(
                "The JSON ends at character 9 inside the top-level object, before it is \
                 complete: EOF while parsing a value."
            )
        );
        assert!(!result.ok);
        assert_eq!(result.call_id, "c1");
    }
}