Skip to content

feat: multi-instance browser isolation for concurrent Claude Code sessions#113

Open
ca1sar wants to merge 1 commit intoepiral:mainfrom
ca1sar:feat/multi-instance-isolation
Open

feat: multi-instance browser isolation for concurrent Claude Code sessions#113
ca1sar wants to merge 1 commit intoepiral:mainfrom
ca1sar:feat/multi-instance-isolation

Conversation

@ca1sar
Copy link
Copy Markdown

@ca1sar ca1sar commented Mar 26, 2026

Summary

  • When multiple Claude Code windows run bb-browser commands simultaneously, they all share a single Chrome instance, causing tab/navigation conflicts
  • This PR adds automatic per-session browser isolation: each Claude Code window gets its own Chrome on a dedicated CDP port
  • New instances inherit login state (cookies, localStorage) from the template ~/.bb-browser/browser/user-data/ directory
  • Fully backward compatible — non-session invocations (direct CLI usage) use the existing single-instance path

Design

New session-manager.ts module (self-contained, single integration point in discoverCdpPort):

  • Session detection: walks process tree to find Claude Code's --session-id
  • Port allocation: range 19825–19899, atomic O_EXCL file locks prevent race conditions
  • Session map: ~/.bb-browser/instances/session-map.json with file-lock-protected read-modify-write
  • Login state inheritance: new instances copy user-data from ~/.bb-browser/browser/user-data/ template, with --disable-cookie-encryption so cookies survive cross-profile copy
  • Stale cleanup: dead Claude Code processes' sessions are auto-released on next port allocation
  • Crash recovery: if a session's browser dies, it relaunches on the same port to preserve accumulated state
  • Env var overrides: BB_BROWSER_PORT (explicit port) and BB_BROWSER_SESSION_ID (explicit session identity)

Files changed

File Change
packages/cli/src/session-manager.ts New module — all multi-instance logic
packages/cli/src/cdp-discovery.ts 3 lines added — import + call discoverSessionPort() before legacy path

Test plan

  • Single Claude Code window: bb-browser open url works as before
  • Two Claude Code windows: each gets independent browser, bb-browser snapshot returns different pages
  • Browser crash recovery: kill Chrome, next command relaunches on same port preserving user-data
  • Login state inheritance: new instance has cookies from template directory
  • Direct CLI usage (outside Claude Code): falls through to legacy single-instance path
  • BB_BROWSER_PORT=19830 bb-browser open url: launches on specified port
  • Existing tests pass (15/15)

🤖 Generated with Claude Code

…sions

Each Claude Code window is now automatically assigned its own Chrome
browser instance on a dedicated CDP port, eliminating conflicts when
running multiple sessions simultaneously.

Key changes:
- Add session-manager.ts: detects Claude Code session ID from process
  tree, maintains session-to-port bindings in ~/.bb-browser/instances/
- New instances inherit login state from template user-data directory
- Atomic port allocation via O_EXCL file locks prevents race conditions
- Stale sessions are automatically cleaned up when their CC process exits
- Supports BB_BROWSER_PORT and BB_BROWSER_SESSION_ID env var overrides
- Fully backward compatible: no-session invocations use legacy single-instance path

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.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