Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(task): reconcile agent status when cancelling tasks by issue (#1587) (#1648)

jmoney88963c3e3bd33001
diff --git a/server/internal/service/task.go b/server/internal/service/task.go
index f76694bc1c..fe885ff26b 100644
--- a/server/internal/service/task.go
+++ b/server/internal/service/task.go
@@ -140,9 +140,25 @@ func (s *TaskService) EnqueueChatTask(ctx context.Context, chatSession db.ChatSe
return task, nil
}
-// CancelTasksForIssue cancels all active tasks for an issue.
+// CancelTasksForIssue cancels every active task on the issue, reconciles each
+// affected agent's status, and broadcasts task:cancelled events so frontends
+// clear their live cards.
+//
+// Before #1587 this path was "cancel rows and return" — issue-status flips
+// (e.g. user marks the issue `done` or `cancelled` while a task is still
+// running) left the agent stuck at status="working" indefinitely, requiring a
+// manual `multica agent update <id> --status idle` to unwedge. Matches the
+// pattern already used by CancelTask and RerunIssue.
func (s *TaskService) CancelTasksForIssue(ctx context.Context, issueID pgtype.UUID) error {
- return s.Queries.CancelAgentTasksByIssue(ctx, issueID)
+ cancelled, err := s.Queries.CancelAgentTasksByIssue(ctx, issueID)
+ if err != nil {
+ return err
+ }
+ for _, t := range cancelled {
+ s.ReconcileAgentStatus(ctx, t.AgentID)
+ s.broadcastTaskEvent(ctx, protocol.EventTaskCancelled, t)
+ }
+ return nil
}
// CancelTask cancels a single task by ID. It broadcasts a task:cancelled event
diff --git a/server/pkg/db/generated/agent.sql.go b/server/pkg/db/generated/agent.sql.go
index ad85dc9c0c..9a83ea6543 100644
--- a/server/pkg/db/generated/agent.sql.go
+++ b/server/pkg/db/generated/agent.sql.go
@@ -100,15 +100,60 @@ func (q *Queries) CancelAgentTasksByAgent(ctx context.Context, agentID pgtype.UU
return err
}
-const cancelAgentTasksByIssue = `-- name: CancelAgentTasksByIssue :exec
+const cancelAgentTasksByIssue = `-- name: CancelAgentTasksByIssue :many
UPDATE agent_task_queue
-SET status = 'cancelled'
+SET status = 'cancelled', completed_at = now()
WHERE issue_id = $1 AND status IN ('queued', 'dispatched', 'running')
+RETURNING id, agent_id, issue_id, status, priority, dispatched_at, started_at, completed_at, result, error, created_at, context, runtime_id, session_id, work_dir, trigger_comment_id, chat_session_id, autopilot_run_id, attempt, max_attempts, parent_task_id, failure_reason, last_heartbeat_at
`
-func (q *Queries) CancelAgentTasksByIssue(ctx context.Context, issueID pgtype.UUID) error {
- _, err := q.db.Exec(ctx, cancelAgentTasksByIssue, issueID)
- return err
+// Cancels every active task on the issue and returns the affected rows so the
+// caller can reconcile each agent's status and broadcast task:cancelled
+// events (#1587). Prior :exec form silently dropped that info, so internal
+// cancel paths (issue status flips to cancelled/done, etc.) left agents stuck
+// at status="working" with no self-correction.
+func (q *Queries) CancelAgentTasksByIssue(ctx context.Context, issueID pgtype.UUID) ([]AgentTaskQueue, error) {
+ rows, err := q.db.Query(ctx, cancelAgentTasksByIssue, issueID)
+ if err != nil {
+ return nil, err
+ }
+ defer rows.Close()
+ items := []AgentTaskQueue{}
+ for rows.Next() {
+ var i AgentTaskQueue
+ if err := rows.Scan(
+ &i.ID,
+ &i.AgentID,
+ &i.IssueID,
+ &i.Status,
+ &i.Priority,
+ &i.DispatchedAt,
+ &i.StartedAt,
+ &i.CompletedAt,
+ &i.Result,
+ &i.Error,
+ &i.CreatedAt,
+ &i.Context,
+ &i.RuntimeID,
+ &i.SessionID,
+ &i.WorkDir,
+ &i.TriggerCommentID,
+ &i.ChatSessionID,
+ &i.AutopilotRunID,
+ &i.Attempt,
+ &i.MaxAttempts,
+ &i.ParentTaskID,
+ &i.FailureReason,
+ &i.LastHeartbeatAt,
+ ); err != nil {
+ return nil, err
+ }
+ items = append(items, i)
+ }
+ if err := rows.Err(); err != nil {
+ return nil, err
+ }
+ return items, nil
}
const cancelAgentTasksByIssueAndAgent = `-- name: CancelAgentTasksByIssueAndAgent :many
diff --git a/server/pkg/db/queries/agent.sql b/server/pkg/db/queries/agent.sql
index 9e98003b0f..c4c12ad462 100644
--- a/server/pkg/db/queries/agent.sql
+++ b/server/pkg/db/queries/agent.sql
@@ -89,10 +89,16 @@ FROM agent_task_queue p
WHERE p.id = $1
RETURNING *;
--- name: CancelAgentTasksByIssue :exec
+-- name: CancelAgentTasksByIssue :many
+-- Cancels every active task on the issue and returns the affected rows so the
+-- caller can reconcile each agent's status and broadcast task:cancelled events
+-- (#1587). Prior :exec form silently dropped that info, so internal cancel
+-- paths (issue status flips to cancelled/done, etc.) left agents stuck at
+-- status="working" with no self-correction.
UPDATE agent_task_queue
-SET status = 'cancelled'
-WHERE issue_id = $1 AND status IN ('queued', 'dispatched', 'running');
+SET status = 'cancelled', completed_at = now()
+WHERE issue_id = $1 AND status IN ('queued', 'dispatched', 'running')
+RETURNING *;
-- name: CancelAgentTasksByIssueAndAgent :many
-- Cancels active tasks for a single (issue, agent) pair without touching