Skip to content

Graph-Native Infrastructure for Context and Accountable AI Systems

fix(explorer): resolve ontology ownership on the backend for entity deep links (#1439)

Wei Taoa9f1b29292e2
diff --git a/explorer/src/workspaces/OntologyWorkspace/OntologyEditor.tsx b/explorer/src/workspaces/OntologyWorkspace/OntologyEditor.tsx
index 043d4678..f09ec164 100644
--- a/explorer/src/workspaces/OntologyWorkspace/OntologyEditor.tsx
+++ b/explorer/src/workspaces/OntologyWorkspace/OntologyEditor.tsx
@@ -28,9 +28,9 @@ import { loadOntologyEntityOwner, loadOntologyGraph } from "./api";
import type { OntologyGraphEdge, OntologyGraphNode } from "./api";
import {
classifyNodeType,
- inferOntologyUri,
isEditableEntityType,
ONTOLOGY_MINIMAP_THEME,
+ resolveEditorOntology,
} from "./ontologyEditorModel";
import type { EditorEntityType, RegistryEntry } from "./ontologyEditorModel";
import { clearEntitySelection, readOntologyUrlState, writeEntitySelection } from "./ontologyUrlState";
@@ -222,6 +222,7 @@ export function OntologyEditor() {
const [flowInstance, setFlowInstance] = useState<ReactFlowInstance<OntologyNode, OntologyEdge> | null>(null);
const [isLoadingGraph, setIsLoadingGraph] = useState(false);
const [graphError, setGraphError] = useState("");
+ const [unownedEntity, setUnownedEntity] = useState("");
const [draftDiff, setDraftDiff] = useState<DraftDiff>({
added_classes: [],
removed_classes: [],
@@ -247,11 +248,21 @@ export function OntologyEditor() {
? loadOntologyEntityOwner(requested).catch(() => undefined)
: Promise.resolve(undefined),
])
- .then(([entries, explicitOwner]: [RegistryEntry[], string | undefined]) => {
+ .then(([entries, ownerVerdict]: [RegistryEntry[], string | null | undefined]) => {
if (cancelled) return;
setRegistry(entries);
- const inferredOntology = inferOntologyUri(entries, requested, explicitOwner);
- setOntologyUri((current) => current || inferredOntology || entries[0]?.uri || "");
+ const resolution = resolveEditorOntology(entries, requested, ownerVerdict);
+ // The registry default is the right landing place for "no entity asked
+ // for", but not for "the backend says nothing owns the entity that was
+ // asked for" — that would open an arbitrary ontology whose graph
+ // excludes the entity, and report nothing about why.
+ if (resolution.status === "unowned") {
+ setUnownedEntity(resolution.entityUri);
+ return;
+ }
+ setUnownedEntity("");
+ const resolvedOntology = resolution.status === "resolved" ? resolution.uri : undefined;
+ setOntologyUri((current) => current || resolvedOntology || entries[0]?.uri || "");
})
.catch((error) => {
console.error("Failed to load ontology registry:", error);
@@ -544,6 +555,7 @@ export function OntologyEditor() {
onChange={(event) => {
setOntologyUri(event.target.value);
setSelectedElement(null);
+ setUnownedEntity("");
clearEntitySelection();
}}
style={selectStyle}
@@ -615,7 +627,12 @@ export function OntologyEditor() {
{!isLoadingGraph && graphError && (
<div style={{ ...canvasMessageStyle, color: "#ff9a8d" }}>{graphError}</div>
)}
- {!isLoadingGraph && !graphError && ontologyUri && nodes.length === 0 && (
+ {!isLoadingGraph && !graphError && unownedEntity && (
+ <div style={{ ...canvasMessageStyle, color: "#f2b66d" }}>
+ No registered ontology owns {unownedEntity}. Pick an ontology above to start editing.
+ </div>
+ )}
+ {!isLoadingGraph && !graphError && !unownedEntity && ontologyUri && nodes.length === 0 && (
<div style={canvasMessageStyle}>This ontology has no editable classes or properties.</div>
)}
diff --git a/explorer/src/workspaces/OntologyWorkspace/api.ts b/explorer/src/workspaces/OntologyWorkspace/api.ts
index 520f45f7..b14897ae 100644
--- a/explorer/src/workspaces/OntologyWorkspace/api.ts
+++ b/explorer/src/workspaces/OntologyWorkspace/api.ts
@@ -32,7 +32,11 @@ export type OntologyGraphResponse = {
};
export type OntologyEntityOwner = {
- source_ontology?: string;
+ // Optional on purpose, unlike OntologyGraphNode.entity_type. There, a missing
+ // field degrades to a read-only node — benign. Here it would be read as an
+ // authoritative "nothing owns this entity", which now suppresses selection
+ // outright, so presence has to be checked rather than assumed.
+ owning_ontology?: string | null;
};
async function parseResponse<T>(response: Response): Promise<T> {
@@ -63,10 +67,21 @@ export async function loadOntologyGraph(uri: string, signal?: AbortSignal): Prom
);
}
-export async function loadOntologyEntityOwner(uri: string): Promise<string | undefined> {
+// Three-state verdict: a string names the owner, null is the backend's
+// authoritative "no known ontology owns this entity", and undefined means the
+// request failed so there is no verdict to act on.
+export type OntologyOwnerVerdict = string | null | undefined;
+
+export async function loadOntologyEntityOwner(uri: string): Promise<OntologyOwnerVerdict> {
const response = await fetch(`/api/ontology/entity/${encodeURIComponent(uri)}`);
if (!response.ok) return undefined;
- return (await response.json() as OntologyEntityOwner).source_ontology;
+ const owner = await response.json() as OntologyEntityOwner | null;
+ // Only a field that is actually there carries the verdict. Coercing an absent
+ // field to null would assert the strongest available claim — "nothing owns
+ // this" — on the weakest possible evidence, and that claim now stops the
+ // editor selecting an ontology at all.
+ const verdict = owner?.owning_ontology;
+ return verdict === undefined ? undefined : verdict;
}
export async function loadAlignments(uri?: string): Promise<OntologyAlignment[]> {
diff --git a/explorer/src/workspaces/OntologyWorkspace/ontologyEditorModel.ts b/explorer/src/workspaces/OntologyWorkspace/ontologyEditorModel.ts
index 75eadf12..4b82a54a 100644
--- a/explorer/src/workspaces/OntologyWorkspace/ontologyEditorModel.ts
+++ b/explorer/src/workspaces/OntologyWorkspace/ontologyEditorModel.ts
@@ -45,6 +45,10 @@ export function classifyNodeType(rawType: string): EditorEntityType {
return "external";
}
+// Last-resort guess, reached only when the backend gave no verdict: it has no
+// notion of nested vocabularies, so it can name a parent that does not contain
+// the entity. Authority is owning_ontology from /api/ontology/entity
+// (_resolve_owning_ontology in semantica/explorer/routes/ontology.py).
function ownsByNamespace(entityUri: string, ontologyUri: string): boolean {
const stem = ontologyUri.replace(/[/#]+$/, "");
return entityUri === ontologyUri
@@ -52,12 +56,50 @@ function ownsByNamespace(entityUri: string, ontologyUri: string): boolean {
|| entityUri.startsWith(`${stem}/`);
}
+/**
+ * Three outcomes, deliberately not collapsed into `string | undefined`.
+ *
+ * `unowned` and `unresolved` both yield "no ontology to open", but they must
+ * not be treated alike: a caller that writes `resolve(...) || entries[0]` turns
+ * the backend's authoritative "nothing owns this entity" into "open an
+ * arbitrary ontology", which reintroduces the parent-selection bug this whole
+ * verdict exists to prevent. The union makes that collapse a type error.
+ */
+export type EditorOntologyResolution =
+ | { status: "resolved"; uri: string }
+ | { status: "unowned"; entityUri: string }
+ | { status: "unresolved" };
+
+// Picks the registered ontology to open for a deep-linked entity. A null
+// verdict is the backend's authoritative "nothing owns this entity": the
+// namespace guess must stay suppressed, or an unregistered nested namespace
+// would select its registered parent again. Only an unavailable verdict
+// (undefined) may fall back to inference.
+export function resolveEditorOntology(
+ entries: RegistryEntry[],
+ entityUri: string,
+ ownerVerdict: string | null | undefined,
+): EditorOntologyResolution {
+ if (ownerVerdict === null) {
+ return { status: "unowned", entityUri };
+ }
+ const uri = inferOntologyUri(entries, entityUri, ownerVerdict);
+ return uri === undefined ? { status: "unresolved" } : { status: "resolved", uri };
+}
+
+// Picks the registered ontology to open for an entity: the backend-resolved
+// explicitOwner wins outright, the namespace guess is only the fallback.
export function inferOntologyUri(
entries: RegistryEntry[],
entityUri: string,
explicitOwner?: string,
): string | undefined {
- if (explicitOwner && entries.some((entry) => entry.uri === explicitOwner)) {
+ // Trusted even when the registry does not list it. Falling through to the
+ // namespace guess here would answer a question nobody asked — the backend
+ // named this entity's owner, and silently opening a *different* ontology is
+ // worse than opening one the registry has not been told about yet, which
+ // surfaces as an explicit error from /api/ontology/graph.
+ if (explicitOwner) {
return explicitOwner;
}
return [...entries]
diff --git a/explorer/tests/ontologyEditorModel.test.ts b/explorer/tests/ontologyEditorModel.test.ts
index a4e1e84f..0ca94786 100644
--- a/explorer/tests/ontologyEditorModel.test.ts
+++ b/explorer/tests/ontologyEditorModel.test.ts
@@ -6,6 +6,7 @@ import {
compactNodeType,
inferOntologyUri,
isEditableEntityType,
+ resolveEditorOntology,
ONTOLOGY_MINIMAP_THEME,
} from "../src/workspaces/OntologyWorkspace/ontologyEditorModel";
@@ -29,6 +30,20 @@ test("explicit scheme ownership wins when an entity uses another namespace", ()
);
});
+test("an explicit owner missing from the registry is used, not quietly replaced", () => {
+ // The entity sits under a registered namespace, so the guess has an answer
+ // ready; the backend naming a different, unregistered owner must still win,
+ // or the editor opens an ontology nobody said owned this entity.
+ assert.equal(
+ inferOntologyUri(
+ registry,
+ "https://example.test/foo#Class",
+ "https://unregistered.test/vocab",
+ ),
+ "https://unregistered.test/vocab",
+ );
+});
+
test("only draft-supported class and property nodes are editable", () => {
assert.equal(isEditableEntityType("class"), true);
assert.equal(isEditableEntityType("property"), true);
@@ -64,3 +79,36 @@ test("compactNodeType leaves unknown namespaces untouched", () => {
assert.equal(compactNodeType("https://example.org/custom#Thing"), "https://example.org/custom#Thing");
assert.equal(compactNodeType("owl:Class"), "owl:Class");
});
+
+test("an authoritative no-owner verdict suppresses the namespace guess", () => {
+ // Without suppression the prefix guess would pick the registered parent
+ // for an unregistered nested entity — the deep link must not do that.
+ const nested = "https://example.test/foo/unregistered#Term";
+ assert.deepEqual(resolveEditorOntology(registry, nested, null), {
+ status: "unowned",
+ entityUri: nested,
+ });
+ // An unavailable verdict may still fall back to inference
+ assert.deepEqual(
+ resolveEditorOntology(registry, "https://example.test/foo#Class", undefined),
+ { status: "resolved", uri: "https://example.test/foo" },
+ );
+ // A named owner wins outright
+ assert.deepEqual(
+ resolveEditorOntology(registry, nested, "https://example.test/foo/nested"),
+ { status: "resolved", uri: "https://example.test/foo/nested" },
+ );
+});
+
+test("unowned is distinguishable from unresolved, so neither collapses to a default", () => {
+ // Both mean "no ontology to open", and the editor treats them oppositely:
+ // unresolved may land on the registry default, unowned must not. A caller
+ // writing `resolve(...) || entries[0]` reintroduced exactly the parent
+ // selection this verdict exists to prevent, so the difference is typed.
+ const unowned = resolveEditorOntology(registry, "https://example.test/foo/x#T", null);
+ const unresolved = resolveEditorOntology(registry, "https://elsewhere.test/T", undefined);
+
+ assert.equal(unowned.status, "unowned");
+ assert.equal(unresolved.status, "unresolved");
+ assert.notEqual(unowned.status, unresolved.status);
+});
diff --git a/semantica/explorer/routes/ontology.py b/semantica/explorer/routes/ontology.py
index 66fb6efb..3e9e8342 100644
--- a/semantica/explorer/routes/ontology.py
+++ b/semantica/explorer/routes/ontology.py
@@ -244,6 +244,7 @@ class EntityDetailResponse(BaseModel):
entity_type: str
definition: Optional[str] = None
source_ontology: Optional[str] = None
+ owning_ontology: Optional[str] = None
superclasses: List[str] = Field(default_factory=list)
subclasses: List[str] = Field(default_factory=list)
domain: List[str] = Field(default_factory=list)
@@ -814,6 +815,55 @@ def _node_belongs_to_ontology(
return "#" not in local_name and "/" not in local_name
+def _resolve_owning_ontology(
+ node: Dict[str, Any],
+ known_ontology_uris: set[str],
+) -> Optional[str]:
+ """Return the one known ontology that owns this node, or None if none does.
+
+ The most specific (longest) match wins, so a nested vocabulary claims its
+ own terms instead of the parent absorbing them.
+
+ Kept agreeing with _node_belongs_to_ontology by construction — the same
+ three rules in the same order — but in one pass over the candidates rather
+ than one pass per candidate, each of which rescanned the whole set to find
+ the longest namespace. That made resolution quadratic in the number of
+ registered ontologies.
+ """
+ nid = str(node.get("id", ""))
+ if not nid:
+ return None
+
+ # An ontology node owns itself, ahead of any scheme_uri it may carry.
+ if nid in known_ontology_uris:
+ return nid
+
+ # An explicit owner is authoritative even when it is not registered:
+ # naming a different ontology by namespace guess would be worse than
+ # reporting the one the node itself points at.
+ explicit_owner = _node_source_ontology(node)
+ if explicit_owner:
+ return explicit_owner
+
+ longest_namespace: Optional[str] = None
+ for candidate in known_ontology_uris:
+ stem = candidate.rstrip("#/")
+ if not nid.startswith((stem + "#", stem + "/")):
+ continue
+ if longest_namespace is None or len(candidate) > len(longest_namespace):
+ longest_namespace = candidate
+ if longest_namespace is None:
+ return None
+
+ # Prefix ownership only extends to names minted directly in the namespace.
+ # A further delimiter marks a nested vocabulary, which stays unowned until
+ # it is registered or carries an explicit owner.
+ local_name = nid[len(longest_namespace.rstrip("#/")) + 1 :]
+ if "#" in local_name or "/" in local_name:
+ return None
+ return longest_namespace
+
+
def _is_ontology_entity(node: Dict[str, Any]) -> bool:
return _classify_node_type(node.get("type", "")) in {"class", "property", "concept", "scheme"}
@@ -1952,6 +2002,7 @@ async def get_ontology_graph(
@router.get("/entity/{entity_uri:path}", response_model=EntityDetailResponse)
async def get_entity_detail(
entity_uri: str,
+ request: Request,
session: GraphSession = Depends(get_session),
):
node = await asyncio.to_thread(session.get_node, entity_uri)
@@ -1973,12 +2024,21 @@ async def get_entity_detail(
all_nodes, _ = await asyncio.to_thread(session.get_nodes, skip=0, limit=999_999)
instance_count = sum(1 for n in all_nodes if n.get("type") == entity_uri)
+ # Ownership must use the same candidate set as /graph, so it goes through the
+ # same helper rather than being derived from all_nodes above: that scan is
+ # capped at 999,999, and on a larger graph a truncated set would silently
+ # drop ontologies and make the two endpoints disagree about who owns a node.
+ # The helper iterates only the ontology node types, so it is not a full scan.
+ known_ontology_uris = await asyncio.to_thread(
+ _known_ontology_uris, session, _get_registry(request)
+ )
return EntityDetailResponse(
uri=entity_uri, label=label,
type=ntype, entity_type=_classify_node_type(ntype),
definition=definition,
source_ontology=props.get("scheme_uri"),
+ owning_ontology=_resolve_owning_ontology(node, known_ontology_uris),
superclasses=superclasses, subclasses=subclasses,
domain=domain, range=range_,
instance_count=instance_count, properties=props,
diff --git a/tests/explorer/test_ontology_subissue3.py b/tests/explorer/test_ontology_subissue3.py
index 6294c944..e7116ffd 100644
--- a/tests/explorer/test_ontology_subissue3.py
+++ b/tests/explorer/test_ontology_subissue3.py
@@ -17,6 +17,7 @@ from semantica.explorer.routes.ontology import ( # noqa: E402
OntologyEntry,
_convert_ontology_to_graph,
_node_belongs_to_ontology,
+ _resolve_owning_ontology,
)
from semantica.explorer.session import GraphSession # noqa: E402
@@ -259,6 +260,89 @@ def test_node_belongs_to_ontology_nested_namespace_matrix():
assert _node_belongs_to_ontology(node(f"{child}/Term"), child, {parent, child})
+def test_entity_detail_reports_explicit_owner(client):
+ response = client.get(
+ f"/api/ontology/entity/{quote('http://example.org/onto-a#Person', safe='')}"
+ )
+
+ assert response.status_code == 200
+ payload = response.json()
+ assert payload["source_ontology"] == "http://example.org/onto-a"
+ assert payload["owning_ontology"] == "http://example.org/onto-a"
+
+
+def test_entity_detail_reports_namespace_owner_without_explicit_scheme(client):
+ graph = client.app.state.session.graph
+ minted_directly = "http://example.org/onto-a#Address"
+ graph.add_node(minted_directly, node_type="owl:Class", content="Address")
+
+ response = client.get(f"/api/ontology/entity/{quote(minted_directly, safe='')}")
+
+ assert response.status_code == 200
+ assert response.json()["owning_ontology"] == "http://example.org/onto-a"
+
+
+def test_entity_detail_reports_no_owner_for_unregistered_nested_namespace(client):
+ graph = client.app.state.session.graph
+ nested_term = "http://example.org/onto-a/nested#Term"
+ graph.add_node(nested_term, node_type="owl:Class", content="Nested Term")
+
+ response = client.get(f"/api/ontology/entity/{quote(nested_term, safe='')}")
+
+ assert response.status_code == 200
+ # onto-a must not claim a nested vocabulary its own /graph response
+ # excludes, or a deep link selects onto-a and then finds nothing to select.
+ assert response.json()["owning_ontology"] is None
+
+
+def test_entity_detail_reports_an_explicit_owner_outside_the_registry(client):
+ graph = client.app.state.session.graph
+ borrowed = "http://example.org/onto-a#Borrowed"
+ unregistered_owner = "http://unregistered.example/vocab"
+ # Sits directly in onto-a's namespace, so the namespace rule has an answer
+ # ready — the node's own scheme_uri still has to win, or /entity reports an
+ # owner that contradicts the node and the editor opens the wrong ontology.
+ graph.add_node(
+ borrowed,
+ node_type="owl:Class",
+ content="Borrowed",
+ scheme_uri=unregistered_owner,
+ )
+
+ response = client.get(f"/api/ontology/entity/{quote(borrowed, safe='')}")
+
+ assert response.status_code == 200
+ assert response.json()["owning_ontology"] == unregistered_owner
+
+
+def test_owner_resolution_agrees_with_graph_membership(client):
+ """_resolve_owning_ontology and _node_belongs_to_ontology must not diverge.
+
+ The two answer the same question from opposite directions, and /entity and
+ /graph each use one of them. If they disagree, a deep link opens an ontology
+ whose graph then excludes the entity it was opened for.
+ """
+ parent = "http://example.org/onto-a"
+ nested = "http://example.org/onto-a/nested"
+ known = {parent, nested}
+ cases = [
+ {"id": parent},
+ {"id": f"{parent}#Direct"},
+ {"id": f"{parent}/Direct"},
+ {"id": f"{nested}#Term"},
+ {"id": f"{parent}/unregistered#Term"},
+ {"id": "http://elsewhere.example/Thing"},
+ {"id": f"{parent}#Explicit", "properties": {"scheme_uri": nested}},
+ ]
+
+ for node in cases:
+ owner = _resolve_owning_ontology(node, known)
+ for candidate in known:
+ assert _node_belongs_to_ontology(node, candidate, known) == (
+ owner == candidate
+ ), f"{node['id']} vs {candidate}: owner={owner}"
+
+
def test_load_fallback_import_without_declaration_is_editable(client):
turtle = """
@prefix ex: <http://data.example.org/people#> .