agent-client-protocol 5.0.0

Core protocol types and traits for the Agent Client Protocol
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
#![cfg(feature = "unstable_protocol_v2")]

use agent_client_protocol::RawJsonRpcId;
use std::sync::{
    Arc,
    atomic::{AtomicUsize, Ordering},
};

use agent_client_protocol::{
    ByteStreams, Channel, Client, Conductor, ConnectTo, Error, Proxy, ProxyProtocolRouter,
    RawJsonRpcMessage, RawJsonRpcResponse, TransportFrame,
};
use agent_client_protocol::{schema::ProtocolVersion, v1::schema as v1, v2::schema as v2};
use futures::StreamExt as _;
use serde_json::Value;
use tokio_util::compat::{TokioAsyncReadCompatExt, TokioAsyncWriteCompatExt};

fn v1_proxy_initialize(protocol_version: ProtocolVersion) -> v1::InitializeProxyRequest {
    v1::InitializeProxyRequest::from(
        v1::InitializeRequest::new(protocol_version)
            .client_info(v1::Implementation::new("router-test-client", "1.0.0")),
    )
}

fn v2_proxy_initialize(protocol_version: ProtocolVersion) -> v2::InitializeProxyRequest {
    v2::InitializeProxyRequest::new(v2::InitializeRequest::new(
        protocol_version,
        v2::Implementation::new("router-test-client", "1.0.0"),
    ))
}

fn router(v1_hits: Arc<AtomicUsize>, v2_hits: Arc<AtomicUsize>) -> ProxyProtocolRouter {
    Proxy
        .protocol_router()
        .with_v1(v1_proxy(v1_hits))
        .with_v2(v2_proxy(v2_hits))
}

fn v1_proxy(v1_hits: Arc<AtomicUsize>) -> impl ConnectTo<Conductor> {
    Proxy.v1().on_receive_request_from(
        Client,
        async move |request: v1::InitializeProxyRequest, responder, _cx| {
            v1_hits.fetch_add(1, Ordering::SeqCst);
            responder
                .respond(v1::InitializeResponse::new(request.initialize.protocol_version).into())
        },
        agent_client_protocol::on_receive_request!(),
    )
}

fn v2_proxy(v2_hits: Arc<AtomicUsize>) -> impl ConnectTo<Conductor> {
    Proxy.v2().on_receive_request_from(
        Client,
        async move |request: v2::InitializeProxyRequest, responder, _cx| {
            v2_hits.fetch_add(1, Ordering::SeqCst);
            responder.respond(
                v2::InitializeResponse::new(
                    request.initialize.protocol_version,
                    v2::Implementation::new("router-test-proxy", "1.0.0"),
                )
                .into(),
            )
        },
        agent_client_protocol::on_receive_request!(),
    )
}

async fn initialize(
    router: ProxyProtocolRouter,
    params: Value,
) -> Result<Result<Value, Error>, Error> {
    request(router, "proxy/initialize", params).await
}

async fn request(
    router: impl ConnectTo<Conductor>,
    method: &str,
    params: Value,
) -> Result<Result<Value, Error>, Error> {
    let (Channel { mut rx, tx, .. }, future) =
        ConnectTo::<Conductor>::into_channel_and_future(router);
    let task = tokio::spawn(future.expect("proxy router owns a connection driver"));
    let request_id = RawJsonRpcId::Number(1);

    tx.unbounded_send(TransportFrame::Single(RawJsonRpcMessage::request(
        method.into(),
        params,
        request_id.clone(),
    )?))
    .map_err(Error::into_internal_error)?;

    let result = loop {
        let frame = rx.next().await.ok_or_else(|| {
            Error::internal_error().data("proxy router closed before initialize response")
        })?;
        let TransportFrame::Single(RawJsonRpcMessage::Response(response)) = frame else {
            continue;
        };
        match response {
            RawJsonRpcResponse::Result { id, result } if id == request_id => break Ok(result),
            RawJsonRpcResponse::Error { id, error } if id == request_id => {
                break Err(error.into_acp_error());
            }
            _ => {}
        }
    };

    drop(tx);
    drop(rx);
    task.await.map_err(Error::into_internal_error)??;
    Ok(result)
}

#[tokio::test(flavor = "current_thread")]
async fn routes_exact_versions_to_their_implementation() -> Result<(), Error> {
    let v1_hits = Arc::new(AtomicUsize::new(0));
    let v2_hits = Arc::new(AtomicUsize::new(0));

    let response = initialize(
        router(Arc::clone(&v1_hits), Arc::clone(&v2_hits)),
        serde_json::to_value(v1_proxy_initialize(ProtocolVersion::V1))
            .map_err(Error::into_internal_error)?,
    )
    .await??;
    assert_eq!(response["protocolVersion"], 1);

    let response = initialize(
        router(Arc::clone(&v1_hits), Arc::clone(&v2_hits)),
        serde_json::to_value(v2_proxy_initialize(ProtocolVersion::V2))
            .map_err(Error::into_internal_error)?,
    )
    .await??;
    assert_eq!(response["protocolVersion"], 2);

    assert_eq!(v1_hits.load(Ordering::SeqCst), 1);
    assert_eq!(v2_hits.load(Ordering::SeqCst), 1);
    Ok(())
}

#[tokio::test(flavor = "current_thread")]
async fn v1_only_router_answers_newer_versions_as_v1() -> Result<(), Error> {
    for requested in [2_u16, 3] {
        let v1_hits = Arc::new(AtomicUsize::new(0));
        let response = initialize(
            Proxy
                .protocol_router()
                .with_v1(v1_proxy(Arc::clone(&v1_hits))),
            serde_json::to_value(v2_proxy_initialize(ProtocolVersion::from(requested)))
                .map_err(Error::into_internal_error)?,
        )
        .await??;
        assert_eq!(response["protocolVersion"], 1);
        assert_eq!(v1_hits.load(Ordering::SeqCst), 1);
    }
    Ok(())
}

#[tokio::test(flavor = "current_thread")]
async fn v2_only_router_rejects_v1_like_a_v2_agent() -> Result<(), Error> {
    let v2_hits = Arc::new(AtomicUsize::new(0));
    let error = initialize(
        Proxy
            .protocol_router()
            .with_v2(v2_proxy(Arc::clone(&v2_hits))),
        serde_json::to_value(v1_proxy_initialize(ProtocolVersion::V1))
            .map_err(Error::into_internal_error)?,
    )
    .await?
    .expect_err("a v2-only router must reject v1");
    assert_eq!(i32::from(error.code), -32022);
    assert_eq!(
        error.data,
        Some(serde_json::json!({ "requested": 1, "supported": [2] }))
    );
    assert_eq!(v2_hits.load(Ordering::SeqCst), 0);
    Ok(())
}

/// Wait for the response to `id` on `rx`, skipping other frames.
async fn response_to(
    rx: &mut futures::channel::mpsc::UnboundedReceiver<TransportFrame>,
    id: &RawJsonRpcId,
) -> Result<Result<Value, Error>, Error> {
    loop {
        let frame = rx.next().await.ok_or_else(|| {
            Error::internal_error().data("proxy router closed before the response")
        })?;
        let TransportFrame::Single(RawJsonRpcMessage::Response(response)) = frame else {
            continue;
        };
        match response {
            RawJsonRpcResponse::Result { id: got, result } if got == *id => return Ok(Ok(result)),
            RawJsonRpcResponse::Error { id: got, error } if got == *id => {
                return Ok(Err(error.into_acp_error()));
            }
            _ => {}
        }
    }
}

#[tokio::test(flavor = "current_thread")]
async fn accepts_proxy_initialize_again_after_a_rejection() -> Result<(), Error> {
    let v2_hits = Arc::new(AtomicUsize::new(0));
    let router = Proxy
        .protocol_router()
        .with_v2(v2_proxy(Arc::clone(&v2_hits)));
    let (Channel { mut rx, tx, .. }, future) =
        ConnectTo::<Conductor>::into_channel_and_future(router);
    let task = tokio::spawn(future.expect("proxy router owns a connection driver"));
    let send = |message: RawJsonRpcMessage| {
        tx.unbounded_send(TransportFrame::Single(message))
            .map_err(Error::into_internal_error)
    };

    // A notification before initialization is ignored, and a rejected
    // `proxy/initialize` leaves the connection open for another attempt,
    // exactly as with `Agent::protocol_router`.
    send(RawJsonRpcMessage::notification(
        "_com.example/early".into(),
        serde_json::json!({}),
    )?)?;
    let rejected_id = RawJsonRpcId::Number(1);
    send(RawJsonRpcMessage::request(
        "proxy/initialize".into(),
        serde_json::to_value(v1_proxy_initialize(ProtocolVersion::V1))
            .map_err(Error::into_internal_error)?,
        rejected_id.clone(),
    )?)?;
    let error = response_to(&mut rx, &rejected_id)
        .await?
        .expect_err("a v2-only router must reject v1");
    assert_eq!(i32::from(error.code), -32022);

    let accepted_id = RawJsonRpcId::Number(2);
    send(RawJsonRpcMessage::request(
        "proxy/initialize".into(),
        serde_json::to_value(v2_proxy_initialize(ProtocolVersion::V2))
            .map_err(Error::into_internal_error)?,
        accepted_id.clone(),
    )?)?;
    let response = response_to(&mut rx, &accepted_id).await??;
    assert_eq!(response["protocolVersion"], 2);
    assert_eq!(v2_hits.load(Ordering::SeqCst), 1);

    drop(tx);
    drop(rx);
    task.await.map_err(Error::into_internal_error)??;
    Ok(())
}

#[tokio::test(flavor = "current_thread")]
async fn requires_proxy_initialize_as_the_first_call() -> Result<(), Error> {
    let error = request(
        Proxy
            .protocol_router()
            .with_v1(v1_proxy(Arc::new(AtomicUsize::new(0)))),
        "initialize",
        serde_json::to_value(v1_proxy_initialize(ProtocolVersion::V1))
            .map_err(Error::into_internal_error)?,
    )
    .await?
    .expect_err("an ordinary initialize request must not select a proxy");

    // The request does not select v2, so the v1 proxy answers it exactly as it
    // would without the router.
    let standalone = request(
        v1_proxy(Arc::new(AtomicUsize::new(0))),
        "initialize",
        serde_json::to_value(v1_proxy_initialize(ProtocolVersion::V1))
            .map_err(Error::into_internal_error)?,
    )
    .await?
    .expect_err("a v1 proxy rejects an ordinary initialize request");
    assert_eq!(error, standalone);
    assert_eq!(i32::from(error.code), -32600);
    assert!(
        error
            .data
            .as_ref()
            .and_then(Value::as_str)
            .is_some_and(|data| data.contains("`proxy/initialize`")),
        "{error:?}"
    );
    Ok(())
}

#[tokio::test(flavor = "current_thread")]
async fn selects_v2_for_newer_versions_like_an_agent_router() -> Result<(), Error> {
    let v1_hits = Arc::new(AtomicUsize::new(0));
    let v2_hits = Arc::new(AtomicUsize::new(0));

    let mut params = serde_json::to_value(v2_proxy_initialize(ProtocolVersion::from(3_u16)))
        .map_err(Error::into_internal_error)?;
    let object = params
        .as_object_mut()
        .expect("serialized initialize params should be an object");
    // Written for a newer version: keep what parses under v2.
    object.insert("info".into(), serde_json::json!("not-a-v2-implementation"));
    object.insert("_futureInitializeField".into(), serde_json::json!(true));

    let response = initialize(router(Arc::clone(&v1_hits), Arc::clone(&v2_hits)), params).await??;

    assert_eq!(response["protocolVersion"], 2);
    assert_eq!(v1_hits.load(Ordering::SeqCst), 0);
    assert_eq!(v2_hits.load(Ordering::SeqCst), 1);
    Ok(())
}

fn initialize_params_with_extensions(protocol_version: ProtocolVersion) -> Result<Value, Error> {
    let mut params = if protocol_version == ProtocolVersion::V1 {
        serde_json::to_value(v1_proxy_initialize(protocol_version))
    } else {
        serde_json::to_value(v2_proxy_initialize(protocol_version))
    }
    .map_err(Error::into_internal_error)?;

    let params = params
        .as_object_mut()
        .expect("serialized initialize params should be an object");
    params.insert(
        "_futureInitializeField".into(),
        serde_json::json!({ "preserved": true }),
    );
    Ok(Value::Object(params.clone()))
}

async fn write_wire_json(
    writer: &mut (impl tokio::io::AsyncWrite + Unpin),
    value: &Value,
) -> Result<(), Error> {
    use tokio::io::AsyncWriteExt as _;

    let mut bytes = serde_json::to_vec(value).map_err(Error::into_internal_error)?;
    bytes.push(b'\n');
    writer
        .write_all(&bytes)
        .await
        .map_err(Error::into_internal_error)?;
    writer.flush().await.map_err(Error::into_internal_error)
}

async fn read_wire_json(
    reader: &mut (impl tokio::io::AsyncBufRead + Unpin),
) -> Result<Value, Error> {
    use tokio::io::AsyncBufReadExt as _;

    let mut line = String::new();
    reader
        .read_line(&mut line)
        .await
        .map_err(Error::into_internal_error)?;
    serde_json::from_str(line.trim()).map_err(Error::into_internal_error)
}

#[tokio::test(flavor = "current_thread")]
async fn preserves_the_complete_initial_transport_frame() -> Result<(), Error> {
    use tokio::io::{AsyncWriteExt as _, BufReader};

    for protocol_version in [ProtocolVersion::V1, ProtocolVersion::V2] {
        let (mut conductor_writer, router_reader) = tokio::io::duplex(4096);
        let (router_writer, _conductor_reader) = tokio::io::duplex(4096);
        let conductor_transport =
            ByteStreams::new(router_writer.compat_write(), router_reader.compat());

        let (router_to_proxy_writer, proxy_reader) = tokio::io::duplex(4096);
        let (mut proxy_writer, proxy_to_router_reader) = tokio::io::duplex(4096);
        let external_proxy_transport = ByteStreams::new(
            router_to_proxy_writer.compat_write(),
            proxy_to_router_reader.compat(),
        );
        let router = if protocol_version == ProtocolVersion::V1 {
            Proxy.protocol_router().with_v1(external_proxy_transport)
        } else {
            Proxy.protocol_router().with_v2(external_proxy_transport)
        };
        let router_task = tokio::spawn(router.connect_to(conductor_transport));
        let mut proxy_reader = BufReader::new(proxy_reader);

        let initialize = serde_json::json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "proxy/initialize",
            "params": initialize_params_with_extensions(protocol_version)?,
        });
        write_wire_json(&mut conductor_writer, &initialize).await?;
        assert_eq!(read_wire_json(&mut proxy_reader).await?, initialize);

        conductor_writer
            .shutdown()
            .await
            .map_err(Error::into_internal_error)?;
        proxy_writer
            .shutdown()
            .await
            .map_err(Error::into_internal_error)?;
        router_task.await.map_err(Error::into_internal_error)??;
    }

    Ok(())
}

#[tokio::test(flavor = "current_thread")]
async fn answers_malformed_frames_before_proxy_initialize_and_keeps_reading() -> Result<(), Error> {
    use tokio::io::{AsyncWriteExt as _, BufReader};

    let (mut conductor_writer, router_reader) = tokio::io::duplex(4096);
    let (router_writer, conductor_reader) = tokio::io::duplex(4096);
    let conductor_transport =
        ByteStreams::new(router_writer.compat_write(), router_reader.compat());

    let (router_to_v1_writer, v1_reader) = tokio::io::duplex(4096);
    let (mut v1_writer, v1_to_router_reader) = tokio::io::duplex(4096);
    let v1_transport = ByteStreams::new(
        router_to_v1_writer.compat_write(),
        v1_to_router_reader.compat(),
    );
    let v2_hits = Arc::new(AtomicUsize::new(0));
    let router = Proxy
        .protocol_router()
        .with_v1(v1_transport)
        .with_v2(v2_proxy(Arc::clone(&v2_hits)));
    let router_task = tokio::spawn(router.connect_to(conductor_transport));
    let mut conductor_reader = BufReader::new(conductor_reader);
    let mut v1_reader = BufReader::new(v1_reader);

    // Invalid JSON gets its own parse error with a null id.
    conductor_writer
        .write_all(b"{not json\n")
        .await
        .map_err(Error::into_internal_error)?;
    let response = read_wire_json(&mut conductor_reader).await?;
    assert_eq!(response["id"], Value::Null, "{response}");
    assert_eq!(response["error"]["code"], -32700, "{response}");

    // A batch gets one invalid request error with a null id.
    let batch = serde_json::json!([{
        "jsonrpc": "2.0",
        "id": 7,
        "method": "proxy/initialize",
        "params": initialize_params_with_extensions(ProtocolVersion::V1)?,
    }]);
    write_wire_json(&mut conductor_writer, &batch).await?;
    let response = read_wire_json(&mut conductor_reader).await?;
    assert_eq!(response["id"], Value::Null, "{response}");
    assert_eq!(response["error"]["code"], -32600, "{response}");

    // The router keeps reading, and the selected implementation receives the
    // first valid `proxy/initialize` unchanged.
    let initialize = serde_json::json!({
        "jsonrpc": "2.0",
        "id": 1,
        "method": "proxy/initialize",
        "params": initialize_params_with_extensions(ProtocolVersion::V1)?,
    });
    write_wire_json(&mut conductor_writer, &initialize).await?;
    assert_eq!(read_wire_json(&mut v1_reader).await?, initialize);
    assert_eq!(v2_hits.load(Ordering::SeqCst), 0);

    conductor_writer
        .shutdown()
        .await
        .map_err(Error::into_internal_error)?;
    v1_writer
        .shutdown()
        .await
        .map_err(Error::into_internal_error)?;
    router_task.await.map_err(Error::into_internal_error)??;
    Ok(())
}