track-tools.sh 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. #!/bin/bash
  2. # track-tools.sh - PostToolUse hook: lightweight tool call counter
  3. # Appends tool name to a session-specific temp file.
  4. # Designed to be fast (<5ms) - no SQLite, no network, just a file append.
  5. #
  6. # Special case: when the Skill tool is invoked, we record `Skill:<name>`
  7. # instead of bare `Skill`. evaluate.sh uses the name to whitelist harness
  8. # skills (sync, save, etc) from Gate 1 disqualification.
  9. #
  10. # CRITICAL: This hook must NEVER fail visibly. All errors suppressed.
  11. {
  12. INPUT=$(cat)
  13. TOOL_NAME=$(printf '%s' "$INPUT" | jq -r '.tool_name // empty' 2>/dev/null)
  14. SESSION_ID=$(printf '%s' "$INPUT" | jq -r '.session_id // empty' 2>/dev/null)
  15. [ -z "$TOOL_NAME" ] && exit 0
  16. [ -z "$SESSION_ID" ] && exit 0
  17. SHORT_ID="${SESSION_ID:0:8}"
  18. TRACK_FILE="/tmp/claude_autoskill_${SHORT_ID}"
  19. # Tag Skill tool calls with the skill name so Gate 1 can whitelist
  20. if [ "$TOOL_NAME" = "Skill" ]; then
  21. SKILL_NAME=$(printf '%s' "$INPUT" | jq -r '.tool_input.skill // "unknown"' 2>/dev/null)
  22. # Sanitise: keep parsing simple by normalising separators
  23. SKILL_NAME=$(printf '%s' "$SKILL_NAME" | tr ': ' '_')
  24. TOOL_NAME="Skill:${SKILL_NAME}"
  25. fi
  26. # Append tool name (one per line). Cap at 500 lines to prevent runaway.
  27. if [ ! -f "$TRACK_FILE" ] || [ "$(wc -l < "$TRACK_FILE" 2>/dev/null)" -lt 500 ]; then
  28. echo "$TOOL_NAME" >> "$TRACK_FILE"
  29. fi
  30. } 2>/dev/null
  31. exit 0