Repository navigation
fix(assessment): award results-view XP once, and make the award idempotent (#740) - #742
Merged
Merged
Conversation
…otent (#740) Same defect as #731, found auditing for it. IPIPResultsPage depended on the mutation object: const awardXP = useAwardResultsViewXPMutation({ … }) useEffect(() => { if (!results.isLoading && results.isComplete) awardXP.mutate() }, [results.isLoading, results.isComplete, awardXP]) useMutation returns a fresh object every render, so the effect re-ran on the render its own mutate() caused, and `results.isComplete` stays true — which is exactly the condition that fires it. The comment above it said "Award +2 XP on first view"; it was every render, for as long as the page stayed open. Fixed the way #731 was: destructure the memoised `mutate`, and guard with a ref. Mount-scoped here rather than keyed, because there is one result set per page. ## The award is now idempotent, not just called once The handler read the award row, returned early if present, credited XP, and inserted the row last. That is check-then-act: two requests can both pass the check, both credit +2, and only one insert survive the unique constraint on (user_id, assessment_type, xp_type) — inflated XP with a single award row to show for it. A client looping at render speed is the concurrency such a window needs. Inserting first makes the constraint the lock, and 23505 becomes the already-awarded answer rather than an error. Being straight about the evidence: 20 simultaneous requests against the *old* handler on a local stack did not inflate anything — the local edge runtime appears to serialise, which is the condition under which a race cannot show up. So the window is argued from the ordering, not demonstrated. The new ordering removes it either way and costs nothing. ## No data was harmed Checked before assuming, since the issue asked whether existing XP needed correcting. In production core.user_assessment_xp holds zero rows and only one of fifteen assessments is complete, so this endpoint has never successfully run there. The fix is preventive, and my earlier claim that it "has inflated real XP data" was wrong. Verified against a live stack: first call awards 2 and writes one row, a revisit returns alreadyAwarded and changes neither, and twenty concurrent calls leave xp=2 with one row. Tests count calls across re-renders rather than asserting an award happened, with a third case rendering the old shape to prove the other two are not vacuous. API suite failure sets diffed by name: zero newly failing. Co-Authored-By: Claude Opus 5 <[email protected]>
Contributor
📊 Performance Benchmark Results |
This was referenced Sep 11, 2026
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.
Closes #740. Same defect as #731, found by auditing for it.
useMutationreturns a fresh object every render, so the effect re-ran on the render its ownmutate()caused — andresults.isCompletestays true, which is exactly the condition that fires it. The comment above it said "Award +2 XP on first view". It was every render, for as long as the page stayed open.Fixed as #731 was: destructure the memoised
mutate, guard with a ref. Mount-scoped here rather than keyed by id, because there is one result set per page.The award is now idempotent, not merely called once
The handler read the award row, returned early if present, credited XP, and inserted the row last. That's check-then-act: two requests can both pass the check, both credit +2, and only one insert survive the
UNIQUE (user_id, assessment_type, xp_type)constraint — inflated XP with a single award row to show for it. A client looping at render speed is precisely the concurrency such a window needs.Inserting first makes the constraint the lock;
23505becomes the already-awarded answer rather than an error.Being straight about the evidence: 20 simultaneous requests against the old handler on a local stack did not inflate anything. The local edge runtime appears to serialise, which is the condition under which a race cannot show up — so the window is argued from the ordering, not demonstrated. The new ordering removes it either way and costs nothing. I'd rather say that than imply I reproduced it.
No data was harmed — and I was wrong about that
The issue asked whether existing XP needed correcting, so I checked rather than assumed. In production:
The endpoint has never successfully run in production, so nothing is inflated. My earlier claim that this "has inflated real XP data" was wrong — it's a real bug that had not yet fired.
Verified against a live stack
{"success":true,"newXP":2}— xp=2, rows=1{"success":true,"alreadyAwarded":true}— xp=2, rows=1Tests
Count calls across re-renders rather than asserting an award happened — asserting "XP was awarded" passes on the broken version too. A third case renders the old shape and asserts it loops, so the other two can't pass vacuously.
API suite failure sets diffed by name against the same tree without this change: 82 both ways, zero newly failing, none in personality/assessment.
🤖 Generated with Claude Code