Skip to content

The agent that grows with you

refactor(dashboard): key the 503 payload by classify_persistence_error bucket

kshitijk4poorfd2212975c90
diff --git a/hermes_cli/web_routers/_common.py b/hermes_cli/web_routers/_common.py
index 379cddcf81f..e80f977ef86 100644
--- a/hermes_cli/web_routers/_common.py
+++ b/hermes_cli/web_routers/_common.py
@@ -160,10 +160,12 @@ CORRUPT_STORE_DETAIL = {
"error": "state_db_corrupt",
"message": "state.db corrupt — run `hermes doctor` (then `hermes doctor --fix` or `hermes sessions repair`).",
}
+# One payload per persistence-cause bucket (``classify_persistence_error``); ``error`` codes
+# all follow the ``state_db_*`` scheme of the pre-existing ``state_db_corrupt``.
# Same guidance as the deleted_wal / replaced turn explainers: `doctor --fix` while a holder
# lives would repair the wrong generation in place, so it is deliberately NOT suggested here.
DELETED_WAL_DETAIL = {
- "error": "deleted_wal",
+ "error": "state_db_deleted_wal",
"message": "another Hermes process still holds an old copy of the session database's write-ahead log — "
"quit every Hermes process on this profile, run `hermes doctor` (it names the holders), "
"then start Hermes again. Do not run `hermes doctor --fix` while they run.",
@@ -173,6 +175,8 @@ STATE_DB_REPLACED_DETAIL = {
"message": "state.db was replaced while Hermes was running — stop Hermes, run `hermes doctor`, "
"then start it again. Do not run `hermes doctor --fix`, which would repair the wrong file in place.",
}
+# Every other bucket a malformed image can classify as ("corrupt", "fts_index") is the corrupt payload.
+_STORE_STATUS_DETAIL_BY_CAUSE = {"deleted_wal": DELETED_WAL_DETAIL, "replaced": STATE_DB_REPLACED_DETAIL}
@contextlib.contextmanager
@@ -180,20 +184,16 @@ def corrupt_store_as_status(db_path):
"""Map a corrupt-image ``sqlite3.DatabaseError`` or ``StateDbReplacedError`` from a state.db read to a 503 status
payload, warning once per store per :data:`_CORRUPT_STORE_WARN_INTERVAL_S`.
Busy/locked and every other error propagate unchanged."""
- from hermes_state_errors import is_malformed_db_error, StateDbReplacedError, DeletedWalGenerationError
+ from hermes_state_errors import StateDbReplacedError, classify_persistence_error, is_malformed_db_error
try:
yield
except (sqlite3.DatabaseError, StateDbReplacedError) as exc:
- if isinstance(exc, sqlite3.DatabaseError) and not is_malformed_db_error(exc):
+ if not isinstance(exc, StateDbReplacedError) and not is_malformed_db_error(exc):
raise
key, now = str(db_path), time.monotonic()
last = _corrupt_store_warned_at.get(key)
- detail = CORRUPT_STORE_DETAIL
- if isinstance(exc, DeletedWalGenerationError):
- detail = DELETED_WAL_DETAIL
- elif isinstance(exc, StateDbReplacedError):
- detail = STATE_DB_REPLACED_DETAIL
+ detail = _STORE_STATUS_DETAIL_BY_CAUSE.get(classify_persistence_error(exc), CORRUPT_STORE_DETAIL)
if last is None or now - last >= _CORRUPT_STORE_WARN_INTERVAL_S:
_corrupt_store_warned_at[key] = now
log.warning("state.db at %s is unreadable (%s); dashboard reads return a status payload until it is "
diff --git a/tests/hermes_cli/test_web_analytics_corrupt_store.py b/tests/hermes_cli/test_web_analytics_corrupt_store.py
index 3f8372a157b..275d8287c3e 100644
--- a/tests/hermes_cli/test_web_analytics_corrupt_store.py
+++ b/tests/hermes_cli/test_web_analytics_corrupt_store.py
@@ -1,5 +1,6 @@
"""A malformed state.db must not turn dashboard analytics polling into a traceback storm (#96591)."""
import logging
+import re
import sqlite3
from pathlib import Path
@@ -73,7 +74,7 @@ def test_corrupt_store_as_status_maps_replaced_store_errors_to_503_without_fix_n
monkeypatch.setattr(_common, "_corrupt_store_warned_at", {})
db_path = tmp_path / "state.db"
- for exc_cls, code in ((DeletedWalGenerationError, "deleted_wal"), (StateDbReplacedError, "state_db_replaced")):
+ for exc_cls, code in ((DeletedWalGenerationError, "state_db_deleted_wal"), (StateDbReplacedError, "state_db_replaced")):
with pytest.raises(HTTPException) as info:
with _common.corrupt_store_as_status(db_path):
raise exc_cls("retired WAL held by pid 4242")
@@ -83,7 +84,8 @@ def test_corrupt_store_as_status_maps_replaced_store_errors_to_503_without_fix_n
msg = info.value.detail["message"]
assert "run `hermes doctor`" in msg
# `--fix` may only appear negated — never as the action to take while a holder is live.
- assert msg.count("`hermes doctor --fix`") == 1 and "Do not run `hermes doctor --fix`" in msg
+ negated = re.findall(r"(?i)(?:do not|don't|never) run `hermes doctor --fix`", msg)
+ assert len(negated) == msg.count("`hermes doctor --fix`"), msg
with pytest.raises(sqlite3.OperationalError):
with _common.corrupt_store_as_status(db_path):