Skip to content

fix(tui-gateway): restore token counters on session.resume for correct Desktop usage display - #43050

Open
liuhao1024 wants to merge 1 commit into
NousResearch:mainfrom
liuhao1024:fix/desktop-gateway-session-usage-stats
Open

fix(tui-gateway): restore token counters on session.resume for correct Desktop usage display#43050
liuhao1024 wants to merge 1 commit into
NousResearch:mainfrom
liuhao1024:fix/desktop-gateway-session-usage-stats

Conversation

@liuhao1024

Copy link
Copy Markdown
Contributor

Problem

When viewing a gateway session (e.g. Telegram) in Hermes Desktop, the bottom status bar always shows 0/1.0M-0% for context usage. Token usage information is missing even though the session duration is recorded correctly.

Root Cause

session.resume in the TUI gateway builds a fresh AIAgent whose token counters (session_input_tokens, session_output_tokens, etc.) all start at zero. The actual cumulative usage is stored in state.db (written by update_token_counts() on every API call), but it was never loaded back into the agent.

When _session_info(agent, session) calls _get_usage(agent), it reads the zero-initialized counters, so session.info and session.usage both report all-zero usage back to Desktop.

Fix

Add _restore_session_usage(agent, stored) — called after _make_agent() in the session.resume handler — that copies the stored token counts from the DB session row into the agent's runtime counters:

  • input_tokensagent.session_input_tokens
  • output_tokensagent.session_output_tokens
  • cache_read_tokensagent.session_cache_read_tokens
  • cache_write_tokensagent.session_cache_write_tokens
  • reasoning_tokensagent.session_reasoning_tokens
  • api_call_countagent.session_api_calls
  • estimated_cost_usdagent.session_estimated_cost_usd
  • cost_statusagent.session_cost_status

session_total_tokens is computed as the sum of the five token components.

Files Changed

  • tui_gateway/server.py_restore_session_usage() helper + call in session.resume
  • tests/tui_gateway/test_restore_session_usage.py — 7 tests (unit + _get_usage integration)

Testing

tests/tui_gateway/test_restore_session_usage.py — 7 passed

Closes #42989

…t Desktop usage display

When Desktop views a gateway session (e.g. Telegram), session.resume
builds a fresh agent whose token counters all start at zero. The real
cumulative usage lives in state.db (written by update_token_counts on
every API call), but was never loaded back into the agent.

Add _restore_session_usage() to copy stored token counts from the DB
session row into the agent after _make_agent. This makes session.info
and session.usage return correct data instead of 0/1.0M-0%.

Closes NousResearch#42989
@alt-glitch alt-glitch added type/bug Something isn't working P3 Low — cosmetic, nice to have comp/tui Terminal UI (ui-tui/ + tui_gateway/) labels Jun 10, 2026

@teknium1 teknium1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for tracing the durable token counters and adding focused helper coverage.

Problems

  • The restore call added by this PR is only in the eager resume branch. Current Desktop intentionally calls session.resume without eager_build (apps/desktop/src/app/session/hooks/use-session-actions/index.ts:503-510), so current main takes the deferred path (tui_gateway/server.py:5721-5767). Its later _start_agent_build creates the agent at tui_gateway/server.py:1382 and emits session.info at :1445 without restoring the stored usage. The Desktop path in the report therefore remains unfixed.

Suggested changes

  • Preserve the loaded session row in the deferred record and restore its counters immediately after _make_agent() in _start_agent_build; retain equivalent eager-path coverage.
  • Add a non-eager_build Desktop-resume integration test that verifies the deferred session.info usage payload.

Automated hermes-sweeper review.

Comment thread tui_gateway/server.py
agent = _make_agent(sid, target, session_id=target, session_db=db)
# Restore cumulative token counters from the stored session so
# that session.info / session.usage shows correct usage instead
# of all zeros when viewing gateway sessions in Desktop.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Desktop no longer takes this eager branch: it omits eager_build in apps/desktop/src/app/session/hooks/use-session-actions/index.ts:503-510, so current main defers through tui_gateway/server.py:5721-5767. _start_agent_build then creates the agent and emits session.info without a restore (tui_gateway/server.py:1382,1445). Carry the stored row into that deferred record and restore there too.

@teknium1 teknium1 added sweeper:risk-session-state Sweeper risk: may lose/corrupt/mis-associate session or context state sweeper:blast-moderate Sweeper blast radius: moderate — a subsystem or single platform area/sessions Session lifecycle, resume, persistence, history area/usage-cost Token accounting, usage reporting, billing, cost tracking labels Jul 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/sessions Session lifecycle, resume, persistence, history area/usage-cost Token accounting, usage reporting, billing, cost tracking comp/tui Terminal UI (ui-tui/ + tui_gateway/) P3 Low — cosmetic, nice to have sweeper:blast-moderate Sweeper blast radius: moderate — a subsystem or single platform sweeper:risk-session-state Sweeper risk: may lose/corrupt/mis-associate session or context state type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hermes Desktop 查看 Gateway 会话时不显示上下文占用统计

3 participants