yolop 0.7.0

Yolop — a terminal coding agent built on everruns-runtime
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
//! Minimal LSP client: Content-Length framing, request/response correlation,
//! document sync, and diagnostics capture over any async byte transport.
//!
//! The client is transport-generic (`AsyncRead`/`AsyncWrite`) so tests drive it
//! with an in-process fake server over `tokio::io::duplex`; production wraps a
//! spawned language-server process (see `manager`).

use anyhow::{Context, Result, anyhow, bail};
use serde_json::{Value, json};
use std::collections::HashMap;
use std::pin::Pin;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicI64, Ordering};
use std::time::Duration;
use tokio::io::{AsyncBufReadExt, AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, BufReader};
use tokio::sync::{Mutex, Notify, oneshot};

/// Upper bound on a single framed message; a language server should never
/// legitimately send more, and the cap keeps a corrupt header from OOMing us.
const MAX_MESSAGE_BYTES: usize = 64 * 1024 * 1024;

type BoxWriter = Pin<Box<dyn AsyncWrite + Send>>;

/// Diagnostics last pushed by the server for one document, plus a generation
/// counter so callers can wait for a publish that arrives *after* their edit.
#[derive(Clone, Debug, Default)]
pub(crate) struct PublishedDiagnostics {
    pub diagnostics: Vec<Value>,
    pub generation: u64,
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub(crate) enum SyncOutcome {
    Opened,
    Changed,
    Unchanged,
}

struct OpenDoc {
    version: i64,
    text: String,
}

struct SharedState {
    pending: std::sync::Mutex<HashMap<i64, oneshot::Sender<Result<Value, String>>>>,
    diagnostics: std::sync::Mutex<HashMap<String, PublishedDiagnostics>>,
    diagnostics_generation: AtomicI64,
    diagnostics_changed: Notify,
    exited: AtomicBool,
}

impl SharedState {
    fn fail_all_pending(&self, reason: &str) {
        let mut pending = self.pending.lock().expect("pending lock");
        for (_, tx) in pending.drain() {
            let _ = tx.send(Err(reason.to_string()));
        }
    }
}

pub(crate) struct LspClient {
    writer: Arc<Mutex<BoxWriter>>,
    state: Arc<SharedState>,
    next_id: AtomicI64,
    request_timeout: Duration,
    open_docs: std::sync::Mutex<HashMap<String, OpenDoc>>,
    reader_task: tokio::task::JoinHandle<()>,
}

impl Drop for LspClient {
    fn drop(&mut self) {
        self.reader_task.abort();
    }
}

impl LspClient {
    /// Start a client over raw transport halves and spawn the reader loop.
    /// The handshake (`initialize`) is a separate call so tests can script it.
    pub(crate) fn start(
        reader: impl AsyncRead + Send + Unpin + 'static,
        writer: impl AsyncWrite + Send + Unpin + 'static,
        request_timeout: Duration,
    ) -> Self {
        let writer: Arc<Mutex<BoxWriter>> = Arc::new(Mutex::new(Box::pin(writer)));
        let state = Arc::new(SharedState {
            pending: std::sync::Mutex::new(HashMap::new()),
            diagnostics: std::sync::Mutex::new(HashMap::new()),
            diagnostics_generation: AtomicI64::new(0),
            diagnostics_changed: Notify::new(),
            exited: AtomicBool::new(false),
        });
        let reader_task = tokio::spawn(reader_loop(
            BufReader::new(reader),
            writer.clone(),
            state.clone(),
        ));
        Self {
            writer,
            state,
            next_id: AtomicI64::new(1),
            request_timeout,
            open_docs: std::sync::Mutex::new(HashMap::new()),
            reader_task,
        }
    }

    pub(crate) fn is_exited(&self) -> bool {
        self.state.exited.load(Ordering::SeqCst)
    }

    /// Send a request and await its response, bounded by the client timeout.
    pub(crate) async fn request(&self, method: &str, params: Value) -> Result<Value> {
        if self.is_exited() {
            bail!("language server exited");
        }
        let id = self.next_id.fetch_add(1, Ordering::SeqCst);
        let (tx, rx) = oneshot::channel();
        self.state
            .pending
            .lock()
            .expect("pending lock")
            .insert(id, tx);
        let message = json!({
            "jsonrpc": "2.0",
            "id": id,
            "method": method,
            "params": params,
        });
        if let Err(err) = self.send(&message).await {
            self.state.pending.lock().expect("pending lock").remove(&id);
            return Err(err);
        }
        match tokio::time::timeout(self.request_timeout, rx).await {
            Ok(Ok(Ok(result))) => Ok(result),
            Ok(Ok(Err(err))) => Err(anyhow!("`{method}` failed: {err}")),
            Ok(Err(_)) => Err(anyhow!("`{method}` failed: language server exited")),
            Err(_) => {
                self.state.pending.lock().expect("pending lock").remove(&id);
                Err(anyhow!(
                    "`{method}` timed out after {}s (the server may still be indexing; retry shortly)",
                    self.request_timeout.as_secs()
                ))
            }
        }
    }

    pub(crate) async fn notify(&self, method: &str, params: Value) -> Result<()> {
        if self.is_exited() {
            bail!("language server exited");
        }
        self.send(&json!({
            "jsonrpc": "2.0",
            "method": method,
            "params": params,
        }))
        .await
    }

    async fn send(&self, message: &Value) -> Result<()> {
        let mut writer = self.writer.lock().await;
        write_message(&mut *writer, message).await
    }

    /// LSP `initialize` handshake; returns the server's advertised capabilities.
    pub(crate) async fn initialize(&self, root_uri: &str, root_name: &str) -> Result<Value> {
        let response = self
            .request(
                "initialize",
                json!({
                    "processId": std::process::id(),
                    "clientInfo": { "name": "yolop", "version": env!("CARGO_PKG_VERSION") },
                    "rootUri": root_uri,
                    "workspaceFolders": [{ "uri": root_uri, "name": root_name }],
                    "capabilities": client_capabilities(),
                }),
            )
            .await?;
        self.notify("initialized", json!({})).await?;
        // Some servers (pyright) defer all analysis until the client pushes a
        // configuration; without this, every later request stalls until
        // timeout. Servers that pull config via `workspace/configuration`
        // (answered with nulls by the reader loop) ignore the empty push.
        self.notify(
            "workspace/didChangeConfiguration",
            json!({ "settings": {} }),
        )
        .await?;
        Ok(response.get("capabilities").cloned().unwrap_or(Value::Null))
    }

    /// Make the server's view of a document match `text`: `didOpen` on first
    /// touch, full-text `didChange` when the content moved underneath it.
    pub(crate) async fn sync_document(
        &self,
        uri: &str,
        language_id: &str,
        text: &str,
    ) -> Result<SyncOutcome> {
        enum Action {
            Open,
            Change(i64),
        }
        let action = {
            let mut docs = self.open_docs.lock().expect("open docs lock");
            match docs.get_mut(uri) {
                None => {
                    docs.insert(
                        uri.to_string(),
                        OpenDoc {
                            version: 1,
                            text: text.to_string(),
                        },
                    );
                    Action::Open
                }
                Some(doc) if doc.text != text => {
                    doc.version += 1;
                    doc.text = text.to_string();
                    Action::Change(doc.version)
                }
                Some(_) => return Ok(SyncOutcome::Unchanged),
            }
        };
        match action {
            Action::Open => {
                self.notify(
                    "textDocument/didOpen",
                    json!({
                        "textDocument": {
                            "uri": uri,
                            "languageId": language_id,
                            "version": 1,
                            "text": text,
                        }
                    }),
                )
                .await?;
                Ok(SyncOutcome::Opened)
            }
            Action::Change(version) => {
                self.notify(
                    "textDocument/didChange",
                    json!({
                        "textDocument": { "uri": uri, "version": version },
                        "contentChanges": [{ "text": text }],
                    }),
                )
                .await?;
                Ok(SyncOutcome::Changed)
            }
        }
    }

    /// Snapshot of the latest published diagnostics for `uri`, if any.
    pub(crate) fn published_diagnostics(&self, uri: &str) -> Option<PublishedDiagnostics> {
        self.state
            .diagnostics
            .lock()
            .expect("diagnostics lock")
            .get(uri)
            .cloned()
    }

    /// Wait until the server publishes diagnostics for `uri` with a generation
    /// beyond `after_generation`, or until `timeout` elapses. Returns the
    /// freshest snapshot either way; `bool` reports whether a new publish came.
    pub(crate) async fn wait_for_diagnostics(
        &self,
        uri: &str,
        after_generation: u64,
        timeout: Duration,
    ) -> (Option<PublishedDiagnostics>, bool) {
        let deadline = tokio::time::Instant::now() + timeout;
        loop {
            // Arm the notification *before* checking state so a publish that
            // lands between the check and the wait cannot be missed.
            let notified = self.state.diagnostics_changed.notified();
            if let Some(entry) = self.published_diagnostics(uri)
                && entry.generation > after_generation
            {
                return (Some(entry), true);
            }
            if self.is_exited() {
                return (self.published_diagnostics(uri), false);
            }
            if tokio::time::timeout_at(deadline, notified).await.is_err() {
                return (self.published_diagnostics(uri), false);
            }
        }
    }
}

fn client_capabilities() -> Value {
    json!({
        "general": { "positionEncodings": ["utf-8", "utf-16"] },
        "textDocument": {
            "synchronization": { "dynamicRegistration": false },
            "publishDiagnostics": { "relatedInformation": true, "versionSupport": true },
            "diagnostic": { "dynamicRegistration": false },
            "hover": { "contentFormat": ["markdown", "plaintext"] },
            "definition": { "linkSupport": true },
            "typeDefinition": { "linkSupport": true },
            "implementation": { "linkSupport": true },
            "declaration": { "linkSupport": true },
            "documentSymbol": { "hierarchicalDocumentSymbolSupport": true },
            "codeAction": {
                "codeActionLiteralSupport": {
                    "codeActionKind": { "valueSet": [
                        "", "quickfix", "refactor", "refactor.extract", "refactor.inline",
                        "refactor.rewrite", "source", "source.organizeImports", "source.fixAll"
                    ] }
                },
                "resolveSupport": { "properties": ["edit"] }
            },
            "rename": {}
        },
        "workspace": {
            "workspaceEdit": {
                "documentChanges": true,
                "resourceOperations": ["create", "rename", "delete"]
            },
            "symbol": {},
            "configuration": true,
            "workspaceFolders": true
        },
        "window": { "workDoneProgress": true }
    })
}

async fn reader_loop(
    mut reader: impl AsyncBufReadExt + Unpin,
    writer: Arc<Mutex<BoxWriter>>,
    state: Arc<SharedState>,
) {
    loop {
        match read_message(&mut reader).await {
            Ok(Some(message)) => handle_message(message, &writer, &state).await,
            Ok(None) => break,
            Err(err) => {
                tracing::debug!(target: "yolop::lsp", "lsp read error: {err:#}");
                break;
            }
        }
    }
    state.exited.store(true, Ordering::SeqCst);
    state.fail_all_pending("language server exited");
    // Wake diagnostics waiters so they observe the exit instead of timing out.
    state.diagnostics_changed.notify_waiters();
}

async fn handle_message(message: Value, writer: &Arc<Mutex<BoxWriter>>, state: &Arc<SharedState>) {
    let method = message.get("method").and_then(Value::as_str);
    let id = message.get("id").cloned().filter(|id| !id.is_null());
    match (method, id) {
        // Server-initiated request: answer with a benign default so servers
        // that block on the reply (config, capability registration, progress
        // tokens) keep going.
        (Some(method), Some(id)) => {
            let response = server_request_response(method, message.get("params"), id);
            let mut writer = writer.lock().await;
            if let Err(err) = write_message(&mut *writer, &response).await {
                tracing::debug!(target: "yolop::lsp", "lsp reply failed: {err:#}");
            }
        }
        // Notification.
        (Some("textDocument/publishDiagnostics"), None) => {
            let params = message.get("params").cloned().unwrap_or(Value::Null);
            let Some(uri) = params.get("uri").and_then(Value::as_str) else {
                return;
            };
            let diagnostics = params
                .get("diagnostics")
                .and_then(Value::as_array)
                .cloned()
                .unwrap_or_default();
            let generation = state
                .diagnostics_generation
                .fetch_add(1, Ordering::SeqCst)
                .saturating_add(1) as u64;
            state.diagnostics.lock().expect("diagnostics lock").insert(
                uri.to_string(),
                PublishedDiagnostics {
                    diagnostics,
                    generation,
                },
            );
            state.diagnostics_changed.notify_waiters();
        }
        (Some(_), None) => {} // window/logMessage, $/progress, …
        // Response to one of our requests.
        (None, Some(id)) => {
            let Some(id) = id.as_i64() else { return };
            let Some(tx) = state.pending.lock().expect("pending lock").remove(&id) else {
                return;
            };
            let result = match message.get("error") {
                Some(error) if !error.is_null() => Err(error
                    .get("message")
                    .and_then(Value::as_str)
                    .unwrap_or("unknown server error")
                    .to_string()),
                _ => Ok(message.get("result").cloned().unwrap_or(Value::Null)),
            };
            let _ = tx.send(result);
        }
        (None, None) => {}
    }
}

fn server_request_response(method: &str, params: Option<&Value>, id: Value) -> Value {
    let result = match method {
        // One `null` per requested item — "no client-side configuration".
        "workspace/configuration" => {
            let count = params
                .and_then(|p| p.get("items"))
                .and_then(Value::as_array)
                .map_or(0, Vec::len);
            Value::Array(vec![Value::Null; count])
        }
        "client/registerCapability"
        | "client/unregisterCapability"
        | "window/workDoneProgress/create"
        | "window/showMessageRequest" => Value::Null,
        "workspace/applyEdit" => json!({ "applied": false }),
        _ => {
            return json!({
                "jsonrpc": "2.0",
                "id": id,
                "error": { "code": -32601, "message": format!("method not supported by yolop: {method}") },
            });
        }
    };
    json!({ "jsonrpc": "2.0", "id": id, "result": result })
}

/// Read one `Content-Length`-framed JSON-RPC message; `None` on clean EOF.
pub(crate) async fn read_message<R: AsyncBufReadExt + Unpin>(
    reader: &mut R,
) -> Result<Option<Value>> {
    let mut content_length: Option<usize> = None;
    let mut saw_header = false;
    loop {
        let mut line = String::new();
        let read = reader
            .read_line(&mut line)
            .await
            .context("read LSP header")?;
        if read == 0 {
            if saw_header {
                bail!("unexpected EOF inside LSP message headers");
            }
            return Ok(None);
        }
        let line = line.trim_end_matches(['\r', '\n']);
        if line.is_empty() {
            if saw_header {
                // End of headers; Content-Length must have been among them.
                break;
            }
            // Tolerate stray blank lines between messages.
            continue;
        }
        saw_header = true;
        let Some((name, value)) = line.split_once(':') else {
            bail!("malformed LSP header line: {line:?}");
        };
        if name.eq_ignore_ascii_case("content-length") {
            let length: usize = value
                .trim()
                .parse()
                .with_context(|| format!("invalid Content-Length: {value:?}"))?;
            if length > MAX_MESSAGE_BYTES {
                bail!("LSP message of {length} bytes exceeds the {MAX_MESSAGE_BYTES} byte cap");
            }
            content_length = Some(length);
        }
        // Content-Type and unknown headers are ignored.
    }
    let length = content_length.context("missing Content-Length header")?;
    let mut body = vec![0u8; length];
    reader
        .read_exact(&mut body)
        .await
        .context("read LSP message body")?;
    Ok(Some(
        serde_json::from_slice(&body).context("parse LSP message body")?,
    ))
}

pub(crate) async fn write_message<W: AsyncWrite + Unpin + ?Sized>(
    writer: &mut W,
    message: &Value,
) -> Result<()> {
    let body = serde_json::to_vec(message).context("serialize LSP message")?;
    let header = format!("Content-Length: {}\r\n\r\n", body.len());
    writer
        .write_all(header.as_bytes())
        .await
        .context("write LSP header")?;
    writer.write_all(&body).await.context("write LSP body")?;
    writer.flush().await.context("flush LSP message")?;
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use tokio::io::duplex;

    #[tokio::test]
    async fn framing_roundtrip() {
        let (client_side, server_side) = duplex(4096);
        let (mut read_half, _keep) = tokio::io::split(client_side);
        let (_keep2, mut write_half) = tokio::io::split(server_side);

        let message = json!({ "jsonrpc": "2.0", "method": "ping", "params": { "x": "héllo" } });
        write_message(&mut write_half, &message)
            .await
            .expect("write");
        let mut reader = BufReader::new(&mut read_half);
        let parsed = read_message(&mut reader)
            .await
            .expect("read")
            .expect("some");
        assert_eq!(parsed, message);
    }

    #[tokio::test]
    async fn read_message_returns_none_on_eof() {
        let (client_side, server_side) = duplex(64);
        drop(server_side);
        let mut reader = BufReader::new(client_side);
        assert!(read_message(&mut reader).await.expect("read").is_none());
    }

    #[tokio::test]
    async fn read_message_rejects_missing_content_length() {
        let (client_side, mut server_side) = duplex(256);
        server_side
            .write_all(b"Content-Type: application/json\r\n\r\n")
            .await
            .expect("write");
        drop(server_side);
        let mut reader = BufReader::new(client_side);
        let err = read_message(&mut reader).await.expect_err("should fail");
        assert!(err.to_string().contains("Content-Length"), "{err}");
    }

    /// Scripted fake server: answers `initialize`, replies to a `demo/echo`
    /// request, asks for `workspace/configuration`, publishes diagnostics.
    async fn fake_server(transport: tokio::io::DuplexStream) {
        let (read_half, mut write_half) = tokio::io::split(transport);
        let mut reader = BufReader::new(read_half);
        while let Ok(Some(message)) = read_message(&mut reader).await {
            let method = message.get("method").and_then(Value::as_str).unwrap_or("");
            let id = message.get("id").cloned();
            match method {
                "initialize" => {
                    let response = json!({
                        "jsonrpc": "2.0",
                        "id": id,
                        "result": { "capabilities": { "positionEncoding": "utf-8" } },
                    });
                    write_message(&mut write_half, &response)
                        .await
                        .expect("write");
                }
                "initialized" => {
                    // Exercise the server-request auto-reply path.
                    let request = json!({
                        "jsonrpc": "2.0",
                        "id": 999,
                        "method": "workspace/configuration",
                        "params": { "items": [{}, {}] },
                    });
                    write_message(&mut write_half, &request)
                        .await
                        .expect("write");
                }
                "textDocument/didOpen" => {
                    let uri = message["params"]["textDocument"]["uri"].clone();
                    let notification = json!({
                        "jsonrpc": "2.0",
                        "method": "textDocument/publishDiagnostics",
                        "params": {
                            "uri": uri,
                            "diagnostics": [{
                                "range": {
                                    "start": { "line": 0, "character": 0 },
                                    "end": { "line": 0, "character": 1 }
                                },
                                "severity": 1,
                                "message": "fake error"
                            }]
                        },
                    });
                    write_message(&mut write_half, &notification)
                        .await
                        .expect("write");
                }
                "demo/echo" => {
                    let response = json!({
                        "jsonrpc": "2.0",
                        "id": id,
                        "result": message.get("params").cloned().unwrap_or(Value::Null),
                    });
                    write_message(&mut write_half, &response)
                        .await
                        .expect("write");
                }
                _ => {
                    // Assert the client answered our workspace/configuration
                    // request with two nulls (routed here as a "response").
                    if message.get("id").and_then(Value::as_i64) == Some(999) {
                        assert_eq!(message["result"], json!([null, null]));
                    }
                }
            }
        }
    }

    fn start_pair() -> LspClient {
        let (client_transport, server_transport) = duplex(64 * 1024);
        tokio::spawn(fake_server(server_transport));
        let (read_half, write_half) = tokio::io::split(client_transport);
        LspClient::start(read_half, write_half, Duration::from_secs(5))
    }

    #[tokio::test]
    async fn initialize_and_request_roundtrip() {
        let client = start_pair();
        let capabilities = client.initialize("file:///tmp/x", "x").await.expect("init");
        assert_eq!(capabilities["positionEncoding"], "utf-8");

        let echoed = client
            .request("demo/echo", json!({ "hello": "world" }))
            .await
            .expect("echo");
        assert_eq!(echoed, json!({ "hello": "world" }));
    }

    #[tokio::test]
    async fn did_open_captures_published_diagnostics() {
        let client = start_pair();
        client.initialize("file:///tmp/x", "x").await.expect("init");

        let uri = "file:///tmp/x/main.rs";
        let outcome = client
            .sync_document(uri, "rust", "fn main() {}")
            .await
            .expect("sync");
        assert_eq!(outcome, SyncOutcome::Opened);

        let (entry, fresh) = client
            .wait_for_diagnostics(uri, 0, Duration::from_secs(5))
            .await;
        assert!(fresh);
        let entry = entry.expect("diagnostics entry");
        assert_eq!(entry.diagnostics.len(), 1);
        assert_eq!(entry.diagnostics[0]["message"], "fake error");

        // Same content again: no version bump, no didChange.
        let outcome = client
            .sync_document(uri, "rust", "fn main() {}")
            .await
            .expect("sync");
        assert_eq!(outcome, SyncOutcome::Unchanged);
        let outcome = client
            .sync_document(uri, "rust", "fn main() { }")
            .await
            .expect("sync");
        assert_eq!(outcome, SyncOutcome::Changed);
    }

    #[tokio::test]
    async fn pending_requests_fail_when_server_exits() {
        let (client_transport, server_transport) = duplex(64 * 1024);
        let (read_half, write_half) = tokio::io::split(client_transport);
        let client = LspClient::start(read_half, write_half, Duration::from_secs(30));
        let request = tokio::spawn(async move { client.request("demo/never", json!({})).await });
        tokio::time::sleep(Duration::from_millis(50)).await;
        drop(server_transport);
        let err = request.await.expect("join").expect_err("should fail");
        assert!(err.to_string().contains("exited"), "{err}");
    }
}