Skip to content

fix: Telegram replies duplicate recent context after sent replies#98769

Merged
jalehman merged 1 commit into
openclaw:mainfrom
rabsef-bicrym:codex/telegram-transcript-timestamp-dedupe
Jul 2, 2026
Merged

fix: Telegram replies duplicate recent context after sent replies#98769
jalehman merged 1 commit into
openclaw:mainfrom
rabsef-bicrym:codex/telegram-transcript-timestamp-dedupe

Conversation

@rabsef-bicrym

@rabsef-bicrym rabsef-bicrym commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Closes #98767

What Problem This Solves

Fixes an issue where Telegram direct-message users could receive duplicated recent prompt context after a visible assistant reply was sent back to Telegram.

Why This Change Was Made

Telegram already reads the current turn transcript when recovering the final assistant text for delivery. This change keeps that same transcript timestamp on the Telegram prompt-context cache entry when the cached outbound text matches the transcript final text, instead of deriving prompt-context order from Telegram send completion time. It does not add new per-message IDs, change delivery behavior, or broaden the prompt-context cache policy.

User Impact

Telegram DM sessions keep cleaner prompt context after sent replies. The shared session transcript and Telegram prompt-context cache no longer describe the same assistant reply with different ordering timestamps, which reduces duplicate recent-context rows and token waste.

Live Proof

Captured on the PR branch in a local Telegram DM using TomServoBotBot. Before the proof run, iMessage and WhatsApp were disabled in the local OpenClaw config and the proof gateway reported both as unavailable/unknown channels; Telegram remained running.

Proof sequence:

  1. User sent Telegram DM marker: oc proof one 2026-07-01-1508: reply with exactly alpha proof
  2. Assistant produced final text: alpha proof
  3. User sent Telegram DM marker: oc proof two 2026-07-01-1508: report whether the immediately prior assistant reply appears once or more than once in the conversation context for this turn
  4. Runtime trajectory for the second turn's submitted prompt contained exactly one prior assistant context row for the first turn's assistant reply.

Redacted trajectory extract:

{
  "sessionKey": "agent:main:direct:<redacted>",
  "currentMessageId": "29737",
  "previousAssistantMessageId": "29736",
  "previousAssistantContextRows": 1,
  "redactedContextRows": [
    "#29736 <timestamp> OpenClaw: alpha proof"
  ],
  "assistantTexts": [
    "Once."
  ]
}

The live proof was captured before the branch was narrowed and rebased onto current main; the rebased head contains the same Telegram runtime/test change and no non-Telegram cleanup.

Evidence

  • pnpm docs:list
  • pnpm test extensions/telegram/src/bot-message-dispatch.test.ts --reporter=dot
  • pnpm test extensions/telegram/src/outbound-adapter.test.ts --reporter=dot
  • pnpm test extensions/telegram/src/send.test.ts --reporter=dot
  • pnpm check:test-types
  • pnpm exec oxfmt --check extensions/telegram/src/bot-message-dispatch.ts extensions/telegram/src/bot-message-dispatch.test.ts extensions/telegram/src/outbound-adapter.ts extensions/telegram/src/outbound-adapter.test.ts extensions/telegram/src/send.ts extensions/telegram/src/send.test.ts extensions/telegram/src/outbound-message-context.ts
  • OPENCLAW_CHECK_CHANGED_REMOTE_CHILD=1 pnpm check:changed --base openclaw/main --head HEAD
  • git diff --check openclaw/main...HEAD

@openclaw-barnacle openclaw-barnacle Bot added channel: telegram Channel integration: telegram size: S labels Jul 1, 2026
@clawsweeper

clawsweeper Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed July 2, 2026, 1:03 AM ET / 05:03 UTC.

Summary
The PR carries the current-turn assistant transcript timestamp into matching Telegram outbound prompt-context cache entries and adds regression coverage across dispatch, send, adapter, and cache normalization.

PR surface: Source +102, Tests +105. Total +207 across 8 files.

Reproducibility: yes. at source level: current main dedupes session and cache prompt rows by timestamp_ms plus body while outbound cache rows use Telegram/cache timing. I did not run a live current-main Telegram repro because this review is read-only.

Review metrics: 2 noteworthy metrics.

  • Prompt-context cache metadata: 1 Telegram-local timestamp override added. The new metadata changes how outbound Telegram cache rows order and dedupe against session transcript rows.
  • Durable cache write paths: 4 send branches and 1 preview-finalized hook threaded. The diff covers text, rich fallback, rich text, media, and streamed preview-final cache recording rather than one delivery path only.

Stored data model
Persistent data-model change detected: persistent cache schema: extensions/telegram/src/send.test.ts. Confirm migration or upgrade compatibility proof before merge.

Root-cause cluster
Relationship: fixed_by_candidate
Canonical: #98767
Summary: This PR is the open candidate fix for the canonical Telegram DM timestamp-drift context duplication issue; adjacent merged PRs explain related context behavior but do not supersede this work.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Merge readiness
Overall: 🐚 platinum hermit
Proof: 🦞 diamond lobster
Patch quality: 🐚 platinum hermit
Result: ready for maintainer review.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Rank-up moves:

  • none.

Mantis proof suggestion
A live Telegram probe would add useful maintainer confidence for the visible follow-up context behavior across streamed and non-streamed finals. A maintainer can ask Mantis to capture proof by posting this exact PR comment:

@openclaw-mantis telegram live: verify a Telegram DM follow-up after streamed and streaming-off finals has one prior assistant context row.

Risk before merge

  • [P1] The PR intentionally changes model-visible Telegram prompt-context ordering and dedupe for ongoing direct-message sessions by aligning outbound cache timestamps to the transcript final timestamp.
  • [P1] Existing persisted Telegram cache rows are not retroactively restamped, so the fix applies to new outbound cache entries while old drifted cache history ages out or is replaced.

Maintainer options:

  1. Accept Transcript-Time Cache Alignment (recommended)
    Land after maintainer review if transcript timestamp alignment is the desired dedupe contract for Telegram DM outbound cache rows.
  2. Request Extra Live Transport Proof
    Ask for a Telegram live probe covering both streamed and non-streamed finals if maintainers want more runtime coverage before landing.
  3. Pause For Identity Dedupe Design
    Pause this PR if maintainers prefer waiting for a message-identity based dedupe design instead of timestamp alignment.

Next step before merge

  • [P2] The remaining action is maintainer review and landing judgment for the Telegram session-state cache timestamp contract, not an automated repair.

Security
Cleared: No concrete security or supply-chain concern found; the diff is limited to Telegram runtime/tests and does not change dependencies, workflows, credentials, permissions, downloads, or lockfiles.

Review details

Best possible solution:

Land this PR or a narrow equivalent after maintainer acceptance of transcript-time cache alignment; leave the linked issue open until the candidate fix merges.

Do we have a high-confidence way to reproduce the issue?

Yes, at source level: current main dedupes session and cache prompt rows by timestamp_ms plus body while outbound cache rows use Telegram/cache timing. I did not run a live current-main Telegram repro because this review is read-only.

Is this the best way to solve the issue?

Yes. The patch is the narrowest maintainable fix I found: carry the transcript final timestamp into the matching Telegram outbound cache row instead of adding a broader identity scheme or changing prompt-context policy.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against b2c507c5f04c.

Label changes

Label justifications:

  • P2: This is a bounded Telegram DM context-pollution regression with limited blast radius and no crash, security, or broad channel outage signal.
  • merge-risk: 🚨 session-state: The PR changes Telegram prompt-context cache timestamps, which affects model-visible context ordering and dedupe in ongoing direct-message sessions.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (logs): The PR body includes redacted live Telegram DM trajectory logs showing one prior assistant context row after the fix; this is real runtime proof, not only tests.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR body includes redacted live Telegram DM trajectory logs showing one prior assistant context row after the fix; this is real runtime proof, not only tests.
  • mantis: telegram-visible-proof: Mantis should capture Telegram visible proof. The PR changes Telegram DM follow-up prompt context in a way that can be demonstrated by a short Telegram proof recording or live transcript.
Evidence reviewed

PR surface:

Source +102, Tests +105. Total +207 across 8 files.

View PR surface stats
Area Files Added Removed Net
Source 5 115 13 +102
Tests 3 110 5 +105
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 8 225 18 +207

What I checked:

Likely related people:

  • mikasa0818: Authored the merged direct Telegram session-transcript recency work that put canonical transcript rows into the prompt-context path involved in this timestamp-drift bug. (role: introduced adjacent behavior; confidence: high; commits: d0159e522cd3, 4e8f2268e038, cfe42294fc07; files: extensions/telegram/src/bot-handlers.runtime.ts, extensions/telegram/src/message-cache.ts, extensions/telegram/src/session-transcript-context.ts)
  • obviyus: Authored the tightening commit and merged the direct-session transcript-context PR that made Telegram DM prompt context depend on canonical session transcript rows. (role: recent adjacent contributor and merger; confidence: medium; commits: ec4b75dab0bf, ec7a548062d3; files: extensions/telegram/src/session-transcript-context.ts, src/config/sessions/transcript.ts, extensions/telegram/src/bot-handlers.runtime.ts)
  • sweetcornna: Authored the merged private-DM chat-window duplicate-context fix in a neighboring Telegram DM context surface cited by the linked issue. (role: adjacent prior fix author; confidence: medium; commits: d68002acdcaa, 2100ee7cc843; files: extensions/telegram/src/bot-message-context.session.ts, extensions/telegram/src/bot-message-context.prompt-context.test.ts)
  • joshavant: Current checkout blame points the exact timestamp-sensitive dedupe and outbound-cache helper lines to commit 77f632e, though that commit appears to be a broad import/guardrails commit rather than the original feature PR. (role: current-line provenance; confidence: low; commits: 77f632e7dbd4; files: extensions/telegram/src/bot-handlers.runtime.ts, extensions/telegram/src/outbound-message-context.ts, extensions/telegram/src/bot-message-dispatch.ts)
  • jalehman: Live PR data shows this person assigned to the PR after the contributor requested review. (role: assigned reviewer / likely follow-up owner; confidence: medium; files: extensions/telegram/src/bot-message-dispatch.ts, extensions/telegram/src/outbound-message-context.ts, extensions/telegram/src/message-cache.ts)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

How this review workflow works
  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

@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. P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. labels Jul 1, 2026
@openclaw-barnacle openclaw-barnacle Bot added the commands Command implementations label Jul 1, 2026
@clawsweeper clawsweeper Bot added the mantis: telegram-visible-proof Mantis should capture Telegram visible proof. label Jul 1, 2026
@clawsweeper
clawsweeper Bot temporarily deployed to qa-live-shared July 1, 2026 22:08 Inactive
@rabsef-bicrym

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Command router queued. I will update this comment with the next step.

@clawsweeper

clawsweeper Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed 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. labels Jul 1, 2026
@rabsef-bicrym
rabsef-bicrym force-pushed the codex/telegram-transcript-timestamp-dedupe branch from 3180a0c to 7732e3d Compare July 1, 2026 22:30
@openclaw-barnacle openclaw-barnacle Bot removed the commands Command implementations label Jul 1, 2026
@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. and removed rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. labels Jul 1, 2026
@rabsef-bicrym
rabsef-bicrym force-pushed the codex/telegram-transcript-timestamp-dedupe branch from 7732e3d to 9d3439c Compare July 1, 2026 22:52
@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Security-sensitive guard cleared

This PR no longer has blocked security-sensitive file changes. A future security-sensitive change requires a fresh /allow-security-sensitive-change comment after the guard blocks that new head SHA.

  • Current SHA: 467d09b4edb4cf5927ae9d3e6b9ce3c259ce1aef

@jalehman
jalehman force-pushed the codex/telegram-transcript-timestamp-dedupe branch from 432991d to 467d09b Compare July 2, 2026 04:58
@openclaw-barnacle openclaw-barnacle Bot removed docs Improvements or additions to documentation channel: discord Channel integration: discord channel: imessage Channel integration: imessage channel: mattermost Channel integration: mattermost channel: nostr Channel integration: nostr app: android App: android app: ios App: ios gateway Gateway runtime extensions: memory-core Extension: memory-core scripts Repository scripts agents Agent runtime and tooling extensions: nvidia extensions: vercel-ai-gateway labels Jul 2, 2026
@jalehman

jalehman commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Merged via squash.

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

Labels

channel: telegram Channel integration: telegram mantis: telegram-visible-proof Mantis should capture Telegram visible proof. merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: M status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Telegram direct replies can duplicate session context when outbound cache timestamps drift

2 participants