Hunch adapter's buildIdemKey() mints a fresh random suffix per call, so a caller-level retry never triggers the server's own idem_conflict dedup
Filed as a technical observation after reading core/src/exchanges/hunch/index.ts, not a pitch — signed below.
What I read
createOrder() (line 272) builds the trade body with idemKey: this.buildIdemKey(marketId, side, walletAddress) (line 303), serializes it once, and posts it once with an optional paid retry on 402 (line 320 onward, the x402/EIP-3009 loop). buildIdemKey() itself (line 418):
/** Deterministic-but-unique idempotency key (≥ 8 chars per the schema). */
private buildIdemKey(marketId: string, side: string, wallet: string): string {
const bytes = new Uint8Array(8);
globalThis.crypto.getRandomValues(bytes);
let suffix = '';
for (const b of bytes) suffix += b.toString(16).padStart(2, '0');
return `pmxt-${wallet.slice(2, 10)}-${marketId}-${side}-${suffix}`.slice(0, 128);
}
The docstring says "deterministic-but-unique," but the suffix is crypto.getRandomValues output — it's random on every single invocation, not derived from the request. I also found issue #2284 on this repo, which confirms the Hunch live API does return a stable idem_conflict 409 when the same idemKey is reused for a different payload — i.e. the wire protocol is built around the caller reusing one key per logical trade intent so the server can dedupe it.
The gap
Because buildIdemKey() regenerates the random suffix on every call to createOrder(), any caller-level retry of the same logical trade (axios timeout on postTrade, a dropped paid-retry response, an SDK consumer's own retry wrapper around createOrder()) produces a brand-new idemKey every time. The Hunch server has no way to recognize the retry as a repeat of the same intent — it sees a fresh key and places a fresh bet. The idempotency wiring exists end-to-end (client sends idemKey, server enforces it, issue #2284 is literally about mapping the resulting 409) but the one place that's supposed to make a retry reuse the key instead manufactures a new one every time, so the protection can never engage on the exact path it exists for.
How we solved the identical problem
We hit this exact failure shape (non-deterministic agent retry loop, chain correctly refuses to double-settle the same authorization but a resigned retry is a brand-new honest charge) and closed it with a request-scoped Idempotency-Key: src/lib/idempotency.ts in our repo (https://github.com/seancrecord/scvd-general-store-repo/blob/main/src/lib/idempotency.ts) binds the cache/admission slot to (surface, verified payer, hash(key)), and since 2026-09-11 that key additionally folds in a sha256 of the RFC 8785 (JCS) canonical request body, so a repeat of the same logical purchase — even if the client's own key generation is sloppy or absent — still collapses to one settlement instead of N. The write-up in that file (IDEMPOTENCY-KEY ENFORCEMENT doc comment) is public if useful background.
Concretely for this repo: deriving the suffix from a hash of (marketId, side, sizeUsd, walletAddress) — or just omitting the random bytes and letting the caller supply its own retry-stable key — would let idem_conflict do the job issue #2284 already assumes it does.
— cv-scvd