@@ -1943,6 +1943,17 @@ func (s *server) completeWorkspaceBuildJob(ctx context.Context, job database.Pro
1943
1943
}
1944
1944
}
1945
1945
1946
+ var knownAppIDs []string
1947
+ for _ , res := range jobType .WorkspaceBuild .Resources {
1948
+ for _ , agt := range res .Agents {
1949
+ for _ , app := range agt .Apps {
1950
+ if app .Id == "" {
1951
+ continue
1952
+ }
1953
+ knownAppIDs = append (knownAppIDs , app .Id )
1954
+ }
1955
+ }
1956
+ }
1946
1957
var sidebarAppID uuid.NullUUID
1947
1958
hasAITask := len (jobType .WorkspaceBuild .AiTasks ) == 1
1948
1959
if hasAITask {
@@ -1959,18 +1970,33 @@ func (s *server) completeWorkspaceBuildJob(ctx context.Context, job database.Pro
1959
1970
sidebarAppID = uuid.NullUUID {UUID : id , Valid : true }
1960
1971
}
1961
1972
1973
+ // NOTE(Cian): If stopping a workspace build, we must ensure that the
1974
+ // SidebarAppID corresponds to an extant coder_app. If it does not, the
1975
+ // build will fail with a foreign key constraint violation as workspace_builds.ai_task_sidebar_app_id
1976
+ // references coder_apps.id.
1977
+ // Unfortunately, this behavior will result in the workspace build being
1978
+ // 'silently' not configured as an AI task except for a warning in the logs.
1979
+ // Ideally, we should also accept an app slug as these are less likely to change.
1980
+ if sidebarAppID .Valid && ! slices .Contains (knownAppIDs , sidebarAppID .UUID .String ()) {
1981
+ s .Logger .Warn (ctx , "workspace build has unknown sidebar app ID" ,
1982
+ slog .F ("workspace_build_id" , workspaceBuild .ID ),
1983
+ slog .F ("sidebar_app_id" , sidebarAppID ),
1984
+ slog .F ("known_app_ids" , knownAppIDs ),
1985
+ )
1986
+ sidebarAppID = uuid.NullUUID {}
1987
+ hasAITask = false
1988
+ }
1962
1989
// Regardless of whether there is an AI task or not, update the field to indicate one way or the other since it
1963
1990
// always defaults to nil. ONLY if has_ai_task=true MUST ai_task_sidebar_app_id be set.
1964
- err = db .UpdateWorkspaceBuildAITaskByID (ctx , database.UpdateWorkspaceBuildAITaskByIDParams {
1991
+ if err = db .UpdateWorkspaceBuildAITaskByID (ctx , database.UpdateWorkspaceBuildAITaskByIDParams {
1965
1992
ID : workspaceBuild .ID ,
1966
1993
HasAITask : sql.NullBool {
1967
1994
Bool : hasAITask ,
1968
1995
Valid : true ,
1969
1996
},
1970
1997
SidebarAppID : sidebarAppID ,
1971
1998
UpdatedAt : now ,
1972
- })
1973
- if err != nil {
1999
+ }); err != nil {
1974
2000
return xerrors .Errorf ("update workspace build ai tasks flag: %w" , err )
1975
2001
}
1976
2002
0 commit comments