prepareWorkspace chose between Manager.Prepare (refresh onto base) and a separate Manager.Resume, and Manager.Resume only opened an already-prepared clone. A remediation whose local worktree the terminal cleanup removed, an expired volume dropped, or a different host never had could therefore never be resumed, and the retry path had no way to select the mode that continues pushed work. Unify the two matched methods into Prepare(..., Target): Fresh resets onto origin/<base>, a non-empty Target resets onto origin/<that branch>, and either target clones a missing worktree. The daemon's retry path selects the target from the task row and passes the persisted branch, so a resume lands on the branch the open PR lives on and fails closed when it is not on the remote. Delete the container-mode no-op resume (hybridTrees.Resume and the resumableTrees capability) now that the daemon owns it. The remediate workflow no longer stages a resume, and the retired remediate.resume step word stays resolvable as an inert stage: a stored definition is validated as a whole collection, so a removed word would fail every workflow to decode, not just its own.
225 lines
7.8 KiB
Go
225 lines
7.8 KiB
Go
package workflow
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"log/slog"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/samcharles93/archie-core/internal/agentexec"
|
|
"github.com/samcharles93/archie-core/internal/config"
|
|
)
|
|
|
|
func TestDecodeReviewUnitRejectsEmptyPayload(t *testing.T) {
|
|
if _, err := DecodeReviewUnit(""); !errors.Is(err, ErrNoReviewPayload) {
|
|
t.Fatalf("DecodeReviewUnit(\"\") error = %v, want ErrNoReviewPayload", err)
|
|
}
|
|
}
|
|
|
|
func TestEncodeDecodeReviewUnitRoundTrips(t *testing.T) {
|
|
want := ReviewUnit{
|
|
ReviewID: 42, Author: "reviewer-bot", State: "requested_changes", Body: "please fix",
|
|
Comments: []ReviewUnitComment{{CommentID: 7, Path: "a.go", Line: 3, Body: "nil check missing"}},
|
|
}
|
|
payload, err := EncodeReviewUnit(want)
|
|
if err != nil {
|
|
t.Fatalf("EncodeReviewUnit: %v", err)
|
|
}
|
|
got, err := DecodeReviewUnit(payload)
|
|
if err != nil {
|
|
t.Fatalf("DecodeReviewUnit: %v", err)
|
|
}
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Fatalf("round-tripped unit = %+v, want %+v", got, want)
|
|
}
|
|
}
|
|
|
|
func TestStageCheckReviewPayloadFailsOnEmptyPayload(t *testing.T) {
|
|
tc := &TaskContext{Task: &Task{ID: 1}}
|
|
if err := StageCheckReviewPayload().Run(t.Context(), tc); !errors.Is(err, ErrNoReviewPayload) {
|
|
t.Fatalf("StageCheckReviewPayload error = %v, want ErrNoReviewPayload", err)
|
|
}
|
|
}
|
|
|
|
func TestStageRemediationRoundCapCountsARoundUnderTheCap(t *testing.T) {
|
|
forge := &fakeForge{}
|
|
tc := &TaskContext{
|
|
Task: &Task{ID: 1, RemediationRounds: 1}, Repo: config.Repo{MaxRetries: 3}, Forge: forge,
|
|
Log: slog.New(slog.DiscardHandler),
|
|
}
|
|
if err := StageRemediationRoundCap().Run(t.Context(), tc); err != nil {
|
|
t.Fatalf("StageRemediationRoundCap: %v", err)
|
|
}
|
|
if tc.Task.RemediationRounds != 2 {
|
|
t.Fatalf("RemediationRounds = %d, want 2", tc.Task.RemediationRounds)
|
|
}
|
|
if tc.Outcome.Status != "" {
|
|
t.Fatalf("Outcome = %+v, want zero value (workflow continues)", tc.Outcome)
|
|
}
|
|
if len(forge.commented) != 0 {
|
|
t.Fatalf("commented = %v, want no comment posted under the cap", forge.commented)
|
|
}
|
|
}
|
|
|
|
func TestStageRemediationRoundCapParksAtTheCapAndPostsOneComment(t *testing.T) {
|
|
forge := &fakeForge{}
|
|
tc := &TaskContext{
|
|
Task: &Task{ID: 1, RemediationRounds: 3, PRNumber: 9}, Repo: config.Repo{MaxRetries: 3}, Forge: forge,
|
|
Log: slog.New(slog.DiscardHandler),
|
|
}
|
|
if err := StageRemediationRoundCap().Run(t.Context(), tc); err != nil {
|
|
t.Fatalf("StageRemediationRoundCap: %v", err)
|
|
}
|
|
if tc.Task.RemediationRounds != 3 {
|
|
t.Fatalf("RemediationRounds = %d, want unchanged at 3", tc.Task.RemediationRounds)
|
|
}
|
|
if tc.Outcome.Status != StatusParked {
|
|
t.Fatalf("Outcome.Status = %q, want parked", tc.Outcome.Status)
|
|
}
|
|
if len(forge.commented) != 1 {
|
|
t.Fatalf("commented = %v, want exactly one comment", forge.commented)
|
|
}
|
|
}
|
|
|
|
func TestStageRemediationRoundCapZeroMeansUnlimited(t *testing.T) {
|
|
tc := &TaskContext{
|
|
Task: &Task{ID: 1, RemediationRounds: 50}, Repo: config.Repo{}, Cfg: config.Config{MaxRetries: 0},
|
|
Forge: &fakeForge{}, Log: slog.New(slog.DiscardHandler),
|
|
}
|
|
if err := StageRemediationRoundCap().Run(t.Context(), tc); err != nil {
|
|
t.Fatalf("StageRemediationRoundCap: %v", err)
|
|
}
|
|
if tc.Outcome.Status == StatusParked {
|
|
t.Fatal("Outcome = parked, want no cap enforced when max_retries is unset")
|
|
}
|
|
if tc.Task.RemediationRounds != 51 {
|
|
t.Fatalf("RemediationRounds = %d, want 51", tc.Task.RemediationRounds)
|
|
}
|
|
}
|
|
|
|
func TestStageRemediationCommitPushSkipsAnEmptyTreeWithoutError(t *testing.T) {
|
|
trees := &fakeTrees{commitAllChanged: false}
|
|
tc := &TaskContext{
|
|
Task: &Task{ID: 1, Owner: "o", Repo: "r", IssueNumber: 1}, Trees: trees,
|
|
Dir: "/tmp/x", Branch: "archie/issue-1",
|
|
}
|
|
if err := StageRemediationCommitPush().Run(t.Context(), tc); err != nil {
|
|
t.Fatalf("StageRemediationCommitPush: %v", err)
|
|
}
|
|
if trees.pushed {
|
|
t.Fatal("Push called with nothing committed, want no push")
|
|
}
|
|
}
|
|
|
|
func TestStageRemediationCommitPushCommitsAndPushesAChange(t *testing.T) {
|
|
trees := &fakeTrees{commitAllChanged: true}
|
|
tc := &TaskContext{
|
|
Task: &Task{ID: 1, Owner: "o", Repo: "r", IssueNumber: 1}, Trees: trees,
|
|
Dir: "/tmp/x", Branch: "archie/issue-1", Log: slog.New(slog.DiscardHandler),
|
|
}
|
|
if err := StageRemediationCommitPush().Run(t.Context(), tc); err != nil {
|
|
t.Fatalf("StageRemediationCommitPush: %v", err)
|
|
}
|
|
if !trees.pushed || trees.pushBranch != "archie/issue-1" {
|
|
t.Fatalf("pushed=%v pushBranch=%q, want true archie/issue-1", trees.pushed, trees.pushBranch)
|
|
}
|
|
}
|
|
|
|
func TestStageRemediationReplyThreadsOntoTheFirstComment(t *testing.T) {
|
|
forge := &fakeForge{}
|
|
tc := &TaskContext{
|
|
Task: &Task{ID: 1, Owner: "o", Repo: "r", PRNumber: 9, ReviewPayload: "stale"},
|
|
Forge: forge,
|
|
BuildSummary: "fixed the nil check",
|
|
reviewUnit: ReviewUnit{Comments: []ReviewUnitComment{{CommentID: 55, Body: "fix this"}}},
|
|
}
|
|
if err := StageRemediationReply().Run(t.Context(), tc); err != nil {
|
|
t.Fatalf("StageRemediationReply: %v", err)
|
|
}
|
|
if forge.replyCommentID != 55 {
|
|
t.Fatalf("replyCommentID = %d, want 55", forge.replyCommentID)
|
|
}
|
|
if len(forge.commented) != 0 {
|
|
t.Fatalf("commented = %v, want the plain-comment fallback unused", forge.commented)
|
|
}
|
|
if tc.Task.ReviewPayload != "" {
|
|
t.Fatalf("ReviewPayload = %q, want cleared after the run consumed it", tc.Task.ReviewPayload)
|
|
}
|
|
if tc.Outcome.Status != StatusPROpen {
|
|
t.Fatalf("Outcome.Status = %q, want pr_open", tc.Outcome.Status)
|
|
}
|
|
}
|
|
|
|
func TestStageRemediationReplyFallsBackToAPlainCommentForABareReview(t *testing.T) {
|
|
forge := &fakeForge{}
|
|
tc := &TaskContext{
|
|
Task: &Task{ID: 1, Owner: "o", Repo: "r", PRNumber: 9},
|
|
Forge: forge,
|
|
BuildSummary: "no change needed",
|
|
reviewUnit: ReviewUnit{Body: "looks fine overall"},
|
|
}
|
|
if err := StageRemediationReply().Run(t.Context(), tc); err != nil {
|
|
t.Fatalf("StageRemediationReply: %v", err)
|
|
}
|
|
if forge.replyCommentID != 0 {
|
|
t.Fatalf("replyCommentID = %d, want 0 (no threaded reply)", forge.replyCommentID)
|
|
}
|
|
if len(forge.commented) != 1 {
|
|
t.Fatalf("commented = %v, want exactly one plain comment", forge.commented)
|
|
}
|
|
}
|
|
|
|
func TestRemediateEndToEndAddressesCommentsAndReplies(t *testing.T) {
|
|
trees := &fakeTrees{dir: "/worktrees/o-r-1", commitAllChanged: true}
|
|
forge := &fakeForge{}
|
|
payload, err := EncodeReviewUnit(ReviewUnit{
|
|
ReviewID: 1, State: "requested_changes",
|
|
Comments: []ReviewUnitComment{{CommentID: 10, Path: "a.go", Line: 4, Body: "nil check missing"}},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("EncodeReviewUnit: %v", err)
|
|
}
|
|
task := &Task{
|
|
ID: 1, Owner: "o", Repo: "r", IssueNumber: 1, PRNumber: 9,
|
|
Branch: "archie/issue-1", ReviewPayload: payload,
|
|
}
|
|
runner := agentRunnerFunc(func(_ context.Context, _ string, req agentexec.Request, _ agentexec.ToolCallReporter) (agentexec.Result, error) {
|
|
return agentexec.Result{
|
|
Version: agentexec.ProtocolVersion, TaskID: req.TaskID, Attempt: req.Attempt, Stage: req.Stage,
|
|
Status: agentexec.StatusPassed, Summary: "added the nil check", Changes: []string{"a.go"},
|
|
}, nil
|
|
})
|
|
tc := &TaskContext{
|
|
Task: task, Repo: config.Repo{Owner: "o", Name: "r", MaxRetries: 3},
|
|
Cfg: config.Config{Models: map[string]string{"builder": "provider/model"}},
|
|
Agent: runner, Trees: trees, Forge: forge, Log: slog.New(slog.DiscardHandler),
|
|
}
|
|
|
|
wf := Remediate()
|
|
for _, stage := range wf.Stages {
|
|
if err := stage.Run(t.Context(), tc); err != nil {
|
|
t.Fatalf("stage %s: %v", stage.Name, err)
|
|
}
|
|
if tc.Outcome.Status != "" {
|
|
break
|
|
}
|
|
}
|
|
|
|
if !trees.pushed {
|
|
t.Fatal("remediation commit was never pushed")
|
|
}
|
|
if forge.replyCommentID != 10 {
|
|
t.Fatalf("replyCommentID = %d, want 10", forge.replyCommentID)
|
|
}
|
|
if task.RemediationRounds != 1 {
|
|
t.Fatalf("RemediationRounds = %d, want 1 (one round consumed)", task.RemediationRounds)
|
|
}
|
|
if task.ReviewPayload != "" {
|
|
t.Fatalf("ReviewPayload = %q, want cleared", task.ReviewPayload)
|
|
}
|
|
if tc.Outcome.Status != StatusPROpen {
|
|
t.Fatalf("Outcome.Status = %q, want pr_open", tc.Outcome.Status)
|
|
}
|
|
}
|