mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-04-26 00:46:22 +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.
221 lines
8.1 KiB
Python
221 lines
8.1 KiB
Python
"""
|
|
Unit tests for GraphRAG service message format.
|
|
Tests the new message protocol with message_type, explain_id, and end_of_session.
|
|
Real-time explainability emission via callback.
|
|
"""
|
|
|
|
import pytest
|
|
from unittest.mock import MagicMock, AsyncMock, patch
|
|
|
|
from trustgraph.retrieval.graph_rag.rag import Processor
|
|
from trustgraph.schema import GraphRagQuery, GraphRagResponse
|
|
|
|
|
|
class TestGraphRagService:
|
|
"""Test GraphRAG service message protocol"""
|
|
|
|
@patch('trustgraph.retrieval.graph_rag.rag.GraphRag')
|
|
@pytest.mark.asyncio
|
|
async def test_non_streaming_sends_chunk_then_provenance_messages(self, mock_graph_rag_class):
|
|
"""
|
|
Test that non-streaming mode sends real-time provenance messages
|
|
followed by chunk message with response.
|
|
"""
|
|
# Setup processor
|
|
processor = Processor(
|
|
taskgroup=MagicMock(),
|
|
id="test-processor",
|
|
entity_limit=50,
|
|
triple_limit=30,
|
|
max_subgraph_size=150,
|
|
max_path_length=2
|
|
)
|
|
|
|
# Setup mock GraphRag instance that calls explain_callback
|
|
mock_rag_instance = AsyncMock()
|
|
mock_graph_rag_class.return_value = mock_rag_instance
|
|
|
|
# Mock query() to call the explain_callback with each provenance event
|
|
async def mock_query(**kwargs):
|
|
explain_callback = kwargs.get('explain_callback')
|
|
if explain_callback:
|
|
# Simulate real-time provenance emission
|
|
await explain_callback([], "urn:trustgraph:session:test")
|
|
await explain_callback([], "urn:trustgraph:prov:retrieval:test")
|
|
await explain_callback([], "urn:trustgraph:prov:selection:test")
|
|
await explain_callback([], "urn:trustgraph:prov:answer:test")
|
|
return "A small domesticated mammal.", {"in_token": None, "out_token": None, "model": None}
|
|
|
|
mock_rag_instance.query.side_effect = mock_query
|
|
|
|
# Setup message with non-streaming request
|
|
msg = MagicMock()
|
|
msg.value.return_value = GraphRagQuery(
|
|
query="What is a cat?",
|
|
collection="default",
|
|
entity_limit=50,
|
|
triple_limit=30,
|
|
max_subgraph_size=150,
|
|
max_path_length=2,
|
|
streaming=False
|
|
)
|
|
msg.properties.return_value = {"id": "test-id"}
|
|
|
|
# Setup flow mock
|
|
consumer = MagicMock()
|
|
flow = MagicMock()
|
|
|
|
mock_response_producer = AsyncMock()
|
|
mock_provenance_producer = AsyncMock()
|
|
def flow_router(service_name):
|
|
if service_name == "response":
|
|
return mock_response_producer
|
|
elif service_name == "explainability":
|
|
return mock_provenance_producer
|
|
return AsyncMock()
|
|
flow.side_effect = flow_router
|
|
|
|
# Execute
|
|
await processor.on_request(msg, consumer, flow)
|
|
|
|
# Verify: 5 messages sent (4 provenance + 1 combined chunk with end_of_session)
|
|
assert mock_response_producer.send.call_count == 5
|
|
|
|
# First 4 messages are explain (emitted in real-time during query)
|
|
for i in range(4):
|
|
prov_msg = mock_response_producer.send.call_args_list[i][0][0]
|
|
assert prov_msg.message_type == "explain"
|
|
assert prov_msg.explain_id is not None
|
|
|
|
# 5th message is chunk with response and end_of_session
|
|
chunk_msg = mock_response_producer.send.call_args_list[4][0][0]
|
|
assert chunk_msg.message_type == "chunk"
|
|
assert chunk_msg.response == "A small domesticated mammal."
|
|
assert chunk_msg.end_of_stream is True
|
|
assert chunk_msg.end_of_session is True
|
|
|
|
# Verify provenance triples were sent to provenance queue
|
|
assert mock_provenance_producer.send.call_count == 4
|
|
|
|
@patch('trustgraph.retrieval.graph_rag.rag.GraphRag')
|
|
@pytest.mark.asyncio
|
|
async def test_error_response_closes_session(self, mock_graph_rag_class):
|
|
"""
|
|
Test that error responses set end_of_session=True.
|
|
"""
|
|
# Setup processor
|
|
processor = Processor(
|
|
taskgroup=MagicMock(),
|
|
id="test-processor",
|
|
entity_limit=50,
|
|
triple_limit=30,
|
|
max_subgraph_size=150,
|
|
max_path_length=2
|
|
)
|
|
|
|
# Setup mock GraphRag instance that raises an exception
|
|
mock_rag_instance = AsyncMock()
|
|
mock_graph_rag_class.return_value = mock_rag_instance
|
|
mock_rag_instance.query.side_effect = Exception("Test error")
|
|
|
|
# Setup message with non-streaming request
|
|
msg = MagicMock()
|
|
msg.value.return_value = GraphRagQuery(
|
|
query="What is a cat?",
|
|
collection="default",
|
|
entity_limit=50,
|
|
triple_limit=30,
|
|
max_subgraph_size=150,
|
|
max_path_length=2,
|
|
streaming=False
|
|
)
|
|
msg.properties.return_value = {"id": "test-id"}
|
|
|
|
# Setup flow mock
|
|
consumer = MagicMock()
|
|
flow = MagicMock()
|
|
|
|
mock_response_producer = AsyncMock()
|
|
mock_provenance_producer = AsyncMock()
|
|
def flow_router(service_name):
|
|
if service_name == "response":
|
|
return mock_response_producer
|
|
elif service_name == "explainability":
|
|
return mock_provenance_producer
|
|
return AsyncMock()
|
|
flow.side_effect = flow_router
|
|
|
|
# Execute
|
|
await processor.on_request(msg, consumer, flow)
|
|
|
|
# Verify: error response was sent with session closed
|
|
mock_response_producer.send.assert_called_once()
|
|
sent_response = mock_response_producer.send.call_args[0][0]
|
|
assert isinstance(sent_response, GraphRagResponse)
|
|
assert sent_response.message_type == "chunk"
|
|
assert sent_response.error is not None
|
|
assert sent_response.error.message == "Test error"
|
|
assert sent_response.end_of_stream is True
|
|
assert sent_response.end_of_session is True
|
|
|
|
@patch('trustgraph.retrieval.graph_rag.rag.GraphRag')
|
|
@pytest.mark.asyncio
|
|
async def test_no_provenance_sends_empty_chunk_to_close(self, mock_graph_rag_class):
|
|
"""
|
|
Test that when no provenance callback is invoked, an empty chunk closes the session.
|
|
"""
|
|
# Setup processor
|
|
processor = Processor(
|
|
taskgroup=MagicMock(),
|
|
id="test-processor",
|
|
entity_limit=50,
|
|
triple_limit=30,
|
|
max_subgraph_size=150,
|
|
max_path_length=2
|
|
)
|
|
|
|
# Setup mock GraphRag instance that doesn't call provenance callback
|
|
mock_rag_instance = AsyncMock()
|
|
mock_graph_rag_class.return_value = mock_rag_instance
|
|
|
|
async def mock_query(**kwargs):
|
|
# Don't call explain_callback
|
|
return "Response text", {"in_token": None, "out_token": None, "model": None}
|
|
|
|
mock_rag_instance.query.side_effect = mock_query
|
|
|
|
# Setup message
|
|
msg = MagicMock()
|
|
msg.value.return_value = GraphRagQuery(
|
|
query="Test query",
|
|
collection="default",
|
|
streaming=False
|
|
)
|
|
msg.properties.return_value = {"id": "test-id"}
|
|
|
|
# Setup flow mock
|
|
consumer = MagicMock()
|
|
flow = MagicMock()
|
|
|
|
mock_response_producer = AsyncMock()
|
|
mock_provenance_producer = AsyncMock()
|
|
def flow_router(service_name):
|
|
if service_name == "response":
|
|
return mock_response_producer
|
|
elif service_name == "explainability":
|
|
return mock_provenance_producer
|
|
return AsyncMock()
|
|
flow.side_effect = flow_router
|
|
|
|
# Execute
|
|
await processor.on_request(msg, consumer, flow)
|
|
|
|
# Verify: 1 combined message (chunk with end_of_session)
|
|
assert mock_response_producer.send.call_count == 1
|
|
|
|
# Single message has response and end_of_session
|
|
chunk_msg = mock_response_producer.send.call_args_list[0][0][0]
|
|
assert chunk_msg.message_type == "chunk"
|
|
assert chunk_msg.response == "Response text"
|
|
assert chunk_msg.end_of_stream is True
|
|
assert chunk_msg.end_of_session is True
|