fix(tui-gateway): restore token counters on session.resume for correct Desktop usage display - #43050
Open
liuhao1024 wants to merge 1 commit into
Open
Conversation
…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
15 tasks
teknium1
reviewed
Jul 14, 2026
teknium1
left a comment
Contributor
There was a problem hiding this comment.
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.resumewithouteager_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_buildcreates the agent attui_gateway/server.py:1382and emitssession.infoat:1445without 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_buildDesktop-resume integration test that verifies the deferredsession.infousage payload.
Automated hermes-sweeper review.
| 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. |
Contributor
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.resumein the TUI gateway builds a freshAIAgentwhose token counters (session_input_tokens,session_output_tokens, etc.) all start at zero. The actual cumulative usage is stored instate.db(written byupdate_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, sosession.infoandsession.usageboth report all-zero usage back to Desktop.Fix
Add
_restore_session_usage(agent, stored)— called after_make_agent()in thesession.resumehandler — that copies the stored token counts from the DB session row into the agent's runtime counters:input_tokens→agent.session_input_tokensoutput_tokens→agent.session_output_tokenscache_read_tokens→agent.session_cache_read_tokenscache_write_tokens→agent.session_cache_write_tokensreasoning_tokens→agent.session_reasoning_tokensapi_call_count→agent.session_api_callsestimated_cost_usd→agent.session_estimated_cost_usdcost_status→agent.session_cost_statussession_total_tokensis computed as the sum of the five token components.Files Changed
tui_gateway/server.py—_restore_session_usage()helper + call insession.resumetests/tui_gateway/test_restore_session_usage.py— 7 tests (unit +_get_usageintegration)Testing
Closes #42989