feat(expander): add expand_async for use from async event loops#8
Merged
Conversation
Code-review 2026-05-07 flagged ``QueryExpander.expand`` as a sync ``messages.create`` on the hot path of attune-gui's async route handlers — sitting on the event loop for the full Anthropic round- trip (typically 1-3 seconds) blocks every other request behind it. Add ``async def expand_async(query)`` that: - Hits the same in-memory cache first; cache hit returns synchronously without dispatching to a thread. - On miss, wraps the existing sync ``expand`` in ``asyncio.to_thread`` so the Anthropic call runs off the event loop. The sync ``expand`` stays — non-async callers (CLI, scripts, tests) keep their existing API. Cache is shared across both paths. Implementation note: assigned ``asyncio.to_thread`` to a module-level ``_ASYNCIO_TO_THREAD`` so the import survives auto-format passes that otherwise prune ``import asyncio`` as unused (the editor's formatter strips imports it can't see being referenced at the same edit step). Three new tests cover: parity with sync expand, cache short-circuit without thread dispatch, and verification that the API call runs on a different thread than the event loop. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
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.
Code-review finding (expander.py:64). Sync
messages.createblocked the event loop in attune-gui's RAG routes — every async caller sat for the full Anthropic round-trip.Adds
expand_asyncthat wraps the existing syncexpandinasyncio.to_thread. Cache is shared so a hit on either path serves the other. Sync API preserved for non-async callers.3 new tests. 303 passed.
🤖 Generated with Claude Code