Skip to content

Handle DB failures and tighten token accounting; add baseline-browser-mapping devDependency#63

Merged
Abdulmuiz44 merged 2 commits into
codex/ui-redesignfrom
codex/fix-upgrade-modal-for-credit-check-8lgdr3
Apr 6, 2026
Merged

Handle DB failures and tighten token accounting; add baseline-browser-mapping devDependency#63
Abdulmuiz44 merged 2 commits into
codex/ui-redesignfrom
codex/fix-upgrade-modal-for-credit-check-8lgdr3

Conversation

@Abdulmuiz44
Copy link
Copy Markdown
Collaborator

Motivation

  • Prevent failures during chat save or credit accounting from crashing response generation and provide clear warnings to the client.
  • Make token usage parsing robust against malformed/huge values and ensure safe integer bounds when charging credits.
  • Add a missing dev dependency to keep lockfile consistent.

Description

  • Replace fragile token cost calculation with rawTokenCost = Number(generationResult.usage.totalTokens ?? 0) then sanitize using Number.isFinite, Math.round, and clamp to 1..2_147_483_647, and compute creditsToCharge as Math.max(1, Math.min(tokenCost, creditsRemaining)).
  • Stop throwing on Supabase insert/update errors and instead log errors with console.error, set a persistenceWarning, and track chatPersisted so generation still returns an answer even if DB persistence fails.
  • Use creditsToCharge when calling incrementUserCredits and retry accounting up to maxAccountingAttempts, emitting a usageAccountingWarning and logging if accounting fails.
  • Consolidate persistenceWarning and usageAccountingWarning into a single warning field and compute sessionId returned as currentSessionId only when the chat was actually persisted.
  • Add baseline-browser-mapping to devDependencies in package.json and update pnpm-lock.yaml accordingly.

Testing

  • No automated tests were run for this change.

Codex Task

@Abdulmuiz44 Abdulmuiz44 merged commit 62e2faa into codex/ui-redesign Apr 6, 2026
1 of 2 checks passed
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 42f250653f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread app/actions/generate.ts
Comment on lines 188 to +191
if (error) {
console.error('[chat_update_failed]', { userId: authorId, chatId, error })
persistenceWarning = 'We generated your response, but could not save this chat message.'
} else {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Surface chat persistence failures through existing error path

When the chat update/insert fails here, the function now logs and sets persistenceWarning instead of returning an error, then still returns a successful payload. The only caller (components/PromptShell.tsx, where the result is destructured into answer, sessionId, chatId, and error) does not read warning, so in this failure mode the UI treats the response as saved even though nothing was persisted. This creates silent data loss on refresh whenever Supabase writes fail.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant