Skip to content

The agent that grows with you

feat(state): support the context-manager protocol on SessionDB

Jack Lauab7f48d4b016
diff --git a/hermes_state.py b/hermes_state.py
index b284e0046af..36274ddceb5 100644
--- a/hermes_state.py
+++ b/hermes_state.py
@@ -4341,6 +4341,42 @@ class SessionDB(SessionSearchMixin, SessionSchemaMixin, SessionPortabilityMixin)
except Exception as exc:
logger.warning("WAL checkpoint (PASSIVE) failed: %s", exc)
+ def __enter__(self) -> "SessionDB":
+ """Enter a scope that closes this handle on the way out.
+
+ Ownership of a SessionDB has to be released explicitly: once the
+ background token writer starts, the instance pins ITSELF via
+ ``atexit.register(self._drain_token_queue_at_exit)`` and via the
+ writer thread's bound-method target, and only ``close()``
+ unregisters the former. Dropping the last reference therefore does
+ NOT release the db/-wal/-shm descriptors the way plain refcounting
+ would suggest, and ``__del__`` never runs for exactly the instances
+ that leak. That is why call sites owning a handle are expected to
+ close it (see the ownership comments in ``run_agent.py`` and
+ ``tui_gateway/methods_session.py``).
+
+ This makes the correct usage the easy one, so an owning scope can be
+ exception-safe by construction rather than by remembering a
+ ``try/finally``:
+
+ with SessionDB(path) as db:
+ db.append_message(...)
+
+ Purely additive: it changes nothing for callers that already call
+ ``close()`` directly, and ``close()`` stays idempotent, so a scope
+ that closes early still exits cleanly.
+ """
+ return self
+
+ def __exit__(self, exc_type, exc, tb) -> bool:
+ """Close the handle, then let any exception propagate.
+
+ Returns False (never suppressing), so ``with`` here only manages the
+ descriptor lifetime and never swallows a caller's error.
+ """
+ self.close()
+ return False
+
def close(self):
"""Close the database connection.
diff --git a/tests/test_session_db_context_manager.py b/tests/test_session_db_context_manager.py
new file mode 100644
index 00000000000..862201f87d0
--- /dev/null
+++ b/tests/test_session_db_context_manager.py
@@ -0,0 +1,99 @@
+"""``SessionDB`` must support ``with``, so an owning scope releases its fds.
+
+A SessionDB handle cannot be released by dropping the last reference. Once its
+background token writer starts, the instance pins ITSELF two ways: the writer
+thread's target is a bound method, and ``queue_token_counts`` registers
+``atexit.register(self._drain_token_queue_at_exit)``, which only ``close()``
+unregisters. A dropped-but-pinned handle keeps its ``state.db``/``-wal``/
+``-shm`` descriptors for the life of the process, and ``__del__`` never runs
+for it -- so the safety net is dead code for exactly the instances that leak.
+
+That is why owning call sites are expected to close explicitly; the ownership
+comments in ``run_agent.py`` and ``tui_gateway/methods_session.py`` say so in
+those words. This module pins the ergonomic half of that contract: an owner can
+scope a handle with ``with`` and be exception-safe by construction, instead of
+hand-writing a ``try/finally`` at each of the ~59 instantiation sites (#88033).
+
+Following ``test_session_db_read_conn_pool.py``, these assert on the
+``sqlite_safe_read`` tracking registry rather than on raw descriptor counts:
+SQLite's unix VFS parks a closed descriptor on a per-inode reuse list while any
+connection still holds POSIX locks on that inode, so descriptor counts lag the
+real connection count and make such assertions flaky.
+"""
+
+import pytest
+
+from hermes_state import SessionDB
+
+
+def _live_count(path) -> int:
+ """Live-connection count the tracking registry holds for *path*."""
+ import hermes_cli.sqlite_safe_read as mod
+
+ with mod._live_lock:
+ return mod._live_connections.get(mod._key(path), 0)
+
+
+def test_with_block_closes_the_handle(tmp_path):
+ """The whole point: leaving the scope releases the connection."""
+ path = tmp_path / "state.db"
+
+ with SessionDB(db_path=path) as db:
+ db.create_session(session_id="s1", source="cli", model="m")
+ db.append_message("s1", role="user", content="hello")
+ assert db._conn is not None
+ assert _live_count(path) > 0
+
+ assert db._conn is None
+ assert _live_count(path) == 0
+
+
+def test_enter_returns_the_same_handle(tmp_path):
+ """``with SessionDB(...) as db`` must bind the instance, not a wrapper.
+
+ Returning anything else would silently break every attribute access in the
+ body, so this is cheap insurance on the one line that is easy to get wrong.
+ """
+ db = SessionDB(db_path=tmp_path / "state.db")
+
+ with db as entered:
+ assert entered is db
+
+ assert db._conn is None
+
+
+def test_exception_inside_the_block_still_closes_and_propagates(tmp_path):
+ """Exception safety is the reason to prefer ``with`` over a bare close().
+
+ The handle must be released on the failure path (the path that leaked
+ hardest, since it is the one callers forget), and ``__exit__`` must NOT
+ suppress: swallowing a caller's error to release a descriptor would trade
+ one bug for a worse one.
+ """
+ path = tmp_path / "state.db"
+ db = SessionDB(db_path=path)
+
+ with pytest.raises(ValueError, match="boom"):
+ with db:
+ db.create_session(session_id="s1", source="cli", model="m")
+ raise ValueError("boom")
+
+ assert db._conn is None
+ assert _live_count(path) == 0
+
+
+def test_closing_inside_the_block_leaves_the_exit_clean(tmp_path):
+ """``close()`` is documented idempotent; the scope must not fight a caller.
+
+ A call site converted to ``with`` may still hold an explicit ``close()``
+ (or hit one on an internal error path). The second close from ``__exit__``
+ must be a no-op rather than an error.
+ """
+ path = tmp_path / "state.db"
+
+ with SessionDB(db_path=path) as db:
+ db.create_session(session_id="s1", source="cli", model="m")
+ db.close()
+ assert db._conn is None
+
+ assert _live_count(path) == 0