diff --git a/src/renderer/src/lib/worktree-activation.ts b/src/renderer/src/lib/worktree-activation.ts index 381e6cf1e27..46202c85e3d 100644 --- a/src/renderer/src/lib/worktree-activation.ts +++ b/src/renderer/src/lib/worktree-activation.ts @@ -30,7 +30,10 @@ import type { ExecutionHostId } from '../../../shared/execution-host' import { findFolderWorkspaceOwner } from './folder-workspace-runtime-owner' import type { WorktreeStartupPayload } from '@/lib/worktree-startup-payload' import type { IssueCommandLaunch } from '@/lib/worktree-setup-issue-command-queue' -import { ensureWorktreeHasInitialTerminal } from '@/lib/worktree-initial-terminal-seeding' +import { + ensureWorktreeHasInitialTerminal, + reseedGatedEmptyWorkspace +} from '@/lib/worktree-initial-terminal-seeding' import { ensureWebRuntimeWorktreeTerminalAfterWake } from '@/lib/web-runtime-worktree-terminal-after-wake' import { applyWorktreeNavViewEntry } from '@/lib/worktree-nav-view-history-replay' @@ -146,12 +149,8 @@ export function activateAndRevealFolderWorkspace( } if (shouldGateAgentActivation) { void gateWorktreeAgentActivation(workspaceKey).then((outcome) => { - if (outcome === 'empty' && useAppStore.getState().activeWorktreeId === workspaceKey) { - ensureFolderWorkspaceInitialTerminal( - folderWorkspace, - undefined, - opts?.providesInitialSurface - ) + if (outcome === 'empty') { + reseedGatedEmptyWorkspace(workspaceKey, opts?.providesInitialSurface) } }) } @@ -264,17 +263,8 @@ export function activateAndRevealWorktree( } if (shouldGateAgentActivation) { void gateWorktreeAgentActivation(worktreeId).then((outcome) => { - const currentState = useAppStore.getState() - if (outcome === 'empty' && currentState.activeWorktreeId === worktreeId) { - ensureWorktreeHasInitialTerminal( - currentState, - worktreeId, - undefined, - undefined, - undefined, - undefined, - { reseedEmptiedWorkspace: opts?.providesInitialSurface !== true } - ) + if (outcome === 'empty') { + reseedGatedEmptyWorkspace(worktreeId, opts?.providesInitialSurface) } }) } diff --git a/src/renderer/src/lib/worktree-initial-terminal-seeding.ts b/src/renderer/src/lib/worktree-initial-terminal-seeding.ts index f2057537565..6b4214a3fd3 100644 --- a/src/renderer/src/lib/worktree-initial-terminal-seeding.ts +++ b/src/renderer/src/lib/worktree-initial-terminal-seeding.ts @@ -35,6 +35,29 @@ function getSetupRunnerCommandPlatformForLaunch(setup: WorktreeSetupLaunch): 'wi ) } +/** After the async activation gate reports an empty workspace: re-seed a shell unless the caller + * promised its own surface or the user has already moved on. */ +export function reseedGatedEmptyWorkspace( + workspaceKey: string, + callerProvidesSurface: boolean | undefined +): void { + const state = useAppStore.getState() + if (callerProvidesSurface === true || state.activeWorktreeId !== workspaceKey) { + return + } + ensureWorktreeHasInitialTerminal( + state, + workspaceKey, + undefined, + undefined, + undefined, + undefined, + { + reseedEmptiedWorkspace: true + } + ) +} + export function ensureWorktreeHasInitialTerminal( store: WorktreeActivationStore, worktreeId: string,