The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(dashboard): xAI and MiniMax sign-ins save into the profile they started in, never after a cancel
John Paul Soliva08caaceb35c5
diff --git a/hermes_cli/web_server_oauth.py b/hermes_cli/web_server_oauth.pyindex e90c5afb981..d51bcfbb6b6 100644--- a/hermes_cli/web_server_oauth.py+++ b/hermes_cli/web_server_oauth.py@@ -184,14 +184,6 @@ def _oauth_profile_name(profile: Optional[str]) -> Optional[str]:return requested-def _oauth_session_profile(session_id: str, fallback: Optional[str] = None) -> Optional[str]:- """Return the profile that owns an OAuth session, if one was provided."""- with _oauth_sessions_lock:- sess = _oauth_sessions.get(session_id)- profile = sess.get("profile") if sess else None- return profile or _oauth_profile_name(fallback)--def _oauth_poller(label: str):"""Wrap a device-code poller body ``fn(session_id, sess)``: vanished session is a no-op,success marks ``approved``, any exception records ``error`` + ``error_message`` on the@@ -347,7 +339,9 @@ def _nous_plain_poller(session_id: str, sess: Dict[str, Any]) -> None:),"expires_in": token_ttl,}- with _profile_scope(_oauth_session_profile(session_id)):+ # The profile comes from the poller's own session dict: a cancel or the 15-minute sweep drops+ # the registry entry, and a lookup by id would then save into the dashboard's launch profile.+ with _profile_scope(sess.get("profile")):full_state = refresh_nous_oauth_from_state(auth_state, timeout_seconds=15.0, force_refresh=False)# The final cancellation check and the save share the session lock, so a cancel cannot# land between them.@@ -401,8 +395,14 @@ def _minimax_poller(session_id: str, sess: Dict[str, Any]) -> None:"expires_at": datetime.fromtimestamp(expires_at_ts, tz=timezone.utc).isoformat(),"expires_in": max(0, int(expires_at_ts - now.timestamp())),}- with _profile_scope(_oauth_session_profile(session_id)):- _minimax_save_auth_state(auth_state)+ with _profile_scope(sess.get("profile")):+ # The cancellation check and the save share the session lock, so a cancel cannot land+ # between them (the same contract as the Nous and Codex savers).+ with _oauth_sessions_lock:+ if sess.get("cancelled"):+ sess["status"] = "cancelled"+ return+ _minimax_save_auth_state(auth_state)@_oauth_poller("xai")@@ -428,7 +428,11 @@ def _xai_device_poller(session_id: str, sess: Dict[str, Any]) -> None:"expires_in": token_data.get("expires_in"),"token_type": str(token_data.get("token_type") or "Bearer").strip() or "Bearer",}- with _profile_scope(_oauth_session_profile(session_id)):+ with _profile_scope(sess.get("profile")), _oauth_sessions_lock:+ # One critical section with the cancel check, as in the Nous and Codex savers.+ if sess.get("cancelled"):+ sess["status"] = "cancelled"+ return# set_active=False: persist without hijacking an existing active chat provider._save_xai_oauth_tokens(tokens, discovery=discovery, auth_mode="oauth_device_code", set_active=False,diff --git a/tests/hermes_cli/test_web_oauth_dispatch.py b/tests/hermes_cli/test_web_oauth_dispatch.pyindex ad7191cab60..019906cf022 100644--- a/tests/hermes_cli/test_web_oauth_dispatch.py+++ b/tests/hermes_cli/test_web_oauth_dispatch.py@@ -26,6 +26,7 @@ import timefrom unittest.mock import patchimport httpx+import pytestfrom fastapi.testclient import TestClientfrom hermes_cli.web_server import _SESSION_TOKEN, app@@ -728,3 +729,49 @@ def test_qwen_status_card_carries_the_getter_expiry_token_and_auth_file():++[email protected]("provider", ["xai-oauth", "minimax-oauth"])[email protected]("event", ["cancel", "expire"])+def test_dashboard_poller_saves_only_into_the_profile_the_login_started_in(tmp_path, monkeypatch, provider, event):+ """A login started with ``?profile=coder`` saves into coder or nowhere. The pollers looked the+ profile up by session id AFTER the provider answered; a cancel (DELETE) or the 15-minute sweep+ had removed the registry entry by then, so the tokens landed in the dashboard's launch profile+ (the Codex path was fixed the same way as IA-01). A cancelled login saves nothing."""+ from hermes_cli import auth as auth_mod+ from hermes_constants import get_hermes_home++ coder_home = _make_profile_home(tmp_path, monkeypatch, profile="coder")+ saved_into = []+ sid, sess = _rt_oauth._new_oauth_session(provider, "device_code", profile="coder")++ def provider_answers(*_a, **_k):+ if event == "cancel":+ resp = client.delete(f"/api/providers/oauth/sessions/{sid}?profile=coder", headers=HEADERS)+ assert resp.status_code == 200, resp.text+ else:+ _web_server_oauth._oauth_sessions.pop(sid, None)+ return {"access_token": "at", "refresh_token": "rt", "id_token": "", "expires_in": 3600,+ "expired_in": 3600, "token_type": "Bearer"}++ record = lambda *_a, **_k: saved_into.append(str(get_hermes_home()))+ if provider == "xai-oauth":+ sess.update(device_code="dc", expires_at=time.time() + 600, interval=1)+ monkeypatch.setattr(auth_mod, "_xai_oauth_discovery", lambda *a, **k: {"token_endpoint": "https://x.test/t"})+ monkeypatch.setattr(auth_mod, "_xai_oauth_poll_device_token", provider_answers)+ monkeypatch.setattr(auth_mod, "_save_xai_oauth_tokens", record)+ monkeypatch.setattr(auth_mod, "mark_provider_active_if_unset", lambda *a, **k: None)+ monkeypatch.setattr(auth_mod, "unsuppress_credential_source", lambda *a, **k: None)+ poller = _web_server_oauth._xai_device_poller+ else:+ sess.update(portal_base_url="https://m.test", client_id="cid", user_code="uc", code_verifier="cv",+ expired_in_raw=600, interval_ms=1000)+ monkeypatch.setattr(auth_mod, "_minimax_poll_token", provider_answers)+ monkeypatch.setattr(auth_mod, "_minimax_save_auth_state", record)+ poller = _web_server_oauth._minimax_poller+ try:+ poller(sid)+ finally:+ _web_server_oauth._oauth_sessions.pop(sid, None)++ assert saved_into == ([] if event == "cancel" else [str(coder_home)])