Files
archie/internal/domain/taskactions/cancel_path_test.go
T
sam 36981c2024 feat(statestore): CancelExecution, and one cancel path for daemon stop and operator actions
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.
2026-09-27 06:34:01 +10:00

94 lines
3.7 KiB
Go

package taskactions
import (
"context"
"slices"
"testing"
"github.com/samcharles93/archie-core/internal/events"
"github.com/samcharles93/archie-core/internal/taskstate"
)
// Every operator action that ends a run's work routes through the one cancel
// path, CancelExecution (docs/prds/execution-tree-state-machine.md,
// "Cancellation"): the store records the cancellation first, then the daemon
// cancels the in-memory context that was delivering the run. The actions'
// old Transition-then-interrupt combinations are gone, not alongside.
type cancelRecordingStore struct {
fakeStore
cancels []cancelCall
}
type cancelCall struct {
taskID int64
reason, to string
}
func (f *cancelRecordingStore) CancelExecution(_ context.Context, taskID int64, reason, to string) ([]int64, error) {
f.cancels = append(f.cancels, cancelCall{taskID: taskID, reason: reason, to: to})
return nil, nil
}
func TestOperatorActionsRouteThroughCancelExecution(t *testing.T) {
tests := []struct {
name string
status string
action taskstate.Action
wantTo string
}{
{name: "stop parks the run", status: taskstate.Running, action: taskstate.ActionStop, wantTo: taskstate.Parked},
{name: "cancel declines a queued run", status: taskstate.Queued, action: taskstate.ActionCancel, wantTo: taskstate.Declined},
{name: "abandon declines a parked run", status: taskstate.Parked, action: taskstate.ActionAbandon, wantTo: taskstate.Declined},
{name: "reject declines a running run", status: taskstate.Running, action: taskstate.ActionReject, wantTo: taskstate.Declined},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
store := &cancelRecordingStore{task: &Task{ID: 7, Owner: "acme", Repo: "widgets", Status: tt.status, IssueNumber: 3}}
var deliveries []int64
service := Service{
Store: store,
CancelTask: func(id int64) bool { deliveries = append(deliveries, id); return true },
}
if err := service.Apply(context.Background(), nil, humanActor(), 7, tt.action); err != nil {
t.Fatalf("Apply(%s): %v", tt.action, err)
}
if len(store.cancels) != 1 {
t.Fatalf("CancelExecution calls = %+v, want exactly the one cancel path", store.cancels)
}
if call := store.cancels[0]; call.taskID != 7 || call.to != tt.wantTo {
t.Errorf("CancelExecution = %+v, want task 7 to %q", call, tt.wantTo)
}
// The context cancel is the delivery mechanism, not a second
// store write: it rides the store record, never beside it.
if tt.status == taskstate.Running && !slices.Contains(deliveries, int64(7)) {
t.Errorf("deliveries = %v, want the running execution's context cancelled", deliveries)
}
// The action's own attribution event is the operator's record;
// the store wrote the mechanical rows.
if got := store.last(); got.Kind == "" {
t.Error("the operator event was not recorded")
}
})
}
}
// A stop's detail names the delivery, as before: a context cancel that found
// nothing executing says so, and the parked execution is recorded either way.
func TestStopRecordsEvenWhenNothingWasExecuting(t *testing.T) {
store := &cancelRecordingStore{task: &Task{ID: 7, Owner: "acme", Repo: "widgets", Status: taskstate.Running}}
service := Service{
Store: store,
CancelTask: func(int64) bool { return false },
}
if err := service.Apply(context.Background(), nil, humanActor(), 7, taskstate.ActionStop); err != nil {
t.Fatalf("Apply(stop): %v", err)
}
if len(store.cancels) != 1 || store.cancels[0].to != taskstate.Parked {
t.Fatalf("CancelExecution = %+v, want task 7 parked", store.cancels)
}
if got := store.last(); got.Kind != events.KindTaskStopped || got.Detail == "" {
t.Fatalf("stop event = %+v, want the stop kind and the no-execution detail", got)
}
}