The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(tui_gateway): setup.status / setup.runtime_check answer for the requested profile
Teknium16370ae5394c
diff --git a/hermes_cli/main.py b/hermes_cli/main.pyindex 86454f5af5b..b801ac399df 100644--- a/hermes_cli/main.py+++ b/hermes_cli/main.py@@ -1053,8 +1053,14 @@ def _relative_time(ts) -> str:return relative_time(ts)-def _has_any_provider_configured() -> bool:- """Check if at least one inference provider is usable."""+def _has_any_provider_configured(*, strict_profile_scope: bool = False) -> bool:+ """Check if at least one inference provider is usable.++ ``strict_profile_scope``: the caller has bound a NAMED profile's home and+ secret scope and wants an answer for that profile only — launch-process+ env and host-wide fallbacks (gh auth, Claude Code credentials) must not+ make it appear ready. Unscoped callers keep the legacy behavior.+ """from hermes_cli.config import get_env_path, get_hermes_home, load_configfrom hermes_cli.auth import get_auth_status@@ -1097,7 +1103,13 @@ def _has_any_provider_configured() -> bool:for pconfig in PROVIDER_REGISTRY.values():if pconfig.auth_type == "api_key":provider_env_vars.update(pconfig.api_key_env_vars)- if any(os.getenv(v) for v in provider_env_vars):+ if strict_profile_scope:+ from agent.secret_scope import current_secret_scope++ read_provider_env = (current_secret_scope() or {}).get+ else:+ read_provider_env = os.getenv+ if any(read_provider_env(v) for v in provider_env_vars):return True# Check .env file for keys@@ -1129,7 +1141,10 @@ def _has_any_provider_configured() -> bool:auth = json.loads(auth_file.read_text(encoding="utf-8-sig"))active = auth.get("active_provider")- if active:+ active_config = PROVIDER_REGISTRY.get(str(active or "").strip().lower())+ if active and not (+ strict_profile_scope and active_config and active_config.auth_type == "api_key"+ ):status = get_auth_status(active)if status.get("logged_in"):return True@@ -1148,20 +1163,21 @@ def _has_any_provider_configured() -> bool:return True# Check provider-specific auth fallbacks (for example, Copilot via gh auth).- try:- for provider_id, pconfig in PROVIDER_REGISTRY.items():- if pconfig.auth_type != "api_key":- continue- status = get_auth_status(provider_id)- if status.get("logged_in"):- return True- except Exception:- pass+ if not strict_profile_scope:+ try:+ for provider_id, pconfig in PROVIDER_REGISTRY.items():+ if pconfig.auth_type != "api_key":+ continue+ status = get_auth_status(provider_id)+ if status.get("logged_in"):+ return True+ except Exception:+ pass# Check for Claude Code OAuth credentials (~/.claude/.credentials.json)# Only count these if Hermes has been explicitly configured — Claude Code# being installed doesn't mean the user wants Hermes to use their tokens.- if _has_hermes_config:+ if _has_hermes_config and not strict_profile_scope:try:from agent.anthropic_adapter import (read_claude_code_credentials,diff --git a/tests/test_tui_gateway_server.py b/tests/test_tui_gateway_server.pyindex bf605e79c04..b5ea0fccdcd 100644--- a/tests/test_tui_gateway_server.py+++ b/tests/test_tui_gateway_server.py@@ -8903,7 +8903,7 @@ def test_enable_gateway_prompts_sets_gateway_env(monkeypatch):def test_setup_status_reports_provider_config(monkeypatch):- monkeypatch.setattr("hermes_cli.main._has_any_provider_configured", lambda: False)+ monkeypatch.setattr("hermes_cli.main._has_any_provider_configured", lambda **_kw: False)resp = server.handle_request({"id": "1", "method": "setup.status", "params": {}})@@ -8925,7 +8925,7 @@ def test_probe_credentials_allows_keyless_custom_runtime():def test_setup_runtime_check_rejects_empty_runtime_key(monkeypatch):- monkeypatch.setattr("hermes_cli.main._has_any_provider_configured", lambda: True)+ monkeypatch.setattr("hermes_cli.main._has_any_provider_configured", lambda **_kw: True)monkeypatch.setattr("hermes_cli.runtime_provider.resolve_runtime_provider",lambda requested=None: {@@ -8947,7 +8947,7 @@ def test_setup_runtime_check_rejects_empty_runtime_key(monkeypatch):def test_setup_runtime_check_allows_no_key_custom_runtime(monkeypatch):- monkeypatch.setattr("hermes_cli.main._has_any_provider_configured", lambda: True)+ monkeypatch.setattr("hermes_cli.main._has_any_provider_configured", lambda **_kw: True)monkeypatch.setattr("hermes_cli.runtime_provider.resolve_runtime_provider",lambda requested=None: {@@ -8964,7 +8964,7 @@ def test_setup_runtime_check_allows_no_key_custom_runtime(monkeypatch):def test_setup_runtime_check_rejects_implicit_bedrock_when_unconfigured(monkeypatch):- monkeypatch.setattr("hermes_cli.main._has_any_provider_configured", lambda: False)+ monkeypatch.setattr("hermes_cli.main._has_any_provider_configured", lambda **_kw: False)monkeypatch.setattr("hermes_cli.runtime_provider.resolve_runtime_provider",lambda requested=None: {@@ -8982,7 +8982,7 @@ def test_setup_runtime_check_rejects_implicit_bedrock_when_unconfigured(monkeypadef test_setup_runtime_check_honors_requested_provider(monkeypatch):"""Onboarding must be able to validate the provider the user just connected."""- monkeypatch.setattr("hermes_cli.main._has_any_provider_configured", lambda: True)+ monkeypatch.setattr("hermes_cli.main._has_any_provider_configured", lambda **_kw: True)def fake_resolve(requested=None, **kwargs):if requested == "nous":@@ -9013,6 +9013,67 @@ def test_setup_runtime_check_honors_requested_provider(monkeypatch):assert default["result"]["provider"] == "anthropic"+def test_setup_readiness_scopes_to_requested_profile(monkeypatch, tmp_path):+ """#94071: the Desktop preflights a freshly created bot on its target+ backend. ``profile`` binds THAT profile's home + .env — launch-process+ credentials must not make an unconfigured bot look ready, and the bot's+ own .env must be what the strict check sees."""+ from agent import secret_scope+ from hermes_constants import get_hermes_home++ bot_home = tmp_path / "profiles" / "bot"+ bot_home.mkdir(parents=True)+ monkeypatch.setenv("OPENROUTER_API_KEY", "sk-or-launch-profile-secret-0000")+ monkeypatch.setattr("hermes_cli.profiles.profile_exists", lambda name: name == "bot")+ monkeypatch.setattr(server, "_profile_home", lambda profile: bot_home if profile == "bot" else None)+ seen = {}++ def fake_resolve(requested=None, **kwargs):+ seen["home"] = Path(str(get_hermes_home())).resolve()+ seen["secret"] = secret_scope.get_secret("OPENROUTER_API_KEY")+ return {"provider": "openrouter", "api_key": seen["secret"] or "", "source": "env"}++ monkeypatch.setattr("hermes_cli.runtime_provider.resolve_runtime_provider", fake_resolve)++ secret_scope.set_multiplex_active(True)+ try:+ status = server.handle_request(+ {"id": "1", "method": "setup.status", "params": {"profile": "bot"}}+ )+ assert status["result"] == {"provider_configured": False, "profile": "bot"}++ (bot_home / ".env").write_text("OPENROUTER_API_KEY=sk-or-bot-profile-secret-00001\n")+ status = server.handle_request(+ {"id": "2", "method": "setup.status", "params": {"profile": "bot"}}+ )+ runtime = server.handle_request(+ {"id": "3", "method": "setup.runtime_check", "params": {"profile": "bot"}}+ )+ finally:+ secret_scope.set_multiplex_active(False)++ assert status["result"] == {"provider_configured": True, "profile": "bot"}+ assert runtime["result"]["ok"] is True+ assert runtime["result"]["profile"] == "bot"+ assert seen == {"home": bot_home.resolve(), "secret": "sk-or-bot-profile-secret-00001"}+ assert Path(str(get_hermes_home())).resolve() != bot_home.resolve()+++def test_setup_readiness_unknown_profile_never_answers_for_launch_profile(monkeypatch):+ monkeypatch.setattr("hermes_cli.main._has_any_provider_configured", lambda **_kw: True)+ monkeypatch.setattr(+ "hermes_cli.runtime_provider.resolve_runtime_provider",+ lambda requested=None, **kw: {"provider": "openrouter", "api_key": "sk-or-launch-0000000000", "source": "env"},+ )+ monkeypatch.setattr("hermes_cli.profiles.profile_exists", lambda name: False)++ for method in ("setup.status", "setup.runtime_check"):+ resp = server.handle_request({"id": "1", "method": method, "params": {"profile": "ghost"}})+ assert resp["result"]["ok"] is False+ assert resp["result"]["profile"] == "ghost"+ assert "does not exist" in resp["result"]["error"]++def test_complete_slash_drops_removed_provider_alias():# `/provider` was folded into a single `/model` command, so autocomplete# must no longer offer the dead alias...diff --git a/tui_gateway/methods_config.py b/tui_gateway/methods_config.pyindex 991390dd9a0..d0ffb2396e5 100644--- a/tui_gateway/methods_config.py+++ b/tui_gateway/methods_config.py@@ -389,12 +389,49 @@ def _(rid, params: dict) -> dict:return _err(rid, 4002, f"unknown config key: {key}")+def _readiness_profile_scope(params: dict):+ """Resolve the optional ``profile`` param of the setup readiness RPCs.++ Returns ``(profile, scope)`` where ``scope`` is a context manager binding+ that profile's HERMES_HOME and ``.env`` secret scope (ContextVars, so+ concurrent checks for different profiles stay isolated). The launch+ profile / no param yields ``("", nullcontext())``. A profile unknown to+ this host raises ``FileNotFoundError`` — a readiness check must never+ quietly answer for the launch profile instead (#94071).+ """+ import contextlib++ profile = str(params.get("profile") or "").strip() if isinstance(params, dict) else ""+ if not profile:+ return "", contextlib.nullcontext()+ from hermes_cli import profiles as profiles_mod+ from tui_gateway import server as _server++ if not profiles_mod.profile_exists(profile):+ raise FileNotFoundError(f"Profile '{profile}' does not exist on this backend.")+ home = _server._profile_home(profile)+ if home is None:+ return profile, contextlib.nullcontext()+ return profile, _server._session_profile_runtime_scope({"profile_home": str(home)})++@method("setup.status")def _(rid, params: dict) -> dict:+ """Loose provider check; ``profile`` (optional) scopes it to that profile's home."""try:from hermes_cli.main import _has_any_provider_configured+ from tui_gateway.methods_config import _readiness_profile_scope- return _ok(rid, {"provider_configured": bool(_has_any_provider_configured())})+ try:+ profile, scope = _readiness_profile_scope(params)+ except FileNotFoundError as e:+ return _ok(rid, {"ok": False, "profile": params.get("profile"), "error": str(e)})+ with scope:+ configured = bool(_has_any_provider_configured(strict_profile_scope=bool(profile)))+ payload = {"provider_configured": configured}+ if profile:+ payload["profile"] = profile+ return _ok(rid, payload)except Exception as e:return _err(rid, 5016, str(e))@@ -409,15 +446,27 @@ def _(rid, params: dict) -> dict:uses on session creation. It returns ok=False with the auth error messagewhen the user's configured model cannot actually be served, so UIs cansurface onboarding before the user submits a doomed prompt.++ ``profile`` (optional): answer for THAT profile's home on this host — its+ config.yaml model pin and its ``.env`` — instead of the launch profile's+ (#94071). A profile unknown to this backend answers ``ok=False`` rather+ than reporting the launch profile's readiness."""try:from hermes_cli.runtime_provider import resolve_runtime_providerfrom hermes_cli.auth import has_usable_secretfrom hermes_cli.main import _has_any_provider_configured+ from tui_gateway.methods_config import _readiness_profile_scoperequested = str(params.get("provider") or "").strip() or None- runtime = resolve_runtime_provider(requested=requested)- provider_configured = bool(_has_any_provider_configured())+ try:+ profile, scope = _readiness_profile_scope(params)+ except FileNotFoundError as e:+ return _ok(rid, {"ok": False, "profile": params.get("profile"), "error": str(e)})+ with scope:+ runtime = resolve_runtime_provider(requested=requested)+ provider_configured = bool(_has_any_provider_configured(strict_profile_scope=bool(profile)))+ scoped = {"profile": profile} if profile else {}provider = runtime.get("provider") or "provider"source = str(runtime.get("source") or "")if (@@ -437,6 +486,7 @@ def _(rid, params: dict) -> dict:"model": runtime.get("model"),"source": source,"error": "No Hermes provider is configured.",+ **scoped,},)@@ -458,6 +508,7 @@ def _(rid, params: dict) -> dict:"model": runtime.get("model"),"source": runtime.get("source"),"error": f"No usable credentials found for {provider}.",+ **scoped,},)@@ -468,6 +519,7 @@ def _(rid, params: dict) -> dict:"provider": runtime.get("provider"),"model": runtime.get("model"),"source": runtime.get("source"),+ **scoped,},)except Exception as e: