How to Recover from a Bad Automated Migration
Hướng dẫn chi tiết về How to Recover from a Bad Automated Migration trong Vibe Coding dành cho None.
How to Recover from a Bad Automated Migration
The adrenaline spike is universal. You’ve just spent the last hour orchestrating a complex Vibe Coding session. You gave your AI agent a bold directive: “Migrate this entire frontend from React Class Components to Functional Components with Hooks,” or perhaps, “Extract every hardcoded string into a localized i18n JSON structure.” You watched the terminal flicker with hundreds of file updates. It felt like magic—until you ran the build command.
Error: Objects are not valid as a React child...
Property 't' does not exist on type...
TypeError: Cannot read properties of undefined...
You check git status. Over 400 files modified. The “magic” has just turned into a massive technical debt spike. This is the dark side of Vibe Coding: when the speed of AI-driven transformation outpaces the reliability of its output.
Recovering from a bad automated migration isn’t just about hitting git reset --hard. It’s about surgical restoration, pattern recognition, and turning a catastrophic failure into a controlled recovery. In this guide, we’ll walk through the exact steps to rescue your codebase without losing the progress that did work.
Core Concepts: Why Migrations Fail in Vibe Coding
Before we dive into the recovery, we must understand the “Failure Modes” of AI-driven migrations. In Vibe Coding, we rely on Large Language Models (LLMs) to perform repetitive structural changes. However, LLMs are probabilistic, not deterministic.
- The Hallucination Loop: The AI assumes a library or a helper function exists because it “should” exist according to common patterns, but it hasn’t been defined in your specific project.
- Context Fragmentation: When migrating 50 files in a batch, the agent might lose track of the global state (e.g., updating the component but forgetting to update the corresponding Type definition in a separate file).
- The “Greedy” Regex: If the agent uses regex-based replacements instead of AST-based (Abstract Syntax Tree) transformations, it might inadvertently break logic that looks like the target pattern.
- Partial Application: The agent finishes 80% of a file but gets interrupted or hits a token limit, leaving the file in an unparseable state.
The Recovery Decision Tree
When you face a broken migration, you have three strategic paths:
- Path A: The Nuclear Option (Rollback): Abandon the changes. Use this if >90% of the migration is fundamentally flawed.
- Path B: The Surgical Fix (Fix-Forward): Keep the changes but use a new AI session to hunt and fix specific error patterns. Use this if the core logic is sound but syntax is broken.
- Path C: The Hybrid Revert: Rollback specific directories while keeping others.
The Recovery Workflow: A Practical Example
Let’s imagine a scenario where you attempted to migrate a legacy Astro project to a strict i18n structure. The AI agent replaced <h1>Welcome</h1> with <h1>{t('welcome_title')}</h1> across 100 files, but it forgot to import the t function or initialize the translation hook in 40 of them.
Step 1: Isolate the Damage
First, do not attempt to fix anything until you know exactly what is broken. Use the power of the CLI to categorize the failures.
# Find all files where {t( is used but 'useI18n' is NOT imported
grep -rL "useI18n" src/pages | xargs grep -l "{t("
This command gives you a “Target List” of files that are logically inconsistent.
Step 2: Create a “Recovery Guard” Branch
Never fix a failed migration on the same branch where the failure occurred.
git checkout -b recovery/fix-i18n-migration
Step 3: Orchestrate a “Fix-It” Sub-Agent
Instead of manually editing 40 files, you should use a specialized AI sub-agent. In Vibe Coding, we don’t just “fix code”; we “build a process that fixes code.”
The Directive for your AI Agent:
“I ran an automated i18n migration that failed. I have 40 files that use the
{t(...)function but lack the necessary importimport { useI18n } from '@/hooks/useI18n'and the hook initializationconst { t } = useI18n().Your task:
- Scan
src/pagesfor these specific missing patterns.- Surgically insert the import at the top and the hook call at the start of the component function.
- Do not modify any other logic.
- Validate each file by running a dry-run lint check.”
Step 4: Visual Regression and Snapshotting
Sometimes the code compiles, but the UI is a mess. In Vibe Coding, we use Snapshot Layouts.
If you have access to a tool like pencil or a visual regression suite, run a snapshot of the broken state vs. a known good state (from your main branch). If the layout rectangles have shifted significantly (e.g., a button is now 0px wide), you know the migration broke the CSS class application.
How it Works: The “Diff-Check” Logic
The most effective recovery technique is the Migration Shadowing.
When an automated migration happens, the AI often changes more than it admits. To recover, we compare the “Ideal Logic” with the “Broken Syntax.”
- The Reference: Your
HEADbefore the migration. - The Result: The current broken state.
- The Synthesis: Using a tool like
git checkout -p.
git checkout -p is the secret weapon of the intermediate Vibe Coder. It allows you to go through every “hunk” of the migration and decide: “Keep this change, but revert that one.” This prevents the “Nuclear Option” while maintaining high-quality control.
Best Practices for Migration Safety
Recovery is expensive. Prevention is cheap. To avoid these “bad migration” scenarios in the future, adopt the following Vibe Coding standards:
1. The Atomic Batch Rule
Never allow an AI agent to modify more than 10-15 files in a single turn. Large batches lead to context “bleeding” where the AI starts mixing up variable names from different files. If you have 500 files to migrate, instruct the agent to work in batches of 10 and verify each batch before moving on.
2. The “Pre-Flight” Test Suite
Before running any migration, ensure you have a “Green State.”
- Run your tests.
- Run your linter.
- Commit everything. If the migration fails, you have a mathematically perfect point of return.
3. Automated Validation Gates
Incorporate a “Validation Turn” into your Vibe Coding workflow. Once the AI says “I have finished the migration,” your next command should always be a validation script, not a “Thank you.”
# Example Validation Directive
"Now that you've migrated the files, run 'npm run check' and 'npm test'. If any file fails, immediately list the errors and propose a fix for the first 3 failures."
4. Use Git Worktrees for Parallel Comparison
If you are deep in a recovery, use git worktree to have the “Good Version” and the “Broken Version” open in separate folders simultaneously. This allows the AI agent to “read” the old logic and “write” the fix in the new version without constantly switching branches.
Interactive Exercise: Finding the “Ghost” Hallucination
Imagine the AI migrated your API calls to a new apiClient but hallucinated a method called .fetchWithRetry().
The Discovery Command:
grep -r "fetchWithRetry" src/ | xargs grep -L "function fetchWithRetry"
This identifies every file calling a function that doesn’t actually exist in the codebase. Once identified, you can point your agent to the apiClient definition and say: “Either implement fetchWithRetry here or revert the calls in the following files to use the standard .fetch().”
Conclusion
A bad automated migration feels like a step backward, but in the world of Vibe Coding, it’s actually a diagnostic opportunity. It reveals the weak points in your project’s architecture—unclear boundaries, missing types, or inconsistent naming conventions.
Recovery isn’t just about fixing the code; it’s about refining your instructions. The next time you prompt a migration, you’ll include the lessons learned from the failure: “Ensure you import the hook,” “Keep the CSS classes intact,” “Work in small batches.”
By mastering the tools of recovery—grep, git checkout -p, and surgical sub-agents—you transform AI from a risky “black box” into a powerful, controlled engine for evolution. Don’t fear the breakage; embrace the restoration. That is the path to becoming an elite Vibe Coder.