Files
hermes-agent/tests/hermes_cli/test_native_profile_scope_threads.py
Teknium 7e62d683a6 fix: native-http profile scope follows audio and OAuth work onto worker threads
native_profile_scope sets a secondary-profile native ticket's home as a
task-local override, so implicit selectors (no ?profile=) resolve in the
ticket's profile. The audio and provider-OAuth routers then moved work onto
run_in_executor or a bare threading.Thread, and neither copies contextvars.
The worker thread therefore resolved the LAUNCH profile:

- /api/audio/voice-config, the elevenlabs voices list, the TTS/STT lease
  routes and speak-stream read the launch profile's STT/TTS config and keys.
- The OAuth device-code poller saved provider tokens into the launch profile.

Native-http tickets are new in this PR, so this is not a main regression.
In-process probe: inside the event loop and under to_thread the home is the
secondary; under run_in_executor and in the poller thread it is the process
home. The fix uses asyncio.to_thread, which copies the context, and starts
the poller thread inside contextvars.copy_context().run. Other dashboard
threads (WhatsApp pairing, MCP OAuth) are given explicitly resolved paths and
were left alone.

(cherry picked from commit 57480ee1f5a88692e58b7f076223406356d91dbe)
2026-10-06 16:21:15 -07:00

46 lines
1.4 KiB
Python

"""A secondary-profile native ticket's implicit scope must reach the audio worker thread."""
import asyncio
from hermes_constants import get_hermes_home, reset_hermes_home_override, set_hermes_home_override
def test_config_scoped_worker_keeps_the_request_home_override(tmp_path):
from hermes_cli.web_routers.audio import _run_config_scoped
secondary = tmp_path / "profiles" / "secondary"
secondary.mkdir(parents=True)
async def run():
# What native_profile_scope does for a ticket bound to a non-launch served profile.
token = set_hermes_home_override(str(secondary))
try:
return await _run_config_scoped(None, get_hermes_home)
finally:
reset_hermes_home_override(token)
assert asyncio.run(run()) == secondary
def test_oauth_poller_thread_keeps_the_request_home_override(tmp_path):
import threading
from hermes_cli.web_routers import oauth
secondary = tmp_path / "profiles" / "secondary"
secondary.mkdir(parents=True)
seen, done = {}, threading.Event()
sid, _sess = oauth._new_oauth_session("fixture", "device_code", profile=None)
def poller(_sid):
seen["home"] = get_hermes_home()
done.set()
token = set_hermes_home_override(str(secondary))
try:
oauth._start_poller(poller, sid)
finally:
reset_hermes_home_override(token)
assert done.wait(5)
oauth._drop_oauth_session(sid)
assert seen["home"] == secondary