DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
fix(marginnote4): close the store's SQLite connections
hyeonsang0107161f613b276a1e
diff --git a/deeptutor/capabilities/marginnote4/store.py b/deeptutor/capabilities/marginnote4/store.pyindex f2d7e60ec..6309aaede 100644--- a/deeptutor/capabilities/marginnote4/store.py+++ b/deeptutor/capabilities/marginnote4/store.py@@ -15,6 +15,8 @@ The database file lives under ``data/marginnote4/<kb_name>.db`` by default.from __future__ import annotations+from collections.abc import Iterator+from contextlib import contextmanagerfrom datetime import datetime, timezoneimport hashlibimport json@@ -172,11 +174,21 @@ class MarginNoteStore:# -- internal helpers ---------------------------------------------------- def _connect(self) -> sqlite3.Connection:+ @contextmanager+ def _connect(self) -> Iterator[sqlite3.Connection]:+ # A connection's own context manager commits or rolls back but never+ # closes, so the file stayed open until the GC reached it. On Windows+ # an open file cannot be removed: deleting the KB left the store, and+ # its paired device tokens, for a library reconnected under the same+ # name to pick up again. ``with conn`` keeps each call one transaction.conn = sqlite3.connect(str(self.db_path))conn.row_factory = sqlite3.Rowconn.execute("PRAGMA journal_mode=WAL")- return conn+ try:+ with conn:+ yield conn+ finally:+ conn.close()def _init_schema(self) -> None:with self._connect() as conn:diff --git a/tests/capabilities/marginnote4/test_store.py b/tests/capabilities/marginnote4/test_store.pyindex acf3ab6a1..92004ceee 100644--- a/tests/capabilities/marginnote4/test_store.py+++ b/tests/capabilities/marginnote4/test_store.py@@ -3,6 +3,7 @@from __future__ import annotationsfrom pathlib import Path+import sqlite3import pytest@@ -273,3 +274,46 @@ def test_resolve_db_path_derives_when_no_kb_is_resolvable(tmp_path, monkeypatch)assert resolve_db_path("Nonexistent") == default_db_path("Nonexistent")finally:PathService.reset_instance()+++# ---- connection lifecycle --------------------------------------------------+++class _TrackedConnection(sqlite3.Connection):+ """Connection that records whether ``close()`` was called."""++ was_closed = False++ def close(self) -> None:+ self.was_closed = True+ super().close()+++def test_every_connection_is_closed_after_use(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ """An open connection keeps the store's file locked on Windows until GC.++ ``with sqlite3.Connection`` commits but never closes, so a store relying on+ it could not be deleted there, and a library reconnected under the same name+ picked its paired devices back up.+ """+ opened: list[_TrackedConnection] = []+ real_connect = sqlite3.connect++ def _tracked_connect(*args, **kwargs):+ conn = real_connect(*args, factory=_TrackedConnection, **kwargs)+ opened.append(conn)+ return conn++ monkeypatch.setattr(sqlite3, "connect", _tracked_connect)++ store = MarginNoteStore(tmp_path / "test.db")+ device, token = store.pair_device(device_name="iPad")+ store.ingest(SyncBatch(device_id=device.device_id, objects=_seed_objects(device.device_id)))+ assert store.verify_token(device.device_id, token) is True+ assert store.search("photosynthesis")+ assert store.revoke_device(device.device_id) is True++ assert opened+ assert all(conn.was_closed for conn in opened)