Repository navigation
fix(ulid): unify monotonic overflow with uuidv7 - #118
Conversation
|
Preview removed because the pull request was closed. |
commit: |
98fb0f3 to
64f1ad4
Compare
6880ddb to
59e495b
Compare
Codex reviewReviewed with
1. [P2] My change introduced a 48-bit timestamp overflow — fixedReproduced exactly as reported, with The borrowed millisecond ( Worse, uuidv7 had the same latent bug already — I also restored the 2. [P3] The migration guide silently lost a historical code — fixedDropping Regression tests added
|
Benchmark ResultsGeneration
Validation
Cold Start (Node)
Comparison vs Main
❌ Performance regression detected!
Bundle Impact
*Excludes external dependencies (e.g., @noble/hashes) Generated by workflow run 43772707826 |
UUID v7 and ULID hit the same situation with two different answers. When the per-time-unit sequence is exhausted, `uuidv7()` advanced its timestamp by one millisecond and carried on; `ulid()` filled the remaining random bytes with `0xff` and threw `RANDOM_OVERFLOW`, which also left every later call in that millisecond failing. Both now advance virtual time by one millisecond and continue from a zeroed counter, so `incrementBytesInPlace` leaving the buffer at zero on overflow is exactly the state the new bucket wants. That unification exposed a bug both generators already had: neither bounded the advance against the end of the 48-bit timestamp field. At the maximum millisecond, uuidv7 wrote a timestamp that wrapped to zero, ULID's string path emitted `80000000000000000000000000` (which its own `isValid()` rejects), and ULID's buffer path also wrapped the timestamp. Both now saturate and throw `TIMESTAMP_OUT_OF_RANGE`, and ULID keeps the `fill(0xff)` that makes the saturated state fail closed on every later call. `RANDOM_OVERFLOW` is removed from `ERROR_CODES`: the random-portion overflow now recovers instead of failing, so there is no such failure to report. Pre-1.0, this is a breaking change to the error surface; `migration/v1.mdx` documents the code as removed with no replacement, and drops the `ULID_RANDOM_OVERFLOW` mapping.
64f1ad4 to
c131077
Compare
59e495b to
c7feebf
Compare
Stacked on #117.
Problem
UUID v7 and ULID hit the same situation with two different answers. When a time unit's sequence is exhausted,
uuidv7()advances its timestamp by one millisecond and carries on;ulid()did this:incrementBytesInPlacehas already left the buffer at all zeros when it reports overflow, so thefill(0xff)only served to make every later call in that millisecond overflow too. The generator bricked itself until the wall clock moved.Two generators in one package should not have two policies for one problem.
Fix
Both now advance virtual time by one millisecond and continue from a zeroed counter:
RANDOM_OVERFLOWis removed fromERROR_CODES. At 1 in 2^80 per millisecond, the old branch was unreachable, so a catalogued, documented, documented-as-stable error code could never fire. Keeping it contradicted the claim thatERROR_CODESis the authoritative list of failures the API can produce.Pre-1.0 (
0.8.0), and this follows theTODO(v1-rc.1)cleanup window already in the repo. Concretely:ERROR_CODESno longer containsRANDOM_OVERFLOW.reference/errors.mdxdrops its row.migration/v1.mdxdrops theULID_RANDOM_OVERFLOW→RANDOM_OVERFLOWmapping, anderrors.test.tsdrops it from the legacy list.The repo's own docs↔code parity tests enforce all of that, and they pass.
Tests
Replaces
throws when the monotonic random portion overflowswithadvances virtual time when the monotonic random portion overflows: seeds every random byte to0xff, generates twice, and asserts the second ID carriestimestamp === ms + 1, sorts after the first, and is still valid.Changeset is
minor.