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.
JavaScript agent-orchestrationagentic-workflowai-agentsai-codingclaude-codecode-reviewcodebuddycodexcoding-agentscursordeveloper-toolshuman-on-the-loopllmmcpmulti-agentsdlcself-hostedsopworkflow-automationworkflow-engine
package engine
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"agentflow/internal/domain"
"agentflow/internal/gitwork"
"agentflow/internal/repo"
"agentflow/internal/runtime"
)
// PrepareClarifyWorkspace ensures a local, up-to-date checkout of the given
// repository exists at cacheDir for use during requirement clarification.
//
// Behaviour:
// - If cacheDir already holds a git checkout, it is refreshed to the tip of
// the base branch (equivalent to a clean "git pull").
// - Otherwise the repository is shallow-cloned from its clone URL.
//
// The base branch is repo.DefaultBranch when set, falling back to "master".
// Authentication reuses the same git-token plumbing as SOP execution, so the
// caller only needs to supply the resolved token (see ResolveClarifyGitToken).
//
// On success the checkout directory (cacheDir) is returned.
func PrepareClarifyWorkspace(ctx context.Context, cacheDir string, repo domain.Repository, credentials runtime.RunCredentials) (string, error) {
return PrepareClarifyWorkspaceAtBranch(ctx, cacheDir, repo, "", credentials)
}
// PrepareClarifyWorkspaceAtBranch checks out the requirement's selected branch.
// An empty branch uses the repository default, as in PrepareClarifyWorkspace.
func PrepareClarifyWorkspaceAtBranch(ctx context.Context, cacheDir string, repo domain.Repository, baseBranch string, credentials runtime.RunCredentials) (string, error) {
cloneURL := strings.TrimSpace(repo.CloneURL)
if cloneURL == "" {
return "", fmt.Errorf("repository %q has no cloneUrl", repo.ID)
}
if err := gitwork.ValidateRemoteURL(cloneURL, true); err != nil {
return "", fmt.Errorf("repository %q: %w", repo.ID, err)
}
baseBranch = strings.TrimSpace(baseBranch)
if baseBranch == "" {
baseBranch = strings.TrimSpace(repo.DefaultBranch)
}
if baseBranch == "" {
baseBranch = "master"
}
if repo.Provider == domain.RepositoryProviderSSH {
ctx = gitwork.WithSSHRemote(ctx, cloneURL)
}
ctx = withRunGitCredentials(ctx, credentials)
ctx = gitwork.WithHTTPRemote(ctx, cloneURL)
if _, err := os.Stat(filepath.Join(cacheDir, ".git")); err == nil {
// Existing checkout: refresh to the latest base branch.
if err := refreshClarifyWorkspace(ctx, cacheDir, cloneURL, baseBranch); err != nil {
return "", err
}
return cacheDir, nil
}
// Fresh checkout.
if err := os.MkdirAll(filepath.Dir(cacheDir), 0o700); err != nil {
return "", err
}
// Clear any stale non-git leftovers before cloning.
_ = os.RemoveAll(cacheDir)
args := []string{
"clone", "--no-hardlinks", "--single-branch", "--depth", "1",
"--branch", baseBranch,
"--", cloneRemoteForGit(ctx, cloneURL), cacheDir,
}
if _, err := runGit(ctx, cacheDir, args...); err != nil {
_ = os.RemoveAll(cacheDir)
return "", fmt.Errorf("clone clarify workspace: %w", err)
}
if err := applyWorkspaceGitAuth(ctx, cacheDir); err != nil {
return "", err
}
if _, err := gitC(ctx, cacheDir, "remote", "set-url", "--", "origin", cloneURL); err != nil {
return "", fmt.Errorf("set origin url: %w", err)
}
// git clone --branch also accepts tags. Requirements must start from an
// actual remote branch, matching the explicit refs/heads refresh path.
if _, err := gitC(ctx, cacheDir, "rev-parse", "--verify", "refs/remotes/origin/"+baseBranch+"^{commit}"); err != nil {
_ = os.RemoveAll(cacheDir)
return "", fmt.Errorf("clarify base branch %q is not a remote branch: %w", baseBranch, err)
}
return cacheDir, nil
}
// refreshClarifyWorkspace performs a clean "git pull" of the base branch on an
// existing checkout: re-apply auth, re-point origin, fetch and hard-reset.
func refreshClarifyWorkspace(ctx context.Context, cacheDir, cloneURL, baseBranch string) error {
if err := applyWorkspaceGitAuth(ctx, cacheDir); err != nil {
return err
}
if _, err := gitC(ctx, cacheDir, "remote", "set-url", "--", "origin", cloneURL); err != nil {
return fmt.Errorf("set origin url: %w", err)
}
steps := [][]string{
{"fetch", "--prune", "--depth", "1", "origin", "+refs/heads/" + baseBranch + ":refs/remotes/origin/" + baseBranch},
{"checkout", "--force", "-B", baseBranch, "origin/" + baseBranch},
{"reset", "--hard", "origin/" + baseBranch},
}
for _, args := range steps {
if _, err := gitC(ctx, cacheDir, args...); err != nil {
return fmt.Errorf("refresh clarify workspace (%s): %w", strings.Join(args, " "), err)
}
}
return nil
}
// ResolveClarifyGitCredentials resolves only the responsible user's host credentials.
func ResolveClarifyGitCredentials(s *repo.Store, responsibleUserID int64) runtime.RunCredentials {
return runtime.RunCredentials{GitHTTPCredentials: s.GitHTTPCredentialSecrets(context.Background(), responsibleUserID)}
}