()
| 79 | } |
| 80 | |
| 81 | export async function getSessionEnvironmentScript(): Promise<string | null> { |
| 82 | if (getPlatform() === 'windows') { |
| 83 | logForDebugging('Session environment not yet supported on Windows') |
| 84 | return null |
| 85 | } |
| 86 | |
| 87 | if (sessionEnvScript !== undefined) { |
| 88 | return sessionEnvScript |
| 89 | } |
| 90 | |
| 91 | const scripts: string[] = [] |
| 92 | |
| 93 | // Check for CLAUDE_ENV_FILE passed from parent process (e.g., HFI trajectory runner) |
| 94 | // This allows venv/conda activation to persist across shell commands |
| 95 | const envFile = process.env.CLAUDE_ENV_FILE |
| 96 | if (envFile) { |
| 97 | try { |
| 98 | const envScript = (await readFile(envFile, 'utf8')).trim() |
| 99 | if (envScript) { |
| 100 | scripts.push(envScript) |
| 101 | logForDebugging( |
| 102 | `Session environment loaded from CLAUDE_ENV_FILE: ${envFile} (${envScript.length} chars)`, |
| 103 | ) |
| 104 | } |
| 105 | } catch (e: unknown) { |
| 106 | const code = getErrnoCode(e) |
| 107 | if (code !== 'ENOENT') { |
| 108 | logForDebugging(`Failed to read CLAUDE_ENV_FILE: ${errorMessage(e)}`) |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | // Load hook environment files from session directory |
| 114 | const sessionEnvDir = await getSessionEnvDirPath() |
| 115 | if (!sessionEnvDir) { |
| 116 | logForDebugging('Session environment unavailable; skipping hook env load') |
| 117 | sessionEnvScript = null |
| 118 | return sessionEnvScript |
| 119 | } |
| 120 | try { |
| 121 | const files = await readdir(sessionEnvDir) |
| 122 | // We are sorting the hook env files by the order in which they are listed |
| 123 | // in the settings.json file so that the resulting env is deterministic |
| 124 | const hookFiles = files |
| 125 | .filter(f => HOOK_ENV_REGEX.test(f)) |
| 126 | .sort(sortHookEnvFiles) |
| 127 | |
| 128 | for (const file of hookFiles) { |
| 129 | const filePath = join(sessionEnvDir, file) |
| 130 | try { |
| 131 | const content = (await readFile(filePath, 'utf8')).trim() |
| 132 | if (content) { |
| 133 | scripts.push(content) |
| 134 | } |
| 135 | } catch (e: unknown) { |
| 136 | const code = getErrnoCode(e) |
| 137 | if (code !== 'ENOENT') { |
| 138 | logForDebugging( |
no test coverage detected