atm-agent-mcp 0.14.0

MCP proxy for managing Codex agent sessions with ATM team integration
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
//! Integration tests for the MCP proxy.
//!
//! These tests spawn the `echo-mcp-server` binary as the child process and
//! exercise the proxy's message routing, tool interception, event forwarding,
//! timeout, and crash detection.

use serde_json::{Value, json};
use std::path::PathBuf;
use std::time::Duration;
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader, DuplexStream};

/// Find the path to the `echo-mcp-server` test binary.
fn echo_mcp_server_path() -> PathBuf {
    let mut path = std::env::current_exe().unwrap();
    path.pop(); // remove test binary name
    if path.ends_with("deps") {
        path.pop();
    }
    path.push("echo-mcp-server");
    path
}

/// Create a proxy config pointed at our echo server with an isolated team name.
///
/// Each call generates a unique team name so that concurrent integration tests
/// don't conflict on lock files (which use `<team>/<identity>.lock`).
fn test_config(timeout_secs: u64) -> atm_agent_mcp::proxy::ProxyServer {
    use atm_agent_mcp::config::AgentMcpConfig;

    let config = AgentMcpConfig {
        codex_bin: echo_mcp_server_path().to_string_lossy().to_string(),
        request_timeout_secs: timeout_secs,
        // Disable auto-mail in integration tests to prevent the idle poller
        // from interfering with test timing (it reads ATM inboxes and spawns
        // background tasks that outlive the proxy run loop).
        auto_mail: false,
        ..Default::default()
    };
    // Use a unique team per test invocation so lock files don't collide across
    // concurrently running integration tests.
    let unique_team = format!("test-{}", uuid::Uuid::new_v4());
    atm_agent_mcp::proxy::ProxyServer::new_with_team(config, unique_team)
}

/// Send a JSON-RPC message to the proxy via Content-Length framing.
async fn send_content_length(writer: &mut DuplexStream, msg: &Value) {
    let json = serde_json::to_string(msg).unwrap();
    let header = format!("Content-Length: {}\r\n\r\n", json.len());
    writer.write_all(header.as_bytes()).await.unwrap();
    writer.write_all(json.as_bytes()).await.unwrap();
    writer.flush().await.unwrap();
}

/// Send a JSON-RPC message in newline-delimited format.
async fn send_newline(writer: &mut DuplexStream, msg: &Value) {
    let json = serde_json::to_string(msg).unwrap();
    writer.write_all(json.as_bytes()).await.unwrap();
    writer.write_all(b"\n").await.unwrap();
    writer.flush().await.unwrap();
}

/// Read a Content-Length framed response from the proxy.
async fn read_response(reader: &mut BufReader<DuplexStream>) -> Option<Value> {
    let mut header_line = String::new();

    // Try to read the Content-Length header with a timeout
    match tokio::time::timeout(Duration::from_secs(10), async {
        loop {
            header_line.clear();
            let n = reader.read_line(&mut header_line).await.ok()?;
            if n == 0 {
                return None;
            }
            let trimmed = header_line.trim();
            if trimmed.starts_with("Content-Length:") {
                break;
            }
            // skip blank lines or other headers
        }
        Some(())
    })
    .await
    {
        Ok(Some(())) => {}
        Ok(None) | Err(_) => return None,
    }

    let len: usize = header_line
        .trim()
        .strip_prefix("Content-Length:")
        .unwrap()
        .trim()
        .parse()
        .unwrap();

    // Read until blank line
    loop {
        let mut line = String::new();
        reader.read_line(&mut line).await.ok()?;
        if line.trim().is_empty() {
            break;
        }
    }

    let mut body = vec![0u8; len];
    tokio::io::AsyncReadExt::read_exact(reader, &mut body)
        .await
        .ok()?;
    let s = String::from_utf8(body).ok()?;
    serde_json::from_str(&s).ok()
}

/// Read all responses available within a timeout.
async fn read_all_responses(
    reader: &mut BufReader<DuplexStream>,
    timeout_duration: Duration,
) -> Vec<Value> {
    let mut results = Vec::new();
    let deadline = tokio::time::Instant::now() + timeout_duration;
    loop {
        let remaining = deadline.saturating_duration_since(tokio::time::Instant::now());
        if remaining.is_zero() {
            break;
        }
        match tokio::time::timeout(remaining, read_response(reader)).await {
            Ok(Some(v)) => results.push(v),
            _ => break,
        }
    }
    results
}

/// Helper: run a proxy with a pair of duplex streams.
///
/// Returns (write_end, read_end, join_handle) where:
/// - write_end: send messages TO the proxy
/// - read_end: read messages FROM the proxy
fn spawn_proxy(
    timeout_secs: u64,
) -> (
    DuplexStream,
    BufReader<DuplexStream>,
    tokio::task::JoinHandle<anyhow::Result<()>>,
) {
    let (client_write, proxy_read) = tokio::io::duplex(16384);
    let (proxy_write, client_read) = tokio::io::duplex(16384);

    let handle = tokio::spawn(async move {
        let mut proxy = test_config(timeout_secs);
        proxy.run(proxy_read, proxy_write).await
    });

    (client_write, BufReader::new(client_read), handle)
}

// ─── Initialize handled by proxy ────────────────────────────────────────

#[tokio::test]
async fn test_initialize_passes_through() {
    let (mut writer, mut reader, handle) = spawn_proxy(300);

    // The proxy now handles `initialize` itself (no child spawn required).
    // It responds with the proxy's own serverInfo regardless of child state.
    let init_req = json!({
        "jsonrpc": "2.0",
        "id": 2,
        "method": "initialize",
        "params": {
            "protocolVersion": "2025-03-26",
            "capabilities": {}
        }
    });
    send_content_length(&mut writer, &init_req).await;

    let resp = read_response(&mut reader).await.expect("initialize response");
    assert_eq!(resp["id"], 2);
    assert!(resp.get("result").is_some(), "initialize should succeed");
    assert_eq!(
        resp["result"]["serverInfo"]["name"],
        "atm-agent-mcp",
        "proxy must respond with its own serverInfo, not the child's"
    );

    drop(writer);
    let _ = handle.await;
}

// ─── Notifications initialized pass-through ─────────────────────────────

#[tokio::test]
async fn test_notifications_initialized_passes_through() {
    let (mut writer, _reader, handle) = spawn_proxy(300);

    // Notifications don't get responses, so we just verify no crash
    let notif = json!({
        "jsonrpc": "2.0",
        "method": "notifications/initialized"
    });
    send_newline(&mut writer, &notif).await;

    // Small delay to let proxy process
    tokio::time::sleep(Duration::from_millis(100)).await;

    drop(writer);
    let _ = handle.await;
}

// ─── tools/list interception ────────────────────────────────────────────

#[tokio::test]
async fn test_tools_list_adds_synthetic_tools() {
    let (mut writer, mut reader, handle) = spawn_proxy(300);

    // First spawn child
    let codex_req = json!({
        "jsonrpc": "2.0",
        "id": 1,
        "method": "tools/call",
        "params": {"name": "codex", "arguments": {"prompt": "init"}}
    });
    send_newline(&mut writer, &codex_req).await;
    let _ = read_all_responses(&mut reader, Duration::from_secs(5)).await;

    // Now send tools/list
    let list_req = json!({
        "jsonrpc": "2.0",
        "id": 10,
        "method": "tools/list"
    });
    send_newline(&mut writer, &list_req).await;

    let resp = read_response(&mut reader).await.expect("tools/list response");
    assert_eq!(resp["id"], 10);
    let tools = resp["result"]["tools"].as_array().expect("tools array");

    let names: Vec<&str> = tools
        .iter()
        .filter_map(|t| t.get("name").and_then(|n| n.as_str()))
        .collect();

    // Synthetic tools must be present
    assert!(names.contains(&"atm_send"), "missing atm_send");
    assert!(names.contains(&"atm_read"), "missing atm_read");
    assert!(names.contains(&"atm_broadcast"), "missing atm_broadcast");
    assert!(
        names.contains(&"atm_pending_count"),
        "missing atm_pending_count"
    );
    assert!(
        names.contains(&"agent_sessions"),
        "missing agent_sessions"
    );
    assert!(names.contains(&"agent_status"), "missing agent_status");
    assert!(names.contains(&"agent_close"), "missing agent_close");

    drop(writer);
    let _ = handle.await;
}

#[tokio::test]
async fn test_tools_list_preserves_codex_tools() {
    let (mut writer, mut reader, handle) = spawn_proxy(300);

    // Spawn child
    let codex_req = json!({
        "jsonrpc": "2.0",
        "id": 1,
        "method": "tools/call",
        "params": {"name": "codex", "arguments": {"prompt": "init"}}
    });
    send_newline(&mut writer, &codex_req).await;
    let _ = read_all_responses(&mut reader, Duration::from_secs(5)).await;

    let list_req = json!({
        "jsonrpc": "2.0",
        "id": 20,
        "method": "tools/list"
    });
    send_newline(&mut writer, &list_req).await;

    let resp = read_response(&mut reader).await.expect("tools/list response");
    let tools = resp["result"]["tools"].as_array().expect("tools array");
    let names: Vec<&str> = tools
        .iter()
        .filter_map(|t| t.get("name").and_then(|n| n.as_str()))
        .collect();

    assert!(names.contains(&"codex"), "original codex tool missing");
    assert!(
        names.contains(&"codex-reply"),
        "original codex-reply tool missing"
    );

    drop(writer);
    let _ = handle.await;
}

#[tokio::test]
async fn test_multiple_synthetic_tools_count() {
    let (mut writer, mut reader, handle) = spawn_proxy(300);

    // Spawn child
    let codex_req = json!({
        "jsonrpc": "2.0",
        "id": 1,
        "method": "tools/call",
        "params": {"name": "codex", "arguments": {"prompt": "init"}}
    });
    send_newline(&mut writer, &codex_req).await;
    let _ = read_all_responses(&mut reader, Duration::from_secs(5)).await;

    let list_req = json!({
        "jsonrpc": "2.0",
        "id": 30,
        "method": "tools/list"
    });
    send_newline(&mut writer, &list_req).await;

    let resp = read_response(&mut reader).await.expect("tools/list response");
    let tools = resp["result"]["tools"].as_array().expect("tools array");

    // 2 from echo server + 7 synthetic = 9
    assert_eq!(tools.len(), 9, "expected 2 native + 7 synthetic tools");

    drop(writer);
    let _ = handle.await;
}

// ─── Unknown method pass-through ────────────────────────────────────────

#[tokio::test]
async fn test_unknown_method_passes_through() {
    let (mut writer, mut reader, handle) = spawn_proxy(300);

    // Spawn child first
    let codex_req = json!({
        "jsonrpc": "2.0",
        "id": 1,
        "method": "tools/call",
        "params": {"name": "codex", "arguments": {"prompt": "init"}}
    });
    send_newline(&mut writer, &codex_req).await;
    let _ = read_all_responses(&mut reader, Duration::from_secs(5)).await;

    // Send unknown method
    let req = json!({
        "jsonrpc": "2.0",
        "id": 40,
        "method": "custom/foobar",
        "params": {}
    });
    send_newline(&mut writer, &req).await;

    let resp = read_response(&mut reader).await.expect("should get error response");
    assert_eq!(resp["id"], 40);
    // The echo server returns -32601 for unknown methods
    assert_eq!(resp["error"]["code"], -32601);

    drop(writer);
    let _ = handle.await;
}

// ─── Lazy spawn tests ───────────────────────────────────────────────────

#[tokio::test]
async fn test_lazy_spawn_on_first_codex_call() {
    let (mut writer, mut reader, handle) = spawn_proxy(300);

    // Before any codex call: send tools/list — now returns synthetic tools
    // (proxy handles tools/list without needing the child to be spawned).
    let list_req = json!({
        "jsonrpc": "2.0",
        "id": 1,
        "method": "tools/list"
    });
    send_newline(&mut writer, &list_req).await;

    let resp = read_response(&mut reader).await.expect("tools/list response");
    assert_eq!(resp["id"], 1);
    // Should return synthetic tools, not an error
    assert!(
        resp.get("error").is_none(),
        "tools/list before child spawn should return synthetic tools, not an error; got: {resp}"
    );
    assert!(
        resp["result"]["tools"].is_array(),
        "expected tools array in result"
    );

    // Now send codex — this should spawn the child
    let codex_req = json!({
        "jsonrpc": "2.0",
        "id": 2,
        "method": "tools/call",
        "params": {"name": "codex", "arguments": {"prompt": "hello"}}
    });
    send_newline(&mut writer, &codex_req).await;

    // Should get events + response now
    let responses = read_all_responses(&mut reader, Duration::from_secs(5)).await;
    assert!(!responses.is_empty(), "should have received response(s)");

    // Find the response with id=2
    let main_resp = responses.iter().find(|r| r.get("id") == Some(&json!(2)));
    assert!(
        main_resp.is_some(),
        "should have the codex response with id=2"
    );

    drop(writer);
    let _ = handle.await;
}

// ─── Child crash tests ──────────────────────────────────────────────────

#[tokio::test]
async fn test_child_crash_returns_error() {
    let (mut writer, mut reader, handle) = spawn_proxy(300);

    // Spawn child
    let codex_req = json!({
        "jsonrpc": "2.0",
        "id": 1,
        "method": "tools/call",
        "params": {"name": "codex", "arguments": {"prompt": "init"}}
    });
    send_newline(&mut writer, &codex_req).await;
    let _ = read_all_responses(&mut reader, Duration::from_secs(5)).await;

    // Crash the child
    let crash_req = json!({
        "jsonrpc": "2.0",
        "id": 2,
        "method": "tools/call",
        "params": {"name": "crash", "arguments": {}}
    });
    send_newline(&mut writer, &crash_req).await;

    // Wait for child to die
    tokio::time::sleep(Duration::from_millis(500)).await;

    // Next request should return dead child error
    let codex_req2 = json!({
        "jsonrpc": "2.0",
        "id": 3,
        "method": "tools/call",
        "params": {"name": "codex", "arguments": {"prompt": "after crash"}}
    });
    send_newline(&mut writer, &codex_req2).await;

    let responses = read_all_responses(&mut reader, Duration::from_secs(5)).await;
    let error_resp = responses.iter().find(|r| {
        r.get("id") == Some(&json!(3))
            && r.pointer("/error/code").and_then(|v| v.as_i64()) == Some(-32005)
    });
    assert!(
        error_resp.is_some(),
        "expected -32005 CHILD_PROCESS_DEAD error, got: {responses:?}"
    );

    drop(writer);
    let _ = handle.await;
}

#[tokio::test]
async fn test_child_crash_includes_exit_code() {
    let (mut writer, mut reader, handle) = spawn_proxy(300);

    // Spawn child
    let codex_req = json!({
        "jsonrpc": "2.0",
        "id": 1,
        "method": "tools/call",
        "params": {"name": "codex", "arguments": {"prompt": "init"}}
    });
    send_newline(&mut writer, &codex_req).await;
    let _ = read_all_responses(&mut reader, Duration::from_secs(5)).await;

    // Crash the child (exit code 42)
    let crash_req = json!({
        "jsonrpc": "2.0",
        "id": 2,
        "method": "tools/call",
        "params": {"name": "crash", "arguments": {}}
    });
    send_newline(&mut writer, &crash_req).await;
    tokio::time::sleep(Duration::from_millis(500)).await;

    // Send another request
    let req = json!({
        "jsonrpc": "2.0",
        "id": 3,
        "method": "tools/call",
        "params": {"name": "codex", "arguments": {"prompt": "after"}}
    });
    send_newline(&mut writer, &req).await;

    let responses = read_all_responses(&mut reader, Duration::from_secs(5)).await;
    let error_resp = responses.iter().find(|r| r.get("id") == Some(&json!(3)));
    assert!(error_resp.is_some(), "expected error response");

    let err = error_resp.unwrap();
    let exit_code = err.pointer("/error/data/exit_code").and_then(|v| v.as_i64());
    assert_eq!(exit_code, Some(42), "expected exit code 42");

    drop(writer);
    let _ = handle.await;
}

// ─── Timeout tests ──────────────────────────────────────────────────────

#[tokio::test]
async fn test_request_timeout_returns_error() {
    // Use a 1-second timeout
    let (mut writer, mut reader, handle) = spawn_proxy(1);

    // Send a slow codex call (echo server sleeps 5s)
    let req = json!({
        "jsonrpc": "2.0",
        "id": 1,
        "method": "tools/call",
        "params": {"name": "codex", "arguments": {"prompt": "slow", "slow": true}}
    });
    send_newline(&mut writer, &req).await;

    let responses = read_all_responses(&mut reader, Duration::from_secs(10)).await;
    let timeout_resp = responses.iter().find(|r| {
        r.get("id") == Some(&json!(1))
            && r.pointer("/error/code").and_then(|v| v.as_i64()) == Some(-32006)
    });
    assert!(
        timeout_resp.is_some(),
        "expected -32006 timeout error, got: {responses:?}"
    );

    drop(writer);
    let _ = handle.await;
}

#[tokio::test]
async fn test_timeout_includes_proxy_source() {
    let (mut writer, mut reader, handle) = spawn_proxy(1);

    let req = json!({
        "jsonrpc": "2.0",
        "id": 1,
        "method": "tools/call",
        "params": {"name": "codex", "arguments": {"prompt": "slow", "slow": true}}
    });
    send_newline(&mut writer, &req).await;

    let responses = read_all_responses(&mut reader, Duration::from_secs(10)).await;
    let timeout_resp = responses.iter().find(|r| {
        r.pointer("/error/code").and_then(|v| v.as_i64()) == Some(-32006)
    });
    assert!(timeout_resp.is_some(), "expected timeout error");
    assert_eq!(
        timeout_resp.unwrap().pointer("/error/data/error_source"),
        Some(&json!("proxy"))
    );

    drop(writer);
    let _ = handle.await;
}

// ─── Event forwarding tests ─────────────────────────────────────────────

#[tokio::test]
async fn test_codex_event_forwarded_to_upstream() {
    let (mut writer, mut reader, handle) = spawn_proxy(300);

    // Codex call triggers 2 events before the response
    let req = json!({
        "jsonrpc": "2.0",
        "id": 1,
        "method": "tools/call",
        "params": {"name": "codex", "arguments": {"prompt": "hello"}}
    });
    send_newline(&mut writer, &req).await;

    let responses = read_all_responses(&mut reader, Duration::from_secs(5)).await;

    let events: Vec<&Value> = responses
        .iter()
        .filter(|r| r.get("method") == Some(&json!("codex/event")))
        .collect();
    assert!(
        events.len() >= 2,
        "expected at least 2 codex/event notifications, got {}",
        events.len()
    );

    drop(writer);
    let _ = handle.await;
}

#[tokio::test]
async fn test_codex_event_has_agent_id() {
    let (mut writer, mut reader, handle) = spawn_proxy(300);

    let req = json!({
        "jsonrpc": "2.0",
        "id": 1,
        "method": "tools/call",
        "params": {"name": "codex", "arguments": {"prompt": "hello"}}
    });
    send_newline(&mut writer, &req).await;

    let responses = read_all_responses(&mut reader, Duration::from_secs(5)).await;
    let events: Vec<&Value> = responses
        .iter()
        .filter(|r| r.get("method") == Some(&json!("codex/event")))
        .collect();

    for event in &events {
        let agent_id = event
            .pointer("/params/agent_id")
            .and_then(|v| v.as_str());
        // Events without a known threadId mapping fall back to "proxy:unknown"
        assert!(
            agent_id.is_some(),
            "event should have an agent_id field"
        );
    }

    drop(writer);
    let _ = handle.await;
}

#[tokio::test]
async fn test_event_content_unchanged() {
    let (mut writer, mut reader, handle) = spawn_proxy(300);

    let req = json!({
        "jsonrpc": "2.0",
        "id": 1,
        "method": "tools/call",
        "params": {"name": "codex", "arguments": {"prompt": "hello"}}
    });
    send_newline(&mut writer, &req).await;

    let responses = read_all_responses(&mut reader, Duration::from_secs(5)).await;
    let events: Vec<&Value> = responses
        .iter()
        .filter(|r| r.get("method") == Some(&json!("codex/event")))
        .collect();

    assert!(!events.is_empty(), "expected events");
    // Check that the msg content is preserved from the echo server
    let first_event = events[0];
    let msg_type = first_event
        .pointer("/params/msg/type")
        .and_then(|v| v.as_str());
    assert!(
        msg_type.is_some(),
        "event msg.type should be present"
    );
    // The echo server sends "session_configured" as the first event type
    assert_eq!(msg_type, Some("session_configured"));

    drop(writer);
    let _ = handle.await;
}

// ─── Proxy lifecycle tests ──────────────────────────────────────────────

#[tokio::test]
async fn test_proxy_shuts_down_on_stdin_eof() {
    let (writer, _reader, handle) = spawn_proxy(300);

    // Drop the writer immediately — proxy should exit
    drop(writer);

    let result = tokio::time::timeout(Duration::from_secs(5), handle).await;
    assert!(result.is_ok(), "proxy should exit on stdin EOF");
    assert!(result.unwrap().is_ok(), "proxy should exit without panic");
}

#[tokio::test]
async fn test_tools_list_schema_valid() {
    // Verify all synthetic tools have valid JSON Schema inputSchema
    let tools = atm_agent_mcp::tools::synthetic_tools();
    for tool in &tools {
        let name = tool["name"].as_str().unwrap();
        let schema = tool
            .get("inputSchema")
            .unwrap_or_else(|| panic!("{name} missing inputSchema"));
        assert_eq!(
            schema["type"].as_str(),
            Some("object"),
            "{name} inputSchema type should be 'object'"
        );
        assert!(
            schema.get("properties").is_some(),
            "{name} inputSchema should have properties"
        );
    }
}

// ─── codex-reply pass-through ───────────────────────────────────────────

#[tokio::test]
async fn test_codex_reply_passes_through() {
    let (mut writer, mut reader, handle) = spawn_proxy(300);

    // First spawn with codex
    let codex_req = json!({
        "jsonrpc": "2.0",
        "id": 1,
        "method": "tools/call",
        "params": {"name": "codex", "arguments": {"prompt": "start session"}}
    });
    send_newline(&mut writer, &codex_req).await;
    let _ = read_all_responses(&mut reader, Duration::from_secs(5)).await;

    // Now send codex-reply
    let reply_req = json!({
        "jsonrpc": "2.0",
        "id": 2,
        "method": "tools/call",
        "params": {
            "name": "codex-reply",
            "arguments": {"prompt": "continue", "threadId": "test-thread-001"}
        }
    });
    send_newline(&mut writer, &reply_req).await;

    let responses = read_all_responses(&mut reader, Duration::from_secs(5)).await;
    let main_resp = responses.iter().find(|r| r.get("id") == Some(&json!(2)));
    assert!(main_resp.is_some(), "should get codex-reply response");

    let resp = main_resp.unwrap();
    let content = resp
        .pointer("/result/structuredContent/threadId")
        .and_then(|v| v.as_str());
    assert_eq!(content, Some("test-thread-001"));

    drop(writer);
    let _ = handle.await;
}

// ─── Synthetic ATM tool dispatch ─────────────────────────────────────────

/// ATM tools require an identity.  When no identity is configured on the proxy
/// and none is provided in arguments, the proxy must return ERR_IDENTITY_REQUIRED
/// (-32009) as a JSON-RPC error (not an `isError` result).
///
/// This test replaced the Sprint A.2/A.3 stub test which expected `isError:true`
/// with "not yet implemented" — ATM tools are real as of Sprint A.4.
#[tokio::test]
async fn test_synthetic_tool_returns_not_implemented() {
    let (mut writer, mut reader, handle) = spawn_proxy(300);

    // atm_send requires identity; default proxy config has none → ERR_IDENTITY_REQUIRED
    let req = json!({
        "jsonrpc": "2.0",
        "id": 1,
        "method": "tools/call",
        "params": {
            "name": "atm_send",
            "arguments": {"to": "agent1", "message": "hello"}
        }
    });
    send_newline(&mut writer, &req).await;

    let resp = read_response(&mut reader).await.expect("synthetic tool response");
    assert_eq!(resp["id"], 1);
    // Must be a JSON-RPC error (not a result)
    let code = resp
        .pointer("/error/code")
        .and_then(|v| v.as_i64())
        .expect("error code must be present");
    assert_eq!(
        code,
        atm_agent_mcp::proxy::ERR_IDENTITY_REQUIRED,
        "atm_send without identity should return ERR_IDENTITY_REQUIRED (-32009)"
    );

    drop(writer);
    let _ = handle.await;
}

// ─── Content-Length upstream framing ─────────────────────────────────────

#[tokio::test]
async fn test_content_length_upstream_framing() {
    let (mut writer, mut reader, handle) = spawn_proxy(300);

    // Send a synthetic tool call using Content-Length framing
    let req = json!({
        "jsonrpc": "2.0",
        "id": 1,
        "method": "tools/call",
        "params": {
            "name": "agent_status",
            "arguments": {}
        }
    });
    send_content_length(&mut writer, &req).await;

    let resp = read_response(&mut reader).await.expect("response to CL-framed request");
    assert_eq!(resp["id"], 1);
    // agent_status is fully implemented in Sprint A.5; it returns a successful result.
    assert!(resp.get("error").is_none(), "should not be a protocol error; got: {resp}");
    assert_ne!(
        resp["result"]["isError"],
        json!(true),
        "agent_status should return a success result; got: {resp}"
    );

    drop(writer);
    let _ = handle.await;
}

// ─── Dropped events counter ─────────────────────────────────────────────

#[tokio::test]
async fn test_dropped_events_counter_accessible() {
    use atm_agent_mcp::config::AgentMcpConfig;
    use std::sync::atomic::Ordering;

    let config = AgentMcpConfig::default();
    let proxy = atm_agent_mcp::proxy::ProxyServer::new(config);
    assert_eq!(proxy.dropped_events.load(Ordering::Relaxed), 0);
}

// ─── initialize before child spawn ──────────────────────────────────────

#[tokio::test]
async fn test_initialize_returns_capabilities() {
    let (mut writer, mut reader, handle) = spawn_proxy(5);

    send_content_length(
        &mut writer,
        &json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "initialize",
            "params": {
                "protocolVersion": "2024-11-05",
                "capabilities": {},
                "clientInfo": {"name": "test", "version": "0"}
            }
        }),
    )
    .await;

    let response = read_response(&mut reader)
        .await
        .expect("should get initialize response");
    assert!(
        response.get("error").is_none(),
        "should not have error: {response}"
    );
    let result = &response["result"];
    assert_eq!(result["protocolVersion"], "2024-11-05");
    assert_eq!(result["serverInfo"]["name"], "atm-agent-mcp");

    drop(writer);
    let _ = handle.await;
}

// ─── tools/list before child spawn ──────────────────────────────────────

#[tokio::test]
async fn test_tools_list_before_child_spawn_returns_synthetic_tools() {
    let (mut writer, mut reader, handle) = spawn_proxy(5);

    send_content_length(
        &mut writer,
        &json!({"jsonrpc": "2.0", "id": 2, "method": "tools/list", "params": {}}),
    )
    .await;

    let response = read_response(&mut reader)
        .await
        .expect("should get tools/list response");
    assert!(
        response.get("error").is_none(),
        "should not have error: {response}"
    );
    let tools = response["result"]["tools"]
        .as_array()
        .expect("tools should be array");
    assert_eq!(
        tools.len(),
        7,
        "expected 7 synthetic tools, got {}",
        tools.len()
    );
    let names: Vec<&str> = tools
        .iter()
        .filter_map(|t| t["name"].as_str())
        .collect();
    for expected in &[
        "atm_send",
        "atm_read",
        "atm_broadcast",
        "atm_pending_count",
        "agent_sessions",
        "agent_status",
        "agent_close",
    ] {
        assert!(
            names.contains(expected),
            "missing tool: {expected}, got: {names:?}"
        );
    }

    drop(writer);
    let _ = handle.await;
}

// ─── notifications/initialized before child spawn ───────────────────────

#[tokio::test]
async fn test_notifications_initialized_does_not_produce_response() {
    let (mut writer, mut reader, handle) = spawn_proxy(5);

    // Notifications have no id field — proxy must NOT send a response
    send_content_length(
        &mut writer,
        &json!({"jsonrpc": "2.0", "method": "notifications/initialized", "params": {}}),
    )
    .await;

    let result = tokio::time::timeout(
        Duration::from_millis(200),
        read_response(&mut reader),
    )
    .await;
    assert!(result.is_err(), "notification must not produce a response");

    drop(writer);
    let _ = handle.await;
}