Add shared HTTP retry wrapper and migrated wallet backend - #365
Merged
davedumto merged 1 commit intoAug 31, 2026
Merged
Conversation
Issue Vellar-Wallet#301: retry handling is duplicated across HTTP call sites instead of living centrally. src/http-backend.ts repeats the same request-and-check block three times, and src/policy-client.ts repeats the shape again with its own error class. Neither retries, so a dropped connection surfaces as a failed wallet creation. Adds contrib/examples/issue-301-http-retry-wrapper with the wrapper that centralization needs, written against the real call sites so it can be dropped in without changing any public signature. The substance is which requests must NOT be retried. /wallet/submit sends a signed transaction and /wallet/create deploys a wallet; if either response is lost, the work may already have happened, so retrying can double-submit. Only failures proving the server reached no decision are retried: transport errors and 408/429/502/503/504. A 500 is deliberately terminal, since the handler ran and may have completed the side effect. This matches the existing reasoning in policy-types.ts and x402-guards.ts. Retrying is opt-in, so no call site inherits retries it did not request. Backoff is exponential with subtractive jitter applied after the cap, so a delay can never exceed maxDelayMs. sleep and random are injectable, letting the tests assert exact delay sequences without waiting. 50 tests cover retryable and non-retryable statuses, transport errors, abort handling, backoff sequences, and the three migrated call sites.
|
@Y33t-dev Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
|
@Y33t-dev is attempting to deploy a commit to the david's projects Team on Vercel. A member of the Team first needs to authorize it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
closes #301
Summary
HTTP retry handling is duplicated across call sites instead of living centrally.
src/http-backend.tsdefines one privatepost()helper and then repeats the same request-and-check block three times;src/policy-client.tsrepeats the shape again in its ownreq(), with its own error class. Neither retries at all, so a single dropped connection surfaces as a failed wallet creation.This adds
contrib/examples/issue-301-http-retry-wrapper: the shared wrapper that centralization needs, written against the real call sites so it can be dropped intocreateHttpWalletBackendwithout changing any public signature.The part that actually matters: what must not be retried
Retrying HTTP is not looping until something works. Two of the three wallet endpoints are unsafe to retry blindly, and getting this wrong costs real money:
POST /wallet/connectPOST /wallet/createPOST /wallet/submitSo the rule enforced is: retry only on positive evidence that the server reached no decision — a transport error, or 408/429/502/503/504.
Everything else is terminal, including 500. A 500 means the handler ran and failed partway, which is exactly the state where a retry can duplicate a write; a 503 means nothing was decided. That distinction is the whole safety argument, and it matches the reasoning already written down in
src/policy-types.ts(isRetryableStatus) andsrc/x402-guards.ts(isRetryableSettleFailure), so the SDK keeps one consistent story about when a retry is safe.Retrying is also opt-in (
retryabledefaults tofalse), so no call site inherits retries it never asked for. The safe default is the one that cannot double-spend.Design notes
WalletApiErrororPolicyApiError, which is what lets one wrapper serve both files despite their different error types.[exp * (1 - jitter), exp]and can never exceedmaxDelayMs. Jitter matters when a gateway comes back up: without it, every client that failed at the same moment retries at the same moment.sleepandrandomare injectable, so the tests assert exact delay sequences without waiting.retrying-wallet-backend.tsshows all three real call sites migrated, keeping the interface, error type and base-URL handling identical. The only change is that each call site declares its retryability and the retry lives in one place.Tests
50 tests covering retryable and non-retryable statuses, transport errors, abort handling, exact backoff sequences, and the three migrated call sites — including the assertions that
/wallet/submitand/wallet/createare attempted exactly once on a 503.Notes for the maintainer
Scoped to
contrib/per CONTRIBUTING.md, sosrc/http-backend.tsis not modified here. The migrated backend is a working, tested reference for that change rather than a patch; happy to apply it tosrc/directly if you widen the scope on the issue./wallet/createand/wallet/submitcould become retryable with an idempotency key echoed by the gateway, but that is a protocol change on both sides, so their non-retryability is currently fixed rather than configurable.The 18 test failures on
dev(incontrib/examples/andsrc/session.test.ts, which also failstsc) are pre-existing and untouched by this branch.