Repository navigation
Description
Context
Follow-up to #4464 (session manager DataStorage migration). The architecture is solid but two issues should be addressed before the cache is considered production-ready.
Blocker 1: Cache capacity limit
RestorableCache has no size bound or background eviction sweep. Idle sessions (e.g. clients that disconnect without a clean MCP shutdown) will remain in the node-local cache until their Redis TTL expires — holding references to backend connections, routing tables, and token sources indefinitely.
Required: add at minimum a capacity limit (LRU or similar) so the cache cannot grow without bound. A background sweep keyed on entry age would also bound the leak window for truly idle sessions.
Related: capacity/LRU eviction was intentionally deferred in the initial implementation (see cache.go TODO comment).
Blocker 2: Sentinel pattern complexity
The current terminatedSentinel / Peek / untyped Store(any) / CompareAndSwap(any, any) pattern was introduced to prevent session resurrection after DeleteSession races with a concurrent RestoreSession. It works correctly but adds surface area that reviewers flagged:
StoreandCompareAndSwapacceptany, weakening type safetyPeekexposes rawanyvalues, requiring callers to type-assert- The sentinel type leaks into test utilities (
testSentinel,Peekassertions)
Preferred fix: eliminate the sentinel entirely by pushing the no-resurrection invariant into the storage layer:
- Replace
DataStorage.Upsert(or add anUpdate) with a conditional write —SET XXin Redis semantics — that returnsErrSessionNotFoundif the key no longer exists.RestoreSessionwould callUpdaterather thanUpsertafter reconstructing the session, so a deleted key causes the restore to fail cleanly rather than re-inserting. - Make
RestorableCachefully typed: removeStore(any),Peek, andCompareAndSwap(any, any); all cache writes go through the typedloadpath or an explicit typedSet(K, V).
With this design, a DeleteSession → RestoreSession race is resolved by the storage layer (the conditional write fails), and the cache never needs to distinguish sentinel from live session.
Additional clean-up
- Rename
RestorableCache→ValidatingCache(the defining characteristic is that every cache hit is validated for liveness, not that entries are restorable). - Move to
pkg/cache/so it can be reused by other subsystems without importing a vmcp-internal package.
Acceptance criteria
-
RestorableCache(orValidatingCache) has a configurable capacity limit; entries beyond the limit are evicted (LRU or equivalent) - Optional: background sweep evicts entries older than a configurable max age without touching storage
-
terminatedSentinel,Peek, untypedStore/CompareAndSwapare removed -
DataStorageexposes a conditional-write operation (Update/SET XX) andRestoreSessionuses it - Cache is renamed
ValidatingCacheand lives inpkg/cache/ - All existing
RestorableCachetests pass under the new design (sentinel-specific tests replaced by storage-layer tests)
Activity
Add LRU capacity to ValidatingCache, remove sentinel pattern, add sto…
Add LRU capacity to ValidatingCache, remove sentinel pattern, add sto…
Add LRU capacity to ValidatingCache, remove sentinel pattern, add sto…
Add LRU capacity to ValidatingCache, remove sentinel pattern, add sto…
Add LRU capacity to ValidatingCache, remove sentinel pattern, add sto…
Add LRU capacity to ValidatingCache, remove sentinel pattern, add sto…
Add LRU capacity to ValidatingCache, remove sentinel pattern, add sto…