feat(app): performance stages move the LLM between the normal and the game placement

- while the services run, game mode decides the stage (perf.rs): a game means the game stage at
  once (or pause with game.pauseInGame); its end brings the normal stage back after
  game.revertSeconds (default 30)
- a change tells the voice worker the LLM switches (llm switching, with a notice), waits until
  the brain has written the answers in progress (llm_idle, at most 2 minutes), restarts the LLM in
  the stage and tells the worker it is ready or paused
- the game stage runs at 16K context, one request at a time, half the engine's CPU threads and
  the game placement (reserve game.reserveGb, default 8, else 2 GiB, else 0 GiB, else pause),
  measured once at that context; every placement of a run counts other programs' memory as it
  was when the services started
- the voice worker gates every LLM use: on switching new work waits (at most 180 s), the first
  spoken request that waits hears the notice once under its own id, on ready all go on, on
  paused nothing is answered
- dashboard: "성능 단계: 게임 중 · 게임에 X GB" / 일시정지; game mode screen: game VRAM, revert
  wait, pause in game

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-14 10:23:21 +09:00
parent b6bd1be6fb
commit 7e096b449e
18 changed files with 746 additions and 68 deletions

79
app/src/perf.rs Normal file
View File

@@ -0,0 +1,79 @@
//! The performance stages (`docs/v2/pc-features.md`, "자동 성능 조절" and "전환 규칙"; admin.spec.md,
//! "Performance stages"): which placement the LLM should run in, from game mode and time. The
//! switching itself (waiting for the answer in progress, restarting the LLM) is `services.rs`.
use std::time::{Duration, Instant};
use serde::Serialize;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Stage {
Normal,
Gaming,
/// The LLM is stopped while a game runs (chosen, or no placement leaves the game room).
Paused,
}
/// Picks the stage: a game means the game stage (or pause when the person chose it) at once; after
/// the game ends the normal stage comes back only once `revert_after` has passed, so quitting and
/// starting a game again does not move the model back and forth.
#[derive(Debug)]
pub struct Perf {
revert_after: Duration,
pause_in_game: bool,
game_since_off: Option<Instant>,
current: Stage,
}
impl Perf {
pub fn new(revert_after: Duration, pause_in_game: bool) -> Self {
Self { revert_after, pause_in_game, game_since_off: None, current: Stage::Normal }
}
/// The stage wanted now, given whether a game runs.
pub fn target(&mut self, game: bool, now: Instant) -> Stage {
if game {
self.game_since_off = None;
self.current = if self.pause_in_game { Stage::Paused } else { Stage::Gaming };
} else if self.current != Stage::Normal {
let since = *self.game_since_off.get_or_insert(now);
if now.duration_since(since) >= self.revert_after {
self.current = Stage::Normal;
self.game_since_off = None;
}
}
self.current
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_game_switches_at_once_and_its_end_only_after_the_wait() {
let t = Instant::now();
let s = |n: u64| t + Duration::from_secs(n);
let mut p = Perf::new(Duration::from_secs(30), false);
assert_eq!(p.target(false, s(0)), Stage::Normal);
assert_eq!(p.target(true, s(1)), Stage::Gaming);
// The game ends: still the game stage for 30 s.
assert_eq!(p.target(false, s(2)), Stage::Gaming);
assert_eq!(p.target(false, s(31)), Stage::Gaming);
// Started again within the wait: nothing moves, and the wait starts over at the next end.
assert_eq!(p.target(true, s(32)), Stage::Gaming);
assert_eq!(p.target(false, s(40)), Stage::Gaming);
assert_eq!(p.target(false, s(69)), Stage::Gaming);
assert_eq!(p.target(false, s(70)), Stage::Normal);
assert_eq!(p.target(false, s(500)), Stage::Normal);
}
#[test]
fn with_pause_chosen_a_game_pauses_the_llm() {
let t = Instant::now();
let mut p = Perf::new(Duration::ZERO, true);
assert_eq!(p.target(true, t), Stage::Paused);
assert_eq!(p.target(false, t), Stage::Normal);
}
}