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): 合并 hasWaiting/hasFailed/cancelledNode 重复的节点状态查找 (#296)

fx-kurumi84abc3b4d54b
diff --git a/internal/engine/node_status_lookup_test.go b/internal/engine/node_status_lookup_test.go
new file mode 100644
index 00000000..77ec8de0
--- /dev/null
+++ b/internal/engine/node_status_lookup_test.go
@@ -0,0 +1,49 @@
+package engine
+
+import (
+ "testing"
+
+ "agentflow/internal/domain"
+)
+
+func TestFirstNodeWithStatus(t *testing.T) {
+ wf := domain.WorkflowDefinition{Nodes: []domain.WorkflowNode{{ID: "a"}, {ID: "b"}, {ID: "c"}}}
+ run := func(states map[string]string) domain.Run {
+ r := domain.Run{Nodes: map[string]*domain.NodeRun{}}
+ for id, status := range states {
+ r.Nodes[id] = &domain.NodeRun{Status: status}
+ }
+ return r
+ }
+ cases := []struct {
+ name string
+ states map[string]string
+ status string
+ wantID string
+ wantOK bool
+ }{
+ {"no match", map[string]string{"a": "succeeded", "b": "running"}, "failed", "", false},
+ {"single match", map[string]string{"a": "succeeded", "b": "failed"}, "failed", "b", true},
+ {"definition order wins", map[string]string{"a": "cancelled", "c": "cancelled"}, "cancelled", "a", true},
+ {"undefined run node ignored", map[string]string{"x": "waiting_approval"}, "waiting_approval", "", false},
+ {"missing run nodes", nil, "failed", "", false},
+ }
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ got, ok := firstNodeWithStatus(wf, run(tc.states), tc.status)
+ if ok != tc.wantOK || got.ID != tc.wantID {
+ t.Fatalf("firstNodeWithStatus = (%q, %v), want (%q, %v)", got.ID, ok, tc.wantID, tc.wantOK)
+ }
+ })
+ }
+ r := run(map[string]string{"a": "waiting_approval", "b": "failed", "c": "cancelled"})
+ if !hasWaiting(wf, r) || !hasFailed(wf, r) {
+ t.Fatal("hasWaiting/hasFailed should report matching nodes")
+ }
+ if n, ok := cancelledNode(wf, r); !ok || n.ID != "c" {
+ t.Fatalf("cancelledNode = (%q, %v), want (c, true)", n.ID, ok)
+ }
+ if hasWaiting(wf, run(nil)) || hasFailed(wf, run(nil)) {
+ t.Fatal("empty run should report no waiting/failed nodes")
+ }
+}
diff --git a/internal/engine/workflow_graph.go b/internal/engine/workflow_graph.go
index ed648d3b..d3e8018d 100644
--- a/internal/engine/workflow_graph.go
+++ b/internal/engine/workflow_graph.go
@@ -124,33 +124,32 @@ func allSucceeded(wf domain.WorkflowDefinition, r domain.Run) bool {
return true
}
-func hasWaiting(wf domain.WorkflowDefinition, r domain.Run) bool {
+// firstNodeWithStatus returns the first node, in workflow definition order,
+// whose run state has the given status. Run nodes absent from the definition
+// are ignored.
+func firstNodeWithStatus(wf domain.WorkflowDefinition, r domain.Run, status string) (domain.WorkflowNode, bool) {
for _, definition := range wf.Nodes {
- if n := r.Nodes[definition.ID]; n != nil && n.Status == "waiting_approval" {
- return true
+ if n := r.Nodes[definition.ID]; n != nil && n.Status == status {
+ return definition, true
}
}
- return false
+ return domain.WorkflowNode{}, false
+}
+
+func hasWaiting(wf domain.WorkflowDefinition, r domain.Run) bool {
+ _, ok := firstNodeWithStatus(wf, r, "waiting_approval")
+ return ok
}
// cancelledNode returns a node stopped by a cancel that nothing will run
// again unless an operator resumes it.
func cancelledNode(wf domain.WorkflowDefinition, r domain.Run) (domain.WorkflowNode, bool) {
- for _, definition := range wf.Nodes {
- if n := r.Nodes[definition.ID]; n != nil && n.Status == "cancelled" {
- return definition, true
- }
- }
- return domain.WorkflowNode{}, false
+ return firstNodeWithStatus(wf, r, "cancelled")
}
func hasFailed(wf domain.WorkflowDefinition, r domain.Run) bool {
- for _, definition := range wf.Nodes {
- if n := r.Nodes[definition.ID]; n != nil && n.Status == "failed" {
- return true
- }
- }
- return false
+ _, ok := firstNodeWithStatus(wf, r, "failed")
+ return ok
}
func validate(wf domain.WorkflowDefinition) error {