diff --git a/package.json b/package.json index 017f916..52ff940 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,10 @@ "module": "src/index.ts", "main": "dist/index.js", "exports": { - ".": "./src/index.ts", + ".": { + "bun": "./src/index.ts", + "default": "./dist/index.js" + }, "./rpc": "./src/rpc.ts", "./tui": "./src/tui.tsx" }, diff --git a/src/dashboard.ts b/src/dashboard.ts index e70c746..7bbb4a0 100644 --- a/src/dashboard.ts +++ b/src/dashboard.ts @@ -393,8 +393,12 @@ export async function startDashboard(db: Database, port: number, options?: Dashb // Check if the other process is still alive let alive = false try { process.kill(data.pid, 0); alive = true } catch { /* process is dead */ } - if (alive && data.pid !== process.pid) { - log(`dashboard:already-running port=${port} pid=${data.pid}`) + if (alive) { + log( + data.pid === process.pid + ? `dashboard:already-running port=${port} (same process)` + : `dashboard:already-running port=${port} pid=${data.pid}`, + ) resolve(null) return } @@ -414,6 +418,8 @@ export async function startDashboard(db: Database, port: number, options?: Dashb }) server.listen(port, options?.host ?? "127.0.0.1", () => { + // Auxiliary server: it must not hold the host process open on its own. + server.unref() log(`dashboard:started port=${port} url=http://localhost:${port}`) resolve(toDashboardServer(server)) }) diff --git a/src/v2-setup.ts b/src/v2-setup.ts index cbd80bf..9a23ddd 100644 --- a/src/v2-setup.ts +++ b/src/v2-setup.ts @@ -19,7 +19,7 @@ import { loadConfig } from "./config" import { TokenBucket } from "./rate-limit" import { ActivityBuffer, recordFromToolBefore, recordFromToolAfter } from "./activity" import { buildLeadSystemPrompt, buildTeammateSystemPrompt, buildTeamCompactionContext } from "./system-prompt" -import { startDashboard, type DashboardServer } from "./dashboard" +import { startDashboard } from "./dashboard" import { EnsembleRpc, emitMemberEvent, emitNoticeEvent } from "./v2-rpc" import { isWorktreeInstance } from "./util" import { vlog } from "./log" @@ -381,13 +381,14 @@ export async function setupEnsemble( } await registerV2Tools(ctx.tool, deps) - // Dashboard mirrors V1: main instance only, skipped when port is 0. - let dashboard: DashboardServer | null = null + // Dashboard mirrors V1: main instance only, skipped when port is 0. It is a + // process-global listener, so it is started here but not torn down with this + // instance: a location release disposes the plugin without re-running setup, + // and closing the port left the dashboard unreachable until a restart. const dashboardPort = options.dashboardPort ?? config.dashboardPort if (dashboardPort !== 0 && !isWorktreeInstance(ctx.location.directory)) { - dashboard = await startDashboard(db, dashboardPort, { activityBuffer, client, host: config.dashboardHost }).catch((err) => { + await startDashboard(db, dashboardPort, { activityBuffer, client, host: config.dashboardHost }).catch((err) => { vlog(`init:dashboard:failed err=${err instanceof Error ? err.message : String(err)}`) - return null }) } @@ -398,7 +399,6 @@ export async function setupEnsemble( dispatch, dispose: async () => { controller.abort() - dashboard?.stop() }, } } diff --git a/test/v2-setup.test.ts b/test/v2-setup.test.ts index 416afe1..4f844fd 100644 --- a/test/v2-setup.test.ts +++ b/test/v2-setup.test.ts @@ -235,6 +235,10 @@ describe("v2-setup (issue #36)", () => { } expect(teams.teams?.length).toBe(1) await handle.dispose() + // A location release tears the plugin instance down; the dashboard is + // process-global, so it must keep answering. + const afterRelease = await fetch("http://localhost:47999/api/health") + expect(afterRelease.status).toBe(200) }) test("extractQuestionOutput reads string and part-array results", () => {