Skip to content

Conversation

@adrozdenko
Copy link

Summary

Add a dedicated read event type for Read/Glob/Grep/LS tools to prevent file-editing rules from incorrectly triggering on read operations.

Problem

Currently, Read/Glob/Grep/LS tools have no event mapping (event = None), which causes ALL rules to load regardless of their event field. This is problematic:

User: "Read src/Button.tsx"
Hookify: "⚠️ Console.log detected!"  ← WRONG - user is just reading!

Rules with event: file (designed for Edit/Write) incorrectly fire when simply reading files.

Related: #17246

PR #17246 addresses the same issue but maps Read → file event. This approach has a UX problem: file-editing rules like "no console.log in production" would still trigger when reading files.

This PR proposes a better solution: a dedicated read event type.

Solution

Add read as a new event type:

elif tool_name in ['Read', 'Glob', 'Grep', 'LS']:
    event = 'read'

This gives users precise control:

  • event: file → Only Edit/Write/MultiEdit (actual modifications)
  • event: read → Only Read/Glob/Grep/LS (read operations)
  • event: all → Everything

Files Changed

File Change
hooks/pretooluse.py Add read event mapping
hooks/posttooluse.py Add read event mapping
README.md Document read event type
skills/writing-rules/SKILL.md Document read event type

Usage Example

---
name: warn-reading-secrets
enabled: true
event: read  # Only triggers on Read, not Edit
conditions:
  - field: file_path
    operator: regex_match
    pattern: (\.env|secrets|credentials)
---

You're reading a sensitive file. Be careful with the contents.

Why read is Better Than Reusing file

Scenario Map to file (#17246) Map to read (this PR)
"no console.log" rule fires on Read ❌ Yes (noisy) ✅ No
Can create read-specific rules ❌ No ✅ Yes
Backwards compatible ✅ Yes ✅ Yes
Semantic clarity ❌ Confusing ✅ Clear

Test Plan

  • Python syntax check passes
  • Manual testing confirms read operations don't trigger file rules
  • Backward compatible - existing rules unchanged

Previously, Read/Glob/Grep/LS tools had no event mapping, causing ALL
rules to fire regardless of their event field. This was noisy and
incorrect - file-editing rules like 'no console.log' would trigger
when simply reading a file.

This fix adds a dedicated 'read' event type so:
- Read operations only trigger 'read' or 'all' event rules
- File-editing rules (event: file) don't fire on read operations
- Users can create read-specific rules if needed

Files changed:
- hooks/pretooluse.py: Add read event mapping
- hooks/posttooluse.py: Add read event mapping
- README.md: Document read event type
- SKILL.md: Document read event type
@adrozdenko
Copy link
Author

Update: This feature is now available in hookify-plus - a standalone fork with community fixes.

If you don't want to wait for upstream review, you can use hookify-plus today:

# Backup original & install hookify-plus  
mv ~/.claude/plugins/cache/claude-code-plugins/hookify/0.1.0{,.bak}
ln -s /path/to/hookify-plus ~/.claude/plugins/cache/claude-code-plugins/hookify/0.1.0

hookify-plus includes:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant