#!/usr/bin/env bash # PostSessionStart hook: auto-start autoresearch when API budget is sufficient. # Runs async — must exit quickly. set -euo pipefail CONFIG="${HOME}/.claude/autoresearch.yaml" SESSION="autoresearch" LOG="${HOME}/.claude/autoresearch/hook.log" # Only run if config exists [[ -f "$CONFIG" ]] || exit 0 # Skip if autoresearch tmux session already running tmux has-session -t "$SESSION" 2>/dev/null && exit 0 # Read budget limit from config BUDGET_LIMIT=$(python3 -c " import yaml, os, sys try: cfg = yaml.safe_load(open('${CONFIG}')) print(cfg.get('token_threshold', {}).get('api_budget_usd', 5.0)) except Exception: print(5.0) " 2>/dev/null || echo "5.0") # Check budget (exits 0 = OK, 1 = low, or fails open) python3 "${HOME}/work/autoresearch/bin/check_budget.py" "$BUDGET_LIMIT" || exit 0 # Budget OK — start autoresearch session in background mkdir -p "$(dirname "$LOG")" nohup "${HOME}/work/autoresearch/bin/start.sh" start \ >> "$LOG" 2>&1 & exit 0