11import * as Context from "effect/Context" ;
2+ import * as Clock from "effect/Clock" ;
23import * as Duration from "effect/Duration" ;
34import * as Effect from "effect/Effect" ;
45import * as FileSystem from "effect/FileSystem" ;
@@ -16,6 +17,10 @@ interface ShellEnvironmentConfig {
1617 readonly env : NodeJS . ProcessEnv ;
1718 readonly platform : NodeJS . Platform ;
1819 readonly userShell : Option . Option < string > ;
20+ readonly windowsEnvironmentCache ?: {
21+ readonly path : string ;
22+ readonly directory : string ;
23+ } ;
1924}
2025
2126interface WindowsProbeOptions {
@@ -93,6 +98,20 @@ const WINDOWS_SHELL_CANDIDATES = ["pwsh.exe", "powershell.exe"] as const;
9398const LOGIN_SHELL_TIMEOUT = Duration . seconds ( 5 ) ;
9499const LAUNCHCTL_TIMEOUT = Duration . seconds ( 2 ) ;
95100const PROCESS_TERMINATE_GRACE = Duration . seconds ( 1 ) ;
101+ const WINDOWS_ENVIRONMENT_CACHE_MAX_AGE_MS = Duration . toMillis ( Duration . hours ( 24 ) ) ;
102+ const WINDOWS_ENVIRONMENT_CACHE_FILE_NAME = "windows-shell-environment.json" ;
103+
104+ const WindowsEnvironmentCache = Schema . Struct ( {
105+ version : Schema . Literal ( 1 ) ,
106+ capturedAt : Schema . Number ,
107+ inheritedPath : Schema . String ,
108+ noProfile : Schema . Record ( Schema . String , Schema . String ) ,
109+ profile : Schema . Record ( Schema . String , Schema . String ) ,
110+ } ) ;
111+ type WindowsEnvironmentCache = typeof WindowsEnvironmentCache . Type ;
112+ const WindowsEnvironmentCacheJson = Schema . fromJsonString ( WindowsEnvironmentCache ) ;
113+ const decodeWindowsEnvironmentCache = Schema . decodeUnknownOption ( WindowsEnvironmentCacheJson ) ;
114+ const encodeWindowsEnvironmentCache = Schema . encodeEffect ( WindowsEnvironmentCacheJson ) ;
96115
97116const trimNonEmpty = ( value : string | null | undefined ) : Option . Option < string > =>
98117 Option . fromNullishOr ( value ) . pipe (
@@ -383,19 +402,65 @@ const readWindowsEnvironment = Effect.fn("desktop.shellEnvironment.readWindowsEn
383402const installWindowsEnvironment = Effect . fn ( "desktop.shellEnvironment.installWindowsEnvironment" ) (
384403 function * (
385404 config : ShellEnvironmentConfig ,
386- ) : Effect . fn . Return < void , never , ChildProcessSpawner . ChildProcessSpawner > {
387- // Concurrent, not sequential: these two probes are independent (only their
388- // results are combined below) and each spawns its own PowerShell. Run in
389- // series they sit at offset 0 of desktop.startup, before anything else, and
390- // launch traces measured them at 2718ms then 2066ms — the entire 4.8s
391- // startup span, of which desktop.bootstrap is ~30ms.
392- const [ noProfile , profile ] = yield * Effect . all (
393- [
394- readWindowsEnvironment ( [ "PATH" ] , { loadProfile : false } ) ,
395- readWindowsEnvironment ( WINDOWS_PROFILE_ENV_NAMES , { loadProfile : true } ) ,
396- ] ,
397- { concurrency : 2 } ,
398- ) ;
405+ ) : Effect . fn . Return <
406+ void ,
407+ never ,
408+ ChildProcessSpawner . ChildProcessSpawner | FileSystem . FileSystem
409+ > {
410+ const fileSystem = yield * FileSystem . FileSystem ;
411+ const inheritedPath = Option . getOrElse ( readEnvPath ( config . env ) , ( ) => "" ) ;
412+ const cache = config . windowsEnvironmentCache ?? null ;
413+ const cachePath = cache ?. path ?? null ;
414+ const now = yield * Clock . currentTimeMillis ;
415+ // Loading a PowerShell profile dominates warm Windows launches. Reuse its
416+ // small environment snapshot while the inherited PATH is unchanged, but
417+ // bound its lifetime so profile-only changes are eventually discovered.
418+ // A missing, corrupt, or stale cache simply falls through to the probes.
419+ const cached : Option . Option < WindowsEnvironmentCache > =
420+ cachePath === null
421+ ? Option . none < WindowsEnvironmentCache > ( )
422+ : yield * fileSystem . readFileString ( cachePath ) . pipe (
423+ Effect . map ( decodeWindowsEnvironmentCache ) ,
424+ Effect . orElseSucceed ( ( ) => Option . none ( ) ) ,
425+ Effect . map ( ( entry ) =>
426+ Option . filter (
427+ entry ,
428+ ( value ) =>
429+ value . inheritedPath === inheritedPath &&
430+ now >= value . capturedAt &&
431+ now - value . capturedAt <= WINDOWS_ENVIRONMENT_CACHE_MAX_AGE_MS ,
432+ ) ,
433+ ) ,
434+ ) ;
435+
436+ const [ noProfile , profile ] = Option . isSome ( cached )
437+ ? [ cached . value . noProfile , cached . value . profile ]
438+ : yield * Effect . all (
439+ [
440+ readWindowsEnvironment ( [ "PATH" ] , { loadProfile : false } ) ,
441+ readWindowsEnvironment ( WINDOWS_PROFILE_ENV_NAMES , { loadProfile : true } ) ,
442+ ] ,
443+ { concurrency : 2 } ,
444+ ) ;
445+
446+ const probesCompleted =
447+ Option . isSome ( trimNonEmpty ( noProfile . PATH ) ) && Option . isSome ( trimNonEmpty ( profile . PATH ) ) ;
448+
449+ if ( Option . isNone ( cached ) && cache !== null && probesCompleted ) {
450+ const encoded = yield * encodeWindowsEnvironmentCache ( {
451+ version : 1 ,
452+ capturedAt : now ,
453+ inheritedPath,
454+ noProfile,
455+ profile,
456+ } ) . pipe ( Effect . option ) ;
457+ if ( Option . isSome ( encoded ) ) {
458+ yield * fileSystem . makeDirectory ( cache . directory , { recursive : true } ) . pipe (
459+ Effect . andThen ( fileSystem . writeFileString ( cache . path , `${ encoded . value } \n` ) ) ,
460+ Effect . catchCause ( ( ) => Effect . void ) ,
461+ ) ;
462+ }
463+ }
399464 const mergedPath = mergePaths ( "win32" , [
400465 trimNonEmpty ( profile . PATH ) ,
401466 trimNonEmpty ( knownWindowsCliDirs ( config . env ) . join ( ";" ) ) ,
@@ -539,6 +604,17 @@ export const make = Effect.gen(function* () {
539604 env : process . env ,
540605 platform : environment . platform ,
541606 userShell : Option . none ( ) ,
607+ ...( typeof environment . stateDir === "string" && environment . stateDir . length > 0
608+ ? {
609+ windowsEnvironmentCache : {
610+ path : environment . path . join (
611+ environment . stateDir ,
612+ WINDOWS_ENVIRONMENT_CACHE_FILE_NAME ,
613+ ) ,
614+ directory : environment . stateDir ,
615+ } ,
616+ }
617+ : { } ) ,
542618 } ) . pipe (
543619 Effect . provideService ( FileSystem . FileSystem , fileSystem ) ,
544620 Effect . provideService ( ChildProcessSpawner . ChildProcessSpawner , spawner ) ,
0 commit comments