The loop is familiar: the agent fixes function A, your tests catch a broken import in module B. You ask it to fix B, it does, and the next turn quietly reverts the earlier fix. Ten turns later you are reviewing a 400-line diff for what started as a 20-line change. The agent is not stupid. It is editing from a partial view, and the partial view keeps betraying it.
The mechanism: a view problem, not a model problem
With 100,000+ tokens of repository content in context, model retrieval degrades toward the middle of the prompt: the code the agent saw ten turns ago is technically still "in context" but no longer reliably recalled. What the model actually edits with is whatever it can still see clearly: the current function, fragments of neighbors, and its own recent output. Anything it half-remembers, it fills in from priors. That is where collisions come from: the agent writes code against the interface it remembers, and the real code on disk has different signatures.
Three collision patterns
- Signature drift. The agent calls a helper with parameters as it remembers them. The helper was refactored last week, or the agent never read its full definition, only a fragment. The edit compiles in the agent's head and fails in your build.
- The re-fix loop. To fit the growing history, sessions get trimmed mid-conversation. The trim drops the turn where the agent already fixed something, so the "fix" disappears from its world model and it confidently re-applies the original broken pattern. Editing the middle of a conversation destroys the very context that made the first fix work.
- Stale reads. A teammate or another agent changed the file on disk, but the agent is still looking at bytes it read an hour ago. It edits stale code and overwrites the newer change.
What actually shrinks the collision rate
- A complete, small view beats a huge, partial one. The agent needs the target function plus the typed signatures of everything it touches, not 2,000 lines of bystander code. Signatures carry the contracts: a folded helper still tells the model which parameters exist and what it returns, at 1% of the tokens. Collisions happen when contracts are missing, not when bodies are missing.
- Fresh bytes on every edit. A read cache that keys on file hashes serves repeat reads instantly and invalidates the moment the file actually changes. That kills the stale-read pattern without re-paying for unchanged files.
- Append-only history. Never rewrite the middle of a conversation. If context must shrink, trim from the edges with the earlier fixes summarized, not silently deleted. This is also what keeps provider prompt caches alive, which is its own bill-saving story: KV-cache prefix locking explained.
- Shorter sessions per task. One task, one session. Cross-task context is where partial views compound.
The measured version
In our reference harness across 10 agent tasks, replacing full-file reads with sliced reads and deduplication cut context from 169,761 tokens to 8,178, a -95.2% reduction, with the worst single operation at -99.4%. The sliced byte counts are measured; the baseline comparison is modeled, and we label the split on every number we publish (public benchmark).
Honest boundary: precise context does not make a model hallucination-proof. It removes the stale and partial views that force the model to guess, which is where most edit collisions live. Your test suite stays the final judge either way.
The same mechanics at multi-agent scale, where partial views multiply by worker count, are in multi-agent workflows without the context tax and the Antigravity context window guide.
Set it up
npx tokensafe@latest init
TokenSafe hooks into your agent locally: file reads return the target function plus 1-hop typed contracts, repeat reads hit a hash-validated cache, and conversation prefixes stay append-only. See the quickstart docs or start a 7-day team trial to cut the collision rate across the team.
The agent is not careless. It is editing the real repository through a keyhole. Widen the keyhole and the loop stops.