agent/php/tests/Feature/Brain/ReindexFlagsTest.php

138 lines
4.4 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\Mod\Agentic\Console\Commands\BrainReindexCommand;
use Core\Mod\Agentic\Jobs\EmbedMemory;
use Core\Mod\Agentic\Models\BrainMemory;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Queue\CallQueuedClosure;
use Illuminate\Support\Facades\Queue;
beforeEach(function (): void {
$this->app->make(Kernel::class)->registerCommand(new BrainReindexCommand());
});
function reindexFlagsMemory(array $attributes = []): BrainMemory
{
return BrainMemory::create(array_merge([
'workspace_id' => $attributes['workspace_id'] ?? createWorkspace()->id,
'agent_id' => 'virgil',
'type' => 'observation',
'content' => 'Brain reindex flags test memory.',
'confidence' => 0.83,
'org' => 'core',
'project' => 'agent',
], $attributes));
}
test('BrainReindexCommand_handle_Good_filters_by_org_and_project', function (): void {
Queue::fake();
$workspace = createWorkspace();
$matching = reindexFlagsMemory([
'workspace_id' => $workspace->id,
'org' => 'core',
'project' => 'agent',
]);
reindexFlagsMemory([
'workspace_id' => $workspace->id,
'org' => 'core',
'project' => 'other-project',
]);
reindexFlagsMemory([
'workspace_id' => $workspace->id,
'org' => 'other-org',
'project' => 'agent',
]);
$this->artisan('brain:reindex', [
'--org' => 'core',
'--project' => 'agent',
'--chunk' => 1,
])
->expectsOutputToContain('Dispatched 1 brain memory embedding job(s) for unindexed memories.')
->assertSuccessful();
Queue::assertPushed(EmbedMemory::class, 1);
Queue::assertPushed(EmbedMemory::class, fn (EmbedMemory $job): bool => $job->memoryId === $matching->id);
});
test('BrainReindexCommand_handle_Bad_filters_stale_memories', function (): void {
Queue::fake();
$workspace = createWorkspace();
$neverIndexed = reindexFlagsMemory([
'workspace_id' => $workspace->id,
'content' => 'Never indexed memory.',
'indexed_at' => null,
]);
$stale = reindexFlagsMemory([
'workspace_id' => $workspace->id,
'content' => 'Stale indexed memory.',
'indexed_at' => now()->subDays(21),
]);
$recent = reindexFlagsMemory([
'workspace_id' => $workspace->id,
'content' => 'Recently indexed memory.',
'indexed_at' => now()->subDays(3),
]);
$this->artisan('brain:reindex', [
'--stale' => true,
'--chunk' => 1,
])
->expectsOutputToContain('Dispatched 2 brain memory embedding job(s) for stale memories.')
->assertSuccessful();
Queue::assertPushed(EmbedMemory::class, 2);
Queue::assertPushed(EmbedMemory::class, fn (EmbedMemory $job): bool => $job->memoryId === $neverIndexed->id);
Queue::assertPushed(EmbedMemory::class, fn (EmbedMemory $job): bool => $job->memoryId === $stale->id);
Queue::assertNotPushed(EmbedMemory::class, fn (EmbedMemory $job): bool => $job->memoryId === $recent->id);
});
test('BrainReindexCommand_handle_Ugly_dry_run_counts_matches_without_queueing_jobs', function (): void {
Queue::fake();
$workspace = createWorkspace();
reindexFlagsMemory([
'workspace_id' => $workspace->id,
'org' => 'core',
]);
reindexFlagsMemory([
'workspace_id' => $workspace->id,
'org' => 'other-org',
]);
$this->artisan('brain:reindex', [
'--org' => 'core',
'--dry-run' => true,
'--chunk' => 1,
])
->expectsOutputToContain('DRY RUN: 1 brain memory record(s) match unindexed reindex filters.')
->assertSuccessful();
Queue::assertNothingPushed();
});
test('BrainReindexCommand_handle_Good_elastic_only_dispatches_lighter_reindex_jobs', function (): void {
Queue::fake();
$workspace = createWorkspace();
reindexFlagsMemory([
'workspace_id' => $workspace->id,
'indexed_at' => now()->subDays(30),
'org' => 'core',
]);
$this->artisan('brain:reindex', [
'--all' => true,
'--org' => 'core',
'--elastic-only' => true,
'--chunk' => 1,
])
->expectsOutputToContain('Dispatched 1 brain memory elastic-only reindex job(s) for all memories.')
->assertSuccessful();
Queue::assertNotPushed(EmbedMemory::class);
Queue::assertPushed(CallQueuedClosure::class, 1);
});