Skip to content

The agent that grows with you

fix(dashboard): fold aux usage into the model card it belongs to

AP3X2c48f5ff6ce9
diff --git a/hermes_cli/web_routers/analytics.py b/hermes_cli/web_routers/analytics.py
index 357818bf063..d77175edf21 100644
--- a/hermes_cli/web_routers/analytics.py
+++ b/hermes_cli/web_routers/analytics.py
@@ -220,8 +220,48 @@ def _model_capabilities(provider: str, model_name: str) -> dict:
_AUX_SUMMED_KEYS = (
- "input_tokens", "output_tokens", "cache_read_tokens", "reasoning_tokens", "estimated_cost", "sessions", "api_calls",
+ "input_tokens", "output_tokens", "cache_read_tokens", "reasoning_tokens", "estimated_cost", "api_calls",
)
+
+
+def _merge_aux_into_rows(raw_rows: List[Dict[str, Any]], aux_rows: List[Dict[str, Any]]) -> None:
+ """Add auxiliary usage onto the matching (model, billing_provider) row in place.
+
+ Aux calls happen inside sessions the sessions-derived row already counted, so
+ ``sessions`` is never added onto an existing row; only an aux-only pair (no
+ sessions-derived row) gets a new row that carries its own session count.
+ """
+ index: Dict[tuple, Dict[str, Any]] = {
+ (row.get("model") or "", row.get("billing_provider") or ""): row
+ for row in raw_rows
+ }
+ for aux in aux_rows:
+ key = (aux.get("model") or "unknown", aux.get("billing_provider") or "")
+ target = index.get(key)
+ if target is None:
+ target = {
+ "model": key[0],
+ "billing_provider": key[1],
+ **{k: 0 for k in _AUX_SUMMED_KEYS},
+ "actual_cost": 0,
+ "sessions": aux.get("sessions") or 0,
+ "tool_calls": 0,
+ "last_used_at": None,
+ "avg_tokens_per_session": 0,
+ }
+ index[key] = target
+ raw_rows.append(target)
+ for k in _AUX_SUMMED_KEYS:
+ target[k] = (target.get(k) or 0) + (aux.get(k) or 0)
+ if aux.get("last_used_at") is not None:
+ target["last_used_at"] = max(target.get("last_used_at") or 0, aux["last_used_at"])
+ sessions = target.get("sessions") or 0
+ if sessions:
+ target["avg_tokens_per_session"] = (
+ (target.get("input_tokens") or 0) + (target.get("output_tokens") or 0)
+ ) / sessions
+
+
_MODEL_CARD_KEYS = (
"input_tokens", "output_tokens", "cache_read_tokens", "reasoning_tokens",
"estimated_cost", "actual_cost", "sessions", "api_calls", "tool_calls",
@@ -320,20 +360,14 @@ def _get_models_analytics(days: int = 30, profile: Optional[str] = None):
else:
_attach_tool_calls(db, cutoff, raw_rows)
- # Aux-only models (dedicated vision/compression) as (model, provider) rows,
- # keyed like the GROUP BY above, so they appear on the Models page.
- # See #23270.
- for aux in _aux_usage_rows(db, cutoff):
- raw_rows.append({
- "model": aux.get("model") or "unknown",
- "billing_provider": aux.get("billing_provider") or "",
- **{key: aux.get(key) or 0 for key in _AUX_SUMMED_KEYS},
- "actual_cost": 0,
- "tool_calls": 0,
- "last_used_at": aux.get("last_used_at"),
- "avg_tokens_per_session": 0,
- "aux_task": aux.get("task") or "",
- })
+ # Aux usage (vision/compression/title/approval/...) is folded into the
+ # (model, provider) row the sessions query already produced. #23270 made
+ # aux-only models visible; _aux_usage_rows groups by (model, task,
+ # provider), so appending each row emitted one card per aux task beside
+ # the main card, and their session counts summed past
+ # totals.total_sessions (#89631). Only a pair with no sessions-derived
+ # row becomes a new row, carrying its own session count.
+ _merge_aux_into_rows(raw_rows, _aux_usage_rows(db, cutoff))
rows = _fold_session_only_rows(raw_rows)
rows.sort(
diff --git a/tests/hermes_cli/test_models_analytics_dedup.py b/tests/hermes_cli/test_models_analytics_dedup.py
new file mode 100644
index 00000000000..8eda4f77581
--- /dev/null
+++ b/tests/hermes_cli/test_models_analytics_dedup.py
@@ -0,0 +1,79 @@
+"""Models analytics must emit one row per (model, provider) pair (#89631).
+
+``_aux_usage_rows`` groups by (model, task, billing_provider), so a model
+used for several auxiliary tasks returns several rows for the SAME (model,
+billing_provider) pair. Appending them unconditionally rendered one
+duplicate card per aux task, and their session counts summed past
+``totals.total_sessions`` because aux usage happens inside sessions the
+sessions-derived row already counted.
+"""
+
+import time
+
+import pytest
+
+from hermes_cli.web_routers import analytics as web_server
+from hermes_state import SessionDB
+
+
+def db(tmp_path):
+ return SessionDB(tmp_path / "state.db")
+
+
+def analytics(monkeypatch, db):
+ """Run _get_models_analytics against the test DB."""
+ monkeypatch.setattr(
+ web_server,
+ "_open_session_db_for_profile",
+ lambda profile, read_only=True: db,
+ )
+ # The helper closes the DB it is handed; keep it open for assertions.
+ monkeypatch.setattr(db, "close", lambda: None)
+ return lambda: web_server._get_models_analytics(days=30)
+
+
+def _session(db, session_id, model, provider):
+ db.create_session(session_id, source="cli", model=model)
+ with db._lock:
+ db._conn.execute(
+ "UPDATE sessions SET billing_provider = ?, started_at = ? WHERE id = ?",
+ (provider, time.time(), session_id),
+ )
+ db._conn.commit()
+
+
+def test_aux_tasks_do_not_duplicate_the_model_card(db, analytics):
+ """Several aux tasks on one pair fold into that pair's single row."""
+ _session(db, "s1", "gpt-5.6-terra", "openai-codex")
+ for task in ("vision", "compression", "title_generation"):
+ db.record_auxiliary_usage(
+ "s1", task, model="gpt-5.6-terra",
+ billing_provider="openai-codex",
+ input_tokens=100, output_tokens=10,
+ )
+
+ result = analytics()
+ pairs = [(m["model"], m["provider"]) for m in result["models"]]
+
+ assert pairs == [("gpt-5.6-terra", "openai-codex")]
+ # Session counts must stay reconcilable with the totals block.
+ assert sum(m["sessions"] for m in result["models"]) == result["totals"]["total_sessions"]
+ # Aux tokens are still accounted for, just on the one row.
+ assert result["models"][0]["input_tokens"] == 300
+
+
+def test_aux_only_model_still_gets_its_own_row(db, analytics):
+ """Regression guard for #23270 — a dedicated aux model stays visible."""
+ _session(db, "s1", "gpt-5.6-terra", "openai-codex")
+ db.record_auxiliary_usage(
+ "s1", "vision", model="gemini-3-flash", billing_provider="gemini",
+ input_tokens=500, output_tokens=50,
+ )
+
+ result = analytics()
+ pairs = {(m["model"], m["provider"]) for m in result["models"]}
+
+ assert ("gemini-3-flash", "gemini") in pairs
+ assert ("gpt-5.6-terra", "openai-codex") in pairs