Skip to content

The agent that grows with you

refactor(memory): derive batch receipts from the matched list

kshitijk4poor88dee3e86659
diff --git a/tools/memory_tool.py b/tools/memory_tool.py
index 5d679d2d9d7..7577015e921 100644
--- a/tools/memory_tool.py
+++ b/tools/memory_tool.py
@@ -159,8 +159,8 @@ _BG_DELETE_ACTIONS = ("replace", "remove")
def destructive_ops(payload: Dict[str, Any]) -> List[Dict[str, Any]]:
"""The replace/remove ops of a staged memory payload, single-op or batch shape."""
- ops = payload.get("operations") or [] if payload.get("action") == "batch" else [payload]
- return [op for op in ops if op.get("action") in _BG_DELETE_ACTIONS]
+ ops = (payload.get("operations") or []) if payload.get("action") == "batch" else [payload]
+ return [op for op in ops if (op or {}).get("action") in _BG_DELETE_ACTIONS]
def _background_delete_gate(store, action, operations, target="memory", content=None,
@@ -175,13 +175,11 @@ def _background_delete_gate(store, action, operations, target="memory", content=
if not is_unattended_review():
return None
- hit = action in _BG_DELETE_ACTIONS or any(
- isinstance(op, dict) and op.get("action") in _BG_DELETE_ACTIONS for op in (operations or []))
- if not hit:
- return None
payload = ({"action": "batch", "target": target, "operations": operations}
if operations is not None else
{"action": action, "target": target, "content": content, "old_text": old_text})
+ if not destructive_ops(payload):
+ return None
detail = ("; ".join(_batch_op_line(op) for op in operations) if operations is not None
else _batch_op_line({"action": action, "content": content, "old_text": old_text}))
try:
diff --git a/tools/memory_tool_store.py b/tools/memory_tool_store.py
index e94bc697c6a..339e1c73ae9 100644
--- a/tools/memory_tool_store.py
+++ b/tools/memory_tool_store.py
@@ -381,15 +381,15 @@ class MemoryStore:
if act == "replace" and not content:
return f"{pos}: content is required (use action='remove' to delete).", None
if matched_entry is not None:
- idx, ambiguous = _pinned_index(working, matched_entry), False
+ idx = _pinned_index(working, matched_entry)
if idx is None:
return f"{pos}: {_stale_entry_message(matched_entry)}", None
else:
idx, ambiguous = _find_unique_match(working, old_text)
- if ambiguous:
- return f"{pos}: '{old_text}' matched multiple distinct entries -- be more specific.", None
- if idx is None:
- return f"{pos}: no entry matched '{old_text}'.", None
+ if ambiguous:
+ return f"{pos}: '{old_text}' matched multiple distinct entries -- be more specific.", None
+ if idx is None:
+ return f"{pos}: no entry matched '{old_text}'.", None
previous_content = working[idx]
working[idx:idx + 1] = [content] if act == "replace" else []
return None, previous_content
@@ -420,8 +420,6 @@ class MemoryStore:
def _apply(entries, limit):
working = list(entries) # only committed if the whole batch validates
- replaced = {} # op index -> full entry text its replace overwrote (#117952)
- removed = {}
matched = [] # per op, the entry a replace/remove selected (None for add)
for i, op in enumerate(ops):
act = op.get("action")
@@ -432,10 +430,6 @@ class MemoryStore:
if msg:
return self._batch_failure(target, msg)
matched.append(previous_content)
- if previous_content is not None:
- # 1-based op position, matching the "Operation N" error numbering the
- # model sees for failed ops in the same batch.
- (replaced if act == "replace" else removed)[i + 1] = previous_content
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
@@ -454,6 +448,12 @@ class MemoryStore:
f"entries in the same batch, then retry."))
if not commit:
return {"success": True, "matched_entries": matched}
+ # op index -> full entry text its replace/remove selected (#117952), 1-based to
+ # match the "Operation N" error numbering the model sees for failed ops.
+ replaced, removed = {}, {}
+ for i, (op, previous_content) in enumerate(zip(ops, matched), 1):
+ if previous_content is not None:
+ (replaced if op.get("action") == "replace" else removed)[i] = previous_content
replaced_fields = {"replaced_entries": replaced} if replaced else {}
if removed:
replaced_fields["removed_entries"] = removed