fix(app): every stop voids waiting requests and pending addressed questions

- A voice stop also voids pending "addressed" questions, so a yes that
  arrives afterwards starts nothing.
- The core sends Stopped before the SpeakEnds of what stop() or the end
  of a session cancelled; the conversation drops its queue on it, so the
  slash command's stop no longer starts the next waiting request.
- An addressed answer after its 5 s timeout counts as no even when it is
  handled before the timer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-11 22:32:32 +09:00
parent f892a1dce9
commit de35573e5f
4 changed files with 85 additions and 22 deletions

View File

@@ -63,6 +63,9 @@ pub enum CoreEvent {
Final(HeardFinal),
SpeakStart(String),
SpeakEnd(PlaybackEnd),
/// All speech was stopped (`stop()`, the slash command, or the session ending). Sent before the
/// `SpeakEnd`s of what it stopped.
Stopped,
Chat(ChatMessage),
/// The brain's answer to `addressed()`.
Addressed { id: String, addressed: bool },
@@ -428,6 +431,7 @@ impl DiscordCore {
/// Stop speaking at once and drop everything queued. How many requests were stopped.
pub fn stop(&self) -> usize {
let _ = self.inner.events.send(CoreEvent::Stopped);
with_playback(&self.inner, |p| p.stop())
}
@@ -541,6 +545,7 @@ fn with_playback<T>(inner: &Inner, f: impl FnOnce(&mut Playback<WorkerClient, Pl
/// the connection and points it at the new channel instead).
async fn end_session(inner: &Arc<Inner>, s: Session, reason: &str, disconnect: bool) {
s.alive.store(false, Ordering::Relaxed);
let _ = inner.events.send(CoreEvent::Stopped);
with_playback(inner, |p| p.stop());
let _ = inner.player.send(PlayerCmd::SetCall(None));
let (heard, lost, late, bad) = {
@@ -1011,9 +1016,21 @@ impl Handler {
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn stop_reports_itself_before_the_speech_it_ends() {
let (worker, worker_events) = WorkerClient::new(crate::worker_client::WorkerClientOptions::new("ws://127.0.0.1:1", "t"));
let (core, mut events) = DiscordCore::new(Settings::default(), "token".into(), worker, worker_events);
let id = with_playback(&core.inner, |p| p.adopt());
assert_eq!(core.stop(), 1);
assert_eq!(events.recv().await, Some(CoreEvent::Stopped));
assert!(matches!(events.recv().await, Some(CoreEvent::SpeakEnd(e)) if e.id == id && e.cancelled));
}
#[test]
fn wav_header_describes_48k_stereo_pcm16() {
let w = super::wav(&[1, 2, 3, 4]);
let w = wav(&[1, 2, 3, 4]);
assert_eq!(&w[0..4], b"RIFF");
assert_eq!(u32::from_le_bytes(w[4..8].try_into().unwrap()), 40);
assert_eq!(u16::from_le_bytes(w[22..24].try_into().unwrap()), 2);