fix(app): the game placement reads the card again and stage retries start over with each new wish

- the game stage reads the card again: other programs as at the last normal placement made with
  no game running, and a reserve of at least what grew since (the game and programs opened after
  that reading); with a game already running at the start (game detection now looks once before
  the LLM starts) it counts what the card holds now and leaves only the room for other programs,
  so it no longer pauses for counting the game twice
- the retry count starts over whenever another stage is wanted, also when the game ends with the
  normal stage already running, so the next game tries again
- the wait before the next try counts from when the failed switch and its way back are over
- the worker client keeps, sends and readies a connection under one lock (llm, then state), so an
  LLM state set while a worker connects is not lost
- a game watch dropped by a start that fails part way stops its thread

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-14 10:45:00 +09:00
parent aa6d950af6
commit 4a81b88d41
6 changed files with 243 additions and 33 deletions

View File

@@ -455,3 +455,40 @@ async fn a_worker_that_missed_the_llm_state_gets_it_when_it_connects_again() {
assert_eq!(got[1], Got::Json(json!({"type": "llm", "state": "paused"})), "first after the hello");
c.close();
}
#[tokio::test]
async fn an_llm_state_set_while_the_worker_connects_again_is_the_last_it_gets() {
// The app sets "ready" over and over while a new worker connects: whatever the order, the
// last LLM message the worker gets is "ready", never the earlier "switching" alone.
for round in 0..20 {
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);
let port = w.kill();
next_matching(&mut rx, |e| *e == WorkerEvent::Disconnected).await;
let w2 = FakeWorker::start(port).await;
// "ready" is set from the moment the new worker has the hello until the client is ready.
let received = w2.received.clone();
let c2 = c.clone();
let setter = tokio::task::spawn_blocking(move || {
while received.lock().unwrap().is_empty() {
std::thread::yield_now();
}
while !c2.is_ready() {
c2.llm_state("ready");
}
c2.llm_state("ready");
});
next_matching(&mut rx, |e| matches!(e, WorkerEvent::Ready(_))).await;
setter.await.unwrap();
tokio::time::sleep(Duration::from_millis(50)).await;
let last = w2.got().into_iter().rev().find_map(|g| match g {
Got::Json(v) if v["type"] == "llm" => Some(v["state"].clone()),
_ => None,
});
assert_eq!(last, Some(json!("ready")), "round {round}");
c.close();
}
}