Expose CLAUDE_SESSION_ID env var to hooks
Problem
Hooks have no way to identify which Claude Code session invoked them. Each hook invocation is a fresh process with no shared state across invocations within the same session. The only session-related env vars available are CLAUDE_PROJECT_DIR and CLAUDECODE=1, neither of which distinguishes concurrent sessions.
Use Case
We use a plan lifecycle system where plans move through plans/ → plans/processing/ → plans/completed/. A Stop hook checks that any plans in processing/ touched by the current session are complete before allowing the session to end.
The problem: with multiple concurrent sessions, there's no reliable way to determine which session owns which plan. We currently infer ownership via git diff, but this breaks when one session moves another's plan file (e.g., recovering a misplaced plan), causing the stop hook to incorrectly flag plans belonging to other sessions.
Writing a session marker into plan files was considered, but without a stable session identifier exposed to hooks, there's no value to match against — and a file-based workaround (e.g., .sessions/<uuid>) breaks with concurrent sessions overwriting each other.
Proposed Solution
Expose a CLAUDE_SESSION_ID environment variable (or similar) to all hook processes. This should be:
- Unique per session — each
claudeCLI invocation gets a distinct ID - Stable across hook invocations — the same ID for all hooks fired during one session
- Available in all hook types — PreToolUse, PostToolUse, Stop, etc.
This would enable hooks to reliably track session ownership of files, plans, or other resources without relying on fragile git-state heuristics.
Alternatives Considered
- Git diff heuristics — current approach; breaks when sessions move each other's files
- File-based session markers (
.claude/.sessions/<uuid>) — no stable ID to generate from; concurrent sessions can't coordinate - Conversation transcript path as proxy — not exposed to hooks via env vars
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗