Skip to content

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

Go

fix(issues): start the assignee's run when a coordinator promotes its own issue (#10840)

kevind24caa8fdcae
diff --git a/server/internal/handler/handler_test.go b/server/internal/handler/handler_test.go
index 2266c06d65..fabf067f7a 100644
--- a/server/internal/handler/handler_test.go
+++ b/server/internal/handler/handler_test.go
@@ -2889,6 +2889,52 @@ func TestBacklogToTodoByAgentSameAgentDifferentIssue(t *testing.T) {
}
}
+// TestBacklogToTodoByAgentSameIssueOtherAssigneeEnqueues covers #10829: a
+// coordinator whose run is bound to issue I promotes I out of backlog while I
+// is assigned to a different agent. That is not a self-loop, so the assignee
+// must get a run.
+func TestBacklogToTodoByAgentSameIssueOtherAssigneeEnqueues(t *testing.T) {
+ if testHandler == nil {
+ t.Skip("database not available")
+ }
+ ctx := context.Background()
+
+ coordinator := createHandlerTestAgent(t, "Backlog Coordinator", nil)
+ worker := createHandlerTestAgent(t, "Backlog Worker", nil)
+
+ req := newRequest("POST", "/api/issues?workspace_id="+testWorkspaceID, map[string]any{
+ "title": "Coordinator-promoted backlog",
+ "status": "backlog",
+ "assignee_type": "agent",
+ "assignee_id": worker,
+ })
+ w := testutil.Call(t, testHandler.CreateIssue, req).Want(http.StatusCreated)
+ var created IssueResponse
+ json.NewDecoder(w.Body).Decode(&created)
+ t.Cleanup(func() {
+ testPool.Exec(ctx, `DELETE FROM agent_task_queue WHERE issue_id = $1`, created.ID)
+ testPool.Exec(ctx, `DELETE FROM issue WHERE id = $1`, created.ID)
+ })
+
+ // The coordinator's run is bound to the same issue it promotes.
+ coordinatorTask := createHandlerTestTaskForAgentOnIssue(t, coordinator, created.ID)
+
+ req = newRequest("PUT", "/api/issues/"+created.ID, map[string]any{"status": "todo"})
+ req = withURLParam(req, "id", created.ID)
+ req.Header.Set("X-Agent-ID", coordinator)
+ req.Header.Set("X-Task-ID", coordinatorTask)
+ testutil.Call(t, testHandler.UpdateIssue, req).Want(http.StatusOK)
+
+ var workerTasks int
+ dbfx.QueryRow(t,
+ `SELECT count(*) FROM agent_task_queue WHERE issue_id = $1 AND agent_id = $2 AND status = 'queued'`,
+ created.ID, worker,
+ ).Scan(&workerTasks)
+ if workerTasks != 1 {
+ t.Fatalf("expected 1 queued task for the assignee after a coordinator promoted the issue its run is bound to, got %d", workerTasks)
+ }
+}
+
// TestAssignIssueToSelfWithActiveTargetRunDoesNotDuplicate covers the direct
// assignment form of #6947: an agent already working on an issue may claim its
// ownership, but that ownership write must not enqueue a second run for the
diff --git a/server/internal/handler/issue.go b/server/internal/handler/issue.go
index 76d1348850..7f0d1b2b79 100644
--- a/server/internal/handler/issue.go
+++ b/server/internal/handler/issue.go
@@ -4578,11 +4578,16 @@ func (h *Handler) assigneeFallbackAgent(ctx context.Context, issue db.Issue, act
}
// isAgentRunningOnIssue reports whether the calling agent's current task
-// (identified by X-Task-ID) is running for the exact issue being promoted.
+// (identified by X-Task-ID) is running for the exact issue being promoted
+// and belongs to the target receiving agent (assignee or squad leader).
// That is the only true self-loop on backlog→active: the agent flipping
// the same issue its own task is executing for would immediately re-enqueue
// itself, complete the run, flip again, and so on.
//
+// A coordinator whose run is bound to the issue but which promotes it for a
+// different receiving agent is not a loop: the run goes to another agent, and
+// suppressing it would leave the issue in an active status with no run.
+//
// Same-agent cross-issue handoff (Agent A finishing a task on issue I1 then
// promoting issue I2 — even when I2 is also assigned to A) is NOT a loop
// and must fire; that is the documented serial sub-task chain. Member
@@ -4592,7 +4597,7 @@ func (h *Handler) assigneeFallbackAgent(ctx context.Context, issue db.Issue, act
// "agent": resolveActor demotes the actor to "member" otherwise (handler.go
// resolveActor). We still recheck defensively — a future caller could pass
// agent identity through a different path.
-func (h *Handler) isAgentRunningOnIssue(r *http.Request, actorType string, issue db.Issue) bool {
+func (h *Handler) isAgentRunningOnIssue(r *http.Request, actorType string, issue db.Issue, targetAgentID pgtype.UUID) bool {
if actorType != "agent" {
return false
}
@@ -4611,7 +4616,11 @@ func (h *Handler) isAgentRunningOnIssue(r *http.Request, actorType string, issue
if !task.IssueID.Valid {
return false
}
- return uuidToString(task.IssueID) == uuidToString(issue.ID)
+ if !targetAgentID.Valid {
+ return false
+ }
+ return uuidToString(task.IssueID) == uuidToString(issue.ID) &&
+ uuidToString(task.AgentID) == uuidToString(targetAgentID)
}
func (h *Handler) DeleteIssue(w http.ResponseWriter, r *http.Request) {
diff --git a/server/internal/handler/issue_trigger.go b/server/internal/handler/issue_trigger.go
index 840a44c15b..f03d2a5b3d 100644
--- a/server/internal/handler/issue_trigger.go
+++ b/server/internal/handler/issue_trigger.go
@@ -25,8 +25,8 @@ const maxPreviewTriggerIssues = 500
func (h *Handler) issueTriggerWriteProbe(r *http.Request, actorType, actorID string, issue db.Issue) service.IssueTriggerProbe {
return service.IssueTriggerProbe{
CanAccessAgent: nil, // allow-all; gate lives at the write boundary
- IsSelfLoop: func() bool {
- return h.isAgentRunningOnIssue(r, actorType, issue)
+ IsSelfLoop: func(targetAgentID pgtype.UUID) bool {
+ return h.isAgentRunningOnIssue(r, actorType, issue, targetAgentID)
},
SuppressActiveSelfAssignment: func(agentID pgtype.UUID) bool {
suppress := h.shouldSuppressActiveSelfAssignment(r.Context(), actorType, actorID, issue.ID, agentID)
@@ -51,8 +51,8 @@ func (h *Handler) issueTriggerPreviewProbe(r *http.Request, actorType, actorID,
CanAccessAgent: func(agent db.Agent) bool {
return h.canInvokeAgent(r.Context(), agent, actorType, actorID, originatorUserID, workspaceID)
},
- IsSelfLoop: func() bool {
- return h.isAgentRunningOnIssue(r, actorType, issue)
+ IsSelfLoop: func(targetAgentID pgtype.UUID) bool {
+ return h.isAgentRunningOnIssue(r, actorType, issue, targetAgentID)
},
SuppressActiveSelfAssignment: func(agentID pgtype.UUID) bool {
return h.shouldSuppressActiveSelfAssignment(r.Context(), actorType, actorID, issue.ID, agentID)
diff --git a/server/internal/handler/squad_backlog_self_loop_test.go b/server/internal/handler/squad_backlog_self_loop_test.go
new file mode 100644
index 0000000000..a5a53bae64
--- /dev/null
+++ b/server/internal/handler/squad_backlog_self_loop_test.go
@@ -0,0 +1,88 @@
+package handler
+
+import (
+ "net/http"
+ "testing"
+
+ "github.com/multica-ai/multica/server/internal/testutil"
+)
+
+// A promotion must compare the acting task with the resolved squad leader,
+// including when that task is bound to the issue being promoted.
+func TestBacklogToTodoSquadSameIssueMatchesPreview(t *testing.T) {
+ for _, batch := range []bool{false, true} {
+ path := "single"
+ if batch {
+ path = "batch"
+ }
+ for _, tc := range []struct {
+ name string
+ actorLeads bool
+ wantQueued int
+ }{
+ {name: "leader", actorLeads: true, wantQueued: 0},
+ {name: "other_agent", wantQueued: 1},
+ } {
+ t.Run(path+"/"+tc.name, func(t *testing.T) {
+ leaderID := createHandlerTestAgent(t, "Backlog Self-Loop Leader", nil)
+ actorID := leaderID
+ if !tc.actorLeads {
+ actorID = createHandlerTestAgent(t, "Backlog Self-Loop Coordinator", nil)
+ }
+ squadID := dbfx.Squad(t, "Backlog Self-Loop Squad", leaderID)
+ issueID := dbfx.Issue(t, "Squad backlog promotion", testutil.Cols{
+ "status": "backlog",
+ "assignee_type": "squad",
+ "assignee_id": squadID,
+ })
+ dbfx.Cleanup(t, `DELETE FROM agent_task_queue WHERE issue_id = $1`, issueID)
+ actorTaskID := createHandlerTestTaskForAgentOnIssue(t, actorID, issueID)
+
+ previewReq := newRequest("POST", "/api/issues/preview-trigger?workspace_id="+testWorkspaceID, map[string]any{
+ "issue_ids": []string{issueID},
+ "status": "todo",
+ })
+ previewReq.Header.Set("X-Agent-ID", actorID)
+ previewReq.Header.Set("X-Task-ID", actorTaskID)
+ var preview IssueTriggerPreviewResponse
+ testutil.Call(t, testHandler.PreviewIssueTrigger, previewReq).Want(http.StatusOK).JSON(&preview)
+ if preview.TotalCount != tc.wantQueued || len(preview.Triggers) != tc.wantQueued {
+ t.Errorf("expected %d preview triggers, got %+v", tc.wantQueued, preview)
+ }
+ if len(preview.Triggers) == 1 {
+ trigger := preview.Triggers[0]
+ if trigger.IssueID != issueID || trigger.AgentID != leaderID || trigger.Source != "status" {
+ t.Errorf("expected status trigger for squad leader on %s, got %+v", issueID, trigger)
+ }
+ }
+ if got := queuedTaskCountFor(t, issueID, leaderID); got != 0 {
+ t.Fatalf("preview enqueued %d tasks", got)
+ }
+
+ req := withURLParam(newRequest("PUT", "/api/issues/"+issueID, map[string]any{"status": "todo"}), "id", issueID)
+ update := testHandler.UpdateIssue
+ if batch {
+ req = newRequest("PATCH", "/api/issues/batch?workspace_id="+testWorkspaceID, map[string]any{
+ "issue_ids": []string{issueID},
+ "updates": map[string]any{"status": "todo"},
+ })
+ update = testHandler.BatchUpdateIssues
+ }
+ req.Header.Set("X-Agent-ID", actorID)
+ req.Header.Set("X-Task-ID", actorTaskID)
+ testutil.Call(t, update, req).Want(http.StatusOK)
+ if got := queuedTaskCountFor(t, issueID, leaderID); got != tc.wantQueued {
+ t.Errorf("expected %d queued squad leader tasks, got %d", tc.wantQueued, got)
+ }
+ var status string
+ dbfx.QueryRow(t, `SELECT status FROM issue WHERE id = $1`, issueID).Scan(&status)
+ if status != "todo" {
+ t.Errorf("expected promotion to todo, got %q", status)
+ }
+ if got := taskStatus(t, actorTaskID); got != "running" {
+ t.Errorf("acting task must remain running, got %q", got)
+ }
+ })
+ }
+ }
+}
diff --git a/server/internal/service/issue_trigger.go b/server/internal/service/issue_trigger.go
index 048fac1de1..5f41081a52 100644
--- a/server/internal/service/issue_trigger.go
+++ b/server/internal/service/issue_trigger.go
@@ -32,8 +32,9 @@ const (
// see it. A nil func is treated as allow-all.
//
// IsSelfLoop reports whether promoting this issue out of backlog would be the
-// calling agent re-triggering its own running task. Only the status source
-// consults it. A nil func means "not a self-loop".
+// calling agent re-triggering its own running task for the target agent
+// (the assignee or squad leader). Only the status source consults it, after
+// resolving that target. A nil func means "not a self-loop".
//
// SuppressActiveSelfAssignment reports whether a direct agent assignment is a
// trusted agent claiming ownership for itself while the target (issue, agent)
@@ -42,7 +43,7 @@ const (
// target remain runnable. A nil func means "do not suppress".
type IssueTriggerProbe struct {
CanAccessAgent func(agent db.Agent) bool
- IsSelfLoop func() bool
+ IsSelfLoop func(targetAgentID pgtype.UUID) bool
SuppressActiveSelfAssignment func(agentID pgtype.UUID) bool
}
@@ -140,9 +141,6 @@ func (s *IssueService) WillEnqueueRun(ctx context.Context, in IssueTriggerInput,
case in.StatusChanged && prevStatus == "backlog" &&
currentStatus != "backlog" &&
currentStatus != "done" && currentStatus != "cancelled":
- if probe.IsSelfLoop != nil && probe.IsSelfLoop() {
- return IssueRunTrigger{}, false
- }
source = RunSourceStatus
default:
return IssueRunTrigger{}, false
@@ -157,6 +155,9 @@ func (s *IssueService) WillEnqueueRun(ctx context.Context, in IssueTriggerInput,
if !canAccess(agent) {
return IssueRunTrigger{}, false
}
+ if source == RunSourceStatus && probe.IsSelfLoop != nil && probe.IsSelfLoop(issue.AssigneeID) {
+ return IssueRunTrigger{}, false
+ }
if source == RunSourceAssign && !in.IsCreate && !in.EnteredHandoffStep && probe.SuppressActiveSelfAssignment != nil &&
probe.SuppressActiveSelfAssignment(issue.AssigneeID) {
return IssueRunTrigger{}, false
@@ -179,7 +180,7 @@ func (s *IssueService) WillEnqueueRun(ctx context.Context, in IssueTriggerInput,
// (leader briefing, roles, and member routing), so even when the acting
// agent is that squad's leader it is an intentional group handoff rather
// than a redundant direct self-claim. The status path below still uses
- // the leader's pending-task guard.
+ // the leader's self-loop and pending-task guards.
squad, err := s.Queries.GetSquadInWorkspace(ctx, db.GetSquadInWorkspaceParams{
ID: issue.AssigneeID,
WorkspaceID: issue.WorkspaceID,
@@ -198,6 +199,9 @@ func (s *IssueService) WillEnqueueRun(ctx context.Context, in IssueTriggerInput,
if !canAccess(leader) {
return IssueRunTrigger{}, false
}
+ if source == RunSourceStatus && probe.IsSelfLoop != nil && probe.IsSelfLoop(squad.LeaderID) {
+ return IssueRunTrigger{}, false
+ }
if source == RunSourceStatus && s.hasPendingRun(ctx, issue.ID, squad.LeaderID) {
return IssueRunTrigger{}, false
}