Skip to content

Conversation

@sidneyswift
Copy link
Contributor

Problem

Intermittent 500 errors on new chat due to duplicate key value violates unique constraint "rooms_pkey".

Root Cause

Frontend and backend both try to create the same room:

  1. Recoup-Chat/app/api/chat/create/route.ts → Creates room for optimistic UI
  2. Recoup-API/lib/chat/handleChatCompletion.ts → Also creates room (for API clients)

Race condition: backend's selectRoom() returns null before frontend's insert is visible → duplicate key error.

Solution

Catch the 23505 (unique constraint violation) error in handleChatCompletion. If room already exists, skip notification and continue - the frontend already created it.

Changes

  • Wrap insertRoom in try/catch
  • Catch 23505 error → continue without throwing
  • Only send notification if we successfully created the room

Related

  • Recoup-Chat PR: adds refetchConversations() to onFinish to sync title updates

Catch 23505 unique constraint error when frontend creates room before
backend's selectRoom sees it. Skip notification if room already exists.
@vercel
Copy link

vercel bot commented Jan 26, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Review Updated (UTC)
recoup-api Ready Ready Preview Jan 27, 2026 3:20am

@coderabbitai
Copy link

coderabbitai bot commented Jan 26, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

  • 🔍 Trigger a full review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link

github-actions bot commented Jan 26, 2026

Braintrust eval report

Catalog Opportunity Analysis Evaluation (HEAD-1769483992)

Score Average Improvements Regressions
Catalog_availability 21.7% (+18pp) 1 🟢 3 🔴
Llm_calls 0 (+0) - -
Tool_calls 0 (+0) - -
Errors 0 (+0) - -
Llm_errors 0 (+0) - -
Tool_errors 0 (+0) - -
Prompt_tokens 0tok (+0tok) - -
Prompt_cached_tokens 0tok (+0tok) - -
Prompt_cache_creation_tokens 0tok (+0tok) - -
Completion_tokens 0tok (+0tok) - -
Completion_reasoning_tokens 0tok (+0tok) - -
Total_tokens 0tok (+0tok) - -
Duration 29.67s (-13.47s) 4 🟢 1 🔴

Catalog Songs Count Evaluation (HEAD-1769483992)

Score Average Improvements Regressions
AnswerCorrectness 19.3% (0pp) - 3 🔴
Factuality 66.7% (+0pp) 1 🟢 1 🔴
Llm_calls 4 (+0) - -
Tool_calls 0 (+0) - -
Errors 0 (+0) - -
Llm_errors 0 (+0) - -
Tool_errors 0 (+0) - -
Prompt_tokens 0tok (+0tok) - -
Prompt_cached_tokens 0tok (+0tok) - -
Prompt_cache_creation_tokens 0tok (+0tok) - -
Completion_tokens 0tok (+0tok) - -
Completion_reasoning_tokens 0tok (+0tok) - -
Completion_accepted_prediction_tokens 0tok (+0tok) - -
Completion_rejected_prediction_tokens 0tok (+0tok) - -
Completion_audio_tokens 0tok (+0tok) - -
Total_tokens 0tok (+0tok) - -
Duration 15.99s (+3.97s) - 3 🔴

First Week Album Sales Evaluation (HEAD-1769483992)

Score Average Improvements Regressions
Factuality 70% (-5pp) 1 🟢 1 🔴
Llm_calls 1 (+0) - -
Tool_calls 0 (+0) - -
Errors 0 (+0) - -
Llm_errors 0 (+0) - -
Tool_errors 0 (+0) - -
Prompt_tokens 0tok (+0tok) - -
Prompt_cached_tokens 0tok (+0tok) - -
Prompt_cache_creation_tokens 0tok (+0tok) - -
Completion_tokens 0tok (+0tok) - -
Completion_reasoning_tokens 0tok (+0tok) - -
Completion_accepted_prediction_tokens 0tok (+0tok) - -
Completion_rejected_prediction_tokens 0tok (+0tok) - -
Completion_audio_tokens 0tok (+0tok) - -
Total_tokens 0tok (+0tok) - -
Duration 13.07s (-1.74s) 3 🟢 1 🔴

Memory & Storage Tools Evaluation (HEAD-1769483992)

Score Average Improvements Regressions
Tools_called 0% (+0pp) - -
Llm_calls 0 (+0) - -
Tool_calls 0 (+0) - -
Errors 0 (+0) - -
Llm_errors 0 (+0) - -
Tool_errors 0 (+0) - -
Prompt_tokens 0tok (+0tok) - -
Prompt_cached_tokens 0tok (+0tok) - -
Prompt_cache_creation_tokens 0tok (+0tok) - -
Completion_tokens 0tok (+0tok) - -
Completion_reasoning_tokens 0tok (+0tok) - -
Total_tokens 0tok (+0tok) - -
Duration 14.13s (-0.5s) 1 🟢 -

Monthly Listeners Tracking Evaluation (HEAD-1769483992)

Score Average Improvements Regressions
AnswerSimilarity 77% (+0pp) 2 🟢 3 🔴
Llm_calls 2 (+0) - -
Tool_calls 0 (+0) - -
Errors 0 (+0) - -
Llm_errors 0 (+0) - -
Tool_errors 0 (+0) - -
Prompt_tokens 0tok (+0tok) - -
Prompt_cached_tokens 0tok (+0tok) - -
Prompt_cache_creation_tokens 0tok (+0tok) - -
Completion_tokens 0tok (+0tok) - -
Completion_reasoning_tokens 0tok (+0tok) - -
Total_tokens 0tok (+0tok) - -
Duration 14.12s (-0.75s) 2 🟢 3 🔴

Search Web Tool Evaluation (HEAD-1769483992)

Score Average Improvements Regressions
AnswerCorrectness 26.5% (+1pp) 4 🟢 7 🔴
Llm_calls 3 (+0) - -
Tool_calls 0 (+0) - -
Errors 0 (+0) - -
Llm_errors 0 (+0) - -
Tool_errors 0 (+0) - -
Prompt_tokens 0tok (+0tok) - -
Prompt_cached_tokens 0tok (+0tok) - -
Prompt_cache_creation_tokens 0tok (+0tok) - -
Completion_tokens 0tok (+0tok) - -
Completion_reasoning_tokens 0tok (+0tok) - -
Completion_accepted_prediction_tokens 0tok (+0tok) - -
Completion_rejected_prediction_tokens 0tok (+0tok) - -
Completion_audio_tokens 0tok (+0tok) - -
Total_tokens 0tok (+0tok) - -
Duration 22.95s (-3s) 8 🟢 3 🔴

Social Scraping Evaluation (HEAD-1769483992)

Score Average Improvements Regressions
Tools_called 0% (+0pp) - -
Llm_calls 0 (+0) - -
Tool_calls 0 (+0) - -
Errors 0 (+0) - -
Llm_errors 0 (+0) - -
Tool_errors 0 (+0) - -
Prompt_tokens 0tok (+0tok) - -
Prompt_cached_tokens 0tok (+0tok) - -
Prompt_cache_creation_tokens 0tok (+0tok) - -
Completion_tokens 0tok (+0tok) - -
Completion_reasoning_tokens 0tok (+0tok) - -
Total_tokens 0tok (+0tok) - -
Duration 21.92s (-3.2s) 4 🟢 2 🔴

Spotify Followers Evaluation (HEAD-1769483992)

Score Average Improvements Regressions
AnswerCorrectness 20.5% (0pp) 1 🟢 4 🔴
Llm_calls 3 (+0) - -
Tool_calls 0 (+0) - -
Errors 0 (+0) - -
Llm_errors 0 (+0) - -
Tool_errors 0 (+0) - -
Prompt_tokens 0tok (+0tok) - -
Prompt_cached_tokens 0tok (+0tok) - -
Prompt_cache_creation_tokens 0tok (+0tok) - -
Completion_tokens 0tok (+0tok) - -
Completion_reasoning_tokens 0tok (+0tok) - -
Completion_accepted_prediction_tokens 0tok (+0tok) - -
Completion_rejected_prediction_tokens 0tok (+0tok) - -
Completion_audio_tokens 0tok (+0tok) - -
Total_tokens 0tok (+0tok) - -
Duration 11.7s (-0.94s) 4 🟢 1 🔴

Spotify Tools Evaluation (HEAD-1769483992)

Score Average Improvements Regressions
Llm_calls 0 (+0) - -
Tool_calls 0 (+0) - -
Errors 0 (+0) - -
Llm_errors 0 (+0) - -
Tool_errors 0 (+0) - -
Prompt_tokens 0tok (+0tok) - -
Prompt_cached_tokens 0tok (+0tok) - -
Prompt_cache_creation_tokens 0tok (+0tok) - -
Completion_tokens 0tok (+0tok) - -
Completion_reasoning_tokens 0tok (+0tok) - -
Total_tokens 0tok (+0tok) - -

TikTok Analytics Questions Evaluation (HEAD-1769483992)

Score Average Improvements Regressions
Llm_calls 0 (+0) - -
Tool_calls 0 (+0) - -
Errors 0 (+0) - -
Llm_errors 0 (+0) - -
Tool_errors 0 (+0) - -
Prompt_tokens 0tok (+0tok) - -
Prompt_cached_tokens 0tok (+0tok) - -
Prompt_cache_creation_tokens 0tok (+0tok) - -
Completion_tokens 0tok (+0tok) - -
Completion_reasoning_tokens 0tok (+0tok) - -
Total_tokens 0tok (+0tok) - -
Duration 17.39s (+3.03s) - 2 🔴

Copy link
Contributor

Choose a reason for hiding this comment

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

why are you removing the promise.all?

Simpler fix - keep parallel execution, only catch the 23505 error.
This is the actual fix - createNewRoom is called during setupConversation
at request START, which is where the 500 error occurs.
Frontend no longer creates rooms, so no race condition.
Backend is single source of truth for room creation.
- Changed insertRoom to use upsert with ignoreDuplicates: true
- Reverted try/catch in createNewRoom.ts and handleChatCompletion.ts
- Cleaner solution: duplicates are silently ignored at the DB level
One-line fix: change insert to upsert in insertRoom.ts.
Removes try/catch workarounds since upsert handles duplicates.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants