Fix garbled Claude Code TUI in cmux ssh remote workspaces (#6352)

Merged
AU
5mo ago
+136-42

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 ssh session sharing $HOME) can select TERM=xterm-ghostty while the background tic is still writing the non-atomic terminfo database — so the TUI starts against a missing or half-written xterm-ghostty entry 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 another cmux ssh session never observes a partially-written database.
  • Only select TERM=xterm-ghostty after re-confirming infocmp resolves the entry, so TERM is never xterm-ghostty against an absent/partial terminfo.
  • Graceful fallbacks: direct synchronous compile when mktemp is unavailable; xterm-256color when tic is 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


View with Codesmith Autofix with Codesmith
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 ~/.terminfo and atomically rename into place; when mktemp is missing, use per‑process $HOME/.terminfo.cmux.$$ with the same atomic move (no direct writes to ~/.terminfo).
    • Export TERM=xterm-ghostty only after infocmp confirms it; fall back to xterm-256color when tic is unavailable.
    • Add regression test remoteTerminalSetupInstallsGhosttyTerminfoBeforeChoosingTerm to ensure install completes before TERM is exported.