* fix(webui): accept the merge task action The action whitelist omitted merge, so the dashboard's Merge button on a pr_open task was refused with 400 unknown action. Closes: archie-core-slpoz * fix(workflow): give each parallel branch its own outputs Branches shared the task's Outputs map, so concurrent declared-output writes raced (or were lost when the map started nil). Each branch now writes a copy; join merges new outputs in branch-name order and refuses one written by two branches as a second write. Closes: archie-core-3a7w2 * fix(postgres): dispatch captures oldest first and keep pending ones ListUndispatchedCaptures ordered newest first and listed captures no binding can match (failed HMAC), so sustained intake starved older work and bad signatures held the batch. It is now oldest first and lists only dispatchable captures. Count retention no longer evicts a capture that is still pending for an armed binding. The daemon lists only sources with an enabled workflow, so captures waiting on a disabled one do not hold the oldest-first batch. Closes: archie-core-2et4b, archie-core-o0s0z * fix(ui): refuse renaming a workflow onto an existing id Saving upserts by id, so editing a workflow's id to another workflow's id removed the open one and overwrote the other. The editor now refuses that save and says why. Closes: archie-core-wpoz8 * fix(workflow): reject unknown step settings at save Step factories decoded settings with yaml.Node.Decode, which drops keys it does not know, and settings-less steps ignored settings entirely, so a misspelt key (readOnly for read_only) saved and silently did nothing. Every factory now decodes strictly and settings-less steps refuse settings; ParseDefinition runs the factories, so the save refuses them. Closes: archie-core-8b5nk * fix(postgres): keep a stored capture when its prune fails InsertCapture committed the capture, then returned the prune's error, so intake answered 500 and a sender without a delivery header retried into a duplicate capture and a duplicate dispatch. A failed prune is now logged and the stored capture's id returned. Closes: archie-core-3miuh * fix(crondelivery): fail and advance a job no runner can deliver Router.Run returned nil for a kind with no runner, so the engine reported a successful run while the schedule never advanced and the job fired on every tick. The run now fails with the refusal, which the engine reports, and the schedule advances (a one-shot is removed). The router's own refusal event is gone with it. Closes: archie-core-s6ahh * fix(taskactions): stop the runs of cancelled callees CancelExecution cancelled a caller's waited-on callees in the store, but only the caller's run was stopped, so callee containers ran to the end. CancelExecution now returns the callees it cancelled (nested included) in place of the step ids nobody read, and every cancelling action stops their runs too. Closes: archie-core-9labz * fix(workflow): treat a zero json.Number as false in when Task inputs and step results decode numbers as json.Number, which truthy did not unpack, so when: inputs.count held for a count of 0. Closes: archie-core-uv270 * fix(webui): read a workflow's runs with a workflow-filtered page The workflows page and the canvas run picker filtered the newest 100 tasks of every workflow client-side, so on a busy instance a workflow's older runs were invisible. GET /api/tasks takes a workflow filter, carried through the State Store's Tasks request to the query, and both views ask for the selected workflow's runs. Closes: archie-core-tpiw1 * fix(ui): load the restored workflow into the editor Restore Defaults saved the shipped definition but left the open draft in the editor, still marked unsaved, so a later save re-applied it. The editor now reloads the restored definition. Closes: archie-core-85e7w
225 lines
7.7 KiB
Go
225 lines
7.7 KiB
Go
package workflow
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os/exec"
|
|
"strings"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/agentrun"
|
|
)
|
|
|
|
// The general repository step types. Each needs a repository.
|
|
const (
|
|
RepoPrepareStepName = "repo.prepare"
|
|
RepoCommitStepName = "repo.commit"
|
|
RepoOpenPRStepName = "repo.open-pr"
|
|
GateRepositoryName = "gate.repository"
|
|
GateDiffSizeStepName = "gate.diff-size"
|
|
repoBranchTask = "task"
|
|
gateExpectTestFailure = "test-failure"
|
|
)
|
|
|
|
// RepoStepTypes contributes the general repository step types.
|
|
func RepoStepTypes() []StepType {
|
|
return []StepType{
|
|
{Name: RepoPrepareStepName, Factory: newRepoPrepareStage, Settings: repoPrepareSettings{}},
|
|
{Name: RepoCommitStepName, Factory: newRepoCommitStage, Settings: repoCommitSettings{}},
|
|
{Name: RepoOpenPRStepName, Factory: newRepoOpenPRStage, Settings: repoOpenPRSettings{}},
|
|
{Name: GateRepositoryName, Factory: newGateRepositoryStage, Settings: gateRepositorySettings{}},
|
|
{Name: GateDiffSizeStepName, Factory: func(settings yaml.Node) (Stage, error) {
|
|
if err := noSettings(GateDiffSizeStepName, settings); err != nil {
|
|
return Stage{}, err
|
|
}
|
|
stage := StageDiffCap()
|
|
stage.Name = GateDiffSizeStepName
|
|
return stage, nil
|
|
}},
|
|
}
|
|
}
|
|
|
|
// decodeSettings decodes optional settings strictly.
|
|
func decodeSettings[T any](step string, settings yaml.Node) (T, error) {
|
|
var s T
|
|
if settings.Kind == 0 {
|
|
return s, nil
|
|
}
|
|
if err := decodeSettingsStrict(settings, &s); err != nil {
|
|
return s, fmt.Errorf("%s: %w", step, err)
|
|
}
|
|
return s, nil
|
|
}
|
|
|
|
// noSettings refuses settings on a step type that reads none.
|
|
func noSettings(step string, settings yaml.Node) error {
|
|
if _, err := decodeSettings[struct{}](step, settings); err != nil {
|
|
return fmt.Errorf("%s takes no settings", step)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type repoPrepareSettings struct {
|
|
// Branch is "fresh" (the default, a new branch from the base) or "task",
|
|
// the branch an earlier run of this task pushed.
|
|
Branch string `yaml:"branch" enum:"fresh,task" doc:"fresh starts a new branch from the base; task resumes the branch this task pushed."`
|
|
}
|
|
|
|
func newRepoPrepareStage(settings yaml.Node) (Stage, error) {
|
|
s, err := decodeSettings[repoPrepareSettings](RepoPrepareStepName, settings)
|
|
if err != nil {
|
|
return Stage{}, err
|
|
}
|
|
var stage Stage
|
|
switch s.Branch {
|
|
case "", "fresh":
|
|
stage = StagePrepareWorktree()
|
|
case repoBranchTask:
|
|
stage = StagePrepareWorktreeOnBranch()
|
|
default:
|
|
return Stage{}, fmt.Errorf("%s: settings.branch is fresh or task, not %q", RepoPrepareStepName, s.Branch)
|
|
}
|
|
stage.Name = RepoPrepareStepName
|
|
return stage, nil
|
|
}
|
|
|
|
type repoCommitSettings struct {
|
|
Message string `yaml:"message" doc:"The commit message."`
|
|
// Reference is the verb that links the commit to the task's issue
|
|
// ("Fixes", "Implements", "Refs"); empty links nothing.
|
|
Reference string `yaml:"reference" enum:"Fixes,Implements,Refs" doc:"How the commit links to the task issue."`
|
|
Push bool `yaml:"push" doc:"Push the branch after committing."`
|
|
// IfEmpty is what an empty worktree does: "fail" (the default), "skip",
|
|
// or "complete", which closes the issue and completes the workflow.
|
|
IfEmpty string `yaml:"if_empty" title:"If nothing changed" enum:"fail,skip,complete" doc:"fail stops the run, skip moves on, complete closes the issue and ends the run."`
|
|
}
|
|
|
|
func newRepoCommitStage(settings yaml.Node) (Stage, error) {
|
|
s, err := decodeSettings[repoCommitSettings](RepoCommitStepName, settings)
|
|
if err != nil {
|
|
return Stage{}, err
|
|
}
|
|
if strings.TrimSpace(s.Message) == "" {
|
|
return Stage{}, fmt.Errorf("%s: settings.message is required", RepoCommitStepName)
|
|
}
|
|
switch s.IfEmpty {
|
|
case "", "fail", "skip", "complete":
|
|
default:
|
|
return Stage{}, fmt.Errorf("%s: settings.if_empty is fail, skip or complete, not %q", RepoCommitStepName, s.IfEmpty)
|
|
}
|
|
return Stage{Name: RepoCommitStepName, Run: func(ctx context.Context, tc *TaskContext) error {
|
|
message := s.Message
|
|
if s.Reference != "" {
|
|
message += commitIssueReference(s.Reference, tc.Task)
|
|
}
|
|
changed, err := tc.Trees.CommitAll(ctx, tc.Dir, message)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !changed && !tc.BaselineFixed {
|
|
switch s.IfEmpty {
|
|
case "skip":
|
|
return nil
|
|
case "complete":
|
|
return closeNoChangesIssue(ctx, tc)
|
|
default:
|
|
return fmt.Errorf("worktree has no changes to commit")
|
|
}
|
|
}
|
|
if s.Push {
|
|
if err := tc.Trees.Push(ctx, tc.Dir, tc.Branch); err != nil {
|
|
return err
|
|
}
|
|
tc.captureChanges(ctx, capturedAfterCommitPush)
|
|
return nil
|
|
}
|
|
tc.captureChanges(ctx, capturedAfterCommit)
|
|
return nil
|
|
}}, nil
|
|
}
|
|
|
|
type repoOpenPRSettings struct {
|
|
Body string `yaml:"body" doc:"The pull request description."`
|
|
// Review runs the PR review pipeline on the change first; a blocking
|
|
// finding parks the task instead of opening the PR.
|
|
Review bool `yaml:"review" doc:"Review the change first; a blocking finding parks the task instead of opening the PR."`
|
|
}
|
|
|
|
func newRepoOpenPRStage(settings yaml.Node) (Stage, error) {
|
|
s, err := decodeSettings[repoOpenPRSettings](RepoOpenPRStepName, settings)
|
|
if err != nil {
|
|
return Stage{}, err
|
|
}
|
|
body := func(tc *TaskContext) string {
|
|
return fmt.Sprintf("%s\n\n---\n*workflow: %s · %d iterations · %s*",
|
|
s.Body, tc.Task.Workflow, tc.Task.Iterations, formatTokenUsage(tc.Task.TokensUsed, tc.RunUsage))
|
|
}
|
|
stage := StageOpenPR(body)
|
|
if s.Review {
|
|
stage = StagePRReviewAndOpenPR(body)
|
|
}
|
|
stage.Name = RepoOpenPRStepName
|
|
return stage, nil
|
|
}
|
|
|
|
type gateRepositorySettings struct {
|
|
// Expect is "pass" (the default) or "test-failure": every gate command
|
|
// passes except the test command, which must fail.
|
|
Expect string `yaml:"expect" enum:"pass,test-failure" doc:"pass requires every gate command to pass; test-failure requires the test command to fail."`
|
|
// Repair lets the builder fix a failing gate before the step fails.
|
|
Repair bool `yaml:"repair" doc:"Let the builder fix a failing gate before the step fails."`
|
|
}
|
|
|
|
// newGateRepositoryStage runs the repository's gate commands in the worktree.
|
|
// Its summary is the test command's output, so a later step can quote it.
|
|
func newGateRepositoryStage(settings yaml.Node) (Stage, error) {
|
|
s, err := decodeSettings[gateRepositorySettings](GateRepositoryName, settings)
|
|
if err != nil {
|
|
return Stage{}, err
|
|
}
|
|
switch {
|
|
case s.Expect != "" && s.Expect != "pass" && s.Expect != gateExpectTestFailure:
|
|
return Stage{}, fmt.Errorf("%s: settings.expect is pass or test-failure, not %q", GateRepositoryName, s.Expect)
|
|
case s.Repair && s.Expect == gateExpectTestFailure:
|
|
return Stage{}, fmt.Errorf("%s: repair applies only when the gate is expected to pass", GateRepositoryName)
|
|
}
|
|
if s.Repair {
|
|
stage := StageBaselineGate()
|
|
stage.Name = GateRepositoryName
|
|
return stage, nil
|
|
}
|
|
expectFailure := s.Expect == gateExpectTestFailure
|
|
return Stage{Name: GateRepositoryName, Run: func(ctx context.Context, tc *TaskContext) error {
|
|
gate := GateFromRepo(tc.Repo, tc.Cfg.Budgets)
|
|
if expectFailure {
|
|
gate = tddReproGate(tc.Repo, tc.Cfg.Budgets)
|
|
}
|
|
if len(gate.Commands) == 0 {
|
|
return fmt.Errorf("repository %s has no gate commands", tc.Repo.FullName())
|
|
}
|
|
for _, command := range gate.Commands {
|
|
out, err := runGateCommand(ctx, tc.Dir, command)
|
|
tc.stepResult.Summary = clip(out, 4000)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}}, nil
|
|
}
|
|
|
|
func runGateCommand(ctx context.Context, dir string, command agentrun.Command) (string, error) {
|
|
cmd := exec.CommandContext(ctx, command.Argv[0], command.Argv[1:]...)
|
|
cmd.Dir = dir
|
|
out, err := cmd.CombinedOutput()
|
|
name := strings.Join(command.Argv, " ")
|
|
switch {
|
|
case command.ExpectFailure && err == nil:
|
|
return string(out), fmt.Errorf("%s passed, but it was expected to fail", name)
|
|
case !command.ExpectFailure && err != nil:
|
|
return string(out), fmt.Errorf("%s failed: %w", name, err)
|
|
}
|
|
return string(out), nil
|
|
}
|