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>
107 lines
3.7 KiB
PHP
107 lines
3.7 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\RememberKnowledge;
|
|
use Core\Mod\Agentic\Mcp\Tools\Agent\AgentTool;
|
|
use Core\Mod\Agentic\Models\BrainMemory;
|
|
|
|
/**
|
|
* Store a memory in the shared OpenBrain knowledge store.
|
|
*
|
|
* Agents use this tool to persist decisions, observations, conventions,
|
|
* and other knowledge so that other agents can recall it later.
|
|
*/
|
|
class BrainRemember extends AgentTool
|
|
{
|
|
protected string $category = 'brain';
|
|
|
|
protected array $scopes = ['write'];
|
|
|
|
public function dependencies(): array
|
|
{
|
|
return [
|
|
ToolDependency::contextExists('workspace_id', 'Workspace context required to store memories'),
|
|
];
|
|
}
|
|
|
|
public function name(): string
|
|
{
|
|
return 'brain_remember';
|
|
}
|
|
|
|
public function description(): string
|
|
{
|
|
return 'Store a memory in the shared OpenBrain knowledge store. Use this to persist decisions, observations, conventions, research, plans, bugs, or architecture knowledge for other agents.';
|
|
}
|
|
|
|
public function inputSchema(): array
|
|
{
|
|
return [
|
|
'type' => 'object',
|
|
'properties' => [
|
|
'content' => [
|
|
'type' => 'string',
|
|
'description' => 'The knowledge to remember (max 50,000 characters)',
|
|
'maxLength' => 50000,
|
|
],
|
|
'type' => [
|
|
'type' => 'string',
|
|
'description' => 'Memory type classification',
|
|
'enum' => BrainMemory::VALID_TYPES,
|
|
],
|
|
'tags' => [
|
|
'type' => 'array',
|
|
'items' => ['type' => 'string'],
|
|
'description' => 'Optional tags for categorisation',
|
|
],
|
|
'org' => [
|
|
'type' => 'string',
|
|
'description' => 'Optional organisation scope',
|
|
],
|
|
'project' => [
|
|
'type' => 'string',
|
|
'description' => 'Optional project scope (e.g. repo name)',
|
|
],
|
|
'confidence' => [
|
|
'type' => 'number',
|
|
'description' => 'Confidence level from 0.0 to 1.0 (default: 0.8)',
|
|
'minimum' => 0.0,
|
|
'maximum' => 1.0,
|
|
],
|
|
'supersedes' => [
|
|
'type' => 'string',
|
|
'format' => 'uuid',
|
|
'description' => 'UUID of an older memory this one replaces',
|
|
],
|
|
'expires_in' => [
|
|
'type' => 'integer',
|
|
'description' => 'Hours until this memory expires (null = never)',
|
|
'minimum' => 1,
|
|
],
|
|
],
|
|
'required' => ['content', 'type'],
|
|
];
|
|
}
|
|
|
|
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');
|
|
}
|
|
|
|
$agentId = $context['agent_id'] ?? $context['session_id'] ?? 'anonymous';
|
|
|
|
return $this->withCircuitBreaker('brain', function () use ($args, $workspaceId, $agentId) {
|
|
$memory = RememberKnowledge::run($args, (int) $workspaceId, $agentId);
|
|
|
|
return $this->success([
|
|
'memory' => $memory->toMcpContext(),
|
|
]);
|
|
}, fn () => $this->error('Brain service temporarily unavailable. Memory could not be stored.', 'service_unavailable'));
|
|
}
|
|
}
|