Repository navigation
refactor(usage): add precise token buckets for Usage Mosaic - #74337
Conversation
Greptile SummaryThis PR adds Confidence Score: 5/5This PR is safe to merge — no bugs, no security issues, and backward compatibility is preserved through the documented fallback path. The implementation is well-structured: new type fields are optional, the bucket validation in No files require special attention. Reviews (1): Last reviewed commit: "refactor(usage): add precise token mosai..." | Re-trigger Greptile |
|
Codex review: needs maintainer review before merge. What this changes: This PR adds optional UTC quarter-hour token usage buckets to session cost summaries, aggregates them from transcript usage entries, updates Control UI Usage Mosaic/hour filtering to prefer those buckets with legacy fallback, and adds backend/UI tests plus a changelog entry. Maintainer follow-up before merge: This is an existing focused implementation PR with no discrete blocking bug found; the remaining work is maintainer review, conflict/rebase handling, and validation rather than a separate ClawSweeper repair job. Security review: Security review cleared: The diff touches usage aggregation, UI helpers, tests, and changelog only; it does not add dependencies, workflows, network calls, permissions, secret handling, install scripts, or command execution paths. Review detailsBest possible solution: Land a reviewed version that keeps the token-bucket field optional, derives sparse UTC quarter-hour token buckets from existing transcript usage, validates bucket coordinates in the UI, preserves legacy span fallback for older summaries, and keeps the focused backend/UI coverage for aggregation, timezone mapping, invalid data, and fallback behavior. Do we have a high-confidence way to reproduce the issue? Yes. On current main, a session with token usage in one quarter-hour but an activity span crossing another hour will still have Usage Mosaic tokens proportionally spread across the span and selected-hour filtering matched by first/last activity; the current files show that path directly. Is this the best way to solve the issue? Yes, with maintainer review. The proposed direction is the narrowest maintainable extension of the already-merged #49396 pattern: add an optional sparse bucket contract, consume it in the token mosaic/hour filter, and leave dailyBreakdown and broader UsageTimeBucket work out of scope. Acceptance criteria:
What I checked:
Likely related people:
Remaining risk / open question:
Codex review notes: model gpt-5.5, reasoning high; reviewed against de1ac12f1c04. |
|
@hxy91819 This is the follow-up PR for the Usage Metrics token mosaic issue we discussed after #49396. Main changes:
Targeted checks and local Codex review are done; actionable findings have been addressed. |
|
Codex review: needs maintainer review before merge. What this changes: This PR adds optional UTC quarter-hour token usage buckets to session cost summaries, aggregates them from transcript usage entries, updates Control UI Usage Mosaic/hour filtering to prefer those buckets with legacy fallback, and adds backend/UI tests plus a changelog entry. Maintainer follow-up before merge: The remaining action is normal maintainer PR review and validation of an existing implementation branch; there is no separate narrow repair task for ClawSweeper to queue. Review detailsBest possible solution: Land a reviewed version that adds the optional token-bucket contract, derives sparse buckets from transcript usage, uses them in Usage Mosaic and hour selection with validated legacy fallback, and keeps focused backend/UI coverage for bucket aggregation, invalid data, timezone mapping, and fallback behavior. Acceptance criteria:
What I checked:
Likely related people:
Remaining risk / open question:
Codex review notes: model gpt-5.5, reasoning high; reviewed against 1390eadd9289. |
6d94936 to
1518535
Compare
|
Merged via squash.
Thanks @konanok! |
…#74337) Merged via squash. Prepared head SHA: 1518535 Co-authored-by: konanok <[email protected]> Co-authored-by: hxy91819 <[email protected]> Reviewed-by: @hxy91819
…#74337) Merged via squash. Prepared head SHA: 1518535 Co-authored-by: konanok <[email protected]> Co-authored-by: hxy91819 <[email protected]> Reviewed-by: @hxy91819
…#74337) Merged via squash. Prepared head SHA: 1518535 Co-authored-by: konanok <[email protected]> Co-authored-by: hxy91819 <[email protected]> Reviewed-by: @hxy91819
…#74337) Merged via squash. Prepared head SHA: 1518535 Co-authored-by: konanok <[email protected]> Co-authored-by: hxy91819 <[email protected]> Reviewed-by: @hxy91819
Summary
Describe the problem and fix in 2–5 bullets:
totalTokensacross every hour touched by the session duration, so long or cross-hour sessions can make hourly token distribution look flatter than the actual model usage timeline.Peak Error Hoursalready moved to precise UTC quarter-hour buckets in fix(ui): use precise hourly message counts for Peak Error Hours #49396, and maintainers identified token mosaic as the same follow-up area; token mosaic should follow the same data-modeling pattern so the hourly heatmap and hour filter are based on actual usage timestamps when available.utcQuarterHourTokenUsagebuckets, aggregate them from transcript usage entries, and make Usage Mosaic/hour filtering prefer these buckets before falling back to the legacy session-span behavior.dailyBreakdown, daily chart timezone semantics, provider/channel aggregates, or introduce a globalUsageTimeBucketmodel.Change Type (select all)
Scope (select all touched areas)
Linked Issue/PR
Root Cause (if applicable)
For bug fixes or regressions, explain why this happened, not just what changed. Otherwise write
N/A. If the cause is unclear, writeUnknown.Regression Test Plan (if applicable)
For bug fixes or regressions, name the smallest reliable test coverage that should catch this. Otherwise write
N/A.src/infra/session-cost-usage.test.tsui/src/ui/views/usage-metrics.test.tsbuildPeakErrorHourstests cover message/error bucket mapping, but not token buckets.User-visible / Behavior Changes
Usage Mosaic now uses precise per-turn token timestamps when the new
utcQuarterHourTokenUsagedata is available. The visible heatmap should better reflect when token usage actually happened.Clicking an hour remains a session-level filter: it selects sessions that have token usage in that hour, then displays those sessions' total usage. It does not yet change page totals to hour-bucket-only totals.
Older summaries without the new token bucket field remain backward compatible through the legacy session-span fallback.
Diagram (if applicable)
Security Impact (required)
Yes/No): NoYes/No): NoYes/No): NoYes/No): NoYes/No): NoYes, explain risk + mitigation: N/ARepro + Verification
Environment
Steps
Expected
utcQuarterHourTokenUsagewhen available.Actual
Evidence
Attach at least one:
Relevant local verification:
Known unrelated full-suite result after rebasing onto
origin/main@de0f54b54a:Codex local review:
Human Verification (required)
What you personally verified (not just CI), and how:
loadSessionCostSummaryemits sortedutcQuarterHourTokenUsageentries;usage.totalpreferred over component sum for token bucket total;YYYY-MM-DD/quarterIndexvalues are ignored instead of being silently rolled over byDate.UTC;utcQuarterHourTokenUsageremain compatible.pnpm testgreen, because latestorigin/maincurrently has unrelated failures outside this PR's touched files. Full log:.artifacts/pnpm-test-after-rebase.log.Review Conversations
If a bot review conversation is addressed by this PR, resolve that conversation yourself. Do not leave bot review conversation cleanup for maintainers.
Note: no GitHub bot review conversations exist yet for this PR at the time this content was prepared. A local Codex review was run before PR creation; actionable findings were addressed in the implementation.
Compatibility / Migration
Yes/No): YesYes/No): NoYes/No): NoCompatibility details:
utcQuarterHourTokenUsageis optional onSessionCostSummary.Risks and Mitigations
sessions.usagebecome slightly larger because each session may include token quarter-hour buckets.quarterIndexrange before using a bucket.