Skip to content

The agent that grows with you

fix(memory): a journey edit honours the memory tool's char limit

kshitijk4poor16585588fe0a
diff --git a/agent/learning_mutations.py b/agent/learning_mutations.py
index 0f0705b582c..b582cb698ad 100644
--- a/agent/learning_mutations.py
+++ b/agent/learning_mutations.py
@@ -108,13 +108,21 @@ def _mutate_memory(node_id: str, replacement: str | None) -> dict[str, Any]:
name = _MEMORY_FILES[source]
message = f"deleted memory from {name}" if replacement is None else f"updated memory in {name}"
- def _apply(entries, _limit):
+ def _apply(entries, limit):
+ from tools.memory_tool import ENTRY_DELIMITER
+
_, chunks, local = _locate_memory(node_id)
text = chunks[local].strip()
if text not in entries:
return {"success": False, "error": "memory node id is stale — refresh the graph"}
idx = entries.index(text)
- return entries[:idx] + ([] if replacement is None else [replacement]) + entries[idx + 1:], message
+ 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:
+ return {"success": False,
+ "error": f"Replacement would put memory at {total:,}/{limit:,} chars. Shorten the new content."}
+ return new_entries, message
result = load_on_disk_store()._mutate(_STORE_TARGETS[source], _apply)
if not result.get("success"):
diff --git a/tests/agent/test_learning_mutations.py b/tests/agent/test_learning_mutations.py
index 2b046778cbc..29e4fb1b4fe 100644
--- a/tests/agent/test_learning_mutations.py
+++ b/tests/agent/test_learning_mutations.py
@@ -53,6 +53,19 @@ def test_edit_memory_replaces_chunk(home):
assert (home / "memories" / "USER.md").read_text(encoding="utf-8").strip() == "rewritten profile"
+def test_edit_memory_refuses_an_entry_over_the_memory_tools_limit(home):
+ """Same char cap as the memory tool's replace: an over-limit entry reads as external drift to
+ every later mutation, so the tool's own remove/replace would refuse (with a .bak) until the
+ file is fixed by hand."""
+ from tools.memory_tool import MemoryStore
+
+ result = lm.edit_node("memory:profile:2", "x" * 5000)
+
+ assert not result["ok"] and "Shorten" in result["message"]
+ assert (home / "memories" / "USER.md").read_text(encoding="utf-8") == "user profile note"
+ assert MemoryStore().remove("user", "user profile note")["success"]
+
+