Flag an Abnormal Session-End Reason
Most sessions end cleanly. A `reason` of `prompt_input_exit` or `other` is at minimum worth knowing happened, since it's a less predictable termination path than an explicit logout — this flags the unusual cases so they don't just blend into the rest of the register.
1. Save this as the hook script
#!/usr/bin/env python3
"""SessionEnd hook — flags an unusual session-end reason."""
import json, sys
data = json.load(sys.stdin)
reason = data.get("reason", "")
EXPECTED = {"clear", "resume", "logout"}
if reason not in EXPECTED:
print(f"NOTICE: session ended with reason '{reason}', outside the "
f"expected set {sorted(EXPECTED)} — worth a look.", file=sys.stderr)
sys.exit(1)
sys.exit(0)
Save to a path inside your project, e.g. .claude/hooks/flag-abnormal-session-end-reason.sh, then chmod +x it.
2. Wire it into .claude/settings.json
{
"hooks": {
"SessionEnd": [
{
"hooks": [
{"type": "command", "command": "python3 .claude/hooks/flag-abnormal-session-end-reason.py"}
]
}
]
}
}
Before you use this
A hook runs with your local shell's permissions — review the script above before adding it, the same way you'd review any script you didn't write yourself. See our guide on hook security risks for the specific CVEs this category of feature has already produced.
Related hooks