Skip to content

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

fix(agent): route failure branches only after errors (#21559)

Sbaaoui Idrisscb7cc48ea5b5
diff --git a/internal/agent/canvas/multibranch.go b/internal/agent/canvas/multibranch.go
index 698e78a9d5..548ca383cd 100644
--- a/internal/agent/canvas/multibranch.go
+++ b/internal/agent/canvas/multibranch.go
@@ -14,7 +14,7 @@
// limitations under the License.
//
-// multibranch.go — runtime branch wiring for Switch / Categorize.
+// multibranch.go — runtime branch wiring for control and exception paths.
//
// Switch and Categorize are control-flow components that produce a
// `_next` output identifying which downstream child should run at
@@ -72,9 +72,8 @@ func isBranchableControl(name string) bool {
return branchableControlNames[strings.ToLower(name)]
}
-// wireMultiBranches registers an eino MultiBranch on every
-// branchable parent that has at least one declared downstream
-// child. Pass-2 already wired AddInput edges from parent to each
+// wireMultiBranches registers an eino MultiBranch on control parents and
+// parents with exception targets. Pass-2 already wired AddInput edges from parent to each
// child; the branch adds the control-only gating so only the
// chosen child fires at runtime.
//
@@ -103,7 +102,8 @@ func wireMultiBranches(
if loopMembers[cpnID] {
continue
}
- if !isBranchableControl(comp.Obj.ComponentName) {
+ exceptions := exceptionTargets(comp.Obj.Params)
+ if !isBranchableControl(comp.Obj.ComponentName) && len(exceptions) == 0 {
continue
}
// Filter downstreams: keep only nodes that exist in the
@@ -111,7 +111,7 @@ func wireMultiBranches(
// children are all inside a loop body has no
// outer-graph routing to install.
endNodes := make(map[string]bool, len(comp.Downstream))
- for _, child := range comp.Downstream {
+ for _, child := range append(append([]string(nil), comp.Downstream...), exceptions...) {
if loopMembers[child] {
continue
}
@@ -129,6 +129,16 @@ func wireMultiBranches(
endNodesList = append(endNodesList, n)
}
cond := makeSwitchBranchCondition(endNodes, branchTargetDependencies(c, cpnID, endNodes))
+ if len(exceptions) > 0 {
+ // Rejoining paths must not pull the opposite exception route into execution.
+ selectNext := makeSwitchBranchCondition(endNodes)
+ cond = func(ctx context.Context, in map[string]any) (map[string]bool, error) {
+ if _, hasNext := in["_next"]; hasNext || isBranchableControl(comp.Obj.ComponentName) {
+ return selectNext(ctx, in)
+ }
+ return selectNext(ctx, map[string]any{"_next": comp.Downstream})
+ }
+ }
wf.AddBranch(cpnID, compose.NewGraphMultiBranch(cond, endNodes))
out = append(out, branchRegistration{
Parent: cpnID,
diff --git a/internal/agent/canvas/node_body.go b/internal/agent/canvas/node_body.go
index 83b4f491c2..19adac2c89 100644
--- a/internal/agent/canvas/node_body.go
+++ b/internal/agent/canvas/node_body.go
@@ -161,7 +161,8 @@ func buildNodeBodyWithOptions(ctx context.Context, cpnID, name, displayName stri
// without the runtime.Component interface needing to expose Name().
// The factory returns the class name as the DSL's `component_name`
// field, which is also what ComponentBase.Name() would have returned.
- return realComponentBodyWithOptions(cpnID, name, displayName, comp, opts), nil
+ body := realComponentBodyWithOptions(cpnID, name, displayName, comp, opts)
+ return withExceptionHandler(cpnID, params, body), nil
}
// Fallback: no factory registered. This path is only exercised by
// canvas-only unit tests; production wiring always installs a
@@ -172,6 +173,55 @@ func buildNodeBodyWithOptions(ctx context.Context, cpnID, name, displayName stri
return placeholderBody(cpnID), nil
}
+func exceptionTargets(params map[string]any) []string {
+ switch targets := params["exception_goto"].(type) {
+ case []string:
+ return targets
+ case []any:
+ return decodeStringSlice(targets)
+ }
+ return nil
+}
+
+func withExceptionHandler(cpnID string, params map[string]any, body nodeBodyFn) nodeBodyFn {
+ return func(ctx context.Context, in map[string]any) (map[string]any, error) {
+ out, invokeErr := body(ctx, in)
+ err := invokeErr
+ if err == nil {
+ if message, _ := out["_ERROR"].(string); message != "" {
+ err = errors.New(message)
+ }
+ }
+ if err == nil || IsInterruptError(err) || errors.Is(err, context.Canceled) || ctx.Err() != nil {
+ return out, err
+ }
+ switch params["exception_method"] {
+ case "goto":
+ if targets := exceptionTargets(params); len(targets) > 0 {
+ if out == nil {
+ out = make(map[string]any)
+ }
+ out["_ERROR"] = err.Error()
+ out["_next"] = targets
+ out["__cpn_id__"] = cpnID
+ return out, nil
+ }
+ case "comment":
+ if value, _ := params["exception_default_value"].(string); value != "" {
+ runtime.EmitCanvasMessage(ctx, value)
+ if out == nil {
+ out = make(map[string]any)
+ }
+ out["content"] = value
+ out["_ERROR"] = err.Error()
+ out["__cpn_id__"] = cpnID
+ return out, nil
+ }
+ }
+ return out, invokeErr
+ }
+}
+
// legacyNoOpBody returns the body installed for DSL v1 sentinel
// components (legacyNoOpNames). It echoes the input and tags
// __legacy_noop__ so downstream debuggers can tell the node fired but
diff --git a/internal/agent/canvas/scheduler.go b/internal/agent/canvas/scheduler.go
index 67e0971625..108fb99554 100644
--- a/internal/agent/canvas/scheduler.go
+++ b/internal/agent/canvas/scheduler.go
@@ -904,6 +904,12 @@ func hasStructuredOutput(params map[string]any) bool {
func resolveComponentExecutionOptions(c *Canvas, cpnID string, name string) runtime.ComponentExecutionOptions {
deferToMessage := directMessageDownstream(c, cpnID)
+ if c != nil {
+ // Exception routing needs the producer's actual result before choosing a path.
+ if method, _ := c.Components[cpnID].Obj.Params["exception_method"].(string); method != "" {
+ deferToMessage = false
+ }
+ }
isAgent := strings.EqualFold(name, "Agent")
isLLM := strings.EqualFold(name, "LLM") || strings.EqualFold(name, "Generate")
deferLLM := false