The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(memory): refuse unpinned legacy replace/remove at approval
kshitijk4poor3da1c59377f4
diff --git a/hermes_cli/write_approval_commands.py b/hermes_cli/write_approval_commands.pyindex 71d0b3dfab0..54379ac2daa 100644--- a/hermes_cli/write_approval_commands.py+++ b/hermes_cli/write_approval_commands.py@@ -116,8 +116,10 @@ def _matched_entries(payload) -> List[str]:if not isinstance(payload, dict):return []ops = payload.get("operations") if payload.get("action") == "batch" else [payload]- return [f"{op['action']}s entry: {op['matched_entry']}" for op in (ops if isinstance(ops, list) else [])- if isinstance(op, dict) and op.get("action") in ("replace", "remove") and op.get("matched_entry")]+ return [f"{op['action']}s entry: {op['matched_entry']}" if op.get("matched_entry")+ else f"{op['action']}: unpinned legacy target \u2014 reject and recreate before approving"+ for op in (ops if isinstance(ops, list) else [])+ if isinstance(op, dict) and op.get("action") in ("replace", "remove")]def _apply_one(subsystem: str, rec, memory_store):diff --git a/tests/tools/test_memory_tool.py b/tests/tools/test_memory_tool.pyindex afc7ee9fbfa..5bce00017cc 100644--- a/tests/tools/test_memory_tool.py+++ b/tests/tools/test_memory_tool.py@@ -223,7 +223,7 @@ class TestMemoryStoreReplace:("single", lambda s: s.replace("memory", op["old_text"], op["content"])),("batch", lambda s: s.apply_batch("memory", [op])),("replay", lambda s: apply_memory_pending({"action": "batch", "target": "memory",- "operations": [op]}, s))):+ "operations": [{**op, "matched_entry": entry}]}, s))):store_dir = tmp_path / surfacestore_dir.mkdir()monkeypatch.setattr("tools.memory_tool.get_memory_dir", lambda d=store_dir: d)diff --git a/tests/tools/test_write_approval.py b/tests/tools/test_write_approval.pyindex 03542fdcbc1..1cfdfe0ab1e 100644--- a/tests/tools/test_write_approval.py+++ b/tests/tools/test_write_approval.py@@ -178,7 +178,8 @@ def test_handle_approve_surfaces_overwritten_entry(hermes_home):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."}]},+ {"action": "replace", "old_text": "RULE B: ci per HEAD.", "content": "RULE B: CI is per-head.",+ "matched_entry": entry}]},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@@ -231,8 +232,9 @@ def test_approve_refuses_staged_remove_whose_entry_changed(hermes_home, shape):@pytest.mark.parametrize("shape,legacy", [("single", False), ("batch", False), ("single", True)])def test_approve_names_the_entry_a_remove_deleted(hermes_home, shape, legacy):- """Approve listed what a replace overwrote but was silent about what a remove deleted,- including a record staged before removes were pinned to their full entry."""+ """Approve listed what a replace overwrote but was silent about what a remove deleted. A+ record staged before removes were pinned to their full entry has no verifiable target, so+ approve refuses it (keeping the record) instead of replaying its old_text search."""from hermes_cli.write_approval_commands import handle_pending_subcommandfrom tools.memory_tool import load_on_disk_storefrom tools import write_approval as wa@@ -242,9 +244,15 @@ def test_approve_names_the_entry_a_remove_deleted(hermes_home, shape, legacy):record = json.loads(path.read_text(encoding="utf-8"))record["payload"].pop("matched_entry", None)path.write_text(json.dumps(record), encoding="utf-8")+ assert "unpinned legacy target" in handle_pending_subcommand(wa.MEMORY, ["pending"])out = handle_pending_subcommand(wa.MEMORY, ["approve", pid], memory_store=load_on_disk_store())+ if legacy:+ assert "Approved 0" in out and "predates entry pinning" in out, out+ assert _REVIEWED in load_on_disk_store().memory_entries+ assert wa.get_pending(wa.MEMORY, pid) is not None+ returnassert _REVIEWED not in load_on_disk_store().memory_entries, outassert _REVIEWED in outdiff --git a/tools/memory_tool.py b/tools/memory_tool.pyindex 897faa28d19..9ffaaa61c0b 100644--- a/tools/memory_tool.py+++ b/tools/memory_tool.py@@ -281,12 +281,18 @@ def _memory_target_error(store: "MemoryStore", target: str) -> Optional[Dict[strdef apply_memory_pending(payload: Dict[str, Any], store: "MemoryStore") -> Dict[str, Any]:"""Replay a staged write against the store, bypassing the gate (/memory approve). A- replace/remove pinned to its ``matched_entry`` applies to that exact entry or is refused;- records staged before pinning still replay by old_text (approve lists what they removed)."""+ replace/remove applies to exactly its pinned ``matched_entry`` or is refused; a record+ staged before pinning has no verifiable target, so it is refused rather than replayed by+ old_text (which could hit a newer entry the approver never saw)."""action, target = payload.get("action"), payload.get("target", "memory")target_error = _memory_target_error(store, target)if target_error is not None:return target_error+ ops = payload.get("operations") or [] if action == "batch" else [payload]+ if any(isinstance(op, dict) and op.get("action") in _BG_DELETE_ACTIONS and not op.get("matched_entry")+ for op in ops):+ return {"success": False, "error": "This destructive pending write predates entry pinning and cannot be "+ "verified; nothing was applied. Reject it and recreate the change."}if action == "batch":return store.apply_batch(target, payload.get("operations") or [])if action not in _STORE_ACTIONS:diff --git a/website/docs/user-guide/features/memory.md b/website/docs/user-guide/features/memory.mdindex 02407314ab3..a07cca5f49c 100644--- a/website/docs/user-guide/features/memory.md+++ b/website/docs/user-guide/features/memory.md@@ -320,8 +320,10 @@ ones — waits for your yes/no before it ever enters your profile.A staged `replace` or `remove` (the background review stages these even with thegate off) records the full entry it targets, and `/memory pending` shows it.Approval applies to exactly that entry: if it changed after the write was staged,-the write is refused and stays pending for you to reject. `/memory approve` lists-the full text of every entry it overwrote or removed.+the write is refused and stays pending for you to reject. A `replace`/`remove`+staged before this pinning existed has no verifiable target and is refused too:+reject it and recreate the change. `/memory approve` lists the full text of+every entry it overwrote or removed.## Background review notifications (`display.memory_notifications`)