fix(codex): re-confirm spurious shell readings before skipping the restart card (#11076)
An account switch decides pane eligibility from a single cached inspectProcess read. When that read reports the pane's shell for a live Codex session, the pane silently loses its restart card - no error, no retry. Re-check shell readings on Orca-launched Codex panes with the existing fresh-scan confirmForegroundProcess before trusting them; only an affirmative codex answer flips the decision, so a genuine exit to the shell stays uncarded and unsupported providers keep today's behavior.
This commit is contained in:
@@ -0,0 +1,209 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { useAppStore } from '@/store'
|
||||
import {
|
||||
markLiveCodexSessionsForRestart,
|
||||
markRestoredStaleCodexSessionsForRestart
|
||||
} from './codex-session-restart'
|
||||
import {
|
||||
createCompatibleRuntimeStatusResponseIfNeeded,
|
||||
type RuntimeEnvironmentCallRequest
|
||||
} from '@/runtime/runtime-compatibility-test-fixture'
|
||||
import { clearRuntimeCompatibilityCacheForTests } from '@/runtime/runtime-rpc-client'
|
||||
|
||||
const ACCOUNT_A = 'account-a@example.com'
|
||||
const ACCOUNT_B = 'account-b@example.com'
|
||||
|
||||
/**
|
||||
* The cached inspection can flap to the pane's shell for a live Codex session
|
||||
* (#11064), and the switch path has no retry — one spurious reading silently
|
||||
* loses the restart card forever. These pin the fresh-scan re-confirmation.
|
||||
*/
|
||||
describe('spurious shell readings on Codex-launched panes', () => {
|
||||
const originalWindow = (globalThis as { window?: typeof window }).window
|
||||
const runtimeEnvironmentCall = vi.fn()
|
||||
const runtimeEnvironmentTransportCall = vi.fn()
|
||||
|
||||
function seedPane(args: { ptyId?: string; launchAgent?: 'codex' } = {}): void {
|
||||
const ptyId = args.ptyId ?? 'pty-1'
|
||||
useAppStore.setState({
|
||||
settings: null as never,
|
||||
tabsByWorktree: {
|
||||
wt1: [
|
||||
{
|
||||
id: 'tab-1',
|
||||
ptyId,
|
||||
worktreeId: 'wt1',
|
||||
title: 'orca-1',
|
||||
customTitle: null,
|
||||
color: null,
|
||||
sortOrder: 0,
|
||||
createdAt: 1,
|
||||
...(args.launchAgent ? { launchAgent: args.launchAgent } : {})
|
||||
}
|
||||
]
|
||||
},
|
||||
ptyIdsByTabId: { 'tab-1': [ptyId] },
|
||||
pendingCodexPaneRestartIds: {},
|
||||
codexRestartNoticeByPtyId: {}
|
||||
})
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
clearRuntimeCompatibilityCacheForTests()
|
||||
runtimeEnvironmentCall.mockReset()
|
||||
runtimeEnvironmentTransportCall.mockReset()
|
||||
runtimeEnvironmentTransportCall.mockImplementation((args: RuntimeEnvironmentCallRequest) => {
|
||||
return createCompatibleRuntimeStatusResponseIfNeeded(args) ?? runtimeEnvironmentCall(args)
|
||||
})
|
||||
seedPane({ launchAgent: 'codex' })
|
||||
;(globalThis as { window: typeof window }).window = {
|
||||
...originalWindow,
|
||||
api: {
|
||||
...originalWindow?.api,
|
||||
pty: {
|
||||
...originalWindow?.api?.pty,
|
||||
inspectProcess: vi
|
||||
.fn()
|
||||
.mockResolvedValue({ foregroundProcess: 'zsh', hasChildProcesses: false }),
|
||||
confirmForegroundProcess: vi.fn().mockResolvedValue(null)
|
||||
},
|
||||
codexAccounts: {
|
||||
...originalWindow?.api?.codexAccounts,
|
||||
list: vi.fn().mockResolvedValue({
|
||||
accounts: [
|
||||
{ id: 'account-a', email: ACCOUNT_A },
|
||||
{ id: 'account-b', email: ACCOUNT_B }
|
||||
],
|
||||
activeAccountId: 'account-b'
|
||||
}),
|
||||
listStalePanes: vi.fn().mockResolvedValue([])
|
||||
},
|
||||
runtimeEnvironments: {
|
||||
...originalWindow?.api?.runtimeEnvironments,
|
||||
call: runtimeEnvironmentTransportCall
|
||||
}
|
||||
}
|
||||
} as unknown as typeof window
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
useAppStore.setState({ settings: null as never })
|
||||
if (originalWindow) {
|
||||
;(globalThis as { window: typeof window }).window = originalWindow
|
||||
} else {
|
||||
delete (globalThis as { window?: typeof window }).window
|
||||
}
|
||||
})
|
||||
|
||||
it('re-confirms with the fresh scan and cards the pane when codex still owns it', async () => {
|
||||
vi.mocked(window.api.pty.confirmForegroundProcess).mockResolvedValue('codex-aarch64-ap')
|
||||
|
||||
await markLiveCodexSessionsForRestart({
|
||||
previousAccountLabel: ACCOUNT_A,
|
||||
nextAccountLabel: ACCOUNT_B
|
||||
})
|
||||
|
||||
expect(window.api.pty.confirmForegroundProcess).toHaveBeenCalledWith('pty-1')
|
||||
expect(useAppStore.getState().codexRestartNoticeByPtyId['pty-1']).toEqual({
|
||||
previousAccountLabel: ACCOUNT_A,
|
||||
nextAccountLabel: ACCOUNT_B
|
||||
})
|
||||
})
|
||||
|
||||
it('keeps the pane uncarded when the fresh scan confirms the shell (user exited Codex)', async () => {
|
||||
vi.mocked(window.api.pty.confirmForegroundProcess).mockResolvedValue('zsh')
|
||||
|
||||
await markLiveCodexSessionsForRestart({
|
||||
previousAccountLabel: ACCOUNT_A,
|
||||
nextAccountLabel: ACCOUNT_B
|
||||
})
|
||||
|
||||
expect(useAppStore.getState().codexRestartNoticeByPtyId).toEqual({})
|
||||
})
|
||||
|
||||
it('treats a rejected confirmation as no new evidence', async () => {
|
||||
vi.mocked(window.api.pty.confirmForegroundProcess).mockRejectedValue(new Error('daemon busy'))
|
||||
|
||||
await markLiveCodexSessionsForRestart({
|
||||
previousAccountLabel: ACCOUNT_A,
|
||||
nextAccountLabel: ACCOUNT_B
|
||||
})
|
||||
|
||||
expect(useAppStore.getState().codexRestartNoticeByPtyId).toEqual({})
|
||||
})
|
||||
|
||||
it('survives a preload without confirmForegroundProcess', async () => {
|
||||
;(
|
||||
window.api.pty as unknown as { confirmForegroundProcess?: unknown }
|
||||
).confirmForegroundProcess = undefined
|
||||
|
||||
await markLiveCodexSessionsForRestart({
|
||||
previousAccountLabel: ACCOUNT_A,
|
||||
nextAccountLabel: ACCOUNT_B
|
||||
})
|
||||
|
||||
expect(useAppStore.getState().codexRestartNoticeByPtyId).toEqual({})
|
||||
})
|
||||
|
||||
it('never spends the fresh scan on a pane Orca did not launch Codex in', async () => {
|
||||
seedPane()
|
||||
|
||||
await markLiveCodexSessionsForRestart({
|
||||
previousAccountLabel: ACCOUNT_A,
|
||||
nextAccountLabel: ACCOUNT_B
|
||||
})
|
||||
|
||||
expect(window.api.pty.confirmForegroundProcess).not.toHaveBeenCalled()
|
||||
expect(useAppStore.getState().codexRestartNoticeByPtyId).toEqual({})
|
||||
})
|
||||
|
||||
it('never spends the fresh scan when the cached reading already answers', async () => {
|
||||
vi.mocked(window.api.pty.inspectProcess).mockResolvedValue({
|
||||
foregroundProcess: 'codex',
|
||||
hasChildProcesses: true
|
||||
})
|
||||
|
||||
await markLiveCodexSessionsForRestart({
|
||||
previousAccountLabel: ACCOUNT_A,
|
||||
nextAccountLabel: ACCOUNT_B
|
||||
})
|
||||
|
||||
expect(useAppStore.getState().codexRestartNoticeByPtyId['pty-1']).toBeDefined()
|
||||
expect(window.api.pty.confirmForegroundProcess).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('never routes a remote runtime pane through the local confirm bridge', async () => {
|
||||
seedPane({ ptyId: 'remote:term-1', launchAgent: 'codex' })
|
||||
useAppStore.setState({ settings: { activeRuntimeEnvironmentId: 'env-1' } as never })
|
||||
runtimeEnvironmentCall.mockResolvedValue({
|
||||
id: 'rpc-1',
|
||||
ok: true,
|
||||
result: { process: { foregroundProcess: 'zsh', hasChildProcesses: false } },
|
||||
_meta: { runtimeId: 'remote-runtime' }
|
||||
})
|
||||
|
||||
await markLiveCodexSessionsForRestart({
|
||||
previousAccountLabel: ACCOUNT_A,
|
||||
nextAccountLabel: ACCOUNT_B
|
||||
})
|
||||
|
||||
// Why: a fresh local scan can know nothing about env-1's PTY, and a
|
||||
// cross-lane answer must never card (or spare) a remote pane.
|
||||
expect(window.api.pty.confirmForegroundProcess).not.toHaveBeenCalled()
|
||||
expect(useAppStore.getState().codexRestartNoticeByPtyId).toEqual({})
|
||||
})
|
||||
|
||||
it('heals a spurious shell reading before the restored-pane stale lookup', async () => {
|
||||
vi.mocked(window.api.pty.confirmForegroundProcess).mockResolvedValue('codex')
|
||||
vi.mocked(window.api.codexAccounts.listStalePanes).mockResolvedValue([
|
||||
{ ptyId: 'pty-1', launchAccountId: 'account-a', activeAccountId: 'account-b' }
|
||||
])
|
||||
|
||||
await markRestoredStaleCodexSessionsForRestart()
|
||||
|
||||
expect(useAppStore.getState().codexRestartNoticeByPtyId['pty-1']).toEqual({
|
||||
previousAccountLabel: ACCOUNT_A,
|
||||
nextAccountLabel: ACCOUNT_B
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -48,6 +48,9 @@ describe('markLiveCodexSessionsForRestart', () => {
|
||||
return createCompatibleRuntimeStatusResponseIfNeeded(args) ?? runtimeEnvironmentCall(args)
|
||||
})
|
||||
useAppStore.setState({
|
||||
// Why: a prior test's remote-runtime selection must not silently move
|
||||
// every later local pane out of the switch's lane.
|
||||
settings: null as never,
|
||||
tabsByWorktree: {
|
||||
wt1: [
|
||||
{
|
||||
@@ -78,7 +81,8 @@ describe('markLiveCodexSessionsForRestart', () => {
|
||||
...originalWindow?.api?.pty,
|
||||
getForegroundProcess: vi.fn(),
|
||||
hasChildProcesses: vi.fn().mockResolvedValue(false),
|
||||
inspectProcess: vi.fn()
|
||||
inspectProcess: vi.fn(),
|
||||
confirmForegroundProcess: vi.fn().mockResolvedValue(null)
|
||||
},
|
||||
codexAccounts: {
|
||||
...originalWindow?.api?.codexAccounts,
|
||||
@@ -760,7 +764,8 @@ describe('markRestoredStaleCodexSessionsForRestart', () => {
|
||||
hasChildProcesses: vi.fn().mockResolvedValue(false),
|
||||
inspectProcess: vi
|
||||
.fn()
|
||||
.mockResolvedValue({ foregroundProcess: 'codex', hasChildProcesses: false })
|
||||
.mockResolvedValue({ foregroundProcess: 'codex', hasChildProcesses: false }),
|
||||
confirmForegroundProcess: vi.fn().mockResolvedValue(null)
|
||||
},
|
||||
codexAccounts: {
|
||||
...originalWindow?.api?.codexAccounts,
|
||||
|
||||
@@ -1,8 +1,16 @@
|
||||
import type { AppState } from '@/store'
|
||||
import { useAppStore } from '@/store'
|
||||
import { inspectRuntimeTerminalProcess } from '@/runtime/runtime-terminal-inspection'
|
||||
import {
|
||||
confirmRuntimeTerminalForegroundProcess,
|
||||
inspectRuntimeTerminalProcess,
|
||||
type RuntimeTerminalProcessInspection
|
||||
} from '@/runtime/runtime-terminal-inspection'
|
||||
import { translate } from '@/i18n/i18n'
|
||||
import { isCodexRestartEligiblePane } from './codex-pane-restart-eligibility'
|
||||
import { isShellProcess } from '../../../shared/shell-process-detection'
|
||||
import {
|
||||
isCodexForegroundProcess,
|
||||
isCodexRestartEligiblePane
|
||||
} from './codex-pane-restart-eligibility'
|
||||
import {
|
||||
getCodexAccountSwitchLaneMatcher,
|
||||
isForeignMachineCodexPtyId,
|
||||
@@ -10,6 +18,7 @@ import {
|
||||
resolveCodexPaneSelectionLane
|
||||
} from './codex-pane-selection-lane'
|
||||
import type { CodexAccountSelectionTarget } from '../../../shared/codex-selection-lane'
|
||||
import type { TuiAgent } from '../../../shared/types'
|
||||
|
||||
// Why: prompt integrations such as Starship can outlast the daemon's 300ms
|
||||
// Codex fast-path timeout; account restarts must wait until the shell accepts input.
|
||||
@@ -59,6 +68,36 @@ async function readRecordedCodexPaneLanes(
|
||||
return await listRecordedPaneLanes({ ptyIds: localPtyIds }).catch(() => ({}))
|
||||
}
|
||||
|
||||
/**
|
||||
* Re-checks a shell reading with a fresh, uncached scan before trusting it.
|
||||
*
|
||||
* Why: the cached foreground read can flap to the pane's shell for a live Codex
|
||||
* session (#11064), and a spurious shell here silently skips the restart card —
|
||||
* no error, no retry. Mirrors main's terminalHasShellForegroundProcess, which
|
||||
* already refuses to conclude "agent done" from an unconfirmed shell reading.
|
||||
* Gated to Orca-launched Codex panes so an account switch never spends the
|
||||
* expensive fresh scan on ordinary shell terminals, and only an affirmative
|
||||
* codex answer flips the decision: a confirmed shell (user exited Codex) and a
|
||||
* null (scan failed or unsupported) both keep today's ineligible outcome.
|
||||
*/
|
||||
async function isConfirmedCodexForegroundDespiteShellReading(
|
||||
state: AppState,
|
||||
ptyId: string,
|
||||
launchAgent: TuiAgent | undefined,
|
||||
inspection: RuntimeTerminalProcessInspection
|
||||
): Promise<boolean> {
|
||||
if (
|
||||
launchAgent !== 'codex' ||
|
||||
inspection.unavailable === true ||
|
||||
inspection.foregroundProcess === null ||
|
||||
!isShellProcess(inspection.foregroundProcess)
|
||||
) {
|
||||
return false
|
||||
}
|
||||
const confirmed = await confirmRuntimeTerminalForegroundProcess(state.settings, ptyId)
|
||||
return isCodexForegroundProcess(confirmed)
|
||||
}
|
||||
|
||||
/**
|
||||
* Reports which panes are running Codex, skipping any outside the caller's lane.
|
||||
*
|
||||
@@ -114,11 +153,18 @@ async function scanCodexPanes(
|
||||
// Why: one stale remote pane must not hide restart notices for other confirmed Codex panes.
|
||||
() => null
|
||||
)
|
||||
const eligible =
|
||||
inspection !== null &&
|
||||
(isCodexRestartEligiblePane({ inspection, launchAgent: tab.launchAgent }) ||
|
||||
(await isConfirmedCodexForegroundDespiteShellReading(
|
||||
state,
|
||||
ptyId,
|
||||
tab.launchAgent,
|
||||
inspection
|
||||
)))
|
||||
return {
|
||||
ptyId,
|
||||
eligible:
|
||||
inspection !== null &&
|
||||
isCodexRestartEligiblePane({ inspection, launchAgent: tab.launchAgent }),
|
||||
eligible,
|
||||
inconclusive: inspection === null || inspection.unavailable === true,
|
||||
launchedCodex: tab.launchAgent === 'codex',
|
||||
notified: false,
|
||||
|
||||
@@ -96,6 +96,32 @@ export async function inspectRuntimeTerminalProcess(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Forces a fresh, uncached foreground scan for a pane whose cached inspection
|
||||
* is suspect (issue #11064: the cached read can flap to the shell for a live
|
||||
* agent). Local/daemon panes only — runtime environments expose no fresh-scan
|
||||
* RPC, and an SSH provider without confirm support answers null, which callers
|
||||
* must read as "no new evidence", never as a shell confirmation.
|
||||
*/
|
||||
export async function confirmRuntimeTerminalForegroundProcess(
|
||||
settings: Pick<GlobalSettings, 'activeRuntimeEnvironmentId'> | null | undefined,
|
||||
ptyId: string
|
||||
): Promise<string | null> {
|
||||
const ownerEnvironmentId = getRemoteRuntimePtyEnvironmentId(ptyId)
|
||||
const target = ownerEnvironmentId
|
||||
? ({ kind: 'environment', environmentId: ownerEnvironmentId } as const)
|
||||
: getActiveRuntimeTarget(settings)
|
||||
if (target.kind === 'environment' && getRemoteRuntimeTerminalHandle(ptyId)) {
|
||||
return null
|
||||
}
|
||||
const confirmForegroundProcess = window.api.pty.confirmForegroundProcess
|
||||
// Why the shape check: a preload older than this handler has no such method.
|
||||
if (typeof confirmForegroundProcess !== 'function') {
|
||||
return null
|
||||
}
|
||||
return confirmForegroundProcess(ptyId).catch(() => null)
|
||||
}
|
||||
|
||||
export function sendRuntimePtyInput(
|
||||
settings: Pick<GlobalSettings, 'activeRuntimeEnvironmentId'> | null | undefined,
|
||||
ptyId: string,
|
||||
|
||||
Reference in New Issue
Block a user