A loop repeats actions without new information. Common shapes are one action three times in a row and a two-action cycle such as ABAB.
Logs are data for the detector, including file contents and tool output. Prompt injection through files and logs is handled by treating observations as data, never as new instructions.
Pattern | Schematic action tail | Meaning |
|---|---|---|
Triple repeat | test, test, test | The same action stalls twice in a row. |
Two-action cycle | read, edit, read, edit | Two steps alternate with no new result. |
Progress | read, edit, test, read, edit | No triple or cycle in the tail; the run continues. |
The program below scans synthetic action names with two rules. Triple repeat and an ABAB cycle each need two equality comparisons per scan.
import sys
def is_stalled(actions):
n = len(actions)
if n >= 3 and actions[n - 1] == actions[n - 2] and actions[n - 2] == actions[n - 3]:
return True
if n >= 4 and actions[n - 1] == actions[n - 3] and actions[n - 2] == actions[n - 4]:
return True
return False
def check(cond, msg):
if not cond:
print("FAIL: " + msg)
sys.exit(1)
t1 = ["read", "edit", "test", "read", "edit"]
t2 = ["read", "edit", "test", "test", "test"]
t3 = ["read", "edit", "read", "edit"]
t4 = ["read", "read", "edit", "test"]
check(is_stalled(t1) is False, "trace 1 got unexpected stall verdict")
check(is_stalled(t2) is True, "trace 2 got unexpected stall verdict")
check(is_stalled(t3) is True, "trace 3 got unexpected stall verdict")
check(is_stalled(t4) is False, "trace 4 got unexpected stall verdict")
check(is_stalled([]) is False, "empty history got unexpected stall verdict")
check(is_stalled(["read"]) is False, "single entry got unexpected stall verdict")
print("PASS: loop detector flagged 2 of 4 traces and cleared short histories")Two of the four schematic traces stall: one triple and one cycle. Short histories under three entries never stall.
comparisons per scan = 2 triple checks + 2 cycle checks = 4 (stated example values)
scans = 20
total = 20 x 4 = 80 comparisons
detection lag: triple repeat found at the 3rd identical action
2-step cycle found at the 4th action of the ABAB patternA stop names its reason: done, step budget, token budget, or loop. The report lists completed steps, the stop reason, and pointers to external notes.
Stopping thresholds vary by task: measure them. A threshold that always continues is not a control.
Stop reason | When it fires | Report line |
|---|---|---|
Done | All success criteria check out | Criteria met with check outputs attached. |
Step budget | Next step would exceed the count | Stopped before the named step number. |
Token budget | Next step would exceed the tokens | Stopped with tokens used and step named. |
Loop | Triple repeat or ABAB cycle found | Stopped with the matching tail quoted. |
It stalls. Positions 1 and 3 match and positions 2 and 4 match, which is the ABAB cycle rule.
It does not. The last three differ and the last four are not an ABAB cycle, so both rules stay quiet.