The Problem with String Replacement
When AI assistants try to modify code, simple string replacement (e.g., "replace X with Y") often fails. Code files frequently contain duplicate strings (like return true; or common variable names), leading to accidental modifications in the wrong places.
The Smart Patch Solution
Flowork OS implements a "Smart Patch" system. Instead of searching for text, Mother AI specifies exactly which lines to replace using 1-indexed line numbers.
How It Works
1. Mother AI uses read_files or analyze_code to view the file content, which is returned with line numbers prepended (e.g., 1: function init() {).
2. The AI identifies the exact block of lines it wants to change.
3. It sends a payload to POST /api/ai-smart-patch detailing the target file, the start line, the end line, and the new content.
Bottom-Up Application
Crucially, when applying multiple patches to a single file, the Go Engine applies them in reverse order (bottom-up).
If you replace lines 1-5 with 10 lines of code, all subsequent line numbers in the file shift down by 5. By applying patches from the bottom of the file upwards, the line numbers for earlier patches remain accurate.