Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions packages/opencode/src/session/message-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,20 @@ export const cursor = {
},
}

const info = (row: typeof MessageTable.$inferSelect) =>
({
...row.data,
const info = (row: typeof MessageTable.$inferSelect) => {
const data = row.data as Record<string, any>
// JSON.stringify(NaN) → null in SQLite; on read-back null violates Schema.optional(Schema.Finite).
// Sanitize affected numeric fields so existing corrupted rows don't cause 500s.
if (data.tokens) {
if (data.tokens.total === null) data.tokens.total = undefined
}
if (data.cost === null) data.cost = 0
return {
...data,
id: row.id,
sessionID: row.session_id,
}) as Info
} as Info
}

const part = (row: typeof PartTable.$inferSelect) =>
({
Expand Down
3 changes: 2 additions & 1 deletion packages/opencode/src/session/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,10 @@ export const getUsage = (input: { model: Provider.Model; usage: Usage; metadata?
const adjustedInputTokens = safe(inputTokens - cacheReadInputTokens - cacheWriteInputTokens)

const total = input.usage.totalTokens
const safeTotal = total !== undefined && Number.isFinite(total) ? Math.max(0, total) : undefined

const tokens = {
total,
total: safeTotal,
input: adjustedInputTokens,
output: safe(outputTokens - reasoningTokens),
reasoning: reasoningTokens,
Expand Down
Loading