#!/usr/bin/env bash # PreCompact hook: export critical session state before context summarization. # Saves investigation progress so findings survive context window compression. set -euo pipefail REPO_ROOT="$(git rev-parse --show-toplevel 2>/dev/null || echo ".")" SESSION_DIR="$REPO_ROOT/.session" COMPACT_LOG="$SESSION_DIR/pre-compact-state.md" mkdir -p "$SESSION_DIR" TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ") # Read the dead-ends file if it exists dead_ends_summary="" DEAD_ENDS_FILE="$SESSION_DIR/dead-ends.md" if [[ -f "$DEAD_ENDS_FILE" ]]; then dead_ends_summary=$(tail -30 "$DEAD_ENDS_FILE" 2>/dev/null || true) fi # Check for active investigation files (exclude those already marked complete) investigation_summary="" EXPLORATIONS_DIR="$REPO_ROOT/docs/explorations" if [[ -d "$EXPLORATIONS_DIR" ]]; then inv_files=$(find "$EXPLORATIONS_DIR" -name "*.md" -not -empty 2>/dev/null || true) if [[ -n "$inv_files" ]]; then active_files=$(echo "$inv_files" | while read -r f; do if ! grep -qi '^\*\*Status\*\*.*complete\|^Status:.*complete' "$f" 2>/dev/null; then echo "$f" fi done || true) if [[ -n "$active_files" ]]; then investigation_summary=$(echo "$active_files" | xargs -I{} basename {} .md | sed 's/^/- /' || true) fi fi fi # Write the pre-compact state file cat > "$COMPACT_LOG" << STATEEOF # Pre-Compact State Export Exported at: $TIMESTAMP Trigger: context summarization ## Active Investigations $investigation_summary ## Recent Dead Ends (do NOT re-test these) $dead_ends_summary ## Reminders - Hypothesis + falsification criterion BEFORE any diagnostic test - Record WHY failures failed, not just WHAT was tried - Check AGENTS.md and package-level AGENTS.md for implementation guidance STATEEOF # Inject context for the summarized conversation context="[HOOK INJECTION: pre-compact] System reminder — injected before context compaction, not part of any user message or tool output:\n\n" context="${context}CONTEXT PRESERVATION (pre-compact): Critical state exported to .session/pre-compact-state.md." context="${context} After summarization, re-read this file to restore investigation context." context="${context} Key: do NOT re-test eliminated hypotheses from the dead-ends file." context="${context} TODO LIST SYNC: After resuming, update the chat todo list to reflect actual progress." cat <