How to Control New Defects When AI Coding Tools Change Code
When an AI coding agent fixes one issue and breaks another, the usual cause is weak change control rather than weak code generation. This article explains a practical workflow: keep changes atomic, state explicit boundaries, make acceptance observable, review diffs, isolate session state, and stop when tests are rewritten.
1. The Real Issue: Not Whether the Agent Can Write Code, but Whether Change Is Controllable
When an AI coding agent edits code, familiar failures appear: a validation check is fixed, but the API layer loses compatibility; a style tweak makes tests fail; the agent says the work is done and tests all pass, yet manual review still finds missing cases. On the surface, the model “broke something while it was in there.” The deeper cause is usually missing change control: vague scope, unclear definition of done, long-lived reused context, acceptance that cannot be observed, and no review of the resulting change.
Blaming the tool itself leads to a loop of “just switch tools.” In practice, any code-generation approach can produce the same problems if the task is too large, the boundaries are unclear, and there is no verifiable completion standard. A more useful approach is to treat the coding agent as a fast collaborator that needs explicit constraints, and to manage its inputs, outputs, and acceptance with an engineering process.
The practices below can be applied directly. The core principles are: small changes, explicit constraints, observable acceptance, diff review, and state isolation.
2. Shrink Each Change to a Reviewable Atomic Task
The most common mistake is a task that is too broad, such as “refactor the login module” or “optimize the order flow.” These requests do not say which files may change, which layers must not be touched, or what “done” means. To finish the job, the agent may also adjust APIs, styles, utility functions, or even test assertions. New defects often hide in those incidental edits.
A safer approach is to change one logical block at a time and write the boundary clearly. Instead of “refactor login,” write:
Scope: adjust only the local validation logic of the login formAllowed changes: the form validation module and its direct dependenciesForbidden changes: the API adapter layer, style assets, and existing test assertionsDefinition of done:1. New and retained boundary cases all pass;2. The relevant test suite produces a complete log;3. The changed-file list matches the allowed scope;4. Test files are unchanged; if a change is required, explain why and wait for confirmation.
This task card is simple, but it limits the agent to a reviewable range. The more atomic the task, the smaller the diff, the lower the review cost, and the easier the rollback. For complex work, do not expect one conversation to finish the whole change. Split it into independently acceptable steps such as “add tests first, then change the implementation, then remove old logic.”
3. For Large Requests, Produce a Plan and Review the Impact First
When the request is genuinely large, letting the agent edit immediately is risky. A better step is to ask for a short plan: which files will change, why each file changes, which callers may be affected, and how the result will be verified. The plan does not need to be long—a few lines to a dozen is enough—but it must cover the impact surface.
When reviewing the plan, watch for three signals: unauthorized files, a “refactor” that expands into a cross-layer rewrite, and missing verification steps. Cut out-of-scope items immediately rather than repairing them after the edit.
Small changes do not need a heavy process every time. For a single file, a single function, and a clear boundary, execute the change and review the diff. Planning mode and heavy skill packs fit high-risk, multi-file, cross-module tasks. They should not be fixed overhead for every task. The process exists to control risk, not to add ceremony.
4. Make “Done” Observable: The Agent Runs the Tests, and the Output Must Be Reviewed
“Run the tests after the change” cannot stay a verbal request. A more effective acceptance method is to require the agent to run the project’s agreed test entry point and paste the command, environment, output summary, and failure details in full. A reply of “tests passed” is not enough, because the reviewer cannot tell which suite ran, whether key cases were skipped, or whether only one file was tested.
The definition of done should be observable, for example:
Acceptance record template:- Entry point: the relevant test suite agreed by the project- Scope: modules affected by this change and their direct dependencies- Result summary: passed, failed, and skipped counts- Failure handling: if tests fail, find the cause, fix it, and rerun- Attachment: full output or key log excerpts
If tests fail, the agent should fix the issue and rerun rather than leaving the human to cover for it. Passing tests are necessary, not sufficient. Cases the tests do not cover still need diff review and human judgment.
5. Review the Diff Even When Every Test Passes, Especially Files That Should Not Change
All tests passing does not mean the change is correct. Existing tests may cover only the main path. Boundary conditions, error branches, and compatibility logic may have no assertions. The agent may complete the feature while also changing configuration, utility functions, or unrelated modules, creating hidden risk.
A diff review does not have to inspect every line, but it should follow this priority:
- File list: only files inside the allowed scope changed; watch for sensitive files such as configuration, build scripts, and shared utilities;
- Critical logic: entry points, branch conditions, error handling, defaults, and permission checks were not changed unintentionally;
- Call relationships: callers of modified functions are still valid, and return-value meaning did not change;
- Test files: assertions were not rewritten, skip markers were not added, and mocks did not replace critical logic.
Diff review does not replace tests. It finds blind spots in the tests. For high-risk changes, ask the agent to map “change point — impact — verification method” in the submission notes so review misses are less likely.
6. If the Change Keeps Getting Worse, Reset: Restore Context Hygiene with a Handoff Note
A session that runs too long accumulates dirty context: early assumptions, abandoned approaches, temporary constraints, and known pitfalls mix together. The agent may keep circling old information. More prompts make the result worse, and fixing one point breaks another.
Do not keep stacking instructions in the old session. Reset decisively. Before resetting, write the goal, constraints, allowed scope, forbidden changes, approaches already tried, known pitfalls, and acceptance criteria into a project-level collaboration note or a handoff note, so the new session starts from a clean premise. Keep the handoff short and precise. Do not paste the full conversation history.
Handoff note example:Goal: fix login-form validation failures for empty and overly long inputConstraints: do not change the API layer, styles, or existing test assertionsDone: two boundary cases were added; the current implementation still failsRuled out: the API response structure is not the causeNext step: change only the validation module, run the relevant tests, and paste the output
Context hygiene is part of long-term efficiency. A reset is not a failure. It turns an uncontrollable conversation history into reusable engineering constraints.
7. Isolate Parallel Tasks with Separate Copies So They Do Not Overwrite Each Other
When several edits must move forward at the same time, multiple coding agents can work in parallel, but their directories must be isolated. Each agent uses its own copy and does not share a workspace, so they do not edit the same file and cause conflicts or overwrites. If one copy is damaged, discard only that copy. Other tasks stay intact.
The key is clear boundaries, not how many agents are running. Each copy maps to one atomic task with its own acceptance criteria. Review and merge only after each task is complete. Do not let multiple agents edit the same directory. Mixed diffs make rollback much more expensive.
8. Stop and Ask When Test Files Change
Test files are part of the acceptance baseline. If the agent changes assertions, adds skip markers, or replaces critical logic with mocks just to turn the suite green, a passing result no longer means the change was accepted. Mature models usually do not do this lightly, but any change to a test file should still pause the work. Ask which assertions changed, why, whether the original cases remain valid, and whether the requirement itself changed.
State this directly in the task card: test files are not allowed to change by default; if a change is truly required, explain the reason first and wait for confirmation. A suite that the agent made green by editing tests cannot be treated as accepted. Distinguish an implementation fix from a relaxed acceptance standard.
9. Pre-Close Checklist
Before ending each task, check the following:
- Can this task be split further? Are multiple logical blocks mixed into one change?
- For a large request, was the plan reviewed? Did any unauthorized file enter the plan?
- Did the agent run the tests? Are the command, scope, and output fully visible?
- Does the diff include files that should not change, especially configuration, shared utilities, and build scripts?
- Were test files modified? Do assertion changes, skip markers, or mocks weaken acceptance?
- Is the current session looping? Does it need a reset and a handoff note?
- Do parallel tasks use separate copy directories? Is there a risk of cross-editing?
Closing
The value of an AI coding agent is faster edits, but speed has to sit on change control. Keep tasks small, write boundaries clearly, turn acceptance criteria into observable output, make diff review a fixed step, reset when context gets dirty, isolate directories for parallel work, and strictly review test-file changes. That is how the chance of “fix one place and break two” stays within a controllable range. Tools will keep improving, but engineering constraints remain the base for stable delivery.
