"""The repeat guard does not limit tools that operate something step by step (reply.spec.md, "Repeat guard"): pressing calculator keys is many pcControl calls, the same one can be meant twice.""" from __future__ import annotations from unittest.mock import patch import pytest def _call(i, name, args): return {"message": {"role": "assistant", "content": "", "tool_calls": [ {"id": f"c{i}", "type": "function", "function": {"name": name, "arguments": args}}]}} def _run(mock_config, db, dialogue_memory, name, calls): from jarvis.reply import engine as engine_mod from jarvis.tools.types import ToolExecutionResult mock_config.ollama_chat_model = "gpt-oss:20b" mock_config.evaluator_enabled = False mock_config.stream_browser = False replies = iter([_call(i, name, a) for i, a in enumerate(calls)] + [{"message": {"role": "assistant", "content": "done"}}]) ran = [] def fake_tool(db, cfg, tool_name, tool_args, **kwargs): ran.append(tool_args) return ToolExecutionResult(success=True, reply_text="ok", error_message=None) with patch.object(engine_mod, "run_tool_with_retries", side_effect=fake_tool), \ patch.object(engine_mod, "chat_with_messages", side_effect=lambda *a, **k: next(replies)), \ patch.object(engine_mod, "select_tools", return_value=[name, "stop"]), \ patch.object(engine_mod, "plan_query", return_value=["Reply to the user."]): engine_mod.run_reply_engine(db=db, cfg=mock_config, tts=None, text="press it", dialogue_memory=dialogue_memory) return ran KEYS = [{"action": "click", "ref": "@s1:e2"}, {"action": "click", "ref": "@s1:e2"}, {"action": "click", "ref": "@s1:e11"}, {"action": "click", "ref": "@s1:e4"}] @pytest.mark.unit def test_every_step_of_pc_control_runs_even_the_same_one_twice(mock_config, db, dialogue_memory): assert _run(mock_config, db, dialogue_memory, "pcControl", KEYS) == KEYS @pytest.mark.unit def test_other_tools_are_still_held_to_the_repeat_guard(mock_config, db, dialogue_memory): q = [{"location": "Seoul"}, {"location": "Seoul"}, {"location": "Busan"}, {"location": "Jeju"}] assert _run(mock_config, db, dialogue_memory, "getWeather", q) == q[:1] + q[2:3]