Skip to content

[core] Keep doubles and large bigints exact in the QuickJS serde - #5775

Closed
Ultranmus wants to merge 1 commit into
vercel:mainfrom
Ultranmus:fix/quickjs-serde-doubles-bigints
Closed

Ultranmus wants to merge 1 commit into
vercel:mainfrom
Ultranmus:fix/quickjs-serde-doubles-bigints

Conversation

@Ultranmus

Copy link
Copy Markdown
Contributor

What

With WORKFLOW_VM=quickjs, distinct doubles now stay distinct and bigints outside int64 stay exact when they cross a workflow or step boundary. Fixes #5774.

Why

Both bugs come from quickjs-wasi 3.7.0 (vercel-labs/quickjs-wasi#68, still open) and silently corrupt data:

  • Doubles. A double handle gets a non-zero identity (its low 32 bits). identify in quickjs-serde.ts treated any handle with a non-zero identity, other than a string, as an object, so devalue deduplicated doubles with equal low words into back-references. [0.1, 0.2] came back as [0.1, 0.1].
  • Bigints. vm.newBigInt wraps values outside int64, so the BigInt reviver and fromPrimitive turned 2n ** 70n into 0n. Reading bigints was already exact, through the captured BigInt.prototype.toString.

The fix

  • identify treats every number handle as a primitive, as the issue suggests.
  • Bigints inside int64 still use vm.newBigInt. Larger ones are built by calling the guest's BigInt with their decimal string. BigInt is captured at boot with the other intrinsics, so user code can't patch it.
  • BigInt is captured as optional, because a VM restored from a snapshot taken before this change has a capture root without it. In that case the serde evaluates a bigint literal, which user code can't intercept either.

Once quickjs-wasi ships a fix for #68, the int64 split and the fallback can go.

How did you test your changes?

New cases in quickjs-serde.test.ts:

  • Wire parity with the node reference codec: [0.1, 0.2], [31260, 8104.4, 16056, 30925.6, 4400] (the issue's example), integers held as doubles (2 ** 32 + 1, 2 ** 53), and doubles next to objects.
  • Bigints revived inside the VM: ±2n ** 70n, 2n ** 63n, -(2n ** 63n) - 1n, and a 30-digit value, from reference-codec bytes and from the host serde's own round trip, plus a boxed Object(2n ** 70n) through the BigInt reviver.
  • The fallback: a capture root with BigInt removed, standing in for an older snapshot.

The double and bigint cases (4 tests) fail on main and pass with the fix. All 76 serde tests pass.

I also ran the issue's repro end to end on world-local with a throwaway test (not committed). The workflow takes [[0.1, 0.2], [31260, 8104.4, 16056, 30925.6, 4400], [2n ** 70n, -(2n ** 70n)]], passes it through a step, and returns both:

Engine main This PR
quickjs [0.1, 0.1], 8104.4 twice, [0n, 0n] matches the input
node matches the input matches the input

The full core suite passed: 4976 tests. The one failure, world-local-coalescing ("expected 13 to be less than 13"), is a timing check that passes on its own, with and without this change. tsc --noEmit passes. Biome reports the same 18 warnings as main for these files.

PR Checklist - Required to merge

  • 📦 pnpm changeset was run to create a changelog for this PR
  • 🔒 DCO sign-off passes (run git commit --signoff on your commits)
  • 📝 Ping @vercel/workflow in a comment once the PR is ready, and the above checklist is complete

quickjs-wasi 3.7.0 gives doubles a non-zero identity (their low 32 bits),
so identify() sent them down the object path and devalue deduplicated
distinct doubles with equal low words into back-references: [0.1, 0.2]
came back as [0.1, 0.1]. Treat every number handle as a primitive.

vm.newBigInt wraps values outside int64, so a revived 2n ** 70n became
0n. Build those values with the guest's BigInt, captured at boot with
the other intrinsics, from their decimal string. A capture root from a
snapshot taken before this capture falls back to a bigint literal.

Upstream: vercel-labs/quickjs-wasi#68

Fixes vercel#5774

Signed-off-by: Ultranmus <[email protected]>
@Ultranmus
Ultranmus requested a review from a team as a code owner November 30, 2026 17:02
@changeset-bot

changeset-bot Bot commented Nov 30, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 67443c6

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 18 packages
Name Type
@workflow/core Patch
@workflow/builders Patch
@workflow/cli Patch
@workflow/next Patch
@workflow/nitro Patch
@workflow/vitest Patch
@workflow/web-shared Patch
@workflow/web Patch
workflow Patch
@workflow/world-cloudflare Patch
@workflow/world-testing Patch
@workflow/astro Patch
@workflow/nest Patch
@workflow/rollup Patch
@workflow/sveltekit Patch
@workflow/vite Patch
@workflow/nuxt Patch
@workflow/world-vercel Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel

vercel Bot commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

@Ultranmus is attempting to deploy a commit to the Vercel Labs Team on Vercel.

A member of the Team first needs to authorize it.

@Ultranmus

Copy link
Copy Markdown
Contributor Author

@vercel/workflow ready for review.

@TooTallNate TooTallNate mentioned this pull request Nov 30, 2026
2 of 3 tasks
@TooTallNate

Copy link
Copy Markdown
Member

Thanks @Ultranmus, this was a great find, and the root-cause write-up made the upstream fix straightforward: vercel-labs/quickjs-wasi#68 is fixed in quickjs-wasi 3.7.1 (identity is 0 for numbers, and newBigInt/toBigInt are exact at any size).

I opened #5784, which bumps to 3.7.1 instead of working around it in the serde. It keeps your tests (minus the legacy capture-root case, since that fallback is no longer needed) and credits you as co-author. I'd suggest closing this PR in favor of that one; let us know if you see anything we missed.

TooTallNate added a commit that referenced this pull request Dec 1, 2026
### What

Bumps `quickjs-wasi` from 3.7.0 to 3.7.1. With `WORKFLOW_VM=quickjs`,
distinct doubles no longer collapse into one another and bigints outside
int64 stay exact at workflow and step boundaries. Fixes #5774 and
supersedes #5775.

### Why

#5775 by @Ultranmus traced #5774 to two quickjs-wasi bugs
([vercel-labs/quickjs-wasi#68](vercel-labs/quickjs-wasi#68))
and worked around them in the serde. Both are now fixed upstream in
[3.7.1](vercel-labs/quickjs-wasi#69), so this PR
takes the dependency bump instead of the workarounds and keeps their
tests.

- **Doubles.** A double's handle reported a non-zero `identity` (the low
32 bits of the double), so devalue deduplicated doubles that shared
those bits into back-references. `[0.1, 0.2]` came back as `[0.1, 0.1]`.
In 3.7.1, `identity` is 0 for every number.
- **Bigints.** `vm.newBigInt()` wrapped values outside int64, so `2n **
70n` was revived as `0n`. In 3.7.1, `newBigInt()` and
`handle.toBigInt()` are exact at any size.

#### Changes

- `quickjs-wasi` 3.7.1 in `@workflow/core` and
`@workflow/world-cloudflare`, plus the lockfile.
- The serde reads bigints with `handle.toBigInt()`. It previously called
the captured `BigInt.prototype.toString` only because `toBigInt()`
truncated, so that capture is gone.
- No changes are needed in `identify` or at the `newBigInt()` call
sites.

Snapshots taken under 3.7.0 are refused on restore (their
`engineVersion` doesn't match) and replay from the event log, as with
every engine bump. That means no restored VM can carry a capture root
from before this change.

### How did you test your changes?

The tests are adapted from #5775 into `quickjs-serde.test.ts`:

- **Wire parity with the node reference codec:** `[0.1, 0.2]`, `[31260,
8104.4, 16056, 30925.6, 4400]` (the example from #5774), integers held
as doubles, and doubles next to objects.
- **Bigints revived inside the VM:** `±2n ** 70n`, `2n ** 63n`, `-(2n **
63n) - 1n` and a 30-digit value. They're checked from reference-codec
bytes and from the host serde's own round trip, plus a boxed `Object(2n
** 70n)`.
- I dropped #5775's legacy capture-root test, because the fallback it
covered isn't needed.

Results:

| Check | Result |
|---|---|
| `quickjs-serde.test.ts` on 3.7.1 | 75 passed |
| Same file with `quickjs-wasi` swapped back to 3.7.0 | 4 failed: the 3
new double/bigint cases, plus the existing `bigint` parity case, which
now relies on exact `toBigInt()` |
| `pnpm test` in `packages/core` | 262 files passed, 2 skipped; 5068
tests passed, 6 expected fail, 55 skipped |
| `quickjs-snapshot-fuzz.test.ts` with `FUZZ_TRIALS=20` | passed |
| `tsc --noEmit` (core) | clean |
| Biome on the changed files | same 18 warnings as `main` |
| `node scripts/check-changesets.mjs` | passed |

### PR Checklist - Required to merge

- [x] 📦 `pnpm changeset` was run to create a changelog for this PR
- [x] 🔒 DCO sign-off passes (run `git commit --signoff` on your commits)
- [ ] 📝 Ping `@vercel/workflow` in a comment once the PR is ready, and
the above checklist is complete

_Written by fx_

Signed-off-by: Nathan Rajlich <[email protected]>
Co-authored-by: Ultranmus <[email protected]>
@Ultranmus

Copy link
Copy Markdown
Contributor Author

Thanks, glad the write-up helped! Closing in favor of #5784.

@Ultranmus Ultranmus closed this Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants