Skip to content

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

Go

MUL-7611: fix(comments): read only thread routing owners (#8712)

YYClaw42727f697eca
diff --git a/server/internal/handler/comment.go b/server/internal/handler/comment.go
index 4bf0fe6ff3..a0b5ea77e9 100644
--- a/server/internal/handler/comment.go
+++ b/server/internal/handler/comment.go
@@ -2893,10 +2893,6 @@ func (h *Handler) routeGuestSquadLeaderFallback(ctx context.Context, issue db.Is
return []commentAgentTrigger{trigger}, true
}
-type conversationRoutedAgentInfo struct {
- SquadID pgtype.UUID
-}
-
func (h *Handler) routeThreadRootOwners(ctx context.Context, issue db.Issue, parent *db.Comment, memberID string, opts commentTriggerComputeOptions) ([]commentAgentTrigger, bool) {
if parent == nil || !parent.ID.Valid {
return nil, false
@@ -2922,38 +2918,22 @@ func (h *Handler) routeConversationOwnersForRoot(ctx context.Context, issue db.I
return []commentAgentTrigger{trigger}, true
}
- rootID := uuidToString(root.ID)
- excludedID := uuidToString(opts.ExcludeTriggerCommentID)
-
- tasks, err := h.Queries.ListTasksByIssue(ctx, issue.ID)
- if err != nil {
+ if opts.ExcludeTriggerCommentID == root.ID {
return nil, false
}
- routedAgents := make(map[string]conversationRoutedAgentInfo)
- for _, task := range tasks {
- if !task.TriggerCommentID.Valid || !task.AgentID.Valid {
- continue
- }
- if excludedID != "" && uuidToString(task.TriggerCommentID) == excludedID {
- continue
- }
- if uuidToString(task.TriggerCommentID) != rootID {
- continue
- }
- agentID := uuidToString(task.AgentID)
- info := routedAgents[agentID]
- if !info.SquadID.Valid {
- info.SquadID = task.SquadID
- }
- routedAgents[agentID] = info
+ owners, err := h.Queries.ListConversationRootOwners(ctx, db.ListConversationRootOwnersParams{
+ IssueID: issue.ID, TriggerCommentID: root.ID,
+ })
+ if err != nil {
+ return nil, false
}
- if len(routedAgents) == 0 {
+ if len(owners) == 0 {
return nil, false
}
- triggers := make([]commentAgentTrigger, 0, len(routedAgents))
- for agentID, info := range routedAgents {
- trigger, ok := h.routeConversationContinuationToAgent(ctx, issue, parseUUID(agentID), info.SquadID, memberID, opts)
+ triggers := make([]commentAgentTrigger, 0, len(owners))
+ for _, owner := range owners {
+ trigger, ok := h.routeConversationContinuationToAgent(ctx, issue, owner.AgentID, owner.SquadID, memberID, opts)
if ok {
triggers = append(triggers, trigger)
}
diff --git a/server/internal/handler/comment_root_projection_test.go b/server/internal/handler/comment_root_projection_test.go
new file mode 100644
index 0000000000..0d2c8c7c90
--- /dev/null
+++ b/server/internal/handler/comment_root_projection_test.go
@@ -0,0 +1,159 @@
+package handler
+
+import (
+ "context"
+ "errors"
+ "fmt"
+ "strings"
+ "testing"
+ "time"
+
+ "github.com/jackc/pgx/v5"
+ "github.com/multica-ai/multica/server/internal/testutil"
+ db "github.com/multica-ai/multica/server/pkg/db/generated"
+)
+
+// Observe actual result columns/rows at the database boundary. A query that
+// filters later in Go still transfers the unwanted task payloads and fails.
+type rootOwnerReadDB struct {
+ db.DBTX
+ rows, reads int
+ payload bool
+ fail bool
+}
+type rootOwnerRows struct {
+ pgx.Rows
+ observer *rootOwnerReadDB
+}
+
+func (r *rootOwnerRows) Next() bool {
+ ok := r.Rows.Next()
+ if ok {
+ r.observer.rows++
+ }
+ return ok
+}
+func (d *rootOwnerReadDB) Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error) {
+ if !strings.Contains(sql, "FROM agent_task_queue") {
+ return d.DBTX.Query(ctx, sql, args...)
+ }
+ d.reads++
+ if d.fail {
+ return nil, errors.New("injected owner read failure")
+ }
+ rows, err := d.DBTX.Query(ctx, sql, args...)
+ if err != nil {
+ return nil, err
+ }
+ for _, field := range rows.FieldDescriptions() {
+ if field.Name == "context" || field.Name == "result" {
+ d.payload = true
+ }
+ }
+ return &rootOwnerRows{Rows: rows, observer: d}, nil
+}
+
+func TestConversationRootOwnersReadOnlyRoutingData(t *testing.T) {
+ if testHandler == nil {
+ t.Skip("database not available")
+ }
+ ctx := context.Background()
+ agentA := createHandlerTestAgent(t, "Root owner A", nil)
+ agentB := createHandlerTestAgent(t, "Root owner B", nil)
+ otherAgent := createHandlerTestAgent(t, "Other root owner", nil)
+ issueID := dbfx.Issue(t, "Routing projection")
+ otherIssueID := dbfx.Issue(t, "Other issue")
+ rootID := dbfx.Comment(t, issueID, "No explicit owner")
+ otherRootID := dbfx.Comment(t, issueID, "Another root")
+ oldSquad := dbfx.Squad(t, "Old root squad", agentA)
+ newSquad := dbfx.Squad(t, "New root squad", agentA)
+ for i := 0; i < 96; i++ {
+ squad := any(nil)
+ if i == 0 {
+ squad = oldSquad
+ }
+ if i == 1 {
+ squad = newSquad
+ }
+ dbfx.Task(t, agentA, testutil.Cols{
+ "issue_id": issueID, "trigger_comment_id": rootID, "runtime_id": handlerTestRuntimeID(t), "status": "completed", "squad_id": squad,
+ "created_at": time.Date(2026, 1, 1, 0, 0, i, 0, time.UTC),
+ "context": testutil.Raw(`jsonb_build_object('large',repeat('x',65536))`),
+ "result": testutil.Raw(`jsonb_build_object('large',repeat('y',65536))`),
+ })
+ }
+ dbfx.Task(t, agentB, testutil.Cols{"issue_id": issueID, "trigger_comment_id": rootID, "runtime_id": handlerTestRuntimeID(t), "status": "failed"})
+ dbfx.Task(t, otherAgent, testutil.Cols{"issue_id": issueID, "trigger_comment_id": otherRootID, "runtime_id": handlerTestRuntimeID(t), "status": "completed"})
+ dbfx.Task(t, otherAgent, testutil.Cols{"issue_id": otherIssueID, "trigger_comment_id": rootID, "runtime_id": handlerTestRuntimeID(t), "status": "completed"})
+ issue, err := testHandler.Queries.GetIssue(ctx, parseUUID(issueID))
+ if err != nil {
+ t.Fatal(err)
+ }
+ root, err := testHandler.Queries.GetComment(ctx, parseUUID(rootID))
+ if err != nil {
+ t.Fatal(err)
+ }
+ observer := &rootOwnerReadDB{DBTX: testPool}
+ h := *testHandler
+ h.Queries = db.New(observer)
+ triggers, owned := h.routeConversationOwnersForRoot(ctx, issue, root, testUserID, commentTriggerComputeOptions{})
+ if !owned || len(triggers) != 2 {
+ t.Fatalf("owned=%t triggers=%d, want two root owners", owned, len(triggers))
+ }
+ for _, trigger := range triggers {
+ switch uuidToString(trigger.Agent.ID) {
+ case agentA:
+ if trigger.Squad == nil || uuidToString(trigger.Squad.ID) != newSquad {
+ t.Fatal("lost newest non-null squad")
+ }
+ case agentB:
+ if trigger.Squad != nil {
+ t.Fatal("invented squad")
+ }
+ default:
+ t.Fatal("routed unrelated task owner")
+ }
+ }
+ if observer.reads != 1 || observer.rows != 2 || observer.payload {
+ t.Fatalf("routing read: reads=%d rows=%d payload=%t; want one narrow row per owner", observer.reads, observer.rows, observer.payload)
+ }
+
+ for _, excluded := range []string{rootID, otherRootID} {
+ got, owned := h.routeConversationOwnersForRoot(ctx, issue, root, testUserID, commentTriggerComputeOptions{ExcludeTriggerCommentID: parseUUID(excluded)})
+ if excluded == rootID {
+ if owned || len(got) != 0 {
+ t.Fatal("excluded root routed")
+ }
+ } else if !owned || len(got) != 2 {
+ t.Fatal("unrelated exclusion removed owners")
+ }
+ }
+ emptyID := dbfx.Comment(t, issueID, "No routed task")
+ empty, err := testHandler.Queries.GetComment(ctx, parseUUID(emptyID))
+ if err != nil {
+ t.Fatal(err)
+ }
+ if got, owned := h.routeConversationOwnersForRoot(ctx, issue, empty, testUserID, commentTriggerComputeOptions{}); owned || len(got) != 0 {
+ t.Fatal("invented empty root owner")
+ }
+ observer.fail = true
+ if got, owned := h.routeConversationOwnersForRoot(ctx, issue, root, testUserID, commentTriggerComputeOptions{}); owned || len(got) != 0 {
+ t.Fatal("read failure routed an owner")
+ }
+ // An explicit mention remains authoritative, even if historical lookup fails.
+ root.Content = fmt.Sprintf("[@Other](mention://agent/%s)", otherAgent)
+ if got, owned := h.routeConversationOwnersForRoot(ctx, issue, root, testUserID, commentTriggerComputeOptions{}); !owned || len(got) != 1 || uuidToString(got[0].Agent.ID) != otherAgent {
+ t.Fatal("explicit owner lost precedence")
+ }
+ observer.fail = false
+ root.Content = "No explicit owner"
+ dbfx.Exec(t, "UPDATE agent SET archived_at=now() WHERE id=$1", agentB)
+ if got, owned := h.routeConversationOwnersForRoot(ctx, issue, root, testUserID, commentTriggerComputeOptions{}); !owned || len(got) != 1 || uuidToString(got[0].Agent.ID) != agentA {
+ t.Fatal("archived owner remained invokable")
+ }
+ dbfx.Exec(t, "UPDATE agent SET archived_at=now() WHERE id=$1", agentA)
+ if got, owned := h.routeConversationOwnersForRoot(ctx, issue, root, testUserID, commentTriggerComputeOptions{}); !owned || len(got) != 0 {
+ t.Fatal("unavailable historical owners must still suppress reassignment fallback")
+ }
+
+}
diff --git a/server/pkg/db/generated/conversation_root.sql.go b/server/pkg/db/generated/conversation_root.sql.go
new file mode 100644
index 0000000000..3fb884b6d0
--- /dev/null
+++ b/server/pkg/db/generated/conversation_root.sql.go
@@ -0,0 +1,51 @@
+// Code generated by sqlc. DO NOT EDIT.
+// versions:
+// sqlc v1.31.1
+// source: conversation_root.sql
+
+package db
+
+import (
+ "context"
+
+ "github.com/jackc/pgx/v5/pgtype"
+)
+
+const listConversationRootOwners = `-- name: ListConversationRootOwners :many
+SELECT DISTINCT ON (agent_id) agent_id, squad_id
+FROM agent_task_queue
+WHERE issue_id = $1 AND trigger_comment_id = $2 AND agent_id IS NOT NULL
+ORDER BY agent_id, (squad_id IS NOT NULL) DESC, created_at DESC, id DESC
+`
+
+type ListConversationRootOwnersParams struct {
+ IssueID pgtype.UUID `json:"issue_id"`
+ TriggerCommentID pgtype.UUID `json:"trigger_comment_id"`
+}
+
+type ListConversationRootOwnersRow struct {
+ AgentID pgtype.UUID `json:"agent_id"`
+ SquadID pgtype.UUID `json:"squad_id"`
+}
+
+// Preserve the newest non-null squad per agent, including terminal tasks.
+// Routing does not need execution context, results, or other issue threads.
+func (q *Queries) ListConversationRootOwners(ctx context.Context, arg ListConversationRootOwnersParams) ([]ListConversationRootOwnersRow, error) {
+ rows, err := q.db.Query(ctx, listConversationRootOwners, arg.IssueID, arg.TriggerCommentID)
+ if err != nil {
+ return nil, err
+ }
+ defer rows.Close()
+ items := []ListConversationRootOwnersRow{}
+ for rows.Next() {
+ var i ListConversationRootOwnersRow
+ if err := rows.Scan(&i.AgentID, &i.SquadID); err != nil {
+ return nil, err
+ }
+ items = append(items, i)
+ }
+ if err := rows.Err(); err != nil {
+ return nil, err
+ }
+ return items, nil
+}
diff --git a/server/pkg/db/queries/conversation_root.sql b/server/pkg/db/queries/conversation_root.sql
new file mode 100644
index 0000000000..be1543f388
--- /dev/null
+++ b/server/pkg/db/queries/conversation_root.sql
@@ -0,0 +1,7 @@
+-- name: ListConversationRootOwners :many
+-- Preserve the newest non-null squad per agent, including terminal tasks.
+-- Routing does not need execution context, results, or other issue threads.
+SELECT DISTINCT ON (agent_id) agent_id, squad_id
+FROM agent_task_queue
+WHERE issue_id = $1 AND trigger_comment_id = $2 AND agent_id IS NOT NULL
+ORDER BY agent_id, (squad_id IS NOT NULL) DESC, created_at DESC, id DESC;