Skip to content

S16-03 - Around the Stream

Purpose: What wb writes to the terminal around the filtered stream, the terminal environment the guest gets, and how guest text in wb’s other output is escaped.

wb itself writes these to the terminal:

  • the window title (“Title”);
  • the reset on exit (“Reset on exit”);
  • the CSI ? 6 n sentinel query of the input drain (“Input drain”).

It writes them only when its standard output is a terminal. The drain also needs standard input to be one.

Standard error with frames of its own passes a filter of its own. When it goes to a terminal and got output, it also gets that filter’s reset. So wb claude -p … > out.txt in a terminal still resets what the guest’s standard error changed, before the summary on standard error.

wb claude -p … > out.txt gets nothing else. Its output still passes the filter (S16-01-filterS16-01-filter: FilterWhere wb filters guest output, how the filter splits it into tokens, and the allowlist of what passes., “Where”).

Before wb stops itself for job control, it writes the reset, drains the input and restores the title too (S05-02-exit-status-and-signalsS05-02-exit-status-and-signals: Exit Status and SignalsWhat wb claude exits with, and what it does with each signal., “Job control”).

  1. wb saves the user’s window title on the terminal’s title stack (CSI 22 t).
  2. It sets its own: wb: <session id>, or the shell id for wb shell.
  3. On exit it restores the saved one (CSI 23 t).

A guest title would reach desktop notifications through the bell (SEC14-no-fake-approvalsSEC14-no-fake-approvals: Approvals cannot be spoofed by the guestApproval prompts are never rendered in the agent's terminal stream.). iTerm2’s Notification Center alerts and Ghostty’s bell-features = title do that.

After the relay ends, and before wb prints anything, it resets what the guest may have changed:

When the guest left pushes on the alternate screen and then returned to the main screen, wb visits the alternate screen to pop them, with synchronized output on so the terminal doesn’t draw the visit.

Then it starts a new line. It doesn’t send ESC c, which can clear the scroll-back.

Without the reset, the guest can print a fake summary in the background color, and hide the real flags that SEC03-no-host-execSEC03-no-host-exec: No host code execution via returned workNothing the agent produces executes on the host without an explicit user action; returned changes to files the host may later execute are flagged. relies on. The summary is advisory, and wb diff is authoritative.

Before it restores cooked mode, wb:

  1. sends a sentinel query that the allowlist never passes: CSI ? 6 n (extended cursor position);
  2. reads and discards input until the reply of that form (CSI ? … R) arrives, or 100 ms pass.

Terminals answer in order. So every reply to a guest query comes first, and a guest CSI 6 n can’t end the drain early. A late reply to a guest query doesn’t reach the user’s shell.

Input to the guest is not filtered.

The guest session gets these host variables, and no others:

  • TERM, TERM_PROGRAM, TERM_PROGRAM_VERSION and COLORTERM. Claude Code picks its sequences from these and from query replies.
  • LANG, LC_ALL and LC_CTYPE. They keep the guest’s character encoding the same as the host’s, also on a host that sets LC_ALL or LC_CTYPE without LANG. LC_ALL also sets the formatting categories, as it does on the host.

These stay out:

  • the other LC_* variables, which set one formatting category each (LC_TIME, LC_NUMERIC, …);
  • TMUX and STY, which make Claude Code wrap sequences in DCS;
  • SSH_*, TERMINFO, LC_TERMINAL and ITERM_SESSION_ID.

The window size crosses as resize messages.

TERM crosses unchanged when the guest has a terminfo entry for it. wb never maps it to another name. With the entry, what Claude Code prints is the same as on the host.

Claude Code doesn’t read terminfo. Programs in the session such as less, vim and top look TERM up in it. TERMINFO names a path on the host, and doesn’t cross.

The guest looks TERM up in the terminfo database of its image (S06-02-image-buildS06-02-image-build: Image BuildHow wb image build makes a guest image: the build VM, its two provisioning stages, terminfo, sealing, and storage., “Terminfo”). That database has ncurses’ entries, and the names Ghostty and kitty set: xterm-ghostty and xterm-kitty. The less, vim and top that macOS ships can’t read the kitty entries (“macOS’s ncurses” below).

wb-guestd checks TERM before it starts a session of a project user. When neither the image’s database nor the one macOS ships has an entry of that exact name, the session gets TERM=xterm-256color instead (I474I474 (closed): wb shell: the host's TERM has no terminfo in the guest (xterm-ghostty)wb shell passes the host's TERM to the guest unchanged (#471, S06-09-session-lifecycle).). An entry is a regular file in a directory of the database, and a symbolic link doesn’t count. A session without TERM gets none. The root shell of wb vm admin isn’t a project session, and gets the host’s TERM unchanged.

A TERM that isn’t a terminfo name gets xterm-256color too: one with a byte other than a letter, a digit, ., _, + and -, one that doesn’t start with a letter or a digit, or one over 128 bytes. wb-guestd never looks a value of that kind up, and a TERM can’t name a path. Each replacement is logged with its rule, shell-term-unknown or shell-term-malformed, and the value the host sent. It doesn’t refuse the session.

/usr/bin/less, vim and top link macOS’s own ncurses 6.0. It can’t read an entry in the extended number format, which tic from ncurses 6.1 or later writes for an entry with a number above 32767. kitty and xterm-kitty (pairs#0x10000) are among them.

  • Those programs report an unknown terminal for kitty.
  • Programs that link Homebrew’s ncurses read the entry.
  • For a name macOS ships, such as xterm-256color, macOS’s ncurses skips the file it can’t read, and uses its own copy.

This was measured on a macOS 27 host with ncurses 6.6 (I83I83 (closed): Guest terminfo for the host TERM passed into the sessionS05-cli ("Terminal stream", #81) passes the host's TERM into the guest session, because Claude Code picks its sequences from it.). I203I203 (closed): Guest terminfo: xterm-kitty unreadable by macOS's own ncursesSo a kitty user running less or vim from /usr/bin in wb shell still gets no terminfo entry. decides whether xterm-kitty gets an entry macOS can read.

wb prints text the guest influenced in other places too:

  • the id in the window title;
  • the session summary and its link list;
  • the wraith box: line of a failure;
  • wb diff, wb sessions, wb approve, wb audit, wb learn report and wb status.

There it never passes a code point of the escaped set:

  • a control: Unicode category Cc, which holds ESC, and so every escape sequence;
  • a format character (Cf), such as the bidirectional controls and the zero-width characters;
  • a line or paragraph separator (Zl, Zp);
  • a Default_Ignorable_Code_Point, such as the tag characters, the variation selectors and the Hangul fillers.

wb diff, logs and audit records show them escaped, where a reviewer sees them. S09-11-auditS09-11-audit: AuditWhat the audit log records, the budgets that bound it, and what happens when the log can't be written. gives the form for audit records. Other output removes them (SEC10-auditSEC10-audit: AuditEvery egress decision, approval, and returned change is recorded on the host in a persisted log the guest cannot alter., SEC14-no-fake-approvalsSEC14-no-fake-approvals: Approvals cannot be spoofed by the guestApproval prompts are never rendered in the agent's terminal stream.).

The wraith box: line speaks for Wraith Box, after the reset. So wb-hostd never passes on a failure message from wb-guestd as the guest wrote it. It words a failure by its rule itself, or quotes the guest’s message, stripped, as the guest reported: "<message>". A " or \ in the message gets a backslash in front, so the guest can’t end the quote and write on as Wraith Box.

Write around the stream only to a terminal

Section titled “Write around the stream only to a terminal”

What wb writes around the stream needs standard output to be a terminal, and the drain also standard input.

wb resets the terminal on exit.

wb-guestd gives a session xterm-256color for a TERM the guest lacks. Without it, clear reports an unknown terminal, and zsh’s line editor draws a typed line twice.

Status: Draft