@@ -12,6 +12,7 @@ import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
1212import * as EffectAcpErrors from "effect-acp/errors" ;
1313import type * as EffectAcpSchema from "effect-acp/compat" ;
1414import * as NodeBuffer from "node:buffer" ;
15+ import * as HostProcess from "@t3tools/shared/HostProcess" ;
1516
1617const MAX_LIVE_TERMINALS = 16 ;
1718const MAX_UNRELEASED_TERMINALS = 64 ;
@@ -137,22 +138,38 @@ function acpTerminalCommand(input: {
137138 readonly request : EffectAcpSchema . CreateTerminalRequest ;
138139 readonly shellCommands ?: boolean | undefined ;
139140 readonly defaultCwd : string ;
140- readonly environment ? : NodeJS . ProcessEnv | undefined ;
141+ readonly environment : NodeJS . ProcessEnv ;
141142 readonly forceKillAfter : Duration . Input ;
142143} ) {
143144 return ChildProcess . make ( input . request . command , input . request . args ?? [ ] , {
144145 cwd : input . request . cwd ?? input . defaultCwd ,
145- ... ( input . environment === undefined ? { } : { env : input . environment } ) ,
146+ env : input . environment ,
146147 shell : input . shellCommands === true && ( input . request . args ?. length ?? 0 ) === 0 ,
147148 forceKillAfter : input . forceKillAfter ,
148149 } ) ;
149150}
150151
152+ // Windows variable names are case-insensitive and Node passes only one spelling to the child,
153+ // so a later layer's `Path` must replace an earlier `PATH` rather than sit beside it.
154+ function mergeEnvironments (
155+ isWindows : boolean ,
156+ layers : ReadonlyArray < Iterable < readonly [ string , string | undefined ] > > ,
157+ ) {
158+ const variables = new Map < string , readonly [ string , string | undefined ] > ( ) ;
159+ for ( const layer of layers ) {
160+ for ( const [ name , value ] of layer ) {
161+ variables . set ( isWindows ? name . toUpperCase ( ) : name , [ name , value ] ) ;
162+ }
163+ }
164+ return Object . fromEntries ( variables . values ( ) ) ;
165+ }
166+
151167export const makeAcpClientTerminals = Effect . fn ( "makeAcpClientTerminals" ) ( function * (
152168 options : AcpClientTerminalsOptions ,
153169) : Effect . fn . Return < AcpClientTerminals , never , ChildProcessSpawner . ChildProcessSpawner > {
154170 const spawner = yield * ChildProcessSpawner . ChildProcessSpawner ;
155171 const forceKillAfter = options . forceKillAfter ?? "5 seconds" ;
172+ const isWindows = yield * HostProcess . isWindows ;
156173 const creationPermit = yield * Semaphore . make ( 1 ) ;
157174 const terminals = new Map < string , ManagedAcpTerminal > ( ) ;
158175 let nextTerminalNumber = 1 ;
@@ -215,16 +232,20 @@ export const makeAcpClientTerminals = Effect.fn("makeAcpClientTerminals")(functi
215232 `ACP terminal/create exceeded the limit of ${ MAX_LIVE_TERMINALS } concurrent terminals.` ,
216233 ) ;
217234 }
218- const requestEnvironment = Object . fromEntries (
219- ( request . env ?? [ ] ) . map ( ( variable ) => [ variable . name , variable . value ] as const ) ,
235+ // Thread credentials and identity must come from the requesting session.
236+ const inheritedEnvironment = Object . fromEntries (
237+ Object . entries ( options . environment ?? ( yield * HostProcess . Environment ) ) . filter (
238+ ( [ name ] ) =>
239+ ! name . toUpperCase ( ) . startsWith ( "T3_ACP_MCP_" ) &&
240+ name . toUpperCase ( ) !== "T3CODE_THREAD_ID" ,
241+ ) ,
220242 ) ;
221243 const sessionEnvironment = options . environmentForSession ?.( request . sessionId ) ;
222- const environment =
223- options . environment === undefined &&
224- sessionEnvironment === undefined &&
225- ( request . env ?. length ?? 0 ) === 0
226- ? undefined
227- : { ...options . environment , ...sessionEnvironment , ...requestEnvironment } ;
244+ const environment = mergeEnvironments ( isWindows , [
245+ Object . entries ( inheritedEnvironment ) ,
246+ Object . entries ( sessionEnvironment ?? { } ) ,
247+ ( request . env ?? [ ] ) . map ( ( variable ) => [ variable . name , variable . value ] as const ) ,
248+ ] ) ;
228249 const terminalScope = yield * Scope . make ( ) ;
229250 const child = yield * restore (
230251 spawner
0 commit comments