|
17 | 17 | from pathlib import Path |
18 | 18 |
|
19 | 19 |
|
| 20 | +def _autopilot_socket_basename() -> str: |
| 21 | + """File name (with ``.sock``) under ``$XDG_RUNTIME_DIR`` or ``/tmp``.""" |
| 22 | + instance = os.environ.get("KORU_AUTOPILOT_INSTANCE", "").strip() |
| 23 | + if not instance: |
| 24 | + return "koru-autopilot.sock" |
| 25 | + slug_chars: list[str] = [] |
| 26 | + for ch in instance[:64]: |
| 27 | + if ch.isalnum() or ch in "-_": |
| 28 | + slug_chars.append(ch) |
| 29 | + else: |
| 30 | + slug_chars.append("-") |
| 31 | + slug = "".join(slug_chars).strip("-") or "instance" |
| 32 | + return f"koru-autopilot-{slug}.sock" |
| 33 | + |
| 34 | + |
20 | 35 | def default_socket_path() -> Path: |
21 | 36 | """Return the canonical unix-socket location for the autopilot daemon. |
22 | 37 |
|
23 | | - Uses ``$XDG_RUNTIME_DIR/koru-autopilot.sock`` when available |
24 | | - (it is per-user and auto-cleaned by systemd-logind); otherwise |
25 | | - falls back to ``/tmp/koru-autopilot-<uid>.sock``. |
| 38 | + Resolution order: |
| 39 | +
|
| 40 | + 1. ``KORU_AUTOPILOT_SOCKET`` — absolute path (each IDE/plugin should set a |
| 41 | + distinct path when several instances share one login session). |
| 42 | + 2. Otherwise ``$XDG_RUNTIME_DIR/<name>`` where ``<name>`` is |
| 43 | + ``koru-autopilot.sock`` (legacy) or ``koru-autopilot-<slug>.sock`` when |
| 44 | + ``KORU_AUTOPILOT_INSTANCE`` is set (e.g. ``cursor-main``, ``windsurf-2``). |
| 45 | + 3. Fallback under ``/tmp`` — distinct file per uid; instance sockets |
| 46 | + include the slug in the file name. |
26 | 47 | """ |
| 48 | + explicit = os.environ.get("KORU_AUTOPILOT_SOCKET", "").strip() |
| 49 | + if explicit: |
| 50 | + return Path(explicit).expanduser().resolve() |
| 51 | + |
| 52 | + name = _autopilot_socket_basename() |
27 | 53 | runtime = os.environ.get("XDG_RUNTIME_DIR") |
28 | 54 | if runtime: |
29 | | - path = Path(runtime) / "koru-autopilot.sock" |
| 55 | + path = Path(runtime) / name |
30 | 56 | try: |
31 | 57 | path.parent.mkdir(parents=True, exist_ok=True) |
32 | 58 | except OSError: |
33 | 59 | pass |
34 | 60 | return path |
35 | | - return Path(f"/tmp/koru-autopilot-{os.getuid()}.sock") |
| 61 | + if name == "koru-autopilot.sock": |
| 62 | + return Path(f"/tmp/koru-autopilot-{os.getuid()}.sock") |
| 63 | + stem = name.removesuffix(".sock") |
| 64 | + return Path(f"/tmp/{stem}-{os.getuid()}.sock") |
36 | 65 |
|
37 | 66 |
|
38 | 67 | __all__ = ["default_socket_path"] |
0 commit comments