Skip to content

fix(agentflow): resolve stale $flow.state reads in iteration node#6099

Merged
HenryHengZJ merged 7 commits into
FlowiseAI:mainfrom
manojnaidu619:bugfix/fix-iteration-stale-flow-state
Apr 3, 2026
Merged

fix(agentflow): resolve stale $flow.state reads in iteration node#6099
HenryHengZJ merged 7 commits into
FlowiseAI:mainfrom
manojnaidu619:bugfix/fix-iteration-stale-flow-state

Conversation

@manojnaidu619

Copy link
Copy Markdown
Contributor

This PR fixes a runtime state sync issue inside Iteration node execution in Agentflow.

When a node inside an iteration updates $flow.state, the next node in the same iteration could read an older state value instead of the latest one.

Screenshot 2026-03-31 at 7 54 47 PM

Problem

Inside an Iteration block, later nodes still read the old flow state values even after an earlier node had already updated it in that same iteration.

Expected behavior:

  • If node A updates $flow.state, node B (next in sequence) should read the updated value in that same iteration.

Actual behavior:

  • Node B can read a stale value from the previous step.
  • This causes state-driven logic to run one step behind.

This is a general issue and can affect any node that reads $flow.state inside an iteration block.

Root Cause

During recursive execution for iteration children, state is merged from two sources:

  • current runtime state (updatedState)
  • iteration context state snapshot (iterationContext.agentflowRuntime.state)

The previous merge order gave priority to the iteration snapshot, which can be stale for the current execution step.
As a result, fresh updates could be overwritten before variable resolution in downstream nodes.

Solution

Reversed merge precedence so current runtime state wins.

Simple idea:

  • use iteration context state as baseline
  • keep updatedState as source of truth

This ensures the latest $flow.state value is preserved and available to subsequent nodes in the same iteration.

How I verified

  • Built an iteration flow where one child node updates $flow.state.counter and the next child node reads it.
  • Before fix: downstream node read stale state.
  • After fix: downstream node reads the latest updated state in the same iteration.

Demo

Before

Before.fix.mp4

After

After.fix.mp4

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request modifies the state merging logic in buildAgentflow.ts by reversing the spread order of updatedState and iterationContext.agentflowRuntime.state. Feedback suggests that while this change addresses variable resolution, the agentflowRuntime.state object remains stale for nodes that access it directly, and should be updated to ensure consistency across the execution flow.

Comment thread packages/server/src/utils/buildAgentflow.ts

@HenryHengZJ HenryHengZJ 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.

thank you!

@HenryHengZJ HenryHengZJ merged commit 2827e01 into FlowiseAI:main Apr 3, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Passing State in Iteration [BUG] State Updates not persisting across iterations

2 participants