Skip to content

[fix]: don't auto-relaunch on daemon response - #3426

Merged
seanmcguire12 merged 3 commits into
mainfrom
rm-relaunch-on-daemon-response
Nov 9, 2026
Merged

seanmcguire12 merged 3 commits into
mainfrom
rm-relaunch-on-daemon-response

Conversation

@seanmcguire12

@seanmcguire12 seanmcguire12 commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

why

  • the CLI should not automatically relaunch a session based on daemon reported launch settings

what changed

  • this PR separates session reuse from daemon creation/relaunch
  • daemon creation is now strictly derived from launch settings/cli args
  • if the daemon dies, or conflicting options are provided, an error is thrown

test plan

  • added regression coverage to confirm that daemon-reported launch arguments cannot trigger a replacement launch

Summary by cubic

Stops the CLI from auto-relaunching a session with the daemon's reported launch settings when the daemon disappears.

  • Session reuse is now separate from daemon creation; only caller flags and environment defaults can create a daemon.
  • If a daemon vanishes after selection, the command fails with daemon_not_running instead of starting a replacement.
  • Explicit launch options that conflict with a running session now throw an error; run browse stop --session <name> before changing them.
  • Added regression coverage for vanished daemons, conflicting options, and concurrent daemon starts.
  • Documented the reuse and failure behavior in the CLI README.

Written for commit 32dc50f. Summary will update on new commits.

View guided diff Turn on auto-fix

@changeset-bot

changeset-bot Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 32dc50f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
browse Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 8 files

Confidence score: 3/5

  • In packages/cli/src/lib/driver/command-cli.ts, if another command replaces the daemon on the session socket first, this command can run against the replacement instead of returning daemon_not_running. Preserve the selected daemon identity and verify it at dispatch.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="packages/cli/src/lib/driver/command-cli.ts">

<violation number="1" location="packages/cli/src/lib/driver/command-cli.ts:92">
P2: Preserve the selected daemon identity and verify it at dispatch; otherwise, if another command replaces it on the session socket first, this command runs against the replacement instead of returning `daemon_not_running`.</violation>
</file>
Architecture diagram
sequenceDiagram
    participant CLI as CLI Command
    participant F as Flags Parser
    participant R as resolveDriverSession()
    participant DC as Daemon Client
    participant RT as runDriverCommand()
    participant CD as Create Daemon
    participant SD as Session Daemon
    participant UC as User Code

    Note over CLI,UC: Session Selection and Reuse Flow

    CLI->>F: Parse command flags
    F-->>CLI: DriverFlags

    CLI->>R: resolveDriverSession(session, flags)

    alt Explicit target with mode-only flags
        R->>DC: getDriverStatus(session)
        DC-->>R: Status reply (if running)
    else No explicit target
        R->>DC: getDriverStatus(session)
        DC-->>R: Status reply (if running)
    end

    alt Daemon running
        R->>R: Check target compatibility
        alt Compatible or no explicit target
            R-->>CLI: { kind: "existing" }
        else Incompatible target
            R-->>CLI: Throw conflict error
        end
    else No daemon running
        R->>F: resolveConnectionTarget(flags)
        F-->>R: ConnectionTarget
        R-->>CLI: { kind: "create", target }
    end

    CLI->>RT: runDriverCommand(session, selection, command, params)

    alt Selection kind is "create"
        RT->>CD: ensureDriverDaemon(session, target)
        CD->>SD: Spawn daemon process
        SD-->>CD: Daemon ready
    else Selection kind is "existing"
        Note over RT: No daemon creation - reuse only
    end

    alt Command is "open"
        RT->>SD: openViaDaemon(session, url, params)
    else Other commands
        RT->>SD: runDriverCommandViaDaemon(session, command, params)
    end

    alt Daemon still running
        SD-->>RT: Command result
        RT-->>CLI: Output result
        CLI-->>UC: Result JSON
    else Daemon vanished
        SD-->>RT: Connection error
        RT-->>CLI: daemon_not_running error
        CLI-->>UC: Error output
    end
Loading

Reply with feedback, questions, or to request a fix.

View guided diff | Turn on auto-fix | Re-trigger cubic

}
// A status reply describes a running session; it must never supply the
// launch options for a replacement if that daemon subsequently disappears.
return { kind: "existing" };

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Preserve the selected daemon identity and verify it at dispatch; otherwise, if another command replaces it on the session socket first, this command runs against the replacement instead of returning daemon_not_running.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At packages/cli/src/lib/driver/command-cli.ts, line 92:

<comment>Preserve the selected daemon identity and verify it at dispatch; otherwise, if another command replaces it on the session socket first, this command runs against the replacement instead of returning `daemon_not_running`.</comment>

<file context>
@@ -69,23 +69,33 @@ export async function runDriverCommandFromFlags(
     }
+    // A status reply describes a running session; it must never supply the
+    // launch options for a replacement if that daemon subsequently disappears.
+    return { kind: "existing" };
   }
 
</file context>

@seanmcguire12
seanmcguire12 merged commit f93e6d1 into main Nov 9, 2026
5 checks passed
@seanmcguire12
seanmcguire12 deleted the rm-relaunch-on-daemon-response branch November 9, 2026 18:39
shrey150 pushed a commit that referenced this pull request Nov 13, 2026
Release Browse independently of the Stagehand SDKs. Merging this PR
publishes the CLI from the merged commit.

## 0.11.4

### Patch Changes

- [#3426](#3426)
[`f93e6d1`](f93e6d1)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - Report an
error when an active browser session disconnects during command startup
instead of automatically starting a replacement.

- [#3379](#3379)
[`73fdba0`](73fdba0)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - remove
unused deepmerge dependency

- [#3330](#3330)
[`dc8eb40`](dc8eb40)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - bump zod to
4.6.6

- [#3289](#3289)
[`aa67fee`](aa67fee)
Thanks [@shrey150](https://github.com/shrey150)! - Add `--stagehand`
flag to `browse cloud extensions upload`.

- [#3439](#3439)
[`2fe947c`](2fe947c)
Thanks [@github-actions](https://github.com/apps/github-actions)! - Make
`browse doctor` fail and show the cause when the session's last browser
start failed

- [#3400](#3400)
[`ce298c6`](ce298c6)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - Add token
validation to internal local Functions runtime requests

- [#3408](#3408)
[`6a56cbb`](6a56cbb)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - Fix Windows
package-manager command launching for Functions and templates, and
terminate child processes when skill installation times out.

- [#3391](#3391)
[`3fbda6b`](3fbda6b)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - Validate
daemon directory locations before connecting to a session.

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
cursor Bot pushed a commit that referenced this pull request Nov 17, 2026
…emetry

The browse CLI regression in v0.11.4 (10% 'unexpected' errors vs 2.2% in
v0.11.3) occurred because PR #3426 optimized session reuse to skip daemon
re-initialization, exposing paths where daemon errors lacked proper error
codes.

Root cause: When the daemon or browser is in a bad state, plain Error
exceptions are thrown. These propagate to the client without error codes,
resulting in 'result_code: unexpected' in telemetry.

Changes:
- server.ts: formatError now returns 'driver_error' code for non-DriverError
  exceptions, ensuring all daemon errors have proper telemetry codes
- client.ts: Add proper result codes for all error paths:
  - incompleteResponseError -> daemon_connection_lost
  - JSON parse errors -> daemon_protocol_error
  - Socket errors -> daemon_socket_error_{code}
  - Mode conflicts -> session_mode_conflict
- runtime.ts: Auto-recover on 'open' command for recoverable errors:
  - daemon_not_running: socket connection failed
  - daemon_connection_lost: daemon closed mid-request
  - daemon_socket_error_*: socket errors (EPIPE, ECONNRESET, etc)
  - Excludes daemon_socket_timeout to avoid risking two daemons on one session

Fixes the regression by ensuring daemon errors have proper codes (preventing
'unexpected' telemetry) and recovering gracefully for the 'open' command.

Co-authored-by: Shrey Pandya <[email protected]>
cursor Bot pushed a commit that referenced this pull request Nov 17, 2026
…emetry

The browse CLI regression in v0.11.4 (10% 'unexpected' errors vs 2.2% in
v0.11.3) occurred because PR #3426 optimized session reuse to skip daemon
re-initialization, exposing paths where daemon errors lacked proper error
codes.

Root cause: When the daemon or browser is in a bad state, plain Error
exceptions are thrown. These propagate to the client without error codes,
resulting in 'result_code: unexpected' in telemetry.

Changes:
- server.ts: formatError now returns 'driver_error' code for non-DriverError
  exceptions, ensuring all daemon errors have proper telemetry codes
- client.ts: Add proper result codes for all error paths:
  - incompleteResponseError -> daemon_connection_lost
  - JSON parse errors -> daemon_protocol_error
  - Socket errors -> daemon_socket_error_{code}
  - Mode conflicts -> session_mode_conflict
- runtime.ts: Auto-recover on 'open' command for recoverable errors:
  - daemon_not_running: socket connection failed
  - daemon_connection_lost: daemon closed mid-request
  - daemon_socket_error_*: socket errors (EPIPE, ECONNRESET, etc)
  - Excludes daemon_socket_timeout to avoid risking two daemons on one session

Fixes the regression by ensuring daemon errors have proper codes (preventing
'unexpected' telemetry) and recovering gracefully for the 'open' command.

Co-authored-by: Shrey Pandya <[email protected]>
cursor Bot pushed a commit that referenced this pull request Nov 17, 2026
…emetry

The browse CLI regression in v0.11.4 (10% 'unexpected' errors vs 2.2% in
v0.11.3) occurred because PR #3426 optimized session reuse to skip daemon
re-initialization, exposing paths where daemon errors lacked proper error
codes.

Root cause: When the daemon or browser is in a bad state, plain Error
exceptions are thrown. These propagate to the client without error codes,
resulting in 'result_code: unexpected' in telemetry.

Changes:
- server.ts: formatError now returns 'driver_error' code for non-DriverError
  exceptions, ensuring all daemon errors have proper telemetry codes
- client.ts: Add proper result codes for all error paths:
  - incompleteResponseError -> daemon_connection_lost
  - JSON parse errors -> daemon_protocol_error
  - Socket errors -> daemon_socket_error_{code}
  - Mode conflicts -> session_mode_conflict
- runtime.ts: Auto-recover on 'open' command for recoverable errors:
  - daemon_not_running: socket connection failed
  - daemon_connection_lost: daemon closed mid-request
  - daemon_socket_error_*: socket errors (EPIPE, ECONNRESET, etc)
  - Excludes daemon_socket_timeout to avoid risking two daemons on one session

Fixes the regression by ensuring daemon errors have proper codes (preventing
'unexpected' telemetry) and recovering gracefully for the 'open' command.

Co-authored-by: Shrey Pandya <[email protected]>
cursor Bot pushed a commit that referenced this pull request Nov 17, 2026
…emetry

The browse CLI regression in v0.11.4 (10% 'unexpected' errors vs 2.2% in
v0.11.3) occurred because PR #3426 optimized session reuse to skip daemon
re-initialization, exposing paths where daemon errors lacked proper error
codes.

Root cause: When the daemon or browser is in a bad state, plain Error
exceptions are thrown. These propagate to the client without error codes,
resulting in 'result_code: unexpected' in telemetry.

Changes:
- server.ts: formatError now returns 'driver_error' code for non-DriverError
  exceptions, ensuring all daemon errors have proper telemetry codes
- client.ts: Add proper result codes for all error paths:
  - incompleteResponseError -> daemon_connection_lost
  - JSON parse errors -> daemon_protocol_error
  - Socket errors -> daemon_socket_error_{code}
  - Mode conflicts -> session_mode_conflict
- runtime.ts: Auto-recover on 'open' command for recoverable errors:
  - daemon_not_running: socket connection failed
  - daemon_connection_lost: daemon closed mid-request
  - daemon_socket_error_*: socket errors (EPIPE, ECONNRESET, etc)
  - Excludes daemon_socket_timeout to avoid risking two daemons on one session

Fixes the regression by ensuring daemon errors have proper codes (preventing
'unexpected' telemetry) and recovering gracefully for the 'open' command.

Co-authored-by: Shrey Pandya <[email protected]>
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.

2 participants