diff --git a/app/src/conversation.rs b/app/src/conversation.rs index 2c4015b..7f169bc 100644 --- a/app/src/conversation.rs +++ b/app/src/conversation.rs @@ -130,7 +130,7 @@ pub struct Conversation { chat: ChatCalls, /// Chat answer id -> the message it answers (channel, message) and when it was asked. chat_answers: HashMap, - /// Each speaker's latest utterance that started. + /// Each speaker's latest utterance that started and whose final has not come yet. started: HashMap, /// 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 Conversation { 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 Conversation { 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 Conversation { } // 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); diff --git a/app/src/conversation.spec.md b/app/src/conversation.spec.md index 90e3492..6278e33 100644 --- a/app/src/conversation.spec.md +++ b/app/src/conversation.spec.md @@ -63,7 +63,9 @@ a space between, and judges the joined text as one utterance (so "자비스" the the speaker's next utterance already started when the wake word's final came, or starts within `call.followUpMs` of it, the wake word waits for that utterance's final however long it takes; otherwise, after `call.followUpMs`, the wake word alone is the request. Other speakers' utterances -never join it, and a stop (voice, slash command or leaving) drops it. Chat messages are not joined: +never join it, and a stop (voice, slash command or leaving) drops it. So does a lost worker +connection (`WorkerLost`): the final it waited for will not come, and the restarted worker's +utterances are not continuations of it. Chat messages are not joined: a message is complete as sent. ## Response policy diff --git a/app/src/discord.rs b/app/src/discord.rs index 7f4e622..9807cc9 100644 --- a/app/src/discord.rs +++ b/app/src/discord.rs @@ -73,6 +73,9 @@ pub enum CoreEvent { Addressed { id: String, addressed: bool }, /// The brain's answer to `rank()`: candidate ids, most important first (empty: failed). Ranked { id: String, order: Vec }, + /// The voice worker connection ended: finals of utterances in progress will not come, and the + /// restarted worker numbers utterances afresh. + WorkerLost, } #[derive(Debug, Clone, PartialEq, Eq)] @@ -715,7 +718,10 @@ async fn worker_task(core: std::sync::Weak, mut rx: mpsc::UnboundedReceiv core.inner.worker.chat_action_result(&a.id, result); }); } - WorkerEvent::Disconnected | WorkerEvent::Error(_) => {} + WorkerEvent::Disconnected => { + let _ = inner.events.send(CoreEvent::WorkerLost); + } + WorkerEvent::Error(_) => {} } } } diff --git a/app/src/discord_core.spec.md b/app/src/discord_core.spec.md index fd685ba..8cde7b3 100644 --- a/app/src/discord_core.spec.md +++ b/app/src/discord_core.spec.md @@ -141,7 +141,8 @@ and the late "left" update cancelled the new join (seen on real Discord). server display name; `SpeakingStopped` with the speaker's ID when their `speaking_end` is sent; `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`. + `moved`, `disconnected`, `shutdown`); `Addressed` and `Ranked` with the brain's answers to `ask_addressed` and `ask_rank`. `WorkerLost` when the voice worker connection ends: the finals of utterances in progress will not + come, and the restarted worker numbers utterances afresh. - **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