feat: add deferred tool use example with human-in-the-loop flow#1062
Open
highgroundbkk wants to merge 4 commits into
Open
feat: add deferred tool use example with human-in-the-loop flow#1062highgroundbkk wants to merge 4 commits into
highgroundbkk wants to merge 4 commits into
Conversation
- Fix module docstring: allow flow uses resume=session_id, not continue_conversation+extra_args
- Fix deny flow docstring: opens fresh plan-mode session, not a follow-up
- Add session_id None guard before resume= to prevent silent wrong-session bug
- Handle ResultMessage.is_error in display() so failed sessions are visible
- Fix deny prompt: fresh session framing instead of misleading 'you previously...'
- Use json.dumps(deferred.input) instead of .get('command', fallback) to avoid silent data loss
- Fix display() type annotation: object -> Message
- Add Usage: section to module docstring
- Remove emoji from print() output per CLAUDE.md
There was a problem hiding this comment.
Pull request overview
Adds a new example showcasing “deferred tool use” (human-in-the-loop approval) and updates Claude Code project settings to automatically run mypy after edit/write operations.
Changes:
- Add
examples/deferred_tool_use.pyto demonstratepermissionDecision: "defer"in aPreToolUsehook, including allow/resume and deny/plan flows. - Update
.claude/settings.jsonto run mypy (uv run ... python -m mypy src/ --no-error-summary) as aPostToolUsehook afterEdit|Write|MultiEdit.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| examples/deferred_tool_use.py | New end-to-end demo for deferred tool execution with explicit human approval/denial flows. |
| .claude/settings.json | Adds automatic mypy execution as a PostToolUse hook after file edits/writes. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+117
to
+121
| resume_options = ClaudeAgentOptions( | ||
| allowed_tools=["Bash"], | ||
| permission_mode="bypassPermissions", | ||
| resume=session_id, | ||
| ) |
Comment on lines
+82
to
+88
| options = ClaudeAgentOptions( | ||
| allowed_tools=["Bash"], | ||
| permission_mode="default", | ||
| hooks={ | ||
| "PreToolUse": [HookMatcher(matcher="Bash", hooks=[defer_for_approval])], | ||
| }, | ||
| ) |
| print(f" Tool : {deferred.name}") | ||
| print(f" Input : {deferred.input}") | ||
| else: | ||
| cost = f"${msg.total_cost_usd:.4f}" if msg.total_cost_usd else "n/a" |
Comment on lines
+106
to
+109
| if session_id is None: | ||
| print("\nError: no session ID captured; cannot resume.") | ||
| return | ||
|
|
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.
Summary
examples/deferred_tool_use.pydemonstrating the human-in-the-loop pattern usingpermissionDecision: \"defer\"in aPreToolUsehookresume=session_idwithbypassPermissionsplan-mode session and informs Claude the command was blockedPostToolUsehook to.claude/settings.jsonso type errors surface automatically after every editHow it works
```
Turn 1: Claude attempts Bash
↓
PreToolUse hook → permissionDecision: "defer"
↓
Session PAUSES ⏸
ResultMessage.deferred_tool_use = {tool, input}
Human inspects and decides:
y → resume=session_id + bypassPermissions → Claude runs command
n → fresh plan-mode session → Claude suggests alternative
```
Changes
examples/deferred_tool_use.py— new example demonstrating deferred tool use / human-in-the-loop.claude/settings.json— addsuv run --extra dev python -m mypy src/ --no-error-summaryas aPostToolUsehook onEdit|Write|MultiEditTest plan
echo "y" | uv run python3 examples/deferred_tool_use.py— allow flow runs the commandecho "n" | uv run python3 examples/deferred_tool_use.py— deny flow suggests alternative without executingsrc/and confirm mypy runs automatically via the new hook🤖 Generated with Claude Code