fix(agentflow): resolve stale $flow.state reads in iteration node#6099
Merged
HenryHengZJ merged 7 commits intoApr 3, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
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.
This was
linked to
issues
Apr 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR fixes a runtime state sync issue inside
Iterationnode 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.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:
$flow.state, node B (next in sequence) should read the updated value in that same iteration.Actual behavior:
This is a general issue and can affect any node that reads
$flow.stateinside an iteration block.Root Cause
During recursive execution for iteration children, state is merged from two sources:
updatedState)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:
updatedStateas source of truthThis ensures the latest
$flow.statevalue is preserved and available to subsequent nodes in the same iteration.How I verified
$flow.state.counterand the next child node reads it.Demo
Before
Before.fix.mp4
After
After.fix.mp4