fix(tui): take the window size back when the attached terminal is used

`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>
This commit is contained in:
Kamil KaliszandClaude Opus 5.5 committed 2026-11-24 18:28:06 +01:00
1 parent 5548c17c4a
commit ba61cfe4e6
8 files changed
+480 -1

No files matched your search

+13
View File
@@ -638,6 +638,7 @@ export async function beginAttachHandoff(
const banner = bannerFor(muxName, label);
const dressed: Array<{ muxName: string; options: TuiSessionOptions }> = [];
const resized: Array<{ muxName: string; sizing: TuiWindowSizing }> = [];
const followTyping: string[] = [];
const targets = new Map<string, string>([[muxName, label]]);
for (const tab of tabs.slice(0, 9)) {
if (tab.muxName && !targets.has(tab.muxName)) targets.set(tab.muxName, tab.label);
@@ -659,6 +660,15 @@ export async function beginAttachHandoff(
// let the session we are actually opening follow the terminal live.
if (cols && rows && rows > 1) await client.presizeWindow(target, cols, rows - 1);
if (target === muxName) await client.followAttachingClient(target);
// ⚠️ `latest` does not hold for the whole attach. A browser that opens or
// resizes this session mid-attach goes through Codeman's `resize-window`,
// which puts the window back on `manual` at the BROWSER's size: a phone
// opening it left the terminal showing a phone-sized pane, and typing or
// pasting here changed nothing until a detach and a fresh attach. The hook
// puts `latest` back when THIS terminal is used again, the way plain tmux
// follows whoever typed last. Needs the tty for its guard, like the switch
// keys, so without one the attach keeps the old behaviour.
if (guarded && (await client.armFollowTyping(target, tty as string))) followTyping.push(target);
}
// ⚠️ Armed LAST, as the final tmux call before the caller's `spawnSync`. The
// hook is one-shot for this tty, so a TUI killed between arming and attaching
@@ -676,6 +686,9 @@ export async function beginAttachHandoff(
if (tty && isTuiClientTty(tty)) await client.disarmTuiSwitchGuard(tty);
if (oneKey) await client.unbindDetachKey(ONE_KEY_DETACH, 'root');
if (claimed && alias) await client.unbindDetachKey(alias);
// The hooks before the size: a hook left armed would put `latest` back
// the next time anything fired it, after the browser re-pinned the window.
for (const target of followTyping) await client.disarmFollowTyping(target, tty as string);
// Options first, then the size: dropping the status bar gives its row
// back to the pane, and the resize is what re-pins the browser's
// authority over the window.
+99
View File
@@ -507,6 +507,54 @@ export function tuiSwitchAttachHook(tty: string): string {
);
}
/**
* Session hook that gives the window back to the TUI's terminal when you type
* there. Indexed so it does not replace a user's own `client-active` hook, and
* next to the switch guard's `[90]`.
*
* Why it is needed: the attach sets `window-size latest`, but Codeman's web
* resize (`resizeWindow()` in tmux-manager.ts) runs `resize-window`, which
* puts the window back on `manual` at the browser's size. A phone opening the
* session mid-attach therefore pinned the window to the phone, and typing or
* pasting in the terminal changed nothing until the user detached and
* attached again.
*
* Set per session (`set-hook -t`), never globally: it lives and dies with the
* attach's sessions and never reaches a session the TUI did not dress.
*/
export const TUI_FOLLOW_TYPING_HOOK = 'client-active[91]';
/**
* `set-hook` command for `TUI_FOLLOW_TYPING_HOOK`. tmux runs `client-active`
* when a client becomes the latest one on its session, which includes typing
* or pasting after another client did (measured on tmux 3.8). Only this TUI's
* client (`client_tty`) puts `latest` back; a browser tab's client is left to
* Codeman's own arbitration (`Session.resize()`).
*
* ⚠️ `set-window-option` takes no `-t` here on purpose. tmux parses a hook's
* command when the hook is set, and an unquoted `#{session_name}` there
* starts a comment (measured: "-t expects an argument"). Without `-t` the
* command targets the window of the client that fired the hook, which is the
* one it is meant for.
*/
export function tuiFollowTypingHook(tty: string): string {
return `if-shell -F "#{==:#{client_tty},${tty}}" "set-window-option window-size latest"`;
}
/**
* The tty a `client-active[91]` hook line from `show-hooks -t` was set for,
* or null when the line is not one of ours. The closed form `,<tty>}` is
* matched, so `/dev/pts/4` is not read out of a hook for `/dev/pts/41`.
*/
export function tuiFollowTypingHookTty(line: string): string | null {
const text = line.trim();
if (!text.startsWith(`${TUI_FOLLOW_TYPING_HOOK} `)) return null;
if (!text.includes('set-window-option window-size latest')) return null;
const match = /#\{==:#\{client_tty\},(\/dev\/[^}]+)\}/.exec(text);
const tty = match?.[1] ?? '';
return isTuiClientTty(tty) ? tty : null;
}
const LEGACY_TUI_SWITCH = /^switch-client -t (\S+?)(?: \\; set-window-option -t \1 window-size latest)?$/;
/**
@@ -1172,6 +1220,57 @@ export class TuiClient {
}
}
/**
* Install `TUI_FOLLOW_TYPING_HOOK` on one session for this terminal. Best
* effort, like the rest of the attach dressing: false leaves the attach as it
* was before the hook existed (the window follows the terminal until the web
* UI resizes it).
*
* A hook at that index that is not ours is the user's and is left alone. One
* of ours for another tty is replaced: it belongs to a TUI that was killed
* before its restore (a stale tty), or to a second TUI on the same session,
* and the newest attach is the one in front of the user.
*/
async armFollowTyping(muxName: string, clientTty: string): Promise<boolean> {
if (!MUX_NAME_PATTERN.test(muxName) || !isTuiClientTty(clientTty)) return false;
try {
const current = await this.readFollowTypingHook(muxName);
if (current !== null && tuiFollowTypingHookTty(current) === null) return false;
await this.exec('tmux', [
'-L',
this.socket,
'set-hook',
'-t',
muxName,
TUI_FOLLOW_TYPING_HOOK,
tuiFollowTypingHook(clientTty),
]);
return true;
} catch {
return false;
}
}
/**
* Undo `armFollowTyping`, but only while the hook still names this tty: a
* second TUI that attached later owns it now.
*/
async disarmFollowTyping(muxName: string, clientTty: string): Promise<void> {
if (!MUX_NAME_PATTERN.test(muxName) || !isTuiClientTty(clientTty)) return;
try {
const current = await this.readFollowTypingHook(muxName);
if (current === null || tuiFollowTypingHookTty(current) !== clientTty) return;
await this.exec('tmux', ['-L', this.socket, 'set-hook', '-u', '-t', muxName, TUI_FOLLOW_TYPING_HOOK]);
} catch {
/* the session may be gone (the user exited the agent from inside the attach) */
}
}
/** The session's own `client-active[91]` line, or null when nothing is set there. */
private async readFollowTypingHook(muxName: string): Promise<string | null> {
const { stdout } = await this.exec('tmux', ['-L', this.socket, 'show-hooks', '-t', muxName]);
return stdout.split('\n').find((line) => line.startsWith(`${TUI_FOLLOW_TYPING_HOOK} `)) ?? null;
}
/**
* Give back a switch key, but only while it is still one of ours.
*