From f3e2407e48808011611441495ebe7cf7150a3ef3 Mon Sep 17 00:00:00 2001 From: Mohammad Hijjawi Date: Sun, 27 Sep 2026 11:12:56 +0100 Subject: [PATCH] fix(sessions): keep recycled chats out of history search Moving a chat to the recycle bin hides it from the session list, but history search still matched it: the SQLite search condition and the PocketBase search filter only excluded imported sessions, not soft-deleted ones. Search results (and the total used for paging) therefore included chats the user had already deleted. Exclude rows with deleted_at set in both stores, matching list_sessions. A restored chat becomes searchable again. --- deeptutor/services/session/pocketbase_store.py | 5 ++++- deeptutor/services/session/sqlite_store.py | 1 + tests/services/session/test_history_search.py | 16 ++++++++++++++++ .../session/test_pocketbase_isolation.py | 14 ++++++++++++++ 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/deeptutor/services/session/pocketbase_store.py b/deeptutor/services/session/pocketbase_store.py index cfac09ac5..5288788ef 100644 --- a/deeptutor/services/session/pocketbase_store.py +++ b/deeptutor/services/session/pocketbase_store.py @@ -637,12 +637,15 @@ class PocketBaseSessionStore: def _search() -> dict[str, Any]: pb = _pb() records = pb.collection("sessions").get_full_list( - query_params={"filter": _workspace_filter(f"user_id={json.dumps(uid)}")} + query_params={ + "filter": _workspace_filter(f"user_id={json.dumps(uid)} && deleted_at = null") + } ) records = [ record for record in records if _in_workspace(record) + and not _to_float(getattr(record, "deleted_at", None)) and not str(getattr(record, "session_id", "") or "").startswith("imported_") ] records.sort( diff --git a/deeptutor/services/session/sqlite_store.py b/deeptutor/services/session/sqlite_store.py index a559e2aaa..1708d58c4 100644 --- a/deeptutor/services/session/sqlite_store.py +++ b/deeptutor/services/session/sqlite_store.py @@ -2750,6 +2750,7 @@ class SQLiteSessionStore: match_condition = r""" s.id NOT LIKE 'imported\_%' ESCAPE '\' + AND s.deleted_at IS NULL AND ( INSTR(LOWER(COALESCE(s.title, '')), LOWER(?)) > 0 OR EXISTS ( diff --git a/tests/services/session/test_history_search.py b/tests/services/session/test_history_search.py index 975ddbc00..3b5328f3d 100644 --- a/tests/services/session/test_history_search.py +++ b/tests/services/session/test_history_search.py @@ -64,3 +64,19 @@ def test_search_ignores_system_only_matches_and_empty_queries(tmp_path) -> None: assert run(store.search_sessions("needle")) == {"sessions": [], "total": 0} assert run(store.search_sessions(" ")) == {"sessions": [], "total": 0} + + +def test_search_skips_sessions_in_the_recycle_bin(tmp_path) -> None: + store = SQLiteSessionStore(tmp_path / "history.db") + kept = run(store.create_session(title="Bayes notes", session_id="kept")) + recycled = run(store.create_session(title="Bayes draft", session_id="recycled")) + run(store.add_message(recycled["id"], "user", "Explain Bayes theorem")) + run(store.soft_delete_session(recycled["id"])) + + result = run(store.search_sessions("bayes")) + + assert result["total"] == 1 + assert [row["session_id"] for row in result["sessions"]] == [kept["id"]] + + run(store.restore_session(recycled["id"])) + assert run(store.search_sessions("bayes"))["total"] == 2 diff --git a/tests/services/session/test_pocketbase_isolation.py b/tests/services/session/test_pocketbase_isolation.py index 022d5e805..452b7c064 100644 --- a/tests/services/session/test_pocketbase_isolation.py +++ b/tests/services/session/test_pocketbase_isolation.py @@ -186,6 +186,20 @@ async def test_search_sessions_is_owner_scoped_and_preserves_native_visibility(f assert archived_row["preferences"]["archived"] is True +async def test_search_sessions_skips_sessions_in_the_recycle_bin(fake_pb) -> None: + store = PocketBaseSessionStore() + with as_user("alice"): + kept = await store.create_session(title="Bayes notes", session_id="s_kept") + recycled = await store.create_session(title="Bayes draft", session_id="s_recycled") + await store.add_message(recycled["id"], "user", "Explain Bayes theorem") + await store.soft_delete_session(recycled["id"]) + + result = await store.search_sessions("bayes") + + assert result["total"] == 1 + assert [row["session_id"] for row in result["sessions"]] == [kept["id"]] + + async def test_legacy_workspace_preferences_are_normalized_at_repository_boundary( fake_pb, ) -> None: