Skip to content

Graph-Native Infrastructure for Context and Accountable AI Systems

fixed qodo reviews

Sameer6305fd84be8b6660
diff --git a/semantica/cli.py b/semantica/cli.py
index c66d383f..2523e2e2 100644
--- a/semantica/cli.py
+++ b/semantica/cli.py
@@ -2608,6 +2608,10 @@ def provenance_check(cli_ctx: CLIContext, strict: bool, local_json: bool) -> Non
_jecho(result if isinstance(result, dict) else {"valid": bool(result)})
else:
_ok(cli_ctx, f"Provenance check: {result}")
+ if strict and isinstance(result, dict) and not result.get("valid", True):
+ raise click.ClickException(
+ f"Provenance integrity check failed: {result.get('errors')} error(s)"
+ )
_run_with_error_handling(_action)
diff --git a/semantica/provenance/manager.py b/semantica/provenance/manager.py
index 2cd1cca7..3ec7c6cb 100644
--- a/semantica/provenance/manager.py
+++ b/semantica/provenance/manager.py
@@ -235,7 +235,8 @@ class ProvenanceManager:
metadata=metadata or {},
first_seen=existing.first_seen if existing else datetime.utcnow().isoformat(),
last_updated=datetime.utcnow().isoformat(),
- parent_entity_id=parent_id # Link to history or explicit parent
+ parent_entity_id=parent_id, # Link to history or explicit parent
+ used_entities=kwargs.get("used_entities", []),
)
# Make the archived history entry discoverable via trace_lineage()'s
@@ -776,10 +777,17 @@ class ProvenanceManager:
g.add((act_uri, RDF.type, PROV.Activity))
g.add((ent_uri, PROV.wasGeneratedBy, act_uri))
- for parent_id in getattr(e, "derived_from", []):
- p_uri = URIRef(EX[str(parent_id)])
+ if getattr(e, "parent_entity_id", None):
+ p_uri = URIRef(EX[str(e.parent_entity_id)])
g.add((ent_uri, PROV.wasDerivedFrom, p_uri))
+ for u_id in getattr(e, "used_entities", []):
+ u_uri = URIRef(EX[str(u_id)])
+ g.add((ent_uri, PROV.wasDerivedFrom, u_uri))
+ if getattr(e, "activity_id", None) and e.activity_id != "unknown":
+ act_uri = URIRef(EX[str(e.activity_id)])
+ g.add((act_uri, PROV.used, u_uri))
+
rdf_format = "json-ld" if format == "jsonld" else format
return g.serialize(format=rdf_format)
@@ -798,17 +806,21 @@ class ProvenanceManager:
missing_refs = []
for e in entries:
- for p in getattr(e, "derived_from", []):
- if p not in all_ids:
- missing_refs.append(f"{e.entity_id} -> {p}")
+ if getattr(e, "parent_entity_id", None):
+ if e.parent_entity_id not in all_ids:
+ missing_refs.append(f"{e.entity_id} -> {e.parent_entity_id}")
+ for u_id in getattr(e, "used_entities", []):
+ if u_id not in all_ids:
+ missing_refs.append(f"{e.entity_id} -> {u_id}")
- valid = len(missing_refs) == 0 if strict else True
+ valid = len(missing_refs) == 0
+ errors = len(missing_refs)
return {
"valid": valid,
"total_entries": len(entries),
"missing_references": missing_refs,
"strict": strict,
- "errors": len(missing_refs) if strict else 0,
+ "errors": errors,
}
diff --git a/tests/provenance/test_manager.py b/tests/provenance/test_manager.py
index 6206284f..d74521d3 100644
--- a/tests/provenance/test_manager.py
+++ b/tests/provenance/test_manager.py
@@ -518,16 +518,42 @@ class TestProvenanceManager:
def test_cli_export_prov(self):
"""Test CLI export_prov method on ProvenanceManager."""
prov_mgr = ProvenanceManager()
- prov_mgr.track_entity("e_rdf", source="doc_rdf")
+ prov_mgr.track_entity("e_parent", source="doc_rdf")
+ prov_mgr.track_entity(
+ "e_child",
+ source="doc_rdf",
+ parent_entity_id="e_parent",
+ used_entities=["e_parent"],
+ activity_id="act_transform",
+ )
ttl = prov_mgr.export_prov(format="turtle")
assert "prov:Entity" in ttl or "http://www.w3.org/ns/prov#Entity" in ttl
+ assert "wasDerivedFrom" in ttl
+ assert "used" in ttl
def test_cli_check(self):
"""Test CLI check integrity method on ProvenanceManager."""
prov_mgr = ProvenanceManager()
- prov_mgr.track_entity("e_valid", source="doc_1")
+ prov_mgr.track_entity("e_valid_parent", source="doc_1")
+ prov_mgr.track_entity(
+ "e_valid_child",
+ source="doc_1",
+ parent_entity_id="e_valid_parent",
+ used_entities=["e_valid_parent"],
+ )
check_res = prov_mgr.check(strict=True)
assert check_res["valid"] is True
- assert check_res["total_entries"] >= 1
+ assert check_res["total_entries"] >= 2
assert check_res["errors"] == 0
+ # Confirm non-strict mode still reports valid=False and errors > 0 when references are missing
+ prov_mgr.track_entity(
+ "e_broken_child",
+ source="doc_1",
+ parent_entity_id="non_existent_parent",
+ )
+ check_broken = prov_mgr.check(strict=False)
+ assert check_broken["valid"] is False
+ assert check_broken["errors"] >= 1
+ assert "e_broken_child -> non_existent_parent" in check_broken["missing_references"]
+