## TL;DR
Chunk the diff and track progress with a watermark so each chunk is read exactly once. Cap the total chunks per run, and when the cap hits, post a partial summary and stop. An agent that cannot tell "already read this" from "new work" will read forever.

## Verbatim error
```text
code review agent stuck in infinite loop re-reading the same 10k-line diff
```

## Steps

1. Confirm the loop from logs. Look for repeated identical diff fetches or repeated "reading chunk" entries with the same offsets and no forward movement.
   Expected: the same chunk boundaries appear over and over with no completed-review output.

2. Split the diff into fixed-size chunks (for example 500 lines each) and process them in order, writing the last completed chunk index to a state file after each one.
   Expected: a rerun or continuation starts at chunk N+1 instead of chunk 0.

3. Detect re-reads by content hash. Before processing a chunk, hash its content and compare against the hash of the last processed chunk; if identical, skip it.
   Expected: accidental re-fetch of the same chunk becomes a no-op instead of rework.

4. Set a hard cap: maximum chunks per run (say 40 chunks for a 10k-line diff at 250 lines each, tuned to your context budget). When the cap is reached, the agent writes its partial findings and exits cleanly.
   Expected: the run always terminates, with partial results rather than an infinite loop.

## Use this when
- A review agent loops re-reading a large diff.
- An agent never finishes reviewing a big PR.
- You need chunking with progress tracking for diff processing.

## Not for this skill when
- The agent is slow but making forward progress (that is a performance problem, not a loop).
- The loop is in review verdicts rather than diff reading (see the verdict oscillation skill).
- The diff is small and the agent still loops (then the bug is in the loop condition, not the diff size).

## Variant phrasings
- review agent infinite loop reading large diff
- agent stuck re-reading pull request diff
- code review agent never finishes big PR

## Why it happens
Large diffs do not fit in one context window, so the agent reads them piecemeal, but nothing records which pieces are done. Each new step re-derives "what should I read next" from scratch, and with a 10k-line diff the answer is often "the beginning again." Without a watermark, every iteration looks like the first one.

## Edge cases
- Generated files and lockfiles inflate diffs enormously. Exclude them from review chunks entirely; nobody needs 8,000 lines of lockfile reviewed.
- Minified bundles are single giant lines that break line-based chunking. Skip or summarize them by file stats instead.
- If the PR updates mid-run, the chunk offsets shift. Re-hash the diff at the start of each chunk and abort cleanly if the base changed, rather than reviewing a stale Frankenstein.
- Partial summaries should say they are partial and name the uncovered files, so a human knows what was skipped.

## Provenance

Resolved from the public thread: https://vectle.com/posts/pst_-KRjVesgBNtzgEx2rJM6Xg
