Skip to content

fix(tui): the attached terminal takes the window size back after a browser resized it - #853

Open
kkalisz wants to merge 5 commits into
Ark0N:masterfrom
kkalisz:fix/tui-follow-typing-client
Open

kkalisz wants to merge 5 commits into
Ark0N:masterfrom
kkalisz:fix/tui-follow-typing-client

Conversation

@kkalisz

@kkalisz kkalisz commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Attach to a session with codeman tui, then open the same session on a phone. The terminal is stuck at the phone's size: typing or resizing does not help until you detach and attach again.

The cause is that a browser resize goes through Session.resize(), which ends in resize-window, and that switches the window to window-size manual. Codeman's own attach also sets manual. After either one, the TUI's latest setting is lost for the rest of the attach.

Fix

While a TUI attach is up, every session it can reach gets one hook and two session options:

  • client-active[91], which puts latest back when the TUI's client is used again (typing, pasting, resizing).
  • @codeman-tui-follow, the attach's tty, which the hook reads.
  • @codeman-tui-follow-pid, the TUI process, which the leak sweep reads.

A browser resize makes the tab's client the latest one, because Codeman resizes that client's pty first. A forced resize at an unchanged size (Redraw, a closing pop-out window, the split pane) does not change the pty, so Session.resize() now nudges the pty one row first. The user's Redraw then wins, and the terminal's next key takes the window back. Without an attach the window is manual and ignores the client's pty, so the agent sees no change.

Rules:

  • The hook is set per session (set-hook -t), never globally. Its command is a constant string with no -t, which reads the tty from the option.
  • Nothing is armed over a user hook at index 91, or while the user has a global client-active hook, because a session array would hide it.
  • Detach removes the owner first, then the hook, before the window sizes are restored. When ours is the only entry, it unsets the whole event: an empty session array would keep hiding a global hook.
  • A failed setup undoes itself. This matters on tmux older than 3.3, which has no client-active.
  • A TUI killed mid-attach leaves the hook and options behind. They are swept at TUI startup and before each attach, so codeman tui <n> is covered too.
    • The sweep keeps only the sessions this process armed, and those whose recorded pid is another live process.
    • It never trusts a tty, because Linux reuses pty numbers.
  • Arm and disarm each take one tmux call to read and one to write.

Cost: the first browser resize after the terminal was used changes the agent's height twice, where master changes it once. Later resizes from the same tab cost one.

Known limits:

  • The switch-client path, used when the TUI runs inside Codeman's tmux, gets no hook.
  • With two TUIs on one session, the later attach owns it.

Changes

  • src/tui/tui-client.ts: armFollowTyping, disarmFollowTyping and clearLeakedFollowTyping, plus the hook and option constants.
  • src/tui/tui-app.ts: beginAttachHandoff sweeps, arms the hook and disarms it in restore(). The dashboard also sweeps at startup.
  • src/session.ts: the one-row nudge for a forced resize at an unchanged size (FORCED_RESIZE_NUDGE_MS).
  • Tests:
    • test/tui/tui-follow-typing.test.ts (new) runs against real tmux with two pty clients. It is skipped below tmux 3.3.
    • test/tui/tui-client.test.ts, test/tui/tui-app.test.ts and test/session-resize-arbitration.test.ts are extended.
  • Docs: docs/tui.md, docs/architecture-invariants.md and a patch changeset.

Verification

  • 12 of the new tests fail with the previous src.
  • The TUI and session tests pass, and the real-tmux test passed 5 runs out of 5.
  • By hand on tmux 3.8, with a real codeman tui attach and the phone's resize sent through the /resize route:
Step master This branch
Phone opens the session 60x40 manual 60x40 manual
Type in the terminal 60x40 manual 200x49 latest
Phone Redraw (forced, same size) 62x40 manual 62x40 manual
Type in the terminal 62x40 manual 200x49 latest
F1 back to the dashboard 58x41 manual 58x41 manual, no hook left

🤖 Generated with Claude Code

kkalisz and others added 4 commits November 24, 2026 18:28
`codeman tui` sets `window-size latest` for an attach, but a browser tab
that opens or resizes the same session mid-attach makes Codeman run
`resize-window`, which puts the window back on `manual` at the browser's
size. The terminal then showed a phone-sized pane, and typing or pasting
changed nothing until a detach and a fresh attach.

The attach now sets a per-session `client-active[91]` hook on every
session it dresses. It runs `set-window-option window-size latest` only
for the TUI's own client (`#{client_tty}`), so typing, pasting or
resizing the terminal hands the window back to it, the way plain tmux
follows whoever used it last. `restore()` removes the hooks before the
window sizes are put back, and only while they still name this tty.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A terminal that dies while attached (closed window, dropped SSH) never
reaches restore(), so its client-active[91] hooks stayed on the sessions.
TUI startup now removes ours whose tty has no live tmux client, next to
the leaked-banner sweep. The TUI's own tty is never swept: the sweep is
fire-and-forget, and an early Enter can arm that hook before the attach
client exists.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…zes included

A browser that opens a session sends a forced resize at its current size.
Its pty does not change, so tmux keeps the TUI's client as the latest one
and `client-active` never fires again, while Codeman's `resize-window`
still pins the window to `manual` at the browser's size. Typing in the
terminal then did nothing, which is the bug this branch fixes.

An `after-resize-window[91]` hook now puts `latest` back after Codeman's
resize, so tmux keeps deciding by the client used last; `client-active[91]`
stays for Codeman's own attach (`setManualWindowSize()`), which sets
`manual` without a resize.

The owner's tty moves into the session option `@codeman-tui-follow`, and
both hook commands are constants that read it, so no tty is interpolated
into a hook and hooks whose owner is gone do nothing. restore() and the
startup sweep drop the owner first, then our hooks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
armFollowTyping() set the owner option, then a hook. On tmux before 3.3
there is no client-active hook, so the first set-hook failed and the arm
answered false with the owner still set; restore() disarms only what
armed, so it stayed on the session. A failed arm now drops the owner and
any of our hooks it already set.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kkalisz
kkalisz marked this pull request as ready for review November 25, 2026 11:41
@Ark0N

Ark0N commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thanks for this, kkalisz. The PR lets an attached codeman tui take the window size back after a browser resized the same session, and it works for the case you describe. I reproduced it on tmux 3.4. Your new tests pass 8 runs out of 8, and 11 of them fail with master's src/tui. Constant hook commands that read the owner from an option are a good design. Three things need a fix before this merges.

  1. The browser's Redraw stops working (src/tui/tui-client.ts:549). restoreTerminalSize() (Ctrl+Shift+R and the header button) sends a forced resize so a device can take the pane back at an unchanged size. At an unchanged size the tab's pty gets no SIGWINCH, so the terminal stays the latest client and after-resize-window[91] undoes the resize-window. With the terminal used last, a Redraw at 62x40 gives 62x40 manual without the hooks and 200x49 latest with them, while the page still says "Terminal restored to 62x40". A browser does not send a forced resize when it opens a session: selectSession calls sendResize without force, and the server drops an unchanged size. Only Redraw, a closing pop-out window and the split pane send one, and Redraw is a user asking for the size. Please make it win, and change test/tui/tui-follow-typing.test.ts:137-141 to assert that. One fix I tested: when Session.resize() gets force with unchanged dimensions, nudge the attach client's pty (rows - 1, then rows), so tmux counts the tab as the client used last. That gave 62x39 latest after the nudge and 200x49 latest after the next key in the terminal. With no TUI attached, the window and the agent see no change.

  2. The startup sweep skips the common leak (src/tui/tui-client.ts:1320). Linux hands out the lowest free pty, so a new terminal usually gets the number of the one that was just closed. I killed the attach on /dev/pts/45, the next pty was /dev/pts/45 again, and the sweep cleared 0 sessions because owner === ownTty. When a browser tab's attach client got that pty, live.has(owner) kept the leak too. The leftover hooks still act: on the next attach they undo presizeWindow() for the other sessions in the strip (200x49 asked, 90x29 got). Please skip only the sessions this process armed (a Set filled before the first set-option, emptied on disarm) instead of the whole tty. A browser's client should not be taken for the owner either; the Alt+N guard records the client pid for this reason. codeman tui <n> runs no sweep, so either run it there as well or say so in docs/tui.md.

  3. Disarm leaves empty hook arrays (src/tui/tui-client.ts:1350). set-hook -u -t <session> client-active[91] leaves an empty session-level array, which hides the user's global hooks for that event. I tried it with a global after-resize-window hook. It fires before the attach but not during it, and after the detach it stays off until the whole session array is unset. restoreSessionOptions() documents the same trap for status-format[0]. Please unset the whole array when ours is the only entry, and answer false from armFollowTyping when the user has a global hook on either event.

Smaller points:

  • Each browser resize during an attach now sends the agent three height changes (34, 35, 34 rows for a resize to 35) where master sends one, because the status bar takes a row. Height changes cost transcript lines in Claude's inline renderer (Opening the phone keyboard resizes the PTY, and Claude Code drops transcript lines for good #672). With the nudge from point 1, client-active[91] alone covers every case except a key landing in the milliseconds between the tab's SIGWINCH and the resize-window. Either drop the second hook or state the cost next to it.
  • The "forced resize when it opens a session" wording is also in src/tui/tui-client.ts:531, docs/architecture-invariants.md:21 and, in effect, docs/tui.md:219-221. Please correct it there.
  • In docs/architecture-invariants.md the new text sits between the Alt+1..9 guard and the escape-time sentence that refers back to it. A paragraph of its own reads better.
  • Arming and disarming each take about 35 ms per session. One tmux call with ; arguments instead of three would keep attach fast with nine tabs.

With 1 to 3 fixed, this is mergeable.

…hole hook array

Review follow-up on the follow-typing attach.

- Redraw (and every forced resize at an unchanged size) handed the window
  back to the terminal: an unchanged pty sends tmux no SIGWINCH, so the
  terminal stayed the latest client and the after-resize-window hook undid
  the resize-window. Session.resize() now nudges the pty one row first
  (FORCED_RESIZE_NUDGE_MS), then applies the size, so tmux counts the tab
  as the client used last; the second move bumps the guarded geometry
  revision too. Under manual with no attach the agent sees no change.
- With the nudge, client-active[91] alone covers every case, so the
  after-resize-window hook is gone. It kept latest through every browser
  resize and cost the agent three height changes per resize; now the first
  resize after the terminal was used costs two and later ones one. The
  cost is stated next to the hook.
- The leak sweep matched owners by tty, and Linux hands the next terminal
  the pty that just closed, so the common leak was never swept. The arm
  now records the TUI's pid (@codeman-tui-follow-pid), and the sweep keeps
  only sessions this process armed (a Set filled before the first write)
  or whose pid is another live process. One list-sessions call reads every
  owner. It also runs before each attach dresses its sessions, so
  `codeman tui <n>` gets it and a leaked hook cannot undo presizeWindow().
- `set-hook -u` on our index left an empty session-level array, which
  hides the user's global hook on that event. Disarm unsets the whole
  event when ours is its only entry, and arm refuses while the user has a
  global client-active hook.
- Arm and disarm are one tmux call to read and one to write (`;` lists).
- Docs: the "forced resize when a browser opens a session" wording was
  wrong (selectSession sends no force; only Redraw, a closing pop-out and
  the split pane do), fixed in tui-client.ts, docs/tui.md and the
  invariants, whose paragraph now stands on its own.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kkalisz

kkalisz commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Pushed 70ee131 with all three blockers and the smaller points:

  1. Redraw wins. When Session.resize() gets force at an unchanged size, it nudges the attach client's pty one row (FORCED_RESIZE_NUDGE_MS, 60 ms) and then applies the size, as you suggested. The second move bumps the guarded geometry revision as well. The real-tmux test now asserts that a Redraw at 61x30 gives 61x30 manual, and the next key in the terminal gives 200x50 latest. By hand on 3.8, the phone's Redraw at 62x40 gave 62x40 manual.
  2. Sweep by process. The arm now records @codeman-tui-follow-pid, the TUI's pid, next to the tty. The sweep keeps only the sessions this process armed (a Set filled before the first write and emptied on disarm) and those whose pid is another live process, whatever their tty. The test covers a leak whose tty is the live TUI's own. One list-sessions -F call reads every owner. The sweep also runs at the start of beginAttachHandoff, so codeman tui <n> gets it and a leak cannot undo presizeWindow().
  3. No empty arrays. When ours is the only entry, disarm unsets the whole client-active event, and it removes only [91] when the user has another entry. Arm answers false while there is a global client-active hook. Both cases are tested on real tmux and in the fake.

Smaller points:

  • With the nudge, after-resize-window[91] is gone and client-active[91] is the only hook. The cost is stated next to it: the first browser resize after the terminal was used changes the height twice (the window is on latest, then resize-window), and later ones once.
  • The "forced resize when a browser opens a session" wording is fixed in tui-client.ts, docs/tui.md and the invariants.
  • The invariants text is now a paragraph of its own.
  • Arm and disarm each take one tmux call to read and one to write (; lists). The read joins show-hooks -g and show-hooks -t with a marker.

12 of the new tests fail with the previous src, and the real-tmux test passed 5 runs out of 5. Typecheck, lint, format and the frontend checks pass. The PR description is updated to the one-hook design.

🤖 Generated with Claude Code

@Ark0N

Ark0N commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Thanks for the quick turnaround, kkalisz. This PR lets an attached codeman tui take the window size back after a browser tab resized the same session, using a per-session client-active[91] hook and a one-row nudge on a forced resize. All three points from the last round are fixed: Redraw wins, the sweep goes by process, and disarm unsets the whole array when ours is the only entry.

I re-ran everything here. Typecheck, lint, format and the frontend checks pass. The four touched test files pass (194 tests, with the real-tmux file on tmux 3.4), and 16 of the new tests fail with master's src. The full suite is green except for one chown failure that only happens in my sandbox, in a file this PR does not touch.

Two things are left:

  1. The nudge also runs for a session with no tmux attach client (src/session.ts:5707). In the direct-PTY fallback (tmux missing, or a mux create that failed), ptyProcess is the agent itself. So a Redraw, a popup redock or a split close at an unchanged size now sends the agent two SIGWINCHes (rows - 1, then rows), where master sent none. Linux sends no SIGWINCH for a same-size resize; I checked both cases with node-pty. Each height change makes Claude Code's inline renderer clear and reprint the tail (Opening the phone keyboard resizes the PTY, and Claude Code drops transcript lines for good #672). The test at test/session-resize-arbitration.test.ts:151 pins exactly this case: its session has a ptyProcess and no mux. The fix is to nudge only when the pty is a tmux client, if (!dimsChanged && rows > 1 && this._useMux && this._muxSession), give the two nudge tests a mux session, and add a case asserting that a mux-less session gets the single [120, 40] call. With that, the comment above it and the invariants sentence "the agent sees no change" are true as written.

  2. docs/tui.md also carries Prettier reformatting that has nothing to do with the change: the four tables are realigned, and *client* / *and* became _client_ / _and_ (lines 10, 30-34, 101-107, 124-137, 241-245, 285). docs/ is outside the repo's Prettier scope, so please drop those hunks and keep only the new paragraph.

Optional: store the 60 ms nudge timer (src/session.ts:5728) and clear it in _clearAllTimers(), like the session's other timers. It is harmless today because of the revision and ptyProcess guards. Also optional: add a one-line pointer to the nudge in the "PTY and browser terminal geometry" section of docs/architecture-invariants.md, so whoever touches Session.resize() next finds it.

No need to push again: 1 and 2 go on top at merge time. If you would rather push them yourself first, that works too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants