Session environment cache not invalidated on /resume, causing stale env vars

Resolved 💬 3 comments Opened May 5, 2026 by mcmanustfj 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 ↗

This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗