Overview
Browser Debugger CLI uses a daemon architecture to maintain persistent connections to Chrome while supporting multiple CLI commands. This design enables:- Long-running telemetry collection without keeping CLI commands open
- Multiple commands interacting with the same browser session
- Clean separation between CLI interface and browser connection management
Architecture
- CLI Command: User-facing commands (
bdg status,bdg peek, etc.) - Unix Socket: IPC transport at
~/.bdg/daemon.sockusing JSONL protocol - Daemon (
src/daemon/ipcServer.ts): Routes requests and manages worker lifecycle - Worker (
src/daemon/worker.ts): Detached process handling CDP connection and telemetry - CDP Connection (
src/connection/cdp.ts): WebSocket connection to Chrome DevTools Protocol
Session Lifecycle
Starting a Session
- CLI checks if daemon is already running (reads
~/.bdg/daemon.pid) - If no daemon exists, spawns daemon process
- Daemon spawns worker process with session config
- Worker launches Chrome and establishes CDP connection
- Worker sends
worker_readysignal with Chrome PID and port - Session metadata written to
~/.bdg/session.meta.json - CLI returns control to user while worker continues collecting telemetry
src/session/metadata.ts:22):
Checking Session Status
src/daemon/handlers/requestHandlers.ts):
- Daemon running: Checks if PID in
daemon.pidis alive - Worker running: Verifies worker process via metadata
- Chrome running: Validates Chrome PID from metadata
- CDP connection: Tests WebSocket connection health
Stopping a Session
- CLI sends
stop_session_requestvia socket - Worker receives signal and begins graceful shutdown:
- Closes CDP connection
- Terminates Chrome browser
- Writes final telemetry to
~/.bdg/session.json
- Daemon cleans up socket and PID files
- All processes terminate
~/.bdg/session.json):
Session Files
All session files are stored in~/.bdg/ directory (src/session/paths.ts:18):
| File | Purpose | Lifecycle |
|---|---|---|
daemon.pid | Daemon process ID for liveness checks | Created on daemon start, removed on stop |
daemon.sock | Unix domain socket for IPC | Created on daemon start, removed on stop |
session.meta.json | Live session metadata | Created when worker starts, removed on cleanup |
session.json | Final telemetry output | Created on bdg stop, persists after session |
Worker Process Communication
Worker Ready Signal
When the worker successfully connects to Chrome, it sends a ready message via stdout (src/daemon/worker.ts:46):
IPC Protocol
All daemon-worker communication uses JSONL (JSON Lines) format over Unix sockets and stdin/stdout (src/daemon/ipcServer.ts:128):
- Each message is a single line of JSON
- Messages are delimited by newline characters
- Supports request/response correlation via
sessionIdfield
Error Handling
Worker Startup Failures
If the worker fails to start, it throwsWorkerStartError with semantic error codes (src/daemon/startSession.ts:91):
SPAWN_FAILED: Worker process failed to spawnREADY_TIMEOUT: Worker didn’t send ready signal within 40 secondsWORKER_CRASH: Worker exited before sending ready signalINVALID_READY_MESSAGE: Worker sent malformed ready message
Stale Sessions
If daemon/worker crashes, files may be left in~/.bdg/. Use cleanup commands:
Best Practices
Session persistence: The
session.json file persists after stopping, allowing offline analysis with commands like bdg network list session.json.Related Commands
bdg start- Start a new sessionbdg status- Check session statusbdg stop- Stop session and write outputbdg cleanup- Clean up stale sessionsbdg peek- Preview live telemetry

