fix: reject hook entries that are not HookMatcher instead of silently dropping them #1543
All Vouch review confidence checks passed
Residual risk 0.24 = Change Risk (0.45) × (1 − Review Confidence (0.47)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.45 · 294 lines diff across 3 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Shallow Review Depth
shallow_review_depth
0.344
Diff Size
diff_size
0.340
Ai Authored
ai_authored
0.050
Real PR Conversation Timeline
1 comments
"Review active on #1543"
general
weight: 0.35
tonydzi
Summary
Fixes #1542
_hooks_to_internal_format()read hook entries withhasattr(), so an entry that is not aHookMatcher, such as the dict{"matcher": "Bash", "hooks": [deny_bash]}, became{"matcher": None, "hooks": []}: a match-all matcher with no callbacks. APreToolUsedeny hook written that way never ran, with no error or warning.This change raises
TypeErrorfor any entry that is not aHookMatcher, with a message that names the event and points toHookMatcher(matcher=..., hooks=[...]).Trade-off: a duck-typed object with
.matcher/.hooksattributes that works today is now rejected too. If you would rather keep dicts working, the alternative is to acceptMappingentries by key (and still reject anything else); happy to switch the PR to that.Changes
src/claude_agent_sdk/types.py:isinstance(matcher, HookMatcher)check in_hooks_to_internal_format(),TypeErrorotherwise.tests/test_query.py:test_hooks_to_internal_format_rejects_non_hookmatcher_entries.Test plan
On
main@ 4720d36, Python 3.13:types.pychange: 1 failed. With it: passes.python -m pytest tests/test_query.py tests/test_tool_callbacks.py tests/test_types.py: 216 passed.python -m pytest tests/: 1725 passed, 6 skipped, 4 failed. The same 4 tests fail onmainwithout this change (1724 passed):tests/test_close_cancellation.py::test_close_under_cancellation_still_reaps_child[asyncio|trio]and::test_cancelled_client_context_leaves_no_child[asyncio|trio], which look environment-related (container) and are unrelated to hooks.python -m ruff check src/ tests/ scripts/: all checks passed.python -m ruff format --checkon both files: already formatted.python -m mypy src/ scripts/: no issues found in 34 source files.HookMatcherthe deny hook blocks theBashcall; with the dict form the hook is never called and the command runs. With this change,_hooks_to_internal_format()raisesTypeErrorfor the dict form instead (covered by the new test).AI assistance: the analysis, repro, patch and tests were prepared with an AI coding assistant (Claude Code); the results above are from running them.
🤖 Generated with Claude Code