Skip to content

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

Fix: Clicking the "Run" button on the workflow canvas should output the content of the current canvas draft. (#21698)

balibabu21cdc37705d2
diff --git a/internal/service/agent.go b/internal/service/agent.go
index 53268a2318..cb57e0da66 100644
--- a/internal/service/agent.go
+++ b/internal/service/agent.go
@@ -1800,37 +1800,22 @@ func (s *AgentService) RunAgent(ctx context.Context, userID, canvasID, sessionID
releaseRegistration()
}()
- // Load the version row up front so the run is bound to a real DSL.
+ // DSL resolution: without an explicit version id the run executes the
+ // canvas's current editable draft (canvasRow.DSL) — the DSL the user just
+ // clicked "Run" against, which the front-end auto-saves before running.
+ // Published version rows are only executed through the released path
+ // (runReleasedAgent, embed with release=true) or an explicit ?version= id.
//
- // IDOR guard (v3.5.2 review): when the caller supplies an explicit
- // version id, the row we load must belong to canvasID — otherwise
- // any caller who knows a foreign version id could run that
- // canvas's DSL against their own canvas (a clear
- // integrity/authorization boundary breach). GetVersion (at the
- // read path) already enforces this check; the run path did not.
- // We mirror that check here and surface ErrUserCanvasVersionNotFound
- // so the handler maps to a clean 404 rather than silently using
- // the foreign DSL.
+ // IDOR guard: when the caller supplies an explicit version id, the row we
+ // load must belong to canvasID — otherwise any caller who knows a foreign
+ // version id could run that canvas's DSL against their own canvas (a
+ // clear integrity/authorization boundary breach). GetVersion (at the read
+ // path) already enforces this check; the run path mirrors it and surfaces
+ // ErrUserCanvasVersionNotFound so the handler maps to a clean 404 rather
+ // than silently using the foreign DSL. DB-side errors are wrapped with
+ // ErrAgentStorageError so raw DAO strings (DSNs, table names, gorm stack
+ // frames) never reach the client.
//
- // DAO-error visibility (v3.5.2 review): the previous code did
- // `versionRow, _ = ...` for both GetByID and GetLatest, which
- // masked every database failure as "no version published" and
- // let real ops issues hide behind the V1 placeholder answer.
- // We now distinguish three cases:
- // - explicit version, row not found → 404
- // - explicit version, row from other canvas → 404 (IDOR)
- // - explicit version, DB error → 500 (surface it)
- // - latest path, no rows + no error → fall back to canvasRow.DSL (matches Python `completion()`)
- // - latest path, DB error → 500 (surface it)
- //
- // v3.6 follow-up: when no published version exists, fall back to
- // the canvas's current editable DSL (canvasRow.DSL) instead of
- // the "no published version" placeholder. The Python reference at
- // api/db/services/canvas_service.py:332 does the same via
- // UserCanvasService.get_agent_dsl_with_release(agent_id,
- // release_mode=False, tenant_id=...) when release_mode is unset
- // on the request — the front-end's auto-save-on-run path means
- // the editable DSL is what the user just clicked "Run" against.
// The buildRunFunc placeholder branch is reserved for the rare
// "canvas exists but has no DSL at all" edge case.
var (
@@ -1843,11 +1828,6 @@ func (s *AgentService) RunAgent(ctx context.Context, userID, canvasID, sessionID
if errors.Is(err, dao.ErrUserCanvasVersionNotFound) {
return nil, fmt.Errorf("RunAgent: load version %q: %w", version, err)
}
- // Wrap DB-side errors with ErrAgentStorageError so
- // the handler maps them to CodeServerError (500)
- // with a sanitized message — raw DAO strings
- // (DSNs, table names, gorm stack frames) MUST NOT
- // reach the client.
return nil, fmt.Errorf("RunAgent: load version %q: %w: %w", version, err, ErrAgentStorageError)
}
if row.UserCanvasID != canvasID {
@@ -1861,30 +1841,9 @@ func (s *AgentService) RunAgent(ctx context.Context, userID, canvasID, sessionID
)
}
versionRow = row
- }
- if versionRow == nil {
- row, lerr := s.versionDAO.GetLatest(ctx, dao.DB, canvasID)
- switch {
- case lerr == nil:
- versionRow = row
- case errors.Is(lerr, dao.ErrUserCanvasVersionNotFound):
- // No published version — fall back to the canvas's
- // current editable DSL (see v3.6 follow-up comment
- // above). Mirrors Python's
- // `get_agent_dsl_with_release(...release_mode=False)`
- // fallback in completion().
- if len(canvasRow.DSL) > 0 {
- dsl = dslpkg.NormalizeForRun(canvasRow.DSL)
- }
- default:
- // Wrap DB-side errors with ErrAgentStorageError
- // for the same reason as above (no DAO-string
- // leak to the client).
- return nil, fmt.Errorf("RunAgent: load latest version for canvas %q: %w: %w", canvasID, lerr, ErrAgentStorageError)
- }
- }
- if dsl == nil {
dsl = normalisedDSLForRun(versionRow)
+ } else if len(canvasRow.DSL) > 0 {
+ dsl = dslpkg.NormalizeForRun(canvasRow.DSL)
}
sessionFound := false
if sessionID != "" && s.api4ConversationDAO != nil && dao.DB != nil {
diff --git a/internal/service/agent_run_e2e_test.go b/internal/service/agent_run_e2e_test.go
index fa7e443061..a108389226 100644
--- a/internal/service/agent_run_e2e_test.go
+++ b/internal/service/agent_run_e2e_test.go
@@ -18,7 +18,7 @@
// production chain.
//
// These tests pin the production chain end-to-end: loadCanvasForUser
-// → versionDAO.GetLatest → decodeCanvasFromDSL → canvas.Compile →
+// → draft DSL resolution → decodeCanvasFromDSL → canvas.Compile →
// cc.Workflow.Invoke → orchestrator answer extraction. They also
// cover the boot-wiring surface (Kvrocks-backed CheckPointStore +
// RunTracker) and the failure paths (compile error, invoke
@@ -61,9 +61,9 @@ import (
)
// makeCanvasWithDSL inserts a canvas + tenant + user + a published
-// version whose DSL is the supplied map. The returned version id is
-// what RunAgent consumes when the caller does not pass an explicit
-// version (it falls through to GetLatest).
+// version, with the supplied DSL on both the canvas row (the editable
+// draft) and the version row. RunAgent without an explicit version id
+// executes the draft, so the canvas-row DSL is what these tests run.
func makeCanvasWithDSL(t *testing.T, canvasID, userID, tenantID, versionID string, dsl map[string]any) {
t.Helper()
dao.DB.Where("id = ?", userID).FirstOrCreate(&entity.User{ID: userID, Nickname: "owner", Email: userID + "@test.com"})
@@ -79,6 +79,7 @@ func makeCanvasWithDSL(t *testing.T, canvasID, userID, tenantID, versionID strin
Permission: "me",
CanvasType: sptr("agent"),
CanvasCategory: "agent_canvas",
+ DSL: entity.JSONMap(dsl),
})
dao.DB.Create(&entity.UserCanvasVersion{
ID: versionID,
@@ -209,7 +210,7 @@ func TestRunAgent_RealCanvas_BeginMessage(t *testing.T) {
"user-1",
"canvas-hello",
"session-hello",
- "", // latest version
+ "", // draft DSL
"world", nil)
if err != nil {
t.Fatalf("RunAgent: %v", err)
@@ -1912,7 +1913,7 @@ func TestRunAgent_RunTracker_AttachCheckpoint_CallSequence(t *testing.T) {
"user-1",
"canvas-cp",
"session-cp",
- "", // latest version
+ "", // draft DSL
"world", nil)
if err != nil {
t.Fatalf("RunAgent: %v", err)
diff --git a/internal/service/agent_sessions.go b/internal/service/agent_sessions.go
index 9969b1589f..d2ea1cbc76 100644
--- a/internal/service/agent_sessions.go
+++ b/internal/service/agent_sessions.go
@@ -705,21 +705,9 @@ func (s *AgentService) CreateAgentSession(ctx context.Context, req *CreateAgentS
return nil, common.CodeDataError, err
}
- // Resolve the latest version title so the session records which version it
- // belongs to. GetLatest mirrors the RunAgent path and Python's
- // release_mode=false lookup; a canvas without any version row yet is a
- // normal state and leaves the title nil.
- var versionTitle *string
- if s.versionDAO != nil {
- versionRow, verr := s.versionDAO.GetLatest(ctx, dao.DB, req.AgentID)
- if verr != nil && !errors.Is(verr, dao.ErrUserCanvasVersionNotFound) {
- return nil, common.CodeServerError, fmt.Errorf("load latest version title: %w", verr)
- }
- if versionRow != nil {
- versionTitle = versionRow.Title
- }
- }
-
+ // The session stores the draft DSL (or a caller-supplied one), never a
+ // version row's DSL, so VersionTitle stays nil: the log's Version column
+ // must name the version actually run, and a draft run is bound to none.
name := strings.TrimSpace(req.Name)
if name == "" {
name = "session"
@@ -736,16 +724,15 @@ func (s *AgentService) CreateAgentSession(ctx context.Context, req *CreateAgentS
// by entity.BaseModel.BeforeCreate when the DAO Create() call runs,
// so we do not set them explicitly here.
row := &entity.API4Conversation{
- ID: id,
- Name: namePtr,
- DialogID: req.AgentID,
- UserID: req.UserID,
- ExpUserID: &req.UserID,
- Message: messages,
- Reference: reference,
- Source: sourcePtr,
- DSL: dsl,
- VersionTitle: versionTitle,
+ ID: id,
+ Name: namePtr,
+ DialogID: req.AgentID,
+ UserID: req.UserID,
+ ExpUserID: &req.UserID,
+ Message: messages,
+ Reference: reference,
+ Source: sourcePtr,
+ DSL: dsl,
}
if err := s.api4ConversationDAO.Create(ctx, dao.DB, row); err != nil {
return nil, common.CodeServerError, fmt.Errorf("create agent session: %w", err)
diff --git a/internal/service/agent_test.go b/internal/service/agent_test.go
index 3827ed8d27..498f7afb0f 100644
--- a/internal/service/agent_test.go
+++ b/internal/service/agent_test.go
@@ -693,7 +693,7 @@ func TestRunAgent_NoVersionPublishedPlaceholder(t *testing.T) {
"user-1",
"canvas-empty",
"test-session",
- "", // no explicit version → use GetLatest, which returns ErrUserCanvasVersionNotFound
+ "", // no explicit version → draft DSL, which is empty here
"hi", nil)
if err != nil {
t.Fatalf("RunAgent should proceed with placeholder when no version published: %v", err)
@@ -1079,7 +1079,12 @@ func TestGetAgentSessionServiceSuccess(t *testing.T) {
}
}
-func TestCreateAgentSessionWritesLatestVersionTitle(t *testing.T) {
+// TestCreateAgentSessionLeavesVersionTitleNilWithVersions pins the
+// log-truthfulness rule: a session created against the draft DSL must
+// not be stamped with a version title, even when version rows exist —
+// otherwise the agent log's Version column would name a version whose
+// DSL the session never ran.
+func TestCreateAgentSessionLeavesVersionTitleNilWithVersions(t *testing.T) {
setupAgentSessionServiceTest(t)
createAgentSessionTestCanvas(t, "canvas-1", "user-1")
@@ -1121,25 +1126,22 @@ func TestCreateAgentSessionWritesLatestVersionTitle(t *testing.T) {
if code != common.CodeSuccess {
t.Fatalf("expected code %d, got %d", common.CodeSuccess, code)
}
- if session.VersionTitle == nil {
- t.Fatal("expected version_title to be set")
- }
- if *session.VersionTitle != "owner_Test Agent_newest" {
- t.Fatalf("expected newest version title, got %q", *session.VersionTitle)
+ if session.VersionTitle != nil {
+ t.Fatalf("expected nil version_title for draft session, got %q", *session.VersionTitle)
}
stored, err := dao.NewAPI4ConversationDAO().GetMetadataBySessionID(ctx, dao.DB, session.ID, "canvas-1")
if err != nil {
t.Fatalf("failed to reload session: %v", err)
}
- if stored == nil || stored.VersionTitle == nil {
- t.Fatalf("expected persisted version_title, got %+v", stored)
+ if stored == nil {
+ t.Fatal("expected persisted session")
}
- if *stored.VersionTitle != "owner_Test Agent_newest" {
- t.Fatalf("expected persisted newest title, got %q", *stored.VersionTitle)
+ if stored.VersionTitle != nil {
+ t.Fatalf("expected persisted nil version_title, got %q", *stored.VersionTitle)
}
- // The list surface used by the agent log page must expose it too.
+ // The list surface used by the agent log page must expose nil too.
resp, code, err := svc.ListAgentSessions(ctx, "user-1", "user-1", "canvas-1", ListAgentSessionsRequest{Page: 1, PageSize: 10})
if err != nil {
t.Fatalf("ListAgentSessions failed: %v", err)
@@ -1150,9 +1152,8 @@ func TestCreateAgentSessionWritesLatestVersionTitle(t *testing.T) {
if len(resp.Data) != 1 {
t.Fatalf("expected 1 session, got %d", len(resp.Data))
}
- listed, _ := resp.Data[0]["version_title"].(*string)
- if listed == nil || *listed != "owner_Test Agent_newest" {
- t.Fatalf("expected list version_title newest, got %v", resp.Data[0]["version_title"])
+ if listed, _ := resp.Data[0]["version_title"].(*string); listed != nil {
+ t.Fatalf("expected list version_title nil, got %q", *listed)
}
}
@@ -1259,31 +1260,6 @@ func TestAgentModelValidationUsesCanvasTenant(t *testing.T) {
}
}
-func TestCreateAgentSessionVersionLookupErrorReturnsServerError(t *testing.T) {
- setupAgentSessionServiceTest(t)
-
- createAgentSessionTestCanvas(t, "canvas-1", "user-1")
-
- if err := dao.DB.Exec("DROP TABLE user_canvas_version").Error; err != nil {
- t.Fatalf("failed to drop user_canvas_version: %v", err)
- }
-
- ctx := serviceTestContext(t, "user-1", "user-1")
- session, code, err := NewAgentService().CreateAgentSession(ctx, &CreateAgentSessionRequest{
- UserID: "user-1",
- AgentID: "canvas-1",
- })
- if err == nil {
- t.Fatal("expected version lookup error")
- }
- if code != common.CodeServerError {
- t.Fatalf("expected code %d, got %d", common.CodeServerError, code)
- }
- if session != nil {
- t.Fatalf("expected nil session, got %+v", session)
- }
-}
-
func TestGetAgentSessionServiceNotFoundWhenSessionBelongsToAnotherAgent(t *testing.T) {
setupAgentSessionServiceTest(t)