Skip to content

fix(agent): prevent double reply when Agent node is followed by Message node - #17233

Merged
wangq8 merged 1 commit into
infiniflow:mainfrom
euvre:fix/message-double-reply-agent-qwen-flash
Jul 22, 2026
Merged

wangq8 merged 1 commit into
infiniflow:mainfrom
euvre:fix/message-double-reply-agent-qwen-flash

Conversation

@euvre

@euvre euvre commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Summary

When the canvas layout is Agent → Message, the agent replies twice: the Agent component streams its answer via StreamCallback during the ReAct loop, and the downstream Message component then re-emits the same content through EmitCanvasMessage / EmitAgentMessage. This is particularly noticeable with qwen-flash but 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 in outputs["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.

…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.
@dosubot dosubot Bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Jul 22, 2026
@euvre euvre added the ci Continue Integration label Jul 22, 2026
@coderabbitai

coderabbitai Bot commented Jul 22, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The 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.

Changes

Message emission guard

Layer / File(s) Summary
Guard duplicate emissions and validate behavior
internal/agent/component/message.go, internal/agent/component/message_test.go
MessageComponent.Invoke checks runtime.AgentMessageEventsEmitted(ctx) before emitting rendered content; the test confirms content is retained while no additional events are emitted.

Estimated code review effort: 2 (Simple) | ~10 minutes

Poem

I’m a bunny with a streaming tale,
No duplicate echoes down the trail.
Content stays tucked in the state,
While fresh events wait by the gate.
Hop, hop—clean output feels great!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main fix: preventing duplicate replies when an Agent node is followed by a Message node.
Description check ✅ Passed The description matches the template and gives the summary, background, root cause, fix, and behavior impact.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@euvre
euvre requested a review from wangq8 July 22, 2026 06:21

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between bab7302 and c113274.

📒 Files selected for processing (2)
  • internal/agent/component/message.go
  • internal/agent/component/message_test.go

Comment on lines +219 to +225
// 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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 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 wangq8 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 AgentMessageEventsEmitted will be false in that case ✅
  • Test coverage verifies both the skip-when-emitted and normal-emission paths ✅

No regressions. Well done.

@wangq8
wangq8 merged commit e0ae22e into infiniflow:main Jul 22, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci Continue Integration size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants