Skip to content

fix(session_search): exclude entire session lineage from recent sessions list#3531

Open
dieutx wants to merge 1 commit intoNousResearch:mainfrom
dieutx:fix/session-search-lineage-root
Open

fix(session_search): exclude entire session lineage from recent sessions list#3531
dieutx wants to merge 1 commit intoNousResearch:mainfrom
dieutx:fix/session-search-lineage-root

Conversation

@dieutx
Copy link
Copy Markdown
Contributor

@dieutx dieutx commented Mar 28, 2026

Summary

After context compression, session_search with no query (browsing recent sessions) shows the current conversation's parent session in the results. The agent sees its own conversation as a separate "recent session" and may waste tokens recalling from itself.

Root Cause

_list_recent_sessions at session_search_tool.py:211 uses max(visited, key=len) to find the root session of the current lineage. This picks the session ID with the longest string, not the actual root parent. If the child ID is longer than the parent ID, the wrong session is identified as root and the real parent leaks into results.

The correct pattern already exists in the same file — _resolve_to_parent at line 298 walks the chain and returns the last sid reached. But _list_recent_sessions uses a different, broken approach.

Additionally, line 218 only excludes current_root and current_session_id — but intermediate sessions in a deep chain (grandparent→parent→child) are not excluded.

Fix

Replaced the max(visited, key=len) approach with a proper parent-chain walk that captures the actual root. Changed the exclusion check from comparing two IDs to checking membership in the full current_lineage set — so all sessions in the chain (root, intermediate, current) are excluded.

current_lineage = set()
sid = current_session_id
while sid:
    current_lineage.add(sid)
    parent = db.get_session(sid).get("parent_session_id")
    if parent and parent not in current_lineage:
        sid = parent
    else:
        break

Tests

4 new tests: parent excluded after compression, deep A→B→C chain all excluded, no-compression session excluded, None current_session returns all.

4 passed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant