fix(conversation,voice_worker): going on survives the answer starting, ignores empty finals; cancel checks before every model call; context finals stay batched

- A speaker who starts their next utterance within call.continueMs holds their request open for
  that final, also if the answer starts playing meanwhile; an empty final (rejected noise) joins
  nothing and asks nothing again
- The engine checks the cancel before the tool router, the planner and a planned tool call too
- Finals with a context are batched again: each context is cut from its end to the shortest one in
  the batch (ctranslate2 needs the start token at one position); ten finals at once 1.3 s
- Measured harsh conditions (docs/v2/stt-accuracy-plan.md): the context never made them worse

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-19 20:30:29 +09:00
parent c1b561d66d
commit 251f5e116d
7 changed files with 193 additions and 35 deletions

View File

@@ -64,3 +64,74 @@ def test_an_answer_cancelled_during_a_model_call_runs_no_tool_for_it(mock_config
# 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"])