-
Notifications
You must be signed in to change notification settings - Fork 49.5k
fix(tui-gateway): restore token counters on session.resume for correct Desktop usage display #43050
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
liuhao1024
wants to merge
1
commit into
NousResearch:main
Choose a base branch
from
liuhao1024:fix/desktop-gateway-session-usage-stats
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| """Tests for _restore_session_usage — token counter restore on session.resume. | ||
|
|
||
| When the TUI gateway resumes a session, a fresh agent is built whose | ||
| session_*_tokens counters all start at zero. _restore_session_usage copies | ||
| the cumulative counts from the stored session row so that session.info / | ||
| session.usage reflects actual usage instead of 0/1.0M-0%. | ||
| """ | ||
|
|
||
| import pytest | ||
|
|
||
|
|
||
| class _FakeAgent: | ||
| """Minimal stand-in with the same counter attributes as AIAgent.""" | ||
|
|
||
| def __init__(self): | ||
| self.session_input_tokens = 0 | ||
| self.session_output_tokens = 0 | ||
| self.session_cache_read_tokens = 0 | ||
| self.session_cache_write_tokens = 0 | ||
| self.session_reasoning_tokens = 0 | ||
| self.session_total_tokens = 0 | ||
| self.session_api_calls = 0 | ||
| self.session_estimated_cost_usd = 0.0 | ||
| self.session_cost_status = "unknown" | ||
|
|
||
|
|
||
| def _import_restore(): | ||
| from tui_gateway.server import _restore_session_usage | ||
| return _restore_session_usage | ||
|
|
||
|
|
||
| # ── Unit tests for _restore_session_usage ────────────────────────── | ||
|
|
||
|
|
||
| def test_restore_basic_token_counts(): | ||
| """All five token fields are populated from the stored session row.""" | ||
| restore = _import_restore() | ||
| agent = _FakeAgent() | ||
| stored = { | ||
| "input_tokens": 12000, | ||
| "output_tokens": 3500, | ||
| "cache_read_tokens": 800, | ||
| "cache_write_tokens": 200, | ||
| "reasoning_tokens": 1500, | ||
| "api_call_count": 7, | ||
| } | ||
| restore(agent, stored) | ||
|
|
||
| assert agent.session_input_tokens == 12000 | ||
| assert agent.session_output_tokens == 3500 | ||
| assert agent.session_cache_read_tokens == 800 | ||
| assert agent.session_cache_write_tokens == 200 | ||
| assert agent.session_reasoning_tokens == 1500 | ||
| assert agent.session_total_tokens == 12000 + 3500 + 800 + 200 + 1500 | ||
| assert agent.session_api_calls == 7 | ||
|
|
||
|
|
||
| def test_restore_total_is_sum(): | ||
| """session_total_tokens equals the sum of all five token components.""" | ||
| restore = _import_restore() | ||
| agent = _FakeAgent() | ||
| stored = { | ||
| "input_tokens": 100, | ||
| "output_tokens": 200, | ||
| "cache_read_tokens": 300, | ||
| "cache_write_tokens": 400, | ||
| "reasoning_tokens": 500, | ||
| } | ||
| restore(agent, stored) | ||
| assert agent.session_total_tokens == 100 + 200 + 300 + 400 + 500 | ||
|
|
||
|
|
||
| def test_restore_handles_none_values(): | ||
| """Missing or None fields default to zero.""" | ||
| restore = _import_restore() | ||
| agent = _FakeAgent() | ||
| stored = { | ||
| "input_tokens": None, | ||
| "output_tokens": 50, | ||
| } | ||
| restore(agent, stored) | ||
|
|
||
| assert agent.session_input_tokens == 0 | ||
| assert agent.session_output_tokens == 50 | ||
| assert agent.session_cache_read_tokens == 0 | ||
| assert agent.session_cache_write_tokens == 0 | ||
| assert agent.session_reasoning_tokens == 0 | ||
| assert agent.session_total_tokens == 50 | ||
| assert agent.session_api_calls == 0 | ||
|
|
||
|
|
||
| def test_restore_preserves_cost_estimate(): | ||
| """estimated_cost_usd and cost_status are carried over when present.""" | ||
| restore = _import_restore() | ||
| agent = _FakeAgent() | ||
| stored = { | ||
| "input_tokens": 1000, | ||
| "output_tokens": 500, | ||
| "estimated_cost_usd": 0.042, | ||
| "cost_status": "estimated", | ||
| } | ||
| restore(agent, stored) | ||
|
|
||
| assert agent.session_estimated_cost_usd == pytest.approx(0.042) | ||
| assert agent.session_cost_status == "estimated" | ||
|
|
||
|
|
||
| def test_restore_skips_cost_when_absent(): | ||
| """Cost fields stay at defaults when not in the stored row.""" | ||
| restore = _import_restore() | ||
| agent = _FakeAgent() | ||
| stored = {"input_tokens": 100, "output_tokens": 50} | ||
| restore(agent, stored) | ||
|
|
||
| assert agent.session_estimated_cost_usd == 0.0 | ||
| assert agent.session_cost_status == "unknown" | ||
|
|
||
|
|
||
| def test_restore_empty_dict(): | ||
| """An empty stored dict leaves the agent at zero counters.""" | ||
| restore = _import_restore() | ||
| agent = _FakeAgent() | ||
| restore(agent, {}) | ||
|
|
||
| assert agent.session_input_tokens == 0 | ||
| assert agent.session_output_tokens == 0 | ||
| assert agent.session_total_tokens == 0 | ||
| assert agent.session_api_calls == 0 | ||
|
|
||
|
|
||
| # ── _get_usage picks up restored counters ────────────────────────── | ||
|
|
||
|
|
||
| def test_get_usage_reflects_restored_counters(): | ||
| """After restore, _get_usage returns the stored token counts.""" | ||
| from tui_gateway.server import _get_usage | ||
|
|
||
| agent = _FakeAgent() | ||
| restore = _import_restore() | ||
| stored = { | ||
| "input_tokens": 5000, | ||
| "output_tokens": 2000, | ||
| "cache_read_tokens": 100, | ||
| "cache_write_tokens": 50, | ||
| "reasoning_tokens": 300, | ||
| "api_call_count": 3, | ||
| "estimated_cost_usd": 0.015, | ||
| "cost_status": "estimated", | ||
| } | ||
| restore(agent, stored) | ||
|
|
||
| usage = _get_usage(agent) | ||
|
|
||
| assert usage["input"] == 5000 | ||
| assert usage["output"] == 2000 | ||
| assert usage["cache_read"] == 100 | ||
| assert usage["cache_write"] == 50 | ||
| assert usage["reasoning"] == 300 | ||
| assert usage["total"] == 5000 + 2000 + 100 + 50 + 300 | ||
| assert usage["calls"] == 3 |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Desktop no longer takes this eager branch: it omits
eager_buildinapps/desktop/src/app/session/hooks/use-session-actions/index.ts:503-510, so current main defers throughtui_gateway/server.py:5721-5767._start_agent_buildthen creates the agent and emitssession.infowithout a restore (tui_gateway/server.py:1382,1445). Carry the stored row into that deferred record and restore there too.