Related Code Files:
safe_file_manager_undo_wrapper.py- Lightweight wrapper that adds undo trackingsfm_undo_integration.py- Deep integration module with hook systemsafe_file_manager.py- The core file management tooltext_operation_history.py- Multi-level undo systemtext_undo_system.py- Command-line interface for undo operations
The Safe File Manager Undo Integration provides automatic tracking of file operations performed through safe_file_manager.py, enabling multi-level undo capabilities for recovery from mistakes.
A drop-in replacement for safe_file_manager.py that intercepts operations and tracks them in the undo system.
Features:
- Transparent operation - no changes to safe_file_manager's behavior
- Automatic content tracking for create/write operations
- Operation type mapping to undo system categories
- Minimal overhead - only tracks on successful operations
Usage:
# Instead of:
./run_any_python_tool.sh safe_file_manager.py create file.txt --content "text"
# Use:
./safe_file_manager_undo_wrapper.py create file.txt --content "text"Environment Variables:
SFM_NO_UNDO=1- Disable undo tracking for specific operations
A more sophisticated approach that integrates with safe_file_manager's internal transaction system (if available).
Features:
- Pre/post operation hooks
- Transaction-aware tracking
- Binary file handling
- Atomic operation support
Setup:
# Install hooks
python3 sfm_undo_integration.py
# Disable hooks
python3 sfm_undo_integration.py disable| safe_file_manager Command | Undo Operation Type | Content Tracked |
|---|---|---|
| create | CREATE | New content |
| write | WRITE | Old + New |
| copy/cp | CREATE | New content |
| move/mv | RENAME | Path change |
| trash/rm | DELETE | Old content* |
| chmod | MODIFY | Permission |
| chown | MODIFY | Ownership |
*Note: Delete operations require pre-capture of content
# List recent file operations
./run_any_python_tool.sh text_undo_system.py list --recent 10
# Show operations for specific file
./run_any_python_tool.sh text_undo_system.py list --file myfile.txt# Undo last operation
./run_any_python_tool.sh text_undo_system.py undo
# Undo specific operation
./run_any_python_tool.sh text_undo_system.py undo <operation-id>
# Interactive undo
./run_any_python_tool.sh text_undo_system.py undo --interactive# Create file with undo tracking
./safe_file_manager_undo_wrapper.py create config.json --content '{"key": "value"}'
# Output: ✓ Operation tracked for undo: create_1234567890
# Oops, wrong content\! Undo it
./run_any_python_tool.sh text_undo_system.py undo
# File removed, ready to recreate# Enable undo for batch operations
export SFM_ASSUME_YES=1
# Perform multiple operations
for file in *.tmp; do
./safe_file_manager_undo_wrapper.py trash "$file"
done
# Review what was done
./run_any_python_tool.sh text_undo_system.py list --recent 20
# Undo if needed
./run_any_python_tool.sh text_undo_system.py undo --interactive# In CI scripts, disable undo to avoid overhead
export SFM_NO_UNDO=1
./safe_file_manager_undo_wrapper.py create build/output.txt- Before Operation: Capture existing file content (if applicable)
- Execute Operation: Let safe_file_manager perform the operation
- After Success: Capture new state and record in undo system
- On Failure: Discard tracking data
- Text files: Full content tracked (for reliable undo)
- Binary files: Only metadata tracked (size, permissions)
- Large files: Consider disabling undo with
SFM_NO_UNDO=1
- Delete Recovery: Wrapper cannot capture content before deletion without modifying safe_file_manager
- Atomic Operations: Some complex operations may not be fully reversible
- Binary Files: Content recovery limited to text files
- Deep Integration: Modify safe_file_manager.py to call undo hooks directly
- Binary Support: Add binary file recovery via temporary copies
- Batch Undo: Group related operations for single undo
- Remote Sync: Track operations across machines
# Check if undo system is available
python3 -c "import text_operation_history; print('Undo system available')"
# Check operation history
ls -la ~/.undo_history/# Disable for large operations
SFM_NO_UNDO=1 ./safe_file_manager_undo_wrapper.py copy large_dir/ backup/
# Or use original tool
./run_any_python_tool.sh safe_file_manager.py copy large_dir/ backup/# Test wrapper directly
python3 safe_file_manager_undo_wrapper.py --help
# Check Python path
which python3
python3 --version- Use Wrapper for Interactive Work: Human errors benefit most from undo
- Disable for Automation: CI/CD rarely needs undo overhead
- Review Before Undo: Always check what will be undone
- Test First: Try undo on test files before production use
The Safe File Manager Undo Integration provides a safety net for file operations without changing the core tool's behavior. Choose the wrapper for simple drop-in undo support, or the hook system for deeper integration.