@@ -74,7 +74,7 @@ export class ProjectSetupScriptOperationError extends Schema.TaggedError<Project
7474 projectId : Schema . optional ( Schema . String ) ,
7575 projectCwd : Schema . optional ( Schema . String ) ,
7676 worktreePath : Schema . String ,
77- operation : Schema . Literals ( [ "resolveProject" , "readSettings" , "openTerminal" , "writeCommand" ] ) ,
77+ operation : Schema . Literals ( [ "resolveProject" , "readSettings" , "openTerminal" ] ) ,
7878 cause : Schema . Defect ( ) ,
7979 } ,
8080) {
@@ -187,6 +187,7 @@ export const make = Effect.gen(function* () {
187187 const terminalManager = yield * TerminalManager . TerminalManager ;
188188 const serverSettings = yield * ServerSettings . ServerSettingsService ;
189189 const crypto = yield * Crypto . Crypto ;
190+ const serviceScope = yield * Effect . scope ;
190191 const completionShell = resolveCompletionShell (
191192 yield * HostProcess . Platform ,
192193 yield * HostProcess . Environment ,
@@ -274,50 +275,66 @@ export const make = Effect.gen(function* () {
274275 Effect . tap ( ( ) => Deferred . await ( promptReturned ) . pipe ( Effect . timeoutOption ( "1 second" ) ) ) ,
275276 Effect . ensuring ( Effect . sync ( ( ) => unsubscribe ( ) ) ) ,
276277 ) ;
277- return { completion, unsubscribe } ;
278+ // A startup prompt waits for input on a line that has no newline yet.
279+ const flushPartialLine = Effect . suspend ( ( ) => {
280+ const partial = lineBuffer ;
281+ lineBuffer = "" ;
282+ return handleLine ( partial ) ;
283+ } ) ;
284+ const abandon = settle ( null ) . pipe (
285+ Effect . andThen ( Deferred . succeed ( promptReturned , undefined ) ) ,
286+ ) ;
287+ return { completion, unsubscribe, flushPartialLine, abandon } ;
278288 } ,
279289 ) ;
280290
281291 // Input typed during shell startup can be read by an rc-file prompt (fnm, nvm) instead of the
282- // shell, which loses the command. Wait for the line editor, or for a quiet shell without one.
283- const awaitShellReady = Effect . fn ( "ProjectSetupScriptRunner.awaitShellReady" ) ( function * ( input : {
284- readonly threadId : string ;
285- readonly terminalId : string ;
286- readonly history : string ;
287- } ) {
288- if ( lineEditorReading ( input . history , false ) ) return ;
289- const ready = yield * Deferred . make < void > ( ) ;
290- let reading = false ;
291- let carry = "" ;
292- let busy = false ;
293- let activity = 0 ;
294- const unsubscribe = yield * terminalManager . subscribe ( ( event ) => {
295- if ( event . threadId !== input . threadId || event . terminalId !== input . terminalId ) {
296- return Effect . void ;
297- }
298- activity += 1 ;
299- if ( event . type === "activity" ) busy = event . hasRunningSubprocess ;
300- if ( event . type === "output" ) {
301- const text = carry + event . data ;
302- carry = text . slice ( - ( BRACKETED_PASTE_ON . length - 1 ) ) ;
303- reading = lineEditorReading ( text , reading ) ;
304- }
305- const done = reading || event . type === "exited" || event . type === "closed" ;
306- return done ? Deferred . succeed ( ready , undefined ) . pipe ( Effect . asVoid ) : Effect . void ;
307- } ) ;
308- // A startup prompt runs as a child process, so a busy shell keeps waiting for the user.
309- const quiet = Effect . gen ( function * ( ) {
310- while ( true ) {
311- const seen = activity ;
312- yield * Effect . sleep ( SHELL_QUIET_FALLBACK ) ;
313- if ( activity === seen && ! busy ) return ;
314- }
315- } ) ;
316- yield * Deferred . await ( ready ) . pipe (
317- Effect . raceFirst ( quiet ) ,
318- Effect . ensuring ( Effect . sync ( ( ) => unsubscribe ( ) ) ) ,
319- ) ;
320- } ) ;
292+ // shell, which loses the command. Subscribe before the terminal opens so no activity is missed.
293+ const watchShellReadiness = Effect . fn ( "ProjectSetupScriptRunner.watchShellReadiness" ) (
294+ function * ( input : {
295+ readonly threadId : string ;
296+ readonly terminalId : string ;
297+ readonly onStartupPrompt : Effect . Effect < void > ;
298+ } ) {
299+ const outcome = yield * Deferred . make < "ready" | "exited" > ( ) ;
300+ let reading = false ;
301+ let carry = "" ;
302+ let busy = false ;
303+ let activity = 0 ;
304+ const unsubscribe = yield * terminalManager . subscribe ( ( event ) => {
305+ if ( event . threadId !== input . threadId || event . terminalId !== input . terminalId ) {
306+ return Effect . void ;
307+ }
308+ activity += 1 ;
309+ if ( event . type === "activity" ) busy = event . hasRunningSubprocess ;
310+ if ( event . type === "output" ) {
311+ const text = carry + event . data ;
312+ carry = text . slice ( - ( BRACKETED_PASTE_ON . length - 1 ) ) ;
313+ reading = lineEditorReading ( text , reading ) ;
314+ }
315+ if ( event . type === "exited" || event . type === "closed" ) {
316+ return Deferred . succeed ( outcome , "exited" ) . pipe ( Effect . asVoid ) ;
317+ }
318+ return reading ? Deferred . succeed ( outcome , "ready" ) . pipe ( Effect . asVoid ) : Effect . void ;
319+ } ) ;
320+ // A startup prompt runs as a child process, so a busy shell keeps waiting for the user.
321+ const quiet = Effect . gen ( function * ( ) {
322+ while ( true ) {
323+ const seen = activity ;
324+ yield * Effect . sleep ( SHELL_QUIET_FALLBACK ) ;
325+ if ( activity !== seen ) continue ;
326+ if ( ! busy ) return "ready" as const ;
327+ yield * input . onStartupPrompt ;
328+ }
329+ } ) ;
330+ const wait = ( history : string ) =>
331+ ( lineEditorReading ( history , false )
332+ ? Effect . succeed ( "ready" as const )
333+ : Deferred . await ( outcome ) . pipe ( Effect . raceFirst ( quiet ) )
334+ ) . pipe ( Effect . ensuring ( Effect . sync ( ( ) => unsubscribe ( ) ) ) ) ;
335+ return { wait, unsubscribe } ;
336+ } ,
337+ ) ;
321338
322339 const runForThread : ProjectSetupScriptRunner [ "Service" ] [ "runForThread" ] = Effect . fn (
323340 "ProjectSetupScriptRunner.runForThread" ,
@@ -413,6 +430,28 @@ export const make = Effect.gen(function* () {
413430 )
414431 : script . command ;
415432
433+ // Subscribe before opening so neither startup activity nor the sentinel can race the listeners.
434+ const observed =
435+ observe && completionToken
436+ ? yield * observeTerminalCompletion ( {
437+ threadId : input . threadId ,
438+ terminalId,
439+ sentinel : completionSentinel ( completionToken ) ,
440+ sentinelPattern : completionSentinelPattern ( completionToken ) ,
441+ echoedWrapperLines : commandLine . split ( "\r" ) . filter ( ( line ) => line . length > 0 ) ,
442+ onOutputLine : observe . onOutputLine ,
443+ } )
444+ : undefined ;
445+ // PowerShell has no line-editor signal; keep its immediate write.
446+ const readiness =
447+ completionShell === "powershell"
448+ ? undefined
449+ : yield * watchShellReadiness ( {
450+ threadId : input . threadId ,
451+ terminalId,
452+ onStartupPrompt : observed ?. flushPartialLine ?? Effect . void ,
453+ } ) ;
454+
416455 const terminal = yield * terminalManager
417456 . open ( {
418457 threadId : input . threadId ,
@@ -430,42 +469,33 @@ export const make = Effect.gen(function* () {
430469 cause,
431470 } ) ,
432471 ) ,
472+ Effect . tapError ( ( ) =>
473+ Effect . sync ( ( ) => {
474+ observed ?. unsubscribe ( ) ;
475+ readiness ?. unsubscribe ( ) ;
476+ } ) ,
477+ ) ,
433478 ) ;
434- // Subscribe before writing so the sentinel cannot race past the listener.
435- const observed =
436- observe && completionToken
437- ? yield * observeTerminalCompletion ( {
438- threadId : input . threadId ,
439- terminalId,
440- sentinel : completionSentinel ( completionToken ) ,
441- sentinelPattern : completionSentinelPattern ( completionToken ) ,
442- echoedWrapperLines : commandLine . split ( "\r" ) . filter ( ( line ) => line . length > 0 ) ,
443- onOutputLine : observe . onOutputLine ,
444- } )
445- : undefined ;
446479
447- // PowerShell has no line-editor signal; keep its immediate write.
448- yield * (
449- completionShell === "powershell"
450- ? Effect . void
451- : awaitShellReady ( { threadId : input . threadId , terminalId, history : terminal . history } )
452- ) . pipe (
453- Effect . andThen (
454- terminalManager . write ( {
455- threadId : input . threadId ,
456- terminalId,
457- data : projectScriptTerminalInput ( commandLine ) ,
458- } ) ,
480+ // Return once the terminal exists so callers can show it while a startup prompt waits.
481+ yield * ( readiness ?. wait ( terminal . history ) ?? Effect . succeed ( "ready" as const ) ) . pipe (
482+ Effect . flatMap ( ( shell ) =>
483+ shell === "exited"
484+ ? Effect . void
485+ : terminalManager . write ( {
486+ threadId : input . threadId ,
487+ terminalId,
488+ data : projectScriptTerminalInput ( commandLine ) ,
489+ } ) ,
459490 ) ,
460- Effect . mapError (
461- ( cause ) =>
462- new ProjectSetupScriptOperationError ( {
463- ...errorContext ,
464- operation : "writeCommand" ,
465- cause,
466- } ) ,
491+ Effect . catchCause ( ( cause ) =>
492+ Effect . logWarning ( "project script command was not written" , {
493+ ...errorContext ,
494+ terminalId,
495+ cause,
496+ } ) . pipe ( Effect . andThen ( observed ?. abandon ?? Effect . void ) ) ,
467497 ) ,
468- Effect . onError ( ( ) => Effect . sync ( ( ) => observed ?. unsubscribe ( ) ) ) ,
498+ Effect . forkIn ( serviceScope ) ,
469499 ) ;
470500
471501 const completion = observed ?. completion . pipe (
0 commit comments