fix remote debugging (#533)

* fix remote debugging

* debug log
This commit is contained in:
Chris Tate
2026-02-23 09:19:21 -06:00
committed by GitHub
parent 12d79e4428
commit c0f8f32a55
+27 -15
View File
@@ -1363,7 +1363,10 @@ export class BrowserManager {
* Connect to a running browser via CDP (Chrome DevTools Protocol) * Connect to a running browser via CDP (Chrome DevTools Protocol)
* @param cdpEndpoint Either a port number (as string) or a full WebSocket URL (ws:// or wss://) * @param cdpEndpoint Either a port number (as string) or a full WebSocket URL (ws:// or wss://)
*/ */
private async connectViaCDP(cdpEndpoint: string | undefined): Promise<void> { private async connectViaCDP(
cdpEndpoint: string | undefined,
options?: { timeout?: number }
): Promise<void> {
if (!cdpEndpoint) { if (!cdpEndpoint) {
throw new Error('CDP endpoint is required for CDP connection'); throw new Error('CDP endpoint is required for CDP connection');
} }
@@ -1388,14 +1391,16 @@ export class BrowserManager {
cdpUrl = `http://localhost:${cdpEndpoint}`; cdpUrl = `http://localhost:${cdpEndpoint}`;
} }
const browser = await chromium.connectOverCDP(cdpUrl).catch(() => { const browser = await chromium
throw new Error( .connectOverCDP(cdpUrl, { timeout: options?.timeout })
`Failed to connect via CDP to ${cdpUrl}. ` + .catch(() => {
(cdpUrl.includes('localhost') throw new Error(
? `Make sure the app is running with --remote-debugging-port=${cdpEndpoint}` `Failed to connect via CDP to ${cdpUrl}. ` +
: 'Make sure the remote browser is accessible and the URL is correct.') (cdpUrl.includes('localhost')
); ? `Make sure the app is running with --remote-debugging-port=${cdpEndpoint}`
}); : 'Make sure the remote browser is accessible and the URL is correct.')
);
});
// Validate and set up state, cleaning up browser connection if anything fails // Validate and set up state, cleaning up browser connection if anything fails
try { try {
@@ -1521,20 +1526,27 @@ export class BrowserManager {
for (const dir of userDataDirs) { for (const dir of userDataDirs) {
const activePort = this.readDevToolsActivePort(dir); const activePort = this.readDevToolsActivePort(dir);
if (activePort) { if (activePort) {
// Verify the port is actually responding // Try HTTP discovery first (works with --remote-debugging-port mode)
const wsUrl = await this.probeDebugPort(activePort.port); const wsUrl = await this.probeDebugPort(activePort.port);
if (wsUrl) { if (wsUrl) {
// Connect using the discovered WebSocket URL
await this.connectViaCDP(wsUrl); await this.connectViaCDP(wsUrl);
return; return;
} }
// Port from file exists but not responding; try HTTP endpoint directly // HTTP probe failed -- Chrome M144+ chrome://inspect remote debugging uses a
const httpUrl = `http://127.0.0.1:${activePort.port}`; // WebSocket-only server with no HTTP endpoints. Connect using the WebSocket
// path read directly from DevToolsActivePort.
const directWsUrl = `ws://127.0.0.1:${activePort.port}${activePort.wsPath}`;
try { try {
await this.connectViaCDP(httpUrl); if (process.env.AGENT_BROWSER_DEBUG === '1') {
console.error(
`[DEBUG] HTTP probe failed on port ${activePort.port}, ` +
`attempting direct WebSocket connection to ${directWsUrl}`
);
}
await this.connectViaCDP(directWsUrl, { timeout: 60_000 });
return; return;
} catch { } catch {
// Port listed but not connectable, try next directory // Direct WebSocket also failed, try next directory
} }
} }
} }