@colbymchenry/codegraph-darwin-x64 0.9.3 → 0.9.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (208) hide show
  1. package/lib/dist/bin/codegraph.d.ts +3 -0
  2. package/lib/dist/bin/codegraph.d.ts.map +1 -1
  3. package/lib/dist/bin/codegraph.js +250 -0
  4. package/lib/dist/bin/codegraph.js.map +1 -1
  5. package/lib/dist/context/index.d.ts +13 -0
  6. package/lib/dist/context/index.d.ts.map +1 -1
  7. package/lib/dist/context/index.js +120 -1
  8. package/lib/dist/context/index.js.map +1 -1
  9. package/lib/dist/db/index.d.ts +18 -0
  10. package/lib/dist/db/index.d.ts.map +1 -1
  11. package/lib/dist/db/index.js +31 -0
  12. package/lib/dist/db/index.js.map +1 -1
  13. package/lib/dist/db/queries.d.ts +16 -0
  14. package/lib/dist/db/queries.d.ts.map +1 -1
  15. package/lib/dist/db/queries.js +80 -27
  16. package/lib/dist/db/queries.js.map +1 -1
  17. package/lib/dist/extraction/grammars.d.ts +6 -0
  18. package/lib/dist/extraction/grammars.d.ts.map +1 -1
  19. package/lib/dist/extraction/grammars.js +31 -1
  20. package/lib/dist/extraction/grammars.js.map +1 -1
  21. package/lib/dist/extraction/index.d.ts +15 -2
  22. package/lib/dist/extraction/index.d.ts.map +1 -1
  23. package/lib/dist/extraction/index.js +170 -78
  24. package/lib/dist/extraction/index.js.map +1 -1
  25. package/lib/dist/extraction/languages/index.d.ts.map +1 -1
  26. package/lib/dist/extraction/languages/index.js +2 -0
  27. package/lib/dist/extraction/languages/index.js.map +1 -1
  28. package/lib/dist/extraction/languages/objc.d.ts +3 -0
  29. package/lib/dist/extraction/languages/objc.d.ts.map +1 -0
  30. package/lib/dist/extraction/languages/objc.js +133 -0
  31. package/lib/dist/extraction/languages/objc.js.map +1 -0
  32. package/lib/dist/extraction/tree-sitter-types.d.ts +4 -0
  33. package/lib/dist/extraction/tree-sitter-types.d.ts.map +1 -1
  34. package/lib/dist/extraction/tree-sitter.d.ts.map +1 -1
  35. package/lib/dist/extraction/tree-sitter.js +155 -9
  36. package/lib/dist/extraction/tree-sitter.js.map +1 -1
  37. package/lib/dist/extraction/wasm-runtime-flags.d.ts +12 -0
  38. package/lib/dist/extraction/wasm-runtime-flags.d.ts.map +1 -1
  39. package/lib/dist/extraction/wasm-runtime-flags.js +14 -2
  40. package/lib/dist/extraction/wasm-runtime-flags.js.map +1 -1
  41. package/lib/dist/graph/traversal.d.ts.map +1 -1
  42. package/lib/dist/graph/traversal.js +71 -36
  43. package/lib/dist/graph/traversal.js.map +1 -1
  44. package/lib/dist/index.d.ts +21 -2
  45. package/lib/dist/index.d.ts.map +1 -1
  46. package/lib/dist/index.js +42 -0
  47. package/lib/dist/index.js.map +1 -1
  48. package/lib/dist/installer/instructions-template.d.ts +2 -2
  49. package/lib/dist/installer/instructions-template.d.ts.map +1 -1
  50. package/lib/dist/installer/instructions-template.js +3 -2
  51. package/lib/dist/installer/instructions-template.js.map +1 -1
  52. package/lib/dist/mcp/daemon-paths.d.ts +46 -0
  53. package/lib/dist/mcp/daemon-paths.d.ts.map +1 -0
  54. package/lib/dist/mcp/daemon-paths.js +125 -0
  55. package/lib/dist/mcp/daemon-paths.js.map +1 -0
  56. package/lib/dist/mcp/daemon.d.ts +161 -0
  57. package/lib/dist/mcp/daemon.d.ts.map +1 -0
  58. package/lib/dist/mcp/daemon.js +403 -0
  59. package/lib/dist/mcp/daemon.js.map +1 -0
  60. package/lib/dist/mcp/engine.d.ts +100 -0
  61. package/lib/dist/mcp/engine.d.ts.map +1 -0
  62. package/lib/dist/mcp/engine.js +291 -0
  63. package/lib/dist/mcp/engine.js.map +1 -0
  64. package/lib/dist/mcp/index.d.ts +67 -52
  65. package/lib/dist/mcp/index.d.ts.map +1 -1
  66. package/lib/dist/mcp/index.js +347 -330
  67. package/lib/dist/mcp/index.js.map +1 -1
  68. package/lib/dist/mcp/proxy.d.ts +46 -0
  69. package/lib/dist/mcp/proxy.d.ts.map +1 -0
  70. package/lib/dist/mcp/proxy.js +276 -0
  71. package/lib/dist/mcp/proxy.js.map +1 -0
  72. package/lib/dist/mcp/server-instructions.d.ts +1 -1
  73. package/lib/dist/mcp/server-instructions.d.ts.map +1 -1
  74. package/lib/dist/mcp/server-instructions.js +3 -1
  75. package/lib/dist/mcp/server-instructions.js.map +1 -1
  76. package/lib/dist/mcp/session.d.ts +67 -0
  77. package/lib/dist/mcp/session.d.ts.map +1 -0
  78. package/lib/dist/mcp/session.js +276 -0
  79. package/lib/dist/mcp/session.js.map +1 -0
  80. package/lib/dist/mcp/tools.d.ts +130 -2
  81. package/lib/dist/mcp/tools.d.ts.map +1 -1
  82. package/lib/dist/mcp/tools.js +902 -37
  83. package/lib/dist/mcp/tools.js.map +1 -1
  84. package/lib/dist/mcp/transport.d.ts +111 -29
  85. package/lib/dist/mcp/transport.d.ts.map +1 -1
  86. package/lib/dist/mcp/transport.js +181 -71
  87. package/lib/dist/mcp/transport.js.map +1 -1
  88. package/lib/dist/mcp/version.d.ts +19 -0
  89. package/lib/dist/mcp/version.d.ts.map +1 -0
  90. package/lib/dist/mcp/version.js +71 -0
  91. package/lib/dist/mcp/version.js.map +1 -0
  92. package/lib/dist/resolution/callback-synthesizer.d.ts +10 -0
  93. package/lib/dist/resolution/callback-synthesizer.d.ts.map +1 -0
  94. package/lib/dist/resolution/callback-synthesizer.js +847 -0
  95. package/lib/dist/resolution/callback-synthesizer.js.map +1 -0
  96. package/lib/dist/resolution/frameworks/csharp.d.ts.map +1 -1
  97. package/lib/dist/resolution/frameworks/csharp.js +36 -8
  98. package/lib/dist/resolution/frameworks/csharp.js.map +1 -1
  99. package/lib/dist/resolution/frameworks/drupal.d.ts.map +1 -1
  100. package/lib/dist/resolution/frameworks/drupal.js +44 -12
  101. package/lib/dist/resolution/frameworks/drupal.js.map +1 -1
  102. package/lib/dist/resolution/frameworks/expo-modules.d.ts +3 -0
  103. package/lib/dist/resolution/frameworks/expo-modules.d.ts.map +1 -0
  104. package/lib/dist/resolution/frameworks/expo-modules.js +143 -0
  105. package/lib/dist/resolution/frameworks/expo-modules.js.map +1 -0
  106. package/lib/dist/resolution/frameworks/express.d.ts.map +1 -1
  107. package/lib/dist/resolution/frameworks/express.js +102 -19
  108. package/lib/dist/resolution/frameworks/express.js.map +1 -1
  109. package/lib/dist/resolution/frameworks/fabric.d.ts +3 -0
  110. package/lib/dist/resolution/frameworks/fabric.d.ts.map +1 -0
  111. package/lib/dist/resolution/frameworks/fabric.js +354 -0
  112. package/lib/dist/resolution/frameworks/fabric.js.map +1 -0
  113. package/lib/dist/resolution/frameworks/go.d.ts.map +1 -1
  114. package/lib/dist/resolution/frameworks/go.js +6 -3
  115. package/lib/dist/resolution/frameworks/go.js.map +1 -1
  116. package/lib/dist/resolution/frameworks/index.d.ts +5 -0
  117. package/lib/dist/resolution/frameworks/index.d.ts.map +1 -1
  118. package/lib/dist/resolution/frameworks/index.js +25 -1
  119. package/lib/dist/resolution/frameworks/index.js.map +1 -1
  120. package/lib/dist/resolution/frameworks/java.d.ts.map +1 -1
  121. package/lib/dist/resolution/frameworks/java.js +70 -12
  122. package/lib/dist/resolution/frameworks/java.js.map +1 -1
  123. package/lib/dist/resolution/frameworks/laravel.d.ts.map +1 -1
  124. package/lib/dist/resolution/frameworks/laravel.js +17 -8
  125. package/lib/dist/resolution/frameworks/laravel.js.map +1 -1
  126. package/lib/dist/resolution/frameworks/play.d.ts +19 -0
  127. package/lib/dist/resolution/frameworks/play.d.ts.map +1 -0
  128. package/lib/dist/resolution/frameworks/play.js +111 -0
  129. package/lib/dist/resolution/frameworks/play.js.map +1 -0
  130. package/lib/dist/resolution/frameworks/python.d.ts.map +1 -1
  131. package/lib/dist/resolution/frameworks/python.js +134 -16
  132. package/lib/dist/resolution/frameworks/python.js.map +1 -1
  133. package/lib/dist/resolution/frameworks/react-native.d.ts +3 -0
  134. package/lib/dist/resolution/frameworks/react-native.d.ts.map +1 -0
  135. package/lib/dist/resolution/frameworks/react-native.js +360 -0
  136. package/lib/dist/resolution/frameworks/react-native.js.map +1 -0
  137. package/lib/dist/resolution/frameworks/react.d.ts.map +1 -1
  138. package/lib/dist/resolution/frameworks/react.js +96 -3
  139. package/lib/dist/resolution/frameworks/react.js.map +1 -1
  140. package/lib/dist/resolution/frameworks/ruby.d.ts.map +1 -1
  141. package/lib/dist/resolution/frameworks/ruby.js +106 -2
  142. package/lib/dist/resolution/frameworks/ruby.js.map +1 -1
  143. package/lib/dist/resolution/frameworks/rust.d.ts.map +1 -1
  144. package/lib/dist/resolution/frameworks/rust.js +102 -5
  145. package/lib/dist/resolution/frameworks/rust.js.map +1 -1
  146. package/lib/dist/resolution/frameworks/swift-objc.d.ts +37 -0
  147. package/lib/dist/resolution/frameworks/swift-objc.d.ts.map +1 -0
  148. package/lib/dist/resolution/frameworks/swift-objc.js +252 -0
  149. package/lib/dist/resolution/frameworks/swift-objc.js.map +1 -0
  150. package/lib/dist/resolution/frameworks/swift.d.ts.map +1 -1
  151. package/lib/dist/resolution/frameworks/swift.js +30 -6
  152. package/lib/dist/resolution/frameworks/swift.js.map +1 -1
  153. package/lib/dist/resolution/import-resolver.d.ts.map +1 -1
  154. package/lib/dist/resolution/import-resolver.js +1 -0
  155. package/lib/dist/resolution/import-resolver.js.map +1 -1
  156. package/lib/dist/resolution/index.d.ts.map +1 -1
  157. package/lib/dist/resolution/index.js +61 -9
  158. package/lib/dist/resolution/index.js.map +1 -1
  159. package/lib/dist/resolution/lru-cache.d.ts +24 -0
  160. package/lib/dist/resolution/lru-cache.d.ts.map +1 -0
  161. package/lib/dist/resolution/lru-cache.js +62 -0
  162. package/lib/dist/resolution/lru-cache.js.map +1 -0
  163. package/lib/dist/resolution/swift-objc-bridge.d.ts +134 -0
  164. package/lib/dist/resolution/swift-objc-bridge.d.ts.map +1 -0
  165. package/lib/dist/resolution/swift-objc-bridge.js +256 -0
  166. package/lib/dist/resolution/swift-objc-bridge.js.map +1 -0
  167. package/lib/dist/resolution/types.d.ts +8 -0
  168. package/lib/dist/resolution/types.d.ts.map +1 -1
  169. package/lib/dist/sync/index.d.ts +3 -1
  170. package/lib/dist/sync/index.d.ts.map +1 -1
  171. package/lib/dist/sync/index.js +7 -1
  172. package/lib/dist/sync/index.js.map +1 -1
  173. package/lib/dist/sync/watcher.d.ts +109 -7
  174. package/lib/dist/sync/watcher.d.ts.map +1 -1
  175. package/lib/dist/sync/watcher.js +215 -33
  176. package/lib/dist/sync/watcher.js.map +1 -1
  177. package/lib/dist/sync/worktree.d.ts +54 -0
  178. package/lib/dist/sync/worktree.d.ts.map +1 -0
  179. package/lib/dist/sync/worktree.js +136 -0
  180. package/lib/dist/sync/worktree.js.map +1 -0
  181. package/lib/dist/types.d.ts +1 -1
  182. package/lib/dist/types.d.ts.map +1 -1
  183. package/lib/dist/types.js +1 -0
  184. package/lib/dist/types.js.map +1 -1
  185. package/lib/dist/utils.js +1 -1
  186. package/lib/node_modules/.package-lock.json +29 -1
  187. package/lib/node_modules/chokidar/LICENSE +21 -0
  188. package/lib/node_modules/chokidar/README.md +305 -0
  189. package/lib/node_modules/chokidar/esm/handler.d.ts +90 -0
  190. package/lib/node_modules/chokidar/esm/handler.js +629 -0
  191. package/lib/node_modules/chokidar/esm/index.d.ts +215 -0
  192. package/lib/node_modules/chokidar/esm/index.js +798 -0
  193. package/lib/node_modules/chokidar/esm/package.json +1 -0
  194. package/lib/node_modules/chokidar/handler.d.ts +90 -0
  195. package/lib/node_modules/chokidar/handler.js +635 -0
  196. package/lib/node_modules/chokidar/index.d.ts +215 -0
  197. package/lib/node_modules/chokidar/index.js +804 -0
  198. package/lib/node_modules/chokidar/package.json +69 -0
  199. package/lib/node_modules/readdirp/LICENSE +21 -0
  200. package/lib/node_modules/readdirp/README.md +120 -0
  201. package/lib/node_modules/readdirp/esm/index.d.ts +108 -0
  202. package/lib/node_modules/readdirp/esm/index.js +257 -0
  203. package/lib/node_modules/readdirp/esm/package.json +1 -0
  204. package/lib/node_modules/readdirp/index.d.ts +108 -0
  205. package/lib/node_modules/readdirp/index.js +263 -0
  206. package/lib/node_modules/readdirp/package.json +70 -0
  207. package/lib/package.json +2 -1
  208. package/package.json +1 -1
@@ -14,6 +14,25 @@
14
14
  * const server = new MCPServer('/path/to/project');
15
15
  * await server.start();
16
16
  * ```
17
+ *
18
+ * Runtime modes (decided in {@link MCPServer.start}):
19
+ *
20
+ * - **Direct** — one process serves one MCP client over stdio. The pre-#411
21
+ * behavior; used when the user opts out (`CODEGRAPH_NO_DAEMON=1`), no
22
+ * `.codegraph/` is reachable, or the daemon machinery fails for any reason.
23
+ * - **Proxy** — what an MCP host actually talks to when sharing is on: a thin
24
+ * stdio↔socket pipe to the shared daemon. The proxy carries the #277 PPID
25
+ * watchdog, so a SIGKILL'd host reaps its proxy promptly. See {@link ./proxy.ts}.
26
+ * - **Daemon** — a *detached* background process (its own session/process
27
+ * group) that serves N proxies over a Unix-domain socket / named pipe,
28
+ * sharing one CodeGraph + watcher + SQLite handle. Spawned on demand; never a
29
+ * child of any host, so it survives individual sessions and is reaped by
30
+ * client-refcount + idle timeout. See {@link ./daemon.ts} and issue #411.
31
+ *
32
+ * The detached-daemon + always-proxy split is the fix for the review finding
33
+ * that the original in-process daemon (a) was the first host's child, so closing
34
+ * that terminal severed every other client, and (b) disabled the PPID watchdog,
35
+ * regressing #277 (orphaned daemons on host SIGKILL).
17
36
  */
18
37
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
19
38
  if (k2 === undefined) k2 = k;
@@ -49,405 +68,403 @@ var __importStar = (this && this.__importStar) || (function () {
49
68
  };
50
69
  })();
51
70
  Object.defineProperty(exports, "__esModule", { value: true });
52
- exports.ToolHandler = exports.tools = exports.StdioTransport = exports.MCPServer = void 0;
71
+ exports.CodeGraphPackageVersion = exports.Daemon = exports.ToolHandler = exports.tools = exports.StdioTransport = exports.MCPServer = void 0;
72
+ const fs = __importStar(require("fs"));
53
73
  const path = __importStar(require("path"));
54
- const index_1 = __importStar(require("../index"));
55
- const sync_1 = require("../sync");
74
+ const child_process_1 = require("child_process");
75
+ const index_1 = require("../index");
76
+ const directory_1 = require("../directory");
56
77
  const transport_1 = require("./transport");
57
- const tools_1 = require("./tools");
58
- const server_instructions_1 = require("./server-instructions");
78
+ const engine_1 = require("./engine");
79
+ const session_1 = require("./session");
80
+ const daemon_1 = require("./daemon");
81
+ const proxy_1 = require("./proxy");
82
+ const daemon_paths_1 = require("./daemon-paths");
83
+ const wasm_runtime_flags_1 = require("../extraction/wasm-runtime-flags");
59
84
  /**
60
- * Convert a file:// URI to a filesystem path.
61
- * Handles URL encoding and Windows drive letter paths.
85
+ * How often to poll `process.ppid` to detect parent process death (see #277).
86
+ * 5s is a deliberate trade-off: the failure mode being guarded against is rare
87
+ * (parent SIGKILL'd), and longer poll = less wakeup overhead while idle.
62
88
  */
63
- function fileUriToPath(uri) {
64
- try {
65
- const url = new URL(uri);
66
- let filePath = decodeURIComponent(url.pathname);
67
- // On Windows, file:///C:/path produces pathname /C:/path strip leading /
68
- if (process.platform === 'win32' && /^\/[a-zA-Z]:/.test(filePath)) {
69
- filePath = filePath.slice(1);
70
- }
71
- return path.resolve(filePath);
72
- }
73
- catch {
74
- // Fallback for non-standard URIs
75
- return uri.replace(/^file:\/\/\/?/, '');
76
- }
77
- }
89
+ const DEFAULT_PPID_POLL_MS = 5000;
90
+ /**
91
+ * Env var that marks a process as the *detached daemon* itself (set by
92
+ * {@link spawnDetachedDaemon} when it re-invokes the CLI). Without it a
93
+ * `serve --mcp` invocation is a launcher that connects-or-spawns; with it, the
94
+ * process IS the daemon and must never try to spawn another (infinite spawn).
95
+ */
96
+ const DAEMON_INTERNAL_ENV = 'CODEGRAPH_DAEMON_INTERNAL';
78
97
  /**
79
- * MCP Server Info
98
+ * Retries for the detached daemon arbitrating the O_EXCL lock against a racing
99
+ * sibling. Tiny — the lock resolves on the first round in practice; the retries
100
+ * only cover clearing a genuinely stale (dead-pid) lockfile.
80
101
  */
81
- const SERVER_INFO = {
82
- name: 'codegraph',
83
- version: '0.1.0',
84
- };
102
+ const TAKEOVER_MAX_RETRIES = 5;
103
+ const TAKEOVER_RETRY_DELAY_MS = 100;
85
104
  /**
86
- * MCP Protocol Version
105
+ * How long a launcher waits for a freshly-spawned daemon to bind its socket
106
+ * before giving up and running in-process. The daemon binds the socket *before*
107
+ * the (backgrounded) engine/grammar warm-up, so this only needs to cover node
108
+ * process startup. 60 × 100ms = 6s of headroom for a cold/slow box; on the
109
+ * common path the socket appears within a few rounds.
87
110
  */
88
- const PROTOCOL_VERSION = '2024-11-05';
111
+ const DAEMON_CONNECT_MAX_RETRIES = 60;
112
+ const DAEMON_CONNECT_RETRY_DELAY_MS = 100;
89
113
  /**
90
- * How long to wait for the client's `roots/list` response before giving up
91
- * and falling back to the process cwd.
114
+ * Resolve the PPID watchdog poll interval from an env override. A value of
115
+ * `0` disables the watchdog entirely (escape hatch for embedded scenarios
116
+ * where the parent legitimately re-parents the server on purpose). Anything
117
+ * non-numeric or negative falls back to the default.
92
118
  */
93
- const ROOTS_LIST_TIMEOUT_MS = 5000;
119
+ function parsePpidPollMs(raw) {
120
+ if (raw === undefined || raw === '')
121
+ return DEFAULT_PPID_POLL_MS;
122
+ const parsed = Number(raw);
123
+ if (!Number.isFinite(parsed))
124
+ return DEFAULT_PPID_POLL_MS;
125
+ if (parsed < 0)
126
+ return DEFAULT_PPID_POLL_MS;
127
+ return Math.floor(parsed);
128
+ }
94
129
  /**
95
- * Extract the first usable filesystem path from a `roots/list` result.
96
- * Shape per MCP spec: `{ roots: [{ uri: "file:///path", name?: string }] }`.
97
- * Returns null if the result is empty or malformed.
130
+ * Parse the host PID propagated across the `--liftoff-only` re-exec
131
+ * ({@link HOST_PPID_ENV}). Returns a positive integer PID, or null when
132
+ * unset/invalid the direct-launch path, where the watchdog falls back to
133
+ * `process.ppid` divergence. PIDs of 0/1 are rejected (0 = unknown, 1 = init,
134
+ * i.e. already orphaned), so the watchdog doesn't latch onto init.
98
135
  */
99
- function firstRootPath(result) {
100
- if (!result || typeof result !== 'object')
136
+ function parseHostPpid(raw) {
137
+ if (raw === undefined || raw === '')
101
138
  return null;
102
- const roots = result.roots;
103
- if (!Array.isArray(roots) || roots.length === 0)
139
+ const parsed = Number(raw);
140
+ if (!Number.isInteger(parsed) || parsed <= 1)
104
141
  return null;
105
- const first = roots[0];
106
- if (typeof first?.uri !== 'string')
142
+ return parsed;
143
+ }
144
+ /** Whether `CODEGRAPH_NO_DAEMON` was set to a truthy value. */
145
+ function daemonOptOutSet() {
146
+ const raw = process.env.CODEGRAPH_NO_DAEMON;
147
+ if (!raw)
148
+ return false;
149
+ return raw !== '0' && raw.toLowerCase() !== 'false';
150
+ }
151
+ /** Whether this process was spawned to BE the detached daemon. */
152
+ function daemonInternalSet() {
153
+ const raw = process.env[DAEMON_INTERNAL_ENV];
154
+ return !!raw && raw !== '0' && raw.toLowerCase() !== 'false';
155
+ }
156
+ /**
157
+ * Resolve the project root the daemon machinery should key on. Returns
158
+ * `null` when no `.codegraph/` is reachable from the candidate path — in
159
+ * that case the caller must run in direct mode, since the daemon lockfile
160
+ * and socket both live under `.codegraph/`.
161
+ *
162
+ * The result is canonicalized with `realpathSync` so every client converges on
163
+ * the same socket/lock path regardless of how it expressed the path: a client
164
+ * launched with cwd under a symlink (e.g. macOS `/var` → `/private/var`, where
165
+ * spawned `process.cwd()` is already realpath'd) and one that passed a
166
+ * symlinked `rootUri` would otherwise hash to different sockets and silently
167
+ * fail to share the daemon.
168
+ */
169
+ function resolveDaemonRoot(explicitPath) {
170
+ const candidate = explicitPath ?? process.cwd();
171
+ const root = (0, index_1.findNearestCodeGraphRoot)(candidate);
172
+ if (!root)
107
173
  return null;
108
- return fileUriToPath(first.uri);
174
+ try {
175
+ return fs.realpathSync(root);
176
+ }
177
+ catch {
178
+ return root;
179
+ }
180
+ }
181
+ /**
182
+ * Spawn the shared daemon as a fully detached background process: its own
183
+ * session/process group (so a SIGHUP/SIGINT to the launcher's terminal can't
184
+ * reach it) with stdio decoupled from the launcher (logs to
185
+ * `.codegraph/daemon.log`). Re-invokes the *same* CLI faithfully across dev and
186
+ * bundled launches by reusing `process.argv[0]` (the right node), the current
187
+ * `process.execArgv` (carries `--liftoff-only`, so the daemon never re-execs)
188
+ * and `process.argv[1]` (this script). The spawned process self-arbitrates the
189
+ * O_EXCL lock, so racing launchers may each spawn one — losers exit and every
190
+ * launcher proxies through the single winner.
191
+ */
192
+ function spawnDetachedDaemon(root) {
193
+ const scriptPath = process.argv[1];
194
+ if (!scriptPath) {
195
+ // No resolvable CLI entry point to re-invoke — let the caller fall back to
196
+ // direct mode rather than spawn something broken.
197
+ throw new Error('cannot resolve CLI script path to spawn the daemon');
198
+ }
199
+ let logFd = null;
200
+ let stdio = 'ignore';
201
+ try {
202
+ logFd = fs.openSync(path.join((0, directory_1.getCodeGraphDir)(root), 'daemon.log'), 'a');
203
+ stdio = ['ignore', logFd, logFd];
204
+ }
205
+ catch {
206
+ stdio = 'ignore'; // no log file — discard daemon output rather than fail
207
+ }
208
+ try {
209
+ const child = (0, child_process_1.spawn)(process.execPath, [...process.execArgv, scriptPath, 'serve', '--mcp', '--path', root], {
210
+ detached: true,
211
+ stdio,
212
+ windowsHide: true,
213
+ env: { ...process.env, [DAEMON_INTERNAL_ENV]: '1' },
214
+ });
215
+ child.unref();
216
+ }
217
+ finally {
218
+ // The child holds its own dup of the log fd now; the launcher doesn't need it.
219
+ if (logFd !== null) {
220
+ try {
221
+ fs.closeSync(logFd);
222
+ }
223
+ catch { /* ignore */ }
224
+ }
225
+ }
109
226
  }
110
227
  /**
111
228
  * MCP Server for CodeGraph
112
229
  *
113
230
  * Implements the Model Context Protocol to expose CodeGraph
114
231
  * functionality as tools that can be called by AI assistants.
232
+ *
233
+ * Backwards-compatible constructor and `start()` signature with the
234
+ * pre-issue-#411 implementation: callers continue to do
235
+ * `new MCPServer(path).start()`. Internally we now pick from direct / proxy /
236
+ * daemon at start time.
115
237
  */
116
238
  class MCPServer {
117
- transport;
118
- cg = null;
119
- toolHandler;
120
239
  projectPath;
121
- // In-flight background init kicked off from handleInitialize. Tracked so the
122
- // sync retry path doesn't race against it (double-opening the SQLite file).
123
- initPromise = null;
124
- // Whether the client advertised the MCP `roots` capability during initialize.
125
- // If so, and no explicit project path was given, we ask it for the workspace
126
- // root via roots/list rather than guessing from the (often wrong) cwd.
127
- clientSupportsRoots = false;
128
- // Guards the one-shot deferred resolution (roots/list or cwd) so we don't
129
- // re-issue roots/list on every tool call.
130
- rootsAttempted = false;
240
+ // Direct-mode-only state. In daemon mode the per-connection sessions live
241
+ // inside the Daemon class; in proxy mode there is no session at all.
242
+ session = null;
243
+ engine = null;
244
+ daemon = null;
245
+ ppidWatchdog = null;
246
+ // PPID watchdog baseline — captured at construction so we always have a
247
+ // baseline, even if start() runs after a fork-style reparent.
248
+ originalPpid = process.ppid;
249
+ hostPpid = parseHostPpid(process.env[wasm_runtime_flags_1.HOST_PPID_ENV]);
250
+ // Idempotency guard for stop().
251
+ stopped = false;
252
+ mode = 'unstarted';
131
253
  constructor(projectPath) {
132
254
  this.projectPath = projectPath || null;
133
- this.transport = new transport_1.StdioTransport();
134
- // Create ToolHandler eagerly — cross-project queries work even without a default project
135
- this.toolHandler = new tools_1.ToolHandler(null);
136
255
  }
137
256
  /**
138
- * Start the MCP server
257
+ * Start the MCP server.
139
258
  *
140
- * Note: CodeGraph initialization is deferred until the initialize request
141
- * is received, which includes the rootUri from the client.
142
- */
143
- async start() {
144
- // Start listening for messages immediately - don't check initialization yet
145
- // We'll get the project path from the initialize request's rootUri
146
- this.transport.start(this.handleMessage.bind(this));
147
- // Keep the process running
148
- process.on('SIGINT', () => this.stop());
149
- process.on('SIGTERM', () => this.stop());
150
- // When the parent process (Claude Code) exits, stdin closes.
151
- // Detect this and shut down gracefully to prevent orphaned processes.
152
- process.stdin.on('end', () => this.stop());
153
- process.stdin.on('close', () => this.stop());
154
- }
155
- /**
156
- * Try to initialize CodeGraph for the default project.
157
- *
158
- * Walks up parent directories to find the nearest .codegraph/ folder,
159
- * similar to how git finds .git/ directories.
259
+ * Decision order:
260
+ * 1. `CODEGRAPH_NO_DAEMON=1` direct mode (unchanged pre-#411 behavior).
261
+ * 2. `CODEGRAPH_DAEMON_INTERNAL=1` → we ARE the detached daemon; listen.
262
+ * 3. No `.codegraph/` reachable → direct mode (the daemon's lockfile and
263
+ * socket both live under `.codegraph/`).
264
+ * 4. Otherwise connect to (or spawn) the shared daemon and proxy to it.
160
265
  *
161
- * If initialization fails, the error is recorded but the server continues
162
- * to work cross-project queries and retries on subsequent tool calls
163
- * are still possible.
266
+ * On any unexpected failure in step 4 we transparently fall back to direct
267
+ * modea misbehaving daemon must never block a session from starting.
164
268
  */
165
- async tryInitializeDefault(projectPath) {
166
- // Record where we searched so a later "not initialized" error can name it.
167
- this.toolHandler.setDefaultProjectHint(projectPath);
168
- // Walk up parent directories to find nearest .codegraph/
169
- const resolvedRoot = (0, index_1.findNearestCodeGraphRoot)(projectPath);
170
- if (!resolvedRoot) {
171
- this.projectPath = projectPath;
172
- return;
269
+ async start() {
270
+ // The detached daemon process itself. Checked before the opt-out so the
271
+ // daemon honors the same env it was spawned with (it never sets NO_DAEMON).
272
+ if (daemonInternalSet()) {
273
+ return this.startDaemonProcess();
274
+ }
275
+ // Direct mode if the user opted out. Setting the env var is sufficient to
276
+ // get the pre-#411 single-process behavior.
277
+ if (daemonOptOutSet()) {
278
+ return this.startDirect('CODEGRAPH_NO_DAEMON set');
279
+ }
280
+ const root = resolveDaemonRoot(this.projectPath);
281
+ if (!root) {
282
+ // No initialized project found — daemon mode has nowhere to put its
283
+ // socket. The fresh-checkout / outside-project case; behave as before.
284
+ return this.startDirect('no .codegraph/ root found');
173
285
  }
174
- this.projectPath = resolvedRoot;
175
286
  try {
176
- this.cg = await index_1.default.open(resolvedRoot);
177
- this.toolHandler.setDefaultCodeGraph(this.cg);
178
- this.startWatching();
287
+ const mode = await this.connectOrSpawnDaemon(root);
288
+ if (mode === 'fallback') {
289
+ return this.startDirect('daemon unavailable; fallback to direct');
290
+ }
291
+ // 'proxy': connectOrSpawnDaemon ran the stdio↔socket pipe to completion
292
+ // (it only returns once the host disconnected). The process is now
293
+ // expected to terminate naturally — the proxy installed its own watchdog.
294
+ this.mode = 'proxy';
295
+ return;
179
296
  }
180
297
  catch (err) {
181
- // Log the error so transient failures are diagnosable (see issue #47)
298
+ // Belt-and-braces: if anything throws inside the daemon machinery,
299
+ // never wedge the user — fall back to a working direct-mode session.
182
300
  const msg = err instanceof Error ? err.message : String(err);
183
- process.stderr.write(`[CodeGraph MCP] Failed to open project at ${resolvedRoot}: ${msg}\n`);
301
+ process.stderr.write(`[CodeGraph MCP] Daemon path failed (${msg}); falling back to direct mode.\n`);
302
+ return this.startDirect('daemon path threw');
184
303
  }
185
304
  }
186
305
  /**
187
- * Retry initialization of the default project if it previously failed.
188
- * Called lazily on tool calls that need the default project.
189
- * Re-walks parent directories each time so it picks up projects
190
- * initialized after the MCP server started.
191
- *
192
- * Awaits any in-flight background init (kicked off by handleInitialize) so
193
- * we never open the SQLite file twice concurrently.
306
+ * Stop the server. In daemon mode this triggers graceful shutdown of every
307
+ * connected session; in direct mode it mirrors the pre-#411 behavior (close
308
+ * cg, exit). Proxy mode never routes through here the proxy exits itself.
194
309
  */
195
- async retryInitIfNeeded() {
196
- // Wait for the background init started during handleInitialize, if any.
197
- if (this.initPromise) {
198
- try {
199
- await this.initPromise;
200
- }
201
- catch { /* errored init falls through to retry */ }
202
- }
203
- // Already initialized successfully
204
- if (this.toolHandler.hasDefaultCodeGraph())
310
+ stop() {
311
+ if (this.stopped)
205
312
  return;
206
- // No explicit path was given at initialize. Resolve it now, exactly once:
207
- // ask the client via roots/list (if it advertised roots), else use cwd.
208
- // Deferring to here lets a roots answer override the wrong cwd, and the
209
- // one-shot guard means we never re-issue roots/list per tool call.
210
- if (!this.projectPath && !this.rootsAttempted) {
211
- this.rootsAttempted = true;
212
- this.initPromise = (this.clientSupportsRoots
213
- ? this.initFromRoots()
214
- : this.tryInitializeDefault(process.cwd())).finally(() => { this.initPromise = null; });
215
- try {
216
- await this.initPromise;
217
- }
218
- catch { /* fall through to last-resort below */ }
219
- if (this.toolHandler.hasDefaultCodeGraph())
220
- return;
313
+ this.stopped = true;
314
+ if (this.ppidWatchdog) {
315
+ clearInterval(this.ppidWatchdog);
316
+ this.ppidWatchdog = null;
221
317
  }
222
- // Last resort: re-walk from the best candidate we have. Picks up projects
223
- // initialized after the server started, and covers clients that sent no
224
- // usable initialize signal at all.
225
- const candidate = this.projectPath ?? process.cwd();
226
- this.toolHandler.setDefaultProjectHint(candidate);
227
- const resolvedRoot = (0, index_1.findNearestCodeGraphRoot)(candidate);
228
- if (!resolvedRoot)
318
+ if (this.daemon) {
319
+ void this.daemon.stop('stop()');
320
+ // Daemon.stop calls process.exit; nothing else to do.
229
321
  return;
230
- try {
231
- // Close any previously failed instance to avoid leaking resources
232
- if (this.cg) {
233
- try {
234
- this.cg.close();
235
- }
236
- catch { /* ignore */ }
237
- this.cg = null;
238
- }
239
- this.cg = index_1.default.openSync(resolvedRoot);
240
- this.projectPath = resolvedRoot;
241
- this.toolHandler.setDefaultCodeGraph(this.cg);
242
- this.startWatching();
243
322
  }
244
- catch {
245
- // Still failing — will retry on next tool call
323
+ if (this.session) {
324
+ this.session.stop();
325
+ this.session = null;
246
326
  }
247
- }
248
- /**
249
- * Resolve the project root via the MCP `roots/list` request and initialize
250
- * from the first root the client reports. Falls back to the process cwd if
251
- * the client returns no usable root or doesn't answer in time. See issue #196.
252
- */
253
- async initFromRoots() {
254
- let target = process.cwd();
255
- try {
256
- const result = await this.transport.request('roots/list', undefined, ROOTS_LIST_TIMEOUT_MS);
257
- const rootPath = firstRootPath(result);
258
- if (rootPath) {
259
- target = rootPath;
260
- }
261
- else {
262
- process.stderr.write('[CodeGraph MCP] Client returned no workspace roots; falling back to process cwd.\n');
263
- }
264
- }
265
- catch (err) {
266
- const msg = err instanceof Error ? err.message : String(err);
267
- process.stderr.write(`[CodeGraph MCP] roots/list request failed (${msg}); falling back to process cwd.\n`);
327
+ if (this.engine) {
328
+ this.engine.stop();
329
+ this.engine = null;
268
330
  }
269
- await this.tryInitializeDefault(target);
331
+ process.exit(0);
270
332
  }
271
- /**
272
- * Start file watching on the active CodeGraph instance.
273
- * Logs sync activity to stderr for diagnostics.
274
- */
275
- startWatching() {
276
- if (!this.cg)
277
- return;
278
- // When the watcher is intentionally disabled (e.g. WSL2 /mnt drives, or
279
- // CODEGRAPH_NO_WATCH=1), say so explicitly and tell the user how to keep
280
- // the graph fresh — otherwise the silent staleness is hard to diagnose.
281
- const disabledReason = (0, sync_1.watchDisabledReason)(this.projectPath ?? process.cwd());
282
- if (disabledReason) {
283
- process.stderr.write(`[CodeGraph MCP] File watcher disabled — ${disabledReason}. ` +
284
- `The graph will not auto-update; run \`codegraph sync\` (or install the git sync hooks via \`codegraph init\`) to refresh.\n`);
285
- return;
333
+ /** Single-process stdio MCP session — the pre-issue-#411 code path. */
334
+ async startDirect(reason) {
335
+ if (reason && process.env.CODEGRAPH_MCP_DEBUG) {
336
+ process.stderr.write(`[CodeGraph MCP] Direct mode: ${reason}.\n`);
286
337
  }
287
- const started = this.cg.watch({
288
- onSyncComplete: (result) => {
289
- if (result.filesChanged > 0) {
290
- process.stderr.write(`[CodeGraph MCP] Auto-synced ${result.filesChanged} file(s) in ${result.durationMs}ms\n`);
291
- }
292
- },
293
- onSyncError: (err) => {
294
- process.stderr.write(`[CodeGraph MCP] Auto-sync error: ${err.message}\n`);
295
- },
338
+ this.engine = new engine_1.MCPEngine();
339
+ const transport = new transport_1.StdioTransport();
340
+ this.session = new session_1.MCPSession(transport, this.engine, {
341
+ explicitProjectPath: this.projectPath,
296
342
  });
297
- if (started) {
298
- process.stderr.write('[CodeGraph MCP] File watcher active graph will auto-sync on changes\n');
299
- }
300
- else {
301
- // start() can also return false when recursive fs.watch isn't supported.
302
- process.stderr.write('[CodeGraph MCP] File watcher unavailable on this platform — run `codegraph sync` to refresh the graph after changes.\n');
343
+ if (this.projectPath) {
344
+ // Background init so the initialize response stays fast (#172).
345
+ void this.engine.ensureInitialized(this.projectPath);
303
346
  }
347
+ this.session.start();
348
+ // Detect parent-process death — same logic as pre-refactor. When stdin
349
+ // closes we go through StdioTransport's `process.exit(0)` already, but
350
+ // SIGKILL of the parent doesn't reliably close stdin on Linux (#277).
351
+ process.stdin.on('end', () => this.stop());
352
+ process.stdin.on('close', () => this.stop());
353
+ this.mode = 'direct';
354
+ this.installSignalHandlers();
355
+ this.installPpidWatchdog();
304
356
  }
305
357
  /**
306
- * Stop the server
358
+ * Run as the detached shared daemon (process spawned with
359
+ * `CODEGRAPH_DAEMON_INTERNAL=1`). Arbitrate the O_EXCL lock, then either
360
+ * become the daemon (bind the socket, serve forever) or — if a live daemon
361
+ * already holds the lock — exit so we don't leak a redundant process.
362
+ *
363
+ * No PPID watchdog and no stdin handlers: the daemon is detached on purpose
364
+ * and reaps itself via client-refcount + idle timeout (see {@link Daemon}).
307
365
  */
308
- stop() {
309
- // Close all cached cross-project connections first
310
- this.toolHandler.closeAll();
311
- // Close the main CodeGraph instance
312
- if (this.cg) {
313
- this.cg.close();
314
- this.cg = null;
366
+ async startDaemonProcess() {
367
+ const root = resolveDaemonRoot(this.projectPath) ?? this.projectPath ?? process.cwd();
368
+ for (let attempt = 0; attempt < TAKEOVER_MAX_RETRIES; attempt++) {
369
+ const lock = (0, daemon_1.tryAcquireDaemonLock)(root);
370
+ if (lock.kind === 'acquired') {
371
+ const daemon = new daemon_1.Daemon(root);
372
+ await daemon.start();
373
+ this.daemon = daemon;
374
+ this.mode = 'daemon';
375
+ return; // the net.Server keeps the process alive
376
+ }
377
+ // Taken. If the holder is alive, another daemon already serves (or is
378
+ // binding) — we're redundant; exit cleanly so the launcher proxies to it.
379
+ const existing = lock.existing;
380
+ if (existing && existing.pid > 0 && (0, daemon_1.isProcessAlive)(existing.pid)) {
381
+ process.stderr.write(`[CodeGraph daemon] Another daemon (pid ${existing.pid}) already holds the lock; exiting.\n`);
382
+ process.exit(0);
383
+ }
384
+ // Holder is dead (or the record is unreadable) — clear it (pid-verified,
385
+ // so we never delete a live daemon's lock) and retry the acquire.
386
+ (0, daemon_1.clearStaleDaemonLock)(lock.pidPath, existing?.pid);
387
+ await sleep(TAKEOVER_RETRY_DELAY_MS);
315
388
  }
316
- this.transport.stop();
389
+ process.stderr.write('[CodeGraph daemon] Could not acquire the daemon lock; exiting.\n');
317
390
  process.exit(0);
318
391
  }
319
392
  /**
320
- * Handle incoming JSON-RPC messages
393
+ * Become a proxy to the shared daemon, spawning the daemon first if none is
394
+ * reachable. Returns 'proxy' once the proxied session has run to completion
395
+ * (the host disconnected), or 'fallback' if the caller should run in-process.
321
396
  */
322
- async handleMessage(message) {
323
- // Check if it's a request (has id) or notification (no id)
324
- const isRequest = 'id' in message;
325
- switch (message.method) {
326
- case 'initialize':
327
- if (isRequest) {
328
- await this.handleInitialize(message);
329
- }
330
- break;
331
- case 'initialized':
332
- // Notification that client has finished initialization
333
- // No action needed - the client is ready
334
- break;
335
- case 'tools/list':
336
- if (isRequest) {
337
- await this.handleToolsList(message);
338
- }
339
- break;
340
- case 'tools/call':
341
- if (isRequest) {
342
- await this.handleToolsCall(message);
343
- }
344
- break;
345
- case 'ping':
346
- if (isRequest) {
347
- this.transport.sendResult(message.id, {});
348
- }
349
- break;
350
- default:
351
- if (isRequest) {
352
- this.transport.sendError(message.id, transport_1.ErrorCodes.MethodNotFound, `Method not found: ${message.method}`);
353
- }
397
+ async connectOrSpawnDaemon(root) {
398
+ const socketPath = (0, daemon_paths_1.getDaemonSocketPath)(root);
399
+ // Fast path: a daemon may already be listening. On success runProxy pipes
400
+ // stdio until the host disconnects, so a 'proxied' outcome means this
401
+ // process has finished its entire job.
402
+ let probe = await (0, proxy_1.runProxy)(socketPath);
403
+ if (probe.outcome === 'proxied')
404
+ return 'proxy';
405
+ if (probe.reason === 'version mismatch')
406
+ return 'fallback';
407
+ // No reachable daemon spawn one (detached) and wait for it to bind.
408
+ spawnDetachedDaemon(root);
409
+ for (let attempt = 0; attempt < DAEMON_CONNECT_MAX_RETRIES; attempt++) {
410
+ await sleep(DAEMON_CONNECT_RETRY_DELAY_MS);
411
+ probe = await (0, proxy_1.runProxy)(socketPath);
412
+ if (probe.outcome === 'proxied')
413
+ return 'proxy';
414
+ if (probe.reason === 'version mismatch')
415
+ return 'fallback';
354
416
  }
417
+ // Daemon never came up in time — run in-process so the user is never blocked.
418
+ return 'fallback';
355
419
  }
356
- /**
357
- * Handle initialize request
358
- */
359
- async handleInitialize(request) {
360
- const params = request.params;
361
- // Does the client support the MCP `roots` protocol? If so, and we have no
362
- // explicit path, we ask it for the workspace root after the handshake
363
- // instead of falling back to the (frequently wrong) cwd. See issue #196.
364
- this.clientSupportsRoots = !!params?.capabilities?.roots;
365
- // Explicit project signal, strongest first: a client-provided rootUri /
366
- // workspaceFolders (LSP-style, non-standard but some clients send it), else
367
- // the --path the server was launched with. cwd is NOT used here — we defer
368
- // it so a roots/list answer can win over it.
369
- let explicitPath = null;
370
- if (params?.rootUri) {
371
- explicitPath = fileUriToPath(params.rootUri);
372
- }
373
- else if (params?.workspaceFolders?.[0]?.uri) {
374
- explicitPath = fileUriToPath(params.workspaceFolders[0].uri);
375
- }
376
- else if (this.projectPath) {
377
- explicitPath = this.projectPath;
378
- }
379
- // Respond to the handshake BEFORE doing any heavy initialization. Loading
380
- // the SQLite DB and the tree-sitter WASM runtime can take many seconds on
381
- // slow filesystems (Docker Desktop VirtioFS on macOS, WSL2). Clients like
382
- // Claude Code time out the handshake at ~30s, which manifested as
383
- // "MCP tools never appear" — the child was alive and had received the
384
- // initialize but was still awaiting initGrammars(). See issue #172.
385
- //
386
- // We accept the client's protocol version but respond with our supported
387
- // version. The `instructions` field is surfaced by MCP clients in the
388
- // agent's system prompt automatically — it's the right place for the
389
- // universal tool-selection playbook, ahead of individual tool descriptions.
390
- this.transport.sendResult(request.id, {
391
- protocolVersion: PROTOCOL_VERSION,
392
- capabilities: {
393
- tools: {},
394
- },
395
- serverInfo: SERVER_INFO,
396
- instructions: server_instructions_1.SERVER_INSTRUCTIONS,
397
- });
398
- // If we know the project dir, kick off init in the background now. Tool
399
- // calls that arrive before it finishes fall through to `retryInitIfNeeded`,
400
- // which waits for this promise rather than racing it with a second open.
401
- //
402
- // If we DON'T know it (no rootUri, no --path), defer: the first tool call
403
- // resolves it via roots/list (when the client supports roots) or cwd. This
404
- // is the fix for issue #196 — clients that launch the server outside the
405
- // project and don't pass a rootUri previously got a misleading "not
406
- // initialized" error on every call.
407
- if (explicitPath) {
408
- this.initPromise = this.tryInitializeDefault(explicitPath).finally(() => {
409
- this.initPromise = null;
410
- });
411
- }
412
- }
413
- /**
414
- * Handle tools/list request
415
- */
416
- async handleToolsList(request) {
417
- await this.retryInitIfNeeded();
418
- this.transport.sendResult(request.id, {
419
- tools: this.toolHandler.getTools(),
420
- });
420
+ /** Standard SIGINT/SIGTERM handlers that route to our `stop()` (direct mode). */
421
+ installSignalHandlers() {
422
+ process.on('SIGINT', () => this.stop());
423
+ process.on('SIGTERM', () => this.stop());
421
424
  }
422
425
  /**
423
- * Handle tools/call request
426
+ * PPID watchdog (#277) — direct mode only. Daemon mode is detached on purpose
427
+ * and reaps via idle timeout; proxy mode installs its own watchdog inside
428
+ * {@link runProxy}. So this only ever runs for an in-process direct session.
424
429
  */
425
- async handleToolsCall(request) {
426
- const params = request.params;
427
- if (!params || !params.name) {
428
- this.transport.sendError(request.id, transport_1.ErrorCodes.InvalidParams, 'Missing tool name');
430
+ installPpidWatchdog() {
431
+ if (this.mode !== 'direct')
429
432
  return;
430
- }
431
- const toolName = params.name;
432
- const toolArgs = params.arguments || {};
433
- // Validate tool exists
434
- const tool = tools_1.tools.find(t => t.name === toolName);
435
- if (!tool) {
436
- this.transport.sendError(request.id, transport_1.ErrorCodes.InvalidParams, `Unknown tool: ${toolName}`);
433
+ const pollMs = parsePpidPollMs(process.env.CODEGRAPH_PPID_POLL_MS);
434
+ if (pollMs <= 0)
437
435
  return;
438
- }
439
- // If the default project isn't initialized yet, retry in case it was
440
- // initialized after the MCP server started (e.g. user ran codegraph init)
441
- await this.retryInitIfNeeded();
442
- const result = await this.toolHandler.execute(toolName, toolArgs);
443
- this.transport.sendResult(request.id, result);
436
+ this.ppidWatchdog = setInterval(() => {
437
+ const current = process.ppid;
438
+ const ppidChanged = current !== this.originalPpid;
439
+ const hostGone = this.hostPpid !== null && !(0, daemon_1.isProcessAlive)(this.hostPpid);
440
+ if (ppidChanged || hostGone) {
441
+ const reason = ppidChanged
442
+ ? `ppid ${this.originalPpid} -> ${current}`
443
+ : `host pid ${this.hostPpid} exited`;
444
+ process.stderr.write(`[CodeGraph MCP] Parent process exited (${reason}); shutting down.\n`);
445
+ this.stop();
446
+ }
447
+ }, pollMs);
448
+ this.ppidWatchdog.unref();
444
449
  }
445
450
  }
446
451
  exports.MCPServer = MCPServer;
452
+ function sleep(ms) {
453
+ // Deliberately NOT unref'd. During the daemon connect/takeover retry loop we
454
+ // may be between processes — no socket bound yet, no transport, no listener
455
+ // pinning the event loop. An unref'd timer would let Node drain the loop and
456
+ // exit silently before we get a chance to try again.
457
+ return new Promise((resolve) => { setTimeout(resolve, ms); });
458
+ }
447
459
  // Export for use in CLI
448
460
  var transport_2 = require("./transport");
449
461
  Object.defineProperty(exports, "StdioTransport", { enumerable: true, get: function () { return transport_2.StdioTransport; } });
450
- var tools_2 = require("./tools");
451
- Object.defineProperty(exports, "tools", { enumerable: true, get: function () { return tools_2.tools; } });
452
- Object.defineProperty(exports, "ToolHandler", { enumerable: true, get: function () { return tools_2.ToolHandler; } });
462
+ var tools_1 = require("./tools");
463
+ Object.defineProperty(exports, "tools", { enumerable: true, get: function () { return tools_1.tools; } });
464
+ Object.defineProperty(exports, "ToolHandler", { enumerable: true, get: function () { return tools_1.ToolHandler; } });
465
+ // Surface a few daemon-mode bits for tests + diagnostics.
466
+ var daemon_2 = require("./daemon");
467
+ Object.defineProperty(exports, "Daemon", { enumerable: true, get: function () { return daemon_2.Daemon; } });
468
+ var version_1 = require("./version");
469
+ Object.defineProperty(exports, "CodeGraphPackageVersion", { enumerable: true, get: function () { return version_1.CodeGraphPackageVersion; } });
453
470
  //# sourceMappingURL=index.js.map