feat(hooks): add async_mode support for non-blocking hook execution#1154
Merged
feat(hooks): add async_mode support for non-blocking hook execution#1154
Conversation
Adds async_mode parameter to hooks that allows them to run in background threads without blocking the main execution flow. This is useful for: - Logging and telemetry - Notifications - External service calls - Any non-blocking side effects Based on Claude Code's async hooks feature (add async: true to hook config). Closes ErikBjare/bob#264 Changes: - Add async_mode field to Hook dataclass (default: False) - Update HookRegistry.register to accept async_mode parameter - Modify HookRegistry.trigger to run async hooks in daemon threads - Add _run_async_hook method for background execution - Update register_hook function and all overloads to support async_mode - Add tests for async hook functionality Co-authored-by: Bob <bob@superuserlabs.org>
Contributor
Greptile OverviewGreptile SummaryAdded Key changes:
Confidence Score: 4/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant Client
participant HookRegistry
participant SyncHook
participant AsyncHook
participant Thread
Client->>HookRegistry: trigger(hook_type, args, kwargs)
HookRegistry->>HookRegistry: Separate sync and async hooks
par Async Hooks (Fire-and-Forget)
loop For each async hook
HookRegistry->>Thread: Start daemon thread
Thread->>AsyncHook: _run_async_hook(hook, args, kwargs)
AsyncHook->>AsyncHook: Execute hook function
AsyncHook->>AsyncHook: Log messages (not yielded)
AsyncHook->>AsyncHook: Handle errors (caught, logged)
Note over AsyncHook: Runs in background<br/>without blocking
end
and Sync Hooks (Sequential)
loop For each sync hook
HookRegistry->>SyncHook: hook.func(args, kwargs)
SyncHook-->>HookRegistry: Yield messages
Note over HookRegistry,SyncHook: Check for StopPropagation
alt StopPropagation
SyncHook-->>Client: Stop and return
end
end
HookRegistry-->>Client: Yield all sync messages
end
Note over Thread,AsyncHook: Async hooks continue<br/>in background after<br/>client receives response
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
- Add safety checks for string slicing on msg.content/result.content - Handle empty/non-string content gracefully with preview helper - Add SessionCompleteException handling in async hooks (logged, not propagated) - Add comment explaining daemon=True is intentional for non-blocking side effects
Member
Author
✅ All Greptile Review Comments AddressedPushed fix in commit 6dbee5e: Fixes Applied:
All review threads have individual replies. Ready for re-review! |
Member
|
@greptileai review |
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
Adds
async_modeparameter to hooks that allows them to run in background threads without blocking the main execution flow.This is inspired by Claude Code's async hooks feature:
Use Cases
Async hooks are ideal for:
Changes
async_modefield toHookdataclass (default:False)HookRegistry.registerto acceptasync_modeparameterHookRegistry.triggerto run async hooks in daemon threads_run_async_hookmethod for background execution with error handlingregister_hookfunction and all type overloads to supportasync_modeUsage
Testing
Added tests covering:
async_modeis properly stored in hook registrationCloses ErikBjare/bob#264