Skip to content

Agents: suppress block reply delivery for intermediate tool-call messages - #25659

Closed
rrenamed wants to merge 1 commit into
openclaw:mainfrom
rrenamed:fix/block-reply-tool-leak
Closed

rrenamed wants to merge 1 commit into
openclaw:mainfrom
rrenamed:fix/block-reply-tool-leak

Conversation

@rrenamed

@rrenamed rrenamed commented Feb 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Problem: in multi-turn agentic loops, text from intermediate assistant messages (those containing tool calls) leaks to messaging channels via onBlockReply and onPartialReply
  • Why it matters: users on Slack/iMessage/etc see processing narration, error handling text, and tool-call acknowledgments that were never intended as replies
  • What changed: added currentMessageHadToolCalls flag set at tool_execution_start, reset at message_start. Suppresses streaming block reply delivery (emitBlockChunk, onPartialReply) and handleMessageEnd block replies for intermediate messages. A companion currentMessageUsedMessagingTool flag preserves the existing messaging-tool dedup/error-fallback path
  • What did NOT change (scope boundary): assistantTexts recording (text is still captured for final payload assembly), onBlockReplyFlush timing, emitAgentEvent emissions, the final message in a turn (no tool calls, flag reset at message_start)

Change Type (select all)

  • Bug fix
  • Feature
  • Refactor
  • Docs
  • Security hardening
  • Chore/infra

Scope (select all touched areas)

  • Gateway / orchestration
  • Skills / tool execution
  • Auth / tokens
  • Memory / storage
  • Integrations
  • API / contracts
  • UI / DX
  • CI/CD / infra

Linked Issue/PR

User-visible / Behavior Changes

Messaging channel users no longer receive intermediate text from multi-step agentic turns. Only the final response (the message without tool calls) is delivered.

Security Impact (required)

  • New permissions/capabilities? No
  • Secrets/tokens handling changed? No
  • New/changed network calls? No
  • Command/tool execution surface changed? No
  • Data access scope changed? No

Repro + Verification

Environment

  • OS: macOS
  • Runtime/container: Node.js
  • Model/provider: any provider that streams assistant events
  • Integration/channel: Slack, iMessage, or any channel with onBlockReply

Steps

  1. Configure a channel integration (e.g. Slack) with block reply delivery
  2. Send a message that triggers a multi-step agentic loop (e.g. "read file X and summarize it")
  3. Observe messages delivered to the channel

Expected

  • Only the final response is delivered to the channel

Actual

  • Intermediate text ("I'll read that file for you", processing acknowledgments) also appears as separate messages in the channel

Evidence

  • Failing test/log before + passing after
  • Trace/log snippets
  • Screenshot/recording
  • Perf numbers (if relevant)

All 129 pi-embedded-subscribe tests pass. The two tests that exercised the affected paths were updated:

  • calls-onblockreplyflush-before-tool-execution-start-preserve.test.ts: updated to expect onBlockReply not called for buffered text flushed before tool execution (text is still recorded in assistantTexts)
  • suppresses-message-end-block-replies-message-tool.test.ts: passes unchanged, confirming the messaging-tool error fallback path still works

Human Verification (required)

  • Verified scenarios: ran full pi-embedded-subscribe test suite (129 tests, 27 files), pnpm check passes clean
  • Edge cases checked: messaging tool error fallback (test confirms onBlockReply still fires when messaging tool errors), onBlockReplyFlush callback still fires regardless of suppression, assistantTexts recording preserved for final payload assembly, text_end and message_end block reply break modes both handled
  • Verified channel delivery: the e2e test harness exercises the identical subscribeEmbeddedPiSession code path used by live Slack/iMessage/Discord integrations, confirming intermediate text is suppressed and final replies are delivered

Compatibility / Migration

  • Backward compatible? Yes
  • Config/env changes? No
  • Migration needed? No

Failure Recovery (if this breaks)

  • How to disable/revert this change quickly: revert the commit (single commit)
  • Files/config to restore: pi-embedded-subscribe.ts, pi-embedded-subscribe.handlers.messages.ts, pi-embedded-subscribe.handlers.tools.ts, pi-embedded-subscribe.handlers.types.ts
  • Known bad symptoms reviewers should watch for: final replies not appearing in channels (would indicate the flag is not resetting at message_start), messaging tool error fallback not delivering (would indicate currentMessageUsedMessagingTool not being set)

Risks and Mitigations

  • Risk: first text block in text_end mode may still leak if text_end fires before tool_execution_start
    • Mitigation: this is inherent to the streaming protocol (no look-ahead). The fix covers the common case where text is buffered by the chunker or delivered at message_end. A <final> tag approach could address this but is out of scope.

@dsantoreis

This comment was marked as spam.

@hnykda

hnykda commented Mar 12, 2026 •

Copy link
Copy Markdown
Contributor

We've been running this patch against v2026.3.11 and hit an edge case worth flagging.

Issue: resetAssistantMessageState fires at message_start for every assistant message in a multi-step agentic loop — not just at turn boundaries. So currentMessageHadToolCalls resets to false between messages, and text in message N+1 leaks even after tools fired in message N.

Concrete flow:

  1. Message N: text → tool call → currentMessageHadToolCalls = true ✓
  2. Message N+1 starts → resetAssistantMessageState → currentMessageHadToolCalls = false ✗
  3. Text at the start of message N+1 leaks through

Our fix: don't reset these flags in resetAssistantMessageState. Treat them as per-turn (only reset in resetForCompactionRetry + the turn-end reset). That stops the leak across multi-message turns.

@dsantoreis

This comment was marked as spam.

@dsantoreis

This comment was marked as spam.

@dsantoreis

This comment was marked as spam.

@hnykda

hnykda commented Mar 13, 2026

Copy link
Copy Markdown
Contributor

Bu the way, I was trying to make this work, but I couldn't. The thinking is still leaking through, despite various other attempts to patch this and change config. Trying #19932 now.

This si on mattermost btw

@openclaw-barnacle

Copy link
Copy Markdown

This pull request has been automatically marked as stale due to inactivity.
Please add updates or it will be closed.

@openclaw-barnacle openclaw-barnacle Bot added stale Marked as stale due to inactivity and removed stale Marked as stale due to inactivity labels Mar 24, 2026
@openclaw-barnacle

Copy link
Copy Markdown

This pull request has been automatically marked as stale due to inactivity.
Please add updates or it will be closed.

@openclaw-barnacle

Copy link
Copy Markdown

Closing due to inactivity.
If you believe this PR should be revived, post in #pr-thunderdome-dangerzone on Discord to talk to a maintainer.
That channel is the escape hatch for high-quality PRs that get auto-closed.

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

Labels

agents Agent runtime and tooling size: S stale Marked as stale due to inactivity

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Text between tool calls leaks to messaging channels

3 participants