@@ -205,7 +205,6 @@ import {
205205} from "../proposedPlan" ;
206206import {
207207 DEFAULT_INTERACTION_MODE ,
208- DEFAULT_THREAD_TERMINAL_ID ,
209208 MAX_TERMINALS_PER_GROUP ,
210209 type ChatMessage ,
211210 type SessionPhase ,
@@ -306,6 +305,7 @@ import {
306305import { confirmTerminalClose } from "../lib/terminalCloseConfirm" ;
307306import { isPreviewFocused } from "../lib/previewFocus" ;
308307import { getTerminalFocusOwner } from "../lib/terminalFocus" ;
308+ import { findReusableProjectScriptTerminal } from "../lib/projectScriptTerminal" ;
309309import { resolveNewDraftStartFromOrigin } from "../lib/chatThreadActions" ;
310310import {
311311 deriveLogicalProjectKeyFromSettings ,
@@ -1401,7 +1401,6 @@ export default function ChatView(props: ChatViewProps) {
14011401 const storeSplitTerminal = useTerminalUiStateStore ( ( s ) => s . splitTerminal ) ;
14021402 const storeSplitTerminalVertical = useTerminalUiStateStore ( ( s ) => s . splitTerminalVertical ) ;
14031403 const storeNewTerminal = useTerminalUiStateStore ( ( s ) => s . newTerminal ) ;
1404- const storeSetActiveTerminal = useTerminalUiStateStore ( ( s ) => s . setActiveTerminal ) ;
14051404 const storeCloseTerminal = useTerminalUiStateStore ( ( s ) => s . closeTerminal ) ;
14061405 const serverThreadRefs = useThreadRefs ( ) ;
14071406 const serverThreadKeys = serverThreadRefs . map ( scopedThreadKey ) ;
@@ -1570,9 +1569,6 @@ export default function ChatView(props: ChatViewProps) {
15701569 const activeServerOrderedTerminalIds = activeThreadKnownSessions . map (
15711570 ( session ) => session . target . terminalId ,
15721571 ) ;
1573- const activeKnownTerminalIds = [
1574- ...new Set ( [ ...activeServerOrderedTerminalIds , ...terminalUiState . terminalIds ] ) ,
1575- ] ;
15761572 const activeTerminalLabelsById = ( ( ) => {
15771573 const labels = new Map < string , string > ( ) ;
15781574 for ( const session of activeThreadKnownSessions ) {
@@ -3450,35 +3446,43 @@ export default function ChatView(props: ChatViewProps) {
34503446 } ) ;
34513447 }
34523448 const targetCwd = options ?. cwd ?? gitCwd ?? activeProject . workspaceRoot ;
3453- const baseTerminalId =
3454- terminalUiState . activeTerminalId || activeKnownTerminalIds [ 0 ] || DEFAULT_THREAD_TERMINAL_ID ;
3455- const isBaseTerminalBusy = runningTerminalIds . includes ( baseTerminalId ) ;
3456- const wantsNewTerminal = Boolean ( options ?. preferNewTerminal ) || isBaseTerminalBusy ;
3457- const shouldCreateNewTerminal =
3458- wantsNewTerminal ||
3449+ const reusableTerminalId =
3450+ options ?. preferNewTerminal ||
34593451 ! canReuseTerminal ||
3460- ! readEnvironmentScope ( environmentId , AuthTerminalReadScope ) ;
3452+ ! readEnvironmentScope ( environmentId , AuthTerminalReadScope )
3453+ ? null
3454+ : findReusableProjectScriptTerminal ( {
3455+ // Server sessions cover drawer terminals this client has not opened yet, such as a
3456+ // worktree setup shell; the helper drops the panel-owned ones.
3457+ drawerTerminalIds : [
3458+ ...new Set ( [ ...terminalUiState . terminalIds , ...activeServerOrderedTerminalIds ] ) ,
3459+ ] ,
3460+ activeTerminalId : terminalUiState . activeTerminalId ,
3461+ panelTerminalIds,
3462+ runningTerminalIds,
3463+ } ) ;
34613464 const targetWorktreePath = options ?. worktreePath ?? activeThread . worktreePath ?? null ;
34623465
34633466 setTerminalUiLaunchContext ( {
34643467 threadId : activeThreadId ,
34653468 cwd : targetCwd ,
34663469 worktreePath : targetWorktreePath ,
34673470 } ) ;
3468- setTerminalOpen ( true ) ;
34693471 if ( ! activeThreadRef ) {
34703472 return ;
34713473 }
34723474 setTerminalFocusRequestId ( ( value ) => value + 1 ) ;
34733475
34743476 const launched = terminalActions . launchTerminal ( {
3477+ // The drawer opens with the target already placed: opening it empty would claim
3478+ // DEFAULT_THREAD_TERMINAL_ID, which a right-panel terminal may already own.
34753479 place : ( terminalId ) =>
3476- shouldCreateNewTerminal
3480+ reusableTerminalId === null
34773481 ? storeNewTerminal ( activeThreadRef , terminalId )
3478- : storeSetActiveTerminal ( activeThreadRef , terminalId ) ,
3479- ...( shouldCreateNewTerminal
3482+ : storeEnsureTerminal ( activeThreadRef , terminalId , { open : true } ) ,
3483+ ...( reusableTerminalId === null
34803484 ? { size : { cols : SCRIPT_TERMINAL_COLS , rows : SCRIPT_TERMINAL_ROWS } }
3481- : { terminalId : baseTerminalId } ) ,
3485+ : { terminalId : reusableTerminalId } ) ,
34823486 location : { cwd : targetCwd , worktreePath : targetWorktreePath } ,
34833487 ...( options ?. env ? { extraEnv : options . env } : { } ) ,
34843488 } ) ;
0 commit comments