Repository navigation
feat(hooks): support fire-and-forget async hooks - #329
Open
raymondginger2018-sudo wants to merge 1 commit into
Open
raymondginger2018-sudo wants to merge 1 commit into
raymondginger2018-sudo wants to merge 1 commit into
Conversation
A hook that only observes — an index writer, a telemetry shipper — still ran on the tool-call critical path, and its latency was paid by every tool call. Claude Code's `"async": true` marks such a handler: the engine starts it and moves on, and its verdict never joins the fold (a late block cannot rewrite an event that already happened). `discover_hooks` used to skip async handlers with a warning. It now accepts them and records `Handler.async_`. `HooksEngine` keeps the tasks in a set (so the loop cannot collect a running task), logs a background failure as a warning instead of raising, and exposes `wait_for_background()` for clean shutdown and tests. Tests: three cases in tests/test_hooks.py — discovery accepts the flag; an async handler returns before its command runs and its work lands after `wait_for_background()`; a late block verdict is ignored. Sync-hook behaviour is unchanged.
This branch has not been deployed
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
A hook that only observes — an index writer, a telemetry shipper — still ran on the tool-call critical path, and every tool call paid its latency. Claude Code's
"async": truemarks such a handler: the engine starts it and moves on, and its verdict never joins the fold.Changes
core/harness/hooks/discovery.pyHandler.async_records the flag. Async handlers are no longer skipped with a"async hooks not supported"warning.core/harness/hooks/engine.pyasyncio.create_taskand excluded from the fold; their verdict can never block, rewrite, or inject context.wait_for_background(timeout=…)waits for in-flight tasks (clean shutdown and tests).tests/test_hooks.py— three cases: discovery accepts the flag; the event returns before the command runs and the work lands afterwait_for_background(); a lateblockverdict is ignored.Compatibility
Tests
The three new cases pass. The pre-existing sync-hook tests are unchanged (the same set passes/fails on the base commit on this Windows checkout; CI runs Linux).