"""An answer that is no longer wanted stops at the engine's next model or tool call (turn_state.cancel). The voice worker sets it when the app takes the request back (the asker went on talking and the request is joined to what they said next, a newer request took its place, or a stop). The engine then returns no answer and runs no tool on the request's behalf. """ from __future__ import annotations from unittest.mock import patch import pytest def _native_call(name: str, args: dict) -> dict: return {"message": {"role": "assistant", "content": "", "tool_calls": [ {"id": "call_1", "type": "function", "function": {"name": name, "arguments": args}}]}} def _run(mock_config, db, dialogue_memory, cancel_after_calls: int): from jarvis.memory.history import HistoryContext from jarvis.reply import engine as engine_mod from jarvis.reply import turn_state from jarvis.tools.types import ToolExecutionResult mock_config.ollama_chat_model = "gpt-oss:20b" mock_config.evaluator_enabled = False mock_config.stream_browser = False calls = [] invoked = [] def fake_chat(*args, **kwargs): calls.append(1) return _native_call("getWeather", {"location": "Seoul"}) def fake_tool(db, cfg, tool_name, tool_args, **kwargs): invoked.append(tool_name) return ToolExecutionResult(success=True, reply_text="Seoul: 13C", error_message=None) turn_state.reset() turn_state.set_cancel(lambda: len(calls) >= cancel_after_calls) try: with patch.object(engine_mod, "run_tool_with_retries", side_effect=fake_tool), \ patch.object(engine_mod, "chat_with_messages", side_effect=fake_chat), \ patch.object(engine_mod, "select_tools", return_value=["getWeather", "stop"]), \ patch.object(engine_mod, "plan_query", return_value=["Reply to the user."]): reply = engine_mod.run_reply_engine( db=db, cfg=mock_config, tts=None, text="서울 날씨 어때?", dialogue_memory=dialogue_memory, history=HistoryContext(), legacy_memory=False, intent_judge=True) finally: turn_state.reset() return reply, len(calls), invoked @pytest.mark.unit def test_an_answer_cancelled_before_it_starts_makes_no_model_call(mock_config, db, dialogue_memory): reply, calls, invoked = _run(mock_config, db, dialogue_memory, cancel_after_calls=0) assert reply is None and calls == 0 and invoked == [] @pytest.mark.unit def test_an_answer_cancelled_during_a_model_call_runs_no_tool_for_it(mock_config, db, dialogue_memory): # The model asks for a tool, but the answer was taken back meanwhile: the tool is not run. reply, calls, invoked = _run(mock_config, db, dialogue_memory, cancel_after_calls=1) assert reply is None and calls == 1 and invoked == [] @pytest.mark.unit def test_a_planned_tool_is_not_run_when_the_answer_is_cancelled_while_its_step_is_resolved( mock_config, db, dialogue_memory): # Small models run planned steps directly: the step resolver (a model call) returns a tool call, # but the answer was taken back meanwhile. from jarvis.reply import engine as engine_mod from jarvis.reply import turn_state mock_config.ollama_chat_model = "gemma4:e2b" mock_config.evaluator_enabled = False cancelled = [False] invoked = [] def fake_resolve(*args, **kwargs): cancelled[0] = True return ("webSearch", {"query": "x"}) plan = ["webSearch query='a'", "webSearch query='b from step 1'", "Reply to the user."] turn_state.reset() turn_state.set_cancel(lambda: cancelled[0]) try: with patch.object(engine_mod, "run_tool_with_retries", side_effect=lambda *a, **k: invoked.append(1)), \ patch.object(engine_mod, "chat_with_messages", side_effect=AssertionError("no model call")), \ patch.object(engine_mod, "select_tools", return_value=["webSearch", "stop"]), \ patch.object(engine_mod, "extract_search_params_for_memory", return_value={"keywords": []}), \ patch.object(engine_mod, "plan_query", return_value=plan), \ patch.object(engine_mod, "_resolve_plan_step", side_effect=fake_resolve): reply = engine_mod.run_reply_engine(db=db, cfg=mock_config, tts=None, text="a then b", dialogue_memory=dialogue_memory) finally: turn_state.reset() assert reply is None and invoked == [] @pytest.mark.unit @pytest.mark.parametrize("cancel_in", ["select_tools", "plan_query"]) def test_a_cancel_during_routing_or_planning_stops_before_the_next_call(mock_config, db, dialogue_memory, cancel_in): from jarvis.reply import engine as engine_mod from jarvis.reply import turn_state mock_config.ollama_chat_model = "gpt-oss:20b" mock_config.evaluator_enabled = False cancelled = [False] calls = [] def route(*a, **k): calls.append("route") cancelled[0] = cancel_in == "select_tools" return ["webSearch", "stop"] def plan(*a, **k): calls.append("plan") cancelled[0] = True return ["webSearch query='a'", "Reply to the user."] turn_state.reset() turn_state.set_cancel(lambda: cancelled[0]) try: with patch.object(engine_mod, "chat_with_messages", side_effect=lambda *a, **k: calls.append("chat")), \ patch.object(engine_mod, "run_tool_with_retries", side_effect=lambda *a, **k: calls.append("tool")), \ patch.object(engine_mod, "select_tools", side_effect=route), \ patch.object(engine_mod, "plan_query", side_effect=plan): reply = engine_mod.run_reply_engine(db=db, cfg=mock_config, tts=None, text="날씨 찾아 줘", dialogue_memory=dialogue_memory) finally: turn_state.reset() assert reply is None assert calls == (["route"] if cancel_in == "select_tools" else ["route", "plan"])