Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -536,6 +536,10 @@

it('kills now and defers the Codex respawn to mount when the layout leaf is unknown', async () => {
seedQueuedRestart({ leafId: null })
const command = String.raw`& 'C:\Program Files\Codex\codex.exe'`
useAppStore.setState({
settings: { ...useAppStore.getState().settings, agentCmdOverrides: { codex: command } }

Check failure on line 541 in src/renderer/src/components/terminal-pane/codex-detached-pane-restart.test.ts

View workflow job for this annotation

GitHub Actions / static analysis and typecheck

TS2322

Type '{ openLinksInAppInBackground?: boolean; claudeAccountSwitchRunningSessions?: ClaudeAccountSwitchRunningSessions; experimentalConnectors?: boolean; nativeChatAppearance?: NativeChatAppearanceSettings; ... 251 more ...; agentCmdOverrides: { ...; }; }' is not assignable to type 'GlobalSettings'. Type '{ openLinksInAppInBackground?: boolean; claudeAccountSwitchRunningSessions?: ClaudeAccountSwitchRunningSessions; experimentalConnectors?: boolean; nativeChatAppearance?: NativeChatAppearanceSettings; ... 251 more ...; agentCmdOverrides: { ...; }; }' is not assignable to type 'WorktreeDeletionPreferences'. Types of property 'skipDeleteWorktreeConfirm' are incompatible. Type 'boolean | undefined' is not assignable to type 'boolean'. Type 'undefined' is not assignable to type 'boolean'.

Check failure on line 541 in src/renderer/src/components/terminal-pane/codex-detached-pane-restart.test.ts

View workflow job for this annotation

GitHub Actions / static analysis and typecheck

Type '{ openLinksInAppInBackground?: boolean; claudeAccountSwitchRunningSessions?: ClaudeAccountSwitchRunningSessions; experimentalConnectors?: boolean; nativeChatAppearance?: NativeChatAppearanceSettings; ... 251 more ...; agentCmdOverrides: { ...; }; }' is not assignable to type 'GlobalSettings'.
Comment thread
brennanb2025 marked this conversation as resolved.
Resolved
})

await sweepUnclaimedCodexPaneRestarts()

Expand All @@ -545,7 +549,7 @@
expect(state.ptyIdsByTabId['tab-1']).toEqual([])
expect(state.pendingStartupByTabId['tab-1']).toEqual(
expect.objectContaining({
command: 'codex',
command,
startupCommandDelivery: 'shell-ready',
launchAgent: 'codex'
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import type { AppState } from '@/store'
import { useAppStore } from '@/store'
import { singlePaneLayoutSnapshot } from '@/store/slices/terminal-helpers'
import { hasRegisteredRuntimeTerminalTab } from '@/runtime/mounted-terminal-tab-registry'
import { buildPaneAccountRestartStartup } from '@/lib/agent-account-restart-startup'
import { CODEX_ACCOUNT_RESTART_STARTUP } from '@/lib/codex-session-restart'
import {
buildCodexAccountRestartStartup,
buildPaneAccountRestartStartup
} from '@/lib/agent-account-restart-startup'
import { isClaudePaneMidTurn } from '@/lib/claude-session-account-move'
import type { AccountRestartAgent } from '@/store/terminals/terminal-contracts'
import { isForeignMachineCodexPtyId } from '@/lib/codex-pane-selection-lane'
Expand Down Expand Up @@ -158,11 +160,15 @@ async function executeDetachedCodexPaneRestart(
reopenCurrentCodexRestartPrompt(located, ptyId)
return
}
const startup = buildCodexAccountRestartStartup({
worktreeId: located.worktreeId,
shellOverride: located.tab.shellOverride
})
const store = useAppStore.getState()
store.suppressPtyExit(ptyId)
store.clearTabPtyId(located.tab.id, ptyId)
store.consumeSuppressedPtyExit(ptyId)
store.queueTabStartupCommand(located.tab.id, { ...CODEX_ACCOUNT_RESTART_STARTUP })
store.queueTabStartupCommand(located.tab.id, startup)
store.clearCodexRestartNotice(ptyId)
killReplacedCodexPanePty(ptyId)
return
Expand Down
30 changes: 24 additions & 6 deletions src/renderer/src/lib/agent-account-restart-startup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ describe('buildPaneAccountRestartStartup for Codex', () => {
beforeEach(() => {
seedAgentStatus(null)
projectRuntimeContext = undefined
useAppStore.setState({ settings: getDefaultSettings('/home/dev') })
})

it('names the session so the relaunch continues the conversation', () => {
Expand Down Expand Up @@ -95,11 +96,22 @@ describe('buildPaneAccountRestartStartup for Codex', () => {
expect(startup.startupCommandDelivery).toBe('shell-ready')
})

it('falls back to a bare relaunch when the pane has no Codex session to name', () => {
const startup = build()
it('relaunches the configured command without adding defaults when no session is recorded', () => {
const command = String.raw`& 'C:\Program Files\Codex\codex.exe'`
useAppStore.setState({
settings: {
...getDefaultSettings('/home/dev'),
agentCmdOverrides: { codex: command },
agentDefaultArgs: { codex: '--search' },
agentDefaultEnv: { codex: { CODEX_DEFAULT_ONLY: '1' } }
}
})

expect(startup.command).toBe('codex')
expect(startup.resumeProviderSession).toBeUndefined()
expect(build()).toEqual({
command,
startupCommandDelivery: 'shell-ready',
launchAgent: 'codex'
})
})

it('uses the persisted record when the live status entry is gone', () => {
Expand All @@ -125,7 +137,13 @@ describe('buildPaneAccountRestartStartup for Codex', () => {
expect(startup.resumeProviderSession?.id).toBe(SESSION_ID)
})

it('keeps the bare relaunch for a resolved WSL runtime', () => {
it('keeps the configured fresh relaunch for a resolved WSL runtime', () => {
useAppStore.setState({
settings: {
...getDefaultSettings('/home/dev'),
agentCmdOverrides: { codex: 'codex-wrapper' }
}
})
seedAgentStatus({
agentType: 'codex',
state: 'done',
Expand All @@ -145,7 +163,7 @@ describe('buildPaneAccountRestartStartup for Codex', () => {

const startup = build()

expect(startup.command).toBe('codex')
expect(startup.command).toBe('codex-wrapper')
expect(startup.resumeProviderSession).toBeUndefined()
})

Expand Down
83 changes: 59 additions & 24 deletions src/renderer/src/lib/agent-account-restart-startup.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { useAppStore } from '@/store'
import { useAppStore, type AppState } from '@/store'
import { buildAgentResumeStartupPlan } from '@/lib/tui-agent-startup'
import {
resolveSshHostReportedPlatform,
resolveAgentResumeLaunchTarget
resolveAgentResumeLaunchTarget,
type AgentResumeLaunchTarget
} from '@/lib/agent-resume-launch-target'
import { getExecutionHostIdForWorktree } from '@/lib/worktree-runtime-owner'
import { runtimeEnvironmentIdUnlessTwin } from '@/lib/resolve-owner'
Expand All @@ -18,7 +19,9 @@ import type {
SleepingAgentLaunchConfig
} from '../../../shared/agent-session-resume'
import type { AccountRestartAgent } from '@/store/terminals/terminal-contracts'
import { CODEX_ACCOUNT_RESTART_STARTUP } from './codex-session-restart'
import type { ProjectExecutionRuntimeResolution } from '../../../shared/project-execution-runtime'
import { resolveAgentLaunchCommand } from '../../../shared/tui-agent-launch-command'
import { resolveStartupShell } from '../../../shared/tui-agent-startup-shell'

export type AgentAccountRestartStartup = {
command: string
Expand All @@ -36,6 +39,8 @@ type AccountRestartPane = {
shellOverride?: string
}

type AccountRestartTarget = Pick<AccountRestartPane, 'worktreeId' | 'shellOverride'>

/**
* The resume startup for the pane's own session, or null when it has none to name.
* Capture it before restart clears the old pane's status.
Expand Down Expand Up @@ -69,25 +74,10 @@ export function resolveAgentAccountRestartResumeStartup(
) {
return null
}
const worktree = state.getKnownWorktreeById(args.worktreeId)
const repo = worktree ? state.repos.find((entry) => entry.id === worktree.repoId) : null
const launchConfig =
(entry ? state.getAgentLaunchConfigForStatusEntry(entry) : undefined) ??
(sleeping?.agent === agent ? sleeping.launchConfig : undefined)
const executionHostId = getExecutionHostIdForWorktree(state, args.worktreeId)
const resumeTarget = resolveAgentResumeLaunchTarget({
projectRuntime,
connectionId: repo?.connectionId,
executionHostId,
worktreePath: worktree?.path,
terminalWindowsShell: state.settings?.terminalWindowsShell,
tabShellOverride: args.shellOverride,
remotePlatform: resolveSshHostReportedPlatform(
state,
executionHostId,
runtimeEnvironmentIdUnlessTwin(state, args.worktreeId)
)
})
const resumeTarget = resolveAccountRestartLaunchTarget(state, args, projectRuntime)
const startupPlan = buildAgentResumeStartupPlan({
agent,
providerSession,
Expand Down Expand Up @@ -125,14 +115,59 @@ export function resolveAgentAccountRestartResumeStartup(
}
}

/**
* The startup an account-switch restart relaunches the pane with. Codex falls
* back to a bare relaunch; Claude only ever resumes, so null means skip it.
*/
/** Account-switch startup: Codex can relaunch fresh; Claude requires its recorded conversation. */
export function buildPaneAccountRestartStartup(
agent: AccountRestartAgent,
args: AccountRestartPane
): AgentAccountRestartStartup | null {
const resume = resolveAgentAccountRestartResumeStartup(agent, args)
return resume ?? (agent === 'codex' ? CODEX_ACCOUNT_RESTART_STARTUP : null)
return resume ?? (agent === 'codex' ? buildCodexAccountRestartStartup(args) : null)
}

export function buildCodexAccountRestartStartup(
args: AccountRestartTarget
): AgentAccountRestartStartup {
const state = useAppStore.getState()
const target = resolveAccountRestartLaunchTarget(
state,
args,
getLocalProjectExecutionRuntimeContext(state, args.worktreeId)
)
const command = resolveAgentLaunchCommand({
agent: 'codex',
cmdOverrides: state.settings?.agentCmdOverrides ?? {},
platform: target.platform,
shell: resolveStartupShell(target.platform, target.shell)
})
if (!command.ok) {
throw new Error(command.error)
}
return {
command: command.command,
startupCommandDelivery: 'shell-ready',
launchAgent: 'codex'
}
}

function resolveAccountRestartLaunchTarget(
state: AppState,
args: AccountRestartTarget,
projectRuntime: ProjectExecutionRuntimeResolution | undefined
): AgentResumeLaunchTarget {
const worktree = state.getKnownWorktreeById(args.worktreeId)
const repo = worktree ? state.repos.find((entry) => entry.id === worktree.repoId) : null
const executionHostId = getExecutionHostIdForWorktree(state, args.worktreeId)
return resolveAgentResumeLaunchTarget({
projectRuntime,
connectionId: repo?.connectionId,
executionHostId,
worktreePath: worktree?.path,
terminalWindowsShell: state.settings?.terminalWindowsShell,
tabShellOverride: args.shellOverride,
remotePlatform: resolveSshHostReportedPlatform(
state,
executionHostId,
runtimeEnvironmentIdUnlessTwin(state, args.worktreeId)
)
})
}
14 changes: 0 additions & 14 deletions src/renderer/src/lib/codex-session-restart.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ const markForFocusedHost = (
owner: defaultScopeHost(useAppStore.getState().settings),
...args
})
import { shouldUseShellReadyStartupDelivery } from '../../../shared/codex-startup-delivery'
import type { TuiAgent } from '../../../shared/tui-agent'
import {
CODEX_ACCOUNT_RESTART_STARTUP,
markLiveCodexSessionsForRestart,
markRestoredStaleCodexSessionsForRestart
} from './codex-session-restart'
Expand All @@ -36,18 +34,6 @@ function setLaunchAgentOnFirstTab(launchAgent: TuiAgent): void {
useAppStore.setState({ tabsByWorktree: { wt1: [{ ...tab, launchAgent }, ...rest] } })
}

describe('CODEX_ACCOUNT_RESTART_STARTUP', () => {
it('waits for shell readiness before relaunching Codex after an account switch', () => {
// launchAgent:codex enables managed-auth readiness/launch prep; omitting it can race handoff and record the wrong account.
expect(CODEX_ACCOUNT_RESTART_STARTUP).toEqual({
command: 'codex',
startupCommandDelivery: 'shell-ready',
launchAgent: 'codex'
})
expect(shouldUseShellReadyStartupDelivery(CODEX_ACCOUNT_RESTART_STARTUP)).toBe(true)
})
})

describe('markLiveCodexSessionsForRestart', () => {
const originalWindow = (globalThis as { window?: typeof window }).window
const runtimeEnvironmentCall = vi.fn()
Expand Down
7 changes: 0 additions & 7 deletions src/renderer/src/lib/codex-session-restart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@ import type { CodexAccountSelectionTarget } from '../../../shared/codex-selectio
import type { RuntimeClientTarget } from '../../../shared/runtime-client-target'
import type { TuiAgent } from '../../../shared/tui-agent'

// Wait for shell readiness beyond the daemon fast-path timeout; launchAgent:codex also gates managed-auth handoff and correct account recording.
export const CODEX_ACCOUNT_RESTART_STARTUP = {
command: 'codex',
startupCommandDelivery: 'shell-ready',
launchAgent: 'codex'
} as const

export type CodexPaneScanResult = {
ptyId: string
/** The pane may be shown a restart prompt (see isCodexRestartEligiblePane). */
Expand Down
Loading