From f952b72ed91cfb5c3caa6aa2cd3f033bc888c517 Mon Sep 17 00:00:00 2001 From: javis-bot Date: Sat, 14 Nov 2026 16:39:38 +0900 Subject: [PATCH] fix(app): a reminder's speech that times out is cancelled, so it never plays after the chat post Co-Authored-By: Claude Opus 5.5 --- app/src/admin.spec.md | 5 +++-- app/src/discord.rs | 45 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/app/src/admin.spec.md b/app/src/admin.spec.md index 24ef3b1..eb72159 100644 --- a/app/src/admin.spec.md +++ b/app/src/admin.spec.md @@ -132,8 +132,9 @@ check something at a time or regularly; the brain's `schedule` tool - **Delivery**: a `task` first runs in the brain (`reply` `via: "task"`: read-only tools, its own short-term memory, at most 300 s); no answer is a failure. The text sent is `님, `. Asked by voice: spoken when the person is in Jarvis's voice channel and counted only when - the speech ends played to the end (at most 120 s, queued behind what is being said); otherwise, - or when it failed or was stopped, posted in `schedule.chatChannel`; asked in chat: posted in that channel. Posts ping nobody and need the + the speech ends played to the end (at most 120 s, queued behind what is being said; when no end + comes by then the speech is cancelled, waiting or playing, so it never plays after the post); + otherwise, or when it failed or was stopped, posted in `schedule.chatChannel`; asked in chat: posted in that channel. Posts ping nobody and need the channel's send switch. A delivered one-off schedule is removed; one that failed, or was missed, stays (not firing again) with its outcome `last: {at, ok, note}` for the screen, and a repeating one keeps its latest outcome. diff --git a/app/src/discord.rs b/app/src/discord.rs index 1673bfe..945467d 100644 --- a/app/src/discord.rs +++ b/app/src/discord.rs @@ -477,9 +477,13 @@ impl DiscordCore { } /// Speak text and wait for its end: None when not in a voice channel or when no end came within - /// `SPEECH_TIMEOUT`. + /// `SPEECH_TIMEOUT` (the speech is then cancelled). pub async fn say_to_end(&self, text: &str) -> Option { self.voice_channel_id()?; + self.speak_to_end(text, SPEECH_TIMEOUT).await + } + + async fn speak_to_end(&self, text: &str, timeout: Duration) -> Option { let (tx, rx) = tokio::sync::oneshot::channel(); // Registered before the playback's events go out, so an end at once is not missed. let id = with_playback(&self.inner, |p| { @@ -487,9 +491,14 @@ impl DiscordCore { self.inner.speech_waits.lock().unwrap().insert(id.clone(), tx); id }); - let end = tokio::time::timeout(SPEECH_TIMEOUT, rx).await; + let end = tokio::time::timeout(timeout, rx).await.ok().and_then(Result::ok); self.inner.speech_waits.lock().unwrap().remove(&id); - end.ok().and_then(Result::ok) + if end.is_none() { + // No end came: take it out of the queue (waiting or playing), so it never plays after + // the caller went on without it (a reminder then goes to chat). + with_playback(&self.inner, |p| p.cancel(&id)); + } + end } /// Have the brain answer `user` and speak it as one request in the queue. The request id (its @@ -1235,6 +1244,36 @@ impl Handler { #[cfg(test)] mod tests { + /// A reminder's speech that waits too long behind other speech is taken out of the queue, so it + /// never plays after the reminder already went to chat. + #[tokio::test] + async fn speech_that_times_out_is_cancelled_and_never_plays_later() { + let (worker, worker_events) = WorkerClient::new(crate::worker_client::WorkerClientOptions::new("ws://127.0.0.1:9", "t")); + let (core, mut events) = DiscordCore::new(Settings::default(), String::new(), worker, worker_events); + // An answer at the head of the queue that gets no audio yet holds everything behind it. + let blocker = with_playback(&core.inner, |p| p.adopt()); + let end = core.speak_to_end("철수님, 회의", Duration::from_millis(200)).await; + assert!(end.as_ref().is_none_or(|e| e.cancelled || e.failed), "{end:?}"); + with_playback(&core.inner, |p| p.fail(&blocker)); + tokio::time::sleep(Duration::from_millis(100)).await; + let mut started = vec![]; + let mut ends = vec![]; + while let Ok(ev) = events.try_recv() { + match ev { + CoreEvent::SpeakStart(id) => started.push(id), + CoreEvent::SpeakEnd(e) => ends.push(e), + _ => {} + } + } + let reminder: Vec<_> = ends.iter().filter(|e| e.id != blocker).collect(); + assert_eq!(reminder.len(), 1, "{ends:?}"); + // Taken out at the timeout, not when the queue reached it. + assert_eq!(ends[0].id, reminder[0].id, "{ends:?}"); + assert!(reminder[0].cancelled && reminder[0].played == 0, "{reminder:?}"); + assert!(!started.contains(&reminder[0].id), "the reminder started after it timed out: {started:?}"); + assert!(!with_playback(&core.inner, |p| p.busy()), "nothing is left in the queue"); + } + #[test] fn an_utterance_keeps_the_session_it_started_in() { let mut u = super::UttSessions::default();