Skip to content

The agent that grows with you

fix(tts): resolve default output dir from the active profile

liuhao10247d509657a85a
diff --git a/tests/tools/test_tts_output_dir_profile_scope.py b/tests/tools/test_tts_output_dir_profile_scope.py
new file mode 100644
index 00000000000..264718e20d3
--- /dev/null
+++ b/tests/tools/test_tts_output_dir_profile_scope.py
@@ -0,0 +1,60 @@
+"""Regression tests for profile-scoped TTS default output dir (#98749).
+
+``DEFAULT_OUTPUT_DIR`` was resolved once at import time, so long-lived
+multi-profile runtimes (dashboard console, TUI/Desktop backend, cron, kanban
+workers) kept writing synthesized audio into the launch profile's
+``cache/audio`` even while the request was scoped to a different profile via
+``HERMES_HOME`` or ``set_hermes_home_override()``. The call-time accessor
+``_default_output_dir()`` re-resolves from the live profile-scoped home;
+these pins keep the synthesis paths from re-freezing the launch profile.
+"""
+
+import importlib
+from pathlib import Path
+
+
+def _reload_tts_tool(import_home: Path, monkeypatch):
+ monkeypatch.setenv("HERMES_HOME", str(import_home))
+ import tools.tts_tool as tts_tool
+
+ return importlib.reload(tts_tool)
+
+
+def test_default_output_dir_follows_contextvar_profile_override(tmp_path, monkeypatch):
+ """The web server scopes profiles via set_hermes_home_override() rather
+ than mutating the process env; the accessor must follow that override."""
+ default_home = tmp_path / "default-home"
+ profile_home = tmp_path / "profiles" / "ramona"
+ default_home.mkdir(parents=True)
+ profile_home.mkdir(parents=True)
+
+ tts_tool = _reload_tts_tool(default_home, monkeypatch)
+
+ from hermes_constants import (
+ reset_hermes_home_override,
+ set_hermes_home_override,
+ )
+
+ token = set_hermes_home_override(str(profile_home))
+ try:
+ assert tts_tool._default_output_dir() == str(
+ profile_home / "cache" / "audio"
+ )
+ finally:
+ reset_hermes_home_override(token)
+
+ # Outside the override scope the launch home applies again.
+ assert tts_tool._default_output_dir() == str(default_home / "cache" / "audio")
+
+
+def test_explicit_default_output_dir_monkeypatch_still_wins(tmp_path, monkeypatch):
+ """Existing tests and external patchers can still override
+ tools.tts_tool.DEFAULT_OUTPUT_DIR directly."""
+ default_home = tmp_path / "default-home"
+ default_home.mkdir(parents=True)
+
+ tts_tool = _reload_tts_tool(default_home, monkeypatch)
+
+ monkeypatch.setattr(tts_tool, "DEFAULT_OUTPUT_DIR", "/custom/audio")
+
+ assert tts_tool._default_output_dir() == "/custom/audio"
diff --git a/tools/tts_tool.py b/tools/tts_tool.py
index 6b5478a76fc..70e0a4af709 100644
--- a/tools/tts_tool.py
+++ b/tools/tts_tool.py
@@ -266,6 +266,26 @@ def _get_default_output_dir() -> str:
return str(get_hermes_dir("cache/audio", "audio_cache"))
DEFAULT_OUTPUT_DIR = _get_default_output_dir()
+_DEFAULT_OUTPUT_DIR_AT_IMPORT = DEFAULT_OUTPUT_DIR
+
+def _default_output_dir() -> str:
+ """Return the active profile's audio output dir at call time.
+
+ Same bug class as skills_tool (f8723c478) and skills_sync (#65828):
+ long-lived multi-profile runtimes (dashboard console, TUI/Desktop backend,
+ cron, kanban workers) import this module once under the launch
+ HERMES_HOME and later scope requests to a different profile via
+ ``hermes_constants.set_hermes_home_override()`` — a frozen module
+ constant keeps writing synthesized audio into the launch profile's
+ cache instead of the active profile's (#98749). Keep the legacy
+ ``DEFAULT_OUTPUT_DIR`` module attribute for tests and external patchers;
+ when it has not been patched, re-resolve from the live profile-scoped
+ HERMES_HOME on every call.
+ """
+ configured = DEFAULT_OUTPUT_DIR
+ if configured != _DEFAULT_OUTPUT_DIR_AT_IMPORT:
+ return configured
+ return _get_default_output_dir()
# ---------------------------------------------------------------------------
# Per-provider input-character limits (from official provider docs).
@@ -3498,7 +3518,7 @@ def _text_to_speech_single(
}, ensure_ascii=False)
else:
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S_%f")
- out_dir = Path(DEFAULT_OUTPUT_DIR)
+ out_dir = Path(_default_output_dir())
out_dir.mkdir(parents=True, exist_ok=True)
if command_provider_config is not None:
fmt = _get_command_tts_output_format(command_provider_config)
@@ -3856,7 +3876,7 @@ def text_to_speech_tool(
}, ensure_ascii=False)
else:
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S_%f")
- out_dir = Path(DEFAULT_OUTPUT_DIR)
+ out_dir = Path(_default_output_dir())
out_dir.mkdir(parents=True, exist_ok=True)
if command_provider_config is not None:
fmt = _get_command_tts_output_format(command_provider_config)
@@ -4739,7 +4759,7 @@ if __name__ == "__main__":
print(f" MiniMax: {minimax_status}")
print(f" Piper: {'installed' if _check_piper_available() else 'not installed (pip install piper-tts)'}")
print(f" ffmpeg: {'✅ found' if _has_ffmpeg() else '❌ not found (needed for Telegram Opus)'}")
- print(f"\n Output dir: {DEFAULT_OUTPUT_DIR}")
+ print(f"\n Output dir: {_default_output_dir()}")
provider = _get_provider(config)
print(f" Configured provider: {provider}")