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.

【架构优化】从 assistantDraftWarnings 中拆出 SOP 删除前检查 workflowDeleteWarnings (#535)

fx-kurumi5040c1b9624f
diff --git a/internal/httpapi/assistant_approval.go b/internal/httpapi/assistant_approval.go
index 5e28a18a..4b9a3b26 100644
--- a/internal/httpapi/assistant_approval.go
+++ b/internal/httpapi/assistant_approval.go
@@ -296,38 +296,44 @@ func (h *assistantHandlers) assistantDraftWarnings(d assistantDraft) []string {
}
return sharedAssetWarnings("沙箱", item.Shared, h.workflowNamesReferencingSandbox(id), "流程")
case "workflows_delete":
- workflow, ok := h.repo.Workflow(id)
- if !ok {
- return []string{fmt.Sprintf("SOP %s 不存在,执行将失败", id)}
- }
- out := []string{}
- if workflow.Generic {
- out = append(out, "通用模板不可删除,如需修改请直接保存新版本")
- }
- running := 0
- for _, run := range h.repo.Runs() {
- if run.WorkflowID == id && (run.Status == "running" || run.Status == "waiting") {
- running++
- }
- }
- if running > 0 {
- out = append(out, fmt.Sprintf("该 SOP 还有 %d 个进行中的运行,请先终止后再删除", running))
- }
- var boundIssues []string
- for _, issue := range h.repo.Issues() {
- if issue.WorkflowID == id && issue.Status != "closed" {
- boundIssues = append(boundIssues, issue.ID)
- }
- }
- if len(boundIssues) > 0 {
- out = append(out, fmt.Sprintf("该 SOP 仍被 %d 个未关闭的 Issue 绑定(%s),请先改绑", len(boundIssues), strings.Join(boundIssues, "、")))
- }
- return out
+ return h.workflowDeleteWarnings(id)
default:
return nil
}
}
+// workflowDeleteWarnings 汇总删除 SOP 前的冲突:通用模板不可删、仍有进行中的
+// 运行、仍被未关闭的 Issue 绑定。
+func (h *assistantHandlers) workflowDeleteWarnings(id string) []string {
+ workflow, ok := h.repo.Workflow(id)
+ if !ok {
+ return []string{fmt.Sprintf("SOP %s 不存在,执行将失败", id)}
+ }
+ out := []string{}
+ if workflow.Generic {
+ out = append(out, "通用模板不可删除,如需修改请直接保存新版本")
+ }
+ running := 0
+ for _, run := range h.repo.Runs() {
+ if run.WorkflowID == id && (run.Status == "running" || run.Status == "waiting") {
+ running++
+ }
+ }
+ if running > 0 {
+ out = append(out, fmt.Sprintf("该 SOP 还有 %d 个进行中的运行,请先终止后再删除", running))
+ }
+ var boundIssues []string
+ for _, issue := range h.repo.Issues() {
+ if issue.WorkflowID == id && issue.Status != "closed" {
+ boundIssues = append(boundIssues, issue.ID)
+ }
+ }
+ if len(boundIssues) > 0 {
+ out = append(out, fmt.Sprintf("该 SOP 仍被 %d 个未关闭的 Issue 绑定(%s),请先改绑", len(boundIssues), strings.Join(boundIssues, "、")))
+ }
+ return out
+}
+
func sharedAssetWarnings(kind string, shared bool, refNames []string, refKind string) []string {
out := []string{}
if shared {
diff --git a/internal/httpapi/assistant_workflow_delete_warnings_generic_test.go b/internal/httpapi/assistant_workflow_delete_warnings_generic_test.go
new file mode 100644
index 00000000..830e9f37
--- /dev/null
+++ b/internal/httpapi/assistant_workflow_delete_warnings_generic_test.go
@@ -0,0 +1,71 @@
+package httpapi
+
+import (
+ "strings"
+ "testing"
+
+ "agentflow/internal/domain"
+ "agentflow/internal/repo"
+)
+
+// A generic template must be flagged as undeletable, and runs / issues bound
+// to a different SOP must not leak into its counts.
+func TestWorkflowDeleteWarningsGenericTemplateAndOtherWorkflowsIgnored(t *testing.T) {
+ st, err := repo.OpenTest()
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(func() { st.Close() })
+ if err := st.PutProject(domain.Project{ID: "p", Name: "P"}); err != nil {
+ t.Fatal(err)
+ }
+ if err := st.PutGenericTemplate(repo.GenericTemplate{Workflow: domain.WorkflowDefinition{
+ ID: "gen-wf", Name: "通用", Version: 1, Status: "published", Generic: true,
+ }}); err != nil {
+ t.Fatal(err)
+ }
+ if err := st.PutWorkflow(domain.WorkflowDefinition{ID: "other", Name: "Other", ProjectID: "p"}); err != nil {
+ t.Fatal(err)
+ }
+ h := &assistantHandlers{deps: &deps{repo: st}}
+
+ warnings := h.workflowDeleteWarnings("gen-wf")
+ if len(warnings) != 1 || !strings.Contains(warnings[0], "通用模板不可删除") {
+ t.Fatalf("generic template warnings = %#v", warnings)
+ }
+
+ // Active runs and open issues on another SOP do not count against gen-wf
+ // or a plain unused SOP.
+ for _, run := range []domain.Run{
+ {ID: "r-other", WorkflowID: "other", Status: "running", Nodes: map[string]*domain.NodeRun{}},
+ {ID: "r-gen", WorkflowID: "gen-wf", Status: "waiting", Nodes: map[string]*domain.NodeRun{}},
+ } {
+ if err := st.PutRun(run); err != nil {
+ t.Fatal(err)
+ }
+ }
+ if err := st.PutIssue(domain.Issue{ID: "i-other", ProjectID: "p", Title: "o", Kind: "requirement", Source: "local", Status: "todo", WorkflowID: "other"}); err != nil {
+ t.Fatal(err)
+ }
+
+ warnings = h.workflowDeleteWarnings("gen-wf")
+ if len(warnings) != 2 {
+ t.Fatalf("generic template with a waiting run warnings = %#v", warnings)
+ }
+ if !strings.Contains(warnings[0], "通用模板不可删除") || !strings.Contains(warnings[1], "还有 1 个进行中的运行") {
+ t.Fatalf("warnings order/content = %#v", warnings)
+ }
+ for _, w := range warnings {
+ if strings.Contains(w, "Issue") {
+ t.Fatalf("issue bound to another SOP leaked into gen-wf warnings: %#v", warnings)
+ }
+ }
+
+ otherWarnings := h.workflowDeleteWarnings("other")
+ if len(otherWarnings) != 2 || strings.Contains(strings.Join(otherWarnings, "|"), "通用模板") {
+ t.Fatalf("other workflow warnings = %#v", otherWarnings)
+ }
+ if !strings.Contains(otherWarnings[0], "还有 1 个进行中的运行") || !strings.Contains(otherWarnings[1], "1 个未关闭的 Issue 绑定(i-other)") {
+ t.Fatalf("other workflow warnings = %#v", otherWarnings)
+ }
+}
diff --git a/internal/httpapi/assistant_workflow_delete_warnings_test.go b/internal/httpapi/assistant_workflow_delete_warnings_test.go
new file mode 100644
index 00000000..4301425e
--- /dev/null
+++ b/internal/httpapi/assistant_workflow_delete_warnings_test.go
@@ -0,0 +1,63 @@
+package httpapi
+
+import (
+ "strings"
+ "testing"
+
+ "agentflow/internal/domain"
+ "agentflow/internal/repo"
+)
+
+func TestWorkflowDeleteWarnings(t *testing.T) {
+ st, err := repo.OpenTest()
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(func() { st.Close() })
+ if err := st.PutProject(domain.Project{ID: "p", Name: "P"}); err != nil {
+ t.Fatal(err)
+ }
+ h := &assistantHandlers{deps: &deps{repo: st}}
+
+ if warnings := h.workflowDeleteWarnings("no-such"); len(warnings) != 1 || !strings.Contains(warnings[0], "不存在") {
+ t.Fatalf("missing workflow warnings = %#v", warnings)
+ }
+
+ if err := st.PutWorkflow(domain.WorkflowDefinition{ID: "wf", Name: "WF", ProjectID: "p"}); err != nil {
+ t.Fatal(err)
+ }
+ if warnings := h.workflowDeleteWarnings("wf"); len(warnings) != 0 {
+ t.Fatalf("unused workflow should have no warnings: %#v", warnings)
+ }
+
+ for _, run := range []domain.Run{
+ {ID: "r-running", WorkflowID: "wf", Status: "running", Nodes: map[string]*domain.NodeRun{}},
+ {ID: "r-waiting", WorkflowID: "wf", Status: "waiting", Nodes: map[string]*domain.NodeRun{}},
+ {ID: "r-done", WorkflowID: "wf", Status: "succeeded", Nodes: map[string]*domain.NodeRun{}},
+ } {
+ if err := st.PutRun(run); err != nil {
+ t.Fatal(err)
+ }
+ }
+ for _, issue := range []domain.Issue{
+ {ID: "i-open", ProjectID: "p", Title: "open", Kind: "requirement", Source: "local", Status: "todo", WorkflowID: "wf"},
+ {ID: "i-closed", ProjectID: "p", Title: "closed", Kind: "requirement", Source: "local", Status: "closed", WorkflowID: "wf"},
+ } {
+ if err := st.PutIssue(issue); err != nil {
+ t.Fatal(err)
+ }
+ }
+ warnings := h.workflowDeleteWarnings("wf")
+ if len(warnings) != 2 {
+ t.Fatalf("warnings = %#v", warnings)
+ }
+ if !strings.Contains(warnings[0], "还有 2 个进行中的运行") {
+ t.Fatalf("running warning = %q", warnings[0])
+ }
+ if !strings.Contains(warnings[1], "1 个未关闭的 Issue 绑定(i-open)") {
+ t.Fatalf("bound issue warning = %q", warnings[1])
+ }
+ if got := h.assistantDraftWarnings(assistantDraft{Tool: "workflows_delete", Query: map[string]any{"id": "wf"}}); strings.Join(got, "|") != strings.Join(warnings, "|") {
+ t.Fatalf("assistantDraftWarnings = %#v, want %#v", got, warnings)
+ }
+}