Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(issues): re-rank an issue when its status moves it to a new column (#7422)
Bohan Jiangaa4c504c789e
diff --git a/server/cmd/migrate/main.go b/server/cmd/migrate/main.goindex f4e2d4334d..f9adc3c4a5 100644--- a/server/cmd/migrate/main.go+++ b/server/cmd/migrate/main.go@@ -251,6 +251,7 @@ var concurrentIndexCleanups = map[string]string{"395_plugin_package_version_package_index": "idx_plugin_package_version_package","396_plugin_package_file_path_index": "idx_plugin_package_file_path","397_plugin_installation_package_version_index": "idx_plugin_installation_package_version",+ "398_issue_workspace_status_position_index": "idx_issue_workspace_status_position",}// concurrentDownIndexCleanups covers every migration whose down directiondiff --git a/server/cmd/multica/cmd_issue.go b/server/cmd/multica/cmd_issue.goindex 1fc9465778..f8b0305283 100644--- a/server/cmd/multica/cmd_issue.go+++ b/server/cmd/multica/cmd_issue.go@@ -237,7 +237,9 @@ var issueReorderCmd = &cobra.Command{" --top move it to the top of its column\n" +" --bottom move it to the bottom of its column\n\n" +"Reorder stays inside the issue's current column. To move an issue to a\n" +- "different column, change its status first with `multica issue status`.",+ "different column, change its status first with `multica issue status`,\n" ++ "which lands it at the top of the destination column — no follow-up\n" ++ "reorder needed.",Args: exactArgs(1),RunE: runIssueReorder,}diff --git a/server/internal/handler/issue_status_position_test.go b/server/internal/handler/issue_status_position_test.gonew file mode 100644index 0000000000..fb2df6a415--- /dev/null+++ b/server/internal/handler/issue_status_position_test.go@@ -0,0 +1,165 @@+package handler++import (+ "context"+ "net/http"+ "testing"++ "github.com/multica-ai/multica/server/internal/testutil"+ db "github.com/multica-ai/multica/server/pkg/db/generated"+)++// position ranks an issue inside its own (workspace, status) column, so the+// value is meaningless once the status moves the issue to a different column.+// Carrying it over put agent-completed work below every hand-dragged issue in+// Done — the column an agent writes to and never drags in. These tests pin the+// re-ranking to the two queries that can change a status.++// destinationMin reads the current top of a column so assertions stay valid+// against a shared test workspace other suites have left rows in.+func destinationMin(t *testing.T, status string) float64 {+ t.Helper()+ var min float64+ dbfx.QueryRow(t,+ `SELECT COALESCE(MIN(position), 0) FROM issue WHERE workspace_id = $1 AND status = $2`,+ testWorkspaceID, status).Scan(&min)+ return min+}++func issuePosition(t *testing.T, issueID string) float64 {+ t.Helper()+ var pos float64+ dbfx.QueryRow(t, `SELECT position FROM issue WHERE id = $1`, issueID).Scan(&pos)+ return pos+}++// TestUpdateIssueStatusRanksAboveDestinationColumn reproduces the reported+// board state: a Done column ordered by hand, and an issue arriving from Todo+// carrying the -1 that made it the top of Todo. -1 is above nothing in Done.+func TestUpdateIssueStatusRanksAboveDestinationColumn(t *testing.T) {+ for _, pos := range []float64{-57.75, -20, -7.25} {+ dbfx.Issue(t, "status-position hand-dragged done issue", testutil.Cols{+ "status": "done",+ "position": pos,+ })+ }+ moving := dbfx.Issue(t, "status-position moving issue", testutil.Cols{+ "status": "todo",+ "position": -1.0,+ })++ before := destinationMin(t, "done")++ var updated IssueResponse+ testutil.Call(t, testHandler.UpdateIssue, testutil.WithURLParams(+ newRequest(http.MethodPut, "/api/issues/"+moving, map[string]any{"status": "done"}),+ "id", moving,+ )).Want(http.StatusOK).JSON(&updated)++ if updated.Position >= before {+ t.Errorf("position after move = %v, want above the Done column top (%v)", updated.Position, before)+ }+ if updated.Position >= -57.75 {+ t.Errorf("position after move = %v, want above the hand-dragged issues (-57.75 and lower)", updated.Position)+ }+}++// TestUpdateIssueStatusKeepsExplicitPosition covers cross-column drag-and-drop,+// which sends status and position together and means the slot it dropped on.+func TestUpdateIssueStatusKeepsExplicitPosition(t *testing.T) {+ dbfx.Issue(t, "status-position explicit destination issue", testutil.Cols{+ "status": "done",+ "position": -1000.0,+ })+ moving := dbfx.Issue(t, "status-position explicit moving issue", testutil.Cols{+ "status": "todo",+ "position": -1.0,+ })++ const dropped = -12.5+ var updated IssueResponse+ testutil.Call(t, testHandler.UpdateIssue, testutil.WithURLParams(+ newRequest(http.MethodPut, "/api/issues/"+moving, map[string]any{+ "status": "done",+ "position": dropped,+ }),+ "id", moving,+ )).Want(http.StatusOK).JSON(&updated)++ if updated.Position != dropped {+ t.Errorf("position after drag = %v, want the dropped slot %v", updated.Position, dropped)+ }+}++// TestUpdateIssueWithoutStatusChangeKeepsPosition guards the other direction:+// editing an issue in place must not shuffle the column it is already in.+func TestUpdateIssueWithoutStatusChangeKeepsPosition(t *testing.T) {+ issueID := dbfx.Issue(t, "status-position untouched issue", testutil.Cols{+ "status": "todo",+ "position": -3.5,+ })++ var updated IssueResponse+ testutil.Call(t, testHandler.UpdateIssue, testutil.WithURLParams(+ newRequest(http.MethodPut, "/api/issues/"+issueID, map[string]any{+ "title": "status-position untouched issue, retitled",+ "status": "todo",+ }),+ "id", issueID,+ )).Want(http.StatusOK).JSON(&updated)++ if updated.Position != -3.5 {+ t.Errorf("position after in-place edit = %v, want it left at -3.5", updated.Position)+ }+}++// TestUpdateIssueStatusQueryRanksAboveDestinationColumn covers the query GitHub+// sync and agent task completion call directly, bypassing the UpdateIssue+// handler. Agents close issues through this path and never drag anything, so+// leaving it out would leave the reported bug in place for its main victim.+func TestUpdateIssueStatusQueryRanksAboveDestinationColumn(t *testing.T) {+ dbfx.Issue(t, "direct status-position destination issue", testutil.Cols{+ "status": "done",+ "position": -1000.0,+ })+ moving := dbfx.Issue(t, "direct status-position moving issue", testutil.Cols{+ "status": "todo",+ "position": -1.0,+ })++ before := destinationMin(t, "done")++ updated, err := testHandler.Queries.UpdateIssueStatus(context.Background(), db.UpdateIssueStatusParams{+ ID: parseUUID(moving),+ Status: "done",+ WorkspaceID: parseUUID(testWorkspaceID),+ })+ if err != nil {+ t.Fatalf("UpdateIssueStatus: %v", err)+ }+ if updated.Position >= before {+ t.Errorf("position after move = %v, want above the Done column top (%v)", updated.Position, before)+ }+}++// TestUpdateIssueStatusQueryNoopKeepsPosition pins the same-status write: a+// re-delivered GitHub webhook or a repeated completion must not walk the issue+// further up its own column on every call.+func TestUpdateIssueStatusQueryNoopKeepsPosition(t *testing.T) {+ issueID := dbfx.Issue(t, "direct status-position noop issue", testutil.Cols{+ "status": "done",+ "position": -4.25,+ })++ if _, err := testHandler.Queries.UpdateIssueStatus(context.Background(), db.UpdateIssueStatusParams{+ ID: parseUUID(issueID),+ Status: "done",+ WorkspaceID: parseUUID(testWorkspaceID),+ }); err != nil {+ t.Fatalf("UpdateIssueStatus: %v", err)+ }++ if got := issuePosition(t, issueID); got != -4.25 {+ t.Errorf("position after same-status write = %v, want it left at -4.25", got)+ }+}diff --git a/server/internal/issueposition/position.go b/server/internal/issueposition/position.goindex 2ec255ce72..28b10be4af 100644--- a/server/internal/issueposition/position.go+++ b/server/internal/issueposition/position.go@@ -14,6 +14,10 @@ type queryRower interface {// NextTopPosition returns a position that sorts before every existing issue in// the workspace/status column when manual sorting orders by position ASC.+//+// UpdateIssue and UpdateIssueStatus in queries/issue.sql inline the same+// MIN(position) - 1 policy to re-rank an issue whose status moves it to a+// different column. Change them together.func NextTopPosition(ctx context.Context, q queryRower, workspaceID pgtype.UUID, status string) (float64, error) {var minPos float64if err := q.QueryRow(ctx,diff --git a/server/migrations/398_issue_workspace_status_position_index.down.sql b/server/migrations/398_issue_workspace_status_position_index.down.sqlnew file mode 100644index 0000000000..65a2d08f96--- /dev/null+++ b/server/migrations/398_issue_workspace_status_position_index.down.sql@@ -0,0 +1 @@+DROP INDEX CONCURRENTLY IF EXISTS idx_issue_workspace_status_position;diff --git a/server/migrations/398_issue_workspace_status_position_index.up.sql b/server/migrations/398_issue_workspace_status_position_index.up.sqlnew file mode 100644index 0000000000..c815d16339--- /dev/null+++ b/server/migrations/398_issue_workspace_status_position_index.up.sql@@ -0,0 +1,8 @@+-- Re-ranking an issue into a new column reads MIN(position) for the+-- destination (workspace_id, status). idx_issue_status stops at+-- (workspace_id, status), so that min costs a heap fetch per issue in the+-- column -- paid on every status change, most often against Done, the column+-- that grows without bound. Carrying position in the index makes it an+-- index-only min. New-issue creation takes the same min and benefits equally.+CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_issue_workspace_status_position+ ON issue (workspace_id, status, position);diff --git a/server/pkg/db/generated/issue.sql.go b/server/pkg/db/generated/issue.sql.goindex 7f27b2da44..93c6bbbe34 100644--- a/server/pkg/db/generated/issue.sql.go+++ b/server/pkg/db/generated/issue.sql.go@@ -1554,7 +1554,34 @@ WITH candidate AS (COALESCE($5::text, i.priority) AS next_priority,$6::text AS next_assignee_type,$7::uuid AS next_assignee_id,- COALESCE($8::double precision, i.position) AS next_position,+ CASE+ -- An explicit position wins. Cross-column drag-and-drop sends+ -- status and position together and means the slot it dropped on.+ WHEN $8::double precision IS NOT NULL+ THEN $8::double precision+ -- position ranks an issue *within* its (workspace, status)+ -- column, so it stops meaning anything the moment the column+ -- changes: the value that put the issue on top of Todo lands it+ -- below every hand-dragged issue in Done. Re-rank to the top of+ -- the destination, the same policy new issues get from+ -- NextTopPosition. Keep the two in sync.+ --+ -- Two status changes running at once can read the same MIN and+ -- claim one slot. Every position-ordered query carries a unique+ -- final key (created_at DESC, id DESC), so a tie leaves the+ -- relative order of simultaneous moves arbitrary but never+ -- unstable across pages. Creation avoids the tie by computing its+ -- min under the workspace counter lock; a status change holds no+ -- such lock and is not worth taking one for.+ WHEN i.status IS DISTINCT FROM COALESCE($4::text, i.status)+ THEN (+ SELECT COALESCE(MIN(target.position), 0) - 1+ FROM issue AS target+ WHERE target.workspace_id = i.workspace_id+ AND target.status = $4::text+ )+ ELSE i.position+ END AS next_position,$9::date AS next_start_date,$10::date AS next_due_date,$11::uuid AS next_parent_issue_id,@@ -1677,15 +1704,21 @@ func (q *Queries) UpdateIssue(ctx context.Context, arg UpdateIssueParams) (Issue}const updateIssueStatus = `-- name: UpdateIssueStatus :one-UPDATE issue SET+UPDATE issue AS i SETstatus = $2,- revision = revision + CASE WHEN status IS DISTINCT FROM $2 THEN 1 ELSE 0 END,- last_activity_at = CASE WHEN status IS DISTINCT FROM $2- THEN GREATEST(COALESCE(last_activity_at, updated_at), now())- ELSE last_activity_at+ position = CASE WHEN i.status IS DISTINCT FROM $2 THEN (+ SELECT COALESCE(MIN(target.position), 0) - 1+ FROM issue AS target+ WHERE target.workspace_id = i.workspace_id+ AND target.status = $2+ ) ELSE i.position END,+ revision = i.revision + CASE WHEN i.status IS DISTINCT FROM $2 THEN 1 ELSE 0 END,+ last_activity_at = CASE WHEN i.status IS DISTINCT FROM $2+ THEN GREATEST(COALESCE(i.last_activity_at, i.updated_at), now())+ ELSE i.last_activity_atEND,updated_at = now()-WHERE id = $1 AND workspace_id = $3+WHERE i.id = $1 AND i.workspace_id = $3RETURNING id, workspace_id, title, description, status, priority, assignee_type, assignee_id, creator_type, creator_id, parent_issue_id, acceptance_criteria, context_refs, position, due_date, created_at, updated_at, number, project_id, origin_type, origin_id, first_executed_at, start_date, metadata, stage, properties, revision, last_activity_at`@@ -1696,6 +1729,10 @@ type UpdateIssueStatusParams struct {}// Workspace_id in the WHERE clause is a SQL-layer tenant guard; see DeleteIssue.+// Repositioning lives here rather than in the callers (GitHub sync, agent task+// completion) so a status write cannot land without one: an issue carrying its+// old column's rank into a new column is the bug this guards against. See the+// next_position CASE in UpdateIssue for the policy.func (q *Queries) UpdateIssueStatus(ctx context.Context, arg UpdateIssueStatusParams) (Issue, error) {row := q.db.QueryRow(ctx, updateIssueStatus, arg.ID, arg.Status, arg.WorkspaceID)var i Issuediff --git a/server/pkg/db/queries/issue.sql b/server/pkg/db/queries/issue.sqlindex 6f226c23dc..1fb9540951 100644--- a/server/pkg/db/queries/issue.sql+++ b/server/pkg/db/queries/issue.sql@@ -154,7 +154,34 @@ WITH candidate AS (COALESCE(sqlc.narg('priority')::text, i.priority) AS next_priority,sqlc.narg('assignee_type')::text AS next_assignee_type,sqlc.narg('assignee_id')::uuid AS next_assignee_id,- COALESCE(sqlc.narg('position')::double precision, i.position) AS next_position,+ CASE+ -- An explicit position wins. Cross-column drag-and-drop sends+ -- status and position together and means the slot it dropped on.+ WHEN sqlc.narg('position')::double precision IS NOT NULL+ THEN sqlc.narg('position')::double precision+ -- position ranks an issue *within* its (workspace, status)+ -- column, so it stops meaning anything the moment the column+ -- changes: the value that put the issue on top of Todo lands it+ -- below every hand-dragged issue in Done. Re-rank to the top of+ -- the destination, the same policy new issues get from+ -- NextTopPosition. Keep the two in sync.+ --+ -- Two status changes running at once can read the same MIN and+ -- claim one slot. Every position-ordered query carries a unique+ -- final key (created_at DESC, id DESC), so a tie leaves the+ -- relative order of simultaneous moves arbitrary but never+ -- unstable across pages. Creation avoids the tie by computing its+ -- min under the workspace counter lock; a status change holds no+ -- such lock and is not worth taking one for.+ WHEN i.status IS DISTINCT FROM COALESCE(sqlc.narg('status')::text, i.status)+ THEN (+ SELECT COALESCE(MIN(target.position), 0) - 1+ FROM issue AS target+ WHERE target.workspace_id = i.workspace_id+ AND target.status = sqlc.narg('status')::text+ )+ ELSE i.position+ END AS next_position,sqlc.narg('start_date')::date AS next_start_date,sqlc.narg('due_date')::date AS next_due_date,sqlc.narg('parent_issue_id')::uuid AS next_parent_issue_id,@@ -209,15 +236,25 @@ RETURNING i.*;-- name: UpdateIssueStatus :one-- Workspace_id in the WHERE clause is a SQL-layer tenant guard; see DeleteIssue.-UPDATE issue SET+-- Repositioning lives here rather than in the callers (GitHub sync, agent task+-- completion) so a status write cannot land without one: an issue carrying its+-- old column's rank into a new column is the bug this guards against. See the+-- next_position CASE in UpdateIssue for the policy.+UPDATE issue AS i SETstatus = $2,- revision = revision + CASE WHEN status IS DISTINCT FROM $2 THEN 1 ELSE 0 END,- last_activity_at = CASE WHEN status IS DISTINCT FROM $2- THEN GREATEST(COALESCE(last_activity_at, updated_at), now())- ELSE last_activity_at+ position = CASE WHEN i.status IS DISTINCT FROM $2 THEN (+ SELECT COALESCE(MIN(target.position), 0) - 1+ FROM issue AS target+ WHERE target.workspace_id = i.workspace_id+ AND target.status = $2+ ) ELSE i.position END,+ revision = i.revision + CASE WHEN i.status IS DISTINCT FROM $2 THEN 1 ELSE 0 END,+ last_activity_at = CASE WHEN i.status IS DISTINCT FROM $2+ THEN GREATEST(COALESCE(i.last_activity_at, i.updated_at), now())+ ELSE i.last_activity_atEND,updated_at = now()-WHERE id = $1 AND workspace_id = $3+WHERE i.id = $1 AND i.workspace_id = $3RETURNING *;-- name: CreateIssueWithOrigin :one