Skip to content

Split null and undefined off every union comparator, in place of the discriminant ?. - #460

Open
DZakh wants to merge 14 commits into
mainfrom
claude/schema-failing-cases-u2dnpj
Open

DZakh wants to merge 14 commits into
mainfrom
claude/schema-failing-cases-u2dnpj

Conversation

@DZakh

@DZakh DZakh commented Sep 24, 2026 •

Copy link
Copy Markdown
Owner

Known bug eq-tagged-union-absent. #461 has since fixed it on main by optional-chaining every discriminant read. This PR replaces that with a structural fix, and it's now merged with main.

const eq = S.isEqualOutput(S.nullable(S.union([S.schema({ TAG: "A", _0: S.string }), S.schema({ TAG: "B", _0: S.number })])));
eq(null, { TAG: "A", _0: "x" }); // before #461: TypeError reading 'TAG'

Cause

When object members share a type narrow, the discriminant pass swaps each one's type test for a property read, a.TAG==="A". That read throws on null and undefined, which the union's nullish arm was waiting further down the chain to catch.

Fix

The comparator already split nullish literals off S.optional(X) / S.nullable(X), but only when exactly one member was left. That rule now covers any count: null and undefined are tested first on both values, and the rest of the union dispatches only once each value is known to be neither. No discriminant read ever sees either value, so the ?. #461 added is dropped. That leaves one mechanism instead of two.

-(a,b)=>a===b||(a?.TAG==="A"?b?.TAG==="A"&&a._0===b._0:a?.TAG==="B"?b?.TAG==="B"&&a._0===b._0:b===null)   // main
+(a,b)=>a===b||(a===null?b===null:b!==null&&(a.TAG==="A"?b.TAG==="A"&&a._0===b._0:b.TAG==="B"&&a._0===b._0))

compare is unchanged: a union with more than one member of substance still has no order and still refuses.

What changes for you

Comparing values of a union that holds S.env no longer crashes, and compare on a tuple with an S.env slot orders by every slot.

const eq = S.isEqualOutput(S.union([S.schema({ TAG: "A", _0: S.string }), S.schema({ TAG: "B", _0: S.number }), S.env]));
eq({ TAG: "A", _0: "x" }, undefined); // main: TypeError reading 'TAG'   now: false

const cmp = S.compareOutput(S.tuple([S.env, S.string]));
cmp([undefined, "a"], [undefined, "b"]); // main: 0   now: -1
cmp([undefined, "b"], [undefined, "a"]); // main: 0   now: 1

X | null unions already answered right on main; they now compile to the split form above.

Impact (bundle vs main)

  • Not breaking. Every other isEqual* / compare* answer is unchanged.
  • Library, gzip: compare* +27 B, isEqual* +21/+22 B, total 73883 -> 73881 (-2).
  • Generated code: 51 comparator goldens change, -110 chars in total (the env compare leaf is shorter).
  • Speed: comparators on tagged and optional unions are faster (table below). spec check --perf=only shows no significant change, as it times parse and creation, not comparators.

Decision

Two ways to fix the env crash: (a) this PR's split of null/undefined off every union comparator, or (b) main's discriminant ?. with S.env added to the members that trigger it, plus the same env compare leaf fix. Both answer identically on every spec and both fix both bugs. Measured on the merged branch (isEqualOutput over every value pair, best of 25 runs, ns/call, two runs each):

schema main (a) split (b) ?. + env
A | B | null (tagged) 7.8-9.2 4.1-4.4 9.6-10.2
optional(A | B) (tagged) 12.5 7.1-7.3 12.7-13.1
A | B | S.env (tagged) crashes 10.3-12.8 14.9-15.0
optional(number | string | object) 10.5-10.6 7.6-9.5 10.4-10.5
nullable(number | string | boolean) 5.5-7.0 5.7-6.8 6.7-6.9
library gzip isEqual* / compare* vs main - +21 / +27 +6 / +7
generated comparator code vs main - -110 chars -340 chars

(b) is smaller, but positive-flow speed ranks above bundle size, and (a) is 1.2-2.3x faster on every tagged or optional union because one up-front a===void 0 / a===null replaces a ?. on each discriminant read of both values. Creation+compile time is within noise for both. Kept (a).

Details

  • Specs: eq-tagged-union-env and compare-tuple-env hold the two bugs. eq-tagged-union-nullable, union-tagged-optional, fromjsonschema-allof-base-schema and the protobuf oneof specs take the split form; about 30 env / codec-env-* / optional-env-* specs take the new env compare leaf (b===void 0)-(a===void 0)||(a<b?-1:a>b?1:0).
  • Fuzzers: the env sampler and the tagged-beside-leaf grammar draw both bugs. fuzz:schema (eq laws, compare antisymmetry and agreement with isEqual) and fuzz:union --seed=1 report no findings.
  • Merge: conflicts in fromjsonschema-allof-base-schema.yaml, bundleSize.yaml and the benchmark goldens took main's side and were regenerated; eq.ts merged cleanly.
  • Gates: full pnpm verify passes, every step.
  • Not done: in S.optional(S.env) / S.nullable(S.env) compare, the env leaf still re-tests undefined after the union split it off. Redundant, never a wrong answer, and already present on main.

🤖 Generated with Claude Code

https://claude.ai/code/session_01XdDnkN38pECC46XqMZbwp8

https://claude.ai/code/session_01X9uDwr3yJ2L75BYdetqyE1

Summary by CodeRabbit

  • Bug Fixes

    • Corrected equality checks for unions and schema-derived values, including cases involving null, undefined, and missing selectors.
    • Updated ordering for some optional values, including cases where undefined and null are compared.
  • New Features

    • Added support for comparing and validating fixed-length tuples and unions that include environment values.
  • Chores

    • Refreshed bundle-size measurements; overall reported size decreased slightly.

`S.isEqualOutput(S.nullable(S.union([A, B])))` threw on `(null, a)`: the
discriminant pass swaps each object member's type test for a property read,
`a.TAG==="A"`, and that read is a TypeError on the two values the union's
null arm was waiting further down the chain to catch. The same read on `b`
threw for `(a, null)`.

The nullish split already existed, but only for one member of substance.
It now applies to any count: null and undefined are tested first on both
values, and the rest of the union dispatches only once each value is known
to be neither. That is sound for the reason it already was, since no other
narrow admits either value, and it makes every narrow's domain the values a
property read can't throw on.

Known bug `eq-tagged-union-absent` is gone from scripts/knownBugs.ts and
`eq-tagged-union-nullable` now carries the `null` example the harness could
not compare before. Three specs with a trailing null member now test it
first, which also drops the last arm's redundant narrow on `a`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XdDnkN38pECC46XqMZbwp8
@coderabbitai

coderabbitai Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review.

Walkthrough

The pull request updates generated union equality handling, rewrites environment-value comparison expressions, adds specifications and fuzz inputs for these cases, and refreshes bundle-size measurements.

Changes: Sury nullish and comparison behavior

Layer / File(s) Summary
Nullish union equality and coverage
packages/sury/src/eq.ts, packages/sury/specs/eq-tagged-union-env.yaml, packages/sury/specs/eq-tagged-union-nullable.yaml, packages/sury/specs/codec-json-optional-union-literal-first.yaml, packages/sury/specs/fromjsonschema-*, packages/sury/scripts/unionFuzz/*, packages/sury/specs/bundleSize.yaml, packages/benchmarks/goldens/*
Union equality generation separates nullish members from other members and treats S.env as admitting undefined. Specifications and fuzz generation cover tagged unions, nullish values, and selector equality. Bundle-size measurements are refreshed.
Environment comparison ordering
packages/sury/src/eq.ts, packages/sury/specs/codec-env-*.yaml, packages/sury/specs/codec-nullable-env-to-port.yaml, packages/sury/specs/codec-optional-*.yaml, packages/sury/specs/env.yaml, packages/sury/specs/optional-env-*.yaml, packages/sury/specs/union-env-*.yaml, packages/sury/specs/compare-tuple-env.yaml
Many comparison expressions replace nested undefined checks with arithmetic checks. Most retain their ordering; some now place undefined after defined values, or before null. The tuple specification adds parsing, equality, and ordering expectations.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix

Merge Risk: Minimal · up to 313b5

The nullish-union equality cases are covered, and the comparison behavior remains consistent with the documented contract. No actionable merge risk remains.

Architecture risk: Medium · up to 313b5

The change affects 2 systems.

Changed systems: packages/sury, packages/benchmarks

Architecture concerns
No architecture-level concerns identified.

Review details

Systems and components

  • observed — packages/sury (library) was modified; 38 changed files map to changed impact.
  • observed — packages/benchmarks (library) was modified; 3 changed files map to changed impact.

Before / after behavior

  • observed — Modified behavior in packages/sury/scripts/unionFuzz/sample.ts: For string schemas with format "env", sample now returns undefined when pick is 0 and "x" when pick is 1.
  • observed — Modified behavior in packages/sury/specs/codec-env-boolean-to-optional.yaml: compare.input replaces its nested undefined ordering with a boolean-difference term before comparing defined values.
  • observed — Modified behavior in packages/sury/specs/codec-env-date.yaml: compare.input replaces nested explicit undefined checks with a boolean-difference check before relational comparison; undefined remains ordered before defined values.
  • observed — Modified behavior in packages/sury/specs/codec-env-email.yaml: compare.input replaces nested undefined checks with a boolean-difference ordering check, then compares values using < and >.

Reliability and maintainability

  • inferred — Risk-relevant change factors for packages/sury: blast_radius_1; direct_dependents_1
Pre-merge checks: Passed 4
Check name Status Explanation
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.
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: union comparators now split null and undefined before dispatch, replacing the discriminant optional chaining. The changeset matches this. The raw summary shows the src…
Full details: Title check

Explanation

The title names the main change: union comparators now split null and undefined before dispatch, replacing the discriminant optional chaining. The changeset matches this. The raw summary shows the src/eq.ts nullish split, the removal of nested undefined checks across many env specs, and the new eq-tagged-union-env and compare-tuple-env specs. The PR objectives describe the same nullish split, which replaces optional chaining on union discriminant reads. The title is specific and accurate, though it is long at 86 characters.

Finishing Touches
Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Autopilot is currently an internal CodeRabbit preview.

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
ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: 22ae103c-4a1f-4b16-a4ed-b7ad9cf3c1a7

📥 Commits

Reviewing files that changed from the base of the PR and between bec8731 and 313b5b6.

📒 Files selected for processing (3)
  • packages/benchmarks/goldens/schema.json
  • packages/sury/scripts/unionFuzz/generate.ts
  • packages/sury/specs/bundleSize.yaml
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/sury/specs/bundleSize.yaml

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 0 remain after this review.

@github-actions

Copy link
Copy Markdown

Spec performance

3 slower, 1 faster of 3733 timed targets · 1e2a1e6 vs e84e97a (origin/main)

Slower

target Δ vs baseline
bigint-multipleOf · create+compile · encode +4.5% slower
file-minSize · create+compile · encode +4.1% slower
set-minSize · create+compile · decode +3.0% slower

Faster

target Δ vs baseline
jsonstring-union-encode · decode · accepts ×2 -4.6% faster

Full report ↗

+% slower than baseline, -% faster · noise floor create 3.3% · create+compile 3.0% · run 3.0% · scenario 3.0%
3729 unchanged · 132 constant-schema targets skipped · 40 async examples skipped · advisory only
node 24.16.0 · linux x64 · 4 cores · 8×2 rounds · 2 screening jobs · confirmed by 2 fresh processes

#461 fixed `eq-tagged-union-absent` on main by optional-chaining every
discriminant read (`a?.TAG`, `b?.TAG`) once a union has a nullish member.
With null and undefined tested first on both values, no discriminant read
ever sees either, so the chaining is dropped: one mechanism, not two, and
`eq-tagged-union-nullable` compiles to the split form. `isEqual*` and
`compare*` are 13 gzip bytes smaller than main's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XdDnkN38pECC46XqMZbwp8
@DZakh DZakh changed the title Split null and undefined off every union comparator, not just X | null Split null and undefined off every union comparator, in place of the discriminant ?. Sep 24, 2026
@github-actions

Copy link
Copy Markdown

Spec performance

1 slower, 1 faster of 3848 timed targets · b1696ec vs 4a723c8 (origin/main)

Slower

target Δ vs baseline
jsonstring-object-union-jsonstring-field · parse · accepts ×2 +7.4% slower

Faster

target Δ vs baseline
is-output · scenario -6.8% faster

Full report ↗

+% slower than baseline, -% faster · noise floor create 3.2% · create+compile 4.2% · run 3.0% · scenario 3.0%
3846 unchanged · 132 constant-schema targets skipped · 40 async examples skipped · advisory only
node 24.16.0 · linux x64 · 4 cores · 8×2 rounds · 2 screening jobs · confirmed by 2 fresh processes

Generated files (bundleSize.yaml, benchmark goldens) regenerated rather
than hand-merged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XdDnkN38pECC46XqMZbwp8
A tagged union beside `S.env` still threw in `isEqual`: env is tagged
string but admits undefined, so it stayed in the dispatch and the
discriminant read `a.TAG` on undefined. It now adds undefined to the
nullish split and stays in the dispatch for its strings:

  S.isEqualOutput(S.union([A, B, S.env]))(undefined, { TAG: "A", _0: "x" })
  // before: TypeError, after: false

`compare` of a tuple with an env slot answered 0 for
`[undefined, "a"]` vs `[undefined, "b"]`: env's leaf held a 0 in the
middle of its ternary, and chaining the next slot into the trailing
`:0` left that inner 0 ending the whole compare. The leaf is now
`(b===void 0)-(a===void 0)||(a<b?-1:a>b?1:0)`, which chains with `||`.

Neither was reachable by a fuzzer: the sampler had no value for env,
and a nested union has two members, so a tagged pair beside a third
never drew. Env now samples "x" or undefined, and the grammar draws
two tagged objects beside a leaf; without the fix, fuzz:schema reports
both bugs. Specs: eq-tagged-union-env, compare-tuple-env.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XdDnkN38pECC46XqMZbwp8
@github-actions

Copy link
Copy Markdown

Spec performance

1 slower of 3881 timed targets · 0f9c473 vs b9bf517 (origin/main)

Slower

target Δ vs baseline
port-gte · create+compile · encode +3.3% slower

Full report ↗

+% slower than baseline, -% faster · noise floor create 7.8% · create+compile 3.0% · run 7.5% · scenario 12.9%
3880 unchanged · 132 constant-schema targets skipped · 45 async examples skipped · advisory only
node 24.16.0 · linux x64 · 4 cores · 8×2 rounds · 2 screening jobs · confirmed by 2 fresh processes

@github-actions

Copy link
Copy Markdown

Spec performance

No significant changes of 3881 timed targets · 8518317 vs b9bf517 (origin/main)

Full report ↗

+% slower than baseline, -% faster · noise floor create 3.0% · create+compile 3.2% · run 13.6% · scenario 6.0%
3881 unchanged · 132 constant-schema targets skipped · 45 async examples skipped · advisory only
node 24.16.0 · linux x64 · 4 cores · 8×2 rounds · 2 screening jobs · confirmed by 2 fresh processes

No conflicts; every golden regenerates unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XdDnkN38pECC46XqMZbwp8
Generated files (bundleSize.yaml, benchmark goldens) regenerated rather
than hand-merged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XdDnkN38pECC46XqMZbwp8
@github-actions

Copy link
Copy Markdown

Spec performance

No significant changes of 3954 timed targets · b2f591a vs 0491207 (origin/main)

Full report ↗

+% slower than baseline, -% faster · noise floor create 5.5% · create+compile 4.6% · run 3.4% · async 3.1% · scenario 37.6%
3954 unchanged · 132 constant-schema targets skipped · advisory only
node 24.16.0 · linux x64 · 4 cores · 8×2 rounds · 2 screening jobs · confirmed by 2 fresh processes

claude added 2 commits October 1, 2026 09:54
Generated files (bundleSize.yaml, benchmark goldens) regenerated rather
than hand-merged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XdDnkN38pECC46XqMZbwp8
Generated files (bundleSize.yaml, benchmark goldens) regenerated rather
than hand-merged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XdDnkN38pECC46XqMZbwp8
@github-actions

github-actions Bot commented Oct 1, 2026

Copy link
Copy Markdown

Spec performance

1 slower of 4233 timed targets · bec8731 vs 4b9e6fb (origin/main)

Slower

target Δ vs baseline
codec-json-union-recursive-union-member · parse · accepts ×3 +17.2% slower

Full report ↗

+% slower than baseline, -% faster · noise floor create 6.7% · create+compile 3.0% · run 3.0% · async 3.0% · scenario 3.8%
4232 unchanged · 132 constant-schema targets skipped · advisory only
node 24.16.0 · linux x64 · 4 cores · 8×2 rounds · 2 screening jobs · confirmed by 2 fresh processes

Generated files (bundleSize.yaml, benchmark goldens) regenerated rather
than hand-merged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XdDnkN38pECC46XqMZbwp8
@github-actions

github-actions Bot commented Oct 1, 2026

Copy link
Copy Markdown

Spec performance

1 slower of 4255 timed targets · 67ad18d vs 5522d8f (origin/main)

Slower

target Δ vs baseline
codec-json-union-recursive-union-member · parse · accepts ×3 +12.0% slower

Full report ↗

+% slower than baseline, -% faster · noise floor create 3.0% · create+compile 4.9% · run 3.0% · async 7.1% · scenario 4.3%
4254 unchanged · 132 constant-schema targets skipped · advisory only
node 24.16.0 · linux x64 · 4 cores · 8×2 rounds · 2 screening jobs · confirmed by 2 fresh processes

Version bump only; no conflicts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XdDnkN38pECC46XqMZbwp8
@github-actions

github-actions Bot commented Oct 1, 2026

Copy link
Copy Markdown

Spec performance

1 slower of 4255 timed targets · 313b5b6 vs 63b73cf (origin/main)

Slower

target Δ vs baseline
codec-json-union-recursive-union-member · parse · accepts ×3 +13.7% slower

Full report ↗

+% slower than baseline, -% faster · noise floor create 10.9% · create+compile 3.0% · run 6.2% · async 3.2% · scenario 14.9%
4254 unchanged · 132 constant-schema targets skipped · advisory only
node 24.16.0 · linux x64 · 4 cores · 8×2 rounds · 2 screening jobs · confirmed by 2 fresh processes

Generated goldens take main's side and are regenerated after.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X9uDwr3yJ2L75BYdetqyE1
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X9uDwr3yJ2L75BYdetqyE1
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X9uDwr3yJ2L75BYdetqyE1
@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown

Spec performance

2 faster of 6475 timed targets · 2550c1c vs 2ebcac6 (origin/main)

Faster

target Δ vs baseline
codec-jsonstring-bigint-array · decode · accepts -5.2% faster
codec-string-jsonstring-pack · decode · accepts ×2 -12.1% faster

Full report ↗

+% slower than baseline, -% faster · noise floor create 3.0% · create+compile 4.6% · run 3.8% · async 3.0% · scenario 22.1%
6473 unchanged · 134 constant-schema targets skipped · advisory only
node 24.16.0 · linux x64 · 4 cores · 8×2 rounds · 2 screening jobs · confirmed by 2 fresh processes

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.

2 participants