fix(gateway): chat /cancel declines through the task-action service so the run stops

This commit is contained in:
sam committed 2026-10-27 10:00:54 +11:00
1 parent 0564dfe54a
commit dc1085fb66
4 files changed
+65 -76

No files matched your search

+17 -34
View File
@@ -13,9 +13,8 @@ import (
)
type chatTaskControllerAdapter struct {
taskByID func(context.Context, int64) (*workflow.Task, error)
approve func(context.Context, *string, taskactions.Actor, int64, taskactions.ActionPayload) error
cancelExecution func(context.Context, int64, string, string) ([]int64, error)
taskByID func(context.Context, int64) (*workflow.Task, error)
act func(context.Context, *string, taskactions.Actor, int64, taskstate.Action, taskactions.ActionPayload) error
}
func (a chatTaskControllerAdapter) ChatTaskStatus(ctx context.Context, taskID int64) (gateway.ChatTaskStatus, bool, error) {
@@ -33,44 +32,28 @@ func (a chatTaskControllerAdapter) ChatTaskStatus(ctx context.Context, taskID in
}
func (a chatTaskControllerAdapter) ApproveChatTask(ctx context.Context, taskID int64, actor taskactions.Actor) error {
if a.approve == nil {
return fmt.Errorf("task approval is unavailable")
}
// /approve carries no selection syntax, so the review gate answer posts
// every offered finding.
return a.apply(ctx, taskID, actor, taskstate.ActionApprove)
}
// CancelChatTask declines the task through the task-action service, whose
// daemon end also stops the run in flight. A store-only write leaves the agent
// running.
func (a chatTaskControllerAdapter) CancelChatTask(ctx context.Context, taskID int64, actor taskactions.Actor) error {
return a.apply(ctx, taskID, actor, taskstate.ActionReject)
}
func (a chatTaskControllerAdapter) apply(ctx context.Context, taskID int64, actor taskactions.Actor, action taskstate.Action) error {
if a.act == nil {
return fmt.Errorf("task %s is unavailable", action)
}
var scope *string
if !taskactions.ActsForOperator(string(actor.Identity)) {
id := string(actor.Identity)
scope = &id
}
return a.approve(ctx, scope, actor, taskID, taskactions.ActionPayload{})
}
func (a chatTaskControllerAdapter) CancelChatTask(ctx context.Context, taskID int64, reason string) error {
task, err := a.taskByID(ctx, taskID)
if err != nil {
return err
}
if task == nil {
return fmt.Errorf("task %d not found", taskID)
}
if task.IsForgeBacked() {
return fmt.Errorf("task %d is not chat-originated", taskID)
}
// An operator declining work lands in the same state whichever surface
// they used, through the one cancel path: the store records the
// cancellation -- every non-terminal step of the current attempt plus the
// execution's own move -- and
// this write is what a late worker's next step write fails against. 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.
if a.cancelExecution == nil {
return fmt.Errorf("execution cancellation is unavailable")
}
_, err = a.cancelExecution(ctx, taskID, reason, workflow.StatusClosedWontDo)
return err
return a.act(ctx, scope, actor, taskID, action, taskactions.ActionPayload{})
}
// chatTaskListerAdapter gives the gateway a read view of an identity's tasks.
+39 -1
View File
@@ -10,6 +10,7 @@ import (
"github.com/samcharles93/archie-core/internal/domain/workflow"
"github.com/samcharles93/archie-core/internal/gateway"
"github.com/samcharles93/archie-core/internal/logging"
"github.com/samcharles93/archie-core/internal/taskstate"
)
type emptyTaskLogs struct{ storecontract.TaskLogStore }
@@ -116,7 +117,7 @@ func TestChatTaskScope(t *testing.T) {
}
var scope *string
controller := chatTaskControllerAdapter{approve: func(_ context.Context, s *string, _ taskactions.Actor, _ int64, _ taskactions.ActionPayload) error {
controller := chatTaskControllerAdapter{act: func(_ context.Context, s *string, _ taskactions.Actor, _ int64, _ taskstate.Action, _ taskactions.ActionPayload) error {
scope = s
return nil
}}
@@ -135,3 +136,40 @@ func TestChatTaskScope(t *testing.T) {
})
}
}
// A chat cancel must reach the task-action service, whose daemon end holds the
// running task's context; a store-only cancel leaves the agent running.
func TestCancelChatTaskGoesThroughTaskActions(t *testing.T) {
tests := []struct {
name string
chat string
want *string
}{
{name: "scoped identity", chat: "alice", want: new("alice")},
{name: "operator is unscoped", chat: string(identity.OperatorID())},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var calls int
var scope *string
var action taskstate.Action
controller := chatTaskControllerAdapter{
act: func(_ context.Context, s *string, _ taskactions.Actor, _ int64, a taskstate.Action, _ taskactions.ActionPayload) error {
calls++
scope = s
action = a
return nil
},
}
if err := controller.CancelChatTask(t.Context(), 7, taskactions.ActorFromScope(tt.chat)); err != nil {
t.Fatalf("cancel: %v", err)
}
if calls != 1 || action != taskstate.ActionReject {
t.Fatalf("task-action calls = %d, action = %q; want 1, reject", calls, action)
}
if (scope == nil) != (tt.want == nil) || (scope != nil && *scope != *tt.want) {
t.Fatalf("scope = %v, want %v", scope, tt.want)
}
})
}
}
@@ -73,14 +73,13 @@ func (b *server) setupChatRuntime(ctx context.Context, cfg config.Config, actor
b.setupChatTasks(cfg)
b.chatController = gateway.NewStoreTaskController(chatTaskControllerAdapter{
taskByID: b.stateStore.TaskByID,
// Chat's /approve reaches the daemon's one task-action service rather
// than its own requeue, so a chat approval and a dashboard approval
// cannot record different decisions for one operator intent
approve: func(ctx context.Context, scope *string, by taskactions.Actor, taskID int64, res taskactions.ActionPayload) error {
_, err := actor.ApplyChatTaskAction(ctx, scope, by, taskID, taskstate.ActionApprove, res)
// Chat's /approve and /cancel reach the daemon's one task-action
// service rather than writing the store, so a chat decision and a
// dashboard decision cannot differ, and a cancel stops the run.
act: func(ctx context.Context, scope *string, by taskactions.Actor, taskID int64, action taskstate.Action, res taskactions.ActionPayload) error {
_, err := actor.ApplyChatTaskAction(ctx, scope, by, taskID, action, res)
return err
},
cancelExecution: b.stateStore.CancelExecution,
})
b.updateService = makeUpdateService(b.controlPlane, cfg)
return nil
+4 -35
View File
@@ -111,20 +111,9 @@ type chatTaskController interface {
// ApproveChatTask approves a waiting_approval task through the task-action
// service, posting every offered finding.
ApproveChatTask(ctx context.Context, taskID int64, actor taskactions.Actor) error
// CancelChatTask transitions an active task to a rejected/terminal
// state. Callers must have already validated the current status is
// cancellable.
CancelChatTask(ctx context.Context, taskID int64, reason string) error
}
// TaskRuntime reaches tasks that are currently executing. The daemon
// implements it; the store cannot, because a row records what a task is
// meant to be doing, not the goroutine doing it. It is only the delivery
// mechanism: the store's CancelExecution record precedes it.
type TaskRuntime interface {
// CancelTask interrupts one running task, reporting whether it was
// running.
CancelTask(taskID int64) bool
// CancelChatTask declines an active task and interrupts its run through
// the task-action service.
CancelChatTask(ctx context.Context, taskID int64, actor taskactions.Actor) error
}
// StoreTaskController implements TaskController backed by a store
@@ -132,10 +121,6 @@ type TaskRuntime interface {
// match the task identity, including the empty identity (single-identity deployments).
type StoreTaskController struct {
store chatTaskController
// runtime interrupts running work. Nil leaves running tasks
// uninterruptible, which is the pre-existing behaviour and is
// reported as such rather than silently succeeding.
runtime TaskRuntime
}
// NewStoreTaskController returns a TaskController backed by c.
@@ -143,12 +128,6 @@ func NewStoreTaskController(c chatTaskController) *StoreTaskController {
return &StoreTaskController{store: c}
}
// WithRuntime returns a controller that can also interrupt running tasks.
func (c *StoreTaskController) WithRuntime(rt TaskRuntime) *StoreTaskController {
c.runtime = rt
return c
}
func (c *StoreTaskController) Approve(ctx context.Context, taskID int64, identity string) error {
st, err := c.authorize(ctx, taskID, identity)
if err != nil {
@@ -168,17 +147,7 @@ func (c *StoreTaskController) Cancel(ctx context.Context, taskID int64, identity
if err := taskstate.CheckDecline(st.Status); err != nil {
return err
}
// The store records the cancellation first -- the one cancel path -- and
// the context cancel delivers it. A task the store says is running but
// nothing is executing for (a crashed or migrated daemon) is unstuck by the
// record alone, which is why the delivery is reported, not required.
if err := c.store.CancelChatTask(ctx, taskID, "declined by "+identity); err != nil {
return err
}
if st.Status == taskstate.Running && c.runtime != nil {
c.runtime.CancelTask(taskID)
}
return nil
return c.store.CancelChatTask(ctx, taskID, taskactions.ActorFromScope(identity))
}
func (c *StoreTaskController) authorize(ctx context.Context, taskID int64, identity string) (ChatTaskStatus, error) {