@@ -278,6 +278,7 @@ interface TerminalStartInput {
278278 readonly worktreePath ?: string | null ;
279279 readonly cols : number ;
280280 readonly rows : number ;
281+ readonly initialCommand ?: string ;
281282}
282283
283284interface TerminalSessionState {
@@ -405,6 +406,16 @@ function snapshot(session: TerminalSessionState): TerminalSessionSnapshot {
405406 } ;
406407}
407408
409+ function openSnapshot (
410+ session : TerminalSessionState ,
411+ initialCommandHandled : boolean ,
412+ ) : TerminalSessionSnapshot {
413+ const current = snapshot ( session ) ;
414+ return initialCommandHandled && session . status === "running"
415+ ? { ...current , initialCommandHandled : true }
416+ : current ;
417+ }
418+
408419function summary ( session : TerminalSessionState ) : TerminalSummary {
409420 return {
410421 threadId : session . threadId ,
@@ -596,6 +607,33 @@ function formatShellCandidate(candidate: ShellCandidate): string {
596607 return `${ candidate . shell } ${ candidate . args . join ( " " ) } ` ;
597608}
598609
610+ function quotePosixShellArgument ( value : string ) : string {
611+ return `'${ value . replaceAll ( "'" , `'\\''` ) } '` ;
612+ }
613+
614+ function posixShellCandidateWithInitialCommand (
615+ candidate : ShellCandidate ,
616+ initialCommand : string ,
617+ ) : ShellCandidate {
618+ const baseArgs = candidate . args ?? [ ] ;
619+ const reenterShell = [ candidate . shell , ...baseArgs ] . map ( quotePosixShellArgument ) . join ( " " ) ;
620+ return {
621+ shell : candidate . shell ,
622+ args : [ ...baseArgs , "-i" , "-c" , `trap ':' INT\n${ initialCommand } \nexec ${ reenterShell } ` ] ,
623+ } ;
624+ }
625+
626+ function supportsPosixInitialCommand (
627+ candidates : ReadonlyArray < ShellCandidate > ,
628+ platform : NodeJS . Platform ,
629+ ) : boolean {
630+ if ( platform === "win32" ) return false ;
631+ return candidates . every ( ( candidate ) => {
632+ const shellName = basenameForPlatform ( candidate . shell , platform ) . toLowerCase ( ) ;
633+ return shellName === "zsh" || shellName === "bash" || shellName === "sh" ;
634+ } ) ;
635+ }
636+
599637function uniqueShellCandidates ( candidates : Array < ShellCandidate | null > ) : ShellCandidate [ ] {
600638 const seen = new Set < string > ( ) ;
601639 const ordered : ShellCandidate [ ] = [ ] ;
@@ -2438,12 +2476,28 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
24382476
24392477 let ptyProcess : PtyAdapter . PtyProcess | null = null ;
24402478 let startedShell : string | null = null ;
2479+ let initialCommandHandled = false ;
24412480
24422481 const startResult = yield * Effect . result (
24432482 increment ( terminalSessionsTotal , { lifecycle : eventType } ) . pipe (
24442483 Effect . andThen (
24452484 Effect . gen ( function * ( ) {
2446- const shellCandidates = resolveShellCandidates ( shellResolver , platform , baseEnv ) ;
2485+ const resolvedShellCandidates = resolveShellCandidates (
2486+ shellResolver ,
2487+ platform ,
2488+ baseEnv ,
2489+ ) ;
2490+ const initialCommand = input . initialCommand ;
2491+ let shellCandidates = resolvedShellCandidates ;
2492+ if (
2493+ initialCommand !== undefined &&
2494+ supportsPosixInitialCommand ( resolvedShellCandidates , platform )
2495+ ) {
2496+ initialCommandHandled = true ;
2497+ shellCandidates = resolvedShellCandidates . map ( ( candidate ) =>
2498+ posixShellCandidateWithInitialCommand ( candidate , initialCommand ) ,
2499+ ) ;
2500+ }
24472501 const terminalEnv = createTerminalSpawnEnv (
24482502 baseEnv ,
24492503 session . runtimeEnv ,
@@ -2531,7 +2585,7 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
25312585 ) ;
25322586
25332587 if ( startResult . _tag === "Success" ) {
2534- return ;
2588+ return initialCommandHandled ;
25352589 }
25362590
25372591 {
@@ -2574,6 +2628,7 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
25742628 cause : error ,
25752629 ...( startedShell ? { shell : startedShell } : { } ) ,
25762630 } ) ;
2631+ return false ;
25772632 }
25782633 } ) ;
25792634
@@ -2890,17 +2945,18 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
28902945 } ) ;
28912946
28922947 yield * evictInactiveSessionsIfNeeded ( ) ;
2893- yield * startSession (
2948+ const initialCommandHandled = yield * startSession (
28942949 session ,
28952950 {
28962951 cwd : input . cwd ,
28972952 ...( input . worktreePath !== undefined ? { worktreePath : input . worktreePath } : { } ) ,
28982953 cols,
28992954 rows,
2955+ ...( input . initialCommand ? { initialCommand : input . initialCommand } : { } ) ,
29002956 } ,
29012957 "started" ,
29022958 ) ;
2903- return snapshot ( session ) ;
2959+ return openSnapshot ( session , initialCommandHandled ) ;
29042960 }
29052961
29062962 const liveSession = existing . value ;
@@ -2940,17 +2996,18 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
29402996 }
29412997
29422998 if ( ! liveSession . process ) {
2943- yield * startSession (
2999+ const initialCommandHandled = yield * startSession (
29443000 liveSession ,
29453001 {
29463002 cwd : input . cwd ,
29473003 worktreePath : liveSession . worktreePath ,
29483004 cols : targetCols ,
29493005 rows : targetRows ,
3006+ ...( input . initialCommand ? { initialCommand : input . initialCommand } : { } ) ,
29503007 } ,
29513008 "started" ,
29523009 ) ;
2953- return snapshot ( liveSession ) ;
3010+ return openSnapshot ( liveSession , initialCommandHandled ) ;
29543011 }
29553012
29563013 if ( liveSession . cols !== targetCols || liveSession . rows !== targetRows ) {
0 commit comments