Skip to content

The agent that grows with you

fix(dashboard): a forwarded storage failure answers 503, not a 409 conflict

Teknium5615892f0601
diff --git a/hermes_cli/web_server_sessions.py b/hermes_cli/web_server_sessions.py
index ec185a33060..e2f0167372c 100644
--- a/hermes_cli/web_server_sessions.py
+++ b/hermes_cli/web_server_sessions.py
@@ -236,6 +236,13 @@ async def _mutate_via_gateway_owner(profile, actor, params):
raise HTTPException(status_code=503, detail='owner_unreachable') from exc
+# One reason -> HTTP status map for a session mutation refusal, shared by the direct path
+# (RuntimeStoreError) and the owner forward (GatewayClientError): a storage failure is a
+# server fault (503), never a session conflict.
+_MUTATION_REFUSAL_STATUS = {'permission_denied': 403, 'profile_mismatch': 403, 'not_found': 404,
+ 'invalid_params': 400, 'runtime_draining': 503, 'storage_unavailable': 503}
+
+
async def _mutate_session_request(request, profile, session_id, *, request_id,
expected_revision, operation, payload, expected_generation=None):
"""Resolve the authenticated principal and owner; never open a second writer."""
@@ -275,15 +282,11 @@ async def _mutate_session_request(request, profile, session_id, *, request_id,
result = await mutate_session(authority, actor, SessionRef(authority.profile_id, session_id), params)
return {'ok': True, **result}
except RuntimeStoreError as exc:
- status = {'permission_denied': 403, 'profile_mismatch': 403, 'not_found': 404,
- 'invalid_params': 400, 'runtime_draining': 503}.get(exc.reason, 409)
- raise HTTPException(status_code=status, detail=exc.reason) from exc
+ raise HTTPException(status_code=_MUTATION_REFUSAL_STATUS.get(exc.reason, 409), detail=exc.reason) from exc
except GatewayClientError as exc:
- # The owner's own refusal verdicts, wire-shaped like RuntimeStoreError's
- # reasons (plus dial-time failures that read as the default 409).
- status = {'permission_denied': 403, 'profile_mismatch': 403, 'not_found': 404,
- 'invalid_params': 400, 'runtime_draining': 503}.get(str(exc), 409)
- raise HTTPException(status_code=status, detail=str(exc)) from exc
+ # The owner's own refusal verdicts, wire-shaped like RuntimeStoreError's reasons, so a
+ # forwarded write answers exactly as the direct path (dial-time failures read as 409).
+ raise HTTPException(status_code=_MUTATION_REFUSAL_STATUS.get(str(exc), 409), detail=str(exc)) from exc
except MUTATION_GUARD_REFUSALS as exc:
raise HTTPException(status_code=409, detail='session_busy') from exc
except ValueError as exc:
diff --git a/tests/hermes_cli/test_web_server_session_owner_forward.py b/tests/hermes_cli/test_web_server_session_owner_forward.py
index 2dede1329bc..7d6b70690db 100644
--- a/tests/hermes_cli/test_web_server_session_owner_forward.py
+++ b/tests/hermes_cli/test_web_server_session_owner_forward.py
@@ -216,3 +216,30 @@ async def test_a_multiplexed_profile_keeps_the_multiplexers_socket(tmp_path, mon
await web_server_sessions._mutate_via_gateway_owner('work', SimpleNamespace(subject='x'), dict(
session_id='s', request_id='r', expected_revision=0, operation='sidebar', payload={}))
assert minted['socket_home'] == root
+
+
[email protected]('reason, status', [('storage_unavailable', 503), ('runtime_draining', 503),
+ ('not_found', 404), ('stale_revision', 409)])
+async def test_a_forwarded_refusal_keeps_the_direct_paths_status(tmp_path, monkeypatch, reason, status):
+ """The owner sends ``storage_unavailable`` for a sqlite failure; forwarded, it must stay the
+ 503 the direct path answers, not read as a 409 session conflict."""
+ from hermes_cli.gateway_client import GatewayClientError
+ from hermes_cli import web_server_sessions
+
+ request = _standalone_request(monkeypatch, tmp_path)
+ _discover(monkeypatch, 'owner-endpoint')
+
+ @asynccontextmanager
+ async def refusing_connect(endpoint=None, subject=None):
+ class Refuser:
+ async def rpc(self, method, **params):
+ raise GatewayClientError(reason)
+ yield Refuser()
+
+ monkeypatch.setattr('hermes_cli.gateway_client.connect_gateway', refusing_connect)
+ with pytest.raises(HTTPException) as exc:
+ await web_server_sessions._mutate_session_request(
+ request, None, 'session-5', request_id='req-5', expected_revision=0,
+ operation='archive', payload={})
+ assert (exc.value.status_code, exc.value.detail) == (status, reason)