fix(attach): own the terminal state across attach and detach#23
Merged
Conversation
The native attach client was a verbatim byte bridge with no terminal
ownership: the host replay opens with \x1b[2J on whatever screen the
terminal shows, and detach restored only termios. Attaching from the TUI
therefore painted the session over the user's PRIMARY screen
(tea.ExecProcess leaves the alt screen before the child runs), so
quitting uam revealed session output instead of the shell; and any modes
the agent enabled mid-attach (mouse tracking, kitty keyboard, bracketed
paste, hidden cursor, charsets) stayed active on the real terminal,
corrupting the TUI that resumes after detach.
Own the window the way `tmux attach` did:
- on a tty, enter our own alternate screen (\x1b[?1049h) before the
replay, so the session never touches the primary screen
- on every exit path (detach, session end, and SIGINT/SIGTERM/SIGHUP,
now handled like a detach) emit a mode-reset suffix - kitty pop +
zero, mouse modes and focus reporting off, bracketed paste off,
synchronized output off, DECSTR, keypad and charset reset, cursor
visible - then leave the alt screen, then restore termios, all under
one sync.Once
- stop and drain the host-to-terminal pump before the restore, so
bytes still buffered from the socket land inside the alt screen,
never on the restored primary
Piped (non-tty) attaches stay byte-identical, asserted by test. Verified
end to end on a real PTY pair through the session host.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Problem
Two rendering bugs in the native attach client (post-#22):
\x1b[2Jon the primary buffer (Bubble Tea leaves its alt screen beforetea.ExecProcessruns the child), destroying the user's shell content; quitting uam then revealed session output instead of the shell.tmux attachprevented both by bracketing the session in its own alternate screen and resetting modes on detach.Fix
sync.Oncerestore on every exit path (detach, session end, and external SIGINT/SIGTERM/SIGHUP — now handled like a detach): mode-reset suffix → leave alt screen → restore termios.Test plan
TestAttachOwnsTerminalStateOnTTY), drain-before-restore under a 2000-line output burst (TestAttachDetachDrainsPumpBeforeScreenRestore); non-tty assertion added to the piped test.-racegreen; vet/lint/gosec/govulncheck clean.