Skip to content

PreToolUse hook: Write/Edit avoid-term enforcement for publishing #46

@AlexMikhalev

Description

@AlexMikhalev

Parent

Epic: #43

Problem

The current pre_tool_use.sh hook only processes TOOL_NAME == "Bash" for Aho-Corasick text replacement. During manuscript writing, agents use Write and Edit tools. Avoid terms in these tools bypass the hook entirely.

Proposed Change

Extend ~/.claude/hooks/pre_tool_use.sh to handle Write and Edit tool inputs:

Design Decision

For Write/Edit, do NOT silently replace text. Instead, warn by emitting a permissionDecision: "ask" with a message listing the avoid terms found. The human author decides whether to accept or reject.

Implementation

case "$TOOL_NAME" in
  "Write")
    TEXT=$(echo "$TOOL_INPUT" | jq -r '.content // empty')
    ;;
  "Edit")
    TEXT=$(echo "$TOOL_INPUT" | jq -r '.new_string // empty')
    ;;
esac

if [ -n "$TEXT" ]; then
  cd ~/.config/terraphim
  RESULT=$(echo "$TEXT" | terraphim-agent replace --role "Publishing Editor" --json 2>/dev/null)
  CHANGED=$(echo "$RESULT" | jq -r '.changed // false')
  if [ "$CHANGED" = "true" ]; then
    REPLACEMENTS=$(echo "$RESULT" | jq -r '.replacements[] | "  \(.from) -> \(.to)"')
    echo '{"decision": "ask", "message": "Avoid terms detected in manuscript text:\n'"$REPLACEMENTS"'\n\nAccept replacements?"}'
    exit 0
  fi
fi

Configuration

TERRAPHIM_PUBLISHING_ENFORCE env var (default: warn):

  • warn: Show avoid-term warnings for Write/Edit, replace silently for Bash
  • strict: Block Write/Edit if avoid terms found
  • off: Disable publishing enforcement

Files Changed

  • ~/.claude/hooks/pre_tool_use.sh -- add Write/Edit cases
  • New: ~/.claude/hooks/publishing-enforce.sh (extracted logic for testability)

Dependencies

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions