Skip to content

feat: timestamp versions 3 and 4 lay the magnitude out most significant bit first so TA and TB ids sort by byte order - #90

Merged
Shinrai merged 1 commit into
nextfrom
fix/timestamp-segment-order
Nov 7, 2026
Merged

Shinrai merged 1 commit into
nextfrom
fix/timestamp-segment-order

Conversation

@cldmv-bot

@cldmv-bot cldmv-bot Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Closes #89.

Segment A of the timestamp variants wrote the magnitude's low 32 bits at bits 1-32 and the upper 31 at bits 33-63, so TA/TB byte order followed time only inside one 2^32 window and rolled over at every boundary (49.7 days for TB, 136 years for TA).

Non-breaking fix through the version field:

  • timestamp version 3 (seconds) and version 4 (milliseconds) carry the ordered layout: upper 31 bits at bits 1-31, low 32 at bits 32-63, bits 69-74 unchanged;
  • UUID.TA() mints version 3 and UUID.TB() version 4; an explicit UUID.timestamp(ts, 1|2) still mints the legacy layout;
  • versions 1 and 2 decode exactly as before, so every stored id keeps its timestamp;
  • UUID.migrate(input) rewrites a legacy id to the ordered layout deterministically (same sign, same timestamp, same 49 entropy bits; idempotent; non-timestamp ids pass through), and uuid.isLegacyTimestampLayout() selects rows to migrate;
  • validators.mjs read segment A as one contiguous value, which was wrong for the old layout; it now reads by version.

Docs: README TA/TB sections, version table, bit layout and a "Migrating stored ids to versions 3 and 4" section; uuid-spec.md 8.2 gains versions 3 and 4. Tests: fixed vectors for 3/4, the 1.3.1 legacy vectors decoding unchanged, migration vectors, idempotence, pass-through, a 1000-id legacy sample migrated and byte-sorted equals its timestamp order, and the 2^32 boundaries. 310 vitest tests plus the CJS and type-consumer suites green, build:ci clean.

Consumers migrate stored ids with the helper; old and new ids interleave in byte order until they do.

@cldmv-bot cldmv-bot Bot added ! fix → next v4 flow: fix contributor PR targeting the next integration branch area: core Touches core library / runtime source code area: tests Touches test files, fixtures, or test infrastructure type: documentation Relates to docs, README updates, guides, or inline code comments labels Nov 3, 2026
@Shinrai Shinrai added type: bug Something is broken or not behaving as expected and removed type: documentation Relates to docs, README updates, guides, or inline code comments labels Nov 3, 2026
@Shinrai
Shinrai force-pushed the fix/timestamp-segment-order branch from 5debb41 to 9df1538 Compare November 3, 2026 05:25
@cldmv-bot cldmv-bot Bot changed the title fix!: store the timestamp magnitude most significant bit first so TA and TB ids sort by byte order feat: timestamp versions 3 and 4 lay the magnitude out most significant bit first so TA and TB ids sort by byte order Nov 3, 2026
@cldmv-bot

cldmv-bot Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

Auto-normalized PR title: rewrote PR title to match the highest-priority commit type (feat).

  • Before: fix!: store the timestamp magnitude most significant bit first so TA and TB ids sort by byte order
  • After: feat: timestamp versions 3 and 4 lay the magnitude out most significant bit first so TA and TB ids sort by byte order

If this isn't what you want, edit the title — the normalizer won't re-fire as long as the title stays conventional.

@cldmv-bot cldmv-bot Bot added type: documentation Relates to docs, README updates, guides, or inline code comments type: feature Implements new functionality — a PR or issue that adds a feature labels Nov 3, 2026
…nt bit first so TA and TB ids sort by byte order

The released timestamp layout wrote the low 32 bits of the 69-bit magnitude at bits 1-32 and the upper 31 bits at bits 33-63, so byte order followed the timestamp only inside one 2^32 window and rolled over at every boundary (49.7 days for milliseconds, 136 years for seconds). That broke spec 7.3 for a hex string or a database uuid column.

Versions 3 (seconds) and 4 (milliseconds) use an ordered layout: magnitude bits 62..32 at bits 1-31 and bits 31..0 at bits 32-63, most significant bit first across segment A. Bits 69-74 are unchanged. Versions 1 and 2 keep the old low-word-first layout exactly, so every id already minted decodes as before. getTimestamp() and the validators pick the layout from the version, precision follows the version (1 and 3 seconds, 2 and 4 milliseconds), and version() reports TA for 1 and 3 and TB for 2 and 4.

UUID.TA() now mints version 3 and UUID.TB() mints version 4. UUID.timestamp() and createTimestampVariant() with an explicit version 1 or 2 still mint the legacy layout, with 3 or 4 the ordered one.

UUID.migrate(input) rewrites a stored legacy id (UUID, string or 16-byte buffer) into the ordered layout: version 1 becomes 3, version 2 becomes 4, with the same sign, timestamp value and 49 entropy bits. It is deterministic and idempotent, and returns an equal copy for ids already at version 3 or 4 and for non-timestamp UUIDs. UUID#isLegacyTimestampLayout() selects the rows to migrate. The README documents the migration and uuid-spec.md section 8.2 gains Timestamp Version 3 and 4.

Closes #89
@Shinrai
Shinrai force-pushed the fix/timestamp-segment-order branch from 9df1538 to a634a1e Compare November 3, 2026 05:27
@Shinrai Shinrai removed type: bug Something is broken or not behaving as expected type: documentation Relates to docs, README updates, guides, or inline code comments labels Nov 3, 2026
@Shinrai
Shinrai merged commit 419f9eb into next Nov 7, 2026
27 checks passed
@cldmv-bot
cldmv-bot Bot deleted the fix/timestamp-segment-order branch November 7, 2026 01:21
cldmv-bot Bot added a commit that referenced this pull request Nov 7, 2026
# `@cldmv/uuid` v1.4.0 Changelog

**Release Date**: November 2026
**Release Type**: Minor
**Branch**: `release/1.4.0`

---

## Overview

v1.4.0 makes the Timestamp Variant sort by byte order. Until now `UUID.TA()` and `UUID.TB()` wrote the timestamp magnitude's low 32 bits ahead of its upper 31, so two ids compared as bytes only followed time inside one 2^32 window and reversed at every boundary: every 49.7 days for `TB` (milliseconds), every 136 years for `TA` (seconds). Any store that orders rows or cursors by a `TB` id broke silently at each rollover.

The fix is non-breaking and goes through the version field. Timestamp versions 3 (seconds) and 4 (milliseconds) carry the ordered layout, `UUID.TA()` and `UUID.TB()` now mint them by default, and versions 1 and 2 decode exactly as before, so every stored id keeps its timestamp. `UUID.migrate()` rewrites a stored legacy id to the ordered layout deterministically, and `isLegacyTimestampLayout()` selects the rows that need it.

---

## ✨ Features

### Timestamp versions 3 and 4 sort by byte order ([#90](#90), fixes [#89](#89))

- Versions 3 (seconds) and 4 (milliseconds) lay the magnitude out most significant bit first: the upper 31 bits at bits 1-31 and the low 32 at bits 32-63. The sign bit, the entropy bits and bits 69-74 are unchanged, so the version field is the only thing that tells the layouts apart.
- `UUID.TA()` mints version 3 and `UUID.TB()` mints version 4. An explicit `UUID.timestamp(ts, 1)` or `UUID.timestamp(ts, 2)` still mints the legacy layout for anyone who needs it.
- Versions 1 and 2 decode as before: `getTimestamp()` on an id minted by 1.3.1 or earlier answers the same value it always did.
- `validators.mjs` read segment A as one contiguous value, which was wrong for the legacy layout; it now reads by version.

### `UUID.migrate()` and `isLegacyTimestampLayout()` ([#90](#90))

- `UUID.migrate(input)` rewrites a legacy timestamp id to version 3 or 4 with the same sign, the same timestamp and the same 49 entropy bits, so the result is deterministic. It is idempotent, and an id that is not a Timestamp Variant passes through unchanged.
- `uuid.isLegacyTimestampLayout()` is true for a version 1 or 2 timestamp id, which is the test to select stored rows for migration.
- Old and new ids interleave in byte order until every row is migrated, because versions 1 and 2 keep the legacy layout. Ordering by byte value is chronological only once every id involved is at version 3 or 4.

### Types

- The `.d.mts` declarations cover the new timestamp version modules, `UUID.migrate()` and `isLegacyTimestampLayout()`, and the consumer type test exercises them.

## 🔧 CI & tooling

### Lint and format scripts from `@cldmv/configs` ([#96](#96))

- The repository had an ESLint config that imported plugins it never installed and a Prettier config no script ran. `.configs/eslint.config.mjs` now spreads the shared `@cldmv/configs` base, `.configs/prettier.config.mjs` re-exports the shared options (without the jsonv plugin, since there are no `.jsonv` files here), and `.prettierignore` carries the managed block plus `types/`.
- `lint`, `lint:fix`, `format` and `format:check` come from `cldmv-configs init`; `build:ci` runs `lint` and `format:check` unconditionally; `prepare` installs the fleet pre-commit hook from `.githooks/`, which rejects a commit on a lint or formatting problem.
- Formatting drift in two files and thirteen lint errors (unused imports and bindings, a stale disable directive, two fences without a language) are fixed. No behaviour changes.

## 📚 Documentation

- README: the `TA` and `TB` sections, the version table and the bit layout describe versions 3 and 4, and a new "Migrating stored ids to versions 3 and 4" section walks through the row-by-row rewrite ([#90](#90)).
- `uuid-spec.md` 8.2 gains versions 3 and 4 ([#90](#90)).
- **NEW:** [docs/changelog/v1/v1.4.0.md](./v1.4.0.md): this changelog.
- README What's New promoted to v1.4.0.

## 🧪 Tests

- Fixed vectors for versions 3 and 4, the 1.3.1 legacy vectors decoding unchanged, migration vectors, idempotence and pass-through, a 1000-id legacy sample that byte-sorts in timestamp order after migration, and the 2^32 boundaries ([#90](#90)).

## 🔧 Dependencies

- `@cldmv/configs` ^1.2.4 → ^1.4.0, and `eslint` ^10.14.0 and `prettier` ^3.9.10 added, for the lint and format scripts ([#96](#96)). Development dependencies only.

---

## Upgrade notes

- No breaking changes; drop-in for v1.3.2. New `TA` and `TB` ids are versions 3 and 4; existing ids keep decoding.
- If you order rows or cursors by a `TB` or `TA` id, migrate the stored ids once: select the rows where `new UUID(id).isLegacyTimestampLayout()` is true and write back `UUID.migrate(id).toString()` (or `.toBuffer()`). Until the rewrite is complete, old and new ids interleave in byte order.

---

<!-- coverage-start -->

![coverage](https://img.shields.io/badge/coverage-84.0%25-green?style=for-the-badge&logo=vitest&logoColor=white)

| Metric | Coverage |
|--------|----------|
| Statements | 81.7% |
| Branches   | 83.6% |
| Functions  | 89.8% |
| Lines      | 81.2% |

*Avg: **84.0%** · `e58338c` · Node lts/**

<!-- coverage-end -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: core Touches core library / runtime source code area: tests Touches test files, fixtures, or test infrastructure ! fix → next v4 flow: fix contributor PR targeting the next integration branch type: feature Implements new functionality — a PR or issue that adds a feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant