feat: forward hook_decision_reason from PreToolUse hooks to can_use_tool callback#820
Open
Nik-Reddy wants to merge 1 commit intoanthropics:mainfrom
Open
feat: forward hook_decision_reason from PreToolUse hooks to can_use_tool callback#820Nik-Reddy wants to merge 1 commit intoanthropics:mainfrom
Nik-Reddy wants to merge 1 commit intoanthropics:mainfrom
Conversation
…ool callback When a PreToolUse hook returns permissionDecision: 'ask' with a permissionDecisionReason, the reason string was not being forwarded to the SDK's can_use_tool callback, making it inaccessible to consumers building custom permission UIs. Changes: - Add hook_decision_reason field to SDKControlPermissionRequest - Add hook_decision_reason field to ToolPermissionContext - Pass hook_decision_reason through in Query._handle_control_request - Add tests for presence and absence of hook_decision_reason Fixes anthropics#816
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When a
PreToolUsehook returnspermissionDecision: "ask"with apermissionDecisionReason, the reason string is silently dropped before reaching the SDK'scan_use_toolcallback. This means SDK consumers — such as Slack bots, custom approval UIs, or compliance dashboards — have no way to display why a hook is requesting human confirmation.For example, a safety hook might flag a destructive shell command with the reason
"Destructive command detected by safety hook", but downstream consumers never see this context, making the permission prompt less informative and harder to act on.Solution
Thread the
hook_decision_reasonfield through the permission request pipeline:types.py→SDKControlPermissionRequesthook_decision_reasonfieldtypes.py→ToolPermissionContexthook_decision_reason: str | Nonewith docstring_internal/query.py→_handle_control_requestThe change is fully backward-compatible —
hook_decision_reasondefaults toNonewhen the CLI or hook does not provide it.Testing
Added two focused test cases:
test_permission_callback_receives_hook_decision_reason— Verifies the reason string is correctly forwarded when presenttest_permission_callback_missing_hook_decision_reason— Verifies graceful default toNonewhen the field is absentAll 462 existing tests continue to pass with no regressions.
Impact
This is a small but important change for the agent safety/governance story — it enables SDK consumers to build more informative permission UIs that surface why a tool call needs approval, not just that it does.
Fixes #816