prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  dvc exp gc: can't run experiments after garbage collection
  # Bug Report
  
  ## Description
  
  Experiments fail after garbage collection via `dvc exp gc`.
  
  ### Reproduce
  
  1. dvc init
  2. dvc stage add -n foo -M foo.yaml 'echo "score: 1" > foo.yaml'
  3. git add .
  4. git commit -m "add stage"
  5. dvc exp run
  6. dvc exp gc -fT
  7. dvc exp run
  
  ```console
  Stage 'foo' didn't change, skipping
  ERROR: Failed to set 'refs/exps/d1/5cee9fd7a4fc20c5d7167671df4ed78b3cfb7a/exp-985c5'
  ERROR: Failed to set 'refs/exps/d1/5cee9fd7a4fc20c5d7167671df4ed78b3cfb7a/exp-985c5'
  ```
  
  ### Expected
  
  `dvc exp run` to successfully run experiment.
  
  ### Environment information
  
  **Output of `dvc version`:**
  
  ```console
  $ dvc version
  DVC version: 2.0.0a0+adedd1
  ---------------------------------
  Platform: Python 3.9.1 on Linux-5.8.0-38-generic-x86_64-with-glibc2.31
  Supports: http, https
  Cache types: <https://error.dvc.org/no-dvc-cache>
  Caches: local
  Remotes: None
  Workspace directory: ext4 on /dev/mapper/vgubuntu-root
  Repo: dvc, git
  ```
  
  **Additional Information (if any):**
  
  <!--
  If applicable, please also provide a `--verbose` output of the command, eg: `dvc add --verbose`.
  -->
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `dvc/repo/experiments/gc.py`
  Content:
  ```
  1 import logging
  2 from typing import Optional
  3 
  4 from dvc.repo import locked
  5 
  6 from .utils import exp_refs
  7 
  8 logger = logging.getLogger(__name__)
  9 
  10 
  11 @locked
  12 def gc(
  13     repo,
  14     all_branches: Optional[bool] = False,
  15     all_tags: Optional[bool] = False,
  16     all_commits: Optional[bool] = False,
  17     workspace: Optional[bool] = False,
  18     queued: Optional[bool] = False,
  19 ):
  20     keep_revs = set(
  21         repo.brancher(
  22             all_branches=all_branches,
  23             all_tags=all_tags,
  24             all_commits=all_commits,
  25             sha_only=True,
  26         )
  27     )
  28     if workspace:
  29         keep_revs.add(repo.scm.get_rev())
  30 
  31     if not keep_revs:
  32         return 0
  33 
  34     removed = 0
  35     for ref_info in exp_refs(repo.scm):
  36         if ref_info.baseline_sha not in keep_revs:
  37             repo.scm.remove_ref(str(ref_info))
  38             removed += 1
  39 
  40     delete_stashes = []
  41     for _, entry in repo.experiments.stash_revs.items():
  42         if not queued or entry.baseline_rev not in keep_revs:
  43             delete_stashes.append(entry.index)
  44     for index in sorted(delete_stashes, reverse=True):
  45         repo.experiments.stash.drop(index)
  46     removed += len(delete_stashes)
  47 
  48     return removed
  49
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/dvc/repo/experiments/gc.py b/dvc/repo/experiments/gc.py
  --- a/dvc/repo/experiments/gc.py
  +++ b/dvc/repo/experiments/gc.py
  @@ -3,6 +3,7 @@
   
   from dvc.repo import locked
   
  +from .base import EXEC_APPLY, EXEC_BRANCH, EXEC_CHECKPOINT
   from .utils import exp_refs
   
   logger = logging.getLogger(__name__)
  @@ -31,9 +32,20 @@
       if not keep_revs:
           return 0
   
  +    exec_branch = repo.scm.get_ref(EXEC_BRANCH, follow=False)
  +    exec_apply = repo.scm.get_ref(EXEC_APPLY)
  +    exec_checkpoint = repo.scm.get_ref(EXEC_CHECKPOINT)
  +
       removed = 0
       for ref_info in exp_refs(repo.scm):
           if ref_info.baseline_sha not in keep_revs:
  +            ref = repo.scm.get_ref(str(ref_info))
  +            if exec_branch and str(ref_info):
  +                repo.scm.remove_ref(EXEC_BRANCH)
  +            if exec_apply and exec_apply == ref:
  +                repo.scm.remove_ref(EXEC_APPLY)
  +            if exec_checkpoint and exec_checkpoint == ref:
  +                repo.scm.remove_ref(EXEC_CHECKPOINT)
               repo.scm.remove_ref(str(ref_info))
               removed += 1
  
verification_info: {"golden_diff": "diff --git a/dvc/repo/experiments/gc.py b/dvc/repo/experiments/gc.py\n--- a/dvc/repo/experiments/gc.py\n+++ b/dvc/repo/experiments/gc.py\n@@ -3,6 +3,7 @@\n \n from dvc.repo import locked\n \n+from .base import EXEC_APPLY, EXEC_BRANCH, EXEC_CHECKPOINT\n from .utils import exp_refs\n \n logger = logging.getLogger(__name__)\n@@ -31,9 +32,20 @@\n     if not keep_revs:\n         return 0\n \n+    exec_branch = repo.scm.get_ref(EXEC_BRANCH, follow=False)\n+    exec_apply = repo.scm.get_ref(EXEC_APPLY)\n+    exec_checkpoint = repo.scm.get_ref(EXEC_CHECKPOINT)\n+\n     removed = 0\n     for ref_info in exp_refs(repo.scm):\n         if ref_info.baseline_sha not in keep_revs:\n+            ref = repo.scm.get_ref(str(ref_info))\n+            if exec_branch and str(ref_info):\n+                repo.scm.remove_ref(EXEC_BRANCH)\n+            if exec_apply and exec_apply == ref:\n+                repo.scm.remove_ref(EXEC_APPLY)\n+            if exec_checkpoint and exec_checkpoint == ref:\n+                repo.scm.remove_ref(EXEC_CHECKPOINT)\n             repo.scm.remove_ref(str(ref_info))\n             removed += 1\n", "issue": "dvc exp gc: can't run experiments after garbage collection\n# Bug Report\r\n\r\n## Description\r\n\r\nExperiments fail after garbage collection via `dvc exp gc`.\r\n\r\n### Reproduce\r\n\r\n1. dvc init\r\n2. dvc stage add -n foo -M foo.yaml 'echo \"score: 1\" > foo.yaml'\r\n3. git add .\r\n4. git commit -m \"add stage\"\r\n5. dvc exp run\r\n6. dvc exp gc -fT\r\n7. dvc exp run\r\n\r\n```console\r\nStage 'foo' didn't change, skipping\r\nERROR: Failed to set 'refs/exps/d1/5cee9fd7a4fc20c5d7167671df4ed78b3cfb7a/exp-985c5'\r\nERROR: Failed to set 'refs/exps/d1/5cee9fd7a4fc20c5d7167671df4ed78b3cfb7a/exp-985c5'\r\n```\r\n\r\n### Expected\r\n\r\n`dvc exp run` to successfully run experiment.\r\n\r\n### Environment information\r\n\r\n**Output of `dvc version`:**\r\n\r\n```console\r\n$ dvc version\r\nDVC version: 2.0.0a0+adedd1\r\n---------------------------------\r\nPlatform: Python 3.9.1 on Linux-5.8.0-38-generic-x86_64-with-glibc2.31\r\nSupports: http, https\r\nCache types: <https://error.dvc.org/no-dvc-cache>\r\nCaches: local\r\nRemotes: None\r\nWorkspace directory: ext4 on /dev/mapper/vgubuntu-root\r\nRepo: dvc, git\r\n```\r\n\r\n**Additional Information (if any):**\r\n\r\n<!--\r\nIf applicable, please also provide a `--verbose` output of the command, eg: `dvc add --verbose`.\r\n-->\n", "before_files": [{"content": "import logging\nfrom typing import Optional\n\nfrom dvc.repo import locked\n\nfrom .utils import exp_refs\n\nlogger = logging.getLogger(__name__)\n\n\n@locked\ndef gc(\n    repo,\n    all_branches: Optional[bool] = False,\n    all_tags: Optional[bool] = False,\n    all_commits: Optional[bool] = False,\n    workspace: Optional[bool] = False,\n    queued: Optional[bool] = False,\n):\n    keep_revs = set(\n        repo.brancher(\n            all_branches=all_branches,\n            all_tags=all_tags,\n            all_commits=all_commits,\n            sha_only=True,\n        )\n    )\n    if workspace:\n        keep_revs.add(repo.scm.get_rev())\n\n    if not keep_revs:\n        return 0\n\n    removed = 0\n    for ref_info in exp_refs(repo.scm):\n        if ref_info.baseline_sha not in keep_revs:\n            repo.scm.remove_ref(str(ref_info))\n            removed += 1\n\n    delete_stashes = []\n    for _, entry in repo.experiments.stash_revs.items():\n        if not queued or entry.baseline_rev not in keep_revs:\n            delete_stashes.append(entry.index)\n    for index in sorted(delete_stashes, reverse=True):\n        repo.experiments.stash.drop(index)\n    removed += len(delete_stashes)\n\n    return removed\n", "path": "dvc/repo/experiments/gc.py"}], "after_files": [{"content": "import logging\nfrom typing import Optional\n\nfrom dvc.repo import locked\n\nfrom .base import EXEC_APPLY, EXEC_BRANCH, EXEC_CHECKPOINT\nfrom .utils import exp_refs\n\nlogger = logging.getLogger(__name__)\n\n\n@locked\ndef gc(\n    repo,\n    all_branches: Optional[bool] = False,\n    all_tags: Optional[bool] = False,\n    all_commits: Optional[bool] = False,\n    workspace: Optional[bool] = False,\n    queued: Optional[bool] = False,\n):\n    keep_revs = set(\n        repo.brancher(\n            all_branches=all_branches,\n            all_tags=all_tags,\n            all_commits=all_commits,\n            sha_only=True,\n        )\n    )\n    if workspace:\n        keep_revs.add(repo.scm.get_rev())\n\n    if not keep_revs:\n        return 0\n\n    exec_branch = repo.scm.get_ref(EXEC_BRANCH, follow=False)\n    exec_apply = repo.scm.get_ref(EXEC_APPLY)\n    exec_checkpoint = repo.scm.get_ref(EXEC_CHECKPOINT)\n\n    removed = 0\n    for ref_info in exp_refs(repo.scm):\n        if ref_info.baseline_sha not in keep_revs:\n            ref = repo.scm.get_ref(str(ref_info))\n            if exec_branch and str(ref_info):\n                repo.scm.remove_ref(EXEC_BRANCH)\n            if exec_apply and exec_apply == ref:\n                repo.scm.remove_ref(EXEC_APPLY)\n            if exec_checkpoint and exec_checkpoint == ref:\n                repo.scm.remove_ref(EXEC_CHECKPOINT)\n            repo.scm.remove_ref(str(ref_info))\n            removed += 1\n\n    delete_stashes = []\n    for _, entry in repo.experiments.stash_revs.items():\n        if not queued or entry.baseline_rev not in keep_revs:\n            delete_stashes.append(entry.index)\n    for index in sorted(delete_stashes, reverse=True):\n        repo.experiments.stash.drop(index)\n    removed += len(delete_stashes)\n\n    return removed\n", "path": "dvc/repo/experiments/gc.py"}]}