@colbymchenry/codegraph-darwin-arm64 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
@@ -0,0 +1,403 @@
1
+ "use strict";
2
+ /**
3
+ * Shared MCP daemon — issue #411.
4
+ *
5
+ * One detached `codegraph serve --mcp` daemon process per project root,
6
+ * accepting N concurrent MCP clients over a Unix-domain socket (or named pipe
7
+ * on Windows). Each incoming connection gets its own {@link MCPSession}; all
8
+ * sessions share a single {@link MCPEngine}, which means a single file watcher
9
+ * (one inotify set), a single SQLite connection (one WAL writer), and a single
10
+ * tree-sitter warm-up — paid once, amortized across every agent talking to the
11
+ * project.
12
+ *
13
+ * Lifecycle (see also `./index.ts` and `./proxy.ts`):
14
+ * - The daemon is spawned **detached** (its own session/process group, stdio
15
+ * decoupled) by the first launcher that finds no daemon running. It is NOT
16
+ * a child of any MCP host, so closing one terminal / Ctrl-C'ing one session
17
+ * can't take it down and sever the others. That's why this process has no
18
+ * PPID watchdog: it deliberately outlives every individual client.
19
+ * - Every MCP host talks to the daemon through a thin `proxy` process (the
20
+ * thing the host actually spawned). The proxy keeps the #277 PPID watchdog,
21
+ * so a SIGKILL'd host still reaps its proxy promptly; the proxy's socket
22
+ * close then decrements the daemon's refcount.
23
+ * - When the last client disconnects the daemon lingers for
24
+ * `CODEGRAPH_DAEMON_IDLE_TIMEOUT_MS` (default 300s) so back-to-back agent
25
+ * runs in the same project don't repay startup, then exits cleanly. This is
26
+ * what keeps a single-agent session from leaking a daemon forever (#277).
27
+ *
28
+ * What this file owns:
29
+ * - Listening on the daemon socket and spawning per-connection sessions.
30
+ * - The handshake "hello" line that lets a proxy verify it found a
31
+ * same-version daemon before piping any JSON-RPC through it.
32
+ * - The lockfile (`.codegraph/daemon.pid`) competing daemons arbitrate
33
+ * against — atomic `O_EXCL` create with the full record written in the same
34
+ * breath (no empty-file window) + cleanup on exit.
35
+ * - Reference counting + idle timeout.
36
+ * - Graceful shutdown on SIGTERM/SIGINT and idle exit.
37
+ *
38
+ * What this file does NOT own:
39
+ * - The proxy side (`./proxy.ts`).
40
+ * - The decision of *whether* to run as daemon at all — that's `MCPServer`.
41
+ * - The MCP protocol state machine — that's `./session.ts`.
42
+ */
43
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
44
+ if (k2 === undefined) k2 = k;
45
+ var desc = Object.getOwnPropertyDescriptor(m, k);
46
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
47
+ desc = { enumerable: true, get: function() { return m[k]; } };
48
+ }
49
+ Object.defineProperty(o, k2, desc);
50
+ }) : (function(o, m, k, k2) {
51
+ if (k2 === undefined) k2 = k;
52
+ o[k2] = m[k];
53
+ }));
54
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
55
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
56
+ }) : function(o, v) {
57
+ o["default"] = v;
58
+ });
59
+ var __importStar = (this && this.__importStar) || (function () {
60
+ var ownKeys = function(o) {
61
+ ownKeys = Object.getOwnPropertyNames || function (o) {
62
+ var ar = [];
63
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
64
+ return ar;
65
+ };
66
+ return ownKeys(o);
67
+ };
68
+ return function (mod) {
69
+ if (mod && mod.__esModule) return mod;
70
+ var result = {};
71
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
72
+ __setModuleDefault(result, mod);
73
+ return result;
74
+ };
75
+ })();
76
+ Object.defineProperty(exports, "__esModule", { value: true });
77
+ exports.MAX_HELLO_LINE_BYTES = exports.Daemon = void 0;
78
+ exports.tryAcquireDaemonLock = tryAcquireDaemonLock;
79
+ exports.clearStaleDaemonLock = clearStaleDaemonLock;
80
+ exports.isProcessAlive = isProcessAlive;
81
+ const fs = __importStar(require("fs"));
82
+ const net = __importStar(require("net"));
83
+ const path = __importStar(require("path"));
84
+ const engine_1 = require("./engine");
85
+ const session_1 = require("./session");
86
+ const transport_1 = require("./transport");
87
+ const daemon_paths_1 = require("./daemon-paths");
88
+ const version_1 = require("./version");
89
+ /** Default idle linger after the last client disconnects. */
90
+ const DEFAULT_IDLE_TIMEOUT_MS = 300_000;
91
+ /** Bytes/parse-window for an oversized hello line — bounded against a malicious peer. */
92
+ const MAX_HELLO_LINE_BYTES = 4096;
93
+ exports.MAX_HELLO_LINE_BYTES = MAX_HELLO_LINE_BYTES;
94
+ /**
95
+ * Run as the shared daemon for `projectRoot`. Resolves once the socket is
96
+ * listening. The Daemon owns the socket, the engine, and the lockfile until
97
+ * `stop()` is called or it exits on idle/signal.
98
+ *
99
+ * Race-safe: callers must first call `tryAcquireDaemonLock(projectRoot)` and
100
+ * only construct a Daemon if they got the lock (`kind: 'acquired'`). The atomic
101
+ * `O_EXCL` create inside the acquire helper — which now also writes the full
102
+ * record before returning — is the only synchronization between competing
103
+ * daemons.
104
+ */
105
+ class Daemon {
106
+ projectRoot;
107
+ server = null;
108
+ clients = new Set();
109
+ idleTimer = null;
110
+ idleTimeoutMs;
111
+ engine;
112
+ stopping = false;
113
+ socketPath;
114
+ pidPath;
115
+ constructor(projectRoot, opts = {}) {
116
+ this.projectRoot = projectRoot;
117
+ this.socketPath = (0, daemon_paths_1.getDaemonSocketPath)(projectRoot);
118
+ this.pidPath = (0, daemon_paths_1.getDaemonPidPath)(projectRoot);
119
+ this.idleTimeoutMs = opts.idleTimeoutMs ?? resolveIdleTimeoutMs();
120
+ this.engine = new engine_1.MCPEngine();
121
+ this.engine.setProjectPathHint(projectRoot);
122
+ }
123
+ /**
124
+ * Bind the socket, kick off engine init, and register signal handlers. The
125
+ * lockfile body was already written atomically by `tryAcquireDaemonLock`, so
126
+ * there is nothing to write here. The promise resolves once the server is
127
+ * listening — the daemon then sticks around until idle/shutdown.
128
+ */
129
+ async start() {
130
+ // Engine init is deliberately backgrounded — see #172. The first session
131
+ // to land waits on `ensureInitialized` either way, and unloaded sessions
132
+ // (cross-project tool calls only) shouldn't pay any open cost.
133
+ void this.engine.ensureInitialized(this.projectRoot);
134
+ // Stale socket file (left over from a SIGKILL'd previous daemon) will
135
+ // wedge `listen` with EADDRINUSE. We arrived here holding the lockfile,
136
+ // which means there's no live daemon, so it's safe to clear.
137
+ if (process.platform !== 'win32') {
138
+ try {
139
+ fs.unlinkSync(this.socketPath);
140
+ }
141
+ catch { /* not-exists is fine */ }
142
+ }
143
+ await new Promise((resolve, reject) => {
144
+ const server = net.createServer((socket) => this.handleConnection(socket));
145
+ server.once('error', (err) => reject(err));
146
+ server.listen(this.socketPath, () => {
147
+ // POSIX: tighten permissions to user-only — the socket lives under
148
+ // `.codegraph/`, which is git-ignored but may be on a shared FS.
149
+ if (process.platform !== 'win32') {
150
+ try {
151
+ fs.chmodSync(this.socketPath, 0o600);
152
+ }
153
+ catch { /* best-effort */ }
154
+ }
155
+ this.server = server;
156
+ resolve();
157
+ });
158
+ });
159
+ const lock = {
160
+ pid: process.pid,
161
+ version: version_1.CodeGraphPackageVersion,
162
+ socketPath: this.socketPath,
163
+ startedAt: Date.now(),
164
+ };
165
+ process.stderr.write(`[CodeGraph daemon] Listening on ${this.socketPath} (pid ${process.pid}, v${version_1.CodeGraphPackageVersion}). Idle timeout ${this.idleTimeoutMs}ms.\n`);
166
+ // No clients yet: arm the idle timer immediately so a daemon that nobody
167
+ // ever connects to (e.g. spawned then abandoned because the launcher died)
168
+ // doesn't pin resources forever.
169
+ this.armIdleTimer();
170
+ process.on('SIGINT', () => this.stop('SIGINT'));
171
+ process.on('SIGTERM', () => this.stop('SIGTERM'));
172
+ return { socketPath: this.socketPath, lock };
173
+ }
174
+ /** Currently-connected client count. Exposed for tests / status output. */
175
+ getClientCount() {
176
+ return this.clients.size;
177
+ }
178
+ /** The socket path the daemon is (or will be) listening on. */
179
+ getSocketPath() {
180
+ return this.socketPath;
181
+ }
182
+ /** Graceful shutdown: close all sessions, the engine, and clean up the lock. */
183
+ async stop(reason = 'stop') {
184
+ if (this.stopping)
185
+ return;
186
+ this.stopping = true;
187
+ if (this.idleTimer) {
188
+ clearTimeout(this.idleTimer);
189
+ this.idleTimer = null;
190
+ }
191
+ process.stderr.write(`[CodeGraph daemon] Shutting down (${reason}; clients=${this.clients.size}).\n`);
192
+ for (const session of [...this.clients]) {
193
+ try {
194
+ session.stop();
195
+ }
196
+ catch { /* best-effort */ }
197
+ }
198
+ this.clients.clear();
199
+ if (this.server) {
200
+ await new Promise((resolve) => this.server.close(() => resolve()));
201
+ this.server = null;
202
+ }
203
+ this.engine.stop();
204
+ this.cleanupLockfile();
205
+ if (process.platform !== 'win32') {
206
+ try {
207
+ fs.unlinkSync(this.socketPath);
208
+ }
209
+ catch { /* may already be gone */ }
210
+ }
211
+ process.exit(0);
212
+ }
213
+ handleConnection(socket) {
214
+ // Hello first so the proxy can verify versions before piping any
215
+ // application bytes. The proxy reads exactly one line, then forwards.
216
+ const hello = {
217
+ codegraph: version_1.CodeGraphPackageVersion,
218
+ pid: process.pid,
219
+ socketPath: this.socketPath,
220
+ protocol: 1,
221
+ };
222
+ socket.write(JSON.stringify(hello) + '\n');
223
+ const transport = new transport_1.SocketTransport(socket);
224
+ const session = new session_1.MCPSession(transport, this.engine, {
225
+ explicitProjectPath: this.projectRoot,
226
+ });
227
+ transport.onClose(() => this.dropClient(session));
228
+ this.clients.add(session);
229
+ this.disarmIdleTimer();
230
+ session.start();
231
+ }
232
+ dropClient(session) {
233
+ if (!this.clients.delete(session))
234
+ return;
235
+ if (this.clients.size === 0)
236
+ this.armIdleTimer();
237
+ }
238
+ armIdleTimer() {
239
+ if (this.idleTimer || this.stopping)
240
+ return;
241
+ if (this.idleTimeoutMs <= 0)
242
+ return; // 0 = never idle-exit
243
+ this.idleTimer = setTimeout(() => {
244
+ this.idleTimer = null;
245
+ // Last-second sanity check: if a connection landed between the timer
246
+ // firing and now, don't exit. (setImmediate-ordering is the only way
247
+ // this races; cheap to defend against.)
248
+ if (this.clients.size > 0) {
249
+ this.armIdleTimer();
250
+ return;
251
+ }
252
+ void this.stop('idle timeout');
253
+ }, this.idleTimeoutMs);
254
+ // Don't keep the event loop alive just for this — the net.Server keeps the
255
+ // loop alive while listening, so the timer still fires; once we stop() the
256
+ // loop should drain naturally.
257
+ this.idleTimer.unref?.();
258
+ }
259
+ disarmIdleTimer() {
260
+ if (!this.idleTimer)
261
+ return;
262
+ clearTimeout(this.idleTimer);
263
+ this.idleTimer = null;
264
+ }
265
+ cleanupLockfile() {
266
+ try {
267
+ if (fs.existsSync(this.pidPath)) {
268
+ // Only remove if it still belongs to us — another daemon may have
269
+ // already taken over while we were shutting down (extremely rare).
270
+ const raw = fs.readFileSync(this.pidPath, 'utf8');
271
+ const info = (0, daemon_paths_1.decodeLockInfo)(raw);
272
+ if (info && info.pid === process.pid) {
273
+ fs.unlinkSync(this.pidPath);
274
+ }
275
+ }
276
+ }
277
+ catch { /* best-effort; we're exiting anyway */ }
278
+ }
279
+ }
280
+ exports.Daemon = Daemon;
281
+ /**
282
+ * Atomically create the daemon pidfile with its full record already in place.
283
+ * Returns either an `acquired` result (the caller is the daemon-elect and may
284
+ * construct a {@link Daemon}) or a `taken` result.
285
+ *
286
+ * must-fix 1 (issue #411 review): the lockfile must appear in ONE atomic step,
287
+ * already complete — never empty, even momentarily. The first attempt at this
288
+ * (`O_EXCL` create then a separate `writeSync`) left a microsecond window where
289
+ * the file existed but was empty; under concurrent daemon startup a third
290
+ * candidate could read that empty file, decode it as `null`, and `unlink` the
291
+ * winner's lock → two daemons (two watchers, two writers). The window was
292
+ * normally too small to hit, but the chokidar watcher's extra startup time made
293
+ * concurrent daemons overlap enough to reproduce it reliably.
294
+ *
295
+ * The fix writes the complete record to a private temp file, then hard-links it
296
+ * into place: `link()` is atomic AND exclusive (EEXIST if the target exists), so
297
+ * the pidfile becomes visible in one step already containing a full record.
298
+ * Whoever links first wins; everyone else gets EEXIST and reads a complete file.
299
+ * There is no empty-file window at all.
300
+ */
301
+ function tryAcquireDaemonLock(projectRoot) {
302
+ const pidPath = (0, daemon_paths_1.getDaemonPidPath)(projectRoot);
303
+ // Make sure the .codegraph/ directory exists — the daemon may be the first
304
+ // thing to touch it on a fresh-clone-but-already-initialized checkout.
305
+ fs.mkdirSync(path.dirname(pidPath), { recursive: true });
306
+ const info = {
307
+ pid: process.pid,
308
+ version: version_1.CodeGraphPackageVersion,
309
+ socketPath: (0, daemon_paths_1.getDaemonSocketPath)(projectRoot),
310
+ startedAt: Date.now(),
311
+ };
312
+ // Temp name is pid-scoped so racing candidates never collide on it.
313
+ const tmp = `${pidPath}.${process.pid}.tmp`;
314
+ let acquired = false;
315
+ try {
316
+ fs.writeFileSync(tmp, (0, daemon_paths_1.encodeLockInfo)(info), { mode: 0o600 });
317
+ try {
318
+ fs.linkSync(tmp, pidPath); // atomic + exclusive
319
+ acquired = true;
320
+ }
321
+ catch (err) {
322
+ if (err.code !== 'EEXIST')
323
+ throw err;
324
+ }
325
+ }
326
+ finally {
327
+ try {
328
+ fs.unlinkSync(tmp);
329
+ }
330
+ catch { /* temp already gone */ }
331
+ }
332
+ if (acquired)
333
+ return { kind: 'acquired', pidPath, info };
334
+ // Taken. Because the pidfile was link'd atomically it always holds a complete
335
+ // record — `existing` is null only for a genuinely corrupt leftover, never a
336
+ // mid-write race.
337
+ let existing = null;
338
+ try {
339
+ existing = (0, daemon_paths_1.decodeLockInfo)(fs.readFileSync(pidPath, 'utf8'));
340
+ }
341
+ catch { /* unreadable lockfile — treat as malformed */ }
342
+ return { kind: 'taken', existing, pidPath };
343
+ }
344
+ /**
345
+ * Remove a stale pidfile, but only if it still names a dead process. Re-reads
346
+ * the file immediately before unlinking so we never delete a lock that a live
347
+ * daemon (re)acquired in the meantime.
348
+ *
349
+ * must-fix 1 (issue #411 review): the original unconditionally `unlink`'d,
350
+ * which let a racing candidate delete a healthy daemon's lock. Passing
351
+ * `expectedDeadPid` (the pid the caller believed was dead) makes the clear a
352
+ * compare-and-delete: bail if the file now holds a different pid, or any live
353
+ * pid. Returns true when the stale lock is gone (or was already gone).
354
+ */
355
+ function clearStaleDaemonLock(pidPath, expectedDeadPid) {
356
+ try {
357
+ const raw = fs.readFileSync(pidPath, 'utf8');
358
+ const info = (0, daemon_paths_1.decodeLockInfo)(raw);
359
+ if (info) {
360
+ // A different pid took over since we read it — not ours to clear.
361
+ if (expectedDeadPid !== undefined && info.pid !== expectedDeadPid)
362
+ return false;
363
+ // Holder is actually alive — never clear a live daemon's lock.
364
+ if (info.pid > 0 && isProcessAlive(info.pid))
365
+ return false;
366
+ }
367
+ fs.unlinkSync(pidPath);
368
+ return true;
369
+ }
370
+ catch (err) {
371
+ const e = err;
372
+ if (e.code === 'ENOENT')
373
+ return true; // already gone
374
+ return false;
375
+ }
376
+ }
377
+ /**
378
+ * Probe whether `pid` is currently alive (signal-0). Treats EPERM as alive on
379
+ * every platform (the process exists, it's just not ours to signal) so we never
380
+ * mistake a live daemon for a dead one and clear its lock.
381
+ */
382
+ function isProcessAlive(pid) {
383
+ try {
384
+ process.kill(pid, 0);
385
+ return true;
386
+ }
387
+ catch (err) {
388
+ const e = err;
389
+ if (e.code === 'EPERM')
390
+ return true; // exists, just not ours to signal
391
+ return false;
392
+ }
393
+ }
394
+ function resolveIdleTimeoutMs() {
395
+ const raw = process.env.CODEGRAPH_DAEMON_IDLE_TIMEOUT_MS;
396
+ if (raw === undefined || raw === '')
397
+ return DEFAULT_IDLE_TIMEOUT_MS;
398
+ const parsed = Number(raw);
399
+ if (!Number.isFinite(parsed) || parsed < 0)
400
+ return DEFAULT_IDLE_TIMEOUT_MS;
401
+ return Math.floor(parsed);
402
+ }
403
+ //# sourceMappingURL=daemon.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"daemon.js","sourceRoot":"","sources":["../../src/mcp/daemon.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;;;;;;;;;;;;;;;;;;;;;;;;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D,EAAE,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;QACvB,OAAO,IAAI,CAAC;IACd,CAAC;IAAC,OAAO,GAAY,EAAE,CAAC;QACtB,MAAM,CAAC,GAAG,GAA4B,CAAC;QACvC,IAAI,CAAC,CAAC,IAAI,KAAK,QAAQ;YAAE,OAAO,IAAI,CAAC,CAAC,eAAe;QACrD,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC;AAED;;;;GAIG;AACH,SAAgB,cAAc,CAAC,GAAW;IACxC,IAAI,CAAC;QACH,OAAO,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;QACrB,OAAO,IAAI,CAAC;IACd,CAAC;IAAC,OAAO,GAAY,EAAE,CAAC;QACtB,MAAM,CAAC,GAAG,GAA4B,CAAC;QACvC,IAAI,CAAC,CAAC,IAAI,KAAK,OAAO;YAAE,OAAO,IAAI,CAAC,CAAC,kCAAkC;QACvE,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC;AAED,SAAS,oBAAoB;IAC3B,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,gCAAgC,CAAC;IACzD,IAAI,GAAG,KAAK,SAAS,IAAI,GAAG,KAAK,EAAE;QAAE,OAAO,uBAAuB,CAAC;IACpE,MAAM,MAAM,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;IAC3B,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,MAAM,GAAG,CAAC;QAAE,OAAO,uBAAuB,CAAC;IAC3E,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;AAC5B,CAAC"}
@@ -0,0 +1,100 @@
1
+ /**
2
+ * MCP shared engine — the heavyweight, *shared* state for an MCP server:
3
+ * the project's {@link CodeGraph} instance, file watcher, and the
4
+ * {@link ToolHandler} cache for cross-project queries.
5
+ *
6
+ * One engine, many sessions:
7
+ * - direct mode (single stdio session) instantiates one engine + one session;
8
+ * - daemon mode instantiates one engine and a new session per socket
9
+ * connection. Every session reads from the same SQLite WAL and the same
10
+ * inotify watch set — that's the entire point of issue #411.
11
+ */
12
+ import { ToolHandler } from './tools';
13
+ export interface MCPEngineOptions {
14
+ /**
15
+ * Whether to start the file watcher when initializing. Daemon and direct
16
+ * modes both want this true; tests may set it false to keep the engine
17
+ * cheap. Honors {@link watchDisabledReason} regardless.
18
+ */
19
+ watch?: boolean;
20
+ }
21
+ /**
22
+ * Shared MCP engine. Thread-safe in the sense that multiple sessions can
23
+ * call its methods concurrently — internally it serializes initialization
24
+ * through a single promise so multiple sessions racing each other on first
25
+ * connect never double-open the SQLite file.
26
+ */
27
+ export declare class MCPEngine {
28
+ private cg;
29
+ private toolHandler;
30
+ private projectPath;
31
+ private initPromise;
32
+ private watcherStarted;
33
+ private opts;
34
+ private closed;
35
+ constructor(opts?: MCPEngineOptions);
36
+ /**
37
+ * Convenience for {@link MCPServer} compatibility: pre-seed an explicit
38
+ * project path (from the `--path` CLI flag) without yet opening it. This
39
+ * keeps the synchronous constructor cheap; the actual open happens on the
40
+ * first `ensureInitialized` call.
41
+ */
42
+ setProjectPathHint(projectPath: string): void;
43
+ /** Project root that the engine resolved on first init (null if none). */
44
+ getProjectPath(): string | null;
45
+ /** Shared ToolHandler — sessions delegate tool dispatch through this. */
46
+ getToolHandler(): ToolHandler;
47
+ /** Whether the default project's CodeGraph is open. */
48
+ hasDefaultCodeGraph(): boolean;
49
+ /**
50
+ * Walk up from `searchFrom` to find the nearest `.codegraph/` and open it.
51
+ * Idempotent: concurrent callers share one in-flight init; subsequent
52
+ * callers after success are no-ops.
53
+ *
54
+ * The original `MCPServer.tryInitializeDefault` carried the same retry-on-
55
+ * subsequent-tool-call semantics; we preserve them by NOT throwing when the
56
+ * search misses (just leaves `cg` null so the next call can retry).
57
+ */
58
+ ensureInitialized(searchFrom: string): Promise<void>;
59
+ /**
60
+ * Synchronous last-resort init used by the per-session retry loop when the
61
+ * background `ensureInitialized` already finished (or failed) and we need
62
+ * to pick up a project that appeared *after* the engine started.
63
+ */
64
+ retryInitializeSync(searchFrom: string): void;
65
+ /**
66
+ * Close everything. Used on graceful daemon shutdown (SIGTERM/idle timeout)
67
+ * and on direct-mode stop. Idempotent.
68
+ */
69
+ stop(): void;
70
+ private doInitialize;
71
+ /**
72
+ * Start file watching on the active CodeGraph instance. Idempotent — the
73
+ * watcher is per-engine, not per-session, which is why the daemon path
74
+ * collapses N inotify sets to one. The wording of the disabled-reason log
75
+ * exactly matches the prior in-tree implementation so log-driven dashboards
76
+ * keep working.
77
+ */
78
+ private startWatching;
79
+ /**
80
+ * Reconcile the index with the current filesystem once, right after open —
81
+ * catches edits, adds, deletes, and `git pull`/`checkout` changes made while
82
+ * no watcher was running. Background, never awaited.
83
+ */
84
+ private catchUpSync;
85
+ }
86
+ /**
87
+ * Parse and clamp the CODEGRAPH_WATCH_DEBOUNCE_MS env override.
88
+ *
89
+ * Issue #403: workspaces with bursty writes (formatter-on-save, multi-file
90
+ * refactors) sometimes want a longer quiet window before sync. Returns
91
+ * `undefined` for unset / empty / non-numeric / out-of-range values so the
92
+ * FileWatcher default (2000ms) takes over — never throws.
93
+ *
94
+ * Clamp range: 100ms (faster would mean a sync per keystroke) to 60s (longer
95
+ * and the watcher feels broken). Out-of-range values are treated as "ignore
96
+ * this misconfiguration" rather than capped, since silently capping a 0 or
97
+ * a typoed value would mask a real config bug.
98
+ */
99
+ export declare function parseDebounceEnv(raw: string | undefined): number | undefined;
100
+ //# sourceMappingURL=engine.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"engine.d.ts","sourceRoot":"","sources":["../../src/mcp/engine.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAIH,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAEtC,MAAM,WAAW,gBAAgB;IAC/B;;;;OAIG;IACH,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB;AAED;;;;;GAKG;AACH,qBAAa,SAAS;IACpB,OAAO,CAAC,EAAE,CAA0B;IACpC,OAAO,CAAC,WAAW,CAAc;IAIjC,OAAO,CAAC,WAAW,CAAuB;IAE1C,OAAO,CAAC,WAAW,CAA8B;IACjD,OAAO,CAAC,cAAc,CAAS;IAC/B,OAAO,CAAC,IAAI,CAA6B;IACzC,OAAO,CAAC,MAAM,CAAS;gBAEX,IAAI,GAAE,gBAAqB;IAKvC;;;;;OAKG;IACH,kBAAkB,CAAC,WAAW,EAAE,MAAM,GAAG,IAAI;IAK7C,0EAA0E;IAC1E,cAAc,IAAI,MAAM,GAAG,IAAI;IAI/B,yEAAyE;IACzE,cAAc,IAAI,WAAW;IAI7B,uDAAuD;IACvD,mBAAmB,IAAI,OAAO;IAI9B;;;;;;;;OAQG;IACG,iBAAiB,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAmB1D;;;;OAIG;IACH,mBAAmB,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI;IAsB7C;;;OAGG;IACH,IAAI,IAAI,IAAI;YAUE,YAAY;IAuB1B;;;;;;OAMG;IACH,OAAO,CAAC,aAAa;IA+CrB;;;;OAIG;IACH,OAAO,CAAC,WAAW;CAgBpB;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,GAAG,SAAS,CAM5E"}