From 75f5b60eb6a674220249e7da73158faef44db289 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Mon, 23 Mar 2026 11:52:34 +0200 Subject: [PATCH] fix: return 409 Conflict instead of 502 when task agent is busy (#23424) The "Task app is not ready to accept input" error occurs when the agent responds successfully but its status is not "stable" (e.g. "running"). This is a state conflict, not a gateway error. 502 was semantically wrong because the gateway communication succeeded. 409 Conflict is correct because the request conflicts with the agent's current state. This is consistent with how authAndDoWithTaskAppClient already returns 409 for pending, initializing, and paused agent states. --- coderd/aitasks.go | 2 +- coderd/aitasks_test.go | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/coderd/aitasks.go b/coderd/aitasks.go index 967cf361a4..f0adb3b8ea 100644 --- a/coderd/aitasks.go +++ b/coderd/aitasks.go @@ -773,7 +773,7 @@ func (api *API) taskSend(rw http.ResponseWriter, r *http.Request) { } if statusResp.Status != agentapisdk.StatusStable { - return httperror.NewResponseError(http.StatusBadGateway, codersdk.Response{ + return httperror.NewResponseError(http.StatusConflict, codersdk.Response{ Message: "Task app is not ready to accept input.", Detail: fmt.Sprintf("Status: %s", statusResp.Status), }) diff --git a/coderd/aitasks_test.go b/coderd/aitasks_test.go index b16b2345f0..b1f703b912 100644 --- a/coderd/aitasks_test.go +++ b/coderd/aitasks_test.go @@ -789,6 +789,11 @@ func TestTasks(t *testing.T) { }) require.Error(t, err, "wanted error due to bad status") + var sdkErr *codersdk.Error + require.ErrorAs(t, err, &sdkErr) + require.Equal(t, http.StatusConflict, sdkErr.StatusCode()) + require.Contains(t, sdkErr.Message, "not ready to accept input") + statusResponse = agentapisdk.StatusStable //nolint:tparallel // Not intended to run in parallel.