Files
craft-ai-integrations/tools/mutation
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
..

Deliberate-bug checks

Each case breaks one safety rule in the source, runs the one test that should notice, and puts the source back. A case is caught only if the mutated code compiled and the test run failed. A mutant that does not compile counts as not caught, so every survivor is either a test gap (fix the test) or a bad mutant (fix the mutant). Cargo runs serially, targeted with -p and --locked, honouring CARGO_BUILD_JOBS from the build resource.

Script Groups
mutation_check.py u1 u3 u4 p4 p10 v1 v2 v3 sd vx lc lr nr ct u2 wf (connections, model sources, node packs, media, client flows, declarative adapter, workflow library). Cases for u2 are in mutation_u2.py.
mutation_check.py seg Shared selection contract: points, box, description and checked result metadata.
mutation_check.py rt rten model runner, bounded picture decoding, resampling and prompt assembly.
mutation_check.py rt rten engine picture handling: bounded PNG and JPEG decoding, mask encoding, resampling.
mutation_check.py bp Local Burn weight preparation (ai-burn-setup): source hashes, cache key, damaged saved weights, cancel.
mutation_check.py be Burn engine: saved-weights loading, the panic guard, answer checks, file names, its catalogue route, the .bpk binding rule and the shared architecture revision.
mutation_check.py bo A recipe only built-in runtimes run is never offered, planned, installed, registered or advised as a ComfyUI model.
mutation_check.py fr First-run setup flow: the licence tick only where needed, the one start button, the suggested folder, adopting checked files, the route-aware try-it sample, status and picker when a file has gone, route notes and engine advice. Cases in mutation_first_run.py.
mutation_check.py gs Generic selection: scratch sinks, the refusal of clicks and mapped selection workflows.
mutation_check.py gs Generic selection workflows: the scratch sinks, the refusal of clicks and mapped selection workflows.
mutation_check.py gu Generic selection uploads: one upload per picture, the bounded memory, the resend after a cleared scratch area or a failed job.
mutation_check.py gm Generic selection masks: size, one channel, the empty-mask answer and the picture header.
mutation_check.py s21 SAM 2.1 selection (sam21_session.rs, detector.rs): the Florence-2-large preflight, the detector name and the choice check.
mutation_check.py ca Upstream contract alignment: model-test address provenance, required inputs, and app-facing refusal before key access. Cases in mutation_contract.py.
mutation_check.py ps Node pack status report (pack/status.rs): links, hidden folders, names, the install record.
mutation_check.py vx Vertex express adapter (ai-vertex/src/lib.rs): the API key travels in the x-goog-api-key header, never in the request URL.
mutation_check.py lg Remembered licences (registry/licences.rs, install.rs): terms, versions, local files, damaged record.
mutation_check.py eg Connection panel lifecycle (ai-egui/src/panel.rs): the same-frame Save dedup and the stale-answer generation guard. Cases in mutation_egui.py; receipt and logs in verification/eg-mutations.json.
mutation_check.py kf Standard secret chain (secrets/standard.rs): a locked secret file is reported, not read as empty; an empty passphrase counts as none; when to ask for a passphrase. Cases in mutation_keys.py.
sam3_mutation_check.py sg1 sg2 sg2b sg2c sg3 (SAM 3 masks, sessions, slots, cancel).
python tools/mutation/mutation_check.py u2            # one group
python tools/mutation/mutation_check.py u2 stale      # cases whose name contains "stale"

The "Deliberate-bug checks" workflow runs a group on Ubuntu and macOS. It takes the group and an optional case-name filter, so one case can be checked on both systems in minutes instead of waiting for the group. A filter that names no case in the group fails instead of reporting success:

group:  p4
filter: input-folder-read

Group ids are historical labels. The header of each test file says what it covers.

Run them exclusively. They snapshot and restore source files, so do not edit, format or build those files while one runs. Afterwards read the restored source and rerun the focused tests.

Logs go to tools/mutation/verification/ (not committed) after passing through scrub.py, which replaces a Windows or Unix profile folder with %USERPROFILE% so no account name is written.

gi checks that Gemini retains unread terminal states and that a running poll does not mark a result delivered. It uses seeded job tracking and a closed loopback endpoint, with no live provider.