From 9753ca9d703bc2a95ce2a25cc0b416b6ebb484fe Mon Sep 17 00:00:00 2001 From: Kamil Kalisz Date: Tue, 24 Nov 2026 18:42:21 +0100 Subject: [PATCH] fix(tui): undo a half-done follow arm 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 --- src/tui/tui-client.ts | 14 ++++++++++++++ test/tui/tui-client.test.ts | 14 +++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/tui/tui-client.ts b/src/tui/tui-client.ts index 7684a35b..defbabd9 100644 --- a/src/tui/tui-client.ts +++ b/src/tui/tui-client.ts @@ -1240,18 +1240,32 @@ export class TuiClient { * installed. An owner already recorded is replaced: it is a TUI killed before * its restore, or a second TUI on the same session, and the newest attach is * the one in front of the user. + * + * ⚠️ A half-done arm is undone before answering false, because `restore()` + * only disarms what armed. tmux before 3.3 has no `client-active` hook, so + * there the owner is set and the first hook is refused; left alone, the + * owner would stay on the session for good. */ async armFollowTyping(muxName: string, clientTty: string): Promise { if (!MUX_NAME_PATTERN.test(muxName) || !isTuiClientTty(clientTty)) return false; try { const hooks = await this.readFollowHooks(muxName); if (Object.values(hooks).includes('theirs')) return false; + } catch { + return false; + } + try { await this.exec('tmux', ['-L', this.socket, 'set-option', '-t', muxName, TUI_FOLLOW_OWNER, clientTty]); for (const [name, command] of Object.entries(TUI_FOLLOW_HOOKS)) { await this.exec('tmux', ['-L', this.socket, 'set-hook', '-t', muxName, name, command]); } return true; } catch { + try { + await this.dropFollowTyping(muxName); + } catch { + /* the session is gone, or tmux is: nothing is left to undo then */ + } return false; } } diff --git a/test/tui/tui-client.test.ts b/test/tui/tui-client.test.ts index 34ad095e..5e891106 100644 --- a/test/tui/tui-client.test.ts +++ b/test/tui/tui-client.test.ts @@ -527,7 +527,9 @@ describe('the follow hooks', () => { * A fake tmux holding one session's options and hooks, so arm, disarm and * the sweep run against state rather than a script of canned answers. */ - function fakeTmux(initial: { owner?: string; hooks?: Record; clients?: string[] } = {}) { + function fakeTmux( + initial: { owner?: string; hooks?: Record; clients?: string[]; refuseHook?: string } = {} + ) { const state = { owner: initial.owner ?? null, hooks: { ...(initial.hooks ?? {}) } as Record }; const writes: string[] = []; const exec: TuiExecFile = async (_file, args) => { @@ -544,6 +546,9 @@ describe('the follow hooks', () => { const lines = Object.entries(state.hooks).map(([name, command]) => `${name} ${command}`); return { stdout: lines.join('\n'), stderr: '' }; } + if (verb === 'set-hook' && !args.includes('-u') && args.includes(initial.refuseHook ?? '\0')) { + throw new Error(`invalid option: ${initial.refuseHook}`); + } writes.push(args.slice(2).join(' ')); if (verb === 'set-option' && args.includes('-u')) state.owner = null; else if (verb === 'set-option') state.owner = args[args.length - 1] ?? null; @@ -598,6 +603,13 @@ describe('the follow hooks', () => { expect(state).toEqual({ owner: null, hooks: {} }); }); + it('undoes a half-done arm, as on a tmux without client-active (before 3.3)', async () => { + const { client, state } = fakeTmux({ refuseHook: 'client-active[91]' }); + await expect(client.armFollowTyping('codeman-1a2b3c4d', '/dev/pts/4')).resolves.toBe(false); + // restore() disarms only what armed, so nothing may be left behind here. + expect(state).toEqual({ owner: null, hooks: {} }); + }); + it('installs nothing over a hook of the user, and never removes one', async () => { const { client, state, writes } = fakeTmux({ hooks: { 'after-resize-window[91]': 'display-message mine' } }); await expect(client.armFollowTyping('codeman-1a2b3c4d', '/dev/pts/4')).resolves.toBe(false);