DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
fix: keep recycled sessions out of question bank counts
Mohammad Hijjawidbdecf114b39
diff --git a/deeptutor/services/session/sqlite_store.py b/deeptutor/services/session/sqlite_store.pyindex a559e2aaa..46f50b91f 100644--- a/deeptutor/services/session/sqlite_store.py+++ b/deeptutor/services/session/sqlite_store.py@@ -112,6 +112,14 @@ _GRADED_RESULT_SQL = "COALESCE(NULLIF(n.result,''),'graded') NOT IN ('ungraded',_GRADED_RESULT_SQL_UNALIASED = ("COALESCE(NULLIF(result,''),'graded') NOT IN ('ungraded','voided','')")+# An entry whose session sits in the recycle bin is hidden from the question+# bank until the session is restored. ``{entries}`` names the notebook_entries+# alias of the surrounding query, so every listing, count and chip applies the+# same rule.+_NOT_RECYCLED_ENTRY_SQL = (+ "NOT EXISTS (SELECT 1 FROM sessions s"+ " WHERE s.id = {entries}.session_id AND s.deleted_at IS NOT NULL)"+)ACTIVE_TURN_STATUSES = frozenset({"queued", "running", "waiting_input"})TERMINAL_TURN_STATUSES = frozenset({"completed", "failed", "cancelled"})ALL_TURN_STATUSES = ACTIVE_TURN_STATUSES | TERMINAL_TURN_STATUSES@@ -3579,14 +3587,7 @@ class SQLiteSessionStore:conditions: list[str] = []params: list[Any] = []- conditions.append(- """- NOT EXISTS (- SELECT 1 FROM sessions s- WHERE s.id = n.session_id AND s.deleted_at IS NOT NULL- )- """- )+ conditions.append(_NOT_RECYCLED_ENTRY_SQL.format(entries="n"))if query.mistakes_only:conditions.append("EXISTS (SELECT 1 FROM practice_review_state r WHERE r.entry_id = n.id AND r.is_mistake = 1)"@@ -3805,11 +3806,11 @@ class SQLiteSessionStore:# not scope", empty means "scoped to nothing". The rail's counts sit# beside the list, so anything the list excludes must not be counted# here either.- where = ""+ where = "WHERE " + _NOT_RECYCLED_ENTRY_SQL.format(entries="notebook_entries")params: list[str] = []if session_ids is not None:placeholders = ",".join("?" for _ in session_ids) or "NULL"- where = f"WHERE session_id IN ({placeholders})"+ where += f" AND session_id IN ({placeholders})"params = list(session_ids)row = conn.execute(f"""@@ -3873,7 +3874,9 @@ class SQLiteSessionStore:self,session_ids: Sequence[str] | None = None,) -> list[dict[str, Any]]:- where = "material_id != ''"+ where = "material_id != '' AND " + _NOT_RECYCLED_ENTRY_SQL.format(+ entries="notebook_entries"+ )params: list[str] = []if session_ids is not None:placeholders = ",".join("?" for _ in session_ids) or "NULL"@@ -4087,7 +4090,8 @@ class SQLiteSessionStore:# created still exists inside a course that has not filled it yet, and# dropping the row would make it look deleted. Hence the condition rides# on the join instead of a WHERE clause.- join = "LEFT JOIN notebook_entries e ON e.id = ec.entry_id"+ not_recycled = _NOT_RECYCLED_ENTRY_SQL.format(entries="e")+ join = f"LEFT JOIN notebook_entries e ON e.id = ec.entry_id AND {not_recycled}"params: list[str] = []if session_ids is not None:placeholders = ",".join("?" for _ in session_ids) or "NULL"diff --git a/tests/services/session/test_sqlite_store.py b/tests/services/session/test_sqlite_store.pyindex c00f97861..546047222 100644--- a/tests/services/session/test_sqlite_store.py+++ b/tests/services/session/test_sqlite_store.py@@ -763,6 +763,43 @@ def test_entries_follow_a_session_into_and_out_of_the_recycle_bin(assert detached["items"][0]["origin_ref"] == session["id"]+def test_bank_counts_skip_entries_of_recycled_sessions(store: SQLiteSessionStore) -> None:+ """The rail's counts, material chips and category badges match the list."""+ kept = asyncio.run(store.create_session())+ recycled = asyncio.run(store.create_session())+ category = asyncio.run(store.create_category("Algebra"))+ for session, material in ((kept, "book-a"), (recycled, "book-b")):+ item = _make_items(("q1", "Q?", False))[0]+ item.update(material_id=material, material_title=material)+ asyncio.run(store.upsert_notebook_entries(session["id"], [item]))+ entry = asyncio.run(store.find_notebook_entry(session["id"], "q1"))+ asyncio.run(store.update_notebook_entry(entry["id"], {"bookmarked": True}))+ asyncio.run(store.add_entry_to_category(entry["id"], category["id"]))++ asyncio.run(store.soft_delete_session(recycled["id"]))++ assert asyncio.run(store.list_notebook_entries())["total"] == 1+ assert asyncio.run(store.question_bank_stats()) == {+ "total": 1,+ "wrong": 1,+ "unresolved": 1,+ "bookmarked": 1,+ "uncategorized": 0,+ }+ materials = asyncio.run(store.list_question_bank_materials())+ assert [m["material_id"] for m in materials] == ["book-a"]+ assert asyncio.run(store.list_categories())[0]["entry_count"] == 1+ # Scoped counts (a course's sessions) follow the same rule.+ scope = [kept["id"], recycled["id"]]+ assert asyncio.run(store.question_bank_stats(scope))["total"] == 1+ assert asyncio.run(store.list_categories(scope))[0]["entry_count"] == 1++ assert asyncio.run(store.restore_session(recycled["id"]))+ assert asyncio.run(store.question_bank_stats())["total"] == 2+ assert len(asyncio.run(store.list_question_bank_materials())) == 2+ assert asyncio.run(store.list_categories())[0]["entry_count"] == 2++def test_entries_keep_provenance_when_a_session_is_deleted_outright(store: SQLiteSessionStore,) -> None: