Repository navigation
fix(attach): restore full terminal state on reattach - #29
Merged
Merged
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
serabi
added a commit
that referenced
this pull request
Nov 7, 2026
Port PR metadata and size checks from awesoMux and add Zig 0.16 unit and automation checks. Preserve human comments and checklist confirmations when workflows reconcile their own comments. Add the approved fork-specific merge-risk skill. Defer attach CI until the regression harness lands through PR #29.
serabi
added a commit
that referenced
this pull request
Nov 7, 2026
Add Zig 0.16 CI, PR metadata and size checks, and fork-specific merge-risk guidance. Attach regression CI follows PR #29.
serabi
force-pushed
the
fix/preserve-unresponsive-sessions
branch
from
November 7, 2026 04:48
ffa16fa to
6c6ab41
Compare
serabi
force-pushed
the
fix/attach-terminal-state
branch
from
November 7, 2026 04:50
cc24771 to
9a9aea6
Compare
This comment has been minimized.
This comment has been minimized.
External contributor checklistThanks for contributing. A maintainer will handle the trusted checks and final merge.
|
Reattach cleared the receiver's screen right after replaying history, so the last screenful never reached its scrollback. Push those rows out first, counting the attaching client's rows because a Watch or follower can be taller than the daemon's grid. Replay history as logical lines so it reflows after a resize. The viewport stays hard-wrapped because the restored cursor assumes the source row layout. Sessions with DECAWM off stay hard-wrapped throughout so a receiver still in that mode cannot truncate a line.
Restore primary history, viewport, and cursor before entering alt. Normalize receivers onto primary and emit one alt transition so mode aliases cannot overwrite the saved cursor. Exercise real-daemon snapshots with fresh and already-alt receivers, taller clients, mode aliases, and a 10,000-line primary buffer.
A receiver reattached without RIS could keep DECAWM off and truncate unwrapped history, so turn wrapping on first when the source uses it. Entering 1049 saved the receiver's origin mode instead of the primary screen's saved one. Set that mode before entering, then clear it so the viewport replay applies the alternate screen's own mode.
serabi
force-pushed
the
fix/preserve-unresponsive-sessions
branch
from
November 7, 2026 05:15
6c6ab41 to
3f06807
Compare
serabi
force-pushed
the
fix/attach-terminal-state
branch
from
November 7, 2026 05:15
9a9aea6 to
2c8f9f3
Compare
Code Review Completed! 🔥The code review was successfully completed based on your current configurations. Kody Guide: Usage and ConfigurationInteracting with Kody
Current Kody ConfigurationReview OptionsThe following review options are enabled or disabled:
|
serabi
changed the base branch from
fix/preserve-unresponsive-sessions
to
main
November 7, 2026 05:24
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.
Why
Restore terminal contents and settings when a client reattaches. Keep scrollback complete and restore the primary screen after a full-screen app exits.
What's Included
UI / UX
Not applicable. This changes terminal replay in the backend. Earlier awesoMux and synthetic phone-watch results were from the pre-rebase revision; no new app or physical-phone proof is claimed here.
Notes
Models: Claude Opus 5.5, GPT-6.1 Sol
Merge risk: medium. Replay changes screen contents and terminal modes. The real-daemon regression covers Ghostty, but other terminals and the rebased revision in awesoMux remain unchecked. Recovery: revert for future snapshots; already connected terminals and running daemons can keep their state.
Current macOS checks with Zig 0.16: formatting, unit tests, build, and terminal replay tests pass.
git diff --checkpasses. Withtrashunavailable on PATH, all 96 attach cases pass. A separate forced-failure run injects both an attach assertion and a cleanup error: cleanup removes the owned fixture, reports its error separately, and the final traceback keeps the original assertion. The cleanup review thread remains open and unanswered for Sarah. The earlier 3 reattach cases passed at9a9aea6; they were not rerun for this harness/docs change. Hosted CI for the new head is reported separately. Remaining limits: very large snapshots grow with scrollback; clients must enforce their own size limits. Long viewport lines do not reflow. Tab stops past the source width are not restored. A real iPhone and tab stops in awesoMux itself remain untested.After this lands, adding the attach regression to zmx CI and updating awesoMux's submodule pin need separate approval. Keep
awesomux-pin-cc24771andawesomux-pin-ffa16fa. Merge with a merge commit and keep the branch.Open in [code]smith · Need help on this PR? Tag
@codesmith-botwith what you need.fix(attach): restore full terminal state on reattach
Summary
Reattaching to a session previously restored only the visible screen plus a narrow set of extras. This PR makes the serialized snapshot a faithful reproducer: scrollback, primary and alternate screens, cursor shape, palette overrides, tab stops, wrap mode, origin mode, and retained margins all survive a detach/attach cycle, and the snapshot is now laid out for the attaching client's row count rather than the daemon's.
What changed
serializeTerminalState(alloc, term, client_rows)— newclient_rowsparameter; both daemon call sites (loop.zig) passclient.requested.rows. Snapshot geometry now matches the receiver, so Watch/follower clients and resized viewers no longer get a layout sized for someone else.ScreenFormatterwithunwrapenabled (only when the source has DECAWM on), instead of a hard-wrappedTerminalFormatter. Long logical lines re-render correctly on a wider receiver; sessions with DECAWM off stay hard-wrapped because a receiver without RIS is still in that mode.1047l,6l,r,2J,Hclear any leftover alt buffer, origin mode, and margins; a client that died on alt no longer shows its stale primary through the reattached session. Replayed rows are pushed into history withclient_rowsCRLFs before the screen is cleared.1049/1047/47, preferring 1049) is issued once, with SGR, charset, and DECSCA protection reset in between so primary styling cannot bleed into alt.hasCustomTabstops, avoiding the cursor-corruption that forcedtabstops = false), and DECAWM.terminal-replaybinary (zig build test-replay) andtest/attach_scrollback.py, an end-to-end harness driving the real daemon over its socket and comparing ordered rows against a fresh reference terminal.Security-relevant changes
defer(with a regression test using aFailingAllocator). Previously an allocation failure inside serialization left the live terminal with synchronized output disabled — a resource-exhaustion path that silently changed a running session's rendering mode. It is now restored on every exit.2J/Hand1047ldrop content from a prior client that terminated while on the alternate screen, preventing leftovers from a previous viewer appearing in a new one.ESC(0line-drawing) restyled alternate-screen output.Performance impact
Attach is now measurably more expensive: the snapshot can carry the full primary screen in addition to the active viewport (when alt is active), the whole palette mask is walked per serialize, and up to 10 000 history lines are expanded. The test records snapshot size for a 10 000-line primary plus alt and asserts it stays under the protocol's 8 MiB ceiling. Cost scales with scrollback depth × client rows, so this is attach-path only, not per-frame.
Review finding (medium, unresolved)
src/util.zig:948—serializeTerminalStatemutates live terminal alt-screen modes. Theformat_term = term.*copy deliberately clears the alt-screen mode bits for the viewport formatter; the reviewer flags that this does not reliably isolate the live terminal. Needs confirmation whether these fields are by-value before merging.Code quality notes
boundary/narrowcases onlyprint('OBSERVE ...')and never assert, so they can't fail — either assert them or drop them to keep the suite honest.writeCursorStyleis called twice per serialize (primary and active screen) and the selection/pin construction is duplicated between the primary formatter and the viewport formatter — a small helper would keep these in sync.hasCustomTabstopsand the palette loop re-scan every serialize; caching on the terminal would avoid repeat work at negligible complexity.