From ea26f2271073e8ee826a6637f03256f489c03162 Mon Sep 17 00:00:00 2001 From: yoniebans Date: Mon, 29 Jun 2026 11:46:18 +0200 Subject: [PATCH] feat(gateway): add AsyncSessionDB offload facade --- hermes_state.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/hermes_state.py b/hermes_state.py index e69dd4b02c9..10b481a05db 100644 --- a/hermes_state.py +++ b/hermes_state.py @@ -14,6 +14,7 @@ Key design decisions: - Session source tagging ('cli', 'telegram', 'discord', etc.) for filtering """ +import asyncio import json import logging import random @@ -5525,3 +5526,20 @@ class SessionDB: (error[:500], session_id), ) self._execute_write(_do) + + +class AsyncSessionDB: + """Async door onto SessionDB: offloads each call via asyncio.to_thread so a blocking SQLite call never freezes the event loop. Generic forwarder — the audit confirms no method returns a live cursor/generator.""" + + def __init__(self, db: "SessionDB") -> None: + self._db = db + + def __getattr__(self, name: str): + attr = getattr(self._db, name) + if not callable(attr): + return attr + + async def _offloaded(*args, **kwargs): + return await asyncio.to_thread(attr, *args, **kwargs) + + return _offloaded