vtcode 0.169.3

A Rust-based terminal coding agent with modular architecture supporting multiple LLM providers
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
use vtcode_core::llm::provider as uni;

/// Pushed when an interim/status reply (or a recap while tracker steps remain)
/// would otherwise end the turn. Also covers the tracker override path, so it
/// must not claim the reply was specifically a progress update.
pub(super) const AUTONOMOUS_CONTINUE_DIRECTIVE: &str = "The last reply did not finish the task, so the turn continues. \
     Take the next concrete action, then report the result or what is blocking it.";

/// Maximum number of consecutive relaxed continuation decisions before the turn
/// is forced to end. This prevents infinite loops where the model keeps producing
/// continuation-worthy text without making actual progress.
pub(super) const MAX_CONSECUTIVE_RELAXED_CONTINUATIONS: u32 = 3;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) struct InterimTextContinuationDecision {
    pub(super) should_continue: bool,
    pub(super) reason: &'static str,
    pub(super) is_interim_progress: bool,
    pub(super) last_user_follow_up: bool,
    pub(super) recent_tool_activity: bool,
    pub(super) last_user_requested_progressive_work: bool,
    /// True if this continuation decision came from the relaxed path
    /// (recent_tool_activity_relaxed or progressive_relaxed).
    pub(super) is_relaxed_continuation: bool,
}

impl InterimTextContinuationDecision {
    fn with(
        should_continue: bool,
        reason: &'static str,
        is_interim_progress: bool,
        last_user_follow_up: bool,
        recent_tool_activity: bool,
        last_user_requested_progressive_work: bool,
    ) -> Self {
        Self {
            should_continue,
            reason,
            is_interim_progress,
            last_user_follow_up,
            recent_tool_activity,
            last_user_requested_progressive_work,
            is_relaxed_continuation: false,
        }
    }

    fn with_relaxed(
        should_continue: bool,
        reason: &'static str,
        is_interim_progress: bool,
        last_user_follow_up: bool,
        recent_tool_activity: bool,
        last_user_requested_progressive_work: bool,
    ) -> Self {
        Self {
            should_continue,
            reason,
            is_interim_progress,
            last_user_follow_up,
            recent_tool_activity,
            last_user_requested_progressive_work,
            is_relaxed_continuation: true,
        }
    }
}

pub(super) fn evaluate_interim_text_continuation(
    full_auto: bool,
    planning_active: bool,
    history: &[uni::Message],
    text: &str,
    consecutive_relaxed_continuations: u32,
) -> InterimTextContinuationDecision {
    let is_interim_progress = is_interim_progress_update(text);
    let lower = text.to_ascii_lowercase();
    let last_user_follow_up = last_user_message_is_follow_up(history);
    let recent_tool_activity = has_recent_tool_activity(history);
    let last_user_requested_progressive_work = last_user_requested_progressive_work(history);
    let read_only_request = last_user_requested_read_only_answer(history);

    let d = |should_continue: bool, reason: &'static str| {
        InterimTextContinuationDecision::with(
            should_continue,
            reason,
            is_interim_progress,
            last_user_follow_up,
            recent_tool_activity,
            last_user_requested_progressive_work,
        )
    };

    let d_relaxed = |should_continue: bool, reason: &'static str| {
        InterimTextContinuationDecision::with_relaxed(
            should_continue,
            reason,
            is_interim_progress,
            last_user_follow_up,
            recent_tool_activity,
            last_user_requested_progressive_work,
        )
    };

    if planning_active {
        return d(false, "planning_active");
    }

    // Full-auto sessions must not mistake an ordinary progress update for a
    // completed turn. The model often emits a short, non-interim sentence
    // before it makes its next tool call; returning here would hand control
    // back to the TUI input prompt and require a manual `continue`.
    //
    // Keep explicit terminal signals ahead of this autonomous fallback. A
    // response cap and the existing loop guards remain the authoritative
    // bounds when the model keeps producing non-conclusive text.
    if full_auto {
        let asks_user_input = text.contains('?') || contains_user_input_request(&lower);
        if text.trim().is_empty() {
            return d(false, "full_auto_empty_response");
        }
        if asks_user_input {
            return d(false, "full_auto_user_input_handoff");
        }
        // Verification-gate / safety recaps are terminal even in full-auto:
        // autonomous continuation must not race past the anti-blind checkpoint.
        if lower.contains("verification is still pending")
            || lower.contains("unverified assistant responses")
            || vtcode_core::core::agent::completion::tracker_final_text_is_safety_handoff(text)
        {
            return d(false, "full_auto_safety_handoff");
        }
        if has_explicit_blocker(&lower) {
            return d(false, "full_auto_blocked_handoff");
        }
        // Full-auto is an execution policy, not a reason to keep generating
        // after an informational request has been answered. The old fallback
        // below treated every non-keyword response as unfinished work, so a
        // read-only request such as "explore the codebase and summarize" could
        // trigger a second provider call after the answer was already shown.
        // If that follow-up was interrupted, the stream had visible content
        // but no confirmed final response and the turn was reported as a
        // recovery fallback. Decide this from the user's request, not the
        // answer's formatting or vocabulary.
        if read_only_request {
            return d(false, "full_auto_read_only_answer");
        }
        if full_auto_response_is_conclusive(&lower) {
            return d(false, "full_auto_final_completion");
        }
    }

    if !is_interim_progress {
        let not_conclusive = !last_clause_contains_conclusive_marker(&lower);
        let has_relaxed_continuation_intent = has_relaxed_continuation_intent(&lower);
        // Relaxed: if the model just ran tools, or the user asked for progressive work,
        // and the text still contains a continuation-intent clause, treat long
        // analysis text as continuation-worthy even if it exceeded the strict
        // interim-progress shape.
        // However, if the text contains a user-directed question, it's asking for
        // input and should NOT be treated as continuation-worthy. This prevents
        // infinite loops where the model explains blockers and asks the user how
        // to proceed, but the system keeps injecting continue directives.
        let asks_user_question = text.contains('?') || contains_user_input_request(&lower);
        // Also cap relaxed continuations to prevent infinite loops where the model
        // keeps producing continuation-worthy text without progress.
        let relaxed_budget_exhausted = consecutive_relaxed_continuations >= MAX_CONSECUTIVE_RELAXED_CONTINUATIONS;
        if !asks_user_question
            && !relaxed_budget_exhausted
            && not_conclusive
            && has_relaxed_continuation_intent
            && recent_tool_activity
        {
            return d_relaxed(true, "recent_tool_activity_relaxed");
        }
        if !asks_user_question
            && !relaxed_budget_exhausted
            && not_conclusive
            && has_relaxed_continuation_intent
            && last_user_requested_progressive_work
        {
            return d_relaxed(true, "progressive_relaxed");
        }
        if full_auto {
            if relaxed_budget_exhausted {
                return d(false, "full_auto_relaxed_cap");
            }
            return d_relaxed(true, "full_auto_continuation");
        }
        return d(false, "non_interim_text");
    }

    if last_user_follow_up {
        return d(true, "follow_up_prompt");
    }

    if recent_tool_activity {
        return d(true, "recent_tool_activity");
    }

    if last_user_requested_progressive_work {
        return d(true, "progressive_request");
    }

    if full_auto {
        return d(true, "full_auto_continuation");
    }

    d(false, "interactive_mode")
}

/// Budget/recovery phrasing that must not end a tracker-incomplete turn,
/// including tool-call / tool-loop budget vocabulary the model uses in status
/// recaps ("blocked by turn tool budget", "tool loop budget", "read cap").
fn recoverable_tracker_budget_phrasing(lower: &str) -> bool {
    vtcode_core::core::agent::completion::recoverable_status_recap_phrasing(lower)
}

/// True-handoff detector used when `task_tracker` still has incomplete steps.
///
/// Mid-text `?` in status sections and optional-offer closers (`happy to`,
/// `let me know if`, `if you want me to`) are **not** handoffs — only a
/// trailing clarifying question or strong interview/permission phrases end the
/// turn while TODO work remains. Broad mid-text fragments like "can you" /
/// "need your" are omitted so status recaps that mention code or config are
/// not treated as user asks.
fn tracker_incomplete_text_is_user_handoff(text: &str) -> bool {
    let trimmed = text.trim();
    if trimmed.is_empty() {
        return true;
    }
    if trimmed.ends_with('?') {
        return true;
    }
    let lower = trimmed.to_ascii_lowercase();
    const STRONG_HANDOFF: &[&str] = &[
        "please provide",
        "please confirm",
        "please approve",
        "need your approval",
        "need your permission",
        "need your decision",
        "need you to choose",
        "need you to confirm",
        "waiting for your",
        "awaiting your",
        "your choice",
        "your decision",
        "need approval",
        "requires approval",
        "require approval",
        "approval is required",
        "need permission",
        "requires permission",
        "require permission",
        "permission is required",
        "grant permission",
        "authorize this",
        "need a decision",
        "need clarification",
        "waiting for input",
        "awaiting input",
        "waiting on you",
        "how should i proceed",
        "what should i do",
        "what would you like",
        "how would you like",
        "do you want me to",
    ];
    // Clause-start interview asks (not bare mid-text "can you" in status prose).
    const CLAUSE_START_HANDOFF: &[&str] = &["could you ", "can you ", "shall i ", "should i "];
    if STRONG_HANDOFF.iter().any(|pattern| lower.contains(pattern)) {
        return true;
    }
    CLAUSE_START_HANDOFF
        .iter()
        .any(|pattern| lower.trim_start().starts_with(pattern) || lower.contains(&format!("\n{pattern}")))
}

/// Tracker-aware override: when `task_tracker` still has incomplete steps,
/// status-only responses (including "blocked by budget" / "next step on
/// resume" recaps) must not end the turn and nudge the user. Force a
/// non-relaxed continuation unless the text is a true user handoff.
pub(super) fn apply_tracker_continuation_override(
    mut decision: InterimTextContinuationDecision,
    tracker_incomplete: bool,
    planning_active: bool,
    text: &str,
) -> InterimTextContinuationDecision {
    if !tracker_incomplete || planning_active {
        return decision;
    }
    // True safety/permission/credential/verification-gate handoffs always end
    // the turn, even if an earlier full-auto/relaxed path already chose to
    // continue. In-turn tracker continuation must not race past the anti-blind
    // gate the outer loop will not auto-queue.
    if vtcode_core::core::agent::completion::tracker_final_text_is_safety_handoff(text)
        || text.to_ascii_lowercase().contains("verification is still pending")
        || text.to_ascii_lowercase().contains("unverified assistant responses")
    {
        decision.should_continue = false;
        decision.reason = "tracker_safety_handoff";
        decision.is_relaxed_continuation = false;
        return decision;
    }
    if decision.should_continue {
        return decision;
    }
    if tracker_incomplete_text_is_user_handoff(text) {
        return decision;
    }
    let lower = text.to_ascii_lowercase();
    let explicit_safety_handoff = lower.contains("safety fuse")
        || lower.contains("permission denied")
        || lower.contains("access denied")
        || lower.contains("requires manual intervention")
        || lower.contains("missing credentials")
        || lower.contains("credentials are missing")
        || lower.contains("policy block")
        || lower.contains("blocked by policy")
        || lower.contains("denied by policy")
        || lower.contains("denied by tool policy")
        || lower.contains("denied by workspace tool policy");
    if explicit_safety_handoff {
        decision.should_continue = false;
        decision.reason = "tracker_safety_handoff";
        decision.is_relaxed_continuation = false;
        return decision;
    }
    // Recoverable budget/recovery phrasing continues even when the recap also
    // contains blocker tokens like "blocked by".
    if has_explicit_blocker(&lower) && !recoverable_tracker_budget_phrasing(&lower) {
        return decision;
    }
    decision.should_continue = true;
    decision.reason = "tracker_incomplete_continuation";
    decision.is_relaxed_continuation = false;
    decision
}

/// Classify the outcome emitted with the text-response telemetry record.
///
/// The canonical turn events still describe completed and blocked turns. This
/// finer-grained metric makes the continuation decision visible before the
/// outer turn loop finalizes, including the intentional input handoff and the
/// bounded full-auto continuation path.
pub(super) fn continuation_telemetry_outcome(
    full_auto: bool,
    decision: &InterimTextContinuationDecision,
) -> &'static str {
    if decision.should_continue {
        return if full_auto {
            "full_auto_continuation"
        } else {
            "continuation"
        };
    }

    match decision.reason {
        "full_auto_user_input_handoff" => "user_input_handoff",
        "full_auto_relaxed_cap" => "safety_cap_handoff",
        "full_auto_safety_handoff" => "safety_handoff",
        "full_auto_blocked_handoff" | "full_auto_empty_response" => "blocked_handoff",
        _ => "final_completion",
    }
}

pub(super) fn push_system_directive_once(history: &mut Vec<uni::Message>, directive: &str) {
    let current_turn_start = history
        .iter()
        .rposition(|message| message.role == uni::MessageRole::User)
        .unwrap_or(0);
    let already_present = history
        .iter()
        .skip(current_turn_start)
        .any(|message| message.role == uni::MessageRole::System && message.content.as_text().trim() == directive);
    if !already_present {
        history.push(uni::Message::system(directive.to_string()));
    }
}

/// Returns true when the **last** clause (after the final sentence boundary) contains
/// a conclusive marker like "completed", "done", "fixed", "summary", etc.
/// A middle clause with "completed" followed by "Now let me ..." does NOT match —
/// only the final clause determines conclusiveness.
fn last_clause_contains_conclusive_marker(lower: &str) -> bool {
    let conclusive_markers = [
        "completed",
        "done",
        "fixed",
        "resolved",
        "summary",
        "final review",
        "final blocker",
        "next action",
        "what changed",
        "validation",
        "passed",
        "passes",
        "cannot proceed",
        "can't proceed",
        "blocked by",
        "all set",
    ];
    let last_clause = lower
        .char_indices()
        .rfind(|(_, ch)| ['.', '!', '\n', '—', '…'].contains(ch))
        .map(|(idx, ch)| lower[idx + ch.len_utf8()..].trim_start())
        .unwrap_or(lower);
    conclusive_markers.iter().any(|marker| last_clause.contains(marker))
}

/// Full-auto responses need to recognize a conclusive marker even when the
/// final sentence ends with punctuation. The legacy helper intentionally keeps
/// its existing clause behavior for interactive relaxed continuation; this
/// companion handles the terminal full-auto classification without broadening
/// the interactive path.
fn full_auto_response_is_conclusive(lower: &str) -> bool {
    let conclusive_markers = [
        "completed",
        "complete",
        "done",
        "fixed",
        "resolved",
        "summary",
        "result summary",
        "final review",
        "final blocker",
        "next action",
        "what changed",
        "validation",
        "validated",
        "passed",
        "passes",
        "finished",
        "successful",
        "successfully",
        "no issues",
        "no errors",
        "nothing to fix",
        "all set",
        "that's all",
        "that’s all",
    ];
    // Strip terminal sentence/Markdown punctuation before looking for the
    // last clause. This keeps an inline-code `!` from becoming the apparent
    // final clause in text such as ``printing `Hello!`.``.
    let terminal_text =
        lower.trim_end_matches(|ch| ['.', '!', '\n', '—', '…', '`', '*', '_', ')', ']', '}'].contains(&ch));
    let last_non_empty_clause = terminal_text
        .split(|ch| ['.', '!', '\n', '—', '…'].contains(&ch))
        .rfind(|clause| !clause.trim().is_empty())
        .unwrap_or(terminal_text)
        .trim();
    // A future action can mention a successful outcome as part of its plan,
    // e.g. "I'll run the linter again to verify the warnings are resolved."
    // Treat only a final clause that is not itself continuation intent as a
    // completed result.
    let last_clause_has_continuation_intent =
        has_interim_intent_clause(last_non_empty_clause) || has_relaxed_continuation_intent(last_non_empty_clause);
    if !last_clause_has_continuation_intent
        && (last_clause_contains_conclusive_marker(terminal_text)
            || conclusive_markers.iter().any(|marker| last_non_empty_clause.contains(marker)))
    {
        return true;
    }

    let has_summary_heading = lower.lines().any(|line| {
        let line = line.trim().trim_start_matches(['#', '*', '-', '>', ' ']);
        ["summary:", "result:", "results:", "what changed:", "final answer:"]
            .iter()
            .any(|marker| line.starts_with(marker))
    });
    has_summary_heading && !has_interim_intent_clause(lower)
}

fn has_explicit_blocker(lower: &str) -> bool {
    [
        "i'm blocked",
        "im blocked",
        "i’m blocked",
        "i am blocked",
        "is blocked",
        "blocked by",
        "cannot proceed",
        "can't proceed",
        "can’t proceed",
        "unable to proceed",
        "cannot continue",
        "can't continue",
        "can’t continue",
        "unable to continue",
        "cannot complete",
        "can't complete",
        "can’t complete",
        "unable to complete",
        "no access to",
        "permission denied",
        "access denied",
        "missing credentials",
        "credentials are missing",
        "not possible to proceed",
        "requires manual intervention",
        "safety fuse",
        "tool-call safety fuse",
        "policy block",
        "verification is still pending",
        "unverified assistant responses",
        "anti-blind",
        "verification gate",
        "turn blocked after",
    ]
    .iter()
    .any(|pattern| lower.contains(pattern))
}

pub(super) fn is_interim_progress_update(text: &str) -> bool {
    let trimmed = text.trim();
    if trimmed.is_empty() || trimmed.len() > 800 {
        return false;
    }

    let lower = trimmed.to_ascii_lowercase();
    if !has_interim_intent_clause(&lower) {
        return false;
    }

    if trimmed.contains('?') || contains_user_input_request(&lower) {
        return false;
    }

    !last_clause_contains_conclusive_marker(&lower)
}

fn last_user_message_is_follow_up(history: &[uni::Message]) -> bool {
    history
        .iter()
        .rev()
        .find(|message| message.role == uni::MessageRole::User)
        .is_some_and(|message| {
            crate::agent::runloop::unified::state::is_follow_up_prompt_like(message.content.as_text().as_ref())
        })
}

fn has_recent_tool_activity(history: &[uni::Message]) -> bool {
    history.iter().rev().take(16).any(|message| {
        message.role == uni::MessageRole::Tool || message.tool_call_id.is_some() || message.tool_calls.is_some()
    })
}

fn last_user_requested_progressive_work(history: &[uni::Message]) -> bool {
    let Some(text) = last_user_message_text(history) else {
        return false;
    };
    [
        "explore",
        "inspect",
        "look into",
        "investigate",
        "debug",
        "trace",
        "check",
        "review",
        "analy",
        "walk through",
        "run ",
        "execute",
        "format",
        "cargo fmt",
        "cargo check",
        "cargo test",
        "fix",
        "edit",
        "update",
        "change",
        "modify",
        "scan",
        "search",
        "grep",
        "ast-grep",
        "find ",
        "use vt code",
        "semantic code understanding",
        "show me how",
    ]
    .iter()
    .any(|needle| text.contains(needle))
}

/// Returns true when the latest user request asks for information or a
/// summary, without also asking the agent to change or verify the workspace.
/// Such requests are terminal once the model has supplied the answer, even in
/// full-auto mode. Action-oriented requests deliberately remain on the
/// autonomous continuation path.
fn last_user_requested_read_only_answer(history: &[uni::Message]) -> bool {
    let Some(text) = last_user_message_text(history) else {
        return false;
    };

    let asks_for_information = [
        "summarize",
        "summary",
        "overview",
        "explain",
        "describe",
        "what is",
        "what's",
        "what are",
        "why ",
        "how does",
        "how do",
        "show me",
        "tell me",
        "compare",
        "explore",
        "walk me through",
    ]
    .iter()
    .any(|needle| text.contains(needle));
    if !asks_for_information {
        return false;
    }

    // Match imperative/action clauses rather than any occurrence of a verb.
    // For example, "How do I fix the parser?" is still an informational
    // question, while "Explore the parser and fix the regression" is an
    // execution request.
    let action_verbs = [
        "fix ",
        "edit ",
        "update ",
        "change ",
        "modify ",
        "create ",
        "implement ",
        "refactor ",
        "write ",
        "delete ",
        "remove ",
        "apply ",
        "run ",
        "execute ",
        "format ",
        "test ",
        "build ",
    ];
    let action_clause = action_verbs
        .iter()
        .any(|verb| text.starts_with(verb) || text.match_indices(verb).any(|(index, _)| is_clause_start(&text, index)));
    let chained_action = action_verbs.iter().any(|verb| {
        [" and ", " then ", "; ", ", "]
            .iter()
            .any(|connector| text.contains(&format!("{connector}{verb}")))
    });

    !(action_clause || chained_action)
}

fn has_interim_intent_clause(lower: &str) -> bool {
    if clause_has_continuation_intent(lower) {
        return true;
    }

    for (idx, ch) in lower.char_indices() {
        if matches!(ch, '.' | '!' | '?' | ':' | ';' | '\n' | '—' | '…') {
            let remainder = lower[idx + ch.len_utf8()..].trim_start();
            if !remainder.is_empty() && clause_has_continuation_intent(remainder) {
                return true;
            }
        }
    }

    false
}

fn has_relaxed_continuation_intent(lower: &str) -> bool {
    if has_interim_intent_clause(lower) {
        return true;
    }

    [
        " let me ",
        " i'll ",
        " i will ",
        " i need to ",
        " i want to ",
        " i'd like to ",
        " next step ",
        " next up:",
        " continuing ",
        " time to ",
    ]
    .iter()
    .any(|needle| lower.contains(needle))
}

/// Returns true when the text asks for user input,
/// indicating the model is waiting for input rather than continuing autonomously.
/// This prevents infinite loops where the model explains blockers and asks the
/// user how to proceed, but the system keeps injecting continue directives.
fn contains_user_input_request(lower: &str) -> bool {
    let anywhere_patterns = [
        "please provide",
        "need your",
        "need you to",
        "please confirm",
        "please let me know",
        "waiting for your",
        "awaiting your",
        "your choice",
        "your decision",
        "need approval",
        "need your approval",
        "requires approval",
        "require approval",
        "approval is required",
        "please approve",
        "need permission",
        "need your permission",
        "requires permission",
        "require permission",
        "permission is required",
        "grant permission",
        "authorize this",
        "need a decision",
        "need clarification",
        "waiting for input",
        "awaiting input",
        "waiting on you",
        // Closing offers of optional follow-up work ("…say the word and I'll
        // do a larger pass"): the model has finished and is waiting on the
        // user, so the relaxed continuation paths must not re-prompt past the
        // final answer (checkpoint session-vtcode-20260912T083718Z: a verified
        // recap was continued twice and the turn blocked on the text cap).
        "say the word",
        "tell me and i'll",
        "let me know if you want",
        "if you want me to",
        "just ask",
        "happy to",
    ];
    if anywhere_patterns.iter().any(|pattern| lower.contains(pattern)) {
        return true;
    }

    let clause_start_patterns = [
        "could you",
        "can you",
        "how would you like",
        "what would you like",
        "which option",
        "would you like me to",
        "shall i",
        "do you want me to",
        "should i",
        "how should i proceed",
        "what should i do",
        "how do you want to proceed",
        "how would you like to proceed",
        "what would you prefer",
        "which approach",
        "any suggestions",
        "let me know how",
        "let me know if",
        "let me know what",
        "let me know which",
        "let me know whether",
        "let me know where",
        "let me know when",
        "let me know why",
        "tell me how",
        "tell me if",
        "tell me what",
        "tell me which",
        "tell me whether",
        "tell me where",
        "tell me when",
        "tell me why",
    ];
    clause_start_patterns
        .iter()
        .any(|pattern| contains_phrase_at_clause_start(lower, pattern))
}

fn contains_phrase_at_clause_start(lower: &str, phrase: &str) -> bool {
    lower.match_indices(phrase).any(|(idx, _)| is_clause_start(lower, idx))
}

fn is_clause_start(text: &str, idx: usize) -> bool {
    for ch in text[..idx].chars().rev() {
        if ch == '\n' {
            return true;
        }
        if ch.is_whitespace() {
            continue;
        }
        return matches!(ch, '.' | '!' | '?' | ':' | ';' | ',' | '—' | '…');
    }
    true
}

/// Returns true when a single clause expresses an intent to continue working.
///
/// Instead of matching an ever-growing list of specific prefixes, this
/// normalizes away clause-initial transition words ("now", "next", "then",
/// "first") and subjects ("i", "we"), then checks a compact set of
/// grammatical patterns that capture the underlying linguistic structure
/// of continuation intent.  This handles many more phrasings automatically
/// than explicitly listing every variant.
fn clause_has_continuation_intent(clause: &str) -> bool {
    let clause = clause.trim_start();
    if clause.is_empty() {
        return false;
    }

    let normalized = normalize_clause(clause);
    if normalized.is_empty() {
        return false;
    }

    core_intent_matches(normalized) || starts_with_present_progress_update(normalized)
}

/// Strip leading transition words and subjects to reach the core intent
/// expression.  Reduces hundreds of possible phrasings down to a handful
/// of grammatical patterns checked by [`core_intent_matches`].
///
/// Transitions: "now", "next" (only before i/we), "then", "first"
/// Possessives: "my" (for "my next step is ...")
/// Subjects: "i" (and optionally "am"), "we"
fn normalize_clause(s: &str) -> &str {
    let s = s.trim_start();
    let s = s.strip_prefix("now ").unwrap_or(s);
    let s = s.strip_prefix("then ").unwrap_or(s);
    let s = s.strip_prefix("first, ").unwrap_or(s);
    let s = s.strip_prefix("first ").unwrap_or(s);
    let s = s.strip_prefix("next, ").unwrap_or(s);
    // "next" before a subject is a transition; otherwise it may be
    // part of an intent expression ("next step", "next up").
    let s = match s.strip_prefix("next ") {
        Some(after) if after.starts_with("i ") || after.starts_with("we ") => after,
        _ => s,
    };
    let s = s.strip_prefix("my ").unwrap_or(s);
    let s = s.strip_prefix("i am ").unwrap_or(s);
    // Handle both "i " (uncontracted) and contractions (i'll, i'd, i'm, i've).
    // For contractions we strip only the "i", keeping the apostrophe so that
    // patterns like "'ll " and "'d like to " still match.
    let s = if let Some(after) = s.strip_prefix("i ") {
        after
    } else if let Some(after) = s.strip_prefix("i").filter(|after| after.starts_with('\'')) {
        after
    } else {
        s
    };
    let s = s.strip_prefix("we ").unwrap_or(s);
    s.trim_start()
}

/// Check the core grammatical patterns of continuation intent.
fn core_intent_matches(text: &str) -> bool {
    // "let {me|us|'s}" + action verb
    if text.starts_with("let ") {
        return true;
    }

    // <intent-verb> "to" <action>
    // Covers: need to, want to, going to, plan to, intend to,
    //         have to, 'm going to, 'd like to, hope to, etc.
    const TO_INTENTS: &[&str] = &[
        "need to ",
        "want to ",
        "going to ",
        "plan to ",
        "intend to ",
        "have to ",
        "'m going to ",
        "'d like to ",
        "hope to ",
    ];
    if TO_INTENTS.iter().any(|v| text.starts_with(v)) {
        return true;
    }

    // <modal> <action> — exclude conclusive follow-ups
    if let Some(rest) = text.strip_prefix("will ").or_else(|| text.strip_prefix("'ll ")) {
        return !rest.starts_with("be ") && !rest.starts_with("now be ");
    }

    // Standalone expressions that don't fit the verb patterns above
    if text.starts_with("time to ")
        || text.starts_with("next up:")
        || text.starts_with("next step ")
        || text.starts_with("continuing")
    {
        return true;
    }

    false
}

fn starts_with_present_progress_update(lower: &str) -> bool {
    let present_progress_prefixes = [
        "running ",
        "checking ",
        "formatting ",
        "scanning ",
        "inspecting ",
        "searching ",
        "reading ",
        "reviewing ",
        "tracing ",
        "debugging ",
    ];
    let forward_markers = [
        " now",
        " then ",
        " next ",
        " follow-up",
        " to confirm",
        " to check",
        " to verify",
        " to inspect",
    ];

    present_progress_prefixes.iter().any(|prefix| lower.starts_with(prefix))
        && forward_markers.iter().any(|marker| lower.contains(marker))
}

fn last_user_message_text(history: &[uni::Message]) -> Option<String> {
    history
        .iter()
        .rev()
        .find(|message| message.role == uni::MessageRole::User)
        .map(|message| message.content.as_text().to_ascii_lowercase())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::agent::runloop::unified::run_loop_context::RecoveryMode;
    use crate::agent::runloop::unified::turn::context::{TurnHandlerOutcome, TurnLoopResult};
    use crate::agent::runloop::unified::turn::turn_processing::test_support::TestTurnProcessingBacking;

    #[test]
    fn system_directive_is_not_duplicated_after_intervening_messages() {
        let directive = "Synthesize a final response now.";
        let mut history = vec![uni::Message::system(directive.to_string())];
        for index in 0..4 {
            history.push(uni::Message::assistant(format!("intervening message {index}")));
        }

        push_system_directive_once(&mut history, directive);

        assert_eq!(
            history
                .iter()
                .filter(|message| message.role == uni::MessageRole::System && message.content.as_text() == directive)
                .count(),
            1
        );
    }

    #[test]
    fn system_directive_is_reissued_for_a_new_user_turn() {
        let directive = "Synthesize a final response now.";
        let mut history = vec![
            uni::Message::system(directive.to_string()),
            uni::Message::user("first request".to_string()),
        ];

        push_system_directive_once(&mut history, directive);

        assert_eq!(
            history
                .iter()
                .filter(|message| message.role == uni::MessageRole::System && message.content.as_text() == directive)
                .count(),
            2
        );
    }

    #[test]
    fn follow_up_prompt_detection_accepts_continue_variants() {
        assert!(crate::agent::runloop::unified::state::is_follow_up_prompt_like("continue"));
        assert!(crate::agent::runloop::unified::state::is_follow_up_prompt_like("continue."));
        assert!(crate::agent::runloop::unified::state::is_follow_up_prompt_like("go on"));
        assert!(crate::agent::runloop::unified::state::is_follow_up_prompt_like("please continue"));
        assert!(crate::agent::runloop::unified::state::is_follow_up_prompt_like(
            "Continue autonomously from the last stalled turn. Stall reason: x."
        ));
        assert!(!crate::agent::runloop::unified::state::is_follow_up_prompt_like("run cargo clippy and fix"));
    }

    #[test]
    fn interim_progress_detection_requires_non_conclusive_intent_text() {
        assert!(is_interim_progress_update("Let me fix the second collapsible if statement:"));
        assert!(is_interim_progress_update(
            "Let me fix the second collapsible if statement in the Anthropic provider:"
        ));
        assert!(is_interim_progress_update(
            "Now I need to update the function body to use settings.reasoning_effort and settings.verbosity:"
        ));
        assert!(is_interim_progress_update("I'll continue with the next fix."));
        assert!(is_interim_progress_update(
            "Running formatter now, then I'll do a quick follow-up check (`cargo check`) to confirm nothing regressed."
        ));
        assert!(is_interim_progress_update(
            "The structural search keeps returning empty results. Let me verify the indexer is working and try with a simpler known pattern:"
        ));
        assert!(!is_interim_progress_update("I need you to choose which option to apply."));
        assert!(!is_interim_progress_update("Let me know what you'd like to dig into next."));
        assert!(!is_interim_progress_update("Running cargo fmt uses rustfmt to rewrite the source files."));
        assert!(!is_interim_progress_update("Completed. All requested fixes are done."));
        assert!(!is_interim_progress_update("Final review: two blockers remain with next action."));
    }

    #[test]
    fn autonomous_continue_triggers_for_follow_up_and_interim_text() {
        let history = vec![uni::Message::user("continue".to_string())];
        assert!(
            evaluate_interim_text_continuation(true, false, &history, "Let me fix the next issue.", 0).should_continue
        );
        assert!(
            !evaluate_interim_text_continuation(true, true, &history, "Let me fix the next issue.", 0).should_continue
        );
        assert!(
            evaluate_interim_text_continuation(false, false, &history, "Let me fix the next issue.", 0).should_continue
        );
    }

    #[test]
    fn autonomous_continue_triggers_for_interim_text_after_tool_activity() {
        let history = vec![
            uni::Message::user("run cargo clippy and fix".to_string()),
            uni::Message::assistant("I will run cargo clippy now.".to_string()).with_tool_calls(vec![
                uni::ToolCall::function("call_1".to_string(), "command_session".to_string(), "{}".to_string()),
            ]),
            uni::Message::tool_response("call_1".to_string(), "warning: ...".to_string()),
        ];

        assert!(
            evaluate_interim_text_continuation(
                true,
                false,
                &history,
                "Now I need to update the function body to use settings.reasoning_effort and settings.verbosity:",
                0
            )
            .should_continue
        );
    }

    #[test]
    fn autonomous_continue_triggers_for_execution_request_without_prior_tool_activity() {
        let history = vec![
            uni::Message::user("run cargo clippy and fix".to_string()),
            uni::Message::assistant("I will start now.".to_string()),
        ];

        assert!(
            evaluate_interim_text_continuation(
                true,
                false,
                &history,
                "Now I need to update the function body to use settings.reasoning_effort and settings.verbosity:",
                0
            )
            .should_continue
        );
    }

    #[test]
    fn full_auto_continues_after_non_conclusive_text_without_tool_activity() {
        let history = vec![uni::Message::user("work through the requested task".to_string())];
        let decision = evaluate_interim_text_continuation(
            true,
            false,
            &history,
            "I reviewed the request and have more work to do.",
            0,
        );

        assert!(decision.should_continue);
        assert_eq!(decision.reason, "full_auto_continuation");
        assert!(decision.is_relaxed_continuation);
    }

    #[test]
    fn full_auto_continues_after_inspect_fix_and_check_updates() {
        let history = vec![uni::Message::user("complete the requested work".to_string())];
        for text in [
            "I'll inspect the remaining files now.",
            "I'll fix the parser branch next.",
            "I'll check the targeted test output before summarizing.",
        ] {
            let decision = evaluate_interim_text_continuation(true, false, &history, text, 0);
            assert!(decision.should_continue, "expected continuation for {text:?}");
        }
    }

    #[test]
    fn full_auto_stops_after_a_read_only_summary_request_without_keyword_markers() {
        let history = vec![uni::Message::user(
            "Explore the codebase and summarize what makes this project special.".to_string(),
        )];
        let answer = "Here's a consolidated table of the project's differentiators:\n\n| Area | Meaning |\n|---|---|\n| Runtime | The harness coordinates tools and context. |\n| Safety | Commands are checked before execution. |";

        let decision = evaluate_interim_text_continuation(true, false, &history, answer, 0);

        assert!(!decision.should_continue);
        assert_eq!(decision.reason, "full_auto_read_only_answer");
    }

    #[test]
    fn full_auto_keeps_action_requests_on_the_autonomous_path() {
        let history = vec![uni::Message::user(
            "Explore the parser, summarize the issue, and fix the regression.".to_string(),
        )];

        let decision = evaluate_interim_text_continuation(true, false, &history, "I reviewed the request.", 0);

        assert!(decision.should_continue);
        assert_eq!(decision.reason, "full_auto_continuation");
    }

    #[test]
    fn full_auto_stops_for_an_informational_how_to_fix_question() {
        let history = vec![uni::Message::user("How do I fix the parser regression?".to_string())];
        let decision = evaluate_interim_text_continuation(
            true,
            false,
            &history,
            "The parser loses the trailing token when the input ends with a delimiter.",
            0,
        );

        assert!(!decision.should_continue);
        assert_eq!(decision.reason, "full_auto_read_only_answer");
    }

    #[tokio::test]
    async fn read_only_summary_is_published_as_final_without_a_follow_up_directive() {
        let mut backing = TestTurnProcessingBacking::new(4).await;
        let mut ctx = backing.turn_processing_context();
        ctx.full_auto = true;
        ctx.working_history.push(uni::Message::user(
            "Explore the codebase and summarize what makes this project special.".to_string(),
        ));

        let outcome = ctx
            .handle_text_response(
                "Here's a consolidated table of the project's differentiators:\n\n| Area | Meaning |\n|---|---|\n| Runtime | The harness coordinates tools and context. |\n| Safety | Commands are checked before execution. |"
                    .to_string(),
                Vec::new(),
                None,
                None,
                true,
            )
            .await
            .expect("read-only summary should complete");

        assert!(matches!(outcome, TurnHandlerOutcome::Break(TurnLoopResult::Completed { .. })));
        assert!(!ctx.working_history.iter().any(|message| {
            message.role == uni::MessageRole::System
                && message.content.as_text().contains(AUTONOMOUS_CONTINUE_DIRECTIVE)
        }));
        assert_eq!(
            ctx.working_history
                .iter()
                .rev()
                .find(|message| message.role == uni::MessageRole::Assistant)
                .and_then(|message| message.phase),
            Some(uni::AssistantPhase::FinalAnswer)
        );
    }

    #[test]
    fn full_auto_stops_on_completion_summaries() {
        let history = vec![uni::Message::user("finish the requested task".to_string())];
        for text in [
            "Completed the requested changes. All targeted tests passed.",
            "Summary:\n- Updated the parser and verified the targeted test.",
            "The result is complete.",
        ] {
            let decision = evaluate_interim_text_continuation(true, false, &history, text, 0);
            assert!(!decision.should_continue, "expected terminal completion for {text:?}");
            assert_eq!(decision.reason, "full_auto_final_completion");
        }
    }

    #[test]
    fn full_auto_stops_on_questions_approval_requests_and_blockers() {
        let history = vec![uni::Message::user("complete the requested task".to_string())];
        let cases = [
            ("Should I continue with the migration?", "full_auto_user_input_handoff"),
            ("I need your approval before I proceed.", "full_auto_user_input_handoff"),
            // Credentials are a true safety handoff (S2A): the shared
            // `tracker_final_text_is_safety_handoff` classifier wins over the
            // generic blocker bucket, so the turn ends as a safety handoff
            // rather than a plain blocked handoff.
            ("I’m blocked by missing credentials.", "full_auto_safety_handoff"),
        ];

        for (text, reason) in cases {
            let decision = evaluate_interim_text_continuation(true, false, &history, text, 0);
            assert!(!decision.should_continue, "expected terminal handoff for {text:?}");
            assert_eq!(decision.reason, reason);
        }
    }

    #[test]
    fn interactive_mode_keeps_non_conclusive_text_terminal() {
        let history = vec![uni::Message::user("work through the requested task".to_string())];
        let decision = evaluate_interim_text_continuation(
            false,
            false,
            &history,
            "I reviewed the request and have more work to do.",
            0,
        );

        assert!(!decision.should_continue);
        assert_eq!(decision.reason, "non_interim_text");
    }

    #[test]
    fn continuation_telemetry_distinguishes_terminal_paths() {
        let history = vec![uni::Message::user("complete the requested task".to_string())];
        let continuing = evaluate_interim_text_continuation(true, false, &history, "I reviewed the request.", 0);
        assert_eq!(continuation_telemetry_outcome(true, &continuing), "full_auto_continuation");

        let completed = evaluate_interim_text_continuation(true, false, &history, "The result is complete.", 0);
        assert_eq!(continuation_telemetry_outcome(true, &completed), "final_completion");

        let input = evaluate_interim_text_continuation(true, false, &history, "Should I continue?", 0);
        assert_eq!(continuation_telemetry_outcome(true, &input), "user_input_handoff");

        let capped = evaluate_interim_text_continuation(true, false, &history, "I have more work to do.", 3);
        assert_eq!(capped.reason, "full_auto_relaxed_cap");
        assert_eq!(continuation_telemetry_outcome(true, &capped), "safety_cap_handoff");

        let blocked = evaluate_interim_text_continuation(true, false, &history, "I cannot proceed without access.", 0);
        assert_eq!(continuation_telemetry_outcome(true, &blocked), "blocked_handoff");

        // Safety/verification handoffs must not be counted as completions.
        let safety = evaluate_interim_text_continuation(
            true,
            false,
            &history,
            "Verification is still pending; run cargo check next.",
            0,
        );
        assert_eq!(safety.reason, "full_auto_safety_handoff");
        assert_eq!(continuation_telemetry_outcome(true, &safety), "safety_handoff");
    }

    #[test]
    fn autonomous_continue_triggers_for_exploration_request_without_full_auto() {
        let history = vec![
            uni::Message::user("explore about vtcode core agent loop".to_string()),
            uni::Message::assistant("I can help.".to_string()),
        ];

        assert!(evaluate_interim_text_continuation(
            false,
            false,
            &history,
            "I'll quickly inspect the actual vtcode-core runloop files and then summarize the core agent loop concretely from code.",
            0
        )
        .should_continue);
    }

    #[test]
    fn autonomous_continue_does_not_trigger_for_explanatory_request_without_full_auto() {
        let history = vec![
            uni::Message::user("tell me about core agent loop".to_string()),
            uni::Message::assistant("I can help.".to_string()),
        ];

        assert!(!evaluate_interim_text_continuation(
            false,
            false,
            &history,
            "I'll quickly inspect the actual vtcode-core runloop files and then summarize the core agent loop concretely from code.",
            0
        )
        .should_continue);
    }

    #[tokio::test]
    async fn recovery_pass_progress_only_text_completes_turn() {
        let mut backing = TestTurnProcessingBacking::new(4).await;
        let mut ctx = backing.turn_processing_context();
        ctx.activate_recovery("loop detector");
        assert!(ctx.consume_recovery_pass());

        let outcome = ctx
            .handle_text_response("Let me try a narrower search next.".to_string(), Vec::new(), None, None, false)
            .await
            .expect("recovery response should be handled");

        assert!(matches!(outcome, TurnHandlerOutcome::Break(TurnLoopResult::Completed { .. })));
        assert!(!ctx.is_recovery_active());
    }

    #[tokio::test]
    async fn recovery_pass_diagnostic_then_next_step_text_completes_turn() {
        let mut backing = TestTurnProcessingBacking::new(4).await;
        let mut ctx = backing.turn_processing_context();
        ctx.activate_recovery("turn balancer");
        assert!(ctx.consume_recovery_pass());

        let outcome = ctx
            .handle_text_response(
                "The structural search keeps returning empty results. Let me verify the indexer is working and try with a simpler known pattern:".to_string(),
                Vec::new(),
                None,
                None,
                false,
            )
            .await
            .expect("recovery response should be handled");

        assert!(matches!(outcome, TurnHandlerOutcome::Break(TurnLoopResult::Completed { .. })));
        assert!(!ctx.is_recovery_active());
    }

    #[tokio::test]
    async fn tool_free_recovery_continuation_intent_text_is_terminal() {
        // Regression guard for the post-tool follow-up infinite loop.
        // During tool-free recovery, even when the text expresses continuation
        // intent AND there is recent tool activity (the exact combination that
        // previously produced a non-relaxed `Continue` via "recent_tool_activity",
        // resetting `consecutive_relaxed_continuations` to 0 and re-enabling
        // tools after `finish_recovery_pass()`), the turn must end. The recovery
        // text IS the final answer; allowing continuation re-enables tools and
        // re-triggers recovery when the follow-up fails again — an infinite
        // cycle no existing bound catches.
        let mut backing = TestTurnProcessingBacking::new(4).await;
        let mut ctx = backing.turn_processing_context();
        ctx.working_history
            .push(uni::Message::user("run cargo nextest and summarize".to_string()));
        ctx.working_history
            .push(uni::Message::assistant(String::new()).with_tool_calls(vec![uni::ToolCall::function(
                "call_1".to_string(),
                "command_session".to_string(),
                "{}".to_string(),
            )]));
        ctx.working_history
            .push(uni::Message::tool_response("call_1".to_string(), "test result: ok".to_string()));
        ctx.activate_recovery("post-tool follow-up failure");
        assert!(ctx.consume_recovery_pass());
        assert!(ctx.recovery_is_tool_free());

        // Sanity: without recovery this exact text+history would continue.
        // "Let me continue analyzing the results." is interim progress
        // (<=800 chars, has "let me" intent clause, no question, no
        // conclusive marker) and recent_tool_activity is true → the raw
        // evaluator returns should_continue=true, is_relaxed_continuation=false.
        assert!(
            evaluate_interim_text_continuation(
                true,
                false,
                ctx.working_history,
                "Let me continue analyzing the results.",
                0,
            )
            .should_continue
        );

        // Under tool-free recovery, the turn loop must override to terminal.
        let outcome = ctx
            .handle_text_response("Let me continue analyzing the results.".to_string(), Vec::new(), None, None, false)
            .await
            .expect("recovery response should be handled");

        assert!(
            matches!(outcome, TurnHandlerOutcome::Break(TurnLoopResult::Completed { .. })),
            "tool-free recovery text with continuation intent must end the turn, \
             not re-enable tools and loop"
        );
        // Recovery is finished (not active), and the turn did not continue.
        assert!(!ctx.is_recovery_active());
        assert!(!ctx.working_history.iter().any(|message| {
            message.role == uni::MessageRole::System
                && message.content.as_text().trim() == AUTONOMOUS_CONTINUE_DIRECTIVE
        }));
    }

    #[test]
    fn detects_new_intent_prefixes_as_interim() {
        assert!(is_interim_progress_update("I'd like to check the next file before proceeding."));
        assert!(is_interim_progress_update("I want to verify the output of the previous step."));
        assert!(is_interim_progress_update("My next step is to run the full test suite."));
        assert!(is_interim_progress_update("Time to fix the remaining lint warnings."));
        assert!(is_interim_progress_update("Let me now inspect the second module for regressions."));
    }

    #[test]
    fn em_dash_boundary_detected_as_interim() {
        assert!(is_interim_progress_update("First check passed—now let me verify the second component."));
        assert!(has_interim_intent_clause("done with the first task—now i'll move to the next"));
    }

    #[test]
    fn ellipsis_boundary_detected_as_interim() {
        assert!(has_interim_intent_clause("checking results…now i need to update the config"));
        assert!(is_interim_progress_update("Scanning the logs…now let me check for error patterns."));
    }

    #[test]
    fn long_text_with_progressive_request_continues_via_relaxed_path() {
        // User asked for progressive work ("fix"), model responds with long analysis
        // (> 800 chars, non-interim pattern), no tool activity.
        // Should continue via "progressive_relaxed" path.
        let long_analysis_base = "The root cause of this issue is a race condition in the connection \
            pool initialization. When multiple threads attempt to acquire connections \
            simultaneously, the pool's internal counter can overflow. This happens because \
            the increment operation is not atomic. The fix should wrap the counter update \
            in a mutex lock. Additionally, we should consider using atomic operations for \
            better performance. ";
        // Pad to exceed 800 chars
        let padding = "x".repeat(820usize.saturating_sub(long_analysis_base.len()));
        let long_text = format!(
            "{long_analysis_base}{padding} Let me now implement the mutex-based fix in the connection pool module."
        );
        assert!(long_text.len() > 800, "test text must exceed 800-char limit to trigger relaxed path");

        let history = vec![
            uni::Message::user("fix the race condition in connection pool".to_string()),
            uni::Message::assistant("I'll look into it.".to_string()),
        ];

        // Without tool activity, the text is > 800 chars → not interim → hits relaxed path
        // last_user_requested_progressive_work is true → should continue
        assert!(evaluate_interim_text_continuation(false, false, &history, &long_text, 0).should_continue);
    }

    #[test]
    fn long_text_with_tool_activity_and_not_conclusive_continues() {
        let long_analysis = "I've reviewed the output from the linter. There are several \
            warnings in the networking module. The main issues are unused imports and \
            a potential memory leak in the connection handler. The fixes are \
            straightforward: remove the unused imports and add proper cleanup in the \
            deinit method. Let me apply these changes to the affected files now. \
            Starting with the networking module, I'll remove the unused imports and \
            then fix the memory leak in the connection handler. After that, I'll \
            run the linter again to verify the warnings are resolved.";
        assert!(long_analysis.len() > 280, "test text must exceed original 280-char limit");

        let history =
            vec![
                uni::Message::user("run cargo clippy and fix warnings".to_string()),
                uni::Message::assistant("Running clippy now.".to_string()).with_tool_calls(vec![
                    uni::ToolCall::function("call_1".to_string(), "command_session".to_string(), "{}".to_string()),
                ]),
                uni::Message::tool_response("call_1".to_string(), "warning: ...".to_string()),
            ];

        assert!(evaluate_interim_text_continuation(true, false, &history, long_analysis, 0).should_continue);
    }

    #[test]
    fn short_completion_after_tool_activity_does_not_continue_via_relaxed_path() {
        let history = vec![
            uni::Message::user("create a simple rust hello world program".to_string()),
            uni::Message::assistant("Let me compile and run it to confirm it works:".to_string()).with_tool_calls(
                vec![uni::ToolCall::function(
                    "call_1".to_string(),
                    "command_session".to_string(),
                    "{}".to_string(),
                )],
            ),
            uni::Message::tool_response("call_1".to_string(), "Hello, World!".to_string()),
        ];

        assert!(
            !evaluate_interim_text_continuation(
                true,
                false,
                &history,
                "It works! The program compiled and ran successfully, printing `Hello, World!`.",
                0
            )
            .should_continue
        );
    }

    #[test]
    fn short_completion_after_progressive_request_does_not_continue_via_relaxed_path() {
        let history = vec![
            uni::Message::user("fix the parser regression".to_string()),
            uni::Message::assistant("I'll inspect the parser.".to_string()),
        ];

        assert!(
            !evaluate_interim_text_continuation(
                false,
                false,
                &history,
                "I updated the parser logic and the targeted regression test now passes.",
                0
            )
            .should_continue
        );
    }

    #[tokio::test]
    async fn tool_enabled_recovery_pass_can_continue_after_interim_progress() {
        let mut backing = TestTurnProcessingBacking::new(4).await;
        let mut ctx = backing.turn_processing_context();
        ctx.working_history
            .push(uni::Message::user("run cargo fmt and follow up".to_string()));
        ctx.activate_recovery_with_mode("empty response", RecoveryMode::ToolEnabledRetry);
        assert!(ctx.consume_recovery_pass());

        let outcome = ctx
            .handle_text_response(
                "Running formatter now, then I'll do a quick follow-up check (`cargo check`) to confirm nothing regressed."
                    .to_string(),
                Vec::new(),
                None,
                None,
                false,
            )
            .await
            .expect("tool-enabled recovery response should be handled");

        assert!(matches!(outcome, TurnHandlerOutcome::Continue));
        assert!(!ctx.is_recovery_active());
        assert!(ctx.recovery_pass_used());
    }

    #[tokio::test]
    async fn continuing_text_response_is_recorded_as_commentary_phase() {
        let mut backing = TestTurnProcessingBacking::new(4).await;
        let mut ctx = backing.turn_processing_context();
        ctx.working_history
            .push(uni::Message::user("fix the parser regression".to_string()));

        let outcome = ctx
            .handle_text_response(
                "Now I need to update the parser branch and rerun the targeted test.".to_string(),
                Vec::new(),
                None,
                None,
                false,
            )
            .await
            .expect("continuing text response should be handled");

        assert!(matches!(outcome, TurnHandlerOutcome::Continue));
        let last_assistant = ctx
            .working_history
            .iter()
            .rev()
            .find(|message| message.role == uni::MessageRole::Assistant)
            .expect("assistant message should be recorded");
        assert_eq!(last_assistant.phase, Some(uni::AssistantPhase::Commentary));
    }

    #[tokio::test]
    async fn completed_text_response_is_recorded_as_final_answer_phase() {
        let mut backing = TestTurnProcessingBacking::new(4).await;
        let mut ctx = backing.turn_processing_context();
        ctx.working_history
            .push(uni::Message::user("explain the core loop".to_string()));

        let outcome = ctx
            .handle_text_response(
                "The core loop requests model output, dispatches tool calls, and ends once a final textual answer is produced."
                    .to_string(),
                Vec::new(),
                None,
                None,
                false,
            )
            .await
            .expect("completed text response should be handled");

        assert!(matches!(outcome, TurnHandlerOutcome::Break(TurnLoopResult::Completed { .. })));
        let last_assistant = ctx
            .working_history
            .iter()
            .rev()
            .find(|message| message.role == uni::MessageRole::Assistant)
            .expect("assistant message should be recorded");
        assert_eq!(last_assistant.phase, Some(uni::AssistantPhase::FinalAnswer));
    }

    #[test]
    fn relaxed_continuation_does_not_trigger_for_user_directed_questions() {
        // This is the exact scenario from the infinite loop bug:
        // Agent tries to fetch a URL, both tools fail, agent explains blockers
        // and asks "How would you like to proceed?" - this should NOT trigger
        // the relaxed continuation path.
        let blocker_explanation = "I don't have a direct web-fetch tool available in this session. \
            My available tools are scoped to local file/code operations, and `exec_command` \
            requires explicit safety approval \
            for outbound network requests. A few options: 1. Approve the network call 2. Use a \
            subagent 3. Paste the content. How would you like to proceed? If you want me to fetch \
            it, please confirm and I'll retry with the appropriate sandbox permission.";
        let history = vec![
            uni::Message::user("can you fetch https://www.google.com.vn/".to_string()),
            uni::Message::assistant("I'll try to fetch it.".to_string()).with_tool_calls(vec![
                uni::ToolCall::function("call_1".to_string(), "exec_command".to_string(), "{}".to_string()),
            ]),
            uni::Message::tool_response("call_1".to_string(), "Tool preflight validation failed".to_string()),
        ];

        // Should NOT continue - the model is asking the user a question
        assert!(
            !evaluate_interim_text_continuation(true, false, &history, blocker_explanation, 0).should_continue,
            "User-directed questions should not trigger relaxed continuation"
        );
    }

    #[test]
    fn relaxed_continuation_does_not_trigger_for_first_turn_handoff_offer() {
        let repo_overview = checkpoint_shaped_repo_overview();
        let history = vec![
            uni::Message::user("what's in this repo?".to_string()),
            uni::Message::assistant(String::new()).with_tool_calls(vec![uni::ToolCall::function(
                "call_1".to_string(),
                "exec_command".to_string(),
                "{}".to_string(),
            )]),
            uni::Message::tool_response("call_1".to_string(), "README summary".to_string()),
        ];

        let decision = evaluate_interim_text_continuation(true, false, &history, repo_overview, 0);

        assert!(
            !decision.should_continue,
            "handoff offers after answering a first-turn question should end the turn"
        );
    }

    #[tokio::test]
    async fn first_turn_handoff_offer_completes_without_continue_directive() {
        let mut backing = TestTurnProcessingBacking::new(4).await;
        let mut ctx = backing.turn_processing_context();
        ctx.working_history.push(uni::Message::user("what's in this repo?".to_string()));
        ctx.working_history
            .push(uni::Message::assistant(String::new()).with_tool_calls(vec![uni::ToolCall::function(
                "call_1".to_string(),
                "file_operation".to_string(),
                "{}".to_string(),
            )]));
        ctx.working_history
            .push(uni::Message::tool_response("call_1".to_string(), "README summary".to_string()));

        let outcome = ctx
            .handle_text_response(checkpoint_shaped_repo_overview().to_string(), Vec::new(), None, None, false)
            .await
            .expect("repo overview response should be handled");

        assert!(matches!(outcome, TurnHandlerOutcome::Break(TurnLoopResult::Completed { .. })));
        let last_assistant = ctx
            .working_history
            .iter()
            .rev()
            .find(|message| message.role == uni::MessageRole::Assistant)
            .expect("assistant message should be recorded");
        assert_eq!(last_assistant.phase, Some(uni::AssistantPhase::FinalAnswer));
        assert!(!ctx.working_history.iter().any(|message| {
            message.role == uni::MessageRole::System
                && message.content.as_text().trim() == AUTONOMOUS_CONTINUE_DIRECTIVE
        }));
    }

    fn checkpoint_shaped_repo_overview() -> &'static str {
        "Here's a quick overview of the repo:\n\n\
        **VT Code** — a Rust coding agent for long-running autonomous workflows, with OS-native sandboxing, multi-provider LLM support, open protocols, and extensible Skills.\n\n\
        **Layout**\n\
        - Root crate `vtcode` (binary) + workspace of ~30 member crates under `vtcode-*` (e.g. `vtcode-core`, `vtcode-ui`, `vtcode-llm`, `vtcode-mcp`, `vtcode-safety`, `vtcode-exec-events`, `vtcode-indexer`, `vtcode-skills`, `vtcode-config`, `vtcode-a2a`, `vtcode-acp`, etc.)\n\
        - Rust stable, MSRV 1.98.1, edition 2024; CI runs `RUSTFLAGS=\"-D warnings\"` with `--locked`\n\
        - `default-members` = root, `vtcode-core`, `vtcode-ui`\n\n\
        **Capabilities**\n\
        - Agent runtime: interactive TUI, slash commands, streaming, `ask`/`exec` CLI, session resume\n\
        - Coding tools: safe file ops, ripgrep search, fuzzy discovery, code intelligence, project indexing, terminal execution\n\
        - Extensibility: Agent Skills, MCP client/server, lifecycle hooks, subagents, custom providers, Zed ACP, VS Code, Claude Code\n\
        - Model providers: 21+ LLMs (Anthropic, OpenAI, Gemini, OpenRouter, Ollama, LM Studio, etc.)\n\
        - Safety: restricted shell sandbox, tool guardrails, subprocess isolation, full audit logging\n\
        - Protocols: Open Responses, Agent2Agent (A2A), ATIF, Anthropic Messages API\n\n\
        **Default model**: MiMo V2.6 Pro (Xiaomi), 1M-token context (`mimo-v2.6-pro`).\n\n\
        **Other top-level dirs**: `docs/`, `plans/`, `rules/`, `examples/`, `tests/`, `evals/`, `fuzz/`, `scripts/`, `extensions/vscode-extension/`, `extensions/zed-extension/`, `crates/codegen/xtask/`, `homebrew/`.\n\n\
        Quick start:\n\
        ```shell\n\
        curl -fsSL https://raw.githubusercontent.com/vinhnx/vtcode/main/scripts/install.sh | bash\n\
        vtcode init\n\
        vtcode\n\
        ```\n\n\
        Let me know what you'd like to dig into next — a specific crate, the agent loop, the TUI, sandboxing, or something else."
    }

    #[test]
    fn relaxed_continuation_still_works_for_genuine_interim_progress() {
        // Legitimate interim progress should still trigger continuation
        let interim_text = "I've reviewed the linter output. There are several warnings. \
            Let me apply these changes to the affected files now. Starting with the networking \
            module, I'll remove the unused imports and then fix the memory leak.";
        let history =
            vec![
                uni::Message::user("run cargo clippy and fix warnings".to_string()),
                uni::Message::assistant("Running clippy now.".to_string()).with_tool_calls(vec![
                    uni::ToolCall::function("call_1".to_string(), "command_session".to_string(), "{}".to_string()),
                ]),
                uni::Message::tool_response("call_1".to_string(), "warning: ...".to_string()),
            ];

        // Should continue - this is genuine interim progress with no user question
        assert!(
            evaluate_interim_text_continuation(true, false, &history, interim_text, 0).should_continue,
            "Genuine interim progress should still trigger continuation"
        );
    }

    #[test]
    fn relaxed_continuation_stops_after_budget_exhausted() {
        // After MAX_CONSECUTIVE_RELAXED_CONTINuations, relaxed path should stop.
        // Use a long text (> 800 chars) to trigger the relaxed path (not interim progress).
        let long_analysis = "I've reviewed the output from the linter. There are several \
            warnings in the networking module. The main issues are unused imports and \
            a potential memory leak in the connection handler. The fixes are \
            straightforward: remove the unused imports and add proper cleanup in the \
            deinit method. Let me apply these changes to the affected files now. \
            Starting with the networking module, I'll remove the unused imports and \
            then fix the memory leak in the connection handler. After that, I'll \
            run the linter again to verify the warnings are resolved. The key changes \
            involve updating the connection pool initialization and adding proper \
            resource cleanup in the deinitialization path. I will also review the \
            authentication module for similar issues and ensure all error paths are \
            properly handled with appropriate cleanup routines to prevent resource leaks.";
        assert!(long_analysis.len() > 800, "test text must exceed 800-char limit to trigger relaxed path");
        let history =
            vec![
                uni::Message::user("run cargo clippy and fix warnings".to_string()),
                uni::Message::assistant("Running clippy now.".to_string()).with_tool_calls(vec![
                    uni::ToolCall::function("call_1".to_string(), "command_session".to_string(), "{}".to_string()),
                ]),
                uni::Message::tool_response("call_1".to_string(), "warning: ...".to_string()),
            ];

        // Should continue with budget = 0
        assert!(evaluate_interim_text_continuation(true, false, &history, long_analysis, 0).should_continue);

        // Should continue with budget = 1 (still under limit)
        assert!(evaluate_interim_text_continuation(true, false, &history, long_analysis, 1).should_continue);

        // Should continue with budget = 2 (still under limit)
        assert!(evaluate_interim_text_continuation(true, false, &history, long_analysis, 2).should_continue);

        // Should NOT continue with budget = 3 (at limit)
        assert!(
            !evaluate_interim_text_continuation(true, false, &history, long_analysis, 3).should_continue,
            "Relaxed continuation should stop after MAX_CONSECUTIVE_RELAXED_CONTINuations"
        );

        // Should NOT continue with budget = 4 (over limit)
        assert!(!evaluate_interim_text_continuation(true, false, &history, long_analysis, 4).should_continue);
    }

    #[test]
    fn contains_user_input_request_detects_various_patterns() {
        assert!(contains_user_input_request("how would you like to proceed?"));
        assert!(contains_user_input_request("what would you like me to do?"));
        assert!(contains_user_input_request("which option should i choose?"));
        assert!(contains_user_input_request("shall i continue?"));
        assert!(contains_user_input_request("do you want me to retry?"));
        assert!(contains_user_input_request("please confirm and i'll retry"));
        assert!(contains_user_input_request("let me know how to proceed"));
        assert!(contains_user_input_request("let me know what you'd like to dig into next"));
        assert!(contains_user_input_request("tell me which area you want next"));
        assert!(contains_user_input_request("waiting for your approval"));
        assert!(!contains_user_input_request(
            "The compiler errors tell me what to fix next. Let me update the parser branch now."
        ));
        assert!(!contains_user_input_request("Let me check whether this should initialize the cache before use."));
        assert!(!contains_user_input_request("let me fix the next issue"));
        assert!(!contains_user_input_request("i'll apply these changes now"));
    }

    #[test]
    fn contains_user_input_request_detects_closing_offers() {
        assert!(contains_user_input_request(
            "verified and done. if you want a deeper pass, say the word and i'll do a larger pass."
        ));
        assert!(contains_user_input_request("all checks pass; happy to iterate further."));
        assert!(contains_user_input_request("the docs are updated — just ask if you want the full diff."));
    }

    #[test]
    fn verified_recap_with_trailing_offer_does_not_continue() {
        // Regression guard for session-vtcode-20260912T083718Z: a conclusive
        // verification recap that closes by offering optional follow-up work
        // ("say the word and I'll do a larger pass") was relaxed-continued,
        // re-prompted into a second recap, and the turn ended Blocked on the
        // text cap after the verification gate had already cleared.
        let mut recap = String::from(
            "Verification passed: `cargo fmt --all -- --check && cargo check --locked -p vtcode` → exit 0.\n\n\
             **Recap**\n\n\
             **Changed (README.md):** tightened Overview prose, sharpened the pillar table, \
             streamlined Quick start phrasing, condensed the Documentation table, compacted the \
             Providers paragraph, and fixed a hyphen→em-dash in the TUI tip. Contributor and \
             sponsor HTML blocks preserved verbatim.\n\n\
             **Verified:** all referenced doc paths exist; the fmt and check commands exit 0.\n\n",
        );
        while recap.len() <= 800 {
            recap.push_str("Extra verified detail line that keeps the recap above the interim length cap.\n");
        }
        recap.push_str(
            "**Remaining risk:** none functional. If you want a deeper revamp (restructured \
             sections, feature highlights, refreshed screenshots), say the word and I'll do a \
             larger pass.",
        );
        assert!(recap.len() > 800, "recap must exceed the interim length cap to reach the relaxed path");

        let history = vec![
            uni::Message::user("polish the README intro".to_string()),
            uni::Message::assistant(String::new()).with_tool_calls(vec![uni::ToolCall::function(
                "call_1".to_string(),
                "exec_command".to_string(),
                "{}".to_string(),
            )]),
            uni::Message::tool_response("call_1".to_string(), "Finished `dev` profile".to_string()),
        ];

        assert!(
            !evaluate_interim_text_continuation(true, false, &history, &recap, 0).should_continue,
            "a conclusive recap closing with an optional-work offer must end the turn"
        );
    }

    #[test]
    fn tracker_incomplete_override_continues_status_recaps() {
        use super::apply_tracker_continuation_override;
        let history: Vec<uni::Message> = Vec::new();
        let recap = "## Status\nBlocked by turn budget. Next step on resume: read design/diff.rs.";
        let base = evaluate_interim_text_continuation(true, false, &history, recap, 0);
        assert!(!base.should_continue);
        let overridden = apply_tracker_continuation_override(base, true, false, recap);
        assert!(overridden.should_continue);
        assert_eq!(overridden.reason, "tracker_incomplete_continuation");
        assert!(!overridden.is_relaxed_continuation);

        let question = "Should I proceed with the remaining steps?";
        let q_base = evaluate_interim_text_continuation(true, false, &history, question, 0);
        let q_over = apply_tracker_continuation_override(q_base, true, false, question);
        assert!(!q_over.should_continue);

        let plan_over = apply_tracker_continuation_override(
            evaluate_interim_text_continuation(true, true, &history, recap, 0),
            true,
            true,
            recap,
        );
        assert!(!plan_over.should_continue);

        let complete_tracker = apply_tracker_continuation_override(base, false, false, recap);
        assert!(!complete_tracker.should_continue);

        // Permission/safety handoffs stay terminal for the user.
        for blocker in [
            "Permission denied for exec_command. Next step: retry after access is granted.",
            "Access denied to the sandbox; policy block.",
            "I hit the tool-call safety fuse mid-verification; policy block.",
            "Requires manual intervention from the workspace owner.",
            "Missing credentials for the provider route.",
        ] {
            let b_base = evaluate_interim_text_continuation(true, false, &history, blocker, 0);
            let b_over = apply_tracker_continuation_override(b_base, true, false, blocker);
            assert!(!b_over.should_continue, "must not continue on blocker: {blocker}");
        }

        // Budget-like "blocked by …" still continues when tracker work remains.
        let budget = "## Status\nBlocked by turn budget. Next step: read design/diff.rs.";
        let budget_over = apply_tracker_continuation_override(
            evaluate_interim_text_continuation(true, false, &history, budget, 0),
            true,
            false,
            budget,
        );
        assert!(budget_over.should_continue);
        assert_eq!(budget_over.reason, "tracker_incomplete_continuation");

        // Tool-loop / tool-budget recaps continue even with "blocked by".
        for recap in [
            "## Status\nBlocked by turn tool budget. Next step: patch FileChangeItem.",
            "## Status\nTool loop budget exhausted this turn. Next: run cargo check.",
            "Task 3 mid-flight — hit the per-file read cap before editing. Next: one targeted read then patch.",
        ] {
            let over = apply_tracker_continuation_override(
                evaluate_interim_text_continuation(true, false, &history, recap, 0),
                true,
                false,
                recap,
            );
            assert!(over.should_continue, "must continue on recoverable recap: {recap}");
        }

        // Mid-text `?` in a status section is not a user handoff when tracker incomplete.
        let status_question =
            "## Status\nTask 2 done — is the next step clear? Continuing with task 3 now using tools.";
        let s_over = apply_tracker_continuation_override(
            evaluate_interim_text_continuation(true, false, &history, status_question, 0),
            true,
            false,
            status_question,
        );
        assert!(s_over.should_continue, "mid-text question in status recap must continue");

        // Broad mid-text "can you" / "need your" in status prose is not a handoff.
        let status_prose =
            "## Status\nNext we need your workspace-relative path in CONFIG; can you see it in the dump? Patching now.";
        let prose_over = apply_tracker_continuation_override(
            evaluate_interim_text_continuation(true, false, &history, status_prose, 0),
            true,
            false,
            status_prose,
        );
        assert!(prose_over.should_continue, "status prose mentioning need-your/can-you must continue");

        // Clause-start interview ask without a trailing `?` still ends the turn.
        let clause_ask = "Can you confirm which migration path to take before I edit the schema.";
        let c_over = apply_tracker_continuation_override(
            evaluate_interim_text_continuation(true, false, &history, clause_ask, 0),
            true,
            false,
            clause_ask,
        );
        assert!(!c_over.should_continue);

        // Trailing `?` stays terminal.
        let trailing_q = "Should I proceed with the remaining steps?";
        let t_over = apply_tracker_continuation_override(
            evaluate_interim_text_continuation(true, false, &history, trailing_q, 0),
            true,
            false,
            trailing_q,
        );
        assert!(!t_over.should_continue);

        // Verification-pending recaps are true handoffs — outer verification
        // recovery owns that path; in-turn tracker continuation must not race
        // past the anti-blind gate, even when full-auto already continued.
        for recap in [
            "Turn blocked after repeated unverified assistant responses; verification is still pending.",
            "## Status\nBlocked by turn budget. Verification is still pending; run cargo check --locked.",
            "Anti-blind checkpoint: verification is still pending before further edits.",
        ] {
            let base = evaluate_interim_text_continuation(true, false, &history, recap, 0);
            let over = apply_tracker_continuation_override(base, true, false, recap);
            assert!(!over.should_continue, "must not continue past verification gate: {recap}");
        }

        // Session evidence: budget/status recaps without a gate still continue.
        // Full-auto may already continue some of these; the tracker override
        // must not reverse that. Assert `tracker_incomplete_continuation` only
        // when the base classifier would have stopped.
        for recap in [
            "## Status\nTask 7 blocked by the turn's preview budget before I could read docs.",
            "## Status\nTool budget ran out after evidence gathering; next step is the patch.",
            "Task 3 mid-flight — hit the per-file read cap before editing.",
            "Fix 1 partially applied, needs verification; continuing with the remaining checks now.",
        ] {
            let base = evaluate_interim_text_continuation(true, false, &history, recap, 0);
            let over = apply_tracker_continuation_override(base, true, false, recap);
            assert!(over.should_continue, "must continue on session budget recap: {recap}");
            if !base.should_continue {
                assert_eq!(over.reason, "tracker_incomplete_continuation");
            }
        }

        // When the base classifier would end the turn, tracker override forces
        // non-relaxed continuation with the tracker reason.
        let status_only =
            "## Status\nBlocked by the turn's preview budget. Next step on resume: read docs/development/README.md.";
        let s_base = evaluate_interim_text_continuation(false, false, &history, status_only, 0);
        assert!(!s_base.should_continue, "interactive status recap must not auto-continue without tracker");
        let s_over = apply_tracker_continuation_override(s_base, true, false, status_only);
        assert!(s_over.should_continue);
        assert_eq!(s_over.reason, "tracker_incomplete_continuation");
        assert!(!s_over.is_relaxed_continuation);
    }

    #[test]
    fn recoverable_status_recap_vocabulary_matches_outer_classifier() {
        use vtcode_core::core::agent::completion::recoverable_status_recap_phrasing;
        for phrase in [
            "blocked by the turn's preview budget",
            "tool budget ran out",
            "per-file read cap",
            "preview budget exhausted",
            "tool loop budget exhausted",
            "recovery fallback",
            "reached the safety cap",
        ] {
            let lower = phrase.to_ascii_lowercase();
            assert!(recoverable_status_recap_phrasing(&lower), "shared vocab must cover {phrase}");
            assert!(
                crate::agent::runloop::unified::turn::tool_outcomes::helpers::tracker_auto_continue_is_recoverable_block(
                    Some(phrase)
                ),
                "outer classifier must cover {phrase}"
            );
        }
        for phrase in [
            "Turn blocked after repeated unverified assistant responses; verification is still pending.",
            "exec_command is denied by permission policy",
            "request_user_input is required",
        ] {
            assert!(
                !crate::agent::runloop::unified::turn::tool_outcomes::helpers::tracker_auto_continue_is_recoverable_block(
                    Some(phrase)
                ),
                "outer classifier must deny {phrase}"
            );
        }
    }
}