Skip to content

fix(hosts): expand PreToolUse matcher to cover Read, Grep, Glob, Agent, Task#86

Merged
claudioemmanuel merged 1 commit intomainfrom
fix/claude-code-pretooluse-matcher-coverage
Apr 22, 2026
Merged

fix(hosts): expand PreToolUse matcher to cover Read, Grep, Glob, Agent, Task#86
claudioemmanuel merged 1 commit intomainfrom
fix/claude-code-pretooluse-matcher-coverage

Conversation

@claudioemmanuel
Copy link
Copy Markdown
Owner

Summary

  • The Claude Code PreToolUse hook was registered with "matcher": "Bash", so Claude Code never invoked it for Read, Grep, or Glob tool calls.
  • The hook already had a working Read/Grep/Glob branch calling squeez budget-params to enforce read_max_lines/grep_max_results — but it was dead code because the matcher prevented the hook from firing.
  • Matcher is now "Bash|Read|Grep|Glob|Agent|Task" (a regex, per Claude Code's re.match evaluation), activating the existing budget enforcement for all relevant tools.

The bug this fixes

  • src/hosts/claude_code.rs:71PATCH_SCRIPT appended "matcher": "Bash" for the PreToolUse hook entry.
  • hooks/pretooluse.sh:45-66 — the Read/Grep/Glob branch that calls squeez budget-params <tool> and injects caps into tool_input was already implemented but unreachable.
  • src/config.rs:95-96read_max_lines=300 and grep_max_results=100 defaults were effectively ignored on Claude Code despite README claiming "Budget inject (Read/Grep): ✅ native".

Idempotency

The patch script now iterates existing PreToolUse entries and finds any whose hooks[0].command contains pretooluse.sh. If found, it updates the matcher field in place instead of appending a new entry. Users with the old matcher: "Bash" entry from a prior install get upgraded on their next squeez setup call — no duplicate entries.

Test plan

  • cargo test — all tests pass including 4 new tests in tests/test_hosts_claude_code.rs
  • claude_code_capabilities_include_budget_hard — verifies adapter capability flags
  • claude_code_install_pretooluse_matcher_covers_read_grep_glob_agent_task — verifies the expanded matcher appears in settings.json
  • claude_code_install_is_idempotent_no_duplicate_pretooluse — verifies second install does not create duplicate entry
  • claude_code_install_upgrades_old_bash_only_matcher_in_place — verifies a pre-existing old-style entry is upgraded, not duplicated
  • Manual: run squeez setup against a real ~/.claude/settings.json and verify the PreToolUse entry has matcher "Bash|Read|Grep|Glob|Agent|Task"
  • Manual: confirm Read tool calls now trigger the hook and budget limits are applied

…t, Task

The Claude Code adapter registered the PreToolUse hook with matcher "Bash",
which meant Claude Code only invoked pretooluse.sh for Bash tool calls.
The hook already handled Read, Grep, and Glob by calling `squeez budget-params`
and injecting caps (read_max_lines, grep_max_results) into tool_input — but
that branch was dead code because the hook never fired for those tools.

This fix changes the registered matcher from "Bash" to the regex
"Bash|Read|Grep|Glob|Agent|Task", which Claude Code evaluates via re.match
against the tool name. This activates the existing Read/Grep/Glob budget
enforcement that was already implemented but unreachable.

The patch script now handles upgrades in place: it searches for an existing
squeez pretooluse.sh entry in PreToolUse and updates its matcher field if it
differs from the new value, rather than appending a duplicate. Users who already
ran `squeez setup` with the old narrow matcher get the correct matcher on the
next `squeez setup` call without acquiring a duplicate hook entry.

A new test file tests/test_hosts_claude_code.rs verifies:
- capabilities include BUDGET_HARD
- install writes the expanded matcher to settings.json
- install is idempotent (no duplicate pretooluse.sh entries)
- install upgrades an old "Bash"-only entry in place
@claudioemmanuel claudioemmanuel merged commit 59fff7c into main Apr 22, 2026
3 of 5 checks passed
@claudioemmanuel claudioemmanuel deleted the fix/claude-code-pretooluse-matcher-coverage branch April 22, 2026 00:43
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