cpa-exec 0.4.3

Part of cliproxy: upstream provider executors
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
//! The xAI upstream WebSocket against goldens from the pinned Go `XAIAutoExecutor`
//! (`tests/fixtures/xai_ws_go.json`, generated by `tests/reference/xai_ws`): each
//! scenario's turns run in order on one downstream session against a scripted loopback
//! upstream (axum WebSocket for `/v1/responses`, HTTP for `/v1/responses/compact`), and
//! every turn's downstream chunks, error, upstream frames, compaction requests and
//! handshake headers are compared.

use super::*;
use std::sync::Mutex as StdMutex;

use axum::extract::ws::{CloseFrame, Message as AxMessage, WebSocket as AxSocket, WebSocketUpgrade};
use axum::extract::{FromRequestParts, State};
use axum::response::{IntoResponse, Response};
use cpa_core::exec::{Caller, Operation};
use serde_json::Value;

use crate::openai_compat_usage::{Captured, published, sinks};

/// Clock-derived timestamps (`created_at`/`completed_at` from now, when the compact reply
/// has none) as `"<now>"`.
fn clock(text: &str) -> String {
    static NOW: std::sync::LazyLock<regex::Regex> =
        std::sync::LazyLock::new(|| regex::Regex::new(r#""(created_at|completed_at)":(1[7-9]\d{8})"#).unwrap());
    NOW.replace_all(text, |c: &regex::Captures| {
        if c[2].parse::<i64>().unwrap_or(0) >= 1_750_000_000 {
            format!("\"{}\":\"<now>\"", &c[1])
        } else {
            c[0].to_owned()
        }
    })
    .into_owned()
}

const FIXTURE: &str = include_str!("../tests/fixtures/xai_ws_go.json");

/// The handshake headers compared with Go. Go's dialer also sends `User-Agent:
/// Go-http-client/1.1` and offers `permessage-deflate`; those belong to the shared dialer.
const COMPARED_HEADERS: [&str; 4] = ["Authorization", "Content-Type", "X-Grok-Conv-Id", "X-Trace"];

/// Turns whose Go error text is gorilla's close message, which the shared reader does
/// not keep (see `pool_error`): only status and retry hint are compared.
const GORILLA_CLOSE_TEXT: [&str; 1] = ["upstream_closes_mid_turn"];

#[derive(Default)]
struct Recorded {
    upgrades: Vec<String>,
    frames: Vec<String>,
    http: Vec<String>,
    pongs: Vec<String>,
}

#[derive(Default)]
struct Script {
    turn: Option<Value>,
    recorded: Recorded,
}

type Shared = Arc<StdMutex<Script>>;

fn canonical(name: &str) -> String {
    name.split('-')
        .map(|part| {
            let mut chars = part.chars();
            match chars.next() {
                Some(first) => first.to_ascii_uppercase().to_string() + &chars.as_str().to_ascii_lowercase(),
                None => String::new(),
            }
        })
        .collect::<Vec<_>>()
        .join("-")
}

async fn handler(State(shared): State<Shared>, req: axum::extract::Request) -> Response {
    let (mut parts, body) = req.into_parts();
    let uri = parts
        .uri
        .path_and_query()
        .map(|p| p.as_str().to_owned())
        .unwrap_or_default();
    if parts.headers.contains_key("upgrade") {
        let mut lines: Vec<String> = COMPARED_HEADERS
            .iter()
            .filter_map(|name| {
                parts
                    .headers
                    .get(name.to_ascii_lowercase())
                    .map(|v| format!("{}: {}", canonical(name), v.to_str().unwrap()))
            })
            .collect();
        lines.sort();
        let reject = {
            let mut script = shared.lock().unwrap();
            script.recorded.upgrades.push(format!("{uri}\n{}", lines.join("\n")));
            script
                .turn
                .as_ref()
                .and_then(|t| t.get("reject"))
                .filter(|r| !r.is_null())
                .cloned()
        };
        if let Some(reject) = reject {
            let status = http::StatusCode::from_u16(reject["status"].as_u64().unwrap() as u16).unwrap();
            return (
                status,
                [("content-type", "application/json")],
                reject["body"].as_str().unwrap().to_owned(),
            )
                .into_response();
        }
        let upgrade = WebSocketUpgrade::from_request_parts(&mut parts, &()).await.unwrap();
        return upgrade.on_upgrade(move |socket| serve_socket(socket, shared));
    }
    let body = axum::body::to_bytes(body, usize::MAX).await.unwrap();
    let compact = {
        let mut script = shared.lock().unwrap();
        script
            .recorded
            .http
            .push(format!("{} {uri}\n{}", parts.method, String::from_utf8_lossy(&body)));
        script
            .turn
            .as_ref()
            .and_then(|t| t.get("compact"))
            .filter(|r| !r.is_null())
            .cloned()
    };
    match compact {
        Some(reply) => (
            http::StatusCode::from_u16(reply["status"].as_u64().unwrap() as u16).unwrap(),
            [("content-type", "application/json")],
            reply["body"].as_str().unwrap().to_owned(),
        )
            .into_response(),
        None => http::StatusCode::from_u16(599).unwrap().into_response(),
    }
}

async fn serve_socket(mut socket: AxSocket, shared: Shared) {
    while let Some(Ok(message)) = socket.recv().await {
        let AxMessage::Text(text) = message else { continue };
        let previous = gjson::get(text.as_str(), "previous_response_id").str().to_owned();
        let act = {
            let mut script = shared.lock().unwrap();
            script.recorded.frames.push(text.as_str().to_owned());
            let n = script.recorded.frames.len();
            script
                .turn
                .as_ref()
                .and_then(|t| t["acts"].as_array())
                .and_then(|acts| acts.get(n - 1))
                .cloned()
        };
        let Some(act) = act else { continue };
        if let Some(ping) = act["ping"].as_str() {
            let _ = socket.send(AxMessage::Ping(ping.as_bytes().to_vec().into())).await;
            let deadline = tokio::time::Instant::now() + Duration::from_secs(2);
            while let Ok(Some(Ok(message))) = tokio::time::timeout_at(deadline, socket.recv()).await {
                if let AxMessage::Pong(data) = message {
                    let pong = String::from_utf8_lossy(&data).into_owned();
                    shared.lock().unwrap().recorded.pongs.push(pong);
                    break;
                }
            }
        }
        let quoted = serde_json::to_string(&previous).unwrap();
        for frame in act["send"].as_array().into_iter().flatten() {
            let frame = frame.as_str().unwrap().replace("\"PREVIOUS_ID\"", &quoted);
            if socket.send(AxMessage::text(frame)).await.is_err() {
                return;
            }
        }
        if act["drop"].as_bool().unwrap_or(false) {
            // No close frame: the connection just ends.
            return;
        }
        if act["binary"].as_bool().unwrap_or(false) {
            let _ = socket.send(AxMessage::Binary(vec![1u8, 2, 3].into())).await;
        }
        if let Some(code) = act["close"].as_u64().filter(|c| *c != 0) {
            let _ = socket
                .send(AxMessage::Close(Some(CloseFrame {
                    code: code as u16,
                    reason: act["close_text"].as_str().unwrap_or_default().into(),
                })))
                .await;
            return;
        }
    }
}

fn credential(id: &str, attrs: &Value, addr: &str) -> Credential {
    let mut metadata = serde_json::Map::new();
    metadata.insert("type".into(), "xai".into());
    let mut c = Credential::from_file(
        std::path::Path::new("/fixture"),
        std::path::Path::new("/fixture/xai.json"),
        metadata,
    )
    .unwrap();
    c.id = id.to_owned();
    for (k, v) in attrs.as_object().unwrap() {
        if k == "access_token" {
            c.metadata.insert(k.clone(), v.clone());
            continue;
        }
        c.attributes
            .insert(k.clone(), v.as_str().unwrap().replace("UPSTREAM", addr));
    }
    c
}

fn request(turn: &Value, session: &str, usage: cpa_core::exec::UsageSink) -> ExecRequest {
    let payload = Bytes::from(turn["payload"].as_str().unwrap().to_owned());
    let headers = HeaderMap::new();
    // Go's executor-side session fallback (EnsureSessionContext -> CanonicalSessionID).
    let meta = cpa_common::session::Meta {
        execution_session: Some(session),
        derived: None,
    };
    let id = cpa_common::session::extract_session_id(&headers, &payload, &meta);
    let model = turn["model"].as_str().unwrap_or("grok-4.3").to_owned();
    ExecRequest {
        operation: Operation::Generate,
        source_format: Format::OpenAIResponse,
        response_format: turn["response"]
            .as_str()
            .and_then(Format::parse)
            .unwrap_or(Format::OpenAIResponse),
        stream_payloads: false,
        requested_model: model.clone(),
        model,
        original_body: payload.clone(),
        body: payload,
        stream: true,
        alt: None,
        session: Some(id)
            .filter(|id| !id.is_empty())
            .map(|id| cpa_common::session::bound_session_identity(&id)),
        execution_session: Some(session.to_owned()),
        derived_session: None,
        resolved_model: None,
        usage,
        request_path: "/v1/responses".into(),
        headers,
        caller: Caller {
            principal: String::new(),
            source: "authorization",
            provider: String::new(),
        },
    }
}

fn error_json(e: &ExecError) -> Value {
    serde_json::json!({
        "status": e.status,
        "message": String::from_utf8_lossy(&e.body),
        "retry_after_ms": e.retry_after.map_or(0, |d| d.as_millis() as i64),
    })
}

#[tokio::test]
async fn turns_match_go() {
    let scenarios: Vec<Value> = serde_json::from_str(FIXTURE).unwrap();
    assert_eq!(scenarios.len(), 49);
    let mut failures = Vec::new();
    for s in &scenarios {
        let name = s["name"].as_str().unwrap();
        let shared: Shared = Arc::default();
        let app = axum::Router::new().fallback(handler).with_state(shared.clone());
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap().to_string();
        let server = tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
        let executor = XaiExecutor::with_client(wreq::Client::new());
        let cfg = Config::default();
        let session = s["session"].as_str().unwrap();
        let credentials: HashMap<String, Credential> = s["auths"]
            .as_object()
            .unwrap()
            .iter()
            .map(|(id, attrs)| (id.clone(), credential(id, attrs, &addr)))
            .collect();
        let normalize = |text: &str| text.replace(&addr, "UPSTREAM");
        for (i, turn) in s["turns"].as_array().unwrap().iter().enumerate() {
            {
                let mut script = shared.lock().unwrap();
                script.turn = Some(turn.clone());
                script.recorded = Recorded::default();
            }
            let mut chunks: Vec<String> = Vec::new();
            let mut error = Value::Null;
            let (recorder, capture, usage) = sinks();
            if turn["close"].as_bool().unwrap_or(false) {
                executor.close_session(session);
            } else {
                let credential = &credentials[turn["auth"].as_str().unwrap()];
                let exec_session = ExecSession {
                    id: session.to_owned(),
                    continuation: turn["continuation"].as_bool().unwrap_or(false),
                    lease: None,
                };
                match executor
                    .execute_in_session(credential, request(turn, session, usage), &cfg, &exec_session)
                    .await
                {
                    Err(e) => error = error_json(&e),
                    Ok(response) => {
                        let ResponseBody::Stream(mut stream) = response.body else {
                            panic!("{name}: buffered response");
                        };
                        while let Some(item) = stream.next().await {
                            match item {
                                Ok(chunk) => chunks.push(clock(&normalize(&String::from_utf8_lossy(&chunk)))),
                                Err(e) => error = error_json(&e),
                            }
                        }
                    }
                }
            }
            tokio::time::sleep(Duration::from_millis(50)).await;
            let recorded = std::mem::take(&mut shared.lock().unwrap().recorded);
            let mut diffs = Vec::new();
            let want_chunks: Vec<String> = turn["chunks"]
                .as_array()
                .unwrap()
                .iter()
                .map(|c| clock(c.as_str().unwrap()))
                .collect();
            if chunks != want_chunks {
                diffs.push(format!("chunks:\n  go   {want_chunks:#?}\n  rust {chunks:#?}"));
            }
            let mut want_error = turn["error"].clone();
            if let Some(message) = want_error.get_mut("message") {
                *message = Value::String(normalize(message.as_str().unwrap()));
            }
            if let Some(message) = error.get_mut("message") {
                *message = Value::String(normalize(message.as_str().unwrap()));
            }
            if GORILLA_CLOSE_TEXT.contains(&name) {
                for e in [&mut error, &mut want_error] {
                    if let Some(map) = e.as_object_mut() {
                        map.remove("message");
                    }
                }
            }
            if error != want_error {
                diffs.push(format!("error:\n  go   {want_error}\n  rust {error}"));
            }
            // Go's usage record for the turn, or its absence, against what the server
            // publishes from this turn's reports (CloseExecutionSession publishes none;
            // an error Go raises before its reporter exists reports `discard`).
            let reports = recorder.0.lock().unwrap();
            let want_usage = turn.get("usage").filter(|u| !u.is_null()).cloned();
            if !turn["close"].as_bool().unwrap_or(false) {
                let got_usage = published(&reports, !error.is_null());
                if got_usage != want_usage {
                    diffs.push(format!("usage:\n  go   {want_usage:?}\n  rust {got_usage:?}"));
                }
            }
            drop(reports);
            if !turn["close"].as_bool().unwrap_or(false) {
                // A frame or a compact request went upstream; a message or compact body
                // came back.
                let acts = turn["acts"].as_array().cloned().unwrap_or_default();
                let message = recorded
                    .frames
                    .iter()
                    .zip(&acts)
                    .flat_map(|(_, act)| act["send"].as_array().cloned().unwrap_or_default())
                    .any(|m| !m.as_str().unwrap().trim().is_empty());
                let compact_body =
                    !recorded.http.is_empty() && turn["compact"]["body"].as_str().is_some_and(|b| !b.is_empty());
                let sent = !recorded.frames.is_empty() || !recorded.http.is_empty();
                if let Some(problem) =
                    crate::openai_compat_usage::ttft_problem(&recorder.0.lock().unwrap(), sent, message || compact_body)
                {
                    diffs.push(problem);
                }
                let credential = &credentials[turn["auth"].as_str().unwrap()];
                diffs.extend(capture_problems(
                    name,
                    turn,
                    &recorded,
                    &capture.take(),
                    credential,
                    &addr,
                ));
            }
            for (field, got) in [
                ("frames", &recorded.frames),
                ("http", &recorded.http),
                ("pongs", &recorded.pongs),
            ] {
                let got: Vec<String> = got.iter().map(|g| normalize(g)).collect();
                let want: Vec<String> = turn[field]
                    .as_array()
                    .unwrap()
                    .iter()
                    .map(|v| v.as_str().unwrap().to_owned())
                    .collect();
                if got != want {
                    diffs.push(format!("{field}:\n  go   {want:#?}\n  rust {got:#?}"));
                }
            }
            let want_upgrades: Vec<String> = turn["upgrades"]
                .as_array()
                .unwrap()
                .iter()
                .map(|u| {
                    let text = u.as_str().unwrap();
                    let mut lines = text.lines();
                    let uri = lines.next().unwrap_or_default().to_owned();
                    let kept: Vec<&str> = lines
                        .filter(|l| COMPARED_HEADERS.iter().any(|h| l.starts_with(&format!("{h}: "))))
                        .collect();
                    format!("{uri}\n{}", kept.join("\n"))
                })
                .collect();
            let got_upgrades: Vec<String> = recorded.upgrades.iter().map(|u| normalize(u)).collect();
            if got_upgrades != want_upgrades {
                diffs.push(format!(
                    "upgrades:\n  go   {want_upgrades:#?}\n  rust {got_upgrades:#?}"
                ));
            }
            if !diffs.is_empty() {
                failures.push(format!("== {name} turn {i}\n{}", diffs.join("\n")));
            }
        }
        executor.close_session(session);
        server.abort();
    }
    assert!(
        failures.is_empty(),
        "{} turn(s) differ from Go:\n{}",
        failures.len(),
        failures.join("\n\n")
    );
}

/// Turns that log their WebSocket request although no frame goes out: Go records it
/// before it finds the continuation has no socket.
const WS_REQUEST_WITHOUT_FRAME: [&str; 1] = ["continuation_without_socket"];

/// One capture event as the order check sees it.
fn shape(event: &Captured) -> String {
    match event {
        Captured::Request {
            websocket: true,
            url,
            body,
            ..
        } => format!("ws-request {url} {body}"),
        Captured::Request { method, url, body, .. } => format!("http-request {method} {url} {body}"),
        Captured::Metadata(status, _) => format!("metadata {status}"),
        Captured::Chunk(body) => format!("chunk {body}"),
        Captured::Error(error) => format!("error {error}"),
        Captured::Handshake(status, _) => format!("handshake {status}"),
        Captured::WsResponse(payload) => format!("response {payload}"),
        Captured::WsError(stage, _) => format!("ws-error {stage}"),
    }
}

/// The turn's capture by the rules of Go's call sites, built from what the upstream saw:
/// a compact request is an HTTP attempt (`recordXAIRequest`, metadata, whole body); a
/// WebSocket turn logs its request frame (`RecordAPIWebsocketRequest`), then a rejected
/// upgrade as an HTTP attempt (GET on the http URL, metadata, body) or a new socket's
/// 101 handshake, then every upstream message (`AppendAPIWebsocketResponse`) and the
/// terminal error's stage.
fn capture_problems(
    name: &str,
    turn: &Value,
    recorded: &Recorded,
    events: &[Captured],
    credential: &Credential,
    addr: &str,
) -> Vec<String> {
    let mut problems = Vec::new();
    let mut want = Vec::new();
    for http in &recorded.http {
        let (line, body) = http.split_once('\n').unwrap_or((http, ""));
        let uri = line.split(' ').nth(1).unwrap_or_default();
        let reply = &turn["compact"];
        want.push(format!("http-request POST http://{addr}{uri} {body}"));
        want.push(format!("metadata {}", reply["status"]));
        want.push(format!("chunk {}", reply["body"].as_str().unwrap()));
    }
    let uri = recorded.upgrades.first().map(|u| u.lines().next().unwrap_or_default());
    let ws_url = format!("ws://{addr}{}", uri.unwrap_or("/v1/responses"));
    let websocket =
        !recorded.upgrades.is_empty() || !recorded.frames.is_empty() || WS_REQUEST_WITHOUT_FRAME.contains(&name);
    if websocket {
        match recorded.frames.first() {
            Some(frame) => want.push(format!("ws-request {ws_url} {frame}")),
            None => want.push(format!("ws-request {ws_url} <any>")),
        }
        let reject = turn.get("reject").filter(|r| !r.is_null());
        for _ in &recorded.upgrades {
            match reject {
                Some(reply) => {
                    want.push(format!("http-request GET http://{addr}{} ", uri.unwrap()));
                    want.push(format!("metadata {}", reply["status"]));
                    want.push(format!("chunk {}", reply["body"].as_str().unwrap()));
                }
                None => want.push("handshake 101".to_owned()),
            }
        }
        let acts = turn["acts"].as_array().cloned().unwrap_or_default();
        let mut stage = None;
        for (frame, act) in recorded.frames.iter().zip(&acts) {
            let previous = gjson::get(frame, "previous_response_id").str().to_owned();
            let quoted = serde_json::to_string(&previous).unwrap();
            for message in act["send"].as_array().into_iter().flatten() {
                let message = message.as_str().unwrap().replace("\"PREVIOUS_ID\"", &quoted);
                let message = message.trim();
                if message.is_empty() {
                    continue;
                }
                want.push(format!("response {message}"));
                if crate::xai_ws::ws_error(message.as_bytes()).is_some() {
                    stage = Some("upstream_error");
                }
            }
            // The session reader hands a binary message, a close or a dropped connection
            // to the turn as a read error.
            if act["binary"] == true || act["close"].as_u64().is_some_and(|c| c != 0) || act["drop"] == true {
                stage = Some("read");
            }
        }
        // A read error with an unfinished apply_patch call ends through the bridge's
        // failure, which Go returns before logging the read.
        let bridge_failure = turn["error"]["message"] == cpa_translate::APPLY_PATCH_UPSTREAM_ERROR;
        if let Some(stage) = stage.filter(|s| !(*s == "read" && bridge_failure)) {
            want.push(format!("ws-error {stage}"));
        }
    }
    let got: Vec<String> = events
        .iter()
        .map(|e| match e {
            Captured::Request {
                websocket: true, url, ..
            } if recorded.frames.is_empty() => {
                format!("ws-request {url} <any>")
            }
            e => shape(e),
        })
        .collect();
    if got != want {
        problems.push(format!("capture:\n  want {want:#?}\n  got  {got:#?}"));
    }
    // RecordAPIWebsocketError logs the error the turn fails with (a gorilla close text
    // is not kept by the shared reader).
    let go_error = turn["error"]["message"]
        .as_str()
        .unwrap_or_default()
        .replace("UPSTREAM", addr);
    for event in events {
        match event {
            Captured::WsError(stage, text) if !GORILLA_CLOSE_TEXT.contains(&name) && *text != go_error => {
                problems.push(format!("capture {stage} error: {text:?} != {go_error:?}"));
            }
            Captured::Metadata(_, headers)
                if turn.get("reject").is_some_and(|r| !r.is_null())
                    && !headers
                        .iter()
                        .any(|(n, v)| n == "Content-Type" && v == "application/json") =>
            {
                problems.push(format!("capture rejection headers: {headers:?}"));
            }
            _ => {}
        }
    }
    let (auth_type, auth_value) = cpa_core::registry::dynamic::account_info(credential);
    for event in events {
        if let Captured::Request {
            websocket,
            method,
            headers,
            provider,
            auth,
            ..
        } = event
        {
            let want_auth = [
                credential.id.clone(),
                credential.label.clone(),
                auth_type.to_owned(),
                auth_value.clone(),
            ];
            if provider != "xai" || auth != &want_auth {
                problems.push(format!("capture account: {provider} {auth:?}"));
            }
            let has = |n: &str, v: &str| headers.iter().any(|(hn, hv)| hn == n && hv == v);
            if *websocket && method != "WEBSOCKET" {
                problems.push(format!("capture ws method: {method}"));
            }
            // websocketUpgradeRequestLog: the upgrade headers a rejected dial carried.
            if method == "GET" && !(has("Connection", "Upgrade") && has("Upgrade", "websocket")) {
                problems.push(format!("capture rejected upgrade headers: {headers:?}"));
            }
        }
    }
    problems
}

#[test]
fn websockets_flag_follows_go() {
    let mut c = credential("a", &serde_json::json!({}), "");
    assert!(!websockets_enabled(&c));
    c.metadata.insert("websockets".into(), Value::Bool(true));
    assert!(websockets_enabled(&c));
    c.attributes.insert("websockets".into(), "false".into());
    assert!(!websockets_enabled(&c), "a parsable attribute wins");
    c.attributes.insert("websockets".into(), "maybe".into());
    assert!(websockets_enabled(&c), "an unparsable attribute falls back to metadata");
    c.metadata.insert("websockets".into(), Value::String(" TRUE ".into()));
    assert!(websockets_enabled(&c));
    c.metadata.insert("websockets".into(), Value::from(1));
    assert!(!websockets_enabled(&c), "numbers are not bools");
}

/// `buildXAIResponsesWebsocketURL` answers, recorded from Go v8.0.35 with the inputs of `url.Parse` and
/// `URL.String()` (tests/reference/go_url).
#[test]
fn ws_url_follows_go() {
    let cases: Value = serde_json::from_str(include_str!("../tests/fixtures/go_url_string.json")).unwrap();
    for case in cases.as_array().unwrap() {
        let input = case["in"].as_str().unwrap();
        let want = &case["xai_ws"];
        match ws_url(input) {
            Ok(got) => assert_eq!(Some(got.as_str()), want["out"].as_str(), "{input}"),
            Err(e) => {
                assert_eq!(
                    String::from_utf8_lossy(&e.body),
                    want["err"].as_str().unwrap(),
                    "{input}"
                );
                // A plain Go error before dialing: no status, no retry round, and the
                // model cools for the credential.
                assert_eq!((e.status, e.scope), (0, cpa_core::exec::FailureScope::Model), "{input}");
            }
        }
    }
}

/// Go sets no read deadline on xAI sockets; the shared reader's must outlast any session.
#[test]
fn xai_sockets_have_no_read_deadline() {
    assert!(Ws::default().pool.idle >= Duration::from_secs(365 * 24 * 3600));
}

/// Go's reader always delivers its terminal read (`sendTerminalWebsocketRead` waits for
/// room). Here the queue is full when the close 1009 arrives: 4097 deltas fill the
/// 4096-slot queue while the consumer pauses, so the reader's error cannot be queued and
/// must still reach the turn as the request-scoped 413.
#[tokio::test]
async fn terminal_error_survives_a_full_queue() {
    let shared: Shared = Arc::default();
    let app = axum::Router::new().fallback(handler).with_state(shared.clone());
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap().to_string();
    let server = tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
    let delta = r#"{"type":"response.output_text.delta","item_id":"m","output_index":0,"content_index":0,"delta":"x"}"#;
    let turn = serde_json::json!({
        "auth": "auth-a",
        "payload": r#"{"model":"grok-4.3","input":[{"type":"message","role":"user","content":[{"type":"input_text","text":"hi"}]}],"stream":true}"#,
        "acts": [{"send": vec![delta; 4097], "close": 1009, "close_text": "too big"}],
    });
    shared.lock().unwrap().turn = Some(turn.clone());
    let attrs = serde_json::json!({"api_key": "sk-fake", "base_url": "http://UPSTREAM/v1", "websockets": "true"});
    let credential = credential("auth-a", &attrs, &addr);
    let executor = XaiExecutor::with_client(wreq::Client::new());
    let session = ExecSession {
        id: "sess-full".into(),
        continuation: false,
        lease: None,
    };
    let response = executor
        .execute_in_session(
            &credential,
            request(&turn, "sess-full", Default::default()),
            &Config::default(),
            &session,
        )
        .await
        .unwrap();
    let ResponseBody::Stream(mut stream) = response.body else {
        panic!("buffered response");
    };
    // Let the reader fill the queue, take one event so it queues the last delta, then pause
    // again while it reads the close.
    tokio::time::sleep(Duration::from_millis(300)).await;
    assert!(stream.next().await.unwrap().is_ok());
    tokio::time::sleep(Duration::from_millis(300)).await;
    let mut events = 1;
    let mut last = None;
    while let Some(item) = stream.next().await {
        match item {
            Ok(_) => events += 1,
            Err(e) => last = Some(e),
        }
    }
    assert_eq!(events, 4097);
    let error = last.expect("terminal error");
    assert_eq!(error.status, 413);
    assert_eq!(
        String::from_utf8_lossy(&error.body),
        r#"{"error":{"message":"upstream websocket message too big","type":"invalid_request_error","code":"message_too_big"}}"#
    );
    executor.close_session("sess-full");
    server.abort();
}

/// Go `EmitWebSocketResponseEvent` beside `AppendAPIWebsocketResponse`: every non-empty
/// upstream frame, trimmed, goes to the attempt's observer with the executor identifier,
/// the selected credential and the upstream model. The executor awaits each
/// observation before it handles the frame, as Go calls the observer in its read loop.
#[tokio::test]
async fn upstream_frames_reach_the_websocket_observer() {
    /// (provider, credential ID, model, payload).
    type Frame = (String, String, String, Vec<u8>);
    /// The frames observed, and the order observations and client chunks finished in.
    #[derive(Default)]
    struct Seen(std::sync::Mutex<Vec<Frame>>, Arc<std::sync::Mutex<Vec<&'static str>>>);
    impl cpa_core::exec::WebSocketObserver for Seen {
        fn response<'a>(
            &'a self,
            provider: &'a str,
            credential: &'a Credential,
            model: &'a str,
            payload: &'a [u8],
        ) -> futures_util::future::BoxFuture<'a, ()> {
            Box::pin(async move {
                // A slow observer: the frame must not reach the client before it returns.
                tokio::time::sleep(std::time::Duration::from_millis(50)).await;
                self.0.lock().unwrap().push((
                    provider.to_owned(),
                    credential.id.clone(),
                    model.to_owned(),
                    payload.to_vec(),
                ));
                self.1.lock().unwrap().push("observed");
            })
        }
    }
    let shared: Shared = Arc::default();
    let app = axum::Router::new().fallback(handler).with_state(shared.clone());
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap().to_string();
    let server = tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
    let created = r#"{"type":"response.created","response":{"id":"r1","status":"in_progress","output":[]}}"#;
    let done = r#"{"type":"response.completed","response":{"id":"r1","status":"completed","output":[],"usage":{"input_tokens":1,"output_tokens":1,"total_tokens":2}}}"#;
    let turn = serde_json::json!({
        "auth": "auth-a",
        "payload": r#"{"model":"grok-4.3","input":[{"type":"message","role":"user","content":[{"type":"input_text","text":"hi"}]}],"stream":true}"#,
        "acts": [{"send": [format!("  {created}\n"), "   ".to_owned(), done.to_owned()]}],
    });
    shared.lock().unwrap().turn = Some(turn.clone());
    let attrs = serde_json::json!({"api_key": "sk-fake", "base_url": "http://UPSTREAM/v1", "websockets": "true"});
    let credential = credential("auth-a", &attrs, &addr);
    let executor = XaiExecutor::with_client(wreq::Client::new());
    let session = ExecSession {
        id: "sess-observe".into(),
        continuation: false,
        lease: None,
    };
    let seen = Arc::new(Seen::default());
    let usage = cpa_core::exec::UsageSink::default().with_websocket(Some(seen.clone()));
    let response = executor
        .execute_in_session(
            &credential,
            request(&turn, "sess-observe", usage),
            &Config::default(),
            &session,
        )
        .await
        .unwrap();
    let ResponseBody::Stream(mut stream) = response.body else {
        panic!("buffered response");
    };
    let order = seen.1.clone();
    while let Some(item) = stream.next().await {
        item.unwrap();
        order.lock().unwrap().push("chunk");
    }
    // Each frame's observation finishes before its client chunk.
    let order = order.lock().unwrap().clone();
    assert_eq!(order.first(), Some(&"observed"), "{order:?}");
    assert_eq!(order.iter().filter(|o| **o == "observed").count(), 2, "{order:?}");
    assert_eq!(order.last(), Some(&"chunk"), "{order:?}");
    let seen = seen.0.lock().unwrap().clone();
    let expected: Vec<Frame> = [created, done]
        .into_iter()
        .map(|frame| {
            (
                "xai".into(),
                credential.id.clone(),
                "grok-4.3".into(),
                frame.as_bytes().to_vec(),
            )
        })
        .collect();
    assert_eq!(seen, expected);
    executor.close_session("sess-observe");
    server.abort();
}

// --- unit-level ports of xai_websockets_executor_test.go -------------------------------

/// The server's `classify::upstream_attempted` (Go `UpstreamAttempted`).
fn attempted(e: &ExecError) -> bool {
    !e.headers.is_empty() || e.scope == FailureScope::Transport || e.status == 0
}

/// TestXAIWebsocketsEnabledForConfigAPIKey, directly and through the config synthesizer.
#[test]
fn websockets_enabled_for_config_api_key() {
    let attrs = serde_json::json!({"api_key": "xai-key", "websockets": "true"});
    assert!(websockets_enabled(&credential("a", &attrs, "")));
    let cfg = Config::parse("api-keys:\n  xai:\n    - base-url: http://127.0.0.1:9/v1\n      keys:\n        - api-key: xai-key\n          websockets: true\n").unwrap();
    let synthesized = cpa_core::config::credentials::from_config(&cfg);
    let xai = synthesized
        .iter()
        .find(|c| c.provider == "xai")
        .expect("xai credential");
    assert!(websockets_enabled(xai));
}

/// TestXAIWebsocketMissingRequiredSessionDoesNotMarkUpstreamAttempt and
/// TestXAIWebsocketsRequiredUpstreamRejectsCompactionHTTPFallback: a continuation without
/// a retained socket is the request-scoped replay signal and no upstream attempt.
#[tokio::test]
async fn missing_required_session_is_replay_without_attempt() {
    let executor = XaiExecutor::with_client(wreq::Client::new());
    let attrs = serde_json::json!({"api_key": "xai-key", "websockets": "true", "base_url": "http://127.0.0.1:9/v1"});
    let credential = credential("xai-required-session", &attrs, "");
    let session = ExecSession {
        id: "missing-xai-session".into(),
        continuation: true,
        lease: None,
    };
    for payload in [
        r#"{"model":"grok-4","previous_response_id":"resp-1","input":[{"type":"message","role":"user","content":"hello"}]}"#,
        r#"{"model":"grok-4","input":[{"type":"compaction_trigger"}]}"#,
    ] {
        let turn = serde_json::json!({"model": "grok-4", "payload": payload});
        let Err(error) = executor
            .execute_in_session(
                &credential,
                request(&turn, "missing-xai-session", Default::default()),
                &Config::default(),
                &session,
            )
            .await
        else {
            panic!("{payload}: continuation succeeded");
        };
        assert!(error.is_replay_required(), "{payload}");
        assert_eq!(error.scope, FailureScope::Request);
        assert!(!attempted(&error), "{payload}: marked as an upstream attempt");
    }
}

/// TestXAIWebsocketsExecuteStreamHandshakeFreeUsageExhaustedSetsRetryAfter's
/// `UpstreamAttempted`: a rejected upgrade is an upstream attempt (Go marks it on the dial
/// error), while a successful handshake alone marks nothing
/// (TestXAIWebsocketSuccessfulHandshakeDoesNotMarkRequestAttempt: no error exists).
#[tokio::test]
async fn rejected_handshake_is_an_upstream_attempt() {
    let app = axum::Router::new().fallback(|| async {
        (
            http::StatusCode::TOO_MANY_REQUESTS,
            [("content-type", "application/json")],
            r#"{"code":"subscription:free-usage-exhausted","error":"You've used all the included free usage for now."}"#,
        )
    });
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap().to_string();
    let server = tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
    let attrs =
        serde_json::json!({"base_url": "http://UPSTREAM/v1", "websockets": "true", "access_token": "xai-token"});
    let credential = credential("xai-auth-free-usage", &attrs, &addr);
    let executor = XaiExecutor::with_client(wreq::Client::new());
    let turn = serde_json::json!({"payload": r#"{"model":"grok-4.3","input":"hello"}"#});
    let session = ExecSession {
        id: "sess-attempt".into(),
        continuation: false,
        lease: None,
    };
    let Err(error) = executor
        .execute_in_session(
            &credential,
            request(&turn, "sess-attempt", Default::default()),
            &Config::default(),
            &session,
        )
        .await
    else {
        panic!("rejected upgrade succeeded");
    };
    assert_eq!(error.status, 429);
    assert_eq!(error.retry_after, Some(Duration::from_secs(24 * 3600)));
    assert!(attempted(&error));
    server.abort();
}

/// TestMapXAIWebsocketWriteErrorStopsRetryForMessageTooBig: a write failure after the
/// upstream closed with 1009 is the request-scoped 413 and never retries; any other write
/// failure retries on a fresh socket.
#[test]
fn write_error_after_message_too_big_stops_retry() {
    let too_big = pool_error(ExecError::local(
        413,
        FailureScope::Request,
        r#"{"error":{"message":"frame too large","type":"invalid_request_error","code":"message_too_big"}}"#,
    ));
    assert_eq!(too_big.status, 413);
    assert_eq!(too_big.scope, FailureScope::Request);
    assert_eq!(
        String::from_utf8_lossy(&too_big.body),
        r#"{"error":{"message":"upstream websocket message too big","type":"invalid_request_error","code":"message_too_big"}}"#
    );
    assert!(!should_retry_send(&too_big));
    let stale = pool_error(ExecError::local(
        502,
        FailureScope::Transport,
        "codex websockets executor: write failed",
    ));
    assert!(should_retry_send(&stale));
    assert_eq!(
        String::from_utf8_lossy(&stale.body),
        "xai websockets executor: write failed"
    );
}

/// TestXAIWebsocketPostCompactionAppendWithoutPreviousReplaysCompactedTranscript.
#[test]
fn post_compaction_append_replays_compacted_transcript() {
    let state = Arc::new(IdState::default());
    state.replace(&[br#"{"type":"compaction","encrypted_content":"compact-state"}"#]);
    state.map("resp-compact", "");
    let input = |body: &[u8]| -> Vec<String> {
        gj::get(body, "input")
            .array()
            .iter()
            .map(|i| String::from_utf8_lossy(i.raw()).into_owned())
            .collect()
    };

    let full = br#"{"type":"response.create","model":"grok-4.3","input":[{"type":"message","id":"msg-full"}]}"#;
    let out = IdMapper::new(state.clone(), full).upstream_request(full.to_vec());
    assert_eq!(
        input(&out).len(),
        1,
        "a self-contained response.create replayed the transcript"
    );

    let append = br#"{"type":"response.append","model":"grok-4.3","input":[{"type":"message","id":"msg-2","role":"user","content":"second"}]}"#;
    let got = IdMapper::new(state.clone(), append).upstream_request(append.to_vec());
    let items = input(&got);
    assert_eq!(items.len(), 2, "{}", String::from_utf8_lossy(&got));
    assert_eq!(gj::get(items[0].as_bytes(), "type").str(), "compaction");
    assert_eq!(gj::get(items[1].as_bytes(), "id").str(), "msg-2");

    state.record(
        &got,
        br#"{"type":"response.completed","response":{"id":"resp-after-compact","output":[{"type":"message","id":"out-2"}]}}"#,
        true,
    );
    let next = br#"{"type":"response.create","model":"grok-4.3","input":[{"type":"message","id":"msg-3"}]}"#;
    let out = IdMapper::new(state, next).upstream_request(next.to_vec());
    let items = input(&out);
    assert_eq!(
        items.len(),
        1,
        "replay not cleared after success: {}",
        String::from_utf8_lossy(&out)
    );
    assert_eq!(gj::get(items[0].as_bytes(), "id").str(), "msg-3");
}

/// TestXAIWebsocketPostCompactionWarmupPreservesTranscriptForLaterCompaction.
#[test]
fn post_compaction_warmup_preserves_transcript() {
    let state = Arc::new(IdState::default());
    state.replace(&[br#"{"type":"compaction","encrypted_content":"compact-state"}"#]);
    let warmup = br#"{"type":"response.append","model":"grok-4.3","generate":false,"input":[{"type":"message","id":"warmup-context"}]}"#;
    let mut mapper = IdMapper::new(state.clone(), warmup);
    let warmup_upstream = mapper.upstream_request(warmup.to_vec());
    assert!(
        mapper.replayed_compacted,
        "post-compaction warmup did not mark the replay"
    );
    state.record(
        &warmup_upstream,
        br#"{"type":"response.completed","response":{"id":"resp-warmup","output":[]}}"#,
        true,
    );

    let append =
        br#"{"type":"response.append","model":"grok-4.3","input":[{"type":"message","id":"msg-after-warmup"}]}"#;
    let append_upstream = IdMapper::new(state.clone(), append).upstream_request(append.to_vec());
    let input = gj::get(&append_upstream, "input");
    assert_eq!(input.array().len(), 1, "{}", String::from_utf8_lossy(&append_upstream));
    assert_eq!(input.get("0.id").str(), "msg-after-warmup");
    state.record(
        &append_upstream,
        br#"{"type":"response.completed","response":{"id":"resp-after-warmup","output":[{"type":"message","id":"out-after-warmup"}]}}"#,
        false,
    );

    let transcript = state.snapshot().unwrap();
    let kinds: Vec<String> = gj::parse(&transcript)
        .array()
        .iter()
        .map(|i| i.get("type").str().into_owned())
        .collect();
    assert_eq!(kinds, ["compaction", "message", "message", "message"]);
}

/// TestXAIWebsocketEmptyFullResetClearsPendingCompactionReplay.
#[test]
fn empty_full_reset_clears_pending_compaction_replay() {
    let state = Arc::new(IdState::default());
    state.replace(&[br#"{"type":"compaction","encrypted_content":"stale-compact-state"}"#]);
    state.record(
        br#"{"type":"response.create","model":"grok-4.3","input":[]}"#,
        br#"{"type":"response.completed","response":{"id":"resp-empty","output":[]}}"#,
        true,
    );
    let append = br#"{"type":"response.append","model":"grok-4.3","input":[{"type":"message","id":"msg-new"}]}"#;
    let got = IdMapper::new(state, append).upstream_request(append.to_vec());
    let input = gj::get(&got, "input");
    assert_eq!(input.array().len(), 1, "{}", String::from_utf8_lossy(&got));
    assert_eq!(input.get("0.id").str(), "msg-new");
}

/// TestValidateXAIWebsocketCompactionResponse.
#[test]
fn validate_compaction_response_follows_go() {
    let (id, item) = validate_compaction(
        br#"{"id":"resp_compact","output":[{"type":"compaction","encrypted_content":"opaque-state"}]}"#,
    )
    .unwrap();
    assert_eq!(id, "resp_compact");
    assert_eq!(gj::get(&item, "encrypted_content").str(), "opaque-state");
    for payload in [
        &b""[..],
        b"{}",
        br#"{"id":"resp_empty","output":[]}"#,
        br#"{"id":123,"output":[{"type":"compaction","encrypted_content":"opaque"}]}"#,
        br#"{"id":"resp_object","output":{"0":{"type":"compaction","encrypted_content":"opaque"}}}"#,
        br#"{"id":"resp_numeric_state","output":[{"type":"compaction","encrypted_content":123}]}"#,
        br#"{"id":"resp_missing_state","output":[{"type":"compaction"}]}"#,
    ] {
        assert!(
            validate_compaction(payload).is_err(),
            "{}",
            String::from_utf8_lossy(payload)
        );
    }
}

/// TestBuildXAIWebsocketRequestBodySetsStoreAndKeepsPromptCacheKey.
#[test]
fn request_frame_sets_store_and_keeps_prompt_cache_key() {
    let frame = request_frame(br#"{"model":"grok-4.3","stream":true,"stream_options":{"include_usage":true},"background":true,"prompt_cache_key":"cache-1","previous_response_id":"resp-prev","instructions":"system prompt","input":[{"type":"message","role":"user","content":"hello"}]}"#, |b| b);
    assert_eq!(gj::get(&frame, "type").str(), "response.create");
    for absent in ["stream", "stream_options", "background", "instructions"] {
        assert!(
            !gj::get(&frame, absent).exists(),
            "{absent}: {}",
            String::from_utf8_lossy(&frame)
        );
    }
    assert_eq!(gj::get(&frame, "prompt_cache_key").str(), "cache-1");
    assert!(gj::get(&frame, "store").bool());
}

/// Without a capture observer the turn's capture keeps no copy of the request frame.
#[test]
fn disabled_capture_keeps_no_frame() {
    struct Ignore;
    impl cpa_core::exec::CaptureObserver for Ignore {
        fn record(&self, _: cpa_core::exec::CaptureEvent<'_>) {}
    }
    let turn = serde_json::json!({"payload": r#"{"model":"grok-4.3","input":"hi"}"#});
    let c = credential("xai-1", &serde_json::json!({"api_key": "sk-fake"}), "127.0.0.1:1");
    let frame = br#"{"type":"response.create","input":"hi"}"#;
    let headers = HeaderMap::new();
    let req = request(&turn, "s1", cpa_core::exec::UsageSink::default());
    let disabled = WsCapture::new(&req, &c, "ws://127.0.0.1:1/v1/responses", &headers, frame);
    assert!(disabled.frame.is_empty() && disabled.url.is_empty());
    let capture = cpa_core::exec::CaptureSink::new(Arc::new(Ignore));
    let req = request(&turn, "s1", cpa_core::exec::UsageSink::default().with_capture(capture));
    let enabled = WsCapture::new(&req, &c, "ws://127.0.0.1:1/v1/responses", &headers, frame);
    assert_eq!(enabled.frame, frame);
}

/// TestXAIWebsockets_PingHandlerDoesNotBlockOnWriteMu: an upstream ping is answered while
/// the socket's writer is held (Go holds `writeMu`; here the pooled socket's sink lock),
/// because the reader sends the pong.
#[tokio::test]
async fn ping_is_answered_while_the_writer_is_held() {
    let (pong_tx, mut pong_rx) = tokio::sync::mpsc::channel::<String>(1);
    let app = axum::Router::new().fallback(move |upgrade: WebSocketUpgrade| {
        let pong_tx = pong_tx.clone();
        async move {
            upgrade.on_upgrade(move |mut socket: AxSocket| async move {
                // The turn's request, then its completion.
                let _ = socket.recv().await;
                let completed = r#"{"type":"response.completed","response":{"id":"resp-1","output":[]}}"#;
                let _ = socket.send(AxMessage::text(completed)).await;
                // Ping once the test holds the writer.
                tokio::time::sleep(Duration::from_millis(300)).await;
                let _ = socket
                    .send(AxMessage::Ping(b"xai-keepalive-ping".to_vec().into()))
                    .await;
                while let Some(Ok(message)) = socket.recv().await {
                    if let AxMessage::Pong(data) = message {
                        let _ = pong_tx.send(String::from_utf8_lossy(&data).into_owned()).await;
                    }
                }
            })
        }
    });
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap().to_string();
    let server = tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
    let attrs = serde_json::json!({"api_key": "xai-key", "base_url": "http://UPSTREAM/v1", "websockets": "true"});
    let credential = credential("xai-ping", &attrs, &addr);
    let executor = XaiExecutor::with_client(wreq::Client::new());
    let session = ExecSession {
        id: "test-xai-keepalive".into(),
        continuation: false,
        lease: None,
    };
    let turn = serde_json::json!({"payload": r#"{"model":"grok-4","input":"ping"}"#});
    let response = executor
        .execute_in_session(
            &credential,
            request(&turn, "test-xai-keepalive", Default::default()),
            &Config::default(),
            &session,
        )
        .await
        .unwrap();
    let ResponseBody::Stream(mut stream) = response.body else {
        panic!("buffered response");
    };
    while stream.next().await.is_some() {}
    drop(stream);
    let conn = executor
        .ws
        .pool
        .session("test-xai-keepalive")
        .current()
        .expect("pooled socket");
    let _writer = conn.sink.lock().await;
    let pong = tokio::time::timeout(Duration::from_secs(2), pong_rx.recv())
        .await
        .expect("pong starved by the held writer");
    assert_eq!(pong.as_deref(), Some("xai-keepalive-ping"));
    executor.close_session("test-xai-keepalive");
    server.abort();
}

/// A loopback xAI upstream answering every frame with a completed `resp-1`, so a repeated
/// ID gets a `-xai-N` downstream suffix.
async fn repeating_upstream() -> (Shared, String, tokio::task::JoinHandle<()>) {
    let completed = r#"{"type":"response.completed","response":{"id":"resp-1","output":[],"usage":{"input_tokens":1,"output_tokens":1,"total_tokens":2}}}"#;
    let shared: Shared = Arc::default();
    let acts: Vec<Value> = (0..8).map(|_| serde_json::json!({"send": [completed]})).collect();
    shared.lock().unwrap().turn = Some(serde_json::json!({ "acts": acts }));
    let app = axum::Router::new().fallback(handler).with_state(shared.clone());
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap().to_string();
    let server = tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
    (shared, addr, server)
}

/// One turn on `session` with `previous_response_id` = `previous` (none when empty);
/// returns the downstream chunks.
async fn turn_with(
    executor: &XaiExecutor,
    credential: &Credential,
    session: &str,
    previous: &str,
    cfg: &Config,
    continuation: bool,
) -> Result<Vec<String>, ExecError> {
    let payload = if previous.is_empty() {
        r#"{"model":"grok-4.3","input":[{"type":"message","role":"user","content":"hi"}]}"#.to_owned()
    } else {
        format!(
            r#"{{"model":"grok-4.3","previous_response_id":"{previous}","input":[{{"type":"message","role":"user","content":"hi"}}]}}"#
        )
    };
    let req = request(&serde_json::json!({ "payload": payload }), session, Default::default());
    let exec_session = ExecSession {
        id: session.to_owned(),
        continuation,
        lease: None,
    };
    let response = executor.execute_in_session(credential, req, cfg, &exec_session).await?;
    let ResponseBody::Stream(stream) = response.body else {
        panic!("buffered response");
    };
    let chunks = stream
        .collect::<Vec<_>>()
        .await
        .into_iter()
        .collect::<Result<Vec<_>, _>>()?;
    Ok(chunks.iter().map(|c| String::from_utf8_lossy(c).into_owned()).collect())
}

fn upstream_previous(shared: &Shared) -> Vec<String> {
    let script = shared.lock().unwrap();
    script
        .recorded
        .frames
        .iter()
        .map(|f| gjson::get(f, "previous_response_id").str().to_owned())
        .collect()
}

/// Go 1e37b52b for xAI: after a reload that changes only `requests.proxy-url`, the open
/// socket keeps serving its credential and the session's upstream response IDs (a
/// downstream `resp-1-xai-1` still reaches upstream as `resp-1`), while a new session
/// dials through the new proxy.
#[tokio::test]
async fn a_global_proxy_reload_keeps_the_open_socket() {
    let (shared, addr, server) = repeating_upstream().await;
    let executor = XaiExecutor::with_client(wreq::Client::new());
    let attrs = serde_json::json!({"api_key": "xai-key", "websockets": "true", "base_url": "http://UPSTREAM/v1"});
    let credential = credential("xai-reload", &attrs, &addr);
    // Nothing listens on port 1: a dial through this proxy fails.
    let reloaded = Config::parse("requests:\n  proxy-url: http://127.0.0.1:1\n").unwrap();
    let session = "xai-reload-session";
    let default = Config::default();
    turn_with(&executor, &credential, session, "", &default, false)
        .await
        .unwrap();
    let second = turn_with(&executor, &credential, session, "resp-1", &default, true)
        .await
        .unwrap();
    assert!(
        second.iter().any(|c| c.contains(r#""id":"resp-1-xai-1""#)),
        "{second:?}"
    );
    for continuation in [true, false] {
        turn_with(&executor, &credential, session, "resp-1-xai-1", &reloaded, continuation)
            .await
            .unwrap_or_else(|e| panic!("continuation={continuation}: {e:?}"));
    }
    assert_eq!(
        shared.lock().unwrap().recorded.upgrades.len(),
        1,
        "every turn kept the first socket"
    );
    assert_eq!(
        upstream_previous(&shared),
        ["", "resp-1", "resp-1", "resp-1"],
        "the upstream ID mapping survived"
    );
    let error = turn_with(&executor, &credential, "xai-reload-new", "", &reloaded, false)
        .await
        .expect_err("a new socket dials through the reloaded proxy");
    assert_eq!(error.scope, FailureScope::Transport, "{error}");
    assert_eq!(shared.lock().unwrap().recorded.upgrades.len(), 1);
    executor.close_session(session);
    server.abort();
}

/// A credential removal drops a session and its ID state together, and a turn takes the
/// ID state only after it holds its session (Go `closeXAIWebsocketSessionsInStoreForAuthID`
/// and `ExecuteStream`). A turn that waits on its session while the removal runs keeps
/// its mapping in the live state, so the next turn still sends upstream `resp-1` for
/// the `resp-1-xai-1` that turn returned.
#[tokio::test]
async fn a_removal_during_a_turn_keeps_the_replacement_sessions_ids() {
    let (shared, addr, server) = repeating_upstream().await;
    let executor = Arc::new(XaiExecutor::with_client(wreq::Client::new()));
    let attrs = serde_json::json!({"api_key": "xai-key", "websockets": "true", "base_url": "http://UPSTREAM/v1"});
    let credential = credential("xai-removed", &attrs, &addr);
    let (session, cfg) = ("xai-race-session", Config::default());
    turn_with(&executor, &credential, session, "", &cfg, false)
        .await
        .unwrap();

    let (reached_tx, reached) = tokio::sync::oneshot::channel();
    let (resume, resume_rx) = tokio::sync::oneshot::channel();
    *executor.ws.pause.lock().unwrap() = Some((reached_tx, resume_rx));
    let racing = {
        let (executor, credential, cfg) = (executor.clone(), credential.clone(), cfg.clone());
        tokio::spawn(async move { turn_with(&executor, &credential, session, "resp-1", &cfg, false).await })
    };
    reached.await.expect("the turn holds its session");
    executor.ws.close_credential("xai-removed");
    resume.send(()).unwrap();
    let chunks = racing.await.unwrap().unwrap();
    assert!(
        chunks.iter().any(|c| c.contains(r#""id":"resp-1-xai-1""#)),
        "{chunks:?}"
    );

    turn_with(&executor, &credential, session, "resp-1-xai-1", &cfg, false)
        .await
        .unwrap();
    assert_eq!(upstream_previous(&shared), ["", "resp-1", "resp-1"]);
    executor.close_session(session);
    server.abort();
}

/// TestPayloadBarrierXAIWebsocketRetry (868e137): the rules run on the WebSocket frame,
/// after `store=true` and the instructions drop, and only the frame type is set after
/// them; a retry builds the same frame.
#[test]
fn request_frame_runs_the_payload_rules_last() {
    let cfg = Config::parse(
        r#"
payload:
  override:
    - models: [{name: "*"}]
      params: {store: false, instructions: configured}
  filter:
    - models: [{name: "*"}]
      params: ["input.0", "previous_response_id", "type"]
"#,
    )
    .unwrap();
    let body = br#"{"previous_response_id":"previous","input":[{"content":"first"},{"content":"second"}]}"#;
    let req = request(
        &serde_json::json!({"payload": std::str::from_utf8(body).unwrap()}),
        "s",
        Default::default(),
    );
    let finalize = |b| {
        crate::payload_barrier::Barrier {
            executor: crate::xai::PROVIDER,
            model: "grok-4.3",
            protocol: "codex",
            root: "",
            original: body,
        }
        .apply(&cfg, &req, b)
    };
    let first = request_frame(body, finalize);
    assert_eq!(first, request_frame(body, finalize));
    let text = String::from_utf8_lossy(&first);
    assert!(!gj::get(&first, "store").bool(), "{text}");
    assert_eq!(gj::get(&first, "instructions").str(), "configured", "{text}");
    assert!(!gj::get(&first, "previous_response_id").exists(), "{text}");
    assert_eq!(gj::get(&first, "input.#").int(), 1, "{text}");
    assert_eq!(gj::get(&first, "type").str(), "response.create", "{text}");
}