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
+66 -2
View File
@@ -33,6 +33,11 @@ const defaultTmuxDeps: TmuxUtilDeps = {
const SESSION_TIMEOUT_MS = 10 * 60 * 1000
// Stability detection constants (prevents premature closure - see issue #1330)
// Mirrors the proven pattern from background-agent/manager.ts
const MIN_STABILITY_TIME_MS = 10 * 1000 // Must run at least 10s before stability detection kicks in
const STABLE_POLLS_REQUIRED = 3 // 3 consecutive idle polls (~6s with 2s poll interval)
/**
* State-first Tmux Session Manager
*
@@ -324,18 +329,77 @@ export class TmuxSessionManager {
const missingSince = !status ? now - tracked.lastSeenAt.getTime() : 0
const missingTooLong = missingSince >= SESSION_MISSING_GRACE_MS
const isTimedOut = now - tracked.createdAt.getTime() > SESSION_TIMEOUT_MS
const elapsedMs = now - tracked.createdAt.getTime()
// Stability detection: Don't close immediately on idle
// Wait for STABLE_POLLS_REQUIRED consecutive polls with same message count
let shouldCloseViaStability = false
if (isIdle && elapsedMs >= MIN_STABILITY_TIME_MS) {
// Fetch message count to detect if agent is still producing output
try {
const messagesResult = await this.client.session.messages({
path: { id: sessionId }
})
const currentMsgCount = Array.isArray(messagesResult.data)
? messagesResult.data.length
: 0
if (tracked.lastMessageCount === currentMsgCount) {
// Message count unchanged - increment stable polls
tracked.stableIdlePolls = (tracked.stableIdlePolls ?? 0) + 1
if (tracked.stableIdlePolls >= STABLE_POLLS_REQUIRED) {
// Double-check status before closing
const recheckResult = await this.client.session.status({ path: undefined })
const recheckStatuses = (recheckResult.data ?? {}) as Record<string, { type: string }>
const recheckStatus = recheckStatuses[sessionId]
if (recheckStatus?.type === "idle") {
shouldCloseViaStability = true
} else {
// Status changed - reset stability counter
tracked.stableIdlePolls = 0
log("[tmux-session-manager] stability reached but session not idle on recheck, resetting", {
sessionId,
recheckStatus: recheckStatus?.type,
})
}
}
} else {
// New messages - agent is still working, reset stability counter
tracked.stableIdlePolls = 0
}
tracked.lastMessageCount = currentMsgCount
} catch (msgErr) {
log("[tmux-session-manager] failed to fetch messages for stability check", {
sessionId,
error: String(msgErr),
})
// On error, don't close - be conservative
}
} else if (!isIdle) {
// Not idle - reset stability counter
tracked.stableIdlePolls = 0
}
log("[tmux-session-manager] session check", {
sessionId,
statusType: status?.type,
isIdle,
elapsedMs,
stableIdlePolls: tracked.stableIdlePolls,
lastMessageCount: tracked.lastMessageCount,
missingSince,
missingTooLong,
isTimedOut,
shouldClose: isIdle || missingTooLong || isTimedOut,
shouldCloseViaStability,
})
if (isIdle || missingTooLong || isTimedOut) {
// Close if: stability detection confirmed OR missing too long OR timed out
// Note: We no longer close immediately on idle - stability detection handles that
if (shouldCloseViaStability || missingTooLong || isTimedOut) {
sessionsToClose.push(sessionId)
}
}