Skip to content

[Bug]: MEDIA directive final replies still duplicate text through channel.message split path #91861

Closed

Description

@xmoxmo

Summary

Follow-up to #91818. That issue was closed as resolved, but the closure appears to have matched the wrong fix model. The live Bncr reproduction is not just “block-streamed text plus final media payload”; it is a MEDIA: directive + channel.message delivery split where the intermediate/block path intentionally does not parse MEDIA: while the final/outbound path does parse it and later uses the remaining text as media caption.

As a result, a mixed final reply can still become:

  1. block/intermediate text-only send;
  2. final media send via channel.message / outbound delivery;
  3. the same text reused as media caption or emitted again around the media handoff.

This produces duplicated user-visible text even on OpenClaw 2026.6.5.

Why #91818 should not have been closed

The #91818 close comment cites two different things:

  • Relevant-looking code around src/auto-reply/reply/agent-runner-payloads.ts / block-reply-pipeline.ts that can strip text from a final media payload when the block pipeline can prove the text was already sent.
  • But it also cites PR fix(gateway): skip deleted-agent guard for ACP harness session keys #91219 / commit 696c1ecd2068, which is unrelated (fix(gateway): skip deleted-agent guard for ACP harness session keys, files under src/gateway/session-utils*).

More importantly, the cited fix does not cover the actual MEDIA: directive split path described below.

Concrete source path in v2026.6.5

1. Block/intermediate replies do not extract MEDIA: directives

In src/auto-reply/reply/reply-delivery.ts, createBlockReplyDeliveryHandler normalizes block replies with:

const normalized = normalizeReplyPayloadDirectives({
  payload: taggedPayload,
  currentMessageId: params.currentMessageId,
  silentToken: SILENT_REPLY_TOKEN,
  trimLeadingWhitespace: true,
  parseMode: "auto",
  extractMediaDirectives: false,
});

Because extractMediaDirectives: false, an assistant block/intermediate response that contains visible text plus a MEDIA: line cannot become a media-aware payload at this stage. The intermediate layer is effectively text-only.

2. Final delivery does parse MEDIA: directives

In src/auto-reply/reply/agent-runner-payloads.ts, final payload construction calls:

const parsed = normalizeReplyPayloadDirectives({
  payload,
  currentMessageId: params.currentMessageId,
  silentToken: SILENT_REPLY_TOKEN,
  parseMode: "always",
  extractMarkdownImages: params.extractMarkdownImages,
});

normalizeReplyPayloadDirectives(...) then uses parseReplyDirectives(...) to split final text and media:

const mediaUrls = params.payload.mediaUrls ?? parsed?.mediaUrls;
const mediaUrl = params.payload.mediaUrl ?? parsed?.mediaUrl ?? mediaUrls?.[0];

return {
  payload: copyReplyPayloadMetadata(params.payload, {
    ...params.payload,
    text,
    mediaUrls,
    mediaUrl,
    ...
  }),
  isSilent: parsed?.isSilent ?? false,
};

So final delivery is media-aware while the block/intermediate path was not.

3. The cited dedupe only works if the block pipeline can prove a text-only payload was already sent

The close comment points at the preserveUnsentMediaAfterBlockStream logic:

const preserveUnsentMediaAfterBlockStream = (payload: ReplyPayload): ReplyPayload | null => {
  ...
  const reply = resolveSendableOutboundReplyParts(payload);
  if (!reply.hasMedia) {
    return null;
  }
  if (!reply.trimmedText) {
    return payload;
  }
  const textOnlyPayload = copyReplyPayloadMetadata(payload, {
    ...payload,
    mediaUrl: undefined,
    mediaUrls: undefined,
    audioAsVoice: undefined,
  });
  if (!params.blockReplyPipeline?.hasSentPayload(textOnlyPayload)) {
    return payload;
  }
  return copyReplyPayloadMetadata(payload, {
    ...payload,
    text: undefined,
    audioAsVoice: payload.audioAsVoice || undefined,
  });
};

This only strips final media text when blockReplyPipeline.hasSentPayload(textOnlyPayload) returns true. That is not guaranteed for MEDIA: directive final replies delivered through the source-channel/channel.message path, especially when the earlier text was sent by a direct block path, tool/progress path, or a different source delivery mechanism.

4. Outbound media delivery uses remaining text as media caption

In dist/deliver-D56UzjS7.js from installed 2026.6.5, final media delivery plans media units using:

const mediaUnits = planOutboundMediaMessageUnits({
  mediaUrls: payloadSummary.mediaUrls,
  caption: payloadSummary.text,
  overrides: sendOverrides,
  consumeReplyTo: applySendReplyToConsumption
});

Then each media unit is sent as:

await deliveryHandler.sendMedia(unit.caption ?? "", unit.mediaUrl, unit.overrides)

Therefore, if the dedupe path above fails to clear payload.text, the final media handoff reuses the same visible text as caption after the intermediate text was already sent.

Live reproduction evidence

Environment:

OpenClaw 2026.6.5 (5181e4f)
@xmoxmo/bncr 0.3.6

Final assistant reply shape:

测试 txt 附件来了,陌。  
如果链路正常,你这边应该能看到这段文字 + 一个 `.txt` 文件。

MEDIA:/root/.openclaw/workspace/tmp/bncr-channel-message-test-20260610-1121.txt

Gateway/Bncr journal:

2026-06-10T11:22:18+08:00 [bncr] outbound text|Bncr:tgBot:-1003776014601:6278285192|测试 txt 附…
2026-06-10T11:22:18+08:00 [bncr] outbox push mid=d8c528dc-3c5c-4d89-9d48-3c03a72b1541|q=1
2026-06-10T11:22:18+08:00 [bncr] outbound message.outbound|Bncr:tgBot:-1003776014601:6278285192|-
2026-06-10T11:22:20+08:00 [bncr] outbox ack ok mid=d8c528dc-3c5c-4d89-9d48-3c03a72b1541|q=1|queueMs=1691|pushMs=1688
2026-06-10T11:22:21+08:00 [bncr] outbox push mid=58a44239-5d5b-4ad8-8717-92a29db514ca|q=1
2026-06-10T11:22:21+08:00 [bncr] outbound text|Bncr:tgBot:-1003776014601:6278285192|测试 txt 附…
2026-06-10T11:22:22+08:00 [bncr] outbox ack ok mid=58a44239-5d5b-4ad8-8717-92a29db514ca|q=1|queueMs=3348|pushMs=887
2026-06-10T11:22:22+08:00 [bncr] outbox push mid=e1cded76-cc16-43f4-bb43-f860f262f4c7|q=1
2026-06-10T11:22:23+08:00 [bncr] outbox ack ok mid=e1cded76-cc16-43f4-bb43-f860f262f4c7|q=0|queueMs=1960|pushMs=1221

All outbox entries ACK successfully. This is not an ACK retry or client-side duplicate rendering.

Expected behavior

For one logical assistant final reply containing text plus MEDIA: attachment(s), the user-visible text must be delivered exactly once.

Acceptable fixes:

  1. Make intermediate/block replies media-aware too. If the intermediate layer sends text from a reply that contains MEDIA:, it should also carry the attachment metadata / logical media identity, so final delivery can dedupe/merge correctly.
  2. Strip duplicated text from one side. Either suppress the earlier standalone text, or clear the final media caption/text when the final payload came from the same text+MEDIA directive.
  3. Carry stable logical reply/media identity across block/final/source-channel handoffs. Then adapters can safely downgrade the later duplicate to already-sent without relying on text-only hasSentPayload heuristics.

Suggested test

Add a regression that exercises the exact MEDIA: directive path, not just { text, mediaUrl } payloads:

  • input assistant content: hello\n\nMEDIA:/tmp/a.txt
  • block/intermediate delivery enabled
  • source-channel/channel.message final delivery enabled
  • assert final observable send plan contains:
    • one visible hello text, and
    • one /tmp/a.txt media send,
    • but no second hello caption/text.

The existing test keeps unsent final media after block pipeline streamed the text is close, but it starts from a structured { text, mediaUrl } payload. The bug is in the legacy MEDIA: directive split where block delivery does not parse media directives and final delivery does.

Activity

  1. clawsweeper commented on Jun 10, 2026

    @clawsweeper
    Contributor

    Codex review: keeping this open for maintainer follow-up; there is still a little grit to resolve. Reviewed June 10, 2026, 3:40 AM ET / 07:40 UTC.

    Summary
    Keep open: current main still has the asymmetric MEDIA directive handling described in the report, and the open design follow-up tracks the broader channel.message contract rather than replacing this narrower duplicate-text bug.

    Reproducibility: yes. for source-level reproduction: current main still has block delivery that skips MEDIA extraction, final delivery that parses MEDIA, and outbound media delivery that uses remaining text as caption. I did not execute a live Bncr transport repro in this read-only review.

    Next step
    This is a narrow source-reproducible delivery bug with clear likely files and a focused regression-test shape; the linked design issue should remain separate.

    Review details

    Best possible solution:

    Fix the shared reply payload/dedupe path so a text+MEDIA directive whose visible text was already sent delivers the attachment without reusing that text as a second caption, while leaving the broader channel.message payload contract to the linked design issue.

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

    Yes for source-level reproduction: current main still has block delivery that skips MEDIA extraction, final delivery that parses MEDIA, and outbound media delivery that uses remaining text as caption. I did not execute a live Bncr transport repro in this read-only review.

    Is this the best way to solve the issue?

    Yes for the narrow bug: the maintainable fix belongs in shared reply payload construction or block/final dedupe before transport delivery, not in a Bncr-specific workaround. The broader choice to delegate logical text+media physical sending to plugins is separately tracked in #91867.

    AGENTS.md: found and applied where relevant.

    Remaining risk / open question:

    Codex review notes: model gpt-5.5, reasoning high; reviewed against 7f1d82ab2518.

    Label changes

    Label justifications:

    • P2: The report is a normal-priority user-visible channel delivery bug with clear source evidence and bounded blast radius.
    • impact:message-loss: The issue is about duplicated visible channel text during mixed text-plus-media reply delivery.
    Evidence reviewed

    Acceptance criteria:

    • node scripts/run-vitest.mjs src/auto-reply/reply/agent-runner-payloads.test.ts -t "MEDIA|block pipeline"
    • node scripts/run-vitest.mjs src/auto-reply/reply/block-reply-pipeline.test.ts
    • node scripts/run-vitest.mjs src/infra/outbound/deliver.test.ts -t "media|sendPayload"
    • node scripts/run-vitest.mjs src/infra/outbound/payloads.test.ts src/infra/outbound/message-plan.test.ts

    What I checked:

    • Root policy read: Read the full root AGENTS.md and applied its source/history/test/owner-boundary review guidance; also read scoped channel and outbound AGENTS.md files for the touched surfaces. (AGENTS.md:1, 7f1d82ab2518)
    • Block replies still skip MEDIA extraction: On current origin/main, createBlockReplyDeliveryHandler calls normalizeReplyPayloadDirectives with parseMode auto and extractMediaDirectives false, so streamed/block text containing MEDIA remains text-only at this stage. (src/auto-reply/reply/reply-delivery.ts:123, 7f1d82ab2518)
    • Final payloads still parse MEDIA directives: Final reply payload construction calls normalizeReplyPayloadDirectives with parseMode always before media normalization, so the same raw final text can become text plus media fields later in the turn. (src/auto-reply/reply/agent-runner-payloads.ts:223, 7f1d82ab2518)
    • Current dedupe depends on text-only block evidence: preserveUnsentMediaAfterBlockStream only clears final media text when blockReplyPipeline.hasSentPayload matches a text-only version of the payload; this is the narrow heuristic the issue says can miss the channel.message split path. (src/auto-reply/reply/agent-runner-payloads.ts:335, 7f1d82ab2518)
    • Outbound media planning still reuses remaining text as caption: Ordinary media payload delivery plans media units with caption: payloadSummary.text and sends that caption through sendFormattedMedia/sendMedia, so uncleared final text can be emitted again around the attachment. (src/infra/outbound/deliver.ts:1876, 7f1d82ab2518)
    • Existing tests cover nearby structured media, not the raw directive split: The closest regression keeps unsent final media after streamed text for a structured { text, mediaUrl } payload; the report asks for raw text plus MEDIA directive coverage through block/intermediate and final channel.message delivery. (src/auto-reply/reply/agent-runner-payloads.test.ts:519, 7f1d82ab2518)

    Likely related people:

    • Shakker: Available git blame attributes the current MEDIA directive parse asymmetry, block dedupe heuristic, and outbound caption planning lines to commit ef2ca9e. (role: recent current-line provenance; confidence: medium; commits: ef2ca9e50eaf; files: src/auto-reply/reply/reply-delivery.ts, src/auto-reply/reply/agent-runner-payloads.ts, src/infra/outbound/deliver.ts)
    • Peter Steinberger: git shortlog and recent path history show the most commits across the relevant reply/outbound payload files, including outbound media and reply-payload refactors adjacent to this behavior. (role: adjacent outbound/reply-payload contributor; confidence: medium; commits: cf3c1994dc06, 570fb5594c72, 62edfdffbdd0; files: src/infra/outbound/deliver.ts, src/infra/outbound/message-plan.ts, src/plugin-sdk/reply-payload.ts)
    • Josh Avant: History on the implicated outbound payload files includes the central payload normalization plan commit, which is adjacent to delivery projection and media/text send planning. (role: adjacent payload-normalization contributor; confidence: low; commits: c4764095f859; files: src/infra/outbound/deliver.ts, src/infra/outbound/payloads.ts)
    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.
  2. added
    P2Normal backlog priority with limited blast radius.
    impact:message-lossChannel message delivery can be lost, duplicated, or misrouted.
    issue-rating: 🦞 diamond lobsterVery strong issue quality with high-confidence source-level or clear reproduction.
    clawsweeper:source-reproClawSweeper found a high-confidence source-level issue reproduction.
    clawsweeper:queueable-fixClawSweeper marked this issue as an existing queue_fix_pr work candidate.
    clawsweeper:fix-shape-clearClawSweeper found a clear likely implementation shape for this issue.
    on Jun 10, 2026
  3. xmoxmo commented on Jun 10, 2026

    @xmoxmo
    Author

    Additional live reproduction and a shorter chain summary from the latest Bncr/channel.message test.

    Test environment:

    OpenClaw 2026.6.5 (5181e4f)
    @xmoxmo/bncr 0.3.6
    chat_id: Bncr:tgBot:-1003776014601:6278285192
    source message_id: 1781073031040
    

    Test final reply shape:

    验证 channel.message 小附件 + 文本。
    如果现象仍在,你可能会看到文字被单独出现一次,同时附件消息/附件 caption 又带一次相同文字。
    
    MEDIA:/root/.openclaw/workspace/tmp/channel-message-phenomenon-test-20260610-1416.txt
    

    Observed Bncr/Gateway log around the test:

    2026-06-10T14:20:58+08:00 [bncr] outbound text|Bncr:tgBot:-1003776014601:6278285192|验证 chann…
    2026-06-10T14:20:58+08:00 [bncr] outbox push mid=f993ee59-7378-4561-bd3f-9bab48dab0bf|q=1
    2026-06-10T14:20:58+08:00 [bncr] outbound message.outbound|Bncr:tgBot:-1003776014601:6278285192|-
    2026-06-10T14:21:00+08:00 [bncr] outbox ack ok mid=f993ee59-7378-4561-bd3f-9bab48dab0bf|q=1|queueMs=1475|pushMs=1472
    2026-06-10T14:21:00+08:00 [bncr] outbound text|Bncr:tgBot:-1003776014601:6278285192|验证 chann…
    2026-06-10T14:21:00+08:00 [bncr] outbox push mid=fd979ff9-e361-4a33-8f22-5bb745ab5690|q=3
    2026-06-10T14:21:01+08:00 [bncr] outbox ack ok mid=fd979ff9-e361-4a33-8f22-5bb745ab5690|q=2|queueMs=2962|pushMs=912
    2026-06-10T14:21:02+08:00 [bncr] outbox push mid=37f5e7bd-dfda-465c-a3c0-6ca1f938fc65|q=2
    2026-06-10T14:21:03+08:00 [bncr] outbox ack ok mid=37f5e7bd-dfda-465c-a3c0-6ca1f938fc65|q=1|queueMs=2646|pushMs=1136
    2026-06-10T14:21:03+08:00 [bncr] outbox push mid=e625d288-191b-49d5-82e3-15330c56ef5c|q=1
    2026-06-10T14:21:04+08:00 [bncr] outbox ack ok mid=e625d288-191b-49d5-82e3-15330c56ef5c|q=0|queueMs=4219|pushMs=1066
    

    The important part is this sequence:

    14:20:58 outbound text        # first visible text copy
    14:20:58 outbound message.outbound
    14:21:00 outbound text        # same visible text copy again
    

    All outbox messages ACK successfully, so this is still not an ACK retry or adapter retry.

    The clearest chain summary is:

    final reply text + MEDIA directive
    → channel.message / message.outbound split
    → one path sends the text first
    → the final/media handoff path still carries or re-emits the same text around the attachment
    → user sees duplicated text
    

    So the bug is best described as a cross-layer invariant break: once a logical final reply containing text + MEDIA is split into source-channel/channel.message deliveries, the visible text is not consistently marked as already-sent or removed from the later media handoff. The fix should ensure that this logical reply produces exactly one visible text unit plus the attachment, regardless of whether the attachment travels through message.outbound, source-channel delivery, or the final media/caption path.

  4. xmoxmo commented on Jun 10, 2026

    @xmoxmo
    Author

    One more design clarification from the adapter/plugin side.

    I do not think this means channel.message should be removed. It is useful as the runtime delivery contract for final replies, reply/thread context, durable final delivery, receipts, and channel capabilities.

    The concern is that the backend send planning for channel.message should not diverge from the already stable message.send / plugin send semantics by eagerly turning one logical message into separate physical text/media sends in core.

    A better invariant would be:

    channel.message remains the runtime delivery entry point
    → it preserves one logical payload: { text, mediaUrls, replyTo, thread, formatting, silent, ... }
    → it hands that logical payload to the channel adapter/plugin send backend
    → the plugin decides whether to send text+media together, split them, truncate caption, group media, or use platform-specific behavior
    

    This matters for Bncr in particular because Bncr is a bridge to many downstream platforms. Some downstream platforms support media + caption/summary as one message, while others require text and attachment to be split. Bncr's backend can already make that platform-specific decision. If OpenClaw core splits text and media before Bncr sees the logical payload, it removes that choice and can make capable platforms behave worse.

    In other words, the issue is not only duplicated text. The deeper delivery-contract problem is:

    core currently appears to expand one logical final reply into physical send steps too early
    

    For a final reply like:

    summary text
    
    MEDIA:/path/file.txt
    

    the adapter should ideally receive one logical text + media payload and decide:

    • send as one media-with-caption message when the backend/platform supports it;
    • split into text then media when the backend/platform requires it;
    • trim or move caption when platform caption limits require it;
    • group or sequence multi-media messages according to backend capabilities.

    So the preferred fix shape is not just "clear captions somewhere". The more robust fix is to make channel.message preserve logical message shape and reuse the same stable plugin/backend sending semantics as message.send / sendPayload where possible, leaving physical splitting policy to the channel plugin.

  5. xmoxmo commented on Jun 10, 2026

    @xmoxmo
    Author

    I opened a separate design issue for the broader delivery-contract concern so this bug can stay focused on the concrete duplicate-text reproduction:

    Short version: channel.message should remain the runtime delivery contract, but it should preserve logical text + media payload shape as long as possible and let the channel plugin/backend choose the physical send shape, instead of core eagerly splitting text/media before adapter-specific handling.

  6. vincentkoc commented on Jun 10, 2026

    @vincentkoc
    Member

    Fixed on main in aeb537c.

    The final reply now carries assistant-message identity and compares against successfully delivered direct block payloads, preserving emission order and meaningful whitespace. Already-sent text/media is removed while genuinely new final attachments remain deliverable.

    Verification:

    • 98 focused tests passed across embedded payload construction and reply delivery.
    • Fresh autoreview: clean, no accepted/actionable findings.
    • Azure Crabbox check:changed passed on lease cbx_77c61f6b4a29 (blue-hermit).
  7. locked as resolved and limited conversation to collaborators on Jun 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Normal backlog priority with limited blast radius.clawsweeper:fix-shape-clearClawSweeper found a clear likely implementation shape for this issue.clawsweeper:queueable-fixClawSweeper marked this issue as an existing queue_fix_pr work candidate.clawsweeper:source-reproClawSweeper found a high-confidence source-level issue reproduction.impact:message-lossChannel message delivery can be lost, duplicated, or misrouted.issue-rating: 🦞 diamond lobsterVery strong issue quality with high-confidence source-level or clear reproduction.

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions