The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(web): give the managed-search refusal an accurate reason
kshitijk4poor509d0f12d4ee
diff --git a/hermes_cli/nous_account.py b/hermes_cli/nous_account.pyindex c6046d2779e..7ba90a015c0 100644--- a/hermes_cli/nous_account.py+++ b/hermes_cli/nous_account.py@@ -381,7 +381,8 @@ def _fresh_account_info(state: dict[str, Any], force_fresh: bool, portal_base_ur_account_info_cache = (cache_key, time.monotonic(), info)return infoexcept Exception as exc:- return _error_info(error=exc, logged_in=bool(state.get("access_token")), portal_base_url=portal_base_url)+ return _error_info(error=exc, logged_in=bool(state.get("access_token")),+ portal_base_url=portal_base_url, account_tier=_coerce_str(state.get("account_tier")))def _info_from_inference_key_pool(portal_base_url: Optional[str]) -> Optional[NousPortalAccountInfo]:@@ -428,7 +429,8 @@ def _info_from_oauth_pool(try:return _info_from_fetched_account(access_token, state, entry_portal_url)except Exception as exc:- return _error_info(error=exc, logged_in=True, portal_base_url=entry_portal_url)+ return _error_info(error=exc, logged_in=True, portal_base_url=entry_portal_url,+ account_tier=_coerce_str(state.get("account_tier")))def _info_from_fetched_account(@@ -436,12 +438,14 @@ def _info_from_fetched_account() -> NousPortalAccountInfo:"""Call ``/api/oauth/account`` and normalize; empty or ``error`` payloads become error infos."""payload = _fetch_nous_account_info(access_token, portal_base_url)+ tier = _coerce_str(state.get("account_tier"))if not payload:- return _error_info(error="empty_account_response", logged_in=True, portal_base_url=portal_base_url)+ return _error_info(error="empty_account_response", logged_in=True,+ portal_base_url=portal_base_url, account_tier=tier)if isinstance(payload.get("error"), str):return _error_info(error=payload["error"] or "account_response_error", logged_in=True,- portal_base_url=portal_base_url, raw_account=payload,+ portal_base_url=portal_base_url, raw_account=payload, account_tier=tier,)return _info_from_account_payload(payload, state=state, portal_base_url=portal_base_url)@@ -576,11 +580,14 @@ def _subscription_from_payload(value: Any) -> Optional[NousPortalSubscriptionInfdef _error_info(- *, error: object, logged_in: bool, portal_base_url: Optional[str] = None, raw_account: Optional[dict[str, Any]] = None+ *, error: object, logged_in: bool, portal_base_url: Optional[str] = None,+ raw_account: Optional[dict[str, Any]] = None, account_tier: Optional[str] = None,) -> NousPortalAccountInfo:+ """A failed-lookup snapshot. ``account_tier`` is carried through when the caller still holds the+ stored state: without it a guest whose lookup failed reads as a registered identity."""return NousPortalAccountInfo(logged_in=logged_in, source="error", fresh=False, portal_base_url=portal_base_url,- raw_account=raw_account, error=str(error),+ raw_account=raw_account, error=str(error), account_tier=account_tier,)diff --git a/plugins/web/perplexity/provider.py b/plugins/web/perplexity/provider.pyindex eb2bb6cfa99..eb0b0f2a10f 100644--- a/plugins/web/perplexity/provider.py+++ b/plugins/web/perplexity/provider.py@@ -222,14 +222,11 @@ class PerplexityWebSearchProvider(WebSearchProvider):managed = False if direct else _managed_web_search()gateway = _managed_gateway(managed=managed) if managed else Noneif gateway is None and managed:- from hermes_cli.nous_account import FREE_TIER_NEEDS_ACCOUNT, get_nous_portal_account_info- from tools.tool_backend_helpers import NOUS_MANAGED_PROVIDER, selection_error-- # A guest holds a real identity but is excluded by tier, so it must not be told- # there is no identity.- failure = (FREE_TIER_NEEDS_ACCOUNT if get_nous_portal_account_info().is_anonymous_tier- else "the Nous Tool Gateway is not available (unreachable)")- raise ValueError(selection_error("web", NOUS_MANAGED_PROVIDER, failure))+ from tools.tool_backend_helpers import (+ NOUS_MANAGED_PROVIDER, fast_search_unavailable_message, selection_error)++ raise ValueError(selection_error("web", NOUS_MANAGED_PROVIDER,+ fast_search_unavailable_message()))logger.info("Perplexity search: '%s' (limit=%d%s)", query, limit, ", managed" if gateway else "")payload = {"query": query,diff --git a/tests/tools/test_web_free_fast_search.py b/tests/tools/test_web_free_fast_search.pyindex cc9de07f8d2..8d21e644e97 100644--- a/tests/tools/test_web_free_fast_search.py+++ b/tests/tools/test_web_free_fast_search.py@@ -22,7 +22,12 @@ def _nous_state(claims: dict, auth_method: str, exp_offset: int = 3600) -> dict:def seg(obj):return base64.urlsafe_b64encode(json.dumps(obj).encode()).rstrip(b"=").decode()token = f"{seg({'alg': 'none'})}.{seg({**claims, 'exp': int(time.time()) + exp_offset})}.sig"- return {"auth_method": auth_method, "access_token": token, "expires_at": "2099-01-01T00:00:00Z"}+ state = {"auth_method": auth_method, "access_token": token, "expires_at": "2099-01-01T00:00:00Z"}+ # anon_auth persists the tier alongside the token, and it is what survives a failed lookup+ # (the JWT claim is unreachable once the token is inside its refresh window).+ if claims.get("account_tier"):+ state["account_tier"] = claims["account_tier"]+ return state@pytest.fixture@@ -157,18 +162,35 @@ def test_degraded_portal_lookup_does_not_grant_free_fast_search(monkeypatch, tmpdef test_guest_on_a_stored_nous_selection_is_told_it_needs_an_account(monkeypatch, tmp_path, gateway_server):"""A guest holds a real Nous identity, so the refusal must not claim it has none."""- from hermes_cli.nous_account import FREE_TIER_NEEDS_ACCOUNT-_write_home(tmp_path / "home", monkeypatch, nous_state=_nous_state(ANON, "anonymous"),config={"web": {"backend": "nous", "keyless_rescue": False}})result = _search()assert result["success"] is False- assert FREE_TIER_NEEDS_ACCOUNT in result["error"]+ assert "needs a Nous account" in result["error"]assert "no Nous identity" not in result["error"]+def test_guest_whose_lookup_fails_is_still_told_it_needs_an_account(monkeypatch, tmp_path, gateway_server):+ """A failed lookup must not make a guest look like a registered identity in the refusal."""+ from hermes_cli.nous_account import get_nous_portal_account_info++ state = _nous_state(ANON, "anonymous", exp_offset=30)+ state["portal_base_url"] = "http://127.0.0.1:9" # closed port: the lookup cannot succeed+ _write_home(tmp_path / "home", monkeypatch, nous_state=state,+ config={"web": {"backend": "nous", "keyless_rescue": False}})++ info = get_nous_portal_account_info()+ # Premise: the snapshot carries the error AND the tier it could not re-read.+ assert info.error is not None and info.is_anonymous_tier is True++ result = _search()++ assert result["success"] is False+ assert "needs a Nous account" in result["error"]++def test_free_fast_search_failure_skips_paid_firecrawl_but_keeps_keyless_rescue(monkeypatch, tmp_path, gateway_server):from plugins.web import keyless_mcpdiff --git a/tests/tools/test_web_managed_routing.py b/tests/tools/test_web_managed_routing.pyindex be8d3cd658f..93478f56aa9 100644--- a/tests/tools/test_web_managed_routing.py+++ b/tests/tools/test_web_managed_routing.py@@ -135,7 +135,7 @@ def test_managed_search_without_identity_names_the_gateway(monkeypatch, tmp_pathregister_all_web_providers()error = json.loads(web_tools.web_search_tool("local fixture", limit=3))["error"]- assert "Nous Tool Gateway" in error and "hermes tools" in error+ assert "no usable Nous identity" in error and "hermes tools" in errorassert "PERPLEXITY_API_KEY" not in errorassert local_gateway == []diff --git a/tools/tool_backend_helpers.py b/tools/tool_backend_helpers.pyindex c11e2026053..38a3bca23fc 100644--- a/tools/tool_backend_helpers.py+++ b/tools/tool_backend_helpers.py@@ -42,10 +42,28 @@ def fast_search_entitled() -> bool:(no credit requirement) and so must reject an error snapshot itself: a failed lookup is stillstamped ``logged_in=True``, and only ``error`` distinguishes it from a real account."""account_info = _account_info()+ # ``error`` is load-bearing, not belt-and-braces: a failed lookup is still stamped+ # ``logged_in=True``, and the error paths that cannot recover the tier from stored state would+ # otherwise read as a registered identity.return bool(account_info is not None and account_info.logged_inand account_info.error is None and not account_info.is_anonymous_tier)+def fast_search_unavailable_message() -> str:+ """Why the managed Perplexity search route did not resolve, phrased as a clause for+ :func:`selection_error`'s "but …" template and kept beside :func:`fast_search_entitled` so the+ predicate and its explanation cannot drift.++ ``FREE_TIER_NEEDS_ACCOUNT`` and ``_CHAT`` are standalone sentences, so the guest case is phrased+ as the matching clause rather than embedded verbatim."""+ account_info = _account_info()+ if account_info is not None and account_info.is_anonymous_tier:+ return "it needs a Nous account (sign in with `/login`)"+ if account_info is None or account_info.error is not None or not account_info.logged_in:+ return "there is no usable Nous identity (sign in with `/login`)"+ return "the Nous Tool Gateway is unreachable"++def nous_tool_gateway_unavailable_message(capability: str = "the Nous Tool Gateway", *,force_fresh: bool = False) -> str:"""Return account-aware guidance for an unavailable Nous Tool Gateway path."""