|
| 1 | +#!/usr/bin/env bash |
| 2 | +# PostToolUse hook for ExitPlanMode |
| 3 | +# When a plan is approved and an active goal exists, nudges Claude to |
| 4 | +# capture the plan's steps as goal steps so they persist across sessions. |
| 5 | +set -euo pipefail |
| 6 | + |
| 7 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../scripts" && pwd)" |
| 8 | +PROJECT_ROOT="${PWD}" |
| 9 | +CLAUDE_DIR="${PROJECT_ROOT}/.claude" |
| 10 | +CURRENT_GOAL_FILE="${CLAUDE_DIR}/.current-goal" |
| 11 | +PLANS_DIR="${HOME}/.claude/plans" |
| 12 | + |
| 13 | +# Quick exit if no active goal or no plans directory |
| 14 | +if [[ ! -f "${CURRENT_GOAL_FILE}" ]] || [[ ! -d "${PLANS_DIR}" ]]; then |
| 15 | + echo '{}' |
| 16 | + exit 0 |
| 17 | +fi |
| 18 | + |
| 19 | +# Find the most recently modified plan file (modified in the last 60 seconds) |
| 20 | +PLAN_FILE=$(find "${PLANS_DIR}" -name '*.md' -not -name '*.backup-*' -mmin -1 -print0 2>/dev/null \ |
| 21 | + | xargs -0 ls -t 2>/dev/null \ |
| 22 | + | head -1) |
| 23 | + |
| 24 | +if [[ -z "${PLAN_FILE}" ]]; then |
| 25 | + echo '{}' |
| 26 | + exit 0 |
| 27 | +fi |
| 28 | + |
| 29 | +# Extract numbered steps from the plan (### N. Title pattern) |
| 30 | +# Also handles ## Changes / ### N. Title patterns |
| 31 | +export _PC_PLAN_FILE="${PLAN_FILE}" |
| 32 | +STEPS=$(python3 -c ' |
| 33 | +import os |
| 34 | +import re |
| 35 | +from pathlib import Path |
| 36 | +
|
| 37 | +plan_file = os.environ["_PC_PLAN_FILE"] |
| 38 | +content = Path(plan_file).read_text() |
| 39 | +
|
| 40 | +steps = [] |
| 41 | +for m in re.finditer(r"^###\s+(\d+)\.\s+(.+)$", content, re.MULTILINE): |
| 42 | + num = m.group(1) |
| 43 | + title = m.group(2).strip() |
| 44 | + # Clean up markdown formatting from title |
| 45 | + title = re.sub(r"`([^`]+)`", r"\1", title) |
| 46 | + # Truncate long titles |
| 47 | + if len(title) > 80: |
| 48 | + title = title[:77] + "..." |
| 49 | + steps.append(f" {num}. {title}") |
| 50 | +
|
| 51 | +if steps: |
| 52 | + print("\n".join(steps)) |
| 53 | +' 2>/dev/null || true) |
| 54 | + |
| 55 | +if [[ -z "${STEPS}" ]]; then |
| 56 | + echo '{}' |
| 57 | + exit 0 |
| 58 | +fi |
| 59 | + |
| 60 | +# Get current goal info for the message |
| 61 | +GOAL_STATUS=$(bash "${SCRIPT_DIR}/goal-context-helper.sh" --status "${PROJECT_ROOT}" 2>/dev/null || true) |
| 62 | + |
| 63 | +# Build the nudge message |
| 64 | +MSG="📋 Plan approved with steps that should be tracked in your active goal. |
| 65 | +
|
| 66 | +Plan steps detected: |
| 67 | +${STEPS} |
| 68 | +
|
| 69 | +**ACTION**: Use goal_add_step to add these as goal steps (skip any that duplicate existing steps). Use short kebab-case step IDs derived from the titles." |
| 70 | + |
| 71 | +MSG_ESCAPED=$(echo -n "${MSG}" | python3 -c "import sys,json; print(json.dumps(sys.stdin.read()))" | sed 's/^"//;s/"$//') |
| 72 | +cat << EOF |
| 73 | +{ |
| 74 | + "systemMessage": "${MSG_ESCAPED}" |
| 75 | +} |
| 76 | +EOF |
0 commit comments