#8477: TUI: persist session token totals when usage metadata is missing
stale
Cluster:
Session Management Improvements
## Summary
- persist session token totals when live usage metadata is missing
- fall back to transcript usage parsing, then a bounded token estimate from transcript tail
- pass session identifiers into usage persistence to locate transcripts reliably
## Testing
- pnpm vitest run src/auto-reply/reply/session-usage.test.ts src/auto-reply/reply/followup-runner.test.ts src/auto-reply/reply/agent-runner.response-usage-footer.test.ts
Fixes #8196
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR extends `persistSessionUsageUpdate` to keep session token totals updated even when live provider usage metadata is missing. It does this by (1) attempting to recover the latest assistant usage from the session transcript tail, then (2) falling back to a bounded token estimate computed from the transcript tail when no usage objects are available. It also threads `sessionId`/`sessionFile` through `agent-runner.ts` and `followup-runner.ts` so transcript lookup is more reliable.
Key logic lives in `src/auto-reply/reply/session-usage.ts`, which now tail-scans a limited number of bytes/lines from the transcript to avoid unbounded IO, and persists either normalized usage fields (input/output/cache/total) or an increasing-only estimated total token count.
<h3>Confidence Score: 3/5</h3>
- This PR is generally safe to merge, but the new tests and some edge-case behaviors should be rechecked.
- Core changes are additive and bounded (tail reads with line/byte caps) and integrate cleanly via additional parameters. However, the new unit test fixture/expectations appear inconsistent with the usage schema and could mask regressions, and there are known logic issues already flagged in prior threads around tail reads and total token computation that should be fixed before relying on the persisted totals.
- src/auto-reply/reply/session-usage.ts, src/auto-reply/reply/session-usage.test.ts
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#13841: fix: use last attempt prompt tokens for session total instead of ac...
by 1kuna · 2026-02-11
80.8%
#22387: fix: session_status context tracking undercount for cached providers
by 1ucian · 2026-02-21
80.3%
#17109: fix: preserve responseUsage across session resets
by Limitless2023 · 2026-02-15
79.2%
#22701: fix(reply): make usage footer reflect full turn usage and avoid tra...
by artwalker · 2026-02-21
76.7%
#22089: feat: persist default /usage mode via agents.defaults.usage
by AIflow-Labs · 2026-02-20
76.3%
#17253: fix: propagate lastTurnTotal through usage accumulator for accurate...
by robbyczgw-cla · 2026-02-15
76.1%
#11109: fix(tui): prefer config contextTokens over persisted session value
by marezgui · 2026-02-07
75.8%
#15126: fix(status): avoid false 100% context usage when totals mirror context
by AlexAnys · 2026-02-13
75.8%
#14879: fix: persist session metadata to sessions.json after context pruning
by skylarkoo7 · 2026-02-12
75.4%
#14913: fix: update context pruning to notify session metadata after prunin...
by ScreenTechnicals · 2026-02-12
75.4%