Repository navigation
Conversation
…enclaw#19932) Buffer all onBlockReply payloads during each assistant turn; at message_end, discard the buffer when stopReason === "toolUse" (tool-calling turn) or flush it when stopReason === "stop" (final answer). Handles both text_end (streaming) and message_end (non-streaming) delivery paths. Adds EmbeddedPiSubscribeState.suppressPreToolText (enabled whenever onBlockReply is registered) and pendingBlockReplies buffer. Both the outer emitBlockReplySafely in subscribeEmbeddedPiSession (streaming/text_end path) and the local one in handleMessageEnd (message_end path) buffer through pendingBlockReplies. Regression test covers text_end mode (streaming), message_end mode, and multiple intermediate turns all suppressed before a single final delivery. Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
…scard (openclaw#19932) The previous suppression fix buffered onBlockReply calls and discarded them for toolUse turns — preventing intermediate text from being streamed to Mattermost as separate messages. But the texts were already pushed to state.assistantTexts in emitBlockChunk before the stopReason was known. This meant they survived in the final reply payload (hasSentPayload deduplication can't filter them because they never touched the pipeline). Fix: capture preTurnAssistantTextBaseline at the start of handleMessageEnd (before finalizeAssistantTexts updates it), and when discarding for toolUse, splice assistantTexts back to that baseline and reset the baseline. Adds a regression test verifying intermediate texts are absent from assistantTexts after a toolUse turn.
Greptile SummaryThis PR fixes intermediate assistant text leaking as standalone unthreaded Mattermost messages during multi-step tool-call sequences. It implements a two-layer buffering approach: all Key changes:
Note on behavioral scope: Confidence Score: 4/5
Prompt To Fix All With AIThis is a comment left during a code review.
Path: src/agents/pi-embedded-subscribe.suppress-pre-tool-text.test.ts
Line: 1-10
Comment:
**Misplaced JSDoc block comment**
The module-level JSDoc comment is placed between the first `import` statement and the rest of the imports. Most linters/formatters expect the file-level doc comment to appear before all imports, or alternatively the imports to be grouped before the comment. Consider moving it before line 1:
```suggestion
/**
* Tests for suppressPreToolText behavior: intermediate text blocks written
* between tool calls must NOT be delivered via onBlockReply.
*
* Regression test for the Mattermost intermediate-text leak:
* https://github.com/openclaw/openclaw/pull/19932
*/
import type { AssistantMessage } from "@mariozechner/pi-ai";
import { describe, expect, it, vi } from "vitest";
import { createSubscribedSessionHarness } from "./pi-embedded-subscribe.e2e-harness.js";
```
How can I resolve this? If you propose a fix, please make it concise.Last reviewed commit: 35716ff |
| import type { AssistantMessage } from "@mariozechner/pi-ai"; | ||
| /** | ||
| * Tests for suppressPreToolText behavior: intermediate text blocks written | ||
| * between tool calls must NOT be delivered via onBlockReply. | ||
| * | ||
| * Regression test for the Mattermost intermediate-text leak: | ||
| * https://github.com/openclaw/openclaw/pull/19932 | ||
| */ | ||
| import { describe, expect, it, vi } from "vitest"; | ||
| import { createSubscribedSessionHarness } from "./pi-embedded-subscribe.e2e-harness.js"; |
There was a problem hiding this comment.
Misplaced JSDoc block comment
The module-level JSDoc comment is placed between the first import statement and the rest of the imports. Most linters/formatters expect the file-level doc comment to appear before all imports, or alternatively the imports to be grouped before the comment. Consider moving it before line 1:
| import type { AssistantMessage } from "@mariozechner/pi-ai"; | |
| /** | |
| * Tests for suppressPreToolText behavior: intermediate text blocks written | |
| * between tool calls must NOT be delivered via onBlockReply. | |
| * | |
| * Regression test for the Mattermost intermediate-text leak: | |
| * https://github.com/openclaw/openclaw/pull/19932 | |
| */ | |
| import { describe, expect, it, vi } from "vitest"; | |
| import { createSubscribedSessionHarness } from "./pi-embedded-subscribe.e2e-harness.js"; | |
| /** | |
| * Tests for suppressPreToolText behavior: intermediate text blocks written | |
| * between tool calls must NOT be delivered via onBlockReply. | |
| * | |
| * Regression test for the Mattermost intermediate-text leak: | |
| * https://github.com/openclaw/openclaw/pull/19932 | |
| */ | |
| import type { AssistantMessage } from "@mariozechner/pi-ai"; | |
| import { describe, expect, it, vi } from "vitest"; | |
| import { createSubscribedSessionHarness } from "./pi-embedded-subscribe.e2e-harness.js"; |
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/pi-embedded-subscribe.suppress-pre-tool-text.test.ts
Line: 1-10
Comment:
**Misplaced JSDoc block comment**
The module-level JSDoc comment is placed between the first `import` statement and the rest of the imports. Most linters/formatters expect the file-level doc comment to appear before all imports, or alternatively the imports to be grouped before the comment. Consider moving it before line 1:
```suggestion
/**
* Tests for suppressPreToolText behavior: intermediate text blocks written
* between tool calls must NOT be delivered via onBlockReply.
*
* Regression test for the Mattermost intermediate-text leak:
* https://github.com/openclaw/openclaw/pull/19932
*/
import type { AssistantMessage } from "@mariozechner/pi-ai";
import { describe, expect, it, vi } from "vitest";
import { createSubscribedSessionHarness } from "./pi-embedded-subscribe.e2e-harness.js";
```
How can I resolve this? If you propose a fix, please make it concise.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 35716ff3e6
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| successfulCronAdds: 0, | ||
| pendingMessagingMediaUrls: new Map(), | ||
| deterministicApprovalPromptSent: false, | ||
| suppressPreToolText: Boolean(params.onBlockReply), |
There was a problem hiding this comment.
Keep text_end block replies streaming for non-tool turns
suppressPreToolText is enabled up front for any session with onBlockReply, so all block replies are buffered even before we know whether the turn is a tool-use turn. In blockReplyBreak: "text_end" mode this changes normal stop turns from emitting at text_end to emitting only at message_end, which regresses the existing text_end streaming behavior (for example, src/agents/pi-embedded-subscribe.subscribe-embedded-pi-session.emits-block-replies-text-end-does-not.test.ts expects emission on text_end) and risks dropping content if message_end is missing because pending payloads are later cleared on reset. Buffering should be conditional on detecting a tool-use turn rather than globally enabled.
Useful? React with 👍 / 👎.
|
Closing this as implemented after Codex review. Current main already addresses the Mattermost leak through the newer draft-preview delivery path: Mattermost disables generic block streaming for inbound replies, streams partial/tool/thinking updates into one preview post, and finalizes that post in place. That behavior is documented, covered by Mattermost tests, present in the shipped What I checked:
So I’m closing this as already implemented rather than keeping a duplicate issue open. Review notes: reviewed against 4630ce3d9e2f; fix evidence: release v2026.4.22, commit 00bd2cf7a376. |
Fixes #45134
Summary
When an agent performs multi-step tool calls in
text_endstreaming mode (Mattermost's delivery path), any text written between tool calls leaked as standalone unthreaded messages in the Mattermost channel. This PR implements the buffering approach from #19932.Two-layer fix
Layer 1 — Buffer
onBlockReplyuntilstopReasonis knownAdded to
EmbeddedPiSubscribeState:Both
emitBlockReplySafelyfunctions (outer insubscribe.tsfortext_endmode, local inhandleMessageEndformessage_endmode) now push topendingBlockRepliesinstead of callingonBlockReplyimmediately. Atmessage_end, the buffer is flushed (final turn) or discarded (stopReason === "toolUse").Layer 2 — Remove intermediate texts from
assistantTextsemitBlockChunkpushes text tostate.assistantTextsindependently of theonBlockReplypath. This array is later consumed bybuildEmbeddedRunPayloads. ThehasSentPayloaddeduplication couldn't filter these out because discarded intermediate texts were never enqueued into the pipeline.Fix: capture
preTurnAssistantTextBaselinebeforefinalizeAssistantTextsupdates it, then spliceassistantTextsback when discarding fortoolUse.Tests
New file:
src/agents/pi-embedded-subscribe.suppress-pre-tool-text.test.ts4 tests covering:
text_endmode suppressionmessage_endmode suppressionassistantTextsregression (Layer 2)Known limitation
Text written by the model in the final turn (
stopReason=stop) before the actual answer cannot be suppressed without a model-level signal (e.g.enforceFinalTag). The primary production bug (intermediate tool-call turns leaking) is fully resolved.Related