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(runeval): split applyGateList into gate ID, remove and upsert helpers (#360)

fx-kurumic65d8f47bb37
diff --git a/internal/runeval/apply.go b/internal/runeval/apply.go
index e1ffa01b..a5cde9cb 100644
--- a/internal/runeval/apply.go
+++ b/internal/runeval/apply.go
@@ -113,6 +113,19 @@ func upsertAgentGate(agent *domain.AgentDefinition, rec domain.RunEvalRecommenda
}
func applyGateList(gates []domain.VerificationGate, rec domain.RunEvalRecommendation, remove bool) ([]domain.VerificationGate, error) {
+ id := recommendationGateID(rec)
+ if id == "" {
+ return nil, fmt.Errorf("gate id is required")
+ }
+ if remove {
+ return removeGate(gates, id)
+ }
+ return upsertGate(gates, id, rec)
+}
+
+// recommendationGateID resolves the target gate ID; an explicit Gate.ID wins
+// over set.gateId.
+func recommendationGateID(rec domain.RunEvalRecommendation) string {
id := ""
if rec.Set != nil {
id, _ = stringFrom(rec.Set, "gateId")
@@ -120,24 +133,26 @@ func applyGateList(gates []domain.VerificationGate, rec domain.RunEvalRecommenda
if rec.Gate != nil && strings.TrimSpace(rec.Gate.ID) != "" {
id = strings.TrimSpace(rec.Gate.ID)
}
- if id == "" {
- return nil, fmt.Errorf("gate id is required")
- }
- if remove {
- out := gates[:0]
- found := false
- for _, gate := range gates {
- if gate.ID == id {
- found = true
- continue
- }
- out = append(out, gate)
- }
- if !found {
- return gates, fmt.Errorf("gate %q not found", id)
+ return id
+}
+
+func removeGate(gates []domain.VerificationGate, id string) ([]domain.VerificationGate, error) {
+ out := gates[:0]
+ found := false
+ for _, gate := range gates {
+ if gate.ID == id {
+ found = true
+ continue
}
- return out, nil
+ out = append(out, gate)
+ }
+ if !found {
+ return gates, fmt.Errorf("gate %q not found", id)
}
+ return out, nil
+}
+
+func upsertGate(gates []domain.VerificationGate, id string, rec domain.RunEvalRecommendation) ([]domain.VerificationGate, error) {
gate := domain.VerificationGate{ID: id, Mode: "command", Required: true}
if rec.Gate != nil {
gate = *rec.Gate
diff --git a/internal/runeval/apply_gate_test.go b/internal/runeval/apply_gate_test.go
new file mode 100644
index 00000000..e101a5ea
--- /dev/null
+++ b/internal/runeval/apply_gate_test.go
@@ -0,0 +1,61 @@
+package runeval
+
+import (
+ "testing"
+
+ "agentflow/internal/domain"
+)
+
+func TestRecommendationGateIDPrefersGateID(t *testing.T) {
+ cases := []struct {
+ name string
+ rec domain.RunEvalRecommendation
+ want string
+ }{
+ {"empty", domain.RunEvalRecommendation{}, ""},
+ {"set only", domain.RunEvalRecommendation{Set: map[string]any{"gateId": " g1 "}}, "g1"},
+ {"gate wins", domain.RunEvalRecommendation{Set: map[string]any{"gateId": "g1"}, Gate: &domain.VerificationGate{ID: " g2 "}}, "g2"},
+ {"blank gate falls back", domain.RunEvalRecommendation{Set: map[string]any{"gateId": "g1"}, Gate: &domain.VerificationGate{ID: " "}}, "g1"},
+ }
+ for _, tc := range cases {
+ if got := recommendationGateID(tc.rec); got != tc.want {
+ t.Errorf("%s: got %q want %q", tc.name, got, tc.want)
+ }
+ }
+}
+
+func TestRemoveGate(t *testing.T) {
+ gates := []domain.VerificationGate{{ID: "a"}, {ID: "b"}, {ID: "c"}}
+ out, err := removeGate(gates, "b")
+ if err != nil || len(out) != 2 || out[0].ID != "a" || out[1].ID != "c" {
+ t.Fatalf("remove b: %+v %v", out, err)
+ }
+ orig := []domain.VerificationGate{{ID: "a"}}
+ out, err = removeGate(orig, "missing")
+ if err == nil || err.Error() != `gate "missing" not found` || len(out) != 1 {
+ t.Fatalf("remove missing: %+v %v", out, err)
+ }
+}
+
+func TestUpsertGate(t *testing.T) {
+ out, err := upsertGate(nil, "new", domain.RunEvalRecommendation{})
+ if err != nil || len(out) != 1 || out[0].ID != "new" || out[0].Mode != "command" || !out[0].Required {
+ t.Fatalf("default gate: %+v %v", out, err)
+ }
+ gates := []domain.VerificationGate{{ID: "x", Name: "old"}, {ID: "y"}}
+ out, err = upsertGate(gates, "x", domain.RunEvalRecommendation{Gate: &domain.VerificationGate{ID: "ignored", Name: "new", OnFailure: "loop"}})
+ if err != nil || len(out) != 2 || out[0].ID != "x" || out[0].Name != "new" || out[0].Mode != "command" || out[0].Required {
+ t.Fatalf("replace gate: %+v %v", out, err)
+ }
+ if _, err := upsertGate(nil, "z", domain.RunEvalRecommendation{Gate: &domain.VerificationGate{OnFailure: "bad"}}); err == nil || err.Error() != `unsupported gate onFailure "bad"` {
+ t.Fatalf("bad onFailure: %v", err)
+ }
+}
+
+func TestApplyGateListRequiresID(t *testing.T) {
+ for _, remove := range []bool{false, true} {
+ if _, err := applyGateList(nil, domain.RunEvalRecommendation{}, remove); err == nil || err.Error() != "gate id is required" {
+ t.Fatalf("remove=%v: %v", remove, err)
+ }
+ }
+}