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

@@ -416,19 +416,42 @@ async fn a_chat_reply_is_not_spoken_and_its_text_comes_back() {
}
#[tokio::test]
async fn an_llm_switch_is_announced_and_the_client_waits_until_the_brain_is_idle() {
async fn an_llm_switch_is_announced_and_the_client_waits_until_the_brain_is_idle_for_that_switch() {
let w = FakeWorker::start(0).await;
let (c, _rx) = client(&w.url(), json!({}));
c.start();
c.wait_ready(Duration::from_secs(2)).await.unwrap();
assert!(c.llm_state("switching", Some("잠깐만요")));
until(|| w.got().iter().any(|g| *g == Got::Json(json!({"type": "llm", "state": "switching", "notice": "잠깐만요"})))).await;
let first = c.llm_switching(Some("잠깐만요"));
until(|| w.got().iter().any(|g| *g == Got::Json(json!({"type": "llm", "state": "switching", "id": first, "notice": "잠깐만요"})))).await;
// Nothing idle yet: the wait gives up after its time.
assert!(!c.wait_llm_idle(Duration::from_millis(100)).await);
// The worker reports idle (before or while the app waits): the wait ends.
w.send(Message::text(json!({"type": "llm_idle"}).to_string()));
assert!(c.wait_llm_idle(Duration::from_secs(2)).await);
assert!(c.llm_state("ready", None));
assert!(!c.wait_llm_idle(first, Duration::from_millis(100)).await);
// The idle for this switch comes late, after the app gave up on it, and a new switch starts:
// that late idle must not let the new switch through.
let second = c.llm_switching(None);
w.send(Message::text(json!({"type": "llm_idle", "id": first}).to_string()));
assert!(!c.wait_llm_idle(second, Duration::from_millis(200)).await, "a late idle of an earlier switch");
w.send(Message::text(json!({"type": "llm_idle", "id": second}).to_string()));
assert!(c.wait_llm_idle(second, Duration::from_secs(2)).await);
assert!(c.llm_state("ready"));
until(|| w.got().iter().any(|g| *g == Got::Json(json!({"type": "llm", "state": "ready"})))).await;
c.close();
}
#[tokio::test]
async fn a_worker_that_missed_the_llm_state_gets_it_when_it_connects_again() {
let w = FakeWorker::start(0).await;
let (c, mut rx) = client(&w.url(), json!({}));
c.start();
c.wait_ready(Duration::from_secs(2)).await.unwrap();
c.llm_switching(None);
// The worker goes away; the app sets the LLM paused meanwhile (that message is lost).
let port = w.kill();
next_matching(&mut rx, |e| *e == WorkerEvent::Disconnected).await;
assert!(!c.llm_state("paused"), "not sent: no connection");
let w2 = FakeWorker::start(port).await;
until(|| w2.got().iter().any(|g| *g == Got::Json(json!({"type": "llm", "state": "paused"})))).await;
let got = w2.got();
assert!(matches!(&got[0], Got::Json(v) if v["type"] == "hello"));
assert_eq!(got[1], Got::Json(json!({"type": "llm", "state": "paused"})), "first after the hello");
c.close();
}