dify icon indicating copy to clipboard operation
dify copied to clipboard

Agent V2 node inside an Iteration fails to publish with "references non-upstream previous node" when referencing iteration.item or outer upstream variables

Open wazgii opened this issue 1 month ago • 4 comments

Self Checks

  • [x] I have read the Contributing Guide and Language Policy.
  • [x] This is only for bug report, if you would like to ask a question, please head to Discussions.
  • [x] I have searched for existing issues search for existing issues, including closed ones.
  • [x] I confirm that I am using English to submit this report, otherwise it will be closed.
  • [x] 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :)
  • [x] Please do not modify this template :) and fill in all the required fields.

Dify version

1.17.1

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

  1. Create a Workflow with a Start node and an Iteration node.
  2. Inside the Iteration node, add a v2 Agent node (dify_agent / roster_agent).
  3. Connect iteration-start -> Agent node.
  4. In the Agent node's prompt (workflow_prompt), reference either:
    • The iteration variable {{#<iteration_node_id>.item#}}, OR
    • An outer upstream node variable (e.g., {{#<start_node_id>.input#}}).
  5. Click Publish.

✔️ Expected Behavior

The workflow should publish successfully, just like LLM, Code, or Template nodes inside an Iteration container can natively reference iteration.item and outer upstream variables.

❌ Actual Behavior

❌ Actual Behavior

Publishing fails with a toast error: Workflow Agent node <agent_node_id> references non-upstream previous node <iteration_node_id / start_node_id>.

🔍 Root Cause Analysis (For Developers)

When validating agent_job.previous_node_output_refs on a v2 Agent node placed inside an Iteration (or Loop) subgraph:

  • The validator only traverses edges backward within the subgraph, stopping at <iteration_id>start (type: custom-iteration-start).
  • It fails to include the parent container node (<iteration_id>) itself (for .item / .index) and fails to traverse across the parent Iteration node's incoming edges in the root graph. Current Workaround: Insert a Code or Template node right after iteration-start to relay iteration.item and outer variables into the Agent node.

wazgii avatar Oct 25 '26 01:10 wazgii

Hi @wazgii! I'm Dosu and I'm helping the Dify team.

Your diagnosis matches the validation path in the current checkout: WorkflowAgentNodeValidator.validate_node_job() raises this exact error when topology.is_upstream() returns false. The shared helper, WorkflowGraphTopology, builds ancestry only from explicit edges[].source/target links. It does not incorporate container membership, so the graph shape you described leaves the parent Iteration and its outer predecessors unreachable from the Agent's backward traversal. [1] [2]

Related PR #39192 relaxed checks for draft saves only. It deliberately retained strict publish validation, so it does not resolve this container-boundary case. [3]

For the relay workaround, make sure the Agent prompt references only the relay node's outputs, not the original iteration.item or Start selectors. That gives the validator an explicit upstream edge path to the referenced producer. This follows from the validator's logic; I haven't executed your workflow here. [1] [4]

To reply, just mention @dosu.


Docs are dead. Just use Dosu.

Leave Feedback Ask Dosu about dify

dosubot[bot] avatar Oct 25 '26 01:10 dosubot[bot]

test1349.yml Image Here is the reproducible DSL (test1349.yml) and the error screenshot when clicking Publish:

wazgii avatar Oct 25 '26 01:10 wazgii

I'd like to take this one. I verified the defect is still present on current main, checked that no open PR covers it, and prepared a minimal patch with an offline check. Opening a focused PR from my fork now.

severus0044 avatar Nov 08 '26 08:11 severus0044

Confirmed on Dify 1.19.0, Self Hosted (Docker). This still blocks publishing a workflow that uses a New Agent inside a parallel Iteration.

The Agent task references the enclosing Iteration's current item, inserted through the / variable picker. The item is a small service object (namespace/deployment). Draft saving succeeds, but Publish fails with:

Workflow Agent node <agent_node_id> references non-upstream previous node <iteration_node_id>.

The referenced node is the Agent's parent Iteration. The error occurs during publication, before model/tool execution. Sequential iteration has not been tested in this environment.

Documentation supporting the expected behavior:

  • Iteration documents that internal nodes can reference the current item and its index.
  • New Agent node documents / references in the Agent task. The item here is well below the documented 2,000-character limit.

1.19.0 source verification:

  • Agent publish validator: publication validates previous-node references using topology.is_upstream(...); draft validation skips the topology check.
  • Graph topology helper: ancestry is built from explicit edges, without enclosing-container membership.

Using the actual 1.19.0 helper with a reduced parent Iteration / internal Start / Agent graph reproduces is_upstream(parent_iteration, internal_agent) == False. An internal Code -> Agent edge is recognized as upstream. The relay workaround has only been checked at the topology-helper level here; its full publication/execution has not yet been tested.

Adding this 1.19.0 confirmation to the existing issue for tracking alongside #45825.

qkblog avatar Nov 11 '26 03:11 qkblog