Skip to content

fix: handle duplicate room creation race condition - #165

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

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

Conversation

@sidneyswift

Copy link
Copy Markdown
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

vercelBot commented Jan 27, 2026

Copy link
Copy Markdown
Contributor

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

ProjectDeploymentReviewUpdated (UTC)
recoup-apiReadyReadyPreviewJan 27, 2026 9:05pm

@coderabbitai

Copy link
Copy Markdown

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

github-actionsBot commented Jan 27, 2026

Copy link
Copy Markdown

Braintrust eval report

Catalog Opportunity Analysis Evaluation (HEAD-1769547899)

ScoreAverageImprovementsRegressions
Catalog_availability1.9% (-3pp)1 🟢2 🔴
Llm_calls0 (+0)--
Tool_calls0 (+0)--
Errors0 (+0)--
Llm_errors0 (+0)--
Tool_errors0 (+0)--
Prompt_tokens0tok (+0tok)--
Prompt_cached_tokens0tok (+0tok)--
Prompt_cache_creation_tokens0tok (+0tok)--
Completion_tokens0tok (+0tok)--
Completion_reasoning_tokens0tok (+0tok)--
Total_tokens0tok (+0tok)--
Duration43.81s (-8.18s)4 🟢1 🔴

Catalog Songs Count Evaluation (HEAD-1769547899)

ScoreAverageImprovementsRegressions
AnswerCorrectness19.1% (0pp)1 🟢2 🔴
Factuality66.7% (+0pp)--
Llm_calls4 (+0)--
Tool_calls0 (+0)--
Errors0 (+0)--
Llm_errors0 (+0)--
Tool_errors0 (+0)--
Prompt_tokens0tok (+0tok)--
Prompt_cached_tokens0tok (+0tok)--
Prompt_cache_creation_tokens0tok (+0tok)--
Completion_tokens0tok (+0tok)--
Completion_reasoning_tokens0tok (+0tok)--
Completion_accepted_prediction_tokens0tok (+0tok)--
Completion_rejected_prediction_tokens0tok (+0tok)--
Completion_audio_tokens0tok (+0tok)--
Total_tokens0tok (+0tok)--
Duration17.12s (+1.66s)1 🟢2 🔴

First Week Album Sales Evaluation (HEAD-1769547899)

ScoreAverageImprovementsRegressions
Factuality60% (+20pp)1 🟢1 🔴
Llm_calls1 (+0)--
Tool_calls0 (+0)--
Errors0 (+0)--
Llm_errors0 (+0)--
Tool_errors0 (+0)--
Prompt_tokens0tok (+0tok)--
Prompt_cached_tokens0tok (+0tok)--
Prompt_cache_creation_tokens0tok (+0tok)--
Completion_tokens0tok (+0tok)--
Completion_reasoning_tokens0tok (+0tok)--
Completion_accepted_prediction_tokens0tok (+0tok)--
Completion_rejected_prediction_tokens0tok (+0tok)--
Completion_audio_tokens0tok (+0tok)--
Total_tokens0tok (+0tok)--
Duration16.26s (+0.11s)2 🟢2 🔴

Memory & Storage Tools Evaluation (HEAD-1769547899)

ScoreAverageImprovementsRegressions
Tools_called0% (+0pp)--
Llm_calls0 (+0)--
Tool_calls0 (+0)--
Errors0 (+0)--
Llm_errors0 (+0)--
Tool_errors0 (+0)--
Prompt_tokens0tok (+0tok)--
Prompt_cached_tokens0tok (+0tok)--
Prompt_cache_creation_tokens0tok (+0tok)--
Completion_tokens0tok (+0tok)--
Completion_reasoning_tokens0tok (+0tok)--
Total_tokens0tok (+0tok)--
Duration14.68s (-4.96s)1 🟢-

Monthly Listeners Tracking Evaluation (HEAD-1769547899)

ScoreAverageImprovementsRegressions
AnswerSimilarity78.3% (+2pp)4 🟢1 🔴
Llm_calls2 (+0)--
Tool_calls0 (+0)--
Errors0 (+0)--
Llm_errors0 (+0)--
Tool_errors0 (+0)--
Prompt_tokens0tok (+0tok)--
Prompt_cached_tokens0tok (+0tok)--
Prompt_cache_creation_tokens0tok (+0tok)--
Completion_tokens0tok (+0tok)--
Completion_reasoning_tokens0tok (+0tok)--
Total_tokens0tok (+0tok)--
Duration14.74s (-0.23s)2 🟢3 🔴

Search Web Tool Evaluation (HEAD-1769547899)

ScoreAverageImprovementsRegressions
AnswerCorrectness29.3% (+3pp)7 🟢4 🔴
Llm_calls3 (+0)--
Tool_calls0 (+0)--
Errors0 (+0)--
Llm_errors0 (+0)--
Tool_errors0 (+0)--
Prompt_tokens0tok (+0tok)--
Prompt_cached_tokens0tok (+0tok)--
Prompt_cache_creation_tokens0tok (+0tok)--
Completion_tokens0tok (+0tok)--
Completion_reasoning_tokens0tok (+0tok)--
Completion_accepted_prediction_tokens0tok (+0tok)--
Completion_rejected_prediction_tokens0tok (+0tok)--
Completion_audio_tokens0tok (+0tok)--
Total_tokens0tok (+0tok)--
Duration25.21s (-6.65s)9 🟢2 🔴

Social Scraping Evaluation (HEAD-1769547899)

ScoreAverageImprovementsRegressions
Tools_called0% (+0pp)--
Llm_calls0 (+0)--
Tool_calls0 (+0)--
Errors0 (+0)--
Llm_errors0 (+0)--
Tool_errors0 (+0)--
Prompt_tokens0tok (+0tok)--
Prompt_cached_tokens0tok (+0tok)--
Prompt_cache_creation_tokens0tok (+0tok)--
Completion_tokens0tok (+0tok)--
Completion_reasoning_tokens0tok (+0tok)--
Total_tokens0tok (+0tok)--
Duration26.23s (-6.64s)5 🟢1 🔴

Spotify Followers Evaluation (HEAD-1769547899)

ScoreAverageImprovementsRegressions
AnswerCorrectness20.7% (0pp)3 🟢2 🔴
Llm_calls3 (+0)--
Tool_calls0 (+0)--
Errors0 (+0)--
Llm_errors0 (+0)--
Tool_errors0 (+0)--
Prompt_tokens0tok (+0tok)--
Prompt_cached_tokens0tok (+0tok)--
Prompt_cache_creation_tokens0tok (+0tok)--
Completion_tokens0tok (+0tok)--
Completion_reasoning_tokens0tok (+0tok)--
Completion_accepted_prediction_tokens0tok (+0tok)--
Completion_rejected_prediction_tokens0tok (+0tok)--
Completion_audio_tokens0tok (+0tok)--
Total_tokens0tok (+0tok)--
Duration15.82s (+0.86s)2 🟢3 🔴

Spotify Tools Evaluation (HEAD-1769547899)

ScoreAverageImprovementsRegressions
Tools_called0% (+0pp)--
Llm_calls0 (+0)--
Tool_calls0 (+0)--
Errors0 (+0)--
Llm_errors0 (+0)--
Tool_errors0 (+0)--
Prompt_tokens0tok (+0tok)--
Prompt_cached_tokens0tok (+0tok)--
Prompt_cache_creation_tokens0tok (+0tok)--
Completion_tokens0tok (+0tok)--
Completion_reasoning_tokens0tok (+0tok)--
Total_tokens0tok (+0tok)--
Duration38.23s (-13.41s)2 🟢-

TikTok Analytics Questions Evaluation (HEAD-1769547899)

ScoreAverageImprovementsRegressions
Question_answered0% (-5pp)-1 🔴
Llm_calls0 (+0)--
Tool_calls0 (+0)--
Errors0 (+0)--
Llm_errors0 (+0)--
Tool_errors0 (+0)--
Prompt_tokens0tok (+0tok)--
Prompt_cached_tokens0tok (+0tok)--
Prompt_cache_creation_tokens0tok (+0tok)--
Completion_tokens0tok (+0tok)--
Completion_reasoning_tokens0tok (+0tok)--
Total_tokens0tok (+0tok)--
Duration14.08s (-3.71s)2 🟢-

@sweetmantechsweetmantech left a comment

Copy link
Copy Markdown
Contributor

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?

Comment threadlib/supabase/rooms/upsertRoom.ts
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 testJan 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 freeto 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

@sidneyswift@sweetmantech