code: def run_test_continue_through_error(
      command, test_directory, env, timeout, stepcurrent_key, output
  ):
      # Run the test with -x to stop at first failure. Try again, skipping the
      # previously run tests, repeating this until there is a test that fails 3
      # times (same number of rVetries we typically give).  Then we skip that
      # test, and keep going. Basically if the same test fails 3 times in a row,
      # skip the test on the next run, but still fail in the end. I take advantage
      # of the value saved in stepcurrent to keep track of the most recently run
      # test (which is the one that failed if there was a failure).
  
      num_failures = defaultdict(int)
  
      sc_command = f"--sc={stepcurrent_key}"
      while True:
          ret_code = shell(
              command + [sc_command],
              test_directory,
              stdout=output,
              stderr=output,
              env=env,
              timeout=timeout,
          )
          ret_code = 0 if ret_code == 5 or ret_code == 4 else ret_code
          if ret_code == 0:
              break  # Got to the end of the test suite successfully
          signal_name = (
              f" ({SIGNALS_TO_NAMES_DICT[-ret_code]})"
              if ret_code < 0
              else ""
          )
          print(
              f"Got exit code {ret_code}{signal_name}, retrying...",
              file=output,
              flush=True,
          )
  
          # Read what just failed
          with open(
              REPO_ROOT
              / ".pytest_cache/v/cache/stepcurrent"
              / stepcurrent_key
          ) as f:
              current_failure = f.read()
  
          num_failures[current_failure] += 1
          if num_failures[current_failure] >= 3:
              sc_command = f"--scs={stepcurrent_key}"
          else:
              sc_command = f"--sc={stepcurrent_key}"
  
      consistent_failures = [
          x[1:-1] for x in num_failures.keys() if num_failures[x] >= 3
      ]
      flaky_failures = [
          x[1:-1]
          for x in num_failures.keys()
          if 0 < num_failures[x] < 3
      ]
      if len(flaky_failures) > 0:
          print(
              "The following tests failed flakily (had to be rerun in a new process,"
              + f" doesn't include reruns froms same process): {flaky_failures}",
              file=output,
              flush=True,
          )
      if len(consistent_failures) > 0:
          print(
              f"The following tests failed consistently: {consistent_failures}",
              file=output,
              flush=True,
          )
          return 1, True
      return 0, any(x > 0 for x in num_failures.values())
  
imports: import argparse
  import copy
  import glob
  import json
  import os
  import pathlib
  import re
  import shutil
  import signal
  import subprocess
  import sys
  import tempfile
  import time
  from collections import defaultdict
  from contextlib import ExitStack
  from datetime import datetime
  from typing import Any, cast, Dict, List, NamedTuple, Optional, Sequence, Tuple, Union
  import pkg_resources
  import torch
  import torch.distributed
  from torch.multiprocessing import current_process, get_context
  from torch.testing._internal.common_utils import FILE_SCHEMA, get_report_path, IS_CI, parser, retry_shell, set_cwd, shell, TEST_WITH_ASAN, TEST_WITH_ROCM, TEST_WITH_SLOW_GRADCHECK
  from tools.stats.import_test_stats import ADDITIONAL_CI_FILES_FOLDER, TEST_CLASS_TIMES_FILE, TEST_TIMES_FILE
  from tools.stats.upload_metrics import add_global_metric, emit_metric
  from tools.testing.target_determination.determinator import AggregatedHeuristics, get_prediction_confidences, get_test_prioritizations
  from tools.testing.test_run import TestRun
  from tools.testing.test_selections import calculate_shards, get_test_case_configs, NUM_PROCS, ShardedTest, THRESHOLD
label_desc: Use image node_id 8 for calling a global function with example usage: run_test_continue_through_error(command, test_directory, env, timeout, stepcurrent_key, output) and returns: int,  any, int,  True