The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(memory): a one-item remove batch can delete the last saved fact
Tekniumfaef4e0ab8a1
diff --git a/tests/tools/test_memory_tool.py b/tests/tools/test_memory_tool.pyindex 386ccd5f88a..a149af47e03 100644--- a/tests/tools/test_memory_tool.py+++ b/tests/tools/test_memory_tool.py@@ -895,10 +895,12 @@ class TestBatchRefusesToEmptyNonEmptyStore:self, store, target, seed):assert store.add(target, seed)["success"] is True+ assert store.add(target, "second entry here")["success"] is Truepath = store._path_for(target)before = path.read_text(encoding="utf-8")- result = store.apply_batch(target, [{"action": "remove", "old_text": seed}])+ result = store.apply_batch(target, [{"action": "remove", "old_text": seed},+ {"action": "remove", "old_text": "second entry"}])assert result["success"] is Falseassert "current_entries" not in result # batch abort never echoes the store (#97316)diff --git a/tests/tools/test_memory_tool_schema.py b/tests/tools/test_memory_tool_schema.pyindex 4caba4b8954..ddfdb027b2a 100644--- a/tests/tools/test_memory_tool_schema.py+++ b/tests/tools/test_memory_tool_schema.py@@ -50,3 +50,21 @@ def test_each_memory_field_is_advertised_once_and_legacy_shapes_still_write(tmp_batch = {"target": "memory", "operations": [{"action": "patch", "old_text": "alpha", "new_text": "beta"}]}assert json.loads(handler(batch, store=store))["success"]assert store.memory_entries == ["beta fact"]+++def test_advertised_one_item_remove_deletes_the_last_entry_but_a_multi_op_batch_cannot_empty(tmp_path, monkeypatch):+ """operations[] is the only advertised shape, so a one-item remove is how the agent forgets+ the final fact; a larger batch that would empty the store is still refused (#103419)."""+ monkeypatch.setattr("tools.memory_tool.get_memory_dir", lambda: tmp_path)+ store = MemoryStore(memory_char_limit=500, user_char_limit=300)+ store.load_from_disk()+ handler = registry.get_entry("memory").handler+ for fact in ("alpha fact", "beta fact"):+ assert json.loads(handler({"target": "user", "operations": [{"action": "add", "content": fact}]}, store=store))["success"]+ both = [{"action": "remove", "old_text": "alpha"}, {"action": "remove", "old_text": "beta"}]+ refused = json.loads(handler({"target": "user", "operations": both}, store=store))+ assert not refused["success"] and "one-item" in refused["error"]+ assert store.user_entries == ["alpha fact", "beta fact"]+ for old in ("alpha", "beta"):+ assert json.loads(handler({"target": "user", "operations": [{"action": "remove", "old_text": old}]}, store=store))["success"]+ assert store.user_entries == []diff --git a/tools/memory_tool_store.py b/tools/memory_tool_store.pyindex 3666bf9fd56..ca50b4349b3 100644--- a/tools/memory_tool_store.py+++ b/tools/memory_tool_store.py@@ -580,16 +580,18 @@ class MemoryStore:matched.append(previous_content)if after is not None:produced[i + 1] = after- if entries and not working:+ if entries and not working and not (len(ops) == 1 and ops[0].get("action") == "remove"):# #103419: a consolidation batch that removes the last entry would- # commit an empty file as a normal successful write. Refuse; single- # remove() is the deliberate-wipe path.+ # commit an empty file as a normal successful write. Refuse; a batch of+ # exactly one remove is the deliberate single delete (operations[] is the+ # memory tool's only advertised shape), and unattended forks never get+ # that far: the #105921 delete gate stages their removes.label = self._path_for(target).namereturn self._batch_failure(target, (f"Refusing to empty {label}: this batch would remove every entry from a "f"previously non-empty store. Keep at least one entry — merge overlapping "f"entries into a shorter one instead of removing the last one. To delete the "- f"final entry deliberately, use single remove() calls."), "would_empty")+ f"final entry deliberately, remove it alone in a one-item batch."), "would_empty")new_total = len(ENTRY_DELIMITER.join(working)) # budget check against the FINAL state onlyif new_total > limit:return self._batch_failure(target, (