fix(gateway): read /status token totals from SessionDB (#17158)#18206
Merged
Conversation
/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>
1 task
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.
Summary
/statusalways showedTokens: 0because it readsession_entry.total_tokensfrom the in-memorySessionStore, which nothing ever writes to. The agent already persists token deltas into the SQLiteSessionDB(run_agent.py:11497) for every platform with a session_id — route/statusthrough 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 areasoning_tokensfield toSessionEntry— both unnecessary once we read fromSessionDB).Changes
gateway/run.py:_handle_status_commandsums 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
/statuson an active sessionTokens: 0Tokens: <real total>Tokens: 0Tokens: 0tests/gateway/test_status_command.pyE2E verified with a real
SessionDB+ realGatewayRunner._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.