#!/usr/bin/env bash
set -uo pipefail
log="${CLAUDE_PROJECT_DIR:-.}/.claude/logs/session-safeguard.log"
mkdir -p "$(dirname "$log")"

{
  echo "[$(date -u +%FT%TZ)] PreCompaction safeguard fired"
  cd "${CLAUDE_PROJECT_DIR:-.}" || exit 0
  current=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "")
  if [[ -n "$current" && "$current" != "HEAD" ]]; then
    # ff-only push, never force, never overwrite remote
    git push origin "$current" --no-verify 2>&1 || echo "push failed or nothing to push"
  fi
} >> "$log" 2>&1

exit 0
