netget-tool 0.3.5

Network protocol stubbing toolkit: ~280 protocol servers and clients whose responses come from an LLM, a script, a static rule or a person.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
//! Conversation-based LLM interaction with tool calling and retry logic
//!
//! This module provides a unified conversation handler that:
//! 1. Maintains conversation history (system, user, assistant messages)
//! 2. Handles multi-turn tool calling automatically
//! 3. Retries malformed responses with corrective feedback
//! 4. Works for both user input and network events

use crate::llm::actions::{execute_tool, ActionDefinition, ActionResponse, ToolAction, ToolResult};
use crate::llm::conversation_state::ConversationState;
use crate::llm::ollama_client::{ChatRequest, ChatResponse, Message, OllamaClient};
use crate::llm::{RateLimiter, RequestSource};
use crate::logging::emit::Log;
use crate::state::app_state::{AppState, ConversationSource, WebApprovalRequest, WebSearchMode};
use anyhow::{Context, Result};
use std::sync::{Arc, Mutex};
use tracing::{debug, error, info, trace, warn};

/// Extract reasoning from LLM response and return (reasoning, cleaned_response)
///
/// Looks for `<reasoning>...</reasoning>` tags in the response, extracts the content,
/// and returns a cleaned response with the tags removed.
///
/// # Arguments
/// * `response` - The raw LLM response text
///
/// # Returns
/// * `(Option<String>, String)` - Reasoning content (if found) and cleaned response
/// Whether any value in this action still points at a tool result rather than
/// carrying one.
///
/// Models write these unprompted — `"block_hex": "{{tools[0].result}}"`,
/// `"token": "{{tool_results[1]}}"` — reasoning that the tool call they issued in
/// the same response will be substituted in. Nothing substitutes it:
/// `reference_parser` resolves `<tagname>` XML references, and there is no
/// `{{...}}` mechanism on the LLM path at all. The placeholder would reach the
/// wire as literal text.
///
/// Deliberately narrow. It matches only `{{…}}` whose body mentions a tool, so
/// `{{event.field}}` — which *is* substituted, later and elsewhere, inside static
/// event-handler definitions — is left alone.
fn action_references_tool_result(action: &serde_json::Value) -> bool {
    fn mentions_tool_placeholder(text: &str) -> bool {
        let mut rest = text;
        while let Some(open) = rest.find("{{") {
            let after = &rest[open + 2..];
            let Some(close) = after.find("}}") else { break };
            if after[..close].to_lowercase().contains("tool") {
                return true;
            }
            rest = &after[close + 2..];
        }
        false
    }

    match action {
        serde_json::Value::String(s) => mentions_tool_placeholder(s),
        serde_json::Value::Array(items) => items.iter().any(action_references_tool_result),
        serde_json::Value::Object(map) => map.values().any(action_references_tool_result),
        _ => false,
    }
}

fn extract_reasoning(response: &str) -> (Option<String>, String) {
    let reasoning_start = response.find("<reasoning>");
    let reasoning_end = response.find("</reasoning>");

    match (reasoning_start, reasoning_end) {
        (Some(start), Some(end)) if end > start => {
            // Extract reasoning content (between tags)
            let reasoning_content = response[start + 11..end].trim().to_string();

            // Remove the entire reasoning tag (including tags themselves)
            let before = &response[..start];
            let after = &response[end + 12..];
            let cleaned = format!("{}{}", before, after).trim().to_string();

            (Some(reasoning_content), cleaned)
        }
        _ => (None, response.to_string()),
    }
}

/// Default cap on the number of messages kept in the in-flight conversation.
///
/// A legitimate run is bounded by `max_tool_iterations` (5), each iteration contributing at
/// most an assistant response, an action acknowledgement and a tool-results message — 15,
/// plus the system message and the initial trigger. 24 leaves room for a couple of
/// corrections on top of a full-length legitimate run and clips only pathological ones.
pub const DEFAULT_MAX_HISTORY_MESSAGES: usize = 24;

/// Default cap on the total characters of the non-system messages sent to the model.
///
/// Roughly 8k tokens. `format_tool_results` already caps each tool result at 2000 chars, so
/// this holds a full 5-iteration run's worth of tool output plus the surrounding turns. It
/// is deliberately larger than `ConversationState`'s 8000-char cross-*call* window, which
/// bounds a different structure and never bounded what is actually sent.
pub const DEFAULT_MAX_HISTORY_CHARS: usize = 32_000;

/// Stand-in message left in place of history dropped by [`ConversationHandler::trim_history`].
const TRIM_NOTICE: &str = "[Earlier turns of this conversation were omitted to bound the prompt size. The system message and the original request above are intact.]";

/// Conversation handler for multi-turn LLM interactions
pub struct ConversationHandler {
    /// Unique conversation ID for tracking
    conversation_id: String,

    /// Conversation messages (system, user, assistant, tool)
    messages: Vec<Message>,

    /// Conversation state for history tracking with token limits
    conversation_state: Arc<Mutex<ConversationState>>,

    /// Ollama client for chat API calls
    client: Arc<OllamaClient>,

    /// Model name (e.g., "qwen3-coder:30b")
    model: String,

    /// Maximum number of retries for malformed responses
    max_retries: usize,

    /// Maximum tool calling iterations
    max_tool_iterations: usize,

    /// Status channel for user-visible logs
    status_tx: Option<tokio::sync::mpsc::UnboundedSender<String>>,

    /// Index of last logged message (to avoid re-logging entire conversation)
    last_logged_index: usize,

    /// Whether protocol documentation has been read in this conversation (enables open_server and open_client)
    protocol_docs_read: bool,

    /// Whether server documentation has been read in this conversation (enables open_server)
    _server_docs_read: bool,

    /// Whether client documentation has been read in this conversation (enables open_client)
    _client_docs_read: bool,

    /// Application state (for conversation tracking)
    state: Option<AppState>,

    /// Source of this conversation (for UI display)
    source: Option<ConversationSource>,

    /// Details text for UI display
    details: Option<String>,

    /// Whether this conversation has been registered (to avoid duplicate registration)
    registered: bool,

    /// Rate limiter for controlling LLM call frequency and token usage
    rate_limiter: RateLimiter,

    /// Source of the request (User or Network) for rate limiting behavior
    request_source: RequestSource,

    /// Native tool schemas for chat_with_tools() API (empty = use prompt-based fallback)
    tool_schemas: Vec<serde_json::Value>,

    /// Whether to use native tool calling (chat_with_tools) vs prompt-based (generate_with_format)
    use_native_tools: bool,

    /// The actions the current `generate_with_tools_and_retry` call offers. Handed to the
    /// client with every request so a bridge host can show them as structured data.
    offered_actions: Vec<ActionDefinition>,

    /// Structured metadata for a single network event, retained across all retries.
    bridge_event: Option<crate::llm::bridge::BridgeEventContext>,

    /// Conversation-local policy retained once credential parameters are offered.
    private_payloads: bool,

    /// Cap on `messages.len()` — see [`DEFAULT_MAX_HISTORY_MESSAGES`]
    max_history_messages: usize,

    /// Cap on the total characters of the non-system messages — see [`DEFAULT_MAX_HISTORY_CHARS`]
    max_history_chars: usize,

    /// Bumped every time [`Self::trim_history`] actually removes messages.
    ///
    /// Trimming shifts every index after the protected prefix, so code holding a message
    /// index across a possible trim compares this first and gives up on a mismatch rather
    /// than draining the wrong range.
    trim_generation: u64,
}

impl ConversationHandler {
    /// Create a new conversation handler with a system message
    pub fn new(
        system_message: String,
        client: Arc<OllamaClient>,
        model: String,
        rate_limiter: RateLimiter,
        request_source: RequestSource,
    ) -> Self {
        let messages = vec![Message::system(system_message)];

        // Generate unique conversation ID using timestamp and random bytes
        let conversation_id = Self::generate_conversation_id();

        // Create conversation state with default token limit (8000 characters)
        // This can be made configurable later
        let conversation_state = Arc::new(Mutex::new(ConversationState::new(8000)));

        Self {
            conversation_id,
            messages,
            conversation_state,
            client,
            model,
            max_retries: 1,
            max_tool_iterations: 5,
            status_tx: None,
            last_logged_index: 0, // No messages logged yet
            protocol_docs_read: false,
            _server_docs_read: false,
            _client_docs_read: false,
            state: None,
            source: None,
            details: None,
            registered: false,
            rate_limiter,
            request_source,
            tool_schemas: Vec::new(),
            use_native_tools: false,
            offered_actions: Vec::new(),
            bridge_event: None,
            private_payloads: false,
            max_history_messages: DEFAULT_MAX_HISTORY_MESSAGES,
            max_history_chars: DEFAULT_MAX_HISTORY_CHARS,
            trim_generation: 0,
        }
    }

    /// Attach the event for a bridge host without making it recover context from prompt text.
    pub fn with_bridge_event(
        mut self,
        server_id: crate::state::ServerId,
        connection_id: Option<crate::server::connection::ConnectionId>,
        protocol: &str,
        event: &crate::protocol::Event,
    ) -> Self {
        // Native transports already render event data into the prompt and never consume
        // this metadata. Avoid another potentially large clone for every native request.
        if self.client.backend_type() != "bridge" {
            return self;
        }
        self.bridge_event = Some(crate::llm::bridge::BridgeEventContext {
            token: self.conversation_id.clone(),
            server_id: server_id.as_u32(),
            connection_id: connection_id.map(|id| id.as_u32()),
            protocol: protocol.to_string(),
            event_type: event.id().to_string(),
            data: event.data.clone(),
        });
        self
    }

    /// Override the conversation-history caps (see [`DEFAULT_MAX_HISTORY_MESSAGES`] and
    /// [`DEFAULT_MAX_HISTORY_CHARS`]).
    pub fn with_history_limits(mut self, max_messages: usize, max_chars: usize) -> Self {
        self.max_history_messages = max_messages.max(3);
        self.max_history_chars = max_chars;
        self
    }

    /// Dual-sink log facade bound to this conversation's status channel.
    ///
    /// The conversation layer owns the *semantic* narration of a round-trip — which
    /// attempt, that a response arrived, the outcome — and is the only layer that
    /// narrates it to the TUI. Wire facts (sizes, tokens) belong to the transport
    /// (`OllamaClient`) and stay file-only. Full payloads are TRACE/file-only.
    fn log(&self) -> Log<'_> {
        Log::new(self.status_tx.as_ref()).with_payloads(!self.private_payloads)
    }

    fn display_payload(&self, text: &str) -> String {
        if self.private_payloads {
            crate::utils::redact::REDACTED.into()
        } else {
            text.into()
        }
    }

    /// Total characters of everything but the system message.
    fn history_chars(&self) -> usize {
        self.messages.iter().skip(1).map(|m| m.content.len()).sum()
    }

    /// Whether the conversation exceeds either history cap.
    fn over_history_budget(&self) -> bool {
        self.messages.len() > self.max_history_messages
            || self.history_chars() > self.max_history_chars
    }

    /// Bound the conversation that is actually sent to the model.
    ///
    /// Without this, `messages` only ever grows: five tool iterations each append an
    /// assistant response, an acknowledgement and a tool-results block, and every failed
    /// attempt leaves its response plus a correction behind for the rest of the
    /// conversation. `ConversationState`'s 8000-char window is a different structure and
    /// bounds only cross-call history, never the request.
    ///
    /// The system message and the first user message — the instruction and the request the
    /// whole conversation is about — are never dropped, nor are the two most recent
    /// messages. Everything between is dropped oldest-first and replaced by a single
    /// [`TRIM_NOTICE`], so the model is told its history was cut rather than left to
    /// reference turns that silently vanished.
    ///
    /// Called before every request; public so the bound is directly assertable.
    pub fn trim_history(&mut self) {
        if !self.over_history_budget() {
            return;
        }

        let protected = if self.messages.len() > 1 && self.messages[1].role == "user" {
            2
        } else {
            1
        };
        // Keep at least the two most recent messages after the protected prefix.
        let min_len = protected + 2;

        let mut dropped_messages = 0usize;
        let mut dropped_chars = 0usize;
        let mut removed_any = false;
        while self.messages.len() > min_len && self.over_history_budget() {
            let removed = self.messages.remove(protected);
            removed_any = true;
            // A notice left by an earlier trim is replaced, not counted as lost content.
            if removed.content != TRIM_NOTICE {
                dropped_messages += 1;
                dropped_chars += removed.content.len();
            }
        }

        if !removed_any {
            return;
        }

        // Any removal shifts every index after the protected prefix.
        self.trim_generation += 1;
        self.messages
            .insert(protected, Message::user(TRIM_NOTICE.to_string()));
        // The notice itself counts against the message cap.
        if self.messages.len() > self.max_history_messages && self.messages.len() > min_len + 1 {
            let removed = self.messages.remove(protected + 1);
            dropped_messages += 1;
            dropped_chars += removed.content.len();
        }

        // Trimming is a DEBUG summary — file-only, off the TUI stream.
        self.log().debug(format!(
            "Trimmed conversation history: dropped {} message(s) / {} chars, {} message(s) and \
             {} chars remain (caps: {} messages, {} chars)",
            dropped_messages,
            dropped_chars,
            self.messages.len(),
            self.history_chars(),
            self.max_history_messages,
            self.max_history_chars
        ));
    }

    /// Generate a unique conversation ID
    fn generate_conversation_id() -> String {
        use crate::utils::clock::SystemTime;
        let timestamp = SystemTime::now()
            .duration_since(SystemTime::UNIX_EPOCH)
            .unwrap()
            .as_millis();
        let random: u32 = rand::random();
        format!("conv-{}-{:x}", timestamp, random)
    }

    /// Hide incidental payload/error diagnostics when structured request input
    /// contains credentials. Sticky only within this conversation's history.
    pub fn with_private_payloads(mut self, private: bool) -> Self {
        self.private_payloads |= private;
        self
    }

    /// Set the status channel for user-visible logs
    pub fn with_status_tx(mut self, tx: tokio::sync::mpsc::UnboundedSender<String>) -> Self {
        self.status_tx = Some(tx);
        self
    }

    /// Set an existing conversation state
    pub fn with_conversation_state(mut self, state: Arc<Mutex<ConversationState>>) -> Self {
        self.conversation_state = state;
        self
    }

    /// Set conversation tracking information
    pub fn with_tracking(
        mut self,
        state: AppState,
        source: ConversationSource,
        details: String,
    ) -> Self {
        self.state = Some(state);
        self.source = Some(source);
        self.details = Some(details);
        self
    }

    /// Enable native tool calling with the given action definitions
    ///
    /// When set, `generate_with_retry()` will use `chat_with_tools()` instead of
    /// `generate_with_format()`, sending structured messages and tool schemas.
    /// The response is converted back to ActionResponse text format so the existing
    /// tool calling loop in `generate_with_tools_and_retry()` works unchanged.
    ///
    /// Also strips redundant action/tool descriptions and JSON format instructions from
    /// the system message to reduce token usage (these are now handled by native tool schemas).
    pub fn with_native_tools(mut self, actions: &[ActionDefinition]) -> Self {
        self.tool_schemas = actions.iter().map(|a| a.to_tool_schema()).collect();
        self.use_native_tools = !self.tool_schemas.is_empty();

        // Strip redundant sections from system message when native tools are active
        if self.use_native_tools && !self.messages.is_empty() && self.messages[0].role == "system" {
            let content = &self.messages[0].content;
            let mut cleaned = content.clone();

            // Remove "# Available Tools" section (text descriptions of tools)
            if let Some(start) = cleaned.find("# Available Tools") {
                if let Some(next_section) = cleaned[start + 1..].find("\n# ") {
                    cleaned = format!(
                        "{}{}",
                        &cleaned[..start],
                        &cleaned[start + 1 + next_section..]
                    );
                }
            }

            // Remove "# Available Actions" section (text descriptions of actions)
            if let Some(start) = cleaned.find("# Available Actions") {
                if let Some(next_section) = cleaned[start + 1..].find("\n# ") {
                    cleaned = format!(
                        "{}{}",
                        &cleaned[..start],
                        &cleaned[start + 1 + next_section..]
                    );
                }
            }

            // Remove "# Response Format" section (JSON format instructions)
            if let Some(start) = cleaned.find("# Response Format") {
                if let Some(next_section) = cleaned[start + 1..].find("\n# ") {
                    cleaned = format!(
                        "{}{}",
                        &cleaned[..start],
                        &cleaned[start + 1 + next_section..]
                    );
                }
            }

            // Remove the "CRITICAL - READ THIS FIRST" JSON format block from task.hbs
            if let Some(start) = cleaned.find("**⚠️  CRITICAL - READ THIS FIRST ⚠️**") {
                // Find the end of this block (next "---" or "## ")
                if let Some(end_marker) = cleaned[start..].find("\n---\n") {
                    let end = start + end_marker + 5; // include the "---\n"
                    cleaned = format!("{}{}", &cleaned[..start], &cleaned[end..]);
                }
            }

            if cleaned.len() < content.len() {
                debug!(
                    "Stripped {} chars of redundant action/format descriptions from system message (native tools active)",
                    content.len() - cleaned.len()
                );
                self.messages[0] = Message::system(cleaned);
            }
        }

        self
    }

    /// Update the native tool schemas (e.g., after documentation is read and new tools are enabled)
    pub fn update_tool_schemas(&mut self, actions: &[ActionDefinition]) {
        self.tool_schemas = actions.iter().map(|a| a.to_tool_schema()).collect();
        self.use_native_tools = !self.tool_schemas.is_empty();
    }

    /// Convert a ChatResponse (native tool calls) into ActionResponse-compatible text
    ///
    /// This bridges native tool calling with the existing text-based parsing pipeline.
    /// Each tool_call is converted to a JSON object with "type" field matching the function name,
    /// then separated into tools vs actions arrays.
    fn chat_response_to_action_text(response: &ChatResponse) -> String {
        let mut tools = Vec::new();
        let mut actions = Vec::new();

        for tc in &response.tool_calls {
            // Build action JSON: merge function_name as "type" with arguments
            let obj = if let Some(map) = tc.arguments.as_object() {
                let mut new_map = serde_json::Map::new();
                new_map.insert(
                    "type".to_string(),
                    serde_json::Value::String(tc.function_name.clone()),
                );
                for (k, v) in map {
                    new_map.insert(k.clone(), v.clone());
                }
                serde_json::Value::Object(new_map)
            } else {
                serde_json::json!({"type": tc.function_name})
            };

            // Separate into tools vs actions using existing classification
            if ToolAction::is_tool_action(&obj) {
                tools.push(obj);
            } else {
                actions.push(obj);
            }
        }

        // If there's text content and no tool calls, convert to show_message
        if let Some(content) = &response.content {
            if !content.is_empty() && tools.is_empty() && actions.is_empty() {
                actions.push(serde_json::json!({"type": "show_message", "message": content}));
            }
        }

        serde_json::to_string(&serde_json::json!({
            "tools": tools,
            "actions": actions
        }))
        .unwrap_or_default()
    }

    /// Manually end conversation tracking (for error paths where generate_with_tools_and_retry doesn't complete)
    pub async fn end_tracking(&self) {
        if let Some(state) = &self.state {
            state.end_conversation(&self.conversation_id).await;
        }
    }

    /// Check if protocol documentation has been read in this conversation
    pub fn is_protocol_docs_read(&self) -> bool {
        self.protocol_docs_read
    }

    /// Mark protocol documentation as read in this conversation (enables open_server and open_client)
    /// This also updates the system message to enable the open_server and open_client actions
    fn mark_protocol_docs_read(&mut self, available_actions: &[ActionDefinition]) {
        self.protocol_docs_read = true;
        debug!("Protocol documentation read in conversation - open_server and open_client actions are now enabled");

        // Enable open_server and open_client in the available actions
        // by filtering out the disabled versions and regenerating them with enabled flag
        let mut enabled_actions = Vec::new();

        for action in available_actions {
            // Skip the disabled open_server and open_client actions - we'll add enabled versions
            if action.name == "open_server" && action.parameters.is_empty() {
                // This is the disabled version - skip it
                continue;
            }
            if action.name == "open_client" && action.parameters.is_empty() {
                // This is the disabled version - skip it
                continue;
            }
            enabled_actions.push(action.clone());
        }

        // We need to regenerate the enabled actions, but since we don't have access to the
        // state/env here, we'll add placeholder descriptions in the actions.
        // The real solution would be to pass state/env to this function, but that's more invasive.
        // For now, let's just update the existing disabled ones to be enabled by replacing them
        // with newly built ones that have full parameters.

        // Since we can't easily regenerate here without state/env, let's just pass the
        // existing actions. The update_actions_section function will render them as-is.
        // The actions will remain disabled in this iteration, but the next iteration will
        // have them enabled because the conversation will have a new set of actions built
        // with the enabled flags.

        // Rebuild the actions section in the system prompt
        self.update_actions_section(available_actions);
    }

    /// Mark server documentation as read in this conversation (enables open_server)
    #[allow(dead_code)]
    fn _mark_server_docs_read(&mut self, available_actions: &[ActionDefinition]) {
        self._server_docs_read = true;
        debug!("Server documentation read in conversation - open_server action is now enabled");

        // Rebuild the actions section in the system prompt
        self.update_actions_section(available_actions);
    }

    /// Mark client documentation as read in this conversation (enables open_client)
    #[allow(dead_code)]
    fn _mark_client_docs_read(&mut self, available_actions: &[ActionDefinition]) {
        self._client_docs_read = true;
        debug!("Client documentation read in conversation - open_client action is now enabled");

        // Rebuild the actions section in the system prompt
        self.update_actions_section(available_actions);
    }

    /// Update the "Available Actions" section in the system message
    ///
    /// This is used after read_base_stack_docs is called to enable the open_server action.
    fn update_actions_section(&mut self, available_actions: &[ActionDefinition]) {
        use crate::llm::prompt::PromptBuilder;

        if self.messages.is_empty() {
            warn!("Cannot update actions section: no system message found");
            return;
        }

        // Get the system message (first message)
        let system_msg = &self.messages[0];
        if system_msg.role != "system" {
            warn!("First message is not a system message, cannot update actions section");
            return;
        }

        let old_content = &system_msg.content;

        // Find the "# Available Tools" or "# Available Actions" section
        let section_start = if let Some(pos) = old_content.find("# Available Tools") {
            Some(pos)
        } else {
            old_content.find("# Available Actions")
        };

        if let Some(start_pos) = section_start {
            // Find where this section ends (next "# " or "---" or end of string)
            let content_after_section = &old_content[start_pos..];

            // Find the next major section marker
            let mut end_pos = start_pos;
            let mut found_end = false;

            // Skip past the section header
            if let Some(first_newline) = content_after_section.find('\n') {
                let search_start = start_pos + first_newline + 1;
                let remaining = &old_content[search_start..];

                // Look for next section (starts with "# " at line start or "---")
                if let Some(next_section) = remaining.find("\n# ") {
                    end_pos = search_start + next_section;
                    found_end = true;
                } else if let Some(divider) = remaining.find("\n---") {
                    end_pos = search_start + divider;
                    found_end = true;
                }
            }

            if !found_end {
                end_pos = old_content.len();
            }

            // Build new actions section using PromptBuilder
            let new_actions_section =
                PromptBuilder::build_actions_section_public(available_actions);

            // Replace the old actions section with the new one
            let mut new_content = String::new();
            new_content.push_str(&old_content[..start_pos]);
            new_content.push_str(&new_actions_section);
            if end_pos < old_content.len() {
                new_content.push_str(&old_content[end_pos..]);
            }

            // Update the system message
            self.messages[0] = Message::system(new_content);

            debug!("Updated Available Actions section in system message with open_server enabled");
        } else {
            warn!("Could not find '# Available Tools' or '# Available Actions' section in system message");
        }
    }

    /// Add a user message to the conversation
    pub fn add_user_message(&mut self, content: String) {
        // Track in conversation state
        if let Ok(mut state) = self.conversation_state.lock() {
            state.add_user_input(content.clone());
        }

        self.messages.push(Message::user(content));
    }

    /// Generate response with tool calling and retry logic
    ///
    /// This is the main entry point that handles:
    /// 1. Multi-turn tool calling loop
    /// 2. Automatic retry on parse/validation errors
    /// 3. Tool execution with result feedback
    ///
    /// # Arguments
    /// * `approval_tx` - Optional channel for web search approval
    /// * `web_search_mode` - Web search configuration
    /// * `available_actions` - List of actions available in this context
    ///
    /// # Returns
    /// * `Ok(Vec<serde_json::Value>)` - Array of non-tool actions to execute
    pub async fn generate_with_tools_and_retry(
        &mut self,
        approval_tx: Option<tokio::sync::mpsc::UnboundedSender<WebApprovalRequest>>,
        web_search_mode: WebSearchMode,
        available_actions: Vec<ActionDefinition>,
    ) -> Result<Vec<serde_json::Value>> {
        self.private_payloads |= crate::utils::redact::actions_have_credentials(&available_actions);
        // This handle belongs to this generation, including its tool/retry loop.
        // Dropping the future (e.g. stopping a server) also clears its activity.
        let _activity = self
            .state
            .as_ref()
            .zip(self.source.as_ref())
            .map(|(state, source)| {
                state.llm_activity.begin(
                    source.clone(),
                    self.display_payload(self.details.as_deref().unwrap_or_default()),
                )
            });
        let result = self
            .generate_until_final_response(approval_tx, web_search_mode, available_actions)
            .await;
        if result.is_err() {
            // Rejected/exhausted responses must not leave a live conversation in the UI.
            self.end_tracking().await;
        }
        result
    }

    async fn generate_until_final_response(
        &mut self,
        approval_tx: Option<tokio::sync::mpsc::UnboundedSender<WebApprovalRequest>>,
        web_search_mode: WebSearchMode,
        available_actions: Vec<ActionDefinition>,
    ) -> Result<Vec<serde_json::Value>> {
        // Register conversation if tracking is enabled and not already registered
        if !self.registered {
            if let (Some(state), Some(source), Some(details)) =
                (&self.state, &self.source, &self.details)
            {
                state
                    .register_conversation(
                        self.conversation_id.clone(),
                        source.clone(),
                        details.clone(),
                    )
                    .await;
                self.registered = true;
            }
        }

        let mut final_actions = None;
        let mut tool_results = Vec::new();
        // Index of the oldest rejected assistant response not yet superseded by a valid one.
        // Everything from there up to the current response is dropped once the model gets it
        // right, so a correction round-trip does not stay in the prompt for the rest of the
        // conversation.
        let mut rejected_block_start: Option<(usize, u64)> = None;
        let mut consecutive_tool_failures = 0;
        let mut unknown_action_retries = 0;
        let mut malformed_action_retries = 0;
        const MAX_CONSECUTIVE_FAILURES: usize = 2;
        const MAX_UNKNOWN_ACTION_RETRIES: usize = 2;
        const MAX_MALFORMED_ACTION_RETRIES: usize = 2;

        // Build set of valid action names for validation (mutable to allow updates after docs read)
        let mut valid_action_names: std::collections::HashSet<String> =
            available_actions.iter().map(|a| a.name.clone()).collect();
        let mut valid_action_names_list: Vec<String> =
            available_actions.iter().map(|a| a.name.clone()).collect();
        // History can retain a credential after the currently offered actions are narrowed.
        self.private_payloads |= crate::utils::redact::actions_have_credentials(&available_actions);
        self.offered_actions = available_actions.clone();

        for iteration in 1..=self.max_tool_iterations {
            debug!(
                "Conversation iteration {}/{}",
                iteration, self.max_tool_iterations
            );

            // Generate response from LLM
            let (original_response, cleaned_response) = self
                .generate_with_retry()
                .await
                .context("✗  LLM failed to generate valid response after retries.")?;

            // Add assistant's response to conversation history (with reasoning preserved)
            let assistant_idx = self.messages.len();
            self.messages
                .push(Message::assistant(original_response.clone()));

            // Extract XML references from response (scripts, configs, large content)
            let (json_only, references) =
                crate::llm::reference_parser::extract_references(&cleaned_response)
                    .context("Failed to extract XML references from response")?;

            if !references.is_empty() && !self.private_payloads {
                debug!(
                    "Extracted {} XML references from LLM response",
                    references.len()
                );
                for (tag_name, content) in &references {
                    trace!("  Reference <{}>: {} chars", tag_name, content.len());
                }
            }

            // Parse as action response (using JSON-only portion)
            let action_response = ActionResponse::from_str(&json_only)
                .context("Failed to parse action response (should not happen after retry)")?;

            // Resolve references in both tools and actions (replace <tagname> placeholders with actual content)
            let resolve_refs = |item: serde_json::Value| {
                let mut resolved_item = item;
                // Convert to JSON string, resolve references, parse back
                if let Ok(item_json) = serde_json::to_string(&resolved_item) {
                    if crate::llm::reference_parser::contains_references(&item_json) {
                        let resolved_json = crate::llm::reference_parser::resolve_references(
                            &item_json,
                            &references,
                        );
                        if let Ok(new_item) = serde_json::from_str(&resolved_json) {
                            resolved_item = new_item;
                        }
                    }
                }
                resolved_item
            };

            let tools_with_refs: Vec<_> = action_response
                .tools
                .into_iter()
                .map(&resolve_refs)
                .collect();

            let actions_with_refs: Vec<_> = action_response
                .actions
                .into_iter()
                .map(&resolve_refs)
                .collect();

            // Create new action response with resolved references
            let action_response = ActionResponse {
                tools: tools_with_refs,
                actions: actions_with_refs,
            };

            // Validate action names against available actions
            let unknown_actions: Vec<String> = action_response
                .actions
                .iter()
                .chain(action_response.tools.iter())
                .filter_map(|action| {
                    let Some(action_type) = action.get("type").and_then(|v| v.as_str()) else {
                        return Some("(missing or non-string action type)".to_string());
                    };
                    // Check if action exists in valid actions
                    if !valid_action_names.contains(action_type) {
                        Some(action_type.to_string())
                    } else {
                        None
                    }
                })
                .collect();

            if !unknown_actions.is_empty() {
                unknown_action_retries += 1;
                let shown_unknown_actions = if self.private_payloads {
                    vec![crate::utils::redact::REDACTED.to_string(); unknown_actions.len()]
                } else {
                    unknown_actions.clone()
                };

                // Log warning with the actual response
                warn!(
                    "LLM returned unknown action(s): {:?}. Response was: {}",
                    shown_unknown_actions,
                    self.display_payload(&crate::utils::truncate_for_log(&cleaned_response, 500))
                );

                if let Some(ref tx) = self.status_tx {
                    let _ = tx.send(format!(
                        "[WARN] LLM returned unknown action(s): {:?}",
                        shown_unknown_actions
                    ));
                }

                if unknown_action_retries >= MAX_UNKNOWN_ACTION_RETRIES {
                    // All retries exhausted - log error
                    error!(
                        "LLM failed to use valid actions after {} retries. Unknown actions: {:?}",
                        unknown_action_retries, shown_unknown_actions
                    );
                    if let Some(ref tx) = self.status_tx {
                        let _ = tx.send(format!(
                            "[ERROR] LLM failed to use valid actions after {} retries",
                            unknown_action_retries
                        ));
                    }
                    // Return error instead of silently continuing
                    anyhow::bail!(
                        "LLM returned unknown action(s) after {} retries: {:?}",
                        unknown_action_retries,
                        shown_unknown_actions
                    );
                }

                // Build retry prompt for unknown actions
                let correction =
                    crate::llm::prompt::PromptBuilder::build_unknown_action_retry_prompt(
                        &unknown_actions,
                        &valid_action_names_list,
                    );

                // Log the correction before adding to messages
                trace!(
                    "→ Sending unknown action correction and retrying (attempt {})...",
                    unknown_action_retries + 1
                );
                if let Some(ref tx) = self.status_tx {
                    let _ = tx.send(
                        "[TRACE] → Sending correction to LLM for unknown action...".to_string(),
                    );
                    // Show the correction message being sent (indented and dimmed)
                    for line in crate::llm::format_indented_dimmed_lines(
                        &self.display_payload(&correction),
                        8,
                    ) {
                        let _ = tx.send(format!("[TRACE] {}", line));
                    }
                }

                // The rejected response is already in `messages` (pushed above); pushing it a
                // second time only made the model read its own mistake twice.
                rejected_block_start.get_or_insert((assistant_idx, self.trim_generation));

                // Add correction as user message
                self.messages.push(Message::user(correction));

                // Continue to next iteration to retry
                continue;
            }

            // Get tool calls and regular actions from separate fields
            // Note: action_response.tools and action_response.actions are already separated
            // by the parsing logic (with backward compatibility for old format)
            let tools = action_response.tools;
            let regular = action_response.actions;

            // Validate regular actions by trying to parse them as CommonAction
            // This catches missing required parameters before execution
            let mut malformed_actions: Vec<(serde_json::Value, String)> = Vec::new();
            let mut valid_regular: Vec<serde_json::Value> = Vec::new();

            for action in regular {
                let action_type = action
                    .get("type")
                    .and_then(|t| t.as_str())
                    .unwrap_or("unknown");

                // Skip validation for actions that don't map to CommonAction (protocol-specific actions)
                // These will be validated later by their respective handlers
                let is_common_action = matches!(
                    action_type,
                    "open_server"
                        | "open_client"
                        | "close_server"
                        | "close_client"
                        | "close_all_servers"
                        | "close_all_clients"
                        | "update_instruction"
                        | "change_model"
                        | "set_memory"
                        | "schedule_task"
                        | "cancel_task"
                        | "show_message"
                        | "close_connection_by_id"
                        | "reconnect_client"
                        | "update_client_instruction"
                );

                if is_common_action {
                    // Try to parse as CommonAction to validate
                    match crate::llm::actions::common::CommonAction::from_json(&action) {
                        Ok(_) => valid_regular.push(action),
                        Err(e) => {
                            let error = if self.private_payloads {
                                "invalid credential-bearing action".into()
                            } else {
                                e.to_string()
                            };
                            malformed_actions.push((action, error));
                        }
                    }
                } else {
                    // Non-common actions pass through without validation here
                    valid_regular.push(action);
                }
            }

            // If we have malformed actions, trigger retry
            if !malformed_actions.is_empty() {
                malformed_action_retries += 1;

                // Log error with the actual malformed actions
                for (action_json, error) in &malformed_actions {
                    error!(
                        "LLM returned malformed action: {}. Error: {}. JSON: {}",
                        action_json
                            .get("type")
                            .and_then(|t| t.as_str())
                            .unwrap_or("unknown"),
                        error,
                        self.display_payload(
                            &serde_json::to_string(action_json)
                                .unwrap_or_else(|_| action_json.to_string())
                        )
                    );
                }

                if let Some(ref tx) = self.status_tx {
                    for (action_json, error) in &malformed_actions {
                        let action_type = action_json
                            .get("type")
                            .and_then(|t| t.as_str())
                            .unwrap_or("unknown");
                        let _ = tx.send(format!(
                            "[ERROR] Malformed action '{}': {}",
                            action_type, error
                        ));
                        // Show the actual JSON that was returned
                        let _ = tx.send(format!(
                            "[ERROR]   JSON: {}",
                            self.display_payload(
                                &serde_json::to_string(action_json)
                                    .unwrap_or_else(|_| action_json.to_string())
                            )
                        ));
                    }
                }

                if malformed_action_retries >= MAX_MALFORMED_ACTION_RETRIES {
                    // All retries exhausted - return error
                    let action_types: Vec<_> = malformed_actions
                        .iter()
                        .map(|(a, _)| a.get("type").and_then(|t| t.as_str()).unwrap_or("unknown"))
                        .collect();
                    error!(
                        "LLM failed to provide valid actions after {} retries. Malformed: {:?}",
                        malformed_action_retries, action_types
                    );
                    if let Some(ref tx) = self.status_tx {
                        let _ = tx.send(format!(
                            "[ERROR] LLM failed to provide valid actions after {} retries",
                            malformed_action_retries
                        ));
                    }
                    anyhow::bail!(
                        "LLM returned malformed action(s) after {} retries: {:?}",
                        malformed_action_retries,
                        action_types
                    );
                }

                // Build retry prompt for malformed actions
                let correction =
                    crate::llm::prompt::PromptBuilder::build_malformed_action_retry_prompt(
                        &malformed_actions,
                    );

                trace!(
                    "→ Sending malformed action correction and retrying (attempt {})...",
                    malformed_action_retries + 1
                );
                if let Some(ref tx) = self.status_tx {
                    let _ = tx.send(format!(
                        "[INFO] → Retrying due to malformed action(s) (attempt {}/{})...",
                        malformed_action_retries, MAX_MALFORMED_ACTION_RETRIES
                    ));
                }

                // The rejected response is already in `messages` (pushed above); pushing it a
                // second time only made the model read its own mistake twice.
                rejected_block_start.get_or_insert((assistant_idx, self.trim_generation));

                // Add correction as user message
                self.messages.push(Message::user(correction));

                // Continue to next iteration to retry
                continue;
            }

            // This response passed validation, so the rejected ones before it are superseded:
            // drop them and their corrections instead of re-sending them every iteration.
            if let Some((start, generation)) = rejected_block_start.take() {
                // A trim between the rejection and now would have shifted every index.
                if generation == self.trim_generation
                    && assistant_idx > start
                    && assistant_idx <= self.messages.len()
                {
                    let dropped = assistant_idx - start;
                    self.messages.drain(start..assistant_idx);
                    self.last_logged_index = self.last_logged_index.min(self.messages.len());
                    debug!(
                        "Dropped {} superseded message(s) from rejected action attempt(s)",
                        dropped
                    );
                }
            }

            // An action may not reference a tool result it has not been given yet.
            //
            // A model that emits a tool call and, in the *same* response, an action
            // whose parameter is `{{tools[0].result}}` is describing a substitution
            // nothing here performs: `reference_parser` resolves `<tagname>` XML
            // references, not `{{...}}`, so the placeholder survives verbatim. Without
            // this check the action was collected below, the tools ran, and the literal
            // string `{{tools[0].result}}` went out on the wire as if it were the value
            // — a BitTorrent `send_piece` whose block_hex is that text, a session token
            // that is that text. Silently wrong in the worst way: the action looked
            // well-formed at every layer that inspected it.
            //
            // The recovery is the one the model expects anyway: run the tools, hand it
            // the results, and let it re-emit the action with the real value. Nothing is
            // collected from this response, so the placeholder never reaches execution.
            let (deferred, ready): (Vec<_>, Vec<_>) = valid_regular
                .into_iter()
                .partition(|action| action_references_tool_result(action));

            if !deferred.is_empty() {
                let names: Vec<&str> = deferred
                    .iter()
                    .filter_map(|a| a.get("type").and_then(|t| t.as_str()))
                    .collect();
                // Dual-logged through the facade rather than a hand-written level
                // prefix; `tests/llm_log_prefix_guard_test.rs` fails the build on any
                // new one of those in src/llm/. (That guard counts substrings without
                // parsing, so even naming the pattern in a comment trips it.)
                Log::new(self.status_tx.as_ref())
                    .with_payloads(!self.private_payloads)
                    .warn(format!(
                        "LLM action(s) {:?} reference a tool result that has not been produced \
                     yet; holding them back until the tool(s) have run",
                        names
                    ));

                if tools.is_empty() {
                    // No tool call to wait for: the placeholder can never be filled in,
                    // so this is simply a malformed action. Ask for a literal value.
                    self.messages.push(Message::user(format!(
                        "The action(s) {:?} contain a placeholder like \
                         \"{{{{tools[0].result}}}}\". Placeholders are never substituted: \
                         whatever you write is used literally. You did not call any tool \
                         in that response either. Re-send the action with the actual \
                         value written out in full.",
                        names
                    )));
                    rejected_block_start.get_or_insert((assistant_idx, self.trim_generation));
                    continue;
                }

                // Tools are pending; they run below and their results are appended as a
                // user message, after which the model re-emits the action for real.
                self.messages.push(Message::user(format!(
                    "Hold on: the action(s) {:?} referenced a tool result with a \
                     placeholder, but placeholders are never substituted — whatever you \
                     write is sent literally. Your tool call(s) are running now. Wait for \
                     the results below, then re-send those action(s) with the actual \
                     values written out in full.",
                    names
                )));
            }

            // Only a final response commits actions. An intermediate tool round can
            // contain a draft answer, which the next response may repeat or replace.
            // Keeping it would execute both drafts (for example, two IMAP greetings).
            if tools.is_empty() {
                final_actions = Some(ready);
                debug!("No tool calls in response, finishing conversation");
                break;
            }

            if !ready.is_empty() {
                self.messages.push(Message::user(
                    "Actions in a response with tool calls are drafts and have not been executed. \
                     After reading the tool results, return all actions to execute in one \
                     final response with no tool calls."
                        .to_string(),
                ));
            }

            // If this is the last iteration, warn about unused tool calls
            if iteration == self.max_tool_iterations {
                warn!(
                    "Maximum iterations reached with {} pending tool calls",
                    tools.len()
                );
                if let Some(ref tx) = self.status_tx {
                    let _ = tx.send(format!(
                        "[WARN] Maximum iterations ({}) reached with {} pending tool call(s)",
                        self.max_tool_iterations,
                        tools.len()
                    ));
                }
                anyhow::bail!("Tool iteration limit reached before a final action response");
            }

            // Execute tool calls
            debug!("Executing {} tool calls", tools.len());
            if let Some(ref tx) = self.status_tx {
                let _ = tx.send(format!("[INFO] Executing {} tool call(s)...", tools.len()));
            }
            tool_results.clear();

            for tool_json in tools {
                match ToolAction::from_json(&tool_json) {
                    Ok(tool_action) => {
                        info!(
                            "→ Executing tool: {}",
                            self.display_payload(&tool_action.describe())
                        );
                        if let Some(ref tx) = self.status_tx {
                            let _ = tx.send(format!(
                                "[INFO] → Executing tool: {}",
                                self.display_payload(&tool_action.describe())
                            ));
                        }

                        // Track tool call in conversation state
                        if let Ok(mut state) = self.conversation_state.lock() {
                            let tool_name = match &tool_action {
                                ToolAction::ReadFile { .. } => "read_file",
                                ToolAction::WebSearch { .. } => "web_search",
                                ToolAction::ReadBaseStackDocs { .. } => "read_base_stack_docs",
                                ToolAction::ReadServerDocumentation { .. } => {
                                    "read_server_documentation"
                                }
                                ToolAction::ReadClientDocumentation { .. } => {
                                    "read_client_documentation"
                                }
                                ToolAction::ReadDocumentation { .. } => "read_documentation",
                                ToolAction::ListModels => "list_models",
                                ToolAction::GenerateRandom { .. } => "generate_random",
                                ToolAction::ListTasks => "list_tasks",
                                #[cfg(feature = "sqlite")]
                                ToolAction::ExecuteSql { .. } => "execute_sql",
                                #[cfg(feature = "sqlite")]
                                ToolAction::ListDatabases => "list_databases",
                            };
                            state.add_tool_call(
                                tool_name.to_string(),
                                self.display_payload(&tool_action.describe()),
                            );
                        }

                        // Check if this is a doc reading tool
                        let is_read_server_docs =
                            matches!(tool_action, ToolAction::ReadServerDocumentation { .. });
                        let is_read_client_docs =
                            matches!(tool_action, ToolAction::ReadClientDocumentation { .. });
                        let is_read_base_docs =
                            matches!(tool_action, ToolAction::ReadBaseStackDocs { .. });
                        let is_read_docs =
                            matches!(tool_action, ToolAction::ReadDocumentation { .. });

                        let execution =
                            execute_tool(&tool_action, approval_tx.as_ref(), web_search_mode, None);
                        let result = if self.private_payloads {
                            use tracing::instrument::WithSubscriber;
                            execution
                                .with_subscriber(tracing::subscriber::NoSubscriber::default())
                                .await
                        } else {
                            execution.await
                        };
                        info!("  Result: {}", self.display_payload(&result.summary()));
                        if let Some(ref tx) = self.status_tx {
                            let status = if result.success { "✓" } else { "✗" };
                            let _ = tx.send(format!(
                                "[INFO]   {} {}",
                                status,
                                self.display_payload(&result.summary())
                            ));
                        }

                        // Mark protocol docs as read if the tool succeeded
                        // This will update the system prompt to enable open_server/open_client actions
                        if result.success {
                            if is_read_server_docs {
                                self.mark_protocol_docs_read(&available_actions);
                                // Extract protocols and update AppState and ConversationState
                                if let ToolAction::ReadServerDocumentation {
                                    protocols,
                                    protocol,
                                } = &tool_action
                                {
                                    let mut all_protocols = protocols.clone();
                                    if let Some(p) = protocol {
                                        if !all_protocols.contains(p) {
                                            all_protocols.push(p.clone());
                                        }
                                    }
                                    // Update ConversationState to persist documented protocols
                                    if let Ok(mut conv_state) = self.conversation_state.lock() {
                                        conv_state.mark_server_protocols_documented(&all_protocols);
                                    }
                                    // Update AppState (global persistence)
                                    if let Some(ref state) = self.state {
                                        let state_clone = state.clone();
                                        let protocols_clone = all_protocols.clone();
                                        tokio::spawn(async move {
                                            state_clone
                                                .mark_server_protocols_documented(&protocols_clone)
                                                .await;
                                        });
                                    }
                                }
                                // Enable open_server in valid actions
                                if !valid_action_names.contains("open_server") {
                                    valid_action_names.insert("open_server".to_string());
                                    valid_action_names_list.push("open_server".to_string());
                                }
                            }
                            if is_read_client_docs {
                                self.mark_protocol_docs_read(&available_actions);
                                // Extract protocols and update AppState and ConversationState
                                if let ToolAction::ReadClientDocumentation {
                                    protocols,
                                    protocol,
                                } = &tool_action
                                {
                                    let mut all_protocols = protocols.clone();
                                    if let Some(p) = protocol {
                                        if !all_protocols.contains(p) {
                                            all_protocols.push(p.clone());
                                        }
                                    }
                                    // Update ConversationState to persist documented protocols
                                    if let Ok(mut conv_state) = self.conversation_state.lock() {
                                        conv_state.mark_client_protocols_documented(&all_protocols);
                                    }
                                    // Update AppState (global persistence)
                                    if let Some(ref state) = self.state {
                                        let state_clone = state.clone();
                                        let protocols_clone = all_protocols.clone();
                                        tokio::spawn(async move {
                                            state_clone
                                                .mark_client_protocols_documented(&protocols_clone)
                                                .await;
                                        });
                                    }
                                }
                                // Enable open_client in valid actions
                                if !valid_action_names.contains("open_client") {
                                    valid_action_names.insert("open_client".to_string());
                                    valid_action_names_list.push("open_client".to_string());
                                }
                            }
                            if is_read_base_docs {
                                self.mark_protocol_docs_read(&available_actions);
                                // Enable both open_server and open_client in valid actions
                                if !valid_action_names.contains("open_server") {
                                    valid_action_names.insert("open_server".to_string());
                                    valid_action_names_list.push("open_server".to_string());
                                }
                                if !valid_action_names.contains("open_client") {
                                    valid_action_names.insert("open_client".to_string());
                                    valid_action_names_list.push("open_client".to_string());
                                }
                            }
                            // Handle unified read_documentation tool
                            if is_read_docs {
                                self.mark_protocol_docs_read(&available_actions);
                                // Extract protocols and update both server and client state
                                if let ToolAction::ReadDocumentation {
                                    protocols,
                                    protocol,
                                } = &tool_action
                                {
                                    let mut all_protocols = protocols.clone();
                                    if let Some(p) = protocol {
                                        if !all_protocols.contains(p) {
                                            all_protocols.push(p.clone());
                                        }
                                    }
                                    // Update ConversationState to persist documented protocols (both server and client)
                                    if let Ok(mut conv_state) = self.conversation_state.lock() {
                                        conv_state.mark_server_protocols_documented(&all_protocols);
                                        conv_state.mark_client_protocols_documented(&all_protocols);
                                    }
                                    // Update AppState (global persistence for both server and client)
                                    if let Some(ref state) = self.state {
                                        let state_clone = state.clone();
                                        let protocols_clone = all_protocols.clone();
                                        tokio::spawn(async move {
                                            state_clone
                                                .mark_server_protocols_documented(&protocols_clone)
                                                .await;
                                            state_clone
                                                .mark_client_protocols_documented(&protocols_clone)
                                                .await;
                                        });
                                    }
                                }
                                // CRITICAL: Enable open_server and open_client in valid actions
                                // so the LLM can use them in subsequent iterations
                                if !valid_action_names.contains("open_server") {
                                    valid_action_names.insert("open_server".to_string());
                                    valid_action_names_list.push("open_server".to_string());
                                    debug!(
                                        "Enabled open_server action after reading documentation"
                                    );
                                }
                                if !valid_action_names.contains("open_client") {
                                    valid_action_names.insert("open_client".to_string());
                                    valid_action_names_list.push("open_client".to_string());
                                    debug!(
                                        "Enabled open_client action after reading documentation"
                                    );
                                }
                            }
                        }

                        tool_results.push(result);
                    }
                    Err(e) => {
                        let error = if self.private_payloads {
                            "invalid credential-bearing tool action".to_string()
                        } else {
                            e.to_string()
                        };
                        error!("Failed to parse tool action: {}", error);
                        if let Some(ref tx) = self.status_tx {
                            let _ =
                                tx.send(format!("[ERROR] Failed to parse tool action: {}", error));
                        }
                        tool_results.push(ToolResult::error(
                            "unknown",
                            "parse_error",
                            format!("Failed to parse tool action: {}", error),
                        ));
                    }
                }
            }

            // Check if all tools failed
            let all_failed = !tool_results.is_empty() && tool_results.iter().all(|r| !r.success);
            if all_failed {
                consecutive_tool_failures += 1;
                warn!(
                    "All {} tool calls failed (consecutive failures: {})",
                    tool_results.len(),
                    consecutive_tool_failures
                );
                if let Some(ref tx) = self.status_tx {
                    let _ = tx.send(format!(
                        "[WARN] All {} tool calls failed (consecutive failures: {})",
                        tool_results.len(),
                        consecutive_tool_failures
                    ));
                }

                if consecutive_tool_failures >= MAX_CONSECUTIVE_FAILURES {
                    error!(
                        "Breaking tool calling loop after {} consecutive failures",
                        consecutive_tool_failures
                    );
                    if let Some(ref tx) = self.status_tx {
                        let _ = tx.send(format!(
                            "[ERROR] Breaking tool loop after {} consecutive failures",
                            consecutive_tool_failures
                        ));
                    }
                    // Add a final message explaining the issue
                    self.messages.push(Message::user(
                        "CRITICAL: All tool calls are failing. Stop calling tools and respond with regular actions instead.".to_string()
                    ));
                    break;
                }
            } else {
                // Reset counter if at least one tool succeeded
                consecutive_tool_failures = 0;
            }

            // Add tool results as a user message for the next iteration
            if !tool_results.is_empty() {
                let tool_results_text = self.format_tool_results(&tool_results);
                self.messages.push(Message::user(tool_results_text));
            }

            debug!(
                "Completed iteration {}/{}, {} tool results provided for next iteration",
                iteration,
                self.max_tool_iterations,
                tool_results.len()
            );
            if let Some(ref tx) = self.status_tx {
                let _ = tx.send(format!(
                    "[TRACE] Iteration {}/{} complete, continuing with {} tool result(s)...",
                    iteration,
                    self.max_tool_iterations,
                    tool_results.len()
                ));
            }
        }

        let all_actions = final_actions.context(
            "Conversation ended without a final action response; no draft actions executed",
        )?;

        // Log details for each action (validation already happened above)
        for action in &all_actions {
            let action_type = action
                .get("type")
                .and_then(|t| t.as_str())
                .unwrap_or("unknown");

            // Build action details string based on action type
            let details = match action_type {
                "open_server" => {
                    let port = action.get("port").and_then(|p| p.as_u64());
                    let base_stack = action.get("base_stack").and_then(|b| b.as_str());
                    format!(
                        "port={}, base_stack={}",
                        port.map(|p| p.to_string())
                            .unwrap_or_else(|| "auto".to_string()),
                        base_stack.unwrap_or("?")
                    )
                }
                "open_client" => {
                    let protocol = action.get("protocol").and_then(|p| p.as_str());
                    let remote_addr = action.get("remote_addr").and_then(|r| r.as_str());
                    format!(
                        "protocol={}, remote_addr={}",
                        protocol.unwrap_or("?"),
                        remote_addr.unwrap_or("?")
                    )
                }
                "close_server" => {
                    let server_id = action.get("server_id").and_then(|p| p.as_u64());
                    format!(
                        "server_id={}",
                        server_id
                            .map(|p| p.to_string())
                            .unwrap_or_else(|| "?".to_string())
                    )
                }
                "update_instruction" => {
                    let instruction = action.get("instruction").and_then(|i| i.as_str());
                    format!(
                        "instruction={}",
                        instruction
                            .map(|i| {
                                let preview: String = i.chars().take(30).collect();
                                if i.len() > 30 {
                                    format!("\"{}...\"", preview)
                                } else {
                                    format!("\"{}\"", i)
                                }
                            })
                            .unwrap_or_else(|| "?".to_string())
                    )
                }
                "show_message" => {
                    let message = action.get("message").and_then(|m| m.as_str()).unwrap_or("");
                    let preview: String = message.chars().take(50).collect();
                    if message.len() > 50 {
                        format!("\"{}...\"", preview)
                    } else {
                        format!("\"{}\"", preview)
                    }
                }
                _ => {
                    // For other actions, show first few keys
                    let keys: Vec<&str> = action
                        .as_object()
                        .map(|obj| {
                            obj.keys()
                                .filter(|k| *k != "type")
                                .take(3)
                                .map(|s| s.as_str())
                                .collect()
                        })
                        .unwrap_or_default();
                    if keys.is_empty() {
                        String::new()
                    } else {
                        format!("keys=[{}]", keys.join(", "))
                    }
                }
            };

            let details = self.display_payload(&details);
            let log_msg = if details.is_empty() {
                format!("[INFO]   → {}", action_type)
            } else {
                format!("[INFO]   → {} ({})", action_type, details)
            };

            info!("Action: {} {}", action_type, details);
            if let Some(ref tx) = self.status_tx {
                let _ = tx.send(log_msg);
            }
        }

        info!(
            "Conversation complete: {} total actions collected",
            all_actions.len()
        );
        if let Some(ref tx) = self.status_tx {
            let _ = tx.send(format!(
                "[INFO] ✓ Conversation complete: {} action(s)",
                all_actions.len()
            ));
        }

        // End conversation tracking if enabled
        if let Some(state) = &self.state {
            state.end_conversation(&self.conversation_id).await;
        }

        Ok(all_actions)
    }

    /// Generate a response with automatic retry on parse errors
    ///
    /// Attempts to get a valid ActionResponse from the LLM. If parsing fails,
    /// sends a corrective message and retries once.
    ///
    /// Returns (original_response, cleaned_response):
    /// - original_response: Response with reasoning tags (for conversation history)
    /// - cleaned_response: Response with reasoning stripped (for JSON parsing)
    async fn generate_with_retry(&mut self) -> Result<(String, String)> {
        // Where this call's messages start. A parse failure appends the unparseable response
        // and a correction; once a valid response supersedes them they are pure noise that
        // would otherwise be re-sent for the rest of the conversation, so they are dropped.
        let attempt_block_start = self.messages.len();
        let attempt_block_generation = self.trim_generation;

        for attempt in 1..=self.max_retries + 1 {
            // Bound what is actually sent before building the request.
            self.trim_history();

            // ONE semantic line per request attempt, to BOTH channels. The transport
            // (OllamaClient) separately logs the wire facts (model, sizes) file-only, so
            // the round-trip is no longer announced twice per layer to each channel.
            self.log().info(format!(
                "LLM request (attempt {}/{})",
                attempt,
                self.max_retries + 1
            ));

            // Message-count summary and the per-message payload dump are DEBUG/TRACE:
            // file-only, never streamed to the unbounded TUI channel.
            let new_message_count = self.messages.len().saturating_sub(self.last_logged_index);
            self.log().debug(format!(
                "Conversation state: {} messages, {} new since last call",
                self.messages.len(),
                new_message_count
            ));

            if new_message_count > 0 {
                let log = self.log();
                log.trace("New messages:");
                for (idx, msg) in self
                    .messages
                    .iter()
                    .enumerate()
                    .skip(self.last_logged_index)
                {
                    log.trace(format!(
                        "  Message {}: [{}] {}",
                        idx + 1,
                        msg.role,
                        crate::utils::truncate_for_log(&msg.content, 200)
                    ));
                }
            }

            // Update the last logged index (track what's been sent, don't re-log)
            self.last_logged_index = self.messages.len();

            // Acquire rate limiter permit (waits for user requests, discards network requests if limited)
            let permit = self
                .rate_limiter
                .acquire_permit(self.request_source)
                .await
                .context("Rate limit exceeded")?;

            // Choose between native tool calling (chat API) and prompt-based (generate API)
            let response_text = if self.use_native_tools {
                // Native tool calling path: send structured messages with tool schemas
                let chat_request = ChatRequest {
                    messages: self.messages.clone(),
                    tools: self.tool_schemas.clone(),
                    model: self.model.clone(),
                    offered_actions: self.offered_actions.clone(),
                };

                let chat_response = self
                    .client
                    .chat_with_tools(&chat_request, self.private_payloads)
                    .await
                    .context("Chat API call failed")?;

                // Record token usage
                permit
                    .record_usage(
                        chat_response.token_usage.prompt_tokens,
                        chat_response.token_usage.completion_tokens,
                    )
                    .await;

                // Convert native tool_calls to ActionResponse text format
                // so the existing parsing/validation pipeline works unchanged
                if !chat_response.tool_calls.is_empty() {
                    debug!(
                        "Native tool calling: {} tool_calls received",
                        chat_response.tool_calls.len()
                    );
                    Self::chat_response_to_action_text(&chat_response)
                } else if let Some(ref content) = chat_response.content {
                    // No tool calls, just text - try to use it as-is
                    // (may be JSON actions from prompt-based models that ignore tools param)
                    content.clone()
                } else {
                    // Empty response
                    "{}".to_string()
                }
            } else {
                // Prompt-based fallback: concatenate messages into single prompt
                let mut full_prompt = String::new();
                for msg in &self.messages {
                    match msg.role.as_str() {
                        "system" => {
                            full_prompt.push_str(&msg.content);
                            full_prompt.push_str("\n\n");
                        }
                        "user" => {
                            full_prompt.push_str(&msg.content);
                            full_prompt.push_str("\n\n");
                        }
                        "assistant" => {
                            // Include previous assistant responses in conversation
                            full_prompt.push_str("Actions you have executed:\n");
                            full_prompt.push_str(&msg.content);
                            full_prompt.push_str("\n\n");
                        }
                        _ => {}
                    }
                }

                // Call generate API with concatenated prompt
                // We rely on prompt engineering for JSON responses rather than format enforcement,
                // as some models (e.g., gpt-oss) don't support Ollama's JSON format mode
                let generate_response = self
                    .client
                    .generate_offering_with_event(
                        &self.model,
                        &full_prompt,
                        None,
                        &self.offered_actions,
                        self.bridge_event.as_ref(),
                        self.private_payloads,
                    )
                    .await
                    .context("Generate API call failed")?;

                // Record token usage
                permit
                    .record_usage(
                        generate_response.token_usage.prompt_tokens,
                        generate_response.token_usage.completion_tokens,
                    )
                    .await;

                generate_response.text
            };

            // ONE semantic line that a response arrived, to BOTH channels (the result
            // half of the round-trip). The transport already logged response size/tokens
            // file-only, and the full body goes to the file at TRACE below — it is never
            // streamed to the TUI.
            self.log().info(format!(
                "LLM response received (attempt {}): {} chars",
                attempt,
                response_text.len(),
            ));

            // Extract reasoning if present (before normalization to preserve formatting)
            let (reasoning, cleaned_response) = extract_reasoning(&response_text);

            // Reasoning is a full payload: TRACE, file-only.
            if let Some(ref reasoning_text) = reasoning {
                self.log()
                    .trace(format!("LLM Reasoning: {}", reasoning_text));
            }

            // Normalize the cleaned response: collapse whitespace and remove extra newlines
            // This handles cases where LLM returns formatted JSON with lots of whitespace
            let normalized_response = cleaned_response
                .lines()
                .map(|line| line.trim())
                .collect::<Vec<_>>()
                .join("");

            // Normalized-response preview is a DEBUG summary and the full body a TRACE
            // payload — both file-only. The full normalized body used to be pushed to the
            // TUI here, duplicating the transport's own body dump; that is removed.
            let log = self.log();
            log.debug(format!(
                "Response (normalized): {}",
                crate::utils::truncate_for_log(&normalized_response, 200)
            ));
            log.payload(
                "LLM response (normalized)",
                &normalized_response,
                usize::MAX,
            );

            // Try to parse as ActionResponse (use normalized version for better compatibility)
            match ActionResponse::from_str(&normalized_response) {
                Ok(action_response) => {
                    // Valid response!
                    // Track in conversation state
                    if let Ok(mut state) = self.conversation_state.lock() {
                        state.add_llm_response(
                            normalized_response.clone(),
                            Some(serde_json::json!(action_response)),
                        );
                    }

                    if attempt > 1 {
                        info!(
                            "✓ Retry successful! LLM provided valid format on attempt {}",
                            attempt
                        );
                        if let Some(ref tx) = self.status_tx {
                            let _ = tx
                                .send(format!("[INFO] ✓ Retry successful on attempt {}", attempt));
                        }
                        // This response supersedes the failed attempts: drop them and their
                        // corrections rather than carrying them for the rest of the
                        // conversation. Nothing was appended on the success path, so the
                        // block is exactly what the failed attempts added.
                        if self.trim_generation == attempt_block_generation
                            && self.messages.len() > attempt_block_start
                        {
                            let dropped = self.messages.len() - attempt_block_start;
                            self.messages.truncate(attempt_block_start);
                            self.last_logged_index =
                                self.last_logged_index.min(self.messages.len());
                            debug!(
                                "Dropped {} superseded message(s) from {} failed parse attempt(s)",
                                dropped,
                                attempt - 1
                            );
                        }
                    } else {
                        info!("✓ Valid response format on first attempt");
                    }
                    // Return both original (with reasoning) and normalized (for parsing)
                    return Ok((response_text.clone(), normalized_response));
                }
                Err(e) => {
                    let e = if self.private_payloads {
                        anyhow::anyhow!("invalid credential-bearing model response")
                    } else {
                        e
                    };
                    if attempt <= self.max_retries {
                        // We have retries left, send corrective feedback
                        warn!("✗ Parse error on attempt {}: {}", attempt, e);
                        warn!(
                            "Malformed response (raw): {}",
                            if response_text.is_empty() {
                                "(empty response)".to_string()
                            } else {
                                self.display_payload(&crate::utils::truncate_for_log(
                                    &response_text,
                                    500,
                                ))
                            }
                        );

                        if let Some(ref tx) = self.status_tx {
                            let error_preview = if normalized_response.is_empty() {
                                "(empty)".to_string()
                            } else {
                                self.display_payload(&crate::utils::truncate_for_log(
                                    &normalized_response,
                                    100,
                                ))
                            };
                            let _ = tx.send(format!(
                                "[WARN] ✗ Invalid format (attempt {}): {}. Response: {}",
                                attempt, e, error_preview
                            ));
                        }

                        // Add the malformed response as an assistant message (use normalized for conversation)
                        self.messages
                            .push(Message::assistant(normalized_response.clone()));

                        // Track invalid response in conversation state
                        if let Ok(mut state) = self.conversation_state.lock() {
                            state.add_llm_response(normalized_response.clone(), None);
                        }

                        // Build corrective user message using minimal retry prompt
                        let correction =
                            crate::llm::prompt::PromptBuilder::build_retry_prompt(&e.to_string());
                        debug!(
                            "Correction message preview: {}",
                            crate::utils::truncate_for_log(&correction, 200)
                        );

                        // Track retry instruction in conversation state
                        if let Ok(mut state) = self.conversation_state.lock() {
                            state.add_retry_instruction(correction.clone());
                        }

                        info!(
                            "→ Sending correction and retrying (attempt {})...",
                            attempt + 1
                        );
                        if let Some(ref tx) = self.status_tx {
                            let _ = tx.send("[INFO] → Sending correction to LLM...".to_string());
                            // Show the correction message being sent (indented and dimmed)
                            for line in crate::llm::format_indented_dimmed_lines(
                                &self.display_payload(&correction),
                                8,
                            ) {
                                let _ = tx.send(format!("[INFO] {}", line));
                            }
                        }

                        self.messages.push(Message::user(correction));
                    } else {
                        // No more retries
                        error!("✗ Failed to get valid response after {} attempts", attempt);
                        if let Some(ref tx) = self.status_tx {
                            let _ = tx.send(format!(
                                "[ERROR] ✗ Failed after {} attempts: {}",
                                attempt, e
                            ));
                        }
                        return Err(e).context("LLM failed to provide valid format after retry");
                    }
                }
            }
        }

        unreachable!("Loop should always return or error")
    }

    /// Format tool results for inclusion in the next message
    fn format_tool_results(&self, results: &[ToolResult]) -> String {
        let mut formatted = String::from("Tool execution results:\n\n");

        for (i, result) in results.iter().enumerate() {
            formatted.push_str(&format!("{}. {}\n", i + 1, result.summary()));
            formatted.push_str(&format!(
                "Status: {}\n",
                if result.success { "Success" } else { "Error" }
            ));

            // Truncate very long results on a char boundary, telling the model
            // explicitly how much was omitted so it can narrow its next request.
            let result_text = &result.result;
            formatted.push_str(&format!(
                "Result: {}\n",
                crate::utils::truncate_with_notice(result_text, 2000)
            ));

            formatted.push('\n');
        }

        formatted
    }

    /// Get the current conversation messages (for debugging)
    pub fn messages(&self) -> &[Message] {
        &self.messages
    }

    /// Get the number of messages in the conversation
    pub fn message_count(&self) -> usize {
        self.messages.len()
    }

    /// Get the conversation ID
    pub fn conversation_id(&self) -> &str {
        &self.conversation_id
    }

    /// Mark this conversation as already registered (to prevent duplicate registration)
    pub fn mark_registered(&mut self) {
        self.registered = true;
    }

    /// Update the "Current State" section in the system message
    ///
    /// This is used after actions like `open_server` that modify application state.
    /// The system message is rebuilt with updated state so subsequent tool calls
    /// see the current state.
    ///
    /// # Arguments
    /// * `state` - Application state
    /// * `server_id` - Optional server context
    pub async fn update_current_state(
        &mut self,
        state: &crate::state::app_state::AppState,
        server_id: Option<crate::state::ServerId>,
    ) {
        use crate::llm::prompt::PromptBuilder;

        if self.messages.is_empty() {
            warn!("Cannot update current state: no system message found");
            return;
        }

        // Get the system message (first message)
        let system_msg = &self.messages[0];
        if system_msg.role != "system" {
            warn!("First message is not a system message, cannot update current state");
            return;
        }

        let old_content = &system_msg.content;

        // Find the "# Current State" section
        if let Some(state_start) = old_content.find("# Current State") {
            // Find the next section (starts with "# ")
            let state_content_start = state_start;
            let state_end = old_content[state_content_start..]
                .find("\n# ")
                .map(|pos| state_content_start + pos)
                .unwrap_or(old_content.len());

            // Build new current state section
            let new_state_section =
                PromptBuilder::build_current_state_section_public(state, server_id).await;

            // Replace the old state section with the new one
            let mut new_content = String::new();
            new_content.push_str(&old_content[..state_start]);
            new_content.push_str(&new_state_section);
            // Don't include the newline before next section, it's already in new_state_section
            if state_end < old_content.len() {
                new_content.push_str(&old_content[state_end..]);
            }

            // Update the system message
            self.messages[0] = Message::system(new_content);

            debug!("Updated Current State section in system message");
        } else {
            warn!("Could not find '# Current State' section in system message");
        }
    }
}