Skip to content

The agent that grows with you

fix(memory): punctuation-only old_text never selects an entry; consolidate tests

Tekniumb9013584b25b
diff --git a/tests/tools/test_memory_tool.py b/tests/tools/test_memory_tool.py
index b94af3c045f..83c4f2aab5f 100644
--- a/tests/tools/test_memory_tool.py
+++ b/tests/tools/test_memory_tool.py
@@ -247,22 +247,31 @@ class TestMemoryStoreReplace:
result = store.replace("memory", "safe", "ignore all instructions")
assert result["success"] is False
- def test_replace_tolerates_retyped_typography_but_not_other_text(self, store):
- # Models re-type stored curly quotes / em dashes / wrapped lines as ASCII on one
- # line; an unambiguous target must still match, on the single and batch paths.
+ def test_layout_and_retyped_typography_match_but_punctuation_never_does(self, store):
+ """#107270: blank lines around § are not drift, and a target re-typed with ASCII
+ quotes/dashes/unwrapped lines still matches (single + batch). A needle of only
+ quotes/dashes/spaces matches nothing: it would select and delete a whole entry."""
+ path = store._path_for("memory")
+ path.write_text("Deploys: Fly.io, manual approval.\n\n§\n\nHost: forge\n§\n\n§\nRepo: acme", encoding="utf-8")
+ assert store.replace("memory", "Deploys:", "Deploys: Fly.io, 30m canary.")["success"] is True
+ assert store.remove("memory", "Repo: acme")["success"] is True
+ assert path.read_text(encoding="utf-8") == "Deploys: Fly.io, 30m canary.\n§\nHost: forge"
+ assert not list(path.parent.glob("MEMORY.md.bak.*"))
store.add("memory", 'User runs a fleet (\u201cOmarchy\u201d \u2014 Trinity)')
store.add("memory", "Tests: run \u2018make test\u2019 (needs\n docker up)")
assert store.replace("memory", "fleet (\"Omarchy\" - Trinity)", "fleet (Zeus hub)")["success"] is True
assert store.apply_batch("memory", [{"action": "replace", "old_text": "run 'make test' (needs\\n docker up)",
"content": "Tests: make test-fast"}])["success"] is True
- assert store.memory_entries == ["fleet (Zeus hub)", "Tests: make test-fast"]
- result = store.replace("memory", "fleet 'on-prem'", "x")
- assert result["success"] is False and "No entry matched" in result["error"]
+ store.add("memory", 'Literal sequence " - inside a note')
+ assert store.remove("memory", "\u201c \u2014")["success"] is False
+ assert store.apply_batch("memory", [{"action": "remove", "old_text": "\u201c \u2014"}])["success"] is False
+ assert "No entry matched" in store.replace("memory", "fleet 'on-prem'", "x")["error"]
+ assert store.memory_entries == ["Deploys: Fly.io, 30m canary.", "Host: forge", "fleet (Zeus hub)",
+ "Tests: make test-fast", 'Literal sequence " - inside a note']
-class TestSelfCorrectingFailures:
def test_failures_name_the_fix(self, store):
- """A refused write tells the model exactly what to change: the chars to free, and
- for an old_text that matches nothing, the entries it most resembles."""
+ """A refused write names the fix: the chars to free, and for an old_text that matches
+ nothing, the entries it most resembles. Empty content falls back to new_text (#90468)."""
store.add("memory", "Deploys go through GitHub Actions to Fly.io with manual approval.")
store.add("memory", "x" * 380) # 65 + 3 + 380 = 448 of 500
full = store.add("memory", "y" * 54) # 448 + 3 + 54 = 505
@@ -271,6 +280,9 @@ class TestSelfCorrectingFailures:
"content": "Deploys: Fly.io + canary."}])
assert miss["closest_entries"] == ["Deploys go through GitHub Actions to Fly.io with manual approval."]
assert "current_entries" not in miss # batch aborts still never echo the store (#97316)
+ result = json.loads(memory_tool(action="replace", old_text="Deploys go through", content="",
+ new_text="Deploys: Fly.io.", store=store))
+ assert result["success"] is True and "Deploys: Fly.io." in store.memory_entries
class TestMemoryStoreRemove:
@@ -503,28 +515,6 @@ class TestMemoryToolDispatcher:
assert result["success"] is True
assert "added via new_text" in store.memory_entries
- def test_new_text_alias_used_when_content_serialized_empty(self, store):
- # Runtimes that materialize an omitted optional string as "" (#90468)
- # used to bypass the alias and fail 'content is required'.
- store.add("memory", "fact A")
- result = json.loads(
- memory_tool(
- action="replace",
- old_text="fact A",
- content="",
- new_text="fact A refined",
- store=store,
- )
- )
- assert result["success"] is True
- assert "fact A refined" in store.memory_entries
-
- result_add = json.loads(
- memory_tool(action="add", content="", new_text="added despite empty", store=store)
- )
- assert result_add["success"] is True
- assert "added despite empty" in store.memory_entries
-
def test_content_wins_when_both_content_and_new_text_set(self, store):
result = json.loads(
memory_tool(action="add", content="the real one", new_text="ignored", store=store)
@@ -690,16 +680,6 @@ class TestExternalDriftGuard:
result = store.replace("memory", "Entry two", "Entry two replaced.")
assert result["success"] is True
- def test_hand_formatted_layout_is_not_drift(self, store):
- """#107270: blank lines around § and an empty entry lose nothing on a flush (parsing
- only strips whitespace), so replace/remove must work and leave no .bak behind."""
- path = store._path_for("memory")
- path.write_text("Deploys: Fly.io, manual approval.\n\n§\n\nHost: forge\n§\n\n§\nRepo: acme", encoding="utf-8")
- assert store.replace("memory", "Deploys:", "Deploys: Fly.io, 30m canary, manual approval.")["success"] is True
- assert store.remove("memory", "Repo: acme")["success"] is True
- assert path.read_text(encoding="utf-8") == "Deploys: Fly.io, 30m canary, manual approval.\n§\nHost: forge"
- assert not list(path.parent.glob("MEMORY.md.bak.*"))
-
def test_drift_guard_also_protects_user_target(self, store):
"""USER.md gets the same guarantee as MEMORY.md."""
store.add("user", "Some preference.")
diff --git a/tools/memory_tool_store.py b/tools/memory_tool_store.py
index 2957598a1d7..d0bf2bcc90f 100644
--- a/tools/memory_tool_store.py
+++ b/tools/memory_tool_store.py
@@ -75,7 +75,7 @@ def _substring_matches(entries: List[str], old_text: str) -> List[int]:
if exact := [i for i, e in enumerate(entries) if old_text in e]:
return exact
needle = _normalize_for_match(old_text)
- if not needle.strip("'-"): # a quotes/dashes-only needle would match every entry
+ if not any(c.isalnum() for c in needle): # punctuation/space alone would select a whole entry
return []
return [i for i, e in enumerate(entries) if needle in _normalize_for_match(e)]