Identify Session Id
Resolve the current session's id — the root transcript filename stem under
~/.claude/projects/<encoded-workspace>/ — without asking the human. Try the
sources in order and stop at the first that yields a non-empty id. Always
report which source was used.
Resolution Order
Environment variable (primary). Read
CLAUDE_SESSION_IDfrom the environment with a single command, e.g.:- Bash:
printf '%s' "$CLAUDE_SESSION_ID" - pwsh:
pwsh -NoProfile -Command 'Write-Output $env:CLAUDE_SESSION_ID'
This variable is provisioned by the SessionStart hook
.claude/hooks/persist-session-id.ps1through theCLAUDE_ENV_FILEchannel. If it is non-empty, use it and report sourceenv:CLAUDE_SESSION_ID.- Bash:
State file (secondary). Read
.claude/state/current-session-id(written by the same hook whenCLAUDE_ENV_FILEis unset). If it exists and is non-empty, use its trimmed contents and report source.claude/state/current-session-id.Newest transcript (tertiary heuristic). List the root
*.jsonlfiles directly under~/.claude/projects/<encodeWorkspacePath(cwd)>/(the encoding replaces every path separator and:with-), pick the one with the newest modification time, and use its filename stem (without.jsonl). Report sourcenewest-mtime transcriptand note that this heuristic can pick the wrong sibling only when multiple concurrent sessions share one workspace path.
Output
Report the resolved session id and the source that supplied it, for example:
session_id = <id> (source: env:CLAUDE_SESSION_ID). If every source is empty,
state that the session id could not be resolved and which sources were checked.