Skip to content

The agent that grows with you

fix(memory): surface the overwritten entry on the /memory approve surface too

teknium105b9bacbf8cf
diff --git a/hermes_cli/write_approval_commands.py b/hermes_cli/write_approval_commands.py
index 150b72dfd17..590279154b5 100644
--- a/hermes_cli/write_approval_commands.py
+++ b/hermes_cli/write_approval_commands.py
@@ -74,36 +74,50 @@ def _approve(subsystem: str, rest: List[str], memory_store) -> str:
return f"No pending {subsystem} write with id '{target}'."
targets = [rec]
- applied, failed = 0, []
+ applied, failed, overwritten = 0, [], []
for rec in targets:
- ok, msg = _apply_one(subsystem, rec, memory_store)
+ ok, msg, result = _apply_one(subsystem, rec, memory_store)
if ok:
wa.discard_pending(subsystem, rec["id"])
applied += 1
+ overwritten.extend(f" {rec['id']}: {text}" for text in _replaced_entries(result))
else:
failed.append(f"{rec['id']}: {msg}")
out = [f"Approved {applied} {subsystem} write(s)."]
+ if overwritten:
+ # A memory 'replace' overwrites the WHOLE matched entry (#117952); the approver
+ # is the last person who can notice a clause went missing, so show what was lost.
+ out.append("Overwrote entire entry (re-add anything you still need):")
+ out.extend(overwritten)
if failed:
out.append("Failed:")
out.extend(f" {f}" for f in failed)
return "\n".join(out)
+def _replaced_entries(result: dict) -> List[str]:
+ """Full text of every entry a memory replace overwrote, single-op or batch shape."""
+ single = result.get("replaced_entry")
+ batch = result.get("replaced_entries") or {}
+ return ([single] if single else []) + [batch[k] for k in sorted(batch, key=int)]
+
+
def _apply_one(subsystem: str, rec, memory_store):
+ """``(ok, error, result)`` — *result* is the applier's full payload (empty on exceptions)."""
payload = rec.get("payload", {})
try:
if subsystem == wa.MEMORY:
if memory_store is None:
- return False, "memory store unavailable"
+ return False, "memory store unavailable", {}
from tools.memory_tool import apply_memory_pending
result = apply_memory_pending(payload, memory_store)
else:
from tools.skill_manager_tool import apply_skill_pending
result = json.loads(apply_skill_pending(payload))
- return bool(result.get("success")), result.get("error", "")
+ return bool(result.get("success")), result.get("error", ""), result
except Exception as e:
- return False, str(e)
+ return False, str(e), {}
def _reject(subsystem: str, rest: List[str]) -> str:
diff --git a/tests/tools/test_write_approval.py b/tests/tools/test_write_approval.py
index b99960d6907..37fd4ea3532 100644
--- a/tests/tools/test_write_approval.py
+++ b/tests/tools/test_write_approval.py
@@ -189,6 +189,24 @@ def test_handle_approve_all(hermes_home):
assert len(store.user_entries) == 2
+def test_handle_approve_surfaces_overwritten_entry(hermes_home):
+ """#117952: on the /memory approve surface a partial-entry replace must show the
+ approver the FULL entry it overwrote — the store's replaced_entries field used to be
+ dropped by _apply_one, so the incident path stayed silent."""
+ from hermes_cli.write_approval_commands import handle_pending_subcommand
+ from tools.memory_tool import MemoryStore
+ from tools import write_approval as wa
+ store = MemoryStore(); store.load_from_disk()
+ entry = "RULE A: gate merges. RULE B: ci per HEAD. RULE C: never squash."
+ store.add("memory", entry)
+ wa.stage_write("memory", {"action": "batch", "target": "memory", "operations": [
+ {"action": "replace", "old_text": "RULE B: ci per HEAD.", "content": "RULE B: CI is per-head."}]},
+ summary="batch", origin="background_review")
+ out = handle_pending_subcommand(wa.MEMORY, ["approve", "all"], memory_store=store)
+ assert "Approved 1" in out and entry in out
+ assert store.memory_entries == ["RULE B: CI is per-head."]
+
+
def test_handle_approval_on(hermes_home):
from hermes_cli.write_approval_commands import handle_pending_subcommand
from tools import write_approval as wa
diff --git a/tools/memory_tool.py b/tools/memory_tool.py
index 3369a51f9ea..f770bb24662 100644
--- a/tools/memory_tool.py
+++ b/tools/memory_tool.py
@@ -83,7 +83,8 @@ _STORE_ACTIONS = {
"add": (lambda store, target, content, old_text: store.add(target, content),
lambda label, content, old_text: (f"add to {label}", content or "")),
"replace": (lambda store, target, content, old_text: store.replace(target, old_text, content),
- lambda label, content, old_text: (f"replace in {label}", f"old: {old_text}\nnew: {content}")),
+ lambda label, content, old_text: (f"replace in {label}",
+ f"entry matching: {old_text}\nwhole entry becomes: {content}")),
"remove": (lambda store, target, content, old_text: store.remove(target, old_text),
lambda label, content, old_text: (f"remove from {label}", old_text or ""))}
@@ -93,7 +94,9 @@ def _batch_op_line(op: Dict[str, Any]) -> str:
act, content, old = op.get("action", "?"), op.get("content") or op.get("new_text") or "", op.get("old_text", "")
if act == "remove":
return f"- remove: {old}"
- return f"- replace: {old} -> {content}" if act == "replace" else f"- {act}: {content}"
+ # Whole-entry contract (#117952): the approver must not read this as a span patch.
+ return (f"- replace entry matching '{old}' -> whole entry becomes: {content}" if act == "replace"
+ else f"- {act}: {content}")
def _apply_write_gate(action: str, target: str, content: Optional[str], old_text: Optional[str],