Log terminal lifecycle anomalies

This commit is contained in:
Jinwoo-H
2026-04-26 00:23:53 -04:00
parent 54c46b1f22
commit 1b232f760d
3 changed files with 76 additions and 4 deletions
@@ -11,7 +11,12 @@ import { createIpcPtyTransport } from './pty-transport'
import { shouldSeedCacheTimerOnInitialTitle } from './cache-timer-seeding'
import type { PtyConnectionDeps } from './pty-connection-types'
import { isPaneReplaying, replayIntoTerminal } from './replay-guard'
import { POST_REPLAY_MODE_RESET, POST_REPLAY_FOCUS_REPORTING_RESET } from './layout-serialization'
import {
paneLeafId,
POST_REPLAY_MODE_RESET,
POST_REPLAY_FOCUS_REPORTING_RESET
} from './layout-serialization'
import { warnTerminalLifecycleAnomaly } from './terminal-lifecycle-diagnostics'
const pendingSpawnByTabId = new Map<string, Promise<string | null>>()
@@ -462,6 +467,13 @@ export function connectPanePty(
const ptyId =
connectResult?.id ?? (typeof result === 'string' ? result : transport.getPtyId())
if (!ptyId) {
warnTerminalLifecycleAnomaly('restored PTY reattach returned no PTY id', {
tabId: deps.tabId,
worktreeId: deps.worktreeId,
leafId: deps.restoredLeafId ?? paneLeafId(pane.id),
paneId: pane.id,
ptyId: staleSessionId ?? null
})
// Why: a stale restored daemon/SSH session can fail reattach after the
// pane is mounted. Do not leave xterm alive without a backing PTY.
deps.syncPanePtyLayoutBinding(pane.id, null)
@@ -663,7 +675,19 @@ export function connectPanePty(
handleReattachResult(result, deferredReattachSessionId)
})
.catch((err) => {
reportError(err instanceof Error ? err.message : String(err))
const message = err instanceof Error ? err.message : String(err)
warnTerminalLifecycleAnomaly('restored PTY reattach threw', {
tabId: deps.tabId,
worktreeId: deps.worktreeId,
leafId: deps.restoredLeafId ?? paneLeafId(pane.id),
paneId: pane.id,
ptyId: deferredReattachSessionId,
reason: message
})
reportError(message)
deps.syncPanePtyLayoutBinding(pane.id, null)
deps.clearTabPtyId(deps.tabId, deferredReattachSessionId)
startFreshSpawn()
})
} else if (detachedLivePtyId) {
allowInitialIdleCacheSeed = false
@@ -0,0 +1,34 @@
type TerminalLifecycleDiagnosticDetails = {
tabId?: string
worktreeId?: string
leafId?: string | null
paneId?: number
ptyId?: string | null
reason?: string
}
const emittedDiagnostics = new Set<string>()
const MAX_EMITTED_DIAGNOSTICS = 500
export function warnTerminalLifecycleAnomaly(
event: string,
details: TerminalLifecycleDiagnosticDetails
): void {
const key = [
event,
details.tabId ?? '',
details.worktreeId ?? '',
details.leafId ?? '',
details.paneId ?? '',
details.ptyId ?? '',
details.reason ?? ''
].join('|')
if (emittedDiagnostics.has(key)) {
return
}
if (emittedDiagnostics.size >= MAX_EMITTED_DIAGNOSTICS) {
emittedDiagnostics.clear()
}
emittedDiagnostics.add(key)
console.warn(`[terminal-lifecycle] ${event}`, details)
}
+16 -2
View File
@@ -1,4 +1,5 @@
import { paneLeafId, serializePaneTree } from '@/components/terminal-pane/layout-serialization'
import { warnTerminalLifecycleAnomaly } from '@/components/terminal-pane/terminal-lifecycle-diagnostics'
import type { PaneManager } from '@/lib/pane-manager/pane-manager'
import type { AppState } from '@/store/types'
import type { RuntimeSyncWindowGraph } from '../../../shared/runtime-types'
@@ -83,13 +84,26 @@ async function syncRuntimeGraph(): Promise<void> {
layout: serializePaneTree(root)
})
const savedPtyIdsByLeafId = state.terminalLayoutsByTabId[tabId]?.ptyIdsByLeafId ?? {}
for (const pane of manager?.getPanes() ?? []) {
const leafId = paneLeafId(pane.id)
const ptyId = registeredTab.getPtyIdForPane(pane.id)
const savedPtyId = savedPtyIdsByLeafId[leafId] ?? null
if (!ptyId && savedPtyId) {
warnTerminalLifecycleAnomaly('mounted terminal leaf has saved PTY but no live transport', {
tabId,
worktreeId: registeredTab.worktreeId,
leafId,
paneId: pane.id,
ptyId: savedPtyId
})
}
graph.leaves.push({
tabId,
worktreeId: registeredTab.worktreeId,
leafId: paneLeafId(pane.id),
leafId,
paneRuntimeId: pane.id,
ptyId: registeredTab.getPtyIdForPane(pane.id)
ptyId
})
}
}