# Multi-Client / Multi-Agent Support This document describes how multiple AI agents (or CLI users) safely share a single `cremotedaemon` + Chrome instance. ## Architecture ``` ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │ Agent 1 │ │ Agent 2 │ │ Agent N │ │ (LLM/CLI) │ │ (LLM/CLI) │ │ (LLM/CLI) │ └──────┬───────┘ └──────┬───────┘ └──────┬───────┘ │ stdio │ stdio │ stdio (or HTTP CLI) ┌──────┴───────┐ ┌──────┴───────┐ ┌──────┴───────┐ │ cremote-mcp │ │ cremote-mcp │ │ cremote-mcp │ ← one MCP process │ (process) │ │ (process) │ │ (process) │ per agent; each │ currentTab, │ │ currentTab, │ │ currentTab, │ holds its OWN │ iframeMode │ │ iframeMode │ │ iframeMode │ session state └──────┬───────┘ └──────┬───────┘ └──────┬───────┘ │ HTTP (always sends an explicit tab id) └──────────────────┼──────────────────┘ │ ┌─────────────┴─────────────┐ │ cremotedaemon │ ← ONE shared daemon │ (stateless re: current │ (no current tab, no history) │ tab; per-tab maps keyed │ │ by Chrome TargetID) │ └─────────────┬─────────────┘ │ CDP / WebSocket ┌─────────────┴─────────────┐ │ Chrome / Chromium │ │ (all tabs accessible) │ └───────────────────────────┘ ``` ### How isolation is achieved There is **no session layer** and **no daemon-side "current tab"**. Instead, isolation comes from two properties: 1. **One `cremote-mcp` process per agent.** Each agent's MCP server is a separate process with its own in-memory `currentTab`, `tabHistory`, and `iframeMode`. There is no shared process state between agents. (The MCP server is served over **stdio**; it does not run an HTTP listener.) 2. **The daemon requires an explicit tab id on every call** and keys all per-tab state (cached page, iframe context, console logs) by the globally-unique Chrome `TargetID`. Because each agent opens its own tabs and always passes the tab id it received, two agents never address the same map entry. ### What was removed and why Earlier revisions of this guide described an HTTP transport with `Mcp-Session-Id` headers and a daemon-tracked "current tab" / tab history. Neither existed in the code: the MCP server only ever spoke stdio, and a process-global `currentTab` is **not safe to share** across concurrent clients (two agents that omit the tab id would silently operate on whichever tab was touched last). The current-tab fallback was therefore removed entirely. Concretely, in `daemon/daemon.go`: - `Daemon.currentTab` and `Daemon.tabHistory` fields: **removed**. - `getTabID()` (empty → current tab fallback) and `updateTabHistory()`: **removed**. - Every operation (`getTab`, `closeTab`, `switchToIframe`, `switchToMain`, `getConsoleLogs`, and all 20+ command handlers) now returns `tab id is required` when the tab id is empty. - The single mutex was split into `stateMu`, which guards **only** the `tabs` / `iframePages` / `consoleLogs` maps. Long Chrome DevTools Protocol calls (notably `switchToIframe`'s element/frame waits, and `openTab`'s page creation) now run **lock-free**, so one agent switching iframes no longer blocks another agent clicking on a different tab. ## Implications for callers ### MCP clients No change required. Each agent's MCP process already tracked its own `currentTab` and always sent an explicit tab id to the daemon, so the multi-agent workflow continues to work. If an agent's MCP process has not yet opened/navigated a tab, tools that need a tab will now return `tab id is required`; call `web_manage_tabs` (`open` or `switch`) first. ### CLI clients You must now pass `--tab` on every command that operates on a tab. The convenience of omitting `--tab` is gone. Capture the id from `open-tab`: ```bash TAB=$(cremote open-tab) cremote load-url --tab="$TAB" --url="https://example.com" cremote click-element --tab="$TAB" --selector="a.button" cremote close-tab --tab="$TAB" ``` ### `list-tabs` The `*` "current tab" marker and history-based ordering are gone. Tabs are listed by id. `/status` no longer returns `current_tab` or `tab_history`; clients that read those keys get empty values (graceful). ## Remaining concurrency notes / future hardening - **No tab ownership enforcement.** Any client can address any tab by id. This is fine for trusted agents but means a fat-fingered id could touch another agent's tab. Optional `tabOwners` tracking + a `--strict-ownership` flag is a straightforward follow-up. - **No per-tab lock.** Two concurrent operations against the *same* tab are the caller's responsibility (each MCP process is request/response, so this does not arise naturally). A per-tab mutex keyed by TargetID can be added if needed. - **Tab lifecycle.** Tabs opened by an MCP process are not auto-closed when that process exits; agents should close their tabs. A `release-session` cleanup hook is a future improvement. ## Verification - `daemon/daemon_test.go` covers the tab-required contract and concurrent `consoleLogs` access; run with `go test -race ./daemon/...`. - `TestMultiAgentIsolation` is a Chrome-backed integration test (several agents open distinct tabs in parallel and assert no cross-agent content leak). Run it with: ```bash CREMOTE_INTEGRATION=1 go test -race -run TestMultiAgentIsolation ./daemon/... ``` It requires Chrome reachable on port 9222 via `http://localhost:9222/json/version` (note: some environments resolve `localhost` to IPv6 `::1` while Chrome listens on IPv4 `127.0.0.1`; ensure `localhost` resolves to IPv4 for the discovery check to pass).