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>
123 lines
4.4 KiB
PHP
123 lines
4.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Core\Mod\Agentic\Mcp\Tools\Agent\Brain;
|
|
|
|
use Core\Mcp\Dependencies\ToolDependency;
|
|
use Core\Mod\Agentic\Actions\Brain\RecallKnowledge;
|
|
use Core\Mod\Agentic\Mcp\Tools\Agent\AgentTool;
|
|
use Core\Mod\Agentic\Models\BrainMemory;
|
|
|
|
/**
|
|
* Semantic search across the shared OpenBrain knowledge store.
|
|
*
|
|
* Uses vector similarity to find memories relevant to a natural
|
|
* language query, with optional filtering by project, type, agent,
|
|
* or minimum confidence.
|
|
*/
|
|
class BrainRecall extends AgentTool
|
|
{
|
|
protected string $category = 'brain';
|
|
|
|
protected array $scopes = ['read'];
|
|
|
|
public function dependencies(): array
|
|
{
|
|
return [
|
|
ToolDependency::contextExists('workspace_id', 'Workspace context required to recall memories'),
|
|
];
|
|
}
|
|
|
|
public function name(): string
|
|
{
|
|
return 'brain_recall';
|
|
}
|
|
|
|
public function description(): string
|
|
{
|
|
return 'Semantic search across the shared OpenBrain knowledge store. Returns memories ranked by similarity to your query, with optional filtering.';
|
|
}
|
|
|
|
public function inputSchema(): array
|
|
{
|
|
return [
|
|
'type' => 'object',
|
|
'properties' => [
|
|
'query' => [
|
|
'type' => 'string',
|
|
'description' => 'Natural language search query (max 2,000 characters)',
|
|
'maxLength' => 2000,
|
|
],
|
|
'top_k' => [
|
|
'type' => 'integer',
|
|
'description' => 'Number of results to return (default: 5, max: 20)',
|
|
'minimum' => 1,
|
|
'maximum' => 20,
|
|
'default' => 5,
|
|
],
|
|
'filter' => [
|
|
'type' => 'object',
|
|
'description' => 'Optional filters to narrow results',
|
|
'properties' => [
|
|
'org' => [
|
|
'type' => 'string',
|
|
'description' => 'Filter by organisation scope',
|
|
],
|
|
'project' => [
|
|
'type' => 'string',
|
|
'description' => 'Filter by project scope',
|
|
],
|
|
'type' => [
|
|
'oneOf' => [
|
|
['type' => 'string', 'enum' => BrainMemory::VALID_TYPES],
|
|
[
|
|
'type' => 'array',
|
|
'items' => ['type' => 'string', 'enum' => BrainMemory::VALID_TYPES],
|
|
],
|
|
],
|
|
'description' => 'Filter by memory type (single or array)',
|
|
],
|
|
'agent_id' => [
|
|
'type' => 'string',
|
|
'description' => 'Filter by originating agent',
|
|
],
|
|
'min_confidence' => [
|
|
'type' => 'number',
|
|
'description' => 'Minimum confidence threshold (0.0-1.0)',
|
|
'minimum' => 0.0,
|
|
'maximum' => 1.0,
|
|
],
|
|
],
|
|
],
|
|
],
|
|
'required' => ['query'],
|
|
];
|
|
}
|
|
|
|
public function handle(array $args, array $context = []): array
|
|
{
|
|
$workspaceId = $context['workspace_id'] ?? null;
|
|
if ($workspaceId === null) {
|
|
return $this->error('workspace_id is required. Ensure you have authenticated with a valid API key. See: https://host.uk.com/ai');
|
|
}
|
|
|
|
$query = $args['query'] ?? '';
|
|
$topK = $this->optionalInt($args, 'top_k', 5, 1, 20);
|
|
$filter = $this->optional($args, 'filter', []);
|
|
|
|
if (! is_array($filter)) {
|
|
return $this->error('filter must be an object');
|
|
}
|
|
|
|
return $this->withCircuitBreaker('brain', function () use ($query, $workspaceId, $filter, $topK) {
|
|
$result = RecallKnowledge::run($query, (int) $workspaceId, $filter, $topK);
|
|
|
|
return $this->success([
|
|
'count' => $result['count'],
|
|
'memories' => $result['memories'],
|
|
'scores' => $result['scores'],
|
|
]);
|
|
}, fn () => $this->error('Brain service temporarily unavailable. Recall failed.', 'service_unavailable'));
|
|
}
|
|
}
|