Skip to content

The agent that grows with you

fix(memory): key replaced_entries by 1-based op position; tidy review nits

kshitijk4poorc6ed01e77476
diff --git a/tests/tools/test_memory_tool.py b/tests/tools/test_memory_tool.py
index 98f5c9739eb..645515f5734 100644
--- a/tests/tools/test_memory_tool.py
+++ b/tests/tools/test_memory_tool.py
@@ -203,12 +203,12 @@ class TestMemoryStoreReplace:
assert result["success"] is True
assert store.memory_entries == ["RULE B: CI is per-head."]
assert result["replaced_entry"] == entry
- # Batch surface carries the same visibility: op index -> full overwritten text.
+ # Batch surface carries the same visibility: 1-based op position -> full overwritten text.
store.add("memory", "second entry")
batch_result = store.apply_batch("memory", [{"action": "replace", "old_text": "second entry",
"content": "second entry, amended."}])
assert batch_result["success"] is True
- assert batch_result["replaced_entries"] == {0: "second entry"}
+ assert batch_result["replaced_entries"] == {1: "second entry"}
def test_replace_same_across_single_batch_and_approval_replay(self, tmp_path, monkeypatch):
"""The three dispatch surfaces (store.replace, apply_batch, apply_memory_pending
diff --git a/tools/memory_tool.py b/tools/memory_tool.py
index 734132dfeea..3369a51f9ea 100644
--- a/tools/memory_tool.py
+++ b/tools/memory_tool.py
@@ -115,14 +115,14 @@ def _validate_single_op(store, action, target, content, old_text) -> Optional[st
if action == "add" and not content:
return tool_error("Content is required for 'add' action.", success=False)
if action in ("replace", "remove") and not old_text:
+ replace_hint = (" For 'replace', content is the COMPLETE new entry -- the whole "
+ "matched entry is overwritten, not just the old_text span."
+ if action == "replace" else "")
return json.dumps({
"success": False,
"error": (f"'{action}' needs old_text -- a short unique substring of the entry "
f"to {action}. None was provided. Reissue the {action} with old_text "
- f"set to part of one of the current_entries below."
- + (" For 'replace', content is the COMPLETE new entry -- the whole "
- "matched entry is overwritten, not just the old_text span."
- if action == "replace" else "")),
+ f"set to part of one of the current_entries below.{replace_hint}"),
"current_entries": store._entries_for(target), "usage": store._usage(target)}, ensure_ascii=False)
if action == "replace" and not content:
return tool_error("content is required for 'replace' action.", success=False)
diff --git a/tools/memory_tool_store.py b/tools/memory_tool_store.py
index 81616575fd8..9e7c4317bb9 100644
--- a/tools/memory_tool_store.py
+++ b/tools/memory_tool_store.py
@@ -328,8 +328,9 @@ class MemoryStore:
def _apply_batch_op(working: List[str], act: str, content: str, old_text: str,
pos: str) -> Tuple[Optional[str], Optional[str]]:
"""Apply one batch op to *working* in place; return ``(error message, replaced
- entry text)`` — the second value is the full entry a 'replace' overwrote, so
- the caller can surface it (#117952); both are None-typed on non-replace paths."""
+ entry text)``. The first element is None on every success path; the second is
+ the full entry a 'replace' overwrote (None on non-replace paths and on every
+ error path), surfaced so the caller can show what was lost (#117952)."""
if act == "add":
if not content:
return f"{pos}: content is required.", None
@@ -376,7 +377,9 @@ class MemoryStore:
if msg:
return self._batch_failure(target, msg)
if replaced_text is not None:
- replaced[i] = replaced_text
+ # 1-based op position, matching the "Operation N" error numbering the
+ # model sees for failed ops in the same batch.
+ replaced[i + 1] = replaced_text
if entries and not working:
# #103419: a consolidation batch that removes the last entry would
# commit an empty file as a normal successful write. Refuse; single