@@ -1639,6 +1639,7 @@ export default function ChatView(props: ChatViewProps) {
16391639 // worktree send starts and cleared once the turn starts or the next send
16401640 // begins, so a failed or cancelled card stays until the user acts.
16411641 const [ worktreeSetupRef , setWorktreeSetupRef ] = useState < {
1642+ environmentId : EnvironmentId ;
16421643 threadId : ThreadId ;
16431644 ownerKey : string ;
16441645 } | null > ( null ) ;
@@ -3340,10 +3341,13 @@ export default function ChatView(props: ChatViewProps) {
33403341 const worktreeSetupOwnerKey = draftId ?? routeThreadKey ;
33413342 const worktreeSetupActive =
33423343 worktreeSetupRef !== null && worktreeSetupRef . ownerKey === worktreeSetupOwnerKey ;
3344+ // The setup runs on the environment that received the dispatch, so both
3345+ // the subscription and cancel target that one even if the draft's machine
3346+ // picker changes underneath.
33433347 const worktreeSetupQuery = useEnvironmentQuery (
33443348 worktreeSetupActive
33453349 ? vcsEnvironment . worktreeSetup ( {
3346- environmentId,
3350+ environmentId : worktreeSetupRef . environmentId ,
33473351 input : { threadId : worktreeSetupRef . threadId } ,
33483352 } )
33493353 : null ,
@@ -3371,9 +3375,12 @@ export default function ChatView(props: ChatViewProps) {
33713375 reportFailure : false ,
33723376 } ) ;
33733377 const onCancelWorktreeSetup = useCallback ( ( ) => {
3374- if ( ! worktreeSetup || worktreeSetup . phase !== "running" ) return ;
3375- void cancelWorktreeSetup ( { environmentId, input : { threadId : worktreeSetup . threadId } } ) ;
3376- } , [ cancelWorktreeSetup , environmentId , worktreeSetup ] ) ;
3378+ if ( ! worktreeSetup || ! worktreeSetupRef || worktreeSetup . phase !== "running" ) return ;
3379+ void cancelWorktreeSetup ( {
3380+ environmentId : worktreeSetupRef . environmentId ,
3381+ input : { threadId : worktreeSetup . threadId } ,
3382+ } ) ;
3383+ } , [ cancelWorktreeSetup , worktreeSetup , worktreeSetupRef ] ) ;
33773384 // The setup terminal belongs to the thread that was set up. A failed
33783385 // bootstrap deletes that thread and closes its terminals, so only offer the
33793386 // terminal while the setup thread is still the active one.
@@ -7228,7 +7235,9 @@ export default function ChatView(props: ChatViewProps) {
72287235 submissionIntent : resolvedSubmissionIntent ,
72297236 } ) ;
72307237 setWorktreeSetupRef (
7231- baseBranchForWorktree ? { threadId : threadIdForSend , ownerKey : worktreeSetupOwnerKey } : null ,
7238+ baseBranchForWorktree
7239+ ? { environmentId, threadId : threadIdForSend , ownerKey : worktreeSetupOwnerKey }
7240+ : null ,
72327241 ) ;
72337242
72347243 const messageIdForSend = newMessageId ( ) ;
@@ -8278,23 +8287,42 @@ export default function ChatView(props: ChatViewProps) {
82788287 // the bootstrap. The cancelled dispatch fails and puts the message back in
82798288 // the composer; the effect below resends it once that has settled.
82808289 const onWorktreeSetupWorkLocally = useCallback ( ( ) => {
8281- if ( ! worktreeSetup || worktreeSetup . phase !== "running" || ! isLocalDraftThread ) return ;
8282- const threadId = worktreeSetup . threadId ;
8290+ if (
8291+ ! worktreeSetup ||
8292+ ! worktreeSetupRef ||
8293+ worktreeSetup . phase !== "running" ||
8294+ ! isLocalDraftThread
8295+ ) {
8296+ return ;
8297+ }
8298+ const target = {
8299+ environmentId : worktreeSetupRef . environmentId ,
8300+ input : { threadId : worktreeSetup . threadId } ,
8301+ } ;
82838302 void ( async ( ) => {
8284- const result = await cancelWorktreeSetup ( { environmentId , input : { threadId } } ) ;
8303+ const result = await cancelWorktreeSetup ( target ) ;
82858304 if ( result . _tag !== "Success" || ! result . value . cancelled ) return ;
82868305 onEnvModeChange ( "local" ) ;
82878306 setWorkLocallyResendPending ( true ) ;
82888307 } ) ( ) ;
8289- } , [ cancelWorktreeSetup , environmentId , isLocalDraftThread , onEnvModeChange , worktreeSetup ] ) ;
8308+ } , [ cancelWorktreeSetup , isLocalDraftThread , onEnvModeChange , worktreeSetup , worktreeSetupRef ] ) ;
82908309 const onSendRef = useRef ( onSend ) ;
82918310 onSendRef . current = onSend ;
8311+ // Resend once the cancelled dispatch has settled and the composer is free.
8312+ // The flag stays set until `onSend` gets past its own guards, so a reconnect
8313+ // or a loading thread in between does not lose the message.
8314+ const workLocallyResendReady =
8315+ workLocallyResendPending &&
8316+ ! isSendBusy &&
8317+ ! isConnecting &&
8318+ ! threadDetailLoading &&
8319+ clientSettingsHydrated &&
8320+ sendEnvMode === "local" ;
82928321 useEffect ( ( ) => {
8293- if ( ! workLocallyResendPending || isSendBusy || sendInFlightRef . current ) return ;
8294- if ( sendEnvMode !== "local" ) return ;
8322+ if ( ! workLocallyResendReady || sendInFlightRef . current ) return ;
82958323 setWorkLocallyResendPending ( false ) ;
82968324 void onSendRef . current ( ) ;
8297- } , [ isSendBusy , sendEnvMode , workLocallyResendPending ] ) ;
8325+ } , [ workLocallyResendReady ] ) ;
82988326
82998327 const onStartFromOriginChange = ( nextStartFromOrigin : boolean ) => {
83008328 if ( canOverrideServerThreadEnvMode && activeThread ) {
0 commit comments