-
Notifications
You must be signed in to change notification settings - Fork 0
fix(worktree): gate sessions on bootstrap readiness #2
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ import { | |
| rejectPendingDraftWorktreeRequest, | ||
| resolvePendingDraftWorktreeRequest, | ||
| } from '@/lib/worktrees/pendingDraftWorktree'; | ||
| import { waitForWorktreeBootstrap } from '@/lib/worktrees/worktreeBootstrap'; | ||
|
|
||
| const normalizePath = (value: string): string => value.replace(/\\/g, '/').replace(/\/+$/, '') || value; | ||
|
|
||
|
|
@@ -417,6 +418,8 @@ export async function createWorktreeSessionForBranch( | |
| kind, | ||
| }; | ||
|
|
||
| await waitForWorktreeBootstrap(metadata.path); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If try {
await waitForWorktreeBootstrap(metadata.path);
} catch (error) {
await removeProjectWorktree(projectRef, metadata, { deleteLocalBranch: true }).catch(() => undefined);
throw error;
} |
||
|
|
||
| // Create the session | ||
| const sessionStore = useSessionUIStore.getState(); | ||
| const session = await sessionStore.createSession(undefined, metadata.path); | ||
|
|
@@ -520,6 +523,8 @@ export async function createWorktreeSessionForNewBranch( | |
| kind, | ||
| }; | ||
|
|
||
| await waitForWorktreeBootstrap(metadata.path); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If try {
await waitForWorktreeBootstrap(metadata.path);
} catch (error) {
await removeProjectWorktree(projectRef, metadata, { deleteLocalBranch: true }).catch(() => undefined);
throw error;
} |
||
|
|
||
| const sessionStore = useSessionUIStore.getState(); | ||
| const session = await sessionStore.createSession(undefined, metadata.path); | ||
| if (!session) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -7,6 +7,7 @@ import { opencodeClient } from '@/lib/opencode/client'; | |||||||||||||||||
| import { saveWorktreeSetupCommands } from '@/lib/openchamberConfig'; | ||||||||||||||||||
| import type { ProjectRef } from '@/lib/worktrees/worktreeManager'; | ||||||||||||||||||
| import { createWorktreeWithDefaults, resolveRootTrackingRemote } from '@/lib/worktrees/worktreeCreate'; | ||||||||||||||||||
| import { waitForWorktreeBootstrap } from '@/lib/worktrees/worktreeBootstrap'; | ||||||||||||||||||
| import { getRootBranch } from '@/lib/worktrees/worktreeStatus'; | ||||||||||||||||||
| import { checkIsGitRepository } from '@/lib/gitApi'; | ||||||||||||||||||
| import { useDirectoryStore } from './useDirectoryStore'; | ||||||||||||||||||
|
|
@@ -244,6 +245,8 @@ export const useMultiRunStore = create<MultiRunStore>()( | |||||||||||||||||
| kind: 'standard' as const, | ||||||||||||||||||
| }; | ||||||||||||||||||
|
|
||||||||||||||||||
| await waitForWorktreeBootstrap(worktreeMetadata.path); | ||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If
Suggested change
|
||||||||||||||||||
|
|
||||||||||||||||||
| const session = await opencodeClient.withDirectory( | ||||||||||||||||||
| worktreeMetadata.path, | ||||||||||||||||||
| () => opencodeClient.createSession({ title: sessionTitle }), | ||||||||||||||||||
|
|
||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -29,6 +29,7 @@ import { markPendingUserSendAnimation } from "@/lib/userSendAnimation" | |||||||||||||||||||||
| import { flattenAssistantTextParts } from "@/lib/messages/messageText" | ||||||||||||||||||||||
| import { composeForkSessionMessage } from "@/lib/messages/executionMeta" | ||||||||||||||||||||||
| import { waitForPendingDraftWorktreeRequest } from "@/lib/worktrees/pendingDraftWorktree" | ||||||||||||||||||||||
| import { waitForWorktreeBootstrap } from "@/lib/worktrees/worktreeBootstrap" | ||||||||||||||||||||||
| import { resolveProjectForSessionDirectory } from "@/lib/projectResolution" | ||||||||||||||||||||||
| import { | ||||||||||||||||||||||
| getSyncSessions, | ||||||||||||||||||||||
|
|
@@ -401,6 +402,84 @@ const writeRuntimeSessionMemory = (key: string, patch: Partial<RuntimeSessionMem | |||||||||||||||||||||
| }) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| type MaterializedDraftSession = { | ||||||||||||||||||||||
| sessionId: string | ||||||||||||||||||||||
| directory: string | null | ||||||||||||||||||||||
| agent?: string | ||||||||||||||||||||||
| syntheticParts?: SyntheticContextPart[] | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| export async function materializeOpenDraftSession(selection: { | ||||||||||||||||||||||
| providerID: string | ||||||||||||||||||||||
| modelID: string | ||||||||||||||||||||||
| agent?: string | ||||||||||||||||||||||
| variant?: string | ||||||||||||||||||||||
| }): Promise<MaterializedDraftSession | null> { | ||||||||||||||||||||||
| const store = useSessionUIStore.getState() | ||||||||||||||||||||||
| const draft = store.newSessionDraft | ||||||||||||||||||||||
| if (!draft?.open) return null | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const trimmedAgent = typeof selection.agent === "string" && selection.agent.trim().length > 0 | ||||||||||||||||||||||
| ? selection.agent.trim() | ||||||||||||||||||||||
| : undefined | ||||||||||||||||||||||
| const draftTargetFolderId = draft.targetFolderId | ||||||||||||||||||||||
| let draftDirectoryOverride = draft.bootstrapPendingDirectory ?? draft.directoryOverride ?? null | ||||||||||||||||||||||
| const draftProjectId = draft.selectedProjectId ?? null | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if (draft.pendingWorktreeRequestId) { | ||||||||||||||||||||||
| draftDirectoryOverride = await waitForPendingDraftWorktreeRequest(draft.pendingWorktreeRequestId) | ||||||||||||||||||||||
| store.resolvePendingDraftWorktreeTarget(draft.pendingWorktreeRequestId, draftDirectoryOverride) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if (draftDirectoryOverride) { | ||||||||||||||||||||||
| await waitForWorktreeBootstrap(draftDirectoryOverride) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const created = await store.createSession(draft.title, draftDirectoryOverride, draft.parentID ?? null) | ||||||||||||||||||||||
| if (!created?.id) throw new Error("Failed to create session") | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| persistDraftTarget({ | ||||||||||||||||||||||
| projectId: draftProjectId, | ||||||||||||||||||||||
| directory: normalizePath(draftDirectoryOverride ?? created.directory ?? null), | ||||||||||||||||||||||
| }) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const draftSyntheticParts = draft.syntheticParts | ||||||||||||||||||||||
| const createdDirectory = normalizePath(draftDirectoryOverride ?? created.directory ?? null) | ||||||||||||||||||||||
| const configState = useConfigStore.getState() | ||||||||||||||||||||||
| void activateConfigForDirectory(createdDirectory).catch((error) => { | ||||||||||||||||||||||
| console.warn("Failed to activate directory after creating session:", error) | ||||||||||||||||||||||
| }) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const effectiveDraftAgent = trimmedAgent ?? configState.currentAgentName | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| useSelectionStore.getState().saveSessionModelSelection(created.id, selection.providerID, selection.modelID) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if (effectiveDraftAgent) { | ||||||||||||||||||||||
| useSelectionStore.getState().saveSessionAgentSelection(created.id, effectiveDraftAgent) | ||||||||||||||||||||||
| useSelectionStore.getState().saveAgentModelForSession(created.id, effectiveDraftAgent, selection.providerID, selection.modelID) | ||||||||||||||||||||||
| useSelectionStore.getState().saveAgentModelVariantForSession(created.id, effectiveDraftAgent, selection.providerID, selection.modelID, selection.variant) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| store.initializeNewOpenChamberSession(created.id, configState.agents ?? []) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| store.closeNewSessionDraft() | ||||||||||||||||||||||
| store.setCurrentSession(created.id, createdDirectory) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if (draftTargetFolderId) { | ||||||||||||||||||||||
| const scopeKey = draftDirectoryOverride || created.directory || null | ||||||||||||||||||||||
| if (scopeKey) { | ||||||||||||||||||||||
| useSessionFoldersStore.getState().addSessionToFolder(scopeKey, draftTargetFolderId, created.id) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
Comment on lines
+465
to
+473
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The call to
Suggested change
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return { | ||||||||||||||||||||||
| sessionId: created.id, | ||||||||||||||||||||||
| directory: createdDirectory, | ||||||||||||||||||||||
| agent: effectiveDraftAgent, | ||||||||||||||||||||||
| syntheticParts: draftSyntheticParts, | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // --------------------------------------------------------------------------- | ||||||||||||||||||||||
| // Store | ||||||||||||||||||||||
| // --------------------------------------------------------------------------- | ||||||||||||||||||||||
|
|
@@ -913,59 +992,21 @@ export const useSessionUIStore = create<SessionUIState>()((set, get) => ({ | |||||||||||||||||||||
|
|
||||||||||||||||||||||
| // ---- New session from draft ---- | ||||||||||||||||||||||
| if (!options?.sessionId && draft?.open) { | ||||||||||||||||||||||
| const draftTargetFolderId = draft.targetFolderId | ||||||||||||||||||||||
| let draftDirectoryOverride = draft.bootstrapPendingDirectory ?? draft.directoryOverride ?? null | ||||||||||||||||||||||
| const draftProjectId = draft.selectedProjectId ?? null | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if (draft.pendingWorktreeRequestId) { | ||||||||||||||||||||||
| draftDirectoryOverride = await waitForPendingDraftWorktreeRequest(draft.pendingWorktreeRequestId) | ||||||||||||||||||||||
| get().resolvePendingDraftWorktreeTarget(draft.pendingWorktreeRequestId, draftDirectoryOverride) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const created = await get().createSession(draft.title, draftDirectoryOverride, draft.parentID ?? null) | ||||||||||||||||||||||
| if (!created?.id) throw new Error("Failed to create session") | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| persistDraftTarget({ | ||||||||||||||||||||||
| projectId: draftProjectId, | ||||||||||||||||||||||
| directory: normalizePath(draftDirectoryOverride ?? created.directory ?? null), | ||||||||||||||||||||||
| }) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const draftSyntheticParts = draft.syntheticParts | ||||||||||||||||||||||
| const createdDirectory = normalizePath(draftDirectoryOverride ?? created.directory ?? null) | ||||||||||||||||||||||
| const configState = useConfigStore.getState() | ||||||||||||||||||||||
| void activateConfigForDirectory(createdDirectory).catch((error) => { | ||||||||||||||||||||||
| console.warn("Failed to activate directory after creating session:", error) | ||||||||||||||||||||||
| const createdDraftSession = await materializeOpenDraftSession({ | ||||||||||||||||||||||
| providerID, | ||||||||||||||||||||||
| modelID, | ||||||||||||||||||||||
| agent: trimmedAgent, | ||||||||||||||||||||||
| variant, | ||||||||||||||||||||||
| }) | ||||||||||||||||||||||
| if (!createdDraftSession) throw new Error("Failed to create session") | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const effectiveDraftAgent = trimmedAgent ?? configState.currentAgentName | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| useSelectionStore.getState().saveSessionModelSelection(created.id, providerID, modelID) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if (effectiveDraftAgent) { | ||||||||||||||||||||||
| useSelectionStore.getState().saveSessionAgentSelection(created.id, effectiveDraftAgent) | ||||||||||||||||||||||
| useSelectionStore.getState().saveAgentModelForSession(created.id, effectiveDraftAgent, providerID, modelID) | ||||||||||||||||||||||
| useSelectionStore.getState().saveAgentModelVariantForSession(created.id, effectiveDraftAgent, providerID, modelID, variant) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| get().initializeNewOpenChamberSession(created.id, configState.agents ?? []) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| get().closeNewSessionDraft() | ||||||||||||||||||||||
| get().setCurrentSession(created.id, createdDirectory) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if (draftTargetFolderId) { | ||||||||||||||||||||||
| const scopeKey = draftDirectoryOverride || created.directory || null | ||||||||||||||||||||||
| if (scopeKey) { | ||||||||||||||||||||||
| useSessionFoldersStore.getState().addSessionToFolder(scopeKey, draftTargetFolderId, created.id) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const mergedAdditionalParts = draftSyntheticParts?.length | ||||||||||||||||||||||
| ? [...(additionalParts || []), ...draftSyntheticParts] | ||||||||||||||||||||||
| const mergedAdditionalParts = createdDraftSession.syntheticParts?.length | ||||||||||||||||||||||
| ? [...(additionalParts || []), ...createdDraftSession.syntheticParts] | ||||||||||||||||||||||
| : additionalParts | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| notifyMessageSent(created.id) | ||||||||||||||||||||||
| notifyMessageSent(createdDraftSession.sessionId) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| markPendingUserSendAnimation(created.id) | ||||||||||||||||||||||
| markPendingUserSendAnimation(createdDraftSession.sessionId) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const files = attachments?.map((a) => ({ | ||||||||||||||||||||||
| type: "file" as const, | ||||||||||||||||||||||
|
|
@@ -975,12 +1016,12 @@ export const useSessionUIStore = create<SessionUIState>()((set, get) => ({ | |||||||||||||||||||||
| })) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| await routeMessage({ | ||||||||||||||||||||||
| sessionId: created.id, | ||||||||||||||||||||||
| directory: createdDirectory, | ||||||||||||||||||||||
| sessionId: createdDraftSession.sessionId, | ||||||||||||||||||||||
| directory: createdDraftSession.directory, | ||||||||||||||||||||||
| content, | ||||||||||||||||||||||
| providerID, | ||||||||||||||||||||||
| modelID, | ||||||||||||||||||||||
| agent: effectiveDraftAgent, | ||||||||||||||||||||||
| agent: createdDraftSession.agent, | ||||||||||||||||||||||
| agentMentionName, | ||||||||||||||||||||||
| variant, | ||||||||||||||||||||||
| inputMode, | ||||||||||||||||||||||
|
|
@@ -1333,6 +1374,10 @@ export const useSessionUIStore = create<SessionUIState>()((set, get) => ({ | |||||||||||||||||||||
| returnAfterDirectoryCreated: true, | ||||||||||||||||||||||
| }) | ||||||||||||||||||||||
| sessionDirectory = normalizePath(createdWorktree.path) | ||||||||||||||||||||||
| if (!sessionDirectory) { | ||||||||||||||||||||||
| throw new Error("Worktree create missing name/path") | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| await waitForWorktreeBootstrap(sessionDirectory) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
Comment on lines
1376
to
1381
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If sessionDirectory = normalizePath(createdWorktree.path)
if (!sessionDirectory) {
throw new Error("Worktree create missing name/path")
}
try {
await waitForWorktreeBootstrap(sessionDirectory)
} catch (error) {
if (createdWorktree && createdWorktreeProject) {
const { removeProjectWorktree } = await import("@/lib/worktrees/worktreeManager")
await removeProjectWorktree(createdWorktreeProject, createdWorktree, { deleteLocalBranch: true }).catch(() => undefined)
}
throw error
}
} |
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const session = await get().createSession(undefined, sessionDirectory || null, null) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
waitForWorktreeBootstrapfails or times out, it throws an error which is caught by the outertry-catchblock. However, the newly created worktree is not cleaned up, leaving an orphaned worktree and local branch. Wrapping the bootstrap wait in atry-catchblock to clean up the worktree on failure prevents resource leaks.