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.

6.0 KB
Raw Blame
package domain

import (
	"fmt"
	"strings"
)

func commandHasContent(gate VerificationGate) bool {
	for _, command := range gate.Commands {
		if strings.TrimSpace(command) != "" {
			return true
		}
	}
	return false
}

func gateMode(gate VerificationGate) string {
	mode := strings.ToLower(strings.TrimSpace(gate.Mode))
	if mode == "" && commandHasContent(gate) {
		return "command"
	}
	return mode
}

// CommandNeedsRepository reports whether a gate command inspects or mutates a
// git checkout and therefore belongs in executionScope=repositories.
func CommandNeedsRepository(command string) bool {
	cmd := strings.TrimSpace(command)
	if rest, ok := strings.CutPrefix(cmd, "set -e"); ok {
		cmd = strings.TrimSpace(rest)
	}
	if cmd == "" {
		return false
	}
	return strings.Contains(cmd, "git ") || strings.Contains(cmd, "git\t") || strings.Contains(cmd, ".git/") || strings.Contains(cmd, "$(git")
}

func rewriteRepositoryWorkspaceRefs(command string) string {
	out := command
	if strings.Contains(out, ".agentflow/") && !strings.Contains(out, "$AGENTFLOW_WORKSPACE/.agentflow/") && !strings.Contains(out, "${AGENTFLOW_WORKSPACE}/.agentflow/") {
		out = strings.ReplaceAll(out, ".agentflow/", `$AGENTFLOW_WORKSPACE/.agentflow/`)
	}
	if strings.Contains(out, "git ") || strings.Contains(out, "git\t") {
		for _, name := range []string{"change-report.md", "fix-report.md", "e2e-cases.md"} {
			if strings.Contains(out, ":!"+name) || strings.Contains(out, ":(exclude)"+name) {
				continue
			}
			prefixed := `$AGENTFLOW_WORKSPACE/` + name
			if !strings.Contains(out, prefixed) {
				out = strings.ReplaceAll(out, name, prefixed)
			}
		}
	}
	return out
}

func uniqueGateID(base string, used map[string]bool) string {
	id := strings.TrimSpace(base)
	if id == "" {
		id = "gate-git"
	}
	if !used[id] {
		return id
	}
	for i := 2; ; i++ {
		candidate := fmt.Sprintf("%s-%d", id, i)
		if !used[candidate] {
			return candidate
		}
	}
}

// VerificationModeMR gates pass only when, for every repository whose pushed
// work branch carries new commits, the hosting platform has an MR from that
// work branch into the run's target branch that carries the pushed tip. The
// engine runs the check; the gate has no commands.
const VerificationModeMR = "mr"

// ValidateVerificationGate requires command gates to declare run or repositories.
// Manual and MR gates and empty command lists may omit the field.
func ValidateVerificationGate(gate VerificationGate) error {
	mode := gateMode(gate)
	switch mode {
	case "", "command", "manual", VerificationModeMR:
	default:
		return fmt.Errorf("verification gate %s has invalid mode %q", gate.ID, gate.Mode)
	}
	scope := strings.TrimSpace(gate.ExecutionScope)
	if scope != "" && scope != ExecutionScopeRun && scope != ExecutionScopeRepositories {
		return fmt.Errorf("verification gate %s has invalid executionScope %q", gate.ID, gate.ExecutionScope)
	}
	if mode == VerificationModeMR && commandHasContent(gate) {
		return fmt.Errorf("verification gate %s: mode %q takes no commands", gate.ID, VerificationModeMR)
	}
	if mode == "manual" || !commandHasContent(gate) {
		return nil
	}
	if scope == "" {
		id := strings.TrimSpace(gate.ID)
		if id == "" {
			id = strings.TrimSpace(gate.Name)
		}
		if id == "" {
			id = "unnamed"
		}
		return fmt.Errorf("verification gate %s requires executionScope %q or %q", id, ExecutionScopeRun, ExecutionScopeRepositories)
	}
	return nil
}

func ValidateVerificationGates(gates []VerificationGate) error {
	for _, gate := range gates {
		if err := ValidateVerificationGate(gate); err != nil {
			return err
		}
	}
	return nil
}

// SplitVerificationGates separates legacy empty-scope gates into an explicit
// run-scoped deliverable gate and a repositories-scoped git gate. Gates that
// already declare a scope are left unchanged.
func SplitVerificationGates(gates []VerificationGate) []VerificationGate {
	used := make(map[string]bool, len(gates)*2)
	for _, gate := range gates {
		if id := strings.TrimSpace(gate.ID); id != "" {
			used[id] = true
		}
	}
	out := make([]VerificationGate, 0, len(gates)+4)
	for _, gate := range gates {
		if strings.TrimSpace(gate.ExecutionScope) != "" {
			out = append(out, gate)
			continue
		}
		runCmds, repoCmds := partitionGateCommands(gate.Commands)
		if len(runCmds) == 0 && len(repoCmds) == 0 {
			out = append(out, gate)
			continue
		}
		if len(repoCmds) == 0 {
			gate.Commands = runCmds
			gate.ExecutionScope = ExecutionScopeRun
			out = append(out, gate)
			continue
		}
		if len(runCmds) == 0 {
			gate.Commands = repoCmds
			gate.ExecutionScope = ExecutionScopeRepositories
			out = append(out, gate)
			continue
		}
		runGate := gate
		runGate.Commands = runCmds
		runGate.ExecutionScope = ExecutionScopeRun
		repoGate := repositoryGitGate(gate, repoCmds, used)
		out = append(out, runGate, repoGate)
	}
	return out
}

// partitionGateCommands drops blank commands and splits the rest into
// run-scoped deliverable checks and repository-scoped git checks. Repository
// commands are rewritten so run-root paths keep resolving from each repo.
func partitionGateCommands(commands []string) (runCmds, repoCmds []string) {
	for _, command := range commands {
		if strings.TrimSpace(command) == "" {
			continue
		}
		if CommandNeedsRepository(command) {
			repoCmds = append(repoCmds, rewriteRepositoryWorkspaceRefs(command))
			continue
		}
		runCmds = append(runCmds, command)
	}
	return runCmds, repoCmds
}

// repositoryGitGate derives the repositories-scoped companion of gate that
// runs repoCmds, reserving its unique "-git" ID in used.
func repositoryGitGate(gate VerificationGate, repoCmds []string, used map[string]bool) VerificationGate {
	repoGate := gate
	baseID := strings.TrimSpace(gate.ID)
	if baseID == "" {
		baseID = "gate"
	}
	repoGate.ID = uniqueGateID(baseID+"-git", used)
	used[repoGate.ID] = true
	if name := strings.TrimSpace(gate.Name); name != "" {
		repoGate.Name = name + " · Git 检查"
	} else {
		repoGate.Name = "Git 检查"
	}
	repoGate.Commands = repoCmds
	repoGate.ExecutionScope = ExecutionScopeRepositories
	return repoGate
}