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.
Wire grammar (src/terminal.zig)
Section titled “Wire grammar (src/terminal.zig)”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.
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).
Why no full VT emulator?
Section titled “Why no full VT emulator?”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:
- Answer the five startup probes (
src/terminal.zig). - 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.