feat(app): performance stages move the LLM between the normal and the game placement

- while the services run, game mode decides the stage (perf.rs): a game means the game stage at
  once (or pause with game.pauseInGame); its end brings the normal stage back after
  game.revertSeconds (default 30)
- a change tells the voice worker the LLM switches (llm switching, with a notice), waits until
  the brain has written the answers in progress (llm_idle, at most 2 minutes), restarts the LLM in
  the stage and tells the worker it is ready or paused
- the game stage runs at 16K context, one request at a time, half the engine's CPU threads and
  the game placement (reserve game.reserveGb, default 8, else 2 GiB, else 0 GiB, else pause),
  measured once at that context; every placement of a run counts other programs' memory as it
  was when the services started
- the voice worker gates every LLM use: on switching new work waits (at most 180 s), the first
  spoken request that waits hears the notice once under its own id, on ready all go on, on
  paused nothing is answered
- dashboard: "성능 단계: 게임 중 · 게임에 X GB" / 일시정지; game mode screen: game VRAM, revert
  wait, pause in game

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-14 10:23:21 +09:00
parent b6bd1be6fb
commit 7e096b449e
18 changed files with 746 additions and 68 deletions

View File

@@ -1130,3 +1130,96 @@ def test_worker_logs_name_their_service_so_the_app_puts_them_in_its_tab(store, m
err = capfd.readouterr().err
for cat, what in (("stt", "utterance end"), ("llm", "brain reply r1 failed"), ("tts", "tts sentence id=t1")):
assert any(line.startswith(f"[{cat:^10}]") and what in line for line in err.splitlines()), (cat, err[-2000:])
async def drain(ws, seconds):
"""Every message that arrives within `seconds`."""
out, end = [], asyncio.get_running_loop().time() + seconds
while (left := end - asyncio.get_running_loop().time()) > 0:
try:
raw = await asyncio.wait_for(ws.recv(), left)
except asyncio.TimeoutError:
break
out.append(raw if isinstance(raw, bytes) else json.loads(raw))
return out
@pytest.mark.unit
def test_an_llm_switch_waits_for_the_answer_in_progress_then_holds_new_ones_with_one_notice(store):
"""voice_worker.spec.md, "LLM switches": the app switches the LLM only once the brain is idle;
requests meanwhile wait, the first spoken one hears the notice once, and all are answered once
the LLM is ready."""
import threading
release = threading.Event()
def slow_first(user, text):
if text == "first":
release.wait(10)
return f"answer for {text}"
tts = FakeTts()
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="llm", state="switching", notice="잠깐만요, 바꾸는 중이에요")
# Not idle while the first answer is being written.
seen = []
try:
seen += await asyncio.wait_for(recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "llm_idle"), 0.3)
idle_early = True
except asyncio.TimeoutError:
idle_early = False
release.set()
seen += await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "llm_idle")
# Two requests while switching: they wait; only the first spoken one hears the notice.
await send(ws, type="reply", id="r2", user="200", text="second", via="voice", speak=True)
await send(ws, type="reply", id="r3", user="300", text="third", via="voice", speak=True)
seen += await drain(ws, 0.4)
answered_while_switching = [m["id"] for m in seen if isinstance(m, dict) and m.get("type") == "reply_text"]
await send(ws, type="llm", state="ready")
seen += await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "tts_done" and m["id"] == "r3")
return idle_early, answered_while_switching, seen
brain = FakeBrain(slow_first)
idle_early, early, seen = asyncio.run(with_worker(body, store=store, brain=brain, tts=tts))
assert not idle_early, "idle only after the answer in progress"
assert "r2" not in early and "r3" not in early, "nothing new is answered while switching"
assert [c[1] for c in brain.calls] == ["first", "second", "third"]
assert tts.texts.count("잠깐만요, 바꾸는 중이에요") == 1, tts.texts
done = {m["id"]: m for m in seen if isinstance(m, dict) and m.get("type") == "tts_done"}
assert not done["r2"]["failed"] and not done["r3"]["failed"]
# r2 heard the notice, then its answer, under its own id.
assert tts.texts.index("잠깐만요, 바꾸는 중이에요") < tts.texts.index("answer for second")
@pytest.mark.unit
def test_while_paused_the_brain_answers_nothing(store):
async def body(url):
async with connect(url) as ws:
await hello(ws)
await send(ws, type="llm", state="switching")
await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "llm_idle")
await send(ws, type="llm", state="paused")
await send(ws, type="reply", id="r1", user="100", text="q", via="chat", speak=False)
return await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "reply_text")
brain = FakeBrain()
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 == []
@pytest.mark.unit
def test_an_unknown_llm_state_is_refused(store):
async def body(url):
async with connect(url) as ws:
await hello(ws)
await send(ws, type="llm", state="maybe")
return await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "error")
seen = asyncio.run(with_worker(body, store=store, brain=FakeBrain()))
assert [m for m in seen if isinstance(m, dict) and m.get("type") == "error"][0]["code"] == "bad_message"