feat: expose session_id in Stop hook input JSON

Status Fixed / completed
Maintainer reply None cached
Activity 3 comments · opened Feb 28, 2026 · closed Mar 3, 2026

Problem

Stop hooks receive JSON input via stdin but it contains no session metadata. To build workflows that reference the current session (e.g., saving a resume command, logging session duration, linking sessions to external trackers), the hook must reverse-engineer the session ID by:

  1. Mapping $PWD through sed 's|/|-|g' to find ~/.claude/projects/<encoded-path>/
  2. Finding the most recently modified .jsonl file in that directory
  3. Extracting the UUID from the filename

This is fragile because:

  • The path encoding (/-) is undocumented
  • Claude Code maps to the git root, not $PWD, so hooks running from subdirectories fail to find the project directory
  • Race conditions if multiple sessions exist for the same project

Proposed Solution

Include session_id (and optionally project_dir) in the Stop hook's stdin JSON:

{
  "session_id": "c45e6165-b736-44b9-93f1-b0cbe12e9ddf",
  "project_dir": "/Users/user/dev/my-project",
  "stop_hook_active": false
}

This would make Stop hooks like "save resume command" or "log session" trivial to implement without relying on undocumented internals.

Current Workaround

work_dir=$(git rev-parse --show-toplevel 2>/dev/null || echo "$PWD")
project_dir="$HOME/.claude/projects/$(echo "$work_dir" | sed 's|/|-|g')"
session_file=$(ls -t "$project_dir"/*.jsonl 2>/dev/null | head -1)
session_id=$(basename "$session_file" .jsonl)

View original on GitHub ↗

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