fix(voice_worker): the v2 brain answers in the asker's language

v1's tts_engine default (English-only Piper) made the reply engine force
English, so Korean questions got English answers. The v2 brain speaks
through the worker's MeloTTS and chat answers are not spoken, so it runs
with tts_engine=melo; an output_language lock still wins.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-12 09:16:34 +09:00
parent 9e75df266b
commit 9ff307d7fe
4 changed files with 35 additions and 3 deletions

View File

@@ -14,7 +14,7 @@ Every distinct LLM call in Jarvis, what feeds it, what consumes it, and how it i
- **Inputs**:
- Redacted user query
- Recent dialogue (last 5 minutes), including in-loop tool-call + tool-role messages from prior replies within the active conversation (tool carryover, `DialogueMemory.record_tool_turn` / `get_recent_turns_with_tools` in [src/jarvis/memory/conversation.py](src/jarvis/memory/conversation.py); per-prompt cap via `cfg.tool_carryover_max_turns` / `tool_carryover_per_entry_chars`; storage cap `_tool_turns_max_storage = 16`; cleared on `stop` signal AND on new-conversation entry; UNTRUSTED WEB EXTRACT fence markers preserved on truncation; both `content` and `tool_calls[*].function.arguments` scrubbed on write)
- Unified system prompt from [src/jarvis/system_prompt.py](src/jarvis/system_prompt.py) + ASR note + tool-protocol guidance. Reply language is resolved by `reply_language_directive(lang, cfg.tts_engine)` where `lang = _resolve_output_language()` — the single source of truth that prefers the settings-web UI value (config JSON `output_language`) over the compose `OUTPUT_LANGUAGE` env, so changing the language in the settings page takes effect. An explicit lock wins (forces "reply only in `<language>`", also forbidding other scripts so small models stop leaking trailing CJK/Hanja); else a Piper/Chatterbox TTS forces English (English-only voices); else (multilingual TTS, no lock) the assistant replies in the user's own language. The directive is inserted near the FRONT of the guidance list so a small model gives it primacy, and the SAME resolved `lang` feeds `build_system_prompt()`, which rewrites the persona's "in the user's language" clause to the locked language so the persona cannot contradict the directive (previously the persona read the raw env while the directive read the config value, so a settings-UI change was honoured by one and ignored by the other). Gated in `_build_initial_system_message()` at [engine.py](src/jarvis/reply/engine.py).
- Unified system prompt from [src/jarvis/system_prompt.py](src/jarvis/system_prompt.py) + ASR note + tool-protocol guidance. Reply language is resolved by `reply_language_directive(lang, cfg.tts_engine)` where `lang = _resolve_output_language()` — the single source of truth that prefers the settings-web UI value (config JSON `output_language`) over the compose `OUTPUT_LANGUAGE` env, so changing the language in the settings page takes effect. An explicit lock wins (forces "reply only in `<language>`", also forbidding other scripts so small models stop leaking trailing CJK/Hanja); else a Piper/Chatterbox TTS forces English (English-only voices); else (multilingual TTS, no lock) the assistant replies in the user's own language. The v2 voice worker's brain (`src/voice_worker/brain.py`) runs with `tts_engine="melo"`, so v1's English-only Piper default never locks v2 voice or chat answers to English. The directive is inserted near the FRONT of the guidance list so a small model gives it primacy, and the SAME resolved `lang` feeds `build_system_prompt()`, which rewrites the persona's "in the user's language" clause to the locked language so the persona cannot contradict the directive (previously the persona read the raw env while the directive read the config value, so a settings-UI change was honoured by one and ignored by the other). Gated in `_build_initial_system_message()` at [engine.py](src/jarvis/reply/engine.py).
- **Tool honesty rules**: `TOOL_HONESTY_RULES` inside the persona, plus `tool_honesty_reminder(allowed_tools)` appended after every context block (just before the closing reply-language lock) naming the turn's tools. Added after M0 (2026-11-10) caught models answering past an available tool.
- **Operator instructions** (two sources, both framed "Additional instructions from the operator:" and appended near the end of the guidance list): the settings-UI `llm_instructions` config field, and every `*.md` file in `AGENTS_DIR` (default `/app/agents`, bind-mounted from `./agents`). The file-based set is read once per turn by `load_agent_instructions()` in [src/jarvis/system_prompt.py](src/jarvis/system_prompt.py) and concatenated in filename order, so dropping/editing a `.md` applies on the next reply with no rebuild/restart; fail-open to `""` when the folder is absent/empty/unreadable.
- **Warm profile block** (query-agnostic User + Directives excerpt from the knowledge graph, composed by `build_warm_profile()` / `format_warm_profile_block()` in [src/jarvis/memory/graph_ops.py](src/jarvis/memory/graph_ops.py) at Step 3.5 of `reply()`; no LLM call, pure SQLite read; injected unconditionally so personalisation is the default; result cached in `DialogueMemory._hot_cache` under `DialogueMemory.WARM_PROFILE_CACHE_KEY` for the lifetime of the active conversation. Invalidated on `stop`, on new-conversation entry, AND on User/Directives graph mutations via the listener registered in [src/jarvis/daemon.py](src/jarvis/daemon.py) against `register_graph_mutation_listener` in [src/jarvis/memory/graph.py](src/jarvis/memory/graph.py); World-branch writes are ignored)

View File

@@ -3,6 +3,7 @@ interface, so the worker logic is tested with a fake (voice_worker.spec.md, "His
from __future__ import annotations
import dataclasses
import sys
import threading
import time
@@ -121,7 +122,10 @@ class JarvisBrain:
from jarvis.config import load_settings
from jarvis.memory.db import Database
self.cfg = load_settings()
# v1's tts_engine names the v1 desktop voice (English-only Piper by default), which makes the
# engine lock replies to English. v2 speaks through the worker's TTS and chat answers are
# not spoken, so the brain answers in the asker's language (an OUTPUT_LANGUAGE lock still wins).
self.cfg = dataclasses.replace(load_settings(), tts_engine="melo")
self.db = Database(self.cfg.db_path, self.cfg.sqlite_vss_path)
self.language = language
self._memories: dict = {}

View File

@@ -233,7 +233,10 @@ which carry Discord's own time.
`cancelled: true` and no audio. Each user keeps their own short-term dialogue
memory (tool carry-over) across replies. The brain runs the reply engine with v1's single-user
memory off (`legacy_memory=False`: no warm profile, diary or graph), because several people share
this assistant and only the asker's scoped history may reach the prompt.
this assistant and only the asker's scoped history may reach the prompt. It answers in the
asker's language: v1's `tts_engine` setting (an English-only Piper voice by default) does not
lock replies to English, because v2 speaks through the worker's TTS and chat answers are not
spoken. An `output_language` lock in the jarvis settings still wins.
The asker is set in `jarvis.reply.turn_state` for the turn, so tools that keep personal data
(meals) store and read it under that person only.
- `addressed` and `rank` are the conversation's questions (`app/src/conversation.spec.md`), answered

View File

@@ -355,6 +355,31 @@ def test_the_brain_turns_off_the_single_user_memory(tmp_path, monkeypatch):
assert seen["legacy_memory"] is False
@pytest.mark.unit
def test_the_brain_answers_in_the_askers_language_even_with_a_v1_english_voice(tmp_path, monkeypatch):
"""v1's ``tts_engine`` (an English-only Piper voice by default) must not lock v2 replies to
English: v2 speaks through the worker's own TTS, and chat answers are not spoken at all."""
pytest.importorskip("mcp") # the real brain needs the jarvis dependencies (--brain)
from jarvis.memory.history import HistoryContext
from jarvis.system_prompt import reply_language_directive
from voice_worker import brain as brain_mod
cfg = tmp_path / "config.json"
cfg.write_text(json.dumps({"db_path": str(tmp_path / "jarvis.db"), "tts_engine": "piper"}))
monkeypatch.setenv("JARVIS_CONFIG_PATH", str(cfg))
monkeypatch.delenv("OUTPUT_LANGUAGE", raising=False)
seen = {}
def fake_engine(db, cfg, tts, text, mem, language=None, history=None, legacy_memory=True):
seen["directive"] = reply_language_directive(None, cfg.tts_engine)
return "ok"
import jarvis.reply.engine as engine
monkeypatch.setattr(engine, "run_reply_engine", fake_engine)
brain_mod.JarvisBrain().reply("100", "지금 몇 시야?", HistoryContext())
assert seen["directive"] is None
@pytest.mark.unit
def test_the_brain_tells_tools_who_is_asking(tmp_path, monkeypatch):
pytest.importorskip("mcp") # the real brain needs the jarvis dependencies (--brain)