Skip to content

The agent that grows with you

fix(usage): preserve routed profile context in Nous account fetches

NanPanf1b7ae720670
diff --git a/agent/account_usage.py b/agent/account_usage.py
index 2b7346c3e3d..a828f9d47b8 100644
--- a/agent/account_usage.py
+++ b/agent/account_usage.py
@@ -186,9 +186,11 @@ def _nous_logged_in() -> bool:
def _fetch_portal_account(timeout: float):
"""Wall-clock-bounded fresh portal account fetch (raises on any failure/timeout)."""
import concurrent.futures
+ import contextvars
from hermes_cli.nous_account import get_nous_portal_account_info
+ context = contextvars.copy_context()
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as pool:
- return pool.submit(get_nous_portal_account_info, force_fresh=True).result(timeout=timeout)
+ return pool.submit(context.run, get_nous_portal_account_info, force_fresh=True).result(timeout=timeout)
def nous_credits_lines(*, markdown: bool = False, timeout: float = 10.0) -> list[str]:
diff --git a/agent/billing_usage.py b/agent/billing_usage.py
index 1a9f9f5a257..93c48985c17 100644
--- a/agent/billing_usage.py
+++ b/agent/billing_usage.py
@@ -49,9 +49,11 @@ def nous_logged_in() -> bool:
def fetch_nous_account(timeout: float):
"""Wall-clock-bounded fresh portal account fetch. Raises on failure/timeout."""
import concurrent.futures
+ import contextvars
from hermes_cli.nous_account import get_nous_portal_account_info
+ context = contextvars.copy_context()
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as pool:
- return pool.submit(get_nous_portal_account_info, force_fresh=True).result(timeout=timeout)
+ return pool.submit(context.run, get_nous_portal_account_info, force_fresh=True).result(timeout=timeout)
def format_renews(value: Optional[str]) -> Optional[str]:
diff --git a/tests/agent/test_nous_usage_profile_context.py b/tests/agent/test_nous_usage_profile_context.py
new file mode 100644
index 00000000000..b995a7db724
--- /dev/null
+++ b/tests/agent/test_nous_usage_profile_context.py
@@ -0,0 +1,46 @@
+from pathlib import Path
+
+import pytest
+
+from agent import account_usage, billing_usage
+from hermes_constants import (
+ get_hermes_home,
+ reset_hermes_home_override,
+ set_hermes_home_override,
+)
+
+
+ "fetcher",
+ [account_usage._fetch_portal_account, billing_usage.fetch_nous_account],
+)
+def test_nous_account_fetch_preserves_profile_home_in_timeout_worker(
+ monkeypatch, tmp_path: Path, fetcher
+):
+ """The bounded account fetch must read auth from the routed profile, not the launch profile."""
+ launch_home = tmp_path / "launch"
+ profile_home = tmp_path / "profiles" / "secondary"
+ launch_home.mkdir()
+ profile_home.mkdir(parents=True)
+ monkeypatch.setenv("HERMES_HOME", str(launch_home))
+
+ observed_homes: list[Path] = []
+
+ def fake_account_fetch(*, force_fresh: bool = False):
+ assert force_fresh is True
+ observed_homes.append(get_hermes_home())
+ return observed_homes[-1]
+
+ monkeypatch.setattr(
+ "hermes_cli.nous_account.get_nous_portal_account_info",
+ fake_account_fetch,
+ )
+
+ token = set_hermes_home_override(profile_home)
+ try:
+ assert get_hermes_home() == profile_home
+ assert fetcher(1.0) == profile_home
+ finally:
+ reset_hermes_home_override(token)
+
+ assert observed_homes == [profile_home]