Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(agent): preserve branch producer dependencies during routing (#20318)

Sbaaoui Idriss47e831a790b5
diff --git a/internal/agent/canvas/multibranch.go b/internal/agent/canvas/multibranch.go
index d8c346e9ac..698e78a9d5 100644
--- a/internal/agent/canvas/multibranch.go
+++ b/internal/agent/canvas/multibranch.go
@@ -128,7 +128,7 @@ func wireMultiBranches(
for n := range endNodes {
endNodesList = append(endNodesList, n)
}
- cond := makeSwitchBranchCondition(endNodes)
+ cond := makeSwitchBranchCondition(endNodes, branchTargetDependencies(c, cpnID, endNodes))
wf.AddBranch(cpnID, compose.NewGraphMultiBranch(cond, endNodes))
out = append(out, branchRegistration{
Parent: cpnID,
@@ -154,10 +154,9 @@ type branchRegistration struct {
// statePost handler has already written to state.Outputs and
// the lambda has returned).
// 2. When `_next` is a []any (list of cpn_ids — Python's Switch
-// can route to multiple targets simultaneously), all entries
-// that are in the endNodes whitelist are returned as the chosen
-// set. This mirrors the Python behavior where Switch's "to"
-// field is a list and every listed cpn_id fires.
+// can route to multiple targets simultaneously), whitelisted
+// entries are returned together with their direct branch
+// dependencies so referenced outputs exist before invocation.
// 3. When `_next` is a string (single target — legacy or default
// path), it is validated against the whitelist and returned as
// a single-entry map.
@@ -165,17 +164,21 @@ type branchRegistration struct {
// contains no whitelisted entries. eino treats an empty chosen
// set as "no successor" — the workflow simply doesn't continue
// past the parent on this path.
-func makeSwitchBranchCondition(endNodes map[string]bool) compose.GraphMultiBranchCondition[map[string]any] {
+func makeSwitchBranchCondition(endNodes map[string]bool, dependencySets ...map[string]map[string]bool) compose.GraphMultiBranchCondition[map[string]any] {
+ var dependencies map[string]map[string]bool
+ if len(dependencySets) > 0 {
+ dependencies = dependencySets[0]
+ }
return func(_ context.Context, in map[string]any) (map[string]bool, error) {
raw, ok := in["_next"]
if !ok {
return nil, nil
}
- chosen := make(map[string]bool, 1)
+ requested := make(map[string]bool, 1)
switch v := raw.(type) {
case string:
if v != "" && endNodes[v] {
- chosen[v] = true
+ requested[v] = true
}
case []string:
for _, s := range v {
@@ -183,7 +186,7 @@ func makeSwitchBranchCondition(endNodes map[string]bool) compose.GraphMultiBranc
continue
}
if endNodes[s] {
- chosen[s] = true
+ requested[s] = true
}
}
case []any:
@@ -193,10 +196,45 @@ func makeSwitchBranchCondition(endNodes map[string]bool) compose.GraphMultiBranc
continue
}
if endNodes[s] {
- chosen[s] = true
+ requested[s] = true
}
}
}
+ chosen := make(map[string]bool, len(requested))
+ for target := range requested {
+ chosen[target] = true
+ for dependency := range dependencies[target] {
+ chosen[dependency] = true
+ }
+ }
return chosen, nil
}
}
+
+// branchTargetDependencies records direct-child dependencies so selecting a
+// target also activates any producer needed by its declared inputs.
+func branchTargetDependencies(c *Canvas, parent string, endNodes map[string]bool) map[string]map[string]bool {
+ dependencies := make(map[string]map[string]bool)
+ var visit func(string, map[string]bool)
+ visit = func(target string, seen map[string]bool) {
+ comp, ok := c.Components[target]
+ if !ok {
+ return
+ }
+ for _, upstream := range comp.Upstream {
+ if upstream == parent || !endNodes[upstream] || seen[upstream] {
+ continue
+ }
+ seen[upstream] = true
+ visit(upstream, seen)
+ }
+ }
+ for target := range endNodes {
+ seen := make(map[string]bool)
+ visit(target, seen)
+ if len(seen) > 0 {
+ dependencies[target] = seen
+ }
+ }
+ return dependencies
+}