Skip to content

Commit 0f739f6

Browse files
authored
mcp: coordinate singleton daemon launches (#97)
1 parent fb0a041 commit 0f739f6

4 files changed

Lines changed: 490 additions & 63 deletions

File tree

src/mcp/client.ts

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,8 @@ import {
1515
resolveHunkMcpConfig,
1616
type ResolvedHunkMcpConfig,
1717
} from "./config";
18-
import {
19-
isHunkDaemonHealthy,
20-
isLoopbackPortReachable,
21-
launchHunkDaemon,
22-
waitForHunkDaemonHealth,
23-
} from "./daemonLauncher";
18+
import { ensureHunkDaemonAvailable } from "./daemonLauncher";
2419

25-
const DAEMON_LAUNCH_COOLDOWN_MS = 5_000;
2620
const DAEMON_STARTUP_TIMEOUT_MS = 3_000;
2721
const RECONNECT_DELAY_MS = 3_000;
2822
const HEARTBEAT_INTERVAL_MS = 10_000;
@@ -54,7 +48,6 @@ export class HunkHostClient {
5448
private heartbeatTimer: Timer | null = null;
5549
private stopped = false;
5650
private startupPromise: Promise<void> | null = null;
57-
private lastDaemonLaunchStartedAt = 0;
5851
private lastConnectionWarning: string | null = null;
5952

6053
constructor(
@@ -122,39 +115,11 @@ export class HunkHostClient {
122115
}
123116

124117
private async ensureDaemonAvailable(config: ResolvedHunkMcpConfig) {
125-
if (await isHunkDaemonHealthy(config)) {
126-
this.lastConnectionWarning = null;
127-
return;
128-
}
129-
130-
const shouldLaunch = Date.now() - this.lastDaemonLaunchStartedAt >= DAEMON_LAUNCH_COOLDOWN_MS;
131-
if (shouldLaunch) {
132-
this.lastDaemonLaunchStartedAt = Date.now();
133-
launchHunkDaemon();
134-
}
135-
136-
const ready = await waitForHunkDaemonHealth({
118+
await ensureHunkDaemonAvailable({
137119
config,
138-
timeoutMs: shouldLaunch ? DAEMON_STARTUP_TIMEOUT_MS : 1_500,
120+
timeoutMs: DAEMON_STARTUP_TIMEOUT_MS,
139121
});
140-
141-
if (ready) {
142-
this.lastConnectionWarning = null;
143-
return;
144-
}
145-
146-
const portReachable = await isLoopbackPortReachable(config);
147-
if (portReachable) {
148-
throw new Error(
149-
`Hunk MCP port ${config.host}:${config.port} is already in use by another process. ` +
150-
`Stop the conflicting process or set HUNK_MCP_PORT to a different loopback port.`,
151-
);
152-
}
153-
154-
throw new Error(
155-
`Timed out waiting for the Hunk MCP daemon on ${config.host}:${config.port}. ` +
156-
`Hunk will retry in the background.`,
157-
);
122+
this.lastConnectionWarning = null;
158123
}
159124

160125
setBridge(bridge: HunkAppBridge | null) {
@@ -180,7 +145,6 @@ export class HunkHostClient {
180145
this.websocket = websocket;
181146

182147
websocket.onopen = () => {
183-
this.lastDaemonLaunchStartedAt = 0;
184148
this.lastConnectionWarning = null;
185149
this.startHeartbeat();
186150
this.send({

0 commit comments

Comments
 (0)