From 1b232f760db90242fa72657ed3fb1a6bb4350183 Mon Sep 17 00:00:00 2001 From: Jinwoo-H Date: Sat, 25 Apr 2026 03:55:01 -0400 Subject: [PATCH] Log terminal lifecycle anomalies --- .../terminal-pane/pty-connection.ts | 28 +++++++++++++-- .../terminal-lifecycle-diagnostics.ts | 34 +++++++++++++++++++ .../src/runtime/sync-runtime-graph.ts | 18 ++++++++-- 3 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 src/renderer/src/components/terminal-pane/terminal-lifecycle-diagnostics.ts diff --git a/src/renderer/src/components/terminal-pane/pty-connection.ts b/src/renderer/src/components/terminal-pane/pty-connection.ts index 23d971f9f..7213b1a11 100644 --- a/src/renderer/src/components/terminal-pane/pty-connection.ts +++ b/src/renderer/src/components/terminal-pane/pty-connection.ts @@ -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>() @@ -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 diff --git a/src/renderer/src/components/terminal-pane/terminal-lifecycle-diagnostics.ts b/src/renderer/src/components/terminal-pane/terminal-lifecycle-diagnostics.ts new file mode 100644 index 000000000..38fb6a2c3 --- /dev/null +++ b/src/renderer/src/components/terminal-pane/terminal-lifecycle-diagnostics.ts @@ -0,0 +1,34 @@ +type TerminalLifecycleDiagnosticDetails = { + tabId?: string + worktreeId?: string + leafId?: string | null + paneId?: number + ptyId?: string | null + reason?: string +} + +const emittedDiagnostics = new Set() +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) +} diff --git a/src/renderer/src/runtime/sync-runtime-graph.ts b/src/renderer/src/runtime/sync-runtime-graph.ts index 46dc9722f..3ebe8fb08 100644 --- a/src/renderer/src/runtime/sync-runtime-graph.ts +++ b/src/renderer/src/runtime/sync-runtime-graph.ts @@ -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 { 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 }) } }