Merge pull request #884 from shafdev/fix/kg-missing-lock

fix: add missing self._lock to query_relationship, timeline, and stats in KnowledgeGraph
This commit is contained in:
Igor Lins e Silva
2026-04-14 16:47:24 -03:00
committed by GitHub
+4 -1
View File
@@ -260,7 +260,6 @@ class KnowledgeGraph:
def query_relationship(self, predicate: str, as_of: str = None): def query_relationship(self, predicate: str, as_of: str = None):
"""Get all triples with a given relationship type.""" """Get all triples with a given relationship type."""
pred = predicate.lower().replace(" ", "_") pred = predicate.lower().replace(" ", "_")
conn = self._conn()
query = """ query = """
SELECT t.*, s.name as sub_name, o.name as obj_name SELECT t.*, s.name as sub_name, o.name as obj_name
FROM triples t FROM triples t
@@ -274,6 +273,8 @@ class KnowledgeGraph:
params.extend([as_of, as_of]) params.extend([as_of, as_of])
results = [] results = []
with self._lock:
conn = self._conn()
for row in conn.execute(query, params).fetchall(): for row in conn.execute(query, params).fetchall():
results.append( results.append(
{ {
@@ -289,6 +290,7 @@ class KnowledgeGraph:
def timeline(self, entity_name: str = None): def timeline(self, entity_name: str = None):
"""Get all facts in chronological order, optionally filtered by entity.""" """Get all facts in chronological order, optionally filtered by entity."""
with self._lock:
conn = self._conn() conn = self._conn()
if entity_name: if entity_name:
eid = self._entity_id(entity_name) eid = self._entity_id(entity_name)
@@ -329,6 +331,7 @@ class KnowledgeGraph:
# ── Stats ───────────────────────────────────────────────────────────── # ── Stats ─────────────────────────────────────────────────────────────
def stats(self): def stats(self):
with self._lock:
conn = self._conn() conn = self._conn()
entities = conn.execute("SELECT COUNT(*) as cnt FROM entities").fetchone()["cnt"] entities = conn.execute("SELECT COUNT(*) as cnt FROM entities").fetchone()["cnt"]
triples = conn.execute("SELECT COUNT(*) as cnt FROM triples").fetchone()["cnt"] triples = conn.execute("SELECT COUNT(*) as cnt FROM triples").fetchone()["cnt"]