@@ -29,6 +29,7 @@ import { useEnvironments, usePrimaryEnvironmentId } from "../state/environments"
2929import {
3030 resolveAvailableNewThreadProjectRef ,
3131 resolveNewDraftStartFromOrigin ,
32+ resolveWorkspaceOptionsAfterEnvironmentRetarget ,
3233} from "../lib/chatThreadActions" ;
3334import { readT3ProjectFileDefaultThreadEnvMode } from "../lib/t3ProjectFileDefaults" ;
3435import { shouldReadProjectFileForNewThreadDefaults } from "../session-logic" ;
@@ -198,6 +199,11 @@ export function useNewThreadHandler() {
198199 candidate . id === projectRef . projectId &&
199200 candidate . environmentId === projectRef . environmentId ,
200201 ) ?? requestedProject ;
202+ const workspaceOptions = resolveWorkspaceOptionsAfterEnvironmentRetarget ( {
203+ requestedEnvironmentId : requestedProjectRef . environmentId ,
204+ targetEnvironmentId : projectRef . environmentId ,
205+ options,
206+ } ) ;
201207 // The shared resolver owns the priority order. The t3.json read is
202208 // skipped entirely when a higher-priority source decides or the
203209 // environment cannot serve: executeAtomQuery waits on a live RPC, so
@@ -223,10 +229,10 @@ export function useNewThreadHandler() {
223229 const logicalProjectKey = project
224230 ? deriveLogicalProjectKeyFromSettings ( project , projectGroupingSettings )
225231 : scopedProjectKey ( projectRef ) ;
226- const hasBranchOption = options ?. branch !== undefined ;
227- const hasWorktreePathOption = options ?. worktreePath !== undefined ;
228- const hasEnvModeOption = options ?. envMode !== undefined ;
229- const hasStartFromOriginOption = options ?. startFromOrigin !== undefined ;
232+ const hasBranchOption = workspaceOptions ?. branch !== undefined ;
233+ const hasWorktreePathOption = workspaceOptions ?. worktreePath !== undefined ;
234+ const hasEnvModeOption = workspaceOptions ?. envMode !== undefined ;
235+ const hasStartFromOriginOption = workspaceOptions ?. startFromOrigin !== undefined ;
230236 const storedDraftThread = getDraftSessionByLogicalProjectKey ( logicalProjectKey ) ;
231237 const storedDraftThreadRef = storedDraftThread
232238 ? scopeThreadRef ( storedDraftThread . environmentId , storedDraftThread . threadId )
@@ -273,7 +279,7 @@ export function useNewThreadHandler() {
273279 // the user may have just picked a branch in the composer.
274280 let workspaceContext : NewThreadWorkspaceOptions | null = null ;
275281 if ( hasExplicitWorkspaceOption ) {
276- workspaceContext = pickExplicitWorkspaceOptions ( options ) ;
282+ workspaceContext = pickExplicitWorkspaceOptions ( workspaceOptions ) ;
277283 } else if ( ! isDraftAlreadyOpen ) {
278284 const defaultEnvMode = await resolveDefaultEnvMode ( ) ;
279285 // The await yields. If the draft was opened (a concurrent
@@ -379,14 +385,17 @@ export function useNewThreadHandler() {
379385 hasEnvModeOption ||
380386 hasStartFromOriginOption
381387 ) {
382- setDraftThreadContext ( currentRouteTarget . draftId , pickExplicitWorkspaceOptions ( options ) ) ;
388+ setDraftThreadContext (
389+ currentRouteTarget . draftId ,
390+ pickExplicitWorkspaceOptions ( workspaceOptions ) ,
391+ ) ;
383392 }
384393 setLogicalProjectDraftThreadId ( logicalProjectKey , projectRef , currentRouteTarget . draftId , {
385394 threadId : latestActiveDraftThread . threadId ,
386395 createdAt : latestActiveDraftThread . createdAt ,
387396 runtimeMode : latestActiveDraftThread . runtimeMode ,
388397 interactionMode : latestActiveDraftThread . interactionMode ,
389- ...pickExplicitWorkspaceOptions ( options ) ,
398+ ...pickExplicitWorkspaceOptions ( workspaceOptions ) ,
390399 } ) ;
391400 return Promise . resolve ( {
392401 draftId : currentRouteTarget . draftId ,
@@ -398,7 +407,7 @@ export function useNewThreadHandler() {
398407 const threadId = newThreadId ( ) ;
399408 const createdAt = new Date ( ) . toISOString ( ) ;
400409 return ( async ( ) => {
401- const initialEnvMode = options ?. envMode ?? ( await resolveDefaultEnvMode ( ) ) ;
410+ const initialEnvMode = workspaceOptions ?. envMode ?? ( await resolveDefaultEnvMode ( ) ) ;
402411 // The await yields, so a concurrent invocation may have registered a
403412 // draft for this logical project in the meantime. Registering ours
404413 // too would evict that draft while its navigation is in flight —
@@ -426,7 +435,7 @@ export function useNewThreadHandler() {
426435 createdAt : racedDraft . createdAt ,
427436 runtimeMode : racedDraft . runtimeMode ,
428437 interactionMode : racedDraft . interactionMode ,
429- ...pickExplicitWorkspaceOptions ( options ) ,
438+ ...pickExplicitWorkspaceOptions ( workspaceOptions ) ,
430439 } ) ;
431440 carryComposerContentTo ( racedDraft . draftId ) ;
432441 await router . navigate ( {
@@ -439,11 +448,11 @@ export function useNewThreadHandler() {
439448 setLogicalProjectDraftThreadId ( logicalProjectKey , projectRef , draftId , {
440449 threadId,
441450 createdAt,
442- branch : options ?. branch ?? null ,
443- worktreePath : options ?. worktreePath ?? null ,
451+ branch : workspaceOptions ?. branch ?? null ,
452+ worktreePath : workspaceOptions ?. worktreePath ?? null ,
444453 envMode : initialEnvMode ,
445454 startFromOrigin :
446- options ?. startFromOrigin ??
455+ workspaceOptions ?. startFromOrigin ??
447456 resolveNewDraftStartFromOrigin ( {
448457 envMode : initialEnvMode ,
449458 newWorktreesStartFromOrigin : primaryServerSettings . newWorktreesStartFromOrigin ,
0 commit comments