Skip to content

Commit 70bbcc3

Browse files
refactor(mobile): new-task branch defaults are derived, not written after render
The new-task flow filled in branches by writing them into the draft after render: one effect wrote the default branch for a worktree selection with no base, another wrote the current branch into a local selection once refs named it, tracked by a component-local ref set that was lost on remount. fillNewTaskWorkspaceBranch derives both instead. A worktree with no base reads the default branch (origin/<default> included) once the t3.json default has settled; a local selection chosen before refs loaded carries a persisted followsCurrentCheckout flag and reads the current branch when refs arrive. The provider and the draft screen's send path resolve through the same function, so what is shown is what is sent. The home and sidebar pending-drafts list reads the same derived branch: a draft row with no stored branch loads the project's refs (the same first page the new-task flow uses) and resolves through fillNewTaskWorkspaceBranch, so a worktree draft shows its default base and a local draft that follows the checkout shows the current branch. followsCurrentCheckout is intentionally never cleared: a local task runs on the checkout, so the draft follows it until the user picks a branch. Effects: new-task-flow-provider.tsx 3 -> 1 (both branch-writing effects removed; the draft-creation effect stays). Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent becf853 commit 70bbcc3

9 files changed

Lines changed: 242 additions & 107 deletions

‎apps/mobile/src/features/threads/NewTaskDraftScreen.tsx‎

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ import { resolveDraftProjectSelection } from "./new-task-project-selection";
8888
import {
8989
resolveNewTaskBranchLabel,
9090
resolveNewTaskWorkspaceLabel,
91-
resolveNewTaskWorkspaceSelection,
9291
} from "./new-task-context-presentation";
9392
import { useIncomingShare } from "../sharing/IncomingShareProvider";
9493
import { selectIncomingShareAttachmentsForServer } from "../sharing/incoming-share-model";
@@ -808,15 +807,7 @@ export function NewTaskDraftScreen(props: {
808807
draft.modelSelection ?? null,
809808
selectedProject.id,
810809
) ?? flow.selectedModel;
811-
const workspaceSelection = resolveNewTaskWorkspaceSelection({
812-
selection: draft.workspaceSelection,
813-
fallback: {
814-
mode: flow.workspaceMode,
815-
branch: flow.selectedBranchName,
816-
worktreePath: flow.selectedWorktreePath,
817-
},
818-
isGitRepo: flow.isGitRepo,
819-
});
810+
const workspaceSelection = flow.resolveWorkspaceSelection(draft.workspaceSelection);
820811
const workspaceMode = workspaceSelection.mode;
821812
const selectedBranchName = workspaceSelection.branch;
822813
const initialMessageText = draft.text.trim();

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, expect, it } from "vite-plus/test";
22

33
import {
4+
fillNewTaskWorkspaceBranch,
45
resolveNewTaskBranchLabel,
56
resolveNewTaskBranchWorktreePath,
67
resolveNewTaskLocalWorkspaceSelection,
@@ -105,3 +106,66 @@ describe("resolveNewTaskBranchWorktreePath", () => {
105106
).toBeNull();
106107
});
107108
});
109+
110+
describe("fillNewTaskWorkspaceBranch", () => {
111+
const branches = [
112+
{ name: "origin/main", current: false, isDefault: true, isRemote: true },
113+
{ name: "feature", current: true, isDefault: false, worktreePath: "/repo-feature" },
114+
];
115+
const base = {
116+
branches,
117+
projectCwd: "/repo",
118+
worktreeDefaultSettled: true,
119+
};
120+
121+
it("bases a new worktree on the default branch, even when only the remote has it", () => {
122+
expect(
123+
fillNewTaskWorkspaceBranch({
124+
...base,
125+
selection: { mode: "worktree", branch: null, worktreePath: null },
126+
}),
127+
).toEqual({ mode: "worktree", branch: "origin/main", worktreePath: null });
128+
});
129+
130+
it("waits for the workspace default to settle before picking a base", () => {
131+
const selection = { mode: "worktree", branch: null, worktreePath: null } as const;
132+
expect(fillNewTaskWorkspaceBranch({ ...base, selection, worktreeDefaultSettled: false })).toBe(
133+
selection,
134+
);
135+
});
136+
137+
it("follows the current checkout only for a local selection the user just chose", () => {
138+
const selection = { mode: "local", branch: null, worktreePath: null } as const;
139+
expect(fillNewTaskWorkspaceBranch({ ...base, selection })).toBe(selection);
140+
expect(
141+
fillNewTaskWorkspaceBranch({
142+
...base,
143+
selection: { ...selection, followsCurrentCheckout: true },
144+
}),
145+
).toEqual({
146+
mode: "local",
147+
branch: "feature",
148+
worktreePath: "/repo-feature",
149+
followsCurrentCheckout: true,
150+
});
151+
});
152+
153+
it("fills the current branch once refs arrive after local mode was chosen", () => {
154+
const selection = {
155+
mode: "local",
156+
branch: null,
157+
worktreePath: null,
158+
followsCurrentCheckout: true,
159+
} as const;
160+
expect(fillNewTaskWorkspaceBranch({ ...base, branches: [], selection })).toBe(selection);
161+
expect(fillNewTaskWorkspaceBranch({ ...base, selection })).toMatchObject({
162+
branch: "feature",
163+
worktreePath: "/repo-feature",
164+
});
165+
});
166+
167+
it("keeps an explicit branch", () => {
168+
const selection = { mode: "worktree", branch: "release", worktreePath: null } as const;
169+
expect(fillNewTaskWorkspaceBranch({ ...base, selection })).toBe(selection);
170+
});
171+
});

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,43 @@ export function filterNewTaskBranches<T extends { readonly name: string }>(
111111
? branches
112112
: branches.filter((branch) => branch.name.toLowerCase().includes(query));
113113
}
114+
115+
/**
116+
* A worktree with no chosen base starts from the default branch, and a local
117+
* selection the user just switched to follows the current checkout until refs
118+
* load. Both are derived, never written back, so a late ref list or t3.json
119+
* default still wins.
120+
*/
121+
export function fillNewTaskWorkspaceBranch(input: {
122+
readonly selection: ComposerDraftWorkspaceSelection;
123+
readonly branches: ReadonlyArray<{
124+
readonly name: string;
125+
readonly current: boolean;
126+
readonly isDefault: boolean;
127+
readonly isRemote?: boolean;
128+
readonly worktreePath?: string | null;
129+
}>;
130+
readonly projectCwd: string | null;
131+
readonly worktreeDefaultSettled: boolean;
132+
}): ComposerDraftWorkspaceSelection {
133+
const { selection } = input;
134+
if (selection.branch !== null) return selection;
135+
if (selection.mode === "worktree") {
136+
// The default may only exist as origin/<default>, so search remote refs too.
137+
const base =
138+
input.branches.find((branch) => branch.isDefault) ??
139+
input.branches.find((branch) => !branch.isRemote && branch.current) ??
140+
null;
141+
return input.worktreeDefaultSettled && base
142+
? { ...selection, branch: base.name, worktreePath: null }
143+
: selection;
144+
}
145+
if (!selection.followsCurrentCheckout || input.projectCwd === null) return selection;
146+
const local = resolveNewTaskLocalWorkspaceSelection({
147+
branches: input.branches.filter((branch) => !branch.isRemote),
148+
projectCwd: input.projectCwd,
149+
});
150+
return local.awaitsCurrentBranch
151+
? selection
152+
: { ...selection, branch: local.branch, worktreePath: local.worktreePath };
153+
}

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

Lines changed: 36 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import { useAtomCommand } from "../../state/use-atom-command";
5353
import {
5454
appendComposerDraftAttachments,
5555
type ComposerDraftInsertion,
56+
type ComposerDraftWorkspaceSelection,
5657
composerDraftsAtom,
5758
createNewTaskDraft,
5859
getComposerDraftSnapshot,
@@ -98,6 +99,7 @@ import {
9899
} from "../../state/legacy-plan-mode";
99100
import { useLegacyPlanModeState } from "./use-legacy-plan-mode-enabled";
100101
import {
102+
fillNewTaskWorkspaceBranch,
101103
resolveNewTaskBranchWorktreePath,
102104
resolveNewTaskLocalWorkspaceSelection,
103105
resolveNewTaskWorkspaceSelection,
@@ -139,6 +141,9 @@ type NewTaskFlowContextValue = {
139141
readonly canChooseWorkspace: boolean;
140142
readonly selectedBranchName: string | null;
141143
readonly selectedWorktreePath: string | null;
144+
readonly resolveWorkspaceSelection: (
145+
selection: ComposerDraftWorkspaceSelection | undefined,
146+
) => ComposerDraftWorkspaceSelection;
142147
readonly startFromOrigin: boolean;
143148
readonly pullRequestReference: string | undefined;
144149
readonly draftKey: string | null;
@@ -239,15 +244,13 @@ export function NewTaskFlowProvider(
239244
const [activeDraftKey, setActiveDraftKey] = useState<string | null>(null);
240245
const [submitting, setSubmitting] = useState(false);
241246
const [expandedProvider, setExpandedProvider] = useState<string | null>(null);
242-
const pendingLocalBranchSyncDraftKeysRef = useRef(new Set<string>());
243247

244248
const reset = useCallback(() => {
245249
setSelectedEnvironmentId(null);
246250
setSelectedProjectKey(null);
247251
setActiveDraftKey(null);
248252
setSubmitting(false);
249253
setExpandedProvider(null);
250-
pendingLocalBranchSyncDraftKeysRef.current.clear();
251254
}, []);
252255

253256
const projectsForEnvironment = pipe(
@@ -437,11 +440,29 @@ export function NewTaskFlowProvider(
437440
selectedProjectDraft.workspaceSelection?.mode !== undefined ||
438441
projectSettings.sources.defaultThreadEnvMode !== "environment" ||
439442
(!t3ProjectFileQuery.isPending && !fileAccessPending);
440-
const workspaceSelection = resolveNewTaskWorkspaceSelection({
441-
selection: canChooseWorkspace ? selectedProjectDraft.workspaceSelection : undefined,
442-
fallback: { mode: defaultWorkspaceMode, branch: null, worktreePath: null },
443-
isGitRepo,
443+
const branchState = usePaginatedBranches({
444+
environmentId: selectedProject?.environmentId ?? null,
445+
cwd: selectedProject?.workspaceRoot || null,
444446
});
447+
const branchesLoading = branchState.isPending && branchState.data === null;
448+
const allBranchRefs = branchState.refs;
449+
const availableBranches = pipe(
450+
allBranchRefs,
451+
Arr.filter((branch) => !branch.isRemote),
452+
);
453+
454+
const resolveWorkspaceSelection = (selection: ComposerDraftWorkspaceSelection | undefined) =>
455+
fillNewTaskWorkspaceBranch({
456+
selection: resolveNewTaskWorkspaceSelection({
457+
selection: canChooseWorkspace ? selection : undefined,
458+
fallback: { mode: defaultWorkspaceMode, branch: null, worktreePath: null },
459+
isGitRepo,
460+
}),
461+
branches: allBranchRefs,
462+
projectCwd: selectedProject?.workspaceRoot ?? null,
463+
worktreeDefaultSettled: defaultWorkspaceModeSettled,
464+
});
465+
const workspaceSelection = resolveWorkspaceSelection(selectedProjectDraft.workspaceSelection);
445466
const workspaceMode = workspaceSelection.mode;
446467
const selectedBranchName = workspaceSelection.branch;
447468
const selectedWorktreePath = workspaceSelection.worktreePath;
@@ -584,16 +605,6 @@ export function NewTaskFlowProvider(
584605
}
585606
replaceComposerDraftAttachments(selectedProjectDraftKey, []);
586607
};
587-
const branchState = usePaginatedBranches({
588-
environmentId: selectedProject?.environmentId ?? null,
589-
cwd: selectedProject?.workspaceRoot || null,
590-
});
591-
const branchesLoading = branchState.isPending && branchState.data === null;
592-
const allBranchRefs = branchState.refs;
593-
const availableBranches = pipe(
594-
allBranchRefs,
595-
Arr.filter((branch) => !branch.isRemote),
596-
);
597608

598609
const carryDraftContentTo = (project: EnvironmentProject) => {
599610
const target = { environmentId: project.environmentId, projectId: project.id };
@@ -690,60 +701,24 @@ export function NewTaskFlowProvider(
690701
branches: availableBranches,
691702
projectCwd: selectedProject.workspaceRoot,
692703
});
693-
if (mode === "local" && localSelection.awaitsCurrentBranch) {
694-
pendingLocalBranchSyncDraftKeysRef.current.add(selectedProjectDraftKey);
695-
} else {
696-
pendingLocalBranchSyncDraftKeysRef.current.delete(selectedProjectDraftKey);
697-
}
698704
updateComposerDraftSettings(selectedProjectDraftKey, {
699705
workspaceSelection: {
700706
mode,
701707
branch: mode === "local" ? localSelection.branch : selectedBranchName,
702708
worktreePath: mode === "local" ? localSelection.worktreePath : selectedWorktreePath,
703709
...(draftStartFromOrigin !== undefined ? { startFromOrigin: draftStartFromOrigin } : {}),
710+
// Refs have not named the current branch yet; the selection follows it.
711+
...(mode === "local" && localSelection.awaitsCurrentBranch
712+
? { followsCurrentCheckout: true }
713+
: {}),
704714
},
705715
});
706716
};
707717

708-
useEffect(() => {
709-
if (
710-
workspaceMode !== "local" ||
711-
!selectedProject ||
712-
!selectedProjectDraftKey ||
713-
!pendingLocalBranchSyncDraftKeysRef.current.has(selectedProjectDraftKey)
714-
) {
715-
return;
716-
}
717-
const localSelection = resolveNewTaskLocalWorkspaceSelection({
718-
branches: availableBranches,
719-
projectCwd: selectedProject.workspaceRoot,
720-
});
721-
if (localSelection.awaitsCurrentBranch) {
722-
return;
723-
}
724-
725-
pendingLocalBranchSyncDraftKeysRef.current.delete(selectedProjectDraftKey);
726-
updateComposerDraftSettings(selectedProjectDraftKey, {
727-
workspaceSelection: {
728-
mode: "local",
729-
branch: localSelection.branch,
730-
worktreePath: localSelection.worktreePath,
731-
...(draftStartFromOrigin !== undefined ? { startFromOrigin: draftStartFromOrigin } : {}),
732-
},
733-
});
734-
}, [
735-
availableBranches,
736-
draftStartFromOrigin,
737-
selectedProject,
738-
selectedProjectDraftKey,
739-
workspaceMode,
740-
]);
741-
742718
const selectBranch = (branch: VcsRef) => {
743719
if (!selectedProject || !selectedProjectDraftKey) {
744720
return;
745721
}
746-
pendingLocalBranchSyncDraftKeysRef.current.delete(selectedProjectDraftKey);
747722
updateComposerDraftSettings(selectedProjectDraftKey, {
748723
workspaceSelection: {
749724
mode: workspaceMode,
@@ -768,6 +743,9 @@ export function NewTaskFlowProvider(
768743
branch: selectedBranchName,
769744
worktreePath: selectedWorktreePath,
770745
startFromOrigin: value,
746+
...(selectedBranchName === null && workspaceSelection.followsCurrentCheckout
747+
? { followsCurrentCheckout: true }
748+
: {}),
771749
},
772750
});
773751
};
@@ -781,38 +759,6 @@ export function NewTaskFlowProvider(
781759
refreshBranches();
782760
};
783761

784-
useEffect(() => {
785-
if (
786-
!selectedProjectDraftKey ||
787-
!defaultWorkspaceModeSettled ||
788-
workspaceMode !== "worktree" ||
789-
selectedBranchName !== null
790-
) {
791-
return;
792-
}
793-
// Re-read before replacing: the draft screen can update workspace selection in this same commit.
794-
const live = getComposerDraftSnapshot(selectedProjectDraftKey).workspaceSelection;
795-
if (live && (live.mode !== "worktree" || live.branch !== null)) {
796-
return;
797-
}
798-
// Search unfiltered refs: the default may exist only as origin/<default>, excluded by availableBranches.
799-
const preferredBranch =
800-
allBranchRefs.find((branch) => branch.isDefault) ??
801-
availableBranches.find((branch) => branch.current) ??
802-
null;
803-
if (preferredBranch) {
804-
selectBranch(preferredBranch);
805-
}
806-
}, [
807-
allBranchRefs,
808-
availableBranches,
809-
defaultWorkspaceModeSettled,
810-
selectBranch,
811-
selectedBranchName,
812-
selectedProjectDraftKey,
813-
workspaceMode,
814-
]);
815-
816762
const setRuntimeMode = (value: RuntimeMode) => {
817763
if (selectedProjectDraftKey) {
818764
updateComposerDraftSettings(selectedProjectDraftKey, { runtimeMode: value });
@@ -853,11 +799,7 @@ export function NewTaskFlowProvider(
853799
) {
854800
return null;
855801
}
856-
const workspaceSelection = resolveNewTaskWorkspaceSelection({
857-
selection: canChooseWorkspace ? draft.workspaceSelection : undefined,
858-
fallback: { mode: workspaceMode, branch: null, worktreePath: null },
859-
isGitRepo,
860-
});
802+
const workspaceSelection = resolveWorkspaceSelection(draft.workspaceSelection);
861803
const mode = workspaceSelection.mode;
862804
return {
863805
environmentId: selectedProject.environmentId,
@@ -911,6 +853,7 @@ export function NewTaskFlowProvider(
911853
canChooseWorkspace,
912854
selectedBranchName,
913855
selectedWorktreePath,
856+
resolveWorkspaceSelection,
914857
startFromOrigin,
915858
draftKey: selectedProjectDraftKey,
916859
pullRequestReference: selectedProjectDraft.workspaceSelection?.pullRequestReference,

0 commit comments

Comments
 (0)