buddymon/hooks-handlers/roster-stop.py
pyr0ball 81712d06e3 fix: roster via output file, catalog path, already-owned spawn guard
- post-tool-use: find_catalog() checks ~/.claude/buddymon/catalog.json first
  so Veritarch is always found regardless of which cache CLAUDE_PLUGIN_ROOT
  points to; fixes 'your buddy' display bug
- post-tool-use: skip spawn when monster already in collection (was only
  checked on existing encounters, not at spawn time; caused already-caught
  monsters to re-announce)
- post-tool-use: fix spawn block indentation — set_active_encounter was
  outside the else, running even on already-owned skip
- user-prompt-submit: same null buddymon_id fallback fix as post-tool-use
- cli.py cmd_roster: buffer all output to StringIO; write roster_output.txt
  + roster_pending.txt; suppress stdout when piped (isatty check)
- roster-stop.py: read roster_output.txt directly instead of re-running cli.py
  (eliminated the recursive flag-write loop); remove unused subprocess import
2026-04-12 19:02:04 -07:00

50 lines
1.2 KiB
Python

#!/usr/bin/env python3
"""
Buddymon Stop hook — roster re-emitter.
Checks for roster_pending.txt written by `cli.py roster`.
If present, runs the roster CLI and emits full output as additionalContext,
guaranteeing the full roster is visible without Bash-tool truncation.
"""
import json
import sys
from pathlib import Path
BUDDYMON_DIR = Path.home() / ".claude" / "buddymon"
PENDING_FLAG = BUDDYMON_DIR / "roster_pending.txt"
OUTPUT_FILE = BUDDYMON_DIR / "roster_output.txt"
def main():
try:
json.load(sys.stdin)
except Exception:
pass
if not PENDING_FLAG.exists():
sys.exit(0)
# Clear both files before reading — prevents a second Stop event from
# re-delivering the same roster if OUTPUT_FILE lingers.
PENDING_FLAG.unlink(missing_ok=True)
if not OUTPUT_FILE.exists():
sys.exit(0)
output = OUTPUT_FILE.read_text().strip()
OUTPUT_FILE.unlink(missing_ok=True)
if not output:
sys.exit(0)
print(json.dumps({
"hookSpecificOutput": {
"hookEventName": "Stop",
"additionalContext": output,
}
}))
sys.exit(0)
if __name__ == "__main__":
main()