Fix garbled Claude Code TUI in cmux ssh remote workspaces (#6352)
Fixes #6352
Problem
Running the Claude Code CLI (claude) — or any full-screen TUI — inside a cmux ssh remote workspace renders garbled output (stray escape sequences / scrambled frame). Running claude locally is fine, so the bug is specific to the SSH remote path.
Root cause
The remote shell bootstrap (RemoteInteractiveShellBootstrapBuilder.terminalSetupLines, duplicated in CLI/cmux.swift's interactiveRemoteTerminalSetupLines) decided TERM synchronously but installed the bundled xterm-ghostty terminfo in a background job (tic -x - … &):
cmux_term='xterm-256color'
if infocmp xterm-ghostty …; then cmux_term='xterm-ghostty'; fi
export TERM="$cmux_term"
if [ "$cmux_term" != 'xterm-ghostty' ]; then
( … tic -x - … ) & # deferred, non-atomic
fi
On a remote host that lacks the entry:
- The first interactive shell exports
TERM=xterm-256color(the install hasn't happened yet), and - a later shell pass (the re-exec'd login shell, or a concurrent
cmux sshsession sharing$HOME) can selectTERM=xterm-ghosttywhile the backgroundticis still writing the non-atomic terminfo database — so the TUI starts against a missing or half-writtenxterm-ghosttyentry and scrambles its frame.
Fix
Install the bundled terminfo synchronously and atomically, before deciding TERM, in both the app-side bootstrap builder and the CLI script:
- Compile the bundled terminfo into a temp directory on the same filesystem as
~/.terminfo, then move each compiled entry into place with an atomic rename, so a concurrent reader in anothercmux sshsession never observes a partially-written database. - Only select
TERM=xterm-ghosttyafter re-confirminginfocmpresolves the entry, soTERMis neverxterm-ghosttyagainst an absent/partial terminfo. - Graceful fallbacks: direct synchronous compile when
mktempis unavailable;xterm-256colorwhenticis missing. Neither garbles.
Testing
Two-commit red/green regression test in cmuxTests/ShellStartupMatrixTests.swift (remoteTerminalSetupInstallsGhosttyTerminfoBeforeChoosingTerm):
- Commit 1 adds the test (red against the background-install code).
- Commit 2 adds the fix (green).
The test runs the generated setup lines against an isolated $HOME/TERMINFO/TERMINFO_DIRS search path (so the host's own xterm-ghostty can't mask the behavior) and asserts the install resolves xterm-ghostty before TERM is exported.
Validated locally across /bin/sh, /bin/zsh, and bash --posix, including a 12-way concurrency stress (all sessions resolve xterm-ghostty, no corruption, no leftover temp dirs) and nested inside the generated .zshrc heredoc.
Localization
No user-facing strings changed (the diff is shell-script generation only).
🤖 Generated with Claude Code
Need help on this PR? Tag /codesmith with what you need. Autofix is disabled.
Summary by cubic
Fixes garbled TUI output in cmux ssh remote workspaces by installing the bundled xterm-ghostty terminfo synchronously and atomically before choosing TERM. Unifies the setup so app and CLI use the same safe path.
- Bug Fixes
- Use a single implementation in
RemoteInteractiveShellBootstrapBuilder.terminalSetupLines; the CLI now delegates and the duplicate generator was removed. - Compile to a temp dir on the same filesystem as
~/.terminfoand atomically rename into place; whenmktempis missing, use per‑process$HOME/.terminfo.cmux.$$with the same atomic move (no direct writes to~/.terminfo). - Export
TERM=xterm-ghosttyonly afterinfocmpconfirms it; fall back toxterm-256colorwhenticis unavailable. - Add regression test
remoteTerminalSetupInstallsGhosttyTerminfoBeforeChoosingTermto ensure install completes beforeTERMis exported.
- Use a single implementation in