fix(claw): prevent double-tap race condition in switchPlan#1377
Open
jeanduplessis wants to merge 2 commits intomainfrom
Open
fix(claw): prevent double-tap race condition in switchPlan#1377jeanduplessis wants to merge 2 commits intomainfrom
jeanduplessis wants to merge 2 commits intomainfrom
Conversation
…phan cleanup Implement switchPlan and cancelPlanSwitch tRPC endpoints with: - Optimistic concurrency guard on DB write to prevent double-schedule races - Orphaned Stripe schedule detection and cleanup before creating new schedules - Best-effort rollback if Stripe schedule update fails after creation - Idempotent cancel that tolerates already-released schedules - Disabled button states during pending mutations in SubscriptionCard - 11 new tests covering happy path, edge cases, and race conditions
Contributor
Code Review SummaryStatus: 2 Issues Found | Recommendation: Address before merge Overview
Fix these issues in Kilo Cloud Issue Details (click to expand)WARNING
Other Observations (not in diff)None. Files Reviewed (3 files)
Reviewed by gpt-5.4-20260305 · 502,651 tokens |
…e and add canceled to cancel regex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
kiloclaw.switchPlanwhere concurrent requests (e.g. mobile double-tap) could both pass the DB guard and create duplicate Stripe subscription schedules, causing a 500 error on the second request.WHERE stripe_schedule_id IS NULLso only the first concurrent request succeeds; losers release their schedule and return a CONFLICT error.StripeInvalidRequestErrorwhen the schedule is already released, so users aren't stuck with stale DB state.Verification
pnpm typecheck— passedpnpm jest src/routers/kiloclaw-billing-router.test.ts— 39/39 passed (12 new tests forswitchPlanandcancelPlanSwitchcovering happy path, validation, orphan cleanup, optimistic concurrency race, and Stripe error tolerance)Visual Changes
N/A
Reviewer Notes
subscriptionSchedules.updatemock, so the handler's initial DB read sees no schedule but the conditionalUPDATE ... WHERE stripe_schedule_id IS NULLreturns 0 rows.if (sub.stripe_schedule_id)) is preserved for legitimate pending switches — only orphaned Stripe schedules (not tracked in DB) are auto-released.