Skip to content

feat(ats): a stage move can be undone, because it has not been sent yet - #648

Merged
thinkclay merged 1 commit into
mainfrom
redesign/move-undo
Aug 25, 2026
Merged

thinkclay merged 1 commit into
mainfrom
redesign/move-undo

Conversation

@thinkclay

Copy link
Copy Markdown
Contributor

Closes #643, with option A — the delayed write.

Nothing is reversed, because nothing can be

ALLOWED_TRANSITIONS is strictly one-way. There is no pair (a, b) where both directions are legal, and hired / rejected / withdrawn have no outgoing edges at all, so isValidStatusTransition(to, from) is false for every move an employer can make. A reverse-move Undo would render, be clickable, and 400 every time.

So the move is held for five seconds and Undo cancels it before it is ever sent — Gmail's "Undo Send". The board updates optimistically either way, so the delay is invisible unless you use it.

Two rules in the queue that are decisions, not mechanics

pending-writes.ts is deliberately React-free so it can be driven with fake timers.

  • Scheduling over an existing key flushes that one rather than dropping it. Two moves on the same row are two decisions; discarding the first would lose a write the user already saw confirmed.
  • Undo only claims success if the write was still held. Past the window the row has moved for real, and saying otherwise would be the same lie as a button that 400s.

The held write is flushed — never discarded — on unmount and on beforeunload/pagehide. What remains is a hard kill of the process inside the window. That's the honest residual, and it's why the window is seconds rather than minutes.

Hire and reject still write immediately. They already ask before acting, and holding a write the user just explicitly confirmed would mean the dialog closes saying it's done while nothing happens for five seconds. The dialog is the undo there — it comes before the decision rather than after it.

Verified on the wire, not on the screen

The claim is about the network, so the test watches the network:

✓ undo      toast="Tyler Brooks moved to Screening" undo=offered writes=0 (expected exactly 0)
✓ let-it-go toast="Tyler Brooks moved to Screening" undo=offered writes=2 (expected at least 1)

Zero writes when undone. That's the claim and it holds.

Two pre-existing bugs surfaced while proving it

Neither is caused by this change; both are filed.

#646 — the endpoint 404s. PATCH /v1/employer/applications/{id} returns "Application not found" for a row the list endpoint just returned. Moving a candidate from the board has never worked. Nobody noticed because there was no feedback at all: the mutation failed, setError was set, nothing rendered it, and the card simply didn't move.

This change makes that more urgent, not less — the card now moves and the toast says it moved, while the write fails behind it.

#647 — that 404 is then retried, though 404 sits in NON_RETRYABLE_STATUSES. Two identical PATCHes 1,095ms apart from one action.

I chased three wrong explanations before the response bodies settled it — SDK retry, double-click, double-schedule. Only logging status codes ended the guessing, and that's now in the script.

The smoke asserts "at least one write" rather than "exactly one": pinning 1 would bake #647's duplicate into the expectation, pinning 2 would bless it. Response codes print either way so the 404 stays visible instead of being counted as a successful write. #646 carries the follow-up to tighten this to "exactly one, 2xx".

A flaw in my own first test

The first version counted writes from a baseline taken after the move, so a duplicate sent during the move itself was invisible — the undo case reported a clean 0 while a write may already have gone out. It now counts every write for the whole run. A test that can only see part of the wire isn't watching the wire.

Coverage note

The hook's own unit test is commented out of vitest.config.ts and does not run. That's why the new coverage is 12 tests on the pure queue plus the wire-level smoke, rather than an extension of the existing suite.

🤖 Generated with Claude Code

Closes #643.

Undo cannot mean "move it back". ALLOWED_TRANSITIONS is strictly one-way —
there is no pair (a, b) where both directions are legal, and hired/rejected/
withdrawn have no outgoing edges at all — so isValidStatusTransition(to, from)
is false for every move an employer can make. A reverse-move Undo would
render, be clickable, and 400 every time.

So nothing is reversed. The move is held for five seconds and Undo cancels it
before it is ever sent, the same shape as Gmail's "Undo Send". The board
updates optimistically either way, so the delay is invisible unless used.

pending-writes.ts is the queue, kept free of React so it can be tested with
fake timers. Two rules in it are decisions rather than mechanics:

  - scheduling over an existing key FLUSHES that one rather than dropping it.
    Two moves on the same row are two decisions, and discarding the first
    would lose a write the user already saw confirmed.
  - Undo only claims success if the write was really still held. Past the
    window the row has moved for real, and saying otherwise would be the same
    lie as a button that 400s.

The held write is flushed, never discarded, on unmount and on beforeunload/
pagehide. What remains is a hard kill of the process inside the window; that
is the honest residual and it is why the window is seconds rather than
minutes.

Hire and reject still write immediately. They already ask before acting, and
holding a write the user has just explicitly confirmed would mean the dialog
closes saying it is done while nothing happens for five seconds. The dialog
IS the undo there — it comes before the decision rather than after it.

Verified on the wire, not on the screen, by scripts/audit/smoke-move-undo.mjs:

  ✓ undo      toast="Tyler Brooks moved to Screening" undo=offered writes=0
  ✓ let-it-go toast="Tyler Brooks moved to Screening" undo=offered writes=2

Zero writes when undone is the claim, and it holds.

Two pre-existing bugs surfaced while proving it, both filed, neither caused
by this change:

  #646 the endpoint 404s — PATCH /v1/employer/applications/{id} rejects rows
       the list just returned, so moving a candidate has never worked
  #647 that 404 is then retried, though 404 is in NON_RETRYABLE_STATUSES

This change makes #646 more urgent rather than less: the card now moves and
the toast says it moved, while the write fails behind it. The smoke asserts
"at least one write" rather than "exactly one" so it neither bakes in the
duplicate nor blesses it, and prints response codes so the 404 stays visible.

Note: the hook's own unit test is commented out of vitest.config.ts and does
not run, which is why the new coverage is 12 tests on the pure queue plus the
wire-level smoke.

Co-Authored-By: Claude Opus 5 <[email protected]>
@thinkclay
thinkclay merged commit fd25444 into main Aug 25, 2026
1 of 3 checks passed
@thinkclay
thinkclay deleted the redesign/move-undo branch August 25, 2026 09:28
thinkclay added a commit that referenced this pull request Sep 1, 2026
…ound (#676)

`pnpm test:unit` was red on scf-core. Working through the four failures found
one stale test, one test asserting a contract I had already changed, and a
schema defect that had been there since the table was created.

**useApplicationStatusChange** — the test built its own query client,
`{ invalidateQueries }` and nothing else. #648 taught the hook to patch the
cached list optimistically, so it started calling `getQueriesData`, which the
stand-in did not have. Swapped for a real QueryClient: the cache API is real,
`invalidateQueries` is spied on the real method, and the optimistic patch and
its rollback can be asserted against actual cache contents. Two tests added for
exactly that, since neither was covered.

The same test was also named "executes valid non-critical transitions
immediately" and asserted the mutation had fired by the time `changeStatus`
resolved. #648 deliberately stopped that — a non-critical move is held for
UNDO_WINDOW_MS so Undo has something to cancel. I changed the behaviour and did
not update the test; it kept "passing" only because it died earlier, on the
stub. Now it pins both halves: nothing sent during the window, sent after.

**ApplicationsFilters** — asserted a "Clear Filters" button. #624 moved
clear-all out of the filter body and onto the chip strip, so no such button
exists here any more. The assertion is still worth having, so it moves to
filters.test.ts next to the module the screen's `onClearAll` actually calls,
covering EMPTY_FILTERS and the `activeFilterCount > 0` gate.

**office-role-access** — this one was not a stale test. It failed with
PGRST116 on a `.single()`, because the local database held 26 duplicated
(user, role, scope) pairs. The cause:

  UNIQUE (role_id, user_id, scope_org_id, scope_team_id)

has never prevented a duplicate. Two NULLs are not equal in a default UNIQUE
constraint, and the table's own CHECK guarantees every legal row has at least
one NULL scope column — so no two rows ever compare as equal. Every seed's
`ON CONFLICT ... DO NOTHING` on this table silently no-opped and appended
another copy instead, and revoking a role by deleting "the" assignment would
leave a duplicate behind and the user still holding it.

Migration 348 collapses the duplicates (oldest wins, deterministic) and
recreates the constraint as UNIQUE NULLS NOT DISTINCT, which is what it always
meant. Verified locally: a duplicate insert is now rejected, and re-running the
002 seed insert reports INSERT 0 0 rather than appending.

Filed as #675, whose acceptance list is deliberately unfinished — the migration
deletes rows, and production has not had it applied.

scf-core 1148/1148. Typecheck 12/12. Lint 10/10.
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.

feat(ats): stage-move toast — and why Undo cannot mean "move it back"

1 participant