Skip to content

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

Go

perf(tasks): dedupe agent-status reconciliation on bulk cancel (#6726)

Danila Katalshove519dd9e8b88
diff --git a/server/internal/service/task.go b/server/internal/service/task.go
index 6bb01a75a6..337632c47b 100644
--- a/server/internal/service/task.go
+++ b/server/internal/service/task.go
@@ -2157,13 +2157,37 @@ func (s *TaskService) CancelTasksForIssue(ctx context.Context, issueID pgtype.UU
}
for _, t := range cancelled {
s.captureTaskCancelled(ctx, t)
- s.ReconcileAgentStatus(ctx, t.AgentID)
s.broadcastTaskEvent(ctx, protocol.EventTaskCancelled, t)
}
+ // Reconcile once per distinct agent instead of once per cancelled row:
+ // cancelling an issue often stops several tasks owned by the same agent,
+ // and each reconcile is a DB write plus a status broadcast. Matches
+ // CancelTasksForAgent's single-reconcile shape (D#3319).
+ for _, agentID := range distinctAgentIDs(cancelled) {
+ s.ReconcileAgentStatus(ctx, agentID)
+ }
s.notifyTasksFinished(cancelled)
return nil
}
+// distinctAgentIDs returns each agent id appearing in the cancelled rows once,
+// preserving first-seen order. Bulk cancellations frequently stop several tasks
+// owned by the same agent; reconciling per distinct agent (rather than per row)
+// collapses the redundant RefreshAgentStatusFromTasks writes and status
+// broadcasts down to one per agent without changing the final agent status.
+func distinctAgentIDs(cancelled []db.AgentTaskQueue) []pgtype.UUID {
+ seen := make(map[pgtype.UUID]struct{}, len(cancelled))
+ ids := make([]pgtype.UUID, 0, len(cancelled))
+ for _, t := range cancelled {
+ if _, dup := seen[t.AgentID]; dup {
+ continue
+ }
+ seen[t.AgentID] = struct{}{}
+ ids = append(ids, t.AgentID)
+ }
+ return ids
+}
+
// CancelTasksForAgent cancels every active task belonging to an agent
// (queued + dispatched + running), reconciles the agent's status, and
// broadcasts task:cancelled events. Used by the agent-level "Cancel all
@@ -2198,9 +2222,14 @@ func (s *TaskService) CancelTasksByTriggerComment(ctx context.Context, commentID
}
for _, t := range cancelled {
s.captureTaskCancelled(ctx, t)
- s.ReconcileAgentStatus(ctx, t.AgentID)
s.broadcastTaskEvent(ctx, protocol.EventTaskCancelled, t)
}
+ // Reconcile once per distinct agent instead of once per cancelled row: an
+ // edited/deleted trigger comment can cancel several tasks owned by the same
+ // agent, and each reconcile is a DB write plus a status broadcast (D#3319).
+ for _, agentID := range distinctAgentIDs(cancelled) {
+ s.ReconcileAgentStatus(ctx, agentID)
+ }
s.notifyTasksFinished(cancelled)
return cancelled, nil
}
diff --git a/server/internal/service/task_cancel_reconcile_dedup_test.go b/server/internal/service/task_cancel_reconcile_dedup_test.go
new file mode 100644
index 0000000000..6170e31bc4
--- /dev/null
+++ b/server/internal/service/task_cancel_reconcile_dedup_test.go
@@ -0,0 +1,67 @@
+package service
+
+import (
+ "testing"
+
+ "github.com/multica-ai/multica/server/internal/util"
+ db "github.com/multica-ai/multica/server/pkg/db/generated"
+)
+
+// distinctAgentIDs drives how many times CancelTasksForIssue and
+// CancelTasksByTriggerComment call ReconcileAgentStatus: one call per returned
+// id. These tests guard that multiple cancelled rows sharing an agent collapse
+// to a single reconcile, while distinct agents each still get one (D#3319).
+
+const (
+ dedupAgentA = "11111111-1111-1111-1111-111111111111"
+ dedupAgentB = "22222222-2222-2222-2222-222222222222"
+ dedupAgentC = "33333333-3333-3333-3333-333333333333"
+)
+
+func cancelledRowsForAgents(agentIDs ...string) []db.AgentTaskQueue {
+ rows := make([]db.AgentTaskQueue, 0, len(agentIDs))
+ for _, id := range agentIDs {
+ rows = append(rows, db.AgentTaskQueue{AgentID: util.MustParseUUID(id)})
+ }
+ return rows
+}
+
+func TestDistinctAgentIDs_SameAgentReconciledOnce(t *testing.T) {
+ // Three cancelled tasks, all owned by the same agent, must reconcile once.
+ rows := cancelledRowsForAgents(dedupAgentA, dedupAgentA, dedupAgentA)
+
+ got := distinctAgentIDs(rows)
+
+ if len(got) != 1 {
+ t.Fatalf("expected 1 distinct agent (single reconcile), got %d", len(got))
+ }
+ if got[0] != util.MustParseUUID(dedupAgentA) {
+ t.Fatalf("expected agent %s, got %v", dedupAgentA, got[0])
+ }
+}
+
+func TestDistinctAgentIDs_MultipleAgentsEachOnce(t *testing.T) {
+ // Interleaved rows across three agents: each distinct agent once, in
+ // first-seen order, regardless of how many rows each contributes.
+ rows := cancelledRowsForAgents(
+ dedupAgentA, dedupAgentB, dedupAgentA, dedupAgentC, dedupAgentB, dedupAgentA,
+ )
+
+ got := distinctAgentIDs(rows)
+
+ want := []string{dedupAgentA, dedupAgentB, dedupAgentC}
+ if len(got) != len(want) {
+ t.Fatalf("expected %d distinct agents, got %d", len(want), len(got))
+ }
+ for i, id := range want {
+ if got[i] != util.MustParseUUID(id) {
+ t.Fatalf("distinct agent[%d]: expected %s, got %v", i, id, got[i])
+ }
+ }
+}
+
+func TestDistinctAgentIDs_Empty(t *testing.T) {
+ if got := distinctAgentIDs(nil); len(got) != 0 {
+ t.Fatalf("expected no agents for empty input, got %d", len(got))
+ }
+}