Skip to content

Commit 155c7bb

Browse files
authored
chore(coderd/provisionerdserver): avoid fk error on invalid ai_task_sidebar_app_id (#19253)
This is a workaround for #18776 We avoid the foreign key issue by checking the previously inserted workspace applications before calling UpdateWorkspaceAITask. Now, affected workspaces will show as "not running an AI task" on the single task view, which is technically correct. We also insert a provisioner job log at WARN level to ensure that the user sees some information that they have run into this issue, as well as logging on the server side. Longer term, we plan to modify how the workspace tasks view is presented. This is a stopgap measure until we solidify that plan. NOTE: this does **not** address the fact that stopping a workspace with `has_ai_task: true` will result in the completed stop build no longer having `has_ai_task: true`, resulting in tasks "disappearing" on stop.
1 parent afb54f6 commit 155c7bb

File tree

2 files changed

+74
-9
lines changed

2 files changed

+74
-9
lines changed

coderd/provisionerdserver/provisionerdserver.go

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1925,12 +1925,16 @@ func (s *server) completeWorkspaceBuildJob(ctx context.Context, job database.Pro
19251925
return xerrors.Errorf("update workspace build deadline: %w", err)
19261926
}
19271927

1928+
appIDs := make([]string, 0)
19281929
agentTimeouts := make(map[time.Duration]bool) // A set of agent timeouts.
19291930
// This could be a bulk insert to improve performance.
19301931
for _, protoResource := range jobType.WorkspaceBuild.Resources {
19311932
for _, protoAgent := range protoResource.Agents {
19321933
dur := time.Duration(protoAgent.GetConnectionTimeoutSeconds()) * time.Second
19331934
agentTimeouts[dur] = true
1935+
for _, app := range protoAgent.GetApps() {
1936+
appIDs = append(appIDs, app.GetId())
1937+
}
19341938
}
19351939

19361940
err = InsertWorkspaceResource(ctx, db, job.ID, workspaceBuild.Transition, protoResource, telemetrySnapshot)
@@ -1945,33 +1949,78 @@ func (s *server) completeWorkspaceBuildJob(ctx context.Context, job database.Pro
19451949
}
19461950

19471951
var sidebarAppID uuid.NullUUID
1948-
hasAITask := len(jobType.WorkspaceBuild.AiTasks) == 1
1949-
if hasAITask {
1950-
task := jobType.WorkspaceBuild.AiTasks[0]
1951-
if task.SidebarApp == nil {
1952-
return xerrors.Errorf("update ai task: sidebar app is nil")
1952+
var hasAITask bool
1953+
var warnUnknownSidebarAppID bool
1954+
if tasks := jobType.WorkspaceBuild.GetAiTasks(); len(tasks) > 0 {
1955+
hasAITask = true
1956+
task := tasks[0]
1957+
if task == nil || task.GetSidebarApp() == nil || len(task.GetSidebarApp().GetId()) == 0 {
1958+
return xerrors.Errorf("update ai task: sidebar app is nil or empty")
1959+
}
1960+
1961+
sidebarTaskID := task.GetSidebarApp().GetId()
1962+
if !slices.Contains(appIDs, sidebarTaskID) {
1963+
warnUnknownSidebarAppID = true
19531964
}
19541965

1955-
id, err := uuid.Parse(task.SidebarApp.Id)
1966+
id, err := uuid.Parse(task.GetSidebarApp().GetId())
19561967
if err != nil {
19571968
return xerrors.Errorf("parse sidebar app id: %w", err)
19581969
}
19591970

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

1974+
if warnUnknownSidebarAppID {
1975+
// Ref: https://github.com/coder/coder/issues/18776
1976+
// This can happen for a number of reasons:
1977+
// 1. Misconfigured template
1978+
// 2. Count=0 on the agent due to stop transition, meaning the associated coder_app was not inserted.
1979+
// Failing the build at this point is not ideal, so log a warning instead.
1980+
s.Logger.Warn(ctx, "unknown ai_task_sidebar_app_id",
1981+
slog.F("ai_task_sidebar_app_id", sidebarAppID.UUID.String()),
1982+
slog.F("job_id", job.ID.String()),
1983+
slog.F("workspace_id", workspace.ID),
1984+
slog.F("workspace_build_id", workspaceBuild.ID),
1985+
slog.F("transition", string(workspaceBuild.Transition)),
1986+
)
1987+
// In order to surface this to the user, we will also insert a warning into the build logs.
1988+
if _, err := db.InsertProvisionerJobLogs(ctx, database.InsertProvisionerJobLogsParams{
1989+
JobID: jobID,
1990+
CreatedAt: []time.Time{now, now, now, now},
1991+
Source: []database.LogSource{database.LogSourceProvisionerDaemon, database.LogSourceProvisionerDaemon, database.LogSourceProvisionerDaemon, database.LogSourceProvisionerDaemon},
1992+
Level: []database.LogLevel{database.LogLevelWarn, database.LogLevelWarn, database.LogLevelWarn, database.LogLevelWarn},
1993+
Stage: []string{"Cleaning Up", "Cleaning Up", "Cleaning Up", "Cleaning Up"},
1994+
Output: []string{
1995+
fmt.Sprintf("Unknown ai_task_sidebar_app_id %q. This workspace will be unable to run AI tasks. This may be due to a template configuration issue, please check with the template author.", sidebarAppID.UUID.String()),
1996+
"Template author: double-check the following:",
1997+
" - You have associated the coder_ai_task with a valid coder_app in your template (ref: https://registry.terraform.io/providers/coder/coder/latest/docs/resources/ai_task).",
1998+
" - You have associated the coder_agent with at least one other compute resource. Agents with no other associated resources are not inserted into the database.",
1999+
},
2000+
}); err != nil {
2001+
s.Logger.Error(ctx, "insert provisioner job log for ai task sidebar app id warning",
2002+
slog.F("job_id", jobID),
2003+
slog.F("workspace_id", workspace.ID),
2004+
slog.F("workspace_build_id", workspaceBuild.ID),
2005+
slog.F("transition", string(workspaceBuild.Transition)),
2006+
)
2007+
}
2008+
// Important: reset hasAITask and sidebarAppID so that we don't run into a fk constraint violation.
2009+
hasAITask = false
2010+
sidebarAppID = uuid.NullUUID{}
2011+
}
2012+
19632013
// Regardless of whether there is an AI task or not, update the field to indicate one way or the other since it
19642014
// always defaults to nil. ONLY if has_ai_task=true MUST ai_task_sidebar_app_id be set.
1965-
err = db.UpdateWorkspaceBuildAITaskByID(ctx, database.UpdateWorkspaceBuildAITaskByIDParams{
2015+
if err := db.UpdateWorkspaceBuildAITaskByID(ctx, database.UpdateWorkspaceBuildAITaskByIDParams{
19662016
ID: workspaceBuild.ID,
19672017
HasAITask: sql.NullBool{
19682018
Bool: hasAITask,
19692019
Valid: true,
19702020
},
19712021
SidebarAppID: sidebarAppID,
19722022
UpdatedAt: now,
1973-
})
1974-
if err != nil {
2023+
}); err != nil {
19752024
return xerrors.Errorf("update workspace build ai tasks flag: %w", err)
19762025
}
19772026

coderd/provisionerdserver/provisionerdserver_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2794,6 +2794,22 @@ func TestCompleteJob(t *testing.T) {
27942794
},
27952795
expected: true,
27962796
},
2797+
// Checks regression for https://github.com/coder/coder/issues/18776
2798+
{
2799+
name: "non-existing app",
2800+
input: &proto.CompletedJob_WorkspaceBuild{
2801+
AiTasks: []*sdkproto.AITask{
2802+
{
2803+
Id: uuid.NewString(),
2804+
SidebarApp: &sdkproto.AITaskSidebarApp{
2805+
// Non-existing app ID would previously trigger a FK violation.
2806+
Id: uuid.NewString(),
2807+
},
2808+
},
2809+
},
2810+
},
2811+
expected: false,
2812+
},
27972813
} {
27982814
t.Run(tc.name, func(t *testing.T) {
27992815
t.Parallel()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy