Skip to content

vmcp: address RestorableCache capacity limit and sentinel pattern complexity (follow-up to #4464) #4494

Description

@yrobla

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:

  • Store and CompareAndSwap accept any, weakening type safety
  • Peek exposes raw any values, requiring callers to type-assert
  • The sentinel type leaks into test utilities (testSentinel, Peek assertions)

Preferred fix: eliminate the sentinel entirely by pushing the no-resurrection invariant into the storage layer:

  1. Replace DataStorage.Upsert (or add an Update) with a conditional write — SET XX in Redis semantics — that returns ErrSessionNotFound if the key no longer exists. RestoreSession would call Update rather than Upsert after reconstructing the session, so a deleted key causes the restore to fail cleanly rather than re-inserting.
  2. Make RestorableCache fully typed: remove Store(any), Peek, and CompareAndSwap(any, any); all cache writes go through the typed load path or an explicit typed Set(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 (or ValidatingCache) 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, untyped Store/CompareAndSwap are removed
  • DataStorage exposes a conditional-write operation (Update / SET XX) and RestoreSession uses it
  • Cache is renamed ValidatingCache and lives in pkg/cache/
  • All existing RestorableCache tests pass under the new design (sentinel-specific tests replaced by storage-layer tests)

Activity

  1. added
    enhancement
    go
    vmcp
    on Apr 2, 2026
  2. self-assigned this
    on Apr 8, 2026
  3. added 6 commits that reference this issue on Apr 8, 2026
    c98dd3f
    0837d17
    d49605b
    36eeed9
    49e8631
    653fe28
  4. added a commit that references this issue on Apr 10, 2026
    1a6287b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementgovmcp

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions