Skip to content

fix(codegen): sort schema snapshot keys by code unit, not locale - #188

Merged
prisis merged 2 commits into
alphafrom
fix/codegen-locale-sort
Jul 25, 2026
Merged

prisis merged 2 commits into
alphafrom
fix/codegen-locale-sort

Conversation

@prisis

@prisis prisis commented Jul 25, 2026 •

Copy link
Copy Markdown
Member

The two follow-ups flagged during the benchmark/perf audit in #185, which were out of scope there. Independent of that PR — this branches off alpha directly.

1. codegen — schema snapshot ordering was locale-dependent

packages/codegen/src/schema-drift.ts sorted table names and migration ids with localeCompare. That resolves against the runtime's default locale and ICU version, so it is not stable across machines — which defeats the entire point, since .lunora-schema.json is a committed file and the sort exists specifically to make it byte-stable.

The failure mode: two developers, or a developer and CI, or one machine either side of a Node upgrade, regenerate the same schema and get different bytes. That reads as a spurious diff and a false drift signal.

Now sorted by UTF-16 code unit, which is locale-independent. Same bug class as the two instances fixed in @lunora/replica in #185 — this was the third, in a different package.

No committed snapshot changes. I checked all 9 in-repo .lunora-schema.json files object-by-object (191 objects) and no key ordering differs between the two comparators: every table and migration name is lowercase ASCII, where ICU collation and code-unit order agree. So no fixture regeneration, and the diff is confined to source plus a test.

The new test uses fixtures where the two orderings genuinely disagree (Berries / _hidden / apples, and A_one / b_two), so a regression to a locale-aware comparator flips it. Without that, a test using ordinary lowercase names would pass under either comparator and guard nothing.

Field / index / relation keys are left in declaration order — they were never locale-sorted, and that is now documented rather than left ambiguous.

2. cli — the init smoke suite runs against too tight a timeout

Each case scaffolds a whole project, parses its schema and function modules through runCodegen (a real TypeScript parse), and runs the wrangler validator. That is far more work than a unit test, and it lands against the shared 10s local budget (tools/get-vitest-config.ts; CI gets 30s).

On an unloaded machine the heaviest case takes ~6.4s of that 10s — thin enough that running anything else concurrently tips it over. It failed exactly that way twice during the #185 audit and passed on re-run, while CI stayed green. Each occurrence costs a full re-run to diagnose.

Scoped to this suite via describe(name, { timeout }, fn) rather than raising the global default, so genuinely hung unit tests elsewhere still fail fast.

Worth noting for anyone who saw my earlier description of this: the suite is offline — it scaffolds via --from against the local templates root, not giget. The cost is CPU, not network.

Verification

codegen 864 tests, cli 684 tests, types clean, zero eslint errors. Affected run green across 6 projects.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Schema snapshots are now generated consistently across different locales and environments.
    • Migration ordering and snapshot key ordering are deterministic, producing stable output for comparisons and reviews.
    • Existing declaration order for fields, indexes, and relations is preserved.

@netlify

netlify Bot commented Jul 25, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for lunorash ready!

Name Link
🔨 Latest commit b3919a6
🔍 Latest deploy log https://app.netlify.com/projects/lunorash/deploys/6a649bb3a799d900088ac119
😎 Deploy Preview https://deploy-preview-188--lunorash.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@github-actions

Copy link
Copy Markdown
Contributor

Thank you for following the naming conventions! 🙏

@coderabbitai

coderabbitai Bot commented Jul 25, 2026 •

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@prisis, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 15 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 3d5b4c08-7a22-49aa-b847-2ec14ce248be

📥 Commits

Reviewing files that changed from the base of the PR and between 8585afc and b3919a6.

⛔ Files ignored due to path filters (2)
  • packages/cli/__tests__/commands/init-smoke.test.ts is excluded by !**/__tests__/**, !**/*.test.ts and included by packages/**
  • packages/codegen/__tests__/schema-drift.test.ts is excluded by !**/__tests__/**, !**/*.test.ts and included by packages/**
📒 Files selected for processing (1)
  • packages/codegen/src/schema-drift.ts

Walkthrough

Schema snapshot generation now uses locale-independent code-unit sorting for record keys and migration IDs, producing byte-stable JSON while retaining declaration order for fields, indexes, and relations.

Changes

Schema snapshot determinism

Layer / File(s) Summary
Stable snapshot ordering
packages/codegen/src/schema-drift.ts
sortKeys and buildSchemaSnapshot now use plain lexicographic sorting, and comments document byte stability and preserved declaration order for schema members.

Estimated code review effort: 2 (Simple) | ~5 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title matches the main schema-ordering change, though it doesn't mention the CLI timeout tweak.
Description check ✅ Passed It explains the change, rationale, and verification, though it doesn't follow the template headings exactly.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/codegen-locale-sort

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions

Copy link
Copy Markdown
Contributor

Thank you for confirming the Contributor License Agreement! 🙏

prisis and others added 2 commits July 25, 2026 13:19
`.lunora-schema.json` is a COMMITTED file whose whole purpose is to be
byte-stable so drift detection is meaningful. It sorted table names and
migration ids with `localeCompare`, which resolves against the runtime's
default locale and ICU version — so two developers, or a developer and
CI, or one machine either side of a Node upgrade, could regenerate the
same schema and produce different bytes. That surfaces as a spurious diff
and a false drift signal, which is exactly what the sort was there to
prevent.

Sorts by UTF-16 code unit instead, which is locale-independent.

No committed snapshot changes: all 9 in-repo `.lunora-schema.json` files
were checked object-by-object (191 objects) and no key ordering differs
between the two comparators — their names are all lowercase ASCII, where
ICU collation and code-unit order agree. So no fixture regeneration.

Field / index / relation keys are deliberately left in declaration order
and are now documented as such; they were never locale-sorted.

Found while auditing `@lunora/replica`, which had the same bug in two
places (see #185).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Each case scaffolds a whole project, parses its schema and function
modules through `runCodegen` (a real TypeScript parse), and runs the
wrangler validator — far more work than a unit test, against the shared
10s local budget (CI gets 30s).

On an unloaded machine the heaviest case takes ~6.4s of that 10s, thin
enough that running anything else concurrently tips it over. It failed
that way twice during an unrelated audit and passed on re-run, while CI
stayed green — a flake that costs a full re-run to diagnose each time.

Scoped to this suite rather than raising the global default, so genuinely
hung unit tests elsewhere still fail fast.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@prisis
prisis force-pushed the fix/codegen-locale-sort branch from 8585afc to b3919a6 Compare July 25, 2026 11:19
@prisis
prisis merged commit a4ec543 into alpha Jul 25, 2026
39 of 40 checks passed
@prisis
prisis deleted the fix/codegen-locale-sort branch July 25, 2026 11:27
@codspeed

codspeed Bot commented Jul 25, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 4 untouched benchmarks
⏩ 237 skipped benchmarks1


Comparing fix/codegen-locale-sort (b3919a6) with alpha (86ad0e3)2

Open in CodSpeed

Footnotes

  1. 237 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

  2. No successful run was found on alpha (f1369be) during the generation of this report, so 86ad0e3 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@github-actions

github-actions Bot commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using our GitHub Discussions tab for questions.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Sep 8, 2026

This branch was previously deployed

1 inactive deployment
benchmarks — b3919a6c Deployed Jul 25, 2026 by prisis via Benchmarks #939
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant