Skip to content

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

fix: accept ReAct completion on the final iteration (#20875)

adws387fdeb3fff0
diff --git a/internal/harness/core/react_loop.go b/internal/harness/core/react_loop.go
index e528fbe805..ec7745a42d 100644
--- a/internal/harness/core/react_loop.go
+++ b/internal/harness/core/react_loop.go
@@ -106,6 +106,7 @@ func (a *ReActAgent[M]) buildReActRunFunc() typedRunFunc[M] {
tn = NewToolsNode[M](tnCfg)
}
+ completed := false
for state.RemainingIterations > 0 {
state.RemainingIterations--
@@ -149,6 +150,7 @@ func (a *ReActAgent[M]) buildReActRunFunc() typedRunFunc[M] {
toolCalls := extractToolCalls(resp)
if len(toolCalls) == 0 || tn == nil {
+ completed = true
break
}
@@ -163,11 +165,12 @@ func (a *ReActAgent[M]) buildReActRunFunc() typedRunFunc[M] {
}
action = act
if action != nil && action.Exit {
+ completed = true
break
}
}
- if state.RemainingIterations <= 0 {
+ if !completed && state.RemainingIterations <= 0 {
p.generator.Send(&TypedAgentEvent[M]{Err: fmt.Errorf("exceeded max iterations (%d)", a.config.MaxIterations)})
}
if a.config.OutputKey != "" && len(state.Messages) > 0 {
diff --git a/internal/harness/core/react_loop_test.go b/internal/harness/core/react_loop_test.go
new file mode 100644
index 0000000000..61de72ea08
--- /dev/null
+++ b/internal/harness/core/react_loop_test.go
@@ -0,0 +1,76 @@
+package core
+
+import (
+ "context"
+ "testing"
+
+ "ragflow/internal/harness/core/schema"
+)
+
+func TestReActAgentCompletesOnLastIteration(t *testing.T) {
+ tests := []struct {
+ name string
+ maxIterations int
+ callsTool bool
+ returnDirectly bool
+ wantOutput string
+ }{
+ {
+ name: "final answer on first iteration",
+ maxIterations: 1,
+ wantOutput: "Final answer",
+ },
+ {
+ name: "final answer after tool call",
+ maxIterations: 2,
+ callsTool: true,
+ wantOutput: "Final answer",
+ },
+ {
+ name: "return directly on last iteration",
+ maxIterations: 1,
+ callsTool: true,
+ returnDirectly: true,
+ wantOutput: "mock result for search",
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ model := &forcedToolModel{
+ toolCalls: []schema.ToolCall{{ID: "call-1", Function: schema.ToolCallFunction{Name: "search", Arguments: "{}"}}},
+ finalResp: "Final answer",
+ firstCall: tt.callsTool,
+ }
+ tool := &mockTool{name: "search", desc: "Search tool"}
+ afterAgentCalls := 0
+ middleware := &testMiddleware{
+ afterAgent: func(ctx context.Context, _ *ReActAgentState) (context.Context, error) {
+ afterAgentCalls++
+ return ctx, nil
+ },
+ }
+ agent := NewReActAgent(&ReActConfig[*schema.Message]{
+ Model: model,
+ Tools: []Tool{tool},
+ MaxIterations: tt.maxIterations,
+ ReturnDirectly: map[string]bool{"search": tt.returnDirectly},
+ OutputKey: "answer",
+ Middlewares: []TypedReActMiddleware[*schema.Message]{middleware},
+ })
+ input := &AgentInput{Messages: []Message{schema.UserMessage("Search")}}
+ ctx, _ := initRunCtx(t.Context(), "react", input)
+ events := drainAgentEvents(t, agent.Run(ctx, input))
+ for _, event := range events {
+ if event.Err != nil {
+ t.Errorf("successful completion emitted an error: %v", event.Err)
+ }
+ }
+ if got := getSession(ctx).Values["answer"]; got != tt.wantOutput {
+ t.Errorf("saved output = %v, want %q", got, tt.wantOutput)
+ }
+ if afterAgentCalls != 1 {
+ t.Errorf("AfterAgent calls = %d, want 1", afterAgentCalls)
+ }
+ })
+ }
+}