magi-code 0.62.0

Repository-aware CLI coding agent for terminal work
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
use crate::{
    agent::cancellation::AgentCancellation,
    config::LspSettings,
    lsp::{
        MAX_REFERENCES, MAX_TOOL_DIAGNOSTICS,
        client::LspClient,
        diagnostics::{DiagnosticsSummary, format_diagnostics_tool, format_injected_diagnostics},
        queries::{format_references, one_based_to_lsp},
        sync::{LanguageRoute, read_text_for_sync, route_for_path},
    },
    output::redact_sensitive_text,
};
use std::{
    collections::BTreeMap,
    path::{Path, PathBuf},
    sync::{Arc, Mutex},
    time::{Duration, Instant},
};

const MAX_RESPAWNS_PER_SESSION: u8 = 2;

pub(crate) struct LspManager {
    settings: LspSettings,
    workspace_root: PathBuf,
    servers: Mutex<BTreeMap<String, Arc<Mutex<ManagedServer>>>>,
}

struct ManagedServer {
    route: LanguageRoute,
    client: Option<LspClient>,
    respawns: u8,
    disabled_for_session: bool,
    last_used: Instant,
    notice_emitted: bool,
}

pub(crate) struct EditDiagnosticsRequest<'a> {
    pub(crate) path: &'a Path,
    pub(crate) content: String,
}

impl LspManager {
    pub(crate) fn new(settings: LspSettings, workspace_root: PathBuf) -> Self {
        let workspace_root = workspace_root.canonicalize().unwrap_or(workspace_root);
        Self {
            settings,
            workspace_root,
            servers: Mutex::new(BTreeMap::new()),
        }
    }

    pub(crate) fn is_enabled(&self) -> bool {
        self.settings.enabled
    }

    pub(crate) fn inject_diagnostics_on_edit(&self) -> bool {
        self.settings.inject_diagnostics_on_edit
    }

    pub(crate) fn edit_budget(&self) -> Duration {
        Duration::from_millis(self.settings.diagnostics_wait_ms)
    }

    pub(crate) fn sync_and_wait_diagnostics(
        &self,
        request: EditDiagnosticsRequest<'_>,
        deadline: Instant,
        cancellation: &AgentCancellation,
    ) -> Option<String> {
        if !self.is_enabled() || !self.inject_diagnostics_on_edit() || cancellation.is_canceled() {
            return None;
        }
        if !self.is_inside_workspace(request.path) {
            return None;
        }
        let route = route_for_path(request.path, &self.settings)?;
        let server = self.server_entry(route)?;
        let mut server = server.lock().ok()?;
        let client = self.client_for(&mut server, deadline, cancellation).ok()?;
        client.drain_pending_notifications();
        let (version, started) = match client.sync_full_document(
            request.path,
            request.content,
            deadline,
            cancellation,
        ) {
            Ok(result) => result,
            Err(_) => {
                Self::drop_failed_client(&mut server);
                return None;
            }
        };
        let diagnostics = match client.wait_for_fresh_diagnostics(
            request.path,
            version,
            started,
            deadline,
            cancellation,
        ) {
            Some(diagnostics) => diagnostics,
            None => {
                if client.is_closed() {
                    Self::drop_failed_client(&mut server);
                }
                return None;
            }
        };
        server.last_used = Instant::now();
        let block =
            format_injected_diagnostics(&server.route.server_id, request.path, &diagnostics);
        (!block.trim().is_empty()).then_some(block)
    }

    pub(crate) fn diagnostics_tool(
        &self,
        path: Option<&Path>,
        limit: usize,
        _cancellation: &AgentCancellation,
    ) -> anyhow::Result<String> {
        if !self.is_enabled() {
            return Ok("LSP diagnostics disabled; set lsp.enabled=true in settings".to_string());
        }
        let limit = limit.min(MAX_TOOL_DIAGNOSTICS);
        let Some(path) = path else {
            let mut summaries = Vec::new();
            if let Ok(servers) = self.servers.lock() {
                for server in servers.values() {
                    if let Ok(mut server) = server.lock()
                        && let Some(client) = server.client.as_mut()
                    {
                        summaries.extend(client.workspace_diagnostics_summary(limit).files);
                    }
                }
            }
            summaries.truncate(limit);
            let summary = DiagnosticsSummary {
                total_files: summaries.len(),
                files: summaries,
            };
            return Ok(format_diagnostics_tool(None, summary));
        };
        if !self.is_inside_workspace(path) || route_for_path(path, &self.settings).is_none() {
            return Ok(redact_sensitive_text(&format!(
                "no cached diagnostics for {}; run after edit or use project checks",
                path.display()
            )));
        }
        let mut diagnostics = None;
        if let Ok(servers) = self.servers.lock()
            && let Some(server) = route_for_path(path, &self.settings)
                .and_then(|route| servers.get(&route.server_id).cloned())
            && let Ok(mut server) = server.lock()
            && let Some(client) = server.client.as_mut()
            && client.has_cached_diagnostics_for_path(path)
        {
            diagnostics = Some(client.diagnostics_for_path(path));
        }
        let Some(diagnostics) = diagnostics else {
            return Ok(format_diagnostics_tool(
                Some(path),
                DiagnosticsSummary {
                    files: Vec::new(),
                    total_files: 0,
                },
            ));
        };
        Ok(format_injected_diagnostics("cached", path, &diagnostics))
    }

    pub(crate) fn references_tool(
        &self,
        path: &Path,
        line: u32,
        column: u32,
        include_declaration: bool,
        limit: usize,
        cancellation: &AgentCancellation,
    ) -> anyhow::Result<String> {
        if !self.is_enabled() {
            return Ok("LSP references disabled; set lsp.enabled=true in settings".to_string());
        }
        if !self.is_inside_workspace(path) {
            anyhow::bail!("path is outside LSP workspace root");
        }
        let text = read_text_for_sync(path)?;
        let position = one_based_to_lsp(line, column, &text)?;
        let route = route_for_path(path, &self.settings)
            .ok_or_else(|| anyhow::anyhow!("unsupported file type for LSP references"))?;
        let deadline = Instant::now() + self.query_timeout();
        let server = self
            .server_entry(route)
            .ok_or_else(|| anyhow::anyhow!("LSP manager lock poisoned"))?;
        let mut server = server
            .lock()
            .map_err(|_| anyhow::anyhow!("LSP server lock poisoned"))?;
        let client = self.client_for(&mut server, deadline, cancellation)?;
        if let Err(error) = client.sync_full_document(path, text, deadline, cancellation) {
            Self::drop_failed_client(&mut server);
            return Err(error);
        }
        let references = match client.references(
            path,
            position,
            include_declaration,
            limit.min(MAX_REFERENCES),
            self.query_timeout(),
            cancellation,
        ) {
            Ok(references) => references,
            Err(error) => {
                Self::drop_failed_client(&mut server);
                return Err(error);
            }
        };
        server.last_used = Instant::now();
        Ok(format_references(
            &references,
            limit.min(MAX_REFERENCES),
            &self.workspace_root,
        ))
    }

    pub(crate) fn shutdown_idle(&self) {
        let idle_after =
            Duration::from_secs(self.settings.idle_shutdown_minutes.saturating_mul(60));
        let now = Instant::now();
        if let Ok(servers) = self.servers.lock() {
            for server in servers.values() {
                if let Ok(mut server) = server.lock()
                    && server
                        .client
                        .as_ref()
                        .is_some_and(|_| now.duration_since(server.last_used) >= idle_after)
                    && let Some(client) = server.client.take()
                {
                    client.shutdown();
                }
            }
        }
    }

    pub(crate) fn shutdown_all(&self) {
        if let Ok(servers) = self.servers.lock() {
            for server in servers.values() {
                if let Ok(mut server) = server.lock()
                    && let Some(client) = server.client.take()
                {
                    client.shutdown();
                }
            }
        }
    }

    fn server_entry(&self, route: LanguageRoute) -> Option<Arc<Mutex<ManagedServer>>> {
        let mut servers = self.servers.lock().ok()?;
        Some(
            servers
                .entry(route.server_id.clone())
                .or_insert_with(|| {
                    Arc::new(Mutex::new(ManagedServer {
                        route,
                        client: None,
                        respawns: 0,
                        disabled_for_session: false,
                        last_used: Instant::now(),
                        notice_emitted: false,
                    }))
                })
                .clone(),
        )
    }

    fn client_for<'a>(
        &self,
        server: &'a mut ManagedServer,
        deadline: Instant,
        _cancellation: &AgentCancellation,
    ) -> anyhow::Result<&'a mut LspClient> {
        if server.disabled_for_session {
            anyhow::bail!("LSP server disabled for session");
        }
        if server.client.is_none() {
            if server.respawns >= MAX_RESPAWNS_PER_SESSION {
                server.disabled_for_session = true;
                server.notice_emitted = true;
                anyhow::bail!("LSP server respawn limit reached");
            }
            let timeout = deadline.saturating_duration_since(Instant::now());
            if timeout.is_zero() {
                anyhow::bail!("LSP deadline elapsed before spawn");
            }
            match LspClient::start_with_timeout(
                server.route.server_id.clone(),
                &server.route.config,
                &self.workspace_root,
                server.route.language_id.clone(),
                timeout,
            ) {
                Ok(client) => {
                    server.client = Some(client);
                    server.respawns += 1;
                }
                Err(error) => {
                    server.respawns += 1;
                    if server.respawns >= MAX_RESPAWNS_PER_SESSION {
                        server.disabled_for_session = true;
                        server.notice_emitted = true;
                    }
                    return Err(error);
                }
            }
        }
        server
            .client
            .as_mut()
            .ok_or_else(|| anyhow::anyhow!("LSP client unavailable"))
    }

    fn drop_failed_client(server: &mut ManagedServer) {
        if let Some(client) = server.client.take() {
            client.shutdown();
        }
        if server.respawns >= MAX_RESPAWNS_PER_SESSION {
            server.disabled_for_session = true;
            server.notice_emitted = true;
        }
    }

    fn is_inside_workspace(&self, path: &Path) -> bool {
        path.canonicalize()
            .map(|path| path.starts_with(&self.workspace_root))
            .unwrap_or(false)
    }

    fn query_timeout(&self) -> Duration {
        Duration::from_millis(self.settings.diagnostics_wait_ms.min(30_000))
    }
}

impl Drop for LspManager {
    fn drop(&mut self) {
        self.shutdown_all();
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::LspServerConfig;
    use std::fs;

    fn fake_server_script(mode: &str) -> String {
        format!(
            r#"
import json, sys, time
mode = {mode:?}
root_uri = None

def read_msg():
    header = b''
    while not header.endswith(b'\r\n\r\n'):
        chunk = sys.stdin.buffer.readline()
        if not chunk:
            return None
        header += chunk
    length = 0
    for line in header.decode().splitlines():
        if line.lower().startswith('content-length:'):
            length = int(line.split(':', 1)[1].strip())
    return json.loads(sys.stdin.buffer.read(length).decode())

def send(value):
    body = json.dumps(value, separators=(',', ':')).encode()
    sys.stdout.buffer.write(b'Content-Length: ' + str(len(body)).encode() + b'\r\n\r\n' + body)
    sys.stdout.buffer.flush()

while True:
    msg = read_msg()
    if msg is None:
        break
    method = msg.get('method')
    if method == 'initialize':
        if mode == 'crash':
            sys.exit(7)
        if mode == 'crash_after_initialize':
            root_uri = msg.get('params', {{}}).get('rootUri')
            send({{'jsonrpc':'2.0','id':msg['id'],'result':{{'capabilities':{{'textDocumentSync':1,'referencesProvider':True}}}}}})
            continue
        root_uri = msg.get('params', {{}}).get('rootUri')
        if mode == 'stale_unversioned' and root_uri:
            send({{'jsonrpc':'2.0','method':'textDocument/publishDiagnostics','params':{{'uri':root_uri.rstrip('/') + '/lib.rs','diagnostics':[{{'range':{{'start':{{'line':0,'character':0}},'end':{{'line':0,'character':1}}}},'severity':1,'source':'fake','message':'old unversioned'}}]}}}})
        send({{'jsonrpc':'2.0','id':msg['id'],'result':{{'capabilities':{{'textDocumentSync':1,'referencesProvider':True}}}}}})
    elif method == 'initialized':
        if mode == 'crash_after_initialize':
            sys.exit(8)
        pass
    elif method in ('textDocument/didOpen','textDocument/didChange'):
        if mode == 'stale_unversioned':
            continue
        td = msg['params'].get('textDocument', {{}})
        uri = td.get('uri')
        version = td.get('version')
        send({{'jsonrpc':'2.0','method':'textDocument/publishDiagnostics','params':{{'uri':uri,'version':version,'diagnostics':[{{'range':{{'start':{{'line':0,'character':0}},'end':{{'line':0,'character':1}}}},'severity':1,'source':'fake','message':'boom'}}]}}}})
    elif method == 'textDocument/references':
        uri = msg['params']['textDocument']['uri']
        send({{'jsonrpc':'2.0','id':msg['id'],'result':[{{'uri':uri,'range':{{'start':{{'line':1,'character':0}},'end':{{'line':1,'character':1}}}}}}]}})
    elif method == 'shutdown':
        send({{'jsonrpc':'2.0','id':msg['id'],'result':None}})
    elif method == 'exit':
        break
"#
        )
    }

    fn settings_with_fake(mode: &str) -> LspSettings {
        let mut settings = LspSettings {
            enabled: true,
            diagnostics_wait_ms: 1_000,
            ..LspSettings::default()
        };
        settings.servers.insert(
            "rust-analyzer".to_string(),
            LspServerConfig {
                command: "python3".to_string(),
                args: vec!["-u".to_string(), "-c".to_string(), fake_server_script(mode)],
                enabled: true,
            },
        );
        settings
    }

    #[test]
    fn disabled_and_unsupported_paths_do_not_spawn() {
        let temp = tempfile::TempDir::new().unwrap();
        let path = temp.path().join("lib.rs");
        fs::write(&path, "fn main() {}\n").unwrap();
        let disabled = LspManager::new(LspSettings::default(), temp.path().to_path_buf());

        assert!(
            disabled
                .sync_and_wait_diagnostics(
                    EditDiagnosticsRequest {
                        path: &path,
                        content: "fn main() {}\n".to_string(),
                    },
                    Instant::now() + Duration::from_millis(100),
                    &AgentCancellation::default(),
                )
                .is_none()
        );
        assert!(
            disabled
                .diagnostics_tool(Some(&path), 10, &AgentCancellation::default())
                .unwrap()
                .contains("disabled")
        );

        let enabled = LspManager::new(settings_with_fake("normal"), temp.path().to_path_buf());
        let txt = temp.path().join("notes.txt");
        fs::write(&txt, "notes\n").unwrap();
        assert!(
            enabled
                .sync_and_wait_diagnostics(
                    EditDiagnosticsRequest {
                        path: &txt,
                        content: "notes\n".to_string(),
                    },
                    Instant::now() + Duration::from_millis(100),
                    &AgentCancellation::default(),
                )
                .is_none()
        );
        assert!(enabled.servers.lock().unwrap().is_empty());
    }

    #[test]
    fn sync_injects_diagnostics_and_reuses_server() {
        let temp = tempfile::TempDir::new().unwrap();
        let path = temp.path().join("lib.rs");
        fs::write(&path, "fn main() {}\n").unwrap();
        let manager = LspManager::new(settings_with_fake("normal"), temp.path().to_path_buf());

        let first = manager
            .sync_and_wait_diagnostics(
                EditDiagnosticsRequest {
                    path: &path,
                    content: "fn main() {}\n".to_string(),
                },
                Instant::now() + Duration::from_secs(2),
                &AgentCancellation::default(),
            )
            .unwrap();
        assert!(first.contains("DIAGNOSTICS"));
        assert!(first.contains("boom"));
        let second = manager
            .sync_and_wait_diagnostics(
                EditDiagnosticsRequest {
                    path: &path,
                    content: "fn main() { }\n".to_string(),
                },
                Instant::now() + Duration::from_secs(2),
                &AgentCancellation::default(),
            )
            .unwrap();
        assert!(second.contains("boom"));
        let server = manager.servers.lock().unwrap()["rust-analyzer"].clone();
        assert_eq!(server.lock().unwrap().respawns, 1);
    }

    #[test]
    fn stale_unversioned_diagnostics_before_sync_are_not_injected() {
        let temp = tempfile::TempDir::new().unwrap();
        let path = temp.path().join("lib.rs");
        fs::write(&path, "fn main() {}\n").unwrap();
        let manager = LspManager::new(
            settings_with_fake("stale_unversioned"),
            temp.path().to_path_buf(),
        );

        let block = manager.sync_and_wait_diagnostics(
            EditDiagnosticsRequest {
                path: &path,
                content: "fn main() {}\n".to_string(),
            },
            Instant::now() + Duration::from_millis(200),
            &AgentCancellation::default(),
        );

        assert!(
            block.is_none(),
            "stale unversioned diagnostics were injected: {block:?}"
        );
    }

    #[test]
    fn diagnostics_before_edit_reports_no_cache_and_references_syncs_first() {
        let temp = tempfile::TempDir::new().unwrap();
        let path = temp.path().join("lib.rs");
        fs::write(&path, "fn main() {}\nlet x = 1;\n").unwrap();
        let manager = LspManager::new(settings_with_fake("normal"), temp.path().to_path_buf());

        let no_cache = manager
            .diagnostics_tool(Some(&path), 10, &AgentCancellation::default())
            .unwrap();
        assert!(no_cache.contains("no cached diagnostics"));

        let refs = manager
            .references_tool(&path, 1, 1, false, 10, &AgentCancellation::default())
            .unwrap();
        assert_eq!(refs, "lib.rs:2:1");
        let server = manager.servers.lock().unwrap()["rust-analyzer"].clone();
        assert!(server.lock().unwrap().client.is_some());
    }

    #[test]
    fn outside_workspace_does_not_sync_and_spawn_failures_hit_respawn_cap() {
        let temp = tempfile::TempDir::new().unwrap();
        let outside = tempfile::NamedTempFile::new().unwrap();
        let manager = LspManager::new(settings_with_fake("normal"), temp.path().to_path_buf());
        assert!(
            manager
                .sync_and_wait_diagnostics(
                    EditDiagnosticsRequest {
                        path: outside.path(),
                        content: "fn main() {}\n".to_string(),
                    },
                    Instant::now() + Duration::from_millis(100),
                    &AgentCancellation::default(),
                )
                .is_none()
        );
        assert!(manager.servers.lock().unwrap().is_empty());

        let path = temp.path().join("lib.rs");
        fs::write(&path, "fn main() {}\n").unwrap();
        let crashing = LspManager::new(settings_with_fake("crash"), temp.path().to_path_buf());
        for _ in 0..3 {
            assert!(
                crashing
                    .sync_and_wait_diagnostics(
                        EditDiagnosticsRequest {
                            path: &path,
                            content: "fn main() {}\n".to_string(),
                        },
                        Instant::now() + Duration::from_secs(1),
                        &AgentCancellation::default(),
                    )
                    .is_none()
            );
        }
        let server = crashing.servers.lock().unwrap()["rust-analyzer"].clone();
        let server = server.lock().unwrap();
        assert!(server.disabled_for_session);
        assert_eq!(server.respawns, MAX_RESPAWNS_PER_SESSION);
    }

    #[test]
    fn post_initialize_crash_drops_stale_client_and_disables_after_respawn_cap() {
        let temp = tempfile::TempDir::new().unwrap();
        let path = temp.path().join("lib.rs");
        fs::write(&path, "fn main() {}\n").unwrap();
        let manager = LspManager::new(
            settings_with_fake("crash_after_initialize"),
            temp.path().to_path_buf(),
        );

        for _ in 0..3 {
            assert!(
                manager
                    .sync_and_wait_diagnostics(
                        EditDiagnosticsRequest {
                            path: &path,
                            content: "fn main() {}\n".to_string(),
                        },
                        Instant::now() + Duration::from_secs(1),
                        &AgentCancellation::default(),
                    )
                    .is_none()
            );
        }

        let server = manager.servers.lock().unwrap()["rust-analyzer"].clone();
        let server = server.lock().unwrap();
        assert!(server.disabled_for_session);
        assert!(server.client.is_none());
        assert_eq!(server.respawns, MAX_RESPAWNS_PER_SESSION);
    }
}