Edit: Slimmed context of code exe md prompt
deci committed
May 14, 2025 at 00:39 UTC
a4c4d88d419a6b04088b25ea67df59a97e2c54b9
1 file changed
+66
-115
prompts/default/agent.system.tool.code_exe.md
+66
-115
@@ -1,54 +1,84 @@
1
-### code_execution_tool
1
+# Code Execution Tool
2
3
Execute commands and code for computation, data analysis, and file operations.
4
5
-**PARAMETERS:**
6
-- **runtime:** "terminal" (shell), "python", "nodejs", "output" (wait), "reset" (kill)
7
-- **session:** 0 for file operations, 1-10 for running programs (keep separate)
8
-- **code:** Your command or code to execute (use print/console.log for output)
5
+## Parameters
6
+- **runtime:** `terminal` (shell), `python`, `nodejs`, `output` (wait), `reset` (kill)
7
+- **session:** `0` for file operations, `1-10` for running programs
8
+- **code:** Command or code to execute (use print/console.log for output)
9
10
-## CORE PATTERNS:
10
+## Core Best Practices
11
12
-### BASIC FILE OPERATIONS
12
+### 1. Session Management
13
+- Use session 0 **ONLY** for file operations
14
+- Use sessions 1-10 for running programs
15
+- Always reset sessions before running new programs
16
```json
17
{
15
- "thoughts": ["Creating a project structure and files (using a single multi-line string for file content; do NOT use multiple f.write() calls)"],
16
- "tool_name": "code_execution_tool",
18
+ "tool_name": "code_execution_tool",
19
"tool_args": {
18
- "runtime": "python",
19
- "session": 0,
20
- "code": "import os\n\n# Create project structure\nos.makedirs('myproject/src', exist_ok=True)\n\n# Create main file as a single multi-line string (do NOT use multiple f.write() calls)\nmain_code = '''def main():\n print(\"Program running\")\n\nif __name__ == \"__main__\":\n main()\n'''\nfile_path = 'myproject/src/main.py'\nwith open(file_path, 'w') as f:\n f.write(main_code)\n\n# Verify file creation\nif os.path.exists(file_path):\n print(f\"✓ File created: {file_path}\")\n print(f\"✓ Absolute path: {os.path.abspath(file_path)}\")\nelse:\n print(f\"✗ Failed to create file: {file_path}\")"
20
+ "runtime": "reset",
21
+ "session": 1
22
}
23
}
24
```
25
+```json
26
+{
27
+ "tool_name": "code_execution_tool",
28
+ "tool_args": {
29
+ "runtime": "terminal",
30
+ "session": 1,
31
+ "code": "python myproject/main.py"
32
+ }
33
+}
34
+```
35
+
36
+### 2. File Operations (CRITICAL)
37
+- **NEVER use multiple f.write() calls - Always use a single multi-line string**
38
+- Verify file creation before running files
39
+- Use Python for complex file operations
40
+
41
+### 3. File Writing Methods (CRITICAL)
42
25
-### RUNNING CODE (USE SEPARATE SESSIONS)
43
+#### Method A: Terminal Heredoc (PREFERRED)
44
```json
45
{
28
- "thoughts": ["Reset session before running"],
46
"tool_name": "code_execution_tool",
47
"tool_args": {
31
- "runtime": "reset",
32
- "session": 1
48
+ "runtime": "terminal",
49
+ "session": 0,
50
+ "code": "cat > file.py << 'EOF'\ndef main():\n print(\"Hello world\")\n\nif __name__ == \"__main__\":\n main()\nEOF"
51
}
52
}
53
```
54
+**ALWAYS verify after writing:**
55
```json
56
{
38
- "thoughts": ["Running created file in session 1"],
57
"tool_name": "code_execution_tool",
58
"tool_args": {
59
"runtime": "terminal",
42
- "session": 1,
43
- "code": "python myproject/src/main.py"
60
+ "session": 0,
61
+ "code": "cat file.py"
62
+ }
63
+}
64
+```
65
+
66
+#### Method B: Python Single-String (FALLBACK)
67
+If heredoc hangs (shows `>` prompt), use:
68
+```json
69
+{
70
+ "tool_name": "code_execution_tool",
71
+ "tool_args": {
72
+ "runtime": "python",
73
+ "session": 0,
74
+ "code": "content = \"\"\"def main():\n print(\"Hello world\")\n\nif __name__ == \"__main__\":\n main()\"\"\"\nwith open('file.py', 'w') as f:\n f.write(content)\nprint(\"File written successfully\")"
75
}
76
}
77
```
78
48
-### PACKAGE INSTALLATION
79
+### 4. Package Installation
80
```json
81
{
51
- "thoughts": ["Installing required packages"],
82
"tool_name": "code_execution_tool",
83
"tool_args": {
84
"runtime": "terminal",
@@ -58,21 +88,20 @@ Execute commands and code for computation, data analysis, and file operations.
88
}
89
```
90
61
-### INTERACTIVE PROGRAMS WITH INPUT
91
+### 5. Interactive Programs
92
+Create file → Reset session → Run program → Provide input
93
```json
94
{
64
- "thoughts": ["Creating interactive program"],
65
- "tool_name": "code_execution_tool",
95
+ "tool_name": "code_execution_tool",
96
"tool_args": {
97
"runtime": "python",
98
"session": 0,
69
- "code": "file_path = 'interactive.py'\nwith open(file_path, 'w') as f:\n f.write('name = input(\"Enter your name: \")\\nprint(f\"Hello, {name}!\")')\nprint(f\"✓ File created: {file_path}\")"
99
+ "code": "with open('interactive.py', 'w') as f:\n f.write('name = input(\"Enter name: \")\\nprint(f\"Hello, {name}!\")')"
100
}
101
}
102
```
103
```json
104
{
75
- "thoughts": ["Running interactive program"],
105
"tool_name": "code_execution_tool",
106
"tool_args": {
107
"runtime": "reset",
@@ -82,7 +111,6 @@ Execute commands and code for computation, data analysis, and file operations.
111
```
112
```json
113
{
85
- "thoughts": ["Starting interactive program"],
114
"tool_name": "code_execution_tool",
115
"tool_args": {
116
"runtime": "terminal",
@@ -93,7 +121,6 @@ Execute commands and code for computation, data analysis, and file operations.
121
```
122
```json
123
{
96
- "thoughts": ["Providing input to program"],
124
"tool_name": "input",
125
"tool_args": {
126
"keyboard": "John Doe",
@@ -102,93 +129,17 @@ Execute commands and code for computation, data analysis, and file operations.
129
}
130
```
131
105
-## BEST PRACTICES:
106
-
107
-### FILE OPERATIONS
108
-- Always create files with clear paths in session 0
109
-- **NEVER use multiple f.write() calls or line-by-line file writing. Always use a single multi-line string and write it in one go.**
110
-- Verify file creation before attempting to run files
111
-- Use Python's file operations for complex files
112
-- Organize projects with standard directory structure
113
-
114
-### SESSION MANAGEMENT
115
-- Keep session 0 for file creation/editing only
116
-- Use sessions 1+ for running programs
117
-- Reset sessions before running new programs
118
-- Never run commands in sessions waiting for input
119
-
120
-### ERROR HANDLING
121
-- If file operations fail, verify the current directory and file paths
122
-- For import errors, check dependencies and installation
123
-- Reset sessions after errors before trying again
124
-- Use try/except in Python code to handle potential errors
125
-- **If you encounter the same error twice with the same method, switch to a more reliable method (e.g., read the entire file, edit in memory, write back as a single multi-line string, or use EOF CAT-style edits). Document the fallback method used.**
126
-
127
-### DEBUGGING
128
-- Print absolute paths when verifying file creation
129
-- List directory contents to check available files
130
-- Create debugging scripts to test environment setup
131
-- Use explicit path variables for consistency
132
+## Troubleshooting
133
133
-### FILE EDITING BEST PRACTICES
134
-When editing files, especially code files:
135
-
136
-1. **RECOMMENDED METHOD (Terminal Heredoc):**
137
- * Use the `terminal` runtime (session 0) with `cat > /path/to/file << 'EOF' ... EOF`.
138
- * This is generally preferred for reliability with multi-line content and avoids Python environment issues.
139
- * **Formatting:** Ensure `\n` for newlines in your JSON `code` string and that the final `EOF` is on its own line.
140
- * **Verification:** ALWAYS verify the write immediately using `cat /path/to/file` in a subsequent `terminal` call.
141
- * **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.
142
- ```json
143
- {
144
- "thoughts": ["Writing a multi-line file using terminal heredoc"],
145
- "tool_name": "code_execution_tool",
146
- "tool_args": {
147
- "runtime": "terminal",
148
- "session": 0,
149
- "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"
150
- }
151
- }
152
- ```
153
- ```json
154
- {
155
- "thoughts": ["Verifying the file write"],
156
- "tool_name": "code_execution_tool",
157
- "tool_args": {
158
- "runtime": "terminal",
159
- "session": 0,
160
- "code": "cat path/to/my_script.py"
161
- }
162
- }
163
- ```
164
-
165
-2. **PYTHON FALLBACK METHOD (If Terminal Heredoc Hangs):**
166
- * Use this **only if the terminal `cat > EOF` method hangs repeatedly.**
167
- * Use `runtime: python` (session 0) with `with open(...) f.write(...)`.
168
- * Ensure you write the *entire* file content as a single multi-line string.
169
- ```json
170
- {
171
- "thoughts": ["Terminal heredoc hung, using Python fallback to write file"],
172
- "tool_name": "code_execution_tool",
173
- "tool_args": {
174
- "runtime": "python",
175
- "session": 0,
176
- "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.\")"
177
- }
178
- }
179
- ```
134
+### If Files Fail
135
+- Verify paths with `ls -la`
136
+- Use absolute paths when in doubt
137
181
-**Overall Strategy:**
182
-* ALWAYS read necessary context first (e.g., `cat <file>`).
183
-* Construct the full, new file content in memory.
184
-* Attempt to write using the **Terminal Heredoc** method.
185
-* **Verify** the write.
186
-* If the Terminal Heredoc method hangs, use the **Python Fallback** method.
187
-* Verify again.
188
-* **AVOID** naive string/line replacements or partial edits for code/structured files.
138
+### If Commands Fail
139
+- Reset session before retrying
140
+- Check dependencies for import errors
141
190
-### CODE EDITING (PYTHON SPECIFIC)
191
-- Avoid naive string or line replacements for code edits, especially in Python, as this can break indentation and structure.
192
-- Read the file to identify the exact issues. After reviewing the content, implement the necessary fixes to ensure proper syntax throughout the file.
193
-- Prefer reading the whole file, editing in memory, and writing back as a single multi-line string for reliability.
194
-- **If repeated edit failures occur, switch to EOF CAT-style edits or another robust method.**
\ No newline at end of file
142
+### If Same Error Repeats
143
+- Switch methods: If file editing fails twice, use the alternative method
144
+- For Python code: avoid line-by-line replacements; always read, then modify, and then write entire file
145
+- Document any fallback methods used
\ No newline at end of file