Fix the context reset rule for a churning agent
Your task
Deep into a long agent session on a Rust networking module, the agent fixed one borrow checker error and brought back another it had fixed an hour earlier. Then it kept churning: each edit fixed one thing and broke another. Its context window was full of stale attempts. Your team uses a rule to decide when to stop re-steering and reset the context from a clean checkpoint.
An assistant wrote churn_reset_index(errors, tolerance). It is in the editor. It has a bug. Find it and fix it.
Input and output:
errors: the error code left after each attempt, oldest first, likeE0502.""means that attempt built cleanly.tolerance: how many recurrences to allow before a reset.- Return the number of the attempt, counting from 1, at which to reset the context, or 0 if no reset is needed.
Rules the tests check:
- A
tolerancebelow 1 counts as 1. A reset always needs at least one recurrence as evidence. - A clean build (
"") is a known-good checkpoint. Forget everything before it: the errors seen and the recurrence count. - An attempt's error is a recurrence when the same error appeared earlier since the last clean build, and it differs from the error of the attempt just before it. The same error twice in a row means the agent is stuck, not churning, so it does not count. The first error after a clean build is never a recurrence.
- Return the number of the first attempt at which the recurrences since the last clean build reach
tolerance.
Examples
churn_reset_index(errors: list[str], tolerance: int) -> intExample 1
Input
churn_reset_index(["E0502", "E0106", "E0502"], 1)Output
3Example 2
Input
churn_reset_index(["E0502", "E0502", "E0502"], 1)Output
0Example 3
Input
churn_reset_index(["E0502", "E0106", "", "E0106", "E0502", "E0106"], 2)Output
0