Skip to content

fix(hookify): match file rules against Write tool content#69727

Open
clown6613 wants to merge 1 commit into
anthropics:mainfrom
clown6613:fix/hookify-write-content-field
Open

fix(hookify): match file rules against Write tool content#69727
clown6613 wants to merge 1 commit into
anthropics:mainfrom
clown6613:fix/hookify-write-content-field

Conversation

@clown6613

Copy link
Copy Markdown

What

A simple-pattern hookify rule with event: file (e.g. the README's "Warn About Debug Code" example, pattern: console\.log\() silently never fired when Claude created a new file via the Write tool.

Root cause

config_loader infers the field name new_text for event: file simple-pattern rules. In rule_engine._extract_field, the new_text/new_string branch only read tool_input['new_string'] (the Edit field):

elif field == 'new_text' or field == 'new_string':
    return tool_input.get('new_string', '')   # Write content is in 'content', not 'new_string'

The Write tool stores file content in tool_input['content'], so the branch returned '' and the pattern never matched. The adjacent content branch already does the right fallback.

Fix

elif field == 'new_text' or field == 'new_string':
    # Write uses 'content', Edit has 'new_string'
    return tool_input.get('new_string') or tool_input.get('content', '')

Verification

event: file, pattern: console\.log\(:

tool before after
Write (new file, content) ❌ no match ✅ match
Edit (new_string) ✅ match ✅ match
$ python3 -c "..."   # via RuleEngine._rule_matches
before: Write=False Edit=True
after:  Write=True  Edit=True

For an `event: file` simple-pattern rule, config_loader infers the
field name `new_text`. In `_extract_field`, the `new_text`/`new_string`
branch only read `tool_input['new_string']` (the Edit field), so it
returned '' for the Write tool — whose content lives in
`tool_input['content']`. As a result, file rules silently never fired
when Claude created a new file via Write (e.g. the README's own
"Warn About Debug Code" example).

Mirror the adjacent `content` branch by falling back to `content` so
both Write and Edit are covered.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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