anomalyco / anomalyco/opencode

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

Open
#38,703 0 comments 0 reactions 1 assignee View on GitHub

@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

  1. Use a provider that occasionally returns undefined for usage fields (causing NaN via arithmetic)
  2. The message gets stored with tokens.total: null in SQLite
  3. 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.