Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

【架构优化】合并 workflows handler 中重复的沙箱档案校验与 revision conflict 响应 (#750)

fx-kurumi01dfcfe615af
diff --git a/internal/httpapi/catalog.go b/internal/httpapi/catalog.go
index a88bfff3..dc2a80ef 100644
--- a/internal/httpapi/catalog.go
+++ b/internal/httpapi/catalog.go
@@ -1049,12 +1049,7 @@ func (h *catalogHandlers) workflows(c *gin.Context) {
c.JSON(http.StatusConflict, map[string]string{"error": "通用模板为只读,请先复制到本项目再修改"})
return
}
- if strings.TrimSpace(v.SandboxProfileID) == "" {
- c.JSON(http.StatusBadRequest, map[string]string{"error": "workflow sandboxProfileId is required"})
- return
- }
- if _, exists := h.repo.Sandbox(v.SandboxProfileID); !exists {
- c.JSON(http.StatusBadRequest, map[string]string{"error": "sandbox profile not found"})
+ if _, ok := h.requireWorkflowSandbox(c, v); !ok {
return
}
v, err := domain.AttachPreviewBuild(v)
@@ -1064,11 +1059,7 @@ func (h *catalogHandlers) workflows(c *gin.Context) {
}
stored, err := h.repo.UpdateGenericWorkflow(v)
if err != nil {
- if strings.Contains(err.Error(), "revision conflict") {
- c.JSON(http.StatusConflict, map[string]string{"error": err.Error()})
- } else {
- respond(c, err)
- }
+ respondWorkflowSaveError(c, err)
return
}
c.JSON(200, stored)
@@ -1083,13 +1074,8 @@ func (h *catalogHandlers) workflows(c *gin.Context) {
c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()})
return
}
- if strings.TrimSpace(v.SandboxProfileID) == "" {
- c.JSON(http.StatusBadRequest, map[string]string{"error": "workflow sandboxProfileId is required"})
- return
- }
- profile, exists := h.repo.Sandbox(v.SandboxProfileID)
- if !exists {
- c.JSON(http.StatusBadRequest, map[string]string{"error": "sandbox profile not found"})
+ profile, ok := h.requireWorkflowSandbox(c, v)
+ if !ok {
return
}
if v.ProjectID != "" && profile.ProjectID != "" && profile.ProjectID != v.ProjectID && !profile.Shared {
@@ -1116,11 +1102,7 @@ func (h *catalogHandlers) workflows(c *gin.Context) {
}
}
if err := h.repo.PutWorkflow(v); err != nil {
- if strings.Contains(err.Error(), "revision conflict") {
- c.JSON(http.StatusConflict, map[string]string{"error": err.Error()})
- } else {
- respond(c, err)
- }
+ respondWorkflowSaveError(c, err)
return
}
stored, _ := h.repo.Workflow(v.ID)
@@ -1128,6 +1110,31 @@ func (h *catalogHandlers) workflows(c *gin.Context) {
}
}
}
+
+// requireWorkflowSandbox 校验 SOP 载荷指定的沙箱档案:标识必填且必须存在。
+// 校验失败时已写好 400 响应,调用方直接返回即可。
+func (h *catalogHandlers) requireWorkflowSandbox(c *gin.Context, wf domain.WorkflowDefinition) (domain.SandboxProfile, bool) {
+ if strings.TrimSpace(wf.SandboxProfileID) == "" {
+ c.JSON(http.StatusBadRequest, map[string]string{"error": "workflow sandboxProfileId is required"})
+ return domain.SandboxProfile{}, false
+ }
+ profile, exists := h.repo.Sandbox(wf.SandboxProfileID)
+ if !exists {
+ c.JSON(http.StatusBadRequest, map[string]string{"error": "sandbox profile not found"})
+ return domain.SandboxProfile{}, false
+ }
+ return profile, true
+}
+
+// respondWorkflowSaveError 统一 SOP 保存失败响应:版本冲突回 409,其余交给 respond。
+func respondWorkflowSaveError(c *gin.Context, err error) {
+ if strings.Contains(err.Error(), "revision conflict") {
+ c.JSON(http.StatusConflict, map[string]string{"error": err.Error()})
+ return
+ }
+ respond(c, err)
+}
+
func (h *catalogHandlers) workflow(c *gin.Context) {
id := c.Param("id")
// Generic template ids are reserved: the built-in catalog always wins so a
diff --git a/internal/httpapi/workflow_sandbox_validation_test.go b/internal/httpapi/workflow_sandbox_validation_test.go
new file mode 100644
index 00000000..66519354
--- /dev/null
+++ b/internal/httpapi/workflow_sandbox_validation_test.go
@@ -0,0 +1,45 @@
+package httpapi
+
+import (
+ "net/http"
+ "path/filepath"
+ "testing"
+
+ "agentflow/internal/domain"
+ "agentflow/internal/engine"
+ "agentflow/internal/repo"
+ "agentflow/internal/runtime"
+ "agentflow/internal/tracker"
+)
+
+// TestWorkflowSaveRequiresExistingSandboxProfile 校验 SOP 保存路径上的沙箱档案检查:
+// 标识缺失与档案不存在都由 requireWorkflowSandbox 统一回 400,且文案保持不变。
+func TestWorkflowSaveRequiresExistingSandboxProfile(t *testing.T) {
+ s, err := repo.OpenTest()
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(func() { s.Close() })
+ if err := s.PutProject(domain.Project{ID: "alpha", Key: "ALPHA", Name: "Alpha"}); err != nil {
+ t.Fatal(err)
+ }
+ h := New(s, engine.New(s, runtime.NewManager(filepath.Join(t.TempDir(), "workspaces"))), tracker.NewService(), "", "")
+
+ missingID := requestJSON[map[string]string](t, h, http.MethodPut, "/api/workflows", domain.WorkflowDefinition{
+ ID: "alpha-flow", Name: "Alpha flow", ProjectID: "alpha",
+ }, http.StatusBadRequest)
+ if missingID["error"] != "workflow sandboxProfileId is required" {
+ t.Fatalf("missing sandboxProfileId error = %q", missingID["error"])
+ }
+
+ unknownID := requestJSON[map[string]string](t, h, http.MethodPut, "/api/workflows", domain.WorkflowDefinition{
+ ID: "alpha-flow", Name: "Alpha flow", ProjectID: "alpha", SandboxProfileID: "ghost-sandbox",
+ }, http.StatusBadRequest)
+ if unknownID["error"] != "sandbox profile not found" {
+ t.Fatalf("unknown sandboxProfileId error = %q", unknownID["error"])
+ }
+
+ if _, exists := s.Workflow("alpha-flow"); exists {
+ t.Fatal("rejected workflow must not be stored")
+ }
+}
diff --git a/internal/httpapi/workflow_save_error_guard_test.go b/internal/httpapi/workflow_save_error_guard_test.go
new file mode 100644
index 00000000..dc6ac2a4
--- /dev/null
+++ b/internal/httpapi/workflow_save_error_guard_test.go
@@ -0,0 +1,100 @@
+package httpapi
+
+import (
+ "encoding/json"
+ "errors"
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "testing"
+
+ "agentflow/internal/domain"
+ "agentflow/internal/repo"
+
+ "github.com/gin-gonic/gin"
+)
+
+// The workflows handler now routes both save paths through
+// respondWorkflowSaveError and requireWorkflowSandbox. These guard the parts
+// the PR tests leave open: only revision conflicts map to 409, the generic
+// template path keeps its 409, and a blank-but-not-empty sandbox id is still
+// reported as missing rather than "not found".
+
+func TestRespondWorkflowSaveErrorOnlyMapsRevisionConflictTo409(t *testing.T) {
+ gin.SetMode(gin.TestMode)
+ cases := []struct {
+ name string
+ err error
+ code int
+ msg string
+ }{
+ {"revision conflict", errors.New("workflow revision conflict: current revision is 3"), http.StatusConflict, "workflow revision conflict: current revision is 3"},
+ {"plain error falls back to respond", errors.New("disk full"), http.StatusBadRequest, "disk full"},
+ {"typed http error keeps its status", HTTPError{Status: http.StatusForbidden, Message: "forbidden"}, http.StatusForbidden, "forbidden"},
+ }
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ rec := httptest.NewRecorder()
+ c, _ := gin.CreateTestContext(rec)
+ respondWorkflowSaveError(c, tc.err)
+ if rec.Code != tc.code {
+ t.Fatalf("status = %d want %d: %s", rec.Code, tc.code, rec.Body.String())
+ }
+ var body map[string]string
+ if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
+ t.Fatal(err)
+ }
+ if body["error"] != tc.msg {
+ t.Fatalf("error = %q want %q", body["error"], tc.msg)
+ }
+ })
+ }
+}
+
+func TestGenericWorkflowUpdateRevisionConflictReturns409(t *testing.T) {
+ t.Setenv("AGENTFLOW_PLATFORM_ADMINS", "example-admin")
+ f := cvBNewHTTPFixture(t)
+ if err := repo.SeedTestGenericTemplates(f.s); err != nil {
+ t.Fatal(err)
+ }
+ if err := f.s.PutUser(domain.User{LoginName: "example-admin", ChineseName: "Admin"}); err != nil {
+ t.Fatal(err)
+ }
+ admin, _ := f.s.UserByLoginName("example-admin")
+ f.user = admin
+
+ template, ok := f.s.GenericTemplateByWorkflow(repo.GenericBackendDeliveryWorkflowID)
+ if !ok {
+ t.Fatal("generic template not seeded")
+ }
+ stale := template.Workflow
+ if strings.TrimSpace(stale.SandboxProfileID) == "" {
+ t.Fatal("seeded generic workflow has no sandbox profile")
+ }
+ current := stale
+ current.Name += " current"
+ if rec := cvBRequest(t, f, http.MethodPut, "/api/workflows", current, true); rec.Code != http.StatusOK {
+ t.Fatalf("generic update = %d: %s", rec.Code, rec.Body.String())
+ }
+ stale.Name += " stale"
+ rec := cvBRequest(t, f, http.MethodPut, "/api/workflows", stale, true)
+ if rec.Code != http.StatusConflict || !strings.Contains(rec.Body.String(), "revision conflict") {
+ t.Fatalf("stale generic update = %d want 409 revision conflict: %s", rec.Code, rec.Body.String())
+ }
+}
+
+func TestWorkflowSaveTreatsBlankSandboxProfileIDAsMissing(t *testing.T) {
+ f := cvBNewHTTPFixture(t)
+ if err := f.s.PutProject(domain.Project{ID: "blank-box", Key: "BLANK", Name: "Blank"}); err != nil {
+ t.Fatal(err)
+ }
+ rec := cvBRequest(t, f, http.MethodPut, "/api/workflows", domain.WorkflowDefinition{
+ ID: "blank-box-flow", Name: "Blank flow", ProjectID: "blank-box", SandboxProfileID: " ",
+ }, true)
+ if rec.Code != http.StatusBadRequest || !strings.Contains(rec.Body.String(), "workflow sandboxProfileId is required") {
+ t.Fatalf("blank sandboxProfileId = %d: %s", rec.Code, rec.Body.String())
+ }
+ if _, exists := f.s.Workflow("blank-box-flow"); exists {
+ t.Fatal("rejected workflow must not be stored")
+ }
+}