Skip to content

Give a retried sign-up back the spot it already took - #396

Merged
richshaw merged 4 commits into
mainfrom
claude/keen-meitner-3fpbml
Oct 10, 2026
Merged

richshaw merged 4 commits into
mainfrom
claude/keen-meitner-3fpbml

Conversation

@richshaw

@richshaw richshaw commented Oct 10, 2026 •

Copy link
Copy Markdown
Owner

Fixes #387.

The problem

A phone can lose its connection after the server saves a sign-up but before the answer arrives. The form then says "network error", and the person presses Confirm again.

  • Without an email, nothing recognises them, so they get a second spot. The first spot's link is nowhere: the lost answer was the one that would have put it in the browser's cookie.
  • With an email, the second press is refused as "you already committed to this slot", and the link is lost the same way.

The fix

  • The sign-up form sends an idempotencyKey (128 random bits) with each sign-up.
    • It sends the same key again when Confirm is pressed again with the same details. That includes after closing the sheet following an error and opening it again, since the row still says Sign up.
    • It makes a new key when the details change, and after a sign-up goes through.
  • POST /api/slots/[id]/commitments looks for an active commitment on that slot made with the same key and the same details. If it finds one, it hands it back with the same edit link and cookie, instead of making another. It writes nothing and sends no second confirmation email.
  • A repeat gets its spot back even if the slot has filled, the signup has closed, or the email has become required since. The spot was taken before any of that.
  • A cancelled or removed sign-up doesn't match, so a retry after one signs up again. A key applies to one slot only.
  • No key, or a new key, works exactly as before.

Things worth a look

  • Database: migration 0012 adds a nullable commitments.idempotency_key_hash. It holds an HMAC, keyed with AUTH_SECRET, of the key and the request as parsed (name, email, notes, spots). It never holds the key itself, since the key brings back the edit link.
    • Because the details are part of it, the same key sent with other details finds nothing. So a client that picks guessable keys can't hand one person's link to someone else.
    • Because of the secret, a copy of the database can't be checked against guessed keys.
    • There's no index: the lookup only searches one slot's commitments, under that slot's lock.
  • Why the lookup is its own statement: it runs after the slot lock is taken, not as part of the locking query. Postgres reads data as of when a statement starts, before it waits for the lock. A lookup inside the locking query would miss a first try that was still running, which is exactly the case this fixes. The DB test gives the first sign-up back to a retry that queued behind it holds a first try open, queues a retry behind its lock, and fails if the lookup is moved into the locking query.
  • Statement budget raised: sign-ups send one more statement to Postgres, so budgets.json goes 14 → 15 with an email and 12 → 13 without. The form always sends a key, so every sign-up from it pays for the lookup. The lookup can't share a statement that waits on the lock, for the reason above.
  • Logs: idempotencyKey is added to the log redaction list.
  • Diff size: about 2,000 of the added lines are src/db/migrations/meta/0012_snapshot.json. Drizzle generates that file with every migration, as it did for 0010 and 0011.

Testing

  • pnpm lint, pnpm typecheck, pnpm knip, pnpm test, pnpm budgets
  • pnpm test:db, all of it. The new tests cover:
    • the same key twice, different and missing keys, and the same key with other details
    • with and without an email
    • after the slot fills or the signup closes, and after the email becomes required
    • after a cancel, and the same key on another slot
    • a retry queued behind the first
    • that the key itself is never stored
  • Sign-up form unit tests:
    • the key's format
    • the same key after a network error, and after closing and reopening the sheet
    • a new key when details change, and a new key after a sign-up goes through
  • e2e: a sign-up whose first answer is dropped after the server saved it, then Confirm again, shows the first sign-up's link. All chromium specs pass.
  • Tophat: see the comment below.

🤖 Generated with Claude Code

https://claude.ai/code/session_01SSB1D4rKHxrax5TQ64DW8X

A sign-up whose answer was lost (a phone dropping its connection after
the server saved it) could be sent again with the same details. Without
an email that took a second spot, and the first one's link was nowhere;
with an email it was refused as a duplicate, losing the link the same way.

The sign-up form now sends an idempotencyKey with each sign-up, and the
same key again when Confirm is pressed again with the same details.
commitToSlot finds an active commitment on the slot made with that key
in the statement that locks the slot, and returns it instead of making
another. The route sends no second confirmation for a repeat, but
answers with the same link and cookie. The key is kept hashed in the
new commitments.idempotency_key_hash column.

Part of #387.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SSB1D4rKHxrax5TQ64DW8X
@richshaw
richshaw marked this pull request as ready for review October 10, 2026 22:23
The idempotency lookup was a join in the statement that locks the slot.
Under READ COMMITTED that statement reads the snapshot it started with,
before it waited, so a retry queued behind a first try still in progress
missed the commitment the first try made, and took a second spot: the
very case #387 is about. The lookup is now its own statement after the
lock, sent only when a key is.

The form always sends a key, so a sign-up sends one more statement to
Postgres: budgets.json goes from 14 to 15 with an email and from 12 to
13 without. The new DB test holds a first try open after it takes the
place, queues the retry behind its lock, and fails on the joined lookup.

Also redacts idempotencyKey from logs, since it brings back the edit
link, and adds DB, route, query budget and sign-up form tests.

Part of #387.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SSB1D4rKHxrax5TQ64DW8X
@richshaw richshaw changed the title Add idempotency key support to prevent duplicate commitments Give a retried sign-up back the spot it already took Oct 10, 2026
The first request reaches the server and saves the sign-up, then the
browser is told the connection failed. Pressing Confirm again shows the
edit link of the sign-up the first request made.

Part of #387.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SSB1D4rKHxrax5TQ64DW8X

richshaw commented Oct 10, 2026 •

Copy link
Copy Markdown
Owner Author

Tophat: passed

Ran 6346b0d locally with pnpm dev and the worker, Postgres 16, Chromium at 1280×900 and 390×844. For "before", I ran the PR's base, 73e17bf, on the same database. The new column is nullable, so main's code ignores it.

Updated for 6944d81, the review fixes:

  • I re-ran the retry after closing and reopening the sheet in the app, on a fresh signup (the last row of the table).
  • The plain retry is covered again by the e2e run against a production build of this commit. All chromium sign-up specs pass.

To make a response go missing, I wrapped window.fetch in the page. The first sign-up request goes to the server as normal, and the server saves it. The form is then handed a "Failed to fetch" error instead of the response. That's what a dropped connection looks like to the form. One difference from a real dropped connection: the browser still gets the first response's cookie. That doesn't affect anything checked below.

Check Result What I saw
Before: retry without an email (main) ✅ reproduced "network error", then Confirm again, then "You're in.". Two confirmed sign-ups for the same person on the slot.
Before: retry with an email (main) ✅ reproduced The second press was refused with "you already committed to this slot" (409), and no link was shown.
After: retry without an email, phone ✅ "network error", then Confirm again, then "You're in." showing the link to the sign-up the first request made (same commitment id). One sign-up in the database, with its key hash stored. After Done, the row shows "Edit".
After: retry with an email, desktop ✅ "You're in.", one sign-up, and exactly one confirmation email in the server log. It links to that sign-up.
Changed details after a network error ✅ as designed Notes typed before pressing Confirm again count as a new sign-up, so this person has two rows. The form makes a new key when the details change.
Plain sign-up with an email (no retry) ✅ Works as before, and the confirmation email arrives.
Responses tab ✅ Each retried sign-up from this PR's code shows once. The person from main's run shows twice.
API: retry after the organizer closed the signup ✅ The same key returned the same sign-up (200). A new key was refused with closed. One sign-up in the database.
6944d81: retry after closing and reopening the sheet, phone ✅ "network error", then Cancel, then Sign up again with the same name, then Confirm, then "You're in." showing the link to the first request's sign-up (same commitment id). One sign-up in the database.

No browser console errors on any page. No errors in the web or worker logs, apart from the dev server's existing MaxListenersExceededWarning.

Not UX: no screen, wording, email or step changes. The only difference people see is that a retry after a network error ends at "You're in." with their link, instead of a second spot or a "you already committed" error.

Screenshots are in https://claude.ai/code/session_01SSB1D4rKHxrax5TQ64DW8X.


Generated by Claude Code

…ailed try

From the code review of this change:

- Closing the sign-up sheet after a network error threw the key away,
  so opening it again and pressing Confirm with the same details took a
  second spot, the bug this fixes. The row still says Sign up then, so
  that is another way of trying again. The form now forgets the key only
  once a sign-up goes through.
- The server matched on the key alone, so a client that picked guessable
  keys would hand one person's edit link to anyone who sent the same key.
  The key is now stored and looked up as an HMAC, keyed with AUTH_SECRET,
  of the key and the request as parsed: it finds an earlier sign-up only
  when sent with the same name, email, notes and spots, and a copy of the
  database can't be checked against guessed keys.
- The lookup is scoped to the slot's workspace, as CLAUDE.md asks of
  every tenant-table query.

Part of #387.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SSB1D4rKHxrax5TQ64DW8X
@richshaw
richshaw merged commit 03d6fc4 into main Oct 10, 2026
5 checks passed
@richshaw
richshaw deleted the claude/keen-meitner-3fpbml branch October 10, 2026 23:33
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.

A sign-up without an email can be booked twice by a retry

2 participants