Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,8 @@ export function useTerminalPaneLifecycle({
// Why: TerminalPane instances stay mounted for hidden visited worktrees
// so PTYs survive navigation. Creating WebGL for those offscreen panes
// still consumes Chromium's context budget and can blank visible panes.
initialRenderingSuspended: !isVisibleRef.current
initialRenderingSuspended: !isVisibleRef.current,
debugLabel: `tab:${tabId}/wt:${worktreeId}`
})

managerRef.current = manager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ function createPane(): ManagedPaneInternal {
webglAddon: null,
ligaturesAddon: null,
compositionHandler: null,
debugLabel: null,
pendingSplitScrollState: {
wasAtBottom: true,
firstVisibleLineContent: '',
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/src/lib/pane-manager/pane-lifecycle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ function createPane(): ManagedPaneInternal {
webLinksAddon: {} as never,
webglAddon: null,
compositionHandler: null,
pendingSplitScrollState: null
pendingSplitScrollState: null,
debugLabel: null
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/renderer/src/lib/pane-manager/pane-lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ export function createPaneDOM(
webglAddon: null,
ligaturesAddon: null,
compositionHandler: null,
pendingSplitScrollState: null
pendingSplitScrollState: null,
debugLabel: options.debugLabel ?? null
}

// Focus handler: clicking a pane makes it active and explicitly focuses
Expand Down
5 changes: 5 additions & 0 deletions src/renderer/src/lib/pane-manager/pane-manager-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export type PaneManagerOptions = {
terminalOptions?: (paneId: number) => Partial<ITerminalOptions>
onLinkClick?: (event: MouseEvent | undefined, url: string) => void
initialRenderingSuspended?: boolean
// Why: diagnostic label for log correlation. safeFit and other internal
// helpers log warnings that are hard to correlate without knowing which
// tab/worktree the PaneManager belongs to.
debugLabel?: string
}

export type PaneStyleOptions = {
Expand Down Expand Up @@ -91,6 +95,7 @@ export type ManagedPaneInternal = {
// intermediate fit paths skip their own scroll restoration, deferring to
// the splitPane's final authoritative restore.
pendingSplitScrollState: ScrollState | null
debugLabel: string | null
} & ManagedPane

export type DropZone = 'top' | 'bottom' | 'left' | 'right'
3 changes: 2 additions & 1 deletion src/renderer/src/lib/pane-manager/pane-tree-ops.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ function createPane({
webglAddon: null,
ligaturesAddon: null,
compositionHandler: null,
pendingSplitScrollState: null
pendingSplitScrollState: null,
debugLabel: null
}
}

Expand Down
23 changes: 16 additions & 7 deletions src/renderer/src/lib/pane-manager/pane-tree-ops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,27 @@ export function safeFit(pane: ManagedPaneInternal): void {
// cross a cell boundary. Skipping those avoids FitAddon.clear()+refresh()
// churn, which was causing visible terminal blinking while resizing.
//
// Why: diagnostic for intermittent dead-terminal-after-split. If a
// just-reparented pane's proposed dimensions match its current
// dimensions (the default 80×24 at certain screen widths), this
// early-return skips fitAddon.fit() and no terminal.resize() fires
// — leaving the WebGL canvas at stale dimensions.
// Why: wrapInSplit() reparents the pane's container, which can leave
// the WebGL canvas stale even when proposed dimensions match current
// (the browser detaches and reattaches the canvas during the DOM move).
// When pendingSplitScrollState is set we must force a fit + refresh so
// the WebGL renderer repaints. Without this, the pane appears blank
// until something forces a dimension change.
if (pane.pendingSplitScrollState) {
console.warn(
'[terminal] safeFit early-return during pending split for pane',
'[terminal] safeFit forcing fit+refresh during pending split for pane',
pane.id,
`— dims ${dims.cols}×${dims.rows} match current, webgl:`,
!!pane.webglAddon
!!pane.webglAddon,
pane.debugLabel ? `(${pane.debugLabel})` : ''
)
pane.fitAddon.fit()
try {
pane.terminal.refresh(0, pane.terminal.rows - 1)
} catch {
/* ignore — terminal may not be fully initialised */
}
return
}
return
}
Expand Down
12 changes: 11 additions & 1 deletion src/renderer/src/runtime/sync-runtime-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ type RegisteredTerminalTab = {
}

const registeredTabs = new Map<string, RegisteredTerminalTab>()
// Why: track when each tab was registered so we can suppress the "no live
// transport" warning during the initial PTY connection window. The warning
// is noise when it fires on mount (PTY spawn/attach is async and hasn't
// finished yet), but valuable if the transport is still missing after the
// grace period — that indicates a real stuck state.
const tabRegisteredAt = new Map<string, number>()
const NO_TRANSPORT_GRACE_MS = 10_000
let syncScheduled = false
let syncEnabled = false
let getStoreState: (() => AppState) | null = null
Expand All @@ -23,9 +30,11 @@ export function setRuntimeGraphStoreStateGetter(getter: (() => AppState) | null)

export function registerRuntimeTerminalTab(tab: RegisteredTerminalTab): () => void {
registeredTabs.set(tab.tabId, tab)
tabRegisteredAt.set(tab.tabId, Date.now())
scheduleRuntimeGraphSync()
return () => {
registeredTabs.delete(tab.tabId)
tabRegisteredAt.delete(tab.tabId)
scheduleRuntimeGraphSync()
}
}
Expand Down Expand Up @@ -89,7 +98,8 @@ async function syncRuntimeGraph(): Promise<void> {
const leafId = paneLeafId(pane.id)
const ptyId = registeredTab.getPtyIdForPane(pane.id)
const savedPtyId = savedPtyIdsByLeafId[leafId] ?? null
if (!ptyId && savedPtyId) {
const registeredTime = tabRegisteredAt.get(tabId) ?? 0
if (!ptyId && savedPtyId && Date.now() - registeredTime > NO_TRANSPORT_GRACE_MS) {
warnTerminalLifecycleAnomaly('mounted terminal leaf has saved PTY but no live transport', {
tabId,
worktreeId: registeredTab.worktreeId,
Expand Down
168 changes: 168 additions & 0 deletions tests/e2e/dead-terminal-repro.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/**
* Stress test for dead-terminal reproduction (setup-split flow).
*
* Why @headful: the dead-terminal bug is a WebGL canvas staleness issue — after
* wrapInSplit() reparents the existing pane's container, the WebGL canvas can
* fail to repaint. In headless mode WebGL is NEVER active, so the DOM fallback
* renderer is used and the bug cannot manifest. Running headful ensures real
* WebGL contexts matching production.
*
* See helpers/dead-terminal.ts for the shared worktree-creation helper that
* replicates the exact activateAndRevealWorktree + ensureWorktreeHasInitialTerminal
* production flow.
*/

import { test, expect } from './helpers/orca-app'
import {
waitForSessionReady,
waitForActiveWorktree,
getActiveWorktreeId,
switchToWorktree,
ensureTerminalVisible
} from './helpers/store'
import { waitForActiveTerminalManager, waitForPaneCount } from './helpers/terminal'
import {
createAndActivateWorktreeWithSetup,
removeWorktreeViaStore,
waitForAllPanesToHaveContent,
checkWebglState
} from './helpers/dead-terminal'

const STRESS_ITERATIONS = 5

test.describe('Dead Terminal Reproduction @headful', () => {
const createdWorktreeIds: string[] = []

test.beforeEach(async ({ orcaPage }) => {
await waitForSessionReady(orcaPage)
await waitForActiveWorktree(orcaPage)
await ensureTerminalVisible(orcaPage)

await orcaPage.evaluate(async () => {
const state = window.__store?.getState()
if (!state) {
return
}
state.updateSettings({ setupScriptLaunchMode: 'split-vertical' })

// Why: write orca.yaml into the repo so createWorktree IPC returns a
// WorktreeSetupLaunch with a runner script, triggering the setup split.
// This is scoped to the dead-terminal tests to avoid breaking other
// specs that don't expect setup scripts to fire on worktree creation.
const wt = Object.values(state.worktreesByRepo)
.flat()
.find((w) => w.id === state.activeWorktreeId)
if (wt) {
const sep = wt.path.includes('\\') ? '\\' : '/'
await window.api.fs.writeFile({
filePath: `${wt.path}${sep}orca.yaml`,
content: 'scripts:\n setup: echo SETUP_COMPLETE\n'
})
}
})
})

test.afterEach(async ({ orcaPage }) => {
for (const id of createdWorktreeIds) {
await removeWorktreeViaStore(orcaPage, id)
}
createdWorktreeIds.length = 0
})

test('@headful setup-split flow does not produce dead terminals', async ({ orcaPage }) => {
test.setTimeout(120_000)
const homeWorktreeId = await waitForActiveWorktree(orcaPage)
await waitForActiveTerminalManager(orcaPage, 30_000)
await checkWebglState(orcaPage, 'home-initial')

for (let i = 0; i < STRESS_ITERATIONS; i++) {
const direction = i % 2 === 0 ? 'vertical' : 'horizontal'
const newId = await createAndActivateWorktreeWithSetup(orcaPage, `setup-${i}`, direction)
createdWorktreeIds.push(newId)

await expect.poll(async () => getActiveWorktreeId(orcaPage), { timeout: 10_000 }).toBe(newId)
await ensureTerminalVisible(orcaPage)
await waitForActiveTerminalManager(orcaPage, 30_000)
await waitForPaneCount(orcaPage, 2, 15_000)
await checkWebglState(orcaPage, `setup-${i}`)
await waitForAllPanesToHaveContent(orcaPage, `setup-${i} both panes`)

await switchToWorktree(orcaPage, homeWorktreeId)
await expect
.poll(async () => getActiveWorktreeId(orcaPage), { timeout: 10_000 })
.toBe(homeWorktreeId)
await removeWorktreeViaStore(orcaPage, newId)
createdWorktreeIds.pop()
}
})

test('@headful setup-split then switch-back does not leave panes dead', async ({ orcaPage }) => {
test.setTimeout(120_000)
const homeWorktreeId = await waitForActiveWorktree(orcaPage)
await waitForActiveTerminalManager(orcaPage, 30_000)

for (let i = 0; i < STRESS_ITERATIONS; i++) {
const newId = await createAndActivateWorktreeWithSetup(
orcaPage,
`switchback-${i}`,
'vertical'
)
createdWorktreeIds.push(newId)

await expect.poll(async () => getActiveWorktreeId(orcaPage), { timeout: 10_000 }).toBe(newId)
await ensureTerminalVisible(orcaPage)
await waitForActiveTerminalManager(orcaPage, 30_000)
await waitForPaneCount(orcaPage, 2, 15_000)
await waitForAllPanesToHaveContent(orcaPage, `switchback-${i} initial`)

await switchToWorktree(orcaPage, homeWorktreeId)
await expect
.poll(async () => getActiveWorktreeId(orcaPage), { timeout: 10_000 })
.toBe(homeWorktreeId)
await ensureTerminalVisible(orcaPage)
await waitForActiveTerminalManager(orcaPage, 15_000)

await switchToWorktree(orcaPage, newId)
await expect.poll(async () => getActiveWorktreeId(orcaPage), { timeout: 10_000 }).toBe(newId)
await ensureTerminalVisible(orcaPage)
await waitForActiveTerminalManager(orcaPage, 15_000)
await waitForAllPanesToHaveContent(orcaPage, `switchback-${i} after return`)

await switchToWorktree(orcaPage, homeWorktreeId)
await expect
.poll(async () => getActiveWorktreeId(orcaPage), { timeout: 10_000 })
.toBe(homeWorktreeId)
await removeWorktreeViaStore(orcaPage, newId)
createdWorktreeIds.pop()
}
})

test('@headful rapid switching between many setup-split worktrees', async ({ orcaPage }) => {
test.setTimeout(120_000)
const homeWorktreeId = await waitForActiveWorktree(orcaPage)
await waitForActiveTerminalManager(orcaPage, 30_000)

const worktreeIds = [homeWorktreeId]
for (let i = 0; i < 4; i++) {
const newId = await createAndActivateWorktreeWithSetup(orcaPage, `multi-${i}`, 'vertical')
createdWorktreeIds.push(newId)
worktreeIds.push(newId)

await expect.poll(async () => getActiveWorktreeId(orcaPage), { timeout: 10_000 }).toBe(newId)
await ensureTerminalVisible(orcaPage)
await waitForActiveTerminalManager(orcaPage, 30_000)
await waitForPaneCount(orcaPage, 2, 15_000)
await waitForAllPanesToHaveContent(orcaPage, `multi-create-${i}`)
}

for (let round = 0; round < 3; round++) {
for (const wId of worktreeIds) {
await switchToWorktree(orcaPage, wId)
await expect.poll(async () => getActiveWorktreeId(orcaPage), { timeout: 10_000 }).toBe(wId)
await ensureTerminalVisible(orcaPage)
await waitForActiveTerminalManager(orcaPage, 15_000)
await waitForAllPanesToHaveContent(orcaPage, `multi-r${round}-${wId.slice(0, 8)}`)
}
}
})
})
Loading
Loading