Skip to content

Commit df56b94

Browse files
author
Test
committed
fix(session_search): exclude current session lineage
Cherry-picked from PR NousResearch#2201 by @Gutslabs. session_search resolved hits to parent/root sessions but only excluded the exact current_session_id. If the active session was a child continuation (compression/delegation), its parent could still appear as a 'past' conversation result. Fix: resolve current_session_id to its lineage root before filtering, so the entire active lineage (parent and children) is excluded.
1 parent 77dff51 commit df56b94

2 files changed

Lines changed: 68 additions & 3 deletions

File tree

tests/tools/test_session_search.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,61 @@ def test_current_session_excluded_keeps_others(self):
214214
# Current session should be skipped, only other_sid should appear
215215
assert result["sessions_searched"] == 1
216216
assert current_sid not in [r.get("session_id") for r in result.get("results", [])]
217+
218+
def test_current_child_session_excludes_parent_lineage(self):
219+
"""Compression/delegation parents should be excluded for the active child session."""
220+
from unittest.mock import MagicMock
221+
from tools.session_search_tool import session_search
222+
223+
mock_db = MagicMock()
224+
mock_db.search_messages.return_value = [
225+
{"session_id": "parent_sid", "content": "match", "source": "cli",
226+
"session_started": 1709500000, "model": "test"},
227+
]
228+
229+
def _get_session(session_id):
230+
if session_id == "child_sid":
231+
return {"parent_session_id": "parent_sid"}
232+
if session_id == "parent_sid":
233+
return {"parent_session_id": None}
234+
return None
235+
236+
mock_db.get_session.side_effect = _get_session
237+
238+
result = json.loads(session_search(
239+
query="test", db=mock_db, current_session_id="child_sid",
240+
))
241+
242+
assert result["success"] is True
243+
assert result["count"] == 0
244+
assert result["results"] == []
245+
assert result["sessions_searched"] == 0
246+
247+
def test_current_root_session_excludes_child_lineage(self):
248+
"""Delegation child hits should be excluded when they resolve to the current root session."""
249+
from unittest.mock import MagicMock
250+
from tools.session_search_tool import session_search
251+
252+
mock_db = MagicMock()
253+
mock_db.search_messages.return_value = [
254+
{"session_id": "child_sid", "content": "match", "source": "cli",
255+
"session_started": 1709500000, "model": "test"},
256+
]
257+
258+
def _get_session(session_id):
259+
if session_id == "root_sid":
260+
return {"parent_session_id": None}
261+
if session_id == "child_sid":
262+
return {"parent_session_id": "root_sid"}
263+
return None
264+
265+
mock_db.get_session.side_effect = _get_session
266+
267+
result = json.loads(session_search(
268+
query="test", db=mock_db, current_session_id="root_sid",
269+
))
270+
271+
assert result["success"] is True
272+
assert result["count"] == 0
273+
assert result["results"] == []
274+
assert result["sessions_searched"] == 0

tools/session_search_tool.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,20 @@ def _resolve_to_parent(session_id: str) -> str:
251251
break
252252
return sid
253253

254-
# Group by resolved (parent) session_id, dedup, skip current session
254+
current_lineage_root = (
255+
_resolve_to_parent(current_session_id) if current_session_id else None
256+
)
257+
258+
# Group by resolved (parent) session_id, dedup, skip the current
259+
# session lineage. Compression and delegation create child sessions
260+
# that still belong to the same active conversation.
255261
seen_sessions = {}
256262
for result in raw_results:
257263
raw_sid = result["session_id"]
258264
resolved_sid = _resolve_to_parent(raw_sid)
259-
# Skip the current session — the agent already has that context
260-
if current_session_id and resolved_sid == current_session_id:
265+
# Skip the current session lineage — the agent already has that
266+
# context, even if older turns live in parent fragments.
267+
if current_lineage_root and resolved_sid == current_lineage_root:
261268
continue
262269
if current_session_id and raw_sid == current_session_id:
263270
continue

0 commit comments

Comments
 (0)