Pass session_id to all hook types (UserPromptSubmit, PostCompact, etc.)

Resolved 💬 7 comments Opened Mar 22, 2026 by ksander27b Closed May 1, 2026

Feature Request

Currently, only some hook types (e.g., PreCompact) receive session_id via stdin JSON. Other hooks like UserPromptSubmit and PostCompact do not receive session identification.

Request: Pass session_id to all hook types, either via stdin JSON (consistent with PreCompact) or as an environment variable (e.g., CLAUDE_SESSION_ID). An env var would be simplest since it works for both command hooks and doesn't require stdin parsing.

Use Case

I've built a reasoning preservation system using hooks:

  1. PreCompact hook extracts thinking blocks from the session .jsonl transcript and writes them to a sidecar file ({session_id}-reasoning.pending)
  2. UserPromptSubmit hook detects the .pending file and injects the recovered reasoning into context after compaction

The problem: when multiple Claude Code sessions run in parallel (I often have 4-5 across different repos), the UserPromptSubmit hook can't identify which session it belongs to. It picks up .pending files from other sessions, injecting irrelevant reasoning context.

Current Workaround

The PreCompact hook writes a breadcrumb file (last-compacted-session.txt) containing the session ID. The UserPromptSubmit hook reads this breadcrumb to find the correct .pending file. This works but is fragile — if two sessions compact simultaneously, the breadcrumb gets overwritten.

With session_id Available

If UserPromptSubmit received session_id (via env var or stdin), the hook could directly look up {session_id}-reasoning.pending — no breadcrumb needed, no race conditions, no cross-session contamination.

Proposal

Add CLAUDE_SESSION_ID as an environment variable available to all hook commands. This is:

  • Non-breaking: existing hooks ignore env vars they don't use
  • Universal: works for all hook types without changing stdin contracts
  • Simple: one line in the hook runner (env.CLAUDE_SESSION_ID = sessionId)

Environment

  • Claude Code CLI
  • macOS / zsh
  • Multiple concurrent sessions in the same project directory

View original on GitHub ↗

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