Files
pricebuddy/tests/Feature/Services/AiServiceHealthTest.php
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

87 lines
2.8 KiB
PHP

<?php
namespace Tests\Feature\Services;
use App\Dto\AiProviderConfigDto;
use App\Enums\AiProvider;
use App\Exceptions\AiProviderException;
use App\Services\Ai\AiProviderHealth;
use App\Services\AiService;
use Illuminate\Contracts\JsonSchema\JsonSchema;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use Tests\TestCase;
/**
* AiService is where a provider call is known to have succeeded or failed, so it is where
* the circuit breaker is fed. Everything downstream only reads the resulting state.
*/
class AiServiceHealthTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
Cache::flush();
config()->set('price_buddy.ai_provider_breaker.failure_threshold', 1);
config()->set('price_buddy.ai_provider_breaker.cooldown_seconds', 300);
}
public function test_a_failed_agent_run_feeds_the_circuit_breaker(): void
{
Http::fake(['*' => Http::response('upstream is unwell', 500)]);
$provider = $this->provider();
$health = AiProviderHealth::new();
$this->assertFalse($health->isUnavailable($provider));
try {
AiService::new()->runAgent(
'Do the thing.',
fn (JsonSchema $schema): array => ['ok' => $schema->string()],
'Go.',
[],
$provider,
);
$this->fail('The agent run should have failed against a 500.');
} catch (AiProviderException) {
// Expected: the run failed, which is what the breaker counts.
}
$this->assertTrue($health->isUnavailable($provider), 'A failed run did not open the breaker.');
}
public function test_a_reachability_check_does_not_open_the_circuit_breaker(): void
{
Http::fake(['*' => Http::response('upstream is unwell', 500)]);
$provider = $this->provider();
$health = AiProviderHealth::new();
// The Ollama connection test lists models rather than generating, which proves the
// HTTP front end is up but not that generation works — the exact state the breaker
// exists for. It must therefore neither open nor close it.
$result = AiService::new()->testProviderConfig($provider);
$this->assertSame('Could not reach Ollama at http://ai.example:11434.', $result);
$this->assertFalse(
$health->isUnavailable($provider),
'A reachability check must not open the breaker; only a real generation counts.'
);
}
private function provider(): AiProviderConfigDto
{
return new AiProviderConfigDto(
id: 'p1',
name: 'Local',
type: AiProvider::Ollama,
model: 'm',
baseUrl: 'http://ai.example:11434',
timeoutSeconds: 5,
);
}
}