Skip to content

fix(fallback): rank working transports first when every latency is 0 ms - #5595

Merged
jxom merged 2 commits into
mainfrom
fix/fallback-rank-zero-latency
Nov 18, 2026
Merged

jxom merged 2 commits into
mainfrom
fix/fallback-rank-zero-latency

Conversation

@jxom

@jxom jxom commented Nov 18, 2026

Copy link
Copy Markdown
Member

Fixes #5569

With fallback({ rank }), when every sampled latency is 0 ms (in-process or very fast local transports, since Date.now() has 1 ms steps), latencyScore = 1 - meanLatency / maxLatency computed 0 / 0 = NaN. Each working transport's score became NaN, the sort comparator returned NaN, and the transports kept their input order, so a transport failing every ping could stay first.

When maxLatency is 0, every transport is equally fast, so the latency score is now 1. The stabilityScore === 0 early return is unchanged.

The regression test in fallback.test.ts ranks two in-process custom transports (one throws, one answers) with sampleCount: 1. After the cold first round each ping measures 0 ms; on main those rounds rank the failing transport first, and the test fails (5/5 local runs).

When every sampled latency was 0 ms, the latency score divided 0 by 0 and
became NaN, so the sort left a failing transport first.

Fixes #5569

Amp-Thread-ID: https://ampcode.com/threads/T-01a1f0ac-59df-7218-a2c9-78d717929b6f
Co-authored-by: jxom <[email protected]>
@vercel

vercel Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
viem Error Error Nov 18, 2026 10:20pm UTC

Request Review

@changeset-bot

changeset-bot Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 2f2e50c

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

This PR includes changesets to release 1 package
Name Type
viem 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

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-18T22:18:55.792028Z 2f2e50c New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 370fa21061

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/clients/transports/fallback.test.ts Outdated
})

// After the first round, each in-process ping measures 0 ms.
await vi.waitFor(() => expect(rankedTransports.length).toBeGreaterThan(5))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Replace the prohibited vi helper in the regression test

The new regression test calls vi.waitFor, but the root AGENTS.md explicitly prohibits using vi in tests, including additions to existing files that already use it. Await a promise resolved by onTransports after the required number of observations instead, preserving synchronization without introducing another prohibited helper call.

AGENTS.md reference: AGENTS.md:L155-L158

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced vi.waitFor with a promise that onTransports resolves after five rankings in 2f2e50c. The test still fails 5/5 against main's fallback.ts and passes with the fix.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2f2e50c30b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +1192 to +1196
const down = custom(
{
async request() {
throw new Error('node down')
},

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Replace the stubbed providers in the regression test

The new regression test supplies two stubbed providers: one always throws and the other always returns true. Wrapping these canned implementations in custom() does not remove the stubbing. Root AGENTS.md explicitly prohibits stubs and requires tests to exercise the configured test chain or real ephemeral servers. Rework the regression coverage to use an allowed test setup rather than fake provider responses.

AGENTS.md reference: AGENTS.md:L155-L158

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keeping this as is. The bug only happens when every sampled latency is exactly 0 ms, which in practice means in-process providers (the "fast local or in-process transport" case in #5569). An ephemeral HTTP server or the Anvil chain adds enough latency to cross a Date.now() millisecond boundary, so the NaN path is not reached and the test would pass on main. custom is the real transport for in-process EIP-1193 providers, and the test runs it and rankTransports without vi, fake fetch, or fake clients, like other inline custom({ async request() {} }) providers in the suite (e.g. showCallsStatus.test.ts, createAccessList.test.ts, simulateCalls.test.ts). Happy to rework it if a maintainer prefers a different setup.

@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Published [email protected] under the pr-5595 dist-tag, which follows this pull request until it closes.

pnpm add viem@pr-5595
pnpm add [email protected]

@jxom
jxom merged commit 0f73831 into main Nov 18, 2026
39 of 40 checks passed
@jxom
jxom deleted the fix/fallback-rank-zero-latency branch November 18, 2026 22:44
@github-actions github-actions Bot mentioned this pull request Nov 18, 2026

This branch had an error being deployed

1 failed deployment
Preview — 2f2e50c3 Deployed Nov 18, 2026 by vercel[bot]
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.

fallback: rank gives the score NaN when each latency is 0 ms, so a failing transport stays first

2 participants