From 67aa28ac4f571e9bdd342f198a912e819966cbfa Mon Sep 17 00:00:00 2001 From: Kamil Kalisz Date: Tue, 24 Nov 2026 15:41:39 +0100 Subject: [PATCH] test(tmux): close a dead-pane session end to end without signalling its pid The kill rule was pinned only through parsePaneKillRead(). Drive killSession() with tmux commands enabled and exec answering like tmux 3.8 (a dead pane, pid blank), and assert nothing signals the recorded pid while kill-session still runs. Against bee4948e it fails: pid 4242 is signalled 201 times. Group the three no-pid tests in their own describe instead of the launch-cwd one. Co-Authored-By: Claude Opus 5.5 --- test/tmux-manager.test.ts | 85 +++++++++++++++++++++++++++++---------- 1 file changed, 64 insertions(+), 21 deletions(-) diff --git a/test/tmux-manager.test.ts b/test/tmux-manager.test.ts index 9832b83e..35dace11 100644 --- a/test/tmux-manager.test.ts +++ b/test/tmux-manager.test.ts @@ -1238,6 +1238,35 @@ describe('TmuxManager (unit)', () => { } }); + it('treats the resolved spelling of the launch directory as no answer too (macOS /private/tmp)', async () => { + // On macOS /tmp is a symlink and a pane reports its process cwd resolved. + realpathOverrides.set('/tmp', '/private/tmp'); + mockedExecSync.mockImplementation((cmd: string) => { + if (typeof cmd === 'string' && cmd.includes(' list-panes ')) { + return ['codeman-aaaa1111|101|0||||/private/tmp', 'codeman-bbbb2222|102|0||||/tmp'].join('\n'); + } + return ''; + }); + try { + const NonTestTmuxManager = await importWithTmuxCommandsEnabled(); + const nonTestManager = new NonTestTmuxManager(); + try { + await nonTestManager.reconcileSessions(); + const dirs = Object.fromEntries(nonTestManager.getSessions().map((s) => [s.muxName, s.workingDir])); + expect(dirs).toEqual({ + 'codeman-aaaa1111': process.cwd(), + 'codeman-bbbb2222': process.cwd(), + }); + } finally { + nonTestManager.destroy(); + } + } finally { + realpathOverrides.clear(); + } + }); + }); + + describe('a dead pane tmux printed no pid for (tmux 3.8)', () => { // tmux 3.8 prints a blank `#{pane_pid}` for a dead pane. Reading that as "no such // session" dropped the record of a session whose agent had exited, while its tmux // session stayed on the socket where nothing would ever find it again. @@ -1296,30 +1325,44 @@ describe('TmuxManager (unit)', () => { } }); - it('treats the resolved spelling of the launch directory as no answer too (macOS /private/tmp)', async () => { - // On macOS /tmp is a symlink and a pane reports its process cwd resolved. - realpathOverrides.set('/tmp', '/private/tmp'); - mockedExecSync.mockImplementation((cmd: string) => { - if (typeof cmd === 'string' && cmd.includes(' list-panes ')) { - return ['codeman-aaaa1111|101|0||||/private/tmp', 'codeman-bbbb2222|102|0||||/tmp'].join('\n'); - } - return ''; + // The recorded pid of a dead pane belongs to the program that exited, and the OS + // may have handed it to an unrelated process since: closing must not signal it. + it('closes a session whose pane is dead without signalling its recorded pid', async () => { + const defaultExec = mockedExec.getMockImplementation(); + mockedExec.mockImplementation(((cmd: string, optionsOrCallback?: unknown, maybeCallback?: unknown) => { + const callback = (typeof optionsOrCallback === 'function' ? optionsOrCallback : maybeCallback) as + | ((err: Error | null, out: unknown, stderr: string) => void) + | undefined; + // promisify(exec) on this mock resolves with the second argument. tmux 3.8 + // prints a dead pane's pid blank, so a pid-only read answers an empty line. + const stdout = cmd.includes(' display-message ') ? (cmd.includes('#{pane_dead}') ? '1|\n' : '\n') : ''; + if (callback) setImmediate(() => callback(null, { stdout, stderr: '' }, '')); + return { on: vi.fn(), kill: vi.fn(), pid: 12345 }; + }) as unknown as typeof exec); + const kill = vi.spyOn(process, 'kill').mockImplementation(() => true); + const NonTestTmuxManager = await importWithTmuxCommandsEnabled(); + const nonTestManager = new NonTestTmuxManager(); + nonTestManager.registerSession({ + sessionId: 'dead5555', + muxName: 'codeman-dead5555', + pid: 4242, + createdAt: Date.now(), + workingDir: '/tmp', + mode: 'claude', + attached: false, }); + try { - const NonTestTmuxManager = await importWithTmuxCommandsEnabled(); - const nonTestManager = new NonTestTmuxManager(); - try { - await nonTestManager.reconcileSessions(); - const dirs = Object.fromEntries(nonTestManager.getSessions().map((s) => [s.muxName, s.workingDir])); - expect(dirs).toEqual({ - 'codeman-aaaa1111': process.cwd(), - 'codeman-bbbb2222': process.cwd(), - }); - } finally { - nonTestManager.destroy(); - } + expect(await nonTestManager.killSession('dead5555')).toBe(true); + expect(kill.mock.calls.filter(([pid]) => Math.abs(Number(pid)) === 4242)).toEqual([]); + const commands = mockedExec.mock.calls.map(([cmd]) => String(cmd)); + expect(commands.some((cmd) => cmd.includes('display-message') && cmd.includes('#{pane_dead}'))).toBe(true); + expect(commands.some((cmd) => cmd.includes('kill-session -t "codeman-dead5555"'))).toBe(true); + expect(nonTestManager.getSession('dead5555')).toBeUndefined(); } finally { - realpathOverrides.clear(); + kill.mockRestore(); + mockedExec.mockImplementation(defaultExec!); + nonTestManager.destroy(); } }); });