Repository navigation
fix(terminal): keep the wake record when the shell shows through a hibernation kill #27295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
deepbluejay
wants to merge
3
commits into
stablyai:main
from
deepbluejay:fix/hibernation-wake-record-cleared
Closed
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
c7ebe6f
fix(terminal): keep the wake record when the shell shows through a hi…
deepbluejay 40c4ecd
fix(terminal): keep the wake record when the confirmation settles aft…
deepbluejay 0ce0221
fix(terminal): clear the wake record once another PTY is bound
deepbluejay File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
220 changes: 220 additions & 0 deletions
220
...rer/src/components/terminal-pane/pty-connection-hibernation-kill-shell-foreground.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,220 @@ | ||
| import type * as React from 'react' | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' | ||
| import { makePaneKey } from '../../../../shared/stable-pane-id' | ||
| import { flushAsyncTicks } from './pty-connection-test-async' | ||
| import { | ||
| LEAF_1, | ||
| createMockTransport, | ||
| createPane, | ||
| createManager, | ||
| type ConnectCallbacks, | ||
| type MockTransport | ||
| } from './pty-connection-test-pane-fixtures' | ||
| import type { StoreState } from './pty-connection-test-store-state' | ||
| import { buildPaneConnectionDeps } from './pty-connection-test-deps' | ||
| import { createInitialStoreState } from './pty-connection-test-store-fixtures' | ||
| import { | ||
| installTerminalTestGlobals, | ||
| restoreTerminalTestGlobals | ||
| } from './pty-connection-test-environment' | ||
|
|
||
| const { | ||
| resetAndRefreshAllTerminalWebglAtlases, | ||
| scheduleTerminalWebglAtlasRecovery, | ||
| scheduleRuntimeGraphSync, | ||
| shouldSeedCacheTimerOnInitialTitle, | ||
| toastInfo, | ||
| notifyCodexPaneBoundForStaleSweep | ||
| } = vi.hoisted(() => ({ | ||
| resetAndRefreshAllTerminalWebglAtlases: vi.fn(), | ||
| scheduleTerminalWebglAtlasRecovery: vi.fn(), | ||
| scheduleRuntimeGraphSync: vi.fn(), | ||
| shouldSeedCacheTimerOnInitialTitle: vi.fn(() => false), | ||
| toastInfo: vi.fn(), | ||
| notifyCodexPaneBoundForStaleSweep: vi.fn() | ||
| })) | ||
|
|
||
| let mockStoreState: StoreState | ||
| let transportFactoryQueue: MockTransport[] = [] | ||
| let createdTransportOptions: Record<string, unknown>[] = [] | ||
| let storeSubscribers: ((state: StoreState) => void)[] = [] | ||
|
|
||
| vi.mock('@/runtime/sync-runtime-graph', () => ({ | ||
| scheduleRuntimeGraphSync | ||
| })) | ||
|
|
||
| vi.mock('@/lib/pane-manager/pane-manager-registry', async (importOriginal) => ({ | ||
| ...(await importOriginal<Record<string, unknown>>()), | ||
| resetAndRefreshAllTerminalWebglAtlases | ||
| })) | ||
|
|
||
| vi.mock('./terminal-webgl-atlas-recovery', () => ({ | ||
| scheduleTerminalWebglAtlasRecovery | ||
| })) | ||
|
|
||
| vi.mock('@/store', () => ({ | ||
| useAppStore: { | ||
| getState: () => mockStoreState, | ||
| subscribe: (listener: (state: StoreState) => void) => { | ||
| storeSubscribers.push(listener) | ||
| return () => { | ||
| storeSubscribers = storeSubscribers.filter((candidate) => candidate !== listener) | ||
| } | ||
| } | ||
| } | ||
| })) | ||
|
|
||
| vi.mock('@/lib/agent-status', async (importOriginal) => { | ||
| const { buildAgentStatusModuleMock } = await import('./pty-connection-test-environment') | ||
| return buildAgentStatusModuleMock(await importOriginal<Record<string, unknown>>()) | ||
| }) | ||
|
|
||
| vi.mock('./cache-timer-seeding', () => ({ | ||
| shouldSeedCacheTimerOnInitialTitle | ||
| })) | ||
|
|
||
| vi.mock('sonner', () => ({ | ||
| toast: { | ||
| info: toastInfo | ||
| } | ||
| })) | ||
|
|
||
| vi.mock('@/lib/codex-stale-pane-sweep', () => ({ | ||
| notifyCodexPaneBoundForStaleSweep | ||
| })) | ||
|
|
||
| // Why: useCallback must pass through; no test here renders React. | ||
| vi.mock('react', async (importOriginal) => { | ||
| const actual = await importOriginal<typeof React>() | ||
| return { | ||
| ...actual, | ||
| useCallback: <T extends (...args: unknown[]) => unknown>(fn: T): T => fn | ||
| } | ||
| }) | ||
|
|
||
| vi.mock('./pty-transport', () => ({ | ||
| createIpcPtyTransport: vi.fn((options: Record<string, unknown>) => { | ||
| createdTransportOptions.push(options) | ||
| const nextTransport = transportFactoryQueue.shift() | ||
| if (!nextTransport) { | ||
| throw new Error('No mock transport queued') | ||
| } | ||
| return nextTransport | ||
| }) | ||
| })) | ||
|
|
||
| vi.mock('./remote-runtime-pty-transport', () => ({ | ||
| createRemoteRuntimePtyTransport: vi.fn( | ||
| (_environmentId: string, options: Record<string, unknown>) => { | ||
| createdTransportOptions.push(options) | ||
| const nextTransport = transportFactoryQueue.shift() | ||
| if (!nextTransport) { | ||
| throw new Error('No mock transport queued') | ||
| } | ||
| return nextTransport | ||
| } | ||
| ) | ||
| })) | ||
|
|
||
| // Why: stub only getEagerPtyBufferHandle so tests can simulate a live eager buffer (adopt path) without standing up the real IPC dispatcher. | ||
| vi.mock('./pty-dispatcher', async (importOriginal) => { | ||
| const actual = await importOriginal<Record<string, unknown>>() | ||
| return { | ||
| ...actual, | ||
| getEagerPtyBufferHandle: vi.fn(() => undefined) | ||
| } | ||
| }) | ||
|
|
||
| function createDeps(overrides: Record<string, unknown> = {}) { | ||
| return buildPaneConnectionDeps(() => mockStoreState, overrides) | ||
| } | ||
|
|
||
| // A hibernation kill ends the agent before the PTY, so the pane sees its own shell for a moment. | ||
| describe('shell showing through a hibernation kill', () => { | ||
| beforeEach(async () => { | ||
| vi.resetModules() | ||
| vi.clearAllMocks() | ||
| transportFactoryQueue = [] | ||
| createdTransportOptions = [] | ||
| storeSubscribers = [] | ||
| mockStoreState = createInitialStoreState(() => mockStoreState) | ||
| await installTerminalTestGlobals() | ||
| }) | ||
|
|
||
| afterEach(async () => { | ||
| await restoreTerminalTestGlobals() | ||
| }) | ||
|
|
||
| async function connectHibernatingPane(suppressExit: boolean): Promise<{ | ||
| finishCommand: () => void | ||
| paneKey: string | ||
| }> { | ||
| vi.useFakeTimers() | ||
| const { connectPanePty } = await import('./pty-connection') | ||
| vi.mocked(window.api.pty.confirmForegroundProcess).mockResolvedValue('zsh') | ||
| const dataCallbackRef: { current: ((data: string) => void) | null } = { current: null } | ||
| const ptyId = 'pty-hibernating' | ||
| const transport = createMockTransport(ptyId) | ||
| transport.connect.mockImplementation(async ({ callbacks }: { callbacks: ConnectCallbacks }) => { | ||
| dataCallbackRef.current = callbacks.onData ?? null | ||
| return { id: ptyId } | ||
| }) | ||
| transportFactoryQueue.push(transport) | ||
| const paneKey = makePaneKey('tab-1', LEAF_1) | ||
| // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the fixtures implement the pane, manager and deps members connectPanePty reads. | ||
| const args = [ | ||
| createPane(1), | ||
| createManager(1), | ||
| createDeps({ isVisibleRef: { current: false } }) | ||
| ] as unknown as Parameters<typeof connectPanePty> | ||
| connectPanePty(...args) | ||
| await vi.advanceTimersByTimeAsync(20) | ||
| await flushAsyncTicks() | ||
| mockStoreState.agentLaunchConfigByPaneKey[paneKey] = { | ||
| launchConfig: { agentArgs: '', agentEnv: {} }, | ||
| identity: { agentType: 'claude' } | ||
| } | ||
| // Hibernation writes the wake record and suppresses the exit before it kills the PTY. | ||
| mockStoreState.sleepingAgentSessionsByPaneKey[paneKey] = { | ||
| paneKey, | ||
| tabId: 'tab-1', | ||
| worktreeId: 'wt-1', | ||
| agent: 'claude', | ||
| providerSession: { key: 'session_id', id: 'sess-hibernating' }, | ||
| prompt: '', | ||
| state: 'done', | ||
| capturedAt: 1, | ||
| updatedAt: 1, | ||
| origin: 'worktree-sleep' | ||
| } | ||
| if (suppressExit) { | ||
| mockStoreState.suppressedPtyExitIds[ptyId] = true | ||
| } | ||
| return { paneKey, finishCommand: () => dataCallbackRef.current?.('\x1b]133;D;0\x07') } | ||
| } | ||
|
|
||
| it('keeps the wake record while the kill is in flight', async () => { | ||
| const pane = await connectHibernatingPane(true) | ||
|
|
||
| pane.finishCommand() | ||
| await vi.advanceTimersByTimeAsync(350) | ||
|
|
||
| expect(mockStoreState.paneForegroundAgentByPaneKey[pane.paneKey]).toEqual({ | ||
| agent: null, | ||
| shellForeground: true | ||
| }) | ||
| expect(mockStoreState.clearSleepingAgentSession).not.toHaveBeenCalled() | ||
| expect(mockStoreState.sleepingAgentSessionsByPaneKey[pane.paneKey]).toMatchObject({ | ||
| providerSession: { id: 'sess-hibernating' } | ||
| }) | ||
| }) | ||
|
|
||
| it('still clears a record beside a PTY that is not being hibernated', async () => { | ||
| const pane = await connectHibernatingPane(false) | ||
|
|
||
| pane.finishCommand() | ||
| await vi.advanceTimersByTimeAsync(350) | ||
|
|
||
| expect(mockStoreState.clearSleepingAgentSession).toHaveBeenCalledExactlyOnceWith(pane.paneKey) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.