fix(app): the dashboard keeps each run's events, each answer's own times, and only its session's captions
- Each start gets a new dashboard; stop ends the event feeding and waits for it before forgetting, so queued or earlier-run events never reach the screen. - AnswerAsked carries the request's speech end and final times, so a second waiting question does not change the first answer's timing. - Captions are shown only while Jarvis is in a voice channel. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -43,19 +43,22 @@ struct Inner {
|
||||
state: State,
|
||||
error: Option<String>,
|
||||
running: Option<Arc<dyn Running>>,
|
||||
/// The running services' dashboard and the task feeding it their events. Each start gets a new
|
||||
/// one, so an event of an earlier run can never reach a later dashboard.
|
||||
dashboard: Arc<Mutex<Dashboard>>,
|
||||
feeder: Option<tokio::task::JoinHandle<()>>,
|
||||
}
|
||||
|
||||
pub struct Controller {
|
||||
host: Box<dyn Host>,
|
||||
inner: Mutex<Inner>,
|
||||
dashboard: Arc<Mutex<Dashboard>>,
|
||||
usage: tokio::sync::Mutex<Option<(Instant, Usage)>>,
|
||||
}
|
||||
|
||||
impl Controller {
|
||||
pub fn new(host: impl Host) -> Arc<Self> {
|
||||
Arc::new(Self { host: Box::new(host), inner: Mutex::new(Inner { state: State::Stopped, error: None, running: None }),
|
||||
dashboard: Arc::default(), usage: tokio::sync::Mutex::new(None) })
|
||||
Arc::new(Self { host: Box::new(host), inner: Mutex::new(Inner { state: State::Stopped, error: None, running: None,
|
||||
dashboard: Arc::default(), feeder: None }), usage: tokio::sync::Mutex::new(None) })
|
||||
}
|
||||
|
||||
pub fn state(&self) -> State {
|
||||
@@ -75,15 +78,18 @@ impl Controller {
|
||||
crate::info_log!("app", "starting the services");
|
||||
match self.host.start().await {
|
||||
Ok((running, mut events)) => {
|
||||
let dash = self.dashboard.clone();
|
||||
tokio::spawn(async move {
|
||||
let dash: Arc<Mutex<Dashboard>> = Arc::default();
|
||||
let d2 = dash.clone();
|
||||
let feeder = tokio::spawn(async move {
|
||||
while let Some(ev) = events.recv().await {
|
||||
dash.lock().unwrap().on_event(&ev, Instant::now());
|
||||
d2.lock().unwrap().on_event(&ev, Instant::now());
|
||||
}
|
||||
});
|
||||
let mut i = self.inner.lock().unwrap();
|
||||
i.state = State::Running;
|
||||
i.running = Some(running);
|
||||
i.dashboard = dash;
|
||||
i.feeder = Some(feeder);
|
||||
Ok(())
|
||||
}
|
||||
Err(e) => {
|
||||
@@ -110,8 +116,16 @@ impl Controller {
|
||||
if let Some(r) = running {
|
||||
r.stop().await;
|
||||
}
|
||||
self.dashboard.lock().unwrap().clear();
|
||||
self.inner.lock().unwrap().state = State::Stopped;
|
||||
// Stop feeding before forgetting: nothing still queued may reach the screen afterwards.
|
||||
let feeder = self.inner.lock().unwrap().feeder.take();
|
||||
if let Some(f) = feeder {
|
||||
f.abort();
|
||||
let _ = f.await;
|
||||
}
|
||||
let mut i = self.inner.lock().unwrap();
|
||||
i.dashboard = Arc::default();
|
||||
i.state = State::Stopped;
|
||||
drop(i);
|
||||
crate::info_log!("app", "services stopped");
|
||||
Ok(())
|
||||
}
|
||||
@@ -126,13 +140,13 @@ impl Controller {
|
||||
/// Everything the dashboard shows.
|
||||
pub async fn snapshot(&self) -> Value {
|
||||
let now = Instant::now();
|
||||
let (state, error, running) = {
|
||||
let (state, error, running, dashboard) = {
|
||||
let i = self.inner.lock().unwrap();
|
||||
(i.state, i.error.clone(), i.running.clone())
|
||||
(i.state, i.error.clone(), i.running.clone(), i.dashboard.clone())
|
||||
};
|
||||
let status = running.map(|r| r.status(now)).unwrap_or_else(|| json!({"worker": null, "voiceMembers": [],
|
||||
"missed": []}));
|
||||
let mut view = self.dashboard.lock().unwrap().view(&[], now);
|
||||
let mut view = dashboard.lock().unwrap().view(&[], now);
|
||||
view["missed"] = status["missed"].clone();
|
||||
view["worker"] = status["worker"].clone();
|
||||
view["voiceMembers"] = status["voiceMembers"].clone();
|
||||
@@ -294,6 +308,27 @@ mod tests {
|
||||
assert_eq!(counts.stops.load(Ordering::SeqCst), 0, "a failed start leaves nothing to stop");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn events_queued_before_a_stop_or_sent_by_an_earlier_run_never_reach_the_screen() {
|
||||
let (c, _, host) = controller(0);
|
||||
c.start().await.unwrap();
|
||||
let old = host.events.lock().unwrap().clone().unwrap();
|
||||
// Queued right before the stop, never handled before it.
|
||||
old.send(CoreEvent::Joined { guild_id: 1, channel_id: 77 }).unwrap();
|
||||
old.send(CoreEvent::Partial(crate::discord::Heard { speaker_id: 11, speaker_name: "민수".into(), utt: 1 },
|
||||
"자비스".into())).unwrap();
|
||||
c.stop().await.unwrap();
|
||||
tokio::time::sleep(Duration::from_millis(30)).await;
|
||||
let s = c.snapshot().await;
|
||||
assert_eq!((s["state"].clone(), s["voiceChannel"].clone(), s["captions"].clone()),
|
||||
(json!("stopped"), Value::Null, json!([])));
|
||||
// After a new start, the earlier run's sender still reaches nothing.
|
||||
c.start().await.unwrap();
|
||||
let _ = old.send(CoreEvent::Joined { guild_id: 1, channel_id: 78 });
|
||||
tokio::time::sleep(Duration::from_millis(30)).await;
|
||||
assert_eq!(c.snapshot().await["voiceChannel"], Value::Null);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn the_snapshot_carries_the_events_and_stopping_forgets_what_was_said() {
|
||||
let (c, _, host) = controller(0);
|
||||
|
||||
Reference in New Issue
Block a user