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
12 changes: 8 additions & 4 deletions apps/web/src/composerDraftStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1103,13 +1103,14 @@ describe("composerDraftStore project draft thread mapping", () => {
});
});

it("clears branch and worktree context when remapping a draft to another environment", () => {
it("clears branch and worktree but keeps env mode when remapping a draft to another environment", () => {
const store = useComposerDraftStore.getState();
store.setProjectDraftThreadId(projectRef, draftId, {
threadId,
branch: "feature/local-only",
worktreePath: "/tmp/local-worktree",
envMode: "worktree",
startFromOrigin: true,
});

store.setLogicalProjectDraftThreadId(scopedProjectKey(projectRef), remoteProjectRef, draftId, {
Expand All @@ -1121,17 +1122,19 @@ describe("composerDraftStore project draft thread mapping", () => {
projectId,
branch: null,
worktreePath: null,
envMode: "local",
envMode: "worktree",
startFromOrigin: true,
});
});

it("clears branch and worktree context when changing a draft thread project ref", () => {
it("clears branch and worktree but keeps env mode when changing a draft thread project ref", () => {
const store = useComposerDraftStore.getState();
store.setProjectDraftThreadId(projectRef, draftId, {
threadId,
branch: "feature/local-only",
worktreePath: "/tmp/local-worktree",
envMode: "worktree",
startFromOrigin: true,
});

store.setDraftThreadContext(draftId, {
Expand All @@ -1143,7 +1146,8 @@ describe("composerDraftStore project draft thread mapping", () => {
projectId,
branch: null,
worktreePath: null,
envMode: "local",
envMode: "worktree",
startFromOrigin: true,
});
});
});
Expand Down
29 changes: 11 additions & 18 deletions apps/web/src/composerDraftStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1340,6 +1340,10 @@ function createDraftThreadState(
interactionMode?: ProviderInteractionMode;
},
): DraftThreadState {
// A project change (including switching environments within a logical
// project) invalidates machine-specific context: the branch may not exist
// there and the worktree path certainly doesn't. The user's *intent* —
// env mode and start-from-origin — is machine-independent and carries.
const projectChanged =
existingThread !== undefined &&
(existingThread.environmentId !== projectRef.environmentId ||
Expand All @@ -1358,9 +1362,7 @@ function createDraftThreadState(
: (options.branch ?? null);
const nextStartFromOrigin =
options?.startFromOrigin === undefined
? projectChanged
? false
: (existingThread?.startFromOrigin ?? false)
? (existingThread?.startFromOrigin ?? false)
: options.startFromOrigin;
return {
threadId,
Expand All @@ -1374,12 +1376,7 @@ function createDraftThreadState(
branch: nextBranch,
worktreePath: nextWorktreePath,
envMode:
options?.envMode ??
(nextWorktreePath
? "worktree"
: projectChanged
? "local"
: (existingThread?.envMode ?? "local")),
options?.envMode ?? (nextWorktreePath ? "worktree" : (existingThread?.envMode ?? "local")),
startFromOrigin: nextStartFromOrigin,
promotedTo: null,
};
Expand Down Expand Up @@ -2343,6 +2340,9 @@ const composerDraftStore = create<ComposerDraftStoreState>()(
) {
return state;
}
// Mirrors createDraftThreadState: a project/environment change
// drops machine-specific context (branch, worktree path) but
// keeps the user's env mode and start-from-origin intent.
const projectChanged =
nextProjectRef.environmentId !== existing.environmentId ||
nextProjectRef.projectId !== existing.projectId;
Expand All @@ -2360,9 +2360,7 @@ const composerDraftStore = create<ComposerDraftStoreState>()(
: (options.branch ?? null);
const nextStartFromOrigin =
options.startFromOrigin === undefined
? projectChanged
? false
: existing.startFromOrigin
? existing.startFromOrigin
: options.startFromOrigin;
const nextDraftThread: DraftThreadState = {
threadId: existing.threadId,
Expand All @@ -2378,12 +2376,7 @@ const composerDraftStore = create<ComposerDraftStoreState>()(
branch: nextBranch,
worktreePath: nextWorktreePath,
envMode:
options.envMode ??
(nextWorktreePath
? "worktree"
: projectChanged
? "local"
: (existing.envMode ?? "local")),
options.envMode ?? (nextWorktreePath ? "worktree" : (existing.envMode ?? "local")),
startFromOrigin: nextStartFromOrigin,
promotedTo: existing.promotedTo ?? null,
};
Expand Down
3 changes: 1 addition & 2 deletions apps/web/src/hooks/useHandleNewThread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,7 @@ export function useNewThreadHandler() {
// The workspace context must also ride along here: when projectRef
// targets a different physical member of the logical project,
// createDraftThreadState treats the remap as a project change and
// would otherwise wipe branch/worktree and force "local" mode,
// undoing the write above.
// would otherwise wipe branch/worktree, undoing the write above.
setLogicalProjectDraftThreadId(
logicalProjectKey,
projectRef,
Expand Down
Loading