fix(bad): write session state to .claude/ instead of TMPDIR

Store bad-session-state.json next to the statusline script in .claude/
so it persists reliably outside the sandbox-restricted temp directory.
Gate-pre-continuation now reads from .claude/bad-session-state.json
(relative to project root) — no setup-time path substitution required.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
stephenleo
2026-04-13 09:41:06 +08:00
parent db95d2cf2c
commit f78a5b030d
3 changed files with 11 additions and 8 deletions

View File

@@ -1,16 +1,18 @@
#!/bin/bash
# BAD session-state capture — installed to {project-root}/.claude/bad-statusline.sh
# during /bad setup. Configured as the Claude Code statusLine script so it runs
# after every API response and writes the session JSON to a temp file that the
# BAD coordinator reads during Pre-Continuation Checks.
# after every API response and writes the session JSON to .claude/bad-session-state.json
# (next to this script) that the BAD coordinator reads during Pre-Continuation Checks.
#
# To chain with an existing statusline script:
# SESSION_JSON=$(cat)
# echo "$SESSION_JSON" | /path/to/your-existing-script.sh
# echo "$SESSION_JSON" > "${TMPDIR:-/tmp}/bad-session-state.json"
# SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
# echo "$SESSION_JSON" > "$SCRIPT_DIR/bad-session-state.json"
SESSION_JSON=$(cat)
echo "$SESSION_JSON" > "${TMPDIR:-/tmp}/bad-session-state.json"
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
echo "$SESSION_JSON" > "$SCRIPT_DIR/bad-session-state.json"
# Output nothing — add your own status text here if desired, e.g.:
# python3 -c "
# import sys, json

View File

@@ -11,7 +11,7 @@ If `NOTIFY_SOURCE` is not `"terminal"` (i.e. a channel like Telegram is configur
Read the current session state using the Bash tool:
```bash
cat "${TMPDIR:-/tmp}/bad-session-state.json" 2>/dev/null || echo "{}"
cat ".claude/bad-session-state.json" 2>/dev/null || echo "{}"
```
Parse the output as JSON. The relevant fields:
@@ -22,7 +22,7 @@ Parse the output as JSON. The relevant fields:
- `rate_limits.seven_day.used_percentage` — 0100 (Claude Code only)
- `rate_limits.seven_day.resets_at` — Unix epoch seconds when the 7-day window resets
**If the file does not exist** — print `"⚠️ Pre-Continuation: session state unavailable (bad-session-state.json missing — check that the session-state hook is installed via /bad setup Step 3). Skipping rate limit checks."` and proceed.
**If the file does not exist** — print `"⚠️ Pre-Continuation: session state unavailable (.claude/bad-session-state.json missing — check that the session-state hook is installed via /bad setup Step 3). Skipping rate limit checks."` and proceed.
**If a specific field is absent** — silently skip only that check. If the file exists but `rate_limits` is entirely absent, print `"⚠️ Pre-Continuation: rate limit data not in session state — skipping usage checks."` once (not on every gate).

View File

@@ -38,10 +38,11 @@ If `CHAIN_COMMAND` is non-empty, rewrite the script body:
#!/bin/bash
# BAD session-state capture — auto-generated by /bad setup.
# Chains pre-existing statusLine command then writes Claude Code session JSON
# to a temp file for BAD Pre-Continuation Checks.
# to .claude/bad-session-state.json for BAD Pre-Continuation Checks.
SESSION_JSON=$(cat)
echo "$SESSION_JSON" | <CHAIN_COMMAND>
echo "$SESSION_JSON" > "${TMPDIR:-/tmp}/bad-session-state.json"
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
echo "$SESSION_JSON" > "$SCRIPT_DIR/bad-session-state.json"
```
If `CHAIN_COMMAND` is empty, keep the script body as-is.