ramvamp-server 0.1.1

OpenAI-compatible HTTP server for the ramvamp runtime: request/response shapes, SSE streaming, prompt assembly, prefix-cached generation, and error mapping
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
//! The HTTP transport: one thread, one socket, one request at a time.
//!
//! # Loopback only, and not configurable
//!
//! The listener binds `127.0.0.1` and there is no flag to change it. That is a
//! security decision, not an oversight: `tiny_http`'s `read_next_line` has
//! neither a header-size cap nor a read timeout, so a single connection
//! sending an endless header line grows a `Vec` until the process dies — and
//! this process is meant to run inside a `memory.max=3G` cgroup next to a 30B
//! model, where "grows a `Vec`" means the OOM killer takes the model with it.
//! Loopback is the mitigation. Anyone who wants this reachable from elsewhere
//! should put a real reverse proxy in front of it, which is also where request
//! limits, TLS and authentication belong.
//!
//! # Concurrency is one, structurally
//!
//! There is no semaphore and no worker pool. One thread calls `Server::recv`
//! in a loop and handles each request inline, which *is* the concurrency
//! limit: a request that arrives mid-generation waits in the accept queue
//! until the current one finishes. That is the desired behaviour, and it is
//! why [`ServerError::AtCapacity`] is never returned here.
//!
//! Rejecting on arrival would be much worse than it looks. The OpenAI SDKs
//! default to `max_retries = 2` with 0.5 s and 1 s backoff, so a client burns
//! all three attempts in about 1.5 s — against a request that occupies the
//! model for minutes. An immediate 503 therefore guarantees a hard failure,
//! where queueing guarantees an answer. `AtCapacity` stays in the error enum
//! for a future build that adds a *bounded* queue and can overflow it.

use std::io::{Read, Write};
use std::net::{Ipv4Addr, SocketAddr};
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
use std::thread::JoinHandle;
use std::time::Duration;

use thiserror::Error;
use tiny_http::{Header, Method, Request, Response, Server};

use crate::engine::{Engine, NullSink, Plan, StreamError, TokenSink, hush_stream_aborts, unix_now};
use crate::error::ServerError;
use crate::request::ChatCompletionRequest;
use crate::response::{
    COMPLETION_ID_PREFIX, ChatCompletion, ChunkBuilder, FinishReason, Health, ModelList,
    ResponseMessage,
};
use crate::sse::{sse_data, sse_done, sse_event};
use crate::toolcall::{CallStream, Step, wire_call};
use crate::wire::{KEEPALIVE, StreamState};

/// `POST` this to generate.
pub const PATH_CHAT_COMPLETIONS: &str = "/v1/chat/completions";
/// `GET` this for the one-model listing.
pub const PATH_MODELS: &str = "/v1/models";
/// `GET` this for a liveness probe.
pub const PATH_HEALTH: &str = "/health";

/// How the server was started.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ServeConfig {
    /// TCP port on `127.0.0.1`. `0` binds an ephemeral port.
    pub port: u16,
    /// How long a stream may go silent before a keep-alive comment is sent.
    ///
    /// Every client that matters gives up after 300 s of no bytes — undici's
    /// `bodyTimeout`, the `openai` provider's `headerTimeout`, and
    /// `CLAUDE_CODE_BYTE_STREAM_IDLE_TIMEOUT_MS` all land on the same number —
    /// and a 4K prompt takes about six minutes to prefill. Ten seconds is
    /// thirty times under the wall and costs fourteen bytes a tick.
    pub keepalive: Duration,
    /// Largest request body this server will buffer, in bytes.
    pub max_body_bytes: usize,
}

impl Default for ServeConfig {
    fn default() -> Self {
        ServeConfig {
            port: 8080,
            keepalive: Duration::from_secs(10),
            // Derived from the default 4K context, which is what it has always
            // worked out to: 1 MiB. `ramvamp serve` overrides this from the
            // window it actually resolved `--context` to.
            max_body_bytes: max_body_bytes_for_context(DEFAULT_CONTEXT),
        }
    }
}

/// The context window [`ServeConfig::default`] sizes itself against.
///
/// The CLI owns the real default and passes the resolved window in; this is
/// only what a library caller who sets nothing gets, and it is the same 4096.
pub const DEFAULT_CONTEXT: usize = 4096;

/// Request-body bytes allowed per token of context.
///
/// A prompt runs about 4 bytes of text per token, so 256 is sixty-four times
/// the text a completely full window can hold — headroom for JSON escaping,
/// for the `tools` array, and for the whitespace a pretty-printing client
/// sends. It is also small against a 3 GB budget at any window that budget can
/// host: a 32K context allows 8 MiB.
pub const BODY_BYTES_PER_CONTEXT_TOKEN: usize = 256;

/// Floor under [`max_body_bytes_for_context`].
///
/// The JSON envelope — role names, tool-call ids, the `model` string, the
/// framing — does not shrink with the context, so a deliberately tiny window
/// must not produce a cap that a well-formed request cannot fit inside.
pub const MIN_BODY_BYTES: usize = 64 << 10;

/// The request-body cap for a server configured with a `context`-token window.
///
/// This used to be a flat 1 MiB, justified by "a 4K context's whole prompt is
/// on the order of 16 KB of text". That reasoning does not survive `--context`
/// being a dial: an agent client that sends tool schemas against a 16K window
/// writes a request that approaches the old constant, and would have been
/// answered with a 413 for asking for exactly what the server was configured
/// to allow. Deriving it means the cap moves with the window instead.
///
/// Saturating, and floored at [`MIN_BODY_BYTES`], so no window produces a cap
/// that wraps or one too small to hold an envelope.
#[must_use]
pub fn max_body_bytes_for_context(context: usize) -> usize {
    context
        .saturating_mul(BODY_BYTES_PER_CONTEXT_TOKEN)
        .max(MIN_BODY_BYTES)
}

/// Why the server could not run. Per-request failures are [`ServerError`]s and
/// are answered, not returned.
#[derive(Debug, Error)]
pub enum ServeError {
    /// The port could not be bound.
    #[error("binding 127.0.0.1:{port}: {message}")]
    Bind {
        /// The port that was asked for.
        port: u16,
        /// What the listener reported.
        message: String,
    },
    /// The accept loop broke.
    #[error("accepting connections: {0}")]
    Accept(#[from] std::io::Error),
}

/// Serve until the process ends.
///
/// # Errors
///
/// [`ServeError::Bind`] if the port is taken, [`ServeError::Accept`] if the
/// listener dies. Everything a client can cause is answered as HTTP.
pub fn serve(engine: &mut dyn Engine, config: ServeConfig) -> Result<(), ServeError> {
    serve_with(engine, config, |_| {})
}

/// [`serve`], reporting the bound address before the first accept.
///
/// The hook exists so a test can bind port 0 and find out where it landed;
/// nothing in production passes anything but a no-op.
///
/// # Errors
///
/// Exactly [`serve`]'s.
pub fn serve_with(
    engine: &mut dyn Engine,
    config: ServeConfig,
    ready: impl FnOnce(SocketAddr),
) -> Result<(), ServeError> {
    hush_stream_aborts();
    // Loopback, always. See the module docs: there is no flag for this and
    // there must not be one until the header reader has a cap.
    let bind = SocketAddr::from((Ipv4Addr::LOCALHOST, config.port));
    let server = Server::http(bind).map_err(|e| ServeError::Bind {
        port: config.port,
        message: e.to_string(),
    })?;
    let address = match server.server_addr().to_ip() {
        Some(address) => address,
        None => bind,
    };
    tracing::info!(%address, model = engine.model_id(), "listening");
    ready(address);

    loop {
        let request = server.recv()?;
        handle(engine, request, &config);
    }
}

/// Route one request. Never panics on anything a client can send.
fn handle(engine: &mut dyn Engine, request: Request, config: &ServeConfig) {
    // The query string is not part of the route: a client appending
    // `?api-version=...` (Azure-flavoured SDKs do) must still be served.
    let path = request
        .url()
        .split('?')
        .next()
        .unwrap_or_default()
        .to_owned();
    let method = request.method().clone();
    tracing::debug!(%method, %path, "request");

    match (&method, path.as_str()) {
        (Method::Post, PATH_CHAT_COMPLETIONS) => chat_completions(engine, request, config),
        (Method::Get, PATH_MODELS) => {
            let body = ModelList::single(engine.model_id(), engine.created());
            respond_json(request, 200, &body);
        }
        (Method::Get, PATH_HEALTH) => respond_json(request, 200, &Health::ok()),
        _ => respond_error(
            request,
            &ServerError::NotFound {
                method: method.as_str().to_owned(),
                path,
            },
        ),
    }
}

/// `POST /v1/chat/completions`, streaming or not.
fn chat_completions(engine: &mut dyn Engine, mut request: Request, config: &ServeConfig) {
    let body = match read_body(&mut request, config.max_body_bytes) {
        Ok(body) => body,
        Err(e) => return respond_error(request, &e),
    };
    let parsed = match ChatCompletionRequest::from_json(&body) {
        Ok(parsed) => parsed,
        Err(e) => return respond_error(request, &e),
    };
    // Which optional capabilities this client actually asked for, logged
    // before anything is refused. Kept because the refusals above it are only
    // defensible against evidence: `stop` and a forced `tool_choice` are 400s
    // here, and whether that makes the server unusable depends entirely on
    // what the clients pointed at it send on every request.
    tracing::debug!(
        stop = parsed.stop_sequences().len(),
        tools = parsed.tools().len(),
        tool_choice = ?parsed.tool_choice,
        stream = parsed.stream(),
        "chat request"
    );
    // Everything that can be refused is refused here, before a status is
    // committed: once a stream's head is out, 200 is the answer whatever
    // happens next.
    let plan = match engine.prepare(&parsed) {
        Ok(plan) => plan,
        Err(e) => return respond_error(request, &e),
    };

    let id = completion_id();
    let created = unix_now();
    if parsed.stream() {
        stream_into(
            engine,
            &parsed,
            plan,
            request.into_writer(),
            &id,
            created,
            config.keepalive,
        );
    } else {
        // No head to write early and nothing to keep alive: a non-streaming
        // client asked for silence until the reply is complete, and at this
        // runtime's speeds that is very likely to hit its own timeout. That
        // is the client's choice to make, and `stream: true` is the fix.
        let mut sink = NullSink;
        match engine.run(plan, &mut sink) {
            Ok(completion) => {
                // The whole reply is in hand, so the `<tool_call>` blocks in it
                // can be turned into calls before anything is sent. The
                // streaming path reaches the same outcome by a different route
                // — [`CallStream`], driven by the marker token ids — because it
                // has to decide per token, without the text that follows.
                //
                // Gated on the request having declared tools. Without that, a
                // conversation *about* tool calls ("show me what a tool call
                // looks like") gets its example swallowed: the prose vanishes
                // into a structured call the client never enabled, and the
                // reply comes back `finish_reason: "tool_calls"` for a request
                // that offered none. The model only emits these blocks because
                // the tools branch of the template told it to, so when no tools
                // were sent the markers are ordinary text and stay that way.
                let extracted = if parsed.tools().is_empty() {
                    crate::toolcall::Extracted::none(&completion.text)
                } else {
                    crate::toolcall::extract(&completion.text)
                };
                let calls = crate::toolcall::wire_calls(&id, &extracted.calls);
                // A recovered call overrides the stop reason: the model
                // stopped because it wanted a tool, whatever token ended it.
                // Nothing recovered leaves the reason alone, so a block cut off
                // by the token budget still reports `length`.
                let finish_reason = if calls.is_empty() {
                    completion.finish_reason
                } else {
                    FinishReason::ToolCalls
                };
                let body = ChatCompletion::from_message(
                    id,
                    created,
                    &parsed.model,
                    ResponseMessage::calling(extracted.content, calls),
                    finish_reason,
                    completion.usage,
                );
                respond_json(request, 200, &body);
            }
            Err(e) => respond_error(request, &e),
        }
    }
}

/// Run `plan` and write an SSE stream into `writer`.
///
/// Split out from the `tiny_http` plumbing on purpose: `writer` is the only
/// thing this needs from the connection, so a test can hand it a recorder that
/// timestamps every flush and assert what a buffered implementation could
/// never pass — that the events arrive *when* the engine produced them.
pub fn stream_into<W: Write + Send + 'static>(
    engine: &mut dyn Engine,
    request: &ChatCompletionRequest,
    plan: Plan,
    writer: W,
    id: &str,
    created: u64,
    keepalive: Duration,
) {
    let state = Arc::new(Mutex::new(StreamState::new(writer)));

    // The head goes out before a single expert is read. A 4K prompt is about
    // six minutes of prefill and the clients give up after five, so the order
    // of these two statements is the difference between a working server and
    // one that is dead on arrival.
    if let Err(e) = lock(&state).head() {
        tracing::debug!(?e, "client left before the response head");
        return;
    }

    let builder = ChunkBuilder::new(id, created, &request.model);
    // The role chunk rides out with the head, so a client sees a well-formed
    // stream begin before the model has done any work at all.
    if emit(&state, &sse_or_empty(&builder.role())).is_err() {
        return;
    }

    let keeper = KeepAlive::spawn(Arc::clone(&state), keepalive);
    let mut sink = SseSink {
        state: Arc::clone(&state),
        builder: builder.clone(),
        keepalive,
        // The same gate the buffered path applies: without declared tools the
        // markers are ordinary text a user asked about, and swallowing them
        // deletes their answer.
        calls: CallStream::new(!request.tools().is_empty()),
    };
    let include_usage = request.include_usage();
    let outcome = engine.run(plan, &mut sink);
    drop(keeper);

    match outcome {
        Ok(completion) if completion.aborted => {
            tracing::debug!("client left mid-reply; the stream is abandoned unterminated");
        }
        Ok(completion) => {
            // A block the token budget cut in half is not a call, but it is
            // text the model wrote, and it has been held back until now.
            if let Some(unterminated) = sink.calls.flush() {
                let _ = emit(&state, &sse_or_empty(&builder.content(unterminated)));
            }
            // A recovered call overrides the stop reason: the model stopped
            // because it wanted a tool, whatever token ended it. Nothing
            // recovered leaves the reason alone, so a block cut off by the
            // budget still reports `length` — the same rule as the buffered
            // path, reached from a counter instead of from a list.
            let finish_reason = if sink.calls.emitted() > 0 {
                FinishReason::ToolCalls
            } else {
                completion.finish_reason
            };
            let _ = emit(&state, &sse_or_empty(&builder.finish(finish_reason)));
            // Usage goes immediately *before* the sentinel: a client stops
            // reading at `[DONE]`, so usage after it is usage nobody gets.
            if include_usage {
                let _ = emit(&state, &sse_or_empty(&builder.usage(completion.usage)));
            }
            let _ = emit(&state, &sse_done());
            let _ = lock(&state).finish();
        }
        Err(e) => {
            // The status is already spent — the head said 200 minutes ago —
            // so the only honest place left to report this is inside the
            // stream, in the same body shape a 4xx would have had.
            tracing::error!(error = %e, "generation failed mid-stream");
            let _ = emit(&state, &sse_data(&e.body_json()));
            let _ = emit(&state, &sse_done());
            let _ = lock(&state).finish();
        }
    }
}

/// A [`TokenSink`] that frames each token as an SSE chunk on the socket.
struct SseSink<W: Write> {
    state: Arc<Mutex<StreamState<W>>>,
    builder: ChunkBuilder,
    keepalive: Duration,
    /// Recovers `<tool_call>` blocks as they arrive, so an agent client gets
    /// structure rather than the markup it would have to parse itself.
    calls: CallStream,
}

impl<W: Write> TokenSink for SseSink<W> {
    fn prefill(&mut self, done: usize, total: usize) -> Result<(), StreamError> {
        let mut state = lock(&self.state);
        if let Some(failed) = state.failure() {
            return Err(failed);
        }
        // Progress has no OpenAI chunk shape, so nothing is reported as
        // progress. What it is good for is a second, cheaper source of
        // keep-alives: for a 4K prompt these fire roughly every 45 s, which
        // is under the 300 s wall on its own but far too sparse to be the
        // only source — hence [`KeepAlive`] as well, and hence the shared
        // idle clock, so the two do not double up.
        if state.idle_for() >= self.keepalive {
            state.emit(KEEPALIVE)?;
            tracing::trace!(done, total, "keep-alive on prefill progress");
        }
        Ok(())
    }

    fn token(&mut self, id: u32, text: &str) -> Result<(), StreamError> {
        // The id, not the text, decides: `<tool_call>` and `</tool_call>` are
        // single added tokens, so the block boundaries are an integer compare
        // and cannot be split across two deltas.
        let event = match self.calls.push(id, text) {
            // Inside a block. Nothing goes out until it closes, which is what
            // keeps the raw markup off the wire.
            Step::Nothing => return Ok(()),
            // A token whose bytes are still an incomplete UTF-8 character
            // decodes to nothing; sending an empty delta would be a chunk that
            // says nothing, and the next token carries the character anyway.
            Step::Content(text) if text.is_empty() => return Ok(()),
            Step::Content(text) => sse_or_empty(&self.builder.content(text)),
            Step::Call { index, call } => {
                tracing::debug!(index, name = %call.name, "recovered a streamed tool call");
                let wire = wire_call(self.builder.id(), index, &call);
                sse_or_empty(&self.builder.tool_call(index, wire))
            }
        };
        lock(&self.state).emit(&event)
    }
}

/// Serialize one chunk, or the empty string.
///
/// [`sse_event`] can only fail if `serde_json` cannot serialize a struct of
/// `String`s and numbers, which it can. The failure branch is logged rather
/// than unwrapped so that stays true by construction, and an empty payload is
/// dropped by the framer rather than sent as a zero-length chunk.
fn sse_or_empty(chunk: &crate::response::ChatCompletionChunk) -> String {
    match sse_event(chunk) {
        Ok(event) => event,
        Err(e) => {
            tracing::error!(error = %e, "serializing a chunk failed; dropping it");
            String::new()
        }
    }
}

/// A thread that writes an SSE comment whenever the stream has gone quiet.
///
/// It exists because prefill progress is not a clock. `GenerateProgress`
/// fires once per prefill chunk — about eight events for a 4K prompt — so the
/// gap between two of them is tens of seconds and depends on the chunk width,
/// the cache state and the drive. A timer does not.
///
/// It shares the writer through the same mutex the generation thread uses, so
/// a comment can never interleave with half a chunk, and it reads the shared
/// idle clock so it stays quiet while content is flowing.
struct KeepAlive {
    stop: Arc<AtomicBool>,
    handle: Option<JoinHandle<()>>,
}

impl KeepAlive {
    fn spawn<W: Write + Send + 'static>(
        state: Arc<Mutex<StreamState<W>>>,
        interval: Duration,
    ) -> Self {
        let stop = Arc::new(AtomicBool::new(false));
        let flag = Arc::clone(&stop);
        // Quarter of the interval, so a comment is at worst 25% late, and
        // bounded either way so a tiny interval does not spin and a huge one
        // does not delay the join.
        let tick = (interval / 4).clamp(Duration::from_millis(5), Duration::from_secs(1));
        let handle = std::thread::Builder::new()
            .name("rvmp-keepalive".to_owned())
            .spawn(move || {
                while !flag.load(Ordering::Relaxed) {
                    std::thread::sleep(tick);
                    if flag.load(Ordering::Relaxed) {
                        return;
                    }
                    let mut state = lock(&state);
                    if state.failure().is_some() {
                        return;
                    }
                    if state.idle_for() >= interval && state.emit(KEEPALIVE).is_err() {
                        // The generation loop will see the same sticky
                        // failure at its next token and unwind there, where
                        // leaving is safe.
                        return;
                    }
                }
            });
        match handle {
            Ok(handle) => KeepAlive {
                stop,
                handle: Some(handle),
            },
            Err(e) => {
                // A stream without keep-alives still works for short prompts,
                // so this is degraded rather than fatal.
                tracing::warn!(error = %e, "no keep-alive thread; long prefills may time out");
                KeepAlive { stop, handle: None }
            }
        }
    }
}

impl Drop for KeepAlive {
    fn drop(&mut self) {
        self.stop.store(true, Ordering::Relaxed);
        if let Some(handle) = self.handle.take() {
            // Joined, not detached: the thread holds a writer clone, and the
            // terminating chunk must not race a comment.
            let _ = handle.join();
        }
    }
}

/// Lock the shared stream state, ignoring poisoning.
///
/// A poisoned mutex here means a panic while a write was in progress. The
/// state behind it is a writer and two flags — there is no invariant a panic
/// could have broken halfway — and refusing to write would turn one failed
/// request into a hung one.
fn lock<W: Write>(state: &Mutex<StreamState<W>>) -> MutexGuard<'_, StreamState<W>> {
    state.lock().unwrap_or_else(PoisonError::into_inner)
}

fn emit<W: Write>(state: &Mutex<StreamState<W>>, payload: &str) -> Result<(), StreamError> {
    lock(state).emit(payload)
}

/// Read a request body, refusing anything over `limit`.
///
/// Checked twice: the declared `Content-Length` first, so an oversized body is
/// refused without reading it, and the actual byte count second, because
/// `Content-Length` is a claim by the client and a chunked request does not
/// make one at all.
fn read_body(request: &mut Request, limit: usize) -> Result<String, ServerError> {
    if request.body_length().is_some_and(|len| len > limit) {
        return Err(ServerError::BodyTooLarge { limit });
    }
    let mut body = Vec::new();
    // `limit + 1`, so "exactly at the limit" and "over it" are
    // distinguishable without reading the whole thing.
    let capped = u64::try_from(limit).unwrap_or(u64::MAX).saturating_add(1);
    request
        .as_reader()
        .take(capped)
        .read_to_end(&mut body)
        .map_err(|e| ServerError::MalformedJson(format!("reading the request body: {e}")))?;
    if body.len() > limit {
        return Err(ServerError::BodyTooLarge { limit });
    }
    String::from_utf8(body)
        .map_err(|_| ServerError::MalformedJson("request body is not valid UTF-8".to_owned()))
}

/// A completion id: `chatcmpl-` plus something that does not repeat.
///
/// Not random, because a random source is a dependency and this is an
/// identifier, not a secret. The clock plus a counter cannot collide within a
/// process and cannot collide across two processes unless they start in the
/// same nanosecond.
fn completion_id() -> String {
    static COUNTER: AtomicU64 = AtomicU64::new(0);
    let n = COUNTER.fetch_add(1, Ordering::Relaxed);
    format!("{COMPLETION_ID_PREFIX}{:x}{:04x}", unix_now(), n & 0xffff)
}

/// Build a header from ASCII.
///
/// `Header::from_bytes` fails only on non-ASCII, and every call site here
/// passes a literal, so the failure branch is unreachable — reported rather
/// than unwrapped so it stays that way by construction.
fn header(name: &str, value: &str) -> Option<Header> {
    match Header::from_bytes(name.as_bytes(), value.as_bytes()) {
        Ok(header) => Some(header),
        Err(()) => {
            tracing::error!(name, value, "non-ASCII header; dropped");
            None
        }
    }
}

/// Answer with a buffered JSON body.
///
/// The buffered path is correct for everything that is not a stream: it is one
/// short body, written once, and `tiny_http`'s own encoder handles the
/// framing.
fn respond_json<T: serde::Serialize>(request: Request, status: u16, body: &T) {
    match serde_json::to_string(body) {
        Ok(json) => send(request, status, json, &[]),
        Err(e) => {
            let error = ServerError::Serialize(e);
            respond_error(request, &error);
        }
    }
}

/// Answer with an error body, its status and its headers.
fn respond_error(request: Request, error: &ServerError) {
    tracing::debug!(status = error.status(), error = %error, "refusing a request");
    send(request, error.status(), error.body_json(), error.headers());
}

fn send(request: Request, status: u16, body: String, extra: &[(&str, &str)]) {
    let mut response = Response::from_string(body).with_status_code(status);
    if let Some(header) = header("Content-Type", "application/json") {
        response = response.with_header(header);
    }
    for (name, value) in extra {
        if let Some(header) = header(name, value) {
            response = response.with_header(header);
        }
    }
    if let Err(e) = request.respond(response) {
        // `respond` maps the client-gone kinds to `Ok`, so anything reported
        // here is a real write failure.
        tracing::debug!(error = %e, "failed to send a response");
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::engine::Completion;
    use crate::response::{FinishReason, Usage};
    use crate::wire::{SSE_HEAD, TERMINATOR};
    use std::sync::atomic::AtomicUsize;
    use std::time::Instant;

    /// A token source with no model in it: known tokens, at known intervals.
    ///
    /// This is what makes the streaming contract testable at all. A buffered
    /// stream and a live one produce byte-identical transcripts and differ
    /// only in *when* the bytes appear, so the assertion has to be about time,
    /// and an assertion about time needs a source whose timing is known.
    struct StubEngine {
        prefill_steps: usize,
        prefill_delay: Duration,
        /// Each token as (id, text), because the streaming tool-call lane keys
        /// off the id and a source that could only produce text could not
        /// exercise it.
        tokens: Vec<(u32, String)>,
        token_delay: Duration,
        /// Why the stub says it stopped. The tool-call lane overrides this, so
        /// what it overrides has to be settable.
        finish_reason: FinishReason,
        /// How many tokens the engine got as far as producing. The evidence
        /// that a hung-up client actually stops generation.
        produced: Arc<AtomicUsize>,
    }

    /// The id of an ordinary token: anything that is not a marker.
    const TEXT_ID: u32 = 7;

    impl StubEngine {
        fn new(tokens: &[&str]) -> Self {
            StubEngine::with_ids(&tokens.iter().map(|t| (TEXT_ID, *t)).collect::<Vec<_>>())
        }

        fn with_ids(tokens: &[(u32, &str)]) -> Self {
            StubEngine {
                prefill_steps: 0,
                prefill_delay: Duration::ZERO,
                tokens: tokens
                    .iter()
                    .map(|(id, text)| (*id, (*text).to_owned()))
                    .collect(),
                token_delay: Duration::ZERO,
                finish_reason: FinishReason::Stop,
                produced: Arc::new(AtomicUsize::new(0)),
            }
        }
    }

    impl Engine for StubEngine {
        fn model_id(&self) -> &str {
            "stub-model"
        }

        fn created(&self) -> u64 {
            1_700_000_000
        }

        fn prepare(&mut self, request: &ChatCompletionRequest) -> Result<Plan, ServerError> {
            request.validate()?;
            Ok(Plan {
                prompt_tokens: 11,
                max_new: self.tokens.len(),
                cached_prefix: 0,
                prompt_ids: vec![1, 2, 3],
                params: ramvamp_core::generate::GenerateParams {
                    max_new: self.tokens.len(),
                    temperature: 0.7,
                    top_k: Some(20),
                    top_p: 0.8,
                    seed: 42,
                    greedy: false,
                },
            })
        }

        fn run(&mut self, plan: Plan, sink: &mut dyn TokenSink) -> Result<Completion, ServerError> {
            let mut text = String::new();
            for step in 1..=self.prefill_steps {
                std::thread::sleep(self.prefill_delay);
                if sink.prefill(step, self.prefill_steps).is_err() {
                    return Ok(aborted(text, plan.prompt_tokens, 0));
                }
            }
            for (index, (id, token)) in self.tokens.clone().iter().enumerate() {
                std::thread::sleep(self.token_delay);
                self.produced.store(index + 1, Ordering::Relaxed);
                text.push_str(token);
                if sink.token(*id, token).is_err() {
                    return Ok(aborted(text, plan.prompt_tokens, index + 1));
                }
            }
            let generated = self.tokens.len();
            Ok(Completion {
                text,
                usage: Usage::new(plan.prompt_tokens as u32, generated as u32),
                finish_reason: self.finish_reason,
                aborted: false,
            })
        }
    }

    fn aborted(text: String, prompt: usize, generated: usize) -> Completion {
        Completion {
            text,
            usage: Usage::new(prompt as u32, generated as u32),
            finish_reason: FinishReason::Stop,
            aborted: true,
        }
    }

    /// One flushed group: when it went out, and what it was.
    type Flushed = Arc<Mutex<Vec<(Duration, Vec<u8>)>>>;

    /// A writer that timestamps every flush and can be told to die.
    #[derive(Clone)]
    struct Recorder {
        events: Flushed,
        pending: Arc<Mutex<Vec<u8>>>,
        flushes: Arc<AtomicUsize>,
        start: Instant,
        die_after_flushes: Option<usize>,
    }

    impl Recorder {
        fn new() -> Self {
            Recorder {
                events: Arc::new(Mutex::new(Vec::new())),
                pending: Arc::new(Mutex::new(Vec::new())),
                flushes: Arc::new(AtomicUsize::new(0)),
                start: Instant::now(),
                die_after_flushes: None,
            }
        }

        fn dying_after(mut self, flushes: usize) -> Self {
            self.die_after_flushes = Some(flushes);
            self
        }

        /// Every flushed group, as (elapsed, text).
        fn events(&self) -> Vec<(Duration, String)> {
            self.events
                .lock()
                .unwrap_or_else(PoisonError::into_inner)
                .iter()
                .map(|(at, bytes)| (*at, String::from_utf8_lossy(bytes).into_owned()))
                .collect()
        }

        fn body(&self) -> String {
            self.events().into_iter().map(|(_, text)| text).collect()
        }
    }

    impl Write for Recorder {
        fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
            self.pending
                .lock()
                .unwrap_or_else(PoisonError::into_inner)
                .extend_from_slice(buf);
            Ok(buf.len())
        }

        fn flush(&mut self) -> std::io::Result<()> {
            let bytes =
                std::mem::take(&mut *self.pending.lock().unwrap_or_else(PoisonError::into_inner));
            let n = self.flushes.fetch_add(1, Ordering::Relaxed) + 1;
            if self.die_after_flushes.is_some_and(|limit| n > limit) {
                return Err(std::io::Error::from(std::io::ErrorKind::BrokenPipe));
            }
            self.events
                .lock()
                .unwrap_or_else(PoisonError::into_inner)
                .push((self.start.elapsed(), bytes));
            Ok(())
        }
    }

    fn request(stream: bool) -> ChatCompletionRequest {
        let body = format!(
            r#"{{"model":"stub-model","messages":[{{"role":"user","content":"hi"}}],"stream":{stream}}}"#
        );
        ChatCompletionRequest::from_json(&body).expect("valid body")
    }

    /// The same body with a `glob` tool declared, which is the gate the
    /// streaming tool-call lane is behind.
    fn request_with_tools() -> ChatCompletionRequest {
        ChatCompletionRequest::from_json(
            r#"{"model":"stub-model","messages":[{"role":"user","content":"list the rust files"}],
                "stream":true,
                "tools":[{"type":"function","function":{"name":"glob","parameters":{}}}]}"#,
        )
        .expect("valid body")
    }

    fn run_stream(engine: &mut StubEngine, recorder: Recorder, keepalive: Duration) {
        run_stream_for(engine, &request(true), recorder, keepalive);
    }

    fn run_stream_for(
        engine: &mut StubEngine,
        request: &ChatCompletionRequest,
        recorder: Recorder,
        keepalive: Duration,
    ) {
        let plan = engine.prepare(request).expect("a plan");
        stream_into(
            engine,
            request,
            plan,
            recorder,
            "chatcmpl-test",
            1_700_000_000,
            keepalive,
        );
    }

    /// The `data:` payloads of a recorded stream, parsed, with the sentinel
    /// dropped. What a client actually gets.
    fn chunks(body: &str) -> Vec<serde_json::Value> {
        body.split("data: ")
            .skip(1)
            .map(|event| event.split("\n\n").next().unwrap_or_default())
            .filter(|payload| *payload != crate::sse::DONE_SENTINEL)
            .map(|payload| {
                serde_json::from_str(payload)
                    .unwrap_or_else(|e| panic!("a chunk payload: {e}\n{payload}"))
            })
            .collect()
    }

    /// The `tool_calls` entries of a recorded stream, flattened, in order.
    fn streamed_calls(body: &str) -> Vec<serde_json::Value> {
        chunks(body)
            .into_iter()
            .filter_map(|chunk| {
                chunk["choices"][0]["delta"]["tool_calls"]
                    .as_array()
                    .cloned()
            })
            .flatten()
            .collect()
    }

    /// The content deltas of a recorded stream, concatenated.
    fn streamed_text(body: &str) -> String {
        chunks(body)
            .iter()
            .filter_map(|chunk| chunk["choices"][0]["delta"]["content"].as_str())
            .collect()
    }

    /// The reason the stream ended, from its finish chunk.
    fn streamed_finish(body: &str) -> String {
        chunks(body)
            .iter()
            .find_map(|chunk| {
                chunk["choices"][0]["finish_reason"]
                    .as_str()
                    .map(str::to_owned)
            })
            .expect("a finish chunk")
    }

    /// The block a model writes when it wants `glob`, as the tokens it writes
    /// it in: the markers are single added tokens, the body is not.
    fn glob_call_tokens() -> Vec<(u32, &'static str)> {
        vec![
            (crate::toolcall::OPEN_TAG_ID, "<tool_call>"),
            (TEXT_ID, "\n{\"name\": \"glob\", "),
            (TEXT_ID, "\"arguments\": {\"pattern\": \"*/.rs\"}}\n"),
            (crate::toolcall::CLOSE_TAG_ID, "</tool_call>"),
        ]
    }

    /// The assertion a buffered implementation cannot pass: the content
    /// events are separated on the wire by the interval the source produced
    /// them at, rather than arriving together at the end.
    #[test]
    fn events_reach_the_client_at_the_sources_pace() {
        let mut engine = StubEngine::new(&["a", "b", "c", "d", "e"]);
        engine.token_delay = Duration::from_millis(60);
        let recorder = Recorder::new();
        run_stream(&mut engine, recorder.clone(), Duration::from_secs(60));

        let content: Vec<Duration> = recorder
            .events()
            .into_iter()
            .filter(|(_, text)| text.contains(r#""content":"#) && !text.contains(r#""role""#))
            .map(|(at, _)| at)
            .collect();
        assert_eq!(content.len(), 5, "{:?}", recorder.events());
        for pair in content.windows(2) {
            assert!(
                pair[1] - pair[0] >= Duration::from_millis(40),
                "events arrived in a burst: {content:?}"
            );
        }
        assert!(
            content[4] - content[0] >= Duration::from_millis(200),
            "{content:?}"
        );
    }

    /// The head is committed before the engine does any work, which is what
    /// keeps a six-minute prefill under a five-minute client timeout.
    #[test]
    fn the_head_is_written_before_the_engine_runs() {
        let mut engine = StubEngine::new(&["x"]);
        engine.prefill_steps = 1;
        engine.prefill_delay = Duration::from_millis(250);
        let recorder = Recorder::new();
        run_stream(&mut engine, recorder.clone(), Duration::from_secs(60));

        let events = recorder.events();
        assert_eq!(events[0].1, SSE_HEAD);
        assert!(
            events[0].0 < Duration::from_millis(100),
            "the head waited for the model: {:?}",
            events[0].0
        );
        // And the role chunk rides out with it.
        assert!(events[1].1.contains(r#""role":"assistant""#));
        assert!(events[1].0 < Duration::from_millis(100));
    }

    /// A prompt that takes minutes to prefill produces no content events, so
    /// something else has to keep the byte stream alive.
    #[test]
    fn a_slow_prefill_emits_keep_alive_comments() {
        let mut engine = StubEngine::new(&["done"]);
        engine.prefill_steps = 1;
        engine.prefill_delay = Duration::from_millis(400);
        let recorder = Recorder::new();
        run_stream(&mut engine, recorder.clone(), Duration::from_millis(50));

        let comments: Vec<Duration> = recorder
            .events()
            .into_iter()
            .filter(|(_, text)| text.contains(KEEPALIVE))
            .map(|(at, _)| at)
            .collect();
        assert!(
            comments.len() >= 3,
            "expected keep-alives during a 400ms prefill, got {comments:?}"
        );
        // They are comments, so a client discards them: the reply is intact.
        assert!(recorder.body().contains(r#""content":"done""#));
    }

    /// A client that hangs up surfaces as a write error, and the engine stops
    /// generating instead of running for minutes with nobody listening.
    #[test]
    fn a_dropped_client_surfaces_as_a_write_error_and_aborts_generation() {
        let mut engine = StubEngine::new(&["a", "b", "c", "d", "e", "f"]);
        let produced = Arc::clone(&engine.produced);
        // head, role, then one content event; the next flush is a broken pipe.
        let recorder = Recorder::new().dying_after(3);
        run_stream(&mut engine, recorder.clone(), Duration::from_secs(60));

        let got = produced.load(Ordering::Relaxed);
        assert!(
            (1..=2).contains(&got),
            "generation should have stopped at the hangup, produced {got}"
        );
        // Nothing is appended after the client is gone: no sentinel, no
        // terminator, because there is nowhere to put them.
        assert!(!recorder.body().contains("[DONE]"), "{}", recorder.body());
    }

    /// The whole wire sequence: head, framing, order, sentinel, terminator.
    #[test]
    fn the_sentinel_precedes_the_zero_length_chunk() {
        let mut engine = StubEngine::new(&["he", "llo"]);
        let recorder = Recorder::new();
        run_stream(&mut engine, recorder.clone(), Duration::from_secs(60));

        let body = recorder.body();
        let rest = body.strip_prefix(SSE_HEAD).expect("head first");
        assert!(rest.ends_with(TERMINATOR), "{rest:?}");
        let done = rest.find("data: [DONE]").expect("sentinel");
        let end = rest.rfind(TERMINATOR).expect("terminator");
        assert!(done < end, "the sentinel must precede the terminator");
    }

    /// Every chunk's declared length is the byte length of its payload, and
    /// the reassembled deltas are the reply. This is the framing check a
    /// client actually performs.
    #[test]
    fn the_chunk_framing_decodes_back_to_the_reply() {
        let mut engine = StubEngine::new(&["Hé", "llo", "→!"]);
        let recorder = Recorder::new();
        run_stream(&mut engine, recorder.clone(), Duration::from_secs(60));

        let body = recorder.body();
        let mut rest = body.strip_prefix(SSE_HEAD).expect("head first");
        let mut payloads = Vec::new();
        loop {
            let (head, tail) = rest.split_once("\r\n").expect("a chunk header");
            let len = usize::from_str_radix(head, 16).expect("hex length");
            if len == 0 {
                assert_eq!(tail, "\r\n", "the terminator ends the body");
                break;
            }
            assert!(tail.len() >= len + 2, "chunk shorter than its header says");
            payloads.push(tail[..len].to_owned());
            assert_eq!(&tail[len..len + 2], "\r\n", "chunk not CRLF-terminated");
            rest = &tail[len + 2..];
        }

        let text: String = payloads
            .iter()
            .filter_map(|event| event.strip_prefix("data: "))
            .filter_map(|json| serde_json::from_str::<serde_json::Value>(json.trim_end()).ok())
            .filter_map(|chunk| {
                chunk["choices"][0]["delta"]["content"]
                    .as_str()
                    .map(str::to_owned)
            })
            .collect();
        assert_eq!(text, "Héllo→!");
    }

    /// `include_usage` puts the usage chunk between the finish chunk and the
    /// sentinel, and leaves it out entirely when it was not asked for.
    #[test]
    fn usage_is_emitted_only_when_asked_for_and_before_the_sentinel() {
        let mut engine = StubEngine::new(&["x"]);
        let recorder = Recorder::new();
        run_stream(&mut engine, recorder.clone(), Duration::from_secs(60));
        assert!(!recorder.body().contains(r#""usage""#));

        let asked = ChatCompletionRequest::from_json(
            r#"{"model":"stub-model","messages":[{"role":"user","content":"hi"}],
                "stream":true,"stream_options":{"include_usage":true}}"#,
        )
        .expect("valid body");
        let plan = engine.prepare(&asked).expect("a plan");
        let recorder = Recorder::new();
        stream_into(
            &mut engine,
            &asked,
            plan,
            recorder.clone(),
            "chatcmpl-test",
            1_700_000_000,
            Duration::from_secs(60),
        );
        let body = recorder.body();
        let usage = body.find(r#""usage""#).expect("a usage chunk");
        let finish = body.find(r#""finish_reason":"stop""#).expect("a finish");
        let done = body.find("[DONE]").expect("the sentinel");
        assert!(finish < usage && usage < done, "{body}");
        assert!(body.contains(r#""prompt_tokens":11"#), "{body}");
    }

    /// A runtime failure after the head is out cannot be a status code, so it
    /// is an error event inside the stream followed by a clean terminator.
    #[test]
    fn a_mid_stream_failure_becomes_an_error_event() {
        struct Failing;
        impl Engine for Failing {
            fn model_id(&self) -> &str {
                "stub-model"
            }
            fn created(&self) -> u64 {
                0
            }
            fn prepare(&mut self, _: &ChatCompletionRequest) -> Result<Plan, ServerError> {
                unreachable!("the test builds the plan itself")
            }
            fn run(&mut self, _: Plan, _: &mut dyn TokenSink) -> Result<Completion, ServerError> {
                Err(ServerError::Generate(
                    ramvamp_core::generate::GenerateError::EmptyPrompt,
                ))
            }
        }
        let mut stub = StubEngine::new(&[]);
        let request = request(true);
        let plan = stub.prepare(&request).expect("a plan");
        let recorder = Recorder::new();
        stream_into(
            &mut Failing,
            &request,
            plan,
            recorder.clone(),
            "chatcmpl-test",
            0,
            Duration::from_secs(60),
        );
        let body = recorder.body();
        assert!(body.starts_with(SSE_HEAD));
        assert!(body.contains(r#""code":"generate_error""#), "{body}");
        assert!(body.contains("[DONE]"));
        assert!(body.ends_with(TERMINATOR));
    }

    // ---- the buffered endpoints, over a real socket ----

    fn spawn_server() -> SocketAddr {
        spawn_server_with(&["ok"], FinishReason::Stop)
    }

    fn spawn_server_with(tokens: &[&str], finish_reason: FinishReason) -> SocketAddr {
        let tokens: Vec<String> = tokens.iter().map(|t| (*t).to_owned()).collect();
        let (tx, rx) = std::sync::mpsc::channel();
        std::thread::spawn(move || {
            let borrowed: Vec<&str> = tokens.iter().map(String::as_str).collect();
            let mut engine = StubEngine::new(&borrowed);
            engine.finish_reason = finish_reason;
            let _ = serve_with(
                &mut engine,
                ServeConfig {
                    port: 0,
                    ..ServeConfig::default()
                },
                |address| {
                    let _ = tx.send(address);
                },
            );
        });
        rx.recv_timeout(Duration::from_secs(10))
            .expect("the server binds")
    }

    fn roundtrip(address: SocketAddr, raw: &str) -> String {
        let mut stream = std::net::TcpStream::connect(address).expect("connect");
        stream
            .set_read_timeout(Some(Duration::from_secs(10)))
            .expect("timeout");
        stream.write_all(raw.as_bytes()).expect("write");
        stream.flush().expect("flush");
        let mut out = Vec::new();
        let mut buffer = [0u8; 4096];
        loop {
            match stream.read(&mut buffer) {
                Ok(0) | Err(_) => break,
                Ok(n) => out.extend_from_slice(&buffer[..n]),
            }
        }
        String::from_utf8_lossy(&out).into_owned()
    }

    fn get(address: SocketAddr, path: &str) -> String {
        roundtrip(
            address,
            &format!("GET {path} HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n"),
        )
    }

    fn post(address: SocketAddr, path: &str, body: &str) -> String {
        roundtrip(
            address,
            &format!(
                "POST {path} HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\
                 Content-Type: application/json\r\nContent-Length: {}\r\n\r\n{body}",
                body.len()
            ),
        )
    }

    #[test]
    fn the_buffered_routes_answer_over_a_socket() {
        let address = spawn_server();
        // Loopback only, and the bound address proves it.
        assert!(address.ip().is_loopback(), "{address}");

        let health = get(address, PATH_HEALTH);
        assert!(health.starts_with("HTTP/1.1 200"), "{health}");
        assert!(health.contains(r#"{"status":"ok"}"#), "{health}");

        let models = get(address, PATH_MODELS);
        assert!(models.starts_with("HTTP/1.1 200"), "{models}");
        assert!(models.contains(r#""id":"stub-model""#), "{models}");
        assert!(models.contains(r#""object":"list""#), "{models}");

        // A query string is not part of the route.
        let versioned = get(address, "/v1/models?api-version=2024-02-01");
        assert!(versioned.contains(r#""id":"stub-model""#), "{versioned}");

        let missing = get(address, "/v1/embeddings");
        assert!(missing.starts_with("HTTP/1.1 404"), "{missing}");
        assert!(
            missing.contains(r#""type":"invalid_request_error""#),
            "{missing}"
        );
        assert!(missing.contains(r#""code":"not_found""#), "{missing}");

        // Right path, wrong method: still a 404 with a parseable body, never
        // an empty one.
        let wrong = get(address, PATH_CHAT_COMPLETIONS);
        assert!(wrong.starts_with("HTTP/1.1 404"), "{wrong}");
        assert!(wrong.contains(r#""error""#), "{wrong}");
    }

    #[test]
    fn a_non_streaming_completion_comes_back_as_one_json_body() {
        let address = spawn_server();
        let response = post(
            address,
            PATH_CHAT_COMPLETIONS,
            r#"{"model":"stub-model","messages":[{"role":"user","content":"hi"}]}"#,
        );
        assert!(response.starts_with("HTTP/1.1 200"), "{response}");
        assert!(
            response.contains(r#""object":"chat.completion""#),
            "{response}"
        );
        assert!(response.contains(r#""content":"ok""#), "{response}");
        assert!(response.contains(r#""finish_reason":"stop""#), "{response}");
    }

    /// What the model actually writes when it wants a tool: prose, then the
    /// block the template asked for.
    const TOOL_CALL_REPLY: &str = "Let me look.\n<tool_call>\n{\"name\": \"read\", \"arguments\": {\"path\": \"a.txt\"}}\n</tool_call>";

    /// The JSON body of an HTTP response, headers stripped.
    fn body_json(response: &str) -> serde_json::Value {
        let body = response
            .split_once("\r\n\r\n")
            .map_or(response, |(_, body)| body);
        serde_json::from_str(body).unwrap_or_else(|e| panic!("a JSON body: {e}\n{response}"))
    }

    /// The end of this wave: a client can act on the reply. The text the model
    /// wrote is gone from `content` and present as structure, with an id the
    /// client can put on its `tool` message.
    #[test]
    fn a_tool_call_reply_comes_back_as_structured_calls() {
        let address = spawn_server_with(&[TOOL_CALL_REPLY], FinishReason::Stop);
        let response = post(
            address,
            PATH_CHAT_COMPLETIONS,
            r#"{"model":"stub-model","messages":[{"role":"user","content":"read a.txt"}],
                "tools":[{"type":"function","function":{"name":"read","parameters":{}}}]}"#,
        );
        assert!(response.starts_with("HTTP/1.1 200"), "{response}");
        let json = body_json(&response);
        let choice = &json["choices"][0];

        // The stop reason the engine reported is overridden: the model stopped
        // because it wants a tool run.
        assert_eq!(choice["finish_reason"], "tool_calls");

        let calls = choice["message"]["tool_calls"]
            .as_array()
            .unwrap_or_else(|| panic!("a tool_calls array: {json}"));
        assert_eq!(calls.len(), 1);
        assert_eq!(calls[0]["type"], "function");
        assert_eq!(calls[0]["function"]["name"], "read");
        // `arguments` is a JSON *string*, byte for byte what the model wrote.
        assert_eq!(calls[0]["function"]["arguments"], r#"{"path": "a.txt"}"#);
        let call_id = calls[0]["id"].as_str().expect("an id");
        assert!(call_id.starts_with("call_"), "{call_id}");
        assert!(call_id.ends_with("_0"), "{call_id}");
        assert!(
            call_id.contains(
                json["id"]
                    .as_str()
                    .and_then(|id| id.strip_prefix("chatcmpl-"))
                    .expect("a completion id")
            ),
            "the call id is derived from the completion id: {call_id}"
        );

        // And the prose survives without the markup.
        assert_eq!(choice["message"]["content"], "Let me look.");
        assert!(
            !response.contains("<tool_call>"),
            "the raw block is still in the body: {response}"
        );
    }

    /// The same reply, from a request that declared no tools, stays text.
    ///
    /// A conversation *about* tool calling is the case this protects: asked
    /// what a call looks like, the model writes one, and extracting it would
    /// delete the answer from `content` and hand back a call the caller never
    /// enabled, on a request that offered no tools to call.
    #[test]
    fn a_block_in_a_reply_to_a_request_without_tools_stays_text() {
        let address = spawn_server_with(&[TOOL_CALL_REPLY], FinishReason::Stop);
        let response = post(
            address,
            PATH_CHAT_COMPLETIONS,
            r#"{"model":"stub-model","messages":[{"role":"user","content":"show me a tool call"}]}"#,
        );
        assert!(response.starts_with("HTTP/1.1 200"), "{response}");
        let json = body_json(&response);
        let choice = &json["choices"][0];

        assert_eq!(choice["finish_reason"], "stop");
        assert!(
            choice["message"]["tool_calls"].is_null(),
            "a request without tools got tool_calls back: {json}"
        );
        // Byte for byte, markers and all.
        assert_eq!(
            choice["message"]["content"].as_str().expect("content"),
            TOOL_CALL_REPLY
        );
    }

    /// A reply with no block is untouched, and the key stays absent rather
    /// than becoming an empty array a client would read as "tools wanted".
    #[test]
    fn a_plain_reply_carries_no_tool_calls_key() {
        let address = spawn_server();
        let response = post(
            address,
            PATH_CHAT_COMPLETIONS,
            r#"{"model":"stub-model","messages":[{"role":"user","content":"hi"}]}"#,
        );
        let json = body_json(&response);
        assert_eq!(json["choices"][0]["message"]["content"], "ok");
        assert_eq!(json["choices"][0]["finish_reason"], "stop");
        assert!(
            json["choices"][0]["message"].get("tool_calls").is_none(),
            "{json}"
        );
    }

    /// A block cut off by the token budget yields no call, so the reason stays
    /// `length` — the client's cue to raise `max_tokens`, not to run something
    /// that was never fully asked for.
    #[test]
    fn a_block_truncated_by_the_token_budget_keeps_the_length_reason() {
        let address = spawn_server_with(
            &["<tool_call>\n{\"name\": \"read\", \"argum"],
            FinishReason::Length,
        );
        let response = post(
            address,
            PATH_CHAT_COMPLETIONS,
            r#"{"model":"stub-model","messages":[{"role":"user","content":"hi"}],"max_tokens":8}"#,
        );
        let json = body_json(&response);
        assert_eq!(json["choices"][0]["finish_reason"], "length");
        assert!(
            json["choices"][0]["message"].get("tool_calls").is_none(),
            "{json}"
        );
        assert_eq!(
            json["choices"][0]["message"]["content"], "<tool_call>\n{\"name\": \"read\", \"argum",
            "half a block is text, and text is not dropped"
        );
    }

    // ---- streaming tool calls ----

    /// The bug this lane exists to fix, in one assertion: OpenCode streams by
    /// default, and every agentic turn used to render the raw block as
    /// assistant prose. Now it is structure, and the markup is gone.
    #[test]
    fn a_streamed_call_becomes_a_tool_calls_delta_and_no_raw_markup() {
        let mut engine = StubEngine::with_ids(&glob_call_tokens());
        let recorder = Recorder::new();
        run_stream_for(
            &mut engine,
            &request_with_tools(),
            recorder.clone(),
            Duration::from_secs(60),
        );

        let body = recorder.body();
        assert!(
            !body.contains("tool_call>"),
            "the raw markup is still on the wire: {body}"
        );

        let calls = streamed_calls(&body);
        assert_eq!(calls.len(), 1, "{body}");
        assert_eq!(calls[0]["index"], 0);
        assert_eq!(calls[0]["type"], "function");
        assert_eq!(calls[0]["function"]["name"], "glob");
        assert_eq!(
            calls[0]["function"]["arguments"], r#"{"pattern": "*/.rs"}"#,
            "the model's argument bytes reach the client unaltered"
        );
        // The stop reason is overridden: the model stopped to call a tool.
        assert_eq!(streamed_finish(&body), "tool_calls");
        assert_eq!(streamed_text(&body), "", "the block left no prose behind");
        assert!(body.contains("[DONE]"), "{body}");
        assert!(body.ends_with(TERMINATOR), "{body}");
    }

    /// Prose is still prose. Only the blocks are withheld, and only for as long
    /// as they are open.
    #[test]
    fn prose_before_between_and_after_streamed_calls_is_still_content() {
        let mut tokens = vec![(TEXT_ID, "Let me "), (TEXT_ID, "look.\n")];
        tokens.extend(glob_call_tokens());
        tokens.push((TEXT_ID, "\nand the tests\n"));
        tokens.extend(glob_call_tokens());
        tokens.push((TEXT_ID, "\nThen I will compare."));

        let mut engine = StubEngine::with_ids(&tokens);
        let recorder = Recorder::new();
        run_stream_for(
            &mut engine,
            &request_with_tools(),
            recorder.clone(),
            Duration::from_secs(60),
        );

        let body = recorder.body();
        assert_eq!(
            streamed_text(&body),
            "Let me look.\n\nand the tests\n\nThen I will compare."
        );
        assert!(!body.contains("tool_call>"), "{body}");
    }

    /// Two calls in one turn are told apart by `index` and by nothing else.
    #[test]
    fn two_streamed_calls_get_indices_zero_and_one() {
        let mut tokens = glob_call_tokens();
        tokens.extend(glob_call_tokens());
        let mut engine = StubEngine::with_ids(&tokens);
        let recorder = Recorder::new();
        run_stream_for(
            &mut engine,
            &request_with_tools(),
            recorder.clone(),
            Duration::from_secs(60),
        );

        let body = recorder.body();
        let calls = streamed_calls(&body);
        assert_eq!(calls.len(), 2, "{body}");
        assert_eq!(calls[0]["index"], 0);
        assert_eq!(calls[1]["index"], 1);
        assert_ne!(calls[0]["id"], calls[1]["id"]);
        assert_eq!(streamed_finish(&body), "tool_calls");
        // Each call is complete in one delta, so a client never has to
        // reassemble a half-written `arguments` string.
        for call in &calls {
            assert_eq!(call["function"]["arguments"], r#"{"pattern": "*/.rs"}"#);
        }
    }

    /// A block the token budget cut in half is not a call. It is text, it is
    /// flushed, and the reason the generation actually ended stands.
    #[test]
    fn an_unterminated_streamed_block_flushes_as_content_and_keeps_length() {
        let mut engine = StubEngine::with_ids(&[
            (TEXT_ID, "Reading it.\n"),
            (crate::toolcall::OPEN_TAG_ID, "<tool_call>"),
            (TEXT_ID, "\n{\"name\": \"glob\", \"argum"),
        ]);
        engine.finish_reason = FinishReason::Length;
        let recorder = Recorder::new();
        run_stream_for(
            &mut engine,
            &request_with_tools(),
            recorder.clone(),
            Duration::from_secs(60),
        );

        let body = recorder.body();
        assert!(streamed_calls(&body).is_empty(), "{body}");
        assert_eq!(streamed_finish(&body), "length");
        assert_eq!(
            streamed_text(&body),
            "Reading it.\n<tool_call>\n{\"name\": \"glob\", \"argum",
            "half a block is text, and text is not dropped"
        );
    }

    /// A request that declared no tools gets the block back as text. The model
    /// only writes these markers because the template's tools branch asked for
    /// them, so without tools they are an answer, not a call.
    #[test]
    fn a_streamed_block_without_tools_stays_ordinary_text() {
        let mut engine = StubEngine::with_ids(&glob_call_tokens());
        let recorder = Recorder::new();
        run_stream(&mut engine, recorder.clone(), Duration::from_secs(60));

        let body = recorder.body();
        assert!(
            streamed_calls(&body).is_empty(),
            "a request without tools got tool_calls back: {body}"
        );
        assert_eq!(streamed_finish(&body), "stop");
        assert_eq!(
            streamed_text(&body),
            "<tool_call>\n{\"name\": \"glob\", \"arguments\": {\"pattern\": \"*/.rs\"}}\n</tool_call>",
            "byte for byte, markers and all"
        );
    }

    /// Models get this JSON wrong routinely, and a dropped turn would be the
    /// cost. The call goes out with whatever the `arguments` span held.
    #[test]
    fn malformed_json_in_a_streamed_block_still_yields_a_call() {
        let mut engine = StubEngine::with_ids(&[
            (crate::toolcall::OPEN_TAG_ID, "<tool_call>"),
            (
                TEXT_ID,
                "\n{\"name\": \"glob\", \"arguments\": {\"pattern\": \"*\",}}\n",
            ),
            (crate::toolcall::CLOSE_TAG_ID, "</tool_call>"),
        ]);
        let recorder = Recorder::new();
        run_stream_for(
            &mut engine,
            &request_with_tools(),
            recorder.clone(),
            Duration::from_secs(60),
        );

        let body = recorder.body();
        let calls = streamed_calls(&body);
        assert_eq!(calls.len(), 1, "{body}");
        assert_eq!(calls[0]["function"]["name"], "glob");
        assert_eq!(calls[0]["function"]["arguments"], r#"{"pattern": "*",}"#);
        assert_eq!(streamed_finish(&body), "tool_calls");
    }

    /// The two paths name the same call identically, so a client that retries a
    /// turn without `stream` gets ids it can still match against its table.
    #[test]
    fn streamed_call_ids_match_the_buffered_paths_for_the_same_completion() {
        const ID: &str = "chatcmpl-18f3a0001";
        let mut engine = StubEngine::with_ids(&glob_call_tokens());
        let recorder = Recorder::new();
        let request = request_with_tools();
        let plan = engine.prepare(&request).expect("a plan");
        stream_into(
            &mut engine,
            &request,
            plan,
            recorder.clone(),
            ID,
            1_700_000_000,
            Duration::from_secs(60),
        );

        let body = recorder.body();
        let streamed = streamed_calls(&body);
        // What the buffered path would have produced from the same reply.
        let reply: String = glob_call_tokens().iter().map(|(_, text)| *text).collect();
        let buffered = crate::toolcall::wire_calls(ID, &crate::toolcall::extract(&reply).calls);
        assert_eq!(buffered.len(), 1);
        assert_eq!(streamed[0]["id"], buffered[0].id);
        assert_eq!(streamed[0]["id"], "call_18f3a0001_0");
    }

    /// The framing rules do not change because a delta carries a call: one
    /// event per chunk, hex length of the payload only.
    #[test]
    fn a_tool_call_delta_is_framed_like_every_other_event() {
        let mut engine = StubEngine::with_ids(&glob_call_tokens());
        let recorder = Recorder::new();
        run_stream_for(
            &mut engine,
            &request_with_tools(),
            recorder.clone(),
            Duration::from_secs(60),
        );

        let body = recorder.body();
        let mut rest = body.strip_prefix(SSE_HEAD).expect("head first");
        let mut events = 0usize;
        loop {
            let (head, tail) = rest.split_once("\r\n").expect("a chunk header");
            let len = usize::from_str_radix(head, 16).expect("hex length");
            if len == 0 {
                assert_eq!(tail, "\r\n", "the terminator ends the body");
                break;
            }
            assert_eq!(&tail[len..len + 2], "\r\n", "chunk not CRLF-terminated");
            let payload = &tail[..len];
            assert_eq!(payload.matches("\n\n").count(), 1, "{payload:?}");
            events += 1;
            rest = &tail[len + 2..];
        }
        // role, the call, finish, [DONE].
        assert_eq!(events, 4, "{body}");
    }

    /// The array-form `content` every SDK sends, end to end. A server that
    /// models `content` as a `String` answers a question it never received.
    #[test]
    fn the_array_form_content_reaches_the_engine() {
        let address = spawn_server();
        let response = post(
            address,
            PATH_CHAT_COMPLETIONS,
            r#"{"model":"stub-model","messages":[
                {"role":"user","content":[{"type":"text","text":"hi"}]}]}"#,
        );
        assert!(response.starts_with("HTTP/1.1 200"), "{response}");
        assert!(response.contains(r#""content":"ok""#), "{response}");
    }

    #[test]
    fn a_malformed_body_is_a_400_with_the_openai_shape() {
        let address = spawn_server();
        let response = post(address, PATH_CHAT_COMPLETIONS, "{not json");
        assert!(response.starts_with("HTTP/1.1 400"), "{response}");
        assert!(response.contains(r#""code":"invalid_json""#), "{response}");
        assert!(!response.contains(r#""message":null"#), "{response}");
    }

    /// Every byte off the socket is untrusted, and the body is read into
    /// memory to be parsed. Inside a 3 GB cgroup an uncapped read is an OOM.
    #[test]
    fn an_oversized_body_is_refused_by_its_declared_length() {
        const BODY: &str = "0123456789abcdef0123456789abcdef";
        let mut request = tiny_http::TestRequest::new()
            .with_method(Method::Post)
            .with_path(PATH_CHAT_COMPLETIONS)
            .with_body(BODY)
            .into();
        let error = read_body(&mut request, 16).expect_err("over the cap");
        assert!(matches!(error, ServerError::BodyTooLarge { limit: 16 }));
        assert_eq!(error.status(), 413);
    }

    #[test]
    fn a_body_at_the_cap_is_accepted() {
        const BODY: &str = "0123456789abcdef";
        let mut request = tiny_http::TestRequest::new()
            .with_method(Method::Post)
            .with_path(PATH_CHAT_COMPLETIONS)
            .with_body(BODY)
            .into();
        assert_eq!(
            read_body(&mut request, BODY.len()).expect("at the cap"),
            BODY
        );
    }

    #[test]
    fn completion_ids_do_not_repeat() {
        let ids: std::collections::HashSet<String> = (0..64).map(|_| completion_id()).collect();
        assert_eq!(ids.len(), 64);
        assert!(ids.iter().all(|id| id.starts_with("chatcmpl-")));
    }
}