#!/bin/bash # PreCompact Hook - Save state before context compaction # # Runs before Claude compacts context, giving you a chance to # preserve important state that might get lost in summarization. # # Configured in .cursor/hooks.json: # preCompact -> node .cursor/scripts/hooks/pre-compact.js # This shell version is a fallback for environments without Node. ROOT="$(git rev-parse --show-toplevel 2>/dev/null || pwd)" SESSIONS_DIR="${ROOT}/.cursor/sessions" COMPACTION_LOG="${SESSIONS_DIR}/compaction-log.txt" mkdir -p "$SESSIONS_DIR" # Log compaction event with timestamp echo "[$(date '+%Y-%m-%d %H:%M:%S')] Context compaction triggered" >> "$COMPACTION_LOG" # If there's an active session file, note the compaction ACTIVE_SESSION=$(ls -t "$SESSIONS_DIR"/*.tmp 2>/dev/null | head -1) if [ -n "$ACTIVE_SESSION" ]; then echo "" >> "$ACTIVE_SESSION" echo "---" >> "$ACTIVE_SESSION" echo "**[Compaction occurred at $(date '+%H:%M')]** - Context was summarized" >> "$ACTIVE_SESSION" fi echo "[PreCompact] State saved before compaction" >&2