Skip to content

fix(assessment): award results-view XP once, and make the award idempotent (#740) - #742

Merged
thinkclay merged 1 commit into
mainfrom
claude/fix-ipip-xp-loop
Sep 10, 2026
Merged

thinkclay merged 1 commit into
mainfrom
claude/fix-ipip-xp-loop

Conversation

@thinkclay

Copy link
Copy Markdown
Contributor

Closes #740. Same defect as #731, found by auditing for it.

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 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; 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. 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:

core.user_assessment_xp rows:        0
assessments: 15, completed:          1

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

result
first call {"success":true,"newXP":2} — xp=2, rows=1
revisit {"success":true,"alreadyAwarded":true} — xp=2, rows=1
20 concurrent calls xp=2, rows=1

Tests

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.

✓ awards once, however many times the results page re-renders
✓ does not award while the results are still incomplete
✓ the old shape really did loop — the guard above is not vacuous

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

…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]>
@thinkclay
thinkclay merged commit a093229 into main Sep 10, 2026
8 checks passed
@thinkclay
thinkclay deleted the claude/fix-ipip-xp-loop branch September 10, 2026 22:38
@github-actions

Copy link
Copy Markdown
Contributor

📊 Performance Benchmark Results


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.

IPIPResultsPage re-awards XP on every render

1 participant