perf(corpus): cache the SHA-256 version fingerprint#7
Merged
Conversation
``DirectoryCorpus.version`` hashed every entry on every call. Spotted in this morning's code-review (#code-review-2026-05-07): the version property is the cache key for attune-gui's /api/rag routes — hashing the full corpus on every API request is the kind of N-per-request work that turns a 5ms route into a 50ms route once a corpus has hundreds of templates. Cache the fingerprint on the instance, invalidate inside ``_ensure_loaded`` whenever the entries are rebuilt (which already happens on file changes when ``cache=False``, and is a no-op when ``cache=True`` after the first load). Tests: - ``test_version_cached_after_first_computation`` — patches ``hashlib.sha256`` and asserts it's called exactly once across three ``version`` reads. - ``test_version_invalidated_when_corpus_reloaded`` — sets a sentinel, forces a rebuild, confirms the recomputed hash overwrites the sentinel. 300 passed, 3 xpassed. 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.
Spotted in this morning's code-review.
DirectoryCorpus.versionre-hashed every entry on every call. The property is the cache key for attune-gui's /api/rag routes, so the cost compounds with template count and request rate.Cache the result on the instance, invalidate inside
_ensure_loadedwhenever entries are rebuilt. Two new tests cover the cache (patcheshashlib.sha256, asserts one call across three reads) and the invalidation (sentinel + forced rebuild).300 passed, 3 xpassed.
🤖 Generated with Claude Code