fix(app): performance stage switches survive failures, reconnects and late signals

- the normal stage reads other programs' memory afresh (the card after the LLM stopped, less the
  STT worker's model) and keeps it; the game stage counts from the latest normal reading, so the
  game is not counted against its own reserve
- a restart that fails brings back the stage that ran before and tells the brain ready; the
  failed stage is tried again after 60 s, three times at most; when nothing comes back the brain
  is told paused so requests fail at once
- the worker client keeps the latest llm state and sends it first on every connection
- switches are numbered: llm switching carries an id, llm_idle names it, and a late idle of an
  earlier switch no longer lets a new one through
- a request that waited 180 s for a switch fails without calling the brain
- the dashboard shows the stage that runs also on a fallback placement without experts

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-14 10:34:24 +09:00
parent 7e096b449e
commit aa6d950af6
10 changed files with 374 additions and 87 deletions

View File

@@ -1223,3 +1223,34 @@ def test_an_unknown_llm_state_is_refused(store):
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"
@pytest.mark.unit
def test_llm_idle_names_the_switch_it_answers(store):
async def body(url):
async with connect(url) as ws:
await hello(ws)
await send(ws, type="llm", state="switching", id=7)
return await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "llm_idle")
seen = asyncio.run(with_worker(body, store=store, brain=FakeBrain()))
assert [m for m in seen if isinstance(m, dict) and m.get("type") == "llm_idle"] == [{"type": "llm_idle", "id": 7}]
@pytest.mark.unit
def test_a_request_that_waits_too_long_for_a_switch_fails_without_the_brain(store, monkeypatch):
import voice_worker.server as server
monkeypatch.setattr(server, "LLM_WAIT_S", 0.3)
async def body(url):
async with connect(url) as ws:
await hello(ws)
await send(ws, type="llm", state="switching", id=1)
await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "llm_idle")
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 == [], "the LLM may be away: the brain is not called"