Skip to content

[Bug] Telegram bot stuck in infinite loop sending duplicate messages - session state persists across restarts #5806

@jatinkrmalik

Description

@jatinkrmalik

Bug Description

The bot used via Telegram channel gets stuck in an infinite loop, repeatedly sending the same message. The issue is caused by a corrupted/stuck session state that persists across service restarts. Simply clearing the update-offset-default.json file is not sufficient - the session file must also be deleted.

This appears to happen when an agent task fails (e.g., Python script errors during audio generation or even a simple cron related tasks [screenshots below]) and enters a retry loop. The session state grows large (200KB+) and gets reloaded on every restart, causing the bot to continue sending the same message repeatedly.

Environment

  • OpenClaw Version: 2026.1.30
  • Installation Method: npm global install
  • OS: Linux (Ubuntu/Debian)
  • Node Version: v22.22.0
  • Telegram: Using polling mode (not webhook)

Steps to Reproduce

  1. Ask the agent to perform a task that may fail or timeout (e.g., generate audio/theme song using Python)
  2. Let the agent encounter errors and enter a retry loop
  3. Wait for the session to accumulate errors
  4. Restart the service: systemctl --user restart openclaw
  5. Bot loads the stuck session and continues the loop

Expected Behavior

  • Service restart should clear any stuck/failed session states
  • Or: Failed tasks should be cleaned up automatically after N retries
  • Or: Option to "forget" session state on startup

Actual Behavior

  • Session files (~/.openclaw/agents/jatin/sessions/<uuid>.jsonl) persist across restarts
  • The bot re-loads the stuck session and continues sending duplicate messages
  • Manual deletion of both the session file AND update-offset-default.json is required to recover

Workarounds Tried

  1. ✗ Service restart alone: systemctl --user restart openclaw - Does not help
  2. ✗ Clearing telegram offset only: rm ~/.openclaw/telegram/update-offset-default.json - Does not help
  3. ✓ Clearing both session file AND offset: Works

Recovery script that works:

systemctl --user stop openclaw
rm -f ~/.openclaw/telegram/update-offset-default.json
rm -f ~/.openclaw/agents/jatin/sessions/<stuck-session-id>.jsonl
systemctl --user start openclaw

Logs

# From journalctl showing the stuck session being reloaded:
[agent/embedded] Removed orphaned user message to prevent consecutive user turns. 
runId=a5a47ea7-11d3-4f82-9375-39b734ac3fba 
sessionId=8f57b2a1-005e-4edf-bfc6-d20c6469dfa2

Session file sizes observed during stuck state:

  • 8f57b2a1-005e-4edf-bfc6-d20c6469dfa2.jsonl: 438KB
  • fed57ba4-9950-4118-b499-6757f7dbd400.jsonl: 220KB

Screenshots

Image Image

Suggestions

  1. Auto-cleanup: Automatically delete or archive session files that exceed a certain size threshold
  2. Startup flag: Add a --fresh-start flag to ignore existing session states
  3. Session timeout: Automatically expire/forget sessions after X minutes of inactivity
  4. Max retry limit: Enforce a hard limit on retries for failed tasks

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingstaleMarked as stale due to inactivity

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions