The agent created a new file instead of editing the existing one. Now there are two.
The PR diff looks fine. You asked the agent to add a phone-number validator. It added a file called validation-helpers.ts with a clean, well-tested validatePhone() function. CI is green. The code is correct. It is also wrong. Because utils/validators.ts already exists. It has been there for two years. It already exports validateEmail() , validatePostalCode() , and a dozen other validators. The…
The agent generated a new file instead of modifying an existing one, resulting in two validator files. The first reason for this behavior is the shallow search capability of the agent, which only scans the root directory and common locations like src/ for existing functions. The second reason is that creating a new file requires less effort compared to editing an existing one, as it doesn't involve searching for the function, adapting to different styles, or managing compatibility issues.
Additionally, the task given to the agent didn't instruct it to verify if the required functionality already existed. The consequence of this issue is worse than creating a file in the wrong location, as it leads to duplicated functionality, which may go unnoticed until a bug is encountered in one of the validators. Over time, this accumulates, leading to a growing number of duplicate files in different directories.
To prevent this problem, it is advised to instruct the agent to search the codebase for similar existing functions before writing any new shared utility. A simple grep search for relevant keywords across the codebase can help the agent identify if the functionality already exists. Furthermore, specifying the expected location of the new utility file can eliminate ambiguity and prevent the creation of parallel files.
Ultimately, the main issue lies in the fact that agents are primarily designed to generate code, rather than adapting to existing systems. To address this, agents need to be prompted to consider where the new file should be placed and whether it already exists, as humans naturally would do.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.