Skip to content

Plan, apply and roll back changes to a production ontology, with a blast radius report and a proof an auditor can re-check.

Record alignment labels only after native insertion succeeds

Rudy Celeklibaba4f076d8a
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6646495d..a38268b8 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,8 @@ All notable changes to Open Ontologies are documented here.
### Fixed
+- Keep Python alignment labels and query counts consistent when HNSW insertion rejects a full index or an invalid numeric vector.
+
- Keep the last active ontology intact when parsing a replacement source, cache, or refresh fails; publish successful replacements in one graph transaction.
- Bound wildcard repository filtering to pattern/filename pairs so repeated stars cannot stall `onto_repo_list`.
- Sanitize quotes and ASCII control bytes in mapped IRI components so valid structured data does not fail RDF ingestion.
diff --git a/python/src/open_ontologies_lite/align.py b/python/src/open_ontologies_lite/align.py
index 75f37369..777b6e2e 100644
--- a/python/src/open_ontologies_lite/align.py
+++ b/python/src/open_ontologies_lite/align.py
@@ -60,8 +60,8 @@ class AlignmentIndex:
if len(vector) != self.dim:
raise ValueError(f"vector length {len(vector)} != index dim {self.dim}")
label = len(self._ids)
- self._ids.append(id)
self._index.add_items([list(vector)], [label])
+ self._ids.append(id)
self._built = True
return label
diff --git a/python/tests/test_align_failed_insertion.py b/python/tests/test_align_failed_insertion.py
new file mode 100644
index 00000000..6f212950
--- /dev/null
+++ b/python/tests/test_align_failed_insertion.py
@@ -0,0 +1,47 @@
+import pytest
+pytest.importorskip("hnswlib")
+from open_ontologies_lite.align import AlignmentIndex
+
+def state(index):
+ result = {"length": len(index), "native_count": index._index.get_current_count()}
+ try:
+ result["ids"] = sorted(candidate.id for candidate in index.query([1., 0.], k=10))
+ except RuntimeError as exc:
+ result["query_error"] = type(exc).__name__
+ return result
+
[email protected]("failure", ["capacity", "numeric"])
+def test_failed_native_insertion_keeps_existing_index_usable(failure):
+ index = AlignmentIndex(2, max_elements=1 if failure == "capacity" else 3)
+ index.add("first", [1., 0.])
+ with pytest.raises(RuntimeError if failure == "capacity" else ValueError):
+ index.add("rejected", [0., 1.] if failure == "capacity" else ["not-a-number", 0.])
+ assert state(index) == {"length": 1, "native_count": 1, "ids": ["first"]}
+
+def test_valid_insertion_after_rejection_has_no_ghost_label():
+ index = AlignmentIndex(2, max_elements=3)
+ index.add("first", [1., 0.])
+ with pytest.raises(ValueError):
+ index.add("rejected", ["not-a-number", 0.])
+ label = index.add("second", [0., 1.])
+ assert {"label": label, **state(index)} == {"label": 1, "length": 2, "native_count": 2, "ids": ["first", "second"]}
+
+def test_normal_add_and_query_control():
+ index = AlignmentIndex(2, max_elements=2)
+ assert index.add("first", [1., 0.]) == 0
+ assert state(index) == {"length": 1, "native_count": 1, "ids": ["first"]}
+
+def test_empty_index_control():
+ index = AlignmentIndex(2)
+ assert len(index) == 0 and index.query([1., 0.]) == []
+
+
+def test_first_rejected_insertion_keeps_empty_index_and_build_state():
+ index = AlignmentIndex(2, max_elements=2)
+ with pytest.raises(ValueError):
+ index.add("rejected", ["not-a-number", 0.])
+ assert index._built is False
+ assert state(index) == {"length": 0, "native_count": 0, "ids": []}
+ assert index.add("first", [1., 0.]) == 0
+ assert index._built is True
+ assert state(index) == {"length": 1, "native_count": 1, "ids": ["first"]}