feat(voice): in a game, partial transcription waits while an answer is written

- the app's ready message says whether the game stage runs (llm ready game=true); the worker then
  holds partial passes while a reply is being written and runs them again, the newest audio per
  speaker, when no answer is; finals never wait; without a game nothing changes
- M0 gamemode_tune decision: the game placement answered 1.7 times faster with partials held
  (64.9 against 37.5 tok/s with a partial pass every 2 s)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-14 10:58:11 +09:00
parent 5316f4e9b0
commit 3d58cf8ab1
8 changed files with 138 additions and 20 deletions

View File

@@ -1254,3 +1254,39 @@ def test_a_request_that_waits_too_long_for_a_switch_fails_without_the_brain(stor
seen = asyncio.run(with_worker(body, store=store, brain=brain))
reply = [m for m in seen if isinstance(m, dict) and m.get("type") == "reply_text"][0]
assert reply["failed"] and brain.calls == [], "the LLM may be away: the brain is not called"
@pytest.mark.unit
def test_in_a_game_no_partials_are_transcribed_while_an_answer_is_written(store):
"""voice_worker.spec.md, "LLM switches": `game` on ready holds partial passes while the brain
writes an answer (M0 gamemode_tune); without a game they go on."""
import threading
from tests.test_voice_worker_server import stream
for game in (True, False):
release = threading.Event()
def slow(user, text):
release.wait(10)
return f"answer for {text}"
async def body(url):
async with connect(url) as ws:
await hello(ws, stt={"partial_interval_ms": 100})
await send(ws, type="llm", state="ready", game=game)
await send(ws, type="reply", id="r1", user="100", text="q", via="chat", speak=False)
await asyncio.sleep(0.1)
await stream(ws, "200", 0.9, 800, realtime=True)
seen = await drain(ws, 0.8)
release.set()
seen += await drain(ws, 0.5)
return seen
seen = asyncio.run(with_worker(body, store=store, brain=FakeBrain(slow)))
partials = [m for m in seen if isinstance(m, dict) and m.get("type") == "partial"]
replies = [i for i, m in enumerate(seen) if isinstance(m, dict) and m.get("type") == "reply_text"]
first_partial = next((i for i, m in enumerate(seen) if isinstance(m, dict) and m.get("type") == "partial"), None)
if game:
assert first_partial is None or (replies and first_partial > replies[0]), "no partial while answering"
else:
assert partials and replies and first_partial < replies[0], "partials go on without a game"

View File

@@ -251,3 +251,32 @@ def test_partial_batches_are_kept_small_so_finals_can_cut_in():
run(body)
assert t.calls == [[10, 11], [99], [12, 13], [14]]
@pytest.mark.unit
def test_held_partials_wait_while_finals_still_go_and_run_once_released():
"""voice_worker.spec.md, "Scheduling": in a game, partial passes wait while an answer is being
written; finals never wait."""
t = FakeTranscriber()
held = {"on": True}
async def body():
s = SttScheduler(t, partials_held=lambda: held["on"])
s.start()
got = []
s.submit_partial("a", clip(10), PARTIAL, lambda r: got.append(("partial", r.text)))
s.submit_final("b", clip(20), FINAL, lambda r: got.append(("final", r.text)))
await asyncio.sleep(0.2)
before = list(got)
held["on"] = False
s.wake()
for _ in range(100):
if len(got) == 2:
break
await asyncio.sleep(0.01)
await s.stop()
return before, got
before, got = run(body)
assert before == [("final", "n20")], "the final went; the partial waited"
assert got == [("final", "n20"), ("partial", "n10")]