diff --git a/autogpts/forge/forge/sdk/workspace.py b/autogpts/forge/forge/sdk/workspace.py index 89d46150e44c..8859a3cf050b 100644 --- a/autogpts/forge/forge/sdk/workspace.py +++ b/autogpts/forge/forge/sdk/workspace.py @@ -37,11 +37,15 @@ def __init__(self, base_path: str): self.base_path = Path(base_path).resolve() def _resolve_path(self, task_id: str, path: str) -> Path: + path = path if not path.startswith("/") else path[1:] abs_path = (self.base_path / task_id / path).resolve() if not str(abs_path).startswith(str(self.base_path)): print("Error") raise ValueError(f"Directory traversal is not allowed! - {abs_path}") - abs_path.parent.mkdir(parents=True, exist_ok=True) + try: + abs_path.parent.mkdir(parents=True, exist_ok=True) + except FileExistsError: + pass return abs_path def read(self, task_id: str, path: str) -> bytes: diff --git a/benchmark/agbenchmark/agent_api_interface.py b/benchmark/agbenchmark/agent_api_interface.py index 9255884320ed..18ff4520e6d9 100644 --- a/benchmark/agbenchmark/agent_api_interface.py +++ b/benchmark/agbenchmark/agent_api_interface.py @@ -1,5 +1,7 @@ import json +import logging import os +import pathlib import time from typing import Any, Dict, Optional @@ -14,6 +16,8 @@ from agbenchmark.agent_protocol_client.models.step import Step from agbenchmark.utils.data_types import ChallengeData +LOG = logging.getLogger(__name__) + async def run_api_agent( task: ChallengeData, config: Dict[str, Any], artifacts_location: str, timeout: int @@ -63,13 +67,22 @@ async def copy_agent_artifacts_into_temp_folder(api_instance, task_id): artifacts = await api_instance.list_agent_task_artifacts(task_id=task_id) for artifact in artifacts.artifacts: # current absolute path of the directory of the file - directory_location = TEMP_FOLDER_ABS_PATH + directory_location = pathlib.Path(TEMP_FOLDER_ABS_PATH) if artifact.relative_path: - directory_location = os.path.dirname(directory_location / artifact.relative_path) - - os.mkdir(directory_location, recursive=True, exist_ok=True) + path = ( + artifact.relative_path + if not artifact.relative_path.startswith("/") + else artifact.relative_path[1:] + ) + directory_location = pathlib.Path( + os.path.dirname(directory_location / path) + ) + LOG.info(f"Creating directory {directory_location}") + + directory_location.mkdir(parents=True, exist_ok=True) file_path = directory_location / artifact.file_name + LOG.info(f"Writing file {file_path}") with open(file_path, "wb") as f: content = await api_instance.download_agent_task_artifact( task_id=task_id, artifact_id=artifact.artifact_id