I have a Code node, “Loop State,” that receives two incoming connections into the same input: one from “Assemble Full Draft” (the initial pass) and one from “Prep Loop Back” (fed back after a Humanize Draft → AI Score check loop, further downstream).
Inside Loop State I use $input.first().json combined with a $runIndex === 0 check to decide which branch’s data I’m receiving — treating run 0 as the initial pass and any later run as a loop pass from Prep Loop Back. In practice, on loop passes, $input.first().json doesn’t reliably reflect Prep Loop Back’s output — it appears to still carry data associated with the earlier branch, and I lose access to what Prep Loop Back actually returned.
Is there a reliable way, in a manually-built cyclic workflow like this (loop-back connection to an earlier node, not a Split-In-Batches loop), to guarantee a node picks up data from the correct upstream branch on each pass — without depending on $runIndex or input-merge ordering?
Also when ‘loop state’ node run after the execution of ‘prep loop back’ node it clears existing output of all the nodes after ‘loop state’ node, so i am not able to get what the ‘prep loop back’ node returned
Hey karandtech!
I think the main issue is that Loop State has two branches feeding the same input, so “$input.first() + $runIndex” becomes kinda fragile here.
I wouldn’t rely on run order to tell the branches apart, and I’d probably avoid feeding the manual loop back into the same node input at all, keeping the loop path isolated instead, possibly in a subworkflow.
That should also make it easier to preserve the actual Prep Loop Back output instead of depending on execution history.
I’d make the branch identity explicit in the data rather than infer it from $runIndex.
As a diagnostic test, immediately before Loop State you could tag each path with something like:
source = “initial”
source = “loop_back”
and make sure Prep Loop Back carries forward the state you actually need. Then Loop State can inspect the incoming payload’s source field instead of depending on execution order/history.
I’d also avoid relying on the old downstream node outputs as persistent loop state. When an earlier node runs again, those downstream execution views can be replaced/cleared as the new path executes, so keeping the required state inside the payload itself should make the behavior much easier to trace.
Before restructuring anything, I’d capture the input JSON arriving at Loop State on:
the initial pass
the first loop-back pass
That comparison should show whether the wrong branch is actually arriving, or whether the expected state is being lost earlier in Prep Loop Back.
Don’t rely on “$runIndex” for this. In a manual loop-back, use “$prevNode.name” to identify which node triggered the current run, and pass the complete loop state forward in the item itself. Also, don’t depend on previous downstream node outputs, because n8n may invalidate/clear them when the earlier node executes again.