diff --git a/scripts/ollama-tool-proxy.py b/scripts/ollama-tool-proxy.py index 0e08d0e..95d7437 100644 --- a/scripts/ollama-tool-proxy.py +++ b/scripts/ollama-tool-proxy.py @@ -136,6 +136,14 @@ def last_bash_command(messages): return "" +def seen_bash_command(message, history): + """Detect a model retry of an earlier command, not just the last one.""" + for entry in history: + if isinstance(entry, dict) and same_as_last_call(message, [entry]): + return True + return False + + def complete(request, bash_mode): """Retry an initial prose-only answer once; never fabricate a tool call.""" if bash_mode: @@ -192,7 +200,7 @@ def complete(request, bash_mode): "Do not request the procedure again or repeat the PV lookup."}] retried = translate(query_ollama(retry), request["tools"]) print("resource_decision_retry", "tool_call=" + str(bool(retried["choices"][0]["message"].get("tool_calls"))), flush=True) - if same_as_last_call(retried["choices"][0]["message"], messages): + if seen_bash_command(retried["choices"][0]["message"], messages): retried["choices"][0]["message"] = {"role": "assistant", "content": "JEANNIE_STOP_REPEATED_TOOL_CALL: Holmes repeated the preceding command; investigation is incomplete."} retried["choices"][0]["finish_reason"] = "stop" @@ -209,7 +217,7 @@ def complete(request, bash_mode): retried = translate(query_ollama(retry), request["tools"]) accepted = safe_initial_call(retried["choices"][0]["message"], request["tools"], users[-1]["content"]) print("resource_extract_retry", "accepted=" + str(accepted), flush=True) - if accepted and not same_as_last_call(retried["choices"][0]["message"], messages): + if accepted and not seen_bash_command(retried["choices"][0]["message"], messages): return retried if bash_mode and same_as_last_call(message, messages): retry = dict(request) diff --git a/tests/jeannie/test_ollama_tool_call_adapter.py b/tests/jeannie/test_ollama_tool_call_adapter.py index 6b77eec..b00a4ee 100644 --- a/tests/jeannie/test_ollama_tool_call_adapter.py +++ b/tests/jeannie/test_ollama_tool_call_adapter.py @@ -117,6 +117,14 @@ class ToolAdapterTests(unittest.TestCase): messages = [{"role": "assistant", "tool_calls": [{"function": {"name": "bash", "arguments": json.dumps({"command": "kubectl -n n8n get pvc n8n-data"})}}]}, {"role": "tool", "content": "ok"}] self.assertEqual(PROXY.last_bash_command(messages), "kubectl -n n8n get pvc n8n-data") + def test_seen_bash_command_checks_earlier_calls(self): + def call(command): + return {"function": {"name": "bash", "arguments": json.dumps({"command": command})}} + history = [{"role": "assistant", "tool_calls": [call("kubectl get pvc")]}, + {"role": "assistant", "tool_calls": [call("kubectl get pv")]}] + self.assertTrue(PROXY.seen_bash_command({"tool_calls": [call("kubectl get pvc")]}, history)) + self.assertFalse(PROXY.seen_bash_command({"tool_calls": [call("kubectl get pod")]}, history)) + def test_retry_cannot_turn_a_plan_into_a_mutating_command(self): tools = [{"type": "function", "function": {"name": "bash", "parameters": { "type": "object", "properties": {"command": {"type": "string"},