Skip to content

The agent that grows with you

fix(gateway): offload Bot Chat recovery SQLite calls

KoNit-Kfa00787d537f
diff --git a/gateway/platforms/api_server.py b/gateway/platforms/api_server.py
index 509eea277a6..cd97e677c03 100644
--- a/gateway/platforms/api_server.py
+++ b/gateway/platforms/api_server.py
@@ -2811,8 +2811,13 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):
# which would fail `hermes peer dm` resolution and mint transient sessions — same accident
# the tui_gateway lookups heal.
from tools.bot_mode_probe import BOT_CHAT_TITLE
- stale = db.get_session_by_title(title_filter) if title_filter == BOT_CHAT_TITLE else None
- if stale and stale.get("archived") and db.unarchive_recoverable_session(stale["id"]):
+ stale = (
+ await asyncio.to_thread(db.get_session_by_title, title_filter)
+ if title_filter == BOT_CHAT_TITLE else None
+ )
+ if stale and stale.get("archived") and await asyncio.to_thread(
+ db.unarchive_recoverable_session, stale["id"]
+ ):
sessions = await _list()
except Exception:
pass # resolution degrades to today's no-row behavior
diff --git a/tests/gateway/test_session_api.py b/tests/gateway/test_session_api.py
index e78d428a527..8bfe23482a2 100644
--- a/tests/gateway/test_session_api.py
+++ b/tests/gateway/test_session_api.py
@@ -135,6 +135,43 @@ async def test_forked_session_stays_listable_and_parent_survives_failed_fork(ada
assert resp.status >= 500
assert session_db.get_session("solo")["end_reason"] is None
+
+async def test_list_sessions_resurrects_bot_chat_off_the_event_loop(adapter, session_db, monkeypatch):
+ """Canonical Bot Chat recovery must not run SQLite work in the HTTP loop."""
+ session_id = session_db.create_session("archived-bot-chat", "gateway_botmode")
+ assert session_db.set_session_title(session_id, "Bot Chat")
+ session_db.end_session(session_id, "ws_orphan_reap")
+ assert session_db.set_session_archived(session_id, True)
+
+ loop_thread = threading.get_ident()
+ call_threads = {}
+ get_by_title = session_db.get_session_by_title
+ unarchive = session_db.unarchive_recoverable_session
+
+ def record_get_by_title(title):
+ call_threads["get_session_by_title"] = threading.get_ident()
+ return get_by_title(title)
+
+ def record_unarchive(stale_id):
+ call_threads["unarchive_recoverable_session"] = threading.get_ident()
+ return unarchive(stale_id)
+
+ monkeypatch.setattr(session_db, "get_session_by_title", record_get_by_title)
+ monkeypatch.setattr(session_db, "unarchive_recoverable_session", record_unarchive)
+
+ app = _create_session_app(adapter)
+ async with TestClient(TestServer(app)) as cli:
+ response = await cli.get("/api/sessions?title=Bot%20Chat")
+ payload = await response.json()
+
+ assert response.status == 200
+ assert [session["id"] for session in payload["data"]] == [session_id]
+ assert set(call_threads) == {"get_session_by_title", "unarchive_recoverable_session"}
+ assert all(thread_id != loop_thread for thread_id in call_threads.values())
+ assert not session_db.get_session(session_id)["archived"]
+
+
@pytest.mark.asyncio
async def test_run_agent_binds_api_session_context_for_tool_env(adapter, monkeypatch):
"""API-server request sessions should reach tools and terminal subprocess env."""