← Back to PRs

#8477: TUI: persist session token totals when usage metadata is missing

by LarHope open 2026-02-04 02:47 View on GitHub →
stale
## 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