Skip to content

fix(gateway): /status reads token totals from SessionDB (regression of #1465) - #18207

Closed
teknium1 wants to merge 3 commits into
mainfrom
hermes/hermes-ad6e171a
Closed

fix(gateway): /status reads token totals from SessionDB (regression of #1465)#18207
teknium1 wants to merge 3 commits into
mainfrom
hermes/hermes-ad6e171a

Conversation

@teknium1

@teknium1 teknium1 commented May 1, 2026

Copy link
Copy Markdown
Contributor

Make /status read token totals from SQLite (the authoritative store run_agent.py already writes per API call), so Tokens: stops showing stale zeros after #1465 regressed.

Salvaged from #5989 by @Tranquil-Flow. Closes #5960. Supersedes competing PRs #5989 #11088 #13820 #17158 (partial), #12565 (totals portion; the callback_generation snapshot bug in #12565 is a real but separate fix).

Architecture note

Two camps in the open PRs: (A) read from SQLite at display time, (B) mirror totals into SessionStore on every turn. (A) wins — run_agent.py already writes to SQLite per API call (see L11502), SessionStore's total_tokens is vestigial (no production reader). Teaching /status to read the authoritative store means no drift, no double bookkeeping, and naturally covers CLI/cron/delegated sessions too.

Changes

  • hermes_state.py: add SessionDB.get_session_token_totals(session_id) returning {input, output, cache_read, cache_write, reasoning, total} or None.
  • gateway/run.py: _handle_status_command prefers the SQLite totals, falls back to session_entry.total_tokens on missing row or exception.
  • Tests: SessionDB unit tests + /status prefers-SQLite and fallback-on-missing-row tests.
  • Follow-up: swap set_token_counts (nonexistent) for update_token_counts(absolute=True) in one test.

Validation

Before After
/status mid-session Tokens: 0 (from stale session_entry) Tokens: 1,856,650 (live SQLite)
SQLite row missing Tokens: 0 Tokens: session_entry.total_tokens (fallback)
SessionDB raises n/a fallback (no /status crash)

Targeted tests: tests/gateway/test_status_command.py + tests/test_hermes_state.py215/215 passing.

E2E verified with real SessionDB + real SQLite + real _handle_status_command: two simulated API-call update_token_counts deltas (150k in, 5k out, 1.7M cache-read, 150 cache-write, 1.5k reasoning) round-trip correctly via get_session_token_totals, and /status reports the cumulative 1,856,650 instead of the stale session_entry.total_tokens=999.

Credit

Tranquil-Flow and others added 3 commits April 30, 2026 20:25
/status was reading session_entry.total_tokens which is never kept in
sync after the token persistence refactor (commit 20441cf).  Real
token usage lives in SessionDB (SQLite).  Read from SessionDB via the
new get_session_token_totals() helper and fall back to
session_entry.total_tokens when SessionDB is unavailable.

Fixes #5960.
…DB totals test

The test added in the salvage was calling a nonexistent
set_token_counts() method. The real API is update_token_counts with
absolute=True for direct writes. All 215 targeted tests pass.
@teknium1

teknium1 commented May 1, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #17158 (@JezzaHehn), which landed as 7abc9ce on main while this salvage PR was being prepared. Same Camp A architecture (read from SQLite at display time, don't mirror into SessionStore). Closing with no harm done — the bug is fixed on main.

@teknium1 teknium1 closed this May 1, 2026
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/gateway Gateway runner, session dispatch, delivery duplicate This issue or pull request already exists labels May 1, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Likely duplicate of #18206 — same fix for /status token totals reading from SessionDB. #18206 was merged.

@alt-glitch

Copy link
Copy Markdown
Collaborator

Likely duplicate of #18206

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 duplicate This issue or pull request already exists 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.

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

3 participants