pi_agent_rust 0.3.0

Native AI coding agent CLI - Rust port of Pi Agent
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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
//! Agent-facing `lsp` tool: IDE-grade code intelligence over child LSP
//! servers (bd-cv653.1.1).
//!
//! Position-based addressing follows the omp convention: `file` + `line`
//! (1-indexed) + `symbol` substring (with an optional `#N` suffix selecting
//! the Nth occurrence). Project-aware lookups (definition, references,
//! rename, code actions, ...) error without a symbol — no silent fallback.
//! `rename` applies the returned WorkspaceEdit atomically (all files or
//! none); `rename_file` goes through `workspace/willRenameFiles` when the
//! server advertises it so importers update before the file moves.

pub mod client;
pub mod edits;
pub mod jsonrpc;
pub mod registry;
pub mod text;

use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::atomic::Ordering;
use std::time::Duration;

use async_trait::async_trait;
use serde::Deserialize;
use serde_json::{Value, json};

use client::{hover_to_text, parse_locations, path_to_uri, uri_to_path};
use edits::{WorkspaceEditPlan, apply_workspace_edit, parse_workspace_edit};
use registry::{LspRegistry, ServerEntry};
use text::{Position, find_occurrences, line_count, offset_to_position};

use crate::config::Config;
use crate::error::{Error, Result};
use crate::model::{ContentBlock, TextContent};
use crate::tools::{Tool, ToolEffects, ToolOutput, ToolUpdate};

/// Cap on serialized JSON payloads in tool output (symbols/actions lists).
const MAX_PAYLOAD_BYTES: usize = 200 * 1024;
/// Default cap on locations returned by references.
const DEFAULT_LOCATION_LIMIT: usize = 100;
/// Hard cap on locations returned by references.
const HARD_LOCATION_LIMIT: usize = 1000;

fn text_output(text: String, details: Value) -> ToolOutput {
    ToolOutput {
        content: vec![ContentBlock::Text(TextContent::new(text))],
        details: Some(details),
        is_error: false,
    }
}

fn usage_error(message: impl Into<String>) -> ToolOutput {
    ToolOutput {
        content: vec![ContentBlock::Text(TextContent::new(message.into()))],
        details: None,
        is_error: true,
    }
}

fn tool_err(code: &str, message: impl Into<String>) -> Error {
    Error::tool("lsp", format!("[{code}] {}", message.into()))
}

/// Resolve a user-supplied path against the tool working directory.
fn resolve_tool_path(path: &str, cwd: &Path) -> PathBuf {
    let candidate = Path::new(path);
    if candidate.is_absolute() {
        candidate.to_path_buf()
    } else {
        cwd.join(candidate)
    }
}

/// Display path relative to the working directory when possible.
fn display_path(path: &Path, cwd: &Path) -> String {
    path.strip_prefix(cwd).map_or_else(
        |_| path.display().to_string(),
        |rel| rel.display().to_string(),
    )
}

/// Parse a symbol selector: `name` or `name#N` (1-indexed occurrence).
fn parse_symbol_selector(raw: &str) -> (String, Option<usize>) {
    if let Some((name, nth)) = raw.rsplit_once('#')
        && !name.is_empty()
        && let Ok(nth) = nth.parse::<usize>()
        && nth >= 1
    {
        return (name.to_string(), Some(nth));
    }
    (raw.to_string(), None)
}

/// The `lsp` tool.
///
/// One instance per registry construction; owns the server registry (and
/// therefore all spawned language servers) for its lifetime.
pub struct LspTool {
    cwd: PathBuf,
    registry: LspRegistry,
}

impl LspTool {
    #[must_use]
    pub fn new(cwd: &Path, config: Option<&Config>) -> Self {
        Self {
            cwd: cwd.to_path_buf(),
            registry: LspRegistry::new(cwd, config),
        }
    }

    /// Language id for a file under a given server spec.
    fn language_id_for(path: &Path, spec: &registry::ServerSpec) -> String {
        path.extension()
            .and_then(|ext| ext.to_str())
            .map(|ext| format!(".{}", ext.to_ascii_lowercase()))
            .and_then(|dotted| registry::language_id_for_extension(&dotted).map(str::to_string))
            .or_else(|| spec.languages.first().cloned())
            .unwrap_or_else(|| "plaintext".to_string())
    }

    /// Resolve + spawn the server for `path`, install the applyEdit hook on
    /// first use, and return the entry.
    async fn client_for(&self, path: &Path) -> Result<Arc<ServerEntry>> {
        let entry = self.registry.client_for(path).await?;
        install_apply_edit_handler(&entry);
        Ok(entry)
    }

    /// Sync the document and return `(uri, server entry)`.
    async fn synced(&self, path: &Path) -> Result<(String, Arc<ServerEntry>)> {
        let entry = self.client_for(path).await?;
        let spec = self.registry.spec_for_file(path).ok_or_else(|| {
            tool_err("LSP_NO_SERVER", format!("no server for {}", path.display()))
        })?;
        let language_id = Self::language_id_for(path, spec);
        let uri = entry.client.ensure_synced(path, &language_id)?;
        Ok((uri, entry))
    }

    /// Resolve a position from file + line + symbol selector.
    fn resolve_position(path: &Path, line: Option<u32>, symbol: &str) -> Result<Position> {
        let content = std::fs::read_to_string(path).map_err(|err| {
            tool_err(
                "LSP_FILE_UNREADABLE",
                format!("cannot read {}: {err}", path.display()),
            )
        })?;
        let (needle, nth) = parse_symbol_selector(symbol);
        if needle.is_empty() {
            return Err(tool_err("LSP_NO_SYMBOL", "symbol must not be empty"));
        }
        let occurrences = find_occurrences(&content, &needle, line.map(|l| l.saturating_sub(1)));
        if occurrences.is_empty() {
            let scope = line.map_or_else(|| "file".to_string(), |l| format!("line {l}"));
            return Err(tool_err(
                "LSP_NO_SYMBOL",
                format!(
                    "no occurrence of {needle:?} in {scope} of {}",
                    path.display()
                ),
            ));
        }
        let selected = match (nth, occurrences.len()) {
            (Some(n), len) if n <= len => occurrences[n - 1],
            (Some(n), len) => {
                return Err(tool_err(
                    "LSP_SYMBOL_AMBIGUOUS",
                    format!(
                        "selector asked for occurrence #{n} but only {len} match(es) of {needle:?} exist"
                    ),
                ));
            }
            (None, 1) => occurrences[0],
            (None, len) => {
                if line.is_some() {
                    // Within one line, multiple matches: require #N.
                    return Err(tool_err(
                        "LSP_SYMBOL_AMBIGUOUS",
                        format!(
                            "{len} matches of {needle:?} on that line; disambiguate with {needle}#N"
                        ),
                    ));
                }
                return Err(tool_err(
                    "LSP_SYMBOL_AMBIGUOUS",
                    format!(
                        "{len} matches of {needle:?} in file; narrow with `line` or {needle}#N"
                    ),
                ));
            }
        };
        offset_to_position(&content, selected.0).ok_or_else(|| {
            tool_err(
                "LSP_NO_SYMBOL",
                format!("occurrence of {needle:?} does not map to an LSP position"),
            )
        })
    }

    fn request_timeout(&self, input: &LspInput) -> Duration {
        input
            .timeout
            .filter(|secs| *secs > 0)
            .map_or_else(|| self.registry.request_timeout(), Duration::from_secs)
    }

    /// Render a location list as JSON payload + details.
    fn locations_output(
        &self,
        action: &str,
        locations: &[(String, text::Range)],
        limit: usize,
    ) -> ToolOutput {
        let mut entries = Vec::new();
        for (uri, range) in locations.iter().take(limit) {
            let path =
                uri_to_path(uri).map_or_else(|| uri.clone(), |p| display_path(&p, &self.cwd));
            entries.push(json!({
                "file": path,
                "line": range.start.line + 1,
                "character": range.start.character + 1,
            }));
        }
        let truncated = locations.len() > limit;
        let payload = json!({
            "action": action,
            "count": entries.len(),
            "truncated": truncated,
            "locations": entries,
        });
        text_output(payload.to_string(), payload)
    }

    async fn run_diagnostics(&self, input: &LspInput) -> Result<ToolOutput> {
        let Some(file) = input.file.as_deref() else {
            return Ok(usage_error(
                "lsp diagnostics requires `file` (a path or glob like src/**/*.rs)",
            ));
        };
        let is_glob = file.contains(['*', '[', '?']);
        if is_glob {
            // Glob mode: answer from the diagnostics caches of live servers
            // without spawning anything new.
            let override_filter = build_glob_override(&self.cwd, file)?;
            let mut matched = Vec::new();
            for status in self.registry.status() {
                if let Some(entry) = self.registry.entry_for_root(&status.name, &status.root) {
                    for (uri, diags) in entry.client.diagnostics_snapshot() {
                        if let Some(path) = uri_to_path(&uri) {
                            let rel = path.strip_prefix(&self.cwd).unwrap_or(&path);
                            if override_filter.matched(rel, false).is_ignore() {
                                matched.push(json!({
                                    "file": display_path(&path, &self.cwd),
                                    "server": status.name,
                                    "diagnostics": diags,
                                }));
                            }
                        }
                    }
                }
            }
            let payload = json!({
                "action": "diagnostics",
                "glob": file,
                "files": matched.len(),
                "entries": matched,
            });
            return Ok(text_output(payload.to_string(), payload));
        }

        let path = resolve_tool_path(file, &self.cwd);
        let (uri, entry) = self.synced(&path).await?;
        // Give the server a bounded window to publish fresh diagnostics:
        // the caller's timeout when provided (capped), else the default.
        // Cold servers publish only after indexing, so callers on a fresh
        // spawn should pass a generous timeout.
        let wait = input
            .timeout
            .filter(|secs| *secs > 0)
            .map_or(client::DEFAULT_DIAGNOSTICS_WAIT, |secs| {
                Duration::from_secs(secs).min(Duration::from_secs(60))
            });
        entry.client.wait_for_diagnostics(&uri, wait).await;
        let snapshot = entry.client.diagnostics_snapshot();
        let diags = snapshot.get(&uri).cloned().unwrap_or_default();
        let payload = json!({
            "action": "diagnostics",
            "file": display_path(&path, &self.cwd),
            "server": entry.spec_name,
            "count": diags.len(),
            "diagnostics": diags,
        });
        Ok(text_output(payload.to_string(), payload))
    }

    async fn run_position_request(
        &self,
        input: &LspInput,
        action: &str,
        method: &str,
        extra_params: Value,
    ) -> Result<ToolOutput> {
        let (path, position) = self.require_position(input)?;
        let (uri, entry) = self.synced(&path).await?;
        let mut params = json!({
            "textDocument": { "uri": uri },
            "position": position,
        });
        if let (Some(dst), Some(src)) = (params.as_object_mut(), extra_params.as_object()) {
            for (key, value) in src {
                dst.insert(key.clone(), value.clone());
            }
        }
        let result = entry
            .client
            .call(method, params, self.request_timeout(input))
            .await
            .map_err(Error::from)?;
        if action == "hover" {
            let text = hover_to_text(&result).unwrap_or_else(|| "no hover information".to_string());
            let payload = json!({
                "action": "hover",
                "file": display_path(&path, &self.cwd),
                "line": position.line + 1,
                "hover": text,
            });
            return Ok(text_output(payload.to_string(), payload));
        }
        let locations = parse_locations(&result);
        let limit = input
            .limit
            .unwrap_or(DEFAULT_LOCATION_LIMIT)
            .min(HARD_LOCATION_LIMIT);
        if locations.is_empty() {
            let payload = json!({
                "action": action,
                "file": display_path(&path, &self.cwd),
                "line": position.line + 1,
                "count": 0,
                "locations": [],
                "note": format!("no {action} found at that position"),
            });
            return Ok(text_output(payload.to_string(), payload));
        }
        Ok(self.locations_output(action, &locations, limit))
    }

    fn require_position(&self, input: &LspInput) -> Result<(PathBuf, Position)> {
        let file = input.file.as_deref().ok_or_else(|| {
            tool_err("LSP_USAGE", format!("lsp {} requires `file`", input.action))
        })?;
        let symbol = input.symbol.as_deref().ok_or_else(|| {
            tool_err(
                "LSP_USAGE",
                format!(
                    "lsp {} requires `symbol` (project-aware lookups never guess a position)",
                    input.action
                ),
            )
        })?;
        let path = resolve_tool_path(file, &self.cwd);
        let position = Self::resolve_position(&path, input.line, symbol)?;
        Ok((path, position))
    }

    async fn run_symbols(&self, input: &LspInput) -> Result<ToolOutput> {
        match (input.file.as_deref(), input.query.as_deref()) {
            (Some(file), _) => {
                let path = resolve_tool_path(file, &self.cwd);
                let (uri, entry) = self.synced(&path).await?;
                let result = entry
                    .client
                    .call(
                        "textDocument/documentSymbol",
                        json!({ "textDocument": { "uri": uri } }),
                        self.request_timeout(input),
                    )
                    .await
                    .map_err(Error::from)?;
                let (payload, truncated) = cap_payload(json!({
                    "action": "symbols",
                    "file": display_path(&path, &self.cwd),
                    "server": entry.spec_name,
                    "symbols": result,
                }));
                Ok(text_output(
                    payload.to_string(),
                    json!({"truncated": truncated, "payload": payload}),
                ))
            }
            (None, Some(query)) => {
                // Workspace symbols need *a* server; route via the cwd so the
                // user can pick the server by extension of any anchor file.
                let anchor = input
                    .symbol
                    .as_deref()
                    .map(|s| resolve_tool_path(s, &self.cwd));
                let Some(anchor) = anchor else {
                    return Ok(usage_error(
                        "lsp symbols with `query` also needs `symbol` set to an anchor file path (its extension picks the server)",
                    ));
                };
                let entry = self.client_for(&anchor).await?;
                let result = entry
                    .client
                    .call(
                        "workspace/symbol",
                        json!({ "query": query }),
                        self.request_timeout(input),
                    )
                    .await
                    .map_err(Error::from)?;
                let (payload, truncated) = cap_payload(json!({
                    "action": "symbols",
                    "query": query,
                    "server": entry.spec_name,
                    "symbols": result,
                }));
                Ok(text_output(
                    payload.to_string(),
                    json!({"truncated": truncated, "payload": payload}),
                ))
            }
            (None, None) => Ok(usage_error(
                "lsp symbols requires `file` (document symbols) or `query` (workspace symbols)",
            )),
        }
    }

    async fn run_rename(&self, input: &LspInput) -> Result<ToolOutput> {
        let new_name = input
            .new_name
            .as_deref()
            .ok_or_else(|| tool_err("LSP_USAGE", "lsp rename requires `newName`"))?;
        if new_name.is_empty() {
            return Ok(usage_error("lsp rename requires a non-empty `newName`"));
        }
        let (path, position) = self.require_position(input)?;
        let (uri, entry) = self.synced(&path).await?;
        let result = entry
            .client
            .call(
                "textDocument/rename",
                json!({
                    "textDocument": { "uri": uri },
                    "position": position,
                    "newName": new_name,
                }),
                self.request_timeout(input),
            )
            .await
            .map_err(Error::from)?;
        let plan = parse_workspace_edit(&result)?;
        let outcome = apply_workspace_edit(&plan, None)?;
        // External mutation: forget affected docs so the next sync reopens
        // them from disk.
        for changed in &outcome.files_changed {
            entry.client.invalidate(&path_to_uri(changed));
        }
        let files: Vec<String> = outcome
            .files_changed
            .iter()
            .map(|p| display_path(p, &self.cwd))
            .collect();
        let payload = json!({
            "action": "rename",
            "newName": new_name,
            "filesChanged": files,
            "fileOps": outcome.file_ops_applied,
            "atomic": true,
        });
        Ok(text_output(payload.to_string(), payload))
    }

    async fn run_rename_file(&self, input: &LspInput) -> Result<ToolOutput> {
        let (Some(file), Some(new_file)) = (input.file.as_deref(), input.new_file.as_deref())
        else {
            return Ok(usage_error("lsp rename_file requires `file` and `newFile`"));
        };
        let old_path = resolve_tool_path(file, &self.cwd);
        let new_path = resolve_tool_path(new_file, &self.cwd);
        if !old_path.exists() {
            return Err(tool_err(
                "LSP_FILE_UNREADABLE",
                format!("rename source does not exist: {}", old_path.display()),
            ));
        }
        if new_path.exists() {
            return Err(tool_err(
                "LSP_EDIT_CONFLICT",
                format!("rename target already exists: {}", new_path.display()),
            ));
        }
        let (old_uri, entry) = self.synced(&old_path).await?;
        // canonicalize() fails on not-yet-existing paths; canonicalize the
        // parent and re-attach the file name instead.
        let canonical_new = new_path
            .parent()
            .and_then(|parent| parent.canonicalize().ok())
            .and_then(|parent| new_path.file_name().map(|name| parent.join(name)))
            .unwrap_or_else(|| new_path.clone());
        let new_uri = path_to_uri(&canonical_new);
        let mut edits_applied: Vec<String> = Vec::new();

        // Stage 1: willRenameFiles (when advertised) lets the server compute
        // import updates BEFORE the move.
        if entry.client.capabilities().will_rename_files {
            let result = entry
                .client
                .call(
                    "workspace/willRenameFiles",
                    json!({
                        "files": [{ "oldUri": old_uri, "newUri": new_uri }]
                    }),
                    self.request_timeout(input),
                )
                .await;
            match result {
                Ok(edit) if !edit.is_null() => {
                    let plan = parse_workspace_edit(&edit)?;
                    let outcome = apply_workspace_edit(&plan, None)?;
                    for changed in &outcome.files_changed {
                        entry.client.invalidate(&path_to_uri(changed));
                        edits_applied.push(display_path(changed, &self.cwd));
                    }
                }
                Ok(_) => {}
                Err(err) => {
                    return Err(tool_err(
                        "LSP_SERVER_ERROR",
                        format!(
                            "willRenameFiles failed; file NOT moved (fail-closed): {}",
                            err.message()
                        ),
                    ));
                }
            }
        }

        // Stage 2: the move itself.
        if let Some(parent) = new_path.parent() {
            std::fs::create_dir_all(parent).map_err(|err| {
                tool_err(
                    "LSP_EDIT_APPLY",
                    format!("cannot create {}: {err}", parent.display()),
                )
            })?;
        }
        std::fs::rename(&old_path, &new_path).map_err(|err| {
            tool_err(
                "LSP_EDIT_APPLY",
                format!(
                    "cannot rename {} -> {}: {err}",
                    old_path.display(),
                    new_path.display()
                ),
            )
        })?;

        // Stage 3: notify + forget both endpoints.
        let _ = entry.client.call_no_wait_notify(
            "workspace/didRenameFiles",
            json!({ "files": [{ "oldUri": old_uri, "newUri": new_uri }] }),
        );
        entry.client.invalidate(&old_uri);
        entry.client.invalidate(&new_uri);

        let payload = json!({
            "action": "rename_file",
            "from": display_path(&old_path, &self.cwd),
            "to": display_path(&new_path, &self.cwd),
            "importUpdates": edits_applied,
            "willRenameFiles": entry.client.capabilities().will_rename_files,
        });
        Ok(text_output(payload.to_string(), payload))
    }

    /// Code-action range: symbol+line resolves a point; otherwise the whole
    /// document.
    fn code_action_range(input: &LspInput, path: &Path) -> Result<Value> {
        if let Some(symbol) = input.symbol.as_deref() {
            let position = Self::resolve_position(path, input.line, symbol)?;
            return Ok(json!({ "start": position, "end": position }));
        }
        let content = std::fs::read_to_string(path).map_err(|err| {
            tool_err(
                "LSP_FILE_UNREADABLE",
                format!("cannot read {}: {err}", path.display()),
            )
        })?;
        let last_line = line_count(&content).saturating_sub(1);
        Ok(json!({
            "start": Position { line: 0, character: 0 },
            "end": Position { line: last_line, character: 0 },
        }))
    }

    async fn run_code_actions(&self, input: &LspInput) -> Result<ToolOutput> {
        let file = input
            .file
            .as_deref()
            .ok_or_else(|| tool_err("LSP_USAGE", "lsp code_actions requires `file`"))?;
        let path = resolve_tool_path(file, &self.cwd);
        let (uri, entry) = self.synced(&path).await?;

        let range = Self::code_action_range(input, &path)?;
        let diagnostics = entry
            .client
            .diagnostics_snapshot()
            .get(&uri)
            .cloned()
            .unwrap_or_default();
        let result = entry
            .client
            .call(
                "textDocument/codeAction",
                json!({
                    "textDocument": { "uri": uri },
                    "range": range,
                    "context": { "diagnostics": diagnostics },
                }),
                self.request_timeout(input),
            )
            .await
            .map_err(Error::from)?;
        let actions = result.as_array().cloned().unwrap_or_default();
        let summaries: Vec<Value> = actions
            .iter()
            .enumerate()
            .map(|(index, action)| {
                json!({
                    "index": index + 1,
                    "title": action.get("title").and_then(Value::as_str).unwrap_or("<untitled>"),
                    "kind": action.get("kind").and_then(Value::as_str),
                    "isPreferred": action.get("isPreferred").and_then(Value::as_bool).unwrap_or(false),
                    "disabled": action.get("disabled").is_some(),
                    "hasEdit": action.get("edit").is_some(),
                    "hasCommand": action.get("command").is_some(),
                })
            })
            .collect();

        if !input.apply.unwrap_or(false) {
            let payload = json!({
                "action": "code_actions",
                "file": display_path(&path, &self.cwd),
                "count": summaries.len(),
                "actions": summaries,
            });
            return Ok(text_output(payload.to_string(), payload));
        }

        let query = input.query.as_deref().ok_or_else(|| {
            tool_err(
                "LSP_USAGE",
                "lsp code_actions with `apply: true` requires `query` (action title substring or 1-based index)",
            )
        })?;
        let selected = select_code_action(&actions, query)?;
        self.apply_code_action(input, &entry, &selected).await
    }

    /// Apply one selected code action: edit-bearing actions apply atomically
    /// through the WorkspaceEdit machinery; command-only actions go through
    /// `workspace/executeCommand` (server `workspace/applyEdit` requests are
    /// applied by the installed handler).
    async fn apply_code_action(
        &self,
        input: &LspInput,
        entry: &Arc<ServerEntry>,
        selected: &Value,
    ) -> Result<ToolOutput> {
        let title = selected
            .get("title")
            .and_then(Value::as_str)
            .unwrap_or("<untitled>")
            .to_string();

        if let Some(edit) = selected.get("edit") {
            let plan: WorkspaceEditPlan = parse_workspace_edit(edit)?;
            let outcome = apply_workspace_edit(&plan, None)?;
            for changed in &outcome.files_changed {
                entry.client.invalidate(&path_to_uri(changed));
            }
            let files: Vec<String> = outcome
                .files_changed
                .iter()
                .map(|p| display_path(p, &self.cwd))
                .collect();
            let payload = json!({
                "action": "code_actions",
                "applied": title,
                "filesChanged": files,
                "fileOps": outcome.file_ops_applied,
            });
            return Ok(text_output(payload.to_string(), payload));
        }

        if let Some(command) = selected.get("command") {
            let (command_name, arguments) = if let Some(name) = command.as_str() {
                (
                    name.to_string(),
                    selected.get("arguments").cloned().unwrap_or(Value::Null),
                )
            } else {
                let name = command
                    .get("command")
                    .and_then(Value::as_str)
                    .ok_or_else(|| tool_err("LSP_EDIT_MALFORMED", "command object missing name"))?
                    .to_string();
                let arguments = command.get("arguments").cloned().unwrap_or(Value::Null);
                (name, arguments)
            };
            // workspace/applyEdit requests issued by the server during this
            // call are applied by the installed handler.
            entry
                .client
                .call(
                    "workspace/executeCommand",
                    json!({ "command": command_name, "arguments": arguments }),
                    self.request_timeout(input),
                )
                .await
                .map_err(Error::from)?;
            let payload = json!({
                "action": "code_actions",
                "applied": title,
                "executedCommand": command_name,
            });
            return Ok(text_output(payload.to_string(), payload));
        }

        Err(tool_err(
            "LSP_ACTION_NO_EDIT",
            format!("code action {title:?} carries neither an edit nor a command"),
        ))
    }

    async fn run_status(&self) -> Result<ToolOutput> {
        let statuses = self.registry.status();
        let servers: Vec<Value> = statuses
            .iter()
            .map(|s| {
                json!({
                    "name": s.name,
                    "serverName": s.server_name,
                    "root": s.root.display().to_string(),
                    "alive": s.alive,
                    "idleSecs": s.idle_secs,
                    "openDocuments": s.open_documents,
                    "droppedNotifications": s.dropped_notifications,
                })
            })
            .collect();
        let configured: Vec<Value> = self
            .registry
            .configured_servers()
            .iter()
            .map(|spec| {
                json!({
                    "name": spec.name,
                    "command": spec.command,
                    "extensions": spec.extensions,
                })
            })
            .collect();
        let payload = json!({
            "action": "status",
            "cwd": self.cwd.display().to_string(),
            "live": servers,
            "configured": configured,
        });
        Ok(text_output(payload.to_string(), payload))
    }

    async fn run_reload(&self, input: &LspInput) -> Result<ToolOutput> {
        let path = input
            .file
            .as_deref()
            .map(|f| resolve_tool_path(f, &self.cwd));
        let killed = self.registry.kill_matching(path.as_deref()).await;
        let payload = json!({
            "action": "reload",
            "killed": killed,
            "note": "servers respawn lazily on next use",
        });
        Ok(text_output(payload.to_string(), payload))
    }

    async fn run_capabilities(&self, input: &LspInput) -> Result<ToolOutput> {
        let Some(file) = input.file.as_deref() else {
            return Ok(usage_error(
                "lsp capabilities requires `file` (its extension picks the server)",
            ));
        };
        let path = resolve_tool_path(file, &self.cwd);
        let entry = self.client_for(&path).await?;
        let caps = entry.client.capabilities();
        let payload = json!({
            "action": "capabilities",
            "server": entry.spec_name,
            "serverName": caps.server_name,
            "willRenameFiles": caps.will_rename_files,
            "textDocumentSyncKind": caps.sync_kind,
            "capabilities": caps.raw,
        });
        Ok(text_output(payload.to_string(), payload))
    }

    async fn run_raw_request(&self, input: &LspInput) -> Result<ToolOutput> {
        let (Some(method), Some(file)) = (input.method.as_deref(), input.file.as_deref()) else {
            return Ok(usage_error(
                "lsp request requires `method` and `file` (its extension picks the server)",
            ));
        };
        let path = resolve_tool_path(file, &self.cwd);
        let entry = self.client_for(&path).await?;
        let result = entry
            .client
            .call(
                method,
                input.payload.clone().unwrap_or(Value::Null),
                self.request_timeout(input),
            )
            .await
            .map_err(Error::from)?;
        let (payload, truncated) = cap_payload(json!({
            "action": "request",
            "method": method,
            "server": entry.spec_name,
            "result": result,
        }));
        Ok(text_output(
            payload.to_string(),
            json!({"truncated": truncated, "payload": payload}),
        ))
    }
}

/// Install the `workspace/applyEdit` server-request handler once per entry.
fn install_apply_edit_handler(entry: &Arc<ServerEntry>) {
    if entry.handler_installed.swap(true, Ordering::SeqCst) {
        return;
    }
    let weak = Arc::downgrade(entry);
    entry
        .client
        .set_server_request_handler(Arc::new(move |method, params| {
            if method != "workspace/applyEdit" {
                return None;
            }
            let Some(entry) = weak.upgrade() else {
                return Some(json!({ "applied": false, "failureReason": "client dropped" }));
            };
            let Some(edit) = params.get("edit") else {
                return Some(json!({ "applied": false, "failureReason": "missing edit" }));
            };
            let outcome =
                parse_workspace_edit(edit).and_then(|plan| apply_workspace_edit(&plan, None));
            match outcome {
                Ok(outcome) => {
                    for changed in &outcome.files_changed {
                        entry.client.invalidate(&path_to_uri(changed));
                    }
                    Some(json!({ "applied": true }))
                }
                Err(err) => Some(json!({
                    "applied": false,
                    "failureReason": err.to_string(),
                })),
            }
        }));
}

/// Pick one code action by 1-based index or case-insensitive title substring.
fn select_code_action(actions: &[Value], query: &str) -> Result<Value> {
    if let Ok(index) = query.parse::<usize>() {
        return actions
            .get(index.saturating_sub(1))
            .filter(|_| index >= 1)
            .cloned()
            .ok_or_else(|| {
                tool_err(
                    "LSP_USAGE",
                    format!(
                        "code action index {index} out of range ({} actions)",
                        actions.len()
                    ),
                )
            });
    }
    let needle = query.to_ascii_lowercase();
    let matches: Vec<&Value> = actions
        .iter()
        .filter(|action| {
            action
                .get("title")
                .and_then(Value::as_str)
                .is_some_and(|title| title.to_ascii_lowercase().contains(&needle))
        })
        .collect();
    match matches.len() {
        0 => Err(tool_err(
            "LSP_USAGE",
            format!("no code action title contains {query:?}"),
        )),
        1 => Ok(matches[0].clone()),
        n => Err(tool_err(
            "LSP_SYMBOL_AMBIGUOUS",
            format!("{n} code actions match {query:?}; narrow the query or use a 1-based index"),
        )),
    }
}

/// Cap a JSON payload's serialized size; returns (payload, truncated).
fn cap_payload(payload: Value) -> (Value, bool) {
    let serialized = payload.to_string();
    if serialized.len() <= MAX_PAYLOAD_BYTES {
        return (payload, false);
    }
    let mut truncated = serialized;
    truncated.truncate(MAX_PAYLOAD_BYTES);
    truncated.push_str("...[TRUNCATED]");
    (Value::String(truncated), true)
}

/// Build a gitignore-style glob override anchored at `cwd`.
fn build_glob_override(cwd: &Path, glob: &str) -> Result<ignore::overrides::Override> {
    ignore::overrides::OverrideBuilder::new(cwd)
        .add(glob)
        .and_then(|builder| builder.build())
        .map_err(|err| tool_err("LSP_USAGE", format!("invalid glob {glob:?}: {err}")))
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct LspInput {
    action: String,
    #[serde(default)]
    file: Option<String>,
    #[serde(default)]
    line: Option<u32>,
    #[serde(default)]
    symbol: Option<String>,
    #[serde(default)]
    query: Option<String>,
    #[serde(default)]
    new_name: Option<String>,
    #[serde(default)]
    new_file: Option<String>,
    #[serde(default)]
    apply: Option<bool>,
    #[serde(default)]
    timeout: Option<u64>,
    #[serde(default)]
    method: Option<String>,
    #[serde(default)]
    payload: Option<Value>,
    #[serde(default)]
    limit: Option<usize>,
}

#[async_trait]
#[allow(clippy::unnecessary_literal_bound)]
impl Tool for LspTool {
    fn name(&self) -> &str {
        "lsp"
    }

    fn label(&self) -> &str {
        "lsp"
    }

    fn description(&self) -> &str {
        "IDE-grade code intelligence (definition, references, rename) via language servers. \
         Actions: diagnostics (file or glob), definition, references, hover, symbols (file = document, \
         query = workspace), rename (symbol-aware, atomic multi-file apply), rename_file (moves a file \
         and updates importers via willRenameFiles), code_actions (list; apply with apply:true + query), \
         type_definition, implementation, status, reload, capabilities, request (raw LSP method). \
         Position addressing: file + line (1-indexed) + symbol substring; use symbol#N for the Nth \
         occurrence. Project-aware lookups error without `symbol` — they never guess."
    }

    fn parameters(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "action": {
                    "type": "string",
                    "enum": [
                        "diagnostics", "definition", "references", "hover", "symbols",
                        "rename", "rename_file", "code_actions", "type_definition",
                        "implementation", "status", "reload", "capabilities", "request"
                    ],
                    "description": "The LSP operation to run"
                },
                "file": {
                    "type": "string",
                    "description": "Target file (path relative to cwd or absolute); diagnostics also accepts a glob"
                },
                "line": {
                    "type": "integer",
                    "description": "1-indexed line narrowing the symbol search"
                },
                "symbol": {
                    "type": "string",
                    "description": "Symbol substring; append #N to pick the Nth occurrence"
                },
                "query": {
                    "type": "string",
                    "description": "Workspace-symbol query, or code-action title substring/index when applying"
                },
                "newName": {
                    "type": "string",
                    "description": "New symbol name for rename"
                },
                "newFile": {
                    "type": "string",
                    "description": "Destination path for rename_file"
                },
                "apply": {
                    "type": "boolean",
                    "description": "code_actions: apply the action selected by query instead of listing"
                },
                "timeout": {
                    "type": "integer",
                    "description": "Per-request timeout in seconds (0 = registry default)"
                },
                "method": {
                    "type": "string",
                    "description": "Raw LSP method name for the request action"
                },
                "payload": {
                    "description": "Raw JSON params for the request action"
                },
                "limit": {
                    "type": "integer",
                    "description": "Max locations returned (references/definition), capped at 1000"
                }
            },
            "required": ["action"]
        })
    }

    fn effects(&self) -> ToolEffects {
        ToolEffects::read()
            .union(ToolEffects::write())
            .union(ToolEffects::process())
    }

    #[allow(clippy::too_many_lines)]
    async fn execute(
        &self,
        _tool_call_id: &str,
        input: Value,
        _on_update: Option<Box<dyn Fn(ToolUpdate) + Send + Sync>>,
    ) -> Result<ToolOutput> {
        let input: LspInput = serde_json::from_value(input)
            .map_err(|err| tool_err("LSP_USAGE", format!("invalid input: {err}")))?;
        match input.action.as_str() {
            "diagnostics" => self.run_diagnostics(&input).await,
            "definition" => {
                self.run_position_request(
                    &input,
                    "definition",
                    "textDocument/definition",
                    json!({}),
                )
                .await
            }
            "references" => {
                self.run_position_request(
                    &input,
                    "references",
                    "textDocument/references",
                    json!({ "context": { "includeDeclaration": true } }),
                )
                .await
            }
            "hover" => {
                self.run_position_request(&input, "hover", "textDocument/hover", json!({}))
                    .await
            }
            "type_definition" => {
                self.run_position_request(
                    &input,
                    "type_definition",
                    "textDocument/typeDefinition",
                    json!({}),
                )
                .await
            }
            "implementation" => {
                self.run_position_request(
                    &input,
                    "implementation",
                    "textDocument/implementation",
                    json!({}),
                )
                .await
            }
            "symbols" => self.run_symbols(&input).await,
            "rename" => self.run_rename(&input).await,
            "rename_file" => self.run_rename_file(&input).await,
            "code_actions" => self.run_code_actions(&input).await,
            "status" => self.run_status().await,
            "reload" => self.run_reload(&input).await,
            "capabilities" => self.run_capabilities(&input).await,
            "request" => self.run_raw_request(&input).await,
            other => Ok(usage_error(format!(
                "unknown lsp action {other:?}; expected diagnostics|definition|references|hover|symbols|rename|rename_file|code_actions|type_definition|implementation|status|reload|capabilities|request"
            ))),
        }
    }
}

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

    #[test]
    fn symbol_selector_parses_nth() {
        assert_eq!(
            parse_symbol_selector("render#2"),
            ("render".to_string(), Some(2))
        );
        assert_eq!(
            parse_symbol_selector("render"),
            ("render".to_string(), None)
        );
        // Trailing # without digits is part of the name.
        assert_eq!(parse_symbol_selector("c#"), ("c#".to_string(), None));
        // #0 is invalid as an index and stays part of the name.
        assert_eq!(parse_symbol_selector("x#0"), ("x#0".to_string(), None));
    }

    #[test]
    fn position_resolution_picks_and_disambiguates() {
        let temp = tempfile::tempdir().expect("tempdir");
        let file = temp.path().join("a.rs");
        std::fs::write(&file, "fn alpha() {}\nfn beta() { alpha(); }\n").expect("file");

        // Unique on a line.
        let pos = LspTool::resolve_position(&file, Some(1), "alpha").expect("line 1");
        assert_eq!(
            pos,
            Position {
                line: 0,
                character: 3
            }
        );

        // Ambiguous across file without line.
        let err = LspTool::resolve_position(&file, None, "alpha").expect_err("ambiguous");
        assert!(err.to_string().contains("LSP_SYMBOL_AMBIGUOUS"), "{err}");

        // #N selects.
        let pos = LspTool::resolve_position(&file, None, "alpha#2").expect("second occurrence");
        assert_eq!(pos.line, 1);

        // Out-of-range #N errors.
        assert!(LspTool::resolve_position(&file, None, "alpha#9").is_err());

        // Missing symbol errors.
        let err = LspTool::resolve_position(&file, None, "gamma").expect_err("missing");
        assert!(err.to_string().contains("LSP_NO_SYMBOL"), "{err}");
    }

    #[test]
    fn cap_payload_truncates() {
        let small = json!({"a": 1});
        let (payload, truncated) = cap_payload(small.clone());
        assert!(!truncated);
        assert_eq!(payload, small);

        let big = json!({"data": "x".repeat(MAX_PAYLOAD_BYTES + 100)});
        let (_, truncated) = cap_payload(big);
        assert!(truncated);
    }

    #[test]
    fn select_code_action_by_index_and_title() {
        let actions = vec![
            json!({"title": "Add missing import"}),
            json!({"title": "Extract function"}),
        ];
        assert_eq!(
            select_code_action(&actions, "2").expect("index")["title"],
            "Extract function"
        );
        assert_eq!(
            select_code_action(&actions, "missing").expect("title")["title"],
            "Add missing import"
        );
        assert!(select_code_action(&actions, "9").is_err());
        assert!(select_code_action(&actions, "nope").is_err());
        let both = vec![json!({"title": "Fix all"}), json!({"title": "Fix this"})];
        assert!(select_code_action(&both, "fix").is_err());
    }
}