fix(tmux): see an exited agent on tmux 3.8, which blanks a dead pane's pid

tmux 3.8 prints #{pane_pid} blank once a pane's program has exited.
parsePaneRows() skipped any row without a numeric pid, so the watcher
never saw an exited agent: the session stayed idle with no paneExit and
the clean-exit sweep (#446) never closed it. reconcileSessions() read the
same rows, so at server start such a session's record was dropped while
its tmux session stayed on the socket.

Keep a dead row with no pid (a live one is still a bad read), tell one
exit from the next by #{pane_dead_time} next to the pid, and keep a
tracked session with a dead pane alive with its last known pid. An
untracked one is still not rediscovered: a made-up pid would reach
killSession()'s process-tree walk.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kamil KaliszandClaude Opus 5.5 committed 2026-11-19 15:11:30 +01:00
1 parent e99b42c387
commit bee4948e1a
5 files changed
+214 -70

No files matched your search

+24 -24
View File
@@ -48,7 +48,7 @@ const docker: SessionDocker = { hostId: 'd1', label: 'ctr', containerName: 'ctr'
describe('derivePaneCurrentPaths', () => {
it("maps each single-pane session to its pane's directory", () => {
const rows = parsePaneRows(['codeman-a|100|0|||/work/proj/sub', 'codeman-b|200|0|||/srv/app'].join('\n'));
const rows = parsePaneRows(['codeman-a|100|0||||/work/proj/sub', 'codeman-b|200|0||||/srv/app'].join('\n'));
const paths = derivePaneCurrentPaths(rows);
expect(paths.get('codeman-a')).toBe('/work/proj/sub');
expect(paths.get('codeman-b')).toBe('/srv/app');
@@ -56,19 +56,19 @@ describe('derivePaneCurrentPaths', () => {
});
it('keeps a separator that belongs to the directory name', () => {
const paths = derivePaneCurrentPaths(parsePaneRows('codeman-a|100|0|||/home/u/weird|dir/sub'));
const paths = derivePaneCurrentPaths(parsePaneRows('codeman-a|100|0||||/home/u/weird|dir/sub'));
expect(paths.get('codeman-a')).toBe('/home/u/weird|dir/sub');
});
it('answers nothing for a session split into several panes', () => {
// Codeman creates one pane per session; which of a hand-split session's
// panes "the" directory belongs to is a guess, so none is made.
const rows = parsePaneRows(['codeman-a|100|0|||/one', 'codeman-a|101|0|||/two'].join('\n'));
const rows = parsePaneRows(['codeman-a|100|0||||/one', 'codeman-a|101|0||||/two'].join('\n'));
expect(derivePaneCurrentPaths(rows).has('codeman-a')).toBe(false);
});
it('drops a missing or relative directory', () => {
const rows = parsePaneRows(['codeman-a|100|0|||', 'codeman-b|200|0|||rel/ative'].join('\n'));
const rows = parsePaneRows(['codeman-a|100|0||||', 'codeman-b|200|0||||rel/ative'].join('\n'));
expect(derivePaneCurrentPaths(rows).size).toBe(0);
});
@@ -76,7 +76,7 @@ describe('derivePaneCurrentPaths', () => {
// A blank `#{pane_current_path}` is a tmux that could not read the process
// cwd that tick, not a pane that has no directory.
const previous = new Map([['codeman-a', '/work/proj/sub']]);
const paths = derivePaneCurrentPaths(parsePaneRows('codeman-a|100|0|||'), previous);
const paths = derivePaneCurrentPaths(parsePaneRows('codeman-a|100|0||||'), previous);
expect(paths.get('codeman-a')).toBe('/work/proj/sub');
});
@@ -89,7 +89,7 @@ describe('derivePaneCurrentPaths', () => {
['codeman-rel', '/three'],
]);
const rows = parsePaneRows(
['codeman-split|100|0|||', 'codeman-split|101|0|||', 'codeman-rel|300|0|||rel/ative'].join('\n')
['codeman-split|100|0||||', 'codeman-split|101|0||||', 'codeman-rel|300|0||||rel/ative'].join('\n')
);
expect(derivePaneCurrentPaths(rows, previous).size).toBe(0);
});
@@ -117,7 +117,7 @@ describe('TmuxManager pane directory bookkeeping', () => {
it('records the directory from the same read the pane-exit watcher makes', async () => {
const manager = new TestManager();
manager.registerSession(localSession('s1'));
manager.rows = parsePaneRows('codeman-s1|100|0|||/work/proj/sub');
manager.rows = parsePaneRows('codeman-s1|100|0||||/work/proj/sub');
await manager.refreshPaneExits();
expect(manager.getPaneCurrentPath('codeman-s1')).toBe('/work/proj/sub');
});
@@ -125,7 +125,7 @@ describe('TmuxManager pane directory bookkeeping', () => {
it('keeps the last known directory when a read comes back empty', async () => {
const manager = new TestManager();
manager.registerSession(localSession('s1'));
manager.rows = parsePaneRows('codeman-s1|100|0|||/work/proj/sub');
manager.rows = parsePaneRows('codeman-s1|100|0||||/work/proj/sub');
await manager.refreshPaneExits();
manager.rows = [];
await manager.refreshPaneExits();
@@ -135,9 +135,9 @@ describe('TmuxManager pane directory bookkeeping', () => {
it('keeps the last directory across a blank field on a single-pane read', async () => {
const manager = new TestManager();
manager.registerSession(localSession('s1'));
manager.rows = parsePaneRows('codeman-s1|100|0|||/work/proj/sub');
manager.rows = parsePaneRows('codeman-s1|100|0||||/work/proj/sub');
await manager.refreshPaneExits();
manager.rows = parsePaneRows('codeman-s1|100|0|||');
manager.rows = parsePaneRows('codeman-s1|100|0||||');
await manager.refreshPaneExits();
expect(manager.getPaneCurrentPath('codeman-s1')).toBe('/work/proj/sub');
});
@@ -145,9 +145,9 @@ describe('TmuxManager pane directory bookkeeping', () => {
it('forgets the directory once the session is split into several panes', async () => {
const manager = new TestManager();
manager.registerSession(localSession('s1'));
manager.rows = parsePaneRows('codeman-s1|100|0|||/work/proj/sub');
manager.rows = parsePaneRows('codeman-s1|100|0||||/work/proj/sub');
await manager.refreshPaneExits();
manager.rows = parsePaneRows(['codeman-s1|100|0|||/work/proj/sub', 'codeman-s1|101|0|||/work/proj'].join('\n'));
manager.rows = parsePaneRows(['codeman-s1|100|0||||/work/proj/sub', 'codeman-s1|101|0||||/work/proj'].join('\n'));
await manager.refreshPaneExits();
expect(manager.getPaneCurrentPath('codeman-s1')).toBeUndefined();
});
@@ -163,7 +163,7 @@ describe('TmuxManager pane directory bookkeeping', () => {
}
const manager = new RacingManager();
manager.registerSession(localSession('s1'));
manager.rows = parsePaneRows('codeman-s1|100|1|0||/work/proj/sub');
manager.rows = parsePaneRows('codeman-s1|100|1|0|||/work/proj/sub');
await manager.refreshPaneExits();
expect(manager.getPaneCurrentPath('codeman-s1')).toBe('/work/proj/sub');
expect(manager.getPaneExit('codeman-s1')).toBeUndefined();
@@ -174,7 +174,7 @@ describe('TmuxManager pane directory bookkeeping', () => {
const read = vi.spyOn(manager as unknown as { readPaneRows: () => Promise<PaneRow[]> }, 'readPaneRows');
manager.registerSession({ ...localSession('s1'), remote } as MuxSession);
manager.registerSession({ ...localSession('s2'), docker } as MuxSession);
manager.rows = parsePaneRows('codeman-s1|100|0|||/tmp');
manager.rows = parsePaneRows('codeman-s1|100|0||||/tmp');
await manager.refreshPaneExits();
expect(read).not.toHaveBeenCalled();
expect(manager.getPaneCurrentPath('codeman-s1')).toBeUndefined();
@@ -183,9 +183,9 @@ describe('TmuxManager pane directory bookkeeping', () => {
it('forgets a session that an authoritative read no longer lists', async () => {
const manager = new TestManager();
manager.registerSession(localSession('s1'));
manager.rows = parsePaneRows('codeman-s1|100|0|||/work/proj/sub');
manager.rows = parsePaneRows('codeman-s1|100|0||||/work/proj/sub');
await manager.refreshPaneExits();
manager.rows = parsePaneRows('codeman-other|300|0|||/elsewhere');
manager.rows = parsePaneRows('codeman-other|300|0||||/elsewhere');
await manager.refreshPaneExits();
expect(manager.getPaneCurrentPath('codeman-s1')).toBeUndefined();
});
@@ -584,37 +584,37 @@ describe('a pane read reaching the session record', () => {
it('clears the directory once the session is split into several panes', async () => {
const { session, broadcast, tick } = buildWithRealManager();
await tick('codeman-aaaa|100|0|||/work/proj/sub');
await tick('codeman-aaaa|100|0||||/work/proj/sub');
expect(session.toState().paneCwd).toBe('/work/proj/sub');
expect(broadcast).toHaveBeenCalledTimes(1);
await tick(['codeman-aaaa|100|0|||/work/proj/sub', 'codeman-aaaa|101|0|||/work/proj'].join('\n'));
await tick(['codeman-aaaa|100|0||||/work/proj/sub', 'codeman-aaaa|101|0||||/work/proj'].join('\n'));
// An absent cwd is what makes the browser fall back to workingDir.
expect(session.toState().paneCwd).toBeUndefined();
expect(broadcast).toHaveBeenCalledTimes(2);
// The clear is announced once, not on every later tick that agrees.
await tick(['codeman-aaaa|100|0|||/work/proj/sub', 'codeman-aaaa|101|0|||/work/proj'].join('\n'));
await tick(['codeman-aaaa|100|0||||/work/proj/sub', 'codeman-aaaa|101|0||||/work/proj'].join('\n'));
expect(broadcast).toHaveBeenCalledTimes(2);
});
it('clears the directory once a read no longer lists the session', async () => {
const { session, broadcast, tick } = buildWithRealManager();
await tick('codeman-aaaa|100|0|||/work/proj/sub');
await tick('codeman-aaaa|100|0||||/work/proj/sub');
expect(session.toState().paneCwd).toBe('/work/proj/sub');
await tick('codeman-other|300|0|||/elsewhere');
await tick('codeman-other|300|0||||/elsewhere');
expect(session.toState().paneCwd).toBeUndefined();
expect(broadcast).toHaveBeenCalledTimes(2);
await tick('codeman-other|300|0|||/elsewhere');
await tick('codeman-other|300|0||||/elsewhere');
expect(broadcast).toHaveBeenCalledTimes(2);
});
it('keeps the directory across a blank field and across a failed read', async () => {
const { session, broadcast, tick } = buildWithRealManager();
await tick('codeman-aaaa|100|0|||/work/proj/sub');
await tick('codeman-aaaa|100|0||||/work/proj/sub');
// tmux could not read the process cwd for one tick.
await tick('codeman-aaaa|100|0|||');
await tick('codeman-aaaa|100|0||||');
expect(session.toState().paneCwd).toBe('/work/proj/sub');
// A failed read arrives as no rows at all.
await tick('');
+126 -18
View File
@@ -1182,21 +1182,21 @@ describe('TmuxManager (unit)', () => {
mockedExecSync.mockImplementation((cmd: string) => {
if (typeof cmd === 'string' && cmd.includes(' list-panes ')) {
return [
'codeman-aaaa1111|101|0|||/home/user/project-a',
'codeman-aaaa1111|101|0||||/home/user/project-a',
// A hand-split session answers with the pane tmux lists first.
'codeman-bbbb2222|102|0|||/home/user/project-b',
'codeman-bbbb2222|103|0|||/home/user/project-b/sub',
'codeman-bbbb2222|102|0||||/home/user/project-b',
'codeman-bbbb2222|103|0||||/home/user/project-b/sub',
// Set by whoever owns the pane, so it must pass the same check as any workingDir.
'codeman-cccc3333|104|0|||/home/user/$(touch pwned)',
'codeman-cccc3333|104|0||||/home/user/$(touch pwned)',
// A tmux that reported no path.
'codeman-dddd4444|105|0|||',
'codeman-dddd4444|105|0||||',
// The launch directory every pane starts in; a remote or docker pane never leaves it.
'codeman-eeee5555|106|0|||/tmp',
'codeman-eeee5555|106|0||||/tmp',
// A directory deleted while its pane ran, as tmux 3.4 on Linux reports it. isValidPath()
// rejects it for the parentheses; a relaxed path pattern must not adopt a dead directory.
'codeman-abcd7777|108|0|||/home/user/gone (deleted)',
'codeman-abcd7777|108|0||||/home/user/gone (deleted)',
// Last row, path ending in a space: the output must not be trimmed.
'codeman-ffff6666|107|0|||/home/user/trailing ',
'codeman-ffff6666|107|0||||/home/user/trailing ',
].join('\n');
}
return '';
@@ -1237,12 +1237,70 @@ describe('TmuxManager (unit)', () => {
}
});
// 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.
it('keeps a tracked session whose only pane is dead and has no pid', async () => {
mockedExecSync.mockImplementation((cmd: string) => {
if (typeof cmd === 'string' && cmd.includes(' list-panes ')) {
return ['codeman-dead1111||1|0||1795091616|', 'codeman-abcd2222|202|0||||/srv'].join('\n');
}
return '';
});
const NonTestTmuxManager = await importWithTmuxCommandsEnabled();
const nonTestManager = new NonTestTmuxManager();
for (const [sessionId, muxName, pid] of [
['dead1111', 'codeman-dead1111', 101],
['abcd2222', 'codeman-abcd2222', 102],
] as const) {
nonTestManager.registerSession({
sessionId,
muxName,
pid,
createdAt: Date.now(),
workingDir: '/tmp',
mode: 'claude',
attached: false,
});
}
try {
const { alive, dead } = await nonTestManager.reconcileSessions();
expect(alive.sort()).toEqual(['abcd2222', 'dead1111']);
expect(dead).toEqual([]);
const pids = Object.fromEntries(nonTestManager.getSessions().map((s) => [s.muxName, s.pid]));
// The last pid known stays, as tmux itself reported it for a dead pane before 3.8.
expect(pids).toEqual({ 'codeman-dead1111': 101, 'codeman-abcd2222': 202 });
expect(nonTestManager.getPaneExit('codeman-dead1111')).toMatchObject({ status: 0 });
} finally {
nonTestManager.destroy();
}
});
it('does not rediscover an untracked session whose only pane is dead and has no pid', async () => {
// A made-up pid would reach killSession()'s process-tree walk.
mockedExecSync.mockImplementation((cmd: string) => {
if (typeof cmd === 'string' && cmd.includes(' list-panes ')) {
return ['codeman-dead3333||1|0||1795091616|', 'codeman-abcd4444|404|0||||/srv'].join('\n');
}
return '';
});
const NonTestTmuxManager = await importWithTmuxCommandsEnabled();
const nonTestManager = new NonTestTmuxManager();
try {
const { discovered } = await nonTestManager.reconcileSessions();
expect(discovered).toEqual(['restored-abcd4444']);
} finally {
nonTestManager.destroy();
}
});
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 ['codeman-aaaa1111|101|0||||/private/tmp', 'codeman-bbbb2222|102|0||||/tmp'].join('\n');
}
return '';
});
@@ -1337,7 +1395,7 @@ describe('parsePaneRows', () => {
it('reads a live pane as not dead, with no status or signal', () => {
// Measured against tmux 3.2a: a live pane leaves both numeric fields blank.
const [row] = parsePaneRows('codeman-aaaa|1234|0|||1');
const [row] = parsePaneRows('codeman-aaaa|1234|0||||1');
expect(row.dead).toBe(false);
expect(row.exitStatus).toBeUndefined();
expect(row.exitSignal).toBeUndefined();
@@ -1374,27 +1432,53 @@ describe('parsePaneRows', () => {
});
it('reads the pane directory from the last field', () => {
const [row] = parsePaneRows('codeman-aaaa|1234|0|||/home/user/project');
const [row] = parsePaneRows('codeman-aaaa|1234|0||||/home/user/project');
expect(row.pid).toBe(1234);
expect(row.dead).toBe(false);
expect(row.currentPath).toBe('/home/user/project');
});
it('keeps a pane directory that contains the separator whole', () => {
const [row] = parsePaneRows('codeman-aaaa|1234|0|||/home/user/a|b');
const [row] = parsePaneRows('codeman-aaaa|1234|0||||/home/user/a|b');
expect(row.currentPath).toBe('/home/user/a|b');
});
it('leaves the pane directory undefined when tmux reported none', () => {
expect(parsePaneRows('codeman-aaaa|1234')[0].currentPath).toBeUndefined();
expect(parsePaneRows('codeman-aaaa|1234|0|||')[0].currentPath).toBeUndefined();
expect(parsePaneRows('codeman-aaaa|1234|0||||')[0].currentPath).toBeUndefined();
});
it('returns one row per pane of a split session, in tmux order', () => {
const rows = parsePaneRows('codeman-aaaa|100|0|||\ncodeman-aaaa|200|1|0|');
const rows = parsePaneRows('codeman-aaaa|100|0||||\ncodeman-aaaa|200|1|0|');
expect(rows.map((row) => row.pid)).toEqual([100, 200]);
expect(rows.map((row) => row.sessionName)).toEqual(['codeman-aaaa', 'codeman-aaaa']);
});
// Measured on tmux 3.8: once a pane's program exits, `#{pane_pid}` prints
// blank. Skipping that row hid every exited agent from the watcher, so a
// clean `/exit` left the session on the board as idle.
it('keeps a dead pane whose pid tmux left blank', () => {
const [row] = parsePaneRows('codeman-aaaa||1|0||1795091616|');
expect(row).toBeDefined();
expect(row.pid).toBeUndefined();
expect(row.dead).toBe(true);
expect(row.exitStatus).toBe(0);
expect(row.deadTime).toBe(1795091616);
expect(row.currentPath).toBeUndefined();
});
it('still skips a live pane with a blank pid, which is a bad read', () => {
expect(parsePaneRows('codeman-aaaa||0||||/home/user/project')).toEqual([]);
expect(parsePaneRows('codeman-aaaa||')).toEqual([]);
});
it('reads the exit time, and leaves it undefined while the pane runs', () => {
expect(parsePaneRows('codeman-aaaa|1234|1|0||1795091616|/srv')[0]).toMatchObject({
deadTime: 1795091616,
currentPath: '/srv',
});
expect(parsePaneRows('codeman-aaaa|1234|0||||/srv')[0].deadTime).toBeUndefined();
});
});
describe('derivePaneExits', () => {
@@ -1419,7 +1503,7 @@ describe('derivePaneExits', () => {
});
it('says nothing about a live pane', () => {
const exits = derivePaneExits(parsePaneRows('codeman-aaaa|1234|0|||'), NOW);
const exits = derivePaneExits(parsePaneRows('codeman-aaaa|1234|0||||'), NOW);
expect(exits.has('codeman-aaaa')).toBe(false);
});
@@ -1436,11 +1520,16 @@ describe('derivePaneExits', () => {
it("answers per session, so one session's split does not silence another", () => {
const exits = derivePaneExits(
parsePaneRows('codeman-aaaa|100|1|0|\ncodeman-bbbb|200|1|0|\ncodeman-bbbb|201|0|||'),
parsePaneRows('codeman-aaaa|100|1|0|\ncodeman-bbbb|200|1|0|\ncodeman-bbbb|201|0||||'),
NOW
);
expect([...exits.keys()]).toEqual(['codeman-aaaa']);
});
it('reports a clean exit from a dead pane whose pid tmux left blank', () => {
const exits = derivePaneExits(parsePaneRows('codeman-aaaa||1|0||1795091616|'), NOW);
expect(exits.get('codeman-aaaa')).toEqual({ deadTime: 1795091616, exit: { status: 0, at: NOW } });
});
});
describe('TmuxManager pane-exit bookkeeping', () => {
@@ -1470,7 +1559,7 @@ describe('TmuxManager pane-exit bookkeeping', () => {
it('forgets the exit once the same session reports a live pane', () => {
const manager = new TmuxManager();
manager.applyPaneExits(derivePaneExits(parsePaneRows('codeman-aaaa|100|1|0|'), NOW));
manager.applyPaneExits(derivePaneExits(parsePaneRows('codeman-aaaa|101|0|||'), NOW + 2000));
manager.applyPaneExits(derivePaneExits(parsePaneRows('codeman-aaaa|101|0||||'), NOW + 2000));
expect(manager.getPaneExit('codeman-aaaa')).toBeUndefined();
});
@@ -1530,11 +1619,30 @@ describe('TmuxManager pane-exit bookkeeping', () => {
it('drops the count once a read sees the pane alive again', () => {
const manager = new TmuxManager();
manager.applyPaneExits(derivePaneExits(parsePaneRows('codeman-aaaa|100|1|0|'), NOW));
manager.applyPaneExits(derivePaneExits(parsePaneRows('codeman-aaaa|101|0|||'), NOW + 2000));
manager.applyPaneExits(derivePaneExits(parsePaneRows('codeman-aaaa|101|0||||'), NOW + 2000));
expect(manager.getPaneExitReadCount('codeman-aaaa')).toBe(0);
manager.applyPaneExits(derivePaneExits(parsePaneRows('codeman-aaaa|101|1|0|'), NOW + 4000));
expect(manager.getPaneExitReadCount('codeman-aaaa')).toBe(1);
});
// tmux 3.8 blanks a dead pane's pid, so the exit time is what tells one
// exit from the next there.
it('counts repeated reads of one exit when tmux gave no pid', () => {
const manager = new TmuxManager();
manager.applyPaneExits(derivePaneExits(parsePaneRows('codeman-aaaa||1|0||1795091616|'), NOW));
manager.applyPaneExits(derivePaneExits(parsePaneRows('codeman-aaaa||1|0||1795091616|'), NOW + 2000));
expect(manager.getPaneExitReadCount('codeman-aaaa')).toBe(2);
expect(manager.getPaneExit('codeman-aaaa')).toEqual({ status: 0, at: NOW });
});
it('starts a new observation when the exit time changes and tmux gave no pid', () => {
// A hand-run `tmux respawn-pane` whose command exited 0 again between two reads.
const manager = new TmuxManager();
manager.applyPaneExits(derivePaneExits(parsePaneRows('codeman-aaaa||1|0||1795091616|'), NOW));
manager.applyPaneExits(derivePaneExits(parsePaneRows('codeman-aaaa||1|0||1795091620|'), NOW + 2000));
expect(manager.getPaneExitReadCount('codeman-aaaa')).toBe(1);
expect(manager.getPaneExit('codeman-aaaa')).toEqual({ status: 0, at: NOW + 2000 });
});
});
describe('the pane-exit watcher tick', () => {