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:

  1. Start claude, run ! echo $CLAUDE_SESSION — prints current session ID
  2. /resume into a different session
  3. ! 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).

View original on GitHub ↗

3 Comments

github-actions[bot] · 5 months ago

Found 2 possible duplicate issues:

  1. https://github.com/anthropics/claude-code/issues/40391
  2. https://github.com/anthropics/claude-code/issues/51862

This issue will be automatically closed as a duplicate in 3 days.

  • If your issue is a duplicate, please close it and 👍 the existing issue instead
  • To prevent auto-closure, add a comment or 👎 this comment

🤖 Generated with Claude Code

mcmanustfj · 5 months ago

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.ts not being invalidated when switchSession() runs. Even if #40391 were fixed and env files landed in the correct directory, the cached env script would still serve stale values after /resume because invalidateSessionEnvCache() is never called in that code path.

#51862 is about CLAUDE_ENV_FILE not 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).

github-actions[bot] · 3 months ago

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.