codebase-recall 0.7.4

CLI based application for codebase dumper for LLMs and fast review/recall project
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
//! A small synchronous LSP client speaking JSON-RPC over a child process'
//! stdio. Enough of the protocol to open documents and ask for definitions.
//!
//! Everything the rest of code-rcl does is synchronous, so rather than pull in
//! an async runtime this reads the server on a background thread and hands
//! messages to the caller through a channel — which is also what makes every
//! wait here bounded by a timeout instead of blocking forever on a server that
//! died or wedged.

use std::collections::HashSet;
use std::io::{BufRead, BufReader, Read, Write};
use std::path::Path;
use std::process::{Child, ChildStderr, ChildStdin, ChildStdout, Stdio};
use std::sync::mpsc::{Receiver, RecvTimeoutError, Sender, channel};
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::{Duration, Instant};

use anyhow::{Context, Result, anyhow, bail};
use serde_json::{Value, json};

use super::backend::Launcher;
use super::map::path_to_uri;

/// How many lines of the server's stderr to keep for error messages.
const STDERR_TAIL_LINES: usize = 40;

/// Why a readiness wait ended. Both outcomes are usable; the caller decides
/// whether to warn.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Readiness {
    /// The server reported that it finished indexing.
    Ready,
    /// It was still working when the budget ran out. Answers may be incomplete.
    TimedOut,
}

pub struct LspClient {
    child: Child,
    stdin: ChildStdin,
    inbox: Receiver<Value>,
    stderr_tail: Arc<Mutex<Vec<String>>>,
    next_id: i64,
    /// `$/progress` tokens the server has begun but not ended.
    outstanding_progress: HashSet<String>,
    /// Set when the server reports it has gone quiet (rust-analyzer).
    quiescent: bool,
    name: String,
    root: String,
}

impl LspClient {
    /// Spawn the server and wire up its pipes.
    pub fn start(launcher: &Launcher, root: &Path) -> Result<Self> {
        let mut command = launcher.command();
        command
            .current_dir(root)
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped());

        let mut child = command.spawn().with_context(|| {
            format!(
                "starting language server `{}`.\n  \
                 It was found at {}, but could not be executed. \
                 Check that it runs from a terminal and that any runtime it needs \
                 (Node.js for pyright, a JDK for jdtls/kotlin-language-server) is installed.",
                launcher.name,
                launcher.program.display()
            )
        })?;

        let stdin = child
            .stdin
            .take()
            .ok_or_else(|| anyhow!("language server `{}` gave no stdin", launcher.name))?;
        let stdout = child
            .stdout
            .take()
            .ok_or_else(|| anyhow!("language server `{}` gave no stdout", launcher.name))?;
        let stderr = child
            .stderr
            .take()
            .ok_or_else(|| anyhow!("language server `{}` gave no stderr", launcher.name))?;

        let (tx, inbox) = channel();
        thread::spawn(move || read_messages(stdout, tx));

        let stderr_tail = Arc::new(Mutex::new(Vec::new()));
        // Drained on its own thread: a server that fills its stderr pipe while
        // nobody reads it will block and look like a hang.
        thread::spawn({
            let tail = Arc::clone(&stderr_tail);
            move || drain_stderr(stderr, tail)
        });

        Ok(Self {
            child,
            stdin,
            inbox,
            stderr_tail,
            next_id: 1,
            outstanding_progress: HashSet::new(),
            quiescent: false,
            name: launcher.name.clone(),
            root: path_to_uri(root),
        })
    }

    pub fn name(&self) -> &str {
        &self.name
    }

    /// The tail of the server's own log output, for error messages.
    pub fn stderr_tail(&self) -> String {
        self.stderr_tail
            .lock()
            .map(|t| t.join("\n"))
            .unwrap_or_default()
    }

    /// `initialize` + `initialized`. Must be the first thing sent.
    pub fn initialize(&mut self, root: &Path, timeout: Duration) -> Result<()> {
        let params = json!({
            "processId": std::process::id(),
            "clientInfo": { "name": "code-rcl", "version": env!("CARGO_PKG_VERSION") },
            "rootUri": self.root,
            "rootPath": root.to_string_lossy(),
            "workspaceFolders": [{
                "uri": self.root,
                "name": root.file_name().map(|n| n.to_string_lossy().to_string())
                            .unwrap_or_else(|| "workspace".to_string()),
            }],
            "capabilities": {
                "workspace": {
                    "workspaceFolders": true,
                    "configuration": true,
                    "didChangeConfiguration": { "dynamicRegistration": false },
                },
                "textDocument": {
                    "synchronization": { "dynamicRegistration": false, "didSave": false },
                    // linkSupport gets us `targetSelectionRange`: the definition's
                    // name range rather than its whole body.
                    "definition": { "dynamicRegistration": false, "linkSupport": true },
                },
                "window": { "workDoneProgress": true },
                "general": { "positionEncodings": ["utf-16"] },
                // rust-analyzer answers this with `quiescent: true` once it has
                // finished indexing — an exact "ready" signal.
                "experimental": { "serverStatusNotification": true },
            },
        });

        self.request("initialize", params, timeout)
            .with_context(|| format!("handshaking with language server `{}`", self.name))?;
        self.notify("initialized", json!({}))?;
        Ok(())
    }

    /// Wait for the server to finish its initial indexing.
    ///
    /// Querying before that returns empty answers, which would silently look
    /// like "nothing resolves" — so this is a correctness step, not a nicety.
    pub fn wait_until_ready(&mut self, max: Duration) -> Readiness {
        let start = Instant::now();
        // Servers that never report progress at all shouldn't be waited on for
        // the full budget, so give progress a short window to show up.
        let grace = Duration::from_secs(3);
        let settle = Duration::from_millis(400);
        let mut saw_progress = false;
        let mut idle_since: Option<Instant> = None;

        while start.elapsed() < max {
            if self.quiescent {
                return Readiness::Ready;
            }
            if !self.outstanding_progress.is_empty() {
                saw_progress = true;
                idle_since = None;
            } else if saw_progress {
                match idle_since {
                    Some(t) if t.elapsed() >= settle => return Readiness::Ready,
                    Some(_) => {}
                    None => idle_since = Some(Instant::now()),
                }
            } else if start.elapsed() >= grace {
                return Readiness::Ready;
            }
            // Drives the message loop; anything but a response is handled inline.
            let _ = self.pump(None, Instant::now() + Duration::from_millis(200));
        }
        Readiness::TimedOut
    }

    pub fn did_open(&mut self, uri: &str, language_id: &str, text: &str) -> Result<()> {
        self.notify(
            "textDocument/didOpen",
            json!({
                "textDocument": {
                    "uri": uri,
                    "languageId": language_id,
                    "version": 1,
                    "text": text,
                }
            }),
        )
    }

    pub fn did_close(&mut self, uri: &str) -> Result<()> {
        self.notify(
            "textDocument/didClose",
            json!({ "textDocument": { "uri": uri } }),
        )
    }

    /// `textDocument/definition` at a zero-based UTF-16 position.
    pub fn definition(
        &mut self,
        uri: &str,
        line: u32,
        character: u32,
        timeout: Duration,
    ) -> Result<Value> {
        self.request(
            "textDocument/definition",
            json!({
                "textDocument": { "uri": uri },
                "position": { "line": line, "character": character },
            }),
            timeout,
        )
    }

    /// Ask the server to stop, then make sure it actually did.
    pub fn shutdown(mut self) {
        let _ = self.request("shutdown", Value::Null, Duration::from_secs(5));
        let _ = self.notify("exit", Value::Null);

        // Give it a moment to leave on its own before forcing the issue, so a
        // server flushing caches to disk isn't cut off mid-write.
        let deadline = Instant::now() + Duration::from_secs(5);
        while Instant::now() < deadline {
            match self.child.try_wait() {
                Ok(Some(_)) => return,
                Ok(None) => thread::sleep(Duration::from_millis(50)),
                Err(_) => break,
            }
        }
        let _ = self.child.kill();
        let _ = self.child.wait();
    }

    // ----- protocol plumbing ------------------------------------------------

    fn request(&mut self, method: &str, params: Value, timeout: Duration) -> Result<Value> {
        let id = self.next_id;
        self.next_id += 1;
        self.send(json!({
            "jsonrpc": "2.0",
            "id": id,
            "method": method,
            "params": params,
        }))?;
        let deadline = Instant::now() + timeout;
        match self.pump(Some(id), deadline)? {
            Some(result) => Ok(result),
            None => Err(anyhow!(
                "language server `{}` did not answer `{method}` within {}s.\n  \
                 The project may be larger than the timeout allows — raise it with \
                 `--precise-timeout <seconds>`.{}",
                self.name,
                timeout.as_secs(),
                self.stderr_hint(),
            )),
        }
    }

    fn notify(&mut self, method: &str, params: Value) -> Result<()> {
        self.send(json!({ "jsonrpc": "2.0", "method": method, "params": params }))
    }

    fn send(&mut self, message: Value) -> Result<()> {
        let body = serde_json::to_vec(&message)?;
        self.stdin
            .write_all(format!("Content-Length: {}\r\n\r\n", body.len()).as_bytes())
            .and_then(|_| self.stdin.write_all(&body))
            .and_then(|_| self.stdin.flush())
            .map_err(|e| {
                anyhow!(
                    "lost the connection to language server `{}` while sending a request ({e}).\n  \
                     It most likely exited early.{}",
                    self.name,
                    self.stderr_hint(),
                )
            })
    }

    /// Read messages until the response to `want` arrives or `deadline` passes.
    ///
    /// Server-to-client requests are answered as they come in: a server that is
    /// left waiting on one of those (jdtls asks for configuration during
    /// startup) simply stops making progress.
    fn pump(&mut self, want: Option<i64>, deadline: Instant) -> Result<Option<Value>> {
        loop {
            let remaining = deadline.saturating_duration_since(Instant::now());
            if remaining.is_zero() {
                return Ok(None);
            }
            let message = match self.inbox.recv_timeout(remaining) {
                Ok(m) => m,
                Err(RecvTimeoutError::Timeout) => return Ok(None),
                Err(RecvTimeoutError::Disconnected) => {
                    bail!(
                        "language server `{}` exited while code-rcl was talking to it.{}",
                        self.name,
                        self.stderr_hint(),
                    );
                }
            };

            let method = message.get("method").and_then(Value::as_str);
            let id = message.get("id");

            match (method, id) {
                // A request from the server.
                (Some(method), Some(id)) => {
                    let reply = self.server_request_result(method, &message);
                    self.send(json!({ "jsonrpc": "2.0", "id": id, "result": reply }))?;
                }
                // A notification.
                (Some(method), None) => {
                    self.handle_notification(method, message.get("params"));
                }
                // A response to one of ours.
                (None, Some(id)) => {
                    if id.as_i64() != want {
                        continue; // a stale answer we no longer care about
                    }
                    if let Some(error) = message.get("error") {
                        let text = error
                            .get("message")
                            .and_then(Value::as_str)
                            .unwrap_or("unknown error");
                        bail!("language server `{}` returned an error: {text}", self.name);
                    }
                    return Ok(Some(message.get("result").cloned().unwrap_or(Value::Null)));
                }
                (None, None) => {}
            }
        }
    }

    /// What to answer a server-initiated request with.
    fn server_request_result(&self, method: &str, message: &Value) -> Value {
        match method {
            // One settings object per requested item, or the server may wait
            // forever for a well-formed answer. Empty means "use your defaults".
            "workspace/configuration" => {
                let n = message
                    .get("params")
                    .and_then(|p| p.get("items"))
                    .and_then(Value::as_array)
                    .map_or(0, Vec::len);
                Value::Array(vec![json!({}); n])
            }
            "workspace/workspaceFolders" => json!([{ "uri": self.root, "name": "workspace" }]),
            _ => Value::Null,
        }
    }

    fn handle_notification(&mut self, method: &str, params: Option<&Value>) {
        match method {
            "$/progress" => {
                let Some(params) = params else { return };
                let Some(token) = params.get("token") else {
                    return;
                };
                let token = token.to_string();
                match params
                    .get("value")
                    .and_then(|v| v.get("kind"))
                    .and_then(Value::as_str)
                {
                    Some("begin") => {
                        self.outstanding_progress.insert(token);
                    }
                    Some("end") => {
                        self.outstanding_progress.remove(&token);
                    }
                    _ => {}
                }
            }
            "experimental/serverStatus" => {
                if let Some(true) = params
                    .and_then(|p| p.get("quiescent"))
                    .and_then(Value::as_bool)
                {
                    self.quiescent = true;
                }
            }
            _ => {}
        }
    }

    /// The server's own last words, appended to an error when it has any.
    fn stderr_hint(&self) -> String {
        let tail = self.stderr_tail();
        if tail.trim().is_empty() {
            String::new()
        } else {
            format!("\n  Last output from the server:\n{}", indent(&tail))
        }
    }
}

fn indent(text: &str) -> String {
    text.lines()
        .map(|l| format!("    {l}"))
        .collect::<Vec<_>>()
        .join("\n")
}

/// Read `Content-Length`-framed JSON messages until the pipe closes.
fn read_messages(stdout: ChildStdout, tx: Sender<Value>) {
    let mut reader = BufReader::new(stdout);
    loop {
        let mut length: Option<usize> = None;
        loop {
            let mut line = String::new();
            match reader.read_line(&mut line) {
                Ok(0) | Err(_) => return, // server closed its stdout
                Ok(_) => {}
            }
            let line = line.trim_end();
            if line.is_empty() {
                break; // blank line ends the header block
            }
            if let Some(value) = line.strip_prefix("Content-Length:") {
                length = value.trim().parse().ok();
            }
        }

        // Without a length there is no way to find the next message boundary.
        let Some(length) = length else { return };
        let mut body = vec![0u8; length];
        if reader.read_exact(&mut body).is_err() {
            return;
        }
        match serde_json::from_slice(&body) {
            // A message we cannot parse is skipped rather than fatal: the frame
            // boundary is still known, so the stream stays usable.
            Err(_) => continue,
            Ok(value) => {
                if tx.send(value).is_err() {
                    return; // client gone
                }
            }
        }
    }
}

fn drain_stderr(stderr: ChildStderr, tail: Arc<Mutex<Vec<String>>>) {
    let reader = BufReader::new(stderr);
    for line in reader.lines().map_while(std::result::Result::ok) {
        if let Ok(mut tail) = tail.lock() {
            if tail.len() == STDERR_TAIL_LINES {
                tail.remove(0);
            }
            tail.push(line);
        }
    }
}