fix(app): a lost worker drops a waiting wake word
A wake word said alone waited without a time limit once the speaker's next utterance started. If the worker connection ended meanwhile, that final never came and the restarted worker's utterances (numbered from 1 again) were joined to it, so a later sentence without a wake word was answered. The core now reports WorkerLost; the conversation drops waiting wake words and forgets utterances in progress then and on leave. An utterance in progress is tracked until its own final arrives instead of by comparing utterance numbers. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -130,7 +130,7 @@ pub struct Conversation<V: Voice> {
|
||||
chat: ChatCalls,
|
||||
/// Chat answer id -> the message it answers (channel, message) and when it was asked.
|
||||
chat_answers: HashMap<String, (u64, u64, Instant)>,
|
||||
/// Each speaker's latest utterance that started.
|
||||
/// Each speaker's latest utterance that started and whose final has not come yet.
|
||||
started: HashMap<u64, u32>,
|
||||
/// A wake word said alone, per speaker, waiting for what they say next; the deadline for the next
|
||||
/// utterance to start (None while it runs).
|
||||
@@ -206,6 +206,9 @@ impl<V: Voice> Conversation<V> {
|
||||
CoreEvent::SpeakingStopped(user) => self.policy.speaking_stopped(*user, now),
|
||||
CoreEvent::Final(f) => {
|
||||
let speaker = f.heard.speaker_id;
|
||||
if self.started.get(&speaker) == Some(&f.heard.utt) {
|
||||
self.started.remove(&speaker);
|
||||
}
|
||||
// What follows a wake word said alone is joined to it.
|
||||
let text = match self.wakes.remove(&speaker) {
|
||||
Some((wake, _)) => format!("{} {}", wake.text, f.text),
|
||||
@@ -217,7 +220,7 @@ impl<V: Voice> Conversation<V> {
|
||||
let call = self.calls.judge(speaker, &text);
|
||||
debug_log!("conversation", "final speaker={speaker} req={} call={call:?}", req.id);
|
||||
if matches!(call, Call::Request { direct: true }) && self.calls.wake_word_only(&text) {
|
||||
let going_on = self.started.get(&speaker).is_some_and(|&u| u > f.heard.utt);
|
||||
let going_on = self.started.contains_key(&speaker);
|
||||
debug_log!("conversation", "wake word alone req={} speaker={speaker} going_on={going_on}", req.id);
|
||||
if !going_on {
|
||||
let a = self.policy.utterance_closed(speaker, now);
|
||||
@@ -283,7 +286,17 @@ impl<V: Voice> Conversation<V> {
|
||||
}
|
||||
// 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),
|
||||
CoreEvent::Stopped => self.stop_everything(now),
|
||||
// Utterances in progress end with the session or the worker: their finals will not come
|
||||
// (a restarted worker numbers utterances afresh), so nothing waits for them.
|
||||
CoreEvent::Left { .. } => {
|
||||
self.started.clear();
|
||||
self.stop_everything(now);
|
||||
}
|
||||
CoreEvent::WorkerLost => {
|
||||
self.started.clear();
|
||||
self.wakes.clear();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
@@ -531,6 +544,26 @@ mod tests {
|
||||
assert_eq!(f.take(), vec![Did::Answer(A, "자비스".into())]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_lost_worker_drops_a_waiting_wake_word_and_what_was_running() {
|
||||
let (mut c, f, t) = conv(PolicyKind::First, CallMode::WakeWord);
|
||||
c.on_event(&start(A, 1), ms(t, 0));
|
||||
c.on_event(&fin_utt(A, 1, "자비스"), ms(t, 900));
|
||||
c.on_event(&start(A, 2), ms(t, 1200)); // the final of utterance 2 is lost with the worker
|
||||
c.on_event(&CoreEvent::WorkerLost, ms(t, 2000));
|
||||
assert_eq!(c.next_deadline(), None);
|
||||
// The restarted worker numbers utterances from 1 again; nothing old joins or holds them.
|
||||
c.on_event(&start(A, 1), ms(t, 60_000));
|
||||
c.on_event(&fin_utt(A, 1, "오늘 뭐 먹지"), ms(t, 61_000));
|
||||
assert!(f.take().is_empty(), "no wake word: not a call");
|
||||
c.on_event(&start(A, 2), ms(t, 62_000));
|
||||
c.on_event(&fin_utt(A, 2, "자비스"), ms(t, 63_000));
|
||||
let wait = CallSettings::default().follow_up;
|
||||
assert_eq!(c.next_deadline(), Some(ms(t, 63_000) + wait));
|
||||
c.tick(ms(t, 63_000) + wait);
|
||||
assert_eq!(f.take(), vec![Did::Answer(A, "자비스".into())]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_stop_drops_a_waiting_wake_word() {
|
||||
let (mut c, f, t) = conv(PolicyKind::First, CallMode::WakeWord);
|
||||
|
||||
Reference in New Issue
Block a user