Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(subscribers): end issue tree walks on a stored parent loop (#11121)
pseudoyu7216e4a236a3
diff --git a/server/internal/handler/issue_tree_cycle_test.go b/server/internal/handler/issue_tree_cycle_test.gonew file mode 100644index 0000000000..b00bb3822c--- /dev/null+++ b/server/internal/handler/issue_tree_cycle_test.go@@ -0,0 +1,94 @@+package handler++import (+ "context"+ "testing"+ "time"++ "github.com/multica-ai/multica/server/internal/testutil"+ db "github.com/multica-ai/multica/server/pkg/db/generated"+)++// TestIssueTreeWalksStopOnParentCycle pins that the subscriber queries walking+// an issue's ancestors or descendants finish when the stored parent chain loops+// (A -> B -> A). Older servers could write such a loop, and a walk that never+// stops keeps the request and its database connection busy until the client+// gives up. Each query must answer well inside the deadline and still give the+// result it gives on a tree without the loop.+func TestIssueTreeWalksStopOnParentCycle(t *testing.T) {+ projectID := dbfx.Project(t, "cycle walk project")+ a := dbfx.Issue(t, "cycle walk A", testutil.Cols{"project_id": projectID})+ b := dbfx.Issue(t, "cycle walk B", testutil.Cols{"project_id": projectID, "parent_issue_id": a})+ dbfx.Exec(t, `UPDATE issue SET parent_issue_id = $1 WHERE id = $2`, b, a)+ // An opt-out on an unrelated issue, as anyone who ever left an issue has.+ // With it the opt-out checks have to read the whole ancestor chain instead+ // of finding nothing to compare against.+ other := dbfx.Issue(t, "cycle walk unrelated")+ dbfx.Exec(t, `INSERT INTO issue_subscriber (issue_id, user_type, user_id, reason, unsubscribed_at, opt_out_scope)+ VALUES ($1, 'member', $2, 'manual', now(), 'issue')`, other, testUserID)+ t.Cleanup(func() {+ ctx := context.Background()+ testPool.Exec(ctx, `UPDATE issue SET parent_issue_id = NULL WHERE id = ANY($1::uuid[])`, []string{a, b})+ testPool.Exec(ctx, `DELETE FROM issue_subscriber WHERE issue_id = ANY($1::uuid[])`, []string{a, b, other})+ testPool.Exec(ctx, `DELETE FROM project_subscriber WHERE project_id = $1`, projectID)+ })++ q := db.New(testPool)+ userID := parseUUID(testUserID)+ workspaceID := parseUUID(testWorkspaceID)+ withDeadline := func(t *testing.T) context.Context {+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)+ t.Cleanup(cancel)+ return ctx+ }++ t.Run("project subscribers", func(t *testing.T) {+ if err := q.AddProjectSubscriber(withDeadline(t), db.AddProjectSubscriberParams{+ ProjectID: parseUUID(projectID), WorkspaceID: workspaceID, UserID: userID,+ }); err != nil {+ t.Fatalf("AddProjectSubscriber: %v", err)+ }+ rows, err := q.ListProjectSubscribersForIssue(withDeadline(t), parseUUID(a))+ if err != nil {+ t.Fatalf("ListProjectSubscribersForIssue on a parent loop: %v", err)+ }+ if len(rows) != 1 || rows[0].UserID != userID {+ t.Fatalf("project subscribers = %+v, want only the test user", rows)+ }+ })++ t.Run("delegated subscriber", func(t *testing.T) {+ affected, err := q.AddDelegatedSubscriber(withDeadline(t), db.AddDelegatedSubscriberParams{+ IssueID: parseUUID(a), UserID: userID, Reason: "delegated", WorkspaceID: workspaceID,+ })+ if err != nil {+ t.Fatalf("AddDelegatedSubscriber on a parent loop: %v", err)+ }+ if affected != 1 {+ t.Fatalf("AddDelegatedSubscriber affected %d rows, want 1", affected)+ }+ })++ t.Run("subtree unsubscribe and opt-out", func(t *testing.T) {+ dbfx.Exec(t, `INSERT INTO issue_subscriber (issue_id, user_type, user_id, reason)+ VALUES ($1, 'member', $2, 'manual') ON CONFLICT DO NOTHING`, b, testUserID)+ ids, err := q.UnsubscribeFromIssueSubtree(withDeadline(t), db.UnsubscribeFromIssueSubtreeParams{+ ID: parseUUID(a), UserType: "member", UserID: userID,+ })+ if err != nil {+ t.Fatalf("UnsubscribeFromIssueSubtree on a parent loop: %v", err)+ }+ if len(ids) != 2 {+ t.Fatalf("UnsubscribeFromIssueSubtree retired %v, want A and B once each", ids)+ }+ optedOut, err := q.HasAncestorOptOut(withDeadline(t), db.HasAncestorOptOutParams{+ ID: parseUUID(b), UserType: "member", UserID: userID,+ })+ if err != nil {+ t.Fatalf("HasAncestorOptOut on a parent loop: %v", err)+ }+ if !optedOut {+ t.Fatal("HasAncestorOptOut lost the subtree opt-out on a parent loop")+ }+ })+}diff --git a/server/pkg/db/generated/project_subscriber.sql.go b/server/pkg/db/generated/project_subscriber.sql.goindex 0abaf26e2a..6016d02811 100644--- a/server/pkg/db/generated/project_subscriber.sql.go+++ b/server/pkg/db/generated/project_subscriber.sql.go@@ -85,10 +85,10 @@ func (q *Queries) IsProjectSubscriber(ctx context.Context, arg IsProjectSubscrib}const listProjectSubscribersForIssue = `-- name: ListProjectSubscribersForIssue :many-WITH RECURSIVE ancestors(node_id, parent_id, depth) AS (- SELECT root.id, root.parent_issue_id, 0 FROM issue root WHERE root.id = $1- UNION ALL- SELECT i.id, i.parent_issue_id, a.depth + 1 FROM issue i JOIN ancestors a ON i.id = a.parent_id+WITH RECURSIVE ancestors(node_id, parent_id, is_root) AS (+ SELECT root.id, root.parent_issue_id, true FROM issue root WHERE root.id = $1+ UNION+ SELECT i.id, i.parent_issue_id, false FROM issue i JOIN ancestors a ON i.id = a.parent_id)SELECT ps.user_id, ps.created_atFROM issue target@@ -110,7 +110,7 @@ WHERE target.id = $1JOIN ancestors a ON a.node_id = s.issue_idWHERE s.user_type = 'member' AND s.user_id = ps.user_idAND s.unsubscribed_at IS NOT NULL- AND (a.depth = 0 OR s.opt_out_scope = 'subtree')+ AND (a.is_root OR s.opt_out_scope = 'subtree'))ORDER BY ps.created_at, ps.user_id`@@ -126,6 +126,8 @@ type ListProjectSubscribersForIssueRow struct {// issue is the narrower, more recent intent, so the project never brings the// member back. Both notification fanout and the issue's effective subscriber// list read this, so the two cannot disagree.+// UNION drops repeated rows, so a parent loop an older server stored still+// ends the walk.func (q *Queries) ListProjectSubscribersForIssue(ctx context.Context, issueID pgtype.UUID) ([]ListProjectSubscribersForIssueRow, error) {rows, err := q.db.Query(ctx, listProjectSubscribersForIssue, issueID)if err != nil {diff --git a/server/pkg/db/generated/subscriber.sql.go b/server/pkg/db/generated/subscriber.sql.goindex 4b5e8a13d0..bb5c12bd94 100644--- a/server/pkg/db/generated/subscriber.sql.go+++ b/server/pkg/db/generated/subscriber.sql.go@@ -12,10 +12,10 @@ import ()const addDelegatedSubscriber = `-- name: AddDelegatedSubscriber :execrows-WITH RECURSIVE ancestors(node_id, parent_id, depth) AS (- SELECT root.id, root.parent_issue_id, 0 FROM issue root WHERE root.id = $1- UNION ALL- SELECT i.id, i.parent_issue_id, a.depth + 1+WITH RECURSIVE ancestors(node_id, parent_id, is_root) AS (+ SELECT root.id, root.parent_issue_id, true FROM issue root WHERE root.id = $1+ UNION+ SELECT i.id, i.parent_issue_id, falseFROM issue i JOIN ancestors a ON i.id = a.parent_id),active_member AS (@@ -32,7 +32,7 @@ WHERE NOT EXISTS (JOIN ancestors a ON a.node_id = s.issue_idWHERE s.user_type = 'member' AND s.user_id = $2AND s.unsubscribed_at IS NOT NULL- AND (a.depth = 0 OR s.opt_out_scope = 'subtree')+ AND (a.is_root OR s.opt_out_scope = 'subtree'))ON CONFLICT (issue_id, user_type, user_id) DO UPDATESET reason = EXCLUDED.reason@@ -70,6 +70,8 @@ type AddDelegatedSubscriberParams struct {// ON CONFLICT keeps AddIssueSubscriber's two rules: never resurrect a// tombstone, and upgrade an active 'delegated' row when the user becomes// directly involved.+// UNION drops repeated rows, so a parent loop an older server stored still+// ends the walk.func (q *Queries) AddDelegatedSubscriber(ctx context.Context, arg AddDelegatedSubscriberParams) (int64, error) {result, err := q.db.Exec(ctx, addDelegatedSubscriber,arg.IssueID,@@ -244,10 +246,10 @@ func (q *Queries) GetDelegatedSubscriptionFacts(ctx context.Context, arg GetDele}const hasAncestorOptOut = `-- name: HasAncestorOptOut :one-WITH RECURSIVE ancestors(node_id, parent_id, depth) AS (- SELECT root.id, root.parent_issue_id, 0 FROM issue root WHERE root.id = $1- UNION ALL- SELECT i.id, i.parent_issue_id, a.depth + 1 FROM issue i JOIN ancestors a ON i.id = a.parent_id+WITH RECURSIVE ancestors(node_id, parent_id, is_root) AS (+ SELECT root.id, root.parent_issue_id, true FROM issue root WHERE root.id = $1+ UNION+ SELECT i.id, i.parent_issue_id, false FROM issue i JOIN ancestors a ON i.id = a.parent_id)SELECT EXISTS(SELECT 1@@ -255,7 +257,7 @@ SELECT EXISTS(JOIN ancestors a ON a.node_id = s.issue_idWHERE s.user_type = $2 AND s.user_id = $3AND s.unsubscribed_at IS NOT NULL- AND (a.depth = 0 OR s.opt_out_scope = 'subtree')+ AND (a.is_root OR s.opt_out_scope = 'subtree')) AS opted_out`@@ -276,6 +278,8 @@ type HasAncestorOptOutParams struct {//// An 'issue'-scoped tombstone on an ancestor deliberately does NOT match: the// user declined that one issue, not everything the agent files beneath it.+// UNION drops repeated rows, so a parent loop an older server stored still+// ends the walk.func (q *Queries) HasAncestorOptOut(ctx context.Context, arg HasAncestorOptOutParams) (bool, error) {row := q.db.QueryRow(ctx, hasAncestorOptOut, arg.ID, arg.UserType, arg.UserID)var opted_out bool@@ -588,7 +592,7 @@ func (q *Queries) UndoIssueUnsubscribe(ctx context.Context, arg UndoIssueUnsubscconst unsubscribeFromIssueSubtree = `-- name: UnsubscribeFromIssueSubtree :manyWITH RECURSIVE subtree(node_id) AS (SELECT root.id FROM issue root WHERE root.id = $1- UNION ALL+ UNIONSELECT i.id FROM issue i JOIN subtree s ON i.parent_issue_id = s.node_id),retire_descendants AS (@@ -631,6 +635,8 @@ type UnsubscribeFromIssueSubtreeParams struct {// Returns every issue id it actually tombstoned so the caller can broadcast one// subscriber:removed per issue; publishing only the root left other open tabs// showing a stale subscription on the children.+// UNION drops repeated rows, so a parent loop an older server stored still+// ends the walk.func (q *Queries) UnsubscribeFromIssueSubtree(ctx context.Context, arg UnsubscribeFromIssueSubtreeParams) ([]pgtype.UUID, error) {rows, err := q.db.Query(ctx, unsubscribeFromIssueSubtree, arg.ID, arg.UserType, arg.UserID)if err != nil {diff --git a/server/pkg/db/queries/project_subscriber.sql b/server/pkg/db/queries/project_subscriber.sqlindex 469ce994c9..8b89c66ba9 100644--- a/server/pkg/db/queries/project_subscriber.sql+++ b/server/pkg/db/queries/project_subscriber.sql@@ -27,10 +27,12 @@ SELECT EXISTS(-- issue is the narrower, more recent intent, so the project never brings the-- member back. Both notification fanout and the issue's effective subscriber-- list read this, so the two cannot disagree.-WITH RECURSIVE ancestors(node_id, parent_id, depth) AS (- SELECT root.id, root.parent_issue_id, 0 FROM issue root WHERE root.id = sqlc.arg('issue_id')- UNION ALL- SELECT i.id, i.parent_issue_id, a.depth + 1 FROM issue i JOIN ancestors a ON i.id = a.parent_id+-- UNION drops repeated rows, so a parent loop an older server stored still+-- ends the walk.+WITH RECURSIVE ancestors(node_id, parent_id, is_root) AS (+ SELECT root.id, root.parent_issue_id, true FROM issue root WHERE root.id = sqlc.arg('issue_id')+ UNION+ SELECT i.id, i.parent_issue_id, false FROM issue i JOIN ancestors a ON i.id = a.parent_id)SELECT ps.user_id, ps.created_atFROM issue target@@ -52,7 +54,7 @@ WHERE target.id = sqlc.arg('issue_id')JOIN ancestors a ON a.node_id = s.issue_idWHERE s.user_type = 'member' AND s.user_id = ps.user_idAND s.unsubscribed_at IS NOT NULL- AND (a.depth = 0 OR s.opt_out_scope = 'subtree')+ AND (a.is_root OR s.opt_out_scope = 'subtree'))ORDER BY ps.created_at, ps.user_id;diff --git a/server/pkg/db/queries/subscriber.sql b/server/pkg/db/queries/subscriber.sqlindex dd4751021e..2d948c436a 100644--- a/server/pkg/db/queries/subscriber.sql+++ b/server/pkg/db/queries/subscriber.sql@@ -82,10 +82,12 @@ SELECT pg_advisory_xact_lock(-- ON CONFLICT keeps AddIssueSubscriber's two rules: never resurrect a-- tombstone, and upgrade an active 'delegated' row when the user becomes-- directly involved.-WITH RECURSIVE ancestors(node_id, parent_id, depth) AS (- SELECT root.id, root.parent_issue_id, 0 FROM issue root WHERE root.id = $1- UNION ALL- SELECT i.id, i.parent_issue_id, a.depth + 1+-- UNION drops repeated rows, so a parent loop an older server stored still+-- ends the walk.+WITH RECURSIVE ancestors(node_id, parent_id, is_root) AS (+ SELECT root.id, root.parent_issue_id, true FROM issue root WHERE root.id = $1+ UNION+ SELECT i.id, i.parent_issue_id, falseFROM issue i JOIN ancestors a ON i.id = a.parent_id),active_member AS (@@ -102,7 +104,7 @@ WHERE NOT EXISTS (JOIN ancestors a ON a.node_id = s.issue_idWHERE s.user_type = 'member' AND s.user_id = $2AND s.unsubscribed_at IS NOT NULL- AND (a.depth = 0 OR s.opt_out_scope = 'subtree')+ AND (a.is_root OR s.opt_out_scope = 'subtree'))ON CONFLICT (issue_id, user_type, user_id) DO UPDATESET reason = EXCLUDED.reason@@ -244,9 +246,11 @@ SELECT-- Returns every issue id it actually tombstoned so the caller can broadcast one-- subscriber:removed per issue; publishing only the root left other open tabs-- showing a stale subscription on the children.+-- UNION drops repeated rows, so a parent loop an older server stored still+-- ends the walk.WITH RECURSIVE subtree(node_id) AS (SELECT root.id FROM issue root WHERE root.id = $1- UNION ALL+ UNIONSELECT i.id FROM issue i JOIN subtree s ON i.parent_issue_id = s.node_id),retire_descendants AS (@@ -280,10 +284,12 @@ SELECT issue_id FROM retire_root;---- An 'issue'-scoped tombstone on an ancestor deliberately does NOT match: the-- user declined that one issue, not everything the agent files beneath it.-WITH RECURSIVE ancestors(node_id, parent_id, depth) AS (- SELECT root.id, root.parent_issue_id, 0 FROM issue root WHERE root.id = $1- UNION ALL- SELECT i.id, i.parent_issue_id, a.depth + 1 FROM issue i JOIN ancestors a ON i.id = a.parent_id+-- UNION drops repeated rows, so a parent loop an older server stored still+-- ends the walk.+WITH RECURSIVE ancestors(node_id, parent_id, is_root) AS (+ SELECT root.id, root.parent_issue_id, true FROM issue root WHERE root.id = $1+ UNION+ SELECT i.id, i.parent_issue_id, false FROM issue i JOIN ancestors a ON i.id = a.parent_id)SELECT EXISTS(SELECT 1@@ -291,7 +297,7 @@ SELECT EXISTS(JOIN ancestors a ON a.node_id = s.issue_idWHERE s.user_type = $2 AND s.user_id = $3AND s.unsubscribed_at IS NOT NULL- AND (a.depth = 0 OR s.opt_out_scope = 'subtree')+ AND (a.is_root OR s.opt_out_scope = 'subtree')) AS opted_out;-- name: ListIssueSubscribers :many