agent/php/tests/Feature/Brain/CircuitBreakerTest.php

83 lines
2.7 KiB
PHP
Raw Normal View History

fix(brain): close openbrain audit gaps — org scoping + index cleanup + reindex flags + MCP schemas + circuit layer Closes the 5 PARTIAL items flagged in docs/AUDIT-openbrain-20260424.md. - Gap A (org scoping persisted on writes): new migration adds `org` nullable+indexed column to brain_memories; BrainMemory fillable; RememberKnowledge action forwards org; BrainService::remember persists it. - Gap B (supersede/forget Elastic cleanup): BrainService::forget dispatches DeleteFromIndex (handles both Qdrant + Elastic); supersede path dispatches cleanup for the old memory id before replacing it. DeleteFromIndex itself untouched — already handled both indexes. - Gap C (brain:reindex flags): --org, --project, --stale (null OR >14d old), --dry-run (count+stop), --elastic-only added to the artisan command. - Gap D (MCP schemas expose org): brain_remember, brain_recall, brain_list now accept `org` in input schema + forward into action/service. - Gap E (resilience uneven): brain_list now wrapped in withCircuitBreaker('brain', ...) matching the pattern used by BrainRemember/Recall/Forget. BrainService gains retryableHttp() helper — 100/300/900ms exponential backoff, retries only on 5xx + connection errors, not on 4xx. Qdrant calls route through it; Ollama left alone (EmbedMemory job has its own retry). Tests (Good/Bad/Ugly per gap): - Feature/Brain/OrgScopingTest.php - Feature/Brain/SupersedeForgetIndexCleanupTest.php - Feature/Brain/ReindexFlagsTest.php - Feature/Mcp/BrainSchemaOrgTest.php - Feature/Brain/CircuitBreakerTest.php php -l clean on all 13 files. Pest binary not in this checkout — CI path validates the full suite. Closes tasks.lthn.sh/view.php?id=107 Co-authored-by: Codex <noreply@openai.com> Co-Authored-By: Virgil <virgil@lethean.io>
2026-04-24 08:14:06 +01:00
<?php
// SPDX-License-Identifier: EUPL-1.2
declare(strict_types=1);
use Core\Mcp\Services\CircuitBreaker;
use Core\Mod\Agentic\Mcp\Tools\Agent\Brain\BrainList;
use Core\Mod\Agentic\Services\BrainService;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;
function retryableBrainService(): BrainService
{
return new class extends BrainService
{
public array $sleepCalls = [];
protected function sleepMilliseconds(int $milliseconds): void
{
$this->sleepCalls[] = $milliseconds;
}
};
}
test('CircuitBreaker_brain_list_Good_routes_failures_through_with_circuit_breaker', function (): void {
$workspace = createWorkspace();
$breaker = Mockery::mock(CircuitBreaker::class);
$breaker->shouldReceive('call')
->once()
->with('brain', Mockery::type(Closure::class), Mockery::type(Closure::class))
->andReturnUsing(function (string $service, Closure $operation, ?Closure $fallback = null): array {
return $fallback instanceof Closure ? $fallback() : [];
});
$this->app->instance(CircuitBreaker::class, $breaker);
$result = (new BrainList)->handle([], [
'workspace_id' => $workspace->id,
]);
expect($result['code'])->toBe('service_unavailable')
->and($result['error'])->toBe('Brain service temporarily unavailable. Memory list unavailable.');
});
test('CircuitBreaker_retryable_http_Bad_retries_qdrant_requests_on_503', function (): void {
$brain = retryableBrainService();
Http::fake([
'http://localhost:6334/collections/openbrain/points' => Http::sequence()
->push(['error' => 'unavailable'], 503)
->push(['result' => ['status' => 'ok']], 200),
]);
$brain->qdrantUpsert([
['id' => 'memory-1', 'vector' => [0.1, 0.2], 'payload' => ['type' => 'fact']],
]);
expect($brain->sleepCalls)->toBe([100]);
Http::assertSentCount(2);
Http::assertSent(fn (Request $request): bool => $request->url() === 'http://localhost:6334/collections/openbrain/points'
&& $request->method() === 'PUT');
});
test('CircuitBreaker_retryable_http_Ugly_does_not_retry_qdrant_requests_on_401', function (): void {
$brain = retryableBrainService();
Http::fake([
'http://localhost:6334/collections/openbrain/points' => Http::sequence()
->push(['error' => 'unauthorised'], 401)
->push(['result' => ['status' => 'ok']], 200),
]);
expect(fn () => $brain->qdrantUpsert([
['id' => 'memory-2', 'vector' => [0.3, 0.4], 'payload' => ['type' => 'fact']],
]))->toThrow(RuntimeException::class, 'Qdrant upsert failed: 401');
expect($brain->sleepCalls)->toBe([]);
Http::assertSentCount(1);
});