perf: cache get_code_context file fetches by git blob SHA - #102
Open
GoodbyePlanet wants to merge 1 commit into
Open
perf: cache get_code_context file fetches by git blob SHA#102GoodbyePlanet wants to merge 1 commit into
GoodbyePlanet wants to merge 1 commit into
Conversation
get_code_context fetched from the GitHub Contents API on every call, opening a fresh httpx client each time. Repeated context reads for the same file in a session hit the API once per call. The indexed file_hash IS the git blob SHA, so it doubles as a content fingerprint: cache decoded contents in a cachetools TTLCache keyed on (repo, blob_sha). A reindexed file gets a new key, so a stale entry becomes unreachable rather than merely expired — the TTL only bounds memory for keys that fall out of use. Sized via CODE_CONTEXT_CACHE_SIZE (128 entries, 0 disables) and CODE_CONTEXT_CACHE_TTL (900s). Entries indexed before file_hash existed bypass the cache rather than key on a mutable path; failed fetches are not cached. Closes #73 Co-Authored-By: Claude Opus 5 (1M context) <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.
Closes #73.
get_code_contextcalledfetch_file_contenton every invocation, which opens a freshhttpx.AsyncClientand hits the GitHub Contents API. Asking for the same file twice in a session meant two API calls.Approach
The indexed
file_hashis the git blob SHA (server/indexer/pipeline.py), so it doubles as a content fingerprint. Decoded file contents are cached in acachetools.TTLCachekeyed on(repo, blob_sha).Because the key is derived from content, a reindexed file gets a new key and the old entry becomes unreachable rather than stale — correctness doesn't depend on the TTL. The TTL only reclaims memory for entries that fall out of use.
Eviction and expiry are
cachetools' rather than hand-rolled;server/tools/file_cache.pyis a thin typed wrapper whose only added behavior is treatingmax_entries <= 0as "disabled" (TTLCache(maxsize=0)raises on insert).CODE_CONTEXT_CACHE_SIZE1280disables caching entirely.CODE_CONTEXT_CACHE_TTL900Edge cases: entries indexed before
file_hashexisted bypass the cache rather than key on a mutable path, and failed fetches are never cached.How to test
Automated (both green in CI):
Manual verification against a real repo — this is the part worth eyeballing, since the tests mock GitHub:
get_code_contexttwice for the same file (via your MCP client, or the tool directly). Confirm the second call returns identical content.usedshould increase by 1 across both calls, not 2:make index-code, then callget_code_contextagain. It must return the new content: the blob SHA changed, so the key changed.CODE_CONTEXT_CACHE_SIZE=0and repeat step 2. Every call should hit GitHub, and nothing should raise.CODE_CONTEXT_CACHE_TTL=5, call twice with a >5s gap, and confirm the second call refetches.Reviewer notes
Two deliberate tradeoffs, both fine for this use but worth a second opinion:
🤖 Generated with Claude Code