Repository navigation
fix(message-history): fall back within the requested session in get_relevant() - #765
Open
vishal-bala wants to merge 1 commit into
Open
vishal-bala wants to merge 1 commit into
vishal-bala wants to merge 1 commit into
Conversation
…elevant() get_relevant(session_tag=..., fall_back=True) searched the requested session but, when nothing was relevant, fell back to get_recent() without the session_tag. It therefore returned the recent messages of the instance's default session, letting a lookup for one conversation surface another's. The fall-back now passes session_tag through, so it reads the same session the search did. SemanticSessionManager inherits the fix; MessageHistory has no fall_back and is unaffected. The new integration test stores one exchange in the default session and one in another, then asks for the other session with distance_threshold=0 so the fall-back fires. On main it returns the default session's exchange; with this change it returns the requested one.
vishal-bala
marked this pull request as ready for review
October 15, 2026 08:39
This branch has not been deployed
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.
Motivation
SemanticMessageHistory.get_relevant()accepts asession_tagand runs its vector search within that session. When nothing in the session is relevant andfall_back=True, it falls back toget_recent(), but without thesession_tag. The fall-back therefore returns the recent messages of the instance's default session, so a lookup for one conversation can surface another conversation's messages.SemanticSessionManagerinherits the same behaviour.Changes
The fall-back now passes
session_tagthrough toget_recent(), so it reads the same session the search did. When nosession_tagis given, both still use the default session, as before.MessageHistoryhas nofall_backoption and is unaffected.A new integration test,
test_semantic_get_relevant_falls_back_within_the_requested_session, stores one exchange in the default session and one in a second session. It then callsget_relevant()for the second session withdistance_threshold=0, which forces the fall-back. Againstmainthe test fails, returning the default session's exchange; with this change it returns the requested session's.Release Notes
SemanticMessageHistory.get_relevant()andSemanticSessionManager.get_relevant()called with bothsession_tagandfall_back=Truenow fall back to the recent messages of that session. Previously they returned the recent messages of the instance's default session.Note
Medium Risk
Changes multi-session context retrieval for semantic history fallback, which could affect LLM prompt assembly but is a targeted bugfix with a regression test.
Overview
Fixes a session leak when
SemanticMessageHistory.get_relevant()(andSemanticSessionManager, which subclasses it) runs with bothsession_tagandfall_back=True. Vector search was already scoped to that session, but the empty-result fallback calledget_recent()withoutsession_tag, so recent messages from the instance default session could appear in another conversation’s context.The fallback now forwards
session_tag(and existingrole/ formatting args) toget_recent(), so recent context stays in the same session; behavior whensession_tagis omitted is unchanged.Adds integration test
test_semantic_get_relevant_falls_back_within_the_requested_session(two sessions, forced fallback viadistance_threshold=0) to lock in session-scoped fallback.Reviewed by Cursor Bugbot for commit 3d972cc. Bugbot is set up for automated code reviews on this repo. Configure here.