diff --git a/internal/domain/workflow/agent.go b/internal/domain/workflow/agent.go index c4c881137..7eecfd53b 100644 --- a/internal/domain/workflow/agent.go +++ b/internal/domain/workflow/agent.go @@ -220,6 +220,10 @@ func (a AgentStage) deliverResult(tc *TaskContext, res agentrun.Result) error { } return fmt.Errorf("agent %s (%s): %s", res.Status, res.StopReason, clip(detail, 2000)) } + if !a.ReadOnly { + // repo.commit clears this when the stage commits changes. + tc.BuildNoChanges = true + } // Gatekeeping: the daemon reviews agent output before human delivery. if a.ReviewResult != nil { if err := a.ReviewResult(tc, res); err != nil { diff --git a/internal/domain/workflow/repo_commit_no_changes_test.go b/internal/domain/workflow/repo_commit_no_changes_test.go new file mode 100644 index 000000000..8c8f5cb72 --- /dev/null +++ b/internal/domain/workflow/repo_commit_no_changes_test.go @@ -0,0 +1,86 @@ +package workflow + +import ( + "context" + "log/slog" + "testing" + + "gopkg.in/yaml.v3" + + "github.com/samcharles93/archie-core/internal/domain/agentrun" + "github.com/samcharles93/archie-core/internal/domain/workflow/task" +) + +// noChangesTrees reports a fixed commit result and records whether anything +// was pushed; the rest of the worktree API is never reached by these stages. +type noChangesTrees struct { + Trees + changed bool + pushed bool +} + +func (f *noChangesTrees) CommitAll(context.Context, string, string) (bool, error) { + return f.changed, nil +} + +func (f *noChangesTrees) Push(context.Context, string, string) error { + f.pushed = true + return nil +} + +func repoCommitNode(t *testing.T, ifEmpty string) yaml.Node { + t.Helper() + src := "message: fix it\n" + if ifEmpty != "" { + src += "if_empty: " + ifEmpty + "\n" + } + var doc yaml.Node + if err := yaml.Unmarshal([]byte(src), &doc); err != nil { + t.Fatal(err) + } + return *doc.Content[0] +} + +// An empty commit is the workflow's decision, set by if_empty. The builder +// passing with no changes does not choose the outcome on its own: unset, it +// fails; complete closes the issue; skip moves on. +func TestRepoCommitWithNoBuilderChangesFollowsIfEmpty(t *testing.T) { + tests := []struct { + name string + readOnly bool + ifEmpty string + wantErr bool + wantPush bool + wantState string + }{ + {name: "unset fails when the builder passed with no changes", ifEmpty: "", wantErr: true}, + {name: "complete closes the issue", ifEmpty: "complete", wantState: StatusCompleted}, + {name: "skip moves on", ifEmpty: "skip"}, + {name: "read-only stage with no changes fails", readOnly: true, ifEmpty: "", wantErr: true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + trees := &noChangesTrees{changed: false} + tc := &TaskContext{Task: &task.Task{ID: 1, Attempt: 1, Source: task.SourceTest}, Trees: trees, Dir: "/wt", Log: slog.Default()} + + stage := AgentStage{Name: "build", ReadOnly: tt.readOnly} + if err := stage.deliverResult(tc, agentrun.Result{Status: agentrun.StatusPassed}); err != nil { + t.Fatal(err) + } + repo, err := newRepoCommitStage(repoCommitNode(t, tt.ifEmpty)) + if err != nil { + t.Fatal(err) + } + err = repo.Run(context.Background(), tc) + if (err != nil) != tt.wantErr { + t.Fatalf("err = %v, wantErr %v", err, tt.wantErr) + } + if trees.pushed != tt.wantPush { + t.Fatalf("pushed = %v, want %v", trees.pushed, tt.wantPush) + } + if tt.wantState != "" && tc.Outcome.Status != tt.wantState { + t.Fatalf("outcome = %q, want %q", tc.Outcome.Status, tt.wantState) + } + }) + } +} diff --git a/internal/domain/workflow/repo_steps.go b/internal/domain/workflow/repo_steps.go index 3576713ce..abe00b4c1 100644 --- a/internal/domain/workflow/repo_steps.go +++ b/internal/domain/workflow/repo_steps.go @@ -117,6 +117,9 @@ func newRepoCommitStage(settings yaml.Node) (Stage, error) { if err != nil { return err } + if changed { + tc.BuildNoChanges = false + } if !changed && !tc.BaselineFixed { switch s.IfEmpty { case "skip": diff --git a/internal/domain/workflow/workflow.go b/internal/domain/workflow/workflow.go index 5bf40a436..ce564ea38 100644 --- a/internal/domain/workflow/workflow.go +++ b/internal/domain/workflow/workflow.go @@ -132,9 +132,9 @@ type TaskContext struct { workflowBranch string // BuildSummary is the builder agent's finish summary -- the PR body. BuildSummary string - // BuildNoChanges is set when the builder returned StatusPassed but - // made no file changes -- the fix already exists or the issue is a - // no-op. StageCommitPush closes the issue instead of erroring. + // BuildNoChanges is set when a writable agent stage passes and is cleared + // by repo.commit if it commits changes. While set, an empty commit with no + // explicit if_empty closes the issue: the builder found nothing to do. BuildNoChanges bool // BaselineFixed is set when StageBaselineGate committed a fix, so // StageCommitPush pushes it even when the build changed nothing.