(socket: net.Socket, replayInitialize: boolean)
| 546 | }; |
| 547 | |
| 548 | const attachDaemon = (socket: net.Socket, replayInitialize: boolean): void => { |
| 549 | daemonSocket = socket; |
| 550 | daemonStatus = 'ready'; |
| 551 | attachedAt = Date.now(); |
| 552 | let sockBuf = ''; |
| 553 | socket.setEncoding('utf8'); |
| 554 | socket.on('data', (chunk: string) => { |
| 555 | sockBuf += chunk; |
| 556 | let idx: number; |
| 557 | while ((idx = sockBuf.indexOf('\n')) !== -1) { |
| 558 | const line = sockBuf.slice(0, idx); |
| 559 | sockBuf = sockBuf.slice(idx + 1); |
| 560 | if (!line.trim()) continue; |
| 561 | let resp: JsonRpc | null = null; |
| 562 | try { resp = JSON.parse(line) as JsonRpc; } catch { /* not JSON — relay verbatim */ } |
| 563 | if (process.env.CODEGRAPH_MCP_DEBUG) process.stderr.write(`[mcp-debug] daemon->proxy ${line.slice(0, 80)}\n`); |
| 564 | if (resp && resp.id !== undefined && ('result' in resp || 'error' in resp)) { |
| 565 | recordDaemonWarning(inflight.get(resp.id), resp); |
| 566 | inflight.delete(resp.id); // answered — no longer in flight |
| 567 | // Suppress the daemon's reply to the initialize we forwarded to prime it |
| 568 | // (the client already got the local handshake response). |
| 569 | if (clientInitId !== undefined && resp.id === clientInitId) continue; |
| 570 | } |
| 571 | writeClient(line); |
| 572 | } |
| 573 | }); |
| 574 | // The daemon going away does NOT end the session (#662). An MCP host can |
| 575 | // SIGTERM the shared daemon when another session starts; if we exited here, |
| 576 | // this host would silently lose CodeGraph and any in-flight request would |
| 577 | // hang. Instead, fall back to the in-process engine until a daemon answers |
| 578 | // again, and re-serve whatever the dead daemon never answered. |
| 579 | const onDaemonLost = (): void => { |
| 580 | if (shuttingDown || daemonSocket !== socket) return; // host teardown, or already handled |
| 581 | daemonStatus = 'failed'; |
| 582 | daemonSocket = null; |
| 583 | try { socket.destroy(); } catch { /* ignore */ } |
| 584 | process.stderr.write( |
| 585 | `[CodeGraph MCP] Shared daemon connection lost; serving this session in-process (degraded), re-serving ${inflight.size} in-flight request(s).\n` |
| 586 | ); |
| 587 | const orphaned = [...inflight.values()]; |
| 588 | inflight.clear(); |
| 589 | for (const line of orphaned) void handleLocally(line); |
| 590 | // A link that stayed up past the longest wait starts the backoff over; one |
| 591 | // that keeps dropping right after it attaches keeps backing off. |
| 592 | if (Date.now() - attachedAt >= retryMaxMs) retryDelayMs = retryBaseMs; |
| 593 | scheduleDaemonRetry(); |
| 594 | }; |
| 595 | socket.on('close', onDaemonLost); |
| 596 | socket.on('error', onDaemonLost); |
| 597 | // A daemon that came up mid-session has never seen this client: prime its |
| 598 | // session with the client's own initialize, whose reply is suppressed |
| 599 | // above like the first one. |
| 600 | if (replayInitialize && clientInitLine !== undefined) { |
| 601 | try { socket.write(clientInitLine + '\n'); } catch { /* close path */ } |
| 602 | } |
| 603 | for (const line of pending) { |
| 604 | trackInflight(line); |
| 605 | if (process.env.CODEGRAPH_MCP_DEBUG) process.stderr.write(`[mcp-debug] proxy-flush ${line.slice(0, 80)}\n`); |
no test coverage detected