Skip to content

The agent that grows with you

fix(memory): accept a nested target that matches the call's

Teknium2c5453301176
diff --git a/tests/tools/test_memory_target_contract.py b/tests/tools/test_memory_target_contract.py
index 7595c7fee1b..36c8012c508 100644
--- a/tests/tools/test_memory_target_contract.py
+++ b/tests/tools/test_memory_target_contract.py
@@ -32,8 +32,6 @@ def _call(route, store, args):
({"target": None}, {}),
({}, {"target": "user"}),
({"target": "memory"}, {"target": "user"}),
- ({"target": "user"}, {"target": "user"}),
- ({"target": "user"}, {"target": None}),
])
def test_invalid_targets_leave_files_and_pending_queue_unchanged(
route, origin, target_fields, nested_fields,
@@ -66,6 +64,22 @@ def test_invalid_targets_leave_files_and_pending_queue_unchanged(
assert {t: store._path_for(t).read_bytes() for t in before} == before
[email protected]("route", ["registry", "inline", "store", "replay"])
[email protected]("nested", ["user", None])
+def test_nested_target_matching_the_call_is_accepted(route, nested):
+ store = MemoryStore()
+ store.load_from_disk()
+ for name in ("memory", "user"):
+ assert store.add(name, f"Existing {name} fact")["success"]
+ memory_before = store._path_for("memory").read_bytes()
+ args = {"target": "user", "operations": [{"action": "add", "content": "Fact", "target": nested}]}
+ assert _call(route, store, args)["success"] is True
+ fresh = MemoryStore()
+ fresh.load_from_disk()
+ assert fresh._entries_for("user") == ["Existing user fact", "Fact"]
+ assert store._path_for("memory").read_bytes() == memory_before
+
+
@pytest.mark.parametrize("route", ["registry", "inline"])
@pytest.mark.parametrize("batch", [False, True])
@pytest.mark.parametrize("target", ["memory", "user"])
diff --git a/tools/memory_tool.py b/tools/memory_tool.py
index a3e455def7b..aeb839a2e69 100644
--- a/tools/memory_tool.py
+++ b/tools/memory_tool.py
@@ -335,11 +335,13 @@ def _memory_target_error(
"error": _bound_error_text(f"Invalid memory target '{target}'. Use 'memory' or 'user'.")}
if isinstance(operations, list):
for index, op in enumerate(operations, 1):
- if isinstance(op, dict) and "target" in op:
+ # A nested target equal to the call's (or null) is redundant, not wrong; only a
+ # different one asks for two stores in one call, which a batch cannot do.
+ if isinstance(op, dict) and op.get("target") not in (None, target):
FAILURE_CLASS.set("invalid_args")
return {"success": False, "error": (
- f"Operation {index} has a nested target. Retry with target only at the top level "
- "and remove it from every operation. Use separate calls for different stores. "
+ f"Operation {index} names a different target than the one at the top level. "
+ "One call writes one store: retry with separate calls for different stores. "
"Nothing was written or staged.")}
if store.target_enabled(target):
return None
diff --git a/website/docs/user-guide/features/memory.md b/website/docs/user-guide/features/memory.md
index 33977d457ff..83f672120e2 100644
--- a/website/docs/user-guide/features/memory.md
+++ b/website/docs/user-guide/features/memory.md
@@ -104,8 +104,8 @@ for the user profile. A batch uses that one target for every operation:
{"target": "user", "operations": [{"action": "add", "content": "User prefers metric units"}]}
```
-Missing or null targets and targets inside operations return an error before writing
-or staging approval. Correct the payload and retry; use separate calls to update both stores.
+Missing or null targets, and a target inside an operation that differs from the top-level
+one, return an error before writing or staging approval. Correct the payload and retry; use separate calls to update both stores.
### Substring Matching