Skip to content

The agent that grows with you

fix(dashboard): map replaced/deleted-WAL store errors to 503 on GET /api/sessions too

kshitijk4poor4cc40727470e
diff --git a/hermes_cli/web_routers/sessions.py b/hermes_cli/web_routers/sessions.py
index 68d47f9cb70..6e7147e5b50 100644
--- a/hermes_cli/web_routers/sessions.py
+++ b/hermes_cli/web_routers/sessions.py
@@ -22,9 +22,11 @@ from hermes_cli.web_server_gateway import _strip_session_list_rows
from hermes_cli.web_server_sessions import _maybe_auto_archive_for_profile, _session_latest_descendant
from hermes_cli.web_models import (
BulkDeleteSessions, SessionImport, SessionOwnerBackfill, SessionPrune, SessionRename)
-from hermes_cli.web_routers._common import CORRUPT_STORE_DETAIL, log as _log, destructive_profile, http_failure
+from hermes_cli.web_routers._common import (
+ CORRUPT_STORE_DETAIL, corrupt_store_as_status, log as _log, destructive_profile, http_failure,
+)
from hermes_state import is_malformed_db_error
-from hermes_state_errors import is_transient_sqlite_error
+from hermes_state_errors import StateDbReplacedError, is_transient_sqlite_error
from hermes_state_health import STORAGE_CORRUPT, note_storage_error, storage_state
list_router = APIRouter()
@@ -160,6 +162,10 @@ def _resolve_session_id(db, session_id: str) -> Optional[str]:
"Sessions cannot be read until it is repaired — run "
"`hermes doctor` for diagnosis."),
) from exc
+ except StateDbReplacedError:
+ # RuntimeError family, not sqlite3: same 503 payload as the analytics reads (#110054).
+ with corrupt_store_as_status(db.db_path):
+ raise
# ``le=100`` on limit: an unbounded limit lets one request drag every session
@@ -250,6 +256,10 @@ def get_sessions(
raise HTTPException(status_code=500, detail="Internal server error") from exc
_log.error("GET /api/sessions: state.db at %s is corrupt: %s", db_path, exc)
raise HTTPException(status_code=503, detail=dict(CORRUPT_STORE_DETAIL)) from exc
+ except StateDbReplacedError:
+ # RuntimeError family, not sqlite3: same 503 payload as the analytics reads (#110054).
+ with corrupt_store_as_status(_session_db_path_for_profile(profile)):
+ raise
except Exception:
_log.exception("GET /api/sessions failed")
raise HTTPException(status_code=500, detail="Internal server error")
diff --git a/tests/hermes_cli/test_web_analytics_corrupt_store.py b/tests/hermes_cli/test_web_analytics_corrupt_store.py
index 275d8287c3e..ad670aa5c64 100644
--- a/tests/hermes_cli/test_web_analytics_corrupt_store.py
+++ b/tests/hermes_cli/test_web_analytics_corrupt_store.py
@@ -90,3 +90,31 @@ def test_corrupt_store_as_status_maps_replaced_store_errors_to_503_without_fix_n
with pytest.raises(sqlite3.OperationalError):
with _common.corrupt_store_as_status(db_path):
raise sqlite3.OperationalError("database is locked")
+
+ # Sibling route: GET /api/sessions and the session-id resolver used to let the same
+ # RuntimeError family fall through to a generic 500.
+ from hermes_cli.web_routers import sessions
+
+ class _RetiredDb:
+ db_path = str(tmp_path / "state.db")
+
+ def resolve_session_id(self, session_id):
+ raise DeletedWalGenerationError("retired WAL held by pid 4242")
+
+ def list_sessions_rich(self, **kwargs):
+ raise DeletedWalGenerationError("retired WAL held by pid 4242")
+
+ def close(self):
+ pass
+
+ with pytest.raises(HTTPException) as info:
+ sessions._resolve_session_id(_RetiredDb(), "abc")
+ assert (info.value.status_code, info.value.detail["error"]) == (503, "state_db_deleted_wal")
+
+ monkeypatch.setattr(sessions, "_maybe_auto_archive_for_profile", lambda profile: None)
+ monkeypatch.setattr(sessions, "_session_db_path_for_profile", lambda profile: db_path)
+ monkeypatch.setattr(sessions, "_open_session_db_for_profile", lambda profile, read_only: _RetiredDb())
+ app = FastAPI()
+ app.include_router(sessions.list_router)
+ resp = TestClient(app, raise_server_exceptions=False).get("/api/sessions")
+ assert (resp.status_code, resp.json()["detail"]["error"]) == (503, "state_db_deleted_wal")