Repository navigation
Share optimizer embeddings across sessions and replicas - #7008
Conversation
The optimizer reuses a stored vector only when it is already committed in the same process. Builds that start together on a cold store, as the first sessions after a rollout do, each embed the full catalogue, and every replica embeds it again after each restart. On a two-replica vMCP with about 400 tools, the first session after a rollout took 52 seconds to set up, mostly duplicate embedding batches queued behind each other. Builds that need new vectors now take turns on a store: one that waited looks again and reuses what the build before it committed. With Redis session storage, vectors embedded under a verified model identity are also written to that Redis under the session key prefix with a 7-day TTL, and a replica reads them before embedding. A Redis error after startup is logged and costs only reuse. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Aron <[email protected]>
|
The one failing test is |
|
More production data from today, on the same two-replica vMCP right after a rollout restarted both pods:
With this PR, the three builds would take turns and the second and third would reuse the first one's rows. The second replica would also read the vectors the first replica had already published, instead of embedding the catalogue at all. |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #7008 +/- ##
==========================================
- Coverage 82.09% 82.06% -0.03%
==========================================
Files 927 928 +1
Lines 93959 94109 +150
==========================================
+ Hits 77132 77235 +103
- Misses 16822 16869 +47
Partials 5 5 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
jhrozek
left a comment
There was a problem hiding this comment.
Thanks for this, the trace numbers in the description make the case well and the reuse logic is careful.
Two things I'd like to see before merge, both small: bounding the turn wait (sqlite_store.go:401) and keeping the Redis calls out of the turn (sqlite_store.go:353). The session keyspace one (embedding_cache.go:50) is cheap to close now, before the key layout is out in real Redis instances. The rest are docs and nits.
One thing outside the diff: the comment at serve_optimizer.go:95 still says a rebuild costs an embedding round-trip per tool, which hasn't been true since #5847.
CI is green after re-running a flaky awssts test.
|
Sorry it took us so long to get to this one, and thanks for your patience. The review above should give you something concrete to work with, and I'm happy to talk through any of it if something doesn't make sense or you see it differently. |
Serve now assembles the Server before the vMCP session manager so the cache sweep can drop health-resync registrations. The shared-embeddings config is resolved right before sessionmanager.New on that new path. Signed-off-by: Aron Gates <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
A build that needs new vectors waited for the embedding turn with no bound but its own context, which has no deadline on the Serve path. With a hung embedding backend each holder spent the full request timeout before handing over, so the last of N cold builds failed after about N timeouts instead of one. A build now waits at most 30 seconds for the turn and then embeds without it, as every build did before the turn existed. It tries once per build. Signed-off-by: Aron Gates <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
A build published its vectors to the shared cache while it still held the embedding turn, and read the cache again after taking the turn. A slow Redis, rather than a down one, then held up every other cold build on the replica for the Redis timeouts. The vectors are now published after the rows commit and the turn is given back, so a build whose commit fails publishes nothing. The look after the turn reads only the local store, which is where the previous holder's rows are. A build now calls Redis at most twice, never under the turn. Signed-off-by: Aron Gates <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
The shared optimizer embeddings live under the session key prefix, so an ACL scoped to that prefix keeps covering them. A client could name one as its Mcp-Session-Id: the ownership lookup read the vector, failed to decode it, and answered 503 with a WARN. Any authenticated caller could raise WARNs at request rate and tell which keys exist. Redis session storage now marks a value that is not session metadata with ErrUndecodableSessionData, and the vMCP ownership lookup answers it as a missing session: the same 404, logged at DEBUG. Load and Terminate share the decoder and keep treating it as an error. Signed-off-by: Aron Gates <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
A stored vector with NaN or Inf components, which only a replaced shared value can carry, gives a NaN cosine distance. NaN compares false against the distance threshold, so the tool matched every query and the sort got an inconsistent comparator. Semantic search now skips such a distance. Signed-off-by: Aron Gates <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
A plain GET pulled a shared value into the replica whatever its size, so a planted 100 MB value reached every replica before the width check rejected it. Lookups now use GETRANGE and read at most 64 KiB plus one byte, which is enough to reject anything longer. GETRANGE reads an absent key as empty, so empty values count as absent. The connect error is no longer wrapped twice on its way to the caller. Signed-off-by: Aron Gates <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
FactoryConfig.OptimizerOptions reach only an optimizer built from OptimizerConfig. Set together with a pre-built OptimizerFactory, or with no optimizer, they were silently dropped. New now rejects both, as it already rejects an optimizer without AdvertiseFromCore. Signed-off-by: Aron Gates <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
The factory test only checked that the shared cache connected, so it passed even if the store never got the cache. It now builds an optimizer against a stub TEI that reports a model id and checks that each tool's vector lands in Redis under the session prefix. Signed-off-by: Aron Gates <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
The scalability doc said the shared embedding TTL counts from the last write, which reads like a sliding window, and sized each vector by its value alone. It now states the fixed 7-day TTL, per-key overhead, the width rule, which settings split the keyspace, the eviction behavior, the connection count next to rate limiting, the bounded turn wait, and that a replaced vector a replica already read stays in use there. The Serve comment that a rebuild costs an embedding round-trip per tool predates embedding reuse and now says what a rebuild embeds. Signed-off-by: Aron Gates <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Sorry for the slow turnaround on this one. I merged main in (33c1d60). The one real conflict was Serve now building the Server before the session manager, so the shared-embeddings config now resolves right before Each review point has its own commit and a reply on its thread. The |
Summary
The optimizer embeds a tool only when no stored vector matches its content key (#5847). That reuse sees only rows already committed in the same process, so two cases still embed one catalogue several times:
Measured on a two-replica vMCP serving about 400 tools through TEI, the first session after a rollout took 52 s to set up.
initialize(26 s) andtools/list(25 s) landed on different replicas, each building its own index. A client-abandonedinitializekept embedding at the same time, so identical batches reached TEI two or three times and queued behind each other: single batches took up to 14 s against 1 to 3 s uncontended. Aggregation took about 3 s, and warm reconnects take 0.3 to 2 s.What changed:
ErrUndecodableSessionData, and the vMCP ownership lookup answers a session ID that names one with the same 404 as a missing session.Type of change
Test plan
task test)task test-e2e)task lint-fix)task testpasses for every vMCP, optimizer and session package. Three tests fail on my machine with or without this change, the same three on a fresh checkout of main:TestDetectProjectRoot(pkg/client),TestResolve(pkg/skills/identitytoken) andTestTransparentProxy_HealthCheckFailureWithNilCallback(pkg/transport/proxy/transparent).task lintis clean, andtask genandtask docsleave the tree unchanged.New tests cover cold concurrent builds, the bounded turn wait and a caller that gives up while waiting, a slow publish that must not hold the turn, the look after the turn staying off Redis, cross-replica reuse, unverified vectors staying local, unusable and oversized shared values, NaN and Inf vectors in search, a failing Redis, turn release after a failed build, a session ID naming an embedding key, the factory option checks, the factory publishing through a stub TEI, and the Serve wiring.
Changes
pkg/vmcp/optimizer/internal/toolstore/embedding_cache.goEmbeddingCacheand its Redis implementation, with capped readspkg/vmcp/optimizer/internal/toolstore/sqlite_store.goWithEmbeddingCachepkg/vmcp/optimizer/optimizer.goWithSharedEmbeddingsfactory optionpkg/vmcp/server/sessionmanager/factory.go,session_manager.goFactoryConfig.OptimizerOptions, rejected where nothing would use them;LookupOwneranswers non-session keys as missingpkg/transport/session/session_data_storage_redis.goErrUndecodableSessionDatapkg/vmcp/server/serve_optimizer.go,serve.gopkg/vmcp/server/server.gosessionRedisConfigfrombuildSessionDataStorage, unchangeddocs/arch/13-vmcp-scalability.mdDoes this introduce a user-facing change?
Yes, for a vMCP that runs the optimizer with an embedding service.
With any session storage:
With Redis session storage as well:
<keyPrefix>optimizer:embedding:<sha256>keys. Each is a raw float32 vector with a fixed 7-day TTL, about 1.7 KB at 384 dimensions including key and overhead. Each pod opens one more Redis client for them, mostly idle. A vMCP that cannot connect that client fails at startup, right after its session storage connected to the same Redis. A request whoseMcp-Session-Idnames one of these keys gets 404, as for any unknown session.There is no CRD or config change.
Implementation plan
Approved implementation plan
EmbeddingCacheinterface with a Redis implementation. Pipeline single-key reads and SETs so Redis Cluster works, with a TTL on every key and a cap on bytes read.sessionmanager.FactoryConfig. Serve sets it from the session-storage Redis config, so no new config surface is needed.Special notes for reviewers
find_toolranking. Results are still limited to the caller's tools, andcall_toolis still admission-checked. A replaced vector a replica already read stays in its local store until the tool text or model changes or the pod restarts. The scalability doc puts this next to the existing Redis access-control guidance. Keys sit under the session key prefix, so an ACL scoped to that prefix keeps working.Mcp-Session-Idinto session storage:classifyingHandleranddispatchModerncall the core with the caller's identity only, so the non-session-key fix concerns the Legacy ownership lookup.🤖 Generated with Claude Code