Skip to content

Agent Zero AI framework

Edit: Encourage cat > EOF multiline edits for code and documentation

deci929ea8515835
diff --git a/prompts/default/agent.system.tool.code_exe.md b/prompts/default/agent.system.tool.code_exe.md
index 8384e61f6..f35c2ddda 100644
--- a/prompts/default/agent.system.tool.code_exe.md
+++ b/prompts/default/agent.system.tool.code_exe.md
@@ -133,41 +133,61 @@ Execute commands and code for computation, data analysis, and file operations.
### FILE EDITING BEST PRACTICES
When editing files, especially code files:
-1. ALWAYS use the "read entire file → modify in memory → write entire file" pattern:
- ```python
- # CORRECT APPROACH - Read, modify in memory, write as one operation
- with open(file_path, 'r') as f:
- content = f.read() # Read the entire file
-
- # Make modifications to the content in memory
- modified_content = content.replace('old_text', 'new_text')
- # OR use regex if needed
- import re
- modified_content = re.sub(r'pattern', 'replacement', content)
-
- # Write back the entire file at once
- with open(file_path, 'w') as f:
- f.write(modified_content) # Write the entire file
-
- # Verify changes were made
- with open(file_path, 'r') as f:
- verification = f.read()
- print(f"Verification: {'new_text' in verification}")
- ```
-
-AVOID these error-prone approaches:
-
-❌ Line-by-line reading and writing
-❌ Multiple separate f.write() calls
-❌ Complex string manipulation without testing
-
-
-For Python files specifically, preserve indentation:
-
-Use dedicated functions for Python code modification
-Be extremely careful with regex replacements
-
-### CODE EDITING (PYTHON)
+1. **RECOMMENDED METHOD (Terminal Heredoc):**
+ * Use the `terminal` runtime (session 0) with `cat > /path/to/file << 'EOF' ... EOF`.
+ * This is generally preferred for reliability with multi-line content and avoids Python environment issues.
+ * **Formatting:** Ensure `\n` for newlines in your JSON `code` string and that the final `EOF` is on its own line.
+ * **Verification:** ALWAYS verify the write immediately using `cat /path/to/file` in a subsequent `terminal` call.
+ * **Troubleshooting Hangs:** If this command hangs (stuck on a `>` prompt and times out), it indicates the content or `EOF` marker wasn't transmitted correctly. Review your `code` string's formatting and escaping. If it repeatedly hangs, use the Python Fallback.
+ ```json
+ {
+ "thoughts": ["Writing a multi-line file using terminal heredoc"],
+ "tool_name": "code_execution_tool",
+ "tool_args": {
+ "runtime": "terminal",
+ "session": 0,
+ "code": "cat > path/to/my_script.py << 'EOF'\n# Start of script\nimport os\n\ndef main():\n print(f"Hello from {os.getcwd()}" )\n\nif __name__ == "__main__":\n main()\nEOF"
+ }
+ }
+ ```
+ ```json
+ {
+ "thoughts": ["Verifying the file write"],
+ "tool_name": "code_execution_tool",
+ "tool_args": {
+ "runtime": "terminal",
+ "session": 0,
+ "code": "cat path/to/my_script.py"
+ }
+ }
+ ```
+
+2. **PYTHON FALLBACK METHOD (If Terminal Heredoc Hangs):**
+ * Use this **only if the terminal `cat > EOF` method hangs repeatedly.**
+ * Use `runtime: python` (session 0) with `with open(...) f.write(...)`.
+ * Ensure you write the *entire* file content as a single multi-line string.
+ ```json
+ {
+ "thoughts": ["Terminal heredoc hung, using Python fallback to write file"],
+ "tool_name": "code_execution_tool",
+ "tool_args": {
+ "runtime": "python",
+ "session": 0,
+ "code": "file_path = 'path/to/my_script.py'\ncontent = \"\"\"# Start of script\nimport os\n\ndef main():\n print(f\"Hello from {os.getcwd()}\" )\n\nif __name__ == \"__main__\":\n main()\"\"\"\nwith open(file_path, 'w') as f:\n f.write(content)\nprint(f\"File {file_path} written via Python.\")"
+ }
+ }
+ ```
+
+**Overall Strategy:**
+* ALWAYS read necessary context first (e.g., `cat <file>`).
+* Construct the full, new file content in memory.
+* Attempt to write using the **Terminal Heredoc** method.
+* **Verify** the write.
+* If the Terminal Heredoc method hangs, use the **Python Fallback** method.
+* Verify again.
+* **AVOID** naive string/line replacements or partial edits for code/structured files.
+
+### CODE EDITING (PYTHON SPECIFIC)
- Avoid naive string or line replacements for code edits, especially in Python, as this can break indentation and structure.
- Read the file to identify the exact issues. After reviewing the content, implement the necessary fixes to ensure proper syntax throughout the file.
- Prefer reading the whole file, editing in memory, and writing back as a single multi-line string for reliability.
diff --git a/python/tools/team_agent.py b/python/tools/team_agent.py
index 6e4408e16..7c5dd5c5a 100644
--- a/python/tools/team_agent.py
+++ b/python/tools/team_agent.py
@@ -647,43 +647,55 @@ PROJECT CREATION PATTERN:
4. Run code in separate sessions from creation
5. ALWAYS install packages AND run scripts with terminal runtime to maintain environment consistency
-FILE EDITING STRATEGIES (Use Terminal First):
+FILE EDITING STRATEGIES (Use Terminal First for Writes):
Reading Files:
-- Use `cat /path/to/file` in the terminal runtime.
+- Use `cat /path/to/file` in the `terminal` runtime.
-Writing/Overwriting Files (Preferred Method for Reliability):
-- Use `cat > /path/to/file << 'EOF' ... EOF` in the terminal runtime. This is the MOST RELIABLE way to write or overwrite entire files, especially multi-line content or code.
+Writing/Overwriting Files (RECOMMENDED METHOD):
+- Use `cat > /path/to/file << 'EOF' ... EOF` in the **terminal** runtime (session 0). This is generally the most reliable way to write or overwrite entire files, especially multi-line content or code, avoiding Python environment issues.
+- **Formatting:** Ensure correct JSON escaping (`\n` for newlines) and that the final `EOF` is on its own line.
```json
{{
- "thoughts": ["Overwriting file with new content using heredoc"],
+ "thoughts": ["Overwriting file with new content using terminal heredoc"],
"tool_name": "code_execution_tool",
"tool_args": {{
"runtime": "terminal",
- "session": 0, // Use session 0 for file ops
- "code": "cat > /path/to/your/file.py << 'EOF'\\n# Your full new file content here\\nprint(\\'Hello Overwritten World!\\')\\nEOF"
+ "session": 0,
+ "code": "cat > /path/to/your/file.py << 'EOF'\n# Your full new file content here\nprint(\'Hello Overwritten World!\')\nEOF"
}}
}}
```
+- **Verification:** ALWAYS verify the write immediately using `cat /path/to/file` in a subsequent `terminal` call.
+- **Troubleshooting:** If this command hangs (shows a `>` prompt and times out), it likely means the multi-line content or the final `EOF` was not transmitted correctly. Double-check formatting and escaping. If it hangs repeatedly, use the Python Fallback method.
-Creating Empty Files or Simple Overwrites (Less Reliable for complex content):
-- `echo "single line" > /path/to/file` or `touch /path/to/file`
+Creating Empty Files or Simple Overwrites (Use with Caution):
+- `echo "single line" > /path/to/file` or `touch /path/to/file` are acceptable for very simple cases but less reliable for complex content.
-Python Fallback (If Terminal Methods Fail Repeatedly):
-- ONLY if terminal methods fail, use the `python` runtime with file I/O.
+Python Fallback (Use if Terminal `cat > EOF` Hangs):
+- Use this method **ONLY if the terminal `cat > EOF` method hangs repeatedly** (stuck on `>` prompt).
+- Use the `python` runtime (session 0) with standard file I/O.
```json
{{
- "thoughts": ["Terminal file write failed, falling back to Python file I/O"],
+ "thoughts": ["Terminal file write failed/hung, falling back to Python file I/O"],
"tool_name": "code_execution_tool",
"tool_args": {{
- "runtime": "python", // Python runtime specifically for this fallback
- "session": 0, // Still use session 0
- "code": "with open('/path/to/your/file.py', 'w') as f:\\n f.write(\\'\\'\\'# Your full new file content here\\nprint(\\\\\\'Hello Python Fallback!\\\\\\')\\n\\'\\'\\')"
+ "runtime": "python",
+ "session": 0,
+ "code": "with open('/path/to/your/file.py', 'w') as f:\n f.write(\'\'\'# Your full new file content here\nprint(\\\'Hello Python Fallback!\\\')\n\'\'\')\nprint('File written via Python.')"
}}
}}
```
-*NEVER* use naive string/line replacements or partial edits, especially for code or structured files. Always read the necessary context, modify the content appropriately, and write back the *entire* corrected content using the `cat > ... << EOF` method or the Python fallback.
+**Overall Edit Strategy:**
+1. Read the necessary context (`cat <file>`).
+2. Construct the *entire* new file content in memory.
+3. Write the *entire* new content using the **Terminal Heredoc** method first.
+4. **Verify** the write (`cat <file>`).
+5. If the terminal method **hangs** (stuck on `>`), retry using the **Python Fallback** method.
+6. Verify again.
+
+*NEVER* use naive string/line replacements or partial edits, especially for code or structured files.
AVAILABLE TOOLS:
- knowledge_tool: For research and information gathering