A retry now carries the operator's worktree choice (refresh onto the base branch or continue the work already pushed on the task branch) from the dashboard through the task action to the daemon dispatch, and persists it on the task row so it survives a reload. The daemon resolves the mode from the task, never from the workflow; BeginRemediation records continue explicitly so a remediation still resumes its pull request branch.
324 lines
12 KiB
Go
324 lines
12 KiB
Go
package taskactions
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/identity"
|
|
"github.com/samcharles93/archie-core/internal/events"
|
|
"github.com/samcharles93/archie-core/internal/taskstate"
|
|
)
|
|
|
|
type fakeStore struct {
|
|
task *Task
|
|
events []events.Event
|
|
retryMode string
|
|
retried bool
|
|
}
|
|
|
|
func (f *fakeStore) TaskByID(context.Context, int64) (*Task, error) { return f.task, nil }
|
|
func (f *fakeStore) Transition(context.Context, int64, string, string, string) error {
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeStore) CancelExecution(context.Context, int64, string, string) ([]int64, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
func (f *fakeStore) Requeue(context.Context, int64, string, string) error { return nil }
|
|
|
|
func (f *fakeStore) RespondReviewGate(context.Context, int64, string, string, bool, int) error {
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeStore) RetryTask(_ context.Context, _ int64, _, _, retryMode string) error {
|
|
f.retryMode = retryMode
|
|
f.retried = true
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeStore) ArchiveTask(_ context.Context, _ int64, _ string, e events.Event) (int64, error) {
|
|
f.events = append(f.events, e)
|
|
return int64(len(f.events)), nil
|
|
}
|
|
|
|
func (f *fakeStore) InsertEvent(_ context.Context, e events.Event) (int64, error) {
|
|
f.events = append(f.events, e)
|
|
return int64(len(f.events)), nil
|
|
}
|
|
|
|
func (f *fakeStore) last() events.Event {
|
|
if len(f.events) == 0 {
|
|
return events.Event{}
|
|
}
|
|
return f.events[len(f.events)-1]
|
|
}
|
|
|
|
const (
|
|
humanID = identity.IdentityID("20000000-0000-5000-8000-000000000001")
|
|
agentID = identity.IdentityID("20000000-0000-5000-8000-000000000002")
|
|
)
|
|
|
|
func humanActor() Actor {
|
|
return ActorFor(identity.Identity{ID: humanID, Kind: identity.KindUser, Lifecycle: identity.LifecycleActive})
|
|
}
|
|
|
|
func agentActor() Actor {
|
|
return ActorFor(identity.Identity{ID: agentID, Kind: identity.KindBot, Lifecycle: identity.LifecycleActive})
|
|
}
|
|
|
|
// TestEventKindDescribesTheActor is the rule that the bug turned on: an agent's
|
|
// approval was written down as a human's. The kind names the actor, so an
|
|
// agent's action can never be read as a person's, and an action with no verified
|
|
// actor is recorded as a task-level event that credits nobody.
|
|
func TestEventKindDescribesTheActor(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
action taskstate.Action
|
|
actor Actor
|
|
wantKind string
|
|
}{
|
|
{"a person approves", taskstate.ActionApprove, humanActor(), events.KindHumanApproved},
|
|
{"an agent approves", taskstate.ActionApprove, agentActor(), events.KindAgentApproved},
|
|
{"a person rejects", taskstate.ActionReject, humanActor(), events.KindHumanRejected},
|
|
{"an agent rejects", taskstate.ActionReject, agentActor(), events.KindAgentRejected},
|
|
{"an unattributed approval", taskstate.ActionApprove, Actor{}, events.KindTaskApproved},
|
|
{"an unattributed rejection", taskstate.ActionReject, Actor{}, events.KindTaskRejected},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
store := &fakeStore{task: &Task{ID: 7, Owner: "acme", Repo: "widgets", Status: "waiting_human"}}
|
|
service := Service{Store: store}
|
|
|
|
if err := service.Apply(context.Background(), nil, tc.actor, 7, tc.action, ActionPayload{}); err != nil {
|
|
t.Fatalf("Apply() error = %v", err)
|
|
}
|
|
got := store.last()
|
|
if got.Kind != tc.wantKind {
|
|
t.Fatalf("event kind = %q, want %q", got.Kind, tc.wantKind)
|
|
}
|
|
if tc.actor.Human() && got.Kind == events.KindAgentApproved {
|
|
t.Fatal("a person's action was recorded as an agent's")
|
|
}
|
|
if tc.actor.Attributed() && !tc.actor.Human() && got.Kind == events.KindHumanApproved {
|
|
t.Fatal("an agent's action was recorded as a human approval")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestActorAndPrincipalAreRecordedSeparately is the two-attribution requirement:
|
|
// an agent acting under a person's standing authority records both, so "approved
|
|
// by came from me" is answerable from the record.
|
|
func TestActorAndPrincipalAreRecordedSeparately(t *testing.T) {
|
|
store := &fakeStore{task: &Task{ID: 7, Owner: "acme", Repo: "widgets", Status: "waiting_human"}}
|
|
service := Service{Store: store}
|
|
|
|
actor := agentActor().AuthorisedBy(humanID)
|
|
if err := service.Apply(context.Background(), nil, actor, 7, taskstate.ActionApprove, ActionPayload{}); err != nil {
|
|
t.Fatalf("Apply() error = %v", err)
|
|
}
|
|
|
|
got := store.last()
|
|
if got.ActorID != string(agentID) {
|
|
t.Fatalf("actor = %q, want %q", got.ActorID, agentID)
|
|
}
|
|
if got.ActorKind != string(identity.KindBot) {
|
|
t.Fatalf("actor kind = %q, want %q", got.ActorKind, identity.KindBot)
|
|
}
|
|
if got.PrincipalID != string(humanID) {
|
|
t.Fatalf("principal = %q, want %q", got.PrincipalID, humanID)
|
|
}
|
|
if got.Kind != events.KindAgentApproved {
|
|
t.Fatalf("event kind = %q, want %q", got.Kind, events.KindAgentApproved)
|
|
}
|
|
}
|
|
|
|
// TestUnattributedActionClaimsNoApprover: with no verified actor the record must
|
|
// not credit the actor's own authority, and must not name a human.
|
|
func TestUnattributedActionClaimsNoApprover(t *testing.T) {
|
|
store := &fakeStore{task: &Task{ID: 7, Owner: "acme", Repo: "widgets", Status: "waiting_human"}}
|
|
service := Service{Store: store}
|
|
|
|
if err := service.Apply(context.Background(), nil, Actor{}, 7, taskstate.ActionApprove, ActionPayload{}); err != nil {
|
|
t.Fatalf("Apply() error = %v", err)
|
|
}
|
|
|
|
got := store.last()
|
|
if got.ActorID != "" || got.ActorKind != "" || got.PrincipalID != "" {
|
|
t.Fatalf("unattributed action recorded an actor: %+v", got)
|
|
}
|
|
if got.Kind == events.KindHumanApproved || got.Kind == events.KindAgentApproved {
|
|
t.Fatalf("unattributed action claimed an actor through its kind: %q", got.Kind)
|
|
}
|
|
}
|
|
|
|
// TestEveryActionCarriesItsAttribution: no action path may record an event that
|
|
// skipped the attribution, including the ones that also close a forge issue.
|
|
func TestEveryActionCarriesItsAttribution(t *testing.T) {
|
|
actions := []struct {
|
|
action taskstate.Action
|
|
status string
|
|
}{
|
|
{taskstate.ActionApprove, "waiting_human"},
|
|
{taskstate.ActionReject, "waiting_human"},
|
|
{taskstate.ActionRetry, "parked"},
|
|
{taskstate.ActionStop, "running"},
|
|
{taskstate.ActionCancel, "queued"},
|
|
{taskstate.ActionAbandon, "parked"},
|
|
}
|
|
|
|
for _, tc := range actions {
|
|
t.Run(string(tc.action), func(t *testing.T) {
|
|
store := &fakeStore{task: &Task{ID: 7, Owner: "acme", Repo: "widgets", Status: tc.status}}
|
|
service := Service{
|
|
Store: store,
|
|
CancelTask: func(int64) bool { return true },
|
|
}
|
|
if err := service.Apply(context.Background(), nil, agentActor(), 7, tc.action, ActionPayload{}); err != nil {
|
|
t.Fatalf("Apply(%s) error = %v", tc.action, err)
|
|
}
|
|
got := store.last()
|
|
if got.ActorID != string(agentID) || got.ActorKind != string(identity.KindBot) {
|
|
t.Fatalf("%s recorded without its actor: %+v", tc.action, got)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestScopeStillLimitsATaskButIsNotTheActor: scope answers which task a caller
|
|
// may touch. It is not an actor, and passing one must not attribute the action
|
|
// to it.
|
|
func TestScopeStillLimitsATaskButIsNotTheActor(t *testing.T) {
|
|
other := "sam"
|
|
store := &fakeStore{task: &Task{ID: 7, Owner: "acme", Repo: "widgets", Status: "waiting_human", Identity: "archie"}}
|
|
service := Service{Store: store}
|
|
|
|
if err := service.Apply(context.Background(), &other, agentActor(), 7, taskstate.ActionApprove, ActionPayload{}); err == nil {
|
|
t.Fatal("Apply() allowed an identity to act on another identity's task")
|
|
}
|
|
if len(store.events) != 0 {
|
|
t.Fatalf("a refused action recorded %d events", len(store.events))
|
|
}
|
|
|
|
if err := service.Apply(context.Background(), &other, agentActor(), 7, taskstate.ActionApprove, ActionPayload{}); err == nil {
|
|
t.Fatal("Apply() allowed a scoped caller to act on a foreign task")
|
|
}
|
|
}
|
|
|
|
func TestActorForCarriesTheKindAndLeavesTheActionUnattributedWithoutAPrincipal(t *testing.T) {
|
|
actor := agentActor()
|
|
if actor.Principal != "" {
|
|
t.Fatalf("ActorFor() set a principal: %q", actor.Principal)
|
|
}
|
|
if !actor.Attributed() {
|
|
t.Fatal("ActorFor() produced an unattributed actor for a real identity")
|
|
}
|
|
if (Actor{}).Attributed() {
|
|
t.Fatal("the zero Actor reported itself as attributed")
|
|
}
|
|
if (Actor{}).Human() {
|
|
t.Fatal("the zero Actor reported itself as a human")
|
|
}
|
|
}
|
|
|
|
// TestOnlyRejectAndCancelCloseTheIssue: abandoning is archie giving up on its
|
|
// own run, not a verdict on the issue, so the issue stays open for a human.
|
|
func TestOnlyRejectAndCancelCloseTheIssue(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
action taskstate.Action
|
|
status string
|
|
closes bool
|
|
}{
|
|
{taskstate.ActionReject, "waiting_human", true},
|
|
{taskstate.ActionCancel, "queued", true},
|
|
{taskstate.ActionAbandon, "parked", false},
|
|
} {
|
|
t.Run(string(tc.action), func(t *testing.T) {
|
|
store := &fakeStore{task: &Task{ID: 7, Owner: "acme", Repo: "widgets", Status: tc.status, ForgeBacked: true}}
|
|
closed := false
|
|
service := Service{Store: store, CloseIssue: func(context.Context, string, string, int, string) error {
|
|
closed = true
|
|
return nil
|
|
}}
|
|
if err := service.Apply(context.Background(), nil, agentActor(), 7, tc.action, ActionPayload{}); err != nil {
|
|
t.Fatalf("Apply(%s) error = %v", tc.action, err)
|
|
}
|
|
if closed != tc.closes {
|
|
t.Fatalf("Apply(%s) closed the issue = %v, want %v", tc.action, closed, tc.closes)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestRetryPersistsTheOperatorsWorktreeMode pins the retry half of the
|
|
// operator surface: the mode the dashboard sent reaches the store write, so
|
|
// the daemon reads the operator's choice instead of inferring one. The event
|
|
// records the mode too, so the timeline says how the retry was dispatched.
|
|
func TestRetryPersistsTheOperatorsWorktreeMode(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
mode taskstate.RetryMode
|
|
persisted string
|
|
branch string
|
|
wantMode string
|
|
}{
|
|
{"continue pushed work", taskstate.RetryContinuePushedWork, "", "fix/7-thing", "continue_pushed_work"},
|
|
{"refresh onto base", taskstate.RetryRefreshOntoBase, "", "", "refresh_onto_base"},
|
|
{"an empty mode is the explicit refresh default", "", "", "", "refresh_onto_base"},
|
|
{"an empty mode keeps the persisted choice", "", string(taskstate.RetryContinuePushedWork), "fix/7-thing", "continue_pushed_work"},
|
|
{"an explicit mode overrides the persisted choice", taskstate.RetryRefreshOntoBase, string(taskstate.RetryContinuePushedWork), "fix/7-thing", "refresh_onto_base"},
|
|
}
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
store := &fakeStore{task: &Task{ID: 7, Owner: "acme", Repo: "widgets", Status: taskstate.Parked, Branch: tc.branch, RetryMode: tc.persisted}}
|
|
service := Service{Store: store}
|
|
|
|
if err := service.Apply(context.Background(), nil, humanActor(), 7, taskstate.ActionRetry, ActionPayload{RetryMode: tc.mode}); err != nil {
|
|
t.Fatalf("Apply(retry) error = %v", err)
|
|
}
|
|
if !store.retried || store.retryMode != tc.wantMode {
|
|
t.Fatalf("RetryTask mode = %q (called %v), want %q", store.retryMode, store.retried, tc.wantMode)
|
|
}
|
|
if got := store.last().Data["retry_mode"]; got != tc.wantMode {
|
|
t.Errorf("retry event retry_mode = %v, want %q", got, tc.wantMode)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestRetryRefusesModesThatCannotLand keeps a refuse-from-the-operator rule at
|
|
// the service, where every surface reaches it: continuing work with no pushed
|
|
// branch would queue a resume that cannot land, and an unknown mode is a
|
|
// caller bug rather than a task to reset. Both are conflicts and neither
|
|
// writes.
|
|
func TestRetryRefusesModesThatCannotLand(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
mode taskstate.RetryMode
|
|
branch string
|
|
}{
|
|
{"continue with no pushed branch", taskstate.RetryContinuePushedWork, ""},
|
|
{"an unknown mode", taskstate.RetryMode("reset_to_branch_head"), "fix/7-thing"},
|
|
}
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
store := &fakeStore{task: &Task{ID: 7, Owner: "acme", Repo: "widgets", Status: taskstate.Parked, Branch: tc.branch}}
|
|
service := Service{Store: store}
|
|
|
|
err := service.Apply(context.Background(), nil, humanActor(), 7, taskstate.ActionRetry, ActionPayload{RetryMode: tc.mode})
|
|
if err == nil {
|
|
t.Fatal("Apply(retry) = nil error, want a conflict")
|
|
}
|
|
if !errors.Is(err, ErrConflict) {
|
|
t.Fatalf("Apply(retry) error = %v, want ErrConflict", err)
|
|
}
|
|
if store.retried {
|
|
t.Error("RetryTask was called, want the refusal to write nothing")
|
|
}
|
|
})
|
|
}
|
|
}
|