feat(voice_worker): a short voice prompt and one judgement before each v2 answer

- compact_prompt: the v2 persona in a tenth of v1's words, answers as short as
  possible, the full tool guidance only on turns with a data tool. A greeting's
  reply prompt went from about 4,500 to 1,400 tokens on Strata.
- intent_judge: the tool router judges whether the turn needs tools; the
  planner runs only when it does. prepare_intent runs the pick ahead on a
  partial transcript (worker message prepare), and the reply reuses it.
- Without stream_browser the screen-share tools are not offered.
- The time context carries the 12-hour form (10:37 was read as pm).
- A <tool_call> block written as the reply is parsed as a call, not spoken.

Evals (Qwen3.6 on Strata, EVAL_COMPACT_PROMPT=1 EVAL_INTENT_JUDGE=1):
221 passed, 4 failed (known failures); new evals/test_voice_reply_length.py
(v2 answers 234 chars against v1's 416) and test_reply_language_default.py.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-12 12:25:15 +09:00
parent 7623d6d002
commit 46e4e75d05
13 changed files with 721 additions and 44 deletions

View File

@@ -372,9 +372,11 @@ def test_the_brain_answers_in_korean_by_default_even_with_a_v1_english_voice(tmp
seen = {}
def fake_engine(db, cfg, tts, text, mem, language=None, history=None, legacy_memory=True,
default_language=None):
default_language=None, compact_prompt=False, **kw):
seen["directive"] = reply_language_directive(None, cfg.tts_engine, default_language)
seen["language"] = language
seen["compact"] = compact_prompt
seen["stream_browser"] = cfg.stream_browser
return "ok"
import jarvis.reply.engine as engine
@@ -382,6 +384,8 @@ def test_the_brain_answers_in_korean_by_default_even_with_a_v1_english_voice(tmp
brain_mod.JarvisBrain().reply("100", "what time is it?", HistoryContext())
assert seen["directive"] == default_language_directive("Korean")
assert seen["language"] == "ko"
# The short voice prompt, and no v1 screen-share browser (it needs v1's Node bot).
assert seen["compact"] is True and seen["stream_browser"] is False
# Without a default the brain answers in the asker's language.
brain_mod.JarvisBrain(language=None, reply_language=None).reply("100", "q", HistoryContext())
assert seen["directive"] is None and seen["language"] is None
@@ -966,3 +970,138 @@ def test_a_chat_question_the_app_never_reports_counts_as_not_posted(store):
actions, results = run_delete_then_yes(store, dict(via="chat", speak=False), no_report,
delivery_timeout_s=0.1)
assert actions == [] and results == [("지워", True), ("응, 지워", False)]
class PreparingBrain(FakeBrain):
"""Each prepare waits until the test lets it finish."""
def __init__(self):
super().__init__()
import threading
self.prepared = []
self.release = threading.Event()
def prepare(self, user, text, chat):
self.prepared.append((user, text, chat))
self.release.wait(5)
@pytest.mark.unit
def test_prepare_runs_one_at_a_time_per_user_and_only_the_newest_different_text_waits(store):
brain = PreparingBrain()
async def body(url):
async with connect(url) as ws:
await hello(ws, stt={"partials": False})
await send(ws, type="prepare", user="100", text="자비스 오늘")
await asyncio.sleep(0.2)
await send(ws, type="prepare", user="100", text="자비스 오늘 서울")
await send(ws, type="prepare", user="100", text="자비스 오늘 서울 날씨 어때")
await send(ws, type="prepare", user="200", text="자비스 안녕")
await asyncio.sleep(0.2)
assert sorted(brain.prepared) == [("100", "자비스 오늘", False), ("200", "자비스 안녕", False)]
brain.release.set()
await asyncio.sleep(0.3)
# About the same as the one just prepared: not prepared again.
await send(ws, type="prepare", user="100", text="자비스 오늘 서울 날씨 어때?")
await asyncio.sleep(0.3)
await send(ws, type="reply", id="r1", user="100", text="x", via="voice", speak=False)
return await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "reply_text")
seen = asyncio.run(with_worker(body, store=store, brain=brain))
assert [t for u, t, _ in brain.prepared if u == "100"] == ["자비스 오늘", "자비스 오늘 서울 날씨 어때"]
assert not [m for m in seen if isinstance(m, dict) and m.get("type") == "error"], "nothing is sent back"
@pytest.mark.unit
def test_a_brain_without_prepare_ignores_it(store):
async def body(url):
async with connect(url) as ws:
await hello(ws, stt={"partials": False})
await send(ws, type="prepare", user="100", text="자비스 안녕")
await send(ws, type="reply", id="r1", user="100", text="hi", via="voice", speak=False)
return await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "reply_text")
seen = asyncio.run(with_worker(body, store=store, brain=FakeBrain()))
assert not [m for m in seen if isinstance(m, dict) and m.get("type") == "error"]
def _jarvis_brain(tmp_path, monkeypatch, plans):
"""A real JarvisBrain with the engine's judge and reply replaced: ``plans`` maps text to a pick."""
pytest.importorskip("mcp") # the real brain needs the jarvis dependencies (--brain)
from voice_worker import brain as brain_mod
import jarvis.reply.engine as engine
cfg = tmp_path / "config.json"
cfg.write_text(json.dumps({"db_path": str(tmp_path / "jarvis.db")}))
monkeypatch.setenv("JARVIS_CONFIG_PATH", str(cfg))
seen = {"judged": [], "prepared": []}
def fake_prepare(cfg, text, mem, chat_available):
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):
seen["prepared"].append(prepared_tools)
seen["intent_judge"] = intent_judge
mem.add_message("user", text)
mem.add_message("assistant", "ok")
return "ok"
monkeypatch.setattr(engine, "prepare_intent", fake_prepare)
monkeypatch.setattr(engine, "run_reply_engine", fake_engine)
return brain_mod.JarvisBrain(), seen
@pytest.mark.unit
def test_a_reply_uses_the_pick_prepared_for_about_the_same_text(tmp_path, monkeypatch):
from jarvis.memory.history import HistoryContext
plan = ["getWeather"]
b, seen = _jarvis_brain(tmp_path, monkeypatch, {"자비스 오늘 서울 날씨 어": plan})
b.prepare("100", "자비스 오늘 서울 날씨 어", True)
assert seen["judged"] == [("자비스 오늘 서울 날씨 어", True)]
b.reply("100", "자비스, 오늘 서울 날씨 어때?", HistoryContext())
assert seen["prepared"] == [plan] and seen["intent_judge"] is True
# Used once: the next reply judges for itself.
b.reply("100", "자비스, 오늘 서울 날씨 어때?", HistoryContext())
assert seen["prepared"][-1] is None
@pytest.mark.unit
def test_a_prepared_pick_is_not_used_for_other_words_another_person_or_a_changed_dialogue(tmp_path, monkeypatch):
from jarvis.memory.history import HistoryContext
plan = ["webSearch"]
b, seen = _jarvis_brain(tmp_path, monkeypatch, {"자비스 오늘 서울 날씨 어때": plan})
b.prepare("100", "자비스 오늘 서울 날씨 어때", False)
b.reply("100", "자비스, 내일 회의 몇 시지?", HistoryContext())
b.prepare("100", "자비스 오늘 서울 날씨 어때", False)
b.reply("200", "자비스 오늘 서울 날씨 어때", HistoryContext())
assert seen["prepared"] == [None, None]
b.prepare("100", "자비스 오늘 서울 날씨 어때", False)
b._memory("100").add_message("user", "something said meanwhile")
b.reply("100", "자비스 오늘 서울 날씨 어때", HistoryContext())
assert seen["prepared"][-1] is None
@pytest.mark.unit
def test_a_reply_waits_for_a_pick_still_being_prepared_for_its_text(tmp_path, monkeypatch):
import threading
import time as _time
from jarvis.memory.history import HistoryContext
plan = [] # "no tools" is a pick too
b, seen = _jarvis_brain(tmp_path, monkeypatch, {})
import jarvis.reply.engine as engine
def slow_prepare(cfg, text, mem, chat_available):
_time.sleep(0.4)
return plan
monkeypatch.setattr(engine, "prepare_intent", slow_prepare)
t = threading.Thread(target=b.prepare, args=("100", "자비스 날씨 어때", False))
t.start()
_time.sleep(0.1)
b.reply("100", "자비스 날씨 어때?", HistoryContext())
t.join()
assert seen["prepared"] == [plan]