Skip to content

fix: handle duplicate room creation race condition#165

Merged
sidneyswift merged 11 commits intotestfrom
fix/room-creation-race-condition
Jan 27, 2026
Merged

fix: handle duplicate room creation race condition#165
sidneyswift merged 11 commits intotestfrom
fix/room-creation-race-condition

Conversation

@sidneyswift
Copy link
Contributor

Re-opening for review before merge.

This changes insertRoom to use upsert instead of insert to handle race conditions when creating rooms.

Catch 23505 unique constraint error when frontend creates room before
backend's selectRoom sees it. Skip notification if room already exists.
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.
@vercel
Copy link
Contributor

vercel bot commented Jan 27, 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 9:05pm

@coderabbitai
Copy link

coderabbitai bot commented Jan 27, 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 27, 2026

Braintrust eval report

Catalog Opportunity Analysis Evaluation (HEAD-1769547899)

Score Average Improvements Regressions
Catalog_availability 1.9% (-3pp) 1 🟢 2 🔴
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 43.81s (-8.18s) 4 🟢 1 🔴

Catalog Songs Count Evaluation (HEAD-1769547899)

Score Average Improvements Regressions
AnswerCorrectness 19.1% (0pp) 1 🟢 2 🔴
Factuality 66.7% (+0pp) - -
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 17.12s (+1.66s) 1 🟢 2 🔴

First Week Album Sales Evaluation (HEAD-1769547899)

Score Average Improvements Regressions
Factuality 60% (+20pp) 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 16.26s (+0.11s) 2 🟢 2 🔴

Memory & Storage Tools Evaluation (HEAD-1769547899)

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.68s (-4.96s) 1 🟢 -

Monthly Listeners Tracking Evaluation (HEAD-1769547899)

Score Average Improvements Regressions
AnswerSimilarity 78.3% (+2pp) 4 🟢 1 🔴
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.74s (-0.23s) 2 🟢 3 🔴

Search Web Tool Evaluation (HEAD-1769547899)

Score Average Improvements Regressions
AnswerCorrectness 29.3% (+3pp) 7 🟢 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 25.21s (-6.65s) 9 🟢 2 🔴

Social Scraping Evaluation (HEAD-1769547899)

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 26.23s (-6.64s) 5 🟢 1 🔴

Spotify Followers Evaluation (HEAD-1769547899)

Score Average Improvements Regressions
AnswerCorrectness 20.7% (0pp) 3 🟢 2 🔴
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 15.82s (+0.86s) 2 🟢 3 🔴

Spotify Tools Evaluation (HEAD-1769547899)

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 38.23s (-13.41s) 2 🟢 -

TikTok Analytics Questions Evaluation (HEAD-1769547899)

Score Average Improvements Regressions
Question_answered 0% (-5pp) - 1 🔴
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.08s (-3.71s) 2 🟢 -

Copy link
Contributor

@sweetmantech sweetmantech left a comment

Choose a reason for hiding this comment

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

where are your using tests to follow TDD (red-green-refactor) for API changes?

Renames the function and file from insertRoom to upsertRoom to align
with the actual Supabase method being called (.upsert()).

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Adds comprehensive unit tests covering:
- Basic upsert operation
- Null artist_id handling
- Null topic handling
- Error handling on upsert failure
- Upsert behavior (update on conflict)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@sidneyswift sidneyswift merged commit 3def780 into test Jan 27, 2026
5 checks passed
sidneyswift added a commit that referenced this pull request Jan 27, 2026
* fix: handle duplicate room creation race condition

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

* fix: keep Promise.all, just wrap in try/catch

Simpler fix - keep parallel execution, only catch the 23505 error.

* fix: handle duplicate room in createNewRoom.ts

This is the actual fix - createNewRoom is called during setupConversation
at request START, which is where the 500 error occurs.

* fix: revert try/catch bandaid - no longer needed

Frontend no longer creates rooms, so no race condition.
Backend is single source of truth for room creation.

* fix: use upsert instead of insert 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

* fix: use upsert instead of insert for rooms

One-line fix: change insert to upsert in insertRoom.ts.
Removes try/catch workarounds since upsert handles duplicates.

* refactor: rename insertRoom to upsertRoom to match method

Renames the function and file from insertRoom to upsertRoom to align
with the actual Supabase method being called (.upsert()).



* test: add unit tests for upsertRoom function

Adds comprehensive unit tests covering:
- Basic upsert operation
- Null artist_id handling
- Null topic handling
- Error handling on upsert failure
- Upsert behavior (update on conflict)



---------

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
sidneyswift added a commit that referenced this pull request Jan 27, 2026
* fix: handle duplicate room creation race condition (#165)

* fix: handle duplicate room creation race condition

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

* fix: keep Promise.all, just wrap in try/catch

Simpler fix - keep parallel execution, only catch the 23505 error.

* fix: handle duplicate room in createNewRoom.ts

This is the actual fix - createNewRoom is called during setupConversation
at request START, which is where the 500 error occurs.

* fix: revert try/catch bandaid - no longer needed

Frontend no longer creates rooms, so no race condition.
Backend is single source of truth for room creation.

* fix: use upsert instead of insert 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

* fix: use upsert instead of insert for rooms

One-line fix: change insert to upsert in insertRoom.ts.
Removes try/catch workarounds since upsert handles duplicates.

* refactor: rename insertRoom to upsertRoom to match method

Renames the function and file from insertRoom to upsertRoom to align
with the actual Supabase method being called (.upsert()).

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* test: add unit tests for upsertRoom function

Adds comprehensive unit tests covering:
- Basic upsert operation
- Null artist_id handling
- Null topic handling
- Error handling on upsert failure
- Upsert behavior (update on conflict)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>

* perf: optimize bundle and parallelize sequential awaits (#161)

* perf: optimize bundle and parallelize sequential awaits

- Add optimizePackageImports for date-fns, @AI-SDK packages
- Parallelize MCP tools and Composio tools fetching with Promise.all
- Parallelize Trigger.dev schedule deletion and DB deletion in deleteTask

These optimizations improve API response times by parallelizing
independent operations.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* test: add unit tests for parallel execution optimizations

- Add comprehensive tests for deleteTask function including:
  - Basic functionality (fetch, delete from DB, delete from Trigger.dev)
  - Error handling (task not found, propagated errors)
  - Handling tasks without trigger_schedule_id
  - Parallel execution verification

- Add parallel execution tests for setupToolsForRequest:
  - Verify MCP and Composio tools are fetched concurrently
  - Verify both operations are called when authToken is provided

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
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.

2 participants

Comments