#!/bin/bash
#
# Context Checkpoint Skill for Claude Code
# =========================================
# Preserves session context and orchestrates sub-agent calls.
#
# Skill documentation: /opt/agent-governance/checkpoint/CLAUDE.md
#
# Commands:
#   now [--notes "..."]           Create checkpoint capturing current state
#   load [checkpoint_id]          Load latest or specific checkpoint
#   diff [--from ID] [--to ID]    Compare checkpoints
#   list [--limit N]              List available checkpoints
#   summary --level <level>       Context summary (minimal/compact/standard/full)
#   report [--phase <phase>]      Combined checkpoint + directory status report
#   timeline [--limit N]          Show checkpoint history with status changes
#   auto-orchestrate --model <m>  Delegate to OpenRouter models
#   queue list|add|clear|pop      Manage instruction queue
#   prune [--keep N]              Remove old checkpoints
#
# Examples:
#   checkpoint now --notes "Before deploy"
#   checkpoint load --json
#   checkpoint summary --level compact
#   checkpoint auto-orchestrate --model minimax --instruction "run tests"
#

set -euo pipefail

CHECKPOINT_DIR="/opt/agent-governance/checkpoint"
CHECKPOINT_SCRIPT="${CHECKPOINT_DIR}/checkpoint.py"

# Show help if no args or --help
if [[ $# -eq 0 ]] || [[ "${1:-}" == "--help" ]] || [[ "${1:-}" == "-h" ]]; then
    cat << 'EOF'
Context Checkpoint Skill
========================

Commands:
  now [--notes "..."]              Create checkpoint (includes directory statuses)
  load [checkpoint_id] [--json]    Load checkpoint
  diff [--from ID] [--to ID]       Compare checkpoints
  list [--limit N] [--json]        List checkpoints
  summary --level <level>          Context summary
  report [--phase <phase>]         Combined checkpoint + directory status report
  timeline [--limit N]             Checkpoint history with status changes
  auto-orchestrate --model <m>     Delegate to AI models
  queue list|add|clear|pop         Manage instruction queue
  prune [--keep N]                 Remove old checkpoints

Summary Levels:
  minimal   (~500 tokens)   Phase + agent only
  compact   (~1000 tokens)  + tasks + key variables
  standard  (~2000 tokens)  + all tasks + dependencies
  full      (~4000 tokens)  Complete context

Models for auto-orchestrate:
  minimax      Minimax-01 (default, 100K context)
  gemini       Gemini 2.0 Flash Thinking (free)
  gemini-pro   Gemini 2.5 Pro (highest capability)

Examples:
  checkpoint now --notes "Phase 5 complete"
  checkpoint load --json | jq .phase
  checkpoint summary --level compact
  checkpoint report                           # Combined status view
  checkpoint report --phase in_progress       # Filter by phase
  checkpoint timeline --limit 5               # Recent history
  checkpoint auto-orchestrate --model minimax -i "check status"
  checkpoint queue add --instruction "run tests" --priority 5

Documentation: /opt/agent-governance/checkpoint/CLAUDE.md
EOF
    exit 0
fi

# Verify checkpoint script exists
if [[ ! -f "${CHECKPOINT_SCRIPT}" ]]; then
    echo "Error: checkpoint.py not found at ${CHECKPOINT_SCRIPT}"
    exit 1
fi

# Execute checkpoint command
exec python3 "${CHECKPOINT_SCRIPT}" "$@"
