Skip to content

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.

fix: keep parent predicates when clearing a narrower memory scope (#4327)

Vaibhav Hajare4c820e8d51c4
diff --git a/tests/test_memory/test_core_operations.py b/tests/test_memory/test_core_operations.py
index 29a639d74..ef9221b98 100644
--- a/tests/test_memory/test_core_operations.py
+++ b/tests/test_memory/test_core_operations.py
@@ -670,6 +670,39 @@ class TestClearScope:
remaining = await memory_system.count(MemoryFilter(user_id="alice", session_id="s2"))
assert remaining == 1
+ @pytest.mark.asyncio
+ async def test_clear_scope_keeps_a_surviving_turn_in_the_indexes(self, memory_system):
+ """Clearing one turn must not unindex the turn that survives (#4303).
+
+ The orchestrator collects the ids to remove from the indexes with a
+ query, then deletes the rows. If the query describes a wider scope
+ than the delete, a turn that is still stored loses its index entry and
+ stops being findable.
+ """
+ await memory_system.add_batch(
+ [
+ {
+ "content": "planner t1",
+ "user_id": "alice",
+ "agent_id": "planner",
+ "turn_id": "t1",
+ },
+ {
+ "content": "planner t2",
+ "user_id": "alice",
+ "agent_id": "planner",
+ "turn_id": "t2",
+ },
+ ]
+ )
+
+ count = await memory_system.clear_scope(user_id="alice", agent_id="planner", turn_id="t1")
+
+ assert count == 1
+ # t2's row survived, so it must still be findable through the indexes.
+ found = await memory_system.text_search("planner t2", user_id="alice")
+ assert [r.memory.content for r in found] == ["planner t2"]
+
@pytest.mark.asyncio
async def test_clear_scope_empty(self, memory_system):
"""Test clearing scope with no matching memories."""
diff --git a/tests/test_memory/test_hierarchical.py b/tests/test_memory/test_hierarchical.py
index 67d294822..0668533da 100644
--- a/tests/test_memory/test_hierarchical.py
+++ b/tests/test_memory/test_hierarchical.py
@@ -378,6 +378,42 @@ class TestSQLiteMemoryStore:
remaining = await store.query(MemoryFilter(user_id="alice"))
assert len(remaining) == 2
+ @pytest.mark.asyncio
+ async def test_clear_scope_narrows_to_the_turn_without_a_session(self, store):
+ """agent_id + turn_id without a session must scope to that turn (#4303)."""
+ await store.save_batch(
+ [
+ Memory(content="planner t1", user_id="alice", agent_id="planner", turn_id="t1"),
+ Memory(content="planner t2", user_id="alice", agent_id="planner", turn_id="t2"),
+ ]
+ )
+
+ deleted = await store.clear_scope("alice", agent_id="planner", turn_id="t1")
+
+ assert deleted == 1
+ remaining = await store.query(MemoryFilter(user_id="alice", agent_id="planner"))
+ assert [m.content for m in remaining] == ["planner t2"]
+ # The query has to describe the same scope the delete used.
+ assert (
+ len(await store.query(MemoryFilter(user_id="alice", agent_id="planner", turn_id="t1")))
+ == 0
+ )
+
+ @pytest.mark.asyncio
+ async def test_clear_scope_narrows_with_parent_scopes(self, store):
+ """A narrower scope must not delete rows outside its parents (#4303)."""
+ await store.save_batch(
+ [
+ Memory(content="s1 planner", user_id="alice", session_id="s1", agent_id="planner"),
+ Memory(content="s2 planner", user_id="alice", session_id="s2", agent_id="planner"),
+ ]
+ )
+
+ deleted = await store.clear_scope("alice", session_id="s1", agent_id="planner")
+
+ assert deleted == 1
+ assert len(await store.query(MemoryFilter(user_id="alice", session_id="s2"))) == 1
+
# =============================================================================
# LRU Cache Tests