Skip to content

fix(gateway): read /status token usage from SQLite#13820

Closed
wgd753 wants to merge 1 commit into
NousResearch:mainfrom
wgd753:fix/status-token-usage-from-sqlite-local
Closed

fix(gateway): read /status token usage from SQLite#13820
wgd753 wants to merge 1 commit into
NousResearch:mainfrom
wgd753:fix/status-token-usage-from-sqlite-local

Conversation

@wgd753
Copy link
Copy Markdown

@wgd753 wgd753 commented Apr 22, 2026

What does this PR do?

This fixes a gateway /status regression where the command can show Tokens: 0 even when the same session already has substantial token usage persisted in SQLite state.db.

Root cause:

  • gateway token accounting is now written to SQLite / SessionDB
  • /status still renders session_entry.total_tokens from SessionStore / sessions.json
  • that mirror is no longer kept in sync, so it can drift to 0

This PR fixes the bug by making /status prefer the authoritative SQLite session stats first, while keeping SessionEntry.total_tokens as a fallback when no DB row exists. That matches the current source of truth and avoids reintroducing another mirrored accounting path that can drift again.

Related Issue

Fixes #5960
Related: #11087

Type of Change

  • 🐛 Bug fix (non-breaking change that fixes an issue)
  • ✨ New feature (non-breaking change that adds functionality)
  • 🔒 Security fix
  • 📝 Documentation update
  • ✅ Tests (adding or improving test coverage)
  • ♻️ Refactor (no behavior change)
  • 🎯 New skill (bundled or hub)

Changes Made

  • gateway/run.py
    • add _status_total_tokens() helper
    • read input_tokens, output_tokens, cache_read_tokens, and cache_write_tokens from self._session_db.get_session(session_id)
    • make /status display the SQLite-derived total instead of session_entry.total_tokens when DB stats are available
  • tests/gateway/test_status_command.py
    • seed the mocked session DB with get_session.return_value = None by default
    • add a regression test proving /status prefers SQLite-backed token counts over a stale SessionStore total

How to Test

  1. Reproduce on current main
    • run Hermes through the gateway
    • have a longer conversation so the session accumulates token usage
    • run /status
    • observe that it can report Tokens: 0 while the same session has non-zero usage in state.db
  2. Apply this branch and run:
    • pytest tests/gateway/test_status_command.py -q
  3. Verify the focused regression path:
    • python -m py_compile gateway/run.py tests/gateway/test_status_command.py
    • git diff --check
    • confirm /status now reports the SQLite-backed total instead of 0

Checklist

Code

  • I've read the Contributing Guide
  • My commit messages follow Conventional Commits (fix(scope):, feat(scope):, etc.)
  • I searched for existing PRs to make sure this isn't a duplicate
  • My PR contains only changes related to this fix/feature (no unrelated commits)
  • I've run pytest tests/ -q and all tests pass
  • I've added tests for my changes (required for bug fixes, strongly encouraged for features)
  • I've tested on my platform: Ubuntu 24.04 / Linux

Documentation & Housekeeping

  • I've updated relevant documentation (README, docs/, docstrings) — or N/A
  • I've updated cli-config.yaml.example if I added/changed config keys — or N/A
  • I've updated CONTRIBUTING.md or AGENTS.md if I changed architecture or workflows — or N/A
  • I've considered cross-platform impact (Windows, macOS) per the compatibility guide — or N/A
  • I've updated tool descriptions/schemas if I changed tool behavior — or N/A

Screenshots / Logs

Focused validation run:

$ pytest tests/gateway/test_status_[command.py](https://command.py/) -q
10 passed

Additional checks:

$ python -m py_compile gateway/[run.py](https://run.py/) tests/gateway/test_status_[command.py](https://command.py/)
# passed

$ git diff --check
# passed
```# Screenshots / Logs

Focused validation run:
 `$ pytest tests/gateway/test_status_[command.py](https://command.py/) -q`
10 passed

@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 Apr 22, 2026
@alt-glitch
Copy link
Copy Markdown
Collaborator

Related: PRs #13404 and #5989 also fix this same /status token display regression (#5960). One of these three should be chosen.

@teknium1
Copy link
Copy Markdown
Contributor

teknium1 commented May 1, 2026

Fixed on main in 7abc9ce via #17158 — essentially your approach (inline SessionDB.get_session() + sum columns in _handle_status_command). Closing as superseded. Thanks for the fix.

@teknium1 teknium1 closed this 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.

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

3 participants