src/lib/resume-library.test.ts > lists saved resumes newest-first failed a single run while several agents ran concurrently during the #744/#745/#746 batch, then passed 5/5 in isolation. It is untouched by that branch and pre-existing.
Root cause
src/lib/resume-library.ts:156 sorts with:
savedAt is a Date.now() millisecond. Two saves that land in the same millisecond make the comparator return 0, and Array.prototype.sort is required to be stable — so the tie preserves insertion order, which is oldest-first. That is exactly the inversion the test observed.
The test saves back-to-back with no delay, so whether it passes depends on whether the machine crossed a millisecond boundary between the two writes. Under load it does not.
Why it matters beyond the test
The same tie affects the product: a user who saves two resumes in quick succession gets them listed oldest-first in the library. Rare, but not test-only.
Fix options
- Tie-break on a monotonic secondary key in the comparator (insertion index, or
id if it is monotonic) so equal savedAt still resolves newest-first.
- Make the test deterministic — fake timers, or distinct explicit
savedAt values — which fixes the flake but leaves the product behaviour.
(1) addresses both; (2) alone would close the flake while leaving the ordering bug.
Repro
npx vitest run src/lib/resume-library.test.ts
Not reliably reproducible on an idle machine — run it under concurrent load, or temporarily stub Date.now() to a constant to make the tie deterministic.
src/lib/resume-library.test.ts > lists saved resumes newest-firstfailed a single run while several agents ran concurrently during the #744/#745/#746 batch, then passed 5/5 in isolation. It is untouched by that branch and pre-existing.Root cause
src/lib/resume-library.ts:156sorts with:savedAtis aDate.now()millisecond. Two saves that land in the same millisecond make the comparator return0, andArray.prototype.sortis required to be stable — so the tie preserves insertion order, which is oldest-first. That is exactly the inversion the test observed.The test saves back-to-back with no delay, so whether it passes depends on whether the machine crossed a millisecond boundary between the two writes. Under load it does not.
Why it matters beyond the test
The same tie affects the product: a user who saves two resumes in quick succession gets them listed oldest-first in the library. Rare, but not test-only.
Fix options
idif it is monotonic) so equalsavedAtstill resolves newest-first.savedAtvalues — which fixes the flake but leaves the product behaviour.(1) addresses both; (2) alone would close the flake while leaving the ordering bug.
Repro
Not reliably reproducible on an idle machine — run it under concurrent load, or temporarily stub
Date.now()to a constant to make the tie deterministic.