Skip to content

The agent that grows with you

fix(desktop): /usage reads the session pick and the profile Nous login

Teknium37176f14d2fd
diff --git a/tests/tui_gateway/test_session_usage_account_lines.py b/tests/tui_gateway/test_session_usage_account_lines.py
index ad966ff60da..6ff20e663dc 100644
--- a/tests/tui_gateway/test_session_usage_account_lines.py
+++ b/tests/tui_gateway/test_session_usage_account_lines.py
@@ -20,8 +20,11 @@ def _snapshot() -> AccountUsageSnapshot:
def test_usage_slash_shows_account_limits_before_first_turn_inside_session_scope():
from tui_gateway import server
+ # Fresh chat whose composer picked Codex under an OpenRouter-default profile.
session = {"agent": None, "profile_home": "/nonexistent/hermes-profiles/coder", "history": [],
- "running": False, "session_key": "sess-usage"}
+ "running": False, "session_key": "sess-usage",
+ "model_override": {"model": "gpt-5.3-codex", "provider": "openai-codex"}}
+ fetched: list[str] = []
scoped: list[str] = []
class _Scope:
@@ -34,17 +37,23 @@ def test_usage_slash_shows_account_limits_before_first_turn_inside_session_scope
def _fetch(provider, *, base_url=None, api_key=None):
assert scoped == ["enter"], scoped # the profile's key is only readable inside its scope
+ fetched.append(provider)
return _snapshot()
+ def _credits(**kw):
+ assert scoped == ["enter"], scoped # the profile's own Nous login, not the launch profile's
+ return ["Nous credits: 100 remaining"]
+
with (
patch.object(server, "_session_usage_snapshot", return_value={}),
patch.object(server, "_session_profile_runtime_scope", lambda sess, **kw: _Scope()),
patch.object(server, "_config_model_target", lambda: ("openai/gpt-5-mini", "openrouter")),
patch("agent.account_usage.fetch_account_usage", _fetch),
- patch("agent.account_usage.nous_credits_lines", lambda **kw: []),
+ patch("agent.account_usage.nous_credits_lines", _credits),
):
out = server._live_slash_command_output("sid-usage", session, "usage", "")
assert scoped == ["enter", "exit"]
- assert "Account limits" in out and "openrouter" in out, out
+ assert fetched == ["openai-codex"]
+ assert "Account limits" in out and "Nous credits: 100 remaining" in out, out
assert "send a message first" not in out
diff --git a/tui_gateway/methods_session.py b/tui_gateway/methods_session.py
index 8643ff6b6d6..dc0d807adc7 100644
--- a/tui_gateway/methods_session.py
+++ b/tui_gateway/methods_session.py
@@ -1458,19 +1458,6 @@ def _(rid, params: dict, session: dict) -> dict:
return _ok(rid, usage)
-def _account_usage_lines(session: dict) -> list[str]:
- """Account-limit lines for the live agent's route, else ``model.provider``. Session scope: a
- non-launch profile's credentials are unreadable without it when multiplexed (#132422)."""
- from agent.account_usage import fetch_account_usage, render_account_usage_lines
- agent = session.get("agent")
- with _session_profile_runtime_scope(session):
- provider = getattr(agent, "provider", None) or _config_model_target()[1]
- if not provider:
- return []
- snapshot = fetch_account_usage(provider, base_url=getattr(agent, "base_url", None), api_key=getattr(agent, "api_key", None))
- return render_account_usage_lines(snapshot)
-
-
@_session_method("session.context_breakdown")
def _(rid, params: dict, session: dict) -> dict:
if (agent := session.get("agent")) is None:
diff --git a/tui_gateway/methods_slash.py b/tui_gateway/methods_slash.py
index ef507c92a1c..c4ede8c8087 100644
--- a/tui_gateway/methods_slash.py
+++ b/tui_gateway/methods_slash.py
@@ -132,13 +132,20 @@ def _format_live_refine_output(sid: str, session: Optional[dict], arg: str) -> s
def _usage_account_blocks(session: dict) -> list[str]:
- """Provider limits then Nous credits, as CLI/messaging /usage show them; both need no agent."""
+ """Provider limits then Nous credits, as CLI/messaging /usage show them; neither needs an agent.
+ Both read the session profile's credentials, unreadable outside its scope when multiplexed (#132422).
+ Before the first turn the route is the session's composer pick, else the profile's provider."""
+ from agent.account_usage import fetch_account_usage, nous_credits_lines, render_account_usage_lines
+ agent = session.get("agent")
+ pick = session.get("model_override") or {}
lines: list[str] = []
- with contextlib.suppress(Exception):
- lines += _account_usage_lines(session)
- with contextlib.suppress(Exception):
- from agent.account_usage import nous_credits_lines
- lines += nous_credits_lines()
+ with _session_profile_runtime_scope(session):
+ with contextlib.suppress(Exception):
+ if provider := getattr(agent, "provider", None) or pick.get("provider") or _config_model_target()[1]:
+ lines += render_account_usage_lines(fetch_account_usage(
+ provider, base_url=getattr(agent, "base_url", None), api_key=getattr(agent, "api_key", None)))
+ with contextlib.suppress(Exception):
+ lines += nous_credits_lines()
return lines