How to Reign in an Agent That Wants to Rewrite Everything
Hướng dẫn chi tiết về How to Reign in an Agent That Wants to Rewrite Everything trong Vibe Coding dành cho None.
How to Reign in an Agent That Wants to Rewrite Everything
You know the feeling. You’ve just spent three hours getting a complex state machine working in your React application. It’s not “perfect” code—there are a few nested ternaries and a slightly hacky workaround for a third-party library bug—but it works. You ask your AI coding agent a simple question: “Can you add a loading spinner to the submit button?”
Ten seconds later, the progress bar finishes. You look at your terminal. 54 files changed, 1,200 additions, 900 deletions.
The agent didn’t just add a spinner. It decided your state machine was “suboptimal.” It refactored your entire authentication flow, renamed your utility functions to follow a different naming convention, and swapped out your CSS-in-JS for Tailwind because it felt “more modern.” Your app is now a sea of red squiggles, and your original logic is buried under a mountain of “clean” code that doesn’t actually run.
In the world of Vibe Coding, where speed and intuition are our primary drivers, this “Agent Overreach” is the single greatest threat to productivity. To build at the speed of thought, you need an agent that acts like a surgical laser, not a sledgehammer. This article will show you exactly how to reign in an over-aggressive agent and force it to respect your scope, your style, and your sanity.
The Psychology of the “Idealist” Agent
To control the agent, you must first understand why it wants to rewrite your world. Most modern LLMs (Large Language Models) are trained on a massive corpus of “perfect” code—open-source repositories that have been polished, linted, and reviewed. Furthermore, their RLHF (Reinforcement Learning from Human Feedback) training often rewards “helpfulness” and “completeness.”
When an agent sees a piece of “real-world” code—code that is messy, constrained by deadlines, or filled with legacy quirks—its internal “Helpfulness Bias” triggers a rescue mission. It isn’t trying to be annoying; it genuinely believes that by “fixing” your architecture, it is providing a higher-value service.
In Vibe Coding, we call this the Idealist Trap. The agent prioritizes structural perfection over functional stability. Our job is to shift its priority back to the Pragmatic Objective.
Core Concepts: The REIGN Framework
To maintain control over an autonomous agent, we use a framework called REIGN: Restrict, Explicit, Incremental, Guard, and Normative.
1. Restrict the Search Surface
The more an agent sees, the more it wants to change. If you give an agent access to your entire /src directory for a simple UI fix, it will start drawing connections between unrelated components and suggest “global” refactors.
The Fix: Use surgical tool calls. Instead of letting the agent “explore the codebase,” tell it exactly which files to read. Use specific line ranges. If your agent uses grep_search or glob, look at the logs. If it’s searching too broadly, interrupt and narrow the scope.
2. Explicit Non-Goals
Most developers focus on telling the agent what to do. In Vibe Coding, telling the agent what not to do is just as important. These are “Non-Goals.”
The Fix: Every prompt for a complex task should include a “Constraints” section. For example: “Add the loading state to the button. CONSTRAINTS: Do not refactor the parent component. Do not change the existing variable names. Do not update the CSS variables in theme.ts.”
3. Incremental Turn-Taking
The “Rewrite Everything” disaster usually happens when you give one massive, multi-step instruction. The agent gets lost in the middle and starts improvising.
The Fix: The One-Turn, One-Task rule. Break your “Vibe” into micro-tasks.
- “Identify the button component.”
- “Add a
loadingprop to the interface.” - “Implement the CSS for the spinner.”
- “Wire it to the form state.” By forcing the agent to stop and report back after each step, you catch overreach before it hits the disk.
4. Guarded Execution
Autonomous agents often want to write_file immediately. In high-risk areas of your code, you should enforce a “Read-Only Research” phase.
The Fix: Ask the agent to “Propose the diff in text before applying.” This forces the LLM to simulate the change in its internal workspace. If the proposed diff looks like it’s touching unrelated code, you can say: “That’s too much. Only change lines 45-50.”
5. Normative Checkpoints (GEMINI.md / AGENTS.md)
Agents are highly sensitive to “Global Instructions.” If you have a file in your root directory named GEMINI.md or AGENTS.md, most advanced agents are instructed to read it at the start of every session.
The Fix: Encode your “Anti-Rewrite” policy directly into the project context. Example entry: “NEVER refactor existing code unless explicitly asked. Prioritize local changes over global consistency. If you see ‘messy’ code that isn’t related to your task, IGNORE IT.”
Practical Example: The Surgical Strike
Let’s look at a real-world scenario. You have a legacy AuthService.ts that is 500 lines of spaghetti code. You need to change the API endpoint from /v1/login to /v2/auth/login.
The “Rewrite Disaster” Prompt:
“Update the login endpoint in AuthService.ts to the new v2 path.”
The Result: The agent reads the file, realizes the class uses XMLHttpRequest instead of fetch, and decides to rewrite the entire class using async/await, axios, and a new Error Handling middleware. It changes the return types from any to specific interfaces it created. Now, 15 components that import AuthService are broken because the types no longer match.
The “Surgical Vibe” Prompt:
“I need a surgical update to
src/services/AuthService.ts.Goal: Change the login endpoint string. Research: First, use
grepto find the exact line where the string ‘/v1/login’ is defined. Constraint: DO NOT refactor the class. DO NOT change the method signature. DO NOT convert from XHR to fetch. Action: Replace only the string literal. Use thereplacetool for only that specific line. Verification: Runnpm run test:authafter the change.”
The Result: The agent finds the line, replaces the string, and stops. Total files changed: 1. Total time: 15 seconds. Regressions: 0.
Best Practices & Tips for Intermediate Vibe Coders
Use “Contextual Humility” Prompts
If you sense an agent is getting “refactor-happy,” use a meta-prompt to reset its behavior:
“I noticed you’re proposing changes to several files that weren’t mentioned in the original request. Please stop. We are in ‘Hotfix Mode.’ Only perform the minimal changes necessary to satisfy the current objective. If you see code quality issues elsewhere, list them in a ‘Technical Debt’ note at the end but do not modify the files.”
Leverage the grep Tool
One of the best ways to keep an agent in line is to forbid it from using read_file on large files until it has used grep.
“Do not read the whole file. Search for the keyword ‘handleSubmit’ and give me 5 lines of context around it.” This prevents the agent from “ingesting” the whole file and getting ideas about how to “improve” the rest of it.
The “Diff-Check” Skill
If your agent has access to a git tool, make it a habit to ask:
“Show me the
git diffof what you just did. If you modified anything I didn’t ask for, revert those specific lines immediately.” Autonomous agents are surprisingly good at self-correcting when they see their own diff and realize they’ve gone off-script.
Define Your “Stack” in Project Context
Often, an agent rewrites code because it doesn’t know what your “Standard” is. If you prefer Function Components but the agent sees one Class Component, it might try to “upgrade” the whole folder.
The Fix: In your tech-stack.md or project guidelines, be explicit:
“We use a mix of legacy and modern patterns. DO NOT modernize legacy code on sight. Only use the patterns present in the specific file you are editing.”
Advanced Technique: The “Surgical Lock”
When you are working on a particularly fragile part of the system—say, a payment processing script or a complex regex—you can use what we call a Surgical Lock.
This involves creating a temporary comment at the top of the file:
// AGENT_LOCK: ONLY_EDIT_LINES_120_TO_150
Then, tell the agent:
“Read the
AGENT_LOCKcomment at the top of the file and strictly adhere to it. Any changes outside that range will be considered a failure.”
LLMs are excellent at following “in-band” instructions (comments within the code) because they are processed as part of the core logic of the file, giving them higher “attention weight” in the model’s processing.
Why This Solves the Vibe Coding Problem
Vibe Coding is about flow. The moment an agent rewrites 50 files, your flow is shattered. You are no longer “coding at the speed of thought”; you are now a “Code Reviewer” for an over-eager junior developer who just broke your build.
By reigning in the agent, you transform it from an autonomous “Architect” (which you don’t need for a loading spinner) into a highly efficient “Surgical Assistant.” This allows you to:
- Save Tokens: Reading and writing 50 files costs 100x more than editing 5 lines.
- Reduce Debugging: Minimal diffs mean minimal surface area for new bugs.
- Maintain Ownership: You stay the master of the architecture, while the AI handles the mechanical implementation.
Conclusion
The goal of Vibe Coding isn’t to let the AI take over the keyboard—it’s to use the AI to amplify your intent. An agent that rewrites everything is an agent that isn’t listening to your intent.
By applying the REIGN framework—Restricting scope, being Explicit about non-goals, working Incrementally, Guarding execution, and setting Normative standards—you turn the agent into a precision tool.
Next time your agent tries to “fix” your whole project, don’t just undo the changes. Teach it the “Vibe.” Force it to be surgical. Remember: In the age of AI, the best architect isn’t the one who writes the most code; it’s the one who knows exactly which three lines to change.