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
165 changes: 120 additions & 45 deletions src/renderer/src/lib/worktree-creation-flow-execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ import type { WorktreeCreationRequest } from '@/lib/pending-worktree-creation'
import { createBrowserUuid } from '@/lib/browser-uuid'
import { resolveBackendDraftStartup } from '@/lib/worktree-draft-startup-view-mode'
import { buildWorktreeCreationStartupOpt } from '@/lib/worktree-creation-flow-startup'
import { launchStructuredWorktreeSession } from '@/lib/worktree-creation-structured-session'
import {
launchStructuredWorktreeSession,
type WorktreeCreationStructuredSessionResult
} from '@/lib/worktree-creation-structured-session'
import { completeWorktreeCreation } from '@/lib/worktree-creation-completion'
import { markStructuredWorktreeLaunchUnconfirmed } from '@/lib/worktree-creation-structured-recovery'
import { ensureWebRuntimeWorktreeTerminalAfterWake } from '@/lib/web-runtime-worktree-terminal-after-wake'
Expand Down Expand Up @@ -164,46 +167,109 @@ export async function executeWorktreeCreation(
(completionState.activeView === 'terminal' &&
completionState.activePendingCreationId === null))

// Why: the worktree exists past this point and nothing awaits this caller, so
// each follow-up step is best-effort — an escaped throw would strand the
// creation surface over the finished workspace instead of reaching completion.
let activation: ActivateAndRevealResult | false = false
let primaryTabId: string | null
let primaryTabId: string | null = null
if (shouldActivateOnCompletion && !structuredLaunch) {
activation = activateAndRevealWorktree(worktree.id, {
sidebarRevealBehavior: 'auto',
...(preparedRequest.agent !== null ? { agent: preparedRequest.agent } : {}),
...(result.setup ? { setup: result.setup } : {}),
...(result.defaultTabs ? { defaultTabs: result.defaultTabs } : {}),
...(startupOpt ? { startup: startupOpt } : {}),
...(preparedRequest.issueCommand ? { issueCommand: preparedRequest.issueCommand } : {}),
...(backendSpawned ? { backendStartupTerminalSpawned: true } : {})
})
primaryTabId = activation === false ? null : activation.primaryTabId
} else {
// Keep chat creation on its pending surface until the session is ready.
const hasExplicitTerminalWork = Boolean(
startupOpt || result.setup || preparedRequest.issueCommand || result.defaultTabs
)
primaryTabId =
preparedRequest.agent !== null && !hasExplicitTerminalWork
? null
: ensureWorktreeHasInitialTerminal(
try {
activation = activateAndRevealWorktree(worktree.id, {
sidebarRevealBehavior: 'auto',
...(preparedRequest.agent !== null ? { agent: preparedRequest.agent } : {}),
...(result.setup ? { setup: result.setup } : {}),
...(result.defaultTabs ? { defaultTabs: result.defaultTabs } : {}),
...(startupOpt ? { startup: startupOpt } : {}),
...(preparedRequest.issueCommand ? { issueCommand: preparedRequest.issueCommand } : {}),
...(backendSpawned ? { backendStartupTerminalSpawned: true } : {})
})
primaryTabId = activation === false ? null : activation.primaryTabId
} catch (error) {
console.error('worktree create: activate-and-reveal failed', worktree.id, error)
// Activation can publish the worktree before a later step throws. Do not
// infer a primary tab from default-tab ordering; only a fresh seed may
// return one here.
Comment on lines +189 to +191

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This lead-in still says "only a fresh seed may return one here," which this commit just made false — a startup-terminal id or a launch-agent-stamped tab can now set primaryTabId too. The comment two lines below already states the correct rule, so reword this one to keep the two from contradicting.

Suggested change
// Activation can publish the worktree before a later step throws. Do not
// infer a primary tab from default-tab ordering; only a fresh seed may
// return one here.
// Activation can publish the worktree before a later step throws. Recover
// only ids provably tied to this launch — the startup terminal or a tab
// stamped with its agent — never default-tab ordering.

const stateAfterActivationFailure = useAppStore.getState()
const existingTabs = stateAfterActivationFailure.tabsByWorktree[worktree.id] ?? []
const launchAgent = startupOpt?.launchAgent ?? preparedRequest.agent
const verifiedLaunchTabId =
result.startupTerminal?.tabId ??
(launchAgent ? existingTabs.find((tab) => tab.launchAgent === launchAgent)?.id : undefined)
if (verifiedLaunchTabId) {
// Startup terminal ids and stamped agent tabs are the only safe primary
// ids when activation returned no result.
primaryTabId = verifiedLaunchTabId
} else if (existingTabs.length === 0) {
try {
primaryTabId = ensureWorktreeHasInitialTerminal(
useAppStore.getState(),
worktree.id,
startupOpt,
result.setup,
preparedRequest.issueCommand,
result.defaultTabs,
{
activateCreatedTabs: false,
...(preparedRequest.agent !== null ? { callerProvidesSurface: true } : {}),
...(backendSpawned ? { backendStartupTerminalSpawned: true } : {})
}
)
} catch (recoveryError) {
console.error(
'worktree create: activation recovery seeding failed',
worktree.id,
recoveryError
)
}
}
if (!backendSpawned) {
try {
ensureWebRuntimeWorktreeTerminalAfterWake(worktree.id, {
startup: startupOpt,
agent: preparedRequest.agent
})
} catch (recoveryError) {
console.error(
'worktree create: activation recovery after-wake seeding failed',
worktree.id,
recoveryError
)
}
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
} else {
// Keep chat creation on its pending surface until the session is ready.
const hasExplicitTerminalWork = Boolean(
startupOpt || result.setup || preparedRequest.issueCommand || result.defaultTabs
)
if (preparedRequest.agent === null || hasExplicitTerminalWork) {
try {
primaryTabId = ensureWorktreeHasInitialTerminal(
useAppStore.getState(),
worktree.id,
startupOpt,
result.setup,
preparedRequest.issueCommand,
result.defaultTabs,
{
activateCreatedTabs: false,
...(preparedRequest.agent !== null ? { callerProvidesSurface: true } : {}),
...(backendSpawned ? { backendStartupTerminalSpawned: true } : {})
}
)
} catch (error) {
console.error('worktree create: initial terminal seeding failed', worktree.id, error)
}
}
if (!structuredLaunch && !backendSpawned) {
ensureWebRuntimeWorktreeTerminalAfterWake(worktree.id, {
startup: startupOpt,
agent: preparedRequest.agent,
activate: false
})
try {
ensureWebRuntimeWorktreeTerminalAfterWake(worktree.id, {
startup: startupOpt,
agent: preparedRequest.agent,
activate: false
})
} catch (error) {
console.error('worktree create: after-wake terminal seeding failed', worktree.id, error)
}
}
}

Expand All @@ -213,25 +279,34 @@ export async function executeWorktreeCreation(
agentLaunchRoute === 'structured-native-chat' &&
isAgentSessionHandleProvider(preparedRequest.agent)
) {
const structuredSession = await launchStructuredWorktreeSession({
creationId,
request: preparedRequest,
agentLaunchRoute,
worktreeId: worktree.id,
shouldActivateOnCompletion,
fallbackStartupOpt,
activation,
primaryTabId
})
structuredLaunchAccepted = structuredSession.accepted
activation = structuredSession.activation
primaryTabId = structuredSession.primaryTabId
if (structuredSession.cancelled) {
return
let structuredSession: WorktreeCreationStructuredSessionResult | null = null
try {
structuredSession = await launchStructuredWorktreeSession({
creationId,
request: preparedRequest,
agentLaunchRoute,
worktreeId: worktree.id,
shouldActivateOnCompletion,
fallbackStartupOpt,
activation,
primaryTabId
})
} catch (error) {
// Why: plan.launch is guarded inside, but its sync prologue is not; treat
// an escaped throw like a failed launch (accepted) and still complete.
console.error('worktree create: structured session launch failed', worktree.id, error)
}
if (structuredSession.visibilityUnknown) {
markStructuredWorktreeLaunchUnconfirmed(creationId, worktree.id)
return
if (structuredSession) {
structuredLaunchAccepted = structuredSession.accepted
activation = structuredSession.activation
primaryTabId = structuredSession.primaryTabId
if (structuredSession.cancelled) {
return
}
if (structuredSession.visibilityUnknown) {
markStructuredWorktreeLaunchUnconfirmed(creationId, worktree.id)
return
}
}
}

Expand Down
Loading