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>
This commit is contained in:
javis-bot
2026-11-19 18:41:50 +09:00
parent 97b827f424
commit caad09c613
14 changed files with 449 additions and 50 deletions

View File

@@ -0,0 +1,66 @@
"""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 == []

View File

@@ -29,8 +29,9 @@ class FakeBrain:
self.calls = []
def reply(self, user, text, history, chat=None, schedule=None, task=False, notes=None, notes_backend=None,
screen=None, pc=None, trace=None):
screen=None, pc=None, trace=None, cancelled=None):
self.calls.append((user, text, history))
self.cancelled = cancelled
if trace is not None:
trace.append({"kind": "tools", "names": ["getWeather"]})
self.chat, self.schedule, self.task = chat, schedule, task
@@ -730,6 +731,83 @@ def test_a_reply_cancelled_while_the_brain_thinks_is_not_spoken(store):
assert replies == ["r1", "r2"], "the text still comes back; only the speech is skipped"
@pytest.mark.unit
def test_a_cancelled_reply_stops_the_brain_and_one_still_waiting_never_reaches_it(store):
"""A request taken back (the asker went on talking) must not hold up the next one: the brain is
told to stop the running answer, and a reply still waiting for it is never started."""
import time
started = []
def answer(user, text):
started.append(text)
if text == "first":
t0 = time.monotonic()
while not brain.cancelled() and time.monotonic() - t0 < 10:
time.sleep(0.01)
return None if brain.cancelled() else "late answer"
return f"answer for {text}"
brain = FakeBrain(answer)
async def body(url):
async with connect(url) as ws:
await hello(ws)
await send(ws, type="voice_join", channel="vc1")
await send(ws, type="reply", id="r1", user="100", text="first", via="voice", speak=True)
await asyncio.sleep(0.1)
await send(ws, type="reply", id="r2", user="100", text="second", via="voice", speak=True)
await send(ws, type="tts_cancel", id="r1")
await send(ws, type="tts_cancel", id="r2")
await send(ws, type="reply", id="r3", user="100", text="first second", via="voice", speak=True)
t0 = time.monotonic()
seen = await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "tts_done"
and m["id"] == "r3")
return seen, time.monotonic() - t0
seen, waited = asyncio.run(with_worker(body, store=store, brain=brain, tts=FakeTts()))
assert waited < 3, "the joined request did not wait for the cancelled answer to run out"
assert started == ["first", "first second"]
done = {m["id"]: m for m in seen if isinstance(m, dict) and m.get("type") == "tts_done"}
assert done["r1"]["cancelled"] and done["r2"]["cancelled"] and not done["r3"]["cancelled"]
frames = {decode_tts_frame(m)[0] for m in seen if isinstance(m, bytes)}
assert frames == {"r3"}
@pytest.mark.unit
def test_a_request_taken_back_does_not_use_up_the_turn_a_confirmation_needs(store):
"""Jarvis asked something; the asker says "yes" and goes on ("yes, quickly"). The app takes the
"yes" back and sends the joined text: it must still be the very next turn, or a confirmation
(pcControl, chat delete) would lapse."""
import time
turns = {}
def answer(user, text):
turns[text] = brain.pc.turn
if text == "yes":
t0 = time.monotonic()
while not brain.cancelled() and time.monotonic() - t0 < 10:
time.sleep(0.01)
return None
return f"answer for {text}"
brain = FakeBrain(answer)
async def body(url):
async with connect(url) as ws:
await hello(ws)
await send(ws, type="voice_join", channel="vc1")
await send(ws, type="reply", id="r1", user="100", text="open the calculator", via="voice", speak=False)
await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "reply_text")
await send(ws, type="reply", id="r2", user="100", text="yes", via="voice", speak=True)
await asyncio.sleep(0.1)
await send(ws, type="tts_cancel", id="r2")
await send(ws, type="reply", id="r3", user="100", text="yes quickly", via="voice", speak=True)
await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "tts_done" and m["id"] == "r3")
asyncio.run(with_worker(body, store=store, brain=brain, tts=FakeTts()))
assert turns["yes quickly"] == turns["open the calculator"] + 1
@pytest.mark.unit
def test_the_brains_judgements_use_its_model_and_map_back_to_ids(monkeypatch):
import voice_worker.judges as judges