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 httpapi
import (
"crypto/sha256"
"errors"
"fmt"
"net/http"
"strings"
"time"
"agentflow/internal/domain"
"agentflow/internal/repo"
)
type dispatchRequest struct {
IssueID, WorkflowID, ScheduleJobID, TriggerOrigin string
Rerun bool
TriggerUserID int64
}
func dispatchIssue(d *deps, req dispatchRequest) (domain.Task, error) {
issue, ok := d.repo.Issue(req.IssueID)
if !ok {
return domain.Task{}, HTTPError{http.StatusNotFound, "issue not found"}
}
if reason := issue.ExecutionBlockedReason(); reason != "" {
return domain.Task{}, HTTPError{http.StatusConflict, reason}
}
responsible, err := d.repo.ResponsibleUser(issue)
if err != nil {
return domain.Task{}, HTTPError{http.StatusConflict, err.Error()}
}
if issue.AssigneeUserID == 0 {
issue, err = d.repo.PatchIssue(issue.ID, func(current *domain.Issue) error {
if current.AssigneeUserID == 0 {
current.AssigneeUserID = responsible.ID
}
return nil
})
if err != nil {
if errors.Is(err, repo.ErrIssueNotFound) {
return domain.Task{}, HTTPError{http.StatusNotFound, err.Error()}
}
return domain.Task{}, err
}
}
runUserID := responsible.ID
if req.TriggerUserID > 0 {
if user, ok := d.repo.User(req.TriggerUserID); ok && user.ID > 0 {
runUserID = user.ID
}
}
scheduled := strings.TrimSpace(req.ScheduleJobID) != ""
if issue.WithoutSOP() {
if scheduled {
return domain.Task{}, HTTPError{http.StatusConflict, "该需求不使用 SOP"}
}
if strings.TrimSpace(req.WorkflowID) == "" {
return domain.Task{}, HTTPError{http.StatusBadRequest, "该需求不使用 SOP,请先选择要运行的 SOP"}
}
}
project, _ := d.repo.Project(issue.ProjectID)
workflowID := strings.TrimSpace(req.WorkflowID)
if workflowID == "" {
workflowID = issue.WorkflowID
}
if workflowID == "" {
if playbook := domain.ResolveSOPBindings(issue, project); len(playbook) > 0 {
workflowID = playbook[0].WorkflowID
}
}
if workflowID == "" {
if wf, ok := d.repo.Workflow(repo.ProjectIssueWorkflowID(issue.ProjectID)); ok && wf.ID != "" {
workflowID = wf.ID
}
}
if workflowID == "" {
workflowID = repo.DefaultWorkflowID(issue.Source, issue.Kind)
}
if workflowID == "" {
workflowID = project.WorkflowID
}
if workflowID == "" {
return domain.Task{}, HTTPError{http.StatusBadRequest, "issue requires a workflow (bind to SOP first)"}
}
if issue.SOPBindings != nil {
if binding, ok := domain.ResolveSOPBinding(issue, project, workflowID); ok && binding.AssigneeUserID > 0 {
if user, found := d.repo.User(binding.AssigneeUserID); found && user.ID > 0 {
runUserID = user.ID
}
}
}
hasSameSOP := d.repo.IssueHasTaskForWorkflow(issue.ID, workflowID)
// A schedule only picks up untouched issues; another SOP lane is a human decision.
if scheduled && d.repo.IssueHasTask(issue.ID) {
return domain.Task{}, HTTPError{http.StatusConflict, "该需求已经跑过 SOP"}
}
if !scheduled && !req.Rerun && hasSameSOP {
return domain.Task{}, HTTPError{http.StatusConflict, "该需求的这条 SOP 已经跑过,如需重新执行请明确选择重新运行"}
}
if req.Rerun && !hasSameSOP {
return domain.Task{}, HTTPError{http.StatusBadRequest, "该需求尚未运行这条 SOP,不能使用重新运行"}
}
repositoryIDs := issue.EffectiveRepositoryIDs()
if len(repositoryIDs) == 0 {
if p, ok := d.repo.Project(issue.ProjectID); ok && strings.TrimSpace(p.DefaultRepositoryID) != "" {
repositoryIDs = []string{p.DefaultRepositoryID}
}
}
var boundRepositories []domain.Repository
for _, id := range repositoryIDs {
repository, found := d.repo.Repository(id)
if !found || repository.ProjectID != issue.ProjectID {
return domain.Task{}, HTTPError{http.StatusBadRequest, "bound repository is invalid"}
}
boundRepositories = append(boundRepositories, repository)
}
branchSelection := issue
if len(boundRepositories) > 0 && legacyIssueDefaultBranch(branchSelection.BaseBranch, boundRepositories[0]) {
branchSelection.BaseBranch = ""
}
if err := normalizeIssueRepositoryBranches(&branchSelection, boundRepositories); err != nil {
return domain.Task{}, HTTPError{http.StatusBadRequest, err.Error()}
}
var repositories []domain.RepositoryContext
for _, repo := range boundRepositories {
rc := repositoryRunContext(repo)
if selectedWorkBranch := branchSelection.WorkBranchForRepository(repo.ID); selectedWorkBranch != "" {
rc.WorkBranch = selectedWorkBranch
rc.RequireExistingWorkBranch = true
}
if scheduled && !rc.RequireExistingWorkBranch {
if integ := domain.NormalizeIntegrateBranch(repo.IntegrateBranch); integ != "" {
rc.IntegrateBranch = integ
rc.BaseBranch = integ
}
}
repositories = append(repositories, rc)
}
input := issue.Title + "\n\n" + issue.Description
if block := issue.IdentityBlock(); block != "" {
input += "\n\n" + block
}
// Assign workspace directories before building the prompt so the Agent
// sees the real per-repository directories. The engine re-assigns the
// same values.
domain.AssignRepositoryDirs(repositories)
if summary := strings.TrimSpace(issue.Clarification.Summary); summary != "" {
input += "\n\nClarification summary:\n" + summary
}
if len(repositories) > 0 {
if len(repositories) == 1 {
input += "\n\nRepository context (frozen): the repository is checked out in a first-level subdirectory; SOP deliverables stay at the workspace root"
} else {
input += "\n\nRepository context (frozen, multi-repository): every repository below is checked out side by side; treat them as equals"
}
for _, rc := range repositories {
input += fmt.Sprintf("\n- repository: %s (%s)\n - provider: %s\n - base branch: %s\n - clone URL: %s\n - local directory: ./%s (code changes for this repository MUST be written inside this directory)", rc.Name, rc.GongfengProjectID, rc.Provider, rc.BaseBranch, domain.StripURLCredentials(rc.CloneURL), rc.WorkspaceDir)
input += fmt.Sprintf("\n - merge request target branch: %s", rc.BaseBranch)
if rc.RequireExistingWorkBranch {
input += fmt.Sprintf("\n - existing work branch: %s (continue directly on this remote branch; do not create or rename a work branch; fail if the branch does not exist; create or update its MR targeting %s)", rc.WorkBranch, rc.BaseBranch)
} else {
input += " (create a new work branch from this baseline and target this branch when opening the MR)"
}
if scheduled && strings.TrimSpace(rc.IntegrateBranch) != "" {
input += fmt.Sprintf("\n - scheduled integration branch: %s\n - configured main branch: %s\n - before this SOP starts, AgentFlow merges the configured main branch into the integration baseline (never the other way); the workspace is checked out from that baseline. After the SOP, only the work branch is merged back to the integration baseline. Humans merge the baseline into the configured main branch.", rc.IntegrateBranch, rc.DefaultBranch)
}
}
input += "\n- SOP deliverables (fix-plan.md and other reports not tied to one repository) go to the workspace root, never inside a repository directory\n- do not write to any host absolute path"
}
input = appendSOPHandoff(issue, project, workflowID, input)
taskID, dispatchMode := primaryTaskID(issue.ID, workflowID), "primary"
if scheduled {
dispatchMode = "scheduled"
}
if req.Rerun {
taskID, dispatchMode = fmt.Sprintf("task-%d", time.Now().UnixNano()), "rerun"
}
triggerOrigin := strings.TrimSpace(req.TriggerOrigin)
if triggerOrigin == "" {
if scheduled {
triggerOrigin = "timer"
} else {
triggerOrigin = "manual"
}
}
var displayRepository domain.RepositoryContext
if len(repositories) > 0 {
displayRepository = repositories[0]
}
task := domain.Task{ID: taskID, IssueID: issue.ID, AgentID: issue.AssigneeID, WorkflowID: workflowID, Status: "queued", Repository: displayRepository, DispatchMode: dispatchMode, ScheduleJobID: strings.TrimSpace(req.ScheduleJobID), TriggerOrigin: triggerOrigin}
stored, created, err := d.repo.ReserveIssueDispatch(task)
if err != nil {
if errors.Is(err, repo.ErrIssueNotFound) {
return domain.Task{}, HTTPError{http.StatusNotFound, err.Error()}
}
return domain.Task{}, err
}
if !created {
if scheduled {
return domain.Task{}, HTTPError{http.StatusConflict, "该需求的这条 SOP 已经跑过"}
}
return domain.Task{}, HTTPError{http.StatusConflict, "该需求的这条 SOP 正在创建或已经完成首次执行,请选择重新运行"}
}
issue.LatestTaskID = stored.ID
issue.Status = "in_progress"
if err := d.repo.PutIssue(issue); err != nil {
task.Status = "failed"
_ = d.repo.PutTask(task)
return domain.Task{}, err
}
tapdID := issue.RequirementTAPDID()
run, err := d.engine.StartWithRepositoriesContext(workflowID, input, repositories, domain.TelemetryContext{ProjectID: issue.ProjectID, IssueID: issue.ID, TAPDID: tapdID, ResponsibleUserID: runUserID, IssueNumber: issue.Number, TaskID: taskID, ScheduleJobID: strings.TrimSpace(req.ScheduleJobID)})
if err != nil {
task.Status = "failed"
_ = d.repo.PutTask(task)
issue.Status = "blocked"
_ = d.repo.PutIssue(issue)
return domain.Task{}, err
}
// Bind only after the run actually starts. A second lane keeps the SOP
// that already ran; a stored list of several SOPs is left unchanged.
// A failed start returns above and does not reach this write.
if issue.WithoutSOP() {
issue.SOPMode, issue.AutomationState = domain.IssueSOPUse, "eligible"
}
alreadyStarted := strings.TrimSpace(issue.WorkflowID) != "" && issue.WorkflowID != workflowID && d.repo.IssueHasTaskForWorkflow(issue.ID, issue.WorkflowID)
if len(issue.SOPBindings) > 1 {
if strings.TrimSpace(issue.WorkflowID) == "" {
issue.WorkflowID = workflowID
}
} else if !alreadyStarted {
issue.WorkflowID = workflowID
if len(issue.SOPBindings) == 1 {
issue.SOPBindings[0].WorkflowID = workflowID
}
}
if err := d.repo.PutIssue(issue); err != nil {
return domain.Task{}, err
}
// Starting the issue over is not a retry of the previous run, so the
// container that run was holding for a retry has no reader left.
d.engine.SupersedeIssueRuns(issue.ID, run.ID)
task.RunID, task.Status, task.Repository = run.ID, taskStatusFromRun(run.Status), run.Repository
if err := d.repo.PutTask(task); err != nil {
return domain.Task{}, err
}
stored, _ = d.repo.Task(task.ID)
return stored, nil
}
func primaryTaskID(issueID, workflowID string) string {
sum := sha256.Sum256([]byte(strings.TrimSpace(issueID) + "\n" + strings.TrimSpace(workflowID)))
return fmt.Sprintf("task-primary-%x", sum[:12])
}
func appendSOPHandoff(issue domain.Issue, project domain.Project, workflowID, input string) string {
bindings := domain.ResolveSOPBindings(issue, project)
index := -1
var current domain.IssueSOPBinding
for i, binding := range bindings {
if binding.WorkflowID == workflowID {
index = i
current = binding
break
}
}
if index < 0 {
return input
}
prev := make([]domain.IssueSOPBinding, 0, len(current.DependsOn)+1)
if len(current.DependsOn) > 0 {
want := map[string]bool{}
for _, id := range current.DependsOn {
want[id] = true
}
for _, binding := range bindings {
if want[binding.ID] {
prev = append(prev, binding)
}
}
} else if index > 0 {
prev = append(prev, bindings[index-1])
}
if len(prev) == 0 {
return input
}
input += "\n\nHandoff from previous SOP steps on this requirement:"
for _, binding := range prev {
input += "\n- " + binding.WorkflowID
if len(current.Handoff) > 0 {
input += " → expected artifacts: " + strings.Join(current.Handoff, ", ")
}
}
input += "\nContinue from those deliverables; do not redo the previous SOP."
return input
}