code: def delete_spot_requests_and_agents(
      stack_name: str, boto3_session: boto3.session.Session
  ) -> List[str]:
      """
      List all spot requests. Any requests that have an associated instance,
      terminate the instances (this will automatically cancel the spot
      request). Any requests that do not have an associated instance, cancel
      the spot requests.
  
      Returns the list of instance_ids that were deleted so at the end of spot
      cleanup, we can wait until all instances have been terminated.
      """
      spot_reqs = list_spot_requests_for_stack(
          stack_name, boto3_session
      )
      instances_to_del = []
      requests_to_term = []
      for req in spot_reqs:
          if req["instanceId"] is not None:
              instances_to_del.append(req["instanceId"])
          else:
              requests_to_term.append(req["id"])
  
      ec2 = boto3_session.client("ec2")
  
      if len(instances_to_del) > 0:
          ec2.terminate_instances(InstanceIds=instances_to_del)
  
      if len(requests_to_term) > 0:
          ec2.cancel_spot_instance_requests(
              SpotInstanceRequestIds=requests_to_term
          )
  
      return instances_to_del
  
imports: import sys
  import threading
  import time
  from typing import Any, Callable, Dict, List, Optional, Tuple
  import boto3
  import tqdm
  from botocore.exceptions import ClientError, WaiterError
  from determined.deploy.aws import constants
label_desc: Use image node_id 20 for calling a global function with example usage: delete_spot_requests_and_agents(stack_name, boto3_session) and returns: instances_to_del