Skip to content

fix(telegram): stop resending earlier messages to the model on every turn in DM topics - #207684

Merged
obviyus merged 1 commit into
openclaw:mainfrom
stealthybear:dm-topic-context-twice
Nov 29, 2026
Merged

obviyus merged 1 commit into
openclaw:mainfrom
stealthybear:dm-topic-context-twice

Conversation

@stealthybear

@stealthybear stealthybear commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Root cause: extensions/telegram/src/bot-message-context.session.ts:406 drops Telegram's native chat window for an existing private session only when dmThreadId == null, so a DM topic with its own session gets its earlier turns again in the "Conversation context" block on every turn, on top of session replay.

Fixes #207657

Thanks @stealthybear for reporting this and for the fix.

What Problem This Solves

In a Telegram private chat where the bot has topics turned on, each topic has its own session. Every turn in such a topic sends the model the topic's earlier messages again in the "Conversation context" block, once as Telegram #<message id> rows and once as #session: transcript rows, although the session already replays those turns. The copies stay in history, so they pile up turn after turn.

User Impact

DM topics that route to their own session get the same per-turn context as plain DMs: earlier turns arrive once, through session replay. Fresh topics and explicit replies keep their context. A DM thread that shares the DM session (a bot without topics) or is bound to another session keeps the Telegram window it needs to tell its own messages apart. No config or migration.

Why This Change Was Made

#89855 drops Telegram's native window for plain DMs whose session replays the earlier turns, and left DM threads out. Neither #89855 nor #183190 gives a reason; the likely one (our inference) is that a DM thread can share its session with the rest of the DM, and then the window is what tells the thread's messages apart. That reason holds only when the thread has no session of its own. So the rule now keys on that property: hasTelegramDmThreadSession in conversation-route.ts decides whether a DM thread routes to its own session (no binding selects one, and the bot uses DM topic sessions). Session routing and the context builder both use it, so they can't disagree. Routing itself doesn't change: bindings already returned before that check.

Evidence

Live, before and after: real Telegram (Telegram Test Server, upstream's telegram-e2e-userbot runner at a1ced09cd7f with --dm --create-dm-topic, a real test user and bot, and the real Claude CLI model): fresh gateways from each packed build. The user sends four messages ("Message i: remember the code word marker-i-live. Reply with only: noted-i") into a new DM topic. Both runs route to the topic's own session (agent:main:main:thread:<user>:<topic>), get all four replies in the topic, and end with testDmTopic.cleanup.status: restored. Measured: the "Conversation context" window the model receives each turn, from the Claude CLI transcript.

Turn Released 2026.12.1-beta.1 This PR
2 204 B, 2 Telegram rows 0 B, no rows
3 368 B, 4 Telegram rows 0 B, no rows
4 532 B, 6 Telegram rows: messages 2 and 3 and their replies, already in session replay 0 B, no rows
Fourth turn, released build: the window the model got (timestamps cut)
Conversation context (chronological, selected for current message): ⟦openclaw:ctx⟧
#3 OpenClaw QA bot (you): Working
#4 OpenClaw QA bot (you): noted-1
#5 A: Message 2: remember the code word marker-2-live. Reply with only: noted-2
#6 OpenClaw QA bot (you): noted-2
#7 A: Message 3: remember the code word marker-3-live. Reply with only: noted-3
#8 OpenClaw QA bot (you): noted-3

With this PR the fourth turn has no window; the earlier turns reach the model once, through session replay.

Controls, before and after: stub Telegram Bot API and stub model, which also cover the plain DM and a shared reply thread. One script sends four messages ("Message i: remember the code word …") and measures the fourth request.

Fourth turn Released 2026.12.1-beta.1 This PR
DM topic with its own session context block 530 B: 2 Telegram + 3 transcript rows; message 2 sent 3 times, message 3 twice 0 B, no rows; each message once
Plain DM (control) 0 B, each message once unchanged
Reply thread sharing the DM session, bot without topics (control) 1,046 B: 3 Telegram + 7 transcript rows unchanged, on purpose (below)

Fourth-turn context before and after

Tested on 3a8bd09. Request proof: on every chat turn of the three fix runs, the user text sent to the model byte-equals the transcript (ocfix proof sent; the topic-label side request, which carries no chat turn, was left out).

  • Review: autoreview (Claude engine), 68e792edca4..3a8bd0974c5, P0-P2: clean, no findings.
  • Tests: three new cases in the existing table of bot-message-context.prompt-context.test.ts (99 s wall, --maxWorkers=1): an existing DM topic with its own session (fails on main, passes with the fix), a fresh DM topic, and a reply thread that shares the DM session. The DM-session, bot-threads, session-route and bot.test.ts suites pass. Typecheck and lint clean.

Maintainer re-run on 3a8bd0974c5: Telegram Test Server, repo telegram-e2e-userbot runner, --dm --create-dm-topic, mock provider, fresh Gateways built from main (68e792edca4, the merge base) and from this head, each side twice. Four messages into a new DM topic; all four replies arrive in the topic on every run, and every run ends with testDmTopic.cleanup.status: restored and Threaded Mode off.

Turn main, run 1 main, run 2 this PR, run 1 this PR, run 2
2 84 B, 1 Telegram row 84 B, 1 row 0 B 0 B
3 302 B, 3 rows 302 B, 3 rows 0 B 0 B
4 520 B, 5 rows 520 B, 5 rows 0 B 0 B

Stub Telegram Bot API controls, both builds: a fresh topic session's first turn keeps its window; an explicit reply to message 1 keeps the window and reply target; a plain DM stays at 0 B; a reply thread that shares the DM session (bot without topics) keeps its growing window. Proof lines: #207684 (comment)

Not covered

@clawsweeper

clawsweeper Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision.

View the workflow run.

@openclaw-barnacle openclaw-barnacle Bot added channel: telegram Channel integration: telegram size: S labels Nov 28, 2026
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. impact:session-state Session, memory, transcript, context, or agent state can drift or corrupt. clawsweeper:intake:bug Preliminary submission classification; full source review pending. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. labels Nov 28, 2026
@clawsweeper

clawsweeper Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: blocked before merge.

What this changes

Suppress supplemental history for existing Telegram DM topics whose earlier turns already arrive through their own session replay.

Example: Send four messages in one private Telegram bot topic using a Claude CLI agent.

  • Before: The reported fourth live turn includes a 532-byte supplemental window containing six earlier Telegram rows.
  • After: The supplemental window is empty, earlier turns remain in session replay, and all four replies arrive in the topic.

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A worthwhile, focused fix with supplied live behavior evidence and no identified defects; hosted validation remains approval-gated.
Proof confidence 🐚 platinum hermit (4/6) Sufficient (live_output): The updated body reports the reviewed packed Gateway running four real Telegram Test Server topic roundtrips with a real Claude CLI model, with zero supplemental-window bytes on turns 2–4 and all replies delivered; this resolves the prior live-proof gap, while the inspected screenshot supplies supporting stub controls.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: Earlier messages accumulate repeatedly in model context during private Telegram topic conversations, increasing token use and encouraging stale responses.
Reason: Independent topic sessions should follow the established replay-owns-history contract, while shared and bound threads retain their existing context.

Merge readiness

⛔ Blocked before merge - 1 item remains

This PR fixes a current-main Telegram context-duplication bug with no identified correctness defects; required CI awaits workflow approval.

Reviewed head: 3a8bd0974c50ef19bda70f84d68958c28d6bd57c

Before merge

  • Await maintainer action - Approve the validation workflows awaiting approval for head 3a8bd09 so required CI can run.

Findings

None.

Agent review details

How this fits together

Telegram inbound routing selects a session, then context assembly combines the current message, native Telegram history, and supplemental transcript context for the agent.

flowchart TD
  A[Telegram topic message] --> B[Conversation routing]
  B --> C[Target session]
  C --> D[Telegram context selection]
  E[Native message history] --> D
  D --> F[Shared transcript preparation]
  F --> G[Agent session replay and model request]
Loading

Technical review

Best possible solution:

Land the focused Telegram context-selection fix once required CI passes.

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

Current-main source shows that an independent DM-topic session replays its turns while retaining both supplemental history paths; the contributor also supplies a live before/after reproduction, which this review did not rerun.

Is this the best way to solve the issue?

The patch fixes selection at its existing Telegram owner, shares routing facts, and preserves contextual exceptions without adding configuration, persistence, or a public API.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 3586700dbad1.

Provenance checked

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

Likely related people:

  • cornna: Raw commit 2100ee7 adds extensions/telegram/src/bot-message-context.session.ts:394 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 2100ee7cc843; files: extensions/telegram/src/bot-message-context.session.ts)
  • salad-maker: Raw commit 0acdb20 adds extensions/telegram/src/bot-message-context.prompt-context.test.ts:179 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 0acdb207a4c0; files: extensions/telegram/src/bot-message-context.prompt-context.test.ts)
  • Vito Cappello: Raw commit 9ebaece adds extensions/telegram/src/bot-message-context.session.ts:401 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 9ebaece10793; files: extensions/telegram/src/bot-message-context.session.ts)

PR surface

Source +25, Tests +28. Total +53 across 5 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 4 33 8 +25
Tests 1 45 17 +28
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 5 78 25 +53

Preflight source

Source unknown. Source comparison at the observed PR snapshot only. Completion is self-reported; body, policy, tests, execution, and report bytes are not verified.

Root-cause cluster

Relationship: fixed_by_candidate
Canonical: #207657
Summary: This PR addresses the canonical private-topic duplication report; earlier merged fixes cover neighboring plain-DM and group-topic cases.

Members:

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

Labels

Label changes:

  • add proof: sufficient: Contributor real behavior proof is sufficient.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.
  • add status: ⏳ waiting on maintainer: Only explicit maintainer approvals or decisions remain; merge is still blocked.
  • remove rating: 🦪 silver shellfish: Current PR rating is rating: 🐚 platinum hermit, so this older rating label is no longer current.
  • remove status: 📣 needs proof: Current PR status label is status: ⏳ waiting on maintainer.

Label justifications:

  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.
  • status: ⏳ waiting on maintainer: Only explicit maintainer approvals or decisions remain; merge is still blocked.
  • proof: sufficient: Contributor real behavior proof is sufficient.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

History

Review history (1 earlier review cycle)
  • reviewed 2026-11-28T19:21:56.985Z sha 3a8bd09 :: needs real behavior proof before merge. :: none

Reviewed November 28, 2026, 6:29 PM ET / 23:29 UTC (Revision 2).

@stealthybear

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

Added real-Telegram proof to the description: Telegram Test Server, upstream telegram-e2e-userbot runner with --dm --create-dm-topic, a real test user and bot, and the real Claude CLI model, on the released build and on this PR.

@clawsweeper

clawsweeper Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀

Re-review progress:

@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: ⏳ waiting on maintainer Only explicit maintainer approvals or decisions remain; merge is still blocked. and removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Nov 28, 2026
@obviyus

obviyus commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator

Maintainer proof for #207684 on head 3a8bd0974c5 (main side: merge base 68e792edca4). Window = bytes of the "Conversation context" block in each turn's provider request.

## Live: Telegram Test Server, repo telegram-e2e-userbot run-mock-sut-user-e2e.mjs --dm --create-dm-topic --scenario live/scenario.json (4 sends, awaitReply each), mock provider, Convex lease owned by runner
## window = bytes of 'Conversation context' block in each turn's provider request (harness/analyze.mjs)
### live/main-r1
seq=1 turn=1 windowBytes=0 telegramRows=0 sessionRows=0 markersInWindow=[]
seq=4 turn=2 windowBytes=84 telegramRows=1 sessionRows=0 markersInWindow=[]
seq=8 turn=3 windowBytes=302 telegramRows=3 sessionRows=0 markersInWindow=[2]
seq=10 turn=4 windowBytes=520 telegramRows=5 sessionRows=0 markersInWindow=[2,3]
threadedMode.after=true topicCreated=true repliesInTopic=4/4 cleanup=restored threadedModeAfterCleanup=false recordingComplete=true
### live/main-r2
seq=1 turn=1 windowBytes=0 telegramRows=0 sessionRows=0 markersInWindow=[]
seq=4 turn=2 windowBytes=84 telegramRows=1 sessionRows=0 markersInWindow=[]
seq=8 turn=3 windowBytes=302 telegramRows=3 sessionRows=0 markersInWindow=[2]
seq=10 turn=4 windowBytes=520 telegramRows=5 sessionRows=0 markersInWindow=[2,3]
threadedMode.after=true topicCreated=true repliesInTopic=4/4 cleanup=restored threadedModeAfterCleanup=false recordingComplete=true
### live/cand-r1
seq=1 turn=1 windowBytes=0 telegramRows=0 sessionRows=0 markersInWindow=[]
seq=4 turn=2 windowBytes=0 telegramRows=0 sessionRows=0 markersInWindow=[]
seq=6 turn=3 windowBytes=0 telegramRows=0 sessionRows=0 markersInWindow=[]
seq=8 turn=4 windowBytes=0 telegramRows=0 sessionRows=0 markersInWindow=[]
threadedMode.after=true topicCreated=true repliesInTopic=4/4 cleanup=restored threadedModeAfterCleanup=false recordingComplete=true
### live/cand-r2
seq=1 turn=1 windowBytes=0 telegramRows=0 sessionRows=0 markersInWindow=[]
seq=4 turn=2 windowBytes=0 telegramRows=0 sessionRows=0 markersInWindow=[]
seq=8 turn=3 windowBytes=0 telegramRows=0 sessionRows=0 markersInWindow=[]
seq=10 turn=4 windowBytes=0 telegramRows=0 sessionRows=0 markersInWindow=[]
threadedMode.after=true topicCreated=true repliesInTopic=4/4 cleanup=restored threadedModeAfterCleanup=false recordingComplete=true

## Controls: stub Telegram Bot API (harness/telegram-standin.mjs) + scripts/e2e/mock-openai-server.mjs + built Gateway, harness/ctl.sh
### runs/base-topic-t1 sessions=agent:main:main:thread:4207684:77
seq=1 turn=1 windowBytes=0 telegramRows=0 sessionRows=0 markersInWindow=[]
seq=4 turn=2 windowBytes=175 telegramRows=2 sessionRows=0 markersInWindow=[1]
seq=6 turn=3 windowBytes=351 telegramRows=4 sessionRows=0 markersInWindow=[1,2]
seq=8 turn=4 windowBytes=527 telegramRows=6 sessionRows=0 markersInWindow=[1,2,3]
### runs/cand-topic-t1 sessions=agent:main:main:thread:4207684:77
seq=1 turn=1 windowBytes=0 telegramRows=0 sessionRows=0 markersInWindow=[]
seq=4 turn=2 windowBytes=0 telegramRows=0 sessionRows=0 markersInWindow=[]
seq=6 turn=3 windowBytes=0 telegramRows=0 sessionRows=0 markersInWindow=[]
seq=8 turn=4 windowBytes=0 telegramRows=0 sessionRows=0 markersInWindow=[]
### runs/base-fresh-c2 sessions=agent:main:main:thread:4207684:80,agent:main:main
seq=1 turn=1 windowBytes=0 telegramRows=0 sessionRows=0 markersInWindow=[]
seq=4 turn=2 windowBytes=58 telegramRows=1 sessionRows=0 markersInWindow=[]
seq=6 turn=3 windowBytes=351 telegramRows=4 sessionRows=0 markersInWindow=[1,2]
### runs/cand-fresh-c2 sessions=agent:main:main:thread:4207684:80,agent:main:main
seq=1 turn=1 windowBytes=0 telegramRows=0 sessionRows=0 markersInWindow=[]
seq=4 turn=2 windowBytes=175 telegramRows=2 sessionRows=0 markersInWindow=[1]
seq=6 turn=3 windowBytes=351 telegramRows=4 sessionRows=0 markersInWindow=[1,2]
### runs/base-reply-c1 sessions=agent:main:main:thread:4207684:78
seq=1 turn=1 windowBytes=0 telegramRows=0 sessionRows=0 markersInWindow=[]
seq=4 turn=2 windowBytes=175 telegramRows=2 sessionRows=0 markersInWindow=[1]
seq=6 turn=3 windowBytes=351 telegramRows=4 sessionRows=0 markersInWindow=[1,2]
seq=8 turn=4 windowBytes=755 telegramRows=6 sessionRows=0 markersInWindow=[1,2,3,4] replyTargetContext=yes
### runs/cand-reply-c1 sessions=agent:main:main:thread:4207684:78
seq=1 turn=1 windowBytes=0 telegramRows=0 sessionRows=0 markersInWindow=[]
seq=4 turn=2 windowBytes=0 telegramRows=0 sessionRows=0 markersInWindow=[]
seq=6 turn=3 windowBytes=0 telegramRows=0 sessionRows=0 markersInWindow=[]
seq=8 turn=4 windowBytes=623 telegramRows=5 sessionRows=0 markersInWindow=[2,3,1,4] replyTargetContext=yes
### runs/base-dm-c1 sessions=agent:main:main
seq=1 turn=1 windowBytes=0 telegramRows=0 sessionRows=0 markersInWindow=[]
seq=3 turn=2 windowBytes=0 telegramRows=0 sessionRows=0 markersInWindow=[]
seq=5 turn=3 windowBytes=0 telegramRows=0 sessionRows=0 markersInWindow=[]
seq=7 turn=4 windowBytes=0 telegramRows=0 sessionRows=0 markersInWindow=[]
### runs/cand-dm-c1 sessions=agent:main:main
seq=1 turn=1 windowBytes=0 telegramRows=0 sessionRows=0 markersInWindow=[]
seq=3 turn=2 windowBytes=0 telegramRows=0 sessionRows=0 markersInWindow=[]
seq=5 turn=3 windowBytes=0 telegramRows=0 sessionRows=0 markersInWindow=[]
seq=7 turn=4 windowBytes=0 telegramRows=0 sessionRows=0 markersInWindow=[]
### runs/base-shared-c1 sessions=agent:main:main
seq=1 turn=1 windowBytes=0 telegramRows=0 sessionRows=0 markersInWindow=[]
seq=4 turn=2 windowBytes=58 telegramRows=1 sessionRows=0 markersInWindow=[]
seq=6 turn=3 windowBytes=235 telegramRows=3 sessionRows=0 markersInWindow=[2]
seq=8 turn=4 windowBytes=412 telegramRows=5 sessionRows=0 markersInWindow=[2,3]
### runs/cand-shared-c1 sessions=agent:main:main
seq=1 turn=1 windowBytes=0 telegramRows=0 sessionRows=0 markersInWindow=[]
seq=4 turn=2 windowBytes=176 telegramRows=2 sessionRows=0 markersInWindow=[1]
seq=6 turn=3 windowBytes=353 telegramRows=4 sessionRows=0 markersInWindow=[1,2]
seq=8 turn=4 windowBytes=530 telegramRows=6 sessionRows=0 markersInWindow=[1,2,3]
### runs/base-shared-c2 sessions=agent:main:main
seq=1 turn=1 windowBytes=0 telegramRows=0 sessionRows=0 markersInWindow=[]
seq=4 turn=2 windowBytes=176 telegramRows=2 sessionRows=0 markersInWindow=[1]
seq=6 turn=3 windowBytes=353 telegramRows=4 sessionRows=0 markersInWindow=[1,2]
seq=8 turn=4 windowBytes=530 telegramRows=6 sessionRows=0 markersInWindow=[1,2,3]
### runs/cand-shared-c2 sessions=agent:main:main
seq=1 turn=1 windowBytes=0 telegramRows=0 sessionRows=0 markersInWindow=[]
seq=4 turn=2 windowBytes=58 telegramRows=1 sessionRows=0 markersInWindow=[]
seq=6 turn=3 windowBytes=235 telegramRows=3 sessionRows=0 markersInWindow=[2]
seq=8 turn=4 windowBytes=412 telegramRows=5 sessionRows=0 markersInWindow=[2,3]
### runs/base-announce-c1 sessions=agent:main:main:thread:4207684:79
seq=1 turn=1 windowBytes=347 telegramRows=1 sessionRows=2 markersInWindow=[]
### runs/cand-announce-c1 sessions=agent:main:main:thread:4207684:79
seq=1 turn=1 windowBytes=0 telegramRows=0 sessionRows=0 markersInWindow=[]

## Tests
head: ocx test extensions/telegram/src/{bot-message-context.*,sequential-key}.test.ts -> passed (tests-head.log)
merge base + PR test file: 1 failed (existing DM topic with its own session) | 22 passed (tests-red-on-base.log)
note: shared/fresh turn-2 size (58 vs 176 B) varies run to run on both builds: whether user message 1 falls before the session-start cut depends on sub-second timing (Telegram dates are whole seconds); base-shared-c2 == cand-shared-c1 and base-shared-c1 == cand-shared-c2 byte for byte.

@obviyus

obviyus commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator

Maintainer override: the only red job on 3a8bd0974c5 is check-plan (and openclaw/ci-gate, which reports it), and it is inherited from main.

  • Failing step: "Check narrow PR global guards" -> doctor-deprecation: 21 deprecated record(s) reached removeAfter by 2026-11-29 (job). The guard trips on today's date, not on this diff.
  • Same failure on another PR today: run 46205567997 (fix/cron-settlement-probe-deadline), same 21 records.
  • Same failure locally: node --import ./scripts/tsx.mjs scripts/check-doctor-deprecation-registry.ts exits 1 with the same 21 records on the merge base 68e792edca4 and on this head. Current origin/main (935d6230719) still has DOCTOR_COMPAT_RENEWED_REMOVE_AFTER = "2026-11-29" in src/commands/doctor/shared/deprecation-compat.ts.
  • This PR touches only extensions/telegram/src/* (4 production files, 1 test file); nothing under src/commands/doctor or scripts/.
  • Selected jobs that ran passed (24 success, including checks-node-changed, checks-node-changed-extensions-bundle-1, check-lint, check-prod-types, check-test-types, QA Smoke 1-6); 28 jobs were not selected for this change. Cancelled: 0.

@obviyus
obviyus merged commit 987f7b9 into openclaw:main Nov 29, 2026
97 of 102 checks passed
@obviyus

obviyus commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator

Landed in 987f7b9. Thanks @stealthybear for the report, the live Telegram proof and the fix. In a Telegram DM topic that has its own session, each turn no longer re-sends the topic's earlier messages in the Conversation context block; session replay carries them once, as in plain DMs. A fresh topic's first turn, explicit replies and threads that share the DM session keep their context.

vincentkoc added a commit that referenced this pull request Nov 29, 2026
…or baseline

* commit '2f2edbf36836a8ddc6758dfdcce872d4414a3a92': (520 commits)
  chore(doctor): hold expired doctor migrations as removal-pending (#207865)
  fix: show ended one-time automations in History (#201280)
  chore(release): update appcast for 2026.11.11 (#207504)
  fix: let updates back up deeply nested databases (#207756)
  fix(telegram): stop resending earlier messages to the model on every turn in DM topics (#207684)
  feat(workboard): choose which sessions appear on a board (#207781)
  fix: acknowledge updates when the Gateway first becomes ready (#207819)
  chore: stop the cron recovery test from failing CI on slow runners (#207837)
  fix: queue follow-ups when session creation reuses an active chat (#207800)
  feat(clawrouter): consume qualified native memory embeddings (#191874)
  fix: save the account for main-session prompts and converted reminders (#207776)
  test(infra): measure worker Buffer storage directly (#207663)
  feat(workboard): hide sessions and filter subagents per board (#207744)
  chore(i18n): refresh native locales (#207796)
  fix: name safe recovery when agent schema markers disagree (#207705)
  improve(ui): show only setup warnings on skill pages (#203750)
  fix(gateway): keep Linux legacy lock recovery explicit (#207340)
  fix: deliver a saved child answer after a Gateway crash (#207706)
  fix(ui): fold background command wakes into chat activity (#207305)
  fix: restore the Feishu proxy transport tests (#207782)
  ...
vincentkoc added a commit that referenced this pull request Nov 29, 2026
…aseline

* commit '2f2edbf36836a8ddc6758dfdcce872d4414a3a92': (520 commits)
  chore(doctor): hold expired doctor migrations as removal-pending (#207865)
  fix: show ended one-time automations in History (#201280)
  chore(release): update appcast for 2026.11.11 (#207504)
  fix: let updates back up deeply nested databases (#207756)
  fix(telegram): stop resending earlier messages to the model on every turn in DM topics (#207684)
  feat(workboard): choose which sessions appear on a board (#207781)
  fix: acknowledge updates when the Gateway first becomes ready (#207819)
  chore: stop the cron recovery test from failing CI on slow runners (#207837)
  fix: queue follow-ups when session creation reuses an active chat (#207800)
  feat(clawrouter): consume qualified native memory embeddings (#191874)
  fix: save the account for main-session prompts and converted reminders (#207776)
  test(infra): measure worker Buffer storage directly (#207663)
  feat(workboard): hide sessions and filter subagents per board (#207744)
  chore(i18n): refresh native locales (#207796)
  fix: name safe recovery when agent schema markers disagree (#207705)
  improve(ui): show only setup warnings on skill pages (#203750)
  fix(gateway): keep Linux legacy lock recovery explicit (#207340)
  fix: deliver a saved child answer after a Gateway crash (#207706)
  fix(ui): fold background command wakes into chat activity (#207305)
  fix: restore the Feishu proxy transport tests (#207782)
  ...
vincentkoc added a commit that referenced this pull request Nov 29, 2026
…aseline

* commit '2f2edbf36836a8ddc6758dfdcce872d4414a3a92': (520 commits)
  chore(doctor): hold expired doctor migrations as removal-pending (#207865)
  fix: show ended one-time automations in History (#201280)
  chore(release): update appcast for 2026.11.11 (#207504)
  fix: let updates back up deeply nested databases (#207756)
  fix(telegram): stop resending earlier messages to the model on every turn in DM topics (#207684)
  feat(workboard): choose which sessions appear on a board (#207781)
  fix: acknowledge updates when the Gateway first becomes ready (#207819)
  chore: stop the cron recovery test from failing CI on slow runners (#207837)
  fix: queue follow-ups when session creation reuses an active chat (#207800)
  feat(clawrouter): consume qualified native memory embeddings (#191874)
  fix: save the account for main-session prompts and converted reminders (#207776)
  test(infra): measure worker Buffer storage directly (#207663)
  feat(workboard): hide sessions and filter subagents per board (#207744)
  chore(i18n): refresh native locales (#207796)
  fix: name safe recovery when agent schema markers disagree (#207705)
  improve(ui): show only setup warnings on skill pages (#203750)
  fix(gateway): keep Linux legacy lock recovery explicit (#207340)
  fix: deliver a saved child answer after a Gateway crash (#207706)
  fix(ui): fold background command wakes into chat activity (#207305)
  fix: restore the Feishu proxy transport tests (#207782)
  ...
vincentkoc added a commit that referenced this pull request Nov 29, 2026
…line

* commit '652611b9d924c754ac7a18d0081cce6e1ec765df': (527 commits)
  refactor(infra): simplify audit recovery write progress (#157141)
  fix: show restart blockers in service logs (#207879)
  test(infra): exercise group identity in session roundtrip (#207671)
  test(infra): exercise live owner expiry policy (#207672)
  feat: let Knowledge learn from your Gmail through Composio (#207266)
  test: stop the history-worker overload test from failing CI on slow runners (#207862)
  test(auth): assert SecretRef plaintext is absent from storage (#207698)
  chore(doctor): hold expired doctor migrations as removal-pending (#207865)
  fix: show ended one-time automations in History (#201280)
  chore(release): update appcast for 2026.11.11 (#207504)
  fix: let updates back up deeply nested databases (#207756)
  fix(telegram): stop resending earlier messages to the model on every turn in DM topics (#207684)
  feat(workboard): choose which sessions appear on a board (#207781)
  fix: acknowledge updates when the Gateway first becomes ready (#207819)
  chore: stop the cron recovery test from failing CI on slow runners (#207837)
  fix: queue follow-ups when session creation reuses an active chat (#207800)
  feat(clawrouter): consume qualified native memory embeddings (#191874)
  fix: save the account for main-session prompts and converted reminders (#207776)
  test(infra): measure worker Buffer storage directly (#207663)
  feat(workboard): hide sessions and filter subagents per board (#207744)
  ...
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 29, 2026
…turn in DM topics (openclaw#207684)

Root cause: `extensions/telegram/src/bot-message-context.session.ts:406` drops Telegram's native chat window for an existing private session only when `dmThreadId == null`, so a DM topic with its own session gets its earlier turns again in the "Conversation context" block on every turn, on top of session replay.

Fixes openclaw#207657

Thanks @stealthybear for reporting this and for the fix.

## What Problem This Solves

In a Telegram private chat where the bot has topics turned on, each topic has its own session. Every turn in such a topic sends the model the topic's earlier messages again in the "Conversation context" block, once as Telegram `#<message id>` rows and once as `#session:` transcript rows, although the session already replays those turns. The copies stay in history, so they pile up turn after turn.

## User Impact

DM topics that route to their own session get the same per-turn context as plain DMs: earlier turns arrive once, through session replay. Fresh topics and explicit replies keep their context. A DM thread that shares the DM session (a bot without topics) or is bound to another session keeps the Telegram window it needs to tell its own messages apart. No config or migration.

## Why This Change Was Made

openclaw#89855 drops Telegram's native window for plain DMs whose session replays the earlier turns, and left DM threads out. Neither openclaw#89855 nor openclaw#183190 gives a reason; the likely one (our inference) is that a DM thread can share its session with the rest of the DM, and then the window is what tells the thread's messages apart. That reason holds only when the thread has no session of its own. So the rule now keys on that property: `hasTelegramDmThreadSession` in `conversation-route.ts` decides whether a DM thread routes to its own session (no binding selects one, and the bot uses DM topic sessions). Session routing and the context builder both use it, so they can't disagree. Routing itself doesn't change: bindings already returned before that check.

## Evidence

**Live, before and after:** real Telegram (Telegram Test Server, upstream's `telegram-e2e-userbot` runner at `2e58ed939a1` with `--dm --create-dm-topic`, a real test user and bot, and the real Claude CLI model): fresh gateways from each packed build. The user sends four messages ("Message i: remember the code word marker-i-live. Reply with only: noted-i") into a new DM topic. Both runs route to the topic's own session (`agent:main:main:thread:<user>:<topic>`), get all four replies in the topic, and end with `testDmTopic.cleanup.status: restored`. Measured: the "Conversation context" window the model receives each turn, from the Claude CLI transcript.

| Turn | Released 2026.12.1-beta.1 | This PR |
| --- | --- | --- |
| 2 | 204 B, 2 Telegram rows | 0 B, no rows |
| 3 | 368 B, 4 Telegram rows | 0 B, no rows |
| 4 | 532 B, 6 Telegram rows: messages 2 and 3 and their replies, already in session replay | 0 B, no rows |

<details><summary>Fourth turn, released build: the window the model got (timestamps cut)</summary>

```
Conversation context (chronological, selected for current message): ⟦openclaw:ctx⟧
#3 OpenClaw QA bot (you): Working
#4 OpenClaw QA bot (you): noted-1
#5 A: Message 2: remember the code word marker-2-live. Reply with only: noted-2
openclaw#6 OpenClaw QA bot (you): noted-2
openclaw#7 A: Message 3: remember the code word marker-3-live. Reply with only: noted-3
openclaw#8 OpenClaw QA bot (you): noted-3
```

With this PR the fourth turn has no window; the earlier turns reach the model once, through session replay.

</details>

**Controls, before and after:** stub Telegram Bot API and stub model, which also cover the plain DM and a shared reply thread. One script sends four messages ("Message i: remember the code word …") and measures the fourth request.

| Fourth turn | Released 2026.12.1-beta.1 | This PR |
| --- | --- | --- |
| DM topic with its own session | context block 530 B: 2 Telegram + 3 transcript rows; message 2 sent 3 times, message 3 twice | 0 B, no rows; each message once |
| Plain DM (control) | 0 B, each message once | unchanged |
| Reply thread sharing the DM session, bot without topics (control) | 1,046 B: 3 Telegram + 7 transcript rows | unchanged, on purpose (below) |

![Fourth-turn context before and after](https://raw.githubusercontent.com/stealthybear/openclaw/0a3e5d13f000062913a17d55e9cd59c483ba7a35/before-after.png)

Tested on 3a8bd09. Request proof: on every chat turn of the three fix runs, the user text sent to the model byte-equals the transcript (`ocfix proof sent`; the topic-label side request, which carries no chat turn, was left out).

- Review: `autoreview` (Claude engine), `fad9fac5c23..3a8bd09`, P0-P2: clean, no findings.
- Tests: three new cases in the existing table of `bot-message-context.prompt-context.test.ts` (99 s wall, `--maxWorkers=1`): an existing DM topic with its own session (fails on main, passes with the fix), a fresh DM topic, and a reply thread that shares the DM session. The DM-session, bot-threads, session-route and `bot.test.ts` suites pass. Typecheck and lint clean.

**Maintainer re-run on `3a8bd0974c5`:** Telegram Test Server, repo `telegram-e2e-userbot` runner, `--dm --create-dm-topic`, mock provider, fresh Gateways built from main (`fad9fac5c23`, the merge base) and from this head, each side twice. Four messages into a new DM topic; all four replies arrive in the topic on every run, and every run ends with `testDmTopic.cleanup.status: restored` and Threaded Mode off.

| Turn | main, run 1 | main, run 2 | this PR, run 1 | this PR, run 2 |
| --- | --- | --- | --- | --- |
| 2 | 84 B, 1 Telegram row | 84 B, 1 row | 0 B | 0 B |
| 3 | 302 B, 3 rows | 302 B, 3 rows | 0 B | 0 B |
| 4 | 520 B, 5 rows | 520 B, 5 rows | 0 B | 0 B |

Stub Telegram Bot API controls, both builds: a fresh topic session's first turn keeps its window; an explicit reply to message 1 keeps the window and reply target; a plain DM stays at 0 B; a reply thread that shares the DM session (bot without topics) keeps its growing window. Proof lines: openclaw#207684 (comment)

<details><summary>Not covered</summary>

- A DM thread that shares the DM session or is bound still gets the native window plus merged `#session:` rows (the "shared reply thread" row above). That path is the one openclaw#89855 and openclaw#183190 kept on purpose; deduplicating it inside the merge is a separate change.
- A topic that has its own session, is then bound to another session, then unbound: messages sent while it was bound are in neither the topic's replay nor its window after the unbind. Plain DMs have the same gap. It is rare and recoverable (reply to or quote the message), so this PR does not code for it.
- Forum topics in groups are unchanged: group windows carry room messages that never became turns.

</details>

Co-authored-by: Ayaan Zaidi <[email protected]>
tideclaw-bot pushed a commit that referenced this pull request Nov 29, 2026
…turn in DM topics (#207684)

Root cause: `extensions/telegram/src/bot-message-context.session.ts:406` drops Telegram's native chat window for an existing private session only when `dmThreadId == null`, so a DM topic with its own session gets its earlier turns again in the "Conversation context" block on every turn, on top of session replay.

Fixes #207657

Thanks @stealthybear for reporting this and for the fix.

## What Problem This Solves

In a Telegram private chat where the bot has topics turned on, each topic has its own session. Every turn in such a topic sends the model the topic's earlier messages again in the "Conversation context" block, once as Telegram `#<message id>` rows and once as `#session:` transcript rows, although the session already replays those turns. The copies stay in history, so they pile up turn after turn.

## User Impact

DM topics that route to their own session get the same per-turn context as plain DMs: earlier turns arrive once, through session replay. Fresh topics and explicit replies keep their context. A DM thread that shares the DM session (a bot without topics) or is bound to another session keeps the Telegram window it needs to tell its own messages apart. No config or migration.

## Why This Change Was Made

#89855 drops Telegram's native window for plain DMs whose session replays the earlier turns, and left DM threads out. Neither #89855 nor #183190 gives a reason; the likely one (our inference) is that a DM thread can share its session with the rest of the DM, and then the window is what tells the thread's messages apart. That reason holds only when the thread has no session of its own. So the rule now keys on that property: `hasTelegramDmThreadSession` in `conversation-route.ts` decides whether a DM thread routes to its own session (no binding selects one, and the bot uses DM topic sessions). Session routing and the context builder both use it, so they can't disagree. Routing itself doesn't change: bindings already returned before that check.

## Evidence

**Live, before and after:** real Telegram (Telegram Test Server, upstream's `telegram-e2e-userbot` runner at `a1ced09cd7f` with `--dm --create-dm-topic`, a real test user and bot, and the real Claude CLI model): fresh gateways from each packed build. The user sends four messages ("Message i: remember the code word marker-i-live. Reply with only: noted-i") into a new DM topic. Both runs route to the topic's own session (`agent:main:main:thread:<user>:<topic>`), get all four replies in the topic, and end with `testDmTopic.cleanup.status: restored`. Measured: the "Conversation context" window the model receives each turn, from the Claude CLI transcript.

| Turn | Released 2026.12.1-beta.1 | This PR |
| --- | --- | --- |
| 2 | 204 B, 2 Telegram rows | 0 B, no rows |
| 3 | 368 B, 4 Telegram rows | 0 B, no rows |
| 4 | 532 B, 6 Telegram rows: messages 2 and 3 and their replies, already in session replay | 0 B, no rows |

<details><summary>Fourth turn, released build: the window the model got (timestamps cut)</summary>

```
Conversation context (chronological, selected for current message): ⟦openclaw:ctx⟧
#3 OpenClaw QA bot (you): Working
#4 OpenClaw QA bot (you): noted-1
#5 A: Message 2: remember the code word marker-2-live. Reply with only: noted-2
#6 OpenClaw QA bot (you): noted-2
#7 A: Message 3: remember the code word marker-3-live. Reply with only: noted-3
#8 OpenClaw QA bot (you): noted-3
```

With this PR the fourth turn has no window; the earlier turns reach the model once, through session replay.

</details>

**Controls, before and after:** stub Telegram Bot API and stub model, which also cover the plain DM and a shared reply thread. One script sends four messages ("Message i: remember the code word …") and measures the fourth request.

| Fourth turn | Released 2026.12.1-beta.1 | This PR |
| --- | --- | --- |
| DM topic with its own session | context block 530 B: 2 Telegram + 3 transcript rows; message 2 sent 3 times, message 3 twice | 0 B, no rows; each message once |
| Plain DM (control) | 0 B, each message once | unchanged |
| Reply thread sharing the DM session, bot without topics (control) | 1,046 B: 3 Telegram + 7 transcript rows | unchanged, on purpose (below) |

![Fourth-turn context before and after](https://raw.githubusercontent.com/stealthybear/openclaw/0a3e5d13f000062913a17d55e9cd59c483ba7a35/before-after.png)

Tested on 3a8bd09. Request proof: on every chat turn of the three fix runs, the user text sent to the model byte-equals the transcript (`ocfix proof sent`; the topic-label side request, which carries no chat turn, was left out).

- Review: `autoreview` (Claude engine), `68e792edca4..3a8bd09`, P0-P2: clean, no findings.
- Tests: three new cases in the existing table of `bot-message-context.prompt-context.test.ts` (99 s wall, `--maxWorkers=1`): an existing DM topic with its own session (fails on main, passes with the fix), a fresh DM topic, and a reply thread that shares the DM session. The DM-session, bot-threads, session-route and `bot.test.ts` suites pass. Typecheck and lint clean.

**Maintainer re-run on `3a8bd0974c5`:** Telegram Test Server, repo `telegram-e2e-userbot` runner, `--dm --create-dm-topic`, mock provider, fresh Gateways built from main (`68e792edca4`, the merge base) and from this head, each side twice. Four messages into a new DM topic; all four replies arrive in the topic on every run, and every run ends with `testDmTopic.cleanup.status: restored` and Threaded Mode off.

| Turn | main, run 1 | main, run 2 | this PR, run 1 | this PR, run 2 |
| --- | --- | --- | --- | --- |
| 2 | 84 B, 1 Telegram row | 84 B, 1 row | 0 B | 0 B |
| 3 | 302 B, 3 rows | 302 B, 3 rows | 0 B | 0 B |
| 4 | 520 B, 5 rows | 520 B, 5 rows | 0 B | 0 B |

Stub Telegram Bot API controls, both builds: a fresh topic session's first turn keeps its window; an explicit reply to message 1 keeps the window and reply target; a plain DM stays at 0 B; a reply thread that shares the DM session (bot without topics) keeps its growing window. Proof lines: #207684 (comment)

<details><summary>Not covered</summary>

- A DM thread that shares the DM session or is bound still gets the native window plus merged `#session:` rows (the "shared reply thread" row above). That path is the one #89855 and #183190 kept on purpose; deduplicating it inside the merge is a separate change.
- A topic that has its own session, is then bound to another session, then unbound: messages sent while it was bound are in neither the topic's replay nor its window after the unbind. Plain DMs have the same gap. It is rare and recoverable (reply to or quote the message), so this PR does not code for it.
- Forum topics in groups are unchanged: group windows carry room messages that never became turns.

</details>

Co-authored-by: Ayaan Zaidi <[email protected]>

(cherry picked from commit 987f7b9)

Co-authored-by: RomneyDa <[email protected]>
tideclaw-bot pushed a commit that referenced this pull request Nov 30, 2026
…turn in DM topics (#207684)

Root cause: `extensions/telegram/src/bot-message-context.session.ts:406` drops Telegram's native chat window for an existing private session only when `dmThreadId == null`, so a DM topic with its own session gets its earlier turns again in the "Conversation context" block on every turn, on top of session replay.

Fixes #207657

Thanks @stealthybear for reporting this and for the fix.

## What Problem This Solves

In a Telegram private chat where the bot has topics turned on, each topic has its own session. Every turn in such a topic sends the model the topic's earlier messages again in the "Conversation context" block, once as Telegram `#<message id>` rows and once as `#session:` transcript rows, although the session already replays those turns. The copies stay in history, so they pile up turn after turn.

## User Impact

DM topics that route to their own session get the same per-turn context as plain DMs: earlier turns arrive once, through session replay. Fresh topics and explicit replies keep their context. A DM thread that shares the DM session (a bot without topics) or is bound to another session keeps the Telegram window it needs to tell its own messages apart. No config or migration.

## Why This Change Was Made

#89855 drops Telegram's native window for plain DMs whose session replays the earlier turns, and left DM threads out. Neither #89855 nor #183190 gives a reason; the likely one (our inference) is that a DM thread can share its session with the rest of the DM, and then the window is what tells the thread's messages apart. That reason holds only when the thread has no session of its own. So the rule now keys on that property: `hasTelegramDmThreadSession` in `conversation-route.ts` decides whether a DM thread routes to its own session (no binding selects one, and the bot uses DM topic sessions). Session routing and the context builder both use it, so they can't disagree. Routing itself doesn't change: bindings already returned before that check.

## Evidence

**Live, before and after:** real Telegram (Telegram Test Server, upstream's `telegram-e2e-userbot` runner at `a1ced09cd7f` with `--dm --create-dm-topic`, a real test user and bot, and the real Claude CLI model): fresh gateways from each packed build. The user sends four messages ("Message i: remember the code word marker-i-live. Reply with only: noted-i") into a new DM topic. Both runs route to the topic's own session (`agent:main:main:thread:<user>:<topic>`), get all four replies in the topic, and end with `testDmTopic.cleanup.status: restored`. Measured: the "Conversation context" window the model receives each turn, from the Claude CLI transcript.

| Turn | Released 2026.12.1-beta.1 | This PR |
| --- | --- | --- |
| 2 | 204 B, 2 Telegram rows | 0 B, no rows |
| 3 | 368 B, 4 Telegram rows | 0 B, no rows |
| 4 | 532 B, 6 Telegram rows: messages 2 and 3 and their replies, already in session replay | 0 B, no rows |

<details><summary>Fourth turn, released build: the window the model got (timestamps cut)</summary>

```
Conversation context (chronological, selected for current message): ⟦openclaw:ctx⟧
#3 OpenClaw QA bot (you): Working
#4 OpenClaw QA bot (you): noted-1
#5 A: Message 2: remember the code word marker-2-live. Reply with only: noted-2
#6 OpenClaw QA bot (you): noted-2
#7 A: Message 3: remember the code word marker-3-live. Reply with only: noted-3
#8 OpenClaw QA bot (you): noted-3
```

With this PR the fourth turn has no window; the earlier turns reach the model once, through session replay.

</details>

**Controls, before and after:** stub Telegram Bot API and stub model, which also cover the plain DM and a shared reply thread. One script sends four messages ("Message i: remember the code word …") and measures the fourth request.

| Fourth turn | Released 2026.12.1-beta.1 | This PR |
| --- | --- | --- |
| DM topic with its own session | context block 530 B: 2 Telegram + 3 transcript rows; message 2 sent 3 times, message 3 twice | 0 B, no rows; each message once |
| Plain DM (control) | 0 B, each message once | unchanged |
| Reply thread sharing the DM session, bot without topics (control) | 1,046 B: 3 Telegram + 7 transcript rows | unchanged, on purpose (below) |

![Fourth-turn context before and after](https://raw.githubusercontent.com/stealthybear/openclaw/0a3e5d13f000062913a17d55e9cd59c483ba7a35/before-after.png)

Tested on 3a8bd09. Request proof: on every chat turn of the three fix runs, the user text sent to the model byte-equals the transcript (`ocfix proof sent`; the topic-label side request, which carries no chat turn, was left out).

- Review: `autoreview` (Claude engine), `68e792edca4..3a8bd09`, P0-P2: clean, no findings.
- Tests: three new cases in the existing table of `bot-message-context.prompt-context.test.ts` (99 s wall, `--maxWorkers=1`): an existing DM topic with its own session (fails on main, passes with the fix), a fresh DM topic, and a reply thread that shares the DM session. The DM-session, bot-threads, session-route and `bot.test.ts` suites pass. Typecheck and lint clean.

**Maintainer re-run on `3a8bd0974c5`:** Telegram Test Server, repo `telegram-e2e-userbot` runner, `--dm --create-dm-topic`, mock provider, fresh Gateways built from main (`68e792edca4`, the merge base) and from this head, each side twice. Four messages into a new DM topic; all four replies arrive in the topic on every run, and every run ends with `testDmTopic.cleanup.status: restored` and Threaded Mode off.

| Turn | main, run 1 | main, run 2 | this PR, run 1 | this PR, run 2 |
| --- | --- | --- | --- | --- |
| 2 | 84 B, 1 Telegram row | 84 B, 1 row | 0 B | 0 B |
| 3 | 302 B, 3 rows | 302 B, 3 rows | 0 B | 0 B |
| 4 | 520 B, 5 rows | 520 B, 5 rows | 0 B | 0 B |

Stub Telegram Bot API controls, both builds: a fresh topic session's first turn keeps its window; an explicit reply to message 1 keeps the window and reply target; a plain DM stays at 0 B; a reply thread that shares the DM session (bot without topics) keeps its growing window. Proof lines: #207684 (comment)

<details><summary>Not covered</summary>

- A DM thread that shares the DM session or is bound still gets the native window plus merged `#session:` rows (the "shared reply thread" row above). That path is the one #89855 and #183190 kept on purpose; deduplicating it inside the merge is a separate change.
- A topic that has its own session, is then bound to another session, then unbound: messages sent while it was bound are in neither the topic's replay nor its window after the unbind. Plain DMs have the same gap. It is rare and recoverable (reply to or quote the message), so this PR does not code for it.
- Forum topics in groups are unchanged: group windows carry room messages that never became turns.

</details>

Co-authored-by: Ayaan Zaidi <[email protected]>
(cherry picked from commit 987f7b9)

Companion audit: #190121 changes WhatsApp reactions, unrelated to Telegram own-session replay suppression.

Co-authored-by: RomneyDa <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

channel: telegram Channel integration: telegram impact:session-state Session, memory, transcript, context, or agent state can drift or corrupt. 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: S status: ⏳ waiting on maintainer Only explicit maintainer approvals or decisions remain; merge is still blocked.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Telegram DM topics send the model each earlier message twice on every turn

2 participants