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 <noreply@anthropic.com>
This commit is contained in:
Kamil KaliszandClaude Opus 5.5 committed 2026-11-24 18:42:21 +01:00
1 parent dbf545ce8f
commit 9753ca9d70
2 files changed
+27 -1

No files matched your search

+14
View File
@@ -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<boolean> {
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;
}
}
+13 -1
View File
@@ -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<string, string>; clients?: string[] } = {}) {
function fakeTmux(
initial: { owner?: string; hooks?: Record<string, string>; clients?: string[]; refuseHook?: string } = {}
) {
const state = { owner: initial.owner ?? null, hooks: { ...(initial.hooks ?? {}) } as Record<string, string> };
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);