fix(mcp): resolve active session from store for mem_save instead of manual-save fallback#449
Merged
Merged
Conversation
…anual-save fallback (#386)
There was a problem hiding this comment.
Pull request overview
This PR fixes MCP write tools (mem_save, prompt/summary/passive capture) defaulting to manual-save-{project} even when a real UUID session is active in the shared SQLite store, by resolving the most-recent active session directly from persisted sessions rows.
Changes:
- Add
Store.MostRecentActiveSession(project)to select the latest un-ended, non-manual-save%session for a project. - Wire a new
resolveFallbackSessionIDhelper into MCP write-tool fallback paths so implicit writes prefer the persisted active UUID session. - Add store- and MCP-level tests covering active-session resolution, ended-session exclusion, recency ordering, and preserved manual-save fallback.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| internal/store/store.go | Adds MostRecentActiveSession query for cross-process active session resolution. |
| internal/store/store_test.go | Adds unit tests validating active-session selection rules and project scoping. |
| internal/mcp/mcp.go | Uses store-backed fallback session resolution for write tools when session_id is omitted. |
| internal/mcp/mcp_test.go | Adds regression tests reproducing issue #386 and verifying new fallback behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+2710
to
+2714
| func resolveFallbackSessionID(s *store.Store, project string) string { | ||
| if s != nil { | ||
| if id, ok, err := s.MostRecentActiveSession(project); err == nil && ok { | ||
| return id | ||
| } |
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
Closes #386 —
mem_save(and prompt/summary/passive saves) without an explicitsession_idfell back tomanual-save-{project}even when a real UUID session was active, leaving UUID sessions atobservation_count: 0.Root cause
engram serve(HTTP) andengram mcp(stdio) are separate processes sharing only the on-disk SQLite store. TheSessionStarthook registers the UUID session viaPOST /sessions(HTTP process), so any in-process state in the MCP process stays empty in the real flow.Change
MostRecentActiveSession(project)— resolves the most-recent un-ended session from the sharedsessionstable (ended_at IS NULL, excludesmanual-save%,ORDER BY started_at DESC).resolveFallbackSessionIDhelper wired into the 4 MCP fallback sites; falls back tomanual-save-{project}only when no active session exists or the query errors (fail-safe).Test plan
TDD red→green. Covers the real repro (session created as
POST /sessionsdoes →mem_savewith nosession_idlands in the UUID session), plus no-active-session fallback, multi-session recency, ended-session skip,manual-save%exclusion, project scoping.go test -race ./internal/mcp/...clean;go build ./...clean.Notes
Passed adversarial review (cross-process correctness, fail-safe, determinism verified). Non-blocking follow-up: a
sessions(project, ended_at)index if session volume grows.