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

@@ -79,6 +79,7 @@ class VoiceWorker:
self.llm_noticed = False
self.llm_active = 0
self.llm_idle_to = None # the session to tell once nothing runs
self.llm_switch_id = None # the switch to name in llm_idle
self.chat_timeout_s = chat_timeout_s
self.delivery_timeout_s = delivery_timeout_s # a chat answer the app never reports: not posted
# chat_action ids are unique across connections and worker runs, so a late answer from an
@@ -540,9 +541,10 @@ class Session:
asyncio.get_running_loop().create_task(self._reply(rid, user, text, via, speak, early))
def _on_llm(self, msg: dict) -> None:
state, notice = msg.get("state"), msg.get("notice")
if state not in ("switching", "ready", "paused") or not (notice is None or isinstance(notice, str)):
self.error("bad_message", "llm needs state switching|ready|paused and an optional string notice")
state, notice, sid = msg.get("state"), msg.get("notice"), msg.get("id")
if state not in ("switching", "ready", "paused") or not (notice is None or isinstance(notice, str)) \
or not (sid is None or (isinstance(sid, int) and not isinstance(sid, bool))):
self.error("bad_message", "llm needs state switching|ready|paused, an optional string notice and an optional integer id")
return
w = self.worker
w.llm_state = state
@@ -554,7 +556,7 @@ class Session:
w.llm_ready.clear()
if state == "switching":
w.llm_notice, w.llm_noticed = notice or None, False
w.llm_idle_to = self
w.llm_idle_to, w.llm_switch_id = self, sid
self._llm_check_idle()
else:
w.llm_ready.set() # paused: waiting requests stop waiting and answer nothing
@@ -563,13 +565,13 @@ class Session:
w = self.worker
if w.llm_state == "switching" and w.llm_active == 0 and w.llm_idle_to is not None:
to, w.llm_idle_to = w.llm_idle_to, None
to.send({"type": "llm_idle"})
to.send({"type": "llm_idle"} if w.llm_switch_id is None else {"type": "llm_idle", "id": w.llm_switch_id})
@contextlib.asynccontextmanager
async def _llm(self, notice_id: str | None = None):
"""Brain work that uses the LLM: it waits while the LLM switches (the first spoken request
that waits hears the notice once, under its own id), fails while paused, and counts as
running so a switch waits for it."""
that waits hears the notice once, under its own id), fails while paused or after waiting
`LLM_WAIT_S`, and counts as running so a switch waits for it."""
w = self.worker
while w.llm_state != "ready":
if w.llm_state == "paused":
@@ -581,7 +583,8 @@ class Session:
try:
await asyncio.wait_for(w.llm_ready.wait(), LLM_WAIT_S)
except asyncio.TimeoutError:
break # go on: the call fails on its own if the LLM is still away
# The LLM is still away (or a measuring server stands in): fail, never call it.
raise _LlmPaused() from None
w.llm_active += 1
try:
yield

View File

@@ -354,8 +354,8 @@ The app restarts the LLM with another placement when a game starts or ends (app/
"Performance stages"). It tells the worker first and waits for the brain to be idle:
```
-> {"type": "llm", "state": "switching", "notice": "잠깐만요, 게임 모드로 바꾸는 중이에요"} // notice optional
<- {"type": "llm_idle"} // nothing that uses the LLM runs any more
-> {"type": "llm", "state": "switching", "id": 7, "notice": "잠깐만요, 게임 모드로 바꾸는 중이에요"} // id, notice optional
<- {"type": "llm_idle", "id": 7} // nothing that uses the LLM runs any more (id: the switch's, when it had one)
-> {"type": "llm", "state": "ready"} // the LLM answers again
-> {"type": "llm", "state": "paused"} // the LLM is stopped (a game runs)
```
@@ -366,10 +366,12 @@ The app restarts the LLM with another placement when a game starts or ends (app/
- The first spoken `reply` that waits during one switch hears the notice under its own id (its
answer follows in the same speech when ready); later ones wait without it. A notice ends with a
newline, so it is spoken at once and never joined to the answer. A request waits at most 180 s,
then goes on (its LLM call fails on its own if the LLM is still away).
then fails without calling the brain (the LLM may still be away, or a measuring server stand in).
- On `paused` brain work answers nothing at once (`reply_text` `failed`, judgements failed) until
`ready`; what waited for a switch stops waiting the same way.
- Any other `state`, or a notice that is not a string, is `bad_message`.
- Any other `state`, a notice that is not a string or an id that is not an integer is `bad_message`.
- The app sends its latest `llm` message again first on every connection, so a worker that missed
one (a dropped connection, a restarted worker) catches up.
### Errors