Skip to content

[Bug]: /status Tokens: 0 regressed — SessionStore totals drift from SessionDB/state.db #5960

@Louise-Qiuqiu

Description

@Louise-Qiuqiu

Bug Description

/status can still show Tokens: 0 even when the current gateway session has substantial token usage recorded in SQLite.

This appears to be a regression of the symptom fixed in #1465 / PR #1476, but with a changed root cause.

Steps to Reproduce

  1. Run Hermes through the gateway (Telegram in my case).
  2. Have a longer conversation so the session clearly accumulates token usage.
  3. Send /status.
  4. Inspect the same session in both persistence layers.

Expected Behavior

/status should report the real token usage for the current session.

Actual Behavior

/status reports Tokens: 0 even though the same session has non-zero token usage in state.db.

Evidence

In my reproduction, the same session ID had:

  • sessions.json / SessionEntry.total_tokens = 0
  • state.db token fields populated with large non-zero values

Example from one affected session:

  • session_id: 20260407_170736_30bcaff2
  • sessions.json total_tokens = 0
  • state.db input_tokens = 171876
  • state.db output_tokens = 7295
  • state.db cache_read_tokens = 1517184
  • state.db reasoning_tokens = 2648

Regression History

Issue #1465 was fixed by PR #1476 (fix(gateway): make /status report live state and tokens).

That fix explicitly threaded input_tokens / output_tokens from gateway/run.py into session_store.update_session(...), and added regression coverage in tests/gateway/test_status_command.py.

A later refactor appears to have reintroduced the visible bug:

  • commit 20441cf2 (fix(insights): persist token usage for non-CLI sessions)
  • gateway/run.py stopped passing token counts into session_store.update_session(...)
  • gateway/session.py::update_session() was reduced to only updating lightweight metadata like last_prompt_tokens
  • the previous regression test assertion was also weakened so it no longer verifies token propagation into SessionStore

Root Cause Analysis

Current behavior looks like this:

  • real token usage is persisted directly into SQLite / state.db
  • /status still reads session_entry.total_tokens
  • session_entry.total_tokens comes from SessionStore / sessions.json
  • that field is no longer kept in sync with the real token counts

So the symptom is a regression, but the root cause has changed:

Affected Component

Gateway

Messaging Platform (if gateway-related)

Telegram

Operating System

Ubuntu 24.04 Server

Python Version

3.13.5

Hermes Version

0.7.0

Relevant Files

  • gateway/run.py
  • gateway/session.py
  • tests/gateway/test_status_command.py

Proposed Fix (optional)

One of these needs to happen consistently:

  1. Make /status read token usage from SessionDB / SQLite instead of SessionStore.total_tokens, or
  2. Restore synchronization so SessionStore.total_tokens is updated from the authoritative token counts again.

Either way, the regression test should assert the source used by /status, so this does not drift again.

Are you willing to submit a PR for this?

  • I'd like to fix this myself and submit a PR

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Medium — degraded but workaround existscomp/gatewayGateway runner, session dispatch, deliverytype/bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions