@@ -320,8 +320,8 @@ interface TerminalSessionState {
320320 childCommandLabel : string | null ;
321321 runtimeEnv : Record < string , string > | null ;
322322 stopOnSettle : boolean ;
323- /** Input waiting for the current shell to read; null once that shell takes writes directly. */
324- pendingInitialInputs : Array < string > | null ;
323+ /** Writes waiting for the current shell to read input ; null once it takes them directly. */
324+ heldWrites : Array < string > | null ;
325325}
326326
327327interface TerminalGrid {
@@ -2446,7 +2446,7 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
24462446 session . hasRunningSubprocess = false ;
24472447 session . childCommandLabel = null ;
24482448 session . status = "exited" ;
2449- session . pendingInitialInputs = null ;
2449+ session . heldWrites = null ;
24502450 session . pendingHistoryControlSequence = "" ;
24512451 session . pendingProcessEvents = [ ] ;
24522452 session . pendingProcessEventIndex = 0 ;
@@ -2544,7 +2544,7 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
25442544 yield * modifyManagerState ( ( state ) => {
25452545 session . status = "starting" ;
25462546 session . shell = null ;
2547- session . pendingInitialInputs = null ;
2547+ session . heldWrites = null ;
25482548 session . cwd = input . cwd ;
25492549 session . worktreePath = input . worktreePath ?? null ;
25502550 session . cols = input . cols ;
@@ -2954,16 +2954,11 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
29542954 ) ;
29552955
29562956 // Input typed while a shell starts can be read by an rc-file prompt (fnm, nvm) instead of the
2957- // shell, so it waits for the line editor. The queue keeps later opens behind it, in order .
2957+ // shell. Writes that ask to wait are held, in order, until the line editor reads input .
29582958 const startOpenedSession = Effect . fn ( "terminal.startOpenedSession" ) ( function * (
29592959 session : TerminalSessionState ,
29602960 startInput : TerminalStartInput ,
2961- initialInput : string | undefined ,
29622961 ) {
2963- if ( initialInput === undefined ) {
2964- yield * startSession ( session , startInput , "started" ) ;
2965- return snapshot ( session ) ;
2966- }
29672962 const target = { threadId : session . threadId , terminalId : session . terminalId } ;
29682963 // Subscribed before the spawn so no startup output is missed.
29692964 const readiness = yield * watchShellReadiness ( {
@@ -2975,33 +2970,33 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
29752970 Effect . onError ( ( ) => Effect . sync ( readiness . unsubscribe ) ) ,
29762971 ) ;
29772972 const spawned = session . process ;
2978- if ( session . status !== "running" || spawned === null || session . shell === null ) {
2973+ if (
2974+ session . status !== "running" ||
2975+ spawned === null ||
2976+ session . shell === null ||
2977+ ! shellSignalsLineEditor ( session . shell )
2978+ ) {
29792979 readiness . unsubscribe ( ) ;
29802980 return snapshot ( session ) ;
29812981 }
2982- if ( ! shellSignalsLineEditor ( session . shell ) ) {
2983- readiness . unsubscribe ( ) ;
2984- yield * write ( { ...target , data : initialInput } ) ;
2985- return { ...snapshot ( session ) , initialInputHandled : true } ;
2986- }
2987- const queued = [ initialInput ] ;
2988- session . pendingInitialInputs = queued ;
2982+ const held : Array < string > = [ ] ;
2983+ session . heldWrites = held ;
29892984 yield * readiness . wait . pipe (
29902985 Effect . flatMap ( ( outcome ) => {
2991- // A restart or exit already dropped the queue: the input belonged to the shell it was opened for.
2992- if ( session . pendingInitialInputs !== queued ) return Effect . void ;
2993- session . pendingInitialInputs = null ;
2986+ // A restart or exit already dropped the queue: the input belonged to the shell it was typed for.
2987+ if ( session . heldWrites !== held ) return Effect . void ;
2988+ session . heldWrites = null ;
29942989 return outcome === "exited" || session . process !== spawned
29952990 ? Effect . void
2996- : Effect . forEach ( queued , ( data ) => write ( { ...target , data } ) , { discard : true } ) ;
2991+ : Effect . forEach ( held , ( data ) => write ( { ...target , data } ) , { discard : true } ) ;
29972992 } ) ,
29982993 Effect . catchCause ( ( cause ) =>
2999- Effect . logWarning ( "terminal initial input was not written" , { ...target , cause } ) ,
2994+ Effect . logWarning ( "terminal held input was not written" , { ...target , cause } ) ,
30002995 ) ,
30012996 Effect . ensuring ( Effect . sync ( readiness . unsubscribe ) ) ,
30022997 Effect . forkIn ( workerScope ) ,
30032998 ) ;
3004- return { ... snapshot ( session ) , initialInputHandled : true } ;
2999+ return snapshot ( session ) ;
30053000 } ) ;
30063001
30073002 const openWithWorkspaceLease = Effect . fn ( "terminal.openLocked" ) ( function * (
@@ -3054,7 +3049,7 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
30543049 childCommandLabel : null ,
30553050 runtimeEnv : normalizedRuntimeEnv ( input . env ) ,
30563051 stopOnSettle : input . stopOnSettle ?? false ,
3057- pendingInitialInputs : null ,
3052+ heldWrites : null ,
30583053 } ;
30593054
30603055 const createdSession = session ;
@@ -3066,16 +3061,12 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
30663061 } ) ;
30673062
30683063 yield * evictInactiveSessionsIfNeeded ( ) ;
3069- return yield * startOpenedSession (
3070- session ,
3071- {
3072- cwd : input . cwd ,
3073- ...( input . worktreePath !== undefined ? { worktreePath : input . worktreePath } : { } ) ,
3074- cols,
3075- rows,
3076- } ,
3077- input . initialInput ,
3078- ) ;
3064+ return yield * startOpenedSession ( session , {
3065+ cwd : input . cwd ,
3066+ ...( input . worktreePath !== undefined ? { worktreePath : input . worktreePath } : { } ) ,
3067+ cols,
3068+ rows,
3069+ } ) ;
30793070 }
30803071
30813072 const liveSession = existing . value ;
@@ -3115,34 +3106,19 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
31153106 }
31163107
31173108 if ( ! liveSession . process ) {
3118- return yield * startOpenedSession (
3119- liveSession ,
3120- {
3121- cwd : input . cwd ,
3122- worktreePath : liveSession . worktreePath ,
3123- cols : targetCols ,
3124- rows : targetRows ,
3125- } ,
3126- input . initialInput ,
3127- ) ;
3109+ return yield * startOpenedSession ( liveSession , {
3110+ cwd : input . cwd ,
3111+ worktreePath : liveSession . worktreePath ,
3112+ cols : targetCols ,
3113+ rows : targetRows ,
3114+ } ) ;
31283115 }
31293116
31303117 if ( liveSession . cols !== targetCols || liveSession . rows !== targetRows ) {
31313118 yield * resizeSession ( liveSession , liveSession . process , targetCols , targetRows ) ;
31323119 }
31333120
3134- if ( input . initialInput === undefined ) return snapshot ( liveSession ) ;
3135- // Behind input still waiting for this shell, so a second action cannot overtake the first.
3136- if ( liveSession . pendingInitialInputs !== null ) {
3137- liveSession . pendingInitialInputs . push ( input . initialInput ) ;
3138- } else {
3139- yield * write ( {
3140- threadId : liveSession . threadId ,
3141- terminalId : liveSession . terminalId ,
3142- data : input . initialInput ,
3143- } ) ;
3144- }
3145- return { ...snapshot ( liveSession ) , initialInputHandled : true } ;
3121+ return snapshot ( liveSession ) ;
31463122 } ) ;
31473123
31483124 const openLocked = ( input : TerminalOpenInput ) =>
@@ -3538,6 +3514,10 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
35383514 terminalId,
35393515 } ) ;
35403516 }
3517+ if ( input . afterShellStartup === true && session . heldWrites !== null ) {
3518+ session . heldWrites . push ( input . data ) ;
3519+ return ;
3520+ }
35413521 session . inputCount += 1 ;
35423522 yield * Effect . try ( {
35433523 try : ( ) => process . write ( input . data ) ,
0 commit comments