Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
feat(daemon): harden agent mention-loop instructions (#1581)
Bohan Jiange0e91fc792c7
diff --git a/server/internal/daemon/daemon_test.go b/server/internal/daemon/daemon_test.goindex 8e733f915f..6069a41913 100644--- a/server/internal/daemon/daemon_test.go+++ b/server/internal/daemon/daemon_test.go@@ -119,10 +119,15 @@ func TestBuildPromptCommentTriggered(t *testing.T) {for _, want := range []string{issueID,commentContent,- "comment that triggered this task",+ "Focus on THIS comment",commentID,"multica issue comment add " + issueID + " --parent " + commentID,"do NOT reuse --parent values from previous turns",+ // Silence-as-valid-exit for agent-to-agent loops depends on the+ // reply command being framed conditionally rather than as a hard+ // requirement. Guard the phrasing so the conflict with the new+ // workflow (MUL-1323) doesn't come back.+ "If you decide to reply",} {if !strings.Contains(prompt, want) {t.Fatalf("prompt missing %q\n---\n%s", want, prompt)@@ -135,6 +140,67 @@ func TestBuildPromptCommentTriggered(t *testing.T) {}}+// TestBuildPromptCommentTriggeredByAgent covers the agent-to-agent mention+// loop signal injected into the per-turn prompt (MUL-1323 / GH#1576). When+// the triggering comment was posted by another agent, the prompt must name+// the author, warn against sign-off @mentions, and point at silence as a+// valid exit.+func TestBuildPromptCommentTriggeredByAgent(t *testing.T) {+ t.Parallel()++ prompt := BuildPrompt(Task{+ IssueID: "issue-1",+ TriggerCommentID: "comment-1",+ TriggerCommentContent: "thanks, looks good!",+ TriggerAuthorType: "agent",+ TriggerAuthorName: "Atlas",+ Agent: &AgentData{Name: "Test"},+ })++ for _, want := range []string{+ "Another agent (Atlas)",+ "do not @mention the other agent as a sign-off",+ "silence is the preferred way",+ } {+ if !strings.Contains(prompt, want) {+ t.Fatalf("prompt missing %q\n---\n%s", want, prompt)+ }+ }+}++// TestBuildPromptCommentTriggeredByMember guards against the agent-loop warning+// leaking into human-authored triggers — a human asking a question should not+// be pre-discouraged from getting a reply.+func TestBuildPromptCommentTriggeredByMember(t *testing.T) {+ t.Parallel()++ prompt := BuildPrompt(Task{+ IssueID: "issue-1",+ TriggerCommentID: "comment-1",+ TriggerCommentContent: "can you translate this?",+ TriggerAuthorType: "member",+ TriggerAuthorName: "Alice",+ Agent: &AgentData{Name: "Test"},+ })++ if !strings.Contains(prompt, "A user just left a new comment") {+ t.Fatalf("member-triggered prompt should label the author as a user\n---\n%s", prompt)+ }+ if strings.Contains(prompt, "Another agent") {+ t.Fatalf("member-triggered prompt should not claim the author was another agent")+ }+ // Must NOT use the old "You MUST respond" language — that conflicts with+ // the agent-to-agent silence-as-valid-exit workflow. Even on human-authored+ // triggers, the reply command is framed conditionally for a single+ // consistent rule across turn types.+ if strings.Contains(prompt, "MUST respond") {+ t.Fatalf("prompt should not contain unconditional \"MUST respond\" language\n---\n%s", prompt)+ }+ if !strings.Contains(prompt, "If you decide to reply") {+ t.Fatalf("prompt should frame the reply command conditionally\n---\n%s", prompt)+ }+}+func TestBuildPromptCommentTriggeredNoContent(t *testing.T) {t.Parallel()diff --git a/server/internal/daemon/execenv/execenv_test.go b/server/internal/daemon/execenv/execenv_test.goindex 48e92e516f..983751eee6 100644--- a/server/internal/daemon/execenv/execenv_test.go+++ b/server/internal/daemon/execenv/execenv_test.go@@ -1353,3 +1353,76 @@ func TestReadGCMeta_NoFile(t *testing.T) {t.Fatal("expected error for missing file")}}++// TestInjectRuntimeConfigMentionLoopHardening locks in the mention-loop+// instructions (see MUL-1323 / GH#1576). Two agents were stuck in an infinite+// @mention loop because the harness told them mentions were "actions" but did+// not tell them (a) when NOT to mention, (b) that silence ends a thread, or+// (c) that the triggering comment was from another agent. If any of the+// signals below regress, agent-to-agent loops come back.+func TestInjectRuntimeConfigMentionLoopHardening(t *testing.T) {+ t.Parallel()++ commentTriggerCtx := TaskContextForEnv{+ IssueID: "issue-1",+ TriggerCommentID: "comment-1",+ }+ assignmentCtx := TaskContextForEnv{IssueID: "issue-1"}++ readClaudeMD := func(t *testing.T, ctx TaskContextForEnv) string {+ t.Helper()+ dir := t.TempDir()+ if err := InjectRuntimeConfig(dir, "claude", ctx); err != nil {+ t.Fatalf("InjectRuntimeConfig failed: %v", err)+ }+ data, err := os.ReadFile(filepath.Join(dir, "CLAUDE.md"))+ if err != nil {+ t.Fatalf("read CLAUDE.md: %v", err)+ }+ return string(data)+ }++ t.Run("mentions-section-lists-loop-protocol", func(t *testing.T) {+ t.Parallel()+ s := readClaudeMD(t, assignmentCtx)+ for _, want := range []string{+ "side-effecting actions",+ "enqueues a new run for that agent",+ "When NOT to use a mention link",+ "When a mention IS appropriate",+ "end with no mention at all",+ "Silence ends conversations",+ } {+ if !strings.Contains(s, want) {+ t.Errorf("Mentions section missing %q\n---\n%s", want, s)+ }+ }+ })++ t.Run("closing-line-no-longer-says-always-mention", func(t *testing.T) {+ t.Parallel()+ s := readClaudeMD(t, assignmentCtx)+ // The old footer said "**always** use the mention format" which models+ // over-generalized to agent/member mentions. Guard against regression.+ if strings.Contains(s, "**always** use the mention format") {+ t.Errorf("CLAUDE.md still contains the overreaching \"**always** use the mention format\" guidance")+ }+ })++ t.Run("workflow-carries-silence-as-exit-and-no-signoff-mention", func(t *testing.T) {+ t.Parallel()+ s := readClaudeMD(t, commentTriggerCtx)+ // The anti-loop signal for CLAUDE.md lives in the numbered workflow+ // steps (4 + 5), not in a dedicated preamble. Lock in the key phrases+ // so the signal can't decay back into pure prose again.+ for _, want := range []string{+ "Decide whether a reply is warranted",+ "Silence is a valid and preferred way",+ "Never @mention the agent you are replying to as a thank-you or sign-off",+ } {+ if !strings.Contains(s, want) {+ t.Errorf("comment-triggered CLAUDE.md missing %q", want)+ }+ }+ })+}diff --git a/server/internal/daemon/execenv/reply_instructions.go b/server/internal/daemon/execenv/reply_instructions.goindex a541ed899c..e676cb060c 100644--- a/server/internal/daemon/execenv/reply_instructions.go+++ b/server/internal/daemon/execenv/reply_instructions.go@@ -16,7 +16,7 @@ func BuildCommentReplyInstructions(issueID, triggerCommentID string) string {return ""}return fmt.Sprintf(- "Reply by running exactly this command — always use the trigger comment ID below, "++ "If you decide to reply, post it by running exactly this command — always use the trigger comment ID below, "+"do NOT reuse --parent values from previous turns in this session:\n\n"+" multica issue comment add %s --parent %s --content \"...\"\n",issueID, triggerCommentID,diff --git a/server/internal/daemon/execenv/runtime_config.go b/server/internal/daemon/execenv/runtime_config.goindex 0b7aadfbfa..d5b4208c78 100644--- a/server/internal/daemon/execenv/runtime_config.go+++ b/server/internal/daemon/execenv/runtime_config.go@@ -131,10 +131,11 @@ 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. If the comment requests code changes or further work, do the work first\n")- b.WriteString("5. **Post your reply as a comment — this step is mandatory.** Text in your terminal or run logs is NOT delivered to the user. ")+ 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("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))- b.WriteString("6. Do NOT change the issue status unless the comment explicitly asks for it\n\n")+ b.WriteString("7. Do NOT change the issue status unless the comment explicitly asks for it\n\n")} else {// Assignment-triggered: defer to agent Skills for workflow specifics.b.WriteString("You are responsible for managing the issue status throughout your work.\n\n")@@ -170,12 +171,19 @@ func buildMetaSkillContent(provider string, ctx TaskContextForEnv) string {}b.WriteString("## Mentions\n\n")- b.WriteString("When referencing issues or people in comments, use the mention format so they render as interactive links:\n\n")- b.WriteString("- **Issue**: `[MUL-123](mention://issue/<issue-id>)` — renders as a clickable link to the issue\n")- b.WriteString("- **Member**: `[@Name](mention://member/<user-id>)` — renders as a styled mention and sends a notification\n")- b.WriteString("- **Agent**: `[@Name](mention://agent/<agent-id>)` — renders as a styled mention and re-triggers the agent\n\n")- b.WriteString("⚠️ Agent and member mentions are **actions**, not text references: agent mentions enqueue a new task for the agent, and member mentions send a notification. ")- b.WriteString("If you only want to refer to someone by name in prose (e.g. \"GPT-Boy is correct\"), write the plain name without the mention link.\n\n")+ b.WriteString("Mention links are **side-effecting actions**, not just formatting:\n\n")+ b.WriteString("- `[MUL-123](mention://issue/<issue-id>)` — clickable link to an issue (safe, no side effect)\n")+ b.WriteString("- `[@Name](mention://member/<user-id>)` — **sends a notification to a human**\n")+ b.WriteString("- `[@Name](mention://agent/<agent-id>)` — **enqueues a new run for that agent**\n\n")+ b.WriteString("### When NOT to use a mention link\n\n")+ b.WriteString("- Referring to someone in prose (e.g. \"GPT-Boy is right\") — write the plain name, no link.\n")+ b.WriteString("- **Replying to another agent that just spoke to you.** By default, do NOT put a `mention://agent/...` link anywhere in your reply. The platform already shows your comment to everyone on the issue; re-mentioning the other agent will make them run again, and if they reply with a mention back, you will be triggered again. That is a loop and it costs the user money.\n")+ b.WriteString("- Thanking, acknowledging, wrapping up, or signing off. These are exactly the moments where an accidental `@mention` causes the other agent to reply \"you're welcome\" and restart the loop. If the work is done, **end with no mention at all**.\n\n")+ b.WriteString("### When a mention IS appropriate\n\n")+ b.WriteString("- Escalating to a human owner who is not yet involved.\n")+ b.WriteString("- Delegating a concrete sub-task to another agent for the first time, with a clear request.\n")+ b.WriteString("- The user explicitly asked you to loop someone in.\n\n")+ b.WriteString("If you are unsure whether a mention is warranted, **don't mention**. Silence ends conversations; `@` restarts them.\n\n")b.WriteString("Use `multica issue list --output json` to look up issue IDs, and `multica workspace members --output json` for member IDs.\n\n")b.WriteString("## Attachments\n\n")@@ -197,7 +205,7 @@ func buildMetaSkillContent(provider string, ctx TaskContextForEnv) string {b.WriteString("Keep comments concise and natural — state the outcome, not the process.\n")b.WriteString("Good: \"Fixed the login redirect. PR: https://...\"\n")b.WriteString("Bad: \"1. Read the issue 2. Found the bug in auth.go 3. Created branch 4. ...\"\n")- b.WriteString("When referencing issues in comments, **always** use the mention format `[MUL-123](mention://issue/<issue-id>)` so they render as clickable links.\n")+ b.WriteString("When referencing an issue in a comment, use the issue mention format `[MUL-123](mention://issue/<issue-id>)` so it renders as a clickable link. (Issue mentions have no side effect; only member/agent mentions do — see the Mentions section above.)\n")return b.String()}diff --git a/server/internal/daemon/prompt.go b/server/internal/daemon/prompt.goindex 1efab84d46..f3fd537eec 100644--- a/server/internal/daemon/prompt.go+++ b/server/internal/daemon/prompt.go@@ -35,10 +35,21 @@ func buildCommentPrompt(task Task) string {b.WriteString("You are running as a local coding agent for a Multica workspace.\n\n")fmt.Fprintf(&b, "Your assigned issue ID is: %s\n\n", task.IssueID)if task.TriggerCommentContent != "" {- b.WriteString("[NEW COMMENT] A user just left a new comment that triggered this task. You MUST respond to THIS comment, not any previous ones:\n\n")+ authorLabel := "A user"+ if task.TriggerAuthorType == "agent" {+ name := task.TriggerAuthorName+ if name == "" {+ name = "another agent"+ }+ authorLabel = fmt.Sprintf("Another agent (%s)", name)+ }+ 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")+ }}- fmt.Fprintf(&b, "Start by running `multica issue get %s --output json` to understand your task, then complete it.\n\n", task.IssueID)+ 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)b.WriteString(execenv.BuildCommentReplyInstructions(task.IssueID, task.TriggerCommentID))return b.String()}diff --git a/server/internal/daemon/types.go b/server/internal/daemon/types.goindex d24087e1e1..dca6336f02 100644--- a/server/internal/daemon/types.go+++ b/server/internal/daemon/types.go@@ -36,6 +36,8 @@ type Task struct {PriorWorkDir string `json:"prior_work_dir,omitempty"` // work_dir from a previous task on this issueTriggerCommentID string `json:"trigger_comment_id,omitempty"` // comment that triggered this taskTriggerCommentContent string `json:"trigger_comment_content,omitempty"` // content of the triggering comment+ TriggerAuthorType string `json:"trigger_author_type,omitempty"` // "agent" or "member" — author kind for the triggering comment+ TriggerAuthorName string `json:"trigger_author_name,omitempty"` // display name of the triggering comment authorChatSessionID string `json:"chat_session_id,omitempty"` // non-empty for chat tasksChatMessage string `json:"chat_message,omitempty"` // user message content for chat tasks}diff --git a/server/internal/handler/agent.go b/server/internal/handler/agent.goindex 4bfeeab2a0..cc1c837d14 100644--- a/server/internal/handler/agent.go+++ b/server/internal/handler/agent.go@@ -137,6 +137,8 @@ type AgentTaskResponse struct {PriorWorkDir string `json:"prior_work_dir,omitempty"` // work_dir from a previous task on same issueTriggerCommentID *string `json:"trigger_comment_id,omitempty"` // comment that triggered this taskTriggerCommentContent string `json:"trigger_comment_content,omitempty"` // content of the triggering comment+ TriggerAuthorType string `json:"trigger_author_type,omitempty"` // "agent" or "member" — author kind of the triggering comment+ TriggerAuthorName string `json:"trigger_author_name,omitempty"` // display name of the triggering comment authorChatSessionID string `json:"chat_session_id,omitempty"` // non-empty for chat tasksChatMessage string `json:"chat_message,omitempty"` // user message for chat tasksAutopilotRunID string `json:"autopilot_run_id,omitempty"` // non-empty for autopilot-spawned tasksdiff --git a/server/internal/handler/daemon.go b/server/internal/handler/daemon.goindex 8d513cc729..3dab5c4bc1 100644--- a/server/internal/handler/daemon.go+++ b/server/internal/handler/daemon.go@@ -660,10 +660,30 @@ func (h *Handler) ClaimTaskByRuntime(w http.ResponseWriter, r *http.Request) {// Fetch the triggering comment content so the daemon can embed it// directly in the agent prompt (prevents the agent from ignoring comments- // when stale output files exist in a reused workdir).+ // when stale output files exist in a reused workdir). Also surface the+ // comment author's kind and display name so the agent knows whether it+ // was triggered by a human or by another agent — a signal used by the+ // harness instructions to avoid mention loops between agents.if task.TriggerCommentID.Valid {if comment, err := h.Queries.GetComment(r.Context(), task.TriggerCommentID); err == nil {resp.TriggerCommentContent = comment.Content+ resp.TriggerAuthorType = comment.AuthorType+ switch comment.AuthorType {+ case "agent":+ if comment.AuthorID.Valid {+ if a, err := h.Queries.GetAgent(r.Context(), comment.AuthorID); err == nil {+ resp.TriggerAuthorName = a.Name+ }+ }+ case "member":+ // For member-authored comments, AuthorID is a user UUID+ // (see handler.resolveActor) — look up the user's display name.+ if comment.AuthorID.Valid {+ if u, err := h.Queries.GetUser(r.Context(), comment.AuthorID); err == nil {+ resp.TriggerAuthorName = u.Name+ }+ }+ }}}