Skip to content

[Bug]: Telegram DMs can still land in agent:main:main, polluting heartbeat/main session after #40519 #41165

@ZemonVunter

Description

@ZemonVunter

Bug type / 问题类型

Behavior bug (incorrect routing/session selection without crash)
行为错误(路由 / 会话选择异常,无崩溃)


Summary / 摘要

Telegram direct messages can still be routed into agent:main:main instead of staying isolated in agent:main:telegram:direct:<id>, even after the duplicate-reply fix from #40519.

In my case, this polluted the session that was being used for heartbeat, causing heartbeat to inherit unrelated user conversation history and burn a large amount of tokens.

即使在 #40519 修复了“重复回复”之后,Telegram 私聊消息似乎仍然可能被路由进 agent:main:main,而不是稳定地留在 agent:main:telegram:direct:<id> 中。

在我的实际案例里,这直接污染了原本用于 heartbeat 的会话,导致 heartbeat 继承了无关的用户聊天历史,并显著增加 token 消耗。


Related issues / PRs / 相关 Issue / PR


What I expected / 期望行为

Telegram DMs should consistently stay in:

  • agent:main:telegram:direct:<telegram-user-id>

and should not be appended to:

  • agent:main:main

especially when agent:main:main is being used for heartbeat or other main-session purposes.

Telegram 私聊应始终稳定进入:

  • agent:main:telegram:direct:<telegram-user-id>

而不应该被追加到:

  • agent:main:main

尤其不应该在 agent:main:main 已经被用于 heartbeat 或其他 main-session 场景时混入其中。


What actually happened / 实际发生的情况

A session whose origin/delivery context was clearly heartbeat:

  • session key: agent:main:main
  • origin.provider: heartbeat
  • deliveryContext.to: heartbeat

was later reused for ordinary Telegram DM requests.

That same session first handled heartbeat prompts such as:

  • Read HEARTBEAT.md if it exists ... If nothing needs attention, reply HEARTBEAT_OK

but later also received multiple normal user requests, including translation tasks like:

  • translating short English snippets
  • translating multiple OpenClaw release notes into Chinese Markdown

As a result:

  1. the heartbeat/main session accumulated unrelated user conversation history
  2. heartbeat runs became expensive because they inherited this polluted context
  3. session isolation between Telegram DM and main/heartbeat behavior was effectively broken

有一个会话,其 origin / delivery context 明确显示它本应属于 heartbeat:

  • session key: agent:main:main
  • origin.provider: heartbeat
  • deliveryContext.to: heartbeat

但后来这个会话又被普通 Telegram 私聊请求复用了。

这个会话一开始处理的是 heartbeat 提示,例如:

  • Read HEARTBEAT.md if it exists ... If nothing needs attention, reply HEARTBEAT_OK

但之后又收到了多条普通用户请求,包括翻译类任务,例如:

  • 翻译简单英文句子
  • 把多篇 OpenClaw 发布说明翻译成中文 Markdown

结果就是:

  1. heartbeat / main 会话积累了大量无关的用户聊天历史
  2. heartbeat 因继承这部分污染上下文而变得昂贵
  3. Telegram 私聊与 main / heartbeat 之间的会话隔离实际上被破坏了

Why this seems distinct from #40005 / #40519 / 为什么这和 #40005 / #40519 不完全一样

My understanding is that #40519 fixed duplicate execution / duplicate replies for the case where the same Telegram DM is surfaced under both:

  • agent:main:main
  • agent:main:telegram:direct:<id>

However, in my case, the underlying session-selection / routing contamination still appears to happen.

So the duplicate reply symptom may be fixed, but the more fundamental issue remains:

  • Telegram DMs can still land in agent:main:main
  • agent:main:main can still be polluted by direct-chat traffic

我的理解是,#40519 修复的是重复执行 / 重复回复问题,也就是同一条 Telegram 私聊同时出现在:

  • agent:main:main
  • agent:main:telegram:direct:<id>

时只执行一次。

但在我的案例里,更底层的会话选择 / 路由污染似乎仍然存在。

也就是说,“重复回复”这个表层症状可能被修好了,但更深层的问题还在:

  • Telegram 私聊仍然可能落进 agent:main:main
  • agent:main:main 仍然可能被 direct chat 污染

Evidence / 证据

From sessions.json, the polluted session was:

  • session key: agent:main:main
  • sessionId: 392fda28-76ed-4115-b56b-1f77bc87bdcb
  • origin.label: heartbeat
  • origin.provider: heartbeat
  • deliveryContext.to: heartbeat

Its transcript file was:

  • /Users/.../.openclaw/agents/main/sessions/0313f1ed-7b78-4f79-a216-fa40abe3d147.jsonl

Within that same transcript, I observed both:

A. Heartbeat traffic

Examples included prompts like:

  • Read HEARTBEAT.md if it exists ... reply HEARTBEAT_OK

and heartbeat-related actions such as reading:

  • HEARTBEAT.md
  • heartbeat-state.json

B. Normal user DM traffic

Later in the same transcript, I observed many ordinary user requests, including:

  • translation of Hello world
  • translation of multiple OpenClaw release notes:
    • v2026.2.25
    • v2026.2.26
    • v2026.3.1
    • v2026.3.2
    • v2026.3.7
    • v2026.3.7-beta.1
    • v2026.3.8-beta.1
    • v2026.3.8

These were clearly not heartbeat tasks.

根据 sessions.json,被污染的会话是:

  • session key: agent:main:main
  • sessionId: 392fda28-76ed-4115-b56b-1f77bc87bdcb
  • origin.label: heartbeat
  • origin.provider: heartbeat
  • deliveryContext.to: heartbeat

对应 transcript 文件为:

  • /Users/.../.openclaw/agents/main/sessions/0313f1ed-7b78-4f79-a216-fa40abe3d147.jsonl

在同一个 transcript 中,我同时观察到了:

A. Heartbeat 流量

例如:

  • Read HEARTBEAT.md if it exists ... reply HEARTBEAT_OK

以及 heartbeat 相关行为,例如读取:

  • HEARTBEAT.md
  • heartbeat-state.json

B. 普通用户 Telegram 私聊流量

在同一 transcript 的后半段,又出现了多条普通用户请求,包括:

  • 翻译 Hello world
  • 翻译多篇 OpenClaw 发布说明:
    • v2026.2.25
    • v2026.2.26
    • v2026.3.1
    • v2026.3.2
    • v2026.3.7
    • v2026.3.7-beta.1
    • v2026.3.8-beta.1
    • v2026.3.8

这些显然都不是 heartbeat 任务。


Additional observation / 补充观察

Earlier heartbeat turns in the same session also did more than a plain HEARTBEAT_OK:

  • queried weather
  • queried reminders
  • produced morning/evening summaries

So once ordinary Telegram DMs also entered the same session, the context became a mix of:

  • heartbeat logic
  • reminders/weather checks
  • normal user conversation

This made the token cost of heartbeat much worse.

这个会话里更早的 heartbeat 轮次,本身也不只是简单返回 HEARTBEAT_OK,还做过:

  • 查询天气
  • 查询提醒事项
  • 输出早晚简报

所以一旦普通 Telegram 私聊也进入同一个 session,整个上下文就变成了:

  • heartbeat 逻辑
  • 天气 / 提醒检查
  • 普通用户对话

这会明显放大 heartbeat 的 token 成本。


Reproduction idea / 复现思路

I cannot guarantee a 100% minimal repro yet, but the pattern appears to be:

  1. Have heartbeat enabled so agent:main:main is active
  2. Use Telegram DM with the same agent
  3. Over time, observe that normal DM requests may append into agent:main:main instead of remaining isolated in agent:main:telegram:direct:<id>

我目前还不能给出 100% 极简、稳定复现步骤,但观察到的模式大致是:

  1. 开启 heartbeat,让 agent:main:main 处于活跃状态
  2. 同时使用 Telegram 私聊同一个 agent
  3. 经过一段时间后,普通 DM 请求可能会被追加到 agent:main:main,而不是稳定留在 agent:main:telegram:direct:<id>

Temporary workaround / 临时绕过方案

I manually cleaned the polluted agent:main:main session by:

  1. archiving its transcript
  2. removing the agent:main:main entry from sessions.json

After that, the dirty main/heartbeat session disappeared and could be recreated cleanly.

This is only a workaround, not a fix.

我目前的临时处理方式是手动清理被污染的 agent:main:main

  1. 归档它的 transcript
  2. sessions.json 中移除 agent:main:main 这条映射

这样做之后,旧的脏 main/heartbeat session 会消失,下次 heartbeat 会重新创建一个干净会话。

但这只是绕过方法,不是根修复。


Expected fix direction / 建议修复方向

At minimum, Telegram DM session selection should be strictly isolated so that normal DMs cannot append to agent:main:main.

More robustly:

  1. Heartbeat should use a dedicated isolated session namespace rather than agent:main:main
  2. Telegram DMs should never reuse a heartbeat-origin main session
  3. Fixes like fix: dedupe inbound Telegram DM replies per agent #40519 should apply not only to duplicate execution, but also to the underlying session-selection ambiguity

至少应保证 Telegram DM 的会话选择严格隔离,避免普通私聊被追加到 agent:main:main

更稳妥的方向可能包括:

  1. Heartbeat 应使用独立的隔离 session namespace,而不是复用 agent:main:main
  2. Telegram 私聊不应复用一个 origin 为 heartbeat 的 main session
  3. fix: dedupe inbound Telegram DM replies per agent #40519 这样的修复不应只处理“重复执行”,也应覆盖更底层的 session-selection 歧义

OpenClaw version / OpenClaw 版本

Observed on OpenClaw 2026.3.7 runtime, with behavior investigated on March 9, 2026, after reviewing the 2026.3.8 changelog and the merged fix in #40519.

问题发生在 OpenClaw 2026.3.7 运行时环境中;我是在 2026-03-09 排查这个现象时,结合 2026.3.8 的 changelog 和已合并的 #40519 修复一起比对后确认的。


Operating system / 操作系统

macOS (Apple Silicon)


Install method / 安装方式

npm global


Impact and severity / 影响与严重性

Affected: Telegram DM + heartbeat users

Severity: Medium to High

Consequences:

  • heartbeat session pollution
  • token waste
  • session isolation confusion
  • risk of future routing/context leakage across main vs direct traffic

受影响范围:Telegram 私聊 + heartbeat 用户

严重性:中到高

后果包括:

  • heartbeat 会话被污染
  • token 浪费
  • 会话隔离混乱
  • main / direct 流量后续发生上下文泄漏的风险

Metadata

Metadata

Assignees

No one assigned

    Labels

    P1High-priority user-facing bug, regression, or broken workflow.clawsweeper:linked-pr-openClawSweeper found an open linked pull request for this issue.clawsweeper:needs-product-decisionClawSweeper marked this issue as needing a product or behavior decision.clawsweeper:no-new-fix-prClawSweeper does not recommend queueing a new automated fix PR for this issue.clawsweeper:source-reproClawSweeper found a high-confidence source-level issue reproduction.impact:message-lossChannel message delivery can be lost, duplicated, or misrouted.impact:session-stateSession, memory, transcript, context, or agent state can drift or corrupt.issue-rating: 🦞 diamond lobsterVery strong issue quality with high-confidence source-level or clear reproduction.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions