Skip to content

Keep token usage label maps within their bound when merged - #523

Closed
azooz2003-bit wants to merge 2 commits into
mainfrom
azooz-migration/468-fix-token-usage-label-bound-cdcadadbf3fe
Closed

azooz2003-bit wants to merge 2 commits into
mainfrom
azooz-migration/468-fix-token-usage-label-bound-cdcadadbf3fe

Conversation

@azooz2003-bit

@azooz2003-bit azooz2003-bit commented Oct 6, 2026 •

Copy link
Copy Markdown

`addTokenUsageLabelCounts` let a label map fill all `tokenUsageMaxLabelKeys` slots with real labels and then added `other` as a 17th key. When a snapshot merged such a map into an empty one, whether the result stayed at 16 depended on map iteration order, so `TestTokenUsageStopReasonAndErrorLabelsStayBounded` failed roughly one run in seventeen (seen on #397: `stop reasons = 17 keys, want at most 16`).

The fix reserves the last slot for `other`, so a map never exceeds the bound regardless of merge order. Adds a test that merges a full map (16 labels plus `other`) repeatedly and asserts the bound and the total.

Test: `go test -count=20 -run 'TestTokenUsage|TestAddTokenUsageLabel' ./internal/proxy/` passes.

🤖 Generated with Claude Code


Summary by cubic

Fixes addTokenUsageLabelCounts so merged token usage label maps never exceed the tokenUsageMaxLabelKeys bound. Previously, a full map could add other as a 17th key, and whether that key survived merging depended on map iteration order, causing TestTokenUsageStopReasonAndErrorLabelsStayBounded to fail intermittently. The last slot is now reserved for other, and a test repeatedly merges a full map to assert the bound and total.

Written for commit cdcadad. Summary will update on new commits.

Review in cubic


Migrated from #468 after correcting the PR author identity. The original head commit cdcadad is preserved.


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.

addTokenUsageLabelCounts let a map reach tokenUsageMaxLabelKeys real labels and then added "other" as one more key. Merging such a map into an empty one (as a snapshot does) could land at 17 keys depending on iteration order, which made TestTokenUsageStopReasonAndErrorLabelsStayBounded fail about one run in seventeen. Reserve the last slot for "other".
@coderabbitai

coderabbitai Bot commented Oct 6, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 57 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 2 included reviews currently available.

Learn how review limits work.

Review configuration:

⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 87c6a309-558e-4b42-8895-b83277f76e85
📥 Commits

Reviewing files that changed from the base of the PR and between 90c61ae and cdcadad.

📒 Files selected for processing (2)
  • internal/proxy/token_usage.go
  • internal/proxy/token_usage_metadata_test.go
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions

github-actions Bot commented Oct 6, 2026

Copy link
Copy Markdown

Note

Pull Request opener @azooz2003-bit is not an author or co-author of any commit in this PR (commit identities: teamleaderleo). The CLA check will still proceed and requires every listed identity plus @azooz2003-bit to have signed.


Thank you for your submission, we really appreciate it. Like many open-source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution. You can sign the CLA by just posting a Pull Request Comment same as the below format.


I have read the CLA Document v2.2 and I hereby sign the CLA


You can retrigger this bot by commenting recheck in this Pull Request. Posted by the CLA Assistant Lite bot.

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.

2 participants