Files
craft-ai-integrations/tools
Jeremy 32c6553c31 feat: retry provider API calls where it is safe (#170)
* feat: retry provider API calls where it is safe

One retry rule, craft_ai_core::Backoff, now serves media copies, job
sending and polling, instead of a loop private to ai-media.

- AiClient::start tries a rate-limited send again. A dropped send or a
  server error while sending is never repeated: the job may already be
  running, and a second send could charge twice.
- Job::wait no longer ends on the first failed poll. A poll that fails
  for a reason that can pass (dropped connection, rate limit) pauses and
  tries again; five failures in a row, or a minute, ends the wait. A
  success resets the run, and the overall timeout still wins.
- AiClient::with_retries sets both rules; Backoff::NEVER turns either off.
- Job::wait moves to client_wait.rs, so client.rs shrinks.

* fix: tighten poll retries and retry refused worker sends

Review fixes for the retry change.

- Job::wait counts the retry budget from the start of the first failed
  poll, keeps the whole pause (interval included) inside it, and checks
  the timeout after a pause, before another poll starts.
- Provider::submit now states the promise the client's send retry
  relies on: RateLimited from submit means nothing was accepted.
- Gemini and OpenAI send from a worker after submit returns, so a 429
  there failed the job. Each worker now retries its one refused send
  (GeminiConfig/OpenAiConfig::send_retry, default Backoff::STANDARD),
  stops once the job is cancelled, and never repeats any other failure.

* fix: a cancel during a retry pause stops the next send

Second round of review fixes.

- Backoff::run asks its retry test again after each pause, so a job
  cancelled during the pause is not sent again (Gemini and OpenAI).
- Job::wait checks the timeout before every poll, the first included,
  and a run of failed polls that ends past the timeout cancels the job
  and records the give-up.
- Provider::submit's promise is worded to what adapters can keep: no
  job was created, so nothing is billed for the work; files uploaded on
  the way (ArtCraft's input pictures) may stay.
- AiClient::with_retries passes its send rule to the factory's own
  Gemini and OpenAI builders (ProviderFactory::set_send_retry), so
  Backoff::NEVER turns their worker retry off too. A builder an app
  registered itself is left alone.

* test(mutation): re-aim replaced-adapter-keeps-the-old-limits

The factory's register now also clears the standard sender list between
the two lines this case matched, so it had no target.

* fix: refuse caller-cleared job limits as Quota, not RateLimited

The client now sends again after a rate limit from submit. Gemini's full
results registry and ComfyUI's eight-unfinished-jobs limit clear only when
the app polls or collects, so a pause on that same thread freed nothing:
start() stalled about three seconds, then failed with "(after 3 tries)".

Both now refuse as Quota, the provider contract says which kind a local
limit takes, and the history label for Quota reads "out of credit or over
a limit" so a full queue is not reported as no credit. Also moves
run_fill's doc comment back above run_fill.

* chore(mutation): drop the duplicate FACTORY path

* fix: retry budgets count pauses, and a failed wait cancels its job

Review fixes:
- Backoff's budget counts the pauses only. A slow call that fails (a timeout)
  no longer uses it up, so the failure retries exist for can be retried.
- Job::wait: the poll interval is the floor of a retry pause but does not
  count against the budget, so a long interval still retries a dropped poll.
  A wait that gives up because polls kept failing now cancels the job and
  records the give-up, as the timeout already did.
- with_factory applies a send rule chosen with with_retries, so the order of
  the two calls no longer matters.
- start and start_test say they can block during a rate-limit pause.
- MediaStoreConfig holds one `retry: Backoff` in place of three fields.
- The standard sender list is built in place, not as a const array.
2026-11-10 17:08:29 -05:00
..