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.

5.7 KB
Raw Blame
package runeval

import (
	"fmt"
	"strings"

	"agentflow/internal/domain"
)

func ApplyRecommendation(rec domain.RunEvalRecommendation, workflow *domain.WorkflowDefinition, agent *domain.AgentDefinition) error {
	action := domain.NormalizeRunEvalAction(rec.Target, rec.Action)
	switch action {
	case "workflow.update_node":
		if workflow == nil {
			return fmt.Errorf("workflow is required")
		}
		return updateWorkflowNode(workflow, rec)
	case "agent.upsert_gate":
		if agent == nil {
			return fmt.Errorf("agent is required")
		}
		return upsertAgentGate(agent, rec, false)
	case "agent.remove_gate":
		if agent == nil {
			return fmt.Errorf("agent is required")
		}
		return upsertAgentGate(agent, rec, true)
	case "agent.append_instructions", "agent.append_task_prompt":
		// append_instructions predates the merged role prompt; both land there.
		if agent == nil {
			return fmt.Errorf("agent is required")
		}
		return appendAgentText(&agent.Prompt, rec)
	default:
		return fmt.Errorf("unsupported recommendation action %q", action)
	}
}

func recommendationText(rec domain.RunEvalRecommendation) string {
	if text := strings.TrimSpace(rec.Text); text != "" {
		return text
	}
	if rec.Set == nil {
		return ""
	}
	for _, key := range []string{"text", "instructionsDelta"} {
		if value, ok := stringFrom(rec.Set, key); ok {
			return value
		}
	}
	return ""
}

func appendAgentText(dst *string, rec domain.RunEvalRecommendation) error {
	text := recommendationText(rec)
	if text == "" {
		return fmt.Errorf("append text is empty")
	}
	if strings.Contains(*dst, text) {
		return nil
	}
	if strings.TrimSpace(*dst) == "" {
		*dst = text
		return nil
	}
	*dst = strings.TrimSpace(*dst) + "\n\n" + text
	return nil
}

func updateWorkflowNode(wf *domain.WorkflowDefinition, rec domain.RunEvalRecommendation) error {
	node, i, ok := findNode(wf, rec.NodeID)
	if !ok {
		return fmt.Errorf("node %q not found", rec.NodeID)
	}
	if rec.Set == nil {
		return fmt.Errorf("update_node requires set")
	}
	if v, ok := intFrom(rec.Set, "maxAttempts"); ok {
		if v < 1 {
			return fmt.Errorf("maxAttempts must be >= 1")
		}
		node.MaxAttempts = v
	}
	if v, ok := intFrom(rec.Set, "maxReworks"); ok {
		if v < 0 {
			return fmt.Errorf("maxReworks must be >= 0")
		}
		node.MaxReworks = v
	}
	if v, ok := stringFrom(rec.Set, "onFailure"); ok {
		if !allowedNodeFailure(v) {
			return fmt.Errorf("unsupported onFailure %q", v)
		}
		node.OnFailure = v
	}
	if v, ok := stringFrom(rec.Set, "retryStrategy"); ok {
		if !allowedRetry(v) {
			return fmt.Errorf("unsupported retryStrategy %q", v)
		}
		node.RetryStrategy = v
	}
	wf.Nodes[i] = node
	return nil
}

func upsertAgentGate(agent *domain.AgentDefinition, rec domain.RunEvalRecommendation, remove bool) error {
	gates, err := applyGateList(agent.Verification, rec, remove)
	if err != nil {
		return err
	}
	agent.Verification = gates
	return nil
}

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")
	}
	if rec.Gate != nil && strings.TrimSpace(rec.Gate.ID) != "" {
		id = strings.TrimSpace(rec.Gate.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
		}
		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
		gate.ID = id
	}
	if gate.Mode == "" {
		gate.Mode = "command"
	}
	if gate.OnFailure != "" && !allowedGateFailure(gate.OnFailure) {
		return nil, fmt.Errorf("unsupported gate onFailure %q", gate.OnFailure)
	}
	for i, existing := range gates {
		if existing.ID == id {
			gates[i] = gate
			return gates, nil
		}
	}
	return append(gates, gate), nil
}

func findNode(wf *domain.WorkflowDefinition, id string) (domain.WorkflowNode, int, bool) {
	id = strings.TrimSpace(id)
	for i, node := range wf.Nodes {
		if node.ID == id {
			return node, i, true
		}
	}
	return domain.WorkflowNode{}, -1, false
}

func intFrom(set map[string]any, key string) (int, bool) {
	raw, ok := set[key]
	if !ok {
		return 0, false
	}
	switch v := raw.(type) {
	case int:
		return v, true
	case int64:
		return int(v), true
	case float64:
		return int(v), true
	case string:
		var n int
		if _, err := fmt.Sscanf(strings.TrimSpace(v), "%d", &n); err == nil {
			return n, true
		}
	}
	return 0, false
}

func stringFrom(set map[string]any, key string) (string, bool) {
	raw, ok := set[key]
	if !ok {
		return "", false
	}
	value := strings.TrimSpace(fmt.Sprint(raw))
	if value == "" || value == "<nil>" {
		return "", false
	}
	return value, true
}

func allowedNodeFailure(v string) bool {
	return v == "fail" || v == "reject"
}

func allowedGateFailure(v string) bool {
	return v == "loop" || v == "reject" || v == "fail" || v == "human"
}

func allowedRetry(v string) bool {
	return v == "targeted_fix" || v == "full_redo" || v == ""
}

func CloneID(base, projectID string) string {
	base = strings.TrimSpace(base)
	projectID = strings.TrimSpace(projectID)
	if projectID == "" {
		return base + "-copy"
	}
	return base + "-" + projectID
}