diff options
| author | Igor Soarez <igor@soarez.org> | 2026-07-29 10:38:13 +0100 |
|---|---|---|
| committer | Igor Soarez <igor@soarez.org> | 2026-10-14 12:19:26 +0100 |
| commit | 08e558753e8ef9b2aa96114981a5a1dd37357bad (patch) | |
| tree | 1d52d895f98d7880e7b1cd1ab6ed13321814dd86 /src/daemon/spawn.ts | |
| parent | 6eda4b9cce4b2ae7003ccd7429e3f3aad7c04b66 (diff) | |
feat(cdp): /browser-target to switch browsers without leaving pi
Switching between the shared browser and this machine's own previously meant
leaving pi and relaunching with BU_CDP_HTTP — and, less obviously, killing the
daemon first, or the switch silently did not happen.
/browser-target show the current target
/browser-target local this machine's browser
/browser-target default the built-in default
/browser-target host:port somewhere else
Precedence becomes env > stored > default. The environment still wins so a
one-off `BU_CDP_HTTP=… pi` is never silently overridden by a choice made days
ago; when it is set, the command says so rather than pretending to obey.
Clearing falls back to the default rather than to local — "use the usual
browser", not "go local".
The daemon is the reason this needed more than a settings write. It is
machine-wide, resolves its target once per process, and holds the WebSocket to
the old browser, so re-reading the store would leave tool calls going to the
old browser while /browser-status reported the new one. The command therefore
replaces the process: the daemon now honours an inbound control/shutdown (the
action already existed in the protocol, previously only sent daemon→client),
and stopDaemon() asks for it and polls until the process is really gone before
ensureDaemon() respawns it against the new target. /browser-status now reads
the store too, so it cannot disagree with what the tools will do.
Persistence mirrors the profile pin: atomic temp-file-plus-rename, and every
read failure degrades to "nothing stored" so a corrupt file falls back to the
default instead of breaking browser control. It is a separate file from the
pin, so damage to one cannot cost you the other. The stored value stays in
BU_CDP_HTTP's vocabulary so there is exactly one resolution path.
62 tests: the full precedence table including the stored tier, that a bad value
is blamed on the source that set it rather than sending you hunting through
shell config, that the command only accepts values resolution can actually use,
and eight ways of damaging the store file.
Diffstat (limited to 'src/daemon/spawn.ts')
| -rw-r--r-- | src/daemon/spawn.ts | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/daemon/spawn.ts b/src/daemon/spawn.ts index cea8325..726cf84 100644 --- a/src/daemon/spawn.ts +++ b/src/daemon/spawn.ts @@ -32,6 +32,76 @@ export const daemonSocketMayExist = async ( } }; +/** + * FORK: ask a running daemon to exit, and wait until it has. + * + * Needed by /browser-target. The CDP target is resolved per daemon process and + * the daemon holds an open WebSocket to the old browser, so switching browsers + * means replacing the process — re-reading the stored target would not drop + * that connection. + * + * Resolves true once the daemon is gone (or was never running). Resolves false + * if it is still answering after the timeout, so callers can report the failure + * instead of respawning into a port clash. + */ +export const stopDaemon = async (timeoutMs = 5_000): Promise<boolean> => { + if (!(await isDaemonRunning(1_500))) { + cleanupStaleSocket(); + return true; + } + + await new Promise<void>((resolve) => { + let settled = false; + const done = (): void => { + if (settled) return; + settled = true; + resolve(); + }; + + const sock = createConnection(DAEMON_SOCKET_PATH); + const timer = setTimeout(() => { + sock.destroy(); + done(); + }, timeoutMs); + + sock.on("connect", () => { + // Registration is required before the daemon forwards anything to its + // message handler, so the shutdown has to come from a registered client. + const reg: WireControl = { type: "control", action: "register", clientId: "_target_switch" }; + sock.write(serialize(reg) + "\n"); + }); + + sock.on("data", (chunk: Buffer) => { + if (!chunk.toString().includes('"registered"')) return; + const bye: WireControl = { type: "control", action: "shutdown", reason: "browser target changed" }; + try { + sock.write(serialize(bye) + "\n"); + } catch { + // Daemon may already be going away; the poll below is the real check. + } + clearTimeout(timer); + sock.destroy(); + done(); + }); + + sock.on("error", () => { + clearTimeout(timer); + done(); + }); + }); + + // The daemon exits asynchronously, so confirm rather than assume. + const deadline = Date.now() + timeoutMs; + while (Date.now() < deadline) { + if (!(await isDaemonRunning(1_000))) { + cleanupStaleSocket(); + return true; + } + await new Promise((r) => setTimeout(r, 200)); + } + return false; +}; + export const isDaemonRunning = async (timeoutMs = 2_000): Promise<boolean> => { if (!(await daemonSocketMayExist())) return false; |
