Skip to content

Migrate session manager to DataStorage and add RestoreSession - #4464

Merged
yrobla merged 2 commits into
mainfrom
issue-4420-sm-migration
Apr 2, 2026
Merged

yrobla merged 2 commits into
mainfrom
issue-4420-sm-migration

Conversation

@yrobla

@yrobla yrobla commented Mar 31, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Replace the *transportsession.Manager dependency in sessionmanager.Manager with the DataStorage interface (introduced in split PR 1), and simultaneously migrate discovery.Middleware from *transportsession.Manager to a MultiSessionGetter interface.

These two changes are intentionally landed together. Phase 2 (DataStorage migration) and phase 3 (middleware decoupling) cannot be split cleanly:

  • Phase 2 alone is broken: sessions generated by the new sessionmanager.Manager are stored in DataStorage + RestorableCache, but never written into *transportsession.Manager. discovery.Middleware would call sessionManager.Get(sessionID) and find nothing, returning HTTP 401 for every subsequent request. A temporary bridge shim could paper over this, but it would only exist to be deleted in phase 3 — net negative for reviewers.
  • Phase 3 alone is impossible: MultiSessionGetter.GetMultiSession() is backed by vmcpSessionMgr, which only exists after the phase 2 rewrite.
  • The 401→404 behaviour change is a direct consequence of removing the stale sessionManager.Get() check in the middleware, not an independent feature. It belongs with the PR that removes that check.

Key changes:

  • Add RestoreSession() to MultiSessionFactory (and decorating/mock impls); refactor makeSession → makeBaseSession so RestoreSession can reconstruct a live session from stored metadata without a bearer token
  • Add RestoreHijackPrevention() in pkg/vmcp/session/internal/security to recreate the hijack-prevention decorator from stored hash/salt
  • Rewrite sessionmanager.Manager to use DataStorage: node-local RestorableCache[string, MultiSession] for hot-path lookups with singleflight-deduplicated RestoreSession on cache miss and lazy liveness validation via storage.Load() on cache hits
  • Extract RestorableCache[K,V] generic type (sync.Map + singleflight + check/evict callbacks) into cache.go
  • Replace *transportsession.Manager in discovery.Middleware with a MultiSessionGetter interface backed by vmcpSessionMgr
  • Behavior change: unknown or expired sessions in the middleware no longer return HTTP 401 — the middleware passes through and the SDK responds with 404 via Validate()
  • Wire server.go to create LocalSessionDataStorage, pass it to sessionmanager.New, and route the discovery middleware through vmcpSessionMgr

Fixes #4220

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Dependency update
  • Documentation
  • Other (describe):

Test plan

  • Unit tests (task test)
  • E2E tests (task test-e2e)
  • Linting (task lint-fix)
  • Manual testing (describe below)

Does this introduce a user-facing change?

Yes. HTTP clients relying on 401 Unauthorized for unknown/expired session IDs on subsequent requests will now receive 404 Not Found (from the MCP SDK's session Validate()). This is semantically more correct — "session not found" is not an authorization failure.

Special notes for reviewers

Large PR Justification

Phases 2 and 3 of the DataStorage migration are combined here because they are not independently deployable: phase 2 alone leaves discovery broken without a throwaway bridge shim, and phase 3 depends on the session manager that phase 2 introduces. See the Summary above for the full reasoning.

@github-actions github-actions Bot added the size/XL Extra large PR: 1000+ lines changed label Mar 31, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Large PR Detected

This PR exceeds 1000 lines of changes and requires justification before it can be reviewed.

How to unblock this PR:

Add a section to your PR description with the following format:

## Large PR Justification

[Explain why this PR must be large, such as:]
- Generated code that cannot be split
- Large refactoring that must be atomic
- Multiple related changes that would break if separated
- Migration or data transformation

Alternative:

Consider splitting this PR into smaller, focused changes (< 1000 lines each) for easier review and reduced risk.

See our Contributing Guidelines for more details.


This review will be automatically dismissed once you add the justification section.

@yrobla
yrobla requested a review from Copilot March 31, 2026 14:58
@github-actions github-actions Bot added size/XL Extra large PR: 1000+ lines changed and removed size/XL Extra large PR: 1000+ lines changed labels Mar 31, 2026
@github-actions
github-actions Bot dismissed their stale review March 31, 2026 15:00

Large PR justification has been provided. Thank you!

@github-actions

Copy link
Copy Markdown
Contributor

✅ Large PR justification has been provided. The size review has been dismissed and this PR can now proceed with normal review.

@codecov

codecov Bot commented Mar 31, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 71.02564% with 113 lines in your changes missing coverage. Please review.
✅ Project coverage is 69.56%. Comparing base (8b7c40e) to head (4b28c8d).
⚠️ Report is 12 commits behind head on main.

Files with missing lines Patch % Lines
pkg/vmcp/server/sessionmanager/session_manager.go 72.00% 38 Missing and 18 partials ⚠️
pkg/vmcp/session/factory.go 60.71% 18 Missing and 4 partials ⚠️
pkg/vmcp/session/decorating_factory.go 0.00% 17 Missing ⚠️
pkg/vmcp/session/internal/security/security.go 75.86% 5 Missing and 2 partials ⚠️
pkg/vmcp/server/server.go 80.00% 3 Missing and 3 partials ⚠️
pkg/vmcp/server/sessionmanager/cache.go 91.30% 3 Missing and 1 partial ⚠️
pkg/vmcp/discovery/middleware.go 91.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4464      +/-   ##
==========================================
- Coverage   69.60%   69.56%   -0.04%     
==========================================
  Files         494      497       +3     
  Lines       50445    50802     +357     
==========================================
+ Hits        35111    35340     +229     
- Misses      12638    12747     +109     
- Partials     2696     2715      +19     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors vMCP session lifecycle management to decouple live MultiSession state from persisted session metadata by migrating sessionmanager.Manager to the new transportsession.DataStorage interface, and adds a RestoreSession flow to reconstruct sessions on cache misses (e.g., after restart or cross-pod routing when using Redis-backed metadata).

Changes:

  • Replace *transportsession.Manager usage in sessionmanager.Manager with DataStorage, introducing a node-local sync.Map cache plus singleflight-deduped restore on cache miss and a background eviction loop driven by storage.Exists().
  • Extend MultiSessionFactory with RestoreSession() and add security.RestoreHijackPrevention() to rebuild the hijack-prevention decorator from persisted token hash/salt.
  • Update discovery middleware to depend on a MultiSessionGetter (backed by the session manager) and allow unknown sessions to fall through so the SDK can return 404 via Validate() (tests updated accordingly).

Reviewed changes

Copilot reviewed 17 out of 17 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
pkg/vmcp/session/mocks/mock_factory.go Updates gomock factory to include RestoreSession.
pkg/vmcp/session/internal/security/security.go Adds RestoreHijackPrevention to rebuild decorator from stored hash/salt.
pkg/vmcp/session/factory.go Adds MultiSessionFactory.RestoreSession and refactors makeSession → makeBaseSession.
pkg/vmcp/session/decorating_factory.go Adds decorator-aware RestoreSession implementation.
pkg/vmcp/server/testfactory_test.go Updates minimal test factory to satisfy new interface.
pkg/vmcp/server/telemetry_integration_test.go Updates test factory to support RestoreSession.
pkg/vmcp/server/sessionmanager/session_manager.go Major rewrite: DataStorage + node-local cache + restore + eviction loop + adapted prompts.
pkg/vmcp/server/sessionmanager/session_manager_test.go Migrates tests to DataStorage and new behaviors.
pkg/vmcp/server/session_management_realbackend_integration_test.go Updates expected status code after termination (401 → 404).
pkg/vmcp/server/session_management_integration_test.go Updates expected status code after termination (401 → 404).
pkg/vmcp/server/server.go Wires server to create LocalSessionDataStorage and pass it to session manager; updates shutdown/handler wiring.
pkg/vmcp/discovery/middleware.go Switches dependency to MultiSessionGetter; removes 401-on-missing-session behavior.
pkg/vmcp/discovery/middleware_test.go Reworks tests to use a stub MultiSessionGetter.
pkg/transport/session/storage.go Adds Exists() to the session-object Storage interface.
pkg/transport/session/storage_redis.go Implements Storage.Exists() via Redis EXISTS (no TTL refresh).
pkg/transport/session/storage_local.go Implements Storage.Exists() without refreshing last-access timestamp.
pkg/transport/session/manager.go Exposes TTL() and adds Manager.Exists() wrapper.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/vmcp/server/server.go Resolved
Comment thread pkg/vmcp/session/factory.go Resolved
@yrobla
yrobla force-pushed the issue-4420-sm-migration branch from 758fe0c to c1d0cbc Compare March 31, 2026 15:18
@yrobla
yrobla requested a review from Copilot March 31, 2026 15:18
@github-actions github-actions Bot added size/XL Extra large PR: 1000+ lines changed and removed size/XL Extra large PR: 1000+ lines changed labels Mar 31, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 17 out of 17 changed files in this pull request and generated 5 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/vmcp/server/server.go Resolved
@yrobla
yrobla force-pushed the issue-4420-sm-migration branch from c1d0cbc to 31f14c9 Compare March 31, 2026 15:38
@github-actions github-actions Bot added size/XL Extra large PR: 1000+ lines changed and removed size/XL Extra large PR: 1000+ lines changed labels Mar 31, 2026
@yrobla
yrobla requested a review from Copilot March 31, 2026 15:47

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 17 out of 17 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@yrobla
yrobla force-pushed the issue-4420-sm-migration branch from 31f14c9 to bcfbcd7 Compare March 31, 2026 16:05
@github-actions github-actions Bot removed the size/XL Extra large PR: 1000+ lines changed label Mar 31, 2026
@github-actions github-actions Bot added size/XL Extra large PR: 1000+ lines changed and removed size/XL Extra large PR: 1000+ lines changed labels Apr 1, 2026
@yrobla
yrobla force-pushed the issue-4420-sm-migration branch from d133cc2 to e033710 Compare April 1, 2026 09:46
@yrobla
yrobla requested a review from Copilot April 1, 2026 09:47
@github-actions github-actions Bot added size/XL Extra large PR: 1000+ lines changed and removed size/XL Extra large PR: 1000+ lines changed labels Apr 1, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 19 out of 19 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/vmcp/server/sessionmanager/cache.go Resolved
Comment thread pkg/vmcp/session/factory.go Resolved
@yrobla
yrobla force-pushed the issue-4420-sm-migration branch from e033710 to 8796f90 Compare April 1, 2026 10:45
@github-actions github-actions Bot added size/XL Extra large PR: 1000+ lines changed and removed size/XL Extra large PR: 1000+ lines changed labels Apr 1, 2026
@yrobla
yrobla requested a review from Copilot April 1, 2026 10:48
@github-actions github-actions Bot added size/XL Extra large PR: 1000+ lines changed and removed size/XL Extra large PR: 1000+ lines changed labels Apr 1, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 15 out of 15 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/vmcp/server/server.go Resolved
@github-actions github-actions Bot added size/XL Extra large PR: 1000+ lines changed and removed size/XL Extra large PR: 1000+ lines changed labels Apr 1, 2026
@yrobla
yrobla force-pushed the issue-4420-sm-migration branch from 8796f90 to e3f29e4 Compare April 1, 2026 12:53

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/vmcp/server/server.go Resolved
Comment thread pkg/vmcp/session/factory.go Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 17 out of 17 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/vmcp/session/factory.go Resolved
Comment thread pkg/vmcp/server/server.go Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 19 out of 19 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/vmcp/server/sessionmanager/cache.go Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 19 out of 19 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@jerm-dro jerm-dro left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Architecture is solid — the storage split, RestorableCache extraction, middleware decoupling, and RestoreSession implementation are all well-designed. The sentinel pattern is correct but adds complexity that should be addressed.

Two blockers flagged as inline comments, both acceptable as fast follows:

  1. Cache capacity limit: RestorableCache has no size bound or background eviction. Idle sessions leak backend connections indefinitely after their Redis TTL expires. Needs at minimum a capacity limit.

  2. Sentinel simplification: The terminatedSentinel / Peek / untyped Store(any) / CompareAndSwap(any, any) pattern can be eliminated by (a) making the cache fully typed with a check-on-every-access invariant, and (b) replacing DataStorage.Upsert with Update (SET XX) that returns ErrSessionNotFound after delete. This pushes the no-resurrection invariant into the storage layer where it belongs.

Additional suggestions: rename RestorableCache → ValidatingCache, move to pkg/cache/.

Please track the two blockers and link the follow-up issues here.

Comment thread pkg/vmcp/server/sessionmanager/cache.go Resolved
Comment thread pkg/vmcp/server/sessionmanager/cache.go Resolved
Comment thread pkg/vmcp/server/sessionmanager/cache.go Resolved
Comment thread pkg/vmcp/server/sessionmanager/cache.go Resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/XL Extra large PR: 1000+ lines changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Write unit tests for horizontal scaling operator behaviors (THV-0047)

4 participants