Repository navigation
fix(agent): prevent double reply when Agent node is followed by Message node - #17233
Conversation
…mponent When the canvas layout is Agent → Message, the Agent component streams its answer via StreamCallback during the ReAct loop, and the Message component then re-emits the same content via EmitCanvasMessage / EmitAgentMessage. This causes the user to see the answer twice. Fix: guard the Message component's emission with AgentMessageEventsEmitted so it skips SSE output when an upstream Agent already streamed. The rendered content is still stored in outputs["content"] for state persistence and the service-layer answer collector.
📝 WalkthroughWalkthroughThe Message component now skips rendering when agent message events already exist in the current context. It continues storing rendered content and performing subsequent processing. A unit test verifies preserved output content and suppressed duplicate emissions. ChangesMessage emission guard
Estimated code review effort: 2 (Simple) | ~10 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@internal/agent/component/message.go`:
- Around line 219-225: Replace the broad runtime.AgentMessageEventsEmitted(ctx)
check in the rendered-content emission guard with a source-specific marker that
records only upstream Agent answer content streamed during this invocation.
Ensure thinking-only Agent deltas do not suppress the first actual answer, and
Message-node emissions do not mark themselves as upstream Agent content; add
regression coverage for both scenarios.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 313867f8-c8d8-4fbb-84e3-807d5bde2282
📒 Files selected for processing (2)
internal/agent/component/message.gointernal/agent/component/message_test.go
| // Skip emission when an upstream Agent component already streamed its | ||
| // answer via the agent message emitter. Without this guard the user sees | ||
| // the same content twice: once from the Agent's StreamCallback during the | ||
| // ReAct loop, and again from the Message node's own emit call here. | ||
| // The rendered content is still stored in outputs["content"] for state | ||
| // persistence and the service-layer answer collector. | ||
| if rendered != "" && !runtime.AgentMessageEventsEmitted(ctx) { |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift
Use a source-specific emission marker here.
runtime.AgentMessageEventsEmitted(ctx) is set for thinking-only Agent deltas and by EmitCanvasMessage itself (internal/agent/runtime/context.go:106-142). Therefore this guard can suppress the first actual answer after a thinking-only Agent event, or suppress a later standalone Message after an earlier Message emitted in the same invocation. Track upstream Agent content separately, and add regression coverage for both cases.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@internal/agent/component/message.go` around lines 219 - 225, Replace the
broad runtime.AgentMessageEventsEmitted(ctx) check in the rendered-content
emission guard with a source-specific marker that records only upstream Agent
answer content streamed during this invocation. Ensure thinking-only Agent
deltas do not suppress the first actual answer, and Message-node emissions do
not mark themselves as upstream Agent content; add regression coverage for both
scenarios.
wangq8
left a comment
There was a problem hiding this comment.
LGTM. Nice fix with a good test.
The guard !runtime.AgentMessageEventsEmitted(ctx) is consistent with the existing pattern — the Agent component already checks this at agent.go:274 and agent.go:683. The Message node was the missing piece.
Key points:
- The rendered content is still stored in
outputs["content"], so state persistence and the service-layer answer collector are unaffected ✅ - The standalone Message node (no upstream Agent) still emits normally since
AgentMessageEventsEmittedwill befalsein that case ✅ - Test coverage verifies both the skip-when-emitted and normal-emission paths ✅
No regressions. Well done.
Summary
When the canvas layout is Agent → Message, the agent replies twice: the Agent component streams its answer via
StreamCallbackduring the ReAct loop, and the downstream Message component then re-emits the same content throughEmitCanvasMessage/EmitAgentMessage. This is particularly noticeable withqwen-flashbut affects all models.Root cause: In
internal/agent/component/message.go, the Message node unconditionally emits the rendered content to SSE without checking whether an upstream Agent already streamed its answer.Fix: Guard the Message component's emission with
runtime.AgentMessageEventsEmitted(ctx). When an upstream Agent has already streamed, the Message node skips SSE output. The rendered content is still stored inoutputs["content"]for state persistence and the service-layer answer collector, so canvas history and conversation continuity are unaffected.The standalone Message node (without a preceding Agent) continues to emit normally.