perf(voice): ask the done-by-user question while the user still speaks
The done-by-user guard asked its first yes/no question after every follow-up answer was made, adding a short LLM call (~0.4 s) before Jarvis spoke. The question depends only on the user's words, so the voice worker's prepare now asks it on the partial transcript after the tool pick (prepare_done_by_user, follow-up turns only) and the guard uses that judgement (prepared_done), asking only whether the answer refuses when the user said they did it. Without a prepared judgement, or when it failed, the guard asks for itself as before. The reply does not wait for the judgement, only the guard does. Measured on Qwen3.6 through the game PC's Strata, 12 pairs of short follow-up questions in alternating order: brain time median 1.06 s before, 0.69 s after. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -29,7 +29,7 @@ def _content(text: str) -> dict:
|
||||
|
||||
|
||||
def _run(mock_config, db, dialogue_memory, *, text, chat_replies, done="yes", earlier=True, tool_first=False,
|
||||
tool_text="ok", others=()):
|
||||
tool_text="ok", others=(), prepared_done=None):
|
||||
"""``done``: the judge's word for "the user says they did it"; a reply is judged a refusal when
|
||||
it is one of REFUSALS. ``tool_text``: what the tool returns; ``others``: history lines of other
|
||||
people, after Jarvis's answer."""
|
||||
@@ -71,7 +71,7 @@ def _run(mock_config, db, dialogue_memory, *, text, chat_replies, done="yes", ea
|
||||
patch("jarvis.reply.done_guard.call_llm_direct", side_effect=fake_judge):
|
||||
reply = engine_mod.run_reply_engine(
|
||||
db=db, cfg=mock_config, tts=None, text=text, dialogue_memory=dialogue_memory,
|
||||
history=history, legacy_memory=False, intent_judge=True)
|
||||
history=history, legacy_memory=False, intent_judge=True, prepared_done=prepared_done)
|
||||
return reply, seen, judged
|
||||
|
||||
|
||||
@@ -170,3 +170,43 @@ def test_only_jarvis_lines_leave_the_history(mock_config, db, dialogue_memory):
|
||||
assert reply == SPOKEN
|
||||
assert quoted in _said(seen[1])
|
||||
assert "자비스: " + REFUSAL not in _said(seen[1])
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
@pytest.mark.parametrize("said, expected", [(True, SPOKEN), (False, REFUSAL)], ids=["did it", "did not"])
|
||||
def test_a_judgement_made_ahead_is_not_asked_again(mock_config, db, dialogue_memory, said, expected):
|
||||
"""The prepare stage judged the words while the user was still speaking (latency, reply.spec.md):
|
||||
the guard uses that judgement and asks only whether the answer refuses."""
|
||||
reply, seen, judged = _run(mock_config, db, dialogue_memory, text=DID_IT,
|
||||
chat_replies=[_content(REFUSAL), _content(SPOKEN)], done="no" if said else "yes",
|
||||
prepared_done=lambda: said)
|
||||
assert reply == expected
|
||||
assert not [j for j in judged if j.startswith("<utterance>")]
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_no_judgement_made_ahead_asks_for_itself(mock_config, db, dialogue_memory):
|
||||
reply, seen, judged = _run(mock_config, db, dialogue_memory, text=DID_IT,
|
||||
chat_replies=[_content(REFUSAL), _content(SPOKEN)], prepared_done=lambda: None)
|
||||
assert reply == SPOKEN
|
||||
assert DID_IT in judged[0]
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_said_done_ahead_judges_only_a_follow_up(mock_config, dialogue_memory):
|
||||
"""No earlier answer: no guard would run, so nothing is asked ahead either."""
|
||||
from jarvis.reply import engine as engine_mod
|
||||
|
||||
asked = []
|
||||
|
||||
def fake_judge(base_url, model, system, user_content, **kwargs):
|
||||
asked.append(user_content)
|
||||
return "yes"
|
||||
|
||||
with patch("jarvis.reply.done_guard.call_llm_direct", side_effect=fake_judge):
|
||||
assert engine_mod.prepare_done_by_user(mock_config, DID_IT, dialogue_memory) is None
|
||||
assert asked == []
|
||||
dialogue_memory.add_message("user", QUESTION)
|
||||
dialogue_memory.add_message("assistant", REFUSAL)
|
||||
assert engine_mod.prepare_done_by_user(mock_config, DID_IT, dialogue_memory) is True
|
||||
assert len(asked) == 1 and DID_IT in asked[0]
|
||||
|
||||
@@ -1126,14 +1126,20 @@ def _jarvis_brain(tmp_path, monkeypatch, plans):
|
||||
seen["judged"].append((text, chat_available))
|
||||
return plans.get(text)
|
||||
|
||||
def fake_engine(db, cfg, tts, text, mem, prepared_tools=None, intent_judge=False, **kw):
|
||||
def fake_engine(db, cfg, tts, text, mem, prepared_tools=None, intent_judge=False, prepared_done=None, **kw):
|
||||
seen["prepared"].append(prepared_tools)
|
||||
seen["intent_judge"] = intent_judge
|
||||
seen.setdefault("done", []).append(prepared_done() if prepared_done is not None else "not passed")
|
||||
mem.add_message("user", text)
|
||||
mem.add_message("assistant", "ok")
|
||||
return "ok"
|
||||
|
||||
def fake_done(cfg, text, mem):
|
||||
seen.setdefault("done_asked", []).append(text)
|
||||
return True
|
||||
|
||||
monkeypatch.setattr(engine, "prepare_intent", fake_prepare)
|
||||
monkeypatch.setattr(engine, "prepare_done_by_user", fake_done)
|
||||
monkeypatch.setattr(engine, "run_reply_engine", fake_engine)
|
||||
return brain_mod.JarvisBrain(), seen
|
||||
|
||||
@@ -1170,6 +1176,55 @@ def test_a_prepared_pick_is_not_used_for_other_words_another_person_or_a_changed
|
||||
assert seen["prepared"][-1] is None
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_a_reply_uses_the_done_by_user_judgement_prepared_for_its_text(tmp_path, monkeypatch):
|
||||
"""The done-by-user guard's first question is asked while the user still speaks, so a follow-up
|
||||
turn does not wait for it after the answer (reply.spec.md, "Earlier answers after the user did it")."""
|
||||
from jarvis.memory.history import HistoryContext
|
||||
|
||||
b, seen = _jarvis_brain(tmp_path, monkeypatch, {"자비스 속도 조절했으니 말해 봐": []})
|
||||
b.prepare("100", "자비스 속도 조절했으니 말해 봐", False)
|
||||
assert seen["done_asked"] == ["자비스 속도 조절했으니 말해 봐"]
|
||||
b.reply("100", "자비스, 속도 조절했으니 말해 봐", HistoryContext())
|
||||
assert seen["done"] == [True]
|
||||
# Not prepared: the guard asks for itself.
|
||||
b.reply("100", "자비스, 속도 조절했으니 말해 봐", HistoryContext())
|
||||
assert seen["done"][-1] == "not passed"
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_a_slow_done_judgement_does_not_hold_up_the_prepared_pick(tmp_path, monkeypatch):
|
||||
"""The reply starts with the prepared tools at once; only the guard waits for the judgement."""
|
||||
import threading
|
||||
import time as _time
|
||||
from jarvis.memory.history import HistoryContext
|
||||
import jarvis.reply.engine as engine
|
||||
|
||||
b, seen = _jarvis_brain(tmp_path, monkeypatch, {"자비스 날씨 어때": ["getWeather"]})
|
||||
release = threading.Event()
|
||||
|
||||
def slow_done(cfg, text, mem):
|
||||
release.wait(5)
|
||||
return False
|
||||
monkeypatch.setattr(engine, "prepare_done_by_user", slow_done)
|
||||
started = {}
|
||||
|
||||
def fake_engine(db, cfg, tts, text, mem, prepared_tools=None, prepared_done=None, **kw):
|
||||
started["tools"] = prepared_tools
|
||||
release.set() # the answer is being made while the judgement finishes
|
||||
started["done"] = prepared_done()
|
||||
return "ok"
|
||||
monkeypatch.setattr(engine, "run_reply_engine", fake_engine)
|
||||
t = threading.Thread(target=b.prepare, args=("100", "자비스 날씨 어때", False))
|
||||
t.start()
|
||||
_time.sleep(0.1)
|
||||
t0 = _time.monotonic()
|
||||
b.reply("100", "자비스 날씨 어때?", HistoryContext())
|
||||
t.join()
|
||||
assert started == {"tools": ["getWeather"], "done": False}
|
||||
assert _time.monotonic() - t0 < 2
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_a_reply_waits_for_a_pick_still_being_prepared_for_its_text(tmp_path, monkeypatch):
|
||||
import threading
|
||||
|
||||
Reference in New Issue
Block a user