Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(tasks): leave the parent's session behind when a retry changes runtime (#10859)

pseudoyu47b8fc3af7c2
diff --git a/server/internal/handler/agent_runtime_switch_tasks_test.go b/server/internal/handler/agent_runtime_switch_tasks_test.go
index 9a01b07689..91bfb9f1f4 100644
--- a/server/internal/handler/agent_runtime_switch_tasks_test.go
+++ b/server/internal/handler/agent_runtime_switch_tasks_test.go
@@ -9,6 +9,7 @@ import (
"time"
"github.com/multica-ai/multica/server/internal/testutil"
+ db "github.com/multica-ai/multica/server/pkg/db/generated"
)
// taskRuntimes reads each task's runtime_id, keyed by task id.
@@ -115,3 +116,67 @@ func TestUpdateAgent_RuntimeSwitch_ForbiddenLeavesTasks(t *testing.T) {
t.Fatalf("queued task runtime = %s, want unchanged %s", got, oldRuntimeID)
}
}
+
+// A provider session belongs to the runtime that ran it. A retry that moves to
+// the agent's new runtime must not carry the old one: if that attempt ends
+// before it reports a session of its own, the old session would be filed under
+// the new runtime and the next claim there would be told to resume it.
+func TestUpdateAgent_RuntimeSwitch_RetryLeavesSessionOnOldRuntime(t *testing.T) {
+ if testHandler == nil {
+ t.Skip("database not available")
+ }
+ ctx := context.Background()
+
+ oldRuntimeID := dbfx.Runtime(t, "runtime-switch-session-old")
+ newRuntimeID := dbfx.Runtime(t, "runtime-switch-session-new")
+ agentID := dbfx.Agent(t, "runtime-switch-session-agent", oldRuntimeID)
+ issueID := dbfx.Issue(t, "runtime switch retry session", testutil.Cols{
+ "assignee_type": "agent",
+ "assignee_id": agentID,
+ })
+ t.Cleanup(func() {
+ testPool.Exec(context.Background(), `DELETE FROM agent_task_queue WHERE issue_id = $1`, issueID)
+ })
+ const oldSession = "session-from-old-runtime"
+ runningID := dbfx.Task(t, agentID, testutil.Cols{
+ "runtime_id": oldRuntimeID, "issue_id": issueID, "status": "running",
+ "dispatched_at": testutil.Raw("now()"), "started_at": testutil.Raw("now()"),
+ "attempt": 1, "max_attempts": 2, "session_id": oldSession,
+ })
+
+ if w := patchAgentRuntime(t, agentID, newRuntimeID); w.Code != http.StatusOK {
+ t.Fatalf("switch runtime: expected 200, got %d: %s", w.Code, w.Body.String())
+ }
+ if _, err := testHandler.TaskService.FailTask(ctx, parseUUID(runningID),
+ "run died", "", "", "", "timeout", false, "", ""); err != nil {
+ t.Fatalf("fail the run on the old runtime: %v", err)
+ }
+
+ // The retry starts on the new runtime and dies before it reports a session.
+ var retryID string
+ if err := testPool.QueryRow(ctx, `
+ UPDATE agent_task_queue SET status = 'running', dispatched_at = now(), started_at = now()
+ WHERE retry_of_task_id = $1 RETURNING id::text`, runningID,
+ ).Scan(&retryID); err != nil {
+ t.Fatalf("start retry child: %v", err)
+ }
+ if got := taskRuntimes(t, retryID)[retryID]; got != newRuntimeID {
+ t.Fatalf("retry runtime = %s, want the agent's new runtime %s", got, newRuntimeID)
+ }
+ if _, err := testHandler.TaskService.FailTask(ctx, parseUUID(retryID),
+ "runtime went away", "", "", "", "timeout", false, "", ""); err != nil {
+ t.Fatalf("fail the retry on the new runtime: %v", err)
+ }
+
+ prior, err := testHandler.Queries.GetLastTaskSession(ctx, db.GetLastTaskSessionParams{
+ AgentID: parseUUID(agentID),
+ IssueID: parseUUID(issueID),
+ })
+ if err != nil {
+ t.Fatalf("GetLastTaskSession: %v", err)
+ }
+ if prior.SessionID.String != oldSession || uuidToString(prior.RuntimeID) != oldRuntimeID {
+ t.Fatalf("resume lookup = session %q on runtime %s, want %q still on the old runtime %s (new %s)",
+ prior.SessionID.String, uuidToString(prior.RuntimeID), oldSession, oldRuntimeID, newRuntimeID)
+ }
+}
diff --git a/server/pkg/db/generated/agent.sql.go b/server/pkg/db/generated/agent.sql.go
index 16e919fae0..771d211037 100644
--- a/server/pkg/db/generated/agent.sql.go
+++ b/server/pkg/db/generated/agent.sql.go
@@ -2910,7 +2910,9 @@ SELECT
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,
- CASE WHEN p.failure_reason IS NOT DISTINCT FROM 'codex_semantic_inactivity' THEN NULL ELSE p.session_id END,
+ CASE WHEN p.failure_reason IS NOT DISTINCT FROM 'codex_semantic_inactivity'
+ OR COALESCE(a.runtime_id, p.runtime_id) IS DISTINCT FROM p.runtime_id THEN NULL
+ ELSE p.session_id END,
p.work_dir,
p.attempt + 1, COALESCE($3::int, p.max_attempts), p.id,
p.failure_reason IS NOT DISTINCT FROM 'codex_semantic_inactivity',
@@ -2959,8 +2961,10 @@ type CreateRetryTaskParams struct {
// 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.
+// unbound agent keeps the parent's runtime. A retry that moves leaves the
+// parent's session behind: it belongs to the runtime that recorded it, and an
+// attempt that ends before reporting its own would file it under the new one,
+// where the next claim would be told to resume it.
//
// Keep work_dir for every retry, but clear resume context for unsafe sessions.
// Keep failure predicates aligned with resumeUnsafeFailureReason and resume lookups.
diff --git a/server/pkg/db/queries/agent.sql b/server/pkg/db/queries/agent.sql
index 4eae436de4..e9482e8ffd 100644
--- a/server/pkg/db/queries/agent.sql
+++ b/server/pkg/db/queries/agent.sql
@@ -539,8 +539,10 @@ WHERE id = $1 AND issue_id IS NULL
-- 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.
+-- unbound agent keeps the parent's runtime. A retry that moves leaves the
+-- parent's session behind: it belongs to the runtime that recorded it, and an
+-- attempt that ends before reporting its own would file it under the new one,
+-- where the next claim would be told to resume it.
--
-- Keep work_dir for every retry, but clear resume context for unsafe sessions.
-- Keep failure predicates aligned with resumeUnsafeFailureReason and resume lookups.
@@ -569,7 +571,9 @@ SELECT
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,
- CASE WHEN p.failure_reason IS NOT DISTINCT FROM 'codex_semantic_inactivity' THEN NULL ELSE p.session_id END,
+ CASE WHEN p.failure_reason IS NOT DISTINCT FROM 'codex_semantic_inactivity'
+ OR COALESCE(a.runtime_id, p.runtime_id) IS DISTINCT FROM p.runtime_id THEN NULL
+ ELSE p.session_id END,
p.work_dir,
p.attempt + 1, COALESCE(sqlc.narg(max_attempts)::int, p.max_attempts), p.id,
p.failure_reason IS NOT DISTINCT FROM 'codex_semantic_inactivity',