refactor: one definition of what approving or declining a task means
The dashboard and chat both let an operator approve or refuse work, and they had drifted. Declining from the dashboard left the task closed_wont_do; cancelling the same task from chat left it rejected. Two operators looking at one queue saw different states for the same decision, and "rejected" -- which reconcilePRs also uses for a pull request closed without merging -- stopped meaning one thing. internal/gateway deliberately does not import internal/store, so it kept a hand-synced copy of the status strings with a comment asking future editors to keep them aligned. That is the same defect one level down, and it is how the drift happened. internal/taskstate is a leaf package with no dependencies holding the lifecycle vocabulary and the rules for operator actions. gateway, store and the dashboard all import it, so gateway stays decoupled from store without a copy, and CheckApprove/CheckRetry/CheckDecline have one implementation. Declined is named for what it is -- a human said no -- and stays distinct from Rejected, which is something archie observed. Chat cancel now records Declined. That is the behaviour change: same decision, same state, whichever surface the operator used. Rules were preserved rather than tidied while unifying. A parked task still cannot be declined, which both surfaces already refused, even though "not terminal" would have allowed it -- widening a rule under cover of a refactor is how you ship a change nobody reviewed. The comment says where to change it if that is ever wanted. TestDashboardAndChatAgreeOnTerminalStates drives both real surfaces against one store, so this cannot drift apart again without a red test. Also removes a second rule for the dash-top-standalone class: renderSetup already reconciles it from the data at the top of every call, and a later branch cleared it again. Harmless, but it reads as two sources of truth for one bit of layout state.
This commit is contained in:
1 parent
d6e00415fa
commit
1f2e7a42eb
10 files changed
+329
-46
No files matched your search
+7
-1
@@ -788,7 +788,13 @@ func (a chatTaskControllerAdapter) CancelChatTask(ctx context.Context, taskID in
|
||||
if task.IsForgeBacked() {
|
||||
return fmt.Errorf("task %d is not chat-originated", taskID)
|
||||
}
|
||||
return a.transition(ctx, taskID, task.Status, store.StatusRejected, reason)
|
||||
// An operator declining work lands in the same state whichever surface
|
||||
// they used. This used to record StatusRejected while the dashboard
|
||||
// recorded StatusClosedWontDo, so the same decision showed up as two
|
||||
// different states -- and StatusRejected, which the PR reconciler uses
|
||||
// for "the pull request was closed without merging", stopped meaning one
|
||||
// thing.
|
||||
return a.transition(ctx, taskID, task.Status, store.StatusClosedWontDo, reason)
|
||||
}
|
||||
|
||||
func chatTaskProfiles(cfg config.Config) ([]gateway.TaskProfile, string) {
|
||||
|
||||
@@ -419,7 +419,11 @@ func TestChatTaskControllerAdapterTransitions(t *testing.T) {
|
||||
if err := adapter.CancelChatTask(context.Background(), task.ID, "cancelled by test"); err != nil {
|
||||
t.Fatalf("CancelChatTask(): %v", err)
|
||||
}
|
||||
if transitionFrom != store.StatusWaitingHuman || transitionTo != store.StatusRejected ||
|
||||
// Declining from chat lands in the same state as declining from the
|
||||
// dashboard. It used to record StatusRejected, which the PR reconciler
|
||||
// also uses for "the pull request was closed without merging", so the
|
||||
// state could not tell an operator's decision from a forge outcome.
|
||||
if transitionFrom != store.StatusWaitingHuman || transitionTo != store.StatusClosedWontDo ||
|
||||
transitionDetail != "cancelled by test" {
|
||||
t.Errorf("transition = %q/%q/%q", transitionFrom, transitionTo, transitionDetail)
|
||||
}
|
||||
@@ -507,8 +511,9 @@ func TestChatTaskCommandsEndToEnd(t *testing.T) {
|
||||
if err != nil || !strings.Contains(reply, "cancelled") {
|
||||
t.Fatalf("cancel = (%q, %v)", reply, err)
|
||||
}
|
||||
// Same terminal state as the dashboard's Reject: one decision, one state.
|
||||
cancelled, err := st.TaskByID(ctx, cancelID)
|
||||
if err != nil || cancelled == nil || cancelled.Status != store.StatusRejected {
|
||||
if err != nil || cancelled == nil || cancelled.Status != store.StatusClosedWontDo {
|
||||
t.Fatalf("cancelled task = (%+v, %v)", cancelled, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/gateway"
|
||||
"github.com/samcharles93/archie-core/internal/nell"
|
||||
"github.com/samcharles93/archie-core/internal/store"
|
||||
"github.com/samcharles93/archie-core/internal/webui"
|
||||
)
|
||||
|
||||
// The dashboard and chat both let an operator approve or decline a task, and
|
||||
// they had drifted: declining from the dashboard left the task
|
||||
// closed_wont_do, cancelling the same task from chat left it rejected. Two
|
||||
// operators looking at one queue saw different states for the same decision,
|
||||
// and "rejected" -- which reconcilePRs uses for a pull request closed without
|
||||
// merging -- no longer meant one thing.
|
||||
//
|
||||
// This drives both real surfaces against one store and asserts they agree.
|
||||
// It lives in cmd/archied because that is the only place both are wired.
|
||||
func TestDashboardAndChatAgreeOnTerminalStates(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
// viaChat runs the action through the chat router; otherwise it
|
||||
// goes through the dashboard's HTTP handler.
|
||||
viaChat bool
|
||||
action string
|
||||
want string
|
||||
}{
|
||||
{name: "dashboard reject", action: "reject", want: store.StatusClosedWontDo},
|
||||
{name: "chat cancel", viaChat: true, action: "cancel", want: store.StatusClosedWontDo},
|
||||
{name: "dashboard approve", action: "approve", want: store.StatusQueued},
|
||||
{name: "chat approve", viaChat: true, action: "approve", want: store.StatusQueued},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
st, err := nell.OpenStore(filepath.Join(t.TempDir(), "tasks.db"), "test-node")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = st.Close() })
|
||||
|
||||
task, err := st.EnqueueChatTask(ctx, "acme", "widget", "decide on me", "", "", "reviewer", 900_001)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := st.Transition(ctx, task.ID, store.StatusQueued, store.StatusWaitingHuman, "await"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if tc.viaChat {
|
||||
controller := gateway.NewStoreTaskController(chatTaskControllerAdapter{
|
||||
taskByID: st.TaskByID,
|
||||
requeue: st.Requeue,
|
||||
transition: st.Transition,
|
||||
})
|
||||
var actErr error
|
||||
if tc.action == "approve" {
|
||||
actErr = controller.Approve(ctx, task.ID, "reviewer")
|
||||
} else {
|
||||
actErr = controller.Cancel(ctx, task.ID, "reviewer")
|
||||
}
|
||||
if actErr != nil {
|
||||
t.Fatalf("chat %s: %v", tc.action, actErr)
|
||||
}
|
||||
} else {
|
||||
srv := &webui.Server{Store: st, Cfg: &config.Config{MaxRetries: 3}}
|
||||
req := httptest.NewRequestWithContext(ctx, http.MethodPost,
|
||||
"/api/tasks/"+strconv.FormatInt(task.ID, 10)+"/action",
|
||||
bytes.NewBufferString(`{"action":"`+tc.action+`"}`))
|
||||
w := httptest.NewRecorder()
|
||||
srv.Handler().ServeHTTP(w, req)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("dashboard %s: status %d, body %s", tc.action, w.Code, w.Body)
|
||||
}
|
||||
}
|
||||
|
||||
got, err := st.TaskByID(ctx, task.ID)
|
||||
if err != nil || got == nil {
|
||||
t.Fatalf("TaskByID = (%+v, %v)", got, err)
|
||||
}
|
||||
if got.Status != tc.want {
|
||||
t.Errorf("status = %q, want %q: the two surfaces disagree about "+
|
||||
"what this action means", got.Status, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user