Skip to content

fix(gateway): bind session context for auto tts - #34779

Closed
Lbatson wants to merge 2 commits into
NousResearch:mainfrom
Lbatson:fix/telegram-auto-tts-context
Closed

fix(gateway): bind session context for auto tts#34779
Lbatson wants to merge 2 commits into
NousResearch:mainfrom
Lbatson:fix/telegram-auto-tts-context

Conversation

@Lbatson

@Lbatson Lbatson commented May 29, 2026

Copy link
Copy Markdown

What does this PR do?

Fixes Telegram gateway auto-TTS delivery so voice replies are sent with the correct Telegram session context and arrive as native voice bubbles instead of MP3/audio attachments.

The gateway auto-TTS path calls text_to_speech_tool() from the platform send pipeline, outside the normal agent tool-call context. The TTS tool chooses Telegram Opus/OGG output based on gateway session context, but that context was not reliably bound for this out-of-band path. This PR temporarily rebinds the current gateway event context while calling TTS, then restores the previous context afterward.

This keeps the fix scoped to the auto-TTS call, preserves any prior context, and covers more than just the platform string: platform, chat ID, session key, and message ID are all available to the TTS tool during the call.

Related Issue

No issue filed.

Related existing PRs found during duplicate check:

This PR is intentionally minimal: one gateway context-binding fix plus a regression test.

Type of Change

  • 🐛 Bug fix (non-breaking change that fixes an issue)
  • ✨ New feature (non-breaking change that adds functionality)
  • 🔒 Security fix
  • 📝 Documentation update
  • ✅ Tests (adding or improving test coverage)
  • ♻️ Refactor (no behavior change)
  • 🎯 New skill (bundled or hub)

Changes Made

  • gateway/platforms/base.py
    • Rebinds gateway session context around the auto-TTS text_to_speech_tool() call.
    • Restores the previous context in finally so the binding cannot leak past the TTS send scope.
  • tests/gateway/test_tts_media_routing.py
    • Adds regression coverage proving auto-TTS receives:
      • platform: telegram
      • event chat_id
      • gateway session_key
      • event message_id
    • Verifies the Telegram auto-TTS delivery path sends voice rather than an MP3/audio attachment.

How to Test

  1. Enable Telegram gateway voice auto-TTS.
  2. Send a Telegram voice message to the bot.
  3. Confirm the reply arrives as a native Telegram voice bubble, not an MP3/audio attachment.
  4. Run the regression tests listed below.

Test evidence from this branch:

$ python -m pytest tests/gateway/test_tts_media_routing.py tests/tools/test_voice_mode.py tests/tools/test_tts_plugin_dispatch.py tests/tools/test_tts_path_traversal.py -q
114 passed in 7.08s

Full-suite preflight:

$ scripts/run_tests.sh
26911 passed, 3 failed

The full-suite failures were outside this change area:

  • tests/hermes_cli/test_cmd_update.py::TestCmdUpdateBranchFallback::test_update_refreshes_repo_and_tui_node_dependencies
  • tests/tools/test_browser_hardening.py::TestFindAgentBrowserCache::test_not_found_cached_raises_on_subsequent
  • tests/tools/test_browser_homebrew_paths.py::TestFindAgentBrowser::test_raises_when_not_found

The two browser failures timed out while attempting to install/download Chromium via agent-browser; the update-command failure asserted unrelated web dashboard build helper behavior. None touch gateway/platforms/base.py or TTS media routing.

Live verification:

  • Tested on Telegram after restarting hermes-gateway.service.
  • Telegram reply arrived as a voice message, not an MP3/audio attachment.
  • Fresh live-path cache evidence showed .ogg output present (audio_9bb55ddcaceb.ogg), with an MP3 intermediate only.

Checklist

Code

  • I've read the Contributing Guide
  • My commit messages follow Conventional Commits (fix(scope):, feat(scope):, etc.)
  • I searched for existing PRs to make sure this isn't a duplicate
  • My PR contains only changes related to this fix/feature (no unrelated commits)
  • I've run pytest tests/ -q and all tests pass
  • I've added tests for my changes (required for bug fixes, strongly encouraged for features)
  • I've tested on my platform: Ubuntu Linux 6.17.0-29-generic

Documentation & Housekeeping

  • I've updated relevant documentation (README, docs/, docstrings) — or N/A
  • I've updated cli-config.yaml.example if I added/changed config keys — or N/A
  • I've updated CONTRIBUTING.md or AGENTS.md if I changed architecture or workflows — or N/A
  • I've considered cross-platform impact (Windows, macOS) per the compatibility guide — or N/A
  • I've updated tool descriptions/schemas if I changed tool behavior — or N/A

Screenshots / Logs

Live Telegram verification passed: the reply is delivered as a native voice message rather than an MP3/audio attachment.

@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/gateway Gateway runner, session dispatch, delivery tool/tts Text-to-speech and transcription platform/telegram Telegram bot adapter labels May 29, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Competing with #31937 — both fix auto-TTS on Telegram losing session context (resulting in MP3 instead of OGG voice bubbles). #28793 addresses the deeper root cause (_clear_session_env() clears HERMES_SESSION_PLATFORM before auto-TTS). See also #32539.

@Lbatson

Lbatson commented Jun 6, 2026

Copy link
Copy Markdown
Author

Closing this PR per maintainer request; the issue is already covered by other PRs/discussion.

@Lbatson Lbatson closed this Jun 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/gateway Gateway runner, session dispatch, delivery P2 Medium — degraded but workaround exists platform/telegram Telegram bot adapter tool/tts Text-to-speech and transcription type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants