Skip to content

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

Go

perf(search): prune terminal-issue comments in open-only issue search (#10098)

AstralSolipsism10d4496df43e
diff --git a/server/internal/handler/issue.go b/server/internal/handler/issue.go
index a8d3c0b1e8..50586c3c09 100644
--- a/server/internal/handler/issue.go
+++ b/server/internal/handler/issue.go
@@ -950,6 +950,17 @@ func buildSearchQuery(phrase string, terms []string, queryNum int, hasNum bool,
commentSnippetPredicate,
))
+ commentWhere := "c.workspace_id = " + wsParam
+ if terminalStatusesParam != "" {
+ // Terminal issues cannot survive the issue_matches join in an open-only
+ // search; pruning them here skips LOWER(c.content) and BOOL_OR/ARRAY_AGG
+ // aggregation for closed issues' comments.
+ commentWhere += fmt.Sprintf(
+ " AND EXISTS (SELECT 1 FROM issue i WHERE i.id = c.issue_id AND %s)",
+ issueWhere,
+ )
+ }
+
// PostgreSQL otherwise inlines this scalar LATERAL subquery and recomputes
// LOWER(c.content) for every flag. OFFSET 0 is the intentional planner fence
// that keeps the long comment body lowercased once per row. Future indexable
@@ -964,13 +975,13 @@ func buildSearchQuery(phrase string, terms []string, queryNum int, hasNum bool,
SELECT LOWER(c.content) AS lowered
OFFSET 0
) lowered_comment
- WHERE c.workspace_id = %s
+ WHERE %s
GROUP BY c.issue_id
) aggregated_comments
WHERE %s
)`,
strings.Join(commentFlagColumns, ",\n\t\t\t\t"),
- wsParam,
+ commentWhere,
strings.Join(commentCandidateFlags, " OR "),
)
diff --git a/server/internal/handler/search_open_comments_test.go b/server/internal/handler/search_open_comments_test.go
new file mode 100644
index 0000000000..97a7ae2223
--- /dev/null
+++ b/server/internal/handler/search_open_comments_test.go
@@ -0,0 +1,76 @@
+package handler
+
+import (
+ "context"
+ "fmt"
+ "slices"
+ "strings"
+ "testing"
+
+ "github.com/google/uuid"
+ "github.com/multica-ai/multica/server/internal/testutil"
+)
+
+func TestBuildSearchQuery_OpenCommentCandidates(t *testing.T) {
+ if testHandler == nil || testPool == nil {
+ t.Skip("requires DB")
+ }
+
+ token := "opencandidates" + uuid.NewString()[:8]
+ var open, closed []string
+ for _, status := range []string{"todo", "blocked", "done", "cancelled", "verified"} {
+ id := dbfx.Issue(t, "unrelated", testutil.Cols{"status": status})
+ dbfx.Comment(t, id, token+" alpha")
+ dbfx.Comment(t, id, "beta")
+ if status == "todo" || status == "blocked" {
+ open = append(open, id)
+ } else {
+ closed = append(closed, id)
+ }
+ }
+ for _, includeClosed := range []bool{false, true} {
+ t.Run(fmt.Sprintf("include_closed=%t", includeClosed), func(t *testing.T) {
+ query, args := buildSearchQuery(
+ token+" alpha beta",
+ []string{token, "alpha", "beta"},
+ 0,
+ false,
+ includeClosed,
+ []string{"done", "cancelled", "verified"},
+ )
+ args[3], args[len(args)-2], args[len(args)-1] = parseUUID(testWorkspaceID), 20, 0
+ // Inspect the candidate CTE directly: checking final search rows alone
+ // cannot detect wasted aggregation of terminal issues' comments.
+ end := strings.Index(query, "\n\tSELECT i.id")
+ if end < 0 {
+ t.Fatal("missing final search projection")
+ }
+ rows, err := testPool.Query(context.Background(), query[:end]+" SELECT issue_id::text FROM comment_matches", args...)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer rows.Close()
+ var ids []string
+ for rows.Next() {
+ var id string
+ if err := rows.Scan(&id); err != nil {
+ t.Fatal(err)
+ }
+ ids = append(ids, id)
+ }
+ if err := rows.Err(); err != nil {
+ t.Fatal(err)
+ }
+ for _, id := range open {
+ if !slices.Contains(ids, id) {
+ t.Errorf("open issue %s lost its cross-comment candidates", id)
+ }
+ }
+ for _, id := range closed {
+ if slices.Contains(ids, id) != includeClosed {
+ t.Errorf("terminal issue %s in candidates = %t, want %t", id, slices.Contains(ids, id), includeClosed)
+ }
+ }
+ })
+ }
+}
diff --git a/server/internal/handler/search_test.go b/server/internal/handler/search_test.go
index ff82219baa..56a11d8565 100644
--- a/server/internal/handler/search_test.go
+++ b/server/internal/handler/search_test.go
@@ -39,6 +39,9 @@ func TestBuildSearchQuery_SingleTerm(t *testing.T) {
if !strings.Contains(query, "NOT (i.status = ANY(") {
t.Error("query should exclude the expanded terminal status keys when includeClosed=false")
}
+ if !strings.Contains(query, "AND EXISTS (SELECT 1 FROM issue i WHERE i.id = c.issue_id") {
+ t.Error("comment_matches should prune terminal-issue comments via EXISTS when includeClosed=false")
+ }
if strings.Contains(query, "issue_effective_status") {
t.Error("query should not resolve status categories once per issue row")
}
@@ -196,11 +199,14 @@ func TestBuildSearchQuery_WithNumber(t *testing.T) {
}
func TestBuildSearchQuery_IncludeClosed(t *testing.T) {
- query, _ := buildSearchQuery("test", []string{"test"}, 0, false, true, nil)
+ query, _ := buildSearchQuery("test", []string{"test"}, 0, false, true, []string{"done", "cancelled"})
if strings.Contains(query, "i.status = ANY(") {
t.Error("query should not exclude done/cancelled when includeClosed=true")
}
+ if strings.Contains(query, "EXISTS (SELECT 1 FROM issue i") {
+ t.Error("query should not add an issue EXISTS check to comment_matches when includeClosed=true")
+ }
}
func TestBuildSearchQuery_SpecialChars(t *testing.T) {