Files
javis_bot/tests/test_voice_worker_brain.py
javis-bot 864b2e7fdb feat(voice_worker): record history from the app and answer reply requests with the brain
Worker (voice_worker.spec.md "History and brain"):
- --history <file> keeps the conversation history store; --brain hosts the
  jarvis reply engine (JarvisBrain, settings point ollama_base_url at /v1).
- voice_join / voice_leave / member_join / member_leave keep presence; the
  session end closes it. Non-empty finals are stored under the speaker's name
  in the current voice channel.
- chat_message / chat_edit / chat_delete keep registered chat channels.
- reply builds the asker's scoped history context, runs the brain on a thread
  (one at a time), answers reply_text, stores a voice answer, and with
  speak=true says it under the same id. memory settings in hello and
  memory_config. brain_unavailable without --brain.

App (discord_core.spec.md "History reporting"):
- WorkerClient sends the new messages; the core reports the voice channel
  and its heard members after a join, a move and every worker reconnect,
  member joins and leaves, voice_leave when a session ends, and every message
  of a readable registered channel (its own too, with `to` on replies), edits
  and deletes.
- live_discord runs the worker with --history and checks the store at the
  end: 8 finals under the speaker's name, the edited chat message only,
  Jarvis's own messages, 5 presence intervals, none left open (passed, 107.8 s).

Eval: history recall gains a chat-record case and fails a reply that reads a
record stamp out (field run with qwen2.5:7b did once in four).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 13:16:24 +09:00

228 lines
10 KiB
Python

"""Voice worker history and brain messages (voice_worker.spec.md, "History and brain").
Over a real local WebSocket with the fake STT/VAD/TTS engines of test_voice_worker_server and a
fake brain that records the history context it was given. Behaviours: presence and finals reach
the history store, chat events are kept, a reply answers with the asker's own scope, a voice answer
is stored and can be spoken, and the worker reports when it has no brain.
"""
from __future__ import annotations
import asyncio
import json
import pytest
from jarvis.memory.history import HistoryStore, Scope
from voice_worker.protocol import decode_tts_frame
from voice_worker.server import VoiceWorker
from tests.test_voice_worker_server import TOKEN, AmplitudeVad, FakeTts, LevelTranscriber, hello, recv_until, stream
websockets = pytest.importorskip("websockets")
from websockets.asyncio.client import connect # noqa: E402
class FakeBrain:
def __init__(self, answer=lambda user, text: f"answer for {text}"):
self.answer = answer
self.calls = []
def reply(self, user, text, history):
self.calls.append((user, text, history))
return self.answer(user, text)
@pytest.fixture
def store(tmp_path):
s = HistoryStore(str(tmp_path / "history.sqlite"))
yield s
s.close()
async def with_worker(body, store=None, brain=None, tts=None):
worker = VoiceWorker(TOKEN, LevelTranscriber(), AmplitudeVad, tts, history=store, brain=brain)
server = await worker.serve("127.0.0.1", 0)
port = server.sockets[0].getsockname()[1]
try:
return await asyncio.wait_for(body(f"ws://127.0.0.1:{port}"), 15)
finally:
server.close()
await server.wait_closed()
await worker.stop()
async def send(ws, **msg):
await ws.send(json.dumps(msg))
async def say(ws, speaker, level):
await stream(ws, speaker, level, 600)
await send(ws, type="speaking_end", speaker=speaker)
await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "final" and m["speaker"] == speaker)
def texts(records):
return [r.text for r in records]
@pytest.mark.unit
def test_finals_in_a_voice_channel_are_stored_under_the_speakers_name(store):
async def body(url):
async with connect(url) as ws:
await hello(ws, stt={"partials": False})
await say(ws, "100", 0.9) # before Jarvis is in a channel: not stored
await send(ws, type="voice_join", channel="vc1")
await send(ws, type="member_join", user="100", name="Alice")
await say(ws, "100", 0.6)
asyncio.run(with_worker(body, store=store))
recs = store.records_of("100")
assert texts(recs) == ["level 0.6"]
assert recs[0].author_name == "Alice" and recs[0].channel_id == "vc1" and recs[0].kind == "voice"
@pytest.mark.unit
def test_reply_answers_with_the_askers_own_scope_and_stores_the_voice_answer(store):
brain = FakeBrain()
async def body(url):
async with connect(url) as ws:
await hello(ws, stt={"partials": False})
await send(ws, type="voice_join", channel="vc1")
await send(ws, type="member_join", user="100", name="Alice")
await say(ws, "100", 0.9)
await send(ws, type="member_join", user="200", name="Bob")
await say(ws, "200", 0.6)
await send(ws, type="reply", id="r1", user="100", text="what did we say", via="voice", speak=False)
seen = await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "reply_text")
await send(ws, type="reply", id="r2", user="200", text="and me", via="voice", speak=False)
seen += await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "reply_text")
return seen
seen = asyncio.run(with_worker(body, store=store, brain=brain))
answers = [m for m in seen if isinstance(m, dict) and m.get("type") == "reply_text"]
assert answers[0] == {"type": "reply_text", "id": "r1", "text": "answer for what did we say", "failed": False}
# Alice was there when Bob spoke, so she sees both; Bob joined after Alice spoke.
alice_ctx, bob_ctx = brain.calls[0][2], brain.calls[1][2]
assert any("Alice: level 0.9" in x for x in alice_ctx.recent)
assert any("Bob: level 0.6" in x for x in alice_ctx.recent)
assert not any("Alice: level 0.9" in x for x in bob_ctx.recent)
# The voice answer is Alice's own record now.
assert "answer for what did we say" in texts(store.records_of("100"))
@pytest.mark.unit
def test_spoken_reply_comes_back_as_tts_under_the_same_id(store):
async def body(url):
async with connect(url) as ws:
await hello(ws)
await send(ws, type="voice_join", channel="vc1")
await send(ws, type="reply", id="r9", user="100", text="hello", via="voice", speak=True)
return await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "tts_done")
seen = asyncio.run(with_worker(body, store=store, brain=FakeBrain(), tts=FakeTts()))
frames = [decode_tts_frame(m) for m in seen if isinstance(m, bytes)]
assert frames and all(rid == "r9" for rid, _, _ in frames)
done = [m for m in seen if isinstance(m, dict) and m.get("type") == "tts_done"][0]
assert done["id"] == "r9" and not done["failed"]
@pytest.mark.unit
def test_chat_answers_are_not_stored_by_the_worker_but_reported_messages_are(store):
async def body(url):
async with connect(url) as ws:
await hello(ws)
await send(ws, type="reply", id="c1", user="100", text="hi", via="chat", speak=False)
await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "reply_text")
await send(ws, type="chat_message", channel="ch1", message_id="m1", author="100", name="Alice",
text="first", ts=1_800_000_000.0)
await send(ws, type="chat_message", channel="ch1", message_id="m2", author="900", name="Jarvis",
text="answer for hi", ts=1_800_000_001.0, to="100")
await send(ws, type="chat_edit", channel="ch1", message_id="m1", text="first, edited")
await send(ws, type="chat_message", channel="ch1", message_id="m3", author="100", name="Alice",
text="gone soon", ts=1_800_000_002.0)
await send(ws, type="chat_delete", channel="ch1", message_id="m3")
# A round trip makes sure the worker handled everything above.
await send(ws, type="reply", id="c2", user="100", text="x", via="chat", speak=False)
await recv_until(ws, lambda m: isinstance(m, dict) and m.get("id") == "c2")
asyncio.run(with_worker(body, store=store, brain=FakeBrain()))
assert texts(store.records_of("100")) == ["first, edited", "answer for hi"]
@pytest.mark.unit
def test_leaving_the_voice_channel_stops_storing_and_closes_presence(store):
async def body(url):
async with connect(url) as ws:
await hello(ws, stt={"partials": False})
await send(ws, type="voice_join", channel="vc1")
await send(ws, type="member_join", user="100", name="Alice")
await send(ws, type="voice_leave")
await say(ws, "200", 0.6)
asyncio.run(with_worker(body, store=store))
assert store.records_of("200") == []
assert store.recent("100", now=4_000_000_000, scope=Scope(), minutes=10 ** 9) == []
@pytest.mark.unit
def test_memory_off_gives_the_brain_no_history(store):
brain = FakeBrain()
async def body(url):
async with connect(url) as ws:
await hello(ws, stt={"partials": False}, memory={"off": True})
await send(ws, type="voice_join", channel="vc1")
await say(ws, "100", 0.9)
await send(ws, type="reply", id="r1", user="100", text="q", via="voice", speak=False)
await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "reply_text")
await send(ws, type="memory_config", off=False)
await send(ws, type="reply", id="r2", user="100", text="q", via="voice", speak=False)
await recv_until(ws, lambda m: isinstance(m, dict) and m.get("id") == "r2")
asyncio.run(with_worker(body, store=store, brain=brain))
off_ctx, on_ctx = brain.calls[0][2], brain.calls[1][2]
assert off_ctx.recent == [] and off_ctx.matches == []
assert any("level 0.9" in x for x in on_ctx.recent)
@pytest.mark.unit
@pytest.mark.parametrize("answer", [lambda u, t: None, lambda u, t: 1 / 0])
def test_a_failed_brain_answer_is_reported(store, answer):
async def body(url):
async with connect(url) as ws:
await hello(ws)
await send(ws, type="reply", id="r1", user="100", text="q", via="voice", speak=True)
return await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "reply_text")
seen = asyncio.run(with_worker(body, store=store, brain=FakeBrain(answer), tts=FakeTts()))
assert seen[-1] == {"type": "reply_text", "id": "r1", "text": "", "failed": True}
@pytest.mark.unit
def test_without_a_brain_reply_is_an_error():
async def body(url):
async with connect(url) as ws:
await hello(ws)
await send(ws, type="voice_join", channel="vc1")
await send(ws, type="reply", id="r1", user="100", text="q", via="voice", speak=False)
return await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "error")
seen = asyncio.run(with_worker(body))
assert seen[-1]["code"] == "brain_unavailable" and seen[-1]["id"] == "r1"
@pytest.mark.unit
def test_malformed_history_messages_are_rejected(store):
async def body(url):
async with connect(url) as ws:
await hello(ws)
await send(ws, type="member_join", user=5)
await send(ws, type="reply", id="r1", user="100", via="radio")
return [await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "error")
for _ in range(2)]
seen = asyncio.run(with_worker(body, store=store, brain=FakeBrain()))
assert [s[-1]["code"] for s in seen] == ["bad_message", "bad_message"]