Skip to content

The agent that grows with you

fix(memory): a one-item remove or duplicate add is not refused on an over-cap store

Tekniumb044f7cf28bd
diff --git a/tests/tools/test_memory_tool_schema.py b/tests/tools/test_memory_tool_schema.py
index c707a6136c0..12e39592387 100644
--- a/tests/tools/test_memory_tool_schema.py
+++ b/tests/tools/test_memory_tool_schema.py
@@ -84,6 +84,22 @@ def test_one_item_add_that_overflows_returns_the_live_entries_like_the_legacy_ad
assert store.memory_entries == ["written by another session after load"]
+def test_one_item_remove_or_duplicate_add_works_on_a_store_already_over_a_lowered_cap(tmp_path, monkeypatch):
+ """Like the legacy remove (no budget check) and duplicate add ("already exists"), a one-item
+ batch that does not grow the store is not refused for the store's pre-existing excess."""
+ monkeypatch.setattr("tools.memory_tool.get_memory_dir", lambda: tmp_path)
+ facts = ["a" * 50, "b" * 50, "c" * 50]
+ (tmp_path / "MEMORY.md").write_text("\n§\n".join(facts), encoding="utf-8")
+ store = MemoryStore(memory_char_limit=100, user_char_limit=300) # cap lowered below the store
+ store.load_from_disk()
+ handler = registry.get_entry("memory").handler
+ dup = json.loads(handler({"target": "memory", "operations": [{"action": "add", "content": facts[1]}]}, store=store))
+ assert dup["success"] is True
+ gone = json.loads(handler({"target": "memory", "operations": [{"action": "remove", "old_text": "c" * 50}]}, store=store))
+ assert gone["success"] is True
+ assert store.memory_entries == facts[:2]
+
+
def test_background_review_one_item_remove_of_the_last_entry_is_staged_not_applied(tmp_path, monkeypatch):
"""A one-item remove may empty a store, but an unattended background review never gets
that far: the #105921 delete gate stages it, so #103419's USER.md wipe stays closed."""
diff --git a/tools/memory_tool_store.py b/tools/memory_tool_store.py
index 097d69afcce..22616da69b0 100644
--- a/tools/memory_tool_store.py
+++ b/tools/memory_tool_store.py
@@ -598,7 +598,10 @@ class MemoryStore:
f"entries into a shorter one instead of removing the last one. To delete the "
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 only
- if new_total > limit:
+ # A one-item remove or duplicate add never grows the store, so like the legacy
+ # remove/add it is not refused for excess left by a lowered cap.
+ grows = not (len(ops) == 1 and (ops[0].get("action") == "remove" or working == entries))
+ if grows and new_total > limit:
if len(ops) == 1 and ops[0].get("action") == "add": # the advertised single add
return self._add_overflow(target, (ops[0].get("content") or ops[0].get("new_text") or "").strip(), limit)
return self._batch_failure(target, (