Skip to content

Agent browser: agents drive ⌗ tabs through mpx browser - #70

Draft
jhen0409 wants to merge 7 commits into
mainfrom
agent-browser
Draft

jhen0409 wants to merge 7 commits into
mainfrom
agent-browser

Conversation

@jhen0409

@jhen0409 jhen0409 commented Oct 8, 2026 •

Copy link
Copy Markdown
Member

Summary

Host Settings ▸ Agent browser (per host, synced, default off) lets agents on that host open and operate web pages beside its terminal with mpx browser. CLI side: multiplex-term/mpx-cli#2, which also ships the multiplex skill.

  • Bridge. The app runs mpx bridge on its own SSH connection (SSHConnection.openLineChannel, a PTY-less login shell) and speaks JSON lines with it (AgentBrowserWire, pure).
    • Wire integers are bounded (wireInteger).
    • Requests that went stale during a suspension are dropped. The bridge clock is learned only from pongs that answer the ping in flight.
    • The link pings and redials on a network change or a host edit.
  • Agent tabs (ViewportController.Agent). Agents can drive only the tabs they opened.
    • Helpers run in an isolated WKContentWorld. There is still no message handler, so the page gets no bridge into the app.
    • Each tab uses the host's own data store, so the user's cookies are never visible.
    • Tabs carry an ⌗ AGENT label.
    • A tab docks beside the agent's tmux session, preferring a window that is on screen. It takes focus only through AgentFocusGate: never while the user is typing, at most once per 30 s per host.
  • Route: through the host (AgentBrowserProxy).
    • The tabs' data store gets a loopback CONNECT proxy (random credential, no failover). Every connection is an SSH direct-tcpip opened by the host, so the device's LAN is never on the path, and DNS-to-private-address tricks have nothing to reach.
    • A real device never proxies localhost, so host loopback ports are forwarded on the device.
      • Forwards admit a connection only if its first request carries an HttpOnly cookie, set per proxy through the tab's own store.
      • mpx browser forward <port> --open opts a port out of the cookie check. Re-checking the port closes the connections admitted while it was open.
      • Forwards close with the host's last agent tab.
    • A loopback content rule list blocks every other device port. WebRTC is removed from agent tabs.
    • Tunnels are backpressured both ways (AgentProxySplice).
    • DEBUG MULTIPLEX_AGENT_ROUTE=device keeps the old device route, for measurement.
  • Hardening from a Codex security review.
    • Quotas: 8 requests in flight per host, 6 tabs per host, one screenshot at a time, capped answers.
    • Console capture: levels are allowlisted and the buffer has a character budget.
    • Content rules fail closed.
    • Revocation cancels in-flight requests and closes the host's tabs.
  • INSTALL SKILL (same section, shown while the switch is on). It runs mpx skill install --json on the host, which writes the multiplex skill into ~/.agents/skills and ~/.claude/skills, then lists the files written or says what to fix (mpx missing, mpx older than 0.2, a directory mpx could not write).
    • The skill text comes from the host's own mpx, never from the app.
    • It goes through the bridge's road (a throwaway connection, a PTY-less login shell, the same search for mpx), so it finds the mpx the agent browser runs.
    • It uses the form's credentials like the Signal check, so it works before a host is saved.
  • Records: docs/agents/links-and-viewers.md ("Agent tabs"), architecture.md, e2e-headless.md. Strings are translated (zh-Hant, ja), and the TestFlight note is added.

Testing

  • Unit tests: visionOS 1693 ✅, iPad 1704 ✅ (plus 60 Swift Testing tests on each). SwiftLint: 0 violations.
  • New tests cover the wire, the bridge clock, the address and loopback rules, proxy-head parsing, forward-cookie admission, AgentFocusGate, docking order, splice backpressure, the composed AGENT label, and the skill install (script, answer parsing, wording, the sheet's button).
  • INSTALL SKILL in the visionOS simulator against the dev sshd harness: a dev mpx wrote both files; the Mac's Homebrew mpx 0.1.2 gave the "too old" row; an unwritable .claude/skills gave INSTALLED plus a CHECK row.
  • iPhone Air, against this Mac as the host:
    • A loopback-only dev server loads.
    • Forwarded WebSocket (HMR) works.
    • 60 parallel requests take about 0.2 s; a 50 MB download completes without stalling.
    • A listening device port that isn't forwarded is blocked.
    • --open behaves: checked → refused, open → reached, re-checked → refused.
    • Screenshots are capped at 1280 px.
    • A suspended request is not replayed after resume.
  • Simulators (visionOS / iPad): background-scheduling measurements and the bridge lifecycle. Notes are in local-plan/agent-browser.md (not committed).

Known limits

  • Bulk transfer tops out around 7 MB/s over Wi-Fi. swift-nio-ssh's per-channel window equals its 128 KiB maximum packet size; raising it naively made fetches hang.
  • A cross-port fetch without credentials is refused unless that port is --open.
  • *.localhost is not admitted; https://localhost works only on an open port.
  • Requires mpx 0.2 (the mpx-cli PR).

Host Settings ▸ Agent browser (per host, synced, default off) lets agents
on that host open and operate web pages beside its terminal via
`mpx browser` (mpx-cli PR). The app runs `mpx bridge` over its own SSH
connection and serves requests against AGENT viewport tabs only.

- Tabs: helpers in an isolated WKContentWorld (still no message handler —
  the page gets no bridge), the host's own data store, docked beside the
  agent's tmux session on a window that is on screen; focus taken only
  through AgentFocusGate (never mid-typing, once per 30 s per host).
- Route: pages load THROUGH THE HOST — a loopback CONNECT proxy for the
  tabs' data store, every connection an SSH direct-tcpip from the host, so
  the device's LAN is never on the path. A real device never proxies
  loopback, so host localhost ports are forwarded on the device, admitted
  by an HttpOnly per-proxy cookie (`forward --open` opts a port out) and
  closed with the host's last agent tab; a loopback rule list blocks every
  other device port. WebRTC is removed from agent tabs.
- Robustness: bounded wire integers and buffers, per-host quotas, stale
  requests dropped after suspension (bridge clock from trusted pongs),
  revocation cancels in-flight work and closes the host's tabs, the link
  redials on network change, backpressured tunnels.
# Conflicts:
#	Multiplex/Localizable.xcstrings
#	fastlane/testflight-whats-new.txt
…light

Found reviewing the branch after merging main.

Forwards
- An `open` that made no tab (no terminal window for the host, a revoke)
  left the port it had forwarded bound on the device. Nothing closed it
  until some later tab opened and closed, and meanwhile the same port
  was `port_busy` for another host. The host's slot now counts `open`s
  on their way, and forwards are released when neither a tab nor one of
  those is left.
- A host can hold 16 forwards (`too_many_forwards`). A page can ask for
  one per loopback port it navigates a frame to, and each is two
  listeners on the device.
- Two ports forwarded at once compiled two rule lists, and the older
  one could be applied last, blocking a port that was forwarded.
  `contentRules` now answers only with rules for the forwards as they
  are when it returns.
- The six-tab quota is counted again after `open`'s waits.

Teardown
- A link session torn down before its SSH connection existed (a redial
  or a stop during the keychain read) still dialled and started a
  second `mpx bridge`. Nobody read it or ended it, and it took the
  socket from the live bridge.
- A proxy dial retired by a host edit, a network change, or `stop` was
  adopted when it finished. It is closed instead, and a stopped proxy
  binds nothing more. Two first tabs share one proxy listener.
- A host-side connection opened for a device connection that had
  already closed stayed open until the far end gave up: WebKit drops
  spare connections, and that end had fired into no splice. A splice
  joined to a partner that already ended now closes its own side.
- Returning to the foreground re-arms the periodic ping. It slept
  through the suspension, woke with the app, and could find the resume
  ping unanswered an instant after it left, redialling a healthy link.

Tests: the splice tests connect their channels first. An
EmbeddedChannel is inactive until it connects, so "the device side
follows" passed without the splice doing anything. New tests cover the
forward bound, a stopped proxy, and the orphaned side.
A second, independent review (Gemini) of the merged branch. Each item
was confirmed by reading the code or by experiment before it was
changed; the ones marked E2E were also reproduced in the simulator.

Requests
- A cancelled request spun on the main actor. `settle` and `wait` sleep
  between looks with `try?`; a cancelled task's sleep returns at once,
  so the loop ran flat out until its deadline (20 s, or the whole wait).
  The bridge's `cancel`, a redial, and a revoke all cancel requests
  mid-load. The polls now end on cancellation.
- `eval` ran code twice when it threw a SyntaxError at run time
  (`r.json()` on an HTML error page): the statement-body retry could not
  tell that from a body that did not compile, and a POST went out twice.
  The bodies now catch what the code throws. (E2E)
- `click` failed on an <svg> button: only HTML elements have `click()`.
- `open --tab t9` opened a new tab when t9 did not exist.
- `wait` capped at 300 s while the bridge allows 600 s.
- `navigate`, `open`, and `forward` re-check the tab and the permission
  after each wait before loading: a closed tab's web view still took the
  load, unseen and with its navigation gate gone.

Forwards
- Reopening a port right after its last tab closed answered `port_busy`
  for about 30 s. A connection the forward closed first (a refused
  client, a host that closed an idle keep-alive) sits in TIME_WAIT and
  fails a plain re-bind. The listener now retries with SO_REUSEADDR, but
  only when nothing accepts on the port: on its own that option also
  binds over another app's wildcard listener. (E2E)
- `--open` could outlive its forward. The mode is now only set for a
  port that is forwarded, and a client is admitted and recorded under
  one lock.
- Clear Browsing Data wiped the forwards' admission cookie for every
  agent tab of the host; each forward then answered 403 until a new tab
  opened. The cookie is re-seeded before the reload.
- A frame gets a forward only inside a page that is itself host
  loopback, so a page from elsewhere cannot spend the host's forwards
  with hidden frames.
- The loopback guard missed URLs with userinfo (`http://[email protected]/`),
  `[::]`, and IPv4-mapped literals. A test now evaluates the rules
  against each spelling and compiles both lists in WebKit.

Tunnels
- While a tunnel's host side opens, the device socket is no longer read,
  so an upload that starts during an SSH dial does not buffer whole. A
  host side that ends before its splice joins closes the device side,
  and a device side that already left gets nothing sent for it.
- A proxy dial abandoned on its deadline is closed; a connection retired
  after a failed open is not adopted again by a late waiter; a bind
  clears its own bookkeeping.

Focus and windows
- `show` and `open --focus` made the tab active inside its window but
  did not raise the window: the reveal went through a tab's terminal,
  which a ⌗ tab does not have, and a new tab's dock never called it.
- A page in an agent tab behind the user's terminal could raise the
  external-link sheet over it. It now can only while on screen.
- An address typed into an agent tab keeps `localhost` as the host's.

Link
- A session that opens a channel but never says hello ends after 45 s
  and retries, instead of sitting in `.connecting` for good.
The simulator shares the Mac's loopback, so a forward there binds the Mac's own port and would tunnel to itself. Serving the fixture on [::1] only gets around that: the forward takes 127.0.0.1, and the host side resolves localhost to ::1 first. Also notes the listener check with lsof and the sun_path limit on MPX_RUNTIME_DIR.
Host Settings ▸ Agent browser gets an INSTALL SKILL button while its
switch is on. It runs `mpx skill install --json` on the host, which
writes the `multiplex` skill into `~/.agents/skills` and
`~/.claude/skills`, and shows the files written or what to fix.

- The skill's text comes from the host's own `mpx`, never from the app,
  so what an agent reads matches the `mpx browser` it calls.
- The command goes through the bridge's road: a throwaway connection, a
  PTY-less login shell, the same search for `mpx`
  (`AgentBrowserLaunch.exec`, now shared). An exec gets sshd's bare PATH
  and would report mpx missing on a host whose agent browser works.
- Only the one JSON answer line is read; a login shell's own output
  around it is ignored. A clap usage error with no answer means an mpx
  from before the command, and the row says to update it.
- It uses the form's credentials like the Signal check, so it works
  before a host is saved. Editing where the form connects retires the
  result; switching agents off removes the button and cancels a run.
- DEBUG: `MULTIPLEX_AUTO_HOST_SETTINGS=agentbrowser` scrolls to the
  section and `skillinstall` also presses the button, since no headless
  tap reaches the chip.

The section's caption now points at the button instead of telling the
reader to run the command; ten new strings carry zh-Hant and ja drafts
(needs_review). The TestFlight bullet mentions the button and still
fits the 4,000-character cap.
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