diff --git a/mempalace/knowledge_graph.py b/mempalace/knowledge_graph.py index a2f8b54..226c92d 100644 --- a/mempalace/knowledge_graph.py +++ b/mempalace/knowledge_graph.py @@ -87,7 +87,9 @@ class KnowledgeGraph: conn.close() def _conn(self): - return sqlite3.connect(self.db_path, timeout=10) + conn = sqlite3.connect(self.db_path, timeout=10) + conn.execute("PRAGMA journal_mode=WAL") + return conn def _entity_id(self, name: str) -> str: return name.lower().replace(" ", "_").replace("'", "") @@ -284,6 +286,7 @@ class KnowledgeGraph: JOIN entities o ON t.object = o.id WHERE (t.subject = ? OR t.object = ?) ORDER BY t.valid_from ASC NULLS LAST + LIMIT 100 """, (eid, eid), ).fetchall() diff --git a/tests/test_knowledge_graph.py b/tests/test_knowledge_graph.py index a8fcd9a..d7d9838 100644 --- a/tests/test_knowledge_graph.py +++ b/tests/test_knowledge_graph.py @@ -107,6 +107,23 @@ class TestTimeline: tl = kg.timeline() assert len(tl) == 100 # LIMIT 100 + def test_timeline_entity_has_limit(self, kg): + # Add > 100 triples all connected to a single entity + for i in range(105): + kg.add_triple( + "hub", "connects_to", f"spoke_{i}", valid_from=f"2025-01-{(i % 28) + 1:02d}" + ) + tl = kg.timeline("hub") + assert len(tl) == 100 # LIMIT 100 on entity-filtered branch + + +class TestWALMode: + def test_wal_mode_enabled(self, kg): + conn = kg._conn() + mode = conn.execute("PRAGMA journal_mode").fetchone()[0] + conn.close() + assert mode == "wal" + class TestStats: def test_stats_empty(self, kg):