---
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.
<!-- ‹guest · 2026-11-29› -->
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).
<!-- ‹guest · 2026-11-29› -->
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.
این نسخه با نشانهگذاری اختصاصی حساب شما نمایش داده میشود. برای نصب سریع، به تب نصب و راهاندازی بروید.