Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8af30e6

Browse files
committed
feat(coderd): use task data model when fetching a task
Updatescoder/internal#976
1 parent517f72c commit8af30e6

File tree

3 files changed

+49
-78
lines changed

3 files changed

+49
-78
lines changed

‎cli/exp_task_status_test.go‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ import (
2424
funcTest_TaskStatus(t*testing.T) {
2525
t.Parallel()
2626

27-
t.Skip("TODO(mafredri): Remove, fixed down-stack!")
28-
2927
for_,tc:=range []struct {
3028
args []string
3129
expectOutputstring
@@ -90,6 +88,7 @@ func Test_TaskStatus(t *testing.T) {
9088
Healthy:true,
9189
},
9290
WorkspaceAgentLifecycle:ptr.Ref(codersdk.WorkspaceAgentLifecycleReady),
91+
Status:codersdk.TaskStatusActive,
9392
})
9493
default:
9594
t.Errorf("unexpected path: %s",r.URL.Path)
@@ -125,6 +124,7 @@ STATE CHANGED STATUS HEALTHY STATE MESSAGE
125124
Healthy:true,
126125
},
127126
WorkspaceAgentLifecycle:ptr.Ref(codersdk.WorkspaceAgentLifecycleReady),
127+
Status:codersdk.TaskStatusPending,
128128
})
129129
case1:
130130
httpapi.Write(ctx,w,http.StatusOK, codersdk.Task{
@@ -136,6 +136,7 @@ STATE CHANGED STATUS HEALTHY STATE MESSAGE
136136
},
137137
WorkspaceAgentLifecycle:ptr.Ref(codersdk.WorkspaceAgentLifecycleReady),
138138
UpdatedAt:now.Add(-4*time.Second),
139+
Status:codersdk.TaskStatusActive,
139140
})
140141
case2:
141142
httpapi.Write(ctx,w,http.StatusOK, codersdk.Task{
@@ -152,6 +153,7 @@ STATE CHANGED STATUS HEALTHY STATE MESSAGE
152153
Timestamp:now.Add(-3*time.Second),
153154
Message:"Reticulating splines...",
154155
},
156+
Status:codersdk.TaskStatusActive,
155157
})
156158
case3:
157159
httpapi.Write(ctx,w,http.StatusOK, codersdk.Task{
@@ -168,6 +170,7 @@ STATE CHANGED STATUS HEALTHY STATE MESSAGE
168170
Timestamp:now.Add(-2*time.Second),
169171
Message:"Splines reticulated successfully!",
170172
},
173+
Status:codersdk.TaskStatusActive,
171174
})
172175
default:
173176
httpapi.InternalServerError(w,xerrors.New("too many calls!"))
@@ -188,16 +191,18 @@ STATE CHANGED STATUS HEALTHY STATE MESSAGE
188191
"owner_name": "",
189192
"name": "",
190193
"template_id": "00000000-0000-0000-0000-000000000000",
194+
"template_version_id": "00000000-0000-0000-0000-000000000000",
191195
"template_name": "",
192196
"template_display_name": "",
193197
"template_icon": "",
194198
"workspace_id": null,
199+
"workspace_status": "running",
195200
"workspace_agent_id": null,
196201
"workspace_agent_lifecycle": null,
197202
"workspace_agent_health": null,
198203
"workspace_app_id": null,
199204
"initial_prompt": "",
200-
"status": "running",
205+
"status": "active",
201206
"current_state": {
202207
"timestamp": "2025-08-26T12:34:57Z",
203208
"state": "working",
@@ -226,6 +231,7 @@ STATE CHANGED STATUS HEALTHY STATE MESSAGE
226231
Timestamp:ts.Add(time.Second),
227232
Message:"Thinking furiously...",
228233
},
234+
Status:codersdk.TaskStatusActive,
229235
})
230236
default:
231237
t.Errorf("unexpected path: %s",r.URL.Path)

‎coderd/aitasks.go‎

Lines changed: 12 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"github.com/coder/coder/v2/coderd/rbac/policy"
2626
"github.com/coder/coder/v2/coderd/searchquery"
2727
"github.com/coder/coder/v2/coderd/taskname"
28-
"github.com/coder/coder/v2/coderd/util/slice"
2928
"github.com/coder/coder/v2/codersdk"
3029

3130
aiagentapi"github.com/coder/agentapi-sdk-go"
@@ -604,25 +603,22 @@ func (api *API) tasksList(rw http.ResponseWriter, r *http.Request) {
604603
func (api*API)taskGet(rw http.ResponseWriter,r*http.Request) {
605604
ctx:=r.Context()
606605
apiKey:=httpmw.APIKey(r)
606+
task:=httpmw.TaskParam(r)
607607

608-
idStr:=chi.URLParam(r,"id")
609-
taskID,err:=uuid.Parse(idStr)
610-
iferr!=nil {
611-
httpapi.Write(ctx,rw,http.StatusBadRequest, codersdk.Response{
612-
Message:fmt.Sprintf("Invalid UUID %q for task ID.",idStr),
608+
if!task.WorkspaceID.Valid {
609+
httpapi.Write(ctx,rw,http.StatusInternalServerError, codersdk.Response{
610+
Message:"Internal error fetching task.",
611+
Detail:"Task workspace ID is invalid.",
613612
})
614613
return
615614
}
616615

617-
// For now, taskID = workspaceID, once we have a task data model in
618-
// the DB, we can change this lookup.
619-
workspaceID:=taskID
620-
workspace,err:=api.Database.GetWorkspaceByID(ctx,workspaceID)
621-
ifhttpapi.Is404Error(err) {
622-
httpapi.ResourceNotFound(rw)
623-
return
624-
}
616+
workspace,err:=api.Database.GetWorkspaceByID(ctx,task.WorkspaceID.UUID)
625617
iferr!=nil {
618+
ifhttpapi.Is404Error(err) {
619+
httpapi.ResourceNotFound(rw)
620+
return
621+
}
626622
httpapi.Write(ctx,rw,http.StatusInternalServerError, codersdk.Response{
627623
Message:"Internal error fetching workspace.",
628624
Detail:err.Error(),
@@ -642,34 +638,6 @@ func (api *API) taskGet(rw http.ResponseWriter, r *http.Request) {
642638
httpapi.ResourceNotFound(rw)
643639
return
644640
}
645-
ifdata.builds[0].HasAITask==nil||!*data.builds[0].HasAITask {
646-
// TODO(DanielleMaywood):
647-
// This is a temporary workaround. When a task has just been created, but
648-
// not yet provisioned, the workspace build will not have `HasAITask` set.
649-
//
650-
// When we reach this code flow, it is _either_ because the workspace is
651-
// not a task, or it is a task that has not yet been provisioned. This
652-
// endpoint should rarely be called with a non-task workspace so we
653-
// should be fine with this extra database call to check if it has the
654-
// special "AI Task" parameter.
655-
parameters,err:=api.Database.GetWorkspaceBuildParameters(ctx,data.builds[0].ID)
656-
iferr!=nil {
657-
httpapi.Write(ctx,rw,http.StatusInternalServerError, codersdk.Response{
658-
Message:"Internal error fetching workspace build parameters.",
659-
Detail:err.Error(),
660-
})
661-
return
662-
}
663-
664-
_,hasAITask:=slice.Find(parameters,func(t database.WorkspaceBuildParameter)bool {
665-
returnt.Name==codersdk.AITaskPromptParameterName
666-
})
667-
668-
if!hasAITask {
669-
httpapi.ResourceNotFound(rw)
670-
return
671-
}
672-
}
673641

674642
appStatus:= codersdk.WorkspaceAppStatus{}
675643
iflen(data.appStatuses)>0 {
@@ -692,16 +660,8 @@ func (api *API) taskGet(rw http.ResponseWriter, r *http.Request) {
692660
return
693661
}
694662

695-
tasks,err:=api.tasksFromWorkspaces(ctx, []codersdk.Workspace{ws})
696-
iferr!=nil {
697-
httpapi.Write(ctx,rw,http.StatusInternalServerError, codersdk.Response{
698-
Message:"Internal error fetching task prompt and state.",
699-
Detail:err.Error(),
700-
})
701-
return
702-
}
703-
704-
httpapi.Write(ctx,rw,http.StatusOK,tasks[0])
663+
taskResp:=taskFromDBTaskAndWorkspace(task,ws)
664+
httpapi.Write(ctx,rw,http.StatusOK,taskResp)
705665
}
706666

707667
// @Summary Delete AI task by ID

‎coderd/aitasks_test.go‎

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -279,34 +279,37 @@ func TestTasks(t *testing.T) {
279279
t.Run("Get",func(t*testing.T) {
280280
t.Parallel()
281281

282-
t.Skip("TODO(mafredri): Remove, fixed down-stack!")
283-
284282
var (
285283
client,db=coderdtest.NewWithDatabase(t,&coderdtest.Options{IncludeProvisionerDaemon:true})
286284
ctx=testutil.Context(t,testutil.WaitLong)
287285
user=coderdtest.CreateFirstUser(t,client)
288286
template=createAITemplate(t,client,user)
289-
// Create a workspace (task) with a specific prompt.
290287
wantPrompt="review my code"
291-
workspace=coderdtest.CreateWorkspace(t,client,template.ID,func(req*codersdk.CreateWorkspaceRequest) {
292-
req.RichParameterValues= []codersdk.WorkspaceBuildParameter{
293-
{Name:codersdk.AITaskPromptParameterName,Value:wantPrompt},
294-
}
295-
})
288+
exp=codersdk.NewExperimentalClient(client)
296289
)
297290

298-
coderdtest.AwaitWorkspaceBuildJobCompleted(t,client,workspace.LatestBuild.ID)
299-
ws:=coderdtest.MustWorkspace(t,client,workspace.ID)
291+
task,err:=exp.CreateTask(ctx,"me", codersdk.CreateTaskRequest{
292+
TemplateVersionID:template.ActiveVersionID,
293+
Input:wantPrompt,
294+
})
295+
require.NoError(t,err)
296+
require.True(t,task.WorkspaceID.Valid)
297+
298+
// Get the workspace and wait for it to be ready.
299+
ws,err:=client.Workspace(ctx,task.WorkspaceID.UUID)
300+
require.NoError(t,err)
301+
coderdtest.AwaitWorkspaceBuildJobCompleted(t,client,ws.LatestBuild.ID)
302+
ws=coderdtest.MustWorkspace(t,client,task.WorkspaceID.UUID)
300303
// Assert invariant: the workspace has exactly one resource with one agent with one app.
301304
require.Len(t,ws.LatestBuild.Resources,1)
302305
require.Len(t,ws.LatestBuild.Resources[0].Agents,1)
303306
agentID:=ws.LatestBuild.Resources[0].Agents[0].ID
304307
taskAppID:=ws.LatestBuild.Resources[0].Agents[0].Apps[0].ID
305308

306309
// Insert an app status for the workspace
307-
_,err:=db.InsertWorkspaceAppStatus(dbauthz.AsSystemRestricted(ctx), database.InsertWorkspaceAppStatusParams{
310+
_,err=db.InsertWorkspaceAppStatus(dbauthz.AsSystemRestricted(ctx), database.InsertWorkspaceAppStatusParams{
308311
ID:uuid.New(),
309-
WorkspaceID:workspace.ID,
312+
WorkspaceID:task.WorkspaceID.UUID,
310313
CreatedAt:dbtime.Now(),
311314
AgentID:agentID,
312315
AppID:taskAppID,
@@ -316,31 +319,33 @@ func TestTasks(t *testing.T) {
316319
require.NoError(t,err)
317320

318321
// Fetch the task by ID via experimental API and verify fields.
319-
exp:=codersdk.NewExperimentalClient(client)
320-
task,err:=exp.TaskByID(ctx,workspace.ID)
322+
updated,err:=exp.TaskByID(ctx,task.ID)
321323
require.NoError(t,err)
322324

323-
assert.Equal(t,workspace.ID,task.ID,"task ID should match workspace ID")
324-
assert.Equal(t,workspace.Name,task.Name,"task name should map from workspace name")
325-
assert.Equal(t,wantPrompt,task.InitialPrompt,"task prompt should match the AI Prompt parameter")
326-
assert.Equal(t,workspace.ID,task.WorkspaceID.UUID,"workspace id should match")
327-
assert.NotEmpty(t,task.WorkspaceStatus,"task status should not be empty")
325+
assert.Equal(t,task.ID,updated.ID,"task ID should match")
326+
assert.Equal(t,task.Name,updated.Name,"task name should match")
327+
assert.Equal(t,wantPrompt,updated.InitialPrompt,"task prompt should match the AI Prompt parameter")
328+
assert.Equal(t,task.WorkspaceID.UUID,updated.WorkspaceID.UUID,"workspace id should match")
329+
assert.Equal(t,ws.LatestBuild.BuildNumber,updated.WorkspaceBuildNumber,"workspace build number should match")
330+
assert.Equal(t,agentID,updated.WorkspaceAgentID.UUID,"workspace agent id should match")
331+
assert.Equal(t,taskAppID,updated.WorkspaceAppID.UUID,"workspace app id should match")
332+
assert.NotEmpty(t,updated.WorkspaceStatus,"task status should not be empty")
328333

329334
// Stop the workspace
330-
coderdtest.MustTransitionWorkspace(t,client,workspace.ID,codersdk.WorkspaceTransitionStart,codersdk.WorkspaceTransitionStop)
335+
coderdtest.MustTransitionWorkspace(t,client,task.WorkspaceID.UUID,codersdk.WorkspaceTransitionStart,codersdk.WorkspaceTransitionStop)
331336

332337
// Verify that the previous status still remains
333-
updated,err:=exp.TaskByID(ctx,workspace.ID)
338+
updated,err=exp.TaskByID(ctx,task.ID)
334339
require.NoError(t,err)
335340
assert.NotNil(t,updated.CurrentState,"current state should not be nil")
336341
assert.Equal(t,"all done",updated.CurrentState.Message)
337342
assert.Equal(t,codersdk.TaskStateComplete,updated.CurrentState.State)
338343

339344
// Start the workspace again
340-
coderdtest.MustTransitionWorkspace(t,client,workspace.ID,codersdk.WorkspaceTransitionStop,codersdk.WorkspaceTransitionStart)
345+
coderdtest.MustTransitionWorkspace(t,client,task.WorkspaceID.UUID,codersdk.WorkspaceTransitionStop,codersdk.WorkspaceTransitionStart)
341346

342347
// Verify that the status from the previous build is no longer present
343-
updated,err=exp.TaskByID(ctx,workspace.ID)
348+
updated,err=exp.TaskByID(ctx,task.ID)
344349
require.NoError(t,err)
345350
assert.Nil(t,updated.CurrentState,"current state should be nil")
346351
})

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp