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
|
"""Language Server Protocol (LSP) integration.
Provides semantic code navigation tools (go to definition, find references,
and get symbol info) by talking to a language server over stdio.
The agent uses this instead of `grep` when it needs to know what a symbol
*means* rather than where its name appears. A grep match can be a comment,
a string, or a shadowed variable; an LSP response is the compiler's own
understanding of the code.
"""
from __future__ import annotations
import asyncio
import json
import os
import re
import shutil
import subprocess
import threading
from typing import Any, Dict, List, Optional
from ..errors import ToolError
from .base import BaseTool
# ---------------------------------------------------------------------------
# JSON-RPC 2.0 over stdio (LSP framing: Content-Length headers)
# ---------------------------------------------------------------------------
class _LSPClient:
"""A minimal synchronous JSON-RPC 2.0 client for an LSP server process.
The reader runs in a dedicated daemon thread doing blocking ``os.read``
on the server's stdout pipe; the writer is the caller's thread. Responses
are delivered back through ``threading.Event`` objects so ``request()``
can block synchronously. This is the same pattern as the proven sync test
and avoids the fragility of ``loop.connect_read_pipe`` on CPython 3.14,
where the pipe transport can silently stop delivering data.
The async tools wrap calls to this client with ``asyncio.to_thread``.
"""
def __init__(self, command: List[str], root: str) -> None:
self.command = command
self.root = root
self._proc: Optional[subprocess.Popen] = None
self._req_id = 0
self._buf = b""
self._responses: Dict[int, Tuple[threading.Event, Dict[str, Any]]] = {}
self._reader_thread: Optional[threading.Thread] = None
self._closed = False
def start(self) -> None:
if self._proc is not None:
return
env = {
k: v
for k, v in os.environ.items()
if not k.startswith("AGENTUX_")
}
self._proc = subprocess.Popen(
self.command,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
bufsize=0,
env=env,
)
self._reader_thread = threading.Thread(
target=self._read_loop, daemon=True
)
self._reader_thread.start()
def _read_loop(self) -> None:
assert self._proc is not None and self._proc.stdout is not None
fd = self._proc.stdout.fileno()
try:
while True:
chunk = os.read(fd, 65536)
if not chunk:
break
self._buf += chunk
# Parse as many complete LSP messages as are available.
while True:
msg, self._buf = self._extract_message(self._buf)
if msg is None:
break
try:
resp = json.loads(msg)
except Exception:
continue
if "id" in resp and resp["id"] is not None:
entry = self._responses.get(resp["id"])
if entry is not None:
entry[1]["resp"] = resp
entry[0].set()
except OSError:
pass
@staticmethod
def _extract_message(buf: bytes) -> tuple:
"""Return (message_bytes, remaining_buf) or (None, buf)."""
header_end = buf.find(b"\r\n\r\n")
if header_end < 0:
return None, buf
header = buf[:header_end].decode("utf-8", "replace")
length = None
for line in header.split("\r\n"):
if line.lower().startswith("content-length:"):
try:
length = int(line.split(":", 1)[1].strip())
except ValueError:
return None, buf
if length is None:
return None, buf[header_end + 4:]
body_start = header_end + 4
if len(buf) < body_start + length:
return None, buf
return buf[body_start:body_start + length], buf[body_start + length:]
def _write(self, body: bytes) -> None:
assert self._proc is not None and self._proc.stdin is not None
header = f"Content-Length: {len(body)}\r\n\r\n".encode("utf-8")
self._proc.stdin.write(header + body)
self._proc.stdin.flush()
def _send(self, method: str, params: Any, expect_response: bool = True) -> Any:
self.start()
request_id = None
if expect_response:
self._req_id += 1
request_id = self._req_id
# Notifications must OMIT the id field entirely. Sending
# ``"id": null`` is rejected by pyright ("Received message which is
# neither a response nor a notification"), which silently drops the
# handshake and leaves later requests hanging.
if expect_response:
body = json.dumps({
"jsonrpc": "2.0",
"id": request_id,
"method": method,
"params": params or {},
}).encode("utf-8")
else:
body = json.dumps({
"jsonrpc": "2.0",
"method": method,
"params": params or {},
}).encode("utf-8")
self._write(body)
if not expect_response:
return None
event = threading.Event()
holder: Dict[str, Any] = {}
self._responses[request_id] = (event, holder)
event.wait(timeout=30.0)
self._responses.pop(request_id, None)
if not event.is_set():
raise ToolError(f"LSP request '{method}' timed out")
resp = holder.get("resp", {})
if resp.get("error"):
raise ToolError(f"LSP error ({resp['error'].get('code')}): {resp['error'].get('message')}")
return resp.get("result")
def notify(self, method: str, params: Any) -> None:
self._send(method, params, expect_response=False)
def request(self, method: str, params: Any) -> Any:
return self._send(method, params, expect_response=True)
def did_open(self, path: str) -> None:
"""Notify the server that a document is open.
Several servers (pyright, rust-analyzer) will not answer
``textDocument/documentSymbol`` or ``textDocument/definition`` for a
file they have not been told is open. We send ``didOpen`` once per
path and ignore failures -- a server that does not care simply
ignores the notification.
"""
if not os.path.isfile(path):
return
try:
with open(path, "r", encoding="utf-8", errors="replace") as f:
text = f.read()
except Exception:
return
try:
self._write(json.dumps({
"jsonrpc": "2.0",
"method": "textDocument/didOpen",
"params": {
"textDocument": {
"uri": "file://" + path,
"languageId": _language_id_for(path),
"version": 1,
"text": text,
},
},
}).encode("utf-8"))
except Exception:
pass
def close(self) -> None:
if self._closed:
return
self._closed = True
if self._proc is not None:
try:
self._send("shutdown", None)
except Exception:
pass
try:
self.notify("exit", {})
except Exception:
pass
try:
self._proc.wait(timeout=2)
except Exception:
try:
self._proc.kill()
except Exception:
pass
try:
self._proc.wait(timeout=2)
except Exception:
try:
self._proc.kill()
except Exception:
pass
if self._reader_thread is not None and self._reader_thread.is_alive():
self._reader_thread.join(timeout=1)
# ---------------------------------------------------------------------------
# Server registry
# ---------------------------------------------------------------------------
#: Map of file extension -> (language id, command, root marker files)
LSP_SERVERS: Dict[str, Dict[str, Any]] = {
".py": {
"language": "python",
"command": ["pyright-langserver", "--stdio"],
"fallback": [["pyls"], ["pylsp"]],
"root_markers": ["pyproject.toml", "setup.py", "requirements.txt", "setup.cfg"],
},
".go": {
"language": "go",
"command": ["gopls", "serve"],
"fallback": [],
"root_markers": ["go.mod", "go.sum"],
},
".ts": {
"language": "typescript",
"command": ["typescript-language-server", "--stdio"],
"fallback": [],
"root_markers": ["tsconfig.json", "package.json"],
},
".tsx": {
"language": "typescript",
"command": ["typescript-language-server", "--stdio"],
"fallback": [],
"root_markers": ["tsconfig.json", "package.json"],
},
".js": {
"language": "javascript",
"command": ["typescript-language-server", "--stdio"],
"fallback": [],
"root_markers": ["package.json"],
},
".jsx": {
"language": "javascript",
"command": ["typescript-language-server", "--stdio"],
"fallback": [],
"root_markers": ["package.json"],
},
".rs": {
"language": "rust",
"command": ["rust-analyzer"],
"fallback": [],
"root_markers": ["Cargo.toml"],
},
".java": {
"language": "java",
"command": ["jdtls"],
"fallback": [],
"root_markers": ["pom.xml", "build.gradle"],
},
".c": {
"language": "c",
"command": ["clangd"],
"fallback": [],
"root_markers": ["compile_commands.json", "Makefile", "CMakeLists.txt"],
},
".cpp": {
"language": "cpp",
"command": ["clangd"],
"fallback": [],
"root_markers": ["compile_commands.json", "Makefile", "CMakeLists.txt"],
},
}
def _language_id_for(path: str) -> str:
"""Return the LSP ``languageId`` for a file based on its extension."""
ext = os.path.splitext(path)[1].lower()
spec = LSP_SERVERS.get(ext)
if spec is None:
return "text"
return spec.get("language", "text")
def _find_server_root(root: str, markers: List[str]) -> str:
"""Walk up from `root` looking for one of the marker files."""
current = root
for _ in range(10):
for marker in markers:
if os.path.exists(os.path.join(current, marker)):
return current
parent = os.path.dirname(current)
if parent == current:
break
current = parent
return root
def _resolve_binary(command: List[str]) -> Optional[List[str]]:
"""Return the command if the binary is available, else None."""
if not command:
return None
if shutil.which(command[0]) is None:
return None
return command
def _try_resolve(spec: Dict[str, Any]) -> Optional[List[str]]:
"""Try the primary command, then each fallback, returning the first that exists."""
primary = _resolve_binary(spec.get("command") or [])
if primary is not None:
return primary
for fallback in spec.get("fallback", []) or []:
if isinstance(fallback, list):
resolved = _resolve_binary(fallback)
else:
resolved = _resolve_binary([fallback, "--stdio"])
if resolved is not None:
return resolved
return None
class LSPManager:
"""Manages LSP server processes and routes requests to them."""
def __init__(self, root: str) -> None:
self.root = os.path.realpath(root)
self._clients: Dict[str, _LSPClient] = {}
def _language_for(self, path: str) -> Optional[str]:
ext = os.path.splitext(path)[1].lower()
spec = LSP_SERVERS.get(ext)
if spec is None:
return None
return spec["language"]
def _spec_for(self, path: str) -> Optional[Dict[str, Any]]:
ext = os.path.splitext(path)[1].lower()
return LSP_SERVERS.get(ext)
async def _get_or_start(self, path: str) -> _LSPClient:
ext = os.path.splitext(path)[1].lower()
spec = LSP_SERVERS.get(ext)
if spec is None:
raise ToolError(f"No LSP server configured for extension '{ext}'")
lang = spec["language"]
if lang in self._clients:
return self._clients[lang]
command = _try_resolve(spec)
if command is None:
raise ToolError(
f"No LSP server binary found for {ext} ({lang}). "
f"Install one of: {', '.join(spec['command'] + spec.get('fallback', []))}"
)
server_root = _find_server_root(self.root, spec.get("root_markers", []))
client = _LSPClient(command, server_root)
await asyncio.to_thread(client.start)
# Initialize
await asyncio.to_thread(client.request, "initialize", {
"processId": os.getpid(),
"rootUri": "file://" + server_root,
"capabilities": {
"textDocument": {
"completion": {"completionItem": {"snippetSupport": True}},
},
},
})
await asyncio.to_thread(client.notify, "initialized", {})
self._clients[lang] = client
return client
async def request(self, path: str, method: str, params: Any) -> Any:
client = await self._get_or_start(path)
return await asyncio.to_thread(client.request, method, params)
async def close_all(self) -> None:
for client in list(self._clients.values()):
await asyncio.to_thread(client.close)
self._clients.clear()
# ---------------------------------------------------------------------------
# Tools
# ---------------------------------------------------------------------------
def _location_to_str(loc: Dict[str, Any], root: str) -> str:
"""Convert an LSP Location to a human-readable path:line:col."""
uri = loc.get("uri", "")
path = uri.replace("file://", "")
if path.startswith(root):
path = path[len(root):].lstrip("/")
rng = loc.get("range", {})
start = rng.get("start", {})
line = start.get("line", 0) + 1
char = start.get("character", 0) + 1
return f"{path}:{line}:{char}"
class GoToDefinitionTool(BaseTool):
"""Jump to the definition of a symbol using a language server."""
def __init__(self, root: Optional[str] = None) -> None:
self.root = os.path.realpath(root or os.getcwd())
self._manager: Optional[LSPManager] = None
@property
def name(self) -> str:
return "go_to_definition"
@property
def description(self) -> str:
return (
"Find the definition of a symbol at a given position using a "
"language server. Returns the file, line, and column where the "
"symbol is defined. Much more accurate than searching for the name."
)
@property
def parameters(self) -> Dict[str, Any]:
return {
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "Path to the source file containing the symbol.",
},
"line": {
"type": "integer",
"description": "1-indexed line number of the symbol.",
},
"character": {
"type": "integer",
"description": "1-indexed character offset of the symbol.",
},
},
"required": ["path", "line", "character"],
}
def _get_manager(self) -> LSPManager:
if self._manager is None:
self._manager = LSPManager(self.root)
return self._manager
async def execute(self, path: str, line: int, character: int) -> str:
full_path = path if os.path.isabs(path) else os.path.join(self.root, path)
full_path = os.path.realpath(full_path)
if not full_path.startswith(self.root):
raise ToolError(f"Path escapes workspace directory: {path}")
if not os.path.isfile(full_path):
raise ToolError(f"File not found: {path}")
manager = self._get_manager()
client = await manager._get_or_start(full_path)
await asyncio.to_thread(client.did_open, full_path)
try:
result = await asyncio.to_thread(
client.request,
"textDocument/definition",
{
"textDocument": {"uri": "file://" + full_path},
"position": {"line": line - 1, "character": character - 1},
},
)
except ToolError:
raise
except Exception as exc:
raise ToolError(f"LSP definition failed: {exc}")
if not result:
return f"No definition found for symbol at {path}:{line}:{character}."
if isinstance(result, list):
if not result:
return f"No definition found for symbol at {path}:{line}:{character}."
locations = [_location_to_str(loc, self.root) for loc in result]
return "Definitions:\n" + "\n".join(locations)
if isinstance(result, dict) and "uri" in result:
return "Definition: " + _location_to_str(result, self.root)
return f"Definition (raw): {json.dumps(result)}"
class FindReferencesTool(BaseTool):
"""Find all references to a symbol using a language server."""
def __init__(self, root: Optional[str] = None) -> None:
self.root = os.path.realpath(root or os.getcwd())
self._manager: Optional[LSPManager] = None
@property
def name(self) -> str:
return "find_references"
@property
def description(self) -> str:
return (
"Find all references to a symbol at a given position using a "
"language server. Returns a list of locations where the symbol "
"is used. Useful before renaming or refactoring."
)
@property
def parameters(self) -> Dict[str, Any]:
return {
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "Path to the source file containing the symbol.",
},
"line": {
"type": "integer",
"description": "1-indexed line number of the symbol.",
},
"character": {
"type": "integer",
"description": "1-indexed character offset of the symbol.",
},
},
"required": ["path", "line", "character"],
}
def _get_manager(self) -> LSPManager:
if self._manager is None:
self._manager = LSPManager(self.root)
return self._manager
async def execute(self, path: str, line: int, character: int) -> str:
full_path = path if os.path.isabs(path) else os.path.join(self.root, path)
full_path = os.path.realpath(full_path)
if not full_path.startswith(self.root):
raise ToolError(f"Path escapes workspace directory: {path}")
if not os.path.isfile(full_path):
raise ToolError(f"File not found: {path}")
manager = self._get_manager()
client = await manager._get_or_start(full_path)
await asyncio.to_thread(client.did_open, full_path)
try:
result = await asyncio.to_thread(
client.request,
"textDocument/references",
{
"textDocument": {"uri": "file://" + full_path},
"position": {"line": line - 1, "character": character - 1},
"context": {"includeDeclaration": True},
},
)
except ToolError:
raise
except Exception as exc:
raise ToolError(f"LSP references failed: {exc}")
if not result:
return f"No references found for symbol at {path}:{line}:{character}."
locations = [_location_to_str(loc, self.root) for loc in result]
header = f"Found {len(locations)} reference(s) for symbol at {path}:{line}:{character}:"
return header + "\n" + "\n".join(locations)
class GetSymbolInfoTool(BaseTool):
"""Get the document symbols (outline) of a file using a language server."""
def __init__(self, root: Optional[str] = None) -> None:
self.root = os.path.realpath(root or os.getcwd())
self._manager: Optional[LSPManager] = None
@property
def name(self) -> str:
return "get_symbol_info"
@property
def description(self) -> str:
return (
"Get the list of symbols (functions, classes, variables, etc.) "
"in a file using a language server. Returns a structured outline "
"with names, kinds, and line numbers."
)
@property
def parameters(self) -> Dict[str, Any]:
return {
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "Path to the source file to inspect.",
},
},
"required": ["path"],
}
def _get_manager(self) -> LSPManager:
if self._manager is None:
self._manager = LSPManager(self.root)
return self._manager
async def execute(self, path: str) -> str:
full_path = path if os.path.isabs(path) else os.path.join(self.root, path)
full_path = os.path.realpath(full_path)
if not full_path.startswith(self.root):
raise ToolError(f"Path escapes workspace directory: {path}")
if not os.path.isfile(full_path):
raise ToolError(f"File not found: {path}")
manager = self._get_manager()
client = await manager._get_or_start(full_path)
await asyncio.to_thread(client.did_open, full_path)
try:
result = await asyncio.to_thread(
client.request,
"textDocument/documentSymbol",
{"textDocument": {"uri": "file://" + full_path}},
)
except ToolError:
raise
except Exception as exc:
raise ToolError(f"LSP documentSymbol failed: {exc}")
if not result:
return f"No symbols found in {path}."
# Handle both flat DocumentSymbol[] and flat SymbolInformation[]
kind_names = {
1: "File", 2: "Module", 3: "Namespace", 4: "Package", 5: "Class",
6: "Method", 7: "Property", 8: "Field", 9: "Constructor", 10: "Enum",
11: "Interface", 12: "Function", 13: "Variable", 14: "Constant",
15: "String", 16: "Number", 17: "Boolean", 18: "Array", 19: "Object",
20: "Key", 21: "Null", 22: "EnumMember", 23: "Struct", 24: "Event",
25: "Operator", 26: "TypeParameter",
}
lines = [f"Symbols in {path}:"]
if isinstance(result, list):
for sym in result:
if "location" in sym:
# DocumentSymbol (has children)
name = sym.get("name", "?")
kind = kind_names.get(sym.get("kind", 0), "Unknown")
loc = sym["location"]["range"]["start"]
lines.append(f" L{loc['line'] + 1:<5} [{kind:<12}] {name}")
for child in sym.get("children", []) or []:
cname = child.get("name", "?")
ckind = kind_names.get(child.get("kind", 0), "Unknown")
cloc = child["location"]["range"]["start"]
lines.append(f" L{cloc['line'] + 1:<5} [{ckind:<12}] {cname}")
else:
# SymbolInformation (flat)
name = sym.get("name", "?")
kind = kind_names.get(sym.get("kind", 0), "Unknown")
loc = sym.get("location", {}).get("range", {}).get("start", {})
lines.append(f" L{loc.get('line', 0) + 1:<5} [{kind:<12}] {name}")
return "\n".join(lines)
__all__ = [
"GoToDefinitionTool",
"FindReferencesTool",
"GetSymbolInfoTool",
"LSPManager",
]
|