[STG-1859] fix(cli): use exact DevToolsActivePort websocket path

Merged
SH
7mo ago
+587-253

Summary

  • move local CDP discovery into a dedicated module so the DevToolsActivePort behavior is directly testable
  • reuse the exact DevToolsActivePort websocket path for browse env local --auto-connect
  • reuse the same exact-path lookup for bare numeric CDP targets in resolveWsTarget
  • avoid the extra speculative websocket verification pass on the file-backed discovery path
  • add regression coverage for exact-path resolution, stale file cleanup, ambiguity, and fallback port probing

Root Cause

The CLI was reading DevToolsActivePort but then discarding the exact websocket path and probing generic endpoints first. On Chrome's remote-debugging approval flow that could trigger extra preflight requests before the real attach.

Validation

  • pnpm --filter @browserbasehq/stagehand gen-version
  • pnpm --filter @browserbasehq/stagehand build-dom-scripts
  • pnpm --filter @browserbasehq/stagehand build
  • pnpm --filter @browserbasehq/browse-cli build
  • pnpm --filter @browserbasehq/browse-cli typecheck
  • pnpm --filter @browserbasehq/browse-cli test

Linear: https://linear.app/browserbase/issue/STG-1859/use-exact-devtoolsactiveport-websocket-path-for-browse-cli-auto


Summary by cubic

Fixes local CDP auto-connect by using the exact websocket path from DevToolsActivePort to avoid extra probes and speed up attach in Chrome’s approval flow. Addresses Linear STG-1859 and improves @browserbasehq/browse-cli for browse env local --auto-connect and bare port targets.

  • Bug Fixes

    • Reuse the exact DevToolsActivePort websocket path for local auto-connect.
    • Prefer exact-path resolution for bare numeric ports in resolveWsTarget, then fall back to /json/version, then /devtools/browser.
    • Skip speculative websocket verification when a file provides the exact path.
    • Clean up stale DevToolsActivePort files and return null on ambiguity.
  • Refactors

    • Extracted local CDP discovery to local-cdp-discovery.ts with tests for exact-path resolution, stale cleanup, ambiguity, and fallback probing.