fix(voice_worker): keep history and the brain's output scoped to who was there

- A final is stored under the channel, speaker name and time fixed when its
  end was decided. Decoding takes a while; a move, rename or late joiner
  meanwhile used to change where and when it was said, and who heard it.
- At start the worker closes presence a killed run left open
  (HistoryStore.close_open_presence), so a person who is gone no longer
  hears new speech after a restart.
- run_reply_engine(legacy_memory=False) skips v1's single-user memory (warm
  profile, extractor, diary, graph, digest); the v2 brain uses it so another
  user's profile or diary never reaches the prompt.
- While a brain reply runs, stdout and stderr are muted (also helper threads
  such as the web fetch pool); the worker logs through voice_worker/log.py to
  the original stderr.
- Live check: Ollama's /v1 fixes the context at 2,048 tokens and cut the
  prompt in earlier runs; with an 8k tag the chat-record question was answered
  4 of 4 and nothing reached the output. Noted in llm_contexts and the eval.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-11 13:28:25 +09:00
parent 864b2e7fdb
commit b3dcd12ec9
18 changed files with 265 additions and 25 deletions

View File

@@ -13,7 +13,7 @@ from typing import Callable
import numpy as np
from jarvis.debug import debug_log
from .log import debug_log
from .audio import InputConverter, loudness_equalise
from .brain import Brain, MemorySettings
@@ -57,6 +57,12 @@ class VoiceWorker:
self._ids = itertools.count(1)
async def start(self) -> None:
if self.history is not None:
# A killed worker never closed its sessions' presence; nobody is heard until the app
# reports who is there again.
n = self.history.close_open_presence(time.time())
if n:
debug_log(f"closed {n} presence intervals left open by a previous run", "voice_worker")
if self.transcriber is not None:
self.scheduler = SttScheduler(self.transcriber)
self.scheduler.start()
@@ -230,12 +236,16 @@ class Session:
elif isinstance(ev, Final):
debug_log(f"utterance end speaker={speaker} utt={ev.utt} reason={ev.reason} "
f"ms={len(ev.audio) * 1000 // SR}", "voice_worker")
# Where, when and by whom it was said, fixed now: decoding can take a while, and a
# move or a late joiner meanwhile must not change who heard it.
said = (self.voice_channel, self.names.get(speaker, speaker),
time.time() - (time.monotonic() - ev.decided_at))
if sched is None:
self._send_final(speaker, ev, SttResult("", 0, "stt unavailable"))
self._send_final(speaker, ev, SttResult("", 0, "stt unavailable"), said)
else:
sched.submit_final((self.sid, speaker), loudness_equalise(ev.audio, ev.voiced),
self._opts(self.settings.final_beam),
lambda res, ev=ev: self._send_final(speaker, ev, res))
lambda res, ev=ev, said=said: self._send_final(speaker, ev, res, said))
def _partial_cb(self, speaker: str, st: _Speaker, utt: int):
def cb(res: SttResult) -> None:
@@ -244,10 +254,11 @@ class Session:
self.send({"type": "partial", "speaker": speaker, "utt": utt, "text": res.text})
return cb
def _send_final(self, speaker: str, ev: Final, res: SttResult) -> None:
def _send_final(self, speaker: str, ev: Final, res: SttResult, said: tuple) -> None:
channel, name, ts = said
store = self.worker.history
if store is not None and self.voice_channel and res.text.strip():
store.add_voice(self.voice_channel, speaker, self.names.get(speaker, speaker), res.text, time.time())
if store is not None and channel and res.text.strip():
store.add_voice(channel, speaker, name, res.text, ts)
self.send({"type": "final", "speaker": speaker, "utt": ev.utt, "text": res.text,
"end_reason": ev.reason, "audio_ms": len(ev.audio) * 1000 // SR,
"wait_ms": int((time.monotonic() - ev.decided_at) * 1000), "stt_ms": res.stt_ms})