Skip to content

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

fix(memory): fail extraction when parse retries are exhausted (#5171)

Evo0726f4df8c1d
diff --git a/openviking/session/memory/extract_loop.py b/openviking/session/memory/extract_loop.py
index ccca4ddb6..d7288fa6d 100644
--- a/openviking/session/memory/extract_loop.py
+++ b/openviking/session/memory/extract_loop.py
@@ -417,11 +417,11 @@ class ExtractLoop:
# not the console/log stream.
tracer.info(failure_message)
else:
- # ERROR: retries exhausted; this extraction will yield no operations.
+ # ERROR: no attempt remains to repair the invalid response.
tracer.error(failure_message)
- # If no retry remains, treat unparseable response as "no memory
- # operations" rather than failing hard.
+ # Preserve a valid first pass when only its resolution repair failed.
+ # Otherwise propagate failure so the archive is not marked completed.
if not retry_remaining:
if pending_resolution_repair is not None:
final_operations, raw_links = pending_resolution_repair
@@ -432,16 +432,11 @@ class ExtractLoop:
console=True,
)
break
- final_operations = ResolvedOperations(
- upsert_operations=[],
- delete_file_contents=[],
- errors=[
- "Final response could not be parsed as operations "
- f"after {max_iterations} iterations "
- f"(failure_kind={failure_kind})"
- ],
+ raise RuntimeError(
+ "Final response could not be parsed as operations "
+ f"after {max_iterations} iterations "
+ f"(failure_kind={failure_kind})"
)
- break
self._disable_tools_for_iteration = (
not self._output_protocol.keep_tools_enabled_after_parse_error(
diff --git a/openviking/session/memory/extraction_output_protocol/python_protocol.py b/openviking/session/memory/extraction_output_protocol/python_protocol.py
index 9574cd610..d47b8faa2 100644
--- a/openviking/session/memory/extraction_output_protocol/python_protocol.py
+++ b/openviking/session/memory/extraction_output_protocol/python_protocol.py
@@ -93,7 +93,7 @@ _CONTRACT_PREAMBLE = (
'single- or double-quoted literals. Inside triple quotes, escape any literal """ and '
"backslash; never put a real newline inside a single- or double-quoted string.",
"Only keyword arguments are accepted by create, set, and obj.update(); a field's update() takes one positional string. Unknown business fields are ignored.",
- "You may end the program with sdk.commit(); when present it must be the final call. Return an empty program when there are no changes.",
+ "You may end the program with sdk.commit(); when present it must be the final call. If there are no changes, return only sdk.commit().",
"Use the system-provided existing-object variable names exactly as shown. When a newly "
"created memory must be referenced by delete(replacement=...) or link(...), assign the "
"create call to a variable first, for example: canonical = sdk.create_<type>(...); "
@@ -289,7 +289,7 @@ class PythonExtractionOutputProtocol(ExtractionOutputProtocol):
return (
"You have reached the maximum number of tool call iterations. Do not call any more "
"tools. Return the complete restricted Python memory SDK program now. Output only "
- "Python code. If there are no changes, return an empty program."
+ "Python code. If there are no changes, return only sdk.commit()."
)
def render_format_retry(self, error: str | None = None) -> str:
diff --git a/tests/session/memory/test_memory_react.py b/tests/session/memory/test_memory_react.py
index daa055fa6..4b40addc3 100644
--- a/tests/session/memory/test_memory_react.py
+++ b/tests/session/memory/test_memory_react.py
@@ -478,9 +478,8 @@ class TestExtractLoopFinalJsonRetry:
"openviking.session.memory.extract_loop.get_openviking_config",
return_value=config,
):
- result, _ = await extract_loop.run()
- assert result.errors
- assert "Final response could not be parsed" in result.errors[0]
+ with pytest.raises(RuntimeError, match="Final response could not be parsed"):
+ await extract_loop.run()
final_prompts = [
message["content"]
@@ -507,7 +506,7 @@ class TestExtractLoopFinalJsonRetry:
assert "not inferring scope from the file name/topic" in initial_system_prompt
@pytest.mark.asyncio
- async def test_python_protocol_retries_then_degrades_on_invalid_program(self):
+ async def test_python_protocol_retries_then_raises_on_empty_response(self):
class FakeVLM:
model = "test-model"
@@ -516,7 +515,7 @@ class TestExtractLoopFinalJsonRetry:
async def get_completion_async(self, **kwargs):
self.seen_messages.append(list(kwargs["messages"]))
- return "this is not a valid SDK program"
+ return ""
class FakeContextProvider:
read_file_contents = {}
@@ -574,12 +573,10 @@ class TestExtractLoopFinalJsonRetry:
extract_loop._validate_patch_operations = AsyncMock(return_value=[])
extract_loop.finalize_operations = AsyncMock()
- result, _ = await extract_loop.run()
+ with pytest.raises(RuntimeError, match="failure_kind=empty_response"):
+ await extract_loop.run()
- assert result.upsert_operations == []
- assert result.delete_file_contents == []
- assert result.errors
- assert "Final response could not be parsed" in result.errors[0]
+ extract_loop.finalize_operations.assert_not_awaited()
assert len(vlm.seen_messages) == 2
system_prompt = vlm.seen_messages[0][0]["content"]