Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(server): prevent agent-to-agent mention inheritance loops (BRI-34) (#1765)
Bright Zheng81231e06f836
diff --git a/server/internal/daemon/daemon_test.go b/server/internal/daemon/daemon_test.goindex 2495fdd853..33c26ce092 100644--- a/server/internal/daemon/daemon_test.go+++ b/server/internal/daemon/daemon_test.go@@ -191,7 +191,7 @@ func TestBuildPromptCommentTriggeredByAgent(t *testing.T) {for _, want := range []string{"Another agent (Atlas)","do not @mention the other agent as a sign-off",- "silence is the preferred way",+ "Silence is the preferred way",} {if !strings.Contains(prompt, want) {t.Fatalf("prompt missing %q\n---\n%s", want, prompt)diff --git a/server/internal/daemon/execenv/runtime_config.go b/server/internal/daemon/execenv/runtime_config.goindex 62ecc259c2..97a2524431 100644--- a/server/internal/daemon/execenv/runtime_config.go+++ b/server/internal/daemon/execenv/runtime_config.go@@ -168,7 +168,7 @@ func buildMetaSkillContent(provider string, ctx TaskContextForEnv) string {fmt.Fprintf(&b, "2. Run `multica issue comment list %s --output json` to read the conversation\n", ctx.IssueID)b.WriteString(" - If the output is very large or truncated, use pagination: `--limit 30` to get the latest 30 comments, or `--since <timestamp>` to fetch only recent ones\n")fmt.Fprintf(&b, "3. Find the triggering comment (ID: `%s`) and understand what is being asked — do NOT confuse it with previous comments\n", ctx.TriggerCommentID)- b.WriteString("4. **Decide whether a reply is warranted.** If the triggering comment is an acknowledgment / thanks / sign-off from another agent and no concrete question or task is being asked of you, do NOT post a reply — just exit. Silence is a valid and preferred way to end agent-to-agent conversations.\n")+ b.WriteString("4. **Decide whether a reply is warranted.** If the triggering comment is an acknowledgment / thanks / sign-off from another agent and no concrete question or task is being asked of you, do NOT post a reply — and do NOT post a comment saying 'No reply needed' or similar. Simply exit with no output. Silence is a valid and preferred way to end agent-to-agent conversations.\n")b.WriteString("5. If a reply IS warranted: do any requested work first, then **decide whether to include any `@mention` link.** The default is NO mention. Only mention when you are escalating to a human owner who is not yet involved, delegating a concrete new sub-task to another agent for the first time, or the user explicitly asked you to loop someone in. Never @mention the agent you are replying to as a thank-you or sign-off.\n")b.WriteString("6. **If you reply, post it as a comment — this step is mandatory when you reply.** Text in your terminal or run logs is NOT delivered to the user. ")b.WriteString(BuildCommentReplyInstructions(ctx.IssueID, ctx.TriggerCommentID))diff --git a/server/internal/daemon/prompt.go b/server/internal/daemon/prompt.goindex e56a9db37b..23447b2b45 100644--- a/server/internal/daemon/prompt.go+++ b/server/internal/daemon/prompt.go@@ -49,7 +49,7 @@ func buildCommentPrompt(task Task) string {fmt.Fprintf(&b, "[NEW COMMENT] %s just left a new comment. Focus on THIS comment — do not confuse it with previous ones:\n\n", authorLabel)fmt.Fprintf(&b, "> %s\n\n", task.TriggerCommentContent)if task.TriggerAuthorType == "agent" {- b.WriteString("⚠️ The triggering comment was posted by another agent. Before replying, decide whether a reply is warranted at all. If that comment was an acknowledgment, thanks, or sign-off and no concrete question or task is being asked of you, do NOT reply — silence is the preferred way to end agent-to-agent threads. If you do reply, do not @mention the other agent as a sign-off (that re-triggers them and starts a loop).\n\n")+ b.WriteString("⚠️ The triggering comment was posted by another agent. Before replying, decide whether a reply is warranted at all. If that comment was an acknowledgment, thanks, or sign-off and no concrete question or task is being asked of you, do NOT reply — and do NOT post a comment saying 'No reply needed' or similar. Simply exit with no output. Silence is the preferred way to end agent-to-agent threads. If you do reply, do not @mention the other agent as a sign-off (that re-triggers them and starts a loop).\n\n")}}fmt.Fprintf(&b, "Start by running `multica issue get %s --output json` to understand your task, then decide how to proceed.\n\n", task.IssueID)diff --git a/server/internal/handler/comment.go b/server/internal/handler/comment.goindex 22e57000b7..5eac1a4365 100644--- a/server/internal/handler/comment.go+++ b/server/internal/handler/comment.go@@ -424,7 +424,12 @@ func (h *Handler) enqueueMentionedAgentTasks(ctx context.Context, issue db.Issue// but only when the reply contains no mentions at all (a plain follow-up).// If the reply explicitly @mentions anyone (agents or members), the user// is making a deliberate choice about who to involve; don't auto-inherit.- if parentComment != nil && len(mentions) == 0 {+ //+ // CRITICAL: agent-authored replies must NOT inherit parent mentions.+ // Otherwise an agent posting "No reply needed" in a thread whose root+ // mentioned another agent would re-trigger that agent, creating a loop.+ // Explicit @mentions in the agent's own comment still work for delegation.+ if parentComment != nil && len(mentions) == 0 && authorType != "agent" {mentions = util.ParseMentions(parentComment.Content)}for _, m := range mentions {diff --git a/server/internal/handler/handler_test.go b/server/internal/handler/handler_test.goindex 57d37f75b2..60f114418e 100644--- a/server/internal/handler/handler_test.go+++ b/server/internal/handler/handler_test.go@@ -1998,3 +1998,120 @@ func TestDaemonRegisterMissingWorkspaceReturns404(t *testing.T) {t.Fatalf("DaemonRegister: expected workspace not found error, got %s", w.Body.String())}}++// TestAgentReplyDoesNotInheritParentMentions verifies that agent-authored+// replies do NOT inherit parent-comment mentions, preventing agent-to-agent+// re-trigger loops (e.g. "No reply needed" chains). Member-authored replies+// still inherit parent mentions as expected.+func TestAgentReplyDoesNotInheritParentMentions(t *testing.T) {+ if testHandler == nil {+ t.Skip("database not available")+ }+ ctx := context.Background()++ // Create two agents.+ agentA := createHandlerTestAgent(t, "Loop Agent A", nil)+ agentB := createHandlerTestAgent(t, "Loop Agent B", nil)++ // Create an unassigned issue so on_comment doesn't fire.+ w := httptest.NewRecorder()+ req := newRequest("POST", "/api/issues?workspace_id="+testWorkspaceID, map[string]any{+ "title": "Agent mention inheritance test",+ "status": "todo",+ })+ testHandler.CreateIssue(w, req)+ if w.Code != http.StatusCreated {+ t.Fatalf("CreateIssue: expected 201, got %d: %s", w.Code, w.Body.String())+ }+ var issue IssueResponse+ json.NewDecoder(w.Body).Decode(&issue)+ issueID := issue.ID++ t.Cleanup(func() {+ testPool.Exec(ctx, `DELETE FROM agent_task_queue WHERE issue_id = $1`, issueID)+ testPool.Exec(ctx, `DELETE FROM comment WHERE issue_id = $1`, issueID)+ testPool.Exec(ctx, `DELETE FROM issue WHERE id = $1`, issueID)+ })++ // Helper: count queued tasks for a given agent on this issue.+ countTasks := func(agentID string) int {+ var n int+ err := testPool.QueryRow(ctx,+ `SELECT count(*) FROM agent_task_queue WHERE issue_id = $1 AND agent_id = $2 AND status = 'queued'`,+ issueID, agentID,+ ).Scan(&n)+ if err != nil {+ t.Fatalf("failed to count tasks: %v", err)+ }+ return n+ }++ // Helper: cancel all tasks for an agent on this issue.+ cancelTasks := func(agentID string) {+ _, err := testPool.Exec(ctx,+ `UPDATE agent_task_queue SET status = 'cancelled' WHERE issue_id = $1 AND agent_id = $2`,+ issueID, agentID,+ )+ if err != nil {+ t.Fatalf("failed to cancel tasks: %v", err)+ }+ }++ postComment := func(issueID string, body map[string]any, headers map[string]string) *httptest.ResponseRecorder {+ w := httptest.NewRecorder()+ r := newRequest("POST", "/api/issues/"+issueID+"/comments", body)+ r = withURLParam(r, "id", issueID)+ for k, v := range headers {+ r.Header.Set(k, v)+ }+ testHandler.CreateComment(w, r)+ return w+ }++ // 1. Member posts top-level comment mentioning Agent B.+ mentionB := fmt.Sprintf("[@Agent B](mention://agent/%s) please review", agentB)+ w = postComment(issueID, map[string]any{"content": mentionB}, nil)+ if w.Code != http.StatusCreated {+ t.Fatalf("member mention comment: expected 201, got %d: %s", w.Code, w.Body.String())+ }+ var parentComment CommentResponse+ json.NewDecoder(w.Body).Decode(&parentComment)+ if countTasks(agentB) != 1 {+ t.Fatalf("expected 1 task for Agent B after member mention, got %d", countTasks(agentB))+ }++ // 2. Cancel Agent B's task so it's free to be re-triggered.+ cancelTasks(agentB)+ if countTasks(agentB) != 0 {+ t.Fatalf("expected 0 tasks for Agent B after cancel, got %d", countTasks(agentB))+ }++ // 3. Agent A posts a reply in the same thread with NO mentions.+ // With the fix, this must NOT inherit the parent mention of Agent B.+ w = postComment(issueID, map[string]any{+ "content": "No reply needed — just an acknowledgment.",+ "parent_id": parentComment.ID,+ }, map[string]string{"X-Agent-ID": agentA})+ if w.Code != http.StatusCreated {+ t.Fatalf("agent A reply: expected 201, got %d: %s", w.Code, w.Body.String())+ }+ if countTasks(agentB) != 0 {+ t.Fatalf("expected 0 tasks for Agent B after agent reply (no parent inheritance), got %d", countTasks(agentB))+ }++ // 4. Cancel any stray tasks.+ cancelTasks(agentB)++ // 5. Member posts a reply in the same thread with NO mentions.+ // This SHOULD inherit the parent mention and re-trigger Agent B.+ w = postComment(issueID, map[string]any{+ "content": "Thanks for the review.",+ "parent_id": parentComment.ID,+ }, nil)+ if w.Code != http.StatusCreated {+ t.Fatalf("member reply: expected 201, got %d: %s", w.Code, w.Body.String())+ }+ if countTasks(agentB) != 1 {+ t.Fatalf("expected 1 task for Agent B after member reply (parent inheritance allowed), got %d", countTasks(agentB))+ }+}