fix(session_search): order recent mode by last activity instead of start time

- order session_search recent-mode results by last activity instead of session start time
- add an opt-in `order_by_last_active` path to `SessionDB.list_sessions_rich`
- add regression coverage for both the database ordering and recent-mode call path
This commit is contained in:
simbam99
2026-04-30 20:01:51 -07:00
committed by Teknium
parent 96691268df
commit 142b4bf3ce
4 changed files with 81 additions and 4 deletions

View File

@@ -1719,6 +1719,39 @@ class TestListSessionsRich:
# No messages, so last_active falls back to started_at
assert sessions[0]["last_active"] == sessions[0]["started_at"]
def test_order_by_last_active_surfaces_recently_touched_older_session_first(self, db):
t0 = 1709500000.0
db.create_session("old", "cli")
db.create_session("new", "cli")
with db._lock:
db._conn.execute("UPDATE sessions SET started_at=? WHERE id=?", (t0, "old"))
db._conn.execute("UPDATE sessions SET started_at=? WHERE id=?", (t0 + 10, "new"))
db.append_message("old", "user", "old first")
db.append_message("new", "user", "new first")
db.append_message("old", "assistant", "old touched later")
with db._lock:
db._conn.execute(
"UPDATE messages SET timestamp=? WHERE session_id=? AND role=? AND content=?",
(t0 + 1, "old", "user", "old first"),
)
db._conn.execute(
"UPDATE messages SET timestamp=? WHERE session_id=? AND role=? AND content=?",
(t0 + 11, "new", "user", "new first"),
)
db._conn.execute(
"UPDATE messages SET timestamp=? WHERE session_id=? AND role=? AND content=?",
(t0 + 20, "old", "assistant", "old touched later"),
)
db._conn.commit()
assert [s["id"] for s in db.list_sessions_rich(limit=5)] == ["new", "old"]
assert [
s["id"] for s in db.list_sessions_rich(limit=5, order_by_last_active=True)
] == ["old", "new"]
def test_rich_list_includes_title(self, db):
db.create_session("s1", "cli")
db.set_session_title("s1", "refactoring auth")

View File

@@ -242,6 +242,21 @@ class TestSessionSearchConcurrency:
class TestRecentSessionListing:
def test_recent_mode_requests_last_active_ordering(self):
from unittest.mock import MagicMock
mock_db = MagicMock()
mock_db.list_sessions_rich.return_value = []
result = json.loads(_list_recent_sessions(mock_db, limit=5))
assert result["success"] is True
mock_db.list_sessions_rich.assert_called_once_with(
limit=10,
exclude_sources=["tool"],
order_by_last_active=True,
)
def test_current_child_session_excludes_root_lineage_even_when_child_id_is_longer(self):
from unittest.mock import MagicMock