Ontology Synchronization
Ontology synchronization projects existing Kavach governance domain objects into the kavach.governance ontology graph. Domain repositories and services remain the systems of record.
Overview
The synchronization layer is one-way: domain objects are projected into the ontology graph for lineage, provenance, and future traversal workflows. Synchronizers never talk directly to Neo4j — all graph writes go through OntologyService, which preserves relationship validation and idempotency.
Synchronization Contract
Every synchronizer implements:
class OntologySynchronizer:
def synchronize(entity) -> SynchronizationResult
def delete(entity) -> SynchronizationResult
def resynchronize(entity_id: str) -> SynchronizationResultSynchronizationResult includes synchronized entity IDs, relationship IDs, archived entity IDs, errors, duration, failure count, retry count, and entity/relationship counters.
Synchronizers
Registry synchronizers:
PromptOntologySynchronizerModelOntologySynchronizerDatasetOntologySynchronizer
These create logical asset entities, version entities, actor provenance, and version relationships: HAS_VERSION, VERSION_OF, CREATED_BY, OWNED_BY, and SUPERSEDES.
Experiment synchronizers:
ExperimentOntologySynchronizerCandidateOntologySynchronizer
These create experiments, candidates, provider nodes, ownership relationships, and candidate USES links to prompt/model/dataset versions.
Evaluation synchronizers:
EvaluationRunOntologySynchronizerEvaluationResultOntologySynchronizer
These create evaluation runs, results, provider nodes, metric nodes, artifact nodes, and evidence relationships including EXECUTES, PRODUCES, HAS_METRIC, HAS_ARTIFACT, and EVALUATED_BY.
Operational synchronizers:
JobOntologySynchronizerMCPAuditOntologySynchronizerWorkflowExecutionOntologySynchronizerReplayOntologySynchronizer
Governance synchronizers:
GovernanceDecisionOntologySynchronizerGovernanceInsightOntologySynchronizerGovernanceReportOntologySynchronizerDriftOntologySynchronizer
Reconciliation
DiffBasedOntologyReconciler is the primary reconciliation engine. It builds the expected ontology projection from domain repositories, fingerprints the semantic projection, loads the current graph snapshot, and repairs only detected drift.
- 01build expected projection
Domain repositories produce the authoritative semantic graph shape.
- 02fingerprint semantic state
Entity and relationship hashes ignore timestamps and sync metadata.
- 03compare graph snapshot
The reconciler skips matching projections and isolates semantic drift.
- 04repair only the diff
Synchronizers update missing or changed entities and relationships.
governed outcome
IN SYNC
Systems of record preserved · semantic drift repaired · graph traversal ready
The reconciliation workflow:
- Build expected projection with
ProjectionBuilder. - Compute
projection_hashfor semantic entity state. - Compute
relationship_set_hashfor the unordered relationship set. - Load the current graph projection snapshot.
- Skip projections with matching stored hashes.
- Update missing projection metadata when semantic state already matches.
- Run the synchronizer only when entities or relationships have semantic drift.
Fingerprints ignore timestamps, synchronization metadata, and implementation details. Relationship order does not affect relationship_set_hash.
Event-Driven Synchronization
Event-driven synchronization records ontology work as durable events and lets a background worker process those events asynchronously. Domain services publish events after authoritative state changes, but remain unaware of Neo4j.
Domain service -> OntologySyncEventPublisher -> event repository
event repository -> OntologySynchronizationWorker -> DiffBasedOntologyReconcilerSupported statuses: PENDING, PROCESSING, COMPLETED, FAILED, DEAD_LETTER, and CANCELLED.
REST monitoring endpoints:
GET /api/v1/ontology/synchronization/eventsGET /api/v1/ontology/synchronization/events/[event_id]POST /api/v1/ontology/synchronization/events/[event_id]/retryPOST /api/v1/ontology/synchronization/events/[event_id]/cancelGET /api/v1/ontology/synchronization/events/metrics
Deletion Strategy
Synchronizers do not hard-delete ontology entities. delete(entity) marks the projected entity lifecycle as ARCHIVED, records archival metadata, and preserves historical relationships. Physical cleanup remains an explicit orphan cleanup concern outside ontology synchronization.
Running Tests
# Unit tests
uv run pytest tests/unit/ontology_sync
# With foundation ontology tests
uv run pytest tests/unit/ontology_sync tests/unit/ontology
# Neo4j integration tests
export KAVACH_RUN_NEO4J_TESTS=true
uv run pytest tests/integration/ontology_sync