← Back to Pull requests

fix: reject hook entries that are not HookMatcher instead of silently dropping them #1543

Open
unknown opened this pull request with 294 lines diff across 3 files
View on GitHub
unknown unknown commented on Nov 16, 2026 19:30 UTC
Author

Summary

Fixes #1542

_hooks_to_internal_format() read hook entries with hasattr(), so an entry that is not a HookMatcher, such as the dict {"matcher": "Bash", "hooks": [deny_bash]}, became {"matcher": None, "hooks": []}: a match-all matcher with no callbacks. A PreToolUse deny hook written that way never ran, with no error or warning.

This change raises TypeError for any entry that is not a HookMatcher, with a message that names the event and points to HookMatcher(matcher=..., hooks=[...]).

Trade-off: a duck-typed object with .matcher / .hooks attributes that works today is now rejected too. If you would rather keep dicts working, the alternative is to accept Mapping entries 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(), TypeError otherwise.

  • tests/test_query.py: test_hooks_to_internal_format_rejects_non_hookmatcher_entries.
  • Test plan

    On main @ 4720d36, Python 3.13:

  • New test without the types.py change: 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 on main without 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 --check on both files: already formatted.

  • python -m mypy src/ scripts/: no issues found in 34 source files.

  • End to end (SDK 0.2.182, bundled CLI 2.1.325, scripted local Messages API): with a HookMatcher the deny hook blocks the Bash call; with the dict form the hook is never called and the command runs. With this change, _hooks_to_internal_format() raises TypeError for 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

    Diff: 294 lines (+220 / -74) across 3 files Reviewer: tonydzi Review duration: 20353m
    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
    0.45
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 44.9% (MEDIUM) · Higher = larger blast radius
    44.9%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 23.9% · Safe to merge
    23.9%