Skip to content

feat: persist AI task state in template imports & workspace builds #18449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Jun 24, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
9fa7a93
chore: go.mod
dannykopping Jun 18, 2025
d01a050
feat: proto & db changes to persist coder_ai_task state
dannykopping Jun 18, 2025
4383350
feat: store coder_ai_task resource state
dannykopping Jun 18, 2025
957fb08
chore: rename ai_tasks_sidebar_app_id to ai_task_sidebar_app_id
dannykopping Jun 19, 2025
91c8f0c
chore: update to d9b0f892f9c2b3768b9647b089900b0628cb7828, prompt nam…
dannykopping Jun 19, 2025
efe5da9
chore: update provisioner API version comments
dannykopping Jun 19, 2025
5e0d28a
chore: move prompt param validation to ConvertState with other valida…
dannykopping Jun 19, 2025
57da39f
chore: unify tf testdata versions & fix tests
dannykopping Jun 20, 2025
c6c7b5c
chore: dbauthz
dannykopping Jun 20, 2025
4816ed5
chore: fix TestWorkspaceAgentAppHealth test after agent ID field now …
dannykopping Jun 20, 2025
e48df2e
chore: add tests
dannykopping Jun 20, 2025
0b2b732
chore: correct bad rebase
dannykopping Jun 23, 2025
5edee05
chore: undo changes to terraform testdata, so as to not complicate th…
dannykopping Jun 23, 2025
3614a5b
chore: fixing tests
dannykopping Jun 23, 2025
d7e47f9
chore: ensure sidebar app ID must be set when has_ai_task is set
dannykopping Jun 23, 2025
f4111df
chore: test multiple coder_ai_tasks cannot be configured
dannykopping Jun 23, 2025
cc0e290
chore: fix CI
dannykopping Jun 23, 2025
138d865
chore: minor refactor
dannykopping Jun 23, 2025
1aba76b
chore: add clarifying/defensive parentheses
dannykopping Jun 23, 2025
6454ffe
chore: fix conflict
dannykopping Jun 23, 2025
bce4a88
chore: review comments
dannykopping Jun 24, 2025
9a453e8
Merge branch 'main' of github.com:/coder/coder into dk/coder-ai-task-res
dannykopping Jun 24, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: proto & db changes to persist coder_ai_task state
Signed-off-by: Danny Kopping <dannykopping@gmail.com>
  • Loading branch information
dannykopping committed Jun 23, 2025
commit d01a050fae20b7d01c1ea2f932f1ec2a05b86813
29 changes: 29 additions & 0 deletions coderd/database/dbauthz/dbauthz.go
Original file line number Diff line number Diff line change
Expand Up @@ -4535,6 +4535,28 @@ func (q *querier) UpdateTemplateScheduleByID(ctx context.Context, arg database.U
return update(q.log, q.auth, fetch, q.db.UpdateTemplateScheduleByID)(ctx, arg)
}

func (q *querier) UpdateTemplateVersionAITaskByJobID(ctx context.Context, arg database.UpdateTemplateVersionAITaskByJobIDParams) error {
// An actor is allowed to update the template version AI task flag if they are authorized to update the template.
tv, err := q.db.GetTemplateVersionByJobID(ctx, arg.JobID)
if err != nil {
return err
}
var obj rbac.Objecter
if !tv.TemplateID.Valid {
obj = rbac.ResourceTemplate.InOrg(tv.OrganizationID)
} else {
tpl, err := q.db.GetTemplateByID(ctx, tv.TemplateID.UUID)
if err != nil {
return err
}
obj = tpl
}
if err := q.authorizeContext(ctx, policy.ActionUpdate, obj); err != nil {
return err
}
return q.db.UpdateTemplateVersionAITaskByJobID(ctx, arg)
}

func (q *querier) UpdateTemplateVersionByID(ctx context.Context, arg database.UpdateTemplateVersionByIDParams) error {
// An actor is allowed to update the template version if they are authorized to update the template.
tv, err := q.db.GetTemplateVersionByID(ctx, arg.ID)
Expand Down Expand Up @@ -4891,6 +4913,13 @@ func (q *querier) UpdateWorkspaceAutostart(ctx context.Context, arg database.Upd
return update(q.log, q.auth, fetch, q.db.UpdateWorkspaceAutostart)(ctx, arg)
}

func (q *querier) UpdateWorkspaceBuildAITaskByID(ctx context.Context, arg database.UpdateWorkspaceBuildAITaskByIDParams) error {
if err := q.authorizeContext(ctx, policy.ActionUpdate, rbac.ResourceSystem); err != nil {
return err
}
return q.db.UpdateWorkspaceBuildAITaskByID(ctx, arg)
}

// UpdateWorkspaceBuildCostByID is used by the provisioning system to update the cost of a workspace build.
func (q *querier) UpdateWorkspaceBuildCostByID(ctx context.Context, arg database.UpdateWorkspaceBuildCostByIDParams) error {
if err := q.authorizeContext(ctx, policy.ActionUpdate, rbac.ResourceSystem); err != nil {
Expand Down
2 changes: 0 additions & 2 deletions coderd/database/dbgen/dbgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,6 @@ func WorkspaceBuild(t testing.TB, db database.Store, orig database.WorkspaceBuil
UUID: uuid.UUID{},
Valid: false,
}),
HasAITask: orig.HasAITask,
})
if err != nil {
return err
Expand Down Expand Up @@ -985,7 +984,6 @@ func TemplateVersion(t testing.TB, db database.Store, orig database.TemplateVers
JobID: takeFirst(orig.JobID, uuid.New()),
CreatedBy: takeFirst(orig.CreatedBy, uuid.New()),
SourceExampleID: takeFirst(orig.SourceExampleID, sql.NullString{}),
HasAITask: orig.HasAITask,
})
if err != nil {
return err
Expand Down
43 changes: 41 additions & 2 deletions coderd/database/dbmem/dbmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -9463,7 +9463,6 @@ func (q *FakeQuerier) InsertTemplateVersion(_ context.Context, arg database.Inse
JobID: arg.JobID,
CreatedBy: arg.CreatedBy,
SourceExampleID: arg.SourceExampleID,
HasAITask: arg.HasAITask,
}
q.templateVersions = append(q.templateVersions, version)
return nil
Expand Down Expand Up @@ -10143,7 +10142,6 @@ func (q *FakeQuerier) InsertWorkspaceBuild(_ context.Context, arg database.Inser
MaxDeadline: arg.MaxDeadline,
Reason: arg.Reason,
TemplateVersionPresetID: arg.TemplateVersionPresetID,
HasAITask: arg.HasAITask,
}
q.workspaceBuilds = append(q.workspaceBuilds, workspaceBuild)
return nil
Expand Down Expand Up @@ -11348,6 +11346,26 @@ func (q *FakeQuerier) UpdateTemplateScheduleByID(_ context.Context, arg database
return sql.ErrNoRows
}

func (q *FakeQuerier) UpdateTemplateVersionAITaskByJobID(_ context.Context, arg database.UpdateTemplateVersionAITaskByJobIDParams) error {
if err := validateDatabaseType(arg); err != nil {
return err
}

q.mutex.Lock()
defer q.mutex.Unlock()

for index, templateVersion := range q.templateVersions {
if templateVersion.JobID != arg.JobID {
continue
}
templateVersion.HasAITask = arg.HasAITask
templateVersion.UpdatedAt = arg.UpdatedAt
q.templateVersions[index] = templateVersion
return nil
}
return sql.ErrNoRows
}

func (q *FakeQuerier) UpdateTemplateVersionByID(_ context.Context, arg database.UpdateTemplateVersionByIDParams) error {
if err := validateDatabaseType(arg); err != nil {
return err
Expand Down Expand Up @@ -12043,6 +12061,27 @@ func (q *FakeQuerier) UpdateWorkspaceAutostart(_ context.Context, arg database.U
return sql.ErrNoRows
}

func (q *FakeQuerier) UpdateWorkspaceBuildAITaskByID(_ context.Context, arg database.UpdateWorkspaceBuildAITaskByIDParams) error {
err := validateDatabaseType(arg)
if err != nil {
return err
}

q.mutex.Lock()
defer q.mutex.Unlock()

for index, workspaceBuild := range q.workspaceBuilds {
if workspaceBuild.ID != arg.ID {
continue
}
workspaceBuild.HasAITask = arg.HasAITask
workspaceBuild.UpdatedAt = dbtime.Now()
q.workspaceBuilds[index] = workspaceBuild
return nil
}
return sql.ErrNoRows
}

func (q *FakeQuerier) UpdateWorkspaceBuildCostByID(_ context.Context, arg database.UpdateWorkspaceBuildCostByIDParams) error {
if err := validateDatabaseType(arg); err != nil {
return err
Expand Down
14 changes: 14 additions & 0 deletions coderd/database/dbmetrics/querymetrics.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions coderd/database/dbmock/dbmock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions coderd/database/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 51 additions & 9 deletions coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions coderd/database/queries/templateversions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,10 @@ INSERT INTO
readme,
job_id,
created_by,
source_example_id,
has_ai_task
source_example_id
)
VALUES
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12);
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11);

-- name: UpdateTemplateVersionByID :exec
UPDATE
Expand Down Expand Up @@ -123,6 +122,15 @@ SET
WHERE
job_id = $1;

-- name: UpdateTemplateVersionAITaskByJobID :exec
UPDATE
template_versions
SET
has_ai_task = $2,
updated_at = $3
WHERE
job_id = $1;

-- name: GetPreviousTemplateVersion :one
SELECT
*
Expand Down
14 changes: 11 additions & 3 deletions coderd/database/queries/workspacebuilds.sql
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,10 @@ INSERT INTO
deadline,
max_deadline,
reason,
template_version_preset_id,
has_ai_task
template_version_preset_id
)
VALUES
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15);
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14);

-- name: UpdateWorkspaceBuildCostByID :exec
UPDATE
Expand All @@ -152,6 +151,15 @@ SET
updated_at = @updated_at::timestamptz
WHERE id = @id::uuid;

-- name: UpdateWorkspaceBuildAITaskByID :exec
UPDATE
workspace_builds
SET
has_ai_task = @has_ai_task,
ai_tasks_sidebar_app_id = @sidebar_app_id,
updated_at = @updated_at::timestamptz
WHERE id = @id::uuid;

-- name: GetActiveWorkspaceBuildsByTemplateID :many
SELECT wb.*
FROM (
Expand Down
Loading