From 349d9aee4304c31fb4cd5acb0fdc2befc2f731ad Mon Sep 17 00:00:00 2001 From: liuhao1024 Date: Sat, 22 Aug 2026 04:08:53 +0800 Subject: [PATCH] fix(tui): log the refused shared-handle transfer and pin _get_db caching --- .../test_session_db_ownership_teardown.py | 13 +++++++++++++ tui_gateway/server.py | 4 ++++ 2 files changed, 17 insertions(+) diff --git a/tests/tui_gateway/test_session_db_ownership_teardown.py b/tests/tui_gateway/test_session_db_ownership_teardown.py index e65f4be571..766629a7a0 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 04a2f52da4..4fb51eeec4 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