Files
Jeremy c0c8d0a0c8 feat: circuit breaker for repeatedly failing AI providers
Bounding the meta-extraction request stopped a wedged provider costing 120s, but
it still cost the full 25s budget on every single request. A provider that has
failed the last N calls will almost certainly fail the next one, and finding out
is the expensive part.

AiProviderHealth tracks consecutive failures per provider id in the cache. After
failure_threshold consecutive failures (default 3) the provider is treated as
unavailable for cooldown_seconds (default 300), which doubles as the counting
window so thinly spread failures never accumulate. Any success closes it
immediately.

AiService feeds it, since that is the only layer that knows whether a provider
call succeeded. The Ollama reachability check deliberately does not close the
breaker: listing models proves the HTTP front end is up, not that generation
works, which is exactly the state this instance is in.

Only callers on a deadline consult it. The meta-extraction endpoint skips healing
with the new reason `provider_unavailable`; the admin UI heal button and queued
healing keep exercising the provider, so they act as the probe that notices
recovery rather than waiting out the cooldown.

Verified live against the wedged provider: three requests at 24.1s each, then the
fourth returned in 0.105s with reason `provider_unavailable`.

Note for API clients: `provider_unavailable` is a new value in the `healing.reason`
set. It means the instance is degraded rather than the page being unextractable,
so the same URL is worth retrying later.
2026-08-08 13:23:04 +10:00

30 lines
986 B
PHP

<?php
namespace App\Enums;
/**
* Why AI healing did not improve a meta-extraction result. Reported to API clients so
* they can explain a worse-than-usual result instead of silently showing empty fields.
*/
enum HealingReason: string
{
/** Not requested, the store opted out, or no healing provider is configured. */
case Disabled = 'disabled';
/** The deterministic scrape already produced a usable result. */
case NotNeeded = 'not_needed';
/** The request budget was exhausted, or too little of it remained to start. */
case Timeout = 'timeout';
/**
* The AI provider has failed repeatedly and its circuit breaker is open, so healing
* was not attempted. Unlike the others this is about the instance, not the page:
* the same URL is worth retrying once the provider recovers.
*/
case ProviderUnavailable = 'provider_unavailable';
/** Healing ran but errored or produced no usable config. */
case Error = 'error';
}