STG-1668: browse env local auto-discovers existing Chrome via CDP#1886
Open
STG-1668: browse env local auto-discovers existing Chrome via CDP#1886
Conversation
🦋 Changeset detectedLatest commit: dc8adda The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
Contributor
There was a problem hiding this comment.
2 issues found across 6 files
Confidence score: 3/5
- There is a concrete user-impacting risk in
packages/cli/src/resolve-ws.ts: falling back tows://127.0.0.1:<port>without/devtools/browsercan produce invalid CDP websocket targets and failed attaches. packages/cli/src/index.tsalso has a high-confidence endpoint validation issue: plain TCP reachability is treated as a valid websocket target while hardcoding/devtools/browser, which can allow invalid auto-discovery results.- Given both issues are severity 7/10 with high confidence, this looks like a meaningful regression risk in CDP connection behavior rather than a merge-blocking system-wide break.
- Pay close attention to
packages/cli/src/resolve-ws.tsandpackages/cli/src/index.ts- CDP fallback/discovery endpoint construction and validation may accept invalid targets and break attach flows.
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="packages/cli/src/resolve-ws.ts">
<violation number="1" location="packages/cli/src/resolve-ws.ts:23">
P1: Fallbacking to `ws://127.0.0.1:<port>` is not a valid direct CDP WS endpoint for the no-`/json/version` case; use `/devtools/browser` to avoid failed attaches.</violation>
</file>
<file name="packages/cli/src/index.ts">
<violation number="1" location="packages/cli/src/index.ts:368">
P1: CDP auto-discovery fallback can accept invalid endpoints because it hardcodes `/devtools/browser` and treats plain TCP connectivity as a valid websocket target.</violation>
</file>
Architecture diagram
sequenceDiagram
participant User
participant CLI as Browse CLI
participant Config as Local Config (FS)
participant Daemon as Browser Daemon
participant FS as Chrome User Data (FS)
participant Chrome as Existing Chrome (CDP)
participant Isolated as Isolated Browser
Note over User,Isolated: browse env local Flow
User->>CLI: browse env local [target] [--isolated]
CLI->>Config: NEW: writeLocalConfig(strategy, cdpTarget)
CLI->>Daemon: Restart Daemon
Daemon->>Config: NEW: readLocalConfig()
alt strategy is "isolated"
Daemon->>Isolated: Launch new browser
else strategy is "auto" (default)
Daemon->>FS: NEW: Scan DevToolsActivePort files
FS-->>Daemon: Found port(s)
opt No FS matches
Daemon->>Daemon: NEW: Probe common ports (9222, 9229)
end
Daemon->>Chrome: NEW: Probe /json/version (or WS handshake)
Chrome-->>Daemon: Return webSocketDebuggerUrl
alt Single valid Chrome found
Daemon->>Chrome: NEW: Attach via CDP
Daemon->>Config: NEW: writeLocalInfo(attached-existing)
else Ambiguous (>1) or None found
Daemon->>Isolated: NEW: Fallback to isolated launch
Daemon->>Config: NEW: writeLocalInfo(isolated-fallback)
end
else strategy is "cdp" (explicit port/URL)
Daemon->>Daemon: NEW: resolveWsTarget(target)
Daemon->>Chrome: Connect to specific endpoint
Daemon->>Config: NEW: writeLocalInfo(attached-explicit)
end
Note over User,Config: browse status Flow
User->>CLI: browse status
CLI->>Config: CHANGED: readLocalInfo()
Config-->>CLI: localSource, resolvedCdpUrl, fallbackReason
CLI-->>User: Report browser state & strategy details
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
Contributor
There was a problem hiding this comment.
1 issue found across 3 files (changes from recent commits).
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="packages/cli/src/index.ts">
<violation number="1" location="packages/cli/src/index.ts:415">
P1: The WebSocket verification accepts any response containing "101", which can falsely treat non-upgrade HTTP responses as valid CDP endpoints.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
shrey150
added a commit
to browserbase/skills
that referenced
this pull request
Mar 25, 2026
Update SKILL.md and REFERENCE.md to reflect new browse env local behavior: auto-discovers existing Chrome with remote debugging, --isolated flag, and explicit CDP target (port or URL). Sister PR to browserbase/stagehand#1886. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
shrey150
added a commit
to browserbase/skills
that referenced
this pull request
Mar 26, 2026
Update SKILL.md and REFERENCE.md to reflect new browse env local behavior: auto-discovers existing Chrome with remote debugging, --isolated flag, and explicit CDP target (port or URL). Sister PR to browserbase/stagehand#1886. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
When a bare port number is passed (e.g. `--ws 9222`), resolve the CDP WebSocket URL via `GET http://127.0.0.1:{port}/json/version` with a fallback to `ws://127.0.0.1:{port}`. Full URLs continue to work as-is. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Extract resolveWsTarget into its own module so it can be imported
without triggering Commander's program.parse(). Tests cover:
- bare port resolved via /json/version
- fallback to ws://127.0.0.1:{port} when endpoint is unavailable
- ws://, wss://, and http:// URL passthrough
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…via CDP Change browse env local from always launching an isolated browser to preferring an already-debuggable local Chrome. Discovery scans DevToolsActivePort files and common ports, with fallback to isolated. New commands: - browse env local (auto-discover, default) - browse env local --isolated (force clean browser) - browse env local <port|url> (explicit CDP target) browse status now reports localStrategy, localSource, resolvedCdpUrl. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
The daemon's shutdown handler and stopDaemonAndCleanup were both calling cleanupStaleFiles which deleted client-written config (local-config, context). Switch both to cleanupDaemonStateFiles so local-config and context survive daemon restarts triggered by browse env switches. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- resolve-ws.ts: fallback URL now includes /devtools/browser path (bare ws://host:port is not a valid CDP endpoint) - verifyCdpWebSocket: replaced TCP-only check with actual HTTP WebSocket upgrade verification (101 Switching Protocols) to avoid accepting non-CDP services as valid endpoints Addresses cubic-dev-ai review feedback. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Use regex /^HTTP\/1\.[01] 101/ instead of response.includes("101")
to avoid false positives from non-upgrade responses containing "101".
Addresses cubic-dev-ai review feedback.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
The --ws port resolution was an earlier commit that's part of the larger auto-discover feature. One minor changeset covers both. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
cebffac to
dc8adda
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
browse env localnow auto-discovers an already-running Chrome with remote debugging enabled (viaDevToolsActivePortfiles and common port probing), attaching to it instead of always launching an isolated browser--isolatedflag to force clean isolated browser (old behavior)browse env local 9222orbrowse env local ws://...browse statusnow reportslocalStrategy,localSource,resolvedCdpUrl, andfallbackReasonSister PR: browserbase/skills#54
Test plan
browse env localwith Chrome running +--remote-debugging-port=9222→ attaches to existing Chromebrowse env localwithout debuggable Chrome → falls back to isolated launchbrowse env local --isolated→ always launches clean browserbrowse env local 9222→ persists CDP strategy targeting 127.0.0.1:9222browse statusshows local strategy detailsbrowse env remote→ unchanged🤖 Generated with Claude Code