Claude Code 循环:用 loop-guard skill 阻止智能体重复尝试

Dmitriy Solodukha,Caprock 开发者 · 发布于

测试于 (Claude Code 和 Codex)

Claude Code 循环是指反复执行同一个失败的步骤。loop-guard skill 会停止循环,指出错误假设,并改变方法。免费。

当 Claude Code 陷入循环(同一命令反复失败、每次“修复”后出现相同错误、同一文件反复修改又回退)时,loop-guard skill 会让智能体停下来,列出已尝试的方法,指出每次尝试共有的假设,再换一个假设进行验证,或向你提问。它只有一个 SKILL.md 文件,适用于 Claude Code、Codex、OpenCode 和 Cursor,以 Apache-2.0 许可免费提供。

安装 loop-guard skill

每个智能体只需一条命令。skill 只有一个 SKILL.md;安装时不会运行任何内容。

将全部九个 skill 作为插件安装。在 Claude Code 中输入以下内容:

也可以只安装这个 skill,在终端中进入项目文件夹后运行:

添加 -g 即可为所有项目安装。

使用 loop-guard skill 的提示词

你的措辞与 skill 描述匹配时,智能体会选择它;直接指定 skill 名称更有把握。点击提示词即可复制。

loop-guard skill 如何打破 Claude Code 循环

这个 skill 为智能体提供判断循环的方法和跳出循环的流程:

  • 测试。 相同命令以相同输入失败了两次;第二次修复后又出现同样的错误;某个代码块不断被修改又撤销;两种修复方式交替出现;轮询结果一直不变。如果两次运行之间有变化,运行两次没有问题。
  • 先停下,再检查。 不再“再试一次”。智能体会完整读取最后一次错误,列出每次尝试及其确切结果。
  • 循环报告。 重复的内容、做过的尝试、错误的假设,以及下一步。错误假设这一行最关键:循环中的每次尝试都基于同一个判断。
  • 真正改变方法,依次尝试:阅读代码而不运行,检查环境,缩小故障范围,回到上一个正常状态,质疑测试或任务。用新方法尝试一次,然后评估结果。
  • 询问,当所有方法都已用尽,或剩下的“修复”只会掩盖问题时。

我们运行了什么,智能体做了什么

每个 skill 都在 2026年11月29日于专门为它创建的临时仓库中运行,并作为项目 skill 安装。测试用的是一个小型 Python 仓库,其中 tests/test_slug.py 在 sys.path 中把 tests/ 放在 src/ 前面,因此导入了模块的旧副本(tests/textkit/slug.py),对 src/textkit/slug.py 的每次编辑都不起作用。测试时手写了一份 NOTES.md,列出“上一个智能体”的四次尝试,每次都以相同的 AssertionError 结束。

Claude Code (claude -p --model sonnet)

My agent is stuck in a loop: it keeps editing src/textkit/slug.py and rerunning python3 tests/test_slug.py, same error every time (see NOTES.md). Break the loop and fix it. Say which skill you used, if any. Answer in English.

运行了两次。两次都调用了 loop-guard 和 Caprock 的 loop_alerts,打印出测试实际导入的文件,并修正测试中的 sys.path 顺序,没有更改断言或 src/textkit/slug.py。测试通过。两次都在删除过时的 tests/textkit/ 文件夹前询问,因为 git 跟踪了该文件夹。第一次运行的提示词没有以“Answer in English.”结尾。第二次运行的循环报告:

Loop detected: editing src/textkit/slug.py and rerunning python3 tests/test_slug.py, same AssertionError
Attempts (1-4 are from NOTES.md):
  1. Ran the test -> 'hello_world' != 'hello-world'
  2. Edited the regex in src/textkit/slug.py -> same error
  3. Rewrote slugify with str.replace -> same error
  4. Reverted step 3 -> same error
Assumption that was wrong: the test imports src/textkit/slug.py
Next (done): fixed the import path in the test

Codex (0.163.0,codex exec -s workspace-write,于 2026年11月30日)

The slug test still fails and I've tried fixing src/textkit/slug.py three times. Fix it.

找到并读取了 .agents/skills/loop-guard/SKILL.md,调用了 Caprock 的 loop_alerts,然后修复 tests/test_slug.py,让它先加载 src/,未改动断言;测试通过,git diff --check 无异常。报告以循环说明开头,但漏掉了 skill 格式要求的尝试记录和错误假设一行(链接以纯文本显示):

Loop detected: three source edits failed because the test imported the old `tests/textkit/slug.py` copy.
Fixed tests/test_slug.py to load `src` first. The assertion is unchanged.
`python3 tests/test_slug.py` passes, and `git diff --check` is clean.

已测试的智能体

  • Claude Code:测试于 2026年11月29日,使用 claude -p --model sonnet,skill 作为项目 skill 安装在 .claude/skills/ 中。
  • Codex:于 2026年11月30日使用 Codex 0.163.0(codex exec)测试,skill 位于 .agents/skills/。当天较早的一次 Codex 运行未安装 skill,不计入结果。
  • OpenCode 和 Cursor:未测试。此 skill 是以开放的 Agent Skills 格式编写的纯文本指令,但尚未在这两者上实际运行验证。

限制

  • skill 只能依据智能体能看到的内容工作。对于跨会话的循环,或智能体没有察觉的循环,需要你说“you're in a loop”。
  • 没有 Caprock 时,它从会话本身还原之前的尝试;压缩过的对话可能已丢失早期尝试。
  • 它不会限制重试次数,也不会终止进程。如果智能体无视指令,skill 无法阻止它。
  • Codex 在唯一一次运行中只写了一行循环报告,没有使用完整格式。

使用和不使用 Caprock 的情况

连接 Caprock MCP 服务器后,skill 会调用 loop_alerts,找出反复执行同一工具调用的运行中会话,并列出工具、次数、重复的输入和这些重复调用的花费。未连接时,智能体会从对话中列出自己的尝试。Caprock 是一款免费开源应用,记录你电脑上的每个编程智能体会话;此 skill 不依赖它。

获取 Caprock →

完整的 SKILL.md

这是 dspv/skills 中修订版本 dfce799 的文件原样。手动安装时,将它保存在项目中:Claude Code 使用 .claude/skills/loop-guard/SKILL.md,Codex 使用 .agents/skills/loop-guard/SKILL.md。

skills/loop-guard/SKILL.md
---
name: loop-guard
description: Notice when a coding agent is stuck in a loop (running the same failing command again, retrying the same tool call, editing and reverting the same file, hitting the same error after every "fix", or never stopping) and break out of it - stop, state what was tried, then change approach or ask the user. Use when someone says "claude code loop", "the agent keeps doing the same thing", "stuck in a loop", "it keeps retrying", "same error again", "it went back and forth on this file", "why won't it stop", or when you see yourself repeating a step.
license: Apache-2.0
---

# Loop guard

A loop is repeating a step and expecting a different result. It wastes time and tokens, and it
often ends with the agent "fixing" the problem by hiding it. This skill makes you notice the loop,
stop, say plainly what happened, and either try something genuinely different or ask the user.

## When this applies

Check yourself for a loop before every retry, and run the full procedure below as soon as any of
these is true:

- The same command or tool call, with the same input, has failed twice and you are about to run it
  a third time.
- The same error message (same file, line, or test name) comes back after your second fix.
- You are editing a hunk you already changed and reverted, or flipping between two versions of it.
- Two fixes alternate: fixing A breaks B, fixing B breaks A again.
- You are polling or waiting (a build, a server, a CI job) and nothing in the output has changed
  between checks.
- The user says you are looping, stuck, going in circles, or asks why you will not stop.

Running something twice is fine when something changed in between (a file, a setting, an input).
It is a loop when nothing that could change the result has changed.

## Procedure

### 1. Stop

Do not run the next attempt. No "one more try" with a small tweak to the same idea.

### 2. Look at the evidence, not your memory of it

- If the Caprock MCP tools are available (server `caprock`), call `loop_alerts` (it takes no
  arguments). It lists running sessions that repeat the same tool call, with the tool, how many
  times, the repeated input and what the repeats cost. Use it to confirm the loop and its size.
  Caprock (https://caprock.dev, free, open source, runs locally) provides this tool.
- Otherwise, scroll back through this session: list each attempt in order, with the exact command
  or edit and the exact result.
- Read the last error in full, top to bottom. Loops often come from reading only the first line,
  or the last line, of an error that says something else in the middle.

### 3. Write the loop report

Show the user this, before doing anything else:

```text
Loop detected: <what repeats, in one line>
Attempts:
  1. <what I did> -> <what happened>
  2. <what I did> -> <what happened>
  3. ...
Assumption that was wrong: <the belief every attempt shared>
Next: <the different approach>, or a question for you
```

The line "Assumption that was wrong" matters most. Every attempt in a loop shares one belief
(the bug is in this function, the dependency is installed, the test is right, the port is free).
Name it. If you cannot name it, that is the first thing to find out.

### 4. Change approach for real

A new approach tests a different assumption. Changing a flag, a timeout, a retry count or the
wording of the same edit is not a new approach. Pick one of these, in roughly this order:

1. **Read instead of run.** Open the source of the failing function, the config being loaded, the
   test's assertion, or the tool's `--help`. Many loops end once you read what the code really does.
2. **Check the environment.** Which version, which binary on PATH, which branch, which working
   directory, which env vars. Print them; do not assume.
3. **Make it smaller.** Reproduce the failure with the least code or the single test. A small
   failing case shows what the big one hides.
4. **Go back to the last good state.** If edits piled up, compare with the last commit (`git diff`,
   `git stash`) and reapply one change at a time.
5. **Question the target.** Maybe the test is wrong, the requirement is ambiguous, or the task
   needs something you do not have (a credential, a running service, a decision).

Allow yourself one attempt with the new approach, then judge it: did it change what you know? If
yes, continue. If no, go to step 5.

### 5. Ask the user

Ask when any of these is true:

- You have no approach left that tests a different assumption.
- The way forward needs a decision, a credential, access, or a change of scope.
- The only remaining "fixes" would hide the problem (see the list below).

Ask one specific question and include the loop report, so the user does not have to reconstruct
what happened. Then wait. Do not keep trying in the background.

## Never break a loop by hiding it

These end the loop by making the error disappear, not by fixing it. Do not do them unless the user
asks for that exact thing:

- Skipping, deleting or weakening a test or an assertion so it passes.
- Catching and ignoring the exception, or adding `|| true` to the failing command.
- Bypassing hooks or checks (`--no-verify`, disabling a linter rule, force-pushing).
- Hard-coding the expected output.
- Widening the task: rewriting a module to dodge one error.

## Waiting without progress

For a build, deploy or CI job you are waiting on:

- Before polling again, look for a sign of progress (new log lines, a changed status).
- If nothing changed over several checks, stop polling and report the state you saw and when you
  last saw it change. Ask whether to keep waiting, cancel, or look at the job's logs.

## Output

When you stop a loop, your reply to the user starts with the loop report from step 3, even when
the new approach then fixes the problem, and ends with either the result or one question. Keep it
short: what repeated, what you tried, what was wrong, what is next.

关于 loop-guard skill 的问题

什么是 Claude Code 循环?
智能体重复已经失败的步骤,却没有做出任何可能改变结果的调整:重复同一命令、每次修复后遇到同一错误,或反复修改又还原同一个文件。这会消耗时间和 token,最后往往只是把错误藏起来,而没有修复。
如何用 loop-guard skill 停止 Claude Code 循环?
安装 skill,然后说“you're in a loop, use loop-guard”。智能体会停下来,写一份指出错误假设的循环报告,然后尝试一种不同的方法,或向你提问。
loop-guard 会通过跳过失败的测试来让测试通过吗?
不会。这个 skill 将跳过或削弱测试、忽略异常、`|| true`、`--no-verify` 和硬编码输出列为掩盖循环的做法,要求智能体不要使用,除非你明确要求的就是其中某种做法。
循环防护 skill 能用于 Codex 吗?
可以。在 2026年11月30日的测试中,Codex 0.163.0 读取了它,指出循环及其原因,并修复了导入路径,没有修改断言。

来源

应用本身是英文界面。