← Blog

Persistent PTYs in the Browser: Why We Split the Web Server from the Terminal Host

From the initial ConPTY prototype to isolated mthost sidecars, ordered binary WebSockets, and web-only updates that leave running shells untouched.

Architecture diagram showing Browser Client with xterm.js 6 connected over binary /ws/mux to Kestrel web server mt and isolated mthost PTY sidecar processes.
Process boundary introduced between v2.0 and v5.3: each terminal runs inside its own mthost process so web-server updates and restarts preserve live shells.

When we started building what is now tlbx in late December 2025, the operational problem was simple to state and surprisingly brittle to get right: running long-lived AI coding agents and build shells on a workstation, then leaving that desk without killing or losing those sessions.

SSH solves remote shell transport, and tmux keeps processes alive on Unix. Neither gives you a modern browser workspace with native Windows ConPTY support, drag-and-drop image attachments for CLI agents, multi-viewer layout coordination, and zero-downtime web UI upgrades. Within the first 258 commits (v1.1.0 through v5.3.0), almost every major architectural invariant of the terminal runtime was forged by running into real process-lifetime failures.

The First Mistake: Coupling PTYs to the Web Server

In v1.x, the web host spawned pseudo-terminals directly inside the server process. That worked until the first self-update: restarting the HTTP server tore down every active shell, compiler watch loop, and coding agent session. On Windows, running the web server as a system service introduced a second constraint—user shells must run under the configured interactive user identity (v1.3.0 terminal user de-elevation), separate from the service supervisor.

On December 30, 2025 (v2.0.0), we split the runtime into two process roles:

  1. mt (the web server): A Kestrel HTTP + WebSocket server that owns authentication, static assets, session registries, and browser fanout.
  2. mthost (the PTY sidecar, originally mm-con-host / mmttyhost): One dedicated OS process per terminal session wrapping Windows ConPTY or POSIX forkpty, communicating with mt over local Named Pipes (Windows) or Unix Domain Sockets (Linux/macOS).

Separating the web server from the PTY host turns an update into a quick IPC reconnect rather than a session teardown.

By v2.6.0 and v4.6.0, the split became the foundation for web-only updates: mt can stop, swap its binary, restart, and reattach to every surviving mthost process while shells and coding agents keep producing output in the background.

Guaranteeing In-Order Output for Full-Screen TUIs

A plain shell printing line-oriented logs forgives minor timing glitches. Full-screen TUI applications—Claude Code, Codex, vim, htop—do not. If two WebSocket frames arrive out of order, or if a browser connects mid-escape-sequence, cursor addressing breaks and the screen corrupts.

Three concrete changes between v2.5.0 and v5.3.0 locked down the terminal output path:

Version Problem Observed Architectural Fix
v2.5.0 TUI escape sequences corrupted under concurrent async writes Guaranteed strictly ordered terminal output delivery across IPC and WebSocket fanout
v3.2.0 Multi-viewer tabs fought over terminal dimensions Introduced a dimension-aware output protocol so replayed frames carried their grid context
v3.4.0 / v5.2.0 Opening a session dropped bytes emitted between initial HTTP load and socket attach Buffered WebSocket frames until xterm readiness and moved buffer refresh onto the ordered WebSocket channel
v5.3.0 High-volume build logs grew memory unbounded Added bounded queues, backpressure flow control, and dimension-aware scrollback buffers

Rewriting the Frontend for High-Frequency Binary Streams

On January 2, 2026 (v4.0.0–v4.4.0), we replaced the initial script prototype with a modular TypeScript frontend bundled by esbuild and upgraded to xterm.js 6.0.0 with WebGL rendering, in-terminal search (Ctrl+F), clickable URLs, and a resizable sidebar.

Keeping the frontend in vanilla TypeScript without a heavy component framework was deliberate. Terminal rendering is an imperative, high-frequency hot path: binary frames arrive on /ws/mux, decode into bytes, and write straight into xterm's buffer. Keeping DOM chrome decoupled from that stream avoided framework reconciliation overhead during noisy builds.

First-Class Ergonomics for Coding Agents

Even in the first week, real usage made one pattern obvious: most sessions were running CLI coding agents rather than hand-typed shell commands. Developers constantly paste UI screenshots into Claude Code or Codex to describe layout bugs, which breaks completely over SSH.

In v4.5.0 and v5.4.0, we added direct browser drag-and-drop and Ctrl+V / Cmd+V clipboard image paste: the browser uploads the screenshot to the host over authenticated HTTP, writes it to a scoped workspace path, and inserts the local path directly into the PTY input stream. From the agent's perspective, the file already exists on the machine where the repository lives.

Milestone Summary

  • Commits in phase: 258 (2025-12-30 to 2026-01-05)
  • Key tags: v2.0.0, v3.0.0, v4.0.0, v5.3.0
  • Durable result: Per-session mthost isolation, ordered binary /ws/mux delivery, web-only restarts, and clipboard image paste—still the backbone of tlbx's self-hosted web terminal today.