From beaacb39cd2213dbbe11ac1766e61af19634e49d Mon Sep 17 00:00:00 2001 From: javis-bot Date: Wed, 11 Nov 2026 12:13:04 +0900 Subject: [PATCH] fix(app): moving to another voice channel no longer ends the new session - Same-server moves point the existing songbird connection at the new channel and replace its handlers; removing and rejoining at once let the late gateway "left" update cancel the new join. - A driver disconnect without a reason (our own leave or move) is ignored, and a disconnect only ends the session its handler belongs to, so an old connection's late event cannot end a newer one. - Live test: move to a second voice channel and back; the session survives both, only the two moves end one, and the speaker is still heard afterwards. Co-Authored-By: Claude Opus 5.5 --- app/src/discord.rs | 41 ++++++++++++++++++++++++++---------- app/src/discord_core.spec.md | 15 +++++++++---- app/tests/live_discord.rs | 25 ++++++++++++++++++---- 3 files changed, 62 insertions(+), 19 deletions(-) diff --git a/app/src/discord.rs b/app/src/discord.rs index ee40a46..041d40d 100644 --- a/app/src/discord.rs +++ b/app/src/discord.rs @@ -323,7 +323,19 @@ impl DiscordCore { if self.voice_channel_id() == Some(channel_id) { return Ok(()); } - self.leave_locked("moved").await; + let current = self.inner.session.lock().unwrap().take(); + if let Some(old) = current { + if old.guild_id == gc.guild_id.get() { + // Same server: move the existing connection. Leaving first and joining at once races + // the gateway (the late "left" update cancels the new join). + end_session(&self.inner, old, "moved", false).await; + if let Some(call) = self.inner.songbird.get(gc.guild_id) { + call.lock().await.remove_all_global_events(); + } + } else { + end_session(&self.inner, old, "moved", true).await; + } + } let call = self.inner.songbird.join(gc.guild_id, gc.id).await.map_err(|e| { CoreError(format!("could not connect to voice channel {channel_id}: {e}")) }); @@ -337,7 +349,7 @@ impl DiscordCore { let rx = Arc::new(Mutex::new(RxState::default())); { let mut c = call.lock().await; - let h = Receiver { core: Arc::downgrade(&self.inner), rx: rx.clone(), guild_id: gc.guild_id.get() }; + let h = Receiver { core: Arc::downgrade(&self.inner), rx: rx.clone() }; c.add_global_event(CoreEvent_::SpeakingStateUpdate.into(), h.clone()); c.add_global_event(CoreEvent_::RtpPacket.into(), h.clone()); c.add_global_event(CoreEvent_::ClientDisconnect.into(), h.clone()); @@ -365,7 +377,7 @@ impl DiscordCore { async fn leave_locked(&self, reason: &str) -> bool { let Some(s) = self.inner.session.lock().unwrap().take() else { return false }; - end_session(&self.inner, s, reason).await; + end_session(&self.inner, s, reason, true).await; true } @@ -450,7 +462,9 @@ fn with_playback(inner: &Inner, f: impl FnOnce(&mut Playback, s: Session, reason: &str) { +/// End a session's state; with `disconnect`, also leave the voice channel (a same-server move keeps +/// the connection and points it at the new channel instead). +async fn end_session(inner: &Arc, s: Session, reason: &str, disconnect: bool) { s.alive.store(false, Ordering::Relaxed); with_playback(inner, |p| p.stop()); let _ = inner.player.send(PlayerCmd::SetCall(None)); @@ -463,7 +477,9 @@ async fn end_session(inner: &Arc, s: Session, reason: &str) { inner.worker.speaker_leave(&user.to_string()); } inner.gate.lock().unwrap().clear(); - let _ = inner.songbird.remove(GuildId::new(s.guild_id)).await; + if disconnect { + let _ = inner.songbird.remove(GuildId::new(s.guild_id)).await; + } debug_log!("voice", "left voice {} reason={reason} packets lost={lost} late={late} bad={bad}", s.channel_id); let _ = inner.events.send(CoreEvent::Left { guild_id: s.guild_id, channel_id: s.channel_id, reason: reason.into() }); } @@ -597,8 +613,8 @@ async fn player_task(core: std::sync::Weak, mut rx: mpsc::UnboundedReceiv #[derive(Clone)] struct Receiver { core: std::sync::Weak, + /// This session's receive state; also tells this session apart from a later one. rx: Arc>, - guild_id: u64, } impl Receiver { @@ -651,18 +667,21 @@ impl VoiceEventHandler for Receiver { } EventContext::RtpPacket(p) => self.packet(&inner, p), EventContext::ClientDisconnect(d) => self.left(&inner, d.user_id.0), - EventContext::DriverDisconnect(_) => { + // No reason: we asked for it (leave or move), and that path already ended the session. + EventContext::DriverDisconnect(d) if d.reason.is_some() => { // The driver gave up reconnecting (kicked, channel deleted, long network loss). let core = DiscordCore { inner: inner.clone() }; - let guild = self.guild_id; + let rx = self.rx.clone(); tokio::spawn(async move { let _g = core.inner.voice_lock.lock().await; let s = { let mut s = core.inner.session.lock().unwrap(); - if s.as_ref().is_some_and(|s| s.guild_id == guild) { s.take() } else { None } + // Only the session this handler belongs to: a late event from an old + // connection must not end a newer one. + if s.as_ref().is_some_and(|s| Arc::ptr_eq(&s.rx, &rx)) { s.take() } else { None } }; if let Some(s) = s { - end_session(&core.inner, s, "disconnected").await; + end_session(&core.inner, s, "disconnected", true).await; } }); } @@ -742,7 +761,7 @@ impl EventHandler for Handler { } let was_here = old.and_then(|o| o.channel_id).map(|c| c.get()) == Some(session_channel); if was_here && new_channel != Some(session_channel) { - Receiver { core: Arc::downgrade(inner), rx, guild_id: 0 }.left(inner, user); + Receiver { core: Arc::downgrade(inner), rx }.left(inner, user); } } diff --git a/app/src/discord_core.spec.md b/app/src/discord_core.spec.md index 7bf6621..22e5f79 100644 --- a/app/src/discord_core.spec.md +++ b/app/src/discord_core.spec.md @@ -93,7 +93,10 @@ did not open is ignored. ## Voice -One voice session at a time, in the configured server. Joining another channel leaves the current one. +One voice session at a time, in the configured server. Joining another channel ends the current +session (`Left` with `moved`). Within the same server the existing songbird connection is pointed at +the new channel and its event handlers are replaced: leaving and joining at once races the gateway, +and the late "left" update cancelled the new join (seen on real Discord). - **Join** (slash command `join`, auto-join, or `join(channel_id)`): connect through songbird with DAVE end-to-end encryption, not deafened, then post `voice.joinNotice` in the voice channel's chat. A @@ -129,7 +132,9 @@ One voice session at a time, in the configured server. Joining another channel l `cancelled: true`. Stopping by voice ("자비스, 멈춰") needs call detection and comes in M4. - **Leave** (slash command `leave`, or `leave()`): stop speaking, send `speaker_leave` for everyone heard, and disconnect. When songbird's driver gives up the connection (kicked, channel deleted, long - network loss) the session ends with `disconnected`. When someone moves the bot to another channel, + network loss) the session ends with `disconnected`. A driver disconnect without a reason is one the + core asked for (leave or move) and is ignored, and a disconnect only ends the session its handler + was attached to, so an old connection's late event never ends a newer session. When someone moves the bot to another channel, the session follows it. ## Slash command @@ -170,8 +175,10 @@ registered until it is removed (M5 setup). voice channel with the real worker on the GPU. It refuses to run if a person is in the channel or either bot is already in a voice channel, and cleans up even when a step fails. It checks command registration, transcription of recorded sentences played into the channel, speech the other bot - hears and the STT reads back, stop, chat events, history, sending without pings, the speaker - leaving and coming back three times (each a DAVE group change), and leaving. + hears and the STT reads back, stop, chat events, history, sending without pings, moving to a second + voice channel and back (the session survives each move and only the two moves end one), the speaker + leaving and coming back three times (each a DAVE group change), and leaving. It needs a second voice + channel of the server (`JARVIS_LIVE_VOICE_CHANNEL_2`) and refuses if a person is in either. Measured on this server's RTX 5050 (2026-11-11): five sentences all heard (character error rate 0% for three, 5% and 12.5% for two), 0.71-0.90 s from the end of speech to the final, Jarvis's speech read back at 3.4-6.9%, sound gone 0.03-0.08 s after stop, and the sentence heard exactly after each diff --git a/app/tests/live_discord.rs b/app/tests/live_discord.rs index 07d0276..f662d42 100644 --- a/app/tests/live_discord.rs +++ b/app/tests/live_discord.rs @@ -6,6 +6,7 @@ //! //! JARVIS_LIVE_DISCORD=1 JARVIS_LIVE_BOT_TOKEN=... JARVIS_LIVE_SPEAKER_TOKEN=... \ //! JARVIS_LIVE_GUILD= JARVIS_LIVE_VOICE_CHANNEL= \ +//! JARVIS_LIVE_VOICE_CHANNEL_2= \ //! JARVIS_VOICE_PYTHON= JARVIS_MELO_PYTHON= \ //! cargo test --release --test live_discord -- --nocapture //! @@ -134,6 +135,7 @@ async fn discord_voice_core_on_real_discord() { } let guild: u64 = env("JARVIS_LIVE_GUILD").parse().unwrap(); let voice: u64 = env("JARVIS_LIVE_VOICE_CHANNEL").parse().unwrap(); + let voice2: u64 = env("JARVIS_LIVE_VOICE_CHANNEL_2").parse().unwrap(); let bot_token = env("JARVIS_LIVE_BOT_TOKEN"); let root = Path::new(env!("CARGO_MANIFEST_DIR")); let samples = root.join("../scripts/m0/assets/ko_samples"); @@ -151,9 +153,9 @@ async fn discord_voice_core_on_real_discord() { until("guild in cache", 10_000, || s_cache.guild(GuildId::new(guild)).is_some()).await; { let g = s_cache.guild(GuildId::new(guild)).unwrap(); - let people = g.voice_states.values().filter(|v| v.channel_id.map(|c| c.get()) == Some(voice) + let people = g.voice_states.values().filter(|v| v.channel_id.is_some_and(|c| c.get() == voice || c.get() == voice2) && !s_cache.user(v.user_id).map(|u| u.bot).unwrap_or(false)).count(); - assert_eq!(people, 0, "refusing to test: people are in the voice channel"); + assert_eq!(people, 0, "refusing to test: people are in the voice channels"); for id in [speaker_id, jarvis_app_id] { assert!(g.voice_states.get(&UserId::new(id)).and_then(|v| v.channel_id).is_none(), "refusing to test: bot {id} is in a voice channel"); @@ -331,7 +333,22 @@ async fn discord_voice_core_on_real_discord() { tokio::time::sleep(Duration::from_millis(500)).await; assert!(!events.lock().unwrap().iter().any(|e| matches!(e, CoreEvent::Chat(c) if c.content == reply))); - // 6. Group changes: every join and leave changes the DAVE (MLS) group, and songbird has an open + // 6. Moving to another channel and back: the old connection's shutdown must not end the new one. + for (to, what) in [(voice2, "the second channel"), (voice, "the first channel")] { + core.join(to).await.expect("move"); + tokio::time::sleep(Duration::from_secs(3)).await; + assert_eq!(core.voice_channel_id(), Some(to), "session lost after moving to {what}"); + let in_channel = s_cache.guild(GuildId::new(guild)) + .and_then(|g| g.voice_states.get(&UserId::new(jarvis)).and_then(|v| v.channel_id)).map(|c| c.get()); + assert_eq!(in_channel, Some(to), "Jarvis is not in {what} on Discord"); + } + let moves: Vec = events.lock().unwrap().iter().filter_map(|e| match e { + CoreEvent::Left { reason, .. } => Some(reason.clone()), + _ => None, + }).collect(); + assert_eq!(moves, ["moved", "moved"], "only the two moves end a session"); + + // 7. Group changes: every join and leave changes the DAVE (MLS) group, and songbird has an open // report of receive stalling after one (issue #310). The speaker leaves and comes back three // times and must still be heard each time, under its new SSRC. let wav = std::fs::read(samples.join("ko_03.wav")).unwrap(); @@ -362,7 +379,7 @@ async fn discord_voice_core_on_real_discord() { report.insert("rejoins".into(), Value::from(rejoins.clone())); assert!(rejoins.iter().all(|r| r["cer"].as_f64().unwrap() < 0.15), "not heard after a rejoin: {rejoins:?}"); - // 7. Leaving. + // 8. Leaving. assert!(core.leave("command").await); until("Jarvis to leave", 10_000, || { s_cache.guild(GuildId::new(guild)).and_then(|g| g.voice_states.get(&UserId::new(jarvis)).and_then(|v| v.channel_id)).is_none()