Session environment cache not invalidated on /resume, causing stale env vars
Status Fixed / completed
Reported on v2.1.128
Maintainer reply None cached
Activity 3 comments · opened May 5, 2026 · closed May 7, 2026
Description
/resume calls switchSession() but does not call invalidateSessionEnvCache(), so env vars exported via CLAUDE_ENV_FILE in SessionStart hooks remain stale after switching sessions.
Environment Info
- Platform: linux
- Terminal: xterm-256color
- Version: 2.1.128
Repro
SessionStart hook (~/.claude/settings.json):
{
"hooks": {
"SessionStart": [{
"hooks": [{
"type": "command",
"command": "INPUT=$(cat); SID=$(echo \"$INPUT\" | jq -r '.session_id'); if [ -n \"$CLAUDE_ENV_FILE\" ]; then echo \"export CLAUDE_SESSION=$SID\" >> \"$CLAUDE_ENV_FILE\"; fi"
}]
}]
}
}
Steps:
- Start
claude, run! echo $CLAUDE_SESSION— prints current session ID /resumeinto a different session! echo $CLAUDE_SESSION— still prints the first session's ID
Root cause
sessionEnvironment.ts caches the sourced env script in a module-level variable. invalidateSessionEnvCache() resets it, but is only called after SessionStart/Setup/CwdChanged hooks — not after switchSession(). So /resume changes the session ID but the cached env script still has the old session's values.
Suggested fix
Call invalidateSessionEnvCache() in switchSession() (bootstrap/state.ts:468).
3 Comments
Found 2 possible duplicate issues:
This issue will be automatically closed as a duplicate in 3 days.
🤖 Generated with Claude Code
This is not a duplicate of either linked issue.
#40391 is about a session ID directory mismatch — hooks write env files to the startup session's directory, but the loader reads from the resumed session's directory. That's a file-path routing problem.
This issue (#56400) is about the in-memory cache in
sessionEnvironment.tsnot being invalidated whenswitchSession()runs. Even if #40391 were fixed and env files landed in the correct directory, the cached env script would still serve stale values after/resumebecauseinvalidateSessionEnvCache()is never called in that code path.#51862 is about
CLAUDE_ENV_FILEnot being auto-sourced by the Bash tool at all on macOS — a different symptom, platform, and root cause.These are three distinct bugs in the env-file lifecycle: wrong directory (#40391), stale cache (this issue), and missing sourcing (#51862).
This issue has been automatically locked since it was closed and has not had any activity for 7 days. If you're experiencing a similar issue, please file a new issue and reference this one if it's relevant.