Plan item 5 of docs/prds/execution-tree-state-machine.md (archie-core-1217).
Until now a stop was a context cancel plus a caller-side transition, and the
step rows a worker still held running could never be told: the execution moved
while the work carried on, and the old interrupt-first ordering existed only
because the record could not defend itself. The step table can: the PRD's
ordering -- the store is the record, the context cancel is the delivery --
became safe the moment a worker's next step write fails ErrStaleTransition
against a cancelled step.
- storecontract.ExecutionCanceller and the CancelExecution RPC: one
transaction moves every non-terminal StepExecution of the execution's
current attempt to cancelled (one event row each, in the same
transaction), and the execution itself to the status the operator action
names, under the shared transition table with its audit row. Staleness is
decided before legality, the way guardTransition decides it. Earlier
attempts are never rewritten. Deliberately administrative: the
deny-by-default grant rule covers it, pinned by a test.
- taskactions.Service routes stop, cancel, abandon and reject through it and
the old Transition/CancelTask combinations are deleted; the daemon's
in-memory cancel is demoted to the delivery hook the PRD names, called
after the store record.
- The gateway's chat controller records first and delivers second, the same
order, and records even when no runtime is wired here (a standalone
gateway cannot reach the daemon's goroutines): the record is what unsticks
a lost-cancel task, and the delivery is best effort. StopRunning and
TaskRuntime.CancelRunningTasks are deleted outright -- no production
caller remained once the task_action tool owned stopping, and the daemon's
CancelRunningTasks/runningTasks.stopAll go with them.
- The gateway's chat adapter writes through CancelExecution instead of a
bare Transition, so chat /cancel and the dashboard's action service land
on the same path.
PRD decisions recorded here:
- Each cancelled step's event row is stage_finish with {cancelled: true} and
the reason as its detail -- the exact-one-event-per-step-transition rule
covers the pending -> cancelled transition too, even for a step that never
ran.
- A parked cancel carries the needs_human class, matching the Transition
path this replaces; the transient class stays with parkRunningTask's
unwind parks, which now meet ErrStaleTransition for cancelled runs and are
ignored there.
- Cancelling a wait:true callee is deferred to #1216's agent-call children:
the callee's own tree and the wait flag are what make
"cancelling a caller cancels each callee it waits on" decidable, and the
callee writes it cannot fake are this issue's half of the contract.
- RecoverStale's interrupted sweep is plan item 6, untouched here.
129 lines
4.6 KiB
Go
129 lines
4.6 KiB
Go
package archied
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"log/slog"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strconv"
|
|
"testing"
|
|
|
|
"github.com/samcharles93/archie-core/internal/config"
|
|
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
|
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
|
"github.com/samcharles93/archie-core/internal/gateway"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
|
"github.com/samcharles93/archie-core/internal/taskstate/taskstatetest"
|
|
"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: workflow.StatusClosedWontDo},
|
|
{name: "chat cancel", viaChat: true, action: "cancel", want: workflow.StatusClosedWontDo},
|
|
{name: "dashboard approve", action: "approve", want: workflow.StatusQueued},
|
|
{name: "chat approve", viaChat: true, action: "approve", want: workflow.StatusQueued},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
ctx := context.Background()
|
|
st := pgstore.Open(t)
|
|
t.Cleanup(func() { _ = st.Close() })
|
|
|
|
task, err := st.EnqueueChatTask(ctx, "acme", "widget", "decide on me", "", "", "reviewer")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
taskstatetest.Seed(ctx, t, st, task.ID, workflow.StatusQueued, workflow.StatusWaitingHuman, "await")
|
|
|
|
if tc.viaChat {
|
|
runChatAction(t, ctx, st, tc.action, task.ID)
|
|
} else {
|
|
runDashboardAction(t, ctx, st, tc.action, task.ID)
|
|
}
|
|
|
|
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)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// runChatAction performs the approve/cancel action through the chat
|
|
// controller path and fails the test on error.
|
|
func runChatAction(t *testing.T, ctx context.Context, st storecontract.TaskStore, action string, taskID int64) {
|
|
t.Helper()
|
|
canceller, ok := st.(storecontract.ExecutionCanceller)
|
|
if !ok {
|
|
t.Fatal("the task store cannot cancel executions")
|
|
}
|
|
controller := gateway.NewStoreTaskController(chatTaskControllerAdapter{
|
|
taskByID: st.TaskByID,
|
|
requeue: st.Requeue,
|
|
cancelExecution: canceller.CancelExecution,
|
|
})
|
|
var err error
|
|
if action == "approve" {
|
|
err = controller.Approve(ctx, taskID, "reviewer")
|
|
} else {
|
|
err = controller.Cancel(ctx, taskID, "reviewer")
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("chat %s: %v", action, err)
|
|
}
|
|
}
|
|
|
|
// runDashboardAction performs the approve/cancel action through the
|
|
// dashboard's HTTP handler and fails the test when the request is
|
|
// rejected.
|
|
func runDashboardAction(t *testing.T, ctx context.Context, st storecontract.TaskStore, action string, taskID int64) {
|
|
t.Helper()
|
|
// The dashboard reaches task actions through the Gateway contract, which
|
|
// in production carries them back to this daemon's own service. Wiring
|
|
// that service behind a local adapter keeps both surfaces in this test
|
|
// driving the one implementation, which is the whole point of it.
|
|
actor := testTaskActor{&boot{
|
|
st: st, stateStore: st,
|
|
cfg: config.Config{MaxRetries: 3},
|
|
cfgHolder: config.NewHolder(config.Config{MaxRetries: 3}),
|
|
log: slog.New(slog.DiscardHandler),
|
|
}}
|
|
srv := &webui.Server{
|
|
Store: st,
|
|
Chat: &webui.ChatService{Contract: &gateway.LocalChatAdapter{TaskActor: actor}},
|
|
}
|
|
req := httptest.NewRequestWithContext(ctx, http.MethodPost,
|
|
"/api/tasks/"+strconv.FormatInt(taskID, 10)+"/action",
|
|
bytes.NewBufferString(`{"action":"`+action+`"}`))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("X-Archie-CSRF", "1")
|
|
w := httptest.NewRecorder()
|
|
srv.Handler().ServeHTTP(w, req)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("dashboard %s: status %d, body %s", action, w.Code, w.Body)
|
|
}
|
|
}
|