Skip to content

Fix local usage accounting for resumed sessions and duplicate requests - #617

Open
Huang-siyuan wants to merge 1 commit into
Dimillian:mainfrom
Huang-siyuan:codex/fix-local-usage-accounting
Open

Huang-siyuan wants to merge 1 commit into
Dimillian:mainfrom
Huang-siyuan:codex/fix-local-usage-accounting

Conversation

@Huang-siyuan

Copy link
Copy Markdown

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:

  • Read token_usage_record.payload.usage and deduplicate response_id across files and session roots.
  • Treat request records as authoritative in files containing that format. Buffer legacy counters until the file's format is known, so preceding or delayed display events cannot duplicate a request.
  • Keep legacy-only files supported: prefer 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.
  • Count input plus output, keep cached input within input, and avoid adding reasoning output or a stale total_tokens field 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.rs and git diff --check: passed.
  • A temporary harness executing the modified Rust scanner against sanitized local usage events at the issue's cutoff produced input 28,898,641, cached input 26,920,192, output 146,226, total 29,044,867, matching the independent deduplicated request calculation. This is scanner verification, not live application UI or billing verification. Personal rollout records are not included in the PR.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment on lines +362 to +366
identity: UsageIdentity::Legacy(
session_id.clone(),
timestamp_ms,
usage,
),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-09T13:30:28.836190Z 826bce4 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Local usage overcounts resumed sessions by treating inherited cumulative totals as new usage

1 participant