Edit: Code Exe Md prompt to further discourse line by line edits, multiple seperate f.write calls, complex string manipulation. encouraged reading, implement fix, and check syntax. context a little bulky but working for now.
deci committed
May 12, 2025 at 19:00 UTC
856f78145595c4c51f3b00c3130e94e62dab04c0
1 file changed
+38
prompts/default/agent.system.tool.code_exe.md
+38
@@ -130,7 +130,45 @@ Execute commands and code for computation, data analysis, and file operations.
130
- Create debugging scripts to test environment setup
131
- Use explicit path variables for consistency
132
133
+### FILE EDITING BEST PRACTICES
134
+When editing files, especially code files:
135
+
136
+1. ALWAYS use the "read entire file → modify in memory → write entire file" pattern:
137
+ ```python
138
+ # CORRECT APPROACH - Read, modify in memory, write as one operation
139
+ with open(file_path, 'r') as f:
140
+ content = f.read() # Read the entire file
141
+
142
+ # Make modifications to the content in memory
143
+ modified_content = content.replace('old_text', 'new_text')
144
+ # OR use regex if needed
145
+ import re
146
+ modified_content = re.sub(r'pattern', 'replacement', content)
147
+
148
+ # Write back the entire file at once
149
+ with open(file_path, 'w') as f:
150
+ f.write(modified_content) # Write the entire file
151
+
152
+ # Verify changes were made
153
+ with open(file_path, 'r') as f:
154
+ verification = f.read()
155
+ print(f"Verification: {'new_text' in verification}")
156
+ ```
157
+
158
+AVOID these error-prone approaches:
159
+
160
+❌ Line-by-line reading and writing
161
+❌ Multiple separate f.write() calls
162
+❌ Complex string manipulation without testing
163
+
164
+
165
+For Python files specifically, preserve indentation:
166
+
167
+Use dedicated functions for Python code modification
168
+Be extremely careful with regex replacements
169
+
170
### CODE EDITING (PYTHON)
171
- Avoid naive string or line replacements for code edits, especially in Python, as this can break indentation and structure.
172
+- Read the file to identify the exact issues. After reviewing the content, implement the necessary fixes to ensure proper syntax throughout the file.
173
- Prefer reading the whole file, editing in memory, and writing back as a single multi-line string for reliability.
174
- **If repeated edit failures occur, switch to EOF CAT-style edits or another robust method.**
\ No newline at end of file