Skip to content

fix(agentflow): $flow.state inaccessible inside iteration node execution#6142

Open
manojnaidu619 wants to merge 1 commit intoFlowiseAI:mainfrom
manojnaidu619:bugfix/fix-flow-state-access-inside-iteration-node
Open

fix(agentflow): $flow.state inaccessible inside iteration node execution#6142
manojnaidu619 wants to merge 1 commit intoFlowiseAI:mainfrom
manojnaidu619:bugfix/fix-flow-state-access-inside-iteration-node

Conversation

@manojnaidu619
Copy link
Copy Markdown
Contributor

This PR fixes an issue where $flow.state variables are inaccessible (return undefined) inside nodes running within an Iteration block in Agentflow.

Screenshot 2026-04-03 at 8 20 59 PM

Problem

Inside an Iteration block, any node that accesses $flow.state.<variable> (e.g. Custom Function using $flow.state.foo) gets undefined

Expected behavior:

  • $flow.state.<variable> returns the correct value inside nodes within an iteration block.

Actual behavior:

  • $flow.state.<variable> returns undefined because the node's runtime state is an empty object {}.

This affects all node types inside iteration blocks (Custom Function, Agent, LLM, Condition, HTTP, etc.) — not just custom functions.

Root Cause

When an iteration sub-flow is executed recursively, the sub-flow's agentflowRuntime is initialized with an empty state {}. Inside executeNode, the parent's state from iterationContext.agentflowRuntime.state is correctly merged into flowConfig.state (used for template resolution like {{ $flow.state.foo }}), but never synced back to agentflowRuntime.state.

Since every node's run() method reads state from options.agentflowRuntime.state, they all receive the empty object instead of the merged state.

Path Source Used by Status
flowConfig.state Merged with parent state resolveVariables (template {{ }} syntax) Working
agentflowRuntime.state Sub-flow init ({}) node.run() via options.agentflowRuntime.state Broken

Solution

Synced the merged state back to agentflowRuntime.state after the existing iteration context merge in executeNode, so that both flowConfig and agentflowRuntime carry the same resolved state.

How I verified

  • Built an iteration flow with a Custom Function node inside the iteration block.
  • Start node defines a flow state variable foo with no value set.
  • Custom Function reads $flow.state.foo — before fix it was undefined, after fix it reads the correct value.

Demo

Before

Before-fix.mp4

After

After-fix.mov

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

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 updates the buildAgentflow utility to synchronize the agentflowRuntime state during execution. A review comment identifies a potential issue where state overrides might be lost when an iteration context is present, suggesting that flowConfig.state should be used for merging instead of the local updatedState variable to ensure configuration persistence.

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.

1 participant