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:
@@ -156,8 +156,13 @@ struct Inner {
|
||||
events: mpsc::UnboundedSender<WorkerEvent>,
|
||||
ready: watch::Sender<Option<HelloOk>>,
|
||||
wake: Notify,
|
||||
/// The brain finished the answers it was giving when the app said the LLM switches.
|
||||
llm_idle: Notify,
|
||||
/// The last switch the brain reported idle for (`llm_idle` names it).
|
||||
llm_idle: watch::Sender<u64>,
|
||||
/// The LLM state the brain should have: sent again on every connection, so a worker that
|
||||
/// missed it (a dropped connection, a restarted worker) catches up.
|
||||
llm: Mutex<Option<Value>>,
|
||||
/// The number of the latest switch.
|
||||
llm_switches: std::sync::atomic::AtomicU64,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -176,7 +181,9 @@ impl WorkerClient {
|
||||
events,
|
||||
ready: watch::channel(None).0,
|
||||
wake: Notify::new(),
|
||||
llm_idle: Notify::new(),
|
||||
llm_idle: watch::channel(0).0,
|
||||
llm: Mutex::new(None),
|
||||
llm_switches: std::sync::atomic::AtomicU64::new(0),
|
||||
});
|
||||
(Self { inner }, rx)
|
||||
}
|
||||
@@ -354,20 +361,32 @@ impl WorkerClient {
|
||||
self.send_json(json!({"type": "addressed", "id": id, "user": user.to_string(), "text": text}))
|
||||
}
|
||||
|
||||
/// Tell the brain the LLM's state (voice_worker.spec.md, "LLM switches"): `switching` (start no
|
||||
/// new LLM work, say `notice` once to the first spoken request that waits, report `llm_idle`),
|
||||
/// `ready` (answer what waited) or `paused` (answer nothing until ready).
|
||||
pub fn llm_state(&self, state: &str, notice: Option<&str>) -> bool {
|
||||
let mut m = json!({"type": "llm", "state": state});
|
||||
/// Tell the brain the LLM switches (voice_worker.spec.md, "LLM switches"): it starts no new
|
||||
/// LLM work, says `notice` once to the first spoken request that waits, and reports idle for
|
||||
/// this switch, whose number is returned for `wait_llm_idle`.
|
||||
pub fn llm_switching(&self, notice: Option<&str>) -> u64 {
|
||||
let id = self.inner.llm_switches.fetch_add(1, std::sync::atomic::Ordering::SeqCst) + 1;
|
||||
let mut m = json!({"type": "llm", "state": "switching", "id": id});
|
||||
if let Some(n) = notice {
|
||||
m["notice"] = json!(n);
|
||||
}
|
||||
*self.inner.llm.lock().unwrap() = Some(m.clone());
|
||||
self.send_json(m);
|
||||
id
|
||||
}
|
||||
|
||||
/// Tell the brain the LLM is `ready` (answer what waited) or `paused` (answer nothing).
|
||||
pub fn llm_state(&self, state: &str) -> bool {
|
||||
let m = json!({"type": "llm", "state": state});
|
||||
*self.inner.llm.lock().unwrap() = Some(m.clone());
|
||||
self.send_json(m)
|
||||
}
|
||||
|
||||
/// Wait until the brain reports `llm_idle` (also one that came before the wait), at most `t`.
|
||||
pub async fn wait_llm_idle(&self, t: Duration) -> bool {
|
||||
tokio::time::timeout(t, self.inner.llm_idle.notified()).await.is_ok()
|
||||
/// Wait until the brain reports idle for switch `id` (also when it came before the wait); a
|
||||
/// report for an earlier switch does not count. At most `t`.
|
||||
pub async fn wait_llm_idle(&self, id: u64, t: Duration) -> bool {
|
||||
let mut rx = self.inner.llm_idle.subscribe();
|
||||
tokio::time::timeout(t, rx.wait_for(|got| *got >= id)).await.is_ok_and(|r| r.is_ok())
|
||||
}
|
||||
|
||||
/// Tell the brain a call is being said (a partial transcript), so it gets ready to answer it.
|
||||
@@ -501,6 +520,10 @@ async fn connect_once(inner: &Arc<Inner>) -> bool {
|
||||
if st.closed {
|
||||
return true;
|
||||
}
|
||||
// The LLM state the brain should have, before anything else it is asked.
|
||||
if let Some(m) = inner.llm.lock().unwrap().clone() {
|
||||
let _ = tx.send(Message::text(m.to_string()));
|
||||
}
|
||||
st.conn = Some(tx);
|
||||
}
|
||||
debug_log!("worker", "voice worker ready stt={} tts={}", hello_ok.stt_ready, hello_ok.tts_ready);
|
||||
@@ -544,7 +567,9 @@ fn on_text(inner: &Inner, t: &str) {
|
||||
"final" => serde_json::from_value(v).ok().map(WorkerEvent::Final),
|
||||
"chat_action" => serde_json::from_value(v).ok().map(WorkerEvent::ChatAction),
|
||||
"llm_idle" => {
|
||||
inner.llm_idle.notify_one();
|
||||
if let Some(id) = v["id"].as_u64() {
|
||||
inner.llm_idle.send_if_modified(|got| if id > *got { *got = id; true } else { false });
|
||||
}
|
||||
return;
|
||||
}
|
||||
"reply_text" => {
|
||||
|
||||
Reference in New Issue
Block a user