Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.
Python agentaianthropicclaude-codecompressioncontext-engineeringcontext-windowcursorfastapilangchainllmmcpopenaiprompt-engineeringproxypythonragtoken-optimizationtokenstypescript
fix(memory/graph): apply relation_type filter to both sides of a BOTH subgraph query (#3236)
Abhay Singh1a6f94190803
diff --git a/headroom/memory/adapters/sqlite_graph.py b/headroom/memory/adapters/sqlite_graph.pyindex 8c74bde6f..720af9b5b 100644--- a/headroom/memory/adapters/sqlite_graph.py+++ b/headroom/memory/adapters/sqlite_graph.py@@ -485,8 +485,14 @@ class SQLiteGraphStore:rel_query = "SELECT * FROM relationships WHERE target_id = ?"rel_params = [current_id]else: # BOTH+ # The OR must be parenthesized: a later ``AND relation_type+ # IN (...)`` binds tighter than ``OR`` in SQL, so without+ # the parens the type filter would apply only to the+ # ``target_id`` (incoming) side, letting outgoing edges of+ # every type leak into the subgraph. (``get_relationships``+ # already groups this correctly.)rel_query = (- "SELECT * FROM relationships WHERE source_id = ? OR target_id = ?"+ "SELECT * FROM relationships WHERE (source_id = ? OR target_id = ?)")rel_params = [current_id, current_id]diff --git a/tests/test_sqlite_graph_store.py b/tests/test_sqlite_graph_store.pyindex b3db37e62..450fd84c3 100644--- a/tests/test_sqlite_graph_store.py+++ b/tests/test_sqlite_graph_store.py@@ -412,6 +412,46 @@ class TestSQLiteGraphStoreTraversal:assert "C" in entity_namesassert "A" in entity_names+ @pytest.mark.asyncio+ async def test_query_subgraph_both_direction_respects_relation_type_filter(self, tmp_path):+ """A relation_types filter must apply to BOTH the outgoing and incoming+ sides of a ``direction=BOTH`` traversal.++ Regression: the BOTH query was ``WHERE source_id = ? OR target_id = ?``+ without parentheses, then ``AND relation_type IN (...)`` was appended.+ SQL binds ``AND`` tighter than ``OR``, so the type filter applied only to+ the incoming (target_id) side, letting outgoing edges of every type leak+ into the subgraph.+ """+ store = SQLiteGraphStore(db_path=str(tmp_path / "graph.db"))+ x = Entity(user_id="u", name="X", entity_type="node")+ liked = Entity(user_id="u", name="Liked", entity_type="node")+ disliked = Entity(user_id="u", name="Disliked", entity_type="node")+ for entity in (x, liked, disliked):+ await store.add_entity(entity)+ # Two OUTGOING edges from X of different types.+ await store.add_relationship(+ Relationship(user_id="u", source_id=x.id, target_id=liked.id, relation_type="likes")+ )+ await store.add_relationship(+ Relationship(+ user_id="u", source_id=x.id, target_id=disliked.id, relation_type="dislikes"+ )+ )++ subgraph = await store.query_subgraph(+ [x.id],+ max_hops=1,+ direction=RelationshipDirection.BOTH,+ relation_types=["likes"],+ )++ types = {r.relation_type for r in subgraph.relationships}+ assert types == {"likes"}, f"type filter leaked non-'likes' edges: {types}"+ names = {e.name for e in subgraph.entities}+ assert "Disliked" not in names+ assert names == {"X", "Liked"}+@pytest.mark.asyncioasync def test_find_path_direct(self, store_with_graph):"""Test finding a direct path."""