8000 feat: persist AI task state in template imports & workspace builds by dannykopping · Pull Request #18449 · coder/coder · GitHub
[go: up one dir, main page]

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: store coder_ai_task resource state
Signed-off-by: Danny Kopping <dannykopping@gmail.com>
  • Loading branch information
dannykopping committed Jun 23, 2025
commit 438335039ab6d62488d90dd4ff06ca44b7507de0
40 changes: 40 additions & 0 deletions coderd/provisionerdserver/provisionerdserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
protobuf "google.golang.org/protobuf/proto"

"cdr.dev/slog"

"github.com/coder/coder/v2/coderd/util/slice"

"github.com/coder/coder/v2/codersdk/drpcsdk"
Expand Down Expand Up @@ -1654,6 +1655,17 @@ func (s *server) completeTemplateImportJob(ctx context.Context, job database.Pro
if err != nil {
return xerrors.Errorf("update template version external auth providers: %w", err)
}
err = db.UpdateTemplateVersionAITaskByJobID(ctx, database.UpdateTemplateVersionAITaskByJobIDParams{
JobID: jobID,
HasAITask: sql.NullBool{
Bool: jobType.TemplateImport.HasAiTasks,
Valid: true,
},
UpdatedAt: now,
})
if err != nil {
return xerrors.Errorf("update template version external auth providers: %w", err)
}

// Process terraform values
plan := jobType.TemplateImport.Plan
Expand Down Expand Up @@ -1866,6 +1878,34 @@ func (s *server) completeWorkspaceBuildJob(ctx context.Context, job database.Pro
}
}

8000 var sidebarAppID uuid.NullUUID
if len(jobType.WorkspaceBuild.AiTasks) == 1 {
task := jobType.WorkspaceBuild.AiTasks[0]
if task.SidebarApp == nil {
return xerrors.Errorf("update ai task: sidebar app is nil")
}

id, err := uuid.Parse(task.SidebarApp.Id)
if err != nil {
return xerrors.Errorf("parse sidebar app id: %w", err)
}

sidebarAppID = uuid.NullUUID{UUID: id, Valid: true}
}

err = db.UpdateWorkspaceBuildAITaskByID(ctx, database.UpdateWorkspaceBuildAITaskByIDParams{
ID: workspaceBuild.ID,
HasAITask: sql.NullBool{
Bool: len(jobType.WorkspaceBuild.AiTasks) > 0,
Valid: true,
},
SidebarAppID: sidebarAppID,
UpdatedAt: now,
})
if err != nil {
return xerrors.Errorf("update workspace build ai tasks flag: %w", err)
}

// Insert timings inside the transaction now
// nolint:exhaustruct // The other fields are set further down.
params := database.InsertProvisionerJobTimingsParams{
Expand Down
3 changes: 3 additions & 0 deletions provisioner/terraform/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,8 @@ func (e *executor) plan(ctx, killCtx context.Context, env, vars []string, logr l
Plan: planJSON,
ResourceReplacements: resReps,
ModuleFiles: moduleFiles,
HasAiTasks: state.HasAITasks,
AiTasks: state.AITasks,
}

return msg, nil
Expand Down Expand Up @@ -577,6 +579,7 @@ func (e *executor) apply(
ExternalAuthProviders: state.ExternalAuthProviders,
State: stateContent,
Timings: e.timings.aggregate(),
AiTasks: state.AITasks,
}, nil
}

Expand Down
46 changes: 46 additions & 0 deletions provisioner/terraform/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,29 @@ type State struct {
Parameters []*proto.RichParameter
Presets []*proto.Preset
ExternalAuthProviders []*proto.ExternalAuthProviderResource
AITasks []*proto.AITask
HasAITasks bool
}

var ErrInvalidTerraformAddr = xerrors.New("invalid terraform address")

// hasAITaskResources is used to determine if a template has *any* `coder_ai_task` resources defined. During template
// import, it's possible that none of these have `count=1` since count may be dependent on the value of a `coder_parameter`
// or something else.
// We need to know at template import if these resources exist to inform the frontend of their existence.
func hasAITaskResources(graph *gographviz.Graph) bool {
for _, node := range graph.Nodes.Lookup {
// Check if this node is a coder_ai_task resource
if label, exists := node.Attrs["label"]; exists {
labelValue := strings.Trim(label, `"`)
if strings.Contains(labelValue, "coder_ai_task.") {
return true
}
}
}
return false
}

// ConvertState consumes Terraform state and a GraphViz representation
// produced by `terraform graph` to produce resources consumable by Coder.
// nolint:gocognit // This function makes more sense being large for now, until refactored.
Expand All @@ -189,6 +208,7 @@ func ConvertState(ctx context.Context, modules []*tfjson.StateModule, rawGraph s
// Extra array to preserve the order of rich parameters.
tfResourcesRichParameters := make([]*tfjson.StateResource, 0)
tfResourcesPresets := make([]*tfjson.StateResource, 0)
tfResourcesAITasks := make([]*tfjson.StateResource, 0)
var findTerraformResources func(mod *tfjson.StateModule)
findTerraformResources = func(mod *tfjson.StateModule) {
for _, module := range mod.ChildModules {
Expand All @@ -201,6 +221,9 @@ func ConvertState(ctx context.Context, modules []*tfjson.StateModule, rawGraph s
if resource.Type == "coder_workspace_preset" {
tfResourcesPresets = append(tfResourcesPresets, resource)
}
if resource.Type == "coder_ai_task" {
tfResourcesAITasks = append(tfResourcesAITasks, resource)
}

label := convertAddressToLabel(resource.Address)
if tfResourcesByLabel[label] == nil {
Expand Down Expand Up @@ -957,6 +980,27 @@ func ConvertState(ctx context.Context, modules []*tfjson.StateModule, rawGraph s
)
}

// This will only pick up resources which will actually be created.
aiTasks := make([]*proto.AITask, 0, len(tfResourcesAITasks))
for _, resource := range tfResourcesAITasks {
var task provider.AITask
err = mapstructure.Decode(resource.AttributeValues, &task)
if err != nil {
return nil, xerrors.Errorf("decode coder_ai_task attributes: %w", err)
}

if len(task.SidebarApp) < 1 {
return nil, xerrors.Errorf("coder_ai_task has no sidebar_app defined")
}

aiTasks = append(aiTasks, &proto.AITask{
Id: task.ID,
SidebarApp: &proto.AITaskSidebarApp{
Id: task.SidebarApp[0].ID,
},
})
}

// A map is used to ensure we don't have duplicates!
externalAuthProvidersMap := map[string]*proto.ExternalAuthProviderResource{}
for _, tfResources := range tfResourcesByLabel {
Expand Down Expand Up @@ -992,6 +1036,8 @@ func ConvertState(ctx context.Context, modules []*tfjson.StateModule, rawGraph s
Parameters: parameters,
Presets: presets,
ExternalAuthProviders: externalAuthProviders,
HasAITasks: hasAITaskResources(graph),
AITasks: aiTasks,
}, nil
}

Expand Down
20 changes: 18 additions & 2 deletions provisionerd/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import (
"errors"
"fmt"
"reflect"
"slices"
"strings"
"sync"
"sync/atomic"
"time"

"github.com/coder/terraform-provider-coder/v2/provider"
"github.com/google/uuid"
"github.com/prometheus/client_golang/prometheus"
"go.opentelemetry.io/otel/attribute"
Expand Down Expand Up @@ -584,8 +586,6 @@ func (r *Runner) runTemplateImport(ctx context.Context) (*proto.CompletedJob, *p
externalAuthProviderNames = append(externalAuthProviderNames, it.Id)
}

// fmt.Println("completed job: template import: graph:", startProvision.Graph)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drive-by.


return &proto.CompletedJob{
JobId: r.job.JobId,
Type: &proto.CompletedJob_TemplateImport_{
Expand All @@ -603,6 +603,7 @@ func (r *Runner) runTemplateImport(ctx context.Context) (*proto.CompletedJob, *p
ModuleFiles: startProvision.ModuleFiles,
// ModuleFileHash will be populated if the file is uploaded async
ModuleFilesHash: []byte{},
HasAiTasks: startProvision.HasAITasks,
},
},
}, nil
Expand Down Expand Up @@ -666,6 +667,7 @@ type templateImportProvision struct {
Presets []*sdkproto.Preset
Plan json.RawMessage
ModuleFiles []byte
HasAITasks bool
}

// Performs a dry-run provision when importing a template.
Expand Down Expand Up @@ -799,6 +801,15 @@ func (r *Runner) runTemplateImportProvisionWithRichParameters(
}
}

if c.HasAiTasks {
hasPromptParam := slices.ContainsFunc(c.Parameters, func(param *sdkproto.RichParameter) bool {
return param.Name == provider.TaskPromptParameterName
})
if !hasPromptParam {
return nil, xerrors.Errorf("coder_parameter named '%s' is required when 'coder_ai_task' resource is defined", provider.TaskPromptParameterName)
}
}

return &templateImportProvision{
Resources: c.Resources,
Parameters: c.Parameters,
Expand All @@ -807,6 +818,7 @@ func (r *Runner) runTemplateImportProvisionWithRichParameters(
Presets: c.Presets,
Plan: c.Plan,
ModuleFiles: moduleFilesData,
HasAITasks: c.HasAiTasks,
}, nil
default:
return nil, xerrors.Errorf("invalid message type %q received from provisioner",
Expand Down Expand Up @@ -1047,6 +1059,9 @@ func (r *Runner) runWorkspaceBuild(ctx context.Context) (*proto.CompletedJob, *p
},
}
}
if len(planComplete.AiTasks) > 1 {
return nil, r.failedWorkspaceBuildf("only one 'coder_ai_task' resource can be provisioned per template")
}

r.logger.Info(context.Background(), "plan request successful",
slog.F("resource_count", len(planComplete.Resources)),
Expand Down Expand Up @@ -1124,6 +1139,7 @@ func (r *Runner) runWorkspaceBuild(ctx context.Context) (*proto.CompletedJob, *p
Modules: planComplete.Modules,
// Resource replacements are discovered at plan time, only.
ResourceReplacements: planComplete.ResourceReplacements,
AiTasks: applyComplete.AiTasks,
},
},
}, nil
Expand Down
0