github-copilot-sdk 1.0.30-preview.1

Rust SDK for programmatic control of the GitHub Copilot CLI via JSON-RPC.
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
// Copyright (c) Microsoft Corporation. All rights reserved.

#![allow(clippy::unwrap_used)]

#[path = "support/fake_runtime.rs"]
mod fake_runtime;

use std::collections::BTreeMap;
use std::time::Duration;

use fake_runtime::{DropSignal, FakeRuntime, WAIT, read_frame, start, write_frame};
use github_copilot_sdk::network::{
    NetworkAuthenticateNegotiateRequest, NetworkConfig, NetworkResolveProxyRequest, NetworkScope,
};
use github_copilot_sdk::{
    CliProgram, Client, ClientOptions, Error, ErrorKind, Result, SessionConfig, Transport,
};
use serde_json::{Value, json};
use tokio::io::AsyncReadExt;
use tokio::sync::{mpsc, oneshot};
use tokio::time::timeout;

async fn direct(_request: NetworkResolveProxyRequest) -> Result<Option<String>> {
    Ok(None)
}

async fn unavailable(_request: NetworkAuthenticateNegotiateRequest) -> Result<Option<String>> {
    Ok(None)
}

async fn resolve_proxy(request: NetworkResolveProxyRequest) -> Result<Option<String>> {
    match request.url.as_str() {
        "https://api.example.com/" => Ok(Some("PROXY proxy.example:8080; DIRECT".to_string())),
        "http://intranet.example:8080/path?query" => {
            Ok(Some("HTTPS proxy.example:443".to_string()))
        }
        "https://failing.example/" => Err(Error::with_message(ErrorKind::Io, "lookup failed")),
        "https://panicking.example/" => panic!("lookup panicked"),
        _ => Ok(None),
    }
}

async fn authenticate_negotiate(
    request: NetworkAuthenticateNegotiateRequest,
) -> Result<Option<String>> {
    match request.spn.as_str() {
        "HTTP/unavailable" => Ok(None),
        "HTTP/failing" => Err(Error::with_message(ErrorKind::Io, "lookup failed")),
        "HTTP/panicking" => panic!("lookup panicked"),
        spn => Ok(Some(format!("token for {spn}"))),
    }
}

fn both_callbacks() -> ClientOptions {
    ClientOptions::new().with_network(
        NetworkConfig::new()
            .with_resolve_proxy(resolve_proxy)
            .with_authenticate_negotiate(authenticate_negotiate),
    )
}

/// Sends each `(method, params)` request with consecutive IDs from 1000 and
/// returns the responses in request order.
async fn send_requests(runtime: &mut FakeRuntime, requests: &[(&str, Value)]) -> Vec<Value> {
    for (id, (method, params)) in (1000..).zip(requests) {
        write_frame(
            &mut runtime.writer,
            json!({ "jsonrpc": "2.0", "id": id, "method": method, "params": params }),
        )
        .await;
    }
    let mut responses = BTreeMap::new();
    for _ in requests {
        let response = read_frame(&mut runtime.reader).await;
        responses.insert(response["id"].as_u64().unwrap(), response);
    }
    responses.into_values().collect()
}

/// Starts a client whose callbacks never answer, sends it `method` as request
/// `id`, and waits until the callback is running.
///
/// The returned receiver reports `"dropped"` when that callback future is dropped.
async fn start_stalled_request(
    method: &str,
    params: Value,
    id: u64,
) -> (Client, FakeRuntime, mpsc::UnboundedReceiver<&'static str>) {
    let (events_tx, mut events) = mpsc::unbounded_channel();
    let stalled = move || {
        let events = events_tx.clone();
        async move {
            let _dropped = DropSignal(events.clone());
            events.send("started").unwrap();
            std::future::pending::<Result<Option<String>>>().await
        }
    };
    let stalled_proxy = stalled.clone();
    let (client, mut runtime) = start(
        ClientOptions::new().with_network(
            NetworkConfig::new()
                .with_resolve_proxy(move |request: NetworkResolveProxyRequest| {
                    let stalled = stalled_proxy.clone();
                    async move {
                        if request.url == "https://stalled.example/" {
                            stalled().await
                        } else {
                            resolve_proxy(request).await
                        }
                    }
                })
                .with_authenticate_negotiate(
                    move |request: NetworkAuthenticateNegotiateRequest| {
                        let stalled = stalled.clone();
                        async move {
                            if request.spn == "HTTP/stalled" {
                                stalled().await
                            } else {
                                authenticate_negotiate(request).await
                            }
                        }
                    },
                ),
        ),
        vec![json!({ "result": null })],
    )
    .await;
    write_frame(
        &mut runtime.writer,
        json!({ "jsonrpc": "2.0", "id": id, "method": method, "params": params }),
    )
    .await;
    assert_eq!(timeout(WAIT, events.recv()).await.unwrap(), Some("started"));
    (client.unwrap(), runtime, events)
}

#[tokio::test]
async fn registers_both_callbacks_and_answers_concurrent_lookups() {
    let (client, mut runtime) = start(both_callbacks(), vec![json!({ "result": null })]).await;
    let client = client.unwrap();
    let responses = send_requests(
        &mut runtime,
        &[
            (
                "network.resolveProxy",
                json!({ "url": "https://api.example.com/" }),
            ),
            (
                "network.resolveProxy",
                json!({ "url": "http://intranet.example:8080/path?query" }),
            ),
            (
                "network.authenticateNegotiate",
                json!({ "spn": "HTTP/proxy.example" }),
            ),
            (
                "network.authenticateNegotiate",
                json!({ "spn": "ldap/directory.example@EXAMPLE.COM" }),
            ),
        ],
    )
    .await;
    client.force_stop();
    assert_eq!(
        (runtime.registrations(), responses),
        (
            vec![json!({
                "method": "network.setProvider",
                "params": { "resolveProxy": {}, "authenticateNegotiate": {} }
            })],
            vec![
                json!({
                    "jsonrpc": "2.0", "id": 1000,
                    "result": { "proxy": "PROXY proxy.example:8080; DIRECT" }
                }),
                json!({
                    "jsonrpc": "2.0", "id": 1001,
                    "result": { "proxy": "HTTPS proxy.example:443" }
                }),
                json!({
                    "jsonrpc": "2.0", "id": 1002,
                    "result": { "token": "token for HTTP/proxy.example" }
                }),
                json!({
                    "jsonrpc": "2.0", "id": 1003,
                    "result": { "token": "token for ldap/directory.example@EXAMPLE.COM" }
                }),
            ],
        )
    );
}

#[tokio::test]
async fn registers_only_configured_callbacks_and_scope() {
    let cases = [
        (ClientOptions::new(), vec![]),
        (
            ClientOptions::new().with_network(NetworkConfig::new()),
            vec![],
        ),
        (
            ClientOptions::new()
                .with_network(NetworkConfig::new().with_scope(NetworkScope::Runtime)),
            vec![],
        ),
        (
            ClientOptions::new().with_network(NetworkConfig::new().with_resolve_proxy(direct)),
            vec![json!({ "method": "network.setProvider", "params": { "resolveProxy": {} } })],
        ),
        (
            ClientOptions::new().with_network(
                NetworkConfig::new()
                    .with_scope(NetworkScope::Runtime)
                    .with_authenticate_negotiate(unavailable),
            ),
            vec![json!({
                "method": "network.setProvider",
                "params": { "scope": "runtime", "authenticateNegotiate": {} }
            })],
        ),
        (
            ClientOptions::new().with_network(
                NetworkConfig::new()
                    .with_scope(NetworkScope::Connection)
                    .with_resolve_proxy(direct),
            ),
            vec![json!({
                "method": "network.setProvider",
                "params": { "scope": "connection", "resolveProxy": {} }
            })],
        ),
    ];
    let mut registrations = Vec::new();
    let mut expected = Vec::new();
    for (options, registration) in cases {
        let replies = registration
            .iter()
            .map(|_| json!({ "result": null }))
            .collect();
        let (client, runtime) = start(options, replies).await;
        client.unwrap().force_stop();
        registrations.push(runtime.registrations());
        expected.push(registration);
    }
    assert_eq!(registrations, expected);
}

/// A client created from streams takes no options, so it registers its callbacks explicitly, and
/// can retry a registration the runtime rejected.
#[tokio::test]
async fn stream_clients_register_callbacks_explicitly() {
    let (client_io, runtime_io) = tokio::io::duplex(64 * 1024);
    let (client_reader, client_writer) = tokio::io::split(client_io);
    let (mut reader, mut writer) = tokio::io::split(runtime_io);
    let runtime = tokio::spawn(async move {
        let mut frames = Vec::new();
        for reply in [
            json!({ "result": { "ok": true, "protocolVersion": 3, "version": "test" } }),
            json!({ "error": { "code": -32603, "message": "runtime scope is held" } }),
            json!({ "result": null }),
        ] {
            let request = read_frame(&mut reader).await;
            let mut response = json!({ "jsonrpc": "2.0", "id": request["id"] });
            response
                .as_object_mut()
                .unwrap()
                .extend(reply.as_object().unwrap().clone());
            write_frame(&mut writer, response).await;
            frames.push(json!({ "method": request["method"], "params": request["params"] }));
        }
        write_frame(
            &mut writer,
            json!({
                "jsonrpc": "2.0", "id": 1000, "method": "network.resolveProxy",
                "params": { "url": "https://api.example.com/" }
            }),
        )
        .await;
        frames.push(read_frame(&mut reader).await);
        frames
    });
    let client = Client::from_streams(client_reader, client_writer, std::env::temp_dir()).unwrap();
    client.verify_protocol_version().await.unwrap();
    let rejected = client
        .register_network(
            NetworkConfig::new()
                .with_scope(NetworkScope::Runtime)
                .with_resolve_proxy(resolve_proxy)
                .with_authenticate_negotiate(authenticate_negotiate),
        )
        .await;
    let retried = client
        .register_network(NetworkConfig::new().with_resolve_proxy(resolve_proxy))
        .await;
    let frames = timeout(WAIT, runtime).await.unwrap().unwrap();
    client.force_stop();
    assert_eq!(
        (rejected.is_err(), retried.is_ok(), &frames[1..]),
        (
            true,
            true,
            &[
                json!({
                    "method": "network.setProvider",
                    "params": {
                        "scope": "runtime", "resolveProxy": {}, "authenticateNegotiate": {}
                    }
                }),
                json!({ "method": "network.setProvider", "params": { "resolveProxy": {} } }),
                json!({
                    "jsonrpc": "2.0", "id": 1000,
                    "result": { "proxy": "PROXY proxy.example:8080; DIRECT" }
                }),
            ][..]
        )
    );
}

/// A registration the caller stopped waiting for still completes: the runtime's rejection rolls
/// back its handlers, so a later registration on the same connection succeeds.
#[tokio::test]
async fn dropped_registration_is_rolled_back_when_rejected() {
    let (client_io, runtime_io) = tokio::io::duplex(64 * 1024);
    let (client_reader, client_writer) = tokio::io::split(client_io);
    let (mut reader, mut writer) = tokio::io::split(runtime_io);
    let (reject, rejected) = oneshot::channel::<()>();
    let runtime = tokio::spawn(async move {
        let connect = read_frame(&mut reader).await;
        write_frame(
            &mut writer,
            json!({
                "jsonrpc": "2.0", "id": connect["id"],
                "result": { "ok": true, "protocolVersion": 3, "version": "test" }
            }),
        )
        .await;
        let mut registrations = Vec::new();
        for (reply, wait) in [
            (
                json!({ "error": { "code": -32603, "message": "runtime scope is held" } }),
                Some(rejected),
            ),
            (json!({ "result": null }), None),
        ] {
            let request = read_frame(&mut reader).await;
            if let Some(wait) = wait {
                wait.await.unwrap();
            }
            let mut response = json!({ "jsonrpc": "2.0", "id": request["id"] });
            response
                .as_object_mut()
                .unwrap()
                .extend(reply.as_object().unwrap().clone());
            write_frame(&mut writer, response).await;
            registrations.push(json!({ "method": request["method"], "params": request["params"] }));
        }
        registrations
    });
    let client = Client::from_streams(client_reader, client_writer, std::env::temp_dir()).unwrap();
    client.verify_protocol_version().await.unwrap();
    let dropped = timeout(
        Duration::from_millis(50),
        client.register_network(
            NetworkConfig::new()
                .with_scope(NetworkScope::Runtime)
                .with_resolve_proxy(resolve_proxy)
                .with_authenticate_negotiate(authenticate_negotiate),
        ),
    )
    .await;
    reject.send(()).unwrap();
    // The rejected registration's handlers are removed in the background.
    let retried = timeout(WAIT, async {
        loop {
            match client
                .register_network(NetworkConfig::new().with_resolve_proxy(resolve_proxy))
                .await
            {
                Err(error) if error.to_string().contains("already registered") => {
                    tokio::time::sleep(Duration::from_millis(10)).await;
                }
                result => break result,
            }
        }
    })
    .await;
    let registrations = timeout(WAIT, runtime).await.unwrap().unwrap();
    client.force_stop();
    assert_eq!(
        (
            dropped.is_err(),
            matches!(retried, Ok(Ok(()))),
            registrations
        ),
        (
            true,
            true,
            vec![
                json!({
                    "method": "network.setProvider",
                    "params": {
                        "scope": "runtime", "resolveProxy": {}, "authenticateNegotiate": {}
                    }
                }),
                json!({ "method": "network.setProvider", "params": { "resolveProxy": {} } }),
            ]
        )
    );
}

#[tokio::test]
async fn registers_before_other_startup_requests_and_sessions() {
    let plugins = std::env::temp_dir();
    let (client, mut runtime) = start(
        ClientOptions::new()
            .with_builtin_plugin_directories([&plugins])
            .with_network(NetworkConfig::new().with_authenticate_negotiate(unavailable)),
        vec![json!({ "result": null }), json!({ "result": null })],
    )
    .await;
    let client = client.unwrap();
    let session = tokio::spawn({
        let client = client.clone();
        async move { client.create_session(SessionConfig::default()).await }
    });
    let session_request = read_frame(&mut runtime.reader).await;
    client.force_stop();
    session.abort();
    assert_eq!(
        (runtime.registrations(), &session_request["method"]),
        (
            vec![
                json!({
                    "method": "network.setProvider",
                    "params": { "authenticateNegotiate": {} }
                }),
                json!({
                    "method": "plugins.builtin.set",
                    "params": { "paths": [plugins.to_str().unwrap()] }
                }),
            ],
            &json!("session.create"),
        )
    );
}

#[tokio::test]
async fn failed_registration_fails_start() {
    let (client, _runtime) = start(
        ClientOptions::new().with_network(NetworkConfig::new().with_resolve_proxy(direct)),
        vec![json!({ "error": { "code": -32603, "message": "already registered" } })],
    )
    .await;
    let error = client.unwrap_err();
    assert_eq!(
        (error.rpc_code(), error.to_string()),
        (
            Some(-32603),
            "RPC error -32603: already registered".to_string()
        )
    );
}

/// Cancelling `Client::start` while the runtime has not answered the registration closes the
/// connection: the registration still in flight does not keep the abandoned client alive.
#[tokio::test]
async fn cancelled_start_closes_the_connection_during_registration() {
    let listener = tokio::net::TcpListener::bind(("127.0.0.1", 0))
        .await
        .unwrap();
    let port = listener.local_addr().unwrap().port();
    let (registering, registration) = oneshot::channel();
    let runtime = tokio::spawn(async move {
        let (stream, _) = listener.accept().await.unwrap();
        let (mut reader, mut writer) = stream.into_split();
        let connect = read_frame(&mut reader).await;
        write_frame(
            &mut writer,
            json!({
                "jsonrpc": "2.0", "id": connect["id"],
                "result": { "ok": true, "protocolVersion": 3, "version": "test" }
            }),
        )
        .await;
        let request = read_frame(&mut reader).await;
        registering.send(request["method"].clone()).unwrap();
        // Never answer the registration; only the connection closing ends this read.
        timeout(WAIT, reader.read_to_end(&mut Vec::new()))
            .await
            .is_ok()
    });
    let starting = tokio::spawn(Client::start(
        ClientOptions::new()
            .with_network(NetworkConfig::new().with_resolve_proxy(direct))
            .with_program(CliProgram::Path("unused-external-transport".into()))
            .with_transport(Transport::External {
                host: "127.0.0.1".to_string(),
                port,
                connection_token: None,
            }),
    ));
    let method = timeout(WAIT, registration).await.unwrap().unwrap();
    starting.abort();
    let _ = starting.await;
    let closed = timeout(WAIT * 2, runtime).await.unwrap().unwrap();
    assert_eq!((method, closed), (json!("network.setProvider"), true));
}

#[tokio::test]
async fn answers_missing_values_and_returns_callback_failures_and_panics() {
    let (client, mut runtime) = start(both_callbacks(), vec![json!({ "result": null })]).await;
    let client = client.unwrap();
    let responses = send_requests(
        &mut runtime,
        &[
            (
                "network.resolveProxy",
                json!({ "url": "http://direct.example/path" }),
            ),
            (
                "network.authenticateNegotiate",
                json!({ "spn": "HTTP/unavailable" }),
            ),
            (
                "network.resolveProxy",
                json!({ "url": "https://failing.example/" }),
            ),
            (
                "network.authenticateNegotiate",
                json!({ "spn": "HTTP/failing" }),
            ),
            (
                "network.resolveProxy",
                json!({ "url": "https://panicking.example/" }),
            ),
            (
                "network.authenticateNegotiate",
                json!({ "spn": "HTTP/panicking" }),
            ),
        ],
    )
    .await;
    client.force_stop();
    assert_eq!(
        responses,
        vec![
            json!({ "jsonrpc": "2.0", "id": 1000, "result": {} }),
            json!({ "jsonrpc": "2.0", "id": 1001, "result": {} }),
            json!({
                "jsonrpc": "2.0", "id": 1002,
                "error": { "code": -32603, "message": "lookup failed" }
            }),
            json!({
                "jsonrpc": "2.0", "id": 1003,
                "error": { "code": -32603, "message": "lookup failed" }
            }),
            json!({
                "jsonrpc": "2.0", "id": 1004,
                "error": { "code": -32603, "message": "request handler panicked" }
            }),
            json!({
                "jsonrpc": "2.0", "id": 1005,
                "error": { "code": -32603, "message": "request handler panicked" }
            }),
        ]
    );
}

#[tokio::test]
async fn cancelled_requests_drop_the_callback_future() {
    let mut outcomes = Vec::new();
    for (method, stalled, later) in [
        (
            "network.resolveProxy",
            json!({ "url": "https://stalled.example/" }),
            json!({ "url": "https://api.example.com/" }),
        ),
        (
            "network.authenticateNegotiate",
            json!({ "spn": "HTTP/stalled" }),
            json!({ "spn": "HTTP/proxy.example" }),
        ),
    ] {
        let (client, mut runtime, mut events) = start_stalled_request(method, stalled, 1000).await;
        write_frame(
            &mut runtime.writer,
            json!({ "jsonrpc": "2.0", "method": "$/cancelRequest", "params": { "id": 1000 } }),
        )
        .await;
        let dropped = timeout(WAIT, events.recv()).await.unwrap();
        let cancelled = read_frame(&mut runtime.reader).await;
        // The next frame answers a later request, not the cancelled one again.
        write_frame(
            &mut runtime.writer,
            json!({ "jsonrpc": "2.0", "id": 1001, "method": method, "params": later }),
        )
        .await;
        let answered = read_frame(&mut runtime.reader).await;
        client.force_stop();
        outcomes.push((method, dropped, cancelled, answered));
    }
    let cancelled = json!({
        "jsonrpc": "2.0", "id": 1000,
        "error": { "code": -32800, "message": "Request cancelled" }
    });
    assert_eq!(
        outcomes,
        [
            (
                "network.resolveProxy",
                Some("dropped"),
                cancelled.clone(),
                json!({
                    "jsonrpc": "2.0", "id": 1001,
                    "result": { "proxy": "PROXY proxy.example:8080; DIRECT" }
                }),
            ),
            (
                "network.authenticateNegotiate",
                Some("dropped"),
                cancelled,
                json!({
                    "jsonrpc": "2.0", "id": 1001,
                    "result": { "token": "token for HTTP/proxy.example" }
                }),
            ),
        ]
    );
}

#[tokio::test]
async fn closed_connection_drops_pending_callback_futures() {
    let mut dropped = Vec::new();
    for (method, params) in [
        (
            "network.resolveProxy",
            json!({ "url": "https://stalled.example/" }),
        ),
        (
            "network.authenticateNegotiate",
            json!({ "spn": "HTTP/stalled" }),
        ),
    ] {
        let (client, runtime, mut events) = start_stalled_request(method, params, 1000).await;
        drop(runtime);
        dropped.push((method, timeout(WAIT, events.recv()).await.unwrap()));
        client.force_stop();
    }
    assert_eq!(
        dropped,
        [
            ("network.resolveProxy", Some("dropped")),
            ("network.authenticateNegotiate", Some("dropped")),
        ]
    );
}