diff --git a/src/tui/tui-client.ts b/src/tui/tui-client.ts index be1728ed..9683f3b1 100644 --- a/src/tui/tui-client.ts +++ b/src/tui/tui-client.ts @@ -1004,6 +1004,13 @@ export class TuiClient { // It is already the right size (see presizeWindow), so `latest` changes // nothing on arrival and costs no repaint — it matters only if the // terminal is resized while sitting in that session. + // + // ⚠️ The separator MUST be an escaped `\;`. A bare `;` is a command + // separator to tmux's own parser, so it ends the `bind-key` and RUNS + // what follows immediately: the binding kept only `switch-client`, and + // every session got `window-size latest` executed on it at attach time. + // That is what left sessions on `latest` after a detach, since the + // snapshot was then taken from already-corrupted state. await this.exec('tmux', [ '-L', this.socket, @@ -1014,7 +1021,7 @@ export class TuiClient { 'switch-client', '-t', target, - ';', + '\\;', 'set-window-option', '-t', target, diff --git a/test/tui/tui-client.test.ts b/test/tui/tui-client.test.ts index 439814c2..907858d5 100644 --- a/test/tui/tui-client.test.ts +++ b/test/tui/tui-client.test.ts @@ -602,6 +602,32 @@ describe('attach window sizing', () => { }); }); +describe('TuiClient.bindSwitchKey', () => { + it('escapes the command separator, or tmux runs the second command instead of binding it', () => { + // ⚠️ A bare `;` argument is a command separator to tmux's OWN parser: it + // ends the bind-key and executes what follows immediately. That bound only + // `switch-client` and ran `window-size latest` against every session at + // attach time, which is why sessions were left on `latest` after a detach — + // the sizing snapshot was taken from already-corrupted state. Verified + // against real tmux both ways before this test was written. + const calls: string[][] = []; + const exec: TuiExecFile = async (_file, args) => { + calls.push([...args]); + return { stdout: '', stderr: '' }; + }; + const client = new TuiClient({ baseUrl: BASE_URL, socket: 'codeman-beta', exec }); + return client.bindSwitchKey('M-2', 'codeman-aaaa1111').then(() => { + const bind = calls.find((args) => args.includes('bind-key')); + expect(bind).toBeDefined(); + expect(bind).toContain('\\;'); + expect(bind).not.toContain(';'); + // Both commands have to be in the ONE binding. + expect(bind?.join(' ')).toContain('switch-client -t codeman-aaaa1111'); + expect(bind?.join(' ')).toContain('window-size latest'); + }); + }); +}); + describe('parsePrefixBinding', () => { const REAL = [ 'bind-key -T prefix d detach-client',