Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(comments): keep a wakeup running after the comment it came from is deleted (#11562)
pseudoyu2e3a834a6ef4
diff --git a/server/internal/handler/comment.go b/server/internal/handler/comment.goindex 36432a704e..4a08c59c05 100644--- a/server/internal/handler/comment.go+++ b/server/internal/handler/comment.go@@ -4129,6 +4129,15 @@ func (h *Handler) deleteComment(ctx context.Context, commentID, workspaceID pgty}}+ // The comment's content is gone whether it was removed or tombstoned, so a+ // wakeup scheduled from it no longer points at it.+ if err := qtx.DetachWakeupsFromComments(ctx, db.DetachWakeupsFromCommentsParams{+ WorkspaceID: target.WorkspaceID,+ CommentIds: append([]pgtype.UUID{target.ID}, out.RemovedIDs...),+ }); err != nil {+ return out, err+ }+out.IssueRevision, err = qtx.TouchIssueForCommentDelete(ctx, db.TouchIssueForCommentDeleteParams{IssueID: target.IssueID,WorkspaceID: target.WorkspaceID,diff --git a/server/internal/handler/comment_delete_test.go b/server/internal/handler/comment_delete_test.goindex 85b5ec66bd..ededc04a03 100644--- a/server/internal/handler/comment_delete_test.go+++ b/server/internal/handler/comment_delete_test.go@@ -4,6 +4,7 @@ import ("bytes""context""errors"+ "fmt""mime/multipart""net/http""net/http/httptest"@@ -473,3 +474,55 @@ func TestRemoveReactionOnTombstoneIsNotFound(t *testing.T) {"emoji": "👍",}), "commentId", target)).Want(http.StatusNotFound)}++// A wakeup scheduled from a comment keeps firing after that comment is+// deleted. Its runs used to name the removed comment as their trigger, which+// the task table rejects, so the wakeup never ran again.+func TestDeletedCommentLeavesItsWakeupRunnable(t *testing.T) {+ if testHandler == nil || testPool == nil {+ t.Skip("database not available")+ }+ issueID := dbfx.Issue(t, "wakeup outlives its comment")+ dbfx.Cleanup(t, "DELETE FROM agent_task_queue WHERE issue_id=$1", issueID)+ dbfx.Cleanup(t, "DELETE FROM issue_wakeup_receipt WHERE wakeup_id IN (SELECT id FROM issue_wakeup WHERE issue_id=$1)", issueID)+ dbfx.Cleanup(t, "DELETE FROM issue_wakeup WHERE issue_id=$1", issueID)+ root := dbfx.Comment(t, issueID, "check this later")+ reply := dbfx.Comment(t, issueID, "and this", testutil.Cols{"parent_id": root})++ svc := service.IssueWakeupService{Tasks: testHandler.TaskService}+ wakeups := map[string]db.IssueWakeup{}+ // One agent each, so neither run is folded into the other's.+ for i, parent := range []string{root, reply} {+ agentID := dbfx.Agent(t, fmt.Sprintf("wakeup comment target %d", i), testRuntimeID)+ w, err := svc.Create(context.Background(), parseUUID(issueID), parseUUID(testUserID), pgtype.UUID{}, service.WakeupInput{+ AgentID: agentID, Kind: "every", IntervalSeconds: 3600, ParentCommentID: parent, Instruction: "look again",+ })+ if err != nil {+ t.Fatal(err)+ }+ wakeups[parent] = w+ }++ // The root becomes a tombstone, then goes with its last reply.+ rec := newCommentEventRecorder()+ rec.delete(t, root).Want(http.StatusNoContent)+ rec.delete(t, reply).Want(http.StatusNoContent)+ if got := loadCommentRowState(t, root); got.exists {+ t.Fatalf("root = %+v, want it removed", got)+ }++ for parent, w := range wakeups {+ id := uuidToString(w.ID)+ req := withURLParams(newRequest(http.MethodPost, "/", nil), "id", issueID, "wakeupID", id)+ testutil.Call(t, testHandler.TriggerIssueWakeup, req).Want(http.StatusNoContent)+ var trigger *string+ if err := testPool.QueryRow(context.Background(),+ `SELECT trigger_comment_id::text FROM agent_task_queue WHERE context->>'wakeup_id'=$1`, id,+ ).Scan(&trigger); err != nil {+ t.Fatalf("wakeup from deleted comment %s did not run: %v", parent, err)+ }+ if trigger != nil {+ t.Fatalf("run names deleted comment %s as its trigger", *trigger)+ }+ }+}diff --git a/server/pkg/db/generated/wakeup.sql.go b/server/pkg/db/generated/wakeup.sql.goindex a358317139..4b7c878871 100644--- a/server/pkg/db/generated/wakeup.sql.go+++ b/server/pkg/db/generated/wakeup.sql.go@@ -616,6 +616,24 @@ func (q *Queries) DeleteIssueWakeupReceipts(ctx context.Context, id pgtype.UUID)return err}+const detachWakeupsFromComments = `-- name: DetachWakeupsFromComments :exec+UPDATE issue_wakeup SET parent_comment_id=NULL+WHERE workspace_id= $1 AND parent_comment_id=ANY($2::uuid[])+`++type DetachWakeupsFromCommentsParams struct {+ WorkspaceID pgtype.UUID `json:"workspace_id"`+ CommentIds []pgtype.UUID `json:"comment_ids"`+}++// A wakeup outlives the comment it was scheduled from. Its runs carry that+// comment as their trigger, which must exist, so a deleted comment is dropped+// from the rule instead.+func (q *Queries) DetachWakeupsFromComments(ctx context.Context, arg DetachWakeupsFromCommentsParams) error {+ _, err := q.db.Exec(ctx, detachWakeupsFromComments, arg.WorkspaceID, arg.CommentIds)+ return err+}+const disableIssueWakeups = `-- name: DisableIssueWakeups :execUPDATE issue_wakeup SET enabled=false,disabled_at=clock_timestamp(),updated_at=clock_timestamp()WHERE issue_id= $1 AND disabled_at IS NULL AND system_rule IS NULLdiff --git a/server/pkg/db/queries/wakeup.sql b/server/pkg/db/queries/wakeup.sqlindex cdc7578a63..6b5ae3dfe8 100644--- a/server/pkg/db/queries/wakeup.sql+++ b/server/pkg/db/queries/wakeup.sql@@ -332,3 +332,10 @@ WHERE id= @id AND status='dispatched' AND dispatched_at= @dispatched_at RETURNIN-- not finished: the run that made a change still knows about it.SELECT count(*) FROM agent_task_queue WHERE id=ANY(@ids::uuid[]) AND agent_id= @agent_id AND issue_id= @issue_idAND status IN ('dispatched','running','waiting_local_directory');++-- name: DetachWakeupsFromComments :exec+-- A wakeup outlives the comment it was scheduled from. Its runs carry that+-- comment as their trigger, which must exist, so a deleted comment is dropped+-- from the rule instead.+UPDATE issue_wakeup SET parent_comment_id=NULL+WHERE workspace_id= @workspace_id AND parent_comment_id=ANY(@comment_ids::uuid[]);