hook-session-state-isolation
Hook session-state isolation: never default session_id
When building a PreToolUse/PostToolUse hook that tracks per-session state via a filesystem marker (e.g., /tmp/.marker-<session_id>), never fall back to a hardcoded default like 'unknown' if session_id is missing from the hook payload.
Why it breaks
A shared default marker causes cross-session state leak: one session creating /tmp/.marker-unknown grants bypass to all subsequent sessions that also get 'unknown', defeating the purpose of session isolation. The guard silently stops enforcing on every subsequent session.
Correct pattern
session_id = data.get('session_id', '').strip()
if not session_id:
# No valid session_id → treat as "no marker"
# For guards: always warn
# For marker-creation: skip write entirely (don't create shared state)
...Discovered while building
setup-curator-guard.py + setup-curator-marker.py at /root/.claude/hooks/. Applies to ANY Claude Code hook that tracks per-session state in shared filesystem (/tmp, shared state files, lockfiles, etc.).
Verification
Tested with 5 edge cases: missing session_id, present session_id + no marker, present session_id + marker, empty session_id (marker hook refuses write), non-setup file bypass. All isolated correctly.
Related
- docker
- 2026-04-04-oracle-001-self-architecture-analysis
- enterprise-capability-expansion-5-pillars-from-digital-employee-analysis
- notebooklm
- clawteam-openclaw-multi-agent-swarm-evaluation
- session-id-isolation-bug-in-guard-marker-hooks