Skip to content

fix(opencode): sanitize NaN tokens in getUsage and recover null on read#38702

Open
leecoder wants to merge 1 commit into
anomalyco:devfrom
leecoder:fix-nan-tokens-500
Open

fix(opencode): sanitize NaN tokens in getUsage and recover null on read#38702
leecoder wants to merge 1 commit into
anomalyco:devfrom
leecoder:fix-nan-tokens-500

Conversation

@leecoder

@leecoder leecoder commented Jul 24, 2026

Copy link
Copy Markdown

Issue for this PR

Closes #38703

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

getUsage() in session.ts passes input.usage.totalTokens through raw, unlike every other token field which goes through safe(). When a provider returns NaN (from undefined arithmetic), JSON.stringify(NaN) serializes as null in SQLite. On read-back, null violates Schema.optional(Schema.Finite) and the message API returns 500.

Two changes:

  1. Prevention (session.ts): Apply Number.isFinite check to totalTokens before storing, matching the safe() pattern used for all other token fields.
  2. Recovery (message-v2.ts): Sanitize tokens.total === null → undefined and cost === null → 0 at read time so existing corrupted rows load without error.

How did you verify your code works?

  • bun typecheck passes for changed files
  • Verified against a production DB with 189 affected rows — all sessions load without 500 after the recovery path
  • Confirmed JSON.stringify({total: NaN}){"total":null} reproduces the exact corruption pattern

Screenshots / recordings

N/A - not a UI change

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

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.
@github-actions github-actions Bot added needs:compliance This means the issue will auto-close after 2 hours. needs:issue labels Jul 24, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions github-actions Bot removed needs:issue needs:compliance This means the issue will auto-close after 2 hours. labels Jul 24, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

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.

Message API returns 500 when tokens.total is null (NaN serialization)

1 participant