-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaemon.py
More file actions
743 lines (605 loc) · 22.6 KB
/
Copy pathdaemon.py
File metadata and controls
743 lines (605 loc) · 22.6 KB
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
"""vcc daemon - background service for code indexing and search.
Simplified architecture using asyncio + Unix socket + JSON-RPC.
Replaces the complex msgspec + multiprocessing.Listener approach.
"""
from __future__ import annotations
import asyncio
import json
import logging
import os
import signal
import threading
import time
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any
from vectorless_code._runtime import daemon_pid_path, daemon_socket_path
from vectorless_code._version import __version__
from vectorless_code.settings import load_user_settings
# Set up logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)s %(name)s: %(message)s",
handlers=[logging.StreamHandler()],
)
logger = logging.getLogger(__name__)
# ---------------------------------------------------------------------------
# Project state
# ---------------------------------------------------------------------------
@dataclass
class ProjectState:
"""State for a tracked project."""
root: Path
doc_id: str | None = None
last_index_time: float = 0.0
indexing: bool = False
file_count: int = 0
total_lines: int = 0
total_bytes: int = 0
languages: dict[str, int] = field(default_factory=dict)
@property
def is_indexed(self) -> bool:
"""True if the project has been successfully indexed."""
return self.doc_id is not None
# ---------------------------------------------------------------------------
# Daemon
# ---------------------------------------------------------------------------
class Daemon:
"""vcc daemon service using asyncio + Unix socket.
Features:
- JSON-RPC 2.0 over Unix socket (human-readable)
- Project registry with doc_id caching
- File watching for auto-recompile
- Concurrent request handling
- Graceful shutdown
"""
def __init__(
self,
socket_path: str | None = None,
):
"""Initialize the daemon.
Args:
socket_path: Path to the Unix socket (default: from _runtime).
"""
self._socket_path = socket_path or daemon_socket_path()
# State
self._projects: dict[str, ProjectState] = {}
self._watchers: dict[str, FileWatcher] = {}
self._index_locks: dict[str, asyncio.Lock] = {}
self._reindex_tasks: dict[str, asyncio.Task[None]] = {}
# Server
self._server: asyncio.Server | None = None
self._start_time: float = 0.0
self._stop_requested = False
# Settings (lazy loaded)
self._user_settings: Any | None = None
@property
def is_running(self) -> bool:
"""True if the daemon server is running."""
return self._server is not None
@property
def uptime_seconds(self) -> float:
"""Daemon uptime in seconds."""
if self._start_time == 0.0:
return 0.0
return time.monotonic() - self._start_time
@property
def project_count(self) -> int:
"""Number of loaded projects."""
return len(self._projects)
def list_projects(self) -> list[dict[str, Any]]:
"""List all loaded projects with their status."""
return [
{
"project_root": root,
"indexed": state.is_indexed,
"indexing": state.indexing,
"file_count": state.file_count,
}
for root, state in self._projects.items()
]
async def _load_user_settings(self) -> Any:
"""Load user settings (cached)."""
if self._user_settings is None:
self._user_settings = load_user_settings()
return self._user_settings
# ------------------------------------------------------------------
# Server lifecycle
# ------------------------------------------------------------------
async def start(self) -> None:
"""Start the daemon server."""
if self._server is not None:
logger.warning("Daemon already running")
return
# Ensure socket directory exists
socket_path = Path(self._socket_path)
socket_path.parent.mkdir(parents=True, exist_ok=True)
# Remove stale socket if present
if socket_path.exists():
try:
socket_path.unlink()
except OSError:
pass
# Create Unix socket server
self._server = await asyncio.start_unix_server(
self._handle_client,
path=str(socket_path),
)
self._start_time = time.monotonic()
logger.info("Daemon listening on %s", self._socket_path)
# Set up signal handlers for graceful shutdown
self._setup_signals()
def _setup_signals(self) -> None:
"""Set up signal handlers for graceful shutdown."""
loop = asyncio.get_event_loop()
for sig in (signal.SIGTERM, signal.SIGINT):
try:
loop.add_signal_handler(sig, self._request_shutdown)
except (RuntimeError, NotImplementedError):
# Signals not supported on this platform
pass
def _request_shutdown(self) -> None:
"""Request a graceful shutdown."""
logger.info("Shutdown requested")
self._stop_requested = True
async def run_until_shutdown(self) -> None:
"""Run the daemon until shutdown is requested."""
if self._server is None:
raise RuntimeError("Daemon not started")
# Serve until stop is requested
while not self._stop_requested:
await asyncio.sleep(0.1)
await self.stop()
async def stop(self) -> None:
"""Stop the daemon and clean up resources."""
logger.info("Stopping daemon...")
# Stop all file watchers
for watcher in list(self._watchers.values()):
watcher.stop()
self._watchers.clear()
# Cancel reindex tasks
for task in list(self._reindex_tasks.values()):
task.cancel()
self._reindex_tasks.clear()
# Close server
if self._server is not None:
self._server.close()
await self._server.wait_closed()
self._server = None
# Remove socket file
try:
Path(self._socket_path).unlink(missing_ok=True)
except OSError:
pass
logger.info("Daemon stopped")
# ------------------------------------------------------------------
# Client connection handler
# ------------------------------------------------------------------
async def _handle_client(
self,
reader: asyncio.StreamReader,
writer: asyncio.StreamWriter,
) -> None:
"""Handle a client connection."""
addr = writer.get_extra_info("peername")
logger.debug("Client connected: %s", addr)
try:
# Read request line (JSON + newline)
line = await reader.readline()
if not line:
logger.debug("Client disconnected (empty read): %s", addr)
return
try:
request = json.loads(line.decode())
except (json.JSONDecodeError, ValueError) as e:
response = _error_response(
code=-32700,
message=f"Invalid JSON: {e}",
request_id=None,
)
else:
# Dispatch request
response = await self._dispatch(request)
# Write response line (JSON + newline)
writer.write(json.dumps(response).encode() + b"\n")
await writer.drain()
except Exception as e:
logger.exception("Error handling client %s: %s", addr, e)
finally:
writer.close()
await writer.wait_closed()
logger.debug("Client disconnected: %s", addr)
async def _dispatch(self, request: dict) -> dict:
"""Dispatch request to appropriate handler."""
method = request.get("method")
params = request.get("params", {})
req_id = request.get("id")
try:
if method == "compile":
result = await self._handle_compile(params)
elif method == "ask":
result = await self._handle_ask(params)
elif method == "status":
result = await self._handle_status(params)
elif method == "stop":
result = await self._handle_stop()
elif method == "daemon_status":
result = {
"version": str(__version__),
"uptime_seconds": self.uptime_seconds,
"projects": self.list_projects(),
}
elif method == "ping":
result = {"pong": True, "uptime": self.uptime_seconds}
else:
return _error_response(
code=-32601,
message=f"Unknown method: {method}",
request_id=req_id,
)
return _success_response(result, req_id)
except Exception as e:
logger.exception("Error handling %s request: %s", method, e)
return _error_response(
code=-32603,
message=str(e),
request_id=req_id,
)
# ------------------------------------------------------------------
# Request handlers
# ------------------------------------------------------------------
async def _handle_compile(self, params: dict) -> dict:
"""Handle compile request."""
from vectorless_code.compile import compile_project
project_root = params.get("project_root")
if not project_root:
raise ValueError("Missing project_root parameter")
# Get or create project state
if project_root not in self._projects:
self._projects[project_root] = ProjectState(root=Path(project_root))
project = self._projects[project_root]
# Get index lock (one index at a time per project)
lock = self._index_locks.setdefault(project_root, asyncio.Lock())
async with lock:
project.indexing = True
try:
# Compile project
result = await compile_project(
project_root=Path(project_root),
user_settings=await self._load_user_settings(),
)
if not result.ok:
raise RuntimeError(result.error or "Compilation failed")
# Update state
project.doc_id = result.doc_id
project.last_index_time = time.monotonic()
project.file_count = result.file_count
project.total_lines = result.total_lines
project.total_bytes = result.total_bytes
project.languages = result.languages
# Start file watcher if not running
if project_root not in self._watchers:
watcher = FileWatcher(
path=Path(project_root),
on_change=lambda: self._schedule_reindex(project_root),
)
watcher.start()
self._watchers[project_root] = watcher
return {
"success": True,
"doc_id": project.doc_id,
"file_count": project.file_count,
"total_lines": project.total_lines,
"total_bytes": project.total_bytes,
"languages": project.languages,
}
finally:
project.indexing = False
async def _handle_ask(self, params: dict) -> dict:
"""Handle ask request."""
from vectorless_code.ask import ask_codebase
project_root = params.get("project_root")
query = params.get("query")
limit = params.get("limit", 10)
offset = params.get("offset", 0)
if not project_root:
raise ValueError("Missing project_root parameter")
if not query:
raise ValueError("Missing query parameter")
# Check if project exists
if project_root not in self._projects:
raise ValueError(f"Project not loaded: {project_root}")
project = self._projects[project_root]
# Check if project is indexed
if not project.doc_id:
raise ValueError(f"Project not indexed: {project_root}. Call compile() first.")
# Wait for any ongoing indexing to complete
lock = self._index_locks.get(project_root)
if lock and lock.locked():
async with lock:
pass
# Search
output = await ask_codebase(
question=query,
doc_ids=[project.doc_id],
user_settings=await self._load_user_settings(),
)
results = []
for ev in output.evidence:
results.append(
{
"file_path": ev.source_path or "",
"node_title": ev.node_title,
"content": ev.content,
"doc_name": ev.doc_name,
}
)
# Apply pagination
paginated = results[offset : offset + limit]
return {
"success": True,
"results": paginated,
"total_returned": len(paginated),
"offset": offset,
"confidence": output.confidence,
}
async def _handle_status(self, params: dict) -> dict:
"""Handle status request."""
project_root = params.get("project_root")
if not project_root:
raise ValueError("Missing project_root parameter")
if project_root not in self._projects:
# Return empty status for unknown projects
return {
"indexed": False,
"indexing": False,
"file_count": 0,
"total_lines": 0,
"total_bytes": 0,
"languages": {},
"doc_id": None,
}
project = self._projects[project_root]
return {
"indexed": project.is_indexed,
"indexing": project.indexing,
"file_count": project.file_count,
"total_lines": project.total_lines,
"total_bytes": project.total_bytes,
"languages": project.languages,
"doc_id": project.doc_id,
}
async def _handle_stop(self) -> dict:
"""Handle stop request."""
self._request_shutdown()
return {"ok": True}
# ------------------------------------------------------------------
# Auto-reindex scheduling
# ------------------------------------------------------------------
def _schedule_reindex(self, project_root: str) -> None:
"""Schedule a reindex for the project (called from FileWatcher)."""
# Cancel existing task if any
if project_root in self._reindex_tasks:
self._reindex_tasks[project_root].cancel()
# Create new reindex task
async def _do_reindex() -> None:
# Wait for debounce period
await asyncio.sleep(1.0)
try:
logger.info("Auto-reindexing %s", project_root)
await self._handle_compile({"project_root": project_root})
logger.info("Auto-reindex complete for %s", project_root)
except Exception as e:
logger.error("Auto-reindex failed for %s: %s", project_root, e)
task = asyncio.create_task(_do_reindex())
self._reindex_tasks[project_root] = task
# ---------------------------------------------------------------------------
# File watcher (inline, since it's tightly coupled with daemon)
# ---------------------------------------------------------------------------
class FileWatcher:
"""Monitor a directory for file changes using watchdog.
Debounces rapid changes to avoid excessive recompilations.
"""
# Source file extensions to monitor
SOURCE_EXTENSIONS = {
# Python
".py",
".pyi",
# JavaScript/TypeScript
".js",
".jsx",
".ts",
".tsx",
".mjs",
".cjs",
# Rust
".rs",
# Go
".go",
# Java
".java",
# C/C++
".c",
".h",
".cpp",
".hpp",
".cc",
".cxx",
# Ruby
".rb",
# Kotlin
".kt",
".kts",
# Scala
".scala",
# Others
".php",
".sh",
".bash",
".lua",
".sql",
}
def __init__(
self,
path: Path,
on_change: callable,
debounce_secs: float = 0.5,
):
"""Initialize the file watcher.
Args:
path: Directory to monitor (will be watched recursively).
on_change: Callback to invoke when source files change.
debounce_secs: Seconds to wait after last change before triggering.
"""
self._path = path
self._on_change = on_change
self._debounce_secs = debounce_secs
self._observer = None
self._handler: _ChangeHandler | None = None
@property
def is_running(self) -> bool:
"""True if the watcher is currently running."""
return self._observer is not None
def start(self) -> None:
"""Start watching the directory."""
if self._observer is not None:
logger.warning("FileWatcher already running for %s", self._path)
return
try:
from watchdog.observers import Observer
except ImportError:
logger.error(
"watchdog not installed. File watching disabled. "
"Install with: pip install watchdog"
)
return
self._handler = _ChangeHandler(self._on_change, self._debounce_secs)
self._observer = Observer()
self._observer.schedule(
self._handler,
str(self._path),
recursive=True,
)
self._observer.start()
logger.info("FileWatcher started for %s", self._path)
def stop(self) -> None:
"""Stop watching and clean up resources."""
if self._observer is None:
return
if self._handler is not None:
self._handler.stop()
self._handler = None
self._observer.stop()
self._observer.join(timeout=5.0)
self._observer = None
logger.info("FileWatcher stopped for %s", self._path)
class _ChangeHandler:
"""Internal watchdog event handler with debouncing."""
def __init__(self, callback: callable, debounce_secs: float = 0.5):
self._callback = callback
self._debounce_secs = debounce_secs
self._timer: threading.Timer | None = None
self._lock = threading.Lock()
def on_modified(self, event) -> None:
"""Handle file modified event."""
if event.is_directory:
return
path = event.src_path
# Filter to source files only
if not any(path.endswith(ext) for ext in FileWatcher.SOURCE_EXTENSIONS):
return
# Skip hidden files and cache directories
if "/." in path or "\\." in path:
return
if "/node_modules/" in path or "\\node_modules\\" in path:
return
if "/.vectorless_code/" in path or "\\.vectorless_code\\" in path:
return
if "/__pycache__/" in path or "\\__pycache__\\" in path:
return
if "/.git/" in path or "\\.git\\" in path:
return
logger.debug("File changed: %s", path)
# Debounce: cancel existing timer and start new one
with self._lock:
if self._timer is not None:
self._timer.cancel()
self._timer = threading.Timer(self._debounce_secs, self._callback)
self._timer.start()
def stop(self) -> None:
"""Cancel any pending timer."""
with self._lock:
if self._timer is not None:
self._timer.cancel()
self._timer = None
# ---------------------------------------------------------------------------
# JSON-RPC helpers
# ---------------------------------------------------------------------------
def _success_response(result: Any, request_id: int | None) -> dict:
"""Create a JSON-RPC success response."""
response: dict[str, Any] = {
"jsonrpc": "2.0",
"result": result,
}
if request_id is not None:
response["id"] = request_id
return response
def _error_response(code: int, message: str, request_id: int | None) -> dict:
"""Create a JSON-RPC error response."""
response: dict[str, Any] = {
"jsonrpc": "2.0",
"error": {"code": code, "message": message},
}
if request_id is not None:
response["id"] = request_id
return response
# ---------------------------------------------------------------------------
# Daemon entry point
# ---------------------------------------------------------------------------
def _write_pid_file() -> None:
"""Write current PID to the pid file."""
pid_path = daemon_pid_path()
pid_path.parent.mkdir(parents=True, exist_ok=True)
pid_path.write_text(str(os.getpid()))
logger.debug("Wrote PID file: %s", pid_path)
def _remove_pid_file() -> None:
"""Remove the PID file."""
pid_path = daemon_pid_path()
try:
stored = pid_path.read_text().strip()
if stored == str(os.getpid()):
pid_path.unlink(missing_ok=True)
logger.debug("Removed PID file: %s", pid_path)
except (FileNotFoundError, ValueError, OSError):
pass
def run_daemon() -> None:
"""Main entry point for running the daemon (blocking)."""
logger.info("vectorless-code daemon v%s starting (PID %d)", __version__, os.getpid())
# Write PID file
_write_pid_file()
# Create and run daemon
daemon = Daemon()
async def _run() -> None:
try:
await daemon.start()
await daemon.run_until_shutdown()
except KeyboardInterrupt:
logger.info("Interrupted by user")
except Exception as e:
logger.exception("Daemon error: %s", e)
raise
finally:
await daemon.stop()
_remove_pid_file()
try:
asyncio.run(_run())
finally:
# Ensure PID file is cleaned up
_remove_pid_file()
logger.info("Daemon exited")
# Use os._exit to ensure all threads/child processes are terminated
if threading.current_thread() is threading.main_thread():
os._exit(0)
# For direct execution
if __name__ == "__main__":
run_daemon()