* Proxy mode (the common case). Serve the MCP handshake LOCALLY for instant * tool registration, forwarding tool calls to the shared daemon — which is * connected in the background (probed, then spawned + polled if absent; one of * an older release is stopped first, #2335) so the * hands
(root: string)
| 743 | * never wedges a session. |
| 744 | */ |
| 745 | private async runProxyWithLocalHandshake(root: string): Promise<void> { |
| 746 | // The daemon may relocate its socket past an in-project filesystem that can't |
| 747 | // host one (ExFAT/FAT volumes, WSL2 DrvFs; #997) to the deterministic tmpdir |
| 748 | // fallback. We don't read the bound path from the lockfile — both sides walk |
| 749 | // the SAME ordered candidate list, so we converge on whichever the daemon |
| 750 | // bound with zero coordination. The in-project candidate is tried first, so a |
| 751 | // normal repo pays nothing extra (it connects on the very first probe). |
| 752 | const candidates = getDaemonSocketCandidates(root); |
| 753 | const connectAnyCandidate = (deadline?: number) => connectAnyDaemonCandidate(candidates, deadline); |
| 754 | const getDaemonSocket = async () => { |
| 755 | // Fast path: a daemon may already be listening (on either candidate). |
| 756 | const probe = await connectAnyCandidate(); |
| 757 | if (probe === 'version-mismatch') return null; // definitive — serve in-process, don't poll for 6s |
| 758 | if (probe && probe !== 'older-version') return probe; |
| 759 | // A daemon of an older release answered — or none did, while the project |
| 760 | // lock may still name one on a socket we don't probe (on Windows, daemons |
| 761 | // before #2278 named their pipe after the root as typed). Either way it |
| 762 | // makes way for one from this install (#2335). |
| 763 | const answered = probe === 'older-version'; |
| 764 | const older = await replaceOlderDaemon(root, answered); |
| 765 | if (older === 'kept' && answered) return null; |
| 766 | // None reachable — spawn one (detached) and poll for its bind. After a |
| 767 | // replacement this launcher holds the writer slot for it meanwhile. |
| 768 | try { |
| 769 | spawnDetachedDaemon(root, older === 'stopped'); |
| 770 | // Null when nothing answered in time — never bound, or a holder that |
| 771 | // never says hello: the proxy serves this session in-process. |
| 772 | return await pollForDaemon(connectAnyCandidate); |
| 773 | } finally { |
| 774 | // The new daemon has taken the slot over, or never came; a no-op unless still ours. |
| 775 | if (older === 'stopped') releaseWriterLock(root); |
| 776 | } |
| 777 | }; |
| 778 | await runLocalHandshakeProxy({ getDaemonSocket, makeEngine: () => makeFallbackEngine(root), root }); |
| 779 | } |
| 780 | |
| 781 | /** Standard SIGINT/SIGTERM handlers that route to our `stop()` (direct mode). */ |
| 782 | private installSignalHandlers(): void { |
no test coverage detected