fix(tmux): send Ctrl+C before kill-pane and respawn-pane to prevent orphaned processes (#1329)

* fix(tmux): send Ctrl+C before kill-pane and respawn-pane to prevent orphaned processes

* fix(tmux-subagent): prevent premature pane closure with stability detection

Implements stability detection pattern from background-agent to prevent
tmux panes from closing while agents are still working (issue #1330).

Problem: Session status 'idle' doesn't mean 'finished' - agent may still
be thinking/reasoning. Previous code closed panes immediately on idle.

Solution:
- Require MIN_STABILITY_TIME_MS (10s) before stability detection activates
- Track message count changes to detect ongoing activity
- Require STABLE_POLLS_REQUIRED (3) consecutive polls with same message count
- Double-check session status before closing

Changes:
- types.ts: Add lastMessageCount and stableIdlePolls to TrackedSession
- manager.ts: Implement stability detection in pollSessions()
- manager.test.ts: Add 4 tests for stability detection behavior

* test(tmux-subagent): improve stability detection tests to properly verify age gate

- First test now sets session age >10s and verifies 3 polls don't close
- Last test now does 5 polls to prove age gate prevents closure
- Added comments explaining what each poll does
This commit is contained in:
itsmylife44
2026-02-01 11:11:35 +01:00
committed by GitHub
parent c73314f643
commit 6389da3cd6
4 changed files with 315 additions and 2 deletions
+223
View File
@@ -75,6 +75,7 @@ const trackedSessions = new Set<string>()
function createMockContext(overrides?: {
sessionStatusResult?: { data?: Record<string, { type: string }> }
sessionMessagesResult?: { data?: unknown[] }
}) {
return {
serverUrl: new URL('http://localhost:4096'),
@@ -90,6 +91,12 @@ function createMockContext(overrides?: {
}
return { data }
}),
messages: mock(async () => {
if (overrides?.sessionMessagesResult) {
return overrides.sessionMessagesResult
}
return { data: [] }
}),
},
},
} as any
@@ -549,6 +556,222 @@ describe('TmuxSessionManager', () => {
expect(mockExecuteAction).toHaveBeenCalledTimes(2)
})
})
describe('Stability Detection (Issue #1330)', () => {
test('does NOT close session immediately when idle - requires 4 polls (1 baseline + 3 stable)', async () => {
//#given - session that is old enough (>10s) and idle
mockIsInsideTmux.mockReturnValue(true)
const { TmuxSessionManager } = await import('./manager')
const statusMock = mock(async () => ({
data: { 'ses_child': { type: 'idle' } }
}))
const messagesMock = mock(async () => ({
data: [{ id: 'msg1' }] // Same message count each time
}))
const ctx = {
serverUrl: new URL('http://localhost:4096'),
client: {
session: {
status: statusMock,
messages: messagesMock,
},
},
} as any
const config: TmuxConfig = {
enabled: true,
layout: 'main-vertical',
main_pane_size: 60,
main_pane_min_width: 80,
agent_pane_min_width: 40,
}
const manager = new TmuxSessionManager(ctx, config, mockTmuxDeps)
// Spawn a session first
await manager.onSessionCreated(
createSessionCreatedEvent('ses_child', 'ses_parent', 'Task')
)
// Make session old enough for stability detection (>10s)
const sessions = (manager as any).sessions as Map<string, any>
const tracked = sessions.get('ses_child')
tracked.createdAt = new Date(Date.now() - 15000) // 15 seconds ago
mockExecuteAction.mockClear()
//#when - poll only 3 times (need 4: 1 baseline + 3 stable)
await (manager as any).pollSessions() // sets lastMessageCount = 1
await (manager as any).pollSessions() // stableIdlePolls = 1
await (manager as any).pollSessions() // stableIdlePolls = 2
//#then - should NOT have closed yet (need one more poll)
expect(mockExecuteAction).not.toHaveBeenCalled()
})
test('closes session after 3 consecutive stable idle polls', async () => {
//#given
mockIsInsideTmux.mockReturnValue(true)
const { TmuxSessionManager } = await import('./manager')
const statusMock = mock(async () => ({
data: { 'ses_child': { type: 'idle' } }
}))
const messagesMock = mock(async () => ({
data: [{ id: 'msg1' }] // Same message count each time
}))
const ctx = {
serverUrl: new URL('http://localhost:4096'),
client: {
session: {
status: statusMock,
messages: messagesMock,
},
},
} as any
const config: TmuxConfig = {
enabled: true,
layout: 'main-vertical',
main_pane_size: 60,
main_pane_min_width: 80,
agent_pane_min_width: 40,
}
const manager = new TmuxSessionManager(ctx, config, mockTmuxDeps)
await manager.onSessionCreated(
createSessionCreatedEvent('ses_child', 'ses_parent', 'Task')
)
// Simulate session being old enough (>10s) by manipulating createdAt
const sessions = (manager as any).sessions as Map<string, any>
const tracked = sessions.get('ses_child')
tracked.createdAt = new Date(Date.now() - 15000) // 15 seconds ago
mockExecuteAction.mockClear()
//#when - poll 4 times (1st sets lastMessageCount, then 3 stable polls)
await (manager as any).pollSessions() // sets lastMessageCount = 1
await (manager as any).pollSessions() // stableIdlePolls = 1
await (manager as any).pollSessions() // stableIdlePolls = 2
await (manager as any).pollSessions() // stableIdlePolls = 3 -> close
//#then - should have closed the session
expect(mockExecuteAction).toHaveBeenCalled()
const call = mockExecuteAction.mock.calls[0]
expect(call![0].type).toBe('close')
})
test('resets stability counter when new messages arrive', async () => {
//#given
mockIsInsideTmux.mockReturnValue(true)
const { TmuxSessionManager } = await import('./manager')
let messageCount = 1
const statusMock = mock(async () => ({
data: { 'ses_child': { type: 'idle' } }
}))
const messagesMock = mock(async () => {
// Simulate new messages arriving each poll
messageCount++
return { data: Array(messageCount).fill({ id: 'msg' }) }
})
const ctx = {
serverUrl: new URL('http://localhost:4096'),
client: {
session: {
status: statusMock,
messages: messagesMock,
},
},
} as any
const config: TmuxConfig = {
enabled: true,
layout: 'main-vertical',
main_pane_size: 60,
main_pane_min_width: 80,
agent_pane_min_width: 40,
}
const manager = new TmuxSessionManager(ctx, config, mockTmuxDeps)
await manager.onSessionCreated(
createSessionCreatedEvent('ses_child', 'ses_parent', 'Task')
)
const sessions = (manager as any).sessions as Map<string, any>
const tracked = sessions.get('ses_child')
tracked.createdAt = new Date(Date.now() - 15000)
mockExecuteAction.mockClear()
//#when - poll multiple times (message count keeps changing)
await (manager as any).pollSessions()
await (manager as any).pollSessions()
await (manager as any).pollSessions()
await (manager as any).pollSessions()
//#then - should NOT have closed (stability never reached due to changing messages)
expect(mockExecuteAction).not.toHaveBeenCalled()
})
test('does NOT apply stability detection for sessions younger than 10s', async () => {
//#given - freshly created session (age < 10s)
mockIsInsideTmux.mockReturnValue(true)
const { TmuxSessionManager } = await import('./manager')
const statusMock = mock(async () => ({
data: { 'ses_child': { type: 'idle' } }
}))
const messagesMock = mock(async () => ({
data: [{ id: 'msg1' }] // Same message count - would trigger close if age check wasn't there
}))
const ctx = {
serverUrl: new URL('http://localhost:4096'),
client: {
session: {
status: statusMock,
messages: messagesMock,
},
},
} as any
const config: TmuxConfig = {
enabled: true,
layout: 'main-vertical',
main_pane_size: 60,
main_pane_min_width: 80,
agent_pane_min_width: 40,
}
const manager = new TmuxSessionManager(ctx, config, mockTmuxDeps)
await manager.onSessionCreated(
createSessionCreatedEvent('ses_child', 'ses_parent', 'Task')
)
// Session is fresh (createdAt is now) - don't manipulate it
// This tests the 10s age gate - stability detection should NOT activate
mockExecuteAction.mockClear()
//#when - poll 5 times (more than enough to close if age check wasn't there)
await (manager as any).pollSessions() // Would set lastMessageCount if age check passed
await (manager as any).pollSessions() // Would be stableIdlePolls = 1
await (manager as any).pollSessions() // Would be stableIdlePolls = 2
await (manager as any).pollSessions() // Would be stableIdlePolls = 3 -> would close
await (manager as any).pollSessions() // Extra poll to be sure
//#then - should NOT have closed (session too young for stability detection)
expect(mockExecuteAction).not.toHaveBeenCalled()
})
})
})
describe('DecisionEngine', () => {