Repository navigation
fix(fallback): rank working transports first when every latency is 0 ms - #5595
Conversation
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]>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
🦋 Changeset detectedLatest commit: 2f2e50c The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 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".
| }) | ||
|
|
||
| // After the first round, each in-process ping measures 0 ms. | ||
| await vi.waitFor(() => expect(rankedTransports.length).toBeGreaterThan(5)) |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
Co-authored-by: jxom <[email protected]>
There was a problem hiding this comment.
💡 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".
| const down = custom( | ||
| { | ||
| async request() { | ||
| throw new Error('node down') | ||
| }, |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
|
Published pnpm add viem@pr-5595
pnpm add [email protected] |
Fixes #5569
With
fallback({ rank }), when every sampled latency is 0 ms (in-process or very fast local transports, sinceDate.now()has 1 ms steps),latencyScore = 1 - meanLatency / maxLatencycomputed0 / 0 = NaN. Each working transport's score becameNaN, the sort comparator returnedNaN, and the transports kept their input order, so a transport failing every ping could stay first.When
maxLatencyis 0, every transport is equally fast, so the latency score is now 1. ThestabilityScore === 0early return is unchanged.The regression test in
fallback.test.tsranks two in-processcustomtransports (one throws, one answers) withsampleCount: 1. After the cold first round each ping measures 0 ms; onmainthose rounds rank the failing transport first, and the test fails (5/5 local runs).