Skip to content

Add dynamic CDP port allocation with per-profile meta.json#23

Open
gwpl wants to merge 3 commits intoobra:mainfrom
VariousForks:feat-dynamic-port-allocation
Open

Add dynamic CDP port allocation with per-profile meta.json#23
gwpl wants to merge 3 commits intoobra:mainfrom
VariousForks:feat-dynamic-port-allocation

Conversation

@gwpl
Copy link

@gwpl gwpl commented Feb 7, 2026

Summary

Fixes the hardcoded --remote-debugging-port=9222 in startChrome(), which caused silent failures when port 9222 was busy and prevented multiple parallel Chrome instances.

Closes #24

  • Dynamic port allocation: startChrome() finds an available port in range 9222–12111 via net.createServer probe. First session gets 9222 when available (backward compatible).
  • Per-profile meta.json: Port, PID, headless mode persisted at ~/.cache/superpowers/browser-profiles/{name}.meta.json (sibling to profile dir). Enables reconnection to existing Chrome after MCP restart and multi-instance awareness.
  • --port=N flag: Both MCP server and CLI accept explicit port override (node mcp/dist/index.js --port=9444, ./chrome-ws start --port=9555).
  • Stale detection: isPortAlive() verifies Chrome via /json/version response + PID liveness check (process.kill(pid, 0)).
  • Port priority: explicit --port param > CHROME_WS_PORT env var > dynamic allocation.

Why this matters

Without this change, running two Claude Code sessions that both use superpowers-chrome would cause one to fail silently or connect to the wrong Chrome instance. With meta.json, sessions can discover each other's Chrome instances and avoid port collisions.

Test plan

  • npm run build succeeds (533KB bundle)
  • timeout 2 node mcp/dist/index.js starts without errors
  • timeout 2 node mcp/dist/index.js --port=9444 shows port: 9444 (via --port) in log
  • Start Chrome via MCP → verify ~/.cache/superpowers/browser-profiles/superpowers-chrome.meta.json created with port/pid
  • Kill MCP → verify meta.json cleaned up
  • Start Chrome on 9222 externally → start MCP → gets different port (e.g., 9223)
  • CLI: ./chrome-ws start --port=9555 → Chrome starts on 9555

🤖 Generated with Claude Code

gwpl and others added 3 commits February 8, 2026 00:42
Chrome no longer hardcodes --remote-debugging-port=9222. Instead:

* startChrome() finds an available port in range 9222-12111
* Port assignment persisted in {profile-name}.meta.json alongside
  the profile directory (sibling file, not inside it)
* On startup, checks meta.json for existing Chrome — reconnects
  if alive (enables MCP restart without losing browser state)
* Stale meta.json detected via PID liveness + /json/version probe
* killChrome() cleans meta.json so other sessions know port is free
* showBrowser/hideBrowser preserve port across Chrome restart
* Port priority: explicit param > CHROME_WS_PORT env > dynamic

New internal functions:
- chromeHttpAt(host, port, path) for probing before activePort is set
- isPortAlive(host, port, pid) for stale detection
- isPortFree(port) via net.createServer probe
- findAvailablePort(start, end) sequential scan
- readProfileMeta/writeProfileMeta/clearProfileMeta

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
MCP server (mcp/src/index.ts):
* Parse --port=N from command line arguments
* Simplify ensureChromeRunning() to delegate entirely to
  startChrome() which handles meta.json discovery, reconnection,
  and dynamic port allocation
* Include port info in startup log when --port is specified

CLI (skills/browsing/chrome-ws):
* Parse --port=N from argv, filter from positional args
* Use effectivePort in chromeHttp() and start command
* Print correct debug base URL with effective port

Bundle rebuilt with all changes.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Version bumped in package.json and mcp/package.json
* CHANGELOG.md: v1.7.0 entry with full feature description
* README.md: Add port allocation note, update Windows tip
* SKILL.md: browser_mode returns port, note dynamic allocation

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@coderabbitai
Copy link

coderabbitai bot commented Feb 7, 2026

Warning

Rate limit exceeded

@gwpl has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 18 minutes and 25 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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.

Hardcoded CDP port 9222 prevents parallel Chrome instances

1 participant