RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
refa: keep builtin compilation templates out of database (#19760)
buua436c0e1fa69589d
diff --git a/internal/dao/compilation_template.go b/internal/dao/compilation_template.goindex 48c5818d8b..170926e751 100644--- a/internal/dao/compilation_template.go+++ b/internal/dao/compilation_template.go@@ -49,16 +49,9 @@ func (dao *CompilationTemplateDAO) ResolveGroupTemplateIDs(ctx context.Context,return nil, nil}- // Verify the requested groups exist and are valid. The built-in group- // (compiler.json's default) is a global, tenant-agnostic catalogue, so it- // resolves for every tenant; other groups are scoped to the caller's- // tenant.- isBuiltin := make(map[string]bool, len(groupIDs))- for _, gid := range groupIDs {- if gid == BuiltinCompilationTemplateGroupID {- isBuiltin[gid] = true- }- }+ // Verify the requested groups exist, are valid, and belong to the caller's+ // tenant. Built-in template definitions are read-only YAML test fixtures;+ // they are not persisted as a global template group.var validGroups []entity.CompilationTemplateGroupif err := db.WithContext(ctx).Where("id IN ? AND status = ?", groupIDs, string(entity.StatusValid)).@@ -75,11 +68,8 @@ func (dao *CompilationTemplateDAO) ResolveGroupTemplateIDs(ctx context.Context,if _, ok := valid[gid]; !ok {return nil, fmt.Errorf("compilation_template_group %q not found for tenant %q", gid, tenantID)}- // Non-built-in groups must belong to the requesting tenant.- if !isBuiltin[gid] {- if byID[gid].TenantID != tenantID {- return nil, fmt.Errorf("compilation_template_group %q not found for tenant %q", gid, tenantID)- }+ if byID[gid].TenantID != tenantID {+ return nil, fmt.Errorf("compilation_template_group %q not found for tenant %q", gid, tenantID)}}@@ -117,8 +107,8 @@ func (dao *CompilationTemplateDAO) ResolveGroupTemplateIDs(ctx context.Context,func (dao *CompilationTemplateDAO) GetTemplate(ctx context.Context, db *gorm.DB, tenantID, templateID string) (*entity.CompilationTemplate, error) {var t entity.CompilationTemplateif err := db.WithContext(ctx).- Where("id = ? AND status = ? AND (tenant_id = ? OR (tenant_id IS NULL AND (is_builtin = ? OR group_id = ?)))",- templateID, string(entity.StatusValid), tenantID, true, BuiltinCompilationTemplateGroupID).+ Where("id = ? AND status = ? AND tenant_id = ?",+ templateID, string(entity.StatusValid), tenantID).First(&t).Error; err != nil {return nil, fmt.Errorf("load compilation template %q: %w", templateID, err)}@@ -139,19 +129,6 @@ func (dao *CompilationTemplateDAO) ListByGroup(ctx context.Context, db *gorm.DB,return templates, nil}-// ListBuiltins returns the valid, built-in (is_builtin) compilation templates,-// ordered by create_time then name. Mirrors Python list_builtins().-func (dao *CompilationTemplateDAO) ListBuiltins(ctx context.Context, db *gorm.DB) ([]*entity.CompilationTemplate, error) {- var templates []*entity.CompilationTemplate- if err := db.WithContext(ctx).- Where("is_builtin = ? AND status = ?", true, string(entity.StatusValid)).- Order("create_time asc, name asc").- Find(&templates).Error; err != nil {- return nil, err- }- return templates, nil-}-// NameExistsInGroup reports whether a valid, non-built-in template with the// given name already exists inside the group, excluding excludeID. Mirrors the// Python duplicate-child guard.diff --git a/internal/dao/compilation_template_group.go b/internal/dao/compilation_template_group.goindex e1fbd79403..5b11ef47f4 100644--- a/internal/dao/compilation_template_group.go+++ b/internal/dao/compilation_template_group.go@@ -34,11 +34,11 @@ func NewCompilationTemplateGroupDAO() *CompilationTemplateGroupDAO {}// ListSaved returns the tenant's valid groups with optional keyword/scope-// filtering and ordering, mirroring Python list_saved(). Built-in groups-// (empty tenant) are included so every tenant sees the catalogue.+// filtering and ordering, mirroring Python list_saved(). Built-in template+// definitions are not persisted as a group and therefore never appear here.func (dao *CompilationTemplateGroupDAO) ListSaved(ctx context.Context, db *gorm.DB, tenantID, keywords, scope string, terms []OrderTerm) ([]*entity.CompilationTemplateGroup, error) {q := db.WithContext(ctx).- Where("(tenant_id = ? OR tenant_id = '') AND status = ?", tenantID, string(entity.StatusValid))+ Where("tenant_id = ? AND status = ?", tenantID, string(entity.StatusValid))if keywords != "" {q = q.Where("name LIKE ?", "%"+keywords+"%")}@@ -52,10 +52,9 @@ func (dao *CompilationTemplateGroupDAO) ListSaved(ctx context.Context, db *gorm.return groups, nil}-// ListOwnedSaved returns only the tenant's own valid groups (built-in groups-// with empty tenant_id are excluded), mirroring the Python list_saved() query-// (cls.model.tenant_id == tenant_id). The merged /agents list uses this so-// built-in catalogue groups do not leak into a tenant's canvas list.+// ListOwnedSaved returns only the tenant's own valid groups, mirroring the+// Python list_saved() query (cls.model.tenant_id == tenant_id). The merged+// /agents list uses this for tenant-owned canvas groups.func (dao *CompilationTemplateGroupDAO) ListOwnedSaved(ctx context.Context, db *gorm.DB, tenantID, keywords, scope string, terms []OrderTerm) ([]*entity.CompilationTemplateGroup, error) {q := db.WithContext(ctx).Where("tenant_id = ? AND status = ?", tenantID, string(entity.StatusValid))@@ -72,9 +71,8 @@ func (dao *CompilationTemplateGroupDAO) ListOwnedSaved(ctx context.Context, db *return groups, nil}-// CountSavedByTenant counts the tenant's own valid groups; built-in groups-// (empty tenant_id) are excluded, mirroring the group_count query in Python-// get_owner_filter / get_category_filter.+// CountSavedByTenant counts the tenant's own valid groups, mirroring the+// group_count query in Python get_owner_filter / get_category_filter.func (dao *CompilationTemplateGroupDAO) CountSavedByTenant(ctx context.Context, db *gorm.DB, tenantID string) (int64, error) {var count int64err := db.WithContext(ctx).Model(&entity.CompilationTemplateGroup{}).@@ -83,11 +81,11 @@ func (dao *CompilationTemplateGroupDAO) CountSavedByTenant(ctx context.Context,return count, err}-// GetSaved returns a single valid group for the tenant (or built-in), or nil.+// GetSaved returns a single valid group for the tenant, or nil.func (dao *CompilationTemplateGroupDAO) GetSaved(ctx context.Context, db *gorm.DB, tenantID, groupID string) (*entity.CompilationTemplateGroup, error) {var g entity.CompilationTemplateGrouperr := db.WithContext(ctx).- Where("(tenant_id = ? OR tenant_id = '') AND id = ? AND status = ?",+ Where("tenant_id = ? AND id = ? AND status = ?",tenantID, groupID, string(entity.StatusValid)).First(&g).Errorif errors.Is(err, gorm.ErrRecordNotFound) {diff --git a/internal/dao/compilation_template_seed.go b/internal/dao/compilation_template_seed.godeleted file mode 100644index 26c4e987b4..0000000000--- a/internal/dao/compilation_template_seed.go+++ /dev/null@@ -1,123 +0,0 @@-//-// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.-//-// Licensed under the Apache License, Version 2.0 (the "License");-// you may not use this file except in compliance with the License.-// You may obtain a copy of the License at-//-// http://www.apache.org/licenses/LICENSE-2.0-//-// Unless required by applicable law or agreed to in writing, software-// distributed under the License is distributed on an "AS IS" BASIS,-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-// See the License for the specific language governing permissions and-// limitations under the License.-//--package dao--import (- "context"- "ragflow/internal/common"- "ragflow/internal/entity"-- "go.uber.org/zap"- "gorm.io/gorm"- "gorm.io/gorm/clause"-)--// BuiltinCompilationTemplateGroupID is the id of the system-provided-// compilation template group that compiler.json references as its default-// "create from template" group. It is a global (tenant-agnostic) catalogue so-// any tenant can resolve it without depending on Python-side initialization.-const BuiltinCompilationTemplateGroupID = "c3aa748c8b2111f191f3047c16ec874f"--// builtinCompilationTemplateKinds is the set of variants shipped in the-// built-in group. The Go KnowledgeCompiler derives the variant from each-// template's kind via common.KindToVariant.-var builtinCompilationTemplateKinds = []struct {- Kind string- Name string-}{- {Kind: "tree", Name: "Tree — RAPTOR-based document tree"},- {Kind: "mind_map", Name: "Mind map - Radial concept hierarchy"},- {Kind: "wiki", Name: "Wiki — Graph-based wiki"},- {Kind: "knowledge_graph", Name: "Knowledge graph"},- {Kind: "page_index", Name: "Page index"},- {Kind: "session_essence", Name: "Session essence"},- {Kind: "session_graph", Name: "Session graph"},- {Kind: "timeline", Name: "Timeline"},-}--func strPTR(s string) *string { return &s }--// builtinTemplateID derives a deterministic, <=32-byte id for a built-in-// compilation template from "<builtin-group-id>-<kind>". A plain concatenation-// overflows the varchar(32) id column for long kinds (e.g. knowledge_graph),-// so we take the last 32 bytes. Each kind ends up inside those last bytes (the-// "-<kind>" suffix is preserved and kinds are distinct), so the truncated ids-// stay unique and readable, and the seed stays idempotent (OnConflict on id).-func builtinTemplateID(kind string) string {- full := BuiltinCompilationTemplateGroupID + "-" + kind- if len(full) <= 32 {- return full- }- return full[len(full)-32:]-}--// SeedBuiltinCompilationTemplates provisions the built-in compilation template-// group (and its child templates) as a global, tenant-agnostic catalogue, so-// the Go compiler (and the frontend "create from template" catalogue) can-// resolve compiler.json's default group id out of the box. The group row uses-// an empty tenant_id and ResolveGroupTemplateIDs treats this built-in id as-// resolvable for every tenant.-func SeedBuiltinCompilationTemplates(ctx context.Context, db *gorm.DB) error {- if err := SeedBuiltinCompilationTemplatesForTenant(ctx, db, ""); err != nil {- common.Warn("Failed to seed built-in compilation templates", zap.Error(err))- return err- }- return nil-}--// SeedBuiltinCompilationTemplatesForTenant upserts the built-in compilation-// template group and its child templates as a global, tenant-agnostic-// catalogue. The tenantID argument is accepted for API symmetry but the-// built-in entries always use an empty tenant_id, because ResolveGroupTemplateIDs-// resolves the built-in group id for every tenant. It is idempotent-// (OnConflict DO NOTHING on id) so it is safe to call repeatedly.-func SeedBuiltinCompilationTemplatesForTenant(ctx context.Context, db *gorm.DB, tenantID string) error {- valid := string(entity.StatusValid)- return db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {- group := &entity.CompilationTemplateGroup{- ID: BuiltinCompilationTemplateGroupID,- TenantID: "", // global built-in catalogue- Name: "Built-in templates",- Status: &valid,- }- if err := tx.WithContext(ctx).Clauses(clause.OnConflict{- Columns: []clause.Column{{Name: "id"}},- DoNothing: true,- }).Create(group).Error; err != nil {- return err- }-- for _, t := range builtinCompilationTemplateKinds {- tmpl := &entity.CompilationTemplate{- ID: builtinTemplateID(t.Kind),- TenantID: nil, // global built-in catalogue- GroupID: strPTR(BuiltinCompilationTemplateGroupID),- Name: t.Name,- Kind: t.Kind,- Config: entity.JSONMap{"kind": t.Kind},- Status: &valid,- }- if err := tx.WithContext(ctx).Clauses(clause.OnConflict{- Columns: []clause.Column{{Name: "id"}},- DoNothing: true,- }).Create(tmpl).Error; err != nil {- return err- }- }- return nil- })-}diff --git a/internal/dao/compilation_template_seed_test.go b/internal/dao/compilation_template_seed_test.godeleted file mode 100644index d40f6f878a..0000000000--- a/internal/dao/compilation_template_seed_test.go+++ /dev/null@@ -1,173 +0,0 @@-//-// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.-//-// Licensed under the Apache License, Version 2.0 (the "License");-// you may not use this file except in compliance with the License.-// You may obtain a copy of the License at-//-// http://www.apache.org/licenses/LICENSE-2.0-//-// Unless required by applicable law or agreed to in writing, software-// distributed under the License is distributed on an "AS IS" BASIS,-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-// See the License for the specific language governing permissions and-// limitations under the License.-//--package dao--import (- "testing"-- "ragflow/internal/entity"-- "github.com/glebarez/sqlite"- "gorm.io/gorm"-)--// TestBuiltinCompilationTemplateKinds_NoDatasetnav locks the removal of the-// datasetnav template from the built-in catalogue: dataset navigation is not an-// independent compile kind (see component_test.TestKnowledgeCompiler_Datasetnav_NoVariant),-// so it must not appear in the seeded template kinds.-func TestBuiltinCompilationTemplateKinds_NoDatasetnav(t *testing.T) {- for _, k := range builtinCompilationTemplateKinds {- if k.Kind == "datasetnav" || k.Kind == "dataset_nav" {- t.Fatalf("builtin compilation template kind %q must not exist (datasetnav is a by-product, not a compile kind)", k.Kind)- }- }-}--// TestBuiltinTemplateIDWithinColumnWidth guards against a regression where the-// built-in template id exceeds the varchar(32) compilation_template.id column,-// which would make the MySQL seed fail with Error 1406 (data too long).-func TestBuiltinTemplateIDWithinColumnWidth(t *testing.T) {- const idColumnSize = 32- seen := make(map[string]string, len(builtinCompilationTemplateKinds))- for _, k := range builtinCompilationTemplateKinds {- id := builtinTemplateID(k.Kind)- if len(id) > idColumnSize {- t.Fatalf("built-in template id %q is %d bytes > id column size %d", id, len(id), idColumnSize)- }- // Truncated ids must stay unique so OnConflict(id) does not clobber two- // kinds into one row.- if prev, dup := seen[id]; dup {- t.Fatalf("built-in template id %q collides for kinds %q and %q", id, prev, k.Kind)- }- seen[id] = k.Kind- }-}--func TestSeedBuiltinCompilationTemplatesForTenant(t *testing.T) {- db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})- if err != nil {- t.Fatalf("open sqlite: %v", err)- }- if err = db.AutoMigrate(&entity.CompilationTemplateGroup{}, &entity.CompilationTemplate{}); err != nil {- t.Fatalf("migrate: %v", err)- }- // ResolveGroupTemplateIDs reads the package-global DB, so point it at our- // sqlite instance for the duration of the test.- orig := DB- DB = db- t.Cleanup(func() { DB = orig })-- ctx := t.Context()- const tenantID = "tenant-1"-- // Idempotent across repeated calls, and the built-in group is always a- // global (tenant_id = "") catalogue regardless of the tenantID argument.- for i := 0; i < 2; i++ {- if err = SeedBuiltinCompilationTemplatesForTenant(ctx, db, tenantID); err != nil {- t.Fatalf("seed (iter %d): %v", i, err)- }- }-- var groups []entity.CompilationTemplateGroup- if err = db.Find(&groups).Error; err != nil {- t.Fatalf("read groups: %v", err)- }- if len(groups) != 1 {- t.Fatalf("got %d groups, want 1", len(groups))- }- if groups[0].ID != BuiltinCompilationTemplateGroupID || groups[0].TenantID != "" {- t.Fatalf("unexpected group: %#v", groups[0])- }-- var templates []entity.CompilationTemplate- if err = db.Find(&templates).Error; err != nil {- t.Fatalf("read templates: %v", err)- }- if len(templates) != len(builtinCompilationTemplateKinds) {- t.Fatalf("got %d templates, want %d", len(templates), len(builtinCompilationTemplateKinds))- }- for _, tmpl := range templates {- // Built-in templates are a global catalogue (tenant_id nil).- if tmpl.TenantID != nil {- t.Errorf("template %s: tenant_id=%v, want nil (global catalogue)", tmpl.ID, tmpl.TenantID)- }- if tmpl.GroupID == nil || *tmpl.GroupID != BuiltinCompilationTemplateGroupID {- t.Errorf("template %s: group_id=%v, want %q", tmpl.ID, tmpl.GroupID, BuiltinCompilationTemplateGroupID)- }- if tmpl.Kind == "" {- t.Errorf("template %s: empty kind", tmpl.ID)- }- if tmpl.Config["kind"] != tmpl.Kind {- t.Errorf("template %s: config.kind=%v, want %q", tmpl.ID, tmpl.Config["kind"], tmpl.Kind)- }- }-- // Re-seeding with a different tenant is idempotent for the global group:- // the single built-in group row stays (id is the PK), and templates are- // not duplicated.- if err = SeedBuiltinCompilationTemplatesForTenant(ctx, db, "tenant-2"); err != nil {- t.Fatalf("seed tenant-2: %v", err)- }- var allGroups []entity.CompilationTemplateGroup- if err = db.Find(&allGroups).Error; err != nil {- t.Fatalf("read all groups: %v", err)- }- if len(allGroups) != 1 {- t.Fatalf("got %d groups, want 1 (built-in group is global, single row)", len(allGroups))- }- if allGroups[0].TenantID != "" {- t.Errorf("built-in group tenant_id = %q, want empty (global catalogue)", allGroups[0].TenantID)- }-- // ResolveGroupTemplateIDs resolves the built-in group for any tenant.- resolved, err := NewCompilationTemplateDAO().ResolveGroupTemplateIDs(ctx, db, "some-other-tenant", []string{BuiltinCompilationTemplateGroupID})- if err != nil {- t.Fatalf("resolve built-in group: %v", err)- }- if len(resolved) != len(builtinCompilationTemplateKinds) {- t.Fatalf("resolved %d template ids, want %d", len(resolved), len(builtinCompilationTemplateKinds))- }-- got, err := NewCompilationTemplateDAO().GetTemplate(ctx, db, "some-other-tenant", builtinTemplateID("tree"))- if err != nil {- t.Fatalf("resolve global built-in template: %v", err)- }- if got.Kind != "tree" {- t.Fatalf("resolved built-in kind = %q, want tree", got.Kind)- }-}--func TestGetTemplateKeepsTenantScopeForCustomTemplates(t *testing.T) {- db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})- if err != nil {- t.Fatalf("open sqlite: %v", err)- }- if err = db.AutoMigrate(&entity.CompilationTemplate{}); err != nil {- t.Fatalf("migrate: %v", err)- }- tenantID := "tenant-1"- status := string(entity.StatusValid)- if err = db.Create(&entity.CompilationTemplate{- ID: "custom-template", TenantID: &tenantID, Name: "Custom", Kind: "tree",- Config: entity.JSONMap{"kind": "tree"}, Status: &status,- }).Error; err != nil {- t.Fatalf("insert custom template: %v", err)- }- if _, err = NewCompilationTemplateDAO().GetTemplate(t.Context(), db, "tenant-2", "custom-template"); err == nil {- t.Fatal("custom template resolved for the wrong tenant")- }-}diff --git a/internal/dao/compilation_template_test.go b/internal/dao/compilation_template_test.gonew file mode 100644index 0000000000..0c3362661c--- /dev/null+++ b/internal/dao/compilation_template_test.go@@ -0,0 +1,86 @@+//+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.+//++package dao++import (+ "testing"++ "ragflow/internal/entity"++ "github.com/glebarez/sqlite"+ "gorm.io/gorm"+)++func TestCompilationTemplateGroupDAO_TenantScope(t *testing.T) {+ db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})+ if err != nil {+ t.Fatalf("open sqlite: %v", err)+ }+ if err = db.AutoMigrate(&entity.CompilationTemplateGroup{}); err != nil {+ t.Fatalf("migrate: %v", err)+ }+ status := string(entity.StatusValid)+ if err = db.Create([]*entity.CompilationTemplateGroup{+ {ID: "tenant-group", TenantID: "tenant-1", Name: "Tenant", Scope: "file", Status: &status},+ {ID: "global-group", TenantID: "", Name: "Built-in templates", Scope: "file", Status: &status},+ }).Error; err != nil {+ t.Fatalf("insert groups: %v", err)+ }++ dao := NewCompilationTemplateGroupDAO()+ groups, err := dao.ListSaved(t.Context(), db, "tenant-1", "", "", nil)+ if err != nil {+ t.Fatalf("ListSaved: %v", err)+ }+ if len(groups) != 1 || groups[0].ID != "tenant-group" {+ t.Fatalf("ListSaved returned %#v, want only tenant-group", groups)+ }++ group, err := dao.GetSaved(t.Context(), db, "tenant-1", "global-group")+ if err != nil {+ t.Fatalf("GetSaved: %v", err)+ }+ if group != nil {+ t.Fatalf("GetSaved returned global group %#v", group)+ }+}++func TestCompilationTemplateDAO_TenantScope(t *testing.T) {+ db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})+ if err != nil {+ t.Fatalf("open sqlite: %v", err)+ }+ if err = db.AutoMigrate(&entity.CompilationTemplate{}); err != nil {+ t.Fatalf("migrate: %v", err)+ }+ status := string(entity.StatusValid)+ tenantID := "tenant-1"+ if err = db.Create([]*entity.CompilationTemplate{+ {ID: "tenant-template", TenantID: &tenantID, Name: "Tenant", Kind: "tree", Config: entity.JSONMap{"kind": "tree"}, Status: &status},+ {ID: "global-template", TenantID: nil, Name: "Built-in", Kind: "tree", Config: entity.JSONMap{"kind": "tree"}, IsBuiltin: true, Status: &status},+ }).Error; err != nil {+ t.Fatalf("insert templates: %v", err)+ }++ dao := NewCompilationTemplateDAO()+ if _, err = dao.GetTemplate(t.Context(), db, "tenant-1", "tenant-template"); err != nil {+ t.Fatalf("GetTemplate tenant row: %v", err)+ }+ if _, err = dao.GetTemplate(t.Context(), db, "tenant-1", "global-template"); err == nil {+ t.Fatal("GetTemplate resolved a non-persisted global builtin row")+ }+}diff --git a/internal/dao/database.go b/internal/dao/database.goindex 1543d286b2..69d11a9659 100644--- a/internal/dao/database.go+++ b/internal/dao/database.go@@ -226,12 +226,6 @@ func InitDB(ctx context.Context, migrateDB bool) error {if err = SeedCanvasTemplates(ctx, DB); err != nil {common.Warn("Failed to seed canvas templates", zap.Error(err))}- // Seed the built-in compilation template group (c3aa748c...) for every- // tenant so compiler.json's default group resolves out of the box.- if err = SeedBuiltinCompilationTemplates(ctx, DB); err != nil {- common.Warn("Failed to seed built-in compilation templates", zap.Error(err))- }-common.Info("Database connected and migrated successfully")err = models.InitProviderManager("conf/models")diff --git a/internal/service/agent_test.go b/internal/service/agent_test.goindex 005dca3211..3189a7024f 100644--- a/internal/service/agent_test.go+++ b/internal/service/agent_test.go@@ -1972,8 +1972,8 @@ func TestListAgents_MultiCategoryFilter(t *testing.T) {// TestListAgents_MergesCompilationTemplateGroups verifies that a compilation// template group owned by the caller appears in the merged /agents list// (no canvas_category filter), carrying the "compilation_template_group" type-// discriminator and its title = name. Built-in catalogue groups (empty tenant)-// must NOT leak in.+// discriminator and its title = name. Groups without the caller's tenant+// ownership must NOT leak in.func TestListAgents_MergesCompilationTemplateGroups(t *testing.T) {setupAgentSessionServiceTest(t)@@ -1993,7 +1993,7 @@ func TestListAgents_MergesCompilationTemplateGroups(t *testing.T) {}// The caller's own group (must appear), updated before the canvas.createAgentSessionTestCompilationGroup(t, "group-own", "user-1", groupUpdate)- // A built-in catalogue group with empty tenant_id (must NOT appear).+ // An unowned group with empty tenant_id (must NOT appear).if err := dao.DB.Create(&entity.CompilationTemplateGroup{ID: "group-builtin", TenantID: "", Name: "Built-in templates",Scope: "file", BaseModel: entity.BaseModel{CreateTime: &base},diff --git a/internal/service/compilation_template_service.go b/internal/service/compilation_template_service.goindex 6d77141107..73decaab90 100644--- a/internal/service/compilation_template_service.go+++ b/internal/service/compilation_template_service.go@@ -32,7 +32,6 @@ import ("ragflow/internal/utility""gopkg.in/yaml.v3"- "gorm.io/gorm")// fillConfigDefaultLLM mirrors Python CompilationTemplateService.@@ -101,49 +100,30 @@ type WikiPreset struct {}// CompilationTemplateService implements the read-side compilation template-// operations (builtins palette + wiki presets) used by the REST APIs.+// operations (read-only YAML palette + wiki presets) used by the REST APIs.type CompilationTemplateService struct {- templateDAO *dao.CompilationTemplateDAO- tenantDAO *dao.TenantDAO+ tenantDAO *dao.TenantDAO}// NewCompilationTemplateService creates a CompilationTemplateService.func NewCompilationTemplateService() *CompilationTemplateService {return &CompilationTemplateService{- templateDAO: dao.NewCompilationTemplateDAO(),- tenantDAO: dao.NewTenantDAO(),+ tenantDAO: dao.NewTenantDAO(),}}-// ListBuiltins returns the built-in template palette with the tenant's default-// LLM id lazily filled in, mirroring the Python list_builtins blueprint flow-// (list from DB; if empty, seed from files and retry; then fill default LLM).+// ListBuiltins returns the read-only template palette loaded from the YAML+// definitions. Built-in templates are never persisted in the database.func (s *CompilationTemplateService) ListBuiltins(ctx context.Context, tenantID string) ([]*BuiltinTemplate, error) {- templates, err := s.templateDAO.ListBuiltins(ctx, dao.DB)+ templates, err := loadBuiltinTemplates()if err != nil {return nil, err}- if len(templates) == 0 {- if err = s.seedBuiltins(ctx); err != nil {- return nil, err- }- templates, err = s.templateDAO.ListBuiltins(ctx, dao.DB)- if err != nil {- return nil, err- }+ tenantPtr := &tenantID+ for _, template := range templates {+ template.Config = fillConfigDefaultLLM(ctx, s.tenantDAO, template.Config, tenantPtr)}- out := make([]*BuiltinTemplate, 0, len(templates))- for _, t := range templates {- config := fillConfigDefaultLLM(ctx, s.tenantDAO, t.Config, t.TenantID)- out = append(out, &BuiltinTemplate{- ID: t.ID,- Kind: t.Kind,- DisplayName: t.Name,- Description: derefString(t.Description),- Config: config,- })- }- return s.sortBuiltins(out), nil+ return s.sortBuiltins(templates), nil}// LoadWikiPresets loads the wiki page-structure presets from the@@ -307,16 +287,17 @@ func (s *CompilationTemplateService) sortBuiltins(templates []*BuiltinTemplate)return templates}-// seedBuiltins loads the built-in templates from the init_data YAML files and-// upserts them (as a safety net when the DB has no built-ins yet), mirroring-// Python seed_builtins_from_files.-func (s *CompilationTemplateService) seedBuiltins(ctx context.Context) error {+func loadBuiltinTemplates() ([]*BuiltinTemplate, error) {dir := filepath.Join(utility.GetProjectBaseDirectory(),"api", "db", "init_data", "compilation_templates")entries, err := os.ReadDir(dir)if err != nil {- return nil // directory absent is acceptable; nothing to seed+ if os.IsNotExist(err) {+ return []*BuiltinTemplate{}, nil+ }+ return nil, err}+ templates := make([]*BuiltinTemplate, 0, len(entries))for _, entry := range entries {if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".yaml") && !strings.HasSuffix(entry.Name(), ".yml") {continue@@ -338,38 +319,15 @@ func (s *CompilationTemplateService) seedBuiltins(ctx context.Context) error {continue}id := strings.TrimSuffix(entry.Name(), filepath.Ext(entry.Name()))- desc := doc.Description- valid := string(entity.StatusValid)- tmpl := &entity.CompilationTemplate{- ID: id,- Name: doc.DisplayName,- Kind: doc.Kind,- Config: entity.JSONMap(doc.Config),- IsBuiltin: true,- Status: &valid,- }- if doc.Description != "" {- tmpl.Description = &desc- }- _ = s.upsertBuiltin(ctx, tmpl)- }- return nil-}--func (s *CompilationTemplateService) upsertBuiltin(ctx context.Context, t *entity.CompilationTemplate) error {- existing, err := s.templateDAO.GetByID(ctx, dao.DB, t.ID)- if errors.Is(err, gorm.ErrRecordNotFound) {- return s.templateDAO.Save(ctx, dao.DB, t)- }- if err != nil {- return err- }- m := map[string]interface{}{- "name": t.Name, "kind": t.Kind, "config": t.Config,- "description": t.Description, "is_builtin": true,- "status": t.Status,+ templates = append(templates, &BuiltinTemplate{+ ID: id,+ Kind: doc.Kind,+ DisplayName: doc.DisplayName,+ Description: doc.Description,+ Config: entity.JSONMap(doc.Config),+ })}- return s.templateDAO.UpdateFields(ctx, dao.DB, existing.ID, m)+ return templates, nil}func derefString(p *string) string {diff --git a/internal/service/compilation_template_service_test.go b/internal/service/compilation_template_service_test.goindex 710ab3f6d0..feb88d8076 100644--- a/internal/service/compilation_template_service_test.go+++ b/internal/service/compilation_template_service_test.go@@ -3,10 +3,12 @@ package serviceimport ("encoding/json""os"+ "path/filepath""strings""testing""ragflow/internal/entity"+ "ragflow/internal/ingestion/component/knowledge_compiler/common")// TestValidateTemplatePayload_AcceptsJSONMapConfig covers the create-from-UI@@ -152,3 +154,63 @@ func TestLoadWikiPresets_FrontendContract(t *testing.T) {}}}++func TestLoadBuiltinTemplates_AllYAMLDefinitions(t *testing.T) {+ origWd, err := os.Getwd()+ if err != nil {+ t.Fatalf("getwd: %v", err)+ }+ if err = os.Chdir("../.."); err != nil {+ t.Fatalf("chdir to repo root: %v", err)+ }+ t.Cleanup(func() { _ = os.Chdir(origWd) })++ dir := filepath.Join("api", "db", "init_data", "compilation_templates")+ entries, err := os.ReadDir(dir)+ if err != nil {+ t.Fatalf("read builtin template directory: %v", err)+ }+ wantIDs := make(map[string]struct{})+ for _, entry := range entries {+ if entry.IsDir() || filepath.Ext(entry.Name()) != ".yaml" && filepath.Ext(entry.Name()) != ".yml" {+ continue+ }+ wantIDs[strings.TrimSuffix(entry.Name(), filepath.Ext(entry.Name()))] = struct{}{}+ }++ templates, err := loadBuiltinTemplates()+ if err != nil {+ t.Fatalf("loadBuiltinTemplates: %v", err)+ }+ if len(templates) != len(wantIDs) {+ t.Fatalf("loaded %d builtin templates, want %d YAML definitions", len(templates), len(wantIDs))+ }++ for _, template := range templates {+ if _, ok := wantIDs[template.ID]; !ok {+ t.Errorf("loaded unexpected builtin template %q", template.ID)+ }+ if template.Config["kind"] == nil {+ t.Errorf("builtin template %q has no config.kind", template.ID)+ }+ if _, err := common.KindToVariant(template.Kind); err != nil {+ t.Errorf("builtin template %q kind %q is not supported: %v", template.ID, template.Kind, err)+ }+ // Tree intentionally carries empty entity/relation stubs for the shared+ // form shape; those fields are not part of the tree template contract.+ if template.Kind != "tree" {+ payload := map[string]interface{}{+ "name": template.DisplayName,+ "kind": template.Kind,+ "config": map[string]interface{}(template.Config),+ }+ if err := ValidateTemplatePayload(payload, true); err != nil {+ t.Errorf("builtin template %q failed validation: %v", template.ID, err)+ }+ }+ delete(wantIDs, template.ID)+ }+ for id := range wantIDs {+ t.Errorf("builtin YAML %q was not loaded", id)+ }+}diff --git a/internal/service/dataset_artifact_service.go b/internal/service/dataset_artifact_service.goindex 4afd1f9b18..12cb35c739 100644--- a/internal/service/dataset_artifact_service.go+++ b/internal/service/dataset_artifact_service.go@@ -741,7 +741,7 @@ func (s *DatasetArtifactService) valueHasWikiTemplate(ctx context.Context, tenanif id, ok := params["compilation_template_id"].(string); ok && id != "" {var template entity.CompilationTemplateif err := dao.DB.WithContext(ctx).- Where("id = ? AND (tenant_id = ? OR tenant_id IS NULL OR tenant_id = '') AND status = ?", id, tenantID, string(entity.StatusValid)).+ Where("id = ? AND tenant_id = ? AND status = ?", id, tenantID, string(entity.StatusValid)).First(&template).Error; err != nil {if err == gorm.ErrRecordNotFound {return false, nil@@ -790,7 +790,7 @@ func (s *DatasetArtifactService) valueHasWikiTemplate(ctx context.Context, tenanfunc (s *DatasetArtifactService) groupHasWikiTemplate(ctx context.Context, tenantID, groupID string) (bool, error) {var group entity.CompilationTemplateGroupif err := dao.DB.WithContext(ctx).- Where("id = ? AND (tenant_id = ? OR tenant_id = '') AND status = ?", groupID, tenantID, string(entity.StatusValid)).+ Where("id = ? AND tenant_id = ? AND status = ?", groupID, tenantID, string(entity.StatusValid)).First(&group).Error; err != nil {if err == gorm.ErrRecordNotFound {return false, nil