worldmonitor icon indicating copy to clipboard operation
worldmonitor copied to clipboard

fix(global-tenders): spread SAM.gov request budget, stop retrying 429s (#5444)

Open rajanpanth opened this issue 4 months ago • 2 comments

Closes #5444

Problem

SAM.gov enforces a small per-key daily request quota (10/day for non-federal keys). The hourly seed fetched SAM on every tick and retried 429s in-run (up to 3 attempts) — roughly 72 requests/day against a 10/day budget. Once the quota trips, every subsequent run 429s: the source pins at stale, its age climbs past the 180-minute ceiling (seedAgeMin 355 > 180, health SEED_ERROR), and US-scoped tender queries return empty with availability partial — as observed across two pulls ~16h apart in the issue.

Fix

Budget spread, per the direction suggested in the issue:

  1. Pace SAM fetches — skip the request while the previous success is fresher than SAM_MIN_FETCH_INTERVAL_MS (150 min ≈ 9.6 requests/day), carrying the prior records through with lastSuccessfulAt untouched (status gains paced: true), so staleness accounting stays honest and the age stays under the 180-min ceiling in steady state.
  2. No in-run retries on 429 — fetchResponse gains a retry429 opt-out used by the SAM adapter: quota-style limits do not clear in seconds, so retries only burn more budget. Other sources keep the existing retry contract untouched.
  3. Thread previousSnapshot to adapters — fetchGlobalTenders now passes it through so pacing can see the prior sam status; other adapters simply ignore it.

During an already-exhausted quota window the age can still exceed the ceiling until the quota resets — but the fix ends the death spiral, so the next quota window succeeds immediately and steady state stays healthy.

Testing

tests/global-tenders-sam-rate-limit.test.mjs:

  • pacing skip inside the interval (no request spent, prior records carried, real lastSuccessfulAt preserved)
  • interval expiry triggers a real fetch
  • first run without a snapshot is unchanged
  • a SAM 429 fails fast — exactly one request, no retry burn

All 28 existing tender tests (global-tenders, global-tender-sources, global-tenders-transient-retry, global-tender-health) pass unchanged.

rajanpanth avatar Jul 22 '26 16:07 rajanpanth

@rajanpanth is attempting to deploy a commit to the World Monitor Team on Vercel.

A member of the Team first needs to authorize it.

vercel[bot] avatar Jul 22 '26 16:07 vercel[bot]

Greptile Summary

This PR fixes a SAM.gov daily quota exhaustion bug (#5444) where the hourly seed was burning ~72 requests/day against a 10/day budget, causing the source to pin at HTTP 429 and return empty US tender results. The fix introduces two coordinated mechanisms: a 150-minute pacing guard that carries prior SAM records through without making a network request, and a retry429: false opt-out so a quota 429 fails fast instead of burning additional retry attempts.

  • Pacing logic (previousSamResult + SAM_MIN_FETCH_INTERVAL_MS): reads lastSuccessfulAt from the previous snapshot, skips the SAM API call while it is fresh enough, and re-filters carried records through isOpenOpportunity with the current now so expired tenders are not served from cache.
  • retry429 flag on fetchResponse: sets error.nonRetryable = true for HTTP 429 when the opt-out is active, causing withRetry to exit immediately; the default (retry429 = true) is unchanged for all other sources.
  • previousSnapshot threading: fetchGlobalTenders now forwards the snapshot to all adapters; non-SAM adapters ignore the extra parameter harmlessly.

Confidence Score: 4/5

Safe to merge. The two changes are tightly scoped — pacing only activates when a valid prior snapshot exists and its lastSuccessfulAt is within 150 minutes, so the first-run path and all non-SAM sources are completely unaffected.

The fix is mechanically sound and well-tested. When pacing fires, fetchedAt in the returned status carries the timestamp from the previous run rather than the current seeder tick — intentional and signalled by paced:true, but a subtle observable difference worth noting.

No files require special attention. seed-global-tenders.mjs is the only production change and its logic paths are fully exercised by the new tests.

Important Files Changed

Filename Overview
scripts/seed-global-tenders.mjs Adds SAM.gov pacing (150 min interval) via previousSamResult, threads previousSnapshot to all adapters, and opts SAM out of in-run 429 retries via a new retry429 flag in fetchResponse.
tests/global-tenders-sam-rate-limit.test.mjs New regression tests covering pacing skip, interval expiry, first-run unchanged, and 429 fast-fail; all four scenarios verified with precise assertions.

Reviews (1): Last reviewed commit: "fix(global-tenders): spread SAM.gov requ..." | Re-trigger Greptile

greptile-apps avatar Jul 22 '26 17:07 greptile-apps