@@ -52,6 +52,7 @@ import {
5252 useComposerDraft ,
5353} from "../../state/use-composer-drafts" ;
5454import { useDebouncedValue , usePaginatedBranches } from "../../state/queries" ;
55+ import { vcsEnvironment } from "../../state/vcs" ;
5556import {
5657 flattenQueuedThreadMessages ,
5758 threadOutboxManager ,
@@ -139,6 +140,7 @@ type NewTaskFlowContextValue = {
139140 readonly branchesFetchingNextPage : boolean ;
140141 readonly hasMoreBranches : boolean ;
141142 readonly availableBranches : ReadonlyArray < VcsRef > ;
143+ readonly currentCheckoutBranchName : string | null ;
142144 readonly runtimeMode : RuntimeMode ;
143145 readonly interactionMode : ProviderInteractionMode ;
144146 readonly planModeEnabled : boolean ;
@@ -553,6 +555,22 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
553555 ) ,
554556 [ allBranchRefs ] ,
555557 ) ;
558+ // The ref actually checked out in the project root, serialized onto new
559+ // local threads. It comes from the live status stream rather than listRefs'
560+ // `current` flag, which is served from a cache that can lag an out-of-band
561+ // `git switch` by minutes — and from the same value the PR badge compares
562+ // against. Detached HEAD and non-repository projects report no ref, so this
563+ // stays null instead of fabricating a branch. The status family is
564+ // deduplicated per (environmentId, cwd) with the thread rows.
565+ const projectGitStatus = useEnvironmentQuery (
566+ branchTarget . environmentId !== null && branchTarget . cwd !== null
567+ ? vcsEnvironment . status ( {
568+ environmentId : branchTarget . environmentId ,
569+ input : { cwd : branchTarget . cwd } ,
570+ } )
571+ : null ,
572+ ) ;
573+ const currentCheckoutBranchName = projectGitStatus . data ?. refName ?? null ;
556574
557575 const filteredBranches = useMemo ( ( ) => {
558576 const query = branchQuery . trim ( ) . toLowerCase ( ) ;
@@ -859,6 +877,10 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
859877 ...( projectTitle !== undefined ? { projectTitle } : { } ) ,
860878 ...( projectCwd !== undefined ? { projectCwd } : { } ) ,
861879 workspaceMode : mode ,
880+ // Only an explicit picker choice, never the current checkout: a
881+ // queued local task drains days later against whatever is checked
882+ // out then, so recording a queue-time guess would pin a stale label
883+ // to a thread that ran somewhere else.
862884 branch : workspaceSelection ?. branch ?? null ,
863885 worktreePath : mode === "worktree" ? null : ( workspaceSelection ?. worktreePath ?? null ) ,
864886 // The draft only carries the flag when the user touched it; fall
@@ -996,6 +1018,7 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
9961018 branchesFetchingNextPage,
9971019 hasMoreBranches,
9981020 availableBranches,
1021+ currentCheckoutBranchName,
9991022 runtimeMode,
10001023 interactionMode,
10011024 planModeEnabled,
@@ -1043,6 +1066,7 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
10431066 branchesFetchingNextPage ,
10441067 buildPendingTaskMessage ,
10451068 cancelEditingPendingTask ,
1069+ currentCheckoutBranchName ,
10461070 editingPendingTask ,
10471071 environments ,
10481072 expandedProvider ,
0 commit comments