Keep optimizer store's in-memory database alive - #6157
Open
aponcedeleonch wants to merge 1 commit into
Open
Conversation
aponcedeleonch
requested review from
ChrisJBurns,
JAORMX,
amirejaz,
jerm-dro,
jhrozek and
tgrunnagle
as code owners
July 31, 2026 10:41
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #6157 +/- ##
==========================================
- Coverage 72.56% 72.55% -0.01%
==========================================
Files 736 736
Lines 76331 76341 +10
==========================================
Hits 55391 55391
- Misses 17019 17024 +5
- Partials 3921 3926 +5 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
3 tasks
The optimizer's tool store runs on a shared-cache in-memory SQLite database, which exists only while at least one connection to it is open. When the database/sql pool drops to zero connections SQLite discards the whole database, and nothing recreates it because the schema is executed only in the constructor. Every later session build and find_tool then fails with "no such table: llm_capabilities" until the pod restarts, while the process stays healthy and /readyz stays green. The pool does reach zero. modernc.org/sqlite implements context cancellation with sqlite3_interrupt and reports an interrupted connection as unusable from conn.IsValid, so database/sql discards a connection whose statement was canceled rather than returning it to the pool. Because the store is touched only on session build and find_tool, a single pooled connection is the normal steady state, so one canceled request is enough to destroy the database for the life of the process. An embedding-service outage makes this far more likely, since slow and hanging calls cause clients to give up mid-statement, but the fault is not specific to embeddings. Pin a dedicated connection for the store's lifetime. It is acquired with a background context and never used to run statements, so it can never be interrupted and never discarded. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
aponcedeleonch
force-pushed
the
fix-optimizer-store-schema-loss
branch
from
July 31, 2026 12:55
a25e39b to
0241d08
Compare
8 tasks
jerm-dro
approved these changes
Jul 31, 2026
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
The optimizer's tool store runs on a shared-cache in-memory SQLite database, which exists only while at least one connection to it is open. When the
database/sqlpool drops to zero connections SQLite discards the whole database, and nothing recreates it because the schema is executed only in the constructor. Every later session build andfind_toolthen fails withno such table: llm_capabilitiesuntil the pod restarts, while the process stays healthy and/readyzstays green.The pool does reach zero.
modernc.org/sqliteimplements context cancellation withsqlite3_interruptand reports an interrupted connection as unusable fromconn.IsValid, sodatabase/sqldiscards a connection whose statement was canceled rather than returning it to the pool. Because the store is touched only on session build andfind_tool, a single pooled connection is the normal steady state for a quiet pod, so one canceled request is enough to destroy the database for the life of the process. An embedding-service outage makes this much more likely, since slow and hanging calls cause clients to give up mid-statement, but the fault is not specific to embeddings: a well-timed client disconnect does the same thing with a perfectly healthy embedder.What changed:
sqliteToolStorenow pins a dedicatedkeepAliveconnection for its whole lifetime, acquired with a background context and never used to run statements, so it can never be interrupted and never discarded.Closereleases that connection ahead of the pool and toleratessql.ErrConnDone, preserving the documented "safe to call Close multiple times" contract ontypes.ToolStore.Fixes #5889
Type of change
Test plan
task test)task test-e2e)task lint-fix)The new test was verified to actually catch the regression, not just pass: with the keep-alive connection released immediately after acquisition, it fails with the exact error from the issue.
Does this introduce a user-facing change?
Yes. A vMCP pod running with the optimizer enabled could permanently lose its tool index while continuing to report itself healthy, with every
find_toolcall and every new session failing until the pod was deleted. That no longer happens.Special notes for reviewers
Two alternatives I did not take:
CREATE TABLE IF NOT EXISTSon each write would let new session builds repopulate the store, but sessions that already exist only ever callSearch, so they would silently get zero results instead of an error. Fixing the connection lifetime keeps the loud failure.Scope notes: the store is per-pod in-memory state, so there is nothing to coordinate across replicas. With multiple pods only the poisoned one misbehaves, which is part of why this was hard to spot. The change is also inert when the optimizer is disabled, since
resolveOptimizernever builds a store in that case.Pre-existing failures unrelated to this PR, which reproduce on a clean
mainat 33e06f4:task test:TestParseGitReference_RefAndSubdir/git://github.com/org/repoand threeTestValidateOCIRegistryHostsubtests, all inpkg/plugins/pluginsvc. That package has no dependency on the optimizer. All other packages pass, including./pkg/vmcp/optimizer/...under-race.task lint-fix: onegosecG115 finding incmd/thv/app/upgrade.go:204, a file this branch does not touch.Generated with Claude Code