@@ -53,6 +53,7 @@ import { useAtomCommand } from "../../state/use-atom-command";
5353import {
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" ;
99100import { useLegacyPlanModeState } from "./use-legacy-plan-mode-enabled" ;
100101import {
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