Skip to main content
RapidDev - Software Development Agency
lovable-issues

Locking Code Sections from AI Edits in Lovable

Lovable does not have a native code locking feature, but you can prevent the AI from editing specific files by adding an AGENTS.md file to your project root with explicit rules like 'Do NOT modify src/integrations/supabase/client.ts.' Scope your prompts with @file references to limit changes to specific files. Use Plan Mode to review proposed changes before they are applied. For critical files, keep a copy in GitHub so you can revert if the AI modifies them despite your instructions.

Book a free consultation
4.9Clutch rating
600+Happy partners
17+Countries served
190+Team members
Intermediate9 min read~5 minAll Lovable versions (AGENTS.md supported since late 2025)March 2026RapidDev Engineering Team
TL;DR

Lovable does not have a native code locking feature, but you can prevent the AI from editing specific files by adding an AGENTS.md file to your project root with explicit rules like 'Do NOT modify src/integrations/supabase/client.ts.' Scope your prompts with @file references to limit changes to specific files. Use Plan Mode to review proposed changes before they are applied. For critical files, keep a copy in GitHub so you can revert if the AI modifies them despite your instructions.

Why code locking is not native to Lovable's AI workflow

Lovable's AI agent works by understanding your project's full codebase and making changes across whatever files it deems necessary to implement your request. This cross-file capability is one of its strengths — it can refactor imports, update type definitions, and modify dependent components in one step. However, it also means the AI may modify files you consider stable or finalized. This is a real problem in production projects. You might have a carefully configured Supabase client, a custom authentication flow, or a complex data transformation function that took many iterations to get right. If the AI edits these files while implementing an unrelated feature, it can break working functionality and burn credits trying to fix what it broke. Lovable provides indirect mechanisms to control this. The AGENTS.md file (placed in your project root) contains instructions that the AI reads at the start of every session. By listing files that should not be modified, you give the AI clear boundaries. The @file syntax in prompts scopes changes to specific files. Plan Mode lets you review what the AI intends to change before any code is modified. None of these are as strong as a true file lock, but together they significantly reduce unintended edits.

  • AI modifies stable files while implementing unrelated features because it has full codebase access
  • No AGENTS.md file with preservation rules — the AI has no boundaries on which files to edit
  • Prompts are too broad: 'Fix the login' may cause the AI to modify files beyond the login component
  • Plan Mode not used before implementing — changes applied without review
  • The AI's looping behavior may cause it to modify protected files while trying to fix a different bug

Error messages you might see

Previously working feature broke after an AI edit to an unrelated component

The AI modified a file that other features depend on. Add that file to the protected list in AGENTS.md and revert the change using version history.

AI keeps modifying vite.config.ts or tailwind.config.ts during unrelated changes

Configuration files are common targets for the AI when it encounters build issues. Add these to your AGENTS.md protected files list and reference specific files in your prompts to keep changes scoped.

Custom authentication flow replaced by AI-generated boilerplate

The AI regenerated the auth flow from scratch instead of making a targeted change. Use AGENTS.md to list auth files as protected and use @file references to scope future auth changes to specific files.

Before you start

  • A Lovable project with files you want to protect from AI modifications
  • Dev Mode access or GitHub integration to create the AGENTS.md file
  • Knowledge of which files in your project are stable and should not be changed

How to fix it

1

Create an AGENTS.md file with protection rules

AGENTS.md is read by the AI at the start of every session and establishes boundaries for code changes

Create a file called AGENTS.md in the root of your Lovable project (same level as package.json). In this file, list the files and directories that should not be modified. Use clear, direct language that the AI can parse. You can either create this file through Dev Mode or prompt Lovable: 'Create an AGENTS.md file in the project root with rules to never modify the following files: [list your files].' Root-level AGENTS.md files are always read regardless of session length.

Before
typescript
// No AGENTS.md exists
// AI has no boundaries and may modify any file in the project
After
typescript
// AGENTS.md in project root
# Project Agent Instructions
## Protected Files (DO NOT modify these files)
- src/integrations/supabase/client.ts
- src/integrations/supabase/types.ts
- vite.config.ts
- tailwind.config.ts
- src/contexts/AuthContext.tsx
- src/lib/utils.ts
## Rules
- Do NOT rename or move existing component files
- Do NOT change database table schemas without explicit permission
- Do NOT modify the routing structure in src/App.tsx unless specifically asked
- When adding new features, create new files rather than modifying existing ones
- Preserve all existing import paths when refactoring

Expected result: The AI reads AGENTS.md at the start of each session and respects the protection rules. Protected files are not modified during unrelated changes.

2

Scope prompts with @file references

Referencing specific files tells the AI exactly where to make changes, reducing the chance of touching other files

When giving the AI instructions, use the @filename syntax to scope your request to specific files. For example: '@src/components/UserCard.tsx add a loading skeleton to this component.' This tells the AI to focus on UserCard.tsx and avoid modifying other components. Without @file references, the AI decides which files to modify based on its interpretation of your prompt, which may include files you did not intend to change.

Before
typescript
// Broad prompt — AI may modify many files
"Add a loading state to the user profile"
After
typescript
// Scoped prompt — AI focuses on the specified file
"@src/components/UserProfile.tsx add a loading skeleton that shows while user data is being fetched. Use the Skeleton component from @/components/ui/skeleton. Do not modify any other files."

Expected result: The AI modifies only UserProfile.tsx. Other components and the data fetching logic remain untouched.

3

Use Plan Mode to review changes before implementation

Plan Mode lets you see exactly which files the AI will change before any code is modified

Switch to Plan Mode by clicking the mode toggle in the chat interface. Describe the change you want. The AI will create a detailed plan listing which files it intends to modify and what changes it will make. Review the plan carefully. If it includes files you do not want changed, tell the AI to adjust the plan. Once the plan looks correct, click 'Implement the plan' to have Agent Mode execute only the approved changes. Plan Mode costs one credit per message but prevents costly mistakes.

Expected result: You see a detailed plan of proposed changes before any code is modified. You can approve, modify, or reject the plan.

4

Keep a GitHub backup for protected files

If the AI modifies a protected file despite your instructions, GitHub provides a recovery point

Connect your project to GitHub via Settings → Connectors → GitHub. This creates a backup of every file change as a Git commit. If the AI modifies a protected file despite AGENTS.md rules, you can find the file's previous version in the GitHub commit history and restore it. You can also use Lovable's version history to revert, but GitHub provides a more granular file-level recovery option. If protecting critical code across a complex project requires cross-file safeguards, RapidDev's engineers have managed code preservation across 600+ Lovable projects.

Expected result: Every code change is tracked in GitHub. Protected files can be restored to any previous version if the AI modifies them unintentionally.

Complete code example

AGENTS.md
1# Agent Instructions for This Project
2
3## Critical Rules
4
51. NEVER modify files listed in the Protected Files section below
62. NEVER delete or rename existing component files
73. When adding new features, create NEW files instead of heavily editing existing ones
84. Ask for clarification before changing any database schema or table structure
95. Preserve all existing import paths do not reorganize the file structure
10
11## Protected Files (DO NOT EDIT)
12
13These files are stable and tested. Do not modify them for any reason:
14
15- `vite.config.ts` build configuration
16- `tailwind.config.ts` theme configuration
17- `src/integrations/supabase/client.ts` Supabase client setup
18- `src/integrations/supabase/types.ts` database type definitions
19- `src/contexts/AuthContext.tsx` authentication logic
20- `src/lib/utils.ts` shared utility functions including cn()
21- `src/App.tsx` route definitions (add routes only, do not restructure)
22
23## Code Style Preferences
24
25- Use TypeScript strict mode for all new files
26- Use the `@/` import alias (maps to src/)
27- Use shadcn/ui components from `@/components/ui/`
28- Use `cn()` from `@/lib/utils` for all class name merging
29- Name components in PascalCase, utilities in camelCase
30- Add inline comments explaining WHY, not WHAT
31
32## Project Architecture
33
34- Pages go in `src/pages/`
35- Shared components go in `src/components/`
36- Page-specific components go in `src/components/[page-name]/`
37- Hooks go in `src/hooks/`
38- API/data logic goes in `src/services/`

Best practices to prevent this

  • Create AGENTS.md as early as possible in your project — before the AI makes changes you need to protect
  • List specific file paths in AGENTS.md rather than vague rules like 'don't change important files'
  • Use @file references in every prompt to scope changes to the intended files
  • Switch to Plan Mode for any change that might affect multiple files — review the plan before implementing
  • Keep critical files version-controlled in GitHub so you can restore them if the AI ignores AGENTS.md instructions
  • Add coding style preferences to AGENTS.md so the AI generates consistent code that matches your existing patterns
  • Periodically check protected files in Dev Mode to verify they have not been accidentally modified
  • End prompts with 'Do not modify any other files' as an extra safeguard when making targeted changes

Still stuck?

Copy one of these prompts to get a personalized, step-by-step explanation.

ChatGPT Prompt

I'm using Lovable (lovable.dev) to build a project and the AI keeps modifying files I don't want changed. I need to create an AGENTS.md file that protects specific files. Files I want to protect: - [list your protected files] Please: 1. Write a complete AGENTS.md file with clear protection rules 2. Include code style preferences so the AI generates consistent code 3. Add architecture guidelines so new features go in the right directories 4. Suggest any additional rules based on common Lovable AI behaviors

Lovable Prompt

Create an AGENTS.md file in the project root. This file should instruct you to never modify the following files: vite.config.ts, tailwind.config.ts, src/integrations/supabase/client.ts, src/integrations/supabase/types.ts, and src/contexts/AuthContext.tsx. Include rules about creating new files instead of heavily editing existing ones, using TypeScript strict mode, and using the @/ import alias for all imports.

Frequently asked questions

Can I lock specific files from AI editing in Lovable?

There is no hard lock feature, but you can add an AGENTS.md file to your project root listing files the AI should not modify. The AI reads this file at the start of every session and follows the rules. Combine with @file scoped prompts and Plan Mode for stronger protection.

What is AGENTS.md and how does it work?

AGENTS.md is a Markdown file in your project root that provides persistent instructions to Lovable's AI agent. It is read at the start of every session regardless of session length. Use it to list protected files, code style preferences, and architectural guidelines.

Does the AI always follow AGENTS.md rules?

The AI respects AGENTS.md instructions in the vast majority of cases, but it is not a guarantee — especially during complex debugging loops where the AI may modify files trying to fix errors. Keep GitHub backup enabled so you can restore files if this happens.

How do I export my Lovable code to protect it?

Connect your project to GitHub via Settings → Connectors → GitHub. This syncs all code to a GitHub repository where it is version-controlled. You can also use Dev Mode to download individual files or the full project as a ZIP file.

What is Plan Mode and how does it help protect code?

Plan Mode is a non-executing mode where the AI analyzes your request and creates a detailed plan of proposed changes. It lists which files will be modified and what changes will be made. You review the plan before clicking 'Implement the plan' to execute it. It costs one credit per message.

How do @file references help prevent unwanted edits?

The @filename syntax (like @src/components/UserCard.tsx) tells the AI to focus on that specific file. It significantly reduces the chance of the AI modifying other files. Combine with 'Do not modify any other files' at the end of your prompt for maximum scoping.

What if I can't prevent the AI from breaking my code?

If the AI repeatedly modifies critical files despite AGENTS.md rules and scoped prompts, the issue may require isolating certain code into protected modules. RapidDev's engineers have set up code protection strategies across 600+ Lovable projects and can help structure your project to minimize AI interference.

RapidDev

Talk to an Expert

Our team has built 600+ apps. Get personalized help with your issue.

Book a free consultation

Need help with your Lovable project?

Our experts have built 600+ apps and can solve your issue fast. Book a free consultation — no strings attached.

Book a free consultation

We put the rapid in RapidDev

Need a dedicated strategic tech and growth partner? Discover what RapidDev can do for your business! Book a call with our team to schedule a free, no-obligation consultation. We'll discuss your project and provide a custom quote at no cost.