Skip to content

The agent that grows with you

fix(gateway): log SQLite dispatch failures

KoNit-K1a81505ef20a
diff --git a/gateway/session_controls.py b/gateway/session_controls.py
index f54a8a91431..98bfc8e8049 100644
--- a/gateway/session_controls.py
+++ b/gateway/session_controls.py
@@ -61,6 +61,12 @@ def _log_refusal(profile, method, exc: RuntimeStoreError) -> None:
logger.debug(line, *args)
+def _log_storage_failure(profile, method, session_id, exc: sqlite3.Error) -> None:
+ """Keep SQLite's diagnostic at the gateway while its wire error stays retryable."""
+ logger.warning('%s storage failure for profile=%s session_id=%r: %s',
+ method, _profile_label(profile), session_id, exc, exc_info=True)
+
+
# ``session.detach end_reason``: a short snake_case label, stored verbatim as ``sessions.end_reason``.
_END_REASON = re.compile(r'[a-z][a-z_]{0,31}')
# Verbs whose param sets are closed and never carried a ``profile`` key.
@@ -277,7 +283,9 @@ class AuthorityConnection:
_log_refusal(getattr(self.actor, 'profile_id', None), method, exc)
return {'jsonrpc': '2.0', 'id': rid, 'error': {
'code': 4001, 'message': exc.reason, 'data': _refusal_data(exc)}}
- except sqlite3.Error:
+ except sqlite3.Error as exc:
+ _log_storage_failure(getattr(self.actor, 'profile_id', None), method,
+ params.get('session_id'), exc)
return {'jsonrpc': '2.0', 'id': rid, 'error': {
'code': 5001, 'message': 'storage_unavailable', 'data': {'reason': 'storage_unavailable'}}}
diff --git a/tests/gateway/test_authority_connection.py b/tests/gateway/test_authority_connection.py
index 49617fb0b8d..33b9883397c 100644
--- a/tests/gateway/test_authority_connection.py
+++ b/tests/gateway/test_authority_connection.py
@@ -1,5 +1,6 @@
"""Connection membership against the real authority and temporary SQLite."""
import asyncio
+import sqlite3
from types import SimpleNamespace
import pytest
@@ -170,3 +171,31 @@ async def test_stale_or_mismatched_pair_reports_detached_false_and_keeps_the_liv
if observer is not None:
await observer.close()
db.close()
+
+
+async def test_a_sqlite_failure_keeps_the_wire_error_and_logs_its_cause(tmp_path, caplog):
+ """Storage failures retain their client contract while leaving an operator diagnostic."""
+ db = SessionDB(db_path=tmp_path / 'state.db')
+ try:
+ epoch = begin_runtime_epoch(db, instance_id='test')
+ authority = SessionAuthority(SimpleNamespace(), profile_id='test', instance_id='test',
+ db=db, epoch=epoch)
+
+ class Connection(AuthorityConnection):
+ def handlers(self):
+ async def probe(ref, params):
+ raise sqlite3.OperationalError('database is locked')
+ return {'session.storage_probe': probe}
+
+ connection = Connection(authority, object(), {'user_id': 'human'})
+ with caplog.at_level('WARNING', logger='gateway.session_controls'):
+ reply = await connection.dispatch({'id': 9, 'method': 'session.storage_probe', 'params': {
+ 'session_id': 'session-9'}})
+ assert reply['error'] == {
+ 'code': 5001, 'message': 'storage_unavailable', 'data': {'reason': 'storage_unavailable'}}
+ assert any('session.storage_probe storage failure for profile=test session_id=' in record.getMessage()
+ and 'database is locked' in record.getMessage() for record in caplog.records)
+ assert 'sqlite3.OperationalError: database is locked' in caplog.text
+ finally:
+ db.close()