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:
@@ -100,8 +100,7 @@ impl<V: Voice> Conversation<V> {
|
||||
}
|
||||
Call::Stop => {
|
||||
self.voice.stop_all();
|
||||
let a = self.policy.stop();
|
||||
self.apply(a, now);
|
||||
self.stop_everything(now);
|
||||
let a = self.policy.utterance_closed(speaker, now);
|
||||
self.apply(a, now);
|
||||
}
|
||||
@@ -121,9 +120,11 @@ impl<V: Voice> Conversation<V> {
|
||||
}
|
||||
}
|
||||
CoreEvent::Addressed { id, addressed } => {
|
||||
if let Some((req, _)) = self.asks.remove(id) {
|
||||
debug_log!("conversation", "addressed req={} yes={addressed}", req.id);
|
||||
let a = if *addressed {
|
||||
if let Some((req, asked)) = self.asks.remove(id) {
|
||||
// An answer after the timeout counts as no, even before the timer ran.
|
||||
let yes = *addressed && now < asked + ADDRESSED_TIMEOUT;
|
||||
debug_log!("conversation", "addressed req={} yes={yes}", req.id);
|
||||
let a = if yes {
|
||||
self.policy.request(req, now)
|
||||
} else {
|
||||
self.policy.utterance_closed(req.speaker, now)
|
||||
@@ -146,19 +147,23 @@ impl<V: Voice> Conversation<V> {
|
||||
self.apply(a, now);
|
||||
}
|
||||
}
|
||||
CoreEvent::Left { .. } => {
|
||||
// Nothing can be said outside a voice channel: drop what was going on.
|
||||
let a = self.policy.stop();
|
||||
self.apply(a, now);
|
||||
for (_, (req, _)) in self.asks.drain().collect::<Vec<_>>() {
|
||||
let a = self.policy.utterance_closed(req.speaker, now);
|
||||
self.apply(a, now);
|
||||
}
|
||||
}
|
||||
// All speech was stopped (slash command, leaving): drop what was waiting before the stopped
|
||||
// speech's end could start the next answer. After a voice stop this finds nothing left.
|
||||
CoreEvent::Stopped | CoreEvent::Left { .. } => self.stop_everything(now),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
/// The policy's stop, and every pending "was it said to Jarvis?" question is void.
|
||||
fn stop_everything(&mut self, now: Instant) {
|
||||
let a = self.policy.stop();
|
||||
self.apply(a, now);
|
||||
for (_, (req, _)) in self.asks.drain().collect::<Vec<_>>() {
|
||||
let a = self.policy.utterance_closed(req.speaker, now);
|
||||
self.apply(a, now);
|
||||
}
|
||||
}
|
||||
|
||||
/// Timers: the policy's and unanswered "was it said to Jarvis?" questions.
|
||||
pub fn tick(&mut self, now: Instant) {
|
||||
let late: Vec<String> = self.asks.iter()
|
||||
@@ -403,6 +408,43 @@ mod tests {
|
||||
assert_eq!(f.take(), vec![Did::Cancel("s1".into()), Did::Answer(B, "자비스 불 꺼".into())]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_voice_stop_also_drops_a_pending_addressed_question() {
|
||||
let (mut c, f, t) = conv(PolicyKind::InOrder, CallMode::Addressed);
|
||||
c.on_event(&fin(A, "불 좀 꺼 줄래", 100), ms(t, 0));
|
||||
let Did::Addressed(q, _) = f.take().remove(0) else { panic!() };
|
||||
c.on_event(&fin(B, "자비스 멈춰", 100), ms(t, 100));
|
||||
f.take();
|
||||
c.on_event(&CoreEvent::Addressed { id: q, addressed: true }, ms(t, 300));
|
||||
assert!(f.take().is_empty(), "the question asked before the stop is void");
|
||||
assert_eq!(c.next_deadline(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_stop_from_elsewhere_drops_the_queue_before_the_cancelled_speech_ends() {
|
||||
let (mut c, f, t) = conv(PolicyKind::InOrder, CallMode::Addressed);
|
||||
c.on_event(&fin(A, "자비스 날씨", 100), ms(t, 0));
|
||||
c.on_event(&fin(B, "자비스 뉴스", 100), ms(t, 100));
|
||||
c.on_event(&fin(B, "불 좀 꺼 줄래", 100), ms(t, 200)); // waiting for "addressed"
|
||||
let did = f.take();
|
||||
let q = did.iter().find_map(|d| if let Did::Addressed(q, _) = d { Some(q.clone()) } else { None }).unwrap();
|
||||
// The slash command stops all speech: Stopped comes before the speech's end.
|
||||
c.on_event(&CoreEvent::Stopped, ms(t, 300));
|
||||
c.on_event(&end("s1", true), ms(t, 310));
|
||||
c.on_event(&CoreEvent::Addressed { id: q, addressed: true }, ms(t, 400));
|
||||
assert!(!f.take().iter().any(|d| matches!(d, Did::Answer(..))), "nothing starts after a stop");
|
||||
assert_eq!(c.missed().filter(|m| m.reason == MissReason::Stopped).count(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_addressed_answer_after_its_timeout_counts_as_no() {
|
||||
let (mut c, f, t) = conv(PolicyKind::InOrder, CallMode::Addressed);
|
||||
c.on_event(&fin(A, "불 좀 꺼 줄래", 100), ms(t, 0));
|
||||
let Did::Addressed(q, _) = f.take().remove(0) else { panic!() };
|
||||
c.on_event(&CoreEvent::Addressed { id: q, addressed: true }, ms(t, 0) + ADDRESSED_TIMEOUT);
|
||||
assert!(f.take().is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn end_times_come_from_the_finals_wait() {
|
||||
let (mut c, f, t) = conv(PolicyKind::InOrder, CallMode::WakeWord);
|
||||
|
||||
@@ -122,17 +122,20 @@ one request, stop all, ask "addressed", ask "rank"), so the glue is tested with
|
||||
|
||||
- `SpeechStart` opens the speaker's utterance; `SpeakingStopped` marks it ending.
|
||||
- `Final`: a request gets the next id, `ended_at` = arrival minus the final's `wait_ms`, and goes
|
||||
through call detection. Ignore closes the utterance. Stop stops all speech, runs the policy's stop and
|
||||
closes the utterance. Request goes to the policy. Ask sends `addressed` to the brain; a yes becomes the
|
||||
request, a no (or a failed judgement) closes the utterance, and no answer within 5 s counts as no.
|
||||
through call detection. Ignore closes the utterance. Stop stops all speech, runs the policy's stop,
|
||||
voids every pending "addressed" question (closing its utterance) and closes the utterance. Request
|
||||
goes to the policy. Ask sends `addressed` to the brain; a yes becomes the request, a no (or a failed
|
||||
judgement) closes the utterance, and no answer within 5 s counts as no, also when the answer arrives
|
||||
after that but before the timer ran.
|
||||
- Policy actions: `Answer` → `answer(user, text)` (the brain answers and the answer is spoken; when it
|
||||
cannot be queued the answer is done at once); `StopAnswer` → `cancel` of that answer's speech;
|
||||
`Rank { id }` → `rank` with id `k<id>` and the candidates' request ids; `Missed` → a debug log and
|
||||
the missed list (the last 100: speaker, reason, time and length, never the text).
|
||||
- `SpeakEnd` of an answer's speech is that answer's done. The end of a speech the loop already
|
||||
cancelled starts nothing.
|
||||
- `Left` (the bot left the voice channel): the policy's stop, and pending "addressed" questions close
|
||||
their utterances.
|
||||
- `Stopped` (all speech stopped from elsewhere: the slash command, `stop()`, the session ending) and
|
||||
`Left`: the same stop. The core sends `Stopped` before the `SpeakEnd`s of the speech it stopped, so a
|
||||
cancelled answer's end never starts the next waiting request.
|
||||
- A timer wakes the loop at the policy's next deadline or the earliest "addressed" timeout.
|
||||
|
||||
The brain's two judgements are `src/voice_worker/judges.py` (`voice_worker.spec.md`): one short call
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -139,8 +139,9 @@ and the late "left" update cancelled the new join (seen on real Discord).
|
||||
sends `speaker_leave`, so their utterance finishes and the worker frees their place.
|
||||
- **Events** for the layers above: `SpeechStart`, `Partial` and `Final` with the speaker's ID and
|
||||
server display name; `SpeakingStopped` with the speaker's ID when their `speaking_end` is sent;
|
||||
`SpeakStart` and `SpeakEnd`; `Joined` and `Left` (with a reason: `command`, `moved`, `disconnected`,
|
||||
`shutdown`); `Addressed` and `Ranked` with the brain's answers to `ask_addressed` and `ask_rank`.
|
||||
`SpeakStart` and `SpeakEnd`; `Stopped` when all speech is stopped (`stop()`, the slash command or the
|
||||
session ending), sent before the `SpeakEnd`s it causes; `Joined` and `Left` (with a reason: `command`,
|
||||
`moved`, `disconnected`, `shutdown`); `Addressed` and `Ranked` with the brain's answers to `ask_addressed` and `ask_rank`.
|
||||
- **Speaking**: `say(text, more)` queues a TTS request and returns its id (`None` when not in a voice
|
||||
channel); `continue_text(id, text, more)` adds text to an open request. Each sentence the worker
|
||||
sends becomes one clip (a 48 kHz stereo WAV played through songbird without resampling); clips play
|
||||
|
||||
Reference in New Issue
Block a user