fix: soft-delete stale workspace agents on new build (#25207)

This commit is contained in:
Garrett Delfosse
2026-05-18 08:33:29 -04:00
committed by GitHub
parent 159089686a
commit 78d4cf9e47
16 changed files with 723 additions and 3 deletions
@@ -2143,6 +2143,20 @@ func (s *server) completeWorkspaceBuildJob(ctx context.Context, job database.Pro
return xerrors.Errorf("insert provisioner job: %w", err)
}
}
// Soft-delete agents from prior builds now that this build's
// agents have been inserted. Waiting until completion (rather
// than build creation) avoids bricking running workspaces
// whose agents would otherwise be deleted while the new build
// is still queued or provisioning. See #25155.
err = db.SoftDeletePriorWorkspaceAgents(ctx, database.SoftDeletePriorWorkspaceAgentsParams{
WorkspaceID: workspaceBuild.WorkspaceID,
CurrentBuildID: workspaceBuild.ID,
})
if err != nil {
return xerrors.Errorf("soft delete prior workspace agents: %w", err)
}
for _, module := range jobType.WorkspaceBuild.Modules {
if err := InsertWorkspaceModule(ctx, db, job.ID, workspaceBuild.Transition, module, telemetrySnapshot); err != nil {
return xerrors.Errorf("insert provisioner job module: %w", err)
@@ -2391,6 +2405,14 @@ func (s *server) completeWorkspaceBuildJob(ctx context.Context, job database.Pro
return xerrors.Errorf("update workspace deleted: %w", err)
}
// Soft-delete any agents tied to this workspace so the
// aws-instance-identity handler (which filters on
// workspace_agents.deleted) doesn't keep seeing orphaned rows
// after the workspace itself is deleted. See #25155.
if err := db.SoftDeleteWorkspaceAgentsByWorkspaceID(ctx, workspaceBuild.WorkspaceID); err != nil {
return xerrors.Errorf("soft delete workspace agents: %w", err)
}
// A user might delete their task workspace directly, instead of
// deleting the task. To avoid leaving the Task in a scenario where
// it has no workspace, we also attempt to delete the task.