MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / attachDaemon

Function attachDaemon

src/mcp/proxy.ts:549–610  ·  view source on GitHub ↗
(socket: net.Socket, replayInitialize: boolean)

Source from the content-addressed store, hash-verified

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

Callers 2

retryDaemonFunction · 0.85
runLocalHandshakeProxyFunction · 0.85

Calls 8

recordDaemonWarningFunction · 0.85
writeClientFunction · 0.85
trackInflightFunction · 0.85
parseMethod · 0.80
onMethod · 0.65
writeMethod · 0.65
getMethod · 0.65
deleteMethod · 0.65

Tested by

no test coverage detected