(deps: LocalHandshakeDeps)
| 264 | * and proxies again once one answers. |
| 265 | */ |
| 266 | export async function runLocalHandshakeProxy(deps: LocalHandshakeDeps): Promise<void> { |
| 267 | // The proxy is long-lived and can serve fallback tool calls in-process. Match |
| 268 | // direct/daemon mode by killing this launcher if its main thread wedges, so an |
| 269 | // MCP host retry cannot accumulate abandoned `serve --mcp` wrapper processes. |
| 270 | const livenessWatchdog: WatchdogHandle | null = installMainThreadWatchdog(); |
| 271 | let daemonStatus: 'connecting' | 'ready' | 'failed' = 'connecting'; |
| 272 | let daemonSocket: net.Socket | null = null; |
| 273 | let clientInitId: unknown = undefined; // suppress the daemon's reply to the forwarded initialize |
| 274 | let clientInitLine: string | undefined; // replayed to a daemon that comes up mid-session (#2277) |
| 275 | // Telemetry attribution for the in-process fallback only — calls routed to |
| 276 | // the daemon are counted by the daemon's own session (which receives the |
| 277 | // forwarded initialize, clientInfo included), never double-counted here. |
| 278 | let telemetryClient: ClientInfo | undefined; |
| 279 | const pending: string[] = []; // client lines buffered until the daemon resolves |
| 280 | let engine: MCPEngine | null = null; |
| 281 | let engineReady: Promise<void> | null = null; |
| 282 | // Calls being served in-process; the engine is only ever stopped after they finish. |
| 283 | const localCalls = new Set<Promise<void>>(); |
| 284 | // Engines retired but not yet stopped; shutdown() waits for them too. |
| 285 | const retiring = new Set<MCPEngine>(); |
| 286 | let shuttingDown = false; |
| 287 | const retryBaseMs = parseDelayMs(process.env.CODEGRAPH_DAEMON_RETRY_MS, DEFAULT_DAEMON_RETRY_MS); |
| 288 | const retryMaxMs = Math.max(retryBaseMs, parseDelayMs(process.env.CODEGRAPH_DAEMON_RETRY_MAX_MS, DEFAULT_DAEMON_RETRY_MAX_MS)); |
| 289 | let retryDelayMs = retryBaseMs; |
| 290 | let retryTimer: NodeJS.Timeout | null = null; |
| 291 | let attachedAt = 0; |
| 292 | // Requests forwarded to the daemon and not yet answered, keyed by JSON-RPC id. |
| 293 | // If the daemon dies mid-session (#662 — e.g. an MCP host SIGTERM's it when a |
| 294 | // new session starts), these would otherwise hang forever; we re-serve them |
| 295 | // in-process so the host always gets a reply. |
| 296 | const inflight = new Map<unknown, string>(); |
| 297 | // Explore call history for the ONE host connection this proxy serves (CG-17). |
| 298 | // Only the daemon-unavailable fallback below uses it; when the daemon is up, |
| 299 | // the tracking happens on the daemon's own MCPSession. |
| 300 | const exploreSession = new ExploreSessionState(); |
| 301 | // The fallback is still one MCP client session. Keep its one-time warnings |
| 302 | // local just as MCPSession does when the shared daemon is available. |
| 303 | const indexVersionWarnings = new IndexVersionWarningState(); |
| 304 | const recordDaemonWarning = (requestLine: string | undefined, response: JsonRpc): void => { |
| 305 | if (!requestLine) return; |
| 306 | const result = response.result as { content?: Array<{ type?: unknown; text?: unknown }> } | undefined; |
| 307 | const first = result?.content?.[0]; |
| 308 | if (first?.type !== 'text' || typeof first.text !== 'string') return; |
| 309 | if (!first.text.startsWith(INDEX_VERSION_WARNING_PREFIX)) return; |
| 310 | |
| 311 | let request: JsonRpc; |
| 312 | try { request = JSON.parse(requestLine) as JsonRpc; } catch { return; } |
| 313 | const params = request.params as { |
| 314 | arguments?: { projectPath?: unknown }; |
| 315 | } | undefined; |
| 316 | const requestedPath = params?.arguments?.projectPath; |
| 317 | let root = deps.root; |
| 318 | if (typeof requestedPath === 'string') { |
| 319 | const resolved = findNearestCodeGraphRoot(requestedPath); |
| 320 | if (!resolved) return; |
| 321 | root = resolved; |
| 322 | } |
| 323 | indexVersionWarnings.claim(root); |
no test coverage detected