zeph-acp 0.22.4

ACP (Agent Client Protocol) server for IDE embedding
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
// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
// SPDX-License-Identifier: MIT OR Apache-2.0

#![cfg(all(test, feature = "acp-http"))]

use std::pin::Pin;
use std::sync::Arc;
use std::sync::atomic::AtomicU64;

use agent_client_protocol;
use axum::body::Body;
use axum::http::{Request, StatusCode};
use tower::ServiceExt as _;
use zeph_core::channel::LoopbackChannel;

use crate::agent::{AcpContext, SendAgentSpawner, SessionContext};
use crate::transport::http::{AcpHttpState, ConnectionHandle};
use crate::transport::router::acp_router;
use crate::transport::{AcpServerConfig, SharedAvailableModels};

fn shared_models(models: Vec<String>) -> SharedAvailableModels {
    std::sync::Arc::new(parking_lot::RwLock::new(models))
}

fn noop_spawner() -> SendAgentSpawner {
    Arc::new(
        |_channel: LoopbackChannel, _ctx: Option<AcpContext>, _session_ctx: SessionContext| {
            Box::pin(async {}) as Pin<Box<dyn std::future::Future<Output = ()> + Send + 'static>>
        },
    )
}

fn test_state() -> AcpHttpState {
    AcpHttpState::new(
        noop_spawner(),
        AcpServerConfig {
            agent_name: "test".into(),
            agent_version: "0.0.1".into(),
            max_sessions: 4,
            session_idle_timeout_secs: 1800,
            permission_file: None,
            provider_factory: None,
            available_models: shared_models(vec![]),
            mcp_manager: None,
            auth_clients: Vec::new(),
            discovery_enabled: true,
            terminal_timeout_secs: 120,
            project_rules: vec![],
            title_max_chars: 60,
            max_history: 100,
            sqlite_path: None,
            ready_notification: None,
            ..Default::default()
        },
    )
    .with_ready(true)
}

fn test_state_with_session_data_dir(data_dir: std::path::PathBuf) -> AcpHttpState {
    AcpHttpState::new(
        noop_spawner(),
        AcpServerConfig {
            agent_name: "test".into(),
            agent_version: "0.0.1".into(),
            max_sessions: 4,
            session_idle_timeout_secs: 1800,
            permission_file: None,
            provider_factory: None,
            available_models: shared_models(vec![]),
            mcp_manager: None,
            auth_clients: Vec::new(),
            discovery_enabled: true,
            terminal_timeout_secs: 120,
            project_rules: vec![],
            title_max_chars: 60,
            max_history: 100,
            sqlite_path: None,
            session_data_dir: Some(data_dir),
            ready_notification: None,
            ..Default::default()
        },
    )
    .with_ready(true)
}

fn state_with_max_sessions(max: usize) -> AcpHttpState {
    AcpHttpState::new(
        noop_spawner(),
        AcpServerConfig {
            agent_name: "test".into(),
            agent_version: "0.0.1".into(),
            max_sessions: max,
            session_idle_timeout_secs: 1800,
            permission_file: None,
            provider_factory: None,
            available_models: shared_models(vec![]),
            mcp_manager: None,
            auth_clients: Vec::new(),
            discovery_enabled: true,
            terminal_timeout_secs: 120,
            project_rules: vec![],
            title_max_chars: 60,
            max_history: 100,
            sqlite_path: None,
            ready_notification: None,
            ..Default::default()
        },
    )
    .with_ready(true)
}

// ── POST /acp tests ──────────────────────────────────────────────────────────

#[tokio::test]
async fn post_without_session_id_creates_new_connection_and_returns_sse() {
    let router = acp_router(test_state());

    let req = Request::builder()
        .method("POST")
        .uri("/acp")
        .header("content-type", "application/json")
        .body(Body::from(
            r#"{"jsonrpc":"2.0","id":1,"method":"initialize"}"#,
        ))
        .unwrap();

    let response = router.oneshot(req).await.unwrap();
    assert_eq!(response.status(), StatusCode::OK);
    assert!(response.headers().contains_key("acp-session-id"));
    let ct = response
        .headers()
        .get("content-type")
        .unwrap()
        .to_str()
        .unwrap();
    assert!(
        ct.contains("text/event-stream"),
        "expected SSE content-type, got: {ct}"
    );
}

#[tokio::test]
async fn post_with_existing_session_id_reuses_connection() {
    let state = test_state();
    let router = acp_router(state.clone());

    // First request — create session
    let req = Request::builder()
        .method("POST")
        .uri("/acp")
        .body(Body::from("{}"))
        .unwrap();
    let response = router.oneshot(req).await.unwrap();
    assert_eq!(response.status(), StatusCode::OK);
    let session_id = response
        .headers()
        .get("acp-session-id")
        .unwrap()
        .to_str()
        .unwrap()
        .to_owned();

    // Second request — reuse session
    let router2 = acp_router(state);
    let req2 = Request::builder()
        .method("POST")
        .uri("/acp")
        .header("acp-session-id", &session_id)
        .body(Body::from("{}"))
        .unwrap();
    let response2 = router2.oneshot(req2).await.unwrap();
    assert_eq!(response2.status(), StatusCode::OK);
    assert_eq!(
        response2
            .headers()
            .get("acp-session-id")
            .unwrap()
            .to_str()
            .unwrap(),
        session_id
    );
}

/// agent-client-protocol 2.0.0 added standard-transport support for JSON-RPC batches (an array
/// body instead of a single object). `post_handler` relays the raw HTTP body byte-for-byte into
/// the connection's duplex writer (`transport/http.rs`), so a batch now reaches the SDK's
/// dispatch loop end-to-end without any Zeph-side code change. This test proves both batch
/// entries are actually dispatched and individually answered — not silently dropped or
/// short-circuited after the first entry — by checking the response carries both request ids
/// (see spec.md "Breaking Changes Resolution (SDK 1.2.0 -> 2.0.0)"). Both entries below
/// deliberately omit `params` so both come back as individual `Invalid params` errors rather
/// than a real handshake — dispatch/response-tracking is what's under test, not `initialize`
/// semantics, and the SDK aggregates all batch replies into a single JSON array on one SSE line
/// (confirmed empirically), not one line per entry.
#[tokio::test]
async fn post_batch_body_dispatches_all_entries_and_returns_all_responses() {
    use std::collections::HashSet;
    use std::time::Duration;

    use futures::StreamExt as _;

    // Keep `state` alive for the whole test: it owns the `connections` map, which in turn owns
    // the duplex-pipe writer half and the broadcast sender the SSE stream reads from. Passing
    // only `acp_router(test_state())` inline would drop `state` (and close the pipe) as soon as
    // `oneshot()` returns, before the SSE body below is ever polled.
    let state = test_state();
    let router = acp_router(state.clone());

    let batch = r#"[{"jsonrpc":"2.0","id":1,"method":"initialize"},{"jsonrpc":"2.0","id":2,"method":"initialize"}]"#;
    let req = Request::builder()
        .method("POST")
        .uri("/acp")
        .header("content-type", "application/json")
        .body(Body::from(batch))
        .unwrap();

    let response = router.oneshot(req).await.unwrap();
    assert_eq!(response.status(), StatusCode::OK);

    let mut stream = response.into_body().into_data_stream();
    let mut buf = String::new();
    let mut received_ids: HashSet<u64> = HashSet::new();
    let deadline = tokio::time::Instant::now() + Duration::from_secs(10);

    while received_ids.len() < 2 {
        let remaining = deadline.saturating_duration_since(tokio::time::Instant::now());
        assert!(
            remaining > Duration::ZERO,
            "timed out waiting for 2 batch responses, got {received_ids:?} so far"
        );
        let chunk = tokio::time::timeout(remaining, stream.next())
            .await
            .expect("timed out waiting for next SSE chunk")
            .expect("SSE stream ended before both batch responses arrived")
            .expect("SSE stream error");
        buf.push_str(&String::from_utf8_lossy(&chunk));

        while let Some(pos) = buf.find('\n') {
            let line = buf[..pos].to_owned();
            buf.drain(..=pos);
            let Some(data) = line.strip_prefix("data:") else {
                continue;
            };
            let Ok(json) = serde_json::from_str::<serde_json::Value>(data.trim()) else {
                continue;
            };
            // Batch replies arrive aggregated as a single JSON array; a non-batch single-object
            // reply (defensive fallback, in case framing ever changes) is handled too.
            let entries: Vec<&serde_json::Value> = match &json {
                serde_json::Value::Array(entries) => entries.iter().collect(),
                other => vec![other],
            };
            for entry in entries {
                if let Some(id) = entry.get("id").and_then(serde_json::Value::as_u64) {
                    received_ids.insert(id);
                }
            }
        }
    }

    assert_eq!(received_ids, HashSet::from([1, 2]));
}

#[tokio::test]
async fn post_with_unknown_session_id_returns_not_found() {
    let router = acp_router(test_state());

    let req = Request::builder()
        .method("POST")
        .uri("/acp")
        .header("acp-session-id", "00000000-0000-0000-0000-000000000000")
        .body(Body::from("{}"))
        .unwrap();

    let response = router.oneshot(req).await.unwrap();
    assert_eq!(response.status(), StatusCode::NOT_FOUND);
}

#[tokio::test]
async fn post_with_malformed_session_id_returns_bad_request() {
    let router = acp_router(test_state());

    let req = Request::builder()
        .method("POST")
        .uri("/acp")
        .header("acp-session-id", "not-a-uuid!!!")
        .body(Body::from("{}"))
        .unwrap();

    let response = router.oneshot(req).await.unwrap();
    assert_eq!(response.status(), StatusCode::BAD_REQUEST);
}

#[tokio::test]
async fn post_returns_503_when_max_sessions_reached() {
    let state = state_with_max_sessions(0);
    let router = acp_router(state);

    let req = Request::builder()
        .method("POST")
        .uri("/acp")
        .body(Body::from("{}"))
        .unwrap();

    let response = router.oneshot(req).await.unwrap();
    assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);
}

#[tokio::test]
async fn post_returns_500_when_writer_is_closed() {
    use tokio::sync::Mutex;
    use tokio::sync::broadcast;

    let state = test_state();

    // Inject a broken (closed) DuplexStream writer by creating a pair and
    // immediately dropping the reader half so writes will fail.
    let (_, dead_writer) = tokio::io::duplex(64);
    let (tx, _) = broadcast::channel::<String>(4);
    let session_id = uuid::Uuid::new_v4().to_string();
    let handle = Arc::new(ConnectionHandle {
        writer: Arc::new(Mutex::new(dead_writer)),
        output_tx: tx,
        last_activity: AtomicU64::new(0),
        idle_timeout_secs: 1800,
    });
    state.connections.insert(session_id.clone(), handle);

    let router = acp_router(state);

    let req = Request::builder()
        .method("POST")
        .uri("/acp")
        .header("acp-session-id", &session_id)
        .body(Body::from("{}"))
        .unwrap();

    let response = router.oneshot(req).await.unwrap();
    assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
}

// ── GET /acp tests ───────────────────────────────────────────────────────────

#[tokio::test]
async fn get_without_session_id_returns_bad_request() {
    let router = acp_router(test_state());

    let req = Request::builder()
        .method("GET")
        .uri("/acp")
        .body(Body::empty())
        .unwrap();

    let response = router.oneshot(req).await.unwrap();
    assert_eq!(response.status(), StatusCode::BAD_REQUEST);
}

#[tokio::test]
async fn get_with_unknown_session_id_returns_not_found() {
    let router = acp_router(test_state());

    let req = Request::builder()
        .method("GET")
        .uri("/acp")
        .header("acp-session-id", "00000000-0000-0000-0000-000000000000")
        .body(Body::empty())
        .unwrap();

    let response = router.oneshot(req).await.unwrap();
    assert_eq!(response.status(), StatusCode::NOT_FOUND);
}

#[tokio::test]
async fn get_with_malformed_session_id_returns_bad_request() {
    let router = acp_router(test_state());

    // "not-a-uuid" is a valid header value but fails UUID parsing.
    let req = Request::builder()
        .method("GET")
        .uri("/acp")
        .header("acp-session-id", "not-a-uuid-string")
        .body(Body::empty())
        .unwrap();

    let response = router.oneshot(req).await.unwrap();
    assert_eq!(response.status(), StatusCode::BAD_REQUEST);
}

// ── GET /acp/ws tests ────────────────────────────────────────────────────────

/// Bind a real TCP listener, serve the router on it, and return the bound address.
async fn serve_on_random_port(router: axum::Router) -> std::net::SocketAddr {
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();
    tokio::spawn(async move {
        axum::serve(listener, router).await.unwrap();
    });
    addr
}

#[tokio::test]
async fn ws_upgrade_returns_101_switching_protocols() {
    use tokio_tungstenite::connect_async;

    let router = acp_router(test_state());
    let addr = serve_on_random_port(router).await;

    let url = format!("ws://{addr}/acp/ws");
    let result = connect_async(&url).await;
    assert!(
        result.is_ok(),
        "WebSocket connect should succeed: {result:?}"
    );
}

#[tokio::test]
async fn ws_upgrade_returns_503_when_max_sessions_reached() {
    use tokio_tungstenite::connect_async;

    let router = acp_router(state_with_max_sessions(0));
    let addr = serve_on_random_port(router).await;

    let url = format!("ws://{addr}/acp/ws");
    let result = connect_async(&url).await;
    // Server returns 503, tungstenite yields a non-101 HTTP error.
    assert!(result.is_err(), "connect should fail with 503");
    let err = result.unwrap_err().to_string();
    assert!(
        err.contains("503") || err.contains("Service Unavailable"),
        "expected 503 in error, got: {err}"
    );
}

// ── Bearer auth tests ─────────────────────────────────────────────────────────

fn state_with_auth(token: &str) -> AcpHttpState {
    AcpHttpState::new(
        noop_spawner(),
        AcpServerConfig {
            agent_name: "test".into(),
            agent_version: "0.0.1".into(),
            max_sessions: 4,
            session_idle_timeout_secs: 1800,
            permission_file: None,
            provider_factory: None,
            available_models: shared_models(vec![]),
            mcp_manager: None,
            auth_clients: vec![crate::transport::AcpClientToken {
                id: "default".into(),
                token: (token).into(),
            }],
            discovery_enabled: true,
            terminal_timeout_secs: 120,
            project_rules: vec![],
            title_max_chars: 60,
            max_history: 100,
            sqlite_path: None,
            ready_notification: None,
            ..Default::default()
        },
    )
    .with_ready(true)
}

#[tokio::test]
async fn auth_valid_token_passes() {
    let router = acp_router(state_with_auth("secret"));

    let req = Request::builder()
        .method("POST")
        .uri("/acp")
        .header("content-type", "application/json")
        .header("authorization", "Bearer secret")
        .body(Body::from(
            r#"{"jsonrpc":"2.0","id":1,"method":"initialize"}"#,
        ))
        .unwrap();

    let response = router.oneshot(req).await.unwrap();
    assert_eq!(response.status(), StatusCode::OK);
}

#[tokio::test]
async fn auth_missing_token_returns_401() {
    let router = acp_router(state_with_auth("secret"));

    let req = Request::builder()
        .method("POST")
        .uri("/acp")
        .body(Body::from("{}"))
        .unwrap();

    let response = router.oneshot(req).await.unwrap();
    assert_eq!(response.status(), StatusCode::UNAUTHORIZED);
}

#[tokio::test]
async fn auth_wrong_token_returns_401() {
    let router = acp_router(state_with_auth("secret"));

    let req = Request::builder()
        .method("POST")
        .uri("/acp")
        .header("authorization", "Bearer wrong")
        .body(Body::from("{}"))
        .unwrap();

    let response = router.oneshot(req).await.unwrap();
    assert_eq!(response.status(), StatusCode::UNAUTHORIZED);
}

#[tokio::test]
async fn auth_none_mode_allows_all_requests() {
    // test_state() has auth_clients: Vec::new() — no auth layer applied.
    let router = acp_router(test_state());

    let req = Request::builder()
        .method("POST")
        .uri("/acp")
        .body(Body::from("{}"))
        .unwrap();

    let response = router.oneshot(req).await.unwrap();
    // Any non-401 status confirms auth is not enforced.
    assert_ne!(response.status(), StatusCode::UNAUTHORIZED);
}

#[tokio::test]
async fn health_is_public_even_when_bearer_auth_is_enabled() {
    let router = acp_router(state_with_auth("secret"));

    let req = Request::builder()
        .method("GET")
        .uri("/health")
        .body(Body::empty())
        .unwrap();

    let response = router.oneshot(req).await.unwrap();
    assert_ne!(response.status(), StatusCode::UNAUTHORIZED);
}

#[tokio::test]
async fn health_returns_200_when_ready() {
    use axum::body::to_bytes;

    let router = acp_router(test_state());
    let req = Request::builder()
        .method("GET")
        .uri("/health")
        .body(Body::empty())
        .unwrap();

    let response = router.oneshot(req).await.unwrap();
    assert_eq!(response.status(), StatusCode::OK);

    let body = to_bytes(response.into_body(), 65536).await.unwrap();
    let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
    assert_eq!(json["status"], "ok");
    assert_eq!(json["version"], "0.0.1");
    assert!(json["uptime_secs"].is_u64());
}

#[tokio::test]
async fn health_returns_503_when_not_ready() {
    use axum::body::to_bytes;

    let state = AcpHttpState::new(
        noop_spawner(),
        AcpServerConfig {
            agent_name: "test".into(),
            agent_version: "0.0.1".into(),
            max_sessions: 4,
            session_idle_timeout_secs: 1800,
            permission_file: None,
            provider_factory: None,
            available_models: std::sync::Arc::new(parking_lot::RwLock::new(Vec::new())),
            mcp_manager: None,
            auth_clients: vec![crate::transport::AcpClientToken {
                id: "default".into(),
                token: ("secret").into(),
            }],
            discovery_enabled: true,
            terminal_timeout_secs: 120,
            project_rules: vec![],
            title_max_chars: 60,
            max_history: 100,
            sqlite_path: None,
            ready_notification: None,
            ..Default::default()
        },
    );
    let router = acp_router(state);

    let req = Request::builder()
        .method("GET")
        .uri("/health")
        .body(Body::empty())
        .unwrap();

    let response = router.oneshot(req).await.unwrap();
    assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);

    let body = to_bytes(response.into_body(), 65536).await.unwrap();
    let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
    assert_eq!(json["status"], "starting");
}

#[tokio::test]
async fn acp_post_returns_503_when_server_not_ready() {
    let state = AcpHttpState::new(
        noop_spawner(),
        AcpServerConfig {
            agent_name: "test".into(),
            agent_version: "0.0.1".into(),
            max_sessions: 4,
            session_idle_timeout_secs: 1800,
            permission_file: None,
            provider_factory: None,
            available_models: std::sync::Arc::new(parking_lot::RwLock::new(Vec::new())),
            mcp_manager: None,
            auth_clients: Vec::new(),
            discovery_enabled: true,
            terminal_timeout_secs: 120,
            project_rules: vec![],
            title_max_chars: 60,
            max_history: 100,
            sqlite_path: None,
            ready_notification: None,
            ..Default::default()
        },
    );
    let router = acp_router(state);

    let req = Request::builder()
        .method("POST")
        .uri("/acp")
        .body(Body::from("{}"))
        .unwrap();

    let response = router.oneshot(req).await.unwrap();
    assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);
}

// ── Discovery endpoint tests ──────────────────────────────────────────────────

#[tokio::test]
async fn discovery_returns_expected_json_fields() {
    use axum::body::to_bytes;

    let router = acp_router(test_state());

    let req = Request::builder()
        .method("GET")
        .uri("/.well-known/acp.json")
        .body(Body::empty())
        .unwrap();

    let response = router.oneshot(req).await.unwrap();
    assert_eq!(response.status(), StatusCode::OK);

    let body = to_bytes(response.into_body(), 1_048_576).await.unwrap();
    let json: serde_json::Value = serde_json::from_slice(&body).unwrap();

    assert_eq!(json["name"], "test");
    assert_eq!(json["version"], "0.0.1");
    assert!(
        json["transports"].is_object(),
        "transports must be an object"
    );
    assert!(json["transports"]["http_sse"].is_object());
    assert!(json["transports"]["websocket"].is_object());
    assert!(json["transports"]["health"].is_object());
    assert!(
        json["authentication"].is_null(),
        "authentication must be null when no token"
    );
    assert_eq!(json["readiness"]["stdio_notification"], "zeph/ready");
    assert_eq!(json["readiness"]["http_health_endpoint"], "/health");
    // protocol_version must be the integer value of ProtocolVersion::LATEST (1).
    assert_eq!(
        json["protocol_version"],
        serde_json::json!(agent_client_protocol::schema::ProtocolVersion::LATEST)
    );
}

#[tokio::test]
async fn discovery_with_bearer_token_returns_bearer_auth_type() {
    use axum::body::to_bytes;

    let router = acp_router(state_with_auth("secret"));

    let req = Request::builder()
        .method("GET")
        .uri("/.well-known/acp.json")
        .body(Body::empty())
        .unwrap();

    let response = router.oneshot(req).await.unwrap();
    assert_eq!(response.status(), StatusCode::OK);

    let body = to_bytes(response.into_body(), 1_048_576).await.unwrap();
    let json: serde_json::Value = serde_json::from_slice(&body).unwrap();

    assert_eq!(json["authentication"]["type"], "bearer");
}

#[tokio::test]
async fn discovery_disabled_returns_404() {
    let state = AcpHttpState::new(
        noop_spawner(),
        AcpServerConfig {
            agent_name: "test".into(),
            agent_version: "0.0.1".into(),
            max_sessions: 4,
            session_idle_timeout_secs: 1800,
            permission_file: None,
            provider_factory: None,
            available_models: shared_models(vec![]),
            mcp_manager: None,
            auth_clients: Vec::new(),
            discovery_enabled: false,
            terminal_timeout_secs: 120,
            project_rules: vec![],
            title_max_chars: 60,
            max_history: 100,
            sqlite_path: None,
            ready_notification: None,
            ..Default::default()
        },
    );
    let router = acp_router(state);

    let req = Request::builder()
        .method("GET")
        .uri("/.well-known/acp.json")
        .body(Body::empty())
        .unwrap();

    let response = router.oneshot(req).await.unwrap();
    assert_eq!(response.status(), StatusCode::NOT_FOUND);
}

// ── agent.json endpoint tests ─────────────────────────────────────────────────

#[tokio::test]
async fn agent_json_returns_expected_fields() {
    use axum::body::to_bytes;

    let router = acp_router(test_state());

    let req = Request::builder()
        .method("GET")
        .uri("/agent.json")
        .body(Body::empty())
        .unwrap();

    let response = router.oneshot(req).await.unwrap();
    assert_eq!(response.status(), StatusCode::OK);

    let body = to_bytes(response.into_body(), 1_048_576).await.unwrap();
    let json: serde_json::Value = serde_json::from_slice(&body).unwrap();

    assert_eq!(json["id"], "zeph");
    assert_eq!(json["name"], "test");
    assert_eq!(json["version"], "0.0.1");
    assert!(
        json["description"].is_string(),
        "description must be a string"
    );
    assert!(
        json["distribution"].is_object(),
        "distribution must be an object"
    );
    assert_eq!(json["distribution"]["type"], "binary");
    assert!(
        json["distribution"]["platforms"].is_array(),
        "platforms must be an array"
    );
}

#[tokio::test]
async fn agent_json_disabled_returns_404() {
    let state = AcpHttpState::new(
        noop_spawner(),
        AcpServerConfig {
            agent_name: "test".into(),
            agent_version: "0.0.1".into(),
            max_sessions: 4,
            session_idle_timeout_secs: 1800,
            permission_file: None,
            provider_factory: None,
            available_models: shared_models(vec![]),
            mcp_manager: None,
            auth_clients: Vec::new(),
            discovery_enabled: false,
            terminal_timeout_secs: 120,
            project_rules: vec![],
            title_max_chars: 60,
            max_history: 100,
            sqlite_path: None,
            ready_notification: None,
            ..Default::default()
        },
    );
    let router = acp_router(state);

    let req = Request::builder()
        .method("GET")
        .uri("/agent.json")
        .body(Body::empty())
        .unwrap();

    let response = router.oneshot(req).await.unwrap();
    assert_eq!(response.status(), StatusCode::NOT_FOUND);
}

// ── Reaper test ───────────────────────────────────────────────────────────────

#[tokio::test(start_paused = true)]
async fn reaper_removes_expired_connections() {
    use std::time::Duration;
    use tokio::sync::Mutex;
    use tokio::sync::broadcast;

    let state = AcpHttpState::new(
        noop_spawner(),
        AcpServerConfig {
            agent_name: "test".into(),
            agent_version: "0.0.1".into(),
            max_sessions: 4,
            session_idle_timeout_secs: 30,
            permission_file: None,
            provider_factory: None,
            available_models: shared_models(vec![]),
            mcp_manager: None,
            auth_clients: Vec::new(),
            discovery_enabled: true,
            terminal_timeout_secs: 120,
            project_rules: vec![],
            title_max_chars: 60,
            max_history: 100,
            sqlite_path: None,
            ready_notification: None,
            ..Default::default()
        },
    );

    // Insert a connection with last_activity in the far past (expired).
    let (_, writer) = tokio::io::duplex(64);
    let (tx, _) = broadcast::channel::<String>(4);
    let expired_id = uuid::Uuid::new_v4().to_string();
    state.connections.insert(
        expired_id.clone(),
        Arc::new(ConnectionHandle {
            writer: Arc::new(Mutex::new(writer)),
            output_tx: tx,
            // Set last_activity to 0 (Unix epoch) so it's always expired.
            last_activity: AtomicU64::new(0),
            idle_timeout_secs: 30,
        }),
    );

    assert_eq!(state.connections.len(), 1);
    state.start_reaper();

    // Advance time past the reaper interval (60 s).
    tokio::time::advance(Duration::from_secs(61)).await;
    // Yield to let the reaper task run.
    tokio::task::yield_now().await;

    assert_eq!(
        state.connections.len(),
        0,
        "reaper should have removed the expired connection"
    );
}

// ── GET /sessions tests ───────────────────────────────────────────────────────

#[tokio::test]
async fn list_sessions_returns_503_when_store_is_none() {
    let router = acp_router(test_state());

    let req = Request::builder()
        .method("GET")
        .uri("/sessions")
        .body(Body::empty())
        .unwrap();

    let response = router.oneshot(req).await.unwrap();
    assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);
}

#[tokio::test]
async fn list_sessions_returns_empty_array_when_no_sessions() {
    use axum::body::to_bytes;

    let store = zeph_memory::store::SqliteStore::new(":memory:")
        .await
        .expect("SqliteStore::new");
    let state = test_state().with_store(store);
    let router = acp_router(state);

    let req = Request::builder()
        .method("GET")
        .uri("/sessions")
        .body(Body::empty())
        .unwrap();

    let response = router.oneshot(req).await.unwrap();
    assert_eq!(response.status(), StatusCode::OK);

    let body = to_bytes(response.into_body(), 65536).await.unwrap();
    let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
    assert_eq!(json, serde_json::json!([]));
}

#[tokio::test]
async fn list_sessions_returns_session_data() {
    use axum::body::to_bytes;

    let store = zeph_memory::store::SqliteStore::new(":memory:")
        .await
        .expect("SqliteStore::new");
    store
        .create_acp_session("sess-1", Some(crate::transport::OWNER_KEY_LOCAL))
        .await
        .unwrap();
    // Regression guard for the S1 false-green (spec-068 §12.3 / D-2): `list_acp_sessions`
    // now derives `message_count` from `acp_sessions.event_count`, so the fixture must drive
    // it through `zeph_session::SessionStore::update_seq` — the same primitive
    // `SessionSink::record_message` calls in production — not the retired `save_acp_event`,
    // which only ever populated the now-permanently-empty `acp_session_events` table.
    let session_store = zeph_session::SessionStore::new(store.pool().clone());
    session_store.update_seq("sess-1", 0, 1).await.unwrap();
    store
        .update_session_title("sess-1", "Test Session")
        .await
        .unwrap();

    let state = test_state().with_store(store);
    let router = acp_router(state);

    let req = Request::builder()
        .method("GET")
        .uri("/sessions")
        .body(Body::empty())
        .unwrap();

    let response = router.oneshot(req).await.unwrap();
    assert_eq!(response.status(), StatusCode::OK);

    let body = to_bytes(response.into_body(), 65536).await.unwrap();
    let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
    let arr = json.as_array().unwrap();
    assert_eq!(arr.len(), 1);
    assert_eq!(arr[0]["id"], "sess-1");
    assert_eq!(arr[0]["title"], "Test Session");
    assert_eq!(arr[0]["message_count"], 1);
}

// ── Owner scoping tests (#5868) ───────────────────────────────────────────────

fn state_with_two_clients() -> AcpHttpState {
    AcpHttpState::new(
        noop_spawner(),
        AcpServerConfig {
            agent_name: "test".into(),
            agent_version: "0.0.1".into(),
            max_sessions: 4,
            session_idle_timeout_secs: 1800,
            permission_file: None,
            provider_factory: None,
            available_models: shared_models(vec![]),
            mcp_manager: None,
            auth_clients: vec![
                crate::transport::AcpClientToken {
                    id: "alice".into(),
                    token: "token-a".into(),
                },
                crate::transport::AcpClientToken {
                    id: "bob".into(),
                    token: "token-b".into(),
                },
            ],
            discovery_enabled: true,
            terminal_timeout_secs: 120,
            project_rules: vec![],
            title_max_chars: 60,
            max_history: 100,
            sqlite_path: None,
            ready_notification: None,
            ..Default::default()
        },
    )
    .with_ready(true)
}

#[tokio::test]
async fn list_sessions_isolates_distinct_token_clients() {
    use axum::body::to_bytes;

    let store = zeph_memory::store::SqliteStore::new(":memory:")
        .await
        .expect("SqliteStore::new");
    store
        .create_acp_session("alice-sess", Some("alice"))
        .await
        .unwrap();
    store
        .create_acp_session("bob-sess", Some("bob"))
        .await
        .unwrap();

    let state = state_with_two_clients().with_store(store);
    let router = acp_router(state);

    let req = Request::builder()
        .method("GET")
        .uri("/sessions")
        .header("authorization", "Bearer token-a")
        .body(Body::empty())
        .unwrap();
    let response = router.clone().oneshot(req).await.unwrap();
    assert_eq!(response.status(), StatusCode::OK);
    let body = to_bytes(response.into_body(), 65536).await.unwrap();
    let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
    let arr = json.as_array().unwrap();
    assert_eq!(arr.len(), 1);
    assert_eq!(arr[0]["id"], "alice-sess");

    let req2 = Request::builder()
        .method("GET")
        .uri("/sessions")
        .header("authorization", "Bearer token-b")
        .body(Body::empty())
        .unwrap();
    let response2 = router.oneshot(req2).await.unwrap();
    assert_eq!(response2.status(), StatusCode::OK);
    let body2 = to_bytes(response2.into_body(), 65536).await.unwrap();
    let json2: serde_json::Value = serde_json::from_slice(&body2).unwrap();
    let arr2 = json2.as_array().unwrap();
    assert_eq!(arr2.len(), 1);
    assert_eq!(arr2[0]["id"], "bob-sess");
}

#[tokio::test]
async fn session_messages_cross_owner_returns_404() {
    let store = zeph_memory::store::SqliteStore::new(":memory:")
        .await
        .expect("SqliteStore::new");
    let session_id = "00000000-0000-0000-0000-000000000002";
    store
        .create_acp_session(session_id, Some("alice"))
        .await
        .unwrap();

    let state = state_with_two_clients().with_store(store);
    let router = acp_router(state);

    let req = Request::builder()
        .method("GET")
        .uri(format!("/sessions/{session_id}/messages"))
        .header("authorization", "Bearer token-b")
        .body(Body::empty())
        .unwrap();

    let response = router.oneshot(req).await.unwrap();
    assert_eq!(response.status(), StatusCode::NOT_FOUND);
}

// ── GET /sessions/{id}/messages tests ────────────────────────────────────────

#[tokio::test]
async fn session_messages_returns_503_when_store_is_none() {
    let router = acp_router(test_state());

    let req = Request::builder()
        .method("GET")
        .uri("/sessions/00000000-0000-0000-0000-000000000001/messages")
        .body(Body::empty())
        .unwrap();

    let response = router.oneshot(req).await.unwrap();
    assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);
}

#[tokio::test]
async fn session_messages_returns_400_for_non_uuid() {
    let store = zeph_memory::store::SqliteStore::new(":memory:")
        .await
        .expect("SqliteStore::new");
    let state = test_state().with_store(store);
    let router = acp_router(state);

    let req = Request::builder()
        .method("GET")
        .uri("/sessions/not-a-uuid/messages")
        .body(Body::empty())
        .unwrap();

    let response = router.oneshot(req).await.unwrap();
    assert_eq!(response.status(), StatusCode::BAD_REQUEST);
}

#[tokio::test]
async fn session_messages_returns_404_for_unknown_session() {
    let store = zeph_memory::store::SqliteStore::new(":memory:")
        .await
        .expect("SqliteStore::new");
    let state = test_state().with_store(store);
    let router = acp_router(state);

    let req = Request::builder()
        .method("GET")
        .uri("/sessions/00000000-0000-0000-0000-000000000099/messages")
        .body(Body::empty())
        .unwrap();

    let response = router.oneshot(req).await.unwrap();
    assert_eq!(response.status(), StatusCode::NOT_FOUND);
}

#[tokio::test]
async fn session_messages_returns_events_for_known_session() {
    use axum::body::to_bytes;

    let store = zeph_memory::store::SqliteStore::new(":memory:")
        .await
        .expect("SqliteStore::new");
    let session_id = "00000000-0000-0000-0000-000000000001";
    store
        .create_acp_session(session_id, Some(crate::transport::OWNER_KEY_LOCAL))
        .await
        .unwrap();

    // Regression guard for the S1-class false-green: message_messages_handler now reads the
    // durable JSONL event log (spec-068 §12.3 / D-2), not the legacy acp_session_events table —
    // seed the log directly instead of the retired save_acp_event write path.
    let dir = tempfile::tempdir().unwrap();
    let session_path = zeph_session::session_dir(dir.path(), session_id);
    let log = zeph_session::SessionEventLog::open(&session_path)
        .await
        .unwrap();
    log.append(
        None,
        None,
        zeph_session::SessionEvent::UserMessage {
            text: "hello".to_owned(),
            image_refs: Vec::new(),
        },
    )
    .await
    .unwrap();
    drop(log);

    let state = test_state_with_session_data_dir(dir.path().to_path_buf()).with_store(store);
    let router = acp_router(state);

    let req = Request::builder()
        .method("GET")
        .uri(format!("/sessions/{session_id}/messages"))
        .body(Body::empty())
        .unwrap();

    let response = router.oneshot(req).await.unwrap();
    assert_eq!(response.status(), StatusCode::OK);

    let body = to_bytes(response.into_body(), 65536).await.unwrap();
    let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
    let arr = json.as_array().unwrap();
    assert_eq!(arr.len(), 1);
    assert_eq!(arr[0]["event_type"], "user_message");
    assert_eq!(arr[0]["payload"], "hello");
    assert!(arr[0]["created_at"].is_string());
}