From 0b60f78e8a2cfe48e73885a211382a1502af2653 Mon Sep 17 00:00:00 2001 From: Bohan Jiang <52446949+Bohan-J@users.noreply.github.com> Date: Mon, 13 Apr 2026 19:53:23 +0800 Subject: [PATCH] fix(comment): set trigger_comment_id to actual reply, not thread root (#871) * fix(comment): set trigger_comment_id to actual reply, not thread root When a user replies in a thread and @mentions an agent, the enqueued task's trigger_comment_id was incorrectly set to the parent (thread root) comment instead of the reply that contained the mention. This caused the agent to read the wrong comment and miss the user's actual instructions. Always pass comment.ID to EnqueueTaskForMention so agents see the comment that triggered them. Fixes MUL-708 * fix(task): resolve thread root in createAgentComment for reply triggers With trigger_comment_id now correctly pointing to the actual reply (not the thread root), createAgentComment must resolve to the thread root before posting. Otherwise error/system comments would have parent_id pointing to a nested reply, making them invisible in the frontend's flat thread grouping. Part of MUL-708 --- server/internal/handler/comment.go | 9 +++------ server/internal/service/task.go | 7 +++++++ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/server/internal/handler/comment.go b/server/internal/handler/comment.go index 03729018f..d4bbdb20e 100644 --- a/server/internal/handler/comment.go +++ b/server/internal/handler/comment.go @@ -409,12 +409,9 @@ func (h *Handler) enqueueMentionedAgentTasks(ctx context.Context, issue db.Issue if err != nil || hasPending { continue } - // Resolve thread root for reply threading. - replyTo := comment.ID - if comment.ParentID.Valid { - replyTo = comment.ParentID - } - if _, err := h.TaskService.EnqueueTaskForMention(ctx, issue, agentUUID, replyTo); err != nil { + // Always use the current comment as the trigger so the agent reads the + // actual reply that mentioned it, not the thread root. + if _, err := h.TaskService.EnqueueTaskForMention(ctx, issue, agentUUID, comment.ID); err != nil { slog.Warn("enqueue mention agent task failed", "issue_id", uuidToString(issue.ID), "agent_id", m.ID, "error", err) } } diff --git a/server/internal/service/task.go b/server/internal/service/task.go index f0ff5b3be..518bfe2c9 100644 --- a/server/internal/service/task.go +++ b/server/internal/service/task.go @@ -561,6 +561,13 @@ func (s *TaskService) createAgentComment(ctx context.Context, issueID, agentID p if err != nil { return } + // Resolve thread root: if parentID points to a reply (has its own parent), + // use that parent instead so the comment lands in the top-level thread. + if parentID.Valid { + if parent, err := s.Queries.GetComment(ctx, parentID); err == nil && parent.ParentID.Valid { + parentID = parent.ParentID + } + } // Expand bare issue identifiers (e.g. MUL-117) into mention links. content = mention.ExpandIssueIdentifiers(ctx, s.Queries, issue.WorkspaceID, content) comment, err := s.Queries.CreateComment(ctx, db.CreateCommentParams{