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

@@ -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);