Commit 687e22f
docs(types): clarify hooks dispatch is concurrent, not sequential (#956)
## Problem
`ClaudeAgentOptions.hooks` docstring is silent on dispatch order. The
official docs example implies sequential execution:
```python
hooks={
"PreToolUse": [
HookMatcher(hooks=[rate_limiter]), # First: check rate limits
HookMatcher(hooks=[authorization_check]), # Second: verify permissions
HookMatcher(hooks=[input_sanitizer]), # Third: sanitize inputs
HookMatcher(hooks=[audit_logger]), # Last: log the action
]
}
```
The comments ("First", "Second", "Third", "Last") and the example
pattern (rate limiter gating an action it runs concurrently with) only
make sense if hooks execute sequentially. But they don't.
Empirical testing in #910 across Python, TypeScript, and Kotlin SDKs
confirms the CLI fires all `hook_callback` control requests for a given
event **in parallel**. In Python, `_spawn_control_request_handler` is
fire-and-forget — all matchers on the same event start at the same
instant. Total wall time ≈ max single hook delay (not sum), and exit
order is reversed from registration order.
This is a real footgun: a rate limiter or authorization check that runs
concurrently with the action it is supposed to gate **cannot actually
gate it**.
## Fix
Add a **Dispatch order** note to the `hooks` docstring in
`ClaudeAgentOptions` documenting the actual concurrent behavior, so
callers know not to design hooks that depend on sequential ordering.
This is a docs-only change — no runtime behavior changes.
## Tests
- `mypy src/`: clean
- `ruff check src/ tests/`: clean
- `pytest tests/`: not run (docs-only change, no logic touched)
Closes #910.
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>1 parent 5a1f94d commit 687e22f
1 file changed
Lines changed: 6 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1762 | 1762 | | |
1763 | 1763 | | |
1764 | 1764 | | |
| 1765 | + | |
| 1766 | + | |
| 1767 | + | |
| 1768 | + | |
| 1769 | + | |
| 1770 | + | |
1765 | 1771 | | |
1766 | 1772 | | |
1767 | 1773 | | |
| |||
0 commit comments