Summary
After /new session reset, the TUI footer displays stale token counts (e.g., 92k) that gradually converge to the correct value over several messages. session_status tool shows correct values immediately.
Observed Behavior
| Point |
TUI tokens |
session_status |
| Before reset |
~161k |
(unknown) |
| Immediately after /new |
92k |
21k |
| After 1-2 exchanges |
21k → 16k |
17k |
| After convergence |
17k |
17k |
The 92k appears consistently after reset regardless of previous session size (tested with a 16k session → still showed 92k after reset).
Root Cause (via Codex investigation)
1. sessions.reset doesn't clear token counts
src/gateway/server-methods/sessions.ts creates a new entry but omits inputTokens/outputTokens/totalTokens instead of explicitly setting them to 0.
The listing code in src/gateway/session-utils.ts then falls back to:
entry.totalTokens ?? (inputTokens + outputTokens)
...which can pick up stale values from the store.
2. TUI shows stale state during refresh
After reset, the TUI doesn't immediately clear its local sessionInfo. It shows the old cached total until refreshSessionInfo() completes.
3. Token totals are per-run, not cumulative
Each agent run in src/auto-reply/reply/agent-runner.ts overwrites totalTokens with that run's prompt tokens. This explains the decrease pattern (92k → 21k → 16k) — the 92k is from a previous heavy run that got cached somewhere.
Key Files
| Component |
Location |
| sessions.reset |
src/gateway/server-methods/sessions.ts |
| sessions.list (token source) |
src/gateway/session-utils.ts |
| TUI footer rendering |
src/tui/tui.ts (updateFooter()) |
| TUI session refresh |
src/tui/tui-session-actions.ts |
| Token writes per run |
src/auto-reply/reply/agent-runner.ts |
Suggested Fix
sessions.reset should explicitly set inputTokens: 0, outputTokens: 0, totalTokens: 0 on the new entry
- TUI should clear
sessionInfo token fields immediately on reset (before waiting for refresh)
Related
Summary
After
/newsession reset, the TUI footer displays stale token counts (e.g., 92k) that gradually converge to the correct value over several messages.session_statustool shows correct values immediately.Observed Behavior
The 92k appears consistently after reset regardless of previous session size (tested with a 16k session → still showed 92k after reset).
Root Cause (via Codex investigation)
1.
sessions.resetdoesn't clear token countssrc/gateway/server-methods/sessions.tscreates a new entry but omitsinputTokens/outputTokens/totalTokensinstead of explicitly setting them to 0.The listing code in
src/gateway/session-utils.tsthen falls back to:...which can pick up stale values from the store.
2. TUI shows stale state during refresh
After reset, the TUI doesn't immediately clear its local
sessionInfo. It shows the old cached total untilrefreshSessionInfo()completes.3. Token totals are per-run, not cumulative
Each agent run in
src/auto-reply/reply/agent-runner.tsoverwritestotalTokenswith that run's prompt tokens. This explains the decrease pattern (92k → 21k → 16k) — the 92k is from a previous heavy run that got cached somewhere.Key Files
src/gateway/server-methods/sessions.tssrc/gateway/session-utils.tssrc/tui/tui.ts(updateFooter())src/tui/tui-session-actions.tssrc/auto-reply/reply/agent-runner.tsSuggested Fix
sessions.resetshould explicitly setinputTokens: 0,outputTokens: 0,totalTokens: 0on the new entrysessionInfotoken fields immediately on reset (before waiting for refresh)Related