Skip to content

Conversation

@rgarcia
Copy link
Contributor

@rgarcia rgarcia commented Jan 31, 2026

Summary

This PR enables running agent-browser (and other Playwright-based tools) colocated on the VM itself, connecting through the CDP proxy on port 9222 instead of needing to use Chrome's internal port 9223.

Motivation

When running agent-browser directly inside a Kernel browser VM, users should be able to use the natural, documented approach:

agent-browser --cdp 9222 open https://example.com
agent-browser --cdp 9222 snapshot --json

Previously this failed because:

  1. Playwright's connectOverCDP() fetches /json for target discovery, which wasn't implemented
  2. Playwright requests /json/version/ with a trailing slash, which fell through to the WebSocket handler and returned a 426 "Upgrade Required" error

Changes

  • /json and /json/list endpoints: Proxy to Chrome's /json and rewrite webSocketDebuggerUrl and devtoolsFrontendUrl to point back to the proxy (port 9222 instead of 9223)
  • Trailing slash support: Register both /json/version and /json/version/ (and other endpoints) to handle Playwright's URL format
  • HTTP status verification: Check Chrome's response status before processing
  • devtoolsFrontendUrl rewriting: Handle the ws= query parameter format used in DevTools frontend URLs
  • E2E test: Comprehensive test that installs agent-browser in the container and verifies CDP connectivity with multiple command formats

Before

# Inside the VM:
agent-browser --cdp 9222 snapshot  # ✗ Failed to connect
agent-browser --cdp http://127.0.0.1:9222 snapshot  # ✗ Failed to connect

After

# Inside the VM:
agent-browser --cdp 9222 snapshot  # ✓ Works
agent-browser --cdp http://127.0.0.1:9222 snapshot  # ✓ Works
agent-browser --cdp 9222 open https://example.com  # ✓ Works

Test plan

  • Run existing devtoolsproxy tests: go test ./lib/devtoolsproxy/...
  • Run new CDP proxy e2e tests: go test -run TestCDPProxy ./e2e/...
  • Run agent-browser e2e test: go test -run TestAgentBrowserCDPProxy ./e2e/...
  • CI passes

…port

The CDP proxy on port 9222 previously only implemented /json/version,
which returned the WebSocket URL but didn't support target discovery.

Playwright's connectOverCDP() fetches /json to discover browser targets
before establishing a WebSocket connection. Without this endpoint, using
`http://127.0.0.1:9222` with agent-browser or Playwright would fail, even
though direct WebSocket connections (ws://127.0.0.1:9222) worked fine.

This change:
- Adds /json and /json/list endpoints that proxy to Chrome's /json
- Rewrites webSocketDebuggerUrl and devtoolsFrontendUrl in the response
  to use the proxy's host instead of Chrome's internal host
- Enables `agent-browser --cdp http://127.0.0.1:9222` to work correctly
Add comprehensive e2e tests verifying that agent-browser can connect
to Chrome through the CDP proxy on port 9222. Tests validate:

- /json endpoint returns targets with URLs rewritten to proxy port
- /json/list endpoint works correctly with URL rewriting
- /json/version endpoint continues to work
- agent-browser works with various --cdp argument formats:
  - port only (9222)
  - http URL (http://127.0.0.1:9222)
  - localhost:port
  - 127.0.0.1:port
- agent-browser snapshot and navigation commands work via proxy
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

Rewrite the test to focus on validating the /json and /json/list
endpoints added by the PR:

- Test that /json returns targets with webSocketDebuggerUrl rewritten
  from port 9223 (Chrome) to port 9222 (proxy)
- Test that /json/list works the same way
- Test that /json/version continues to work
- Add comparison test showing Chrome's direct /json on 9223 has
  unrewritten URLs

Remove agent-browser tests since they required additional setup
and the core functionality is validated by the JSON endpoint tests.
The TestUpstreamManagerDetectsChromiumAndRestart test was flaky in CI
because Chromium's stderr output was fully buffered when connected to
a file. The "DevTools listening on ws://..." line would sit in a buffer
and never be flushed until the buffer filled or the process exited,
causing the test to timeout after 20 seconds.

Fix by using stdbuf -oL -eL to force line buffering on both stdout and
stderr, ensuring each line is flushed immediately after the newline.

This also improves test speed from ~17-20s to ~0.5s since we no longer
wait for buffered output.
1. Add HTTP status code check before decoding JSON response from Chrome's
   /json endpoint. Previously, 4xx/5xx responses would result in confusing
   JSON decode errors like "invalid character '<'".

2. Fix devtoolsFrontendUrl rewriting for URLs with ws= query parameter.
   Chrome's devtoolsFrontendUrl often has the format:
   https://chrome-devtools-frontend.appspot.com/.../inspector.html?ws=127.0.0.1:9223/...

   The previous code only replaced the URL's host field, but in this format
   the Chrome host appears in the ws= query parameter. Now we handle both
   cases: direct host replacement and ws= query param replacement.
…2e test

Playwright's connectOverCDP requests /json/version/ with a trailing slash,
which was falling through to the WebSocket handler and returning a 426
"Upgrade Required" error. This prevented agent-browser (and other
Playwright-based tools) from connecting via the CDP proxy on port 9222.

- Register trailing-slash variants for /json, /json/, /json/list, /json/list/,
  /json/version, and /json/version/ endpoints
- Add comprehensive e2e test for agent-browser that:
  - Installs agent-browser globally in the container
  - Tests CDP connection with port number format (--cdp 9222)
  - Tests CDP connection with http:// URL format (--cdp http://127.0.0.1:9222)
  - Verifies navigation, snapshot, get url, and get title commands

This enables natural usage of agent-browser within containers:
  agent-browser --cdp 9222 open https://example.com
  agent-browser --cdp 9222 snapshot --json
@rgarcia rgarcia requested a review from Sayan- February 2, 2026 20:38
Copy link
Contributor

@Sayan- Sayan- left a comment

Choose a reason for hiding this comment

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

Reviewed the CDP proxy JSON endpoint changes:

  • main.go: New /json and /json/list handlers that proxy to Chrome and rewrite webSocketDebuggerUrl to point to the proxy (9222) instead of Chrome directly (9223). Also handles ws= query params in devtoolsFrontendUrl. Trailing slash variants added for Playwright compatibility.

  • e2e test: Comprehensive tests covering endpoint responses, URL rewriting verification, and full agent-browser integration through the proxy.

  • proxy_test.go: Reliability fix using stdbuf to prevent test flakiness from buffered stderr in CI.

The two bugbot comments have been addressed in the current code. LGTM.

@rgarcia rgarcia merged commit 40546fc into main Feb 2, 2026
5 checks passed
@rgarcia rgarcia deleted the fix/cdp-proxy-json-endpoint branch February 2, 2026 22:06
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.

3 participants