Skip to content

Personal AI, On Personal Devices

Python

fix: preserve completed agent turn effects (#884)

Elliot Sluskya63fb17a7132
diff --git a/src/openjarvis/agents/executor.py b/src/openjarvis/agents/executor.py
index f0f438d5..8315b841 100644
--- a/src/openjarvis/agents/executor.py
+++ b/src/openjarvis/agents/executor.py
@@ -36,6 +36,12 @@ _MAX_RETRIES = 3
_AGENT_TICK_DEFAULT_MODEL = "gemma4:31b"
+def _should_retry_empty_result(result: AgentResult) -> bool:
+ """Retry only a genuinely empty turn with no completed tool effects."""
+
+ return not (result.content or "").strip() and not result.tool_results
+
+
def _resolve_tick_model(config: dict[str, Any], system: Any) -> str:
"""Resolve a managed tick model without hiding the system default."""
@@ -678,7 +684,7 @@ class AgentExecutor:
# Retry once if the model returned empty content (common with
# Qwen3.5 thinking mode consuming all tokens).
- if not (result.content or "").strip():
+ if _should_retry_empty_result(result):
self._set_activity(
agent["id"],
"Retrying (empty response)...",
diff --git a/src/openjarvis/agents/monitor_operative.py b/src/openjarvis/agents/monitor_operative.py
index d4cf2580..9113c7e8 100644
--- a/src/openjarvis/agents/monitor_operative.py
+++ b/src/openjarvis/agents/monitor_operative.py
@@ -740,9 +740,11 @@ class MonitorOperativeAgent(ToolUsingAgent):
"""Auto-persist a state summary if agent didn't store explicitly."""
if not self._memory_backend or not self._operator_id:
return
+ if not content or not content.strip():
+ return
state_key = f"monitor_operative:{self._operator_id}:state"
try:
- summary = content[:1000] if content else ""
+ summary = content[:1000]
self._memory_backend.store(state_key, summary)
except Exception:
logger.debug(
diff --git a/src/openjarvis/agents/operative.py b/src/openjarvis/agents/operative.py
index 0e9a0529..c16163dc 100644
--- a/src/openjarvis/agents/operative.py
+++ b/src/openjarvis/agents/operative.py
@@ -309,10 +309,12 @@ class OperativeAgent(ToolUsingAgent):
"""Auto-persist a state summary if the agent didn't store state explicitly."""
if not self._memory_backend or not self._operator_id:
return
+ if not content or not content.strip():
+ return
state_key = f"operator:{self._operator_id}:state"
try:
# Store a summary of the agent's response as state
- summary = content[:1000] if content else ""
+ summary = content[:1000]
self._memory_backend.store(state_key, summary)
except Exception:
logger.debug(
diff --git a/tests/agents/test_executor.py b/tests/agents/test_executor.py
index 1f108135..8d9f1eba 100644
--- a/tests/agents/test_executor.py
+++ b/tests/agents/test_executor.py
@@ -11,6 +11,7 @@ import pytest
from openjarvis.agents._stubs import AgentResult
from openjarvis.agents.errors import FatalError, RetryableError
from openjarvis.core.events import EventBus, EventType
+from openjarvis.core.types import ToolResult
@pytest.fixture
@@ -140,6 +141,18 @@ class TestExecutorBasic:
assert manager.get_agent(agent["id"])["status"] == "running"
+def test_empty_turn_with_tool_result_is_not_retried() -> None:
+ from openjarvis.agents.executor import _should_retry_empty_result
+
+ result = AgentResult(
+ content=" ",
+ tool_results=[ToolResult(tool_name="channel_send", content="sent")],
+ )
+
+ assert _should_retry_empty_result(result) is False
+ assert _should_retry_empty_result(AgentResult(content="")) is True
+
+
def test_finalize_tick_reads_agent_result_metadata(tmp_path):
"""_finalize_tick() accumulates cost/tokens from AgentResult.metadata."""
from openjarvis.agents.executor import AgentExecutor
diff --git a/tests/agents/test_monitor_operative.py b/tests/agents/test_monitor_operative.py
index dedb0826..15efbbcb 100644
--- a/tests/agents/test_monitor_operative.py
+++ b/tests/agents/test_monitor_operative.py
@@ -63,3 +63,16 @@ class TestMonitorOperativeAgent:
result = agent.run("What is the answer?")
assert result.content == "The answer is 42."
assert result.turns >= 1
+
+ def test_blank_result_does_not_replace_persisted_state(self) -> None:
+ memory = MagicMock()
+ agent = MonitorOperativeAgent(
+ _make_engine(),
+ "test-model",
+ operator_id="watcher",
+ memory_backend=memory,
+ )
+
+ agent._auto_persist_state(" \n\t")
+
+ memory.store.assert_not_called()
diff --git a/tests/operators/test_operators.py b/tests/operators/test_operators.py
index 6e965748..75a2b503 100644
--- a/tests/operators/test_operators.py
+++ b/tests/operators/test_operators.py
@@ -648,6 +648,23 @@ class TestOperativeAgent:
state = memory.retrieve("operator:persist_test:state")
assert "Tick complete." in state
+ def test_blank_result_preserves_previous_state(self):
+ from openjarvis.agents.operative import OperativeAgent
+
+ memory = FakeMemoryBackend()
+ state_key = "operator:persist_test:state"
+ memory.store(state_key, "waiting for a reply")
+ agent = OperativeAgent(
+ FakeEngine(),
+ "test-model",
+ operator_id="persist_test",
+ memory_backend=memory,
+ )
+
+ agent._auto_persist_state(" \n\t")
+
+ assert memory.retrieve(state_key) == "waiting for a reply"
+
def test_max_turns_exceeded(self):
from openjarvis.agents.operative import OperativeAgent