Files
archie/internal/daemon/running_test.go
T
sam 5f26b1e9dd 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

71 lines
1.7 KiB
Go

package daemon
import (
"context"
"errors"
"testing"
"time"
)
// TestRunningTaskIsInterruptible is the guarantee /stop rests on for
// agents. Cancelling a task must reach the work in flight, not merely
// record an intention in the store.
func TestRunningTaskIsInterruptible(t *testing.T) {
t.Parallel()
var r runningTasks
started := make(chan struct{})
observed := make(chan error, 1)
go func() {
taskCtx, finished := r.begin(context.Background(), 7, "archie")
defer finished()
close(started)
<-taskCtx.Done()
observed <- taskCtx.Err()
}()
<-started
if !r.stop(7) {
t.Fatal("stop reported that task 7 was not running")
}
select {
case err := <-observed:
if !errors.Is(err, context.Canceled) {
t.Errorf("task context error = %v, want context.Canceled", err)
}
case <-time.After(2 * time.Second):
t.Fatal("the task never observed cancellation")
}
}
// TestStopUnknownTaskIsReported keeps a stale store row distinguishable
// from a live task: the caller has to be able to tell the difference to
// decide whether recording a terminal state is safe.
func TestStopUnknownTaskIsReported(t *testing.T) {
t.Parallel()
var r runningTasks
if r.stop(99) {
t.Error("stop claimed to cancel a task that was never running")
}
}
// TestFinishedTaskIsNotCancellable guards the retirement in begin's
// cleanup. A stale entry would let a later /stop report success against a
// task that had already completed.
func TestFinishedTaskIsNotCancellable(t *testing.T) {
t.Parallel()
var r runningTasks
_, finished := r.begin(context.Background(), 3, "archie")
finished()
if r.stop(3) {
t.Error("a completed task was reported as stopped")
}
if got := r.list(); len(got) != 0 {
t.Errorf("list() = %v, want empty after the task finished", got)
}
}