Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(task): enforce per-issue serial execution in task claiming (#330)
LinYushena80d61f8e1ae
diff --git a/apps/web/features/issues/components/agent-live-card.tsx b/apps/web/features/issues/components/agent-live-card.tsxindex 6917fc0f88..aff2a1240b 100644--- a/apps/web/features/issues/components/agent-live-card.tsx+++ b/apps/web/features/issues/components/agent-live-card.tsx@@ -194,10 +194,13 @@ export function AgentLiveCard({ issueId, agentName }: AgentLiveCardProps) {}, [issueId]),);- // Pick up new tasks+ // Pick up new tasks — skip if we're already showing an active task to avoid+ // replacing its timeline mid-execution (per-issue serialization in the+ // backend prevents this race, but this is a defensive safeguard).useWSEvent("task:dispatch",useCallback(() => {+ if (activeTask) return;api.getActiveTaskForIssue(issueId).then(({ task }) => {if (task) {setActiveTask(task);@@ -205,7 +208,7 @@ export function AgentLiveCard({ issueId, agentName }: AgentLiveCardProps) {seenSeqs.current.clear();}}).catch(() => {});- }, [issueId]),+ }, [issueId, activeTask]),);// Elapsed timediff --git a/server/pkg/db/generated/agent.sql.go b/server/pkg/db/generated/agent.sql.goindex befca00e48..069844723c 100644--- a/server/pkg/db/generated/agent.sql.go+++ b/server/pkg/db/generated/agent.sql.go@@ -59,6 +59,11 @@ SET status = 'dispatched', dispatched_at = now()WHERE id = (SELECT atq.id FROM agent_task_queue atqWHERE atq.agent_id = $1 AND atq.status = 'queued'+ AND NOT EXISTS (+ SELECT 1 FROM agent_task_queue active+ WHERE active.issue_id = atq.issue_id+ AND active.status IN ('dispatched', 'running')+ )ORDER BY atq.priority DESC, atq.created_at ASCLIMIT 1FOR UPDATE SKIP LOCKED@@ -66,6 +71,10 @@ WHERE id = (RETURNING id, agent_id, issue_id, status, priority, dispatched_at, started_at, completed_at, result, error, created_at, context, runtime_id, session_id, work_dir, trigger_comment_id`+// Claims the next queued task for an agent, enforcing per-issue serialization:+// a task is only claimable when no other task for the same issue is already+// dispatched or running. This guarantees serial execution within an issue+// while allowing parallel execution across different issues.func (q *Queries) ClaimAgentTask(ctx context.Context, agentID pgtype.UUID) (AgentTaskQueue, error) {row := q.db.QueryRow(ctx, claimAgentTask, agentID)var i AgentTaskQueuediff --git a/server/pkg/db/queries/agent.sql b/server/pkg/db/queries/agent.sqlindex 4511200b94..b55476b8a2 100644--- a/server/pkg/db/queries/agent.sql+++ b/server/pkg/db/queries/agent.sql@@ -60,11 +60,20 @@ SELECT * FROM agent_task_queueWHERE id = $1;-- name: ClaimAgentTask :one+-- Claims the next queued task for an agent, enforcing per-issue serialization:+-- a task is only claimable when no other task for the same issue is already+-- dispatched or running. This guarantees serial execution within an issue+-- while allowing parallel execution across different issues.UPDATE agent_task_queueSET status = 'dispatched', dispatched_at = now()WHERE id = (SELECT atq.id FROM agent_task_queue atqWHERE atq.agent_id = $1 AND atq.status = 'queued'+ AND NOT EXISTS (+ SELECT 1 FROM agent_task_queue active+ WHERE active.issue_id = atq.issue_id+ AND active.status IN ('dispatched', 'running')+ )ORDER BY atq.priority DESC, atq.created_at ASCLIMIT 1FOR UPDATE SKIP LOCKED