Skip to content

Agent bridge: the loopback HTTP API driving live tabs - #469

Open
ianu82 wants to merge 3 commits into
pr/browser-dom-inputfrom
pr/agent-bridge
Open

Agent bridge: the loopback HTTP API driving live tabs#469
ianu82 wants to merge 3 commits into
pr/browser-dom-inputfrom
pr/agent-bridge

Conversation

@ianu82

@ianu82 ianu82 commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

What

The security boundary of the whole feature (part 3 of 5): a small HTTP server in the app's main process that lets the AI agent drive the same live tabs the user is looking at.

  • Transport: node:http, loopback only (127.0.0.1), random port, per-launch 256-bit bearer token (timing-safe compare). Everything except /health requires auth.
  • Discovery: ~/.cowork/browser-bridge.json holds {port, token, pid}, written atomically with 0600 perms and deleted on shutdown — no stale credentials pointing at dead processes. The spawned server also gets env vars; the file is authoritative.
  • Routes: browser state, tab create/close/activate, navigate/back/forward/reload, read, snapshot, click, type, scroll, screenshot, top-sites, plus the trusted-input (click-at, press, insert-text) and paste endpoints from part 2.
  • Semantics that matter for an agent: mutating calls wait for the page to settle (with a grace window for click-triggered navigations, and superseded navigations treated as success rather than errors); bodies capped at 1 MB; client-fixable errors come back as 400 with a readable {error} message.
  • Electron-free by design: the manager injects an actions facade, so the entire route table runs in unit tests against fakes.

Verification

  • 35 unit tests: auth (401 without/with wrong token), every route's payload mapping, error shapes, the 413 path, staleness-token 409s, discovery-file write/remove.

For QA

This one is user-testable without the UI. With the app running (any build that includes it):

  1. cat ~/.cowork/browser-bridge.json — expect a port + long token, file readable only by you (ls -l shows -rw-------).
  2. curl http://127.0.0.1:<port>/health{"ok":true} with no auth.
  3. curl http://127.0.0.1:<port>/state → 401.
  4. Same with Authorization: Bearer <token> → 200 with tab state.
  5. Quit the app — the file is gone.

The HTTP API the agent's tools call to drive the user's live tabs:
node:http on 127.0.0.1 with a random port and a per-launch bearer token
(timing-safe compare), JSON routes for state/tabs/navigate/read/
snapshot/click/type/scroll/back/forward/reload/screenshot/top-sites and
the trusted-input + paste endpoints, load-settle waits after mutating
actions, a 1 MB body cap, and the 0600 discovery file (atomic write,
deleted on shutdown). Electron-free by design: the manager injects a
BridgeActions facade, so the whole route table is unit-tested with fakes.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 011d8f6725

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +372 to +375
await new Promise<void>((resolve, reject) => {
server.once('error', reject);
server.listen(0, '127.0.0.1', () => resolve());
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Track bridge startup before awaiting listen

If two startup paths call startBridge() before the first listen() callback fires, or shutdown calls stopBridge() while this await is pending, running is still null, so each caller can create a separate server and stopBridge() can miss the in-flight one entirely. The later server overwrites the only tracked handle, leaving the earlier bridge listening with its own bearer token until process exit; cache an in-flight start promise or mark the server as starting before awaiting listen().

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed — bridge startup shares one in-flight server across concurrent starts (9813bcf).

ianu82 added 2 commits July 22, 2026 17:33
Review follow-up: two racing startBridge() calls (or one during a
pending listen) could each open a server, leaking a live bridge with
its own token until exit. An in-flight start promise now dedupes them.
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.

1 participant