Files
autoresearch/bin/session-start-hook.sh
thomas.kopp c8486f236f feat: PostSessionStart hook for auto-start on budget OK
Adds bin/session-start-hook.sh as a SessionStart hook that checks
API budget on session start and launches the autoresearch tmux session
in background if budget threshold is met.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-04 16:07:04 +02:00

35 lines
999 B
Bash
Executable File

#!/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