@@ -393,6 +393,7 @@ import {
393393 previewAnnotationContextReference ,
394394 reviewCommentContextLabel ,
395395 terminalContextReference ,
396+ threadContextsBelongToEnvironment ,
396397} from "../lib/composerContextRecords" ;
397398import { type ReviewCommentContext } from "../reviewCommentContext" ;
398399import { environmentCatalog } from "../connection/catalog" ;
@@ -1800,6 +1801,9 @@ export default function ChatView(props: ChatViewProps) {
18001801 const draft = store . getComposerDraft ( composerDraftTarget ) ;
18011802 return ( draft ?. images . length ?? 0 ) > 0 || ( draft ?. files . length ?? 0 ) > 0 ;
18021803 } ) ;
1804+ const composerHasThreadContexts = useComposerDraftStore (
1805+ ( store ) => ( store . getComposerDraft ( composerDraftTarget ) ?. threadContexts . length ?? 0 ) > 0 ,
1806+ ) ;
18031807 // Anything beyond the prompt text: attachments, terminal or element contexts, annotations.
18041808 const composerHasNonPromptContent = useComposerDraftStore ( ( store ) => {
18051809 const draft = store . getComposerDraft ( composerDraftTarget ) ;
@@ -2851,7 +2855,14 @@ export default function ChatView(props: ChatViewProps) {
28512855 projectGroupingSettings ,
28522856 ) ;
28532857 const storedDraftSession = getDraftSessionByLogicalProjectKey ( logicalProjectKey ) ;
2854- if ( storedDraftSession ) {
2858+ if (
2859+ storedDraftSession &&
2860+ threadContextsBelongToEnvironment (
2861+ useComposerDraftStore . getState ( ) . getComposerDraft ( storedDraftSession . draftId )
2862+ ?. threadContexts ?? [ ] ,
2863+ activeProjectRef . environmentId ,
2864+ )
2865+ ) {
28552866 setDraftThreadContext ( storedDraftSession . draftId , input ) ;
28562867 setLogicalProjectDraftThreadId (
28572868 logicalProjectKey ,
@@ -3036,6 +3047,7 @@ export default function ChatView(props: ChatViewProps) {
30363047 canAutoBalanceEnvironments &&
30373048 loadBalancingSettings . loadBalancingEnabled &&
30383049 draftThread ?. environmentSelection !== "manual" &&
3050+ ! composerHasThreadContexts &&
30393051 ( ! composerHasAttachments || Boolean ( draftThread ?. loadBalancedEnvironmentId ) ) &&
30403052 ( ! draftThread ?. branch || draftThread . environmentSelection === "auto" ) &&
30413053 ! draftThread ?. worktreePath ,
@@ -4387,6 +4399,14 @@ export default function ChatView(props: ChatViewProps) {
43874399 ] ) ;
43884400 const onAutoEnvironment = useCallback ( ( ) => {
43894401 if ( envLocked || ! draftId ) return ;
4402+ if ( useComposerDraftStore . getState ( ) . getComposerDraft ( draftId ) ?. threadContexts . length ) {
4403+ toastManager . add ( {
4404+ type : "warning" ,
4405+ title : "Keep thread references on this machine" ,
4406+ description : "Remove thread references before choosing automatic routing." ,
4407+ } ) ;
4408+ return ;
4409+ }
43904410 if ( composerHasAttachments ) {
43914411 toastManager . add ( {
43924412 type : "warning" ,
@@ -4443,6 +4463,22 @@ export default function ChatView(props: ChatViewProps) {
44434463 ( env ) => env . environmentId === nextEnvironmentId ,
44444464 ) ;
44454465 if ( ! target ) return ;
4466+ const referencesAllowTarget = ( ) => {
4467+ if (
4468+ threadContextsBelongToEnvironment (
4469+ useComposerDraftStore . getState ( ) . getComposerDraft ( draftId ) ?. threadContexts ?? [ ] ,
4470+ target . environmentId ,
4471+ )
4472+ )
4473+ return true ;
4474+ toastManager . add ( {
4475+ type : "warning" ,
4476+ title : "Keep thread references on their machine" ,
4477+ description : "Remove thread references before switching to another machine." ,
4478+ } ) ;
4479+ return false ;
4480+ } ;
4481+ if ( ! referencesAllowTarget ( ) ) return ;
44464482 const request = { environmentId : target . environmentId } ;
44474483 environmentChangeRef . current = request ;
44484484 setIsEnvironmentChanging ( false ) ;
@@ -4457,6 +4493,7 @@ export default function ChatView(props: ChatViewProps) {
44574493 currentDraft . projectId !== originalDraft . projectId
44584494 )
44594495 return ;
4496+ if ( ! referencesAllowTarget ( ) ) return ;
44604497 const projectRef = scopeProjectRef ( target . environmentId , project . id ) ;
44614498 if ( activeProjectIsScratch ) {
44624499 // Scratch projects are machine-local, so move their logical mapping too.
@@ -8739,6 +8776,15 @@ export default function ChatView(props: ChatViewProps) {
87398776 notifyDirectAnnotationAttached ( ) ;
87408777 return ;
87418778 }
8779+ if ( ! threadContextsBelongToEnvironment ( sendCtx . threadContexts , environmentId ) ) {
8780+ toastManager . add ( {
8781+ type : "warning" ,
8782+ title : "Thread references are on another machine" ,
8783+ description :
8784+ "Choose the machine that owns these threads, or remove their references before sending." ,
8785+ } ) ;
8786+ return ;
8787+ }
87428788 const multipleModelSelections = sendCtx . multipleModelSelections ;
87438789 if (
87448790 multipleModelSelections !== null &&
0 commit comments