Skip to content

fix(trade): retry transient gateway failures on idempotent wait-polls#42

Merged
Zuhwa merged 1 commit into
mainfrom
fix/retry-transient-poll-5xx
Jun 12, 2026
Merged

fix(trade): retry transient gateway failures on idempotent wait-polls#42
Zuhwa merged 1 commit into
mainfrom
fix/retry-transient-poll-5xx

Conversation

@psmiratisu

@psmiratisu psmiratisu commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Bug

During a live Hyperliquid deposit on 2026-06-11, the settlement loop made ~40 /trade/next polls. One of them hit a transient 502 between Cloudflare and the origin (the request never reached the backend — confirmed in Railway logs), and the CLI threw fatally — reporting a deposit that had already completed on-chain as a failed trade, and leaving the server-side trade orphaned as active.

Fix

post() gains an opt-in retryTransient flag: 502/503/504 responses and network-level fetch failures back off (2s → 5s → 10s) and retry instead of aborting.

Scope is deliberately narrow: only pure wait-polls opt in ({tradeId, step} with no txHash/signature payload — idempotent by construction). Send-result and sign-result posts are never replayed, because the CLI can't know whether the server consumed the original before the connection died.

Companion server-side fix (settle HL deposits by the exchange ledger so the loop ends as soon as funds arrive): internal-trading-bot#18. Related: #41.

npm run typecheck passes.

🤖 Generated with Claude Code


Note

Medium Risk
Changes trade-loop HTTP behavior during long-running flows; scope is limited to idempotent wait polls, but incorrect retry on non-idempotent paths would be serious—those paths are explicitly excluded.

Overview
Fixes a case where a single transient 502/503/504 (or dropped connection) on /trade/next during settlement wait polling could fail the whole trade even when on-chain work had already finished.

post() now accepts an opt-in retryTransient flag that retries those gateway/network failures with 2s → 5s → 10s backoff (up to three delays). Only wait steps pass retryTransient: true—payloads are just {tradeId, step} and treated as idempotent. Send and sign follow-ups still use a single attempt so the CLI does not replay txHash/signature posts when the server may have already processed them.

Reviewed by Cursor Bugbot for commit 01aadc5. Bugbot is set up for automated code reviews on this repo. Configure here.

Observed live 2026-06-11: a deposit's settlement loop made ~40 /trade/next
polls; a single 502 between Cloudflare and the origin aborted the CLI and
reported an already-completed deposit as failed.

post() now takes an opt-in retryTransient flag: 502/503/504 responses and
network-level fetch failures back off (2s/5s/10s) and retry. Only the pure
wait-polls ({tradeId, step} — no txHash/signature payload) opt in; send and
sign result posts are never replayed because the CLI cannot know whether the
server consumed the original before the connection died.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@Zuhwa Zuhwa merged commit d1f6f68 into main Jun 12, 2026
1 check passed
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.

2 participants