AI Coding Agent Broke Your Code? A Step-by-Step Recovery Guide

    Learn how to assess changes made by an AI coding agent, roll back broken edits without losing useful work, and use a fresh agent session to diagnose the problem. The guide also covers four safeguards: version control, automated checks, limited permissions, and human code review.

    AI coding agents have moved beyond autocomplete. They can now plan tasks, read and write files, and run commands. That can save time, but it also means an agent can break a build, silently rewrite core business logic, or leave behind a diff so large that it’s hard to know where to start.

    If an agent breaks your project, resist the urge to have it immediately try another fix. First, find out what changed. Then contain the damage, verify the repair, and put safeguards in place for the next task.

    1. Find Out What the Agent Broke

    Before editing anything, establish the scope of the problem. Agent mistakes generally fall into three categories.

    Silent Changes in Behavior

    The hardest bugs to catch are often the ones that produce no errors. During a refactor, an agent might:

    • Change a boundary condition from > to >=.
    • Alter a function’s side effects in a way that breaks its callers.
    • Delete seemingly unused code that is actually reached through reflection or dynamic loading.

    The project may still compile and some tests may pass, even though specific inputs now produce incorrect results.

    Unnecessary Structural Changes

    An agent may go beyond what the task requires by adding third-party dependencies, reorganizing directories, renaming modules, or copying similar code into multiple places. Even if the requested feature works, these changes can make the patch harder to review and maintain.

    Changes Outside the Project

    An agent may modify global configuration, overwrite environment variables, or run deletion or overwrite commands without confirmation. If that happened, the repository diff won’t tell the whole story.

    If the project is under version control, start with git status and git diff. Review the changes file by file to separate intended edits from unexpected ones. If the project isn’t under version control, make that a priority before giving an agent another task.

    2. Contain the Damage: Preserve Evidence, Salvage Useful Edits, and Roll Back

    Save the evidence before discarding changes. For example, git diff > broken.patch saves the unstaged diff for later review. Check git status for staged and untracked files as well; a plain git diff does not capture them. The record can help you identify where the agent veered off course and decide which instructions or permissions to tighten next time.

    Keep only the parts worth saving. If the agent wrote a useful helper function amid otherwise poor changes, use git add -p to stage the useful hunks selectively. Don’t keep half-finished code just because some effort went into it. If you plan to run a hard reset, preserve the selected edits first: staging alone won’t protect them.

    Roll back when the changes aren’t worth repairing. If the diff is large and the overall quality is poor, returning to a known-good version is often faster than fixing it line by line. Check for unrelated work before running any destructive command.

    1. # See which files have changed
    2. git status
    3. # Discard unstaged changes to a specific tracked file
    4. git checkout -- path/to/file
    5. # Reset tracked files and the index to the current commit
    6. git reset --hard HEAD

    Use the file-level command for a targeted rollback. A hard reset discards both staged and unstaged changes to tracked files, including unrelated edits you haven’t saved.

    3. Repair the Code with a Fresh Agent Session

    Why a Fresh Session Can Help

    An agent that introduced the problem may keep patching its own changes instead of reconsidering the approach. Its conversation history may also contain faulty assumptions and failed attempts that steer it back toward the same mistakes.

    A different coding agent can provide a second opinion. If you only have one tool, start a new session and describe the problem from scratch. Give the agent the relevant code, symptoms, and diff without carrying over the previous troubleshooting conversation.

    Give the New Agent a Narrow, Verifiable Task

    A good repair prompt includes four things:

    1. Symptoms: What broke? Include error messages or describe the unexpected behavior.
    2. Scope: Which files changed, and which edits look suspicious?
    3. Constraints: Which modules are off-limits? Are new dependencies prohibited? Which tests must pass?
    4. Verification: Ask the agent to explain the root cause before editing, then propose the smallest fix.

    For example:

    1. Background: The login endpoint intermittently returns HTTP 500 after an automated refactor.
    2. Suspected change: Token validation was rewritten in auth/service.go. See the attached diff.
    3. Requirements:
    4. 1. Explain the root cause before editing any code.
    5. 2. Keep the fix minimal and do not modify other modules.
    6. 3. After applying the fix, run go test ./... and verify that all tests pass.

    Verify the Fix Yourself

    Run the full test suite, review the diff for critical code paths, and verify the main workflows in staging. A passing build is not enough, and the agent’s report is not a substitute for review. The agent can speed up the repair, but a human should sign off on it.

    4. Put Four Safeguards in Place

    1. Make Rollback Routine with Version Control

    • Make small commits so you can return to a recent working state.
    • Have the agent work on a separate branch and review its pull or merge request before merging.
    • Create a checkpoint commit before consequential changes.

    2. Run Tests and Static Checks

    Unit and integration tests help catch behavior changes that compilation won’t reveal. Run tests and lint checks automatically after agent edits so that “it builds” isn’t the only standard for accepting a change.

    3. Restrict Permissions and Scope

    • Use read-only or restricted mode when possible, and require human approval for commands that delete or overwrite files.
    • State exactly where the agent may make changes. For example: “Only modify files under src/service.”
    • Break larger tasks into small steps and give the agent one verifiable task at a time.

    4. Review the Diff, Not Just the Result

    Review agent-generated changes as closely as you would a contribution from a junior developer. Look at what changed, not just whether the requested feature works. Pay particular attention to boundary conditions, error handling, and changes in side effects.

    5. The Takeaway

    When a coding agent breaks your project, save what you need, roll back the bad changes, and use a fresh agent session to diagnose what went wrong and propose a minimal fix. For everyday work, use branch isolation, automated tests, restricted permissions, and human review. Those safeguards let you benefit from the agent’s speed without giving up control of the codebase.