Skip to content

fix(cli): don't delete connect file when --connect is omitted#2060

Draft
ziruihao wants to merge 1 commit intomainfrom
fix/cli-preserve-connect-file
Draft

fix(cli): don't delete connect file when --connect is omitted#2060
ziruihao wants to merge 1 commit intomainfrom
fix/cli-preserve-connect-file

Conversation

@ziruihao
Copy link
Copy Markdown
Contributor

Summary

The generic command dispatcher unconditionally unlinks the connect file whenever --connect is not present on the current invocation. This breaks the expected browse env remote --connect <id>browse open <url> flow:

  1. browse env remote --connect <id> writes the connect file and spawns the daemon
  2. The daemon starts but initializes the browser lazily on first command
  3. browse open <url> arrives → opts.connect is undefined → the dispatcher unlinks the connect file
  4. The daemon then initializes, reads the (now-missing) connect file, and creates a brand-new companion session tagged browse_cli: true

Net effect: the user's session is never resumed. Logs and recordings end up on the companion session, not the one the user passed in.

Fix

Remove the else { unlink } branch in the dispatcher. The connect file's lifecycle should be managed only when the user explicitly opts in or out via --connect, not as a side effect of unrelated commands.

-  } else {
-    try {
-      await fs.unlink(getConnectPath(session));
-    } catch {}
   }

This pairs naturally with the env handler change in #2059, which writes the connect file when --connect is passed to env remote and removes it when switching to env local.

Reproduction (before)

$ bb sessions create --keep-alive
{ "id": "abc-123", ... }
$ bb browse env remote --connect abc-123
{ "mode": "remote", "restarted": true }
$ ls $TMPDIR/browse-default.connect    # exists
$ bb browse open https://example.com   # connect file silently deleted
$ ls $TMPDIR/browse-default.connect    # gone
$ bb sessions list --q "user_metadata['browse_cli']:'true'"
# A new session was created — abc-123 is untouched

After

The connect file persists through subsequent commands. The daemon resumes abc-123 instead of creating a companion.

Test plan

  • After browse env remote --connect <id>, running browse open <url> does not delete the connect file
  • browse open <url> --connect <other-id> still updates the connect file (existing behavior preserved)
  • Switching to browse env local cleans up the connect file (handled in fix(cli): honor --connect on browse env remote #2059)

🤖 Generated with Claude Code

Every command that goes through the generic dispatcher unlinks the
connect file when --connect isn't on the current invocation. That
breaks the intended flow:

  $ browse env remote --connect <id>     # writes connect file, spawns daemon
  $ browse open <url>                    # no --connect → unlinks connect file
                                          # daemon then initializes lazily,
                                          # finds no connect file, creates a
                                          # brand-new companion session

The connect file should only be managed when the user explicitly
opts in or out of --connect on the current command. Switching modes
(`browse env local`) is the right place to clean up stale connect
state, not every navigation.

Drop the unconditional `else { unlink }` branch. The file lifecycle
is now: created when --connect is set on `env remote` or any command,
removed when switching to local mode (handled in PR for env handler),
or left alone otherwise.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Apr 27, 2026

⚠️ No Changeset found

Latest commit: 0b4a270

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

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