Skip to content

X19 - Terminal stream filtering

Brief: B30-terminal-filterB30-terminal-filter: Can wb filter Claude Code's terminal output without breaking it?Yes, with conditions, for Ghostty and Terminal.app pending a check by eye.

Question: Which terminal escape sequences does Claude Code emit, and can wb drop the ones that act on the host (clipboard writes, file transfer, terminal multiplexer control sequences) without visible damage in the common macOS terminals? (I30I30 (closed): X19-terminal-filter: Terminal stream filteringX19-terminal-filter (added to S11-verification-and-spikes in #14))

Answer: Yes, with conditions, for Ghostty and Terminal.app as far as a headless emulator shows, and as the check by eye in I62I62 (closed): X19-terminal-filter: visual check in real terminalsSplit from #30 (X19-terminal-filter) during triage on 2026-10-04. confirmed. In seven recorded sessions Claude Code 2.1.289 emitted 56 distinct controls and sequences. A short allowlist passes them unchanged except four kinds, plus file:// links within OSC 8: OSC 52 (clipboard), two kinds of desktop notification, and a kitty graphics query. With those dropped or rewritten, every screen of every recording is unchanged on replay in a headless terminal emulator, except that the file:// links are no longer links. The recordings imitated each terminal’s answers to Claude Code’s queries. The iTerm2 and VS Code profiles stay unchecked in the real terminals, because I62I62 (closed): X19-terminal-filter: visual check in real terminalsSplit from #30 (X19-terminal-filter) during triage on 2026-10-04. covered only Ghostty and Terminal.app. In those two, by eye, the filter caused only the losses listed below. Ghostty makes a bare path clickable by itself, which the filter cannot stop. The filter spends about 12 ms of CPU per MB of Claude Code output. The conditions are below.

All on the host (macOS, Apple M2), with the installed Claude Code 2.1.289 and the maintainer’s own login, model Haiku 4.5. No Wraith Box VM was involved. The question is what Claude Code writes to a PTY, and that depends on the terminal it believes it talks to, not on the machine it runs on.

  1. Record. cmd/record runs claude in a PTY behind a headless terminal emulator and writes every byte it prints to an asciicast file. It drives Claude Code from a script (scripts/session.txt): folder trust dialog, a Markdown answer with a table, code block and link, a Bash tool call whose output holds OSC 52, OSC 8 and color, Write and Edit with their permission prompts and diffs, a bracketed paste, focus out and in, mode cycling with Shift+Tab, a resize, a 300-line answer, /cost, and exit. Claude Code picks its sequences by terminal, so the recorder sets TERM, TERM_PROGRAM and related variables and answers queries (device attributes, mode reports, XTVERSION, the kitty keyboard query) the way it expects Ghostty, Terminal.app, iTerm2 or the VS Code terminal to answer. No real terminal answered, so each column below is a profile, not the terminal itself. Ghostty also ran with the full-screen renderer (CLAUDE_CODE_NO_FLICKER=1). Two short scripts cover first-run onboarding without credentials, and /copy (with a stub pbcopy first in PATH).
  2. Classify. cmd/classify splits the output into tokens with the filter’s own tokenizer and counts each kind of sequence.
  3. Static check. The strings in the Claude Code binary name the OSC numbers it knows, so sequences it can emit but did not during the recordings are listed too.
  4. Filter and compare. cmd/compare replays each recording into two emulators, one fed the raw output and one fed the filtered output, and compares their screens, text and style, after every read from the PTY. A negative control that also strips colors from the filtered side makes 71% to 99% of the screens differ per recording: 15 of 21 in onboarding-ghostty, 97 of 109 in copy-ghostty, and over 97% in each of the five full sessions. So the comparison does see style. Its output is results-compare-control.md on the spike branch.
  5. Cost and fuzzing. Go benchmarks on the recordings and on synthetic worst cases, and a fuzz target.

Counts over seven recordings, 818 KB of output, of which 335 KB is text. The Ghostty profile column adds the full-screen, onboarding and /copy recordings.

Sequence Meaning Ghostty profile Terminal.app profile iTerm2 profile VS Code profile Filter
LF, CR, SI line feed, carriage return, back to the normal character set 13335 22072 21255 11755 pass
ESC 7, ESC 8, ESC ( B save and restore cursor, character set 39 12 12 12 pass
CSI A … CSI T, CSI r cursor movement, erase, scroll, scroll region 15838 9798 9271 8048 pass
CSI m colors and text style (SGR) 6974 4616 4270 3725 pass
CSI ? 2026 h/l synchronized output 3330 2150 1648 pass
CSI ? 25, 1004, 2004, 2031, 1016 h/l cursor visible, focus events, bracketed paste, color scheme reports, pixel mouse off 79 23 23 23 pass
CSI ? 1000, 1002, 1003, 1006 h/l mouse reporting 72 8 8 8 pass
CSI ? 1049 h/l alternate screen (full-screen renderer only) 2 pass
CSI > 5 u, CSI < u, CSI ? u kitty keyboard protocol: push, pop, query 58 1 21 1 pass
CSI > 4 ; 2 m, CSI > 4 m xterm modifyOtherKeys 31 2 11 2 pass
CSI c, CSI > 0 q, CSI ? … $ p, CSI 16 t queries: device attributes, terminal version, mode state, cell size in pixels 32 4 10 10 pass
OSC 0 window title (✳ Claude Code) 74 41 32 39 pass
OSC 8 to https://, and OSC 8 ;; hyperlink, and the end of one 7 and 26 2 and 10 2 and 9 pass
OSC 8 to file:// link to a local path: the workspace, and a path printed by a tool 19 8 7 drop
OSC 9 ; 4 progress indicator 33 pass
OSC 777 ; notify desktop notification, Ghostty profile (“Claude needs your permission”) 4 rewrite to BEL
OSC 9 desktop notification, iTerm2 profile, same text 2 rewrite to BEL
OSC 52 write the clipboard (/copy) 1 drop
APC G … a=q kitty graphics support query 4 1 1 drop

Claude Code adapts to the terminal. For the Terminal.app profile it left out hyperlinks, notifications, synchronized output, and the kitty keyboard protocol. The filter passed that recording byte for byte.

The binary also knows these, which the recordings did not trigger: OSC 1 and 2 (title), OSC 4, 10, 11 and 12 and their resets 104 and 110 to 112 (colors), OSC 7 (working directory), OSC 99 (kitty notification), OSC 133 (prompt marks, written as A;redraw=0, C and D), OSC 1337 (iTerm2) and OSC 21337 (tab status). Inside tmux or screen (detected from TMUX and STY) it wraps sequences in a DCS pass-through. None of those is needed for the TUI to work.

Input for I37I37 (closed): S03-requirements, S05-cli: treat the host terminal stream as a boundarywb claude relays the guest's PTY output in raw mode to the user's terminal emulator (S05-cli).. The table describes the filter that was measured, and the list at the end says where I37I37 (closed): S03-requirements, S05-cli: treat the host terminal stream as a boundarywb claude relays the guest's PTY output in raw mode to the user's terminal emulator (S05-cli). tightens it. The filter passes a token only if it matches a rule here, drops it otherwise, and counts every drop by rule. A dropped sequence is dropped whole, including its ESC, so the terminal’s parser is back in its normal state after each token the filter passes.

Class Passes Everything else
Text valid UTF-8 without C1 controls invalid bytes become U+FFFD; C1 code points (U+0080 to U+009F) dropped
C0 controls BEL, BS, HT, LF, CR, SI dropped (ENQ, SO, VT, FF, NUL, DEL, …)
ESC sequences 7, 8, M, D, E, =, >, ( B, ( 0, ) B, ) 0 dropped, including ESC c (full reset)
CSI, general parameters only digits, ; and :, with no cap on a value or on the count; at most 64 bytes dropped
CSI, screen cursor movement and position (A to H, f, `, a, d, e), erase (J, K, any parameter), insert and delete (@, L, M, P, X), repeat (b), scroll (S, T), scroll region (r), tabs (I, Z, g), SGR, save and restore cursor without parameters
CSI, modes ? h/l for 1, 7, 12, 25, 47, 1000, 1002, 1003, 1004, 1006, 1016, 1047, 1048, 1049, 2004, 2026, 2027, 2031; ? … $ p for any mode other modes dropped
CSI, keyboard kitty keyboard (> u, < u, = u, ? u), > m with any parameters (modifyOtherKeys), cursor style (SP q), soft reset (! p)
CSI, queries c, > c, 5 n, 6 n, > q, 14 t, 16 t, 18 t
CSI t title stack push and pop (22, 23) window moves, resizes, iconify and title reports dropped
OSC 0, 1, 2 title of at most 512 bytes, valid UTF-8, no controls dropped
OSC 8 parameters of at most 256 printable ASCII bytes, with an http:// or https:// URL of at most 2048 printable ASCII bytes, or the closing OSC 8 ;; file:// and other schemes dropped
OSC 9 ; 4 progress with numeric parameters
OSC 9, 99, 777 rewritten to one BEL
OSC 10, 11, 12 the query ? only setting a color dropped
OSC, other dropped: 52 (clipboard), 1337 (iTerm2 files and clipboard), 7, 4, 104, 110 to 112, 133, 21337, everything else; at most 16 KB is buffered
DCS, APC, PM, SOS dropped whole: DCS pass-through and XTGETTCAP, kitty graphics (whose t=f mode asks the terminal to read a file by its path)

Queries pass because the terminal’s reply goes to the guest, and Claude Code sends them to find out what the terminal supports. The replies tell the guest about the host: the terminal’s name and version (CSI > q), its colors (OSC 10 to 12), the window size in cells and pixels (CSI 14 t, 16 t, 18 t), and the state of its modes (DECRQM). No allowed query returns clipboard or file contents. OSC 52 with ? (read the clipboard) is dropped with the rest of OSC 52, and so are the title report CSI 21 t and DECRQSS, which is a DCS.

Sequences Claude Code knows but did not emit are left off, even harmless ones such as OSC 133 prompt marks and color resets: the list holds what was seen, and a drop count shows when it is too short. OSC 10 to 12 queries are the exception, because the binary holds the background color query. OSC 9 ; 4 progress passes because it shows only a percentage in the tab, which I37I37 (closed): S03-requirements, S05-cli: treat the host terminal stream as a boundarywb claude relays the guest's PTY output in raw mode to the user's terminal emulator (S05-cli). may revisit.

Every screen, after every read, raw against filtered:

Recording Terminal Reads Differing screens Of which only links Dropped or rewritten
session-ghostty Ghostty profile 922 393 393 7 file:// links, 2 notifications, 1 graphics query
session-ghostty-fullscreen Ghostty profile, full screen 906 180 180 12 file:// links, 2 notifications, 1 graphics query
copy-ghostty Ghostty profile 109 0 0 1 OSC 52, 1 graphics query
onboarding-ghostty Ghostty profile, no login 21 0 0 1 graphics query
session-apple Terminal.app profile 1271 0 0 nothing
session-iterm iTerm2 profile 1199 660 660 8 file:// links, 2 notifications, 1 graphics query
session-vscode VS Code profile 958 377 377 7 file:// links, 1 graphics query

Every difference is a file:// link that is plain text after filtering. The screens show the same characters, colors, and cursor. The links point at guest paths (the workspace path in the status line, and a path a tool printed), which on the host name a different file or none.

  • File paths are not clickable.
  • /copy reports “Copied to clipboard”, but the host clipboard is unchanged.
  • “Claude needs your permission” arrives as a bell, not as a desktop notification with text.
  • Claude Code can’t use kitty graphics (it only queried support, and didn’t show an image in these sessions).

Single core, Apple M2, Go 1.27.1, input in 32 KB reads:

Input Throughput CPU per MB
The seven recordings 85 MB/s 12 ms
Plain ASCII text 847 MB/s 1.2 ms
Dense colors (CSI m every 2 characters) 137 MB/s 7.3 ms
OSC 52 payloads of 16 KB, all dropped 363 MB/s 2.8 ms
Random bytes 29 MB/s 34 ms

The numbers are from results-bench.txt on the spike branch.

A scripted Claude Code session printed 108 to 189 KB in two to five minutes, so the filter spends a few milliseconds per session. Random bytes, as from a cat of a binary file in wb shell, are the worst case, at 29 MB/s, about 34 ms per MB. Memory is bounded: at most 16 KB of an unfinished OSC or 64 bytes of a CSI is buffered, and a longer one is dropped whole.

The real filter parses guest bytes and needs a fuzz target (S11-verification-and-spikesS11-verification-and-spikesthe filter's fuzz properties, including one that replays the filtered output and the exit reset into a headless emulator and checks it is back in its initial state, and a conformance case that prints every dropped seq…). The spike has one (FuzzFilter). It checks that the output contains only tokens the allowlist passes as they are, that filtering twice gives the same bytes as filtering once, that where a read splits the input does not change the output, and that no OSC 52, OSC 1337, DCS, or APC introducer survives. Three runs of one to two minutes passed. Only the last one has a committed log: 17.5 million inputs in two minutes (results-fuzz.txt on the spike branch). A short run before them found a bug: text in front of an incomplete UTF-8 character was held back until the next read. The split check caught it, and it is fixed in the spike.

The headless emulator is not the user’s terminal. In I62I62 (closed): X19-terminal-filter: visual check in real terminalsSplit from #30 (X19-terminal-filter) during triage on 2026-10-04., on 2026-10-16, the maintainer ran the relay from the spike branch in Ghostty 1.3.1 and Terminal.app on macOS 27, with Claude Code 2.1.296 and Haiku 5.5, in a scratch repository. iTerm2 and VS Code are not installed, and their profiles remain unchecked. Each terminal ran the hostile script through the filter, then the same Claude Code session twice, unfiltered (-raw) and filtered. Ghostty also ran the full-screen renderer (CLAUDE_CODE_NO_FLICKER=1), filtered.

Terminal window
GOWORK=off go run ./cmd/relay -log drops.txt -- claude

The session covered the folder trust dialog, a Markdown answer with a table, code block and link, an Edit with its permission prompt and diff while the window was in the background, a Bash tool call, Shift+Enter in the prompt, a multi-line paste, mode cycling with Shift+Tab, a 300-line answer while resizing the window, /copy, focus out and in, /cost, and the terminal state after exit.

Colors, the Markdown table, code highlighting, the diff, the permission dialogs, Shift+Enter, the pasted-text placeholder, mode cycling, redraw during resize, focus, and the terminal after exit looked and behaved the same filtered as unfiltered in both terminals. The full-screen renderer in Ghostty worked too, mouse scrolling included.

  • Hostile script. Filtered, both terminals showed only the text, the red color and the https:// link, and rang two bells. Unfiltered, Terminal.app printed the kitty graphics payload and the tmux pass-through as text (Ga=T,t=f;L2V0Yy9ob3N0cw==, tmux;), because it does not parse APC or DCS. So the filter also removes garbage that Terminal.app would show.
  • Notifications. In Ghostty, the filtered permission prompt showed as a badge on the Dock icon (the BEL), not as a desktop notification with text. Terminal.app showed nothing in either run, because Claude Code sends it no notification.
  • Links. The file:// link to calc.py that Claude Code printed was dropped (osc8-scheme).

The drop logs, notes, and screenshots are not in the repository. The screenshots show the maintainer’s desktop.

This result leaves the specs as they are. The items below are input for I37I37 (closed): S03-requirements, S05-cli: treat the host terminal stream as a boundarywb claude relays the guest's PTY output in raw mode to the user's terminal emulator (S05-cli)., which writes the spec text. F1 to F11 refer to the security review of PR66PR66 (merged): docs(spikes): X19-terminal-filter result, yes with conditionsAnswer: yes, with conditions, for Ghostty and Terminal.app, as far as a headless emulator can show., whose cases passed this spike’s filter unchanged.

The boundary and the filter:

Tightening the allowlist:

Around the stream:

Elsewhere:

  • One version of Claude Code, 2.1.289. A new version can start using a sequence the list drops. The filter fails closed, and the drop counts in the session summary are how a missing rule shows up.
  • The terminals’ answers to queries were imitated, not taken from the real terminals. A real Terminal.app may answer differently and change what Claude Code emits. I62I62 (closed): X19-terminal-filter: visual check in real terminalsSplit from #30 (X19-terminal-filter) during triage on 2026-10-04. ran Claude Code in the real Ghostty and Terminal.app, and the filter caused only these losses there. The iTerm2 and VS Code profiles stay unchecked.
  • The input direction (keys and query replies from the terminal to the guest) was not filtered and is out of scope here.
  • The recordings ran with the maintainer’s Claude Code settings, which allow some tools without a prompt, so not every tool call showed a permission dialog. The dialogs that did show used the same sequences.

Branch spike/x19-terminal-filter, commit e1adf23: recorder, classifier, filter with tests and fuzz target, replay comparison, relay, the seven recordings, and the raw result tables (results-inventory.md, results-compare.md, results-compare-control.md, results-bench.txt, results-fuzz.txt).

Status: Answered