Cache rationale cards in SQLite#12
Open
cvetty wants to merge 1 commit into
Open
Conversation
`whygraph_rationale_brief` used to call the LLM on every invocation. Add a SQLite-backed cache keyed by `(path, line_start, line_end, provider, model)` and invalidated by the sha256 fingerprint of the sorted commit SHAs from `collect_evidence`. New commits landing on the blamed lines change the fingerprint and force a regeneration; the stale row is overwritten by the next store. The cache PK uses the *configured* provider/model so the lookup is deterministic before the LLM call. `config.model = None` is translated to the literal `"default"` token; the LLM-reported provider/model identity persists separately in `actual_provider` / `actual_model` so rows keyed under `"default"` keep their provenance. The MCP response now carries a `cached_at` ISO-8601 UTC timestamp on both fresh and cached returns so agents can surface staleness. - New SQLModel `RationaleCache` (`db/models/rationale_cache.py`), registered in `db/models/__init__.py`. - Alembic migration `b4de974b9f54_add_rationale_cache.py` (hand-written; mirrors the initial-schema migration's shape). - Cache helpers (`mcp/rationale_cache.py`): `lookup_cached`, `store_cached`, evidence fingerprint, `session.merge()` upsert. - Tests: `test_mcp_rationale_cache.py` covers second-call-cached, new-commit-invalidates, stale-fingerprint miss, and fingerprint order-independence. - `test_db_plumbing.py` table-set updated.
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
whygraph_rationale_briefused to call the LLM on every invocation. This PR adds a SQLite-backed cache keyed by(path, line_start, line_end, provider, model)and invalidated by a sha256 fingerprint of the sorted commit SHAs returned bycollect_evidence. New commits landing on the blamed lines change the fingerprint and force a regeneration; the stale row is overwritten by the next store.The cache PK uses the configured provider/model so the lookup is deterministic before the LLM call returns.
config.model = Noneis translated to the literal"default"token; the LLM-reported identity persists separately inactual_provider/actual_modelso rows keyed under"default"keep their provenance. The MCP response now carries acached_atISO-8601 UTC timestamp on both fresh and cached returns so agents can surface staleness.Changes
RationaleCacheSQLModel (db/models/rationale_cache.py), registered indb/models/__init__.py.b4de974b9f54_add_rationale_cache.py, hand-written to mirror the initial-schema migration's shape.mcp/rationale_cache.py—lookup_cached,store_cached, evidence fingerprint,session.merge()upsert.mcp/rationale.pyconsults the cache before generating and stores on miss; response shape extracted into_format_response.test_mcp_rationale_cache.pycover second-call-cached, new-commit-invalidates, stale-fingerprint miss, and fingerprint order-independence.