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.

refactor(engine): split validate into validateNode and validateDAG (#381)

fx-kurumi86494ff22dfa
diff --git a/internal/engine/workflow_graph.go b/internal/engine/workflow_graph.go
index d3e8018d..d4d299c3 100644
--- a/internal/engine/workflow_graph.go
+++ b/internal/engine/workflow_graph.go
@@ -168,52 +168,8 @@ func validate(wf domain.WorkflowDefinition) error {
return fmt.Errorf("duplicate node %s", n.ID)
}
ids[n.ID] = true
- switch n.Type {
- case "agent", "approval":
- case "sop":
- if strings.TrimSpace(n.SOPWorkflowID) == "" {
- return fmt.Errorf("node %s sopWorkflowId is required", n.ID)
- }
- if strings.TrimSpace(n.SOPWorkflowID) == strings.TrimSpace(wf.ID) {
- return fmt.Errorf("node %s cannot reference its own workflow", n.ID)
- }
- case "script", "command":
- language := n.ScriptLanguage
- script := n.Script
- if n.Type == "command" {
- language, script = "shell", n.Command
- }
- if language == "" {
- language = "shell"
- }
- if language != "shell" && language != "python" {
- return fmt.Errorf("node %s has invalid scriptLanguage %q", n.ID, language)
- }
- if strings.TrimSpace(script) == "" {
- return fmt.Errorf("node %s script is required", n.ID)
- }
- default:
- return fmt.Errorf("node %s has invalid type %q", n.ID, n.Type)
- }
- if n.OnFailure != "" && n.OnFailure != "fail" && n.OnFailure != "reject" && n.OnFailure != "human" && n.OnFailure != "loop" {
- return fmt.Errorf("node %s has invalid onFailure %q", n.ID, n.OnFailure)
- }
- if scope := strings.TrimSpace(n.ExecutionScope); scope != "" && scope != domain.ExecutionScopeRun && scope != domain.ExecutionScopeRepositories {
- return fmt.Errorf("node %s has invalid executionScope %q", n.ID, scope)
- }
- if n.RetryStrategy != "" && n.RetryStrategy != "targeted_fix" && n.RetryStrategy != "full_redo" {
- return fmt.Errorf("node %s has invalid retryStrategy %q", n.ID, n.RetryStrategy)
- }
- artifactNames := map[string]bool{}
- for _, contract := range n.ArtifactOutputs {
- clean := filepath.Clean(contract.Path)
- if contract.Name == "" || clean == "." || filepath.IsAbs(clean) || strings.HasPrefix(clean, "..") {
- return fmt.Errorf("node %s has invalid artifact contract %q", n.ID, contract.Name)
- }
- if artifactNames[contract.Name] {
- return fmt.Errorf("node %s has duplicate artifact %s", n.ID, contract.Name)
- }
- artifactNames[contract.Name] = true
+ if err := validateNode(wf.ID, n); err != nil {
+ return err
}
}
for _, n := range wf.Nodes {
@@ -224,9 +180,69 @@ func validate(wf domain.WorkflowDefinition) error {
return fmt.Errorf("node %s reject target is required", n.ID)
}
}
+ return validateDAG(ids, wf.Edges)
+}
+
+// validateNode checks one node's own fields: its type-specific settings, the
+// onFailure/executionScope/retryStrategy enums and its artifact contracts.
+// workflowID guards sop nodes against referencing their own workflow.
+func validateNode(workflowID string, n domain.WorkflowNode) error {
+ switch n.Type {
+ case "agent", "approval":
+ case "sop":
+ if strings.TrimSpace(n.SOPWorkflowID) == "" {
+ return fmt.Errorf("node %s sopWorkflowId is required", n.ID)
+ }
+ if strings.TrimSpace(n.SOPWorkflowID) == strings.TrimSpace(workflowID) {
+ return fmt.Errorf("node %s cannot reference its own workflow", n.ID)
+ }
+ case "script", "command":
+ language := n.ScriptLanguage
+ script := n.Script
+ if n.Type == "command" {
+ language, script = "shell", n.Command
+ }
+ if language == "" {
+ language = "shell"
+ }
+ if language != "shell" && language != "python" {
+ return fmt.Errorf("node %s has invalid scriptLanguage %q", n.ID, language)
+ }
+ if strings.TrimSpace(script) == "" {
+ return fmt.Errorf("node %s script is required", n.ID)
+ }
+ default:
+ return fmt.Errorf("node %s has invalid type %q", n.ID, n.Type)
+ }
+ if n.OnFailure != "" && n.OnFailure != "fail" && n.OnFailure != "reject" && n.OnFailure != "human" && n.OnFailure != "loop" {
+ return fmt.Errorf("node %s has invalid onFailure %q", n.ID, n.OnFailure)
+ }
+ if scope := strings.TrimSpace(n.ExecutionScope); scope != "" && scope != domain.ExecutionScopeRun && scope != domain.ExecutionScopeRepositories {
+ return fmt.Errorf("node %s has invalid executionScope %q", n.ID, scope)
+ }
+ if n.RetryStrategy != "" && n.RetryStrategy != "targeted_fix" && n.RetryStrategy != "full_redo" {
+ return fmt.Errorf("node %s has invalid retryStrategy %q", n.ID, n.RetryStrategy)
+ }
+ artifactNames := map[string]bool{}
+ for _, contract := range n.ArtifactOutputs {
+ clean := filepath.Clean(contract.Path)
+ if contract.Name == "" || clean == "." || filepath.IsAbs(clean) || strings.HasPrefix(clean, "..") {
+ return fmt.Errorf("node %s has invalid artifact contract %q", n.ID, contract.Name)
+ }
+ if artifactNames[contract.Name] {
+ return fmt.Errorf("node %s has duplicate artifact %s", n.ID, contract.Name)
+ }
+ artifactNames[contract.Name] = true
+ }
+ return nil
+}
+
+// validateDAG rejects edges that reference unknown node ids and, via Kahn's
+// topological sort, any cycle among the nodes in ids.
+func validateDAG(ids map[string]bool, edges []domain.WorkflowEdge) error {
indegree := map[string]int{}
adj := map[string][]string{}
- for _, e := range wf.Edges {
+ for _, e := range edges {
if !ids[e.Source] || !ids[e.Target] {
return fmt.Errorf("edge references missing node")
}
diff --git a/internal/engine/workflow_validate_split_test.go b/internal/engine/workflow_validate_split_test.go
new file mode 100644
index 00000000..3f3dc460
--- /dev/null
+++ b/internal/engine/workflow_validate_split_test.go
@@ -0,0 +1,62 @@
+package engine
+
+import (
+ "strings"
+ "testing"
+
+ "agentflow/internal/domain"
+)
+
+func TestValidateNodeChecksOwnFields(t *testing.T) {
+ ok := []domain.WorkflowNode{
+ {ID: "a", Type: "agent"},
+ {ID: "b", Type: "approval", OnFailure: "human", RetryStrategy: "full_redo"},
+ {ID: "c", Type: "sop", SOPWorkflowID: "other"},
+ {ID: "d", Type: "script", Script: "echo ok"},
+ {ID: "e", Type: "command", Command: "echo ok", ScriptLanguage: "ruby"},
+ {ID: "f", Type: "agent", ExecutionScope: domain.ExecutionScopeRepositories, ArtifactOutputs: []domain.ArtifactContract{{Name: "r", Path: "out/r.md"}}},
+ }
+ for _, n := range ok {
+ if err := validateNode("wf", n); err != nil {
+ t.Fatalf("node %s rejected: %v", n.ID, err)
+ }
+ }
+ bad := []struct {
+ node domain.WorkflowNode
+ want string
+ }{
+ {domain.WorkflowNode{ID: "a", Type: "sop", SOPWorkflowID: " "}, "node a sopWorkflowId is required"},
+ {domain.WorkflowNode{ID: "a", Type: "sop", SOPWorkflowID: " wf "}, "node a cannot reference its own workflow"},
+ {domain.WorkflowNode{ID: "a", Type: "script", ScriptLanguage: "ruby", Script: "x"}, `node a has invalid scriptLanguage "ruby"`},
+ {domain.WorkflowNode{ID: "a", Type: "command"}, "node a script is required"},
+ {domain.WorkflowNode{ID: "a", Type: "bad"}, `node a has invalid type "bad"`},
+ {domain.WorkflowNode{ID: "a", Type: "agent", OnFailure: "bad"}, `node a has invalid onFailure "bad"`},
+ {domain.WorkflowNode{ID: "a", Type: "agent", ExecutionScope: " bad "}, `node a has invalid executionScope "bad"`},
+ {domain.WorkflowNode{ID: "a", Type: "agent", RetryStrategy: "bad"}, `node a has invalid retryStrategy "bad"`},
+ {domain.WorkflowNode{ID: "a", Type: "agent", ArtifactOutputs: []domain.ArtifactContract{{Name: "x", Path: "/abs"}}}, `node a has invalid artifact contract "x"`},
+ {domain.WorkflowNode{ID: "a", Type: "agent", ArtifactOutputs: []domain.ArtifactContract{{Name: "x", Path: "a"}, {Name: "x", Path: "b"}}}, "node a has duplicate artifact x"},
+ }
+ for i, tc := range bad {
+ err := validateNode("wf", tc.node)
+ if err == nil || err.Error() != tc.want {
+ t.Fatalf("case %d: err = %v, want %q", i, err, tc.want)
+ }
+ }
+}
+
+func TestValidateDAGRejectsMissingNodesAndCycles(t *testing.T) {
+ ids := map[string]bool{"a": true, "b": true, "c": true}
+ if err := validateDAG(ids, nil); err != nil {
+ t.Fatalf("edgeless graph rejected: %v", err)
+ }
+ if err := validateDAG(ids, []domain.WorkflowEdge{{Source: "a", Target: "b"}, {Source: "a", Target: "c"}, {Source: "b", Target: "c"}}); err != nil {
+ t.Fatalf("diamond DAG rejected: %v", err)
+ }
+ if err := validateDAG(ids, []domain.WorkflowEdge{{Source: "a", Target: "missing"}}); err == nil || err.Error() != "edge references missing node" {
+ t.Fatalf("missing target err = %v", err)
+ }
+ cycle := []domain.WorkflowEdge{{Source: "a", Target: "b"}, {Source: "b", Target: "c"}, {Source: "c", Target: "b"}}
+ if err := validateDAG(ids, cycle); err == nil || !strings.Contains(err.Error(), "workflow must be a DAG") {
+ t.Fatalf("cycle err = %v", err)
+ }
+}