MATIH Platform is in active MVP development. Documentation reflects current implementation status.
14. Context Graph & Ontology
Context Graph Architecture

Context Graph Architecture

The Context Graph is the knowledge infrastructure backbone of the MATIH platform. It captures agent reasoning, entity relationships, and decision lineage in a multi-backend storage system that combines graph databases, vector stores, and bi-temporal relational storage.


High-Level Architecture

The Context Graph module resides within the AI Service and connects to multiple storage backends depending on the query type and data characteristics.

+---------------------------------------------------+
|              AI Service (Port 8000)                |
|                                                    |
|  +---------------------------------------------+  |
|  |           Context Graph Module               |  |
|  |                                              |  |
|  |  +------------------+ +-------------------+  |  |
|  |  | Agent Thinking   | | Orchestrator      |  |  |
|  |  | Service          | | Hooks             |  |  |
|  |  +--------+---------+ +--------+----------+  |  |
|  |           |                     |             |  |
|  |  +--------v---------+ +--------v----------+  |  |
|  |  | Dgraph Context   | | Kafka Producer /  |  |  |
|  |  | Store            | | Consumer          |  |  |
|  |  +--------+---------+ +--------+----------+  |  |
|  |           |                     |             |  |
|  |  +--------v---------+ +--------v----------+  |  |
|  |  | Hybrid Store     | | Metrics Bridge    |  |  |
|  |  | (GraphRAG)       | |                   |  |  |
|  |  +------------------+ +-------------------+  |  |
|  +---------------------------------------------+  |
+---------------------------------------------------+
        |              |              |
   +----v----+   +----v----+   +----v--------+
   | Dgraph  |   | Pinecone|   | PostgreSQL  |
   | (Graph) |   | (Vector)|   | (Bitemporal)|
   +---------+   +---------+   +-------------+

Storage Backends

The Context Graph uses three complementary storage backends, each optimized for different query patterns.

BackendTechnologyPurposeData Types
Graph StoreDgraphEntity relationships, thinking traces, traversalsEntities, relationships, traces
Vector StorePinecone / QdrantSemantic similarity, embedding searchEntity embeddings, decision rationale vectors
Bi-Temporal StorePostgreSQL + TimescaleDBPoint-in-time queries, version history, decisionsEvents, decisions, entity versions

Multi-Tenant Isolation

Every operation in the Context Graph requires a tenant_id parameter. Isolation is enforced at each storage layer:

  • Dgraph: GraphQL queries filter by tenantId field on all entity types
  • Pinecone: Namespaces are prefixed with the tenant ID (format: tenant_id:namespace)
  • PostgreSQL: All tables include a tenant_id column with indexed filters

Data Flow

Agent interactions flow through the Context Graph in a pipeline pattern:

  1. Capture -- Orchestrator hooks intercept agent thinking steps and LLM calls
  2. Persist -- The Thinking Service stores traces in Dgraph with optional Kafka streaming
  3. Embed -- The Thinking Embedding Service generates vectors for input, output, and reasoning
  4. Index -- Vectors are upserted into Pinecone namespaces for similarity search
  5. Query -- The Semantic Search Service combines vector and graph results for retrieval

Key Components

ComponentSource PathDescription
DgraphContextStorestorage/dgraph_context_store.pyCRUD for thinking traces in Dgraph
HybridStorestorage/hybrid_store.pyCombined graph + vector search (GraphRAG)
BiTemporalStorestorage/bitemporal_store.pyPostgreSQL bi-temporal storage for decisions
PineconeVectorStorestorage/pinecone_vector_store.pyProduction Pinecone vector operations
AgentThinkingCaptureServiceservices/agent_thinking_service.pyCaptures agent reasoning during execution
ContextGraphOrchestratorHooksintegration/orchestrator_hooks.pyNon-invasive integration with orchestrators
ContextGraphKafkaConsumerintegration/kafka_consumer.pyConsumes events from Kafka topics
MetricsBridgeintegration/metrics_bridge.pyLinks observability metrics to thinking traces
SemanticSearchServiceservices/semantic_search_service.pyUnified search orchestrator
ContextGraphAuthorizersecurity/authorization.pyRBAC enforcement for API endpoints

GraphRAG Retrieval Strategies

The Hybrid Store implements multiple retrieval strategies based on the Microsoft GraphRAG architecture:

StrategyDescriptionUse Case
LOCALVector similarity + 1-hop graph neighborsQuick entity lookup
GLOBALCommunity detection + summary embeddingsBroad topic exploration
DRIFTTemporal trajectory patternsDetecting changes over time
HYBRIDCombined vector + graph scoring with configurable weightsDefault for most queries
GRAPH_FIRSTGraph traversal followed by vector rerankingWhen structure matters most
VECTOR_FIRSTVector search followed by graph expansionWhen semantics matter most

Feature Flag Control

Context Graph features are rolled out per-tenant using the Semantic Feature Flags service. Features can be toggled independently:

Feature FlagDescription
context_graph_thinkingEnable agent thinking trace capture
context_graph_kafkaEnable Kafka streaming of context events
context_graph_embeddingsEnable embedding generation for traces
context_graph_rbacEnable fine-grained RBAC on API endpoints

Rollout modes include disabled, canary (hash-based percentage), partial, and full.


Configuration

The Context Graph is configured through environment variables. No credentials are hardcoded.

VariableDescriptionDefault
DGRAPH_GRPC_URLDgraph gRPC endpointlocalhost:9080
DGRAPH_HTTP_URLDgraph HTTP/GraphQL endpointlocalhost:8080
PINECONE_API_KEYPinecone API key (from secret)--
PINECONE_ENVIRONMENTPinecone cloud regionus-east-1-aws
PINECONE_INDEX_PREFIXPrefix for Pinecone index namesmatih-context
BITEMPORAL_DATABASE_URLPostgreSQL connection string (from secret)Falls back to DATABASE_URL
KAFKA_BOOTSTRAP_SERVERSKafka bootstrap serverslocalhost:29092