Skip to content

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

Go

MUL-7847 fix(task): comment terminal failure on issue when sweeper fails task (#9018)

Younes Beriane53c0088f1789
diff --git a/server/internal/service/delegated_failure_recovery_test.go b/server/internal/service/delegated_failure_recovery_test.go
index 15bf76325e..c90a0734ff 100644
--- a/server/internal/service/delegated_failure_recovery_test.go
+++ b/server/internal/service/delegated_failure_recovery_test.go
@@ -1030,6 +1030,73 @@ func TestHandleFailedTasksFinalDelegatedFailureWakesCoordinator(t *testing.T) {
}
}
+// insertOrphanedWorkerTask seeds a non-delegated task on the worker's issue
+// that orphan recovery has just failed with runtime_recovery.
+func (f *delegatedFailureFixture) insertOrphanedWorkerTask(t *testing.T, attempt, maxAttempts int32) db.AgentTaskQueue {
+ t.Helper()
+ taskID := dbfx.New(f.pool, f.workspaceID, f.userID).Task(t, f.worker, dbfx.Cols{
+ "runtime_id": f.runtimeID,
+ "issue_id": f.workerIssue,
+ "status": "failed",
+ "attempt": attempt,
+ "max_attempts": maxAttempts,
+ "originator_user_id": f.userID,
+ "accountable_user_id": f.userID,
+ "originator_source": "direct_human",
+ "failure_reason": "runtime_recovery",
+ "error": "daemon restarted while task was in flight",
+ "completed_at": dbfx.Raw("now()"),
+ })
+ task, err := db.New(f.pool).GetAgentTask(context.Background(), util.MustParseUUID(taskID))
+ if err != nil {
+ t.Fatalf("load orphaned task: %v", err)
+ }
+ return task
+}
+
+func TestHandleFailedTasksTerminalFailureCommentsOnTaskIssue(t *testing.T) {
+ f, svc := seedDelegatedFailureFixture(t)
+ ctx := context.Background()
+ failed := f.insertOrphanedWorkerTask(t, 2, 2)
+
+ if retried := svc.HandleFailedTasks(ctx, []db.AgentTaskQueue{failed}); retried != 0 {
+ t.Fatalf("HandleFailedTasks retried = %d, want 0", retried)
+ }
+
+ var comments int
+ var content string
+ if err := f.pool.QueryRow(ctx, `
+ SELECT count(*), COALESCE(max(content), '')
+ FROM comment
+ WHERE issue_id = $1 AND type = 'system' AND source_task_id = $2`, f.workerIssue, failed.ID).
+ Scan(&comments, &content); err != nil {
+ t.Fatalf("read failure comment: %v", err)
+ }
+ if comments != 1 || content != "daemon restarted while task was in flight" {
+ t.Fatalf("terminal failure comments = %d content %q, want one system comment with the task error", comments, content)
+ }
+}
+
+func TestHandleFailedTasksRetryPendingDoesNotCommentOnTaskIssue(t *testing.T) {
+ f, svc := seedDelegatedFailureFixture(t)
+ ctx := context.Background()
+ failed := f.insertOrphanedWorkerTask(t, 1, 2)
+
+ if retried := svc.HandleFailedTasks(ctx, []db.AgentTaskQueue{failed}); retried != 1 {
+ t.Fatalf("HandleFailedTasks retried = %d, want 1", retried)
+ }
+
+ var comments int
+ if err := f.pool.QueryRow(ctx, `
+ SELECT count(*) FROM comment WHERE issue_id = $1 AND type = 'system' AND source_task_id = $2`,
+ f.workerIssue, failed.ID).Scan(&comments); err != nil {
+ t.Fatalf("count failure comments: %v", err)
+ }
+ if comments != 0 {
+ t.Fatalf("retry-pending failure comments = %d, want 0", comments)
+ }
+}
+
func TestFailTaskRetryPendingDoesNotWakeCoordinator(t *testing.T) {
f, svc := seedDelegatedFailureFixture(t)
ctx := context.Background()
diff --git a/server/internal/service/task.go b/server/internal/service/task.go
index e2bf651cad..97b76a8cc9 100644
--- a/server/internal/service/task.go
+++ b/server/internal/service/task.go
@@ -6056,9 +6056,9 @@ func (s *TaskService) terminateTasksInTx(ctx context.Context, fail func(*db.Quer
// task and isn't being retried) resetting the issue back to todo so the
// daemon can pick it up again.
//
-// All callers that surface a task as failed — sweepers, FailTask,
-// recover-orphans — funnel through here so the same UI-consistency
-// guarantees apply on every code path.
+// The bulk failure paths — sweepers and recover-orphans — funnel through
+// here so the same UI-consistency guarantees apply on each of them.
+// FailTaskWithTransition runs its own failure side effects.
func (s *TaskService) HandleFailedTasks(ctx context.Context, tasks []db.AgentTaskQueue) int {
if len(tasks) == 0 {
return 0
@@ -6088,6 +6088,11 @@ func (s *TaskService) HandleFailedTasks(ctx context.Context, tasks []db.AgentTas
"error", err,
)
}
+ // Same terminal-failure comment FailTaskWithTransition posts, so
+ // swept tasks are not silent on their issue.
+ if t.IssueID.Valid && t.Error.String != "" {
+ s.createAgentComment(ctx, t.IssueID, t.AgentID, redact.Text(t.Error.String), "system", t.TriggerCommentID, t.ID)
+ }
}
failureReason := "agent_error"