Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

fix(memory): render existing objects as context records (#6347)

DuTaocffe101ca20b
diff --git a/openviking/session/memory/extraction_output_protocol/prompt_fragments.py b/openviking/session/memory/extraction_output_protocol/prompt_fragments.py
index 474279d83..4a1477886 100644
--- a/openviking/session/memory/extraction_output_protocol/prompt_fragments.py
+++ b/openviking/session/memory/extraction_output_protocol/prompt_fragments.py
@@ -47,8 +47,9 @@ PYTHON_ADD_ONLY_PREAMBLE = (
)
PYTHON_UPSERT_PREAMBLE = (
*PYTHON_FORMAT_RULES,
- "The SDK is write-only. Use the system-provided existing-object bindings; never emit "
- "sdk.search(), sdk.read(), or sdk.existing().",
+ "The SDK is write-only. Existing-memory records are input context only. "
+ "Use each object_name as an already-bound variable; do not output the "
+ "records or redeclare the objects.",
PYTHON_CONTENT_RULE,
"Existing objects shown by the system may use obj.update(field=value, ...) to set complete "
"scalar/immutable fields, or obj.delete(replacement=None).",
@@ -63,7 +64,7 @@ PYTHON_PATCH_RULES = (
" - obj.content.edit(search=..., replace=...): replace one exact snippet in an editable string (merge_op=patch).",
" - obj.content.drop(text=...): delete one exact snippet in an editable string (merge_op=patch).",
"edit()/drop() may be chained, e.g. obj.content.edit(search='a', replace='b').drop(text='c'); do not mix them with .update() in one chain.",
- "Each search= (and drop text=) MUST be copied verbatim from the current field value shown in the object's sdk.existing(...) binding, and must occur exactly once. If the snippet appears more than once, include an adjacent unique line just before or after it so the match is unique. Never use text from the conversation or the new facts you intend to add as a search anchor; that text is not in the current content and the edit will fail.",
+ "Each search= (and drop text=) MUST be copied verbatim from the current field value in the object's existing-memory context record, and must occur exactly once. Decode JSON string escapes when copying the text. If the snippet appears more than once, include an adjacent unique line just before or after it so the match is unique. Never use text from the conversation or the new facts you intend to add as a search anchor; that text is not in the current content and the edit will fail.",
"edit()/drop() only work on an existing memory's editable string field (merge_op=patch); new memories from create/set must be given complete field values.",
"For existing editable strings (merge_op=patch), prefer the smallest unique edit()/drop(). Do not rewrite the entire field just to add or change a few facts; large full-content rewrites are more likely to be truncated or malformed. Use obj.content.update() only when most of such an editable string changes.",
'Use triple-quoted strings ("""...""") for every search=/replace=/text= snippet too.',
@@ -73,11 +74,11 @@ PYTHON_UPSERT_FOOTER = (
"Only keyword arguments are accepted by create, set, and obj.update(); "
"a field's update() takes one positional string.",
PYTHON_COMMIT_RULE,
- "Use the system-provided existing-object variable names exactly as shown. "
+ "Use each system-provided object_name exactly as the variable name shown. "
"When a newly created memory is the replacement, assign its create call to "
"a variable first, for example: canonical = sdk.create_<type>(...); "
"duplicate_1.delete(replacement=canonical). Never recreate or rebind an "
- "existing object with sdk.existing().",
+ "existing object.",
*PYTHON_METHODS_HEADER,
)
PYTHON_LINK_RULES = (
diff --git a/openviking/session/memory/extraction_output_protocol/python_protocol.py b/openviking/session/memory/extraction_output_protocol/python_protocol.py
index a697c4084..953bce5a5 100644
--- a/openviking/session/memory/extraction_output_protocol/python_protocol.py
+++ b/openviking/session/memory/extraction_output_protocol/python_protocol.py
@@ -288,8 +288,9 @@ class PythonExtractionOutputProtocol(ExtractionOutputProtocol):
quote_guidance = ""
if error and "sdk.existing() is reserved" in error:
binding_guidance = (
- " Use the existing-object variable names already supplied by the system; do not "
- "emit sdk.existing(). If a new object is the replacement, assign its create call "
+ " Use the object_name from the system-provided existing-memory context record "
+ "as an already-bound variable; do not declare it or emit sdk.existing(). "
+ "If a new object is the replacement, assign its create call "
"first, for example `canonical = sdk.create_<type>(...)`, then call "
"`duplicate_1.delete(replacement=canonical)`."
)
@@ -337,7 +338,8 @@ class PythonExtractionOutputProtocol(ExtractionOutputProtocol):
return (
"An obj.field.edit()/drop() change could not be applied to the target memory object. "
"The search= (and drop text=) must be copied verbatim from the current field value shown "
- "in that object's sdk.existing(...) binding, and must occur exactly once. If it occurs "
+ "in that object's existing-memory context record, and must occur exactly once. "
+ "Decode JSON string escapes when copying the text. If it occurs "
"more than once, include enough contiguous surrounding context to make it unique. Do not "
"use text from the conversation or the new facts you intend to add as a search anchor. "
"If you copy from numbered read output, exclude the `line_number<TAB>` prefix. If the "
@@ -468,10 +470,9 @@ class PythonExtractionOutputProtocol(ExtractionOutputProtocol):
def _render_search_comment(
context: ExtractionOutputContext, params: dict[str, Any], result: Any
) -> str:
- # Search only returns URIs (no content), so it cannot become an
- # sdk.existing() binding. Files actually read are bound separately as
- # sdk.existing(); how to reach the rest depends on whether a read tool
- # is available this run.
+ # Search only returns URIs, so it cannot register existing objects.
+ # Files actually read receive context records with bound object names;
+ # how to reach the rest depends on whether a read tool is available.
uris = _search_result_uris(result)
query = params.get("query")
query_label = f" query={query!r}" if query else ""
@@ -482,7 +483,7 @@ class PythonExtractionOutputProtocol(ExtractionOutputProtocol):
guidance = "read them with the read tool before updating them"
else:
guidance = (
- "any you need to edit are already provided as sdk.existing bindings; "
+ "any you need to edit are already provided as existing-memory context records; "
"there is no read tool, so treat anything not shown above as new"
)
return f"# Search{query_label} found the following memory files; {guidance}:\n{listing}"
@@ -522,11 +523,15 @@ class PythonExtractionOutputProtocol(ExtractionOutputProtocol):
content = fields.get("content")
if isinstance(content, str) and every_line_has_line_numbers(content):
fields["content"] = strip_line_numbers(content)
- args = [f"memory_type={memory_type!r}"]
- args.extend(f"{_identifier_alias(key)}={value!r}" for key, value in fields.items())
+ record = {
+ "object_name": name,
+ "memory_type": memory_type,
+ "fields": {_identifier_alias(key): value for key, value in fields.items()},
+ }
binding = (
- f"# Existing memory loaded by {source}; this binding is system-provided.\n"
- f"{name} = sdk.existing({', '.join(args)})"
+ f"# Existing memory loaded by {source}; {name} is already bound. "
+ "Context data only; do not output this record.\n"
+ + json.dumps(record, ensure_ascii=False, indent=2)
)
if maintenance_notice is not None:
binding += "\n# Memory maintenance notice: " + json.dumps(
diff --git a/tests/session/memory/test_extraction_output_protocol.py b/tests/session/memory/test_extraction_output_protocol.py
index 07e67d1af..2615f8a24 100644
--- a/tests/session/memory/test_extraction_output_protocol.py
+++ b/tests/session/memory/test_extraction_output_protocol.py
@@ -190,9 +190,10 @@ def test_json_protocol_preserves_stable_parser_and_empty_contract():
def test_python_contract_and_bindings_expose_only_selected_schema_fields():
uri = "viking://user/alice/memories/preferences/editor.md"
+ body = 'Use Vim\n Keep "quotes", tabs\tand 中文; literal \\n.'
context = _context(
[_preference_schema()],
- files=[_existing_preference(uri, "editor", "Use Vim", 2)],
+ files=[_existing_preference(uri, "editor", body, 2)],
)
protocol = create_extraction_output_protocol("python")
@@ -201,9 +202,17 @@ def test_python_contract_and_bindings_expose_only_selected_schema_fields():
assert "sdk.create_preferences" in contract
assert "sdk.create_projects" not in contract
- assert "preferences_1 = sdk.existing" in bindings
- assert "topic='editor'" in bindings
- assert "content='Use Vim'" in bindings
+ record = json.loads(bindings.split("\n", 1)[1])
+ assert record == {
+ "object_name": "preferences_1",
+ "memory_type": "preferences",
+ "fields": {"topic": "editor", "score": 2, "content": body},
+ }
+ assert "Existing-memory records are input context only." in contract
+ assert "Use each object_name as an already-bound variable" in contract
+ assert "sdk.existing" not in contract
+ assert "sdk.existing(" not in bindings
+ assert protocol.binding_name(uri) == record["object_name"]
assert "version" not in bindings
assert uri not in bindings
assert protocol.render_new_bindings(context, source="duplicate read") == ""
@@ -464,8 +473,10 @@ def test_python_protocol_renders_search_result_as_read_tool_comment():
assert "read them with the read tool" in content
assert f"# - {uri_a}" in content
assert f"# - {uri_b}" in content
- # Search yields no content, so it must not fabricate an sdk.existing() binding.
+ # Search yields no content, so it must not register an existing object.
assert "sdk.existing(" not in content
+ assert protocol.binding_name(uri_a) is None
+ assert protocol.binding_name(uri_b) is None
assert "search_results" not in content
assert "0.9" not in content
@@ -539,8 +550,10 @@ def test_python_protocol_renders_read_result_directly_as_existing_binding():
assert len(messages) == 1
content = messages[0]["content"]
assert content.startswith("# Existing memory loaded by tool call")
- assert "preferences_1 = sdk.existing(" in content
- assert "content='Keep plugins small'" in content
+ record = json.loads(content.split("\n", 1)[1].split("\n# Memory maintenance notice:", 1)[0])
+ assert record["object_name"] == "preferences_1"
+ assert record["fields"]["content"] == "Keep plugins small"
+ assert "sdk.existing(" not in content
assert "2\\tKeep plugins small" not in content
assert "Use Vim" not in content
assert "tool_call_name" not in content
@@ -552,9 +565,23 @@ def test_python_protocol_renders_read_result_directly_as_existing_binding():
assert "content_characters" not in content
assert "choose split or compact" in content
+ reread = protocol.render_tool_result_messages(
+ context,
+ call_id="call-2",
+ tool_name="read",
+ params={"uri": uri},
+ result={"content": "Use Neovim\nKeep plugins small"},
+ source="tool call",
+ )
+ updated_record = json.loads(reread[0]["content"].split("\n", 1)[1])
+ assert updated_record["object_name"] == record["object_name"]
+ assert updated_record["fields"]["content"] == "Use Neovim\nKeep plugins small"
+ assert protocol.render_new_bindings(context, source="after reread") == ""
+
def test_python_protocol_converts_prefetch_tool_messages_without_duplicating_read_content():
uri = "viking://user/alice/memories/preferences/editor.md"
+ source_uri = "viking://user/alice/memories/trajectories/source.md"
context = _context(
[_preference_schema()],
files=[_existing_preference(uri, "editor", "Use Vim", 2)],
@@ -589,6 +616,21 @@ def test_python_protocol_converts_prefetch_tool_messages_without_duplicating_rea
),
},
]
+ prefetched.append(
+ {
+ "role": "user",
+ "content": json.dumps(
+ {
+ "tool_call_name": "read",
+ "args": {"uri": source_uri},
+ "result": {
+ "context_role": "candidate_source_trajectory",
+ "content": "Read-only source trajectory",
+ },
+ }
+ ),
+ }
+ )
messages = protocol.render_prefetch_messages(prefetched, context)
combined = "\n".join(message["content"] for message in messages)
@@ -598,11 +640,27 @@ def test_python_protocol_converts_prefetch_tool_messages_without_duplicating_rea
assert "read them with the read tool" in combined
assert f"# - {uri}" in combined
assert "search_results = [" not in combined
- # The file that was actually read becomes a system-provided existing binding.
- assert "preferences_1 = sdk.existing(" in combined
+ # Only the writable memory gets an object reference; source trajectories stay read-only.
+ assert '"object_name": "preferences_1"' in combined
+ assert "sdk.existing(" not in combined
assert combined.count("Use Vim") == 1
assert "tool_call_name" not in combined
assert '"result"' not in combined
+ assert "Read-only context; this is not a mutable memory object." in combined
+ assert protocol.binding_name(source_uri) is None
+
+ operations, error = protocol.parse(
+ 'preferences_1.content.edit(search="Use Vim", replace="Use Neovim")\nsdk.commit()',
+ context,
+ )
+ assert error is None
+ assert operations.model_dump()["preferences"][0]["content"]["blocks"] == [
+ {"search": "Use Vim", "replace": "Use Neovim"}
+ ]
+ operations, error = protocol.parse(
+ 'candidate_source_trajectory.content.update("not allowed")', context
+ )
+ assert operations is None and "unknown name" in error
def test_python_contract_uses_set_for_single_file_schema_and_create_for_collection():
@@ -642,7 +700,8 @@ def test_python_reserved_existing_retry_explains_new_replacement_binding():
"Line 3: sdk.existing() is reserved for system-provided bindings"
)
- assert "existing-object variable names already supplied by the system" in retry
+ assert "object_name from the system-provided existing-memory context record" in retry
+ assert "already-bound variable" in retry
assert "canonical = sdk.create_<type>(...)" in retry
assert "duplicate_1.delete(replacement=canonical)" in retry
@@ -758,9 +817,11 @@ def test_python_edits_aliased_field_on_existing_object():
bindings = _bind(protocol, context)
# Existing-object binding exposes the field under its identifier alias.
- assert "note_body=" in bindings
+ record = json.loads(bindings.split("\n", 1)[1])
+ assert record["fields"]["note_body"] == "Kickoff on 2023-06-09."
+ assert "note-body" not in record["fields"]
- var = bindings.split(" = ", 1)[0].strip().splitlines()[-1]
+ var = record["object_name"]
operations, error = protocol.parse(
f"""
{var}.note_body.edit(search=\"\"\"Kickoff on 2023-06-09.\"\"\", replace=\"\"\"Kickoff moved to 2023-07-01.\"\"\")
@@ -1122,7 +1183,7 @@ def test_python_existing_binding_exposes_but_does_not_update_dynamic_peer_id():
context,
)
- assert "peer_id='bob'" in bindings
+ assert json.loads(bindings.split("\n", 1)[1])["fields"]["peer_id"] == "bob"
assert error is None
item = operations.model_dump()["preferences"][0]
assert item["content"] == "Use Emacs"