Skip to content

fix(telegram): retain superseded preview on generation mismatch race in sendMessageTransportPreview - #85808

Closed
samson1357924 wants to merge 10 commits into
openclaw:mainfrom
samson1357924:fix/telegram-draft-stream-superseded-no-retain
Closed

samson1357924 wants to merge 10 commits into
openclaw:mainfrom
samson1357924:fix/telegram-draft-stream-superseded-no-retain

Conversation

@samson1357924

Copy link
Copy Markdown
Contributor

Description

When Telegram preview streaming (streaming.mode = partial / progress) delivers multiple response segments (e.g., tool progress + final answer text + follow-up), the first streamed message can be silently deleted, leaving the chat with only the second message visible.

Symptoms

  • The bot shows "typing..." and a preview message appears (via sendMessage + editMessageText)
  • When the first message is about to finish, the entire message disappears
  • The next message immediately starts being typed
  • This happens with high probability when there is a subsequent message after the first one
  • The backend dispatch logs show the first message was sent successfully (sendMessage returned a message_id), but the user never sees it

Environment

  • OpenClaw version: 2026.5.20 (also reproduced on current main at commit 281878e910)
  • Channel: Telegram (direct DM)
  • Streaming mode: partial (default)
  • Platform: macOS

Root Cause

Race condition in extensions/telegram/src/draft-stream.ts → sendMessageTransportPreview.

Call chain:

  1. First response text streams → sendMessageTransportPreview is called → bot.api.sendMessage() is in-flight, waiting for Telegram API response
  2. Meanwhile, forceNewMessage() is called (from rotateLaneForNewMessage, rotateAnswerLaneAfterToolProgress, onAssistantMessageStart, or text-splitting paths), which increments generation
  3. Telegram API responds with a valid message_id
  4. sendGeneration !== generation evaluates to true
  5. params.onSupersededPreview?.(...) is called WITHOUT retain: true
  6. The onSupersededPreview callback (in bot-message-dispatch.ts) executes deleteMessage(), permanently removing the just-delivered message

Contrast with the other superseded path: The text-splitting path (when text exceeds maxChars) correctly passes retain: true at draft-stream.ts:220, but the generation-mismatch path at draft-stream.ts:174-179 does not.

Related Issue #39795

Issue #39795 was about duplicate messages caused by forceNewMessage() being called too aggressively in onAssistantMessageStart. That was fixed in v2026.4.22 by reducing unnecessary forceNewMessage() calls and adding archived-preview finalization.

This bug is different: Even when forceNewMessage() is correctly needed (e.g., legitimate tool-progress → answer-text transition), the race condition between an in-flight sendMessage response and generation increment causes the successfully sent message to be deleted. The #39795 fix reduced the frequency of the race but did not eliminate it.

Proposed Fix

In extensions/telegram/src/draft-stream.ts, add retain: true to the onSupersededPreview call in the generation-mismatch block:

     if (sendGeneration !== generation) {
       params.onSupersededPreview?.({
         messageId: normalizedMessageId,
         textSnapshot: renderedText,
         parseMode: renderedParseMode,
         visibleSinceMs,
+        retain: true,
       });
       return true;
     }

This prevents the callback from deleting the successfully sent message, matching the behavior already used by the text-splitting superseded path.

…ected into config

Two-part fix:

A (onboarding-plugin-install.ts): add bundled root guard in
addPluginLoadPath(). Before writing a plugin path to
plugins.load.paths, check whether it resolves to OpenClaw's
bundled plugin directory via resolvePackagedBundledLoadPathAlias.
Bundled plugins are auto-discovered and do not need a config entry.

B (repair-sequencing.ts): move maybeRepairBundledPluginLoadPaths
to the end of the repair sequence so it runs after all plugin
install/enable/auto-enable mutations are complete. Without this,
plugin repair steps could re-add the bundled path after the
mid-sequence cleanup, leaving a dirty config to be persisted.

References: openclaw#85334
…ct flood

Repeated callGateway calls each created a fresh GatewayClient then
stopped it immediately. Each new client started WS exponential backoff
from 1s, so background agents polling sessions.list (~2/sec) never
let backoff converge to 30s, creating a sustained auth flood.

Fix: pool GatewayClients by (method + token + password) key.
acquirePooledClient() reuses a live client from the pool.
releasePooledClient() keeps the client alive for 10s TTL then cleans
up, letting WS backoff converge properly.

Also adds GatewayClient.isShutdown public getter for pool liveness
check without reaching into the private closed field.

References: openclaw#85333
- Add CLIENT_POOL_MAX_SIZE=64 to prevent unbounded Map growth
- Remove TTL-based cleanup (TTL race with in-flight requests)
- Pool entries live until disconnected; stale entries evicted on acquire
- Addresses PR openclaw#85462 review feedback from Argus
Add updateMode field to DoctorHealthContribution type.
Contributions tagged with updateMode:skip are filtered out when
OPENCLAW_UPDATE_IN_PROGRESS=1 is set (update-embedded doctor run).

Tagged as skip: sandbox, skills, bootstrap-size, shell-completion,
browser, hooks-model, systemd-linger, memory-search, device-pairing,
workspace-suggestions.

Critical checks (gateway-config, gateway-auth, auth-profiles,
state-integrity, security, write-config, etc.) remain unaffected.
When sendMessageTransportPreview calls bot.api.sendMessage() and the
API response arrives after forceNewMessage() has incremented generation,
the generation mismatch check fires onSupersededPreview WITHOUT retain:
true, causing the callback to deleteMessage() the just-delivered message.

This is distinct from the issue openclaw#39795 fix (reducing unnecessary
forceNewMessage calls in onAssistantMessageStart). Even when
forceNewMessage is correctly needed, the in-flight API response can
race with generation increment.

The other superseded path (text exceeding maxChars) already passes
retain: true. This patch adds retain: true to the generation-mismatch
path so the delivered message is preserved.
@openclaw-barnacle openclaw-barnacle Bot added channel: telegram Channel integration: telegram gateway Gateway runtime commands Command implementations size: L triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. labels May 23, 2026
@clawsweeper

clawsweeper Bot commented May 23, 2026

Copy link
Copy Markdown
Contributor

ClawSweeper status: review started.

I am starting a fresh review of this pull request: fix(telegram): retain superseded preview on generation mismatch race in sendMessageTransportPreview This is item 1/1 in the current shard. Shard 0/1.

This placeholder means the worker is alive and reading the current context. I will edit this same comment with the actual review when the claws are done clicking.

Crustacean status: shell secured, claws on keyboard, evidence pebbles being sorted.

@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. mantis: telegram-visible-proof Mantis should capture Telegram visible proof. P1 High-priority user-facing bug, regression, or broken workflow. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. merge-risk: 🚨 security-boundary 🚨 May affect sandboxing, authorization, credentials, or sensitive data. merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. labels May 23, 2026
@samson1357924

Copy link
Copy Markdown
Contributor Author

Fixed upstream — both commits 06a8908 and 0a4927d are already in upstream/main. Closing as obsolete.

This branch had an error being deployed

1 failed deployment
qa-live-shared — e224e953 Deployed May 23, 2026 by clawsweeper[bot] via Run agentic native Telegram proof #53325
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

channel: telegram Channel integration: telegram commands Command implementations gateway Gateway runtime mantis: telegram-visible-proof Mantis should capture Telegram visible proof. merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. merge-risk: 🚨 security-boundary 🚨 May affect sandboxing, authorization, credentials, or sensitive data. P1 High-priority user-facing bug, regression, or broken workflow. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. size: L status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant