Skip to content

The agent that grows with you

fix(gateway): keep a retried storage failure to one short log line

unsupportedpastels1dd6ca63c936
diff --git a/gateway/session_controls.py b/gateway/session_controls.py
index 98bfc8e8049..6f0af656650 100644
--- a/gateway/session_controls.py
+++ b/gateway/session_controls.py
@@ -61,10 +61,24 @@ def _log_refusal(profile, method, exc: RuntimeStoreError) -> None:
logger.debug(line, *args)
+# (method, error class) pairs whose traceback this process already logged.
+_STORAGE_TRACEBACKS = set()
+
+
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)
+ """Keep SQLite's diagnostic at the gateway while its wire error stays retryable.
+
+ Clients retry on ``storage_unavailable``, so, like ``_log_refusal``, a retrying client must not
+ flood the log: every failure keeps one line with its cause, the traceback comes once per method
+ and error class, and the client-supplied ``session_id`` is capped."""
+ key = (method, type(exc).__name__)
+ first = key not in _STORAGE_TRACEBACKS
+ _STORAGE_TRACEBACKS.add(key)
+ shown = repr(session_id)
+ if len(shown) > 64:
+ shown = f'{shown[:48]}… ({len(shown)} chars)'
+ logger.warning('%s storage failure for profile=%s session_id=%s: %s',
+ method, _profile_label(profile), shown, exc, exc_info=first)
# ``session.detach end_reason``: a short snake_case label, stored verbatim as ``sessions.end_reason``.
diff --git a/tests/gateway/test_authority_connection.py b/tests/gateway/test_authority_connection.py
index 33b9883397c..817756a1307 100644
--- a/tests/gateway/test_authority_connection.py
+++ b/tests/gateway/test_authority_connection.py
@@ -199,3 +199,33 @@ async def test_a_sqlite_failure_keeps_the_wire_error_and_logs_its_cause(tmp_path
assert 'sqlite3.OperationalError: database is locked' in caplog.text
finally:
db.close()
+
+
+async def test_a_retrying_client_cannot_flood_the_log_with_storage_failures(tmp_path, caplog):
+ """Clients retry on ``storage_unavailable``: each failure keeps one short line, the traceback comes
+ once per method and error class, and a client-sized ``session_id`` is never copied whole."""
+ 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.retry_probe': probe}
+
+ connection = Connection(authority, object(), {'user_id': 'human'})
+ with caplog.at_level('WARNING', logger='gateway.session_controls'):
+ for _ in range(3):
+ reply = await connection.dispatch({'id': 9, 'method': 'session.retry_probe', 'params': {
+ 'session_id': 'x' * 100_000}})
+ assert reply['error']['code'] == 5001
+ failures = [record for record in caplog.records if 'storage failure' in record.getMessage()]
+ assert [bool(record.exc_info) for record in failures] == [True, False, False]
+ assert all(len(record.getMessage()) < 300 for record in failures), failures[0].getMessage()[:300]
+ assert all('database is locked' in record.getMessage() for record in failures)
+ finally:
+ db.close()