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
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
//! Cassandra/CQL server implementation using cassandra-protocol
//!
//! Phase 1 (Minimal Viable):
//! - STARTUP → READY (no auth)
//! - OPTIONS → SUPPORTED
//! - QUERY → RESULT (LLM-generated rows)
//! - Single-stream operation (sequential processing)
//! - Protocol v4 only
//! - Basic types: int, varchar, boolean

pub mod actions;

use crate::llm::action_helper::call_llm;
use crate::llm::actions::protocol_trait::ActionResult;
use crate::llm::ollama_client::OllamaClient;
use crate::logging::emit::Log;
use crate::protocol::Event;
use crate::server::connection::ConnectionId;
use crate::state::app_state::AppState;
use crate::state::server::{ConnectionState, ConnectionStatus, ProtocolConnectionInfo};
use crate::utils::clock::Instant;
use actions::*;
use anyhow::{Context, Result};
use bytes::{Buf, BytesMut};
use cassandra_protocol::compression::Compression;
use cassandra_protocol::frame::{Direction, Envelope, Flags, Opcode, Version};
use serde_json::json;
use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::Arc;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::{TcpListener, TcpStream};
use tokio::sync::{mpsc, Mutex};
use tracing::{debug, error, trace, warn};

/// Largest frame body accepted, matching the native protocol's own 256 MiB maximum.
pub const MAX_FRAME_BODY_BYTES: usize = 256 * 1024 * 1024;

/// How long to wait for a peer's first frame after it has connected.
///
/// CQL is client-speaks-first: the driver sends OPTIONS and then STARTUP inside its connect
/// path, and the server says nothing before them. A peer that has connected and sent nothing
/// has not begun a handshake, so it gets the short bound — and this is the one an
/// unauthenticated flood lives under, because it never reaches the idle bound below.
const FIRST_FRAME_READ_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(30);

/// How long to wait for a *further* frame once the connection is ready.
///
/// Fifteen minutes, and long on purpose. A Cassandra driver holds a *pool* of persistent
/// connections and legitimately sends nothing on most of them for long stretches — that is how
/// the protocol is meant to be used, not a symptom. What keeps the bound meaningful is that the
/// drivers answer the same question themselves: both the DataStax and ScyllaDB drivers send a
/// heartbeat OPTIONS on an idle connection, at a default interval of 30 seconds, precisely so
/// an idle-but-live connection keeps proving it is live. Fifteen minutes is thirty of those
/// intervals; a connection that produces neither a query nor a single heartbeat in that time is
/// indistinguishable from a peer that has gone away. (Cassandra's own
/// `native_transport_idle_timeout_in_ms` ships disabled, so unlike Kafka there is no upstream
/// default to copy.)
const IDLE_BETWEEN_FRAMES_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(900);

/// Concurrent connections this server admits.
///
/// Each connection may buffer a frame of up to [`MAX_FRAME_BODY_BYTES`], so the cap is what
/// turns that per-connection bound into a total one.
const MAX_CONNECTIONS: usize = crate::server::accept_bounded::DEFAULT_MAX_CONNECTIONS;

/// A peer over [`MAX_CONNECTIONS`] gets a plain close, and the reason is specific to CQL.
///
/// There *is* a natural vocabulary — an ERROR frame carrying
/// [`CASSANDRA_ERROR_OVERLOADED`] — but every CQL frame is stamped with the protocol version,
/// and the version is chosen by the client's first frame, which this peer has not sent. Any
/// ERROR written here would have to guess it, and guessing wrong replaces a clear "server is
/// full" with a protocol-version error: strictly less informative than silence. Real Cassandra
/// exceeding `native_transport_max_concurrent_connections` closes the channel without a frame
/// for the same reason. The refusal is logged with `decision=fail_closed_connection_cap`.
const CONNECTION_CAP_REFUSAL: &[u8] = b"";

/// Cap on prepared statements retained per connection.
///
/// This is protocol session state, not storage: it holds the query text a client asked the
/// server to remember so a later EXECUTE can be resolved back to it. It still needs a bound,
/// because a client can PREPARE unlimited distinct queries on one connection and every one of
/// them was retained for the life of that connection.
const MAX_PREPARED_STATEMENTS: usize = 1024;

/// Native-protocol ERROR code 0x0000, "Server error: something unexpected happened".
const CASSANDRA_ERROR_SERVER_ERROR: u32 = 0x0000;

/// Native-protocol ERROR code 0x1001, "Overloaded: the request cannot be processed because the
/// coordinator node is overloaded". Drivers treat it as retryable.
const CASSANDRA_ERROR_OVERLOADED: u32 = 0x1001;

/// Native-protocol ERROR code 0x000A, "Protocol error: some client message triggered a
/// protocol violation". What a real coordinator answers to a frame it cannot parse.
const CASSANDRA_ERROR_PROTOCOL: u32 = 0x000A;

/// Native-protocol ERROR code 0x2500, "Unprepared".
///
/// The only error whose body carries a payload beyond the message: `[short bytes]` naming the
/// statement id the coordinator does not know. Drivers exist to handle it — scylla re-PREPAREs
/// and retries — so answering it turns an unknown id from a dead session into a hiccup.
const CASSANDRA_ERROR_UNPREPARED: u32 = 0x2500;

/// Name the `[short]` consistency level a client asked for (native protocol v4, §3).
///
/// Reported verbatim to the handler. Anything outside the defined set is `UNKNOWN` rather
/// than a plausible-looking guess — the handler can refuse what it does not recognise, but
/// only if it is told the truth.
fn consistency_name(code: u16) -> &'static str {
    match code {
        0x0000 => "ANY",
        0x0001 => "ONE",
        0x0002 => "TWO",
        0x0003 => "THREE",
        0x0004 => "QUORUM",
        0x0005 => "ALL",
        0x0006 => "LOCAL_QUORUM",
        0x0007 => "EACH_QUORUM",
        0x0008 => "SERIAL",
        0x0009 => "LOCAL_SERIAL",
        0x000A => "LOCAL_ONE",
        _ => "UNKNOWN",
    }
}

/// Cassandra server implementation
pub struct CassandraServer {
    llm_client: OllamaClient,
    app_state: Arc<AppState>,
    _status_tx: mpsc::UnboundedSender<String>,
    server_id: Option<crate::state::ServerId>,
}

/// Connection state for a Cassandra client
struct CassandraConnectionState {
    ready: bool,
    protocol_version: u8,
    /// Prepared statements: statement_id -> (query_string, param_count)
    prepared_statements: HashMap<Vec<u8>, (String, usize)>,
    /// Authentication state
    authenticated: bool,
    /// Authenticated username (if any)
    username: Option<String>,
}

/// The write half of one live connection, plus what counting a write needs.
///
/// Every reply goes through this rather than through the socket: the write half is shared with
/// the dashboard's peer-command task (`server::peer_support`), so it lives behind a `Mutex`,
/// and an injected send and the server's own reply cannot interleave bytes into the middle of
/// each other's frame. `bytes_sent`/`packets_sent` are updated here — they were updated
/// nowhere before, so the rail's arrow counters sat at zero for the life of a CQL session.
struct ConnWrite {
    write_half: Arc<Mutex<tokio::io::WriteHalf<TcpStream>>>,
    app_state: Arc<AppState>,
    server_id: Option<crate::state::ServerId>,
    connection_id: ConnectionId,
}

impl ConnWrite {
    /// Write one whole CQL frame and count it. The guard is dropped before the stats update,
    /// so nothing awaits on `AppState`'s lock while holding the write half.
    async fn write(&self, data: &[u8]) -> Result<()> {
        {
            let mut writer = self.write_half.lock().await;
            writer.write_all(data).await?;
            writer.flush().await?;
        }
        if let Some(server_id) = self.server_id {
            self.app_state
                .update_connection_stats(
                    server_id,
                    self.connection_id,
                    None,
                    Some(data.len() as u64),
                    None,
                    Some(1),
                )
                .await;
        }
        Ok(())
    }
}

impl CassandraServer {
    /// Create a new Cassandra server
    pub fn new(
        llm_client: OllamaClient,
        app_state: Arc<AppState>,
        status_tx: mpsc::UnboundedSender<String>,
        server_id: Option<crate::state::ServerId>,
    ) -> Self {
        Self {
            llm_client,
            app_state,
            _status_tx: status_tx,
            server_id,
        }
    }

    /// Spawn Cassandra server with LLM integration
    pub async fn spawn_with_llm_actions(
        listen_addr: SocketAddr,
        llm_client: OllamaClient,
        app_state: Arc<AppState>,
        status_tx: mpsc::UnboundedSender<String>,
        server_id: crate::state::ServerId,
    ) -> Result<SocketAddr> {
        let listener = TcpListener::bind(listen_addr).await?;
        let actual_addr = listener.local_addr()?;

        Log::new(Some(&status_tx))
            .info(format!("Cassandra/CQL server listening on {}", actual_addr));

        let server = Arc::new(CassandraServer::new(
            llm_client,
            app_state.clone(),
            status_tx.clone(),
            Some(server_id),
        ));

        // Spawn the accept loop
        let limiter = crate::server::accept_bounded::ConnectionLimiter::new(MAX_CONNECTIONS);
        let loop_state = app_state.clone();
        let accept_handle = tokio::spawn(async move {
            loop {
                match crate::server::accept_bounded::accept_bounded(
                    &listener,
                    &limiter,
                    CONNECTION_CAP_REFUSAL,
                    "Cassandra",
                    Some(&status_tx),
                )
                .await
                {
                    Ok((stream, addr, permit)) => {
                        Log::new(Some(&status_tx))
                            .debug(format!("Cassandra connection from {}", addr));

                        let server_clone = server.clone();
                        let status_tx_clone = status_tx.clone();

                        // Tracked, not detached: stop_server must abort this task too.
                        let task_owner = loop_state.clone();
                        task_owner
                            .spawn_server_task(server_id, async move {
                                // Held for the life of the connection, so the cap counts live
                                // clients rather than accepts.
                                let _permit = permit;
                                if let Err(e) = server_clone
                                    .handle_connection(stream, addr, status_tx_clone)
                                    .await
                                {
                                    error!("Cassandra connection error: {}", e);
                                }
                            })
                            .await;
                    }
                    Err(e) => {
                        // A persistent accept error (EMFILE, listener torn down) recurs
                        // immediately, so continuing spins a hot loop that floods the
                        // unbounded status channel. Stop the listener instead.
                        Log::new(Some(&status_tx))
                            .error(format!("Cassandra accept failed, listener stopped: {}", e));
                        break;
                    }
                }
            }
        });

        // Register the accept loop so stop_server can abort it and release the port.
        app_state
            .register_server_task(server_id, accept_handle)
            .await;

        Ok(actual_addr)
    }

    /// Set one Cassandra connection up, run it, and tear it down on every exit path.
    async fn handle_connection(
        &self,
        stream: TcpStream,
        addr: SocketAddr,
        status_tx: mpsc::UnboundedSender<String>,
    ) -> Result<()> {
        let connection_id = ConnectionId::new(self.app_state.get_next_unified_id().await);
        let local_addr = stream.local_addr().unwrap_or(addr);

        // Track the connection
        if let Some(server_id) = self.server_id {
            let now = Instant::now();
            let conn_state = ConnectionState {
                id: connection_id,
                remote_addr: addr,
                local_addr,
                bytes_sent: 0,
                bytes_received: 0,
                packets_sent: 0,
                packets_received: 0,
                last_activity: now,
                status: ConnectionStatus::Active,
                status_changed_at: now,
                protocol_info: ProtocolConnectionInfo::empty(),
            };

            self.app_state
                .add_connection_to_server(server_id, conn_state)
                .await;
        }

        // Split (never clone) so the write half can be shared with the peer-command task.
        let (reader, write_half) = tokio::io::split(stream);
        let write_half = Arc::new(Mutex::new(write_half));
        let out = ConnWrite {
            write_half: write_half.clone(),
            app_state: self.app_state.clone(),
            server_id: self.server_id,
            connection_id,
        };

        // Peer messaging, registered BEFORE the first read. CQL is client-speaks-first and the
        // server says nothing until the driver sends OPTIONS/STARTUP, so a `*` manual rule
        // parks the very first event this connection produces - and the operator being asked to
        // decide about it must be able to reach, or hang up, the connection while it waits.
        if let Some(server_id) = self.server_id {
            let peer_rx = crate::server::peer_support::register_peer_channel(
                &self.app_state,
                server_id,
                connection_id.as_u32(),
            )
            .await;
            let protocol = Arc::new(CassandraProtocol::new(
                connection_id,
                self.app_state.clone(),
                status_tx.clone(),
            ));
            crate::server::peer_support::spawn_peer_command_task(
                peer_rx,
                protocol,
                self.app_state.clone(),
                server_id,
                connection_id.as_u32(),
                write_half.clone(),
                status_tx.clone(),
            );
        }

        let result = self
            .run_session(reader, &out, addr, connection_id, &status_tx)
            .await;

        // Every exit path lands here - EOF, a read error, an oversized frame, a handler's
        // close, an injected disconnect. Dropping the handle also ends the peer command task,
        // which releases its clone of the write half; the explicit shutdown makes the FIN
        // immediate rather than waiting on it.
        if let Some(server_id) = self.server_id {
            self.app_state
                .remove_peer_handle(server_id, connection_id.as_u32())
                .await;
        }
        let _ = write_half.lock().await.shutdown().await;

        // Close connection
        if let Some(server_id) = self.server_id {
            self.app_state
                .close_connection_on_server(server_id, connection_id)
                .await;
        }

        result
    }

    /// The read/parse/dispatch loop. Replies go out through `out`, never through the socket.
    async fn run_session(
        &self,
        mut reader: tokio::io::ReadHalf<TcpStream>,
        out: &ConnWrite,
        addr: SocketAddr,
        connection_id: ConnectionId,
        status_tx: &mpsc::UnboundedSender<String>,
    ) -> Result<()> {
        let mut conn_state = CassandraConnectionState {
            ready: false,
            protocol_version: 4,
            prepared_statements: HashMap::new(),
            authenticated: false,
            username: None,
        };

        let mut buffer = BytesMut::with_capacity(4096);
        // Set once a handler asks to close, so the outer read loop stops too. Breaking only
        // out of the inner frame loop left the connection open and re-entered read_buf, which
        // made `close_this_connection` a no-op on every path.
        let mut closing = false;

        loop {
            // The deadline wraps this `read_buf` and nothing else in the loop. The LLM
            // round-trip that answers a query, and a `manual` rule parking it for a human
            // (`src/state/intercepts.rs`, 300s by default), happen in the frame loop below,
            // after bytes have already arrived — so neither is inside a read deadline and
            // neither can be cut short by one. Only a peer that holds the connection while
            // sending nothing is bounded.
            let read_timeout = if conn_state.ready {
                IDLE_BETWEEN_FRAMES_TIMEOUT
            } else {
                FIRST_FRAME_READ_TIMEOUT
            };

            // Read data from the read half
            let read = match tokio::time::timeout(read_timeout, reader.read_buf(&mut buffer)).await
            {
                Ok(read) => read,
                Err(_) => {
                    Log::new(Some(status_tx)).debug(format!(
                        "Cassandra client {} sent nothing for {}s; closing idle connection",
                        addr,
                        read_timeout.as_secs()
                    ));
                    break;
                }
            };
            let n = match read {
                Ok(0) => {
                    Log::new(Some(status_tx))
                        .debug(format!("Cassandra client {} disconnected", addr));
                    break;
                }
                Ok(n) => n,
                Err(e) => {
                    error!("Read error from {}: {}", addr, e);
                    break;
                }
            };

            // Counted on every read, as `ConnWrite::write` counts every write. Nothing
            // updated these before, so a CQL connection's arrow counters never moved.
            if let Some(server_id) = self.server_id {
                self.app_state
                    .update_connection_stats(
                        server_id,
                        connection_id,
                        Some(n as u64),
                        None,
                        Some(1),
                        None,
                    )
                    .await;
            }

            trace!("Read {} bytes from Cassandra client {}", n, addr);

            // Try to parse and handle frames
            while buffer.remaining() >= 9 {
                // Check if we have a complete frame header (9 bytes)
                let frame_start = buffer.as_ref();
                if frame_start.len() < 9 {
                    break;
                }

                // Read frame length from header (bytes 5-8)
                let length = u32::from_be_bytes([
                    frame_start[5],
                    frame_start[6],
                    frame_start[7],
                    frame_start[8],
                ]) as usize;

                // The length is attacker-chosen and up to 4 GiB. Without this check the loop
                // simply waits for the declared bytes while read_buf keeps growing BytesMut,
                // so a client that declares a huge frame and then dribbles data grows the
                // process without limit. 256 MiB is the protocol's own maximum frame size.
                if length > MAX_FRAME_BODY_BYTES {
                    Log::new(Some(status_tx)).error(format!(
                        "Cassandra frame too large ({} bytes, limit {}), closing {}",
                        length, MAX_FRAME_BODY_BYTES, addr
                    ));
                    closing = true;
                    break;
                }

                // Check if we have the complete frame
                if buffer.remaining() < 9 + length {
                    trace!(
                        "Waiting for complete frame: have {}, need {}",
                        buffer.remaining(),
                        9 + length
                    );
                    break;
                }

                // We have a complete frame, parse it
                let frame_bytes = buffer.split_to(9 + length);

                match self
                    .handle_frame(&frame_bytes, &mut conn_state, out, connection_id, status_tx)
                    .await
                {
                    Ok(should_continue) => {
                        if !should_continue {
                            debug!("Closing Cassandra connection to {}", addr);
                            closing = true;
                            break;
                        }
                    }
                    Err(e) => {
                        // The comment here said "send error frame and close connection" and
                        // then closed without sending anything. A driver blocked on the reply
                        // to that stream id saw the socket vanish and reported a *transport*
                        // fault, so a truncated EXECUTE or a frame `Envelope::from_buffer`
                        // rejected killed the whole session with no diagnosis on either end.
                        //
                        // The stream id is at bytes 2..4 of the header and does not depend on
                        // the body parsing, so the reply reaches the right request even when
                        // nothing else about the frame could be understood. The peer gets the
                        // category only — a fixed string, never `e`, which carries anyhow
                        // context and library messages.
                        Log::new(Some(status_tx)).error(format!(
                            "Cassandra connection {} decision=fail_closed_protocol_error: {}",
                            connection_id, e
                        ));
                        let stream_id = i16::from_be_bytes([frame_bytes[2], frame_bytes[3]]);
                        if let Err(send_err) = self
                            .send_error(
                                stream_id,
                                CASSANDRA_ERROR_PROTOCOL,
                                "netget: malformed CQL frame",
                                out,
                                status_tx,
                            )
                            .await
                        {
                            debug!(
                                "Cassandra could not report the protocol error to {}: {}",
                                addr, send_err
                            );
                        }
                        closing = true;
                        break;
                    }
                }
            }

            if closing {
                break;
            }
        }

        Ok(())
    }

    /// Handle a single Cassandra frame
    async fn handle_frame(
        &self,
        frame_bytes: &[u8],
        conn_state: &mut CassandraConnectionState,
        out: &ConnWrite,
        connection_id: ConnectionId,
        status_tx: &mpsc::UnboundedSender<String>,
    ) -> Result<bool> {
        // Parse frame using cassandra-protocol
        let parsed = Envelope::from_buffer(frame_bytes, Compression::None)
            .context("Failed to parse Cassandra frame")?;
        let frame = parsed.envelope;

        Log::new(Some(status_tx)).trace(format!(
            "Cassandra ← {:?} stream={}",
            frame.opcode, frame.stream_id
        ));

        match frame.opcode {
            Opcode::Startup => {
                self.handle_startup(frame, conn_state, out, connection_id, status_tx)
                    .await
            }
            Opcode::Options => {
                self.handle_options(frame, out, connection_id, status_tx)
                    .await
            }
            Opcode::Query => {
                self.handle_query(frame, out, connection_id, status_tx)
                    .await
            }
            Opcode::Prepare => {
                self.handle_prepare(frame, conn_state, out, connection_id, status_tx)
                    .await
            }
            Opcode::Execute => {
                self.handle_execute(frame, conn_state, out, connection_id, status_tx)
                    .await
            }
            Opcode::AuthResponse => {
                self.handle_auth_response(frame, conn_state, out, connection_id, status_tx)
                    .await
            }
            Opcode::Register => {
                // Client wants to register for server events
                // We don't support server events, but respond with READY to acknowledge
                Log::new(Some(status_tx))
                    .debug("Cassandra: Client registered for events (not supported, no-op)");
                self.send_ready(frame.stream_id, out, status_tx).await?;
                Ok(true)
            }
            _ => {
                Log::new(Some(status_tx))
                    .warn(format!("Unsupported Cassandra opcode: {:?}", frame.opcode));
                // Send error response
                self.send_error(
                    frame.stream_id,
                    0x000A,
                    "Unsupported operation",
                    out,
                    status_tx,
                )
                .await?;
                Ok(true)
            }
        }
    }

    /// Handle STARTUP frame
    async fn handle_startup(
        &self,
        frame: Envelope,
        conn_state: &mut CassandraConnectionState,
        out: &ConnWrite,
        connection_id: ConnectionId,
        status_tx: &mpsc::UnboundedSender<String>,
    ) -> Result<bool> {
        debug!("Handling STARTUP from connection {}", connection_id);

        // Parse startup options (CQL_VERSION, etc.)
        // For Phase 1, we just accept any version and send READY

        // Call LLM to decide response
        let protocol =
            CassandraProtocol::new(connection_id, self.app_state.clone(), status_tx.clone());

        let event = Event {
            event_type: &CASSANDRA_STARTUP_EVENT,
            data: json!({
                "protocol_version": conn_state.protocol_version,
                "options": {"CQL_VERSION": "3.0.0"}
            }),
        };

        let server_id = self.server_id.context("Server ID not set")?;

        let execution_result = match call_llm(
            &self.llm_client,
            &self.app_state,
            server_id,
            Some(connection_id),
            &event,
            &protocol,
        )
        .await
        {
            Ok(result) => result,
            Err(e) => {
                self.send_llm_failure_error(
                    frame.stream_id,
                    "STARTUP",
                    &e,
                    out,
                    connection_id,
                    status_tx,
                )
                .await?;
                return Ok(true);
            }
        };

        // Show messages
        for message in &execution_result.messages {
            Log::new(Some(status_tx)).info(format!("{}", message));
        }

        // Execute the protocol actions
        for action_result in execution_result.protocol_results {
            match action_result {
                ActionResult::Custom { name, data } => match name.as_str() {
                    "cassandra_ready" => {
                        conn_state.ready = true;
                        self.send_ready(frame.stream_id, out, status_tx).await?;
                        return Ok(true);
                    }
                    // Answering STARTUP with AUTHENTICATE is the only way a driver is ever
                    // prompted for credentials. Without it the client goes straight to
                    // queries, so cassandra_auth never fired and cassandra_auth_success was
                    // unreachable - the whole "Phase 3" auth path was dead.
                    "cassandra_authenticate" => {
                        let authenticator = data
                            .get("authenticator")
                            .and_then(|v| v.as_str())
                            .unwrap_or("org.apache.cassandra.auth.PasswordAuthenticator");
                        self.send_authenticate(frame.stream_id, authenticator, out, status_tx)
                            .await?;
                        return Ok(true);
                    }
                    "cassandra_error" => {
                        let error_code = data
                            .get("error_code")
                            .and_then(|v| v.as_u64())
                            .unwrap_or(0x0000) as u32;
                        let message = data
                            .get("message")
                            .and_then(|v| v.as_str())
                            .unwrap_or("Unknown error");
                        self.send_model_error(
                            frame.stream_id,
                            "STARTUP",
                            error_code,
                            message,
                            out,
                            connection_id,
                            status_tx,
                        )
                        .await?;
                        return Ok(true);
                    }
                    _ => {}
                },
                ActionResult::CloseConnection => {
                    return Ok(false);
                }
                _ => {
                    warn!("Unexpected action result for STARTUP");
                }
            }
        }

        // Fail closed: a CQL session is granted by an explicit `cassandra_ready` or
        // `cassandra_authenticate` and by nothing else. Falling through to READY here handed
        // out an unauthenticated session on silence, so the model had no way to make a refusal
        // distinguishable from a backend outage.
        self.send_no_answer_error(frame.stream_id, "STARTUP", out, connection_id, status_tx)
            .await?;
        Ok(false)
    }

    /// Handle OPTIONS frame
    async fn handle_options(
        &self,
        frame: Envelope,
        out: &ConnWrite,
        connection_id: ConnectionId,
        status_tx: &mpsc::UnboundedSender<String>,
    ) -> Result<bool> {
        debug!("Handling OPTIONS from connection {}", connection_id);

        let protocol =
            CassandraProtocol::new(connection_id, self.app_state.clone(), status_tx.clone());

        let event = Event {
            event_type: &CASSANDRA_OPTIONS_EVENT,
            data: json!({}),
        };

        let server_id = self.server_id.context("Server ID not set")?;

        let execution_result = match call_llm(
            &self.llm_client,
            &self.app_state,
            server_id,
            Some(connection_id),
            &event,
            &protocol,
        )
        .await
        {
            Ok(result) => result,
            Err(e) => {
                self.send_llm_failure_error(
                    frame.stream_id,
                    "OPTIONS",
                    &e,
                    out,
                    connection_id,
                    status_tx,
                )
                .await?;
                return Ok(true);
            }
        };

        // Show messages
        for message in &execution_result.messages {
            Log::new(Some(status_tx)).info(format!("{}", message));
        }

        // Execute the protocol actions
        for action_result in execution_result.protocol_results {
            match action_result {
                ActionResult::Custom { name, data } => match name.as_str() {
                    "cassandra_supported" => {
                        let options = data
                            .get("options")
                            .and_then(|v| v.as_object())
                            .cloned()
                            .unwrap_or_default();
                        self.send_supported(frame.stream_id, options, out, status_tx)
                            .await?;
                        return Ok(true);
                    }
                    "cassandra_error" => {
                        let error_code = data
                            .get("error_code")
                            .and_then(|v| v.as_u64())
                            .unwrap_or(0x0000) as u32;
                        let message = data
                            .get("message")
                            .and_then(|v| v.as_str())
                            .unwrap_or("Unknown error");
                        self.send_model_error(
                            frame.stream_id,
                            "OPTIONS",
                            error_code,
                            message,
                            out,
                            connection_id,
                            status_tx,
                        )
                        .await?;
                        return Ok(true);
                    }
                    _ => {}
                },
                ActionResult::CloseConnection => {
                    return Ok(false);
                }
                _ => {
                    warn!("Unexpected action result for OPTIONS");
                }
            }
        }

        // If no action was executed, send default SUPPORTED
        self.send_supported(frame.stream_id, serde_json::Map::new(), out, status_tx)
            .await?;
        Ok(true)
    }

    /// Handle QUERY frame
    async fn handle_query(
        &self,
        frame: Envelope,
        out: &ConnWrite,
        connection_id: ConnectionId,
        status_tx: &mpsc::UnboundedSender<String>,
    ) -> Result<bool> {
        // Parse query from frame body
        let (query_str, consistency) = self.parse_query(&frame)?;

        Log::new(Some(status_tx)).debug(format!(
            "Cassandra ← Query from connection {}: {}",
            connection_id, query_str
        ));

        let protocol =
            CassandraProtocol::new(connection_id, self.app_state.clone(), status_tx.clone());

        let event = Event {
            event_type: &CASSANDRA_QUERY_EVENT,
            data: json!({
                "query": query_str,
                "consistency": consistency
            }),
        };

        let server_id = self.server_id.context("Server ID not set")?;

        let execution_result = match call_llm(
            &self.llm_client,
            &self.app_state,
            server_id,
            Some(connection_id),
            &event,
            &protocol,
        )
        .await
        {
            Ok(result) => result,
            Err(e) => {
                self.send_llm_failure_error(
                    frame.stream_id,
                    "QUERY",
                    &e,
                    out,
                    connection_id,
                    status_tx,
                )
                .await?;
                return Ok(true);
            }
        };

        // Show messages
        for message in &execution_result.messages {
            Log::new(Some(status_tx)).info(format!("{}", message));
        }

        // Execute the protocol actions
        for action_result in execution_result.protocol_results {
            match action_result {
                ActionResult::Custom { name, data } => match name.as_str() {
                    "cassandra_result_rows" => {
                        let columns = data
                            .get("columns")
                            .and_then(|v| v.as_array())
                            .cloned()
                            .unwrap_or_default();
                        let rows = data
                            .get("rows")
                            .and_then(|v| v.as_array())
                            .cloned()
                            .unwrap_or_default();
                        self.send_result_rows(frame.stream_id, columns, rows, out, status_tx)
                            .await?;
                        return Ok(true);
                    }
                    "cassandra_error" => {
                        let error_code = data
                            .get("error_code")
                            .and_then(|v| v.as_u64())
                            .unwrap_or(0x0000) as u32;
                        let message = data
                            .get("message")
                            .and_then(|v| v.as_str())
                            .unwrap_or("Unknown error");
                        self.send_model_error(
                            frame.stream_id,
                            "QUERY",
                            error_code,
                            message,
                            out,
                            connection_id,
                            status_tx,
                        )
                        .await?;
                        return Ok(true);
                    }
                    _ => {}
                },
                ActionResult::CloseConnection => {
                    return Ok(false);
                }
                _ => {
                    warn!("Unexpected action result for QUERY");
                }
            }
        }

        // Fail closed: an empty RESULT/Rows frame means "no rows matched", which is an
        // answer about the data. A missing handler answer is not that.
        self.send_no_answer_error(frame.stream_id, "QUERY", out, connection_id, status_tx)
            .await?;
        Ok(true)
    }

    /// Parse the query string and requested consistency level from a QUERY frame.
    ///
    /// Body layout (native protocol v4): `[long string] query`, then `<query_parameters>`,
    /// which begins `[short] consistency [byte] flags …`.
    ///
    /// The consistency level used to be reported to the handler as the literal `"ONE"` for
    /// every query, whatever the client asked for — a fabricated event field, and the one
    /// piece of a CQL request a handler might reasonably branch on ("refuse writes at ALL",
    /// "answer LOCAL_QUORUM from this replica set"). Two bytes off the wire say what the
    /// client actually requested.
    fn parse_query(&self, frame: &Envelope) -> Result<(String, &'static str)> {
        let body = &frame.body;
        if body.len() < 4 {
            return Err(anyhow::anyhow!("Query frame too short"));
        }

        // Read long string length (4 bytes, big-endian)
        let query_len = u32::from_be_bytes([body[0], body[1], body[2], body[3]]) as usize;

        // `4 + query_len` on a 32-bit target could wrap; compare the addends instead.
        if body.len().saturating_sub(4) < query_len {
            return Err(anyhow::anyhow!("Query frame truncated"));
        }

        let query_bytes = &body[4..4 + query_len];
        let query_str = String::from_utf8_lossy(query_bytes).to_string();

        // A QUERY frame always carries query_parameters, but a truncated one must not panic.
        // An absent or unrecognised level is reported as null rather than guessed at: the
        // handler is better served by "the client did not say" than by an invented ONE.
        let consistency = body
            .get(4 + query_len..6 + query_len)
            .map(|b| consistency_name(u16::from_be_bytes([b[0], b[1]])))
            .unwrap_or("UNKNOWN");

        Ok((query_str, consistency))
    }

    /// Send READY response
    async fn send_ready(
        &self,
        stream_id: i16,
        out: &ConnWrite,
        status_tx: &mpsc::UnboundedSender<String>,
    ) -> Result<()> {
        let response = Envelope {
            version: Version::V4,
            direction: Direction::Response,
            flags: Flags::empty(),
            stream_id: stream_id,
            opcode: Opcode::Ready,
            body: vec![],
            tracing_id: None,
            warnings: vec![],
        };

        let bytes = response.encode_with(Compression::None)?;
        out.write(&bytes).await?;

        Log::new(Some(status_tx)).trace(format!("Cassandra → READY ({} bytes)", bytes.len()));

        Ok(())
    }

    /// Send SUPPORTED response
    async fn send_supported(
        &self,
        stream_id: i16,
        options: serde_json::Map<String, serde_json::Value>,
        out: &ConnWrite,
        status_tx: &mpsc::UnboundedSender<String>,
    ) -> Result<()> {
        // Build SUPPORTED body: string multimap
        // For Phase 1, send minimal options
        let mut body = Vec::new();

        // Number of options (2 bytes)
        body.extend_from_slice(&(options.len() as u16).to_be_bytes());

        for (key, value) in options.iter() {
            // Key (string)
            let key_bytes = key.as_bytes();
            body.extend_from_slice(&(key_bytes.len() as u16).to_be_bytes());
            body.extend_from_slice(key_bytes);

            // Value list (string list)
            if let Some(arr) = value.as_array() {
                body.extend_from_slice(&(arr.len() as u16).to_be_bytes());
                for item in arr {
                    if let Some(s) = item.as_str() {
                        let s_bytes = s.as_bytes();
                        body.extend_from_slice(&(s_bytes.len() as u16).to_be_bytes());
                        body.extend_from_slice(s_bytes);
                    }
                }
            } else {
                // Empty list
                body.extend_from_slice(&0u16.to_be_bytes());
            }
        }

        let response = Envelope {
            version: Version::V4,
            direction: Direction::Response,
            flags: Flags::empty(),
            stream_id: stream_id,
            opcode: Opcode::Supported,
            body,
            tracing_id: None,
            warnings: vec![],
        };

        let bytes = response.encode_with(Compression::None)?;
        out.write(&bytes).await?;

        Log::new(Some(status_tx)).trace(format!("Cassandra → SUPPORTED ({} bytes)", bytes.len()));

        Ok(())
    }

    /// Send RESULT with rows
    async fn send_result_rows(
        &self,
        stream_id: i16,
        columns: Vec<serde_json::Value>,
        rows: Vec<serde_json::Value>,
        out: &ConnWrite,
        status_tx: &mpsc::UnboundedSender<String>,
    ) -> Result<()> {
        // Build RESULT body (kind=ROWS)
        // This is complex in Cassandra protocol - for Phase 1, we send a simplified response
        let mut body = Vec::new();

        // Result kind (4 bytes): 0x0002 = Rows
        body.extend_from_slice(&0x00000002u32.to_be_bytes());

        // Metadata
        // Flags (4 bytes): 0x0001 = GlobalTablesSpec
        body.extend_from_slice(&0x00000001u32.to_be_bytes());

        // Column count (4 bytes)
        body.extend_from_slice(&(columns.len() as u32).to_be_bytes());

        // Global keyspace and table (for simplicity)
        let keyspace = b"system";
        let table = b"local";
        body.extend_from_slice(&(keyspace.len() as u16).to_be_bytes());
        body.extend_from_slice(keyspace);
        body.extend_from_slice(&(table.len() as u16).to_be_bytes());
        body.extend_from_slice(table);

        // Column specs
        for col in &columns {
            let name = col.get("name").and_then(|v| v.as_str()).unwrap_or("col");
            let col_type = col
                .get("type")
                .and_then(|v| v.as_str())
                .unwrap_or("varchar");

            // Column name
            body.extend_from_slice(&(name.len() as u16).to_be_bytes());
            body.extend_from_slice(name.as_bytes());

            // Column type (simplified: 0x000D = varchar, 0x0009 = int)
            let type_code: u16 = match col_type {
                "int" => 0x0009,
                "boolean" => 0x0004,
                _ => 0x000D, // varchar
            };
            body.extend_from_slice(&type_code.to_be_bytes());
        }

        // Rows count (4 bytes)
        body.extend_from_slice(&(rows.len() as u32).to_be_bytes());

        // Row data.
        //
        // A driver reads exactly columns.len() cells per row and takes whatever follows as
        // the next row. A handler that returns a row with the wrong number of cells therefore
        // does not just mangle that row - it desynchronizes the rest of the result set. Rows
        // are padded with NULL and truncated to the declared column count so a wrong-arity
        // answer stays parseable.
        for row in &rows {
            let empty = Vec::new();
            let row_arr = row.as_array().unwrap_or(&empty);
            if row_arr.len() != columns.len() {
                warn!(
                    "Cassandra row has {} cell(s) but {} column(s) were declared; padding/truncating",
                    row_arr.len(),
                    columns.len()
                );
            }
            for i in 0..columns.len() {
                let col_type = columns[i].get("type").and_then(|v| v.as_str());
                let cell_bytes = row_arr
                    .get(i)
                    .and_then(|cell| self.serialize_cell_value(cell, col_type));

                if let Some(bytes) = cell_bytes {
                    body.extend_from_slice(&(bytes.len() as i32).to_be_bytes());
                    body.extend_from_slice(&bytes);
                } else {
                    // NULL value (-1)
                    body.extend_from_slice(&(-1i32).to_be_bytes());
                }
            }
        }

        let response = Envelope {
            version: Version::V4,
            direction: Direction::Response,
            flags: Flags::empty(),
            stream_id: stream_id,
            opcode: Opcode::Result,
            body,
            tracing_id: None,
            warnings: vec![],
        };

        let bytes = response.encode_with(Compression::None)?;
        out.write(&bytes).await?;

        Log::new(Some(status_tx)).trace(format!(
            "Cassandra → RESULT ({} rows, {} bytes)",
            rows.len(),
            bytes.len()
        ));

        Ok(())
    }

    /// Serialize a cell for the column type that was declared for it.
    ///
    /// The column type must drive the encoding, not the JSON type. The column spec on the wire
    /// tells the driver how to read the bytes, so a column declared `int` whose value arrives
    /// as the JSON string `"5"` has to go out as a 4-byte big-endian 5 - emitting the ASCII
    /// "5" made the driver read a 1-byte int and fail. This function previously ignored the
    /// column type entirely (the parameter was `_col_type`) and switched on the JSON type
    /// alone, so any handler that quoted a number, or answered a varchar column with a number,
    /// produced a result set the driver rejected.
    ///
    /// Returns `None` for a NULL cell.
    fn serialize_cell_value(
        &self,
        value: &serde_json::Value,
        col_type: Option<&str>,
    ) -> Option<Vec<u8>> {
        if value.is_null() {
            return None;
        }

        match col_type.unwrap_or("varchar") {
            "int" => {
                let n = match value {
                    serde_json::Value::Number(n) => n.as_i64(),
                    serde_json::Value::String(s) => s.trim().parse::<i64>().ok(),
                    serde_json::Value::Bool(b) => Some(if *b { 1 } else { 0 }),
                    _ => None,
                };
                match n {
                    // Out-of-range values are NULL rather than silently wrapped: a truncated
                    // integer is a wrong answer the client cannot detect.
                    Some(n) if n >= i32::MIN as i64 && n <= i32::MAX as i64 => {
                        Some((n as i32).to_be_bytes().to_vec())
                    }
                    _ => {
                        warn!("Cassandra int column got {:?}, sending NULL", value);
                        None
                    }
                }
            }
            "boolean" => {
                let b = match value {
                    serde_json::Value::Bool(b) => Some(*b),
                    serde_json::Value::Number(n) => n.as_i64().map(|n| n != 0),
                    serde_json::Value::String(s) => match s.trim().to_ascii_lowercase().as_str() {
                        "true" | "1" | "yes" => Some(true),
                        "false" | "0" | "no" => Some(false),
                        _ => None,
                    },
                    _ => None,
                };
                match b {
                    Some(b) => Some(vec![u8::from(b)]),
                    None => {
                        warn!("Cassandra boolean column got {:?}, sending NULL", value);
                        None
                    }
                }
            }
            // varchar/text and every unrecognized type are sent as UTF-8, matching the
            // 0x000D type code send_result_rows writes for them. Strings go out verbatim;
            // anything else is rendered rather than dropped.
            _ => match value {
                serde_json::Value::String(s) => Some(s.as_bytes().to_vec()),
                other => Some(other.to_string().into_bytes()),
            },
        }
    }

    /// Handle PREPARE frame
    async fn handle_prepare(
        &self,
        frame: Envelope,
        conn_state: &mut CassandraConnectionState,
        out: &ConnWrite,
        connection_id: ConnectionId,
        status_tx: &mpsc::UnboundedSender<String>,
    ) -> Result<bool> {
        debug!("Handling PREPARE from connection {}", connection_id);

        // Parse query from frame. A PREPARE body is a bare `[long string]` with no
        // query_parameters, so the consistency `parse_query` also returns is always UNKNOWN
        // here and is discarded: the level is chosen at EXECUTE, not at PREPARE.
        let (query, _consistency) = self.parse_query(&frame)?;
        trace!("PREPARE query: {}", query);

        // Generate statement ID from query hash
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};
        let mut hasher = DefaultHasher::new();
        query.hash(&mut hasher);
        let hash = hasher.finish();
        let statement_id = hash.to_be_bytes().to_vec();

        // Count parameters in query (simple heuristic: count '?' occurrences)
        let param_count = query.matches('?').count();

        // Store prepared statement
        if conn_state.prepared_statements.len() >= MAX_PREPARED_STATEMENTS
            && !conn_state.prepared_statements.contains_key(&statement_id)
        {
            warn!(
                "Cassandra connection {} reached {} prepared statements; rejecting PREPARE",
                connection_id, MAX_PREPARED_STATEMENTS
            );
            self.send_error(
                frame.stream_id,
                0x2200,
                "Too many prepared statements on this connection",
                out,
                status_tx,
            )
            .await?;
            return Ok(true);
        }
        conn_state
            .prepared_statements
            .insert(statement_id.clone(), (query.clone(), param_count));

        debug!(
            "Prepared statement ID {:?} with {} params",
            statement_id, param_count
        );

        // Call LLM to decide response
        let protocol =
            CassandraProtocol::new(connection_id, self.app_state.clone(), status_tx.clone());

        let event = Event {
            event_type: &CASSANDRA_PREPARE_EVENT,
            data: json!({
                "query": query,
                "statement_id": hex::encode(&statement_id),
                "param_count": param_count,
            }),
        };

        let server_id = self.server_id.context("Server ID not set")?;

        let execution_result = match call_llm(
            &self.llm_client,
            &self.app_state,
            server_id,
            Some(connection_id),
            &event,
            &protocol,
        )
        .await
        {
            Ok(result) => result,
            Err(e) => {
                self.send_llm_failure_error(
                    frame.stream_id,
                    "PREPARE",
                    &e,
                    out,
                    connection_id,
                    status_tx,
                )
                .await?;
                return Ok(true);
            }
        };

        // Show messages
        for message in &execution_result.messages {
            Log::new(Some(status_tx)).info(format!("{}", message));
        }

        // Execute the protocol actions
        for action_result in execution_result.protocol_results {
            match action_result {
                ActionResult::Custom { name, data } => match name.as_str() {
                    "cassandra_prepared" => {
                        let columns = data
                            .get("columns")
                            .and_then(|v| v.as_array())
                            .cloned()
                            .unwrap_or_default();
                        let params = data.get("params").and_then(|v| v.as_array()).cloned();
                        self.send_prepared(
                            frame.stream_id,
                            statement_id,
                            columns,
                            params,
                            param_count,
                            out,
                            status_tx,
                        )
                        .await?;
                        return Ok(true);
                    }
                    "cassandra_error" => {
                        let error_code = data
                            .get("error_code")
                            .and_then(|v| v.as_u64())
                            .unwrap_or(0x0000) as u32;
                        let message = data
                            .get("message")
                            .and_then(|v| v.as_str())
                            .unwrap_or("Unknown error");
                        self.send_model_error(
                            frame.stream_id,
                            "PREPARE",
                            error_code,
                            message,
                            out,
                            connection_id,
                            status_tx,
                        )
                        .await?;
                        return Ok(true);
                    }
                    _ => {}
                },
                ActionResult::CloseConnection => {
                    return Ok(false);
                }
                _ => {
                    warn!("Unexpected action result for PREPARE");
                }
            }
        }

        // Fail closed: handing back a valid statement id would let a later EXECUTE run off a
        // preparation nobody approved.
        self.send_no_answer_error(frame.stream_id, "PREPARE", out, connection_id, status_tx)
            .await?;
        Ok(true)
    }

    /// Handle EXECUTE frame
    async fn handle_execute(
        &self,
        frame: Envelope,
        conn_state: &mut CassandraConnectionState,
        out: &ConnWrite,
        connection_id: ConnectionId,
        status_tx: &mpsc::UnboundedSender<String>,
    ) -> Result<bool> {
        debug!("Handling EXECUTE from connection {}", connection_id);

        // Parse statement ID and parameters from frame
        let (statement_id, params, consistency) = self.parse_execute(&frame)?;

        // Look up prepared statement.
        //
        // This used to be `?`, so an id we do not hold propagated out of `handle_frame` and
        // the connection was dropped with nothing written. It is reachable in ordinary use:
        // ids are a hash of the query text and are held *per connection*, so a driver
        // executing on a pooled connection that never saw the PREPARE lands here, as does one
        // reconnecting after a restart. CQL has an error for exactly this — UNPREPARED (0x2500)
        // carrying the unknown id — and drivers act on it: scylla re-PREPAREs and retries, so
        // the statement then succeeds instead of the session dying.
        let Some((query, expected_param_count)) = conn_state
            .prepared_statements
            .get(&statement_id)
            .map(|(q, n)| (q.clone(), *n))
        else {
            Log::new(Some(status_tx)).warn(format!(
                "Cassandra connection {} decision=unprepared stage=EXECUTE: statement id {} is \
                 not prepared on this connection; answering ERROR 0x{:04X} so the driver \
                 re-prepares",
                connection_id,
                hex::encode(&statement_id),
                CASSANDRA_ERROR_UNPREPARED
            ));
            self.send_unprepared_error(frame.stream_id, &statement_id, out, status_tx)
                .await?;
            return Ok(true);
        };

        trace!("EXECUTE statement: {} with {} params", query, params.len());

        // Validate parameter count
        if params.len() != expected_param_count {
            let err_msg = format!(
                "Expected {} parameters, got {}",
                expected_param_count,
                params.len()
            );
            self.send_error(frame.stream_id, 0x2200, &err_msg, out, status_tx)
                .await?;
            return Ok(true);
        }

        // Call LLM with query and bound parameters
        let protocol =
            CassandraProtocol::new(connection_id, self.app_state.clone(), status_tx.clone());

        let event = Event {
            event_type: &CASSANDRA_EXECUTE_EVENT,
            data: json!({
                "query": query,
                "statement_id": hex::encode(&statement_id),
                "parameters": params,
                "consistency": consistency,
            }),
        };

        let server_id = self.server_id.context("Server ID not set")?;

        let execution_result = match call_llm(
            &self.llm_client,
            &self.app_state,
            server_id,
            Some(connection_id),
            &event,
            &protocol,
        )
        .await
        {
            Ok(result) => result,
            Err(e) => {
                self.send_llm_failure_error(
                    frame.stream_id,
                    "EXECUTE",
                    &e,
                    out,
                    connection_id,
                    status_tx,
                )
                .await?;
                return Ok(true);
            }
        };

        // Show messages
        for message in &execution_result.messages {
            Log::new(Some(status_tx)).info(format!("{}", message));
        }

        // Execute the protocol actions
        for action_result in execution_result.protocol_results {
            match action_result {
                ActionResult::Custom { name, data } => match name.as_str() {
                    "cassandra_result_rows" => {
                        let columns = data
                            .get("columns")
                            .and_then(|v| v.as_array())
                            .cloned()
                            .unwrap_or_default();
                        let rows = data
                            .get("rows")
                            .and_then(|v| v.as_array())
                            .cloned()
                            .unwrap_or_default();
                        self.send_result_rows(frame.stream_id, columns, rows, out, status_tx)
                            .await?;
                        return Ok(true);
                    }
                    "cassandra_error" => {
                        let error_code = data
                            .get("error_code")
                            .and_then(|v| v.as_u64())
                            .unwrap_or(0x0000) as u32;
                        let message = data
                            .get("message")
                            .and_then(|v| v.as_str())
                            .unwrap_or("Unknown error");
                        self.send_model_error(
                            frame.stream_id,
                            "EXECUTE",
                            error_code,
                            message,
                            out,
                            connection_id,
                            status_tx,
                        )
                        .await?;
                        return Ok(true);
                    }
                    _ => {}
                },
                ActionResult::CloseConnection => {
                    return Ok(false);
                }
                _ => {
                    warn!("Unexpected action result for EXECUTE");
                }
            }
        }

        // Fail closed: an empty RESULT/Rows frame means "no rows matched", which is an
        // answer about the data. A missing handler answer is not that.
        self.send_no_answer_error(frame.stream_id, "EXECUTE", out, connection_id, status_tx)
            .await?;
        Ok(true)
    }

    /// Parse statement ID and parameters from EXECUTE frame
    fn parse_execute(
        &self,
        frame: &Envelope,
    ) -> Result<(Vec<u8>, Vec<serde_json::Value>, &'static str)> {
        let body = &frame.body;
        if body.len() < 2 {
            return Err(anyhow::anyhow!("EXECUTE frame too short"));
        }

        // Read statement ID (short bytes)
        let id_len = u16::from_be_bytes([body[0], body[1]]) as usize;
        if body.len() < 2 + id_len {
            return Err(anyhow::anyhow!("EXECUTE frame truncated (statement ID)"));
        }

        let statement_id = body[2..2 + id_len].to_vec();
        let mut offset = 2 + id_len;

        // Consistency level (2 bytes). These bytes were read and thrown away under a comment
        // saying "skip", so the handler was told nothing about what durability the client
        // asked for — the same gap `parse_query` had, where the answer was hardcoded to ONE.
        if body.len() < offset + 2 {
            return Err(anyhow::anyhow!("EXECUTE frame truncated (consistency)"));
        }
        let consistency = consistency_name(u16::from_be_bytes([body[offset], body[offset + 1]]));
        offset += 2;

        // Skip flags (1 byte)
        if body.len() < offset + 1 {
            return Err(anyhow::anyhow!("EXECUTE frame truncated (flags)"));
        }
        let flags = body[offset];
        offset += 1;

        let mut params = Vec::new();

        // If VALUES flag is set, parse parameter values
        if flags & 0x01 != 0 {
            // Read parameter count (2 bytes)
            if body.len() < offset + 2 {
                return Err(anyhow::anyhow!("EXECUTE frame truncated (param count)"));
            }
            let param_count = u16::from_be_bytes([body[offset], body[offset + 1]]) as usize;
            offset += 2;

            // Parse each parameter (bytes or null)
            for _ in 0..param_count {
                if body.len() < offset + 4 {
                    return Err(anyhow::anyhow!("EXECUTE frame truncated (param length)"));
                }

                let param_len = i32::from_be_bytes([
                    body[offset],
                    body[offset + 1],
                    body[offset + 2],
                    body[offset + 3],
                ]);
                offset += 4;

                if param_len < 0 {
                    // Null value
                    params.push(serde_json::Value::Null);
                } else {
                    let param_len = param_len as usize;
                    if body.len() < offset + param_len {
                        return Err(anyhow::anyhow!("EXECUTE frame truncated (param data)"));
                    }

                    let param_bytes = &body[offset..offset + param_len];
                    // For Phase 2, treat all params as strings
                    let param_str = String::from_utf8_lossy(param_bytes).to_string();
                    params.push(json!(param_str));
                    offset += param_len;
                }
            }
        }

        Ok((statement_id, params, consistency))
    }

    /// Send RESULT (Prepared) response
    async fn send_prepared(
        &self,
        stream_id: i16,
        statement_id: Vec<u8>,
        columns: Vec<serde_json::Value>,
        params: Option<Vec<serde_json::Value>>,
        param_count: usize,
        out: &ConnWrite,
        status_tx: &mpsc::UnboundedSender<String>,
    ) -> Result<()> {
        let mut body = Vec::new();

        // Result kind: Prepared (0x0004)
        body.extend_from_slice(&0x00000004u32.to_be_bytes());

        // Statement ID (short bytes)
        body.extend_from_slice(&(statement_id.len() as u16).to_be_bytes());
        body.extend_from_slice(&statement_id);

        // FIRST: Metadata for bound variables (parameters) - describes bind markers for EXECUTE
        let keyspace = b"netget";
        let table = b"data";

        // Flags: 0x0001 (GLOBAL_TABLES_SPEC)
        body.extend_from_slice(&0x00000001u32.to_be_bytes());

        // Parameter count
        body.extend_from_slice(&(param_count as u32).to_be_bytes());

        // Partition key count (protocol v4+)
        body.extend_from_slice(&0u32.to_be_bytes()); // No PK indexes (token routing not supported)
                                                     // Note: pk_indexes would follow here if pk_count > 0

        // Global keyspace and table for parameters
        body.extend_from_slice(&(keyspace.len() as u16).to_be_bytes());
        body.extend_from_slice(keyspace);
        body.extend_from_slice(&(table.len() as u16).to_be_bytes());
        body.extend_from_slice(table);

        // Parameter specifications
        for i in 0..param_count {
            let param_name = format!("param{}", i);
            let param_bytes = param_name.as_bytes();
            body.extend_from_slice(&(param_bytes.len() as u16).to_be_bytes());
            body.extend_from_slice(param_bytes);

            // Get type from params array if available, otherwise default to varchar
            let param_type = params
                .as_ref()
                .and_then(|p| p.get(i))
                .and_then(|param| param.get("type"))
                .and_then(|v| v.as_str())
                .unwrap_or("varchar");

            // Type code
            let type_code: u16 = match param_type {
                "int" => 0x0009,
                "varchar" | "text" => 0x000D,
                "boolean" => 0x0004,
                _ => 0x000D, // Default to varchar
            };
            body.extend_from_slice(&type_code.to_be_bytes());
        }

        // SECOND: Result metadata - describes columns returned when this statement is executed
        // Uses ROWS metadata format (Section 4.2.5.2) - does NOT include pk_count
        // Flags: 0x0001 (GLOBAL_TABLES_SPEC)
        body.extend_from_slice(&0x00000001u32.to_be_bytes());

        // Column count
        body.extend_from_slice(&(columns.len() as u32).to_be_bytes());

        // NOTE: No pk_count field in result metadata (only in parameters metadata)

        // Global keyspace and table
        body.extend_from_slice(&(keyspace.len() as u16).to_be_bytes());
        body.extend_from_slice(keyspace);
        body.extend_from_slice(&(table.len() as u16).to_be_bytes());
        body.extend_from_slice(table);

        // Column specifications
        for col in columns {
            let name = col.get("name").and_then(|v| v.as_str()).unwrap_or("col");
            let col_type = col
                .get("type")
                .and_then(|v| v.as_str())
                .unwrap_or("varchar");

            // Column name
            let name_bytes = name.as_bytes();
            body.extend_from_slice(&(name_bytes.len() as u16).to_be_bytes());
            body.extend_from_slice(name_bytes);

            // Column type (simple types only for Phase 2)
            let type_code: u16 = match col_type {
                "int" => 0x0009,
                "varchar" | "text" => 0x000D,
                "boolean" => 0x0004,
                _ => 0x000D, // Default to varchar
            };
            body.extend_from_slice(&type_code.to_be_bytes());
        }

        trace!(
            "PREPARED response body hex (first 200 bytes): {}",
            hex::encode(&body[..std::cmp::min(200, body.len())])
        );

        let response = Envelope {
            version: Version::V4,
            direction: Direction::Response,
            flags: Flags::empty(),
            stream_id: stream_id,
            opcode: Opcode::Result,
            body,
            tracing_id: None,
            warnings: vec![],
        };

        let bytes = response.encode_with(Compression::None)?;
        out.write(&bytes).await?;

        Log::new(Some(status_tx)).trace(format!(
            "Cassandra → RESULT (Prepared: {} params, {} bytes)",
            param_count,
            bytes.len()
        ));

        Ok(())
    }

    /// Send the ERROR frame that answers a failed LLM call.
    ///
    /// The native protocol has an ERROR opcode with a numeric code, and a driver surfaces it as
    /// an exception on the exact request that produced it - so unlike silence it cannot be
    /// mistaken for a slow query, and unlike a RESULT frame it cannot be mistaken for an empty
    /// result set, which in CQL means "no rows matched".
    ///
    /// 0x1001 `Overloaded` is the protocol's own "the coordinator cannot take this right now",
    /// and every driver treats it as retryable - so capacity exhaustion gets a signal the
    /// client already knows how to act on. Everything else is 0x0000 `Server error`.
    ///
    /// Note what is *not* used for the AUTH_RESPONSE stage: 0x0100 `Bad credentials`. The
    /// credentials were never examined, and saying they were bad both misattributes the
    /// failure and would make a driver stop retrying with correct ones. An ERROR frame of any
    /// code is a refusal - AUTH_SUCCESS is the only thing that authenticates - so this stays
    /// fail-closed either way.
    async fn send_llm_failure_error(
        &self,
        stream_id: i16,
        stage: &str,
        err: &anyhow::Error,
        out: &ConnWrite,
        connection_id: ConnectionId,
        status_tx: &mpsc::UnboundedSender<String>,
    ) -> Result<()> {
        let overloaded = crate::llm::is_overload_error(err);
        let (code, label) = if overloaded {
            (CASSANDRA_ERROR_OVERLOADED, "Overloaded")
        } else {
            (CASSANDRA_ERROR_SERVER_ERROR, "Server error")
        };
        let message = crate::utils::WireFailure::classify(err).prefixed_text();
        Log::new(Some(status_tx)).warn(format!(
            "Cassandra connection {} answering {} with ERROR 0x{:04X} ({}): {}",
            connection_id, stage, code, label, message
        ));
        self.send_error(stream_id, code, &message, out, status_tx)
            .await
    }

    /// Answer a stage the handler left unanswered.
    ///
    /// A no-answer is not permission. Silence used to fall through to the success frame for
    /// the stage - READY for STARTUP, an empty RESULT/Rows for QUERY and EXECUTE, a valid
    /// statement id for PREPARE - so an LLM outage, a handler that returned nothing, and a
    /// model that deliberately refused were indistinguishable on the wire, and two of the
    /// three granted the client exactly what it asked for.
    ///
    /// Every success frame here has to be produced by an explicit action; nothing synthesises
    /// one. An empty Rows result in particular is a statement about the data ("no rows
    /// matched") and must never be manufactured by a failure.
    ///
    /// The log carries `decision=fail_closed_no_answer`, distinct from
    /// `decision=fail_closed_llm_error` (backend failed) and `decision=model_reject` (the
    /// handler asked for an ERROR), because the CQL wire cannot tell the three apart.
    async fn send_no_answer_error(
        &self,
        stream_id: i16,
        stage: &str,
        out: &ConnWrite,
        connection_id: ConnectionId,
        status_tx: &mpsc::UnboundedSender<String>,
    ) -> Result<()> {
        Log::new(Some(status_tx)).warn(format!(
            "Cassandra connection {} decision=fail_closed_no_answer stage={}: handler produced \
             no recognised action, answering ERROR 0x{:04X} (Server error)",
            connection_id, stage, CASSANDRA_ERROR_SERVER_ERROR
        ));
        self.send_error(
            stream_id,
            CASSANDRA_ERROR_SERVER_ERROR,
            "netget: no handler answer for this request",
            out,
            status_tx,
        )
        .await
    }

    /// Send the ERROR frame a handler explicitly asked for.
    ///
    /// Logged as `decision=model_reject` so a deliberate refusal is separable from the two
    /// failure paths above, which reach the same opcode.
    async fn send_model_error(
        &self,
        stream_id: i16,
        stage: &str,
        error_code: u32,
        message: &str,
        out: &ConnWrite,
        connection_id: ConnectionId,
        status_tx: &mpsc::UnboundedSender<String>,
    ) -> Result<()> {
        Log::new(Some(status_tx)).info(format!(
            "Cassandra connection {} decision=model_reject stage={}: ERROR 0x{:04X}",
            connection_id, stage, error_code
        ));
        self.send_error(stream_id, error_code, message, out, status_tx)
            .await
    }

    /// Send ERROR 0x2500 UNPREPARED, whose body carries the id the driver must re-prepare.
    ///
    /// UNPREPARED is the one native-protocol error with a payload after the message:
    /// `[int code][string message][short bytes id]`. Omitting the id is not a smaller version
    /// of the same answer — a driver reads the trailing bytes unconditionally and treats a
    /// missing id as a framing fault, so it must be written.
    async fn send_unprepared_error(
        &self,
        stream_id: i16,
        statement_id: &[u8],
        out: &ConnWrite,
        status_tx: &mpsc::UnboundedSender<String>,
    ) -> Result<()> {
        let message = "netget: statement not prepared on this connection";
        let mut body = Vec::new();
        body.extend_from_slice(&CASSANDRA_ERROR_UNPREPARED.to_be_bytes());
        body.extend_from_slice(&(message.len() as u16).to_be_bytes());
        body.extend_from_slice(message.as_bytes());
        // [short bytes]: a 2-byte length then the id. Ids come from `send_prepared`, which
        // writes a 16-byte hash, so the cast cannot truncate in practice; clamp anyway rather
        // than emit a length that disagrees with what follows it.
        let id_len = u16::try_from(statement_id.len()).unwrap_or(u16::MAX) as usize;
        body.extend_from_slice(&(id_len as u16).to_be_bytes());
        body.extend_from_slice(&statement_id[..id_len]);

        let response = Envelope {
            version: Version::V4,
            direction: Direction::Response,
            flags: Flags::empty(),
            stream_id,
            opcode: Opcode::Error,
            body,
            tracing_id: None,
            warnings: vec![],
        };

        let bytes = response.encode_with(Compression::None)?;
        out.write(&bytes).await?;

        Log::new(Some(status_tx)).trace(format!(
            "Cassandra → ERROR 0x{:04X} UNPREPARED id={}",
            CASSANDRA_ERROR_UNPREPARED,
            hex::encode(statement_id)
        ));

        Ok(())
    }

    /// Send ERROR response
    async fn send_error(
        &self,
        stream_id: i16,
        error_code: u32,
        message: &str,
        out: &ConnWrite,
        status_tx: &mpsc::UnboundedSender<String>,
    ) -> Result<()> {
        let mut body = Vec::new();

        // Error code (4 bytes)
        body.extend_from_slice(&error_code.to_be_bytes());

        // Error message (string)
        let msg_bytes = message.as_bytes();
        body.extend_from_slice(&(msg_bytes.len() as u16).to_be_bytes());
        body.extend_from_slice(msg_bytes);

        let response = Envelope {
            version: Version::V4,
            direction: Direction::Response,
            flags: Flags::empty(),
            stream_id: stream_id,
            opcode: Opcode::Error,
            body,
            tracing_id: None,
            warnings: vec![],
        };

        let bytes = response.encode_with(Compression::None)?;
        out.write(&bytes).await?;

        Log::new(Some(status_tx)).trace(format!(
            "Cassandra → ERROR 0x{:04X} - {}",
            error_code, message
        ));

        Ok(())
    }

    /// Handle AUTH_RESPONSE frame (Phase 3)
    async fn handle_auth_response(
        &self,
        frame: Envelope,
        conn_state: &mut CassandraConnectionState,
        out: &ConnWrite,
        connection_id: ConnectionId,
        status_tx: &mpsc::UnboundedSender<String>,
    ) -> Result<bool> {
        debug!("Handling AUTH_RESPONSE from connection {}", connection_id);

        // Parse credentials from frame body (SASL PLAIN format: \0username\0password)
        let body = &frame.body;

        // Extract username and password from SASL PLAIN format
        let (username, password) = self.parse_sasl_plain(body)?;

        trace!("AUTH_RESPONSE: username={}", username);

        // Call LLM to decide whether to accept authentication
        let protocol =
            CassandraProtocol::new(connection_id, self.app_state.clone(), status_tx.clone());

        let event = Event {
            event_type: &CASSANDRA_AUTH_EVENT,
            data: json!({
                "username": username,
                "password": password,
            }),
        };

        let server_id = self.server_id.context("Server ID not set")?;

        let execution_result = match call_llm(
            &self.llm_client,
            &self.app_state,
            server_id,
            Some(connection_id),
            &event,
            &protocol,
        )
        .await
        {
            Ok(result) => result,
            Err(e) => {
                self.send_llm_failure_error(
                    frame.stream_id,
                    "AUTH_RESPONSE",
                    &e,
                    out,
                    connection_id,
                    status_tx,
                )
                .await?;
                return Ok(true);
            }
        };

        // Show messages
        for message in &execution_result.messages {
            Log::new(Some(status_tx)).info(format!("{}", message));
        }

        // Execute the protocol actions
        for action_result in execution_result.protocol_results {
            match action_result {
                ActionResult::Custom { name, data } => {
                    match name.as_str() {
                        "cassandra_auth_success" => {
                            conn_state.authenticated = true;
                            conn_state.username = Some(username);
                            self.send_auth_success(frame.stream_id, out, status_tx)
                                .await?;
                            return Ok(true);
                        }
                        "cassandra_error" => {
                            let error_code =
                                data.get("error_code")
                                    .and_then(|v| v.as_u64())
                                    .unwrap_or(0x0000) as u32;
                            let message = data
                                .get("message")
                                .and_then(|v| v.as_str())
                                .unwrap_or("Unknown error");
                            self.send_model_error(
                                frame.stream_id,
                                "AUTH_RESPONSE",
                                error_code,
                                message,
                                out,
                                connection_id,
                                status_tx,
                            )
                            .await?;
                            return Ok(false); // Close connection on auth failure
                        }
                        _ => {}
                    }
                }
                ActionResult::CloseConnection => {
                    return Ok(false);
                }
                _ => {
                    warn!("Unexpected action result for AUTH_RESPONSE");
                }
            }
        }

        // Default: deny authentication
        self.send_error(
            frame.stream_id,
            0x0100,
            "Authentication failed",
            out,
            status_tx,
        )
        .await?;
        Ok(false)
    }

    /// Parse SASL PLAIN credentials (format: \0username\0password)
    fn parse_sasl_plain(&self, body: &[u8]) -> Result<(String, String)> {
        if body.is_empty() {
            return Err(anyhow::anyhow!("Empty AUTH_RESPONSE body"));
        }

        // Skip optional authorization identity (first \0-terminated string)
        let mut idx = 0;
        while idx < body.len() && body[idx] != 0 {
            idx += 1;
        }
        idx += 1; // Skip the \0

        if idx >= body.len() {
            return Err(anyhow::anyhow!("Invalid SASL PLAIN format"));
        }

        // Extract username
        let username_start = idx;
        while idx < body.len() && body[idx] != 0 {
            idx += 1;
        }
        let username = String::from_utf8_lossy(&body[username_start..idx]).to_string();
        idx += 1; // Skip the \0

        if idx >= body.len() {
            return Err(anyhow::anyhow!(
                "Invalid SASL PLAIN format - missing password"
            ));
        }

        // Extract password
        let password = String::from_utf8_lossy(&body[idx..]).to_string();

        Ok((username, password))
    }

    /// Send AUTHENTICATE response (request authentication)
    async fn send_authenticate(
        &self,
        stream_id: i16,
        authenticator: &str,
        out: &ConnWrite,
        status_tx: &mpsc::UnboundedSender<String>,
    ) -> Result<()> {
        let mut body = Vec::new();

        // Authenticator name (string)
        let auth_bytes = authenticator.as_bytes();
        body.extend_from_slice(&(auth_bytes.len() as u16).to_be_bytes());
        body.extend_from_slice(auth_bytes);

        let response = Envelope {
            version: Version::V4,
            direction: Direction::Response,
            flags: Flags::empty(),
            stream_id: stream_id,
            opcode: Opcode::Authenticate,
            body,
            tracing_id: None,
            warnings: vec![],
        };

        let bytes = response.encode_with(Compression::None)?;
        out.write(&bytes).await?;

        Log::new(Some(status_tx)).trace(format!("Cassandra → AUTHENTICATE ({})", authenticator));

        Ok(())
    }

    /// Send AUTH_SUCCESS response
    async fn send_auth_success(
        &self,
        stream_id: i16,
        out: &ConnWrite,
        status_tx: &mpsc::UnboundedSender<String>,
    ) -> Result<()> {
        // AUTH_SUCCESS body can contain optional token (we send empty for SASL PLAIN)
        let body = vec![];

        let response = Envelope {
            version: Version::V4,
            direction: Direction::Response,
            flags: Flags::empty(),
            stream_id: stream_id,
            opcode: Opcode::AuthSuccess,
            body,
            tracing_id: None,
            warnings: vec![],
        };

        let bytes = response.encode_with(Compression::None)?;
        out.write(&bytes).await?;

        Log::new(Some(status_tx)).trace("Cassandra → AUTH_SUCCESS");

        Ok(())
    }
}