Graph-Native Infrastructure for Context and Accountable AI Systems
Python agent-memoryaiai-governanceai-infrastructureartificial-intelligencecontext-engineeringcontext-graphsdata-engineeringdecision-intelligencedeveloper-toolsexplainable-aigenerative-aigraph-ragknowledge-graphllmontologyprovenancepythonreasoningsemantic-search
feat(ontology): declare the Semantica vocabulary, and mint entity IRIs deterministically
FABIOTESSe1092ac507b8
diff --git a/pyproject.toml b/pyproject.tomlindex 341e57ed..286a26b8 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -271,7 +271,7 @@ include = ["semantica*", "integrations*"][tool.setuptools.package-data]# Explicit patterns are more reliable than **/* across setuptools versions.# static/* covers index.html / favicon; static/assets/* covers all JS/CSS chunks.-"semantica" = ["static/*", "static/assets/*"]+"semantica" = ["static/*", "static/assets/*", "ontology/vocabulary/*.ttl"][tool.black]line-length = 88diff --git a/semantica/export/rdf_exporter.py b/semantica/export/rdf_exporter.pyindex 7c50ffc5..40cc4341 100644--- a/semantica/export/rdf_exporter.py+++ b/semantica/export/rdf_exporter.py@@ -30,6 +30,7 @@ License: MIT"""from pathlib import Path+import hashlibfrom typing import Any, Dict, List, Optional, Set, Unionfrom ..utils.exceptions import ProcessingError, ValidationError@@ -38,6 +39,37 @@ from ..utils.logging import get_loggerfrom ..utils.progress_tracker import get_progress_tracker+SEMANTICA_NS = "https://semantica.dev/ns#"++#: Written when an entity carries no type of its own. A full IRI rather than the+#: prefixed form, because the Turtle serializer writes it inside angle brackets,+#: where `semantica:Entity` would be read as an IRI in the scheme `semantica`+#: rather than as the prefix expansion (issue #1101).+DEFAULT_ENTITY_TYPE = f"{SEMANTICA_NS}Entity"++#: Written when a relationship carries no type of its own. Same reasoning.+DEFAULT_RELATION_TYPE = f"{SEMANTICA_NS}related_to"+++def mint_entity_iri(text: str) -> str:+ """Mint a stable IRI for an entity that arrived without an id.++ Python's builtin ``hash()`` is randomised per process (PYTHONHASHSEED), so+ minting from it gave the same entity a different IRI on every run: exports+ could not be diffed, deduplicated against an earlier load, or joined to a+ provenance record written by an earlier process. SHA-256 is stable across+ runs and machines, which is what an identifier has to be.+ """+ digest = hashlib.sha256(str(text).encode("utf-8")).hexdigest()[:16]+ return f"{SEMANTICA_NS}entity_{digest}"+++def mint_relationship_iri(index: int, source: Any, target: Any) -> str:+ """Mint a stable IRI for a relationship that arrived without an id."""+ digest = hashlib.sha256(f"{source}\x00{target}".encode("utf-8")).hexdigest()[:16]+ return f"{SEMANTICA_NS}rel_{index}_{digest}"++class NamespaceManager:"""RDF namespace management engine.@@ -360,9 +392,9 @@ class RDFSerializer:entity_id = entity.get("id")if not entity_id:entity_text = entity.get("text", "")- entity_id = f"semantica:entity_{hash(entity_text)}"+ entity_id = mint_entity_iri(entity_text)- entity_type = entity.get("type", "semantica:Entity")+ entity_type = entity.get("type", DEFAULT_ENTITY_TYPE)text = entity.get("text") or entity.get("label", "")confidence = entity.get("confidence", 1.0)@@ -376,7 +408,7 @@ class RDFSerializer:for idx, rel in enumerate(relationships):source_id = rel.get("source_id") or rel.get("source")target_id = rel.get("target_id") or rel.get("target")- rel_type = rel.get("type", "semantica:related_to")+ rel_type = rel.get("type", DEFAULT_RELATION_TYPE)lines.append(f"<{source_id}> <{rel_type}> <{target_id}> .")@@ -415,7 +447,7 @@ class RDFSerializer:rel_base_id = (rel.get("id")- or f"semantica:rel_{idx}_{hash(str(rel.get('source_id', '')) + str(rel.get('target_id', '')))}"+ or mint_relationship_iri(idx, rel.get('source_id', ''), rel.get('target_id', '')))lines = [""] # blank separator@@ -488,7 +520,7 @@ class RDFSerializer:entity_id = entity.get("id")if not entity_id:entity_text = entity.get("text", "")- entity_id = f"semantica:entity_{hash(entity_text)}"+ entity_id = mint_entity_iri(entity_text)entity_type = entity.get("type", "semantica:Entity")text = entity.get("text") or entity.get("label", "")@@ -644,7 +676,7 @@ class RDFSerializer:entity_id = entity.get("id")if not entity_id:entity_text = entity.get("text", "")- entity_id = f"semantica:entity_{hash(entity_text)}"+ entity_id = mint_entity_iri(entity_text)subject = expand_uri(entity_id)diff --git a/semantica/ontology/vocabulary/__init__.py b/semantica/ontology/vocabulary/__init__.pynew file mode 100644index 00000000..d0665830--- /dev/null+++ b/semantica/ontology/vocabulary/__init__.py@@ -0,0 +1,35 @@+"""The vocabulary Semantica's exporters emit terms from.++Every RDF export mints terms in ``https://semantica.dev/ns#``: ``sem:text``,+``sem:confidence``, the default ``sem:Entity`` type, and the rest. Until this+file existed, nothing declared what those terms meant, so a consumer receiving+an export could not tell ``sem:text`` from a typo of it, and no closed-world+check could be run against them at all (issue #1107).++The document ships inside the package so it can be loaded without a network+round trip, and is the same file intended to be served at the namespace IRI.++ >>> from semantica.ontology.vocabulary import vocabulary_turtle+ >>> ttl = vocabulary_turtle()+"""++from __future__ import annotations++from pathlib import Path++VOCABULARY_FILENAME = "semantica-ns.ttl"++#: The namespace the vocabulary declares terms in.+NAMESPACE = "https://semantica.dev/ns#"++__all__ = ["NAMESPACE", "VOCABULARY_FILENAME", "vocabulary_path", "vocabulary_turtle"]+++def vocabulary_path() -> Path:+ """Filesystem path to the vocabulary document."""+ return Path(__file__).parent / VOCABULARY_FILENAME+++def vocabulary_turtle() -> str:+ """The vocabulary document as Turtle."""+ return vocabulary_path().read_text(encoding="utf-8")diff --git a/semantica/ontology/vocabulary/semantica-ns.ttl b/semantica/ontology/vocabulary/semantica-ns.ttlnew file mode 100644index 00000000..b5695b08--- /dev/null+++ b/semantica/ontology/vocabulary/semantica-ns.ttl@@ -0,0 +1,134 @@+@prefix owl: <http://www.w3.org/2002/07/owl#> .+@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .+@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .+@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .+@prefix dct: <http://purl.org/dc/terms/> .+@prefix prov: <http://www.w3.org/ns/prov#> .+@prefix time: <http://www.w3.org/2006/time#> .+@prefix sem: <https://semantica.dev/ns#> .++<https://semantica.dev/ns> a owl:Ontology ;+ rdfs:label "Semantica vocabulary" ;+ rdfs:comment """Declares the terms the Semantica exporters emit in+https://semantica.dev/ns#. Drafted from the emitting call sites in+semantica 0.6.5: export/rdf_exporter.py, export/json_exporter.py and+provenance/manager.py. Every term below appears in output the package+produces today; no term has been invented for completeness.""" ;+ owl:versionInfo "0.1.0-draft" ;+ dct:created "2026-08-19"^^xsd:date .++# ── Classes ──────────────────────────────────────────────────────────────────++sem:Entity a owl:Class ;+ rdfs:label "Entity" ;+ rdfs:comment """The default type given to an extracted entity when the+source carries no type of its own. Emitted by serialize_to_turtle as the+fallback for entity.get("type").""" ;+ rdfs:isDefinedBy <https://semantica.dev/ns> .++sem:Relationship a owl:Class ;+ rdfs:label "Relationship" ;+ rdfs:comment """A reified relationship, as emitted in the JSON-LD export+where a relationship carries sem:type, sem:source and sem:target rather than+being written as a single triple.""" ;+ rdfs:isDefinedBy <https://semantica.dev/ns> .++# ── Properties on an entity ──────────────────────────────────────────────────++sem:text a owl:DatatypeProperty ;+ rdfs:label "text" ;+ rdfs:comment """The surface text of an extracted entity. Carries the same+intent as rdfs:label; declared separately because the exporters emit it under+this IRI.""" ;+ rdfs:domain sem:Entity ;+ rdfs:range xsd:string ;+ rdfs:isDefinedBy <https://semantica.dev/ns> .++sem:confidence a owl:DatatypeProperty ;+ rdfs:label "confidence" ;+ rdfs:comment """Extractor confidence in the assertion, on the unit interval.+Emitted for both entities and relationships, so the domain is left open rather+than tied to sem:Entity.""" ;+ rdfs:range xsd:decimal ;+ rdfs:isDefinedBy <https://semantica.dev/ns> .++sem:metadata a owl:AnnotationProperty ;+ rdfs:label "metadata" ;+ rdfs:comment """Free-form metadata carried through from extraction. An+annotation property because its value is an arbitrary structure rather than a+modelled one.""" ;+ rdfs:isDefinedBy <https://semantica.dev/ns> .++# ── Relationship terms (JSON-LD export) ──────────────────────────────────────++sem:related_to a owl:ObjectProperty ;+ rdfs:label "related to" ;+ rdfs:comment """The default predicate for a relationship whose type the+extractor did not determine. Deliberately unspecific: it asserts that two+entities are connected and nothing about how.""" ;+ rdfs:isDefinedBy <https://semantica.dev/ns> .++sem:source a owl:ObjectProperty ;+ rdfs:label "source" ;+ rdfs:comment "The subject entity of a reified relationship." ;+ rdfs:domain sem:Relationship ;+ rdfs:isDefinedBy <https://semantica.dev/ns> .++sem:target a owl:ObjectProperty ;+ rdfs:label "target" ;+ rdfs:comment "The object entity of a reified relationship." ;+ rdfs:domain sem:Relationship ;+ rdfs:isDefinedBy <https://semantica.dev/ns> .++sem:type a owl:DatatypeProperty ;+ rdfs:label "type" ;+ rdfs:comment """The relationship type as a label, as emitted in the JSON-LD+export. Distinct from rdf:type, which relates a node to a class rather than to+a string.""" ;+ rdfs:domain sem:Relationship ;+ rdfs:range xsd:string ;+ rdfs:isDefinedBy <https://semantica.dev/ns> .++# ── Document-level terms (JSON-LD export) ────────────────────────────────────++sem:entities a owl:ObjectProperty ;+ rdfs:label "entities" ;+ rdfs:comment "Ordered list of entities in an exported graph document." ;+ rdfs:range sem:Entity ;+ rdfs:isDefinedBy <https://semantica.dev/ns> .++sem:relationships a owl:ObjectProperty ;+ rdfs:label "relationships" ;+ rdfs:comment "Ordered list of relationships in an exported graph document." ;+ rdfs:range sem:Relationship ;+ rdfs:isDefinedBy <https://semantica.dev/ns> .++sem:exportedAt a owl:DatatypeProperty ;+ rdfs:label "exported at" ;+ rdfs:comment """When the export was written. Emitted as an ISO 8601 local+timestamp, so the range is xsd:dateTime rather than xsd:dateTimeStamp: the+values carry no timezone offset.""" ;+ rdfs:range xsd:dateTime ;+ rdfs:isDefinedBy <https://semantica.dev/ns> .++# ── Temporal term (OWL-Time export) ──────────────────────────────────────────++sem:openEndedInterval a owl:DatatypeProperty ;+ rdfs:label "open ended interval" ;+ rdfs:comment """True when an interval has no known end. OWL-Time has no+standard predicate for this, which is the reason the exporter mints one: an+interval with no time:hasEnd is ambiguous between "ongoing" and "end not+recorded", and this term resolves that in favour of the first.""" ;+ rdfs:domain time:Interval ;+ rdfs:range xsd:boolean ;+ rdfs:isDefinedBy <https://semantica.dev/ns> .++# ── Provenance roles ─────────────────────────────────────────────────────────++sem:role_generator a prov:Role ;+ rdfs:label "generator" ;+ rdfs:comment """The default role in a prov:qualifiedAssociation, used when+an agent generated an entity rather than approving or reviewing it. Typed as+prov:Role so that prov:hadRole has a declared value rather than an undeclared+IRI.""" ;+ rdfs:isDefinedBy <https://semantica.dev/ns> .diff --git a/tests/export/test_rdf_exporter_iri_minting.py b/tests/export/test_rdf_exporter_iri_minting.pynew file mode 100644index 00000000..5734db73--- /dev/null+++ b/tests/export/test_rdf_exporter_iri_minting.py@@ -0,0 +1,88 @@+"""Minted IRIs must be stable and must sit in the declared namespace (issue #1101).++An entity that arrives without an id gets one minted for it. That identifier was+built from Python's builtin ``hash()``, which is randomised per process, so the+same entity received a different IRI on every run and exports could not be+diffed, deduplicated against an earlier load, or joined to a provenance record+written by an earlier process.++It was also written as ``semantica:entity_N`` inside angle brackets, which is an+IRI in the scheme ``semantica`` rather than the expansion of the declared+``semantica:`` prefix, so it never joined with anything written through it.+"""++import subprocess+import sys++from semantica.export.rdf_exporter import (+ DEFAULT_ENTITY_TYPE,+ DEFAULT_RELATION_TYPE,+ RDFExporter,+ SEMANTICA_NS,+ mint_entity_iri,+ mint_relationship_iri,+)++UNIDENTIFIED = {+ "entities": [{"text": "Acme Corp", "type": "https://example.org/Org"}],+ "relationships": [],+}+++def test_minted_entity_iri_is_stable_within_a_process():+ assert mint_entity_iri("Acme Corp") == mint_entity_iri("Acme Corp")+++def test_minted_entity_iri_is_stable_across_processes():+ """The regression that matters: identity must survive a restart."""+ script = (+ "from semantica.export.rdf_exporter import mint_entity_iri;"+ "print(mint_entity_iri('Acme Corp'))"+ )+ runs = {+ subprocess.run(+ [sys.executable, "-c", script],+ capture_output=True,+ text=True,+ check=True,+ env={"PYTHONHASHSEED": seed, "PATH": "/usr/bin:/bin"},+ ).stdout.strip()+ for seed in ("0", "1", "random")+ }+ assert len(runs) == 1, f"minted IRI differs between processes: {runs}"+++def test_minted_iris_are_in_the_declared_namespace():+ assert mint_entity_iri("Acme Corp").startswith(SEMANTICA_NS)+ assert mint_relationship_iri(0, "a", "b").startswith(SEMANTICA_NS)+++def test_distinct_entities_get_distinct_iris():+ assert mint_entity_iri("Acme Corp") != mint_entity_iri("Acme Corporation")+++def test_turtle_export_writes_a_resolvable_minted_iri():+ turtle = RDFExporter().export_to_rdf(UNIDENTIFIED, format="turtle")++ assert f"<{SEMANTICA_NS}entity_" in turtle+ assert "<semantica:entity_" not in turtle, "scheme 'semantica' is not the prefix"+++def test_ntriples_export_agrees_with_turtle_on_the_minted_iri():+ exporter = RDFExporter()+ minted = mint_entity_iri("Acme Corp")++ assert minted in exporter.export_to_rdf(UNIDENTIFIED, format="turtle")+ assert minted in exporter.export_to_rdf(UNIDENTIFIED, format="ntriples")+++def test_default_types_are_written_as_full_iris_in_turtle():+ untyped = {"entities": [{"id": "https://example.org/e1", "text": "A"}],+ "relationships": [{"source": "https://example.org/e1",+ "target": "https://example.org/e2"}]}+ turtle = RDFExporter().export_to_rdf(untyped, format="turtle")++ assert f"<{DEFAULT_ENTITY_TYPE}>" in turtle+ assert f"<{DEFAULT_RELATION_TYPE}>" in turtle+ assert "<semantica:Entity>" not in turtle+ assert "<semantica:related_to>" not in turtlediff --git a/tests/ontology/test_vocabulary.py b/tests/ontology/test_vocabulary.pynew file mode 100644index 00000000..49d2e794--- /dev/null+++ b/tests/ontology/test_vocabulary.py@@ -0,0 +1,85 @@+"""The vocabulary must stay true to what the exporters emit (issue #1107).++A vocabulary document that drifts from the code is worse than none, because it+states that terms mean something while the exporters emit different ones. These+tests tie the two together: every term the serializers can write must be+declared here, so adding a term to an exporter without declaring it fails the+build rather than shipping an undeclared IRI.+"""++import pytest++rdflib = pytest.importorskip("rdflib")++from semantica.export.rdf_exporter import ( # noqa: E402+ DEFAULT_ENTITY_TYPE,+ DEFAULT_RELATION_TYPE,+ SEMANTICA_NS,+)+from semantica.ontology.vocabulary import ( # noqa: E402+ NAMESPACE,+ vocabulary_path,+ vocabulary_turtle,+)++#: Every term the exporters emit in the Semantica namespace, by local name.+#: RDF and OWL-Time paths in export/rdf_exporter.py, document and relationship+#: terms in export/json_exporter.py, roles in provenance/manager.py.+EMITTED_TERMS = {+ "Entity",+ "Relationship",+ "text",+ "confidence",+ "metadata",+ "related_to",+ "source",+ "target",+ "type",+ "entities",+ "relationships",+ "exportedAt",+ "openEndedInterval",+ "role_generator",+}++[email protected](scope="module")+def graph():+ g = rdflib.Graph()+ g.parse(data=vocabulary_turtle(), format="turtle")+ return g+++def test_vocabulary_ships_with_the_package():+ assert vocabulary_path().is_file()+++def test_vocabulary_parses(graph):+ assert len(graph) > 0+++def test_namespace_matches_the_one_the_exporters_use():+ assert NAMESPACE == SEMANTICA_NS+++def test_every_emitted_term_is_declared(graph):+ declared = {+ str(s)[len(NAMESPACE) :]+ for s in set(graph.subjects())+ if isinstance(s, rdflib.URIRef) and str(s).startswith(NAMESPACE)+ }+ missing = EMITTED_TERMS - declared+ assert not missing, f"emitted but not declared in the vocabulary: {sorted(missing)}"+++def test_the_defaults_the_exporters_fall_back_to_are_declared(graph):+ for iri in (DEFAULT_ENTITY_TYPE, DEFAULT_RELATION_TYPE):+ assert (rdflib.URIRef(iri), None, None) in graph, f"{iri} is not declared"+++def test_every_declared_term_carries_a_label_and_a_comment(graph):+ for subject in set(graph.subjects()):+ if not (isinstance(subject, rdflib.URIRef) and str(subject).startswith(NAMESPACE)):+ continue+ assert graph.value(subject, rdflib.RDFS.label), f"{subject} has no rdfs:label"+ assert graph.value(subject, rdflib.RDFS.comment), f"{subject} has no rdfs:comment"