← Blog

From Raw Terminals to the Agent Controller: Separating Structured Agent State from PTYs

Why scraping ANSI terminal scrollback breaks down for coding agents, and how we built mtagenthost, canonical virtualized history, Command Bay, and Worktree Spaces.

Architecture diagram comparing the PTY Terminal Lane (xterm.js -> /ws/mux -> mt -> mthost) against the Agent Controller Lane (Virtualized Timeline -> /ws/app-server-control -> mt broker -> mtagenthost canonical history -> Codex/Claude/ACP).
Dual-lane runtime model shipped in v8.8–v9.16: terminal sessions stay raw PTYs in mthost while structured agent conversations run in dedicated mtagenthost processes.

Terminal sessions remain the universal fallback for any CLI tool, and in daily practice roughly 90% of ad-hoc work still benefits from a raw shell. Yet once you supervise long-running coding agents—hundreds of tool calls, multi-file diffs, permission prompts, and plan-mode questions—a fixed-width character grid starts fighting the workflow.

Between March 20 and May 28, 2026 (978 commits from v8.8.0 to v9.16.0), we went through an early experiment ("Lens") and converged on a clean architectural separation: mtagenthost and the Agent Controller Session.

Why We Refused to Scrape Terminal Output

The tempting shortcut for adding a rich agent UI to a web terminal is to parse ANSI output from the PTY and guess when a tool started, finished, or asked for approval. We explicitly rejected that model. Screen scraping breaks every time a CLI changes its spinner, truncates long JSON arguments, wraps lines differently at 390 px, or clears the alternate screen.

Instead, we established a strict runtime boundary (documented in docs/AgentControllerSessionDesign.md):

  • PTY sessions belong to mthost and stream raw terminal bytes over /ws/mux.
  • Agent Controller Sessions each run in their own dedicated mtagenthost process, speak the provider's actual structured protocol (Codex app-server, Claude Agent SDK, or ACP), and reduce provider-specific events once into tlbx-owned canonical history items.
  • mt acts purely as a broker. It never maintains a competing reducer. Restarting mt for a web-only update preserves the live mtagenthost process and its entire canonical history in RAM.

Index-Addressed History Instead of Unbounded Chat DOMs

Long agent runs easily exceed thousands of tool calls and megabytes of command output. Naive web chat UIs mount every message in the DOM and grind to a halt—especially on phones.

In v9.4.0 and v9.5.0, we rewrote history transport around two rules:

  1. Count-and-index window fetches: The browser knows the total canonical history count and requests explicit index ranges (startIndex..endIndex) rather than replaying raw provider event streams or relying on backend pixel estimates.
  2. Two-layer navigation: A dedicated progress navigator seeks across the full canonical index space (100 items or 100,000 items behave identically), while the timeline pane remains a native local pixel scroller over a bounded virtualized kernel of ~50–80 materialized rows.
Design Constraint Implementation in mtagenthost + Frontend
Chronological stability Items append monotonically; streaming updates mutate in place without jumping above completed rows
Readable document hierarchy Single centered reading column (up to 54rem), quiet role labels, folded Ran … command disclosures
Crash & update isolation One mtagenthost process per session; survives mt web-only restarts
Provider decoupling All protocol parsing and canonization live in C# (mtagenthost), keeping TypeScript provider-agnostic

Surrounding Ergonomics: Command Bay and Worktree Spaces

Structured history only solves reading. Directing multiple agents in parallel requires better input and repository hygiene:

  • Command Bay (v9.1.0, v9.16.0): A multiline prompt composer with per-session drafts, reusable saved actions, file/image attachments, touch controls, and a prompt queue that works across both terminal and Agent Controller sessions.
  • Spaces-first worktree management (v9.2.0 / v9.3.0): Parallel coding agents editing the same working directory corrupt each other's diffs. Spaces bind sessions to isolated Git worktrees and restore those bindings across restarts.
  • Windows installer hardening & multi-instance installs (v9.10–v9.18): Hardened UAC elevation, typed Service Control Manager operations, remote ad-hoc session bookmarks (v9.13.0), and the first Grok ACP controller integration (v9.14.0).

Milestone Summary