Skip to content

fix(browse): spawn bun.exe directly for the terminal-agent on Windows (#2295) - #3357

Closed
adammartin0087 wants to merge 1 commit into
garrytan:mainfrom
adammartin0087:fix/windows-terminal-agent-console
Closed

adammartin0087 wants to merge 1 commit into
garrytan:mainfrom
adammartin0087:fix/windows-terminal-agent-console

Conversation

@adammartin0087

Copy link
Copy Markdown
Contributor

Fixes the console flash and 60s respawn loop that #2295 reported. Those were still happening on v1.91.182.0 (Windows 11 26200, npm-global Bun 1.4.2), after the v1.67+ watchdog rework and the polyfill windowsHide forwarding.

Root cause

spawnTerminalAgent spawns a bare 'bun'. Under the Node polyfill, cross-spawn resolves it to npm's bun.cmd shim and runs cmd.exe /d /s /c "bun run ... terminal-agent.ts". That breaks two things:

  1. Console flash. windowsHide applies only to cmd.exe. detached: true gives cmd.exe DETACHED_PROCESS, so it has no console. It then starts bun.exe, which allocates a fresh, visible console. With Windows Terminal as the default terminal, that console opens as a WT window (WindowsTerminal.exe -Embedding) that steals focus for about 2 seconds.
  2. The agent can never start. proc.pid is cmd.exe's PID, so the reservation records the wrong process. terminal-agent.ts main() waits up to 2000 ms for record.pid === process.pid, then throws startup record was not confirmed and exits. That is why the window lasts exactly about 2 seconds. The watchdog sees a dead record and respawns 60 seconds later. The guard trips after 3 respawns, and the cycle restarts with every new daemon.

Process trace (one daemon, node server-node.mjs PID 4636):

06:31:08.882 cmd.exe   <- node.exe (4636)  /d /s /c "bun "run" "--no-env-file" ... terminal-agent.ts --agent-gen=..."
06:31:09.408 OpenConsole.exe -Embedding / WindowsTerminal.exe -Embedding   (focus stolen)
06:32:08.998 cmd.exe   <- node.exe (4636)  (same)
06:32:09.005 OpenConsole.exe -Embedding / WindowsTerminal.exe -Embedding
browse-server.log: respawned by watchdog (PID: 17600) / (PID: 43176) / respawn guard tripped (3 crashes in 300s)

Every one of the 9 project .gstack/browse-*.log files on this machine shows the same respawn-and-trip pattern.

Fix

  • resolveBunExecutable() in terminal-agent-control.ts finds a real bun.exe. It checks, in order: the current executable when running under bun.exe (a compiled browse.exe is not treated as bun), BUN_INSTALL\bin, bun.exe on PATH, the npm-global layout (<dir>\bun.cmd → <dir>\node_modules\bun\bin\bun.exe), and %USERPROFILE%\.bun\bin. It falls back to 'bun', so behavior is unchanged when nothing is found. POSIX is unchanged.
  • spawnTerminalAgent spawns through it. With no cmd.exe in between, windowsHide and detached apply to the agent itself, and proc.pid is the agent's own PID.

The #2637 requirement still holds: the agent stays detached and outlives the CLI.

Verification (Windows 11, the user's real install)

  • browse/test/terminal-agent-bun-exe.test.ts covers resolution order, npm-shim resolution, the compiled-binary case, the fallback, and that the spawn site uses the resolver: 7 pass. bun-child-config.test.ts: 6 pass. bun run typecheck is clean.
  • A Node-side spawn mirroring the polyfill (cross-spawn, windowsHide, detached, stdio: ignore) gets bun.exe as proc.pid, and no OpenConsole or WindowsTerminal process appears.
  • A live daemon on the rebuilt dist/server-node.mjs runs bun.exe ... terminal-agent.ts with the daemon as its direct parent. The agent stays alive, publishes terminal-port, and no console window opens during a 4-minute session.
  • Windows test baseline: terminal-agent-lifecycle.test.ts fails 7 of 14 on unmodified main on this machine, and 4 of 14 with this change. Both runs used the same box and no other changes. The remaining 4 are pre-existing Windows timing failures, not touched here.

Not in this PR

On the cold-start path (the compiled CLI spawns the agent, then exits), the first agent still dies once. The watchdog's next spawn survives. I suspect the CLI's job object takes the child down with it even though it's detached, but I haven't verified that. It's now silent, but it costs up to 60 seconds of terminal-agent availability after each daemon start.

🤖 Generated with Claude Code

Under the Node polyfill a bare 'bun' resolved (via cross-spawn) to npm's
bun.cmd shim and ran as `cmd.exe /c bun run terminal-agent.ts`. windowsHide
only hid cmd.exe, and the detached cmd.exe had no console, so bun.exe
allocated a visible one that stole focus on every spawn. proc.pid was
cmd.exe's, so the agent's startup check (record.pid === process.pid) never
matched; it exited after 2s and the 60s watchdog respawned it (garrytan#2295).

resolveBunExecutable() finds the real bun.exe (own execPath under bun,
BUN_INSTALL, PATH, the npm-global node_modules layout, ~/.bun) and falls
back to 'bun'. POSIX is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@trunk-io

trunk-io Bot commented Oct 23, 2026

Copy link
Copy Markdown

Merging to main in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here

@capy-ai

capy-ai Bot commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

Thank you @adammartin0087, great diagnosis of the bun.cmd shim and the cmd.exe pid. Your commit lands in #3388 with your authorship kept. On top, we moved the bun.exe lookup into browse/src/bun-child-config.ts as findWindowsBunExe() so the Windows native cookie-import worker shares it instead of keeping its own copy, and added three tests for that. Once it merges, could you confirm on your Windows 11 setup that #2295 is fixed? Closing this in favour of #3388.

@capy-ai capy-ai Bot closed this Oct 24, 2026
garrytan added a commit that referenced this pull request Oct 24, 2026
…le() (#3357)

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
capy-ai Bot added a commit that referenced this pull request Oct 25, 2026
…t on Windows (#2295, port of #3357) (#3388)

* fix(browse): spawn bun.exe directly for the terminal-agent on Windows

Under the Node polyfill a bare 'bun' resolved (via cross-spawn) to npm's
bun.cmd shim and ran as `cmd.exe /c bun run terminal-agent.ts`. windowsHide
only hid cmd.exe, and the detached cmd.exe had no console, so bun.exe
allocated a visible one that stole focus on every spawn. proc.pid was
cmd.exe's, so the agent's startup check (record.pid === process.pid) never
matched; it exited after 2s and the 60s watchdog respawned it (#2295).

resolveBunExecutable() finds the real bun.exe (own execPath under bun,
BUN_INSTALL, PATH, the npm-global node_modules layout, ~/.bun) and falls
back to 'bun'. POSIX is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* refactor(browse): one Windows bun.exe lookup for the terminal agent and the native cookie-import worker (#2295)

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* docs(changelog): Windows terminal agent spawns bun.exe directly (#2295, port of #3357)

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* test(browse): the terminal-agent spawn pin expects resolveBunExecutable() (#3357)

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

---------

Co-authored-by: Adam Martin <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant