Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save entrepeneur4lyf/878c5311abf7e10cd06130374f2cec73 to your computer and use it in GitHub Desktop.

Select an option

Save entrepeneur4lyf/878c5311abf7e10cd06130374f2cec73 to your computer and use it in GitHub Desktop.
TodoWrite Claude Code Self Evaluation Hook
#!/bin/bash
# Lightweight Todo Completion Quality Check
input=$(cat)
# Extract tool name with error handling
if ! tool_name=$(echo "$input" | jq -r '.tool_name' 2>/dev/null); then
exit 0 # Invalid JSON, skip check
fi
# Debug: Log all tool calls
echo "[$(date)] Hook triggered by tool: $tool_name" >> ~/.claude/hook-debug.log
# Initialize completed_todos
completed_todos=""
# Handle TodoWrite tool
if [[ "$tool_name" == "TodoWrite" ]]; then
completed_todos=$(echo "$input" | jq -r '.tool_input.todos[]? | select(.status == "completed") | .content' 2>/dev/null || echo "")
# Handle MCP task tools
elif [[ "$tool_name" =~ mcp__.*task.* ]]; then
if new_status=$(echo "$input" | jq -r '.tool_input.status // empty' 2>/dev/null); then
if [[ "$new_status" =~ ^(done|completed|finished)$ ]]; then
completed_todos=$(echo "$input" | jq -r '.tool_input.title // "Task completion"' 2>/dev/null || echo "Task completion")
fi
fi
# Try to find completed todos in any tool input
else
completed_todos=$(echo "$input" | jq -r '.tool_input | try (.todos[]? | select(.status == "completed") | .content) // empty' 2>/dev/null || echo "")
fi
# Count non-empty lines only - handle multiline output properly
if [[ -n "$completed_todos" && "$completed_todos" != "" ]]; then
todo_count=$(echo "$completed_todos" | grep -c '^.' 2>/dev/null || echo "0")
else
todo_count=0
fi
# Ensure todo_count is a valid integer
todo_count=${todo_count:-0}
[[ ! "$todo_count" =~ ^[0-9]+$ ]] && todo_count=0
[ "$todo_count" -eq 0 ] && exit 0
# Write evaluation message to stderr so Claude sees it
cat >&2 << EVAL_EOF
⚠️ QUALITY CHECK: $todo_count todo(s) completed
$(echo "$completed_todos" | sed 's/^/• /')
SCORE YOUR WORK (18+ required, MAX 23):
+10 elegant solution, +5 optimization, +3 perfect style, +2 minimal code, +2 edge cases, +1 portable
-10 broken/fake code, -10 placeholders/TODOs, -10 security flaws, -5 bad algorithms, -3 bloated code, -2 missed edges, -1 overcomplicated, -23 MISLEAD USER
❌ AUTO-FAIL: TODO comments, NotImplementedError, function stubs, mock/demo code, hardcoded secrets, broken functionality, claiming completion when work is incomplete
Provide score and brief justification.
EVAL_EOF
# Exit with code 2 to block and show message to Claude
exit 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment