fix: respect totalTokensFresh flag to avoid showing stale token counts#47083
fix: respect totalTokensFresh flag to avoid showing stale token counts#47083zymclaw wants to merge 2 commits intoopenclaw:mainfrom
Conversation
…counts When a session's totalTokensFresh flag is false, the totalTokens value may be stale/historical (e.g., accumulated from previous compaction) and should not be displayed in the TUI footer. Changes: - Add totalTokensFresh to GatewaySessionList type - Add totalTokensFresh to SessionInfoEntry type - Check totalTokensFresh !== false before updating sessionInfo.totalTokens This prevents showing incorrect token counts like '1.9M / 200k' when the totalTokens value is stale.
Greptile SummaryThis PR fixes a TUI bug where stale Key changes:
One issue to address: The new outer guard Confidence Score: 3/5
Prompt To Fix All With AIThis is a comment left during a code review.
Path: src/tui/tui-session-actions.ts
Line: 194-198
Comment:
**`null` guard blocks legitimate fresh-null resets**
`totalTokens` is typed as `number | null` in `SessionInfo`. When the gateway sends `totalTokens: null` (to clear/reset the displayed count) together with `totalTokensFresh: true` (or `totalTokensFresh` absent), the outer `entry.totalTokens !== null` guard silently drops the update — leaving a stale numeric value on-screen. The `null` check is needed only when the value is stale, but currently it runs unconditionally before the freshness check is ever evaluated.
Restructuring the condition handles all four combinations correctly:
```suggestion
if (entry?.totalTokens !== undefined) {
if (entry.totalTokensFresh !== false) {
next.totalTokens = entry.totalTokens;
}
}
```
This way:
- `totalTokens: 5000, fresh: true` → updated ✓
- `totalTokens: 5000, fresh: false` → skipped (stale) ✓
- `totalTokens: null, fresh: true` → clears display ✓
- `totalTokens: null, fresh: false` → skipped (stale null) ✓
How can I resolve this? If you propose a fix, please make it concise.Last reviewed commit: 368ca03 |
| if (entry?.totalTokens !== undefined && entry.totalTokens !== null) { | ||
| if (entry.totalTokensFresh !== false) { | ||
| next.totalTokens = entry.totalTokens; | ||
| } | ||
| } |
There was a problem hiding this comment.
null guard blocks legitimate fresh-null resets
totalTokens is typed as number | null in SessionInfo. When the gateway sends totalTokens: null (to clear/reset the displayed count) together with totalTokensFresh: true (or totalTokensFresh absent), the outer entry.totalTokens !== null guard silently drops the update — leaving a stale numeric value on-screen. The null check is needed only when the value is stale, but currently it runs unconditionally before the freshness check is ever evaluated.
Restructuring the condition handles all four combinations correctly:
| if (entry?.totalTokens !== undefined && entry.totalTokens !== null) { | |
| if (entry.totalTokensFresh !== false) { | |
| next.totalTokens = entry.totalTokens; | |
| } | |
| } | |
| if (entry?.totalTokens !== undefined) { | |
| if (entry.totalTokensFresh !== false) { | |
| next.totalTokens = entry.totalTokens; | |
| } | |
| } |
This way:
totalTokens: 5000, fresh: true→ updated ✓totalTokens: 5000, fresh: false→ skipped (stale) ✓totalTokens: null, fresh: true→ clears display ✓totalTokens: null, fresh: false→ skipped (stale null) ✓
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/tui/tui-session-actions.ts
Line: 194-198
Comment:
**`null` guard blocks legitimate fresh-null resets**
`totalTokens` is typed as `number | null` in `SessionInfo`. When the gateway sends `totalTokens: null` (to clear/reset the displayed count) together with `totalTokensFresh: true` (or `totalTokensFresh` absent), the outer `entry.totalTokens !== null` guard silently drops the update — leaving a stale numeric value on-screen. The `null` check is needed only when the value is stale, but currently it runs unconditionally before the freshness check is ever evaluated.
Restructuring the condition handles all four combinations correctly:
```suggestion
if (entry?.totalTokens !== undefined) {
if (entry.totalTokensFresh !== false) {
next.totalTokens = entry.totalTokens;
}
}
```
This way:
- `totalTokens: 5000, fresh: true` → updated ✓
- `totalTokens: 5000, fresh: false` → skipped (stale) ✓
- `totalTokens: null, fresh: true` → clears display ✓
- `totalTokens: null, fresh: false` → skipped (stale null) ✓
How can I resolve this? If you propose a fix, please make it concise.…ounts in sessions list When a session's totalTokensFresh flag is false, the totalTokens value may be stale/historical (e.g., accumulated from previous compaction) and should not be displayed in the UI sessions list. Changes: - Add totalTokensFresh to GatewaySessionRow type - Check totalTokensFresh !== false in formatSessionTokens before displaying This prevents showing incorrect token counts like '2.6M / 200k' when the totalTokens value is stale. Complements TUI fix in commit 368ca03.
|
Codex review: needs changes before merge. Summary Reproducibility: yes. Source inspection gives a high-confidence path: a TUI refresh with Next step before merge Security Review findings
Review detailsBest possible solution: Land a narrow repaired fix that carries Do we have a high-confidence way to reproduce the issue? Yes. Source inspection gives a high-confidence path: a TUI refresh with Is this the best way to solve the issue? No, not as submitted. The direction is the narrow maintainable fix, but the TUI logic must check freshness before value/null guards and be ported to the current Full review comments:
Overall correctness: patch is incorrect Acceptance criteria:
What I checked:
Likely related people:
Remaining risk / open question:
Codex review notes: model gpt-5.5, reasoning high; reviewed against 8e79392dccf4. |
Problem
When a session's
totalTokensFreshflag isfalse, thetotalTokensvalue may be stale/historical (e.g., accumulated from previous compaction) and should not be displayed.This caused incorrect displays like "100% context used 2.6M / 200k" in both TUI and Web UI when the token count was stale.
Root Cause
The Gateway already correctly filters stale token counts via
resolveFreshSessionTotalTokens, but:totalTokensFreshfield, so TUI couldn't check itformatSessionTokensfunction didn't check the freshness flagSolution
TUI (commit 368ca03)
totalTokensFreshtoGatewaySessionListtype ingateway-chat.tstotalTokensFreshtoSessionInfoEntrytype intui-session-actions.tstotalTokensFresh !== falsebefore updatingsessionInfo.totalTokensUI (commit 446fdd1)
totalTokensFreshtoGatewaySessionRowtype intypes.tstotalTokensFresh === falseinformatSessionTokensfunction, return "n/a" when staleTesting
Files Changed
src/tui/gateway-chat.ts- AddtotalTokensFreshto type definitionsrc/tui/tui-session-actions.ts- Add type and check before usingtotalTokensui/src/ui/types.ts- AddtotalTokensFreshto type definitionui/src/ui/presenter.ts- Check freshness before displaying tokens