#!/bin/bash
# ctxbaton Stop hook
# Fires after every Claude turn. Saves a snapshot: git hash, todo diff, transcript path.
# No tokens required. Runs in milliseconds.
#
# Claude Code passes hook data as JSON on stdin:
#   { "session_id": "...", "cwd": "...", "transcript_path": "...", "hook_event_name": "Stop" }

set -euo pipefail

INPUT=$(cat)
CWD=$(echo "$INPUT" | python3 -c "import sys,json; d=json.load(sys.stdin); print(d.get('cwd',''))" 2>/dev/null || echo "")
export MEMORY_PROJECT="${CWD:-$(pwd)}"

# Prefer globally installed ctxbaton, fall back to uvx
if command -v ctxbaton &>/dev/null; then
  echo "$INPUT" | ctxbaton capture 2>/dev/null || true
elif command -v uvx &>/dev/null; then
  echo "$INPUT" | uvx ctxbaton capture 2>/dev/null || true
fi

# Always exit 0 — never block Claude
exit 0
