Skip to content

[codex] fix telegram polling liveness health monitor - #51931

Closed
aidenwildenbadt wants to merge 9 commits into
openclaw:mainfrom
aidenwildenbadt:codex/telegram-polling-liveness-health-monitor
Closed

aidenwildenbadt wants to merge 9 commits into
openclaw:mainfrom
aidenwildenbadt:codex/telegram-polling-liveness-health-monitor

Conversation

@aidenwildenbadt

@aidenwildenbadt aidenwildenbadt commented Mar 21, 2026 •

Copy link
Copy Markdown

This fixes a Telegram-specific failure mode where the provider could stop processing new inbound messages even though the gateway still showed the channel as running.

In the failing case, Telegram polling was interrupted by a getUpdates conflict or another poll-loop failure. The provider retried internally, but it never published channel liveness into the gateway runtime snapshot. At the same time, the gateway health monitor intentionally skipped stale-event checks for Telegram. The result was that a wedged or preempted Telegram poller could remain marked as running indefinitely, so new Telegram DMs would stop reaching agent session creation without triggering automatic recovery.

The fix does two things. First, the Telegram polling session now reports successful getUpdates activity back into the channel runtime snapshot by publishing connected and event-liveness state on successful poll cycles, and it clears the connected flag when the polling cycle exits or fails. Second, the gateway health policy now treats Telegram polling the same way as other non-webhook channels when event-liveness data is present, so a stalled Telegram poller can be restarted automatically instead of remaining falsely healthy forever.

I also threaded the Telegram gateway setStatus sink through the provider startup path so the polling monitor can update the runtime snapshot directly, and I added regression coverage for the polling-session liveness behavior and the health-policy evaluation.

Validation was done with:

  • pnpm vitest run --config vitest.config.ts extensions/telegram/src/polling-session.test.ts src/gateway/channel-health-policy.test.ts

This is related to the recent Telegram incident work but addresses a separate root cause from the earlier session-reset/followup fix.

Contribution Notes

  • AI-assisted: yes (Codex)
  • Testing degree: lightly tested
  • Review thread handling: addressed and resolved where applicable before re-requesting review

@openclaw-barnacle openclaw-barnacle Bot added channel: telegram Channel integration: telegram gateway Gateway runtime size: S labels Mar 21, 2026
@aidenwildenbadt
aidenwildenbadt force-pushed the codex/telegram-polling-liveness-health-monitor branch from 606bd32 to ca438cb Compare March 22, 2026 12:52
@aidenwildenbadt
aidenwildenbadt marked this pull request as ready for review March 22, 2026 13:09

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ca438cb2ff

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/gateway/channel-health-policy.ts Resolved
@greptile-apps

greptile-apps Bot commented Mar 22, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR closes a liveness blind spot in the Telegram polling path: the poller now publishes connected: true + lastEventAt after each successful getUpdates and connected: false when a cycle exits, and the gateway health policy no longer gives Telegram a blanket exemption from stale-event detection. Together these changes let the health monitor detect and automatically restart a wedged or preempted Telegram poller, matching how other long-polling channels already behave.

Key changes:

  • polling-session.ts: The bot.api.config.use callback is made async so it can call setStatus(createConnectedChannelStatusPatch(at)) on each successful getUpdates response, and setStatus({ connected: false }) is called in the finally block (plus redundantly in the catch block — see inline comment) when a cycle exits.
  • channel-health-policy.ts: The Telegram-specific stale-socket carve-out is removed; the existing connected === true && lastEventAt != null guard naturally becomes the mechanism for opting Telegram in to liveness detection only when synthetic event timestamps are being published.
  • channel.ts + monitor.ts: setStatus is constructed via createAccountStatusSink and threaded into the polling session; the webhook path is unaffected.
  • Tests: New regression tests cover the liveness-publish and clear-on-exit behaviour in the polling session, and the updated health-policy expectation for a stale Telegram channel.

Confidence Score: 4/5

  • Safe to merge; the fix correctly closes the liveness gap for Telegram polling with a small, well-tested change set.
  • The core logic is sound and well-motivated. The two minor issues — a redundant setStatus({ connected: false }) call in the catch block (already covered by finally) and a slightly wider connected: false window during error-path backoff compared to the normal-stop path — do not affect correctness under the 5-minute health-monitor interval, but are worth cleaning up for consistency.
  • extensions/telegram/src/polling-session.ts — the catch/finally redundancy and the connected: false timing asymmetry in the error path.
Prompt To Fix All With AI
This is a comment left during a code review.
Path: extensions/telegram/src/polling-session.ts
Line: 278-279

Comment:
**Redundant `setStatus` call in catch block**

`setStatus?.({ connected: false })` is called here in the `catch` block and then called again unconditionally in the `finally` block (line 299). Since `finally` always runs — both on the error and the normal-stop paths — the `catch`-block call is redundant and can be removed. Removing it also makes the intent cleaner: "on exit of any polling cycle, clear connected state."

```suggestion
    } catch (err) {
```

How can I resolve this? If you propose a fix, please make it concise.

---

This is a comment left during a code review.
Path: extensions/telegram/src/polling-session.ts
Line: 278-298

Comment:
**`connected: false` set before the backoff wait in the error path**

In the error branch, `setStatus?.({ connected: false })` fires at line 279, and then `#waitBeforeRestart()` is awaited inside the same `catch` block. This means the snapshot shows `connected: false` for the entire backoff duration (up to ~30 s at max backoff). 

In the normal-stop path the situation is reversed — `#waitBeforeRestart()` runs inside the `try` block and only after it returns does `finally` set `connected: false`, so the reconnection gap is only the brief time needed to create a new bot and get the first `getUpdates` back. The error path therefore exposes a wider window where the health monitor could observe `connected: false` and classify the account as `disconnected`.

With the default 5-minute check interval this is unlikely to cause spurious restarts in practice, but it is an asymmetry worth being aware of. If you want the error path to behave the same as the normal-stop path (only clearing connected state at the end of backoff, not the start), the `setStatus` call could be moved to immediately before the `return shouldRestart ? …` line instead of to the top of the catch block.

How can I resolve this? If you propose a fix, please make it concise.

Reviews (1): Last reviewed commit: "fix(telegram): surface polling liveness ..." | Re-trigger Greptile

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: de1fe120b0

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0c19e2d616

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/gateway/channel-health-policy.ts Resolved

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a73e30e104

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b746524425

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread extensions/telegram/src/monitor.ts Resolved

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 27aca9429a

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/gateway/channel-health-policy.ts Resolved
@aidenwildenbadt
aidenwildenbadt force-pushed the codex/telegram-polling-liveness-health-monitor branch from c917475 to 4bbe369 Compare March 23, 2026 23:45

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4bbe3693e5

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/gateway/channel-health-policy.ts Resolved
@aidenwildenbadt
aidenwildenbadt force-pushed the codex/telegram-polling-liveness-health-monitor branch from 3bc5f78 to 213e688 Compare March 27, 2026 00:01
@steipete

Copy link
Copy Markdown
Contributor

Maintainer triage: good direction, but I would land this after the transport fixes and with one behavior tweak.

The core idea is right: Telegram polling needs to publish mode: "polling" and liveness timestamps so the generic gateway health policy can detect a wedged poller instead of relying only on local polling-session watchdog logs. This also supersedes the older #37850 approach.

Concern in the latest patch: runUntilAbort() now emits connected: false plus lastEventAt: null at the start of every cycle, including normal restart/backoff paths after a successful poll. That means a recoverable polling cycle can make the account look disconnected during ordinary retry windows, then healthy again on the next successful getUpdates. The tests encode that behavior, but it may create noisy health transitions.

Suggested fix: keep mode: "polling" at cycle start, but only clear connected/lastEventAt on real session exit or after a restart decision that should be externally visible. For retry/backoff between polling cycles, preserve the last known connected state until the next successful poll or a health-policy threshold decides it is stale.

Recommended path: rebase after #68128/#69243, make that status transition less eager, then merge.

@steipete

Copy link
Copy Markdown
Contributor

Thanks for the polling-health work. I landed a refactored version via #69476 / 60fea81: Telegram now publishes explicit mode: polling liveness from successful getUpdates, the gateway stale-socket check only applies to explicit polling mode, and the restart-cycle path avoids marking the account disconnected between recoverable retries. Closing this PR as superseded by the landed integration.

@steipete steipete closed this Apr 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

channel: telegram Channel integration: telegram gateway Gateway runtime size: L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants