Skip to content

Commit 9a74517

Browse files
refactor(mobile): inline the live-draft guard in the base branch effect
The predicate helper and its truth-table tests only restated the guard; the bug lives in effect ordering, which a pure function cannot exercise. Read the draft snapshot inside the effect and explain the race there. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 4432eb8 commit 9a74517

3 files changed

Lines changed: 11 additions & 91 deletions

File tree

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

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

109
describe("resolveNewTaskLocalWorkspaceSelection", () => {
@@ -127,59 +126,3 @@ describe("resolveNewTaskBranchLabel", () => {
127126
).toBe("Choose branch");
128127
});
129128
});
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: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -74,31 +74,6 @@ 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-
10277
export function shouldCheckoutNewTaskBranch(input: {
10378
readonly branchIsCurrent: boolean;
10479
readonly branchWorktreePath: string | null | undefined;

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

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

875874
useEffect(() => {
876875
if (
877-
!shouldAutoSelectWorktreeBaseBranch({
878-
defaultWorkspaceModeSettled,
879-
workspaceMode,
880-
selectedBranchName,
881-
liveWorkspaceSelection: selectedProjectDraftKey
882-
? getComposerDraftSnapshot(selectedProjectDraftKey).workspaceSelection
883-
: undefined,
884-
})
876+
!selectedProjectDraftKey ||
877+
!defaultWorkspaceModeSettled ||
878+
workspaceMode !== "worktree" ||
879+
selectedBranchName !== null
885880
) {
886881
return;
887882
}
883+
// The draft screen writes a thread's branch and worktree into the draft in
884+
// the same commit this effect runs, so the rendered selection above can be
885+
// stale. Re-read the draft before replacing it.
886+
const live = getComposerDraftSnapshot(selectedProjectDraftKey).workspaceSelection;
887+
if (live && (live.mode !== "worktree" || live.branch !== null)) {
888+
return;
889+
}
888890
// The default may only exist as origin/<default> (isRemote), which
889891
// availableBranches filters out — search the unfiltered refs for it.
890892
const preferredBranch =

0 commit comments

Comments
 (0)