fix(opencode): sanitize NaN tokens in getUsage and recover null on read#38702
Open
leecoder wants to merge 1 commit into
Open
fix(opencode): sanitize NaN tokens in getUsage and recover null on read#38702leecoder wants to merge 1 commit into
leecoder wants to merge 1 commit into
Conversation
getUsage() passed totalTokens through without the safe() guard applied to all other token fields. When a provider returns NaN (e.g. undefined + number arithmetic), JSON.stringify serializes it as null. On read-back, null violates Schema.optional(Schema.Finite) causing a 500 MessageDecodeError. Prevention (session.ts): apply Number.isFinite check to totalTokens before storing, matching the safe() pattern used for all other token fields. Recovery (message-v2.ts): sanitize tokens.total === null → undefined and cost === null → 0 at read time so existing corrupted rows no longer crash the message API.
Contributor
|
Thanks for your contribution! This PR doesn't have a linked issue. All PRs must reference an existing issue. Please:
See CONTRIBUTING.md for details. |
Contributor
|
Thanks for updating your PR! It now meets our contributing guidelines. 👍 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue for this PR
Closes #38703
Type of change
What does this PR do?
getUsage()insession.tspassesinput.usage.totalTokensthrough raw, unlike every other token field which goes throughsafe(). When a provider returns NaN (from undefined arithmetic),JSON.stringify(NaN)serializes asnullin SQLite. On read-back,nullviolatesSchema.optional(Schema.Finite)and the message API returns 500.Two changes:
session.ts): ApplyNumber.isFinitecheck tototalTokensbefore storing, matching thesafe()pattern used for all other token fields.message-v2.ts): Sanitizetokens.total === null → undefinedandcost === null → 0at read time so existing corrupted rows load without error.How did you verify your code works?
bun typecheckpasses for changed filesJSON.stringify({total: NaN})→{"total":null}reproduces the exact corruption patternScreenshots / recordings
N/A - not a UI change
Checklist