Skip to content

fix: deregister stale Telegram webhook, store bg task results - #25420

Closed
LifeJiggy wants to merge 1 commit into
NousResearch:mainfrom
LifeJiggy:fix/various-reliability
Closed

LifeJiggy wants to merge 1 commit into
NousResearch:mainfrom
LifeJiggy:fix/various-reliability

Conversation

@LifeJiggy

Copy link
Copy Markdown
Contributor

What does this PR do?

Two independent bug fixes for gateway reliability and CLI usability:

  1. Telegram token conflict after update/restart (Telegram bot token already in use after hermes update — gateway fails to start, Telegram broken afterwards #23783). When the gateway restarts (via hermes update, crash recovery, or manual restart), Telegram's servers may still consider the old polling connection active. The new instance's connect() call gets rejected with "bot token already in use." Added an explicit delete_webhook(drop_pending_updates=True) call before every connect attempt, which tells Telegram's API to release the stale session before the new one starts. This matches how python-telegram-bot's own startup sequence handles webhook cleanup, applied here to the polling path as well.

  2. Background task results stored for retrieval ([Bug]: Background tasks have no observable state — no logs, no file list, no session context for foreground agent #8568). The /background command spawned a separate AIAgent and displayed results in the CLI, but once the output scrolled off screen there was no way to see what background tasks completed or what they produced. Added an in-memory _background_results list that stores task_id, prompt, and truncated response for each completed task. This gives both the user and the foreground agent a way to query past background results.

Related Issue
Fixes #23783, #8568

Type of Change

  • 🐛 Bug fix (non-breaking change that fixes an issue)

Changes Made

  • gateway/platforms/telegram.py (connect()) — Added Bot(token).delete_webhook(drop_pending_updates=True) call before building the PTB Application. Wrapped in try/except — failures are downgraded to debug log since the subsequent connect may still succeed if the stale lease expired naturally.
  • cli.py — Added self._background_results: list[dict[str, str]] to init. Modified the background task completion path to append {task_num, task_id, prompt, response}. Response truncated to 2000 chars to bound memory usage.

How to Test

  1. Telegram token conflict: Start gateway with Telegram. While it's running, kill the process (kill -9). Immediately restart — the new instance should connect without "bot token already in use." Check logs for "Deregistered stale webhook/polling before reconnect."
  2. Background results: In the CLI, run /background "echo hello". After completion, check the in-memory list via self._background_results (or add a /bg results command in the next iteration). Verify task_id, prompt, and response are stored.

- Deregister stale Telegram webhook/polling before connect
  (NousResearch#23783). Calls delete_webhook(drop_pending_updates=True)
  before starting a new polling session to prevent 'bot token
  already in use' errors after gateway restart or update.

- Store background task results in memory for querying (NousResearch#8568).
  Completed /background results are kept in _background_results
  list with task_id, prompt, and response for later retrieval.
@alt-glitch alt-glitch added type/bug Something isn't working comp/cli CLI entry point, hermes_cli/, setup wizard comp/gateway Gateway runner, session dispatch, delivery platform/telegram Telegram bot adapter P2 Medium — degraded but workaround exists labels May 14, 2026
@teknium1

Copy link
Copy Markdown
Collaborator

Thanks for looking at this — closing. Two unrelated issues are bundled here, and both have problems:

  1. Telegram webhook deregistration (Telegram bot token already in use after hermes update — gateway fails to start, Telegram broken afterwards #23783): there are already competing open PRs (fix(update): warn when gateway is running before hermes update #23793, fix(telegram): make polling conflict retryable with longer backoff #23806) for this issue, and current main at gateway/platforms/telegram.py:1362 already calls delete_webhook inside connect — so the premise that 'no webhook deregistration happens before connect' isn't quite right. The remaining question is whether to pass drop_pending_updates=True and reorder, which is the design call those other PRs are also debating.

  2. Background task results ([Bug]: Background tasks have no observable state — no logs, no file list, no session context for foreground agent #8568): the diff stores results in a _background_results list, but nothing else reads or surfaces that list anywhere — it's dead state. There's also an existing open PR (feat(cli): background task observability and /bg namespace #8592) for this issue going a different direction.

If you want to land either fix, please split them and rebase against current main so the existing code (Telegram side) and the consumer plumbing (bg-results side) are addressed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/cli CLI entry point, hermes_cli/, setup wizard comp/gateway Gateway runner, session dispatch, delivery P2 Medium — degraded but workaround exists platform/telegram Telegram bot adapter type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Telegram bot token already in use after hermes update — gateway fails to start, Telegram broken afterwards

3 participants