Skip to content

Commit caf9000

Browse files
fix(coderd): allow fetching a newly created task
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 caf9000

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

coderd/aitasks.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,8 +440,36 @@ func (api *API) taskGet(rw http.ResponseWriter, r *http.Request) {
440440
return
441441
}
442442
if data.builds[0].HasAITask == nil || !*data.builds[0].HasAITask {
443-
httpapi.ResourceNotFound(rw)
444-
return
443+
// TODO(DanielleMaywood):
444+
// This is a temporary workaround. When a task has just been created, but
445+
// not yet provisioned, the workspace build will not have `HasAITask` set.
446+
//
447+
// When we reach this code flow, it is _either_ because the workspace is
448+
// not a task, or it is a task that has not yet been provisioned. This
449+
// endpoint should rarely be called with a non-task workspace so we
450+
// should be fine with this extra database call to check if it has the
451+
// special "AI Task" parameter.
452+
parameters, err := api.Database.GetWorkspaceBuildParameters(ctx, data.builds[0].ID)
453+
if err != nil {
454+
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
455+
Message: "Internal error fetching workspace build parameters.",
456+
Detail: err.Error(),
457+
})
458+
return
459+
}
460+
461+
hasAITask := false
462+
for _, parameter := range parameters {
463+
if parameter.Name == codersdk.AITaskPromptParameterName {
464+
hasAITask = true
465+
break
466+
}
467+
}
468+
469+
if !hasAITask {
470+
httpapi.ResourceNotFound(rw)
471+
return
472+
}
445473
}
446474

447475
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)