fix(app): a transcript belongs to the voice session its utterance started in

Each Joined carries a new session number; the core records, when an
utterance's speech_start arrives, the session Jarvis is in, and its Partial
and Final carry that number (Heard.session) even when they arrive after a
leave or a move. The dashboard shows only the current session's captions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-12 14:00:21 +09:00
parent 371e87f553
commit e8ddde5be6
6 changed files with 129 additions and 25 deletions

View File

@@ -53,6 +53,8 @@ struct Pending {
pub struct Dashboard {
bot_tag: Option<String>,
voice_channel: Option<u64>,
/// The voice session Jarvis is in (from `Joined`).
session: Option<u64>,
captions: VecDeque<Caption>,
pending: HashMap<String, Pending>,
answers: VecDeque<AnswerTiming>,
@@ -66,16 +68,21 @@ impl Dashboard {
pub fn on_event(&mut self, ev: &CoreEvent, now: Instant) {
match ev {
CoreEvent::Ready { tag, .. } => self.bot_tag = Some(tag.clone()),
CoreEvent::Joined { channel_id, .. } => self.voice_channel = Some(*channel_id),
CoreEvent::Joined { channel_id, session, .. } => {
self.voice_channel = Some(*channel_id);
self.session = Some(*session);
}
CoreEvent::Left { .. } => {
self.voice_channel = None;
self.session = None;
self.captions.clear();
self.pending.clear();
}
// Captions belong to the voice session: a transcript arriving after Jarvis left is dropped.
CoreEvent::Partial(h, text) if self.voice_channel.is_some() =>
// A transcript belongs to the session its utterance started in: one of an earlier session
// (arriving after a leave, or after a move to another channel) is not shown.
CoreEvent::Partial(h, text) if self.session.is_some() && h.session == self.session =>
self.caption(h.speaker_id, &h.speaker_name, h.utt, text, false),
CoreEvent::Final(f) if self.voice_channel.is_some() => {
CoreEvent::Final(f) if self.session.is_some() && f.heard.session == self.session => {
let h = &f.heard;
self.caption(h.speaker_id, &h.speaker_name, h.utt, &f.text, true);
}
@@ -181,18 +188,24 @@ mod tests {
use std::time::Duration;
const A: u64 = 11;
fn heard_in(utt: u32, session: u64) -> Heard {
Heard { speaker_id: A, speaker_name: "민수".into(), utt, session: Some(session) }
}
fn heard(utt: u32) -> Heard {
Heard { speaker_id: A, speaker_name: "민수".into(), utt }
heard_in(utt, 1)
}
fn fin_in(utt: u32, text: &str, wait_ms: u64, session: u64) -> CoreEvent {
CoreEvent::Final(HeardFinal { heard: heard_in(utt, session), text: text.into(), end_reason: "silence".into(),
audio_ms: 900, wait_ms, stt_ms: 200 })
}
fn fin(utt: u32, text: &str, wait_ms: u64) -> CoreEvent {
CoreEvent::Final(HeardFinal { heard: heard(utt), text: text.into(), end_reason: "silence".into(),
audio_ms: 900, wait_ms, stt_ms: 200 })
fin_in(utt, text, wait_ms, 1)
}
fn at(t: Instant, ms: u64) -> Instant {
t + Duration::from_millis(ms)
}
fn joined() -> CoreEvent {
CoreEvent::Joined { guild_id: 1, channel_id: 5 }
CoreEvent::Joined { guild_id: 1, channel_id: 5, session: 1 }
}
fn left() -> CoreEvent {
CoreEvent::Left { guild_id: 1, channel_id: 5, reason: "command".into() }
@@ -241,6 +254,25 @@ mod tests {
assert_eq!(d.view(&[], t)["voiceChannel"], Value::Null);
}
#[test]
fn a_transcript_of_the_channel_jarvis_moved_away_from_stays_out_of_the_new_ones_captions() {
let (mut d, t) = (Dashboard::default(), Instant::now());
d.on_event(&joined(), t);
d.on_event(&CoreEvent::SpeechStart(heard_in(7, 1)), t); // started in channel A, session 1
d.on_event(&left(), t);
d.on_event(&CoreEvent::Joined { guild_id: 1, channel_id: 6, session: 2 }, t);
d.on_event(&CoreEvent::Partial(heard_in(7, 1), "A에서 한 말".into()), t);
d.on_event(&fin_in(7, "A에서 한 말", 300, 1), t);
assert!(d.captions().is_empty(), "session 1's utterance is not session 2's");
// An utterance whose start was not seen in a session (None) is not shown either.
d.on_event(&CoreEvent::Partial(Heard { speaker_id: A, speaker_name: "민수".into(), utt: 8, session: None },
"어디서 온 말".into()), t);
assert!(d.captions().is_empty());
// The same person speaking in B is.
d.on_event(&fin_in(9, "B에서 한 말", 300, 2), t);
assert_eq!(d.captions().iter().map(|c| c.text.as_str()).collect::<Vec<_>>(), vec!["B에서 한 말"]);
}
#[test]
fn an_answer_is_timed_from_its_own_requests_speech_end_to_its_first_sound() {
let (mut d, t) = (Dashboard::default(), Instant::now());