Repository navigation
Fix local usage accounting for resumed sessions and duplicate requests - #617
Huang-siyuan wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 826bce473b
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| identity: UsageIdentity::Legacy( | ||
| session_id.clone(), | ||
| timestamp_ms, | ||
| usage, | ||
| ), |
There was a problem hiding this comment.
Distinguish same-timestamp legacy requests during deduplication
When a legacy log uses the supported second-resolution timestamps (or otherwise timestamps two fast requests identically), two distinct requests in the same session with equal usage produce exactly the same Legacy(session_id, timestamp_ms, usage) key here. The shared seen_usage then suppresses the second request in record_usage, undercounting both daily and model totals; include a stable per-request/cumulative discriminator or otherwise restrict this key to actual copied events.
Useful? React with 👍 / 👎.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Fixes #616
Problem and behavior
A resumed rollout can start with the logical session's historical
total_token_usage. The local scanner currently subtracts zero for each new file, charging that history to the continuation date. On the reported dataset, the existing algorithm produces 199,153,495 tokens for a day containing 29,044,867 tokens in unique request records.This updates the shared local usage core used by both the app and daemon:
token_usage_record.payload.usageand deduplicateresponse_idacross files and session roots.last_token_usage, suppress repeated totals, establish a baseline for the first cumulative-only snapshot and after counter resets, and deduplicate copied events using session ID, timestamp and usage.total_tokensfield again.No IPC payload changes or new dependencies are required.
Compatibility boundary
The legacy fallback deliberately does not infer consumption from an initial cumulative-only snapshot, including the first total after standalone last-usage events. That snapshot can contain inherited history; only subsequent increments are attributable. Request records are authoritative at the file level when present. These remain local request-usage statistics, not provider invoice or account-quota data.
Validation
cargo test --lib shared::local_usage_core::tests --locked: 16 passed, including seven new regressions covering continuations, copied records, cross-root request deduplication, mixed display/request ordering, cached/reasoning tokens, counter resets, aliases and workspace filtering.npm run typecheck: passed.cargo check --locked: passed (existing upstream warnings remain).rustfmt --edition 2021 --check src-tauri/src/shared/local_usage_core.rsandgit diff --check: passed.