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

+78
View File
@@ -676,6 +676,8 @@ describe('attach switch keys stay on the TUI client', () => {
order.push('follow');
return true;
},
armFollowTyping: async () => true,
disarmFollowTyping: async () => undefined,
restoreWindowSizing: async () => true,
} as unknown as TuiClient;
@@ -726,6 +728,82 @@ describe('attach switch keys stay on the TUI client', () => {
});
});
describe('attach takes the window back from a browser when the terminal is used', () => {
/** Records the sizing calls in order; everything else is a no-op. */
function sizingFake(armResult = true) {
const calls: string[] = [];
const client = {
readPrefixKey: async () => 'C-b',
readDetachKey: async () => 'd',
readPrefixBinding: async () => null,
bindDetachKey: async () => true,
unbindDetachKey: async () => true,
armTuiSwitchGuard: async () => true,
disarmTuiSwitchGuard: async () => undefined,
bindSwitchKey: async () => true,
unbindSwitchKey: async () => undefined,
readSessionOptions: async () => null,
applySessionOptions: async () => true,
restoreSessionOptions: async () => undefined,
readWindowSizing: async () => ({ cols: 60, rows: 30, mode: 'manual' }),
presizeWindow: async () => true,
followAttachingClient: async (muxName: string) => {
calls.push(`follow ${muxName}`);
return true;
},
armFollowTyping: async (muxName: string, tty: string) => {
calls.push(`arm ${muxName} ${tty}`);
return armResult;
},
disarmFollowTyping: async (muxName: string, tty: string) => {
calls.push(`disarm ${muxName} ${tty}`);
},
restoreWindowSizing: async (muxName: string) => {
calls.push(`size ${muxName}`);
},
} as unknown as TuiClient;
return { client, calls };
}
const tabs = [
{ index: 1, label: 'w1', active: true, muxName: 'codeman-aaaa1111' },
{ index: 2, label: 'w2', active: false, muxName: 'codeman-bbbb2222' },
];
it('arms the hook on every session the strip can reach, and drops it before the size is put back', async () => {
const { client, calls } = sizingFake();
const handoff = await beginAttachHandoff(client, 'codeman-aaaa1111', 'w1', tabs, 200, 50, '/dev/pts/4');
// Alt+2 lands in the second session, and a phone can pin that one too.
expect(calls).toEqual([
'follow codeman-aaaa1111',
'arm codeman-aaaa1111 /dev/pts/4',
'arm codeman-bbbb2222 /dev/pts/4',
]);
calls.length = 0;
await handoff.restore();
// Disarmed first: the size restore re-pins the browser's authority, and
// a hook still armed after it could hand the window back to a reused pty.
expect(calls).toEqual([
'disarm codeman-aaaa1111 /dev/pts/4',
'disarm codeman-bbbb2222 /dev/pts/4',
'size codeman-aaaa1111',
'size codeman-bbbb2222',
]);
});
it('arms nothing without a tty to guard on, and disarms only what it armed', async () => {
const bare = sizingFake();
await (await beginAttachHandoff(bare.client, 'codeman-aaaa1111', 'w1', tabs, 200, 50, null)).restore();
expect(bare.calls.filter((call) => call.startsWith('arm') || call.startsWith('disarm'))).toEqual([]);
// A session whose hook slot is the user's (arm answered false) is not disarmed.
const refused = sizingFake(false);
await (await beginAttachHandoff(refused.client, 'codeman-aaaa1111', 'w1', tabs, 200, 50, '/dev/pts/4')).restore();
expect(refused.calls.filter((call) => call.startsWith('disarm'))).toEqual([]);
});
});
describe('naming a session the TUI starts', () => {
it("follows the web UI's w<n>-<case> convention", () => {
expect(nextSessionName('mirofish', [])).toBe('w1-mirofish');