* feat: introduce hindsight-api-slim and hindsight-all-slim packages Closes #552 - Move all source code from hindsight-api/ to new hindsight-api-slim/ - hindsight-api-slim has heavy ML deps (torch, sentence-transformers, transformers, einops, flashrank, mlx, mlx-lm, safetensors) and pg0-embedded as optional extras: [local-ml], [embedded-db], [all] - hindsight-api becomes a zero-code meta-package depending on hindsight-api-slim[all] for full backward compatibility - Add hindsight-all-slim meta-package: hindsight-api-slim + client + embed - hindsight-all updated to depend on hindsight-api-slim[all] - pg0.py: lazy-import pg0 with clear ImportError pointing to [embedded-db] - Dockerfile: replace sed hack with proper uv sync --extra flags - Update release.yml, test.yml, lint.sh, release.sh, CLAUDE.md and all path references throughout the repo * refactor: rename hindsight/ directory to hindsight-all/ * docs: document hindsight-api-slim and hindsight-all-slim package variants Add package variants table and extras explanation to installation.md * docs: remove emojis from installation.md, use professional tone * docs: link Docker slim variant to pip package variants section * docs: consolidate Docker image variants into single table * ci: fix working-directory paths after package restructure - Replace all hindsight-api → hindsight-api-slim in test.yml - Replace hindsight → hindsight-all in test.yml - Add --extra embedded-db to test-embed API install step * ci: add local-ml and embedded-db extras to API sync steps These extras were previously implicit in the old hindsight-api package (which bundled everything). Now that hindsight-api-slim uses optional extras, we must explicitly request local-ml and embedded-db in CI. * ci: add API install step with embedded-db to test-embed smoke test The smoke test starts hindsight-api as a daemon, which requires pg0-embedded. Add a dedicated install step for hindsight-api-slim with embedded-db extra so the daemon can start successfully. * ci: remove --no-install-project when using optional extras When --no-install-project is combined with --extra, the optional deps are not installed because extras require the project to be active. Remove --no-install-project from steps that need local-ml or embedded-db. * ci: fix ordering of uv sync steps to preserve optional extras When uv sync runs for a different workspace member, it removes optional extras installed for other members. Fix by always running extra-requiring API sync last, after other workspace member syncs. Also remove --no-install-project from embedded-db sync in test-embed, as --no-install-project prevents optional extras from being active. * ci: add local-ml extra to test-embed API install for smoke test The smoke test starts the full API server which needs sentence-transformers for local embeddings (default provider). Add local-ml extra to the install. * ci: simplify extras with --all-extras and add slim pip smoke test - Replace explicit --extra local-ml --extra embedded-db with --all-extras for cleaner, more maintainable sync steps - Add test-pip-slim job: tests hindsight-api-slim[embedded-db] without local ML models, using Cohere for embeddings/reranking (mirrors Docker slim smoke test approach) * ci: simplify slim smoke test to health check only (mirrors Docker test)
169 lines
6.1 KiB
Python
169 lines
6.1 KiB
Python
"""Tests for MCPExtension loading and tool registration."""
|
|
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import pytest
|
|
from fastmcp import FastMCP
|
|
|
|
from hindsight_api import MemoryEngine
|
|
from hindsight_api.extensions.mcp import MCPExtension
|
|
|
|
|
|
class MockMCPExtension(MCPExtension):
|
|
"""Test extension that registers a custom tool."""
|
|
|
|
def __init__(self, config=None):
|
|
super().__init__(config)
|
|
self.register_tools_called = False
|
|
self.registered_mcp = None
|
|
self.registered_memory = None
|
|
|
|
def register_tools(self, mcp: FastMCP, memory: MemoryEngine) -> None:
|
|
"""Register a test tool to verify extension was called."""
|
|
self.register_tools_called = True
|
|
self.registered_mcp = mcp
|
|
self.registered_memory = memory
|
|
|
|
@mcp.tool()
|
|
async def test_extension_tool(query: str) -> str:
|
|
"""A test tool registered by the extension."""
|
|
return f"Extension tool received: {query}"
|
|
|
|
|
|
class TestMCPExtensionBase:
|
|
"""Tests for MCPExtension base class."""
|
|
|
|
def test_mcp_extension_is_abstract(self):
|
|
"""MCPExtension.register_tools is abstract and must be implemented."""
|
|
with pytest.raises(TypeError, match="abstract method"):
|
|
MCPExtension()
|
|
|
|
def test_subclass_can_be_instantiated(self):
|
|
"""Subclass implementing register_tools can be instantiated."""
|
|
ext = MockMCPExtension()
|
|
assert ext is not None
|
|
assert ext.register_tools_called is False
|
|
|
|
def test_register_tools_receives_mcp_and_memory(self):
|
|
"""register_tools receives FastMCP and MemoryEngine instances."""
|
|
ext = MockMCPExtension()
|
|
mcp = FastMCP("test")
|
|
memory = MagicMock(spec=MemoryEngine)
|
|
|
|
ext.register_tools(mcp, memory)
|
|
|
|
assert ext.register_tools_called is True
|
|
assert ext.registered_mcp is mcp
|
|
assert ext.registered_memory is memory
|
|
|
|
|
|
class TestMCPExtensionLoading:
|
|
"""Tests for MCPExtension loading in create_mcp_server."""
|
|
|
|
@pytest.fixture
|
|
def mock_memory(self):
|
|
"""Create a mock MemoryEngine."""
|
|
memory = MagicMock()
|
|
memory._tenant_extension = MagicMock()
|
|
memory._tenant_extension.authenticate_mcp = MagicMock()
|
|
return memory
|
|
|
|
def test_create_mcp_server_without_extension(self, mock_memory):
|
|
"""create_mcp_server works without MCPExtension configured."""
|
|
from hindsight_api.api.mcp import create_mcp_server
|
|
|
|
with patch("hindsight_api.api.mcp.load_extension", return_value=None):
|
|
mcp = create_mcp_server(mock_memory)
|
|
|
|
# Core tools should be registered
|
|
tools = mcp._tool_manager._tools
|
|
assert "retain" in tools
|
|
assert "recall" in tools
|
|
assert "reflect" in tools
|
|
# Extension tool should NOT be present
|
|
assert "test_extension_tool" not in tools
|
|
|
|
def test_create_mcp_server_with_extension(self, mock_memory):
|
|
"""create_mcp_server loads and calls MCPExtension when configured."""
|
|
from hindsight_api.api.mcp import create_mcp_server
|
|
|
|
mock_ext = MockMCPExtension()
|
|
|
|
with patch("hindsight_api.api.mcp.load_extension", return_value=mock_ext):
|
|
mcp = create_mcp_server(mock_memory)
|
|
|
|
# Extension should have been called
|
|
assert mock_ext.register_tools_called is True
|
|
|
|
# Core tools should still be registered
|
|
tools = mcp._tool_manager._tools
|
|
assert "retain" in tools
|
|
assert "recall" in tools
|
|
|
|
# Extension tool should also be registered
|
|
assert "test_extension_tool" in tools
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_extension_tool_is_callable(self, mock_memory):
|
|
"""Tool registered by extension can be called."""
|
|
from hindsight_api.api.mcp import create_mcp_server
|
|
|
|
mock_ext = MockMCPExtension()
|
|
|
|
with patch("hindsight_api.api.mcp.load_extension", return_value=mock_ext):
|
|
mcp = create_mcp_server(mock_memory)
|
|
|
|
# Get and call the extension tool
|
|
tools = mcp._tool_manager._tools
|
|
test_tool = tools["test_extension_tool"]
|
|
result = await test_tool.fn(query="hello world")
|
|
|
|
assert result == "Extension tool received: hello world"
|
|
|
|
def test_load_extension_called_with_correct_args(self, mock_memory):
|
|
"""load_extension is called with 'MCP' prefix and MCPExtension class."""
|
|
from hindsight_api.api.mcp import create_mcp_server
|
|
|
|
with patch("hindsight_api.api.mcp.load_extension") as mock_load:
|
|
mock_load.return_value = None
|
|
create_mcp_server(mock_memory)
|
|
|
|
mock_load.assert_called_once_with("MCP", MCPExtension)
|
|
|
|
|
|
class TestMCPExtensionIntegration:
|
|
"""Integration tests verifying extension tools work end-to-end."""
|
|
|
|
@pytest.fixture
|
|
def mock_memory(self):
|
|
"""Create a mock MemoryEngine with required methods."""
|
|
memory = MagicMock()
|
|
memory.retain_batch_async = MagicMock()
|
|
memory.submit_async_retain = MagicMock(return_value={"operation_id": "test-op"})
|
|
memory.recall_async = MagicMock(return_value=MagicMock(results=[]))
|
|
memory.reflect_async = MagicMock(return_value=MagicMock(text="reflection"))
|
|
memory.list_banks = MagicMock(return_value=[])
|
|
memory.get_bank_profile = MagicMock(return_value={"id": "test"})
|
|
memory._tenant_extension = MagicMock()
|
|
return memory
|
|
|
|
def test_extension_tools_coexist_with_core_tools(self, mock_memory):
|
|
"""Extension tools are added alongside core tools, not replacing them."""
|
|
from hindsight_api.api.mcp import create_mcp_server
|
|
|
|
mock_ext = MockMCPExtension()
|
|
|
|
with patch("hindsight_api.api.mcp.load_extension", return_value=mock_ext):
|
|
mcp = create_mcp_server(mock_memory)
|
|
|
|
tools = mcp._tool_manager._tools
|
|
# All core tools present
|
|
assert "retain" in tools
|
|
assert "recall" in tools
|
|
assert "reflect" in tools
|
|
assert "list_banks" in tools
|
|
assert "create_bank" in tools
|
|
# Extension tool also present
|
|
assert "test_extension_tool" in tools
|
|
# At least 29 core + 1 extension = 30 tools (may grow as new tools are added)
|
|
assert len(tools) >= 30
|