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

Function connectWithHello

src/mcp/proxy.ts:157–208  ·  view source on GitHub ↗
(
  socketPath: string,
  expectedVersion: string = CodeGraphPackageVersion,
  helloTimeoutMs: number = DAEMON_HELLO_TIMEOUT_MS,
)

Source from the content-addressed store, hash-verified

155 * spend no more than it has.
156 */
157export async function connectWithHello(
158 socketPath: string,
159 expectedVersion: string = CodeGraphPackageVersion,
160 helloTimeoutMs: number = DAEMON_HELLO_TIMEOUT_MS,
161): Promise<net.Socket | 'older-version' | 'version-mismatch' | 'unproven' | null> {
162 if (process.platform !== 'win32' && !fs.existsSync(socketPath)) return null;
163 const socket = net.createConnection(socketPath);
164 socket.setEncoding('utf8');
165 // Keep an 'error' listener attached for the socket's ENTIRE life. readHelloLine
166 // attaches its own and then REMOVES it on success (its cleanup()), which left a
167 // window — from here until the caller attaches its onDaemonLost handler — where
168 // a socket 'error' had NO listener. In Node an unhandled socket 'error' is
169 // re-thrown as an uncaughtException, which the global fatal handler turns into
170 // process.exit(1); to an MCP client that surfaces as a bare "Transport closed"
171 // (#974). The window is rarely hit on a healthy FS but is common on flaky
172 // AF_UNIX-over-DrvFs (WSL2 /mnt drives). A no-op guard makes the error
173 // recoverable: the follow-up 'close' drives the caller's normal fallback.
174 socket.on('error', () => { /* absorbed — see #974; 'close' drives the fallback */ });
175 const hello = await readHelloLine(socket, helloTimeoutMs).catch(() => null);
176 if (!hello) {
177 socket.destroy();
178 return null; // no daemon yet — caller should keep polling
179 }
180 if (hello.codegraph !== expectedVersion) {
181 // A daemon IS up but it's the wrong version — definitive, not a "not yet".
182 socket.destroy();
183 // An older release is the launcher's to replace (#2335); it says what it did.
184 if (isOlderRelease(hello.codegraph, expectedVersion)) return 'older-version';
185 // Any other: don't poll; the caller serves in-process so we never run stale-vs-new.
186 process.stderr.write(
187 `[CodeGraph MCP] Found a daemon on ${socketPath} but version (${hello.codegraph}) ` +
188 `differs from ours (${expectedVersion}); serving this session in-process.\n`
189 );
190 return 'version-mismatch';
191 }
192 // Before any byte of the session, the daemon proves it holds this user's key.
193 const challenge = newDaemonChallenge();
194 sendClientHello(socket, challenge);
195 const proof = await readProofLine(socket, helloTimeoutMs).catch(() => undefined);
196 // Read once the proof is in: the daemon answering the first challenge makes the key.
197 const key = proof === undefined ? null : readDaemonKey();
198 if (!key || !daemonProofMatches(key, challenge, socketPath, proof)) {
199 socket.destroy();
200 process.stderr.write(
201 `[CodeGraph MCP] The daemon on ${socketPath} could not prove it runs as this user ` +
202 `(with the key in ${daemonKeyPath()}); serving this session in-process.\n`
203 );
204 return 'unproven';
205 }
206 logAttachedDaemon(socketPath, hello);
207 return socket;
208}
209
210/**
211 * Tell the daemon our pids right after we verify its hello, so its liveness

Callers 5

activateFunction · 0.90
runProxyFunction · 0.85

Calls 12

isOlderReleaseFunction · 0.90
newDaemonChallengeFunction · 0.90
readDaemonKeyFunction · 0.90
daemonProofMatchesFunction · 0.90
daemonKeyPathFunction · 0.90
readHelloLineFunction · 0.85
sendClientHelloFunction · 0.85
readProofLineFunction · 0.85
logAttachedDaemonFunction · 0.85
onMethod · 0.65
writeMethod · 0.65
destroyMethod · 0.45

Tested by

no test coverage detected