feat(voice_worker): free a speaker's place when they leave the voice channel

- New speaker_leave message: ends the speaker's open utterance (its final is still sent) and drops
  their state, so a long session never hits the 32-speaker limit as people come and go.
- Spec and test cover more than 32 speakers in one session, each leaving mid-utterance.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-11 11:14:25 +09:00
parent 1309525aa8
commit 5838f9edd8
3 changed files with 40 additions and 3 deletions

View File

@@ -246,8 +246,8 @@ class Session:
self.error("bad_message", "not an object")
return
kind = msg.get("type")
handler = {"speaking_end": self._on_speaking_end, "config": self._on_config, "tts": self._on_tts,
"tts_cancel": self._on_tts_cancel}.get(kind)
handler = {"speaking_end": self._on_speaking_end, "speaker_leave": self._on_speaker_leave,
"config": self._on_config, "tts": self._on_tts, "tts_cancel": self._on_tts_cancel}.get(kind)
if handler is None:
self.error("bad_message", f"unknown type: {kind!r}")
return
@@ -258,6 +258,16 @@ class Session:
if st is not None:
self._handle_events(msg["speaker"], st, st.segmenter.speaking_end(time.monotonic()))
def _on_speaker_leave(self, msg: dict) -> None:
# The speaker left the voice channel: finish any open utterance, then free their place.
speaker = msg.get("speaker")
st = self.speakers.pop(speaker, None) if isinstance(speaker, str) else None
if st is not None:
self._handle_events(speaker, st, st.segmenter.speaking_end(time.monotonic()))
if self.worker.scheduler:
self.worker.scheduler.drop_partials(lambda key: key == (self.sid, speaker))
debug_log(f"speaker {speaker} left session {self.sid}", "voice_worker")
def _on_config(self, msg: dict) -> None:
changes = {k: v for k, v in msg.items() if k != "type"}
try:

View File

@@ -54,6 +54,7 @@ token closes with 4401.
```
-> binary 0x01 | u8 n | speaker id (n bytes UTF-8) | PCM16LE samples in the hello "audio" format
-> {"type": "speaking_end", "speaker": "..."} // Discord speaking stopped for this speaker
-> {"type": "speaker_leave", "speaker": "..."} // the speaker left the voice channel
-> {"type": "config", ...any STT settings...} // takes effect immediately
<- {"type": "speech_start", "speaker": "...", "utt": 3}
<- {"type": "partial", "speaker": "...", "utt": 3, "text": "..."}
@@ -61,6 +62,10 @@ token closes with 4401.
"audio_ms": 2310, "wait_ms": 160, "stt_ms": 120}
```
`speaker_leave` ends an open utterance as `speaking_end` does (its final is still sent) and frees the
speaker's state. A session holds at most 32 speakers at once, so the app sends it whenever someone
leaves; a speaker who comes back starts again at `utt` 1.
`utt` counts utterances per speaker. `final` always closes an utterance that sent `speech_start`, even
when the text is empty (noise that Whisper rejected). `wait_ms` is from the moment the end was decided
to the moment the final was sent; `stt_ms` is the model time of the batch it went through.
@@ -179,6 +184,6 @@ never close the connection after the handshake.
## Debug logging
`debug_log(..., "voice_worker")` at: connection accepted or refused, settings changed, utterance end
`debug_log(..., "voice_worker")` at: connection accepted or refused, settings changed, speaker left, utterance end
(speaker, reason, length), each STT batch (kind, size, model time), each TTS sentence (length, time),
cancel, and engine failures.