Skip to content

fix(telegram): avoid duplicate dm chat window context#89855

Merged
vincentkoc merged 1 commit into
openclaw:mainfrom
sweetcornna:fix/87566-telegram-dm-chat-window
Jun 27, 2026
Merged

fix(telegram): avoid duplicate dm chat window context#89855
vincentkoc merged 1 commit into
openclaw:mainfrom
sweetcornna:fix/87566-telegram-dm-chat-window

Conversation

@sweetcornna

@sweetcornna sweetcornna commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

Summary

Telegram private DMs already route into a persistent OpenClaw session transcript, but the inbound prompt also injected the recent Telegram chat_window context on every plain DM turn. That duplicated prior messages inside each new user message and increased token usage for ongoing DM conversations.

This patch filters Telegram chat_window prompt context only for existing unthreaded private DM sessions. Fresh DMs still receive the bootstrap chat window, and replies/topics/groups keep their contextual windows because those surfaces need explicit surrounding context.

Fixes #87566.

Real behavior proof (required for external PRs)

  • Behavior or issue addressed: Existing Telegram private DM sessions no longer duplicate recent Telegram chat-window context when the persistent OpenClaw transcript already contains the prior conversation.
  • Real environment tested: Local macOS OpenClaw checkout rebased on current openclaw/openclaw main, Node 22, running the current Telegram inbound message-context construction path from this branch.
  • Exact steps or command run after this patch: NO_COLOR=1 node --import tsx --input-type=module - from the repo root. The command built one existing private DM turn and one fresh private DM turn with the same Telegram chat_window prompt-context entry, then counted the source=telegram,type=chat_window entries present in the emitted inbound context payload.
  • Evidence after fix: Copied terminal output from the local OpenClaw runtime command:
    existing DM sessionKey: agent:main:main
    existing DM chat_window context count: 0
    fresh DM sessionKey: agent:main:main
    fresh DM chat_window context count: 1
    
  • Observed result after fix: The existing persistent private DM turn emits zero Telegram chat-window prompt-context entries, while the fresh private DM turn still emits one chat-window entry.
  • What was not tested: A live Telegram Bot API roundtrip was not run from this machine.
  • Proof limitations or environment constraints: No live Telegram bot credentials are available in this checkout; the copied output above is from the local OpenClaw runtime path that constructs Telegram inbound context payloads before dispatch.

Tests and validation

  • node scripts/run-vitest.mjs extensions/telegram/src/bot-message-context.prompt-context.test.ts --reporter=dot -> 1 file / 3 tests passed.
  • node scripts/run-vitest.mjs extensions/telegram/src/bot-message-context.prompt-context.test.ts extensions/telegram/src/bot-message-context.dm-threads.test.ts --reporter=dot -> 2 files / 18 tests passed.
  • node scripts/run-vitest.mjs extensions/telegram/src/bot.test.ts --reporter=dot -t "adds live chat and reply-target windows" -> 1 passed / 74 skipped.
  • npx oxfmt --check extensions/telegram/src/bot-message-context.session.ts extensions/telegram/src/bot-message-context.test-harness.ts extensions/telegram/src/bot-message-context.prompt-context.test.ts -> clean.
  • node scripts/run-oxlint.mjs extensions/telegram/src/bot-message-context.session.ts extensions/telegram/src/bot-message-context.test-harness.ts extensions/telegram/src/bot-message-context.prompt-context.test.ts -> exit 0.
  • node scripts/run-tsgo.mjs -p test/tsconfig/tsconfig.extensions.test.json -> exit 0.
  • git diff --check -> exit 0.

Risk

The filter is deliberately narrow: it only removes source: "telegram" + type: "chat_window" entries when the route is an existing unthreaded private DM without a visible reply target or reply chain. Group chats, topics, reply-context turns, and fresh DMs keep the existing context behavior.

@openclaw-barnacle openclaw-barnacle Bot added channel: telegram Channel integration: telegram size: S triage: mock-only-proof Candidate: PR proof only shows tests, mocks, snapshots, lint, typecheck, or CI. labels Jun 3, 2026
@clawsweeper

clawsweeper Bot commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed June 21, 2026, 8:24 AM ET / 12:24 UTC.

Summary
The branch filters Telegram chat_window prompt-context entries for existing unthreaded private DM sessions and adds focused prompt-context tests plus test-harness support.

PR surface: Source +17, Tests +78. Total +95 across 3 files.

Reproducibility: yes. at source level. Current main builds recent Telegram chat_window context and forwards promptContext into supplemental.untrustedContext without persistent-DM filtering; I did not run a live Telegram Bot API roundtrip.

Review metrics: none identified.

Root-cause cluster
Relationship: fixed_by_candidate
Canonical: #87566
Summary: The canonical issue tracks Telegram persistent-DM chat-window duplication, and this PR is the current narrow fix candidate; older attempts are closed unmerged and the newer cross-ingress work is only partial overlap.

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.

Risk before merge

Maintainer options:

  1. Decide the mitigation before merge
    Land this narrow session-aware Telegram filter after maintainer and CI gates, or re-check it against fix #95378: https://github.com/openclaw/openclaw/issues/95378 #95390 if the broader direct-session recency change lands first.
  2. Pause or close
    Do not merge this PR until maintainers decide whether the risk is worth taking.

Next step before merge

  • No automated repair job is needed; this open PR is already the narrow implementation candidate and the remaining action is maintainer review plus normal merge gates.

Security
Cleared: The diff only changes Telegram prompt-context filtering and test harness inputs; it adds no dependencies, workflows, secret handling, network calls, or code-execution surface.

Review details

Best possible solution:

Land this narrow session-aware Telegram filter after maintainer and CI gates, or re-check it against #95390 if the broader direct-session recency change lands first.

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

Yes, at source level. Current main builds recent Telegram chat_window context and forwards promptContext into supplemental.untrustedContext without persistent-DM filtering; I did not run a live Telegram Bot API roundtrip.

Is this the best way to solve the issue?

Yes. Filtering at Telegram inbound context assembly is the narrowest maintainable fix because that layer has the route session key, previous-session timestamp, DM-topic state, and visible reply/group context needed to avoid over-suppression.

AGENTS.md: found and applied where relevant.

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

Label changes

Label justifications:

  • P2: This fixes a bounded Telegram session-context/token-usage bug with limited blast radius and no availability or security emergency signal.
  • 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 (terminal): The PR body supplies after-fix terminal output from the Telegram inbound context construction path showing existing DMs emit zero chat-window entries while fresh DMs still emit one.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR body supplies after-fix terminal output from the Telegram inbound context construction path showing existing DMs emit zero chat-window entries while fresh DMs still emit one.
Evidence reviewed

PR surface:

Source +17, Tests +78. Total +95 across 3 files.

View PR surface stats
Area Files Added Removed Net
Source 2 18 1 +17
Tests 1 78 0 +78
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 3 96 1 +95

What I checked:

Likely related people:

  • vincentkoc: Recent path history and local blame cover the current Telegram prompt-context builder and session-context attachment lines. (role: recent Telegram context contributor; confidence: high; commits: ba36291a8c8b, 5b46a11d2df9, 33bf9874bfd0; files: extensions/telegram/src/bot-handlers.runtime.ts, extensions/telegram/src/bot-message-context.session.ts)
  • steipete: Recent history shows work on inbound supplemental context and Telegram message-cache storage, both adjacent to the duplicated context path. (role: adjacent storage/context refactor author; confidence: medium; commits: 1507a9701b83, 090ca19c05a0, efd1a9ace6f5; files: extensions/telegram/src/bot-message-context.session.ts, extensions/telegram/src/message-cache.ts, src/channels/inbound-event/context.ts)
  • mmaps: Recent work controlled Telegram group history context on the same handler/cache prompt-context surface. (role: adjacent Telegram prompt-context contributor; confidence: medium; commits: 32af1c069727; files: extensions/telegram/src/bot-handlers.runtime.ts, extensions/telegram/src/message-cache.ts)
  • obviyus: Recent Telegram commits touched reset-boundary and command/context routing near the same handler and cache paths. (role: current Telegram context-path contributor; confidence: medium; commits: a9f014e9dfa0, fff5261ade58, a956ab848146; files: extensions/telegram/src/bot-handlers.runtime.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.

@openclaw-barnacle openclaw-barnacle Bot added proof: supplied External PR includes structured after-fix real behavior proof. and removed triage: mock-only-proof Candidate: PR proof only shows tests, mocks, snapshots, lint, typecheck, or CI. labels Jun 3, 2026
@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. P2 Normal backlog priority with limited blast radius. labels Jun 3, 2026
@vincentkoc
vincentkoc merged commit 2100ee7 into openclaw:main Jun 27, 2026
218 of 230 checks passed
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Jun 28, 2026
QiuYuang pushed a commit to QiuYuang/openclaw that referenced this pull request Jul 1, 2026
chenyangjun-xy pushed a commit to chenyangjun-xy/openclaw that referenced this pull request Jul 1, 2026
chenyangjun-xy pushed a commit to chenyangjun-xy/openclaw that referenced this pull request Jul 3, 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 P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. proof: supplied External PR includes structured after-fix real behavior proof. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: S 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 DMs duplicate recent conversation context despite persistent session transcript

2 participants