Files
javis_bot/tests/test_engine_cancel.py
javis-bot caad09c613 feat(conversation): what is said in pieces is answered once, and a dropped answer stops the brain
Live 2026-11-19 every piece of one thought (pauses of 0.1-4.7 s) was answered on its own and the
brain wrote each cancelled answer to its end, so the replies queued up to 35 s.

- conversation: a speaker who goes on before their answer is heard (within call.continueMs,
  default 5 s) has the earlier request taken back (policy withdraw, not missed) and the joined
  text answered as one request; a stop still stops; no second earcon; partials prepare the joined
  text
- voice worker: a reply cancelled while waiting is never started, a running one stops at the
  engine's next model or tool call (turn_state.cancel), and it uses up no turn, so a confirmation
  carried by the joined request does not lapse
- specs: conversation.spec.md (Going on), voice_worker.spec.md, llm_contexts.md

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 18:41:50 +09:00

67 lines
2.8 KiB
Python

"""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 == []