Skip to content

The agent that grows with you

fix(tui): log the refused shared-handle transfer and pin _get_db caching

liuhao1024349d9aee4304
diff --git a/tests/tui_gateway/test_session_db_ownership_teardown.py b/tests/tui_gateway/test_session_db_ownership_teardown.py
index e65f4be571d..766629a7a0b 100644
--- a/tests/tui_gateway/test_session_db_ownership_teardown.py
+++ b/tests/tui_gateway/test_session_db_ownership_teardown.py
@@ -241,6 +241,19 @@ def test_transfer_is_refused_for_the_shared_launch_handle(monkeypatch):
assert agent._owns_session_db is False
+def test_get_db_returns_the_cached_instance(monkeypatch):
+ """The identity defense (``db is _get_db()``) only works while _get_db
+ hands out ONE process-wide instance. Pin the caching semantics: once a
+ handle exists, repeated calls return the same object rather than
+ constructing per-call wrappers (review finding on #91631)."""
+ sentinel = types.SimpleNamespace(closed=0)
+ monkeypatch.setattr(server, "_db", sentinel)
+ monkeypatch.setattr(server, "_db_error", None)
+
+ assert server._get_db() is sentinel
+ assert server._get_db() is server._get_db()
+
+
# ---------------------------------------------------------------------------
# 3. The deferred builder — _start_agent_build
# ---------------------------------------------------------------------------
diff --git a/tui_gateway/server.py b/tui_gateway/server.py
index 04a2f52da45..4fb51eeec42 100644
--- a/tui_gateway/server.py
+++ b/tui_gateway/server.py
@@ -1478,6 +1478,10 @@ def _transfer_db_to_agent(agent, db) -> bool:
# explicitly even if a caller invokes the transfer incorrectly; the
# caller's own `owns_db` gate is the first line of defense.
if db is _get_db():
+ logger.warning(
+ "Refused transfer of the shared launch SessionDB to a session "
+ "agent — the caller's owns_db gate should have prevented this."
+ )
return False
agent._owns_session_db = True
return True