code review agent stuck in infinite loop re-reading the same 10k-line diff
Fixes a review agent that never finishes because it keeps re-reading the same large diff. Use it when an agent loops over a big diff without making progress, burning time and tokens. Key trigger: the agent's logs show it fetching or scanning the same diff repeatedly with no new output.
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
code review agent stuck in infinite loop re-reading the same 10k-line diffSteps
- 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.
- 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.
- 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.
- 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
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.