mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-04-25 08:26:21 +02:00
Introduces `workspace` as the isolation boundary for config, flows,
library, and knowledge data. Removes `user` as a schema-level field
throughout the code, API specs, and tests; workspace provides the
same separation more cleanly at the trusted flow.workspace layer
rather than through client-supplied message fields.
Design
------
- IAM tech spec (docs/tech-specs/iam.md) documents current state,
proposed auth/access model, and migration direction.
- Data ownership model (docs/tech-specs/data-ownership-model.md)
captures the workspace/collection/flow hierarchy.
Schema + messaging
------------------
- Drop `user` field from AgentRequest/Step, GraphRagQuery,
DocumentRagQuery, Triples/Graph/Document/Row EmbeddingsRequest,
Sparql/Rows/Structured QueryRequest, ToolServiceRequest.
- Keep collection/workspace routing via flow.workspace at the
service layer.
- Translators updated to not serialise/deserialise user.
API specs
---------
- OpenAPI schemas and path examples cleaned of user fields.
- Websocket async-api messages updated.
- Removed the unused parameters/User.yaml.
Services + base
---------------
- Librarian, collection manager, knowledge, config: all operations
scoped by workspace. Config client API takes workspace as first
positional arg.
- `flow.workspace` set at flow start time by the infrastructure;
no longer pass-through from clients.
- Tool service drops user-personalisation passthrough.
CLI + SDK
---------
- tg-init-workspace and workspace-aware import/export.
- All tg-* commands drop user args; accept --workspace.
- Python API/SDK (flow, socket_client, async_*, explainability,
library) drop user kwargs from every method signature.
MCP server
----------
- All tool endpoints drop user parameters; socket_manager no longer
keyed per user.
Flow service
------------
- Closure-based topic cleanup on flow stop: only delete topics
whose blueprint template was parameterised AND no remaining
live flow (across all workspaces) still resolves to that topic.
Three scopes fall out naturally from template analysis:
* {id} -> per-flow, deleted on stop
* {blueprint} -> per-blueprint, kept while any flow of the
same blueprint exists
* {workspace} -> per-workspace, kept while any flow in the
workspace exists
* literal -> global, never deleted (e.g. tg.request.librarian)
Fixes a bug where stopping a flow silently destroyed the global
librarian exchange, wedging all library operations until manual
restart.
RabbitMQ backend
----------------
- heartbeat=60, blocked_connection_timeout=300. Catches silently
dead connections (broker restart, orphaned channels, network
partitions) within ~2 heartbeat windows, so the consumer
reconnects and re-binds its queue rather than sitting forever
on a zombie connection.
Tests
-----
- Full test refresh: unit, integration, contract, provenance.
- Dropped user-field assertions and constructor kwargs across
~100 test files.
- Renamed user-collection isolation tests to workspace-collection.
187 lines
No EOL
6.9 KiB
Python
187 lines
No EOL
6.9 KiB
Python
"""
|
|
Unit tests for trustgraph.base.document_embeddings_client
|
|
Testing async document embeddings client functionality
|
|
"""
|
|
|
|
import pytest
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
|
from unittest import IsolatedAsyncioTestCase
|
|
|
|
from trustgraph.base.document_embeddings_client import DocumentEmbeddingsClient, DocumentEmbeddingsClientSpec
|
|
from trustgraph.schema import DocumentEmbeddingsRequest, DocumentEmbeddingsResponse, Error
|
|
|
|
|
|
class TestDocumentEmbeddingsClient(IsolatedAsyncioTestCase):
|
|
"""Test async document embeddings client functionality"""
|
|
|
|
@patch('trustgraph.base.request_response_spec.RequestResponse.__init__')
|
|
async def test_query_success_with_chunks(self, mock_parent_init):
|
|
"""Test successful query returning chunks"""
|
|
# Arrange
|
|
mock_parent_init.return_value = None
|
|
client = DocumentEmbeddingsClient()
|
|
mock_response = MagicMock(spec=DocumentEmbeddingsResponse)
|
|
mock_response.error = None
|
|
mock_response.chunks = ["chunk1", "chunk2", "chunk3"]
|
|
|
|
# Mock the request method
|
|
client.request = AsyncMock(return_value=mock_response)
|
|
|
|
vector = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
|
|
|
|
# Act
|
|
result = await client.query(
|
|
vector=vector,
|
|
limit=10,
|
|
collection="test_collection",
|
|
timeout=30
|
|
)
|
|
|
|
# Assert
|
|
assert result == ["chunk1", "chunk2", "chunk3"]
|
|
client.request.assert_called_once()
|
|
call_args = client.request.call_args[0][0]
|
|
assert isinstance(call_args, DocumentEmbeddingsRequest)
|
|
assert call_args.vector == vector
|
|
assert call_args.limit == 10
|
|
assert call_args.collection == "test_collection"
|
|
|
|
@patch('trustgraph.base.request_response_spec.RequestResponse.__init__')
|
|
async def test_query_with_error_raises_exception(self, mock_parent_init):
|
|
"""Test query raises RuntimeError when response contains error"""
|
|
# Arrange
|
|
mock_parent_init.return_value = None
|
|
client = DocumentEmbeddingsClient()
|
|
mock_response = MagicMock(spec=DocumentEmbeddingsResponse)
|
|
mock_response.error = MagicMock()
|
|
mock_response.error.message = "Database connection failed"
|
|
|
|
client.request = AsyncMock(return_value=mock_response)
|
|
|
|
# Act & Assert
|
|
with pytest.raises(RuntimeError, match="Database connection failed"):
|
|
await client.query(
|
|
vector=[0.1, 0.2, 0.3],
|
|
limit=5
|
|
)
|
|
|
|
@patch('trustgraph.base.request_response_spec.RequestResponse.__init__')
|
|
async def test_query_with_empty_chunks(self, mock_parent_init):
|
|
"""Test query with empty chunks list"""
|
|
# Arrange
|
|
mock_parent_init.return_value = None
|
|
client = DocumentEmbeddingsClient()
|
|
mock_response = MagicMock(spec=DocumentEmbeddingsResponse)
|
|
mock_response.error = None
|
|
mock_response.chunks = []
|
|
|
|
client.request = AsyncMock(return_value=mock_response)
|
|
|
|
# Act
|
|
result = await client.query(vector=[0.1, 0.2, 0.3])
|
|
|
|
# Assert
|
|
assert result == []
|
|
|
|
@patch('trustgraph.base.request_response_spec.RequestResponse.__init__')
|
|
async def test_query_with_default_parameters(self, mock_parent_init):
|
|
"""Test query uses correct default parameters"""
|
|
# Arrange
|
|
mock_parent_init.return_value = None
|
|
client = DocumentEmbeddingsClient()
|
|
mock_response = MagicMock(spec=DocumentEmbeddingsResponse)
|
|
mock_response.error = None
|
|
mock_response.chunks = ["test_chunk"]
|
|
|
|
client.request = AsyncMock(return_value=mock_response)
|
|
|
|
# Act
|
|
result = await client.query(vector=[0.1, 0.2, 0.3])
|
|
|
|
# Assert
|
|
client.request.assert_called_once()
|
|
call_args = client.request.call_args[0][0]
|
|
assert call_args.limit == 20 # Default limit
|
|
assert call_args.collection == "default" # Default collection
|
|
|
|
@patch('trustgraph.base.request_response_spec.RequestResponse.__init__')
|
|
async def test_query_with_custom_timeout(self, mock_parent_init):
|
|
"""Test query passes custom timeout to request"""
|
|
# Arrange
|
|
mock_parent_init.return_value = None
|
|
client = DocumentEmbeddingsClient()
|
|
mock_response = MagicMock(spec=DocumentEmbeddingsResponse)
|
|
mock_response.error = None
|
|
mock_response.chunks = ["chunk1"]
|
|
|
|
client.request = AsyncMock(return_value=mock_response)
|
|
|
|
# Act
|
|
await client.query(
|
|
vector=[0.1, 0.2, 0.3],
|
|
timeout=60
|
|
)
|
|
|
|
# Assert
|
|
assert client.request.call_args[1]["timeout"] == 60
|
|
|
|
@patch('trustgraph.base.request_response_spec.RequestResponse.__init__')
|
|
async def test_query_logging(self, mock_parent_init):
|
|
"""Test query logs response for debugging"""
|
|
# Arrange
|
|
mock_parent_init.return_value = None
|
|
client = DocumentEmbeddingsClient()
|
|
mock_response = MagicMock(spec=DocumentEmbeddingsResponse)
|
|
mock_response.error = None
|
|
mock_response.chunks = ["test_chunk"]
|
|
|
|
client.request = AsyncMock(return_value=mock_response)
|
|
|
|
# Act
|
|
with patch('trustgraph.base.document_embeddings_client.logger') as mock_logger:
|
|
result = await client.query(vector=[0.1, 0.2, 0.3])
|
|
|
|
# Assert
|
|
mock_logger.debug.assert_called_once()
|
|
assert "Document embeddings response" in str(mock_logger.debug.call_args)
|
|
assert result == ["test_chunk"]
|
|
|
|
|
|
class TestDocumentEmbeddingsClientSpec(IsolatedAsyncioTestCase):
|
|
"""Test DocumentEmbeddingsClientSpec configuration"""
|
|
|
|
def test_spec_initialization(self):
|
|
"""Test DocumentEmbeddingsClientSpec initialization"""
|
|
# Act
|
|
spec = DocumentEmbeddingsClientSpec(
|
|
request_name="test-request",
|
|
response_name="test-response"
|
|
)
|
|
|
|
# Assert
|
|
assert spec.request_name == "test-request"
|
|
assert spec.response_name == "test-response"
|
|
assert spec.request_schema == DocumentEmbeddingsRequest
|
|
assert spec.response_schema == DocumentEmbeddingsResponse
|
|
assert spec.impl == DocumentEmbeddingsClient
|
|
|
|
@patch('trustgraph.base.request_response_spec.RequestResponseSpec.__init__')
|
|
def test_spec_calls_parent_init(self, mock_parent_init):
|
|
"""Test spec properly calls parent class initialization"""
|
|
# Arrange
|
|
mock_parent_init.return_value = None
|
|
|
|
# Act
|
|
spec = DocumentEmbeddingsClientSpec(
|
|
request_name="test-request",
|
|
response_name="test-response"
|
|
)
|
|
|
|
# Assert
|
|
mock_parent_init.assert_called_once_with(
|
|
request_name="test-request",
|
|
request_schema=DocumentEmbeddingsRequest,
|
|
response_name="test-response",
|
|
response_schema=DocumentEmbeddingsResponse,
|
|
impl=DocumentEmbeddingsClient
|
|
) |