anomalyco / anomalyco/opencode
Message API returns 500 when tokens.total is null (NaN serialization)
Open
@jlongster is already working on this.
Since Jul 24, 2026.
- Dominant language
- TypeScript
- Stars
- 209k
- Forks
- 27.5k
- Avg merge
- 7h 2m
- Merged PRs (30d)
- 384
Description
Bug
getUsage() passes totalTokens raw without the safe() guard applied to all other token fields. When a provider returns NaN (e.g. from undefined + number), JSON.stringify(NaN) serializes as null. On read-back, null violates Schema.optional(Schema.Finite) causing a 500 MessageDecodeError on the message API.
Steps to Reproduce
- Use a provider that occasionally returns
undefinedfor usage fields (causing NaN via arithmetic) - The message gets stored with
tokens.total: nullin SQLite - Loading that session's messages returns 500
Expected Behavior
tokens.total should be sanitized like other token fields, and existing corrupted rows should be recovered gracefully on read.
Environment
- OS: macOS
- opencode: dev branch
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.