Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(tasks): create retries on the agent's current runtime (#10788)
xiaoke1234567862ef4370a540
diff --git a/server/internal/handler/daemon_test.go b/server/internal/handler/daemon_test.goindex 6235daaba1..e987b0c790 100644--- a/server/internal/handler/daemon_test.go+++ b/server/internal/handler/daemon_test.go@@ -3414,6 +3414,43 @@ func TestClaimTask_ChatAutoRetryFreshSessionReusesParentWorkdir(t *testing.T) {}}+// TestClaimTask_AutoRetryAfterRuntimeMoveStartsNewSession: the agent moved to+// another runtime while the parent ran there, so the retry is created on the+// agent's new runtime (#10717). Its claim there must not resume the session the+// old runtime recorded; the workdir is still offered best-effort, as for any+// cross-runtime prior (TestClaimTask_IssuePriorSessionRuntimeGuard).+func TestClaimTask_AutoRetryAfterRuntimeMoveStartsNewSession(t *testing.T) {+ if testHandler == nil {+ t.Skip("database not available")+ }+ ctx := context.Background()++ agentID, runtimeID, daemonID := createRuntimeGuardAgent(t, ctx)+ oldRuntimeID := createRuntimeGuardRuntime(t, ctx, "opencode")+ issueID := dbfx.Issue(t, "auto-retry after runtime move fixture", testutil.Cols{"status": "in_progress"})+ parentID := dbfx.Task(t, agentID, testutil.Cols{+ "runtime_id": oldRuntimeID,+ "issue_id": issueID,+ "status": "failed",+ "failure_reason": "timeout",+ "started_at": testutil.Raw("now() - interval '20 minutes'"),+ "completed_at": testutil.Raw("now() - interval '1 minute'"),+ "session_id": "old-runtime-session",+ "work_dir": "/tmp/old-runtime-workdir",+ "attempt": 1,+ "max_attempts": 2,+ })+ createAutoRetryForTest(t, ctx, parentID)++ task := claimTaskForRuntimeGuard(t, runtimeID, daemonID)+ if task.PriorSessionID != "" {+ t.Fatalf("PriorSessionID = %q, want empty (the old runtime's session cannot resume here)", task.PriorSessionID)+ }+ if task.PriorWorkDir != "/tmp/old-runtime-workdir" {+ t.Fatalf("PriorWorkDir = %q, want the parent's /tmp/old-runtime-workdir offered best-effort", task.PriorWorkDir)+ }+}+func TestClaimTask_ChatPriorSessionRuntimeGuard(t *testing.T) {if testHandler == nil {t.Skip("database not available")diff --git a/server/internal/service/retry_runtime_test.go b/server/internal/service/retry_runtime_test.gonew file mode 100644index 0000000000..91ac3fa8ff--- /dev/null+++ b/server/internal/service/retry_runtime_test.go@@ -0,0 +1,41 @@+package service++import (+ "context"+ "fmt"+ "testing"++ "github.com/multica-ai/multica/server/internal/testutil"+ db "github.com/multica-ai/multica/server/pkg/db/generated"+ "github.com/multica-ai/multica/server/pkg/dbid"+)++// A manual quick-create retry is claimable only on its agent's current+// runtime: claims fence on agent.runtime_id = task.runtime_id. Automatic+// retries are covered by TestRetryPreservesChildAcrossRuntimeBindingChanges.+func TestManualQuickCreateRetryFollowsAgentToItsNewRuntime(t *testing.T) {+ f, _, _, agent := wakeFixture(t)+ var oldRuntime string+ if err := f.Pool.QueryRow(context.Background(), "SELECT runtime_id::text FROM agent WHERE id=$1", agent).Scan(&oldRuntime); err != nil {+ t.Fatal(err)+ }+ newRuntime := f.Runtime(t, fmt.Sprintf("rt-rebind-%d", principalSeq.Add(1)))+ parent := f.Task(t, agent, testutil.Cols{+ "status": "failed", "runtime_id": oldRuntime,+ "context": testutil.Raw(`'{"type":"quick_create","prompt":"file a bug"}'::jsonb`),+ })+ f.Cleanup(t, "DELETE FROM agent_task_queue WHERE rerun_of_task_id=$1", parent)+ f.Exec(t, "UPDATE agent SET runtime_id=$2 WHERE id=$1", agent, newRuntime)++ child, err := f.q.CreateManualQuickCreateRetryTask(context.Background(), db.CreateManualQuickCreateRetryTaskParams{+ SourceTaskID: parseTestUUID(t, parent),+ ActorUserID: parseTestUUID(t, f.UserID),+ NewTaskID: dbid.NewV7(),+ })+ if err != nil {+ t.Fatal(err)+ }+ if child.RuntimeID != parseTestUUID(t, newRuntime) {+ t.Fatalf("quick-create retry runtime = %v, want the agent's new runtime %s", child.RuntimeID, newRuntime)+ }+}diff --git a/server/internal/service/runtime_retry_binding_test.go b/server/internal/service/runtime_retry_binding_test.goindex 0d06a3e2f3..ef8d9b774e 100644--- a/server/internal/service/runtime_retry_binding_test.go+++ b/server/internal/service/runtime_retry_binding_test.go@@ -15,8 +15,10 @@ import (db "github.com/multica-ai/multica/server/pkg/db/generated")-// A retry retains its parent's runtime even when the agent's binding changes.-// Enqueue must preserve that recovery attempt; claim enforces the current binding.+// A retry survives a change to the agent's runtime binding. A rebound agent's+// retry goes to the new runtime, which is the only one its claim fence admits+// (#10717); an unbound agent's retry keeps the parent's runtime until the agent+// is bound again.func TestRetryPreservesChildAcrossRuntimeBindingChanges(t *testing.T) {for _, tt := range []struct {name string@@ -95,9 +97,13 @@ func TestRetryPreservesChildAcrossRuntimeBindingChanges(t *testing.T) {if err != nil {t.Fatalf("load retry child: %v", err)}- if child.RuntimeID != util.MustParseUUID(runtimeA) || child.Status != "queued" || child.Attempt != 2 {- t.Fatalf("retry child = runtime:%s status:%q attempt:%d, want original runtime %s, queued, attempt 2",- util.UUIDToString(child.RuntimeID), child.Status, child.Attempt, runtimeA)+ wantRuntime := runtimeB+ if tt.unbound {+ wantRuntime = runtimeA+ }+ if child.RuntimeID != util.MustParseUUID(wantRuntime) || child.Status != "queued" || child.Attempt != 2 {+ t.Fatalf("retry child = runtime:%s status:%q attempt:%d, want runtime %s, queued, attempt 2",+ util.UUIDToString(child.RuntimeID), child.Status, child.Attempt, wantRuntime)}var parentStatus stringfx.QueryRow(t, `SELECT status FROM agent_task_queue WHERE id = $1`, parentID).Scan(&parentStatus)@@ -105,13 +111,15 @@ func TestRetryPreservesChildAcrossRuntimeBindingChanges(t *testing.T) {t.Fatalf("parent status = %q, want failed", parentStatus)}- claimParams := db.ClaimAgentTaskParams{- AgentID: util.MustParseUUID(agentID),- RuntimeID: util.MustParseUUID(runtimeA),- PrepareLeaseSecs: 60,- RuntimeStaleSecs: RuntimeClaimFreshnessSeconds,+ claimOn := func(runtimeID string) db.ClaimAgentTaskParams {+ return db.ClaimAgentTaskParams{+ AgentID: util.MustParseUUID(agentID),+ RuntimeID: util.MustParseUUID(runtimeID),+ PrepareLeaseSecs: 60,+ RuntimeStaleSecs: RuntimeClaimFreshnessSeconds,+ }}- if _, err := q.ClaimAgentTask(ctx, claimParams); !errors.Is(err, pgx.ErrNoRows) {+ if _, err := q.ClaimAgentTask(ctx, claimOn(runtimeA)); !errors.Is(err, pgx.ErrNoRows) {t.Fatalf("claim on old runtime with changed binding = %v, want no rows", err)}var childStatus string@@ -120,10 +128,12 @@ func TestRetryPreservesChildAcrossRuntimeBindingChanges(t *testing.T) {t.Fatalf("rejected claim changed retry status to %q, want queued", childStatus)}- fx.Exec(t, `UPDATE agent SET runtime_id = $1 WHERE id = $2`, runtimeA, agentID)- claimed, err := q.ClaimAgentTask(ctx, claimParams)+ if tt.unbound {+ fx.Exec(t, `UPDATE agent SET runtime_id = $1 WHERE id = $2`, runtimeA, agentID)+ }+ claimed, err := q.ClaimAgentTask(ctx, claimOn(wantRuntime))if err != nil {- t.Fatalf("claim retry after restoring original runtime: %v", err)+ t.Fatalf("claim retry on runtime %s: %v", wantRuntime, err)}if claimed.ID != childID {t.Fatalf("claimed task = %s, want existing retry %s", util.UUIDToString(claimed.ID), util.UUIDToString(childID))diff --git a/server/pkg/db/generated/agent.sql.go b/server/pkg/db/generated/agent.sql.goindex 664e3fdd58..40b7d5b4e1 100644--- a/server/pkg/db/generated/agent.sql.go+++ b/server/pkg/db/generated/agent.sql.go@@ -2635,18 +2635,19 @@ INSERT INTO agent_task_queue (originator_source, rerun_of_task_id, id)SELECT- p.agent_id, p.runtime_id, 'queued', p.priority, p.context,+ p.agent_id, COALESCE(a.runtime_id, p.runtime_id), 'queued', p.priority, p.context,TRUE, p.is_leader_task, p.squad_id,$1, $1,$2, $3,'direct_human', p.id, $4FROM agent_task_queue p+LEFT JOIN agent a ON a.id = p.agent_idWHERE p.id = $5AND p.status = 'failed'AND p.issue_id IS NULLAND p.chat_session_id IS NULLAND p.autopilot_run_id IS NULL- AND lock_task_owner_rows(p.agent_id, p.issue_id, p.runtime_id)+ AND lock_task_owner_rows(p.agent_id, p.issue_id, COALESCE(a.runtime_id, p.runtime_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, chat_session_id, autopilot_run_id, attempt, max_attempts, parent_task_id, failure_reason, trigger_summary, force_fresh_session, is_leader_task, wait_reason, initiator_user_id, handoff_note, prepare_lease_expires_at, squad_id, runtime_mcp_overlay, escalation_for_task_id, fire_at, originator_user_id, runtime_connected_apps, coalesced_comment_ids, delivered_comment_ids, chat_input_task_id, chat_finalize_deferred_at, originator_source, delegated_from_task_id, retry_of_task_id, rerun_of_task_id, rule_version_id, trigger_evidence_kind, trigger_evidence_ref_id, accountable_user_id, session_rollout_missing, retired_session_id, quick_actions_disabled, regenerate_quick_actions_for, branch_name, durable_work_dir, channel_context_revision, comment_thread_id, cancelled_by_type, cancelled_by_id, cancelled_by_name, issue_snapshot, workflow_step`@@ -2666,7 +2667,8 @@ type CreateManualQuickCreateRetryTaskParams struct {//// The caller locks and transfers the pending issue_source_context in the same// transaction. lock_task_owner_rows provides the usual workspace-teardown-// fence; a deleted workspace therefore yields pgx.ErrNoRows and no task.+// fence; a deleted workspace therefore yields pgx.ErrNoRows and no task. Like+// CreateRetryTask, the run is stamped with the agent's current runtime (#10717).func (q *Queries) CreateManualQuickCreateRetryTask(ctx context.Context, arg CreateManualQuickCreateRetryTaskParams) (AgentTaskQueue, error) {row := q.db.QueryRow(ctx, createManualQuickCreateRetryTask,arg.ActorUserID,@@ -2881,7 +2883,7 @@ INSERT INTO agent_task_queue (channel_context_revision, handoff_note, workflow_step, id)SELECT- p.agent_id, p.runtime_id, p.issue_id, p.chat_session_id, p.autopilot_run_id,+ p.agent_id, COALESCE(a.runtime_id, p.runtime_id), p.issue_id, p.chat_session_id, p.autopilot_run_id,CASE WHEN $2::timestamptz IS NOT NULL THEN 'deferred' ELSE 'queued' END,CASE WHEN p.chat_session_id IS NOT NULL THEN GREATEST(p.priority, 3) ELSE p.priority END,p.trigger_comment_id, p.coalesced_comment_ids, p.trigger_summary, p.context,@@ -2907,8 +2909,9 @@ SELECT-- Named new_task_id, not id: $1 above is the PARENT task's id.COALESCE($6::uuid, gen_random_uuid())FROM agent_task_queue p+LEFT JOIN agent a ON a.id = p.agent_idWHERE p.id = $1- AND lock_task_owner_rows(p.agent_id, p.issue_id, p.runtime_id)+ AND lock_task_owner_rows(p.agent_id, p.issue_id, COALESCE(a.runtime_id, p.runtime_id))ON CONFLICT (issue_id, agent_id, (COALESCE(comment_thread_id, '00000000-0000-0000-0000-000000000000'::uuid))) WHERE status IN ('queued', 'dispatched')OR (status = 'deferred' AND context->>'channel_issue_media_pending' = 'true')DO NOTHING@@ -2929,6 +2932,13 @@ type CreateRetryTaskParams struct {// Preserve a NULL chat_input_task_id for legacy/channel parents rather than// promoting untagged history to the task-owned input path.//+// The retry is stamped with the agent's current runtime, as an enqueue is.+// Claims fence on agent.runtime_id = task.runtime_id, so a retry stamped with a+// runtime the agent moved off while the parent ran would be claimable by neither+// daemon (#10717); UpdateAgent re-stamps only rows queued at switch time. An+// unbound agent keeps the parent's runtime. Resume needs no change here: claim+// resumes a session only on the runtime that recorded it.+//// Keep work_dir for every retry, but clear resume context for unsafe sessions.// Keep failure predicates aligned with resumeUnsafeFailureReason and resume lookups.// Composio uses the agent owner's connection; originator remains audit provenance.diff --git a/server/pkg/db/queries/agent.sql b/server/pkg/db/queries/agent.sqlindex d69f45b5cc..8d6d9fd882 100644--- a/server/pkg/db/queries/agent.sql+++ b/server/pkg/db/queries/agent.sql@@ -503,6 +503,13 @@ WHERE id = $1 AND issue_id IS NULL-- Preserve a NULL chat_input_task_id for legacy/channel parents rather than-- promoting untagged history to the task-owned input path.--+-- The retry is stamped with the agent's current runtime, as an enqueue is.+-- Claims fence on agent.runtime_id = task.runtime_id, so a retry stamped with a+-- runtime the agent moved off while the parent ran would be claimable by neither+-- daemon (#10717); UpdateAgent re-stamps only rows queued at switch time. An+-- unbound agent keeps the parent's runtime. Resume needs no change here: claim+-- resumes a session only on the runtime that recorded it.+---- Keep work_dir for every retry, but clear resume context for unsafe sessions.-- Keep failure predicates aligned with resumeUnsafeFailureReason and resume lookups.-- Composio uses the agent owner's connection; originator remains audit provenance.@@ -526,7 +533,7 @@ INSERT INTO agent_task_queue (channel_context_revision, handoff_note, workflow_step, id)SELECT- p.agent_id, p.runtime_id, p.issue_id, p.chat_session_id, p.autopilot_run_id,+ p.agent_id, COALESCE(a.runtime_id, p.runtime_id), p.issue_id, p.chat_session_id, p.autopilot_run_id,CASE WHEN sqlc.narg(fire_at)::timestamptz IS NOT NULL THEN 'deferred' ELSE 'queued' END,CASE WHEN p.chat_session_id IS NOT NULL THEN GREATEST(p.priority, 3) ELSE p.priority END,p.trigger_comment_id, p.coalesced_comment_ids, p.trigger_summary, p.context,@@ -552,8 +559,9 @@ SELECT-- Named new_task_id, not id: $1 above is the PARENT task's id.COALESCE(sqlc.narg('new_task_id')::uuid, gen_random_uuid())FROM agent_task_queue p+LEFT JOIN agent a ON a.id = p.agent_idWHERE p.id = $1- AND lock_task_owner_rows(p.agent_id, p.issue_id, p.runtime_id)+ AND lock_task_owner_rows(p.agent_id, p.issue_id, COALESCE(a.runtime_id, p.runtime_id))ON CONFLICT (issue_id, agent_id, (COALESCE(comment_thread_id, '00000000-0000-0000-0000-000000000000'::uuid))) WHERE status IN ('queued', 'dispatched')OR (status = 'deferred' AND context->>'channel_issue_media_pending' = 'true')DO NOTHING@@ -573,7 +581,8 @@ UPDATE agent_task_queue SET workflow_step = @workflow_step WHERE id = @id;---- The caller locks and transfers the pending issue_source_context in the same-- transaction. lock_task_owner_rows provides the usual workspace-teardown--- fence; a deleted workspace therefore yields pgx.ErrNoRows and no task.+-- fence; a deleted workspace therefore yields pgx.ErrNoRows and no task. Like+-- CreateRetryTask, the run is stamped with the agent's current runtime (#10717).INSERT INTO agent_task_queue (agent_id, runtime_id, status, priority, context,force_fresh_session, is_leader_task, squad_id,@@ -582,18 +591,19 @@ INSERT INTO agent_task_queue (originator_source, rerun_of_task_id, id)SELECT- p.agent_id, p.runtime_id, 'queued', p.priority, p.context,+ p.agent_id, COALESCE(a.runtime_id, p.runtime_id), 'queued', p.priority, p.context,TRUE, p.is_leader_task, p.squad_id,sqlc.arg(actor_user_id), sqlc.arg(actor_user_id),sqlc.narg(runtime_mcp_overlay), sqlc.narg(runtime_connected_apps),'direct_human', p.id, sqlc.arg(new_task_id)FROM agent_task_queue p+LEFT JOIN agent a ON a.id = p.agent_idWHERE p.id = sqlc.arg(source_task_id)AND p.status = 'failed'AND p.issue_id IS NULLAND p.chat_session_id IS NULLAND p.autopilot_run_id IS NULL- AND lock_task_owner_rows(p.agent_id, p.issue_id, p.runtime_id)+ AND lock_task_owner_rows(p.agent_id, p.issue_id, COALESCE(a.runtime_id, p.runtime_id))RETURNING *;-- name: DeleteUnstartedQuickCreateRetryTask :execrows