Prevent file corruption during LLM-assisted editing with our backup-first strategy. This guide covers the complete workflow for safely modifying files using AI Toolbox tools.
The Safe Edit Guide provides a systematic approach to modifying files when working with AI assistants. By creating backups before making changes and verifying after editing, you can prevent data loss and recover from mistakes quickly.
Starting with version 1.5.12, four file-editing tools include built-in .bak rollback on write failure:
replace_text_in_file — restores original if atomic write failsinsert_at_line — same automatic restoreappend_file — same automatic restoredelete_lines_in_file — same automatic restoreWhat this means: If a write operation fails (disk full, permissions issue, etc.), the tool automatically copies the .bak backup back to the original file and returns an error. No manual intervention needed for common failure scenarios. The backup is still created before any modification attempt, so it's always available as a safety net even if the write succeeds.
LLM-assisted file editing can introduce unexpected changes:
Backups provide a safety net that allows you to recover quickly if something goes wrong.
This creates src/index.ts.bak with the original content.
Use AI Toolbox tools to modify files as needed:
save_file for complete file replacementreplace_text_in_file for targeted string replacementsinsert_at_line for adding lines at specific positionsdelete_lines_in_file for removing line rangesThis compares your edited file against the backup and reports differences.
List all files that will be modified during the session:
For each file you plan to edit, create a backup:
Each backup is stored as {filename}.bak in the same directory.
Option A: Replace entire file content
Option B: Replace specific text
Option C: Insert lines at specific position
Watch for these common issues during editing:
For each file you edited, verify the changes:
If you notice problems right after editing, restore the backup before doing anything else:
This replaces src/index.ts with the content from src/index.ts.bak.
If you're using version control, revert changes through Git:
If automated tools aren't available, manually restore from backup:
src/index.ts.bak)Always complete and verify changes to one file before moving to the next:
Break large edits into smaller, manageable changes:
Always verify each file before proceeding to the next:
Don't delete backups until you're certain the session is complete and stable:
Before starting any editing session, follow this checklist:
*.bak files)Symptom: node scripts/safe_edit.js verify src/file.ts reports "Backup not found"
Solution: The backup file doesn't exist. Create it first:
Symptom: File diff shows changes you didn't intend
Solution:
Symptom: Too many .bak files cluttering directories
Solution: Clean up old backups:
The Safe Edit Guide complements these AI Toolbox tools:
| Tool | Purpose | Reference |
|---|---|---|
save_file | Write file content with atomic operations | TOOLS_REFERENCE.md |
replace_text_in_file | Replace specific strings in files | TOOLS_REFERENCE.md |
insert_at_line | Insert content at specific line numbers | TOOLS_REFERENCE.md |
delete_lines_in_file | Remove line ranges from files | TOOLS_REFERENCE.md |
file_diff | Compare two files side by side | TOOLS_REFERENCE.md |
If you encounter issues with the Safe Edit workflow:
# Create backup of file before editing
node scripts/safe_edit.js backup src/index.ts
# Check that edits applied correctly
node scripts/safe_edit.js verify src/index.ts
# Remove backups when you're happy with results
node scripts/safe_edit.js cleanup --keep=0
# View current directory structure
list_directory(path=".")
node scripts/safe_edit.js backup src/file1.ts
node scripts/safe_edit.js backup src/file2.ts
node scripts/safe_edit.js backup config.json
# Check that backups exist
ls -la *.bak
# or use file metadata tool
get_file_metadata(path="src/file1.ts.bak")
Tool: save_file
Params: {
"file_name": "src/index.ts",
"content": "<new content here>"
}
Tool: replace_text_in_file
Params: {
"file_name": "src/file1.ts",
"old_string": "old function body",
"new_string": "new function body"
}
Tool: insert_at_line
Params: {
"file_name": "src/file2.ts",
"line_number": 10,
"content_to_insert": "// New comment line"
}
# Verify single file
node scripts/safe_edit.js verify src/index.ts
# Check for syntax errors (if TypeScript project)
npx tsc --noEmit
# Run linting
npm run lint
# Run tests
npm test
# Compare with backup
file_diff(file_a="src/index.ts.bak", file_b="src/index.ts")
# Remove all backups in current directory
node scripts/safe_edit.js cleanup --keep=0
# Keep last N backup files (useful for rolling back to earlier versions)
node scripts/safe_edit.js cleanup --keep=2
# Copy backup back to original location
node scripts/safe_edit.js restore src/index.ts
# View what changed
git diff src/index.ts
# Revert to last commit
git checkout HEAD -- src/index.ts
Tool: read_file
Params: { "file_name": "src/index.ts.bak" }
Tool: save_file
Params: {
"file_name": "src/index.ts",
"content": "<paste backup content here>"
}
# ✅ Good approach
backup src/file1.ts → edit → verify → cleanup
backup src/file2.ts → edit → verify → cleanup
# ❌ Bad approach (harder to isolate issues)
backup src/file1.ts, backup src/file2.ts
edit both files
verify both files together
# ✅ Good — small, focused replacements
replace_text_in_file(old_string="old function", new_string="new function")
replace_text_in_file(old_string="variable = X", new_string="variable = Y")
# ❌ Bad — large, risky overwrites
save_file(content="<entire file replaced>")
verify src/file1.ts # ✅ Check this is correct
edit src/file2.ts # ✅ Then move on
verify src/file2.ts # ✅ Check this too
# At end of session, after all verification
node scripts/safe_edit.js cleanup --keep=0
node scripts/safe_edit.js backup src/file.ts
node scripts/safe_edit.js restore src/file.ts
node scripts/safe_edit.js cleanup --keep=0 # Remove all
# or
node scripts/safe_edit.js cleanup --keep=2 # Keep last 2 versions