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
undefined for usage fields (causing NaN via arithmetic)
- The message gets stored with
tokens.total: null in 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
Bug
getUsage()passestotalTokensraw without thesafe()guard applied to all other token fields. When a provider returns NaN (e.g. fromundefined + number),JSON.stringify(NaN)serializes asnull. On read-back,nullviolatesSchema.optional(Schema.Finite)causing a 500MessageDecodeErroron the message API.Steps to Reproduce
undefinedfor usage fields (causing NaN via arithmetic)tokens.total: nullin SQLiteExpected Behavior
tokens.totalshould be sanitized like other token fields, and existing corrupted rows should be recovered gracefully on read.Environment