claude-code - 💡(How to fix) Fix Session environment cache not invalidated on /resume, causing stale env vars [2 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#56400Fetched 2026-05-06 06:29:05
View on GitHub
Comments
2
Participants
2
Timeline
6
Reactions
0
Timeline (top)
labeled ×4commented ×2

/resume calls switchSession() but does not call invalidateSessionEnvCache(), so env vars exported via CLAUDE_ENV_FILE in SessionStart hooks remain stale after switching sessions.

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.

Code Example

{
  "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"
      }]
    }]
  }
}
RAW_BUFFERClick to expand / collapse

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).

extent analysis

TL;DR

Call invalidateSessionEnvCache() in switchSession() to reset the cached env script after switching sessions.

Guidance

  • Verify the issue by checking the sessionEnvironment.ts file and confirming that the invalidateSessionEnvCache() function is not called after switchSession().
  • To fix the issue, modify the switchSession() function in bootstrap/state.ts:468 to call invalidateSessionEnvCache() after switching sessions.
  • Test the fix by running the repro steps and verifying that the CLAUDE_SESSION environment variable is updated correctly after switching sessions.
  • Consider adding a test case to ensure that the invalidateSessionEnvCache() function is called correctly after switching sessions.

Example

// bootstrap/state.ts:468
function switchSession(newSessionId: string) {
  // ... existing code ...
  invalidateSessionEnvCache(); // Add this line to reset the cached env script
}

Notes

This fix assumes that the invalidateSessionEnvCache() function is correctly implemented and resets the cached env script as expected. If the issue persists after applying this fix, further debugging may be necessary.

Recommendation

Apply the workaround by calling invalidateSessionEnvCache() in switchSession() to ensure that the cached env script is reset after switching sessions. This fix should resolve the issue and prevent stale environment variables from being used after switching sessions.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

claude-code - 💡(How to fix) Fix Session environment cache not invalidated on /resume, causing stale env vars [2 comments, 2 participants]