Skip to content

fix(cron): centralize Telegram private topic delivery#42051

Open
stepanov1975 wants to merge 1 commit into
NousResearch:mainfrom
stepanov1975:fix/telegram-private-topic-root-cause-pr
Open

fix(cron): centralize Telegram private topic delivery#42051
stepanov1975 wants to merge 1 commit into
NousResearch:mainfrom
stepanov1975:fix/telegram-private-topic-root-cause-pr

Conversation

@stepanov1975

@stepanov1975 stepanov1975 commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes Telegram private DM-topic cron delivery by moving the Telegram-specific private-topic routing semantics into the shared gateway delivery layer instead of keeping them in cron or the standalone send_message path.

Cron still preserves the user's delivery target shape, but it now delegates named Telegram private-topic metadata preparation and stale-topic recovery to shared helpers in gateway/delivery.py. This avoids silently falling back to the root DM when a private-topic send is stale or unsafe.

The important behavior changes are:

  • named Telegram private DM-topic targets are detected centrally
  • named-topic metadata is prepared centrally before text and media delivery
  • stale named topics are refreshed with ensure_dm_topic(..., force_create=True) instead of falling back to standalone/root-chat delivery
  • named private-topic cron sends fail closed when no live Telegram adapter is available
  • standalone positive private-chat numeric thread sends fail closed without router metadata, while negative group/forum thread IDs remain allowed

This is related to the narrower open cron fix PR #23249, but takes a shared delivery-router approach instead of adding only a send_message_tool.py retry branch.

Related Issue

Fixes #22773.

Related to #23249, #33375, #35739, and #31501 as adjacent Telegram DM-topic routing failures.

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/delivery.py
    • add shared helpers for Telegram private-topic target detection and metadata preparation
    • add named private-topic stale refresh through the live Telegram adapter
    • normalize named-topic refresh around ensure_dm_topic(..., force_create=True)
  • cron/scheduler.py
    • preserve named Telegram target shape while delegating private-topic routing semantics to the delivery layer
    • fail closed for named Telegram private-topic deliveries when a live adapter is required but unavailable
    • use the same refreshed metadata path for text and media delivery
  • tools/send_message_tool.py
    • fail closed for standalone positive Telegram private-chat chat_id:thread_id sends without router metadata
    • keep negative group/forum thread IDs working normally
  • tests/cron/test_scheduler.py
    • add regression coverage for named private-topic refresh, no-live-adapter fail-closed behavior, and media delivery
  • tests/tools/test_send_message_tool.py
    • add regression coverage for unsafe private-topic standalone sends and allowed group/forum numeric threads

How to Test

Reproduction before the fix:

  1. Configure a cron delivery target for a named Telegram private DM topic.
  2. Delete or stale the underlying Telegram topic/thread so the persisted thread ID is no longer valid.
  3. Run the cron job.
  4. Observe that delivery can fall back to standalone/root DM behavior instead of refreshing the named topic or failing safely.

Proof after the fix:

  1. Named private-topic cron delivery refreshes stale metadata through the live Telegram adapter.
  2. If no live adapter is available for a named private-topic delivery, cron returns a delivery failure instead of falling back to root chat.
  3. Media-only delivery follows the same refreshed named-topic metadata path as text delivery.
  4. Standalone send_message rejects ambiguous positive private-chat thread sends before trying Telegram, while supergroup/forum thread targets still send.

Automated verification run on this branch:

scripts/run_tests.sh tests/gateway/test_delivery.py tests/cron/test_scheduler.py tests/tools/test_send_message_tool.py tests/tools/test_send_message_telegram_proxy.py
# 302 passed, 0 failed

python -m py_compile cron/scheduler.py gateway/delivery.py tools/send_message_tool.py tests/cron/test_scheduler.py tests/gateway/test_delivery.py tests/tools/test_send_message_tool.py tests/tools/test_send_message_telegram_proxy.py
# passed

ruff check cron/scheduler.py gateway/delivery.py tools/send_message_tool.py tests/cron/test_scheduler.py tests/gateway/test_delivery.py tests/tools/test_send_message_tool.py tests/tools/test_send_message_telegram_proxy.py
# All checks passed

git diff --cached --check
# passed

python scripts/check-windows-footguns.py cron/scheduler.py gateway/delivery.py tools/send_message_tool.py tests/cron/test_scheduler.py tests/tools/test_send_message_tool.py
# No Windows footguns found (5 files scanned)

Full pytest tests/ -q was not run for this PR; the verification above covers the touched gateway, cron, and send-message paths.

Tested platform: Linux 6.8.0-117-generic, Python 3.11.15.

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 — focused suite run above instead
  • I've added tests for my changes (required for bug fixes, strongly encouraged for features)
  • I've tested on my platform: Linux 6.8.0-117-generic, Python 3.11.15

Documentation & Housekeeping

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

Screenshots / Logs

Focused test output:

=== Summary: 4 files, 302 tests passed, 0 failed (100% complete) in 7.5s (8 workers) ===

@stepanov1975 stepanov1975 force-pushed the fix/telegram-private-topic-root-cause-pr branch from 442ea56 to 8c92915 Compare June 8, 2026 12:05
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/cron Cron scheduler and job management comp/gateway Gateway runner, session dispatch, delivery platform/telegram Telegram bot adapter labels Jun 8, 2026
@stepanov1975 stepanov1975 force-pushed the fix/telegram-private-topic-root-cause-pr branch from 8c92915 to cda0dc1 Compare June 8, 2026 12:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/cron Cron scheduler and job management 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.

Cron delivery to private DM topics broken after PR #22410 — send_message_tool.py missing three-mode routing

2 participants