Overview
Browser Debugger CLI uses a daemon-first, 3-process architecture that enables fast command execution, persistent CDP connections, and reliable state management.Three-Process Model
CLI Process
Lifecycle: Ephemeral (exits after command) Responsibilities:- Parse command-line arguments
- Connect to daemon via Unix socket
- Send IPC requests
- Format and display responses
- Exit with appropriate exit code
src/index.ts
Daemon Process
Lifecycle: Long-running background process Responsibilities:- IPC server on Unix socket (
~/.bdg/daemon.sock) - Spawn and manage worker process
- Route commands between CLI and worker
- Match requests/responses by
requestId - Handle timeouts (10s default)
src/daemon.ts
src/daemon/ipcServer.ts
Worker Process
Lifecycle: Long-running (per session) Responsibilities:- Launch Chrome browser
- Establish CDP WebSocket connection
- Execute CDP commands
- Collect telemetry (network, console, DOM)
- Respond to IPC commands via stdin/stdout
src/daemon/worker.ts
src/daemon/worker.ts
Communication Protocols
IPC Protocol (CLI ↔ Daemon)
Transport: Unix domain socketFormat: JSONL (newline-delimited JSON)
Location:
~/.bdg/daemon.sock
Request Example:
src/ipc/types.ts
Worker IPC (Daemon ↔ Worker)
Transport: stdin/stdout pipesFormat: JSONL Request from Daemon:
src/daemon/workerIpc.ts
CDP Protocol (Worker ↔ Chrome)
Transport: WebSocketFormat: JSON-RPC 2.0
URL:
ws://localhost:9222/devtools/page/<targetId>
Command Example:
src/connection/cdp.ts
Request/Response Matching
Request ID Flow
- CLI sends request with
sessionId(for logging) - Daemon generates unique
requestIdand stores pending request - Worker receives
requestId, executes command, echoes in response - Daemon matches
requestIdto find original client socket - CLI receives response via socket
src/daemon/handlers/pendingRequests.ts
CDP Connection Management
Connection Lifecycle
- Single persistent WebSocket connection per session
- Automatic keepalive (ping/pong)
- Request/response correlation via message IDs
- Event subscription system
- Graceful error handling
src/connection/cdp.ts
CDP Message Flow
Telemetry Collection
Network Collector
Activation:src/telemetry/network.ts
- Captures all HTTP requests/responses
- Records headers, bodies, timing
- Tracks request/response pairing
- HAR export support
Console Collector
Activation:src/telemetry/console.ts
- Captures console.log, warn, error
- Expands object arguments
- Tracks source location
- Deduplication for smart output
DOM Collector
Activation:src/telemetry/dom.ts
Session Files
File Locations
All session files are stored in~/.bdg/:
| File | Created By | Purpose |
|---|---|---|
daemon.sock | Daemon | Unix socket for IPC |
daemon.pid | Daemon | Daemon process ID |
daemon.lock | CLI | Atomic lock during daemon startup |
session.pid | Worker | Worker process ID |
session.meta.json | Worker | Session metadata (Chrome PID, port, target) |
session.json | Worker | Final output (written on stop only) |
chrome-profile/ | Worker | Chrome user data directory |
Session Metadata Format
File:~/.bdg/session.meta.json
src/session/metadata.ts
Execution Flow: bdg localhost:3000
CLI Entry
- Parse arguments with Commander.js
- Check if daemon is running
- Launch daemon if needed (detached process)
Daemon Activation
- Create Unix socket at
~/.bdg/daemon.sock - Write PID file
- Listen for IPC connections
Session Start Request
- CLI connects to daemon socket
- Sends
start_session_request - Daemon spawns worker process
Worker Initialization
- Launch Chrome with remote debugging
- Connect to CDP WebSocket
- Enable telemetry collectors
- Send
worker_readysignal
docs/architecture/BDG_EXECUTION_FLOW.md in source repo
Performance Optimizations
Daemon Persistence
- Daemon stays alive across commands
- No spawn overhead for subsequent commands
- ~100ms saved per command
Worker Persistence
- Single worker per session
- Persistent CDP connection
- Stable DOM nodeIds (index-based operations)
- No reconnection overhead
Unix Sockets
- Fast local IPC (no TCP overhead)
- JSONL streaming protocol
- Low latency request/response
Event Filtering
- Only subscribe to needed CDP events
- Reduce data processing overhead
- Skip irrelevant events
Adding New Commands
Follow the bidirectional IPC pattern:
Complete Guide:
docs/architecture/BIDIRECTIONAL_IPC.md in source repo
Related Documentation
Exit Codes
Semantic exit code system (0-119)
Troubleshooting
Common issues and diagnostic commands
Source Code Reference
Key Modules:src/commands/- CLI handlerssrc/daemon/- IPC server, worker processsrc/connection/- CDP WebSocket, Chrome launchersrc/telemetry/- Network, console, DOM collectorssrc/ipc/- Client helpers, type definitionssrc/session/- Session files, metadata, cleanup

