|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# / Context: https://ctx.ist |
| 4 | +# ,'`./ do you remember? |
| 5 | +# `.,'\ |
| 6 | +# \ Copyright 2026-present Context contributors. |
| 7 | +# SPDX-License-Identifier: Apache-2.0 |
| 8 | + |
| 9 | +# Prompt coaching hook for Claude Code |
| 10 | +# Detects anti-patterns and suggests better alternatives. |
| 11 | +# Limits suggestions to MAX_SUGGESTIONS per pattern per session. |
| 12 | +# |
| 13 | +# Generated by: ctx init |
| 14 | +# |
| 15 | +# ANTI-PATTERNS DETECTED: |
| 16 | +# - "idiomatic X" → "follow project conventions" |
| 17 | +# - "best practices" → "follow CONVENTIONS.md" |
| 18 | +# - "fix the bug" (vague) → include error message, file, and context |
| 19 | +# - "optimize it" (vague) → specify what to optimize and why |
| 20 | +# - "make it better" (vague) → define "better" with criteria |
| 21 | +# - "update the component" (vague) → specify which component and what changes |
| 22 | +# |
| 23 | +# Output: Warnings to stderr (non-blocking) |
| 24 | +# Exit: Always 0 (never blocks execution) |
| 25 | + |
| 26 | +MAX_SUGGESTIONS=3 |
| 27 | +SESSION_FILE="/tmp/ctx-prompt-coach-$$-$(date +%Y%m%d).state" |
| 28 | + |
| 29 | +# Initialize session file if it doesn't exist |
| 30 | +if [ ! -f "$SESSION_FILE" ]; then |
| 31 | + cat > "$SESSION_FILE" << 'EOF' |
| 32 | +idiomatic=0 |
| 33 | +bestpractices=0 |
| 34 | +fixbug=0 |
| 35 | +optimize=0 |
| 36 | +makebetter=0 |
| 37 | +update=0 |
| 38 | +EOF |
| 39 | +fi |
| 40 | + |
| 41 | +# Read hook input from stdin (JSON) |
| 42 | +HOOK_INPUT=$(cat) |
| 43 | + |
| 44 | +# Extract the prompt text |
| 45 | +PROMPT=$(echo "$HOOK_INPUT" | jq -r '.prompt // empty') |
| 46 | + |
| 47 | +# If no prompt, allow |
| 48 | +if [ -z "$PROMPT" ]; then |
| 49 | + exit 0 |
| 50 | +fi |
| 51 | + |
| 52 | +# Helper: get count for a pattern |
| 53 | +get_count() { |
| 54 | + grep "^$1=" "$SESSION_FILE" 2>/dev/null | cut -d= -f2 || echo "0" |
| 55 | +} |
| 56 | + |
| 57 | +# Helper: increment count for a pattern |
| 58 | +increment_count() { |
| 59 | + local pattern="$1" |
| 60 | + local count=$(get_count "$pattern") |
| 61 | + local new_count=$((count + 1)) |
| 62 | + if grep -q "^$pattern=" "$SESSION_FILE" 2>/dev/null; then |
| 63 | + sed -i "s/^$pattern=.*/$pattern=$new_count/" "$SESSION_FILE" |
| 64 | + else |
| 65 | + echo "$pattern=$new_count" >> "$SESSION_FILE" |
| 66 | + fi |
| 67 | +} |
| 68 | + |
| 69 | +# Helper: output a coaching tip (only if under limit) |
| 70 | +suggest() { |
| 71 | + local pattern="$1" |
| 72 | + local tip="$2" |
| 73 | + local example="$3" |
| 74 | + local count=$(get_count "$pattern") |
| 75 | + |
| 76 | + if [ "$count" -lt "$MAX_SUGGESTIONS" ]; then |
| 77 | + echo "" >&2 |
| 78 | + echo "┌─ Prompt Tip ─────────────────────────────────────" >&2 |
| 79 | + echo "│ $tip" >&2 |
| 80 | + if [ -n "$example" ]; then |
| 81 | + echo "│" >&2 |
| 82 | + echo "│ Example: $example" >&2 |
| 83 | + fi |
| 84 | + echo "└──────────────────────────────────────────────────" >&2 |
| 85 | + echo "" >&2 |
| 86 | + increment_count "$pattern" |
| 87 | + fi |
| 88 | +} |
| 89 | + |
| 90 | +# Calculate prompt length (for detecting short vague prompts) |
| 91 | +PROMPT_LEN=${#PROMPT} |
| 92 | + |
| 93 | +# Check for "idiomatic X" pattern (case-insensitive) |
| 94 | +if echo "$PROMPT" | grep -qiE 'idiomatic (go|python|rust|javascript|typescript|java|c\+\+|ruby)'; then |
| 95 | + suggest "idiomatic" \ |
| 96 | + "Instead of 'idiomatic X', try 'follow project conventions'" \ |
| 97 | + "'follow CONVENTIONS.md patterns for error handling'" |
| 98 | +fi |
| 99 | + |
| 100 | +# Check for "best practices" pattern (case-insensitive) |
| 101 | +if echo "$PROMPT" | grep -qiE '\bbest practices?\b'; then |
| 102 | + suggest "bestpractices" \ |
| 103 | + "Instead of 'best practices', try 'follow CONVENTIONS.md'" \ |
| 104 | + "'follow the error handling pattern from CONVENTIONS.md'" |
| 105 | +fi |
| 106 | + |
| 107 | +# Check for vague "fix the bug" / "fix this bug" patterns |
| 108 | +# Only trigger if the prompt is short and lacks specifics |
| 109 | +if [ "$PROMPT_LEN" -lt 50 ] && echo "$PROMPT" | grep -qiE '\bfix (the|this|that|a) (bug|issue|error|problem)\b'; then |
| 110 | + # Check if prompt lacks context (no file path, no error message, no line number) |
| 111 | + if ! echo "$PROMPT" | grep -qE '(\.[a-z]{2,4}|line [0-9]|:[0-9]+|error:|Error:|failed|Failed)'; then |
| 112 | + suggest "fixbug" \ |
| 113 | + "Add context: which file? what error? what's broken?" \ |
| 114 | + "'fix the JWT validation error in src/auth/login.ts returning 401'" |
| 115 | + fi |
| 116 | +fi |
| 117 | + |
| 118 | +# Check for vague "optimize" patterns |
| 119 | +if [ "$PROMPT_LEN" -lt 40 ] && echo "$PROMPT" | grep -qiE '\b(optimize|optimise) (it|this|that)\b'; then |
| 120 | + suggest "optimize" \ |
| 121 | + "Specify what to optimize and what metric matters" \ |
| 122 | + "'optimize UserList to reduce re-renders when parent state updates'" |
| 123 | +fi |
| 124 | + |
| 125 | +# Check for vague "make it better" patterns |
| 126 | +if echo "$PROMPT" | grep -qiE '\bmake (it|this|that) (better|nicer|cleaner|good)\b'; then |
| 127 | + if [ "$PROMPT_LEN" -lt 50 ]; then |
| 128 | + suggest "makebetter" \ |
| 129 | + "Define 'better' - readability? performance? maintainability?" \ |
| 130 | + "'refactor to be more readable by extracting validation logic'" |
| 131 | + fi |
| 132 | +fi |
| 133 | + |
| 134 | +# Check for vague "update the component/function/file" patterns |
| 135 | +if [ "$PROMPT_LEN" -lt 50 ] && echo "$PROMPT" | grep -qiE '\bupdate (the|this|that|a) (component|function|file|module|class)\b'; then |
| 136 | + # Check if prompt lacks specifics |
| 137 | + if ! echo "$PROMPT" | grep -qE '(\.[a-z]{2,4}|src/|lib/|internal/)'; then |
| 138 | + suggest "update" \ |
| 139 | + "Specify which component and what changes" \ |
| 140 | + "'update Button in src/components/Button.tsx to use new color tokens'" |
| 141 | + fi |
| 142 | +fi |
| 143 | + |
| 144 | +# Always allow the prompt to proceed |
| 145 | +exit 0 |
0 commit comments