Skip to content

Commit 12bce12

Browse files
fix(coderd): ensure a newly created task can be fetched (#19670)
Due to how we currently label a workspace as a task, there is a delay between when a task workspace is created and when it is labelled as a task. This PR introduces fallback check for when a workspace does _not_ have `HasAITask` set. This fallback check tests to see if the special "AI Prompt" parameter is present in the workspace's build parameters.
1 parent 06cbb28 commit 12bce12

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

coderd/aitasks.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/coder/coder/v2/coderd/rbac/policy"
2424
"github.com/coder/coder/v2/coderd/searchquery"
2525
"github.com/coder/coder/v2/coderd/taskname"
26+
"github.com/coder/coder/v2/coderd/util/slice"
2627
"github.com/coder/coder/v2/codersdk"
2728
)
2829

@@ -440,8 +441,32 @@ func (api *API) taskGet(rw http.ResponseWriter, r *http.Request) {
440441
return
441442
}
442443
if data.builds[0].HasAITask == nil || !*data.builds[0].HasAITask {
443-
httpapi.ResourceNotFound(rw)
444-
return
444+
// TODO(DanielleMaywood):
445+
// This is a temporary workaround. When a task has just been created, but
446+
// not yet provisioned, the workspace build will not have `HasAITask` set.
447+
//
448+
// When we reach this code flow, it is _either_ because the workspace is
449+
// not a task, or it is a task that has not yet been provisioned. This
450+
// endpoint should rarely be called with a non-task workspace so we
451+
// should be fine with this extra database call to check if it has the
452+
// special "AI Task" parameter.
453+
parameters, err := api.Database.GetWorkspaceBuildParameters(ctx, data.builds[0].ID)
454+
if err != nil {
455+
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
456+
Message: "Internal error fetching workspace build parameters.",
457+
Detail: err.Error(),
458+
})
459+
return
460+
}
461+
462+
_, hasAITask := slice.Find(parameters, func(t database.WorkspaceBuildParameter) bool {
463+
return t.Name == codersdk.AITaskPromptParameterName
464+
})
465+
466+
if !hasAITask {
467+
httpapi.ResourceNotFound(rw)
468+
return
469+
}
445470
}
446471

447472
appStatus := codersdk.WorkspaceAppStatus{}

coderd/aitasks_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,6 @@ func TestTasks(t *testing.T) {
253253
{Name: codersdk.AITaskPromptParameterName, Value: wantPrompt},
254254
}
255255
})
256-
coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, workspace.LatestBuild.ID)
257256

258257
// Fetch the task by ID via experimental API and verify fields.
259258
exp := codersdk.NewExperimentalClient(client)

0 commit comments

Comments
 (0)