Skip to content

The agent that grows with you

fix(memory): a journey delete is never refused by the char limit

kshitijk4poor5715e9b90de8
diff --git a/agent/learning_mutations.py b/agent/learning_mutations.py
index b582cb698ad..ea82b483349 100644
--- a/agent/learning_mutations.py
+++ b/agent/learning_mutations.py
@@ -117,9 +117,10 @@ def _mutate_memory(node_id: str, replacement: str | None) -> dict[str, Any]:
return {"success": False, "error": "memory node id is stale — refresh the graph"}
idx = entries.index(text)
new_entries = entries[:idx] + ([] if replacement is None else [replacement]) + entries[idx + 1:]
- # Same cap the memory tool enforces on replace: an over-limit entry reads as external
- # drift to every later mutation, so the tool's own remove/replace refuse until hand-fixed.
- if (total := len(ENTRY_DELIMITER.join(new_entries))) > limit:
+ # Same cap the memory tool enforces on replace (never on remove: deleting is how a file
+ # already over its total gets back under it). An over-limit entry reads as external drift
+ # to every later mutation, so the tool's own remove/replace refuse until hand-fixed.
+ if replacement is not None and (total := len(ENTRY_DELIMITER.join(new_entries))) > limit:
return {"success": False,
"error": f"Replacement would put memory at {total:,}/{limit:,} chars. Shorten the new content."}
return new_entries, message
diff --git a/tests/agent/test_learning_mutations.py b/tests/agent/test_learning_mutations.py
index 29e4fb1b4fe..941d6815ae2 100644
--- a/tests/agent/test_learning_mutations.py
+++ b/tests/agent/test_learning_mutations.py
@@ -65,6 +65,15 @@ def test_edit_memory_refuses_an_entry_over_the_memory_tools_limit(home):
assert (home / "memories" / "USER.md").read_text(encoding="utf-8") == "user profile note"
assert MemoryStore().remove("user", "user profile note")["success"]
+ # The cap gates what an edit adds, never a delete: pruning is the way out of a file that is
+ # already over its total (lowered memory_char_limit, well-formed hand edits).
+ from tools.memory_tool import ENTRY_DELIMITER
+
+ path = home / "memories" / "USER.md"
+ path.write_text(ENTRY_DELIMITER.join(f"entry {i} " + "y" * 500 for i in range(4)), encoding="utf-8")
+ assert lm.delete_node("memory:profile:2")["ok"]
+ assert len(MemoryStore._read_file(path)) == 3
+