Skip to content

Commit a76ce26

Browse files
Debug Mode
1 parent 2e173ca commit a76ce26

6 files changed

Lines changed: 120 additions & 97 deletions

File tree

tinyagent/code_agent/providers/base.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,16 @@ def __init__(
7373
async def execute_python(
7474
self,
7575
code_lines: List[str],
76-
timeout: int = 120
76+
timeout: int = 120,
77+
debug_mode: bool = False
7778
) -> Dict[str, Any]:
7879
"""
7980
Execute Python code and return the result.
8081
8182
Args:
8283
code_lines: List of Python code lines to execute
8384
timeout: Maximum execution time in seconds
85+
debug_mode: Whether to print the executed code (useful for debugging)
8486
8587
Returns:
8688
Dictionary containing execution results with keys:

tinyagent/code_agent/providers/bubblewrap_provider.py

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -365,13 +365,14 @@ def _build_bubblewrap_command(
365365

366366
return cmd
367367

368-
async def execute_python(self, code_lines: List[str], timeout: int = 120) -> Dict[str, Any]:
368+
async def execute_python(self, code_lines: List[str], timeout: int = 120, debug_mode: bool = False) -> Dict[str, Any]:
369369
"""
370370
Execute Python code within a bubblewrap sandbox and return the result.
371371
372372
Args:
373373
code_lines: List of Python code lines to execute
374374
timeout: Maximum execution time in seconds
375+
debug_mode: Whether to print the executed code (useful for debugging)
375376
376377
Returns:
377378
Dictionary containing execution results
@@ -381,14 +382,16 @@ async def execute_python(self, code_lines: List[str], timeout: int = 120) -> Dic
381382

382383
full_code = "\n".join(code_lines)
383384

384-
print("#" * 100)
385-
print("##########################################code##########################################")
386-
print(full_code)
387-
print("#" * 100)
385+
if debug_mode:
386+
print("#" * 100)
387+
print("##########################################code##########################################")
388+
print(full_code)
389+
print("#" * 100)
388390

389391
# Prepare the full code with tools and default codes if needed
390392
if self.executed_default_codes:
391-
print("✔️ default codes already executed")
393+
if debug_mode:
394+
print("✔️ default codes already executed")
392395
complete_code = "\n".join(self.code_tools_definitions) + "\n\n" + full_code
393396
else:
394397
complete_code = "\n".join(self.code_tools_definitions) + "\n\n" + "\n".join(self.default_python_codes) + "\n\n" + full_code
@@ -687,7 +690,7 @@ def _sanitize_state_dict(d):
687690
result["error"] = f"Process exited with code {process.returncode}"
688691

689692
# Log the response
690-
self._log_response(result)
693+
self._log_response(result, debug_mode)
691694

692695
return clean_response(result)
693696

@@ -724,29 +727,30 @@ def _sanitize_state_dict(d):
724727
except Exception:
725728
pass
726729

727-
def _log_response(self, response: Dict[str, Any]):
730+
def _log_response(self, response: Dict[str, Any], debug_mode: bool = False):
728731
"""Log the response from code execution."""
729-
print("######################### BUBBLEWRAP EXECUTION #########################")
730-
print("#########################<printed_output>#########################")
731-
print(response["printed_output"])
732-
print("#########################</printed_output>#########################")
733-
if response.get("return_value", None) not in [None, ""]:
734-
print("#########################<return_value>#########################")
735-
print(response["return_value"])
736-
print("#########################</return_value>#########################")
737-
if response.get("stderr", None) not in [None, ""]:
738-
print("#########################<stderr>#########################")
739-
print(response["stderr"])
740-
print("#########################</stderr>#########################")
741-
if response.get("error_traceback", None) not in [None, ""]:
742-
print("#########################<traceback>#########################")
743-
# Check if this is a security exception and highlight it in red if so
744-
error_text = response["error_traceback"]
745-
if "SECURITY" in error_text:
746-
print(f"{COLOR['RED']}{error_text}{COLOR['ENDC']}")
747-
else:
748-
print(error_text)
749-
print("#########################</traceback>#########################")
732+
if debug_mode:
733+
print("######################### BUBBLEWRAP EXECUTION #########################")
734+
print("#########################<printed_output>#########################")
735+
print(response["printed_output"])
736+
print("#########################</printed_output>#########################")
737+
if response.get("return_value", None) not in [None, ""]:
738+
print("#########################<return_value>#########################")
739+
print(response["return_value"])
740+
print("#########################</return_value>#########################")
741+
if response.get("stderr", None) not in [None, ""]:
742+
print("#########################<stderr>#########################")
743+
print(response["stderr"])
744+
print("#########################</stderr>#########################")
745+
if response.get("error_traceback", None) not in [None, ""]:
746+
print("#########################<traceback>#########################")
747+
# Check if this is a security exception and highlight it in red if so
748+
error_text = response["error_traceback"]
749+
if "SECURITY" in error_text:
750+
print(f"{COLOR['RED']}{error_text}{COLOR['ENDC']}")
751+
else:
752+
print(error_text)
753+
print("#########################</traceback>#########################")
750754

751755
def _quote_command_for_shell(self, command: List[str]) -> str:
752756
"""
@@ -862,7 +866,7 @@ async def _prepare_git_sandbox_command(self, command: List[str]) -> List[str]:
862866
# Check if it's a GitHub URL
863867
if [[ "$REMOTE_URL" == *"github.com"* ]]; then
864868
# Extract the repo path from the URL
865-
REPO_PATH=$(echo "$REMOTE_URL" | sed -E 's|https://[^/]*github\.com/||' | sed -E 's|git@github\.com:||' | sed 's|\.git$||')
869+
REPO_PATH=$(echo "$REMOTE_URL" | sed -E 's|https://[^/]*github\\.com/||' | sed -E 's|git@github\\.com:||' | sed 's|\\.git$||')
866870
867871
# Set the remote URL with the token
868872
git remote set-url {remote_name} "https://{github_username}:{github_token}@github.com/$REPO_PATH.git"

tinyagent/code_agent/providers/docker_provider.py

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -548,13 +548,14 @@ def get_environment_variables(self) -> Dict[str, str]:
548548
"""
549549
return self.environment_variables.copy()
550550

551-
async def execute_python(self, code_lines: List[str], timeout: int = 120) -> Dict[str, Any]:
551+
async def execute_python(self, code_lines: List[str], timeout: int = 120, debug_mode: bool = False) -> Dict[str, Any]:
552552
"""
553553
Execute Python code within a Docker container and return the result.
554554
555555
Args:
556556
code_lines: List of Python code lines to execute
557557
timeout: Maximum execution time in seconds
558+
debug_mode: Whether to print the executed code (useful for debugging)
558559
559560
Returns:
560561
Dictionary containing execution results
@@ -564,14 +565,16 @@ async def execute_python(self, code_lines: List[str], timeout: int = 120) -> Dic
564565

565566
full_code = "\n".join(code_lines)
566567

567-
print("#" * 100)
568-
print("##########################################code##########################################")
569-
print(full_code)
570-
print("#" * 100)
568+
if debug_mode:
569+
print("#" * 100)
570+
print("##########################################code##########################################")
571+
print(full_code)
572+
print("#" * 100)
571573

572574
# Prepare the full code with tools and default codes if needed
573575
if self.executed_default_codes:
574-
print("✔️ default codes already executed")
576+
if debug_mode:
577+
print("✔️ default codes already executed")
575578
complete_code = "\n".join(self.code_tools_definitions) + "\n\n" + full_code
576579
else:
577580
complete_code = "\n".join(self.code_tools_definitions) + "\n\n" + "\n".join(self.default_python_codes) + "\n\n" + full_code
@@ -697,7 +700,7 @@ async def execute_python(self, code_lines: List[str], timeout: int = 120) -> Dic
697700
result["error"] = f"Process exited with code {process.returncode}"
698701

699702
# Log the response
700-
self._log_response(result)
703+
self._log_response(result, debug_mode)
701704

702705
return clean_response(result)
703706

@@ -953,29 +956,30 @@ def _sanitize_state_dict(d):
953956
print(json.dumps(cleaned_result))
954957
"""
955958

956-
def _log_response(self, response: Dict[str, Any]):
959+
def _log_response(self, response: Dict[str, Any], debug_mode: bool = False):
957960
"""Log the response from code execution."""
958-
print("######################### DOCKER EXECUTION #########################")
959-
print("#########################<printed_output>#########################")
960-
print(response["printed_output"])
961-
print("#########################</printed_output>#########################")
962-
if response.get("return_value", None) not in [None, ""]:
963-
print("#########################<return_value>#########################")
964-
print(response["return_value"])
965-
print("#########################</return_value>#########################")
966-
if response.get("stderr", None) not in [None, ""]:
967-
print("#########################<stderr>#########################")
968-
print(response["stderr"])
969-
print("#########################</stderr>#########################")
970-
if response.get("error_traceback", None) not in [None, ""]:
971-
print("#########################<traceback>#########################")
972-
# Check if this is a security exception and highlight it in red if so
973-
error_text = response["error_traceback"]
974-
if "SECURITY" in error_text:
975-
print(f"{COLOR['RED']}{error_text}{COLOR['ENDC']}")
976-
else:
977-
print(error_text)
978-
print("#########################</traceback>#########################")
961+
if debug_mode:
962+
print("######################### DOCKER EXECUTION #########################")
963+
print("#########################<printed_output>#########################")
964+
print(response["printed_output"])
965+
print("#########################</printed_output>#########################")
966+
if response.get("return_value", None) not in [None, ""]:
967+
print("#########################<return_value>#########################")
968+
print(response["return_value"])
969+
print("#########################</return_value>#########################")
970+
if response.get("stderr", None) not in [None, ""]:
971+
print("#########################<stderr>#########################")
972+
print(response["stderr"])
973+
print("#########################</stderr>#########################")
974+
if response.get("error_traceback", None) not in [None, ""]:
975+
print("#########################<traceback>#########################")
976+
# Check if this is a security exception and highlight it in red if so
977+
error_text = response["error_traceback"]
978+
if "SECURITY" in error_text:
979+
print(f"{COLOR['RED']}{error_text}{COLOR['ENDC']}")
980+
else:
981+
print(error_text)
982+
print("#########################</traceback>#########################")
979983

980984
def _quote_command_for_shell(self, command: List[str]) -> str:
981985
"""

tinyagent/code_agent/providers/modal_provider.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,14 @@ def _setup_modal_app(self):
163163
if self.code_tools:
164164
self.add_tools(self.code_tools)
165165

166-
async def execute_python(self, code_lines: List[str], timeout: int = 120) -> Dict[str, Any]:
166+
async def execute_python(self, code_lines: List[str], timeout: int = 120, debug_mode: bool = False) -> Dict[str, Any]:
167167
"""
168168
Execute Python code using Modal's native .local() or .remote() methods.
169169
170170
Args:
171171
code_lines: List of Python code lines to execute
172172
timeout: Maximum execution time in seconds
173+
debug_mode: Whether to print the executed code (useful for debugging)
173174
174175
Returns:
175176
Dictionary containing execution results
@@ -179,10 +180,11 @@ async def execute_python(self, code_lines: List[str], timeout: int = 120) -> Dic
179180

180181
full_code = "\n".join(code_lines)
181182

182-
print("#" * 100)
183-
print("##########################################code##########################################")
184-
print(full_code)
185-
print("#" * 100)
183+
if debug_mode:
184+
print("#" * 100)
185+
print("##########################################code##########################################")
186+
print(full_code)
187+
print("#" * 100)
186188

187189

188190
# Use Modal's native execution methods
@@ -303,7 +305,8 @@ def _python_executor(self, code: str, globals_dict: Dict[str, Any] = None, local
303305

304306
# Prepare the full code with default codes if needed
305307
if self.executed_default_codes:
306-
print("✔️ default codes already executed")
308+
if debug_mode:
309+
print("✔️ default codes already executed")
307310
full_code = "\n".join(self.code_tools_definitions) +"\n\n"+code
308311
# Code tools and default code are trusted, user code is not
309312
else:

tinyagent/code_agent/providers/seatbelt_provider.py

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -375,13 +375,14 @@ def _write_seatbelt_profile_to_temp_file(self):
375375
self.logger.error("Failed to write seatbelt profile to temporary file: %s", str(e))
376376
raise RuntimeError(f"Failed to write seatbelt profile: {str(e)}")
377377

378-
async def execute_python(self, code_lines: List[str], timeout: int = 120) -> Dict[str, Any]:
378+
async def execute_python(self, code_lines: List[str], timeout: int = 120, debug_mode: bool = False) -> Dict[str, Any]:
379379
"""
380380
Execute Python code within a sandbox and return the result.
381381
382382
Args:
383383
code_lines: List of Python code lines to execute
384384
timeout: Maximum execution time in seconds
385+
debug_mode: Whether to print the executed code (useful for debugging)
385386
386387
Returns:
387388
Dictionary containing execution results
@@ -391,14 +392,16 @@ async def execute_python(self, code_lines: List[str], timeout: int = 120) -> Dic
391392

392393
full_code = "\n".join(code_lines)
393394

394-
print("#" * 100)
395-
print("##########################################code##########################################")
396-
print(full_code)
397-
print("#" * 100)
395+
if debug_mode:
396+
print("#" * 100)
397+
print("##########################################code##########################################")
398+
print(full_code)
399+
print("#" * 100)
398400

399401
# Prepare the full code with tools and default codes if needed
400402
if self.executed_default_codes:
401-
print("✔️ default codes already executed")
403+
if debug_mode:
404+
print("✔️ default codes already executed")
402405
complete_code = "\n".join(self.code_tools_definitions) + "\n\n" + full_code
403406
else:
404407
complete_code = "\n".join(self.code_tools_definitions) + "\n\n" + "\n".join(self.default_python_codes) + "\n\n" + full_code
@@ -701,7 +704,7 @@ def _sanitize_state_dict(d):
701704
result["error"] = f"Process exited with code {process.returncode}"
702705

703706
# Log the response
704-
self._log_response(result)
707+
self._log_response(result, debug_mode)
705708

706709
return clean_response(result)
707710

@@ -738,29 +741,30 @@ def _sanitize_state_dict(d):
738741
except Exception:
739742
pass
740743

741-
def _log_response(self, response: Dict[str, Any]):
744+
def _log_response(self, response: Dict[str, Any], debug_mode: bool = False):
742745
"""Log the response from code execution."""
743-
print("######################### SEATBELT EXECUTION #########################")
744-
print("#########################<printed_output>#########################")
745-
print(response["printed_output"])
746-
print("#########################</printed_output>#########################")
747-
if response.get("return_value", None) not in [None, ""]:
748-
print("#########################<return_value>#########################")
749-
print(response["return_value"])
750-
print("#########################</return_value>#########################")
751-
if response.get("stderr", None) not in [None, ""]:
752-
print("#########################<stderr>#########################")
753-
print(response["stderr"])
754-
print("#########################</stderr>#########################")
755-
if response.get("error_traceback", None) not in [None, ""]:
756-
print("#########################<traceback>#########################")
757-
# Check if this is a security exception and highlight it in red if so
758-
error_text = response["error_traceback"]
759-
if "SECURITY" in error_text:
760-
print(f"{COLOR['RED']}{error_text}{COLOR['ENDC']}")
761-
else:
762-
print(error_text)
763-
print("#########################</traceback>#########################")
746+
if debug_mode:
747+
print("######################### SEATBELT EXECUTION #########################")
748+
print("#########################<printed_output>#########################")
749+
print(response["printed_output"])
750+
print("#########################</printed_output>#########################")
751+
if response.get("return_value", None) not in [None, ""]:
752+
print("#########################<return_value>#########################")
753+
print(response["return_value"])
754+
print("#########################</return_value>#########################")
755+
if response.get("stderr", None) not in [None, ""]:
756+
print("#########################<stderr>#########################")
757+
print(response["stderr"])
758+
print("#########################</stderr>#########################")
759+
if response.get("error_traceback", None) not in [None, ""]:
760+
print("#########################<traceback>#########################")
761+
# Check if this is a security exception and highlight it in red if so
762+
error_text = response["error_traceback"]
763+
if "SECURITY" in error_text:
764+
print(f"{COLOR['RED']}{error_text}{COLOR['ENDC']}")
765+
else:
766+
print(error_text)
767+
print("#########################</traceback>#########################")
764768

765769

766770
def _quote_command_for_shell(self, command: List[str]) -> str:
@@ -915,7 +919,7 @@ async def _prepare_git_sandbox_command(self, command: List[str]) -> List[str]:
915919
# Check if it's a GitHub URL
916920
if [[ "$REMOTE_URL" == *"github.com"* ]]; then
917921
# Extract the repo path from the URL
918-
REPO_PATH=$(echo "$REMOTE_URL" | sed -E 's|https://[^/]*github\.com/||' | sed -E 's|git@github\.com:||' | sed 's|\.git$||')
922+
REPO_PATH=$(echo "$REMOTE_URL" | sed -E 's|https://[^/]*github\\.com/||' | sed -E 's|git@github\\.com:||' | sed 's|\\.git$||')
919923
920924
# Set the remote URL with the token
921925
git remote set-url {remote_name} "https://{github_username}:{github_token}@github.com/$REPO_PATH.git"

0 commit comments

Comments
 (0)