Skip to content

fix(gateway): read /status token totals from SessionDB (#17158)#18206

Merged
teknium1 merged 1 commit into
mainfrom
hermes/hermes-f1e3d8ff
May 1, 2026
Merged

fix(gateway): read /status token totals from SessionDB (#17158)#18206
teknium1 merged 1 commit into
mainfrom
hermes/hermes-f1e3d8ff

Conversation

@teknium1

@teknium1 teknium1 commented May 1, 2026

Copy link
Copy Markdown
Contributor

Summary

/status always showed Tokens: 0 because it read session_entry.total_tokens from the in-memory SessionStore, which nothing ever writes to. The agent already persists token deltas into the SQLite SessionDB (run_agent.py:11497) for every platform with a session_id — route /status through that single source of truth.

Salvages @JezzaHehn's bug report (#17158). Reimplemented to read from the existing store instead of duplicating token writes into two stores (the original PR added a new update_token_counts() method and a reasoning_tokens field to SessionEntry — both unnecessary once we read from SessionDB).

Changes

  • gateway/run.py: _handle_status_command sums the five token columns from _session_db.get_session(session_id); falls back to 0 on missing row or no SessionDB.
  • tests/gateway/test_status_command.py: two new regression tests (populated row, missing row) + update to the existing test fixture to return a real row dict.

Validation

Before After
/status on an active session Tokens: 0 Tokens: <real total>
Fresh session, no agent calls Tokens: 0 Tokens: 0
tests/gateway/test_status_command.py 11 pass 13 pass (2 added)

E2E verified with a real SessionDB + real GatewayRunner._handle_status_command: populated session (1500 input + 400 output + 800 cache_read + 200 cache_write + 100 reasoning) renders **Tokens:** 3,000; missing DB row renders **Tokens:** 0.

Closes #17158.

/status was reading session_entry.total_tokens from the in-memory
SessionStore (gateway/session.py), which the agent never writes to —
so the token count was always 0.

The agent already persists token deltas to the SQLite SessionDB
(run_agent.py:11497) for every platform with a session_id. Route
/status through that single source of truth instead of duplicating
token writes into a second store.

Fix:
- gateway/run.py: _handle_status_command now calls
  self._session_db.get_session(session_id) and sums the five token
  component columns (input/output/cache_read/cache_write/reasoning).
  Falls back to 0 when no SessionDB is configured or no row exists.
- Two new regression tests covering the populated-row and
  missing-row paths.

Co-authored-by: Hermes <127238744+teknium1@users.noreply.github.com>
@teknium1 teknium1 merged commit 7abc9ce into main May 1, 2026
6 of 10 checks passed
@teknium1 teknium1 deleted the hermes/hermes-f1e3d8ff branch May 1, 2026 03:28
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/gateway Gateway runner, session dispatch, delivery labels May 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/gateway Gateway runner, session dispatch, delivery P2 Medium — degraded but workaround exists type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants