feat(P3): mind/mouth split — separate voice model for the final reply (seam, default off)

The mind (chat backend/model) decides, reasons, and runs tools → a draft; the mouth
re-voices that draft in her character. Default: no mouth configured → the mind's
draft IS the reply, bit-for-bit the old behavior (and old streaming path untouched).

- config: MOUTH_BACKEND / MOUTH_MODEL. The slot for an eventual fine-tuned voice.
- chat: _mind_loop (tool/generation loop, non-stream, returns draft + tools_run),
  _voice_pass / mind.voice_messages (re-voice the draft, keep every fact/number),
  _mouth_target (active only when configured AND != mind). respond + respond_stream
  branch: mouth off = stream the mind directly (unchanged); mouth on = mind decides
  + runs tools, then the mouth streams the re-voiced reply. Falls back to the draft
  on any mouth failure (chat never breaks).
- Key payoff: the mouth needs no tool support (the mind handles tools), so it can be
  a non-tool character model (Dolphin / Claude / fine-tune). Makes the fine-tune
  easy: teach a small model to *sound* like Lyra, not to be smart.
- tests: mouth target on/off, voice_messages shape, voice_pass revoice+fallback.
  Suite 96 green, ruff clean.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-24 06:08:06 +00:00
parent a7af461cdb
commit 03aceec6fa
5 changed files with 183 additions and 58 deletions
+43
View File
@@ -61,3 +61,46 @@ def test_assemble_runs_the_pipeline(lyra, monkeypatch):
assert turn.mode is not None # route ran
assert turn.messages and turn.messages[-1]["role"] == "user" # compose ran
assert turn.messages[-1]["content"] == "hey what's up"
# --- mind/mouth split (P3) ----------------------------------------------
def test_mouth_target_off_by_default(monkeypatch):
import importlib
from lyra import config
monkeypatch.delenv("MOUTH_BACKEND", raising=False)
monkeypatch.delenv("MOUTH_MODEL", raising=False)
import lyra.chat as chat
importlib.reload(chat)
assert chat._mouth_target(config.load(), "cloud", "gpt-4o") is None # mouth == mind
def test_mouth_target_when_configured(monkeypatch):
import importlib
from lyra import config
monkeypatch.setenv("MOUTH_BACKEND", "local")
monkeypatch.setenv("MOUTH_MODEL", "dolphin3:8b")
import lyra.chat as chat
importlib.reload(chat)
assert chat._mouth_target(config.load(), "cloud", "gpt-4o") == ("local", "dolphin3:8b")
def test_voice_messages_carries_draft_and_instruction(lyra):
_, mind = lyra
out = mind.voice_messages([{"role": "user", "content": "hi"}], "draft with FACT 42")
assert out[-2] == {"role": "assistant", "content": "draft with FACT 42"}
assert out[-1]["role"] == "system" and "your own voice" in out[-1]["content"].lower()
def test_voice_pass_revoices_then_falls_back(lyra, monkeypatch):
_, mind = lyra
import importlib
import lyra.chat as chat
importlib.reload(chat)
monkeypatch.setattr(chat.llm, "complete", lambda msgs, backend=None, model=None: "voiced (FACT 42)")
assert chat._voice_pass([], "draft FACT 42", "local", "dolphin3:8b") == "voiced (FACT 42)"
# on failure it keeps the mind's draft (chat must not break)
def boom(*a, **k):
raise RuntimeError("mouth down")
monkeypatch.setattr(chat.llm, "complete", boom)
assert chat._voice_pass([], "draft FACT 42", "local", "dolphin3:8b") == "draft FACT 42"