Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 18, 2025

Problem

The CBOR encoding/decoding test for Configuration was flaky, occasionally failing with:

expect(received).toEqual(expected) // deep equality

- Expected  - 1
+ Received  + 1

  "database": Object {
    "host": "({ERU_fG",
    "name": "bzyd",
-   "port": 11.000000000000002,
+   "port": 11,
  },

The port field is defined as t.Number({format: 'u16', gte: 1, lte: 65535}), which should generate clean integers. However, floating-point precision artifacts like 11.000000000000002 were occasionally being generated, which CBOR would encode as floats. Upon decoding, these became integers, causing the equality check to fail.

Root Cause

In Random.num(), an epsilon value (0.000000000000001) was being added to min when gte was specified and subtracted from max when lte was specified:

// OLD CODE
if (gte !== undefined) min = gte + 0.000000000000001;
if (lte !== undefined) max = lte - 0.000000000000001;

This epsilon handling was intended to distinguish between inclusive (gte/lte) and exclusive (gt/lt) bounds for floating-point numbers. However, when combined with the Math.round() calculation for integer formats, it introduced floating-point precision errors:

// With u16 format, gte: 1, lte: 65535
min = 1.000000000000001  // epsilon added
max = 65535              // epsilon subtracted
result = Math.round(0.1538 * 65534) + 1.000000000000001
       = 10083 + 1.000000000000001
       = 10083.000000000000001  // floating-point artifact!

Solution

The fix detects integer formats (i8, i16, i32, i64, i, u8, u16, u32, u64, u) and avoids adding/subtracting epsilon for these types:

const isIntegerFormat = schema.format && 
  ['i8', 'i16', 'i32', 'i64', 'i', 'u8', 'u16', 'u32', 'u64', 'u'].includes(schema.format);

if (gte !== undefined) min = isIntegerFormat ? gte : gte + 0.000000000000001;
if (lte !== undefined) max = isIntegerFormat ? lte : lte - 0.000000000000001;

For integer formats, this ensures:

  • Clean integer bounds (e.g., min = 1 instead of min = 1.000000000000001)
  • No floating-point artifacts in the final result
  • CBOR correctly encodes values as integers, not floats

For floating-point formats (f32, f64) and untyped numbers, the epsilon behavior is preserved to maintain precise boundary handling.

Testing

Added comprehensive test coverage:

  1. Specific test for u16 format with gte/lte constraints (the failing case)
  2. Test across all integer formats to prevent regression
  3. Verified 100+ iterations produce only clean integers

The fix was validated with standalone tests showing:

  • Before: Found floating-point artifacts like 9.000000000000002
  • After: All 100,000 random values were clean integers ✅

Impact

  • Fixes: Flaky CBOR encoding/decoding tests for any types using integer formats with gte/lte
  • Preserves: Epsilon behavior for float formats where precision matters
  • Prevents: Future regressions with new test coverage

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • classic.yarnpkg.com
    • Triggering command: /usr/local/bin/node /home/REDACTED/.cache/node/corepack/v1/yarn/4.10.3/yarn.js set version classic --only-if-needed --yarn-path (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>Fix flaky test</issue_title>
<issue_description>This test sometimes fail, investigate and propose a solution:

 FAIL  packages/json-type/src/codegen/binary/cbor/__tests__/automated.spec.ts (5.699 s)
  ● can encode and decode Configuration

    expect(received).toEqual(expected) // deep equality

    - Expected  - 1
    + Received  + 1

    @@ -1,10 +1,10 @@
      Object {
        "database": Object {
          "host": "({ERU_fG",
          "name": "bzyd",
    -     "port": 11.000000000000002,
    +     "port": 11,
        },
        "environment": "development",
        "features": Object {
          "%8äm^г诶ä": false,
          "?uw8tZS[": true,

      18 |         const encoded = encoder.writer.flush();
      19 |         const decoded = decoder.decode(encoded);
    > 20 |         expect(decoded).toEqual(json);
         |                         ^
      21 |       } catch (error) {
      22 |         console.log(JSON.stringify(json, null, 2));
      23 |         console.log(type + '');

      at Object.<anonymous> (src/codegen/binary/cbor/__tests__/automated.spec.ts:20:25)
```</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>

Fixes #953


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI self-assigned this Oct 18, 2025
Copilot AI and others added 3 commits October 18, 2025 16:15
Co-authored-by: streamich <9773803+streamich@users.noreply.github.com>
Co-authored-by: streamich <9773803+streamich@users.noreply.github.com>
Co-authored-by: streamich <9773803+streamich@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix flaky test in automated.spec.ts Fix floating-point precision issues in Random.num() for integer formats Oct 18, 2025
Copilot AI requested a review from streamich October 18, 2025 16:21
Copilot finished work on behalf of streamich October 18, 2025 16:21
@streamich streamich marked this pull request as ready for review October 18, 2025 18:27
@streamich streamich merged commit ed523e1 into master Oct 18, 2025
7 of 18 checks passed
@streamich streamich deleted the copilot/fix-flaky-test branch October 18, 2025 19:39
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.

Fix flaky test

2 participants