Skip to content

Commit 4432eb8

Browse files
fix(mobile): keep the source worktree when starting a thread on a branch
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent b44c1ce commit 4432eb8

3 files changed

Lines changed: 92 additions & 3 deletions

File tree

‎apps/mobile/src/features/threads/new-task-context-presentation.test.ts‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
resolveNewTaskBranchWorktreePath,
55
resolveNewTaskBranchLabel,
66
resolveNewTaskLocalWorkspaceSelection,
7+
shouldAutoSelectWorktreeBaseBranch,
78
} from "./new-task-context-presentation";
89

910
describe("resolveNewTaskLocalWorkspaceSelection", () => {
@@ -126,3 +127,59 @@ describe("resolveNewTaskBranchLabel", () => {
126127
).toBe("Choose branch");
127128
});
128129
});
130+
131+
describe("shouldAutoSelectWorktreeBaseBranch", () => {
132+
const settledWorktreeDefault = {
133+
defaultWorkspaceModeSettled: true,
134+
workspaceMode: "worktree" as const,
135+
selectedBranchName: null,
136+
};
137+
138+
it("picks a base branch for a fresh worktree draft", () => {
139+
expect(
140+
shouldAutoSelectWorktreeBaseBranch({
141+
...settledWorktreeDefault,
142+
liveWorkspaceSelection: undefined,
143+
}),
144+
).toBe(true);
145+
expect(
146+
shouldAutoSelectWorktreeBaseBranch({
147+
...settledWorktreeDefault,
148+
liveWorkspaceSelection: { mode: "worktree", branch: null },
149+
}),
150+
).toBe(true);
151+
});
152+
153+
it("keeps a local selection written to the draft in the same commit", () => {
154+
expect(
155+
shouldAutoSelectWorktreeBaseBranch({
156+
...settledWorktreeDefault,
157+
liveWorkspaceSelection: { mode: "local", branch: "t3code/0790af30" },
158+
}),
159+
).toBe(false);
160+
});
161+
162+
it("does nothing once a branch is selected or the mode is local", () => {
163+
expect(
164+
shouldAutoSelectWorktreeBaseBranch({
165+
...settledWorktreeDefault,
166+
selectedBranchName: "main",
167+
liveWorkspaceSelection: { mode: "worktree", branch: "main" },
168+
}),
169+
).toBe(false);
170+
expect(
171+
shouldAutoSelectWorktreeBaseBranch({
172+
...settledWorktreeDefault,
173+
workspaceMode: "local",
174+
liveWorkspaceSelection: undefined,
175+
}),
176+
).toBe(false);
177+
expect(
178+
shouldAutoSelectWorktreeBaseBranch({
179+
...settledWorktreeDefault,
180+
defaultWorkspaceModeSettled: false,
181+
liveWorkspaceSelection: undefined,
182+
}),
183+
).toBe(false);
184+
});
185+
});

‎apps/mobile/src/features/threads/new-task-context-presentation.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,31 @@ export function resolveNewTaskBranchLabel(input: {
7474
return `From ${baseRef}`;
7575
}
7676

77+
/**
78+
* Whether the worktree default still needs a base branch. Reads the draft's
79+
* live selection rather than the rendered one: the route that carries a
80+
* thread's branch writes its local selection in the same commit this effect
81+
* runs, and a stale worktree/null view of the draft must not overwrite it.
82+
*/
83+
export function shouldAutoSelectWorktreeBaseBranch(input: {
84+
readonly defaultWorkspaceModeSettled: boolean;
85+
readonly workspaceMode: WorkspaceMode;
86+
readonly selectedBranchName: string | null;
87+
readonly liveWorkspaceSelection:
88+
| { readonly mode: WorkspaceMode; readonly branch: string | null }
89+
| undefined;
90+
}): boolean {
91+
if (
92+
!input.defaultWorkspaceModeSettled ||
93+
input.workspaceMode !== "worktree" ||
94+
input.selectedBranchName !== null
95+
) {
96+
return false;
97+
}
98+
const live = input.liveWorkspaceSelection;
99+
return live === undefined || (live.mode === "worktree" && live.branch === null);
100+
}
101+
77102
export function shouldCheckoutNewTaskBranch(input: {
78103
readonly branchIsCurrent: boolean;
79104
readonly branchWorktreePath: string | null | undefined;

‎apps/mobile/src/features/threads/new-task-flow-provider.tsx‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ import { useLegacyPlanModeState } from "./use-legacy-plan-mode-enabled";
9898
import {
9999
resolveNewTaskBranchWorktreePath,
100100
resolveNewTaskLocalWorkspaceSelection,
101+
shouldAutoSelectWorktreeBaseBranch,
101102
} from "./new-task-context-presentation";
102103
import { resolveEnvironmentProjectMatch } from "./new-task-project-selection";
103104
import { resolveProjectThreadCreationBranch } from "./projectThreadCreationValidation";
@@ -873,9 +874,14 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
873874

874875
useEffect(() => {
875876
if (
876-
!defaultWorkspaceModeSettled ||
877-
workspaceMode !== "worktree" ||
878-
selectedBranchName !== null
877+
!shouldAutoSelectWorktreeBaseBranch({
878+
defaultWorkspaceModeSettled,
879+
workspaceMode,
880+
selectedBranchName,
881+
liveWorkspaceSelection: selectedProjectDraftKey
882+
? getComposerDraftSnapshot(selectedProjectDraftKey).workspaceSelection
883+
: undefined,
884+
})
879885
) {
880886
return;
881887
}
@@ -894,6 +900,7 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
894900
defaultWorkspaceModeSettled,
895901
selectBranch,
896902
selectedBranchName,
903+
selectedProjectDraftKey,
897904
workspaceMode,
898905
]);
899906

0 commit comments

Comments
 (0)