Skip to content

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

fix(agent-logs): persist failed execution status (#20752)

Sbaaoui Idriss9e43faf48127
diff --git a/internal/service/agent.go b/internal/service/agent.go
index 52bf5ba7a0..1d4a458a62 100644
--- a/internal/service/agent.go
+++ b/internal/service/agent.go
@@ -2062,6 +2062,7 @@ func (s *AgentService) buildRunFunc(canvasID string, versionRow *entity.UserCanv
// DSL → *Canvas.
c, err := decodeCanvasFromDSL(dsl)
if err != nil {
+ s.markAgentSessionFailed(ctx, canvasID, userID, sessionID, "decode: "+err.Error())
s.markRunFailed(ctx, runID, "decode: "+err.Error())
return nil, err
}
@@ -2141,6 +2142,7 @@ func (s *AgentService) buildRunFunc(canvasID string, versionRow *entity.UserCanv
fileSvc := file.NewFileService(CheckFileTeamPermission, nil)
files, ferr := fileSvc.ParseAgentUploads(ctx, userID, rawFiles, beginLayoutRecognize(c))
if ferr != nil {
+ s.markAgentSessionFailed(ctx2, canvasID, userID, sessionID, "parse files: "+ferr.Error())
s.markRunFailed(ctx2, runID, "parse files: "+ferr.Error())
return nil, fmt.Errorf("parse agent files: %w", ferr)
}
@@ -2198,6 +2200,7 @@ func (s *AgentService) buildRunFunc(canvasID string, versionRow *entity.UserCanv
zap.String("run", runID),
zap.String("type", fmt.Sprintf("%T", err)),
zap.Error(err))
+ s.markAgentSessionFailed(ctx2, canvasID, userID, sessionID, "compile: "+err.Error())
s.markRunFailed(ctx2, runID, "compile: "+err.Error())
if errors.Is(err, agenttool.ErrExeSQLNoCredentials) {
return nil, runtime.NewUserFacingError("ExeSQL configuration is incomplete. Set the database connection details before running the agent.")
@@ -2317,6 +2320,7 @@ func (s *AgentService) buildRunFunc(canvasID string, versionRow *entity.UserCanv
visibleAnswer = failureText
}
if persistErr := s.persistAgentRunSession(ctx, canvasID, userID, sessionID, messageID, userInput, visibleAnswer, thinking, referencePayload, dsl, state, visibleAnswer != ""); persistErr != nil {
+ s.markAgentSessionFailed(ctx2, canvasID, userID, sessionID, "persist session: "+persistErr.Error())
s.markRunFailed(ctx2, runID, "persist session: "+persistErr.Error())
return nil, canvas.NewInternalRunError(
fmt.Errorf("persist agent session: %w: %w", persistErr, ErrAgentStorageError),
@@ -2329,9 +2333,11 @@ func (s *AgentService) buildRunFunc(canvasID string, versionRow *entity.UserCanv
})
emit("message_end", string(meData))
}
+ s.markAgentSessionFailed(ctx2, canvasID, userID, sessionID, "invoke: "+err.Error())
s.markRunFailed(ctx2, runID, "invoke: "+err.Error())
return state, nil
}
+ s.markAgentSessionFailed(ctx2, canvasID, userID, sessionID, "invoke: "+err.Error())
s.markRunFailed(ctx2, runID, "invoke: "+err.Error())
return nil, canvasInvokeError(err)
}
@@ -2342,6 +2348,7 @@ func (s *AgentService) buildRunFunc(canvasID string, versionRow *entity.UserCanv
// reproduce and amplify those literals on later turns.
appendAssistantHistory(state, partialAssistantOutput(answer, downloads, attachment))
if persistErr := s.persistAgentRunSession(ctx, canvasID, userID, sessionID, messageID, userInput, answer, thinking, referencePayload, dsl, state, true); persistErr != nil {
+ s.markAgentSessionFailed(ctx2, canvasID, userID, sessionID, "persist session: "+persistErr.Error())
s.markRunFailed(ctx2, runID, "persist session: "+persistErr.Error())
return nil, canvas.NewInternalRunError(
fmt.Errorf("persist agent session: %w: %w", persistErr, ErrAgentStorageError),
@@ -2546,7 +2553,10 @@ func (s *AgentService) persistAgentRunSession(
if appendAssistantMessage {
history.Message = map[string]interface{}{"role": "assistant", "content": agentSessionMessageContent(answer, thinking), "id": messageID, "created_at": now}
}
- updates := map[string]interface{}{"round": gorm.Expr("COALESCE(round, 0) + 1")}
+ updates := map[string]interface{}{
+ "round": gorm.Expr("COALESCE(round, 0) + 1"),
+ "errors": nil,
+ }
if state != nil {
updates["dsl"] = buildPersistedAgentDSL(runDSL, state)
}
@@ -2946,6 +2956,20 @@ func (s *AgentService) markRunFailed(ctx context.Context, runID, reason string)
}
}
+// markAgentSessionFailed keeps the persisted agent log state aligned with the
+// run tracker. A streamed failure may still return a user-facing message, but
+// it must remain a failed execution in the session history.
+func (s *AgentService) markAgentSessionFailed(ctx context.Context, canvasID, userID, sessionID, reason string) {
+ if s == nil || s.api4ConversationDAO == nil || dao.DB == nil || sessionID == "" {
+ return
+ }
+ if err := s.api4ConversationDAO.UpdateHistory(ctx, dao.DB, sessionID, canvasID, userID,
+ map[string]interface{}{"errors": reason}, dao.ConversationHistoryUpdate{}); err != nil {
+ common.Warn("agent run: persist failed session status failed",
+ zap.String("session_id", sessionID), zap.Error(err))
+ }
+}
+
// normalisedDSLForRun returns the DSL map for the given version, or
// nil when the version has no DSL or is missing. The map is a deep
// copy because canvas.Compile mutates some fields in place; reusing