Skip to content

PTY and DEC Terminal Queries

zmux.NativeSession provides a real PTY and a raw byte ring buffer, but it does not emulate a terminal state machine. When Claude Code starts, Ink probes the terminal for capabilities, cursor position, and viewport dimensions. If nothing answers those queries, Ink blocks and SessionStart never fires.

claude (Ink TUI) zmux reader + terminal.zig driver main loop ESC[c ESC[>c ESC[6n ESC[>q ESC[18t DecDecoder.feed() pure scan, zero PTY writes append under write_mutex flushPendingToPty() stack 512 B buffer, 0 allocs ESC[?1;2c · ESC[>0;0;0c · ESC[1;1R · ESC P>|claude-p ESC\ · ESC[8;40;120t Ink boots & renders
Ink probes the terminal at startup. The reader thread scans raw bytes without locking zmux, queues replies across write_mutex, and the main loop writes them back.

terminal.DecDecoder.feed(allocator, chunk, out) scans each incoming PTY chunk for CSI sequences (0x1b '[', optional private '>', parameter bytes 0x30..0x3f, intermediate bytes 0x20..0x2f, final byte) and appends any matching reply to out. The driver keeps one DecDecoder per PTY on its shared state.

PROBE INCOMING FROM INK (PTY OUT) REPLY FROM terminal.zig (PTY IN) DA1 ESC [ 1B 5B [0] opt 30 c 63 ESC [ 1B 5B ? 1 ; 2 VT100+AVO c 63 DA2 ESC [ > 1B 5B 3E [0] opt 30 c 63 ESC [ > 1B 5B 3E 0 ; 0 ; 0 model/fw c 63 DSR (CPR) ESC [ 1B 5B 6 36 n 6E ESC [ 1B 5B 1 ; 1 row 1 col 1 R 52 XTVERSION ESC [ > 1B 5B 3E [0] opt 30 q 71 ESC P >| DCS header claude-p ASCII name ESC \ ST 1B 5C XTWINOPS ESC [ 1B 5B 18 31 38 t 74 ESC [ 1B 5B 8 ; 40 ; 120 40 rows × 120 cols t 74
Byte-level wire grammar for all five queries answered by src/terminal.zig. Purple tiles mark escape introducers (ESC / CSI / DCS), blue tiles mark parameters, and green tiles mark final terminators.

All non-matching bytes and non-query CSI sequences are ignored. DecDecoder carries an incomplete trailing ESC or CSI (up to 32 bytes) into the next feed call, so a query split across two PTY reads is still answered. It is not thread-safe; only zmux’s reader thread touches it. respondToDecQueries is a one-shot form that drops an incomplete trailing sequence; do not use it on a live PTY stream.

The window-size reply ESC [ 8 ; rows ; cols t reports the decoder’s configured rows and cols (default 40 × 120, matching the PTY size).

An early prototype considered embedding libghostty-vt to maintain a screen grid and read replies off the rendered terminal. Once Stop hooks and JSONL transcript parsing replaced screen scraping, claude-p needed only two operations on PTY bytes:

  1. Answer the five startup probes (src/terminal.zig).
  2. Strip CSI, OSC, and DCS escapes into a stack buffer (driver.stripCsiInto) so the startup dialog handler can inspect plain text and the ❯ cursor.

If a future Claude Code release adds a new boot probe, the failure surfaces as SessionStartTimeout (visible under --debug), and the fix is one switch arm in src/terminal.zig.