Handle Unicode errors (#1388)

* Add errors for non-unicode file data or command return, trim excessively long command returns.

* Fix lint issue.

* Fix lint issue (try 2).

* Realized that prompts were trimmed elsewhere and dropped the new addition.
This commit is contained in:
Christian Balcom 2024-04-27 08:15:16 -04:00 committed by GitHub
parent ce47461c5a
commit 546be7ca8e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 16 additions and 9 deletions

View File

@ -6,7 +6,7 @@ from opendevin.schema import ActionType
if TYPE_CHECKING:
from opendevin.controller import AgentController
from opendevin.observation import CmdOutputObservation
from opendevin.observation import CmdOutputObservation, Observation
@dataclass
@ -15,7 +15,7 @@ class CmdRunAction(ExecutableAction):
background: bool = False
action: str = ActionType.RUN
async def run(self, controller: 'AgentController') -> 'CmdOutputObservation':
async def run(self, controller: 'AgentController') -> 'Observation':
return controller.action_manager.run_command(self.command, self.background)
@property

View File

@ -85,6 +85,8 @@ class FileReadAction(ExecutableAction):
code_view = ''.join(read_lines)
except FileNotFoundError:
return AgentErrorObservation(f'File not found: {self.path}')
except UnicodeDecodeError:
return AgentErrorObservation(f'File could not be decoded as utf-8: {self.path}')
except IsADirectoryError:
return AgentErrorObservation(f'Path is a directory: {self.path}. You can only read files')
except PermissionError:
@ -144,6 +146,8 @@ class FileWriteAction(ExecutableAction):
return AgentErrorObservation(f'File not found: {self.path}')
except IsADirectoryError:
return AgentErrorObservation(f'Path is a directory: {self.path}. You can only write to files')
except UnicodeDecodeError:
return AgentErrorObservation(f'File could not be decoded as utf-8: {self.path}')
except PermissionError:
return AgentErrorObservation(f'Malformed paths not permitted: {self.path}')
return FileWriteObservation(content='', path=self.path)

View File

@ -1,7 +1,7 @@
from typing import List
from opendevin import config
from opendevin.observation import CmdOutputObservation
from opendevin.observation import CmdOutputObservation, AgentErrorObservation
from opendevin.sandbox import DockerExecBox, DockerSSHBox, Sandbox, LocalBox, E2BBox
from opendevin.schema import ConfigType
from opendevin.action import (
@ -54,17 +54,20 @@ class ActionManager:
observation = await action.run(agent_controller)
return observation
def run_command(self, command: str, background=False) -> CmdOutputObservation:
def run_command(self, command: str, background=False) -> Observation:
if background:
return self._run_background(command)
else:
return self._run_immediately(command)
def _run_immediately(self, command: str) -> CmdOutputObservation:
exit_code, output = self.sandbox.execute(command)
return CmdOutputObservation(
command_id=-1, content=output, command=command, exit_code=exit_code
)
def _run_immediately(self, command: str) -> Observation:
try:
exit_code, output = self.sandbox.execute(command)
return CmdOutputObservation(
command_id=-1, content=output, command=command, exit_code=exit_code
)
except UnicodeDecodeError:
return AgentErrorObservation('Command output could not be decoded as utf-8')
def _run_background(self, command: str) -> CmdOutputObservation:
bg_cmd = self.sandbox.execute_in_background(command)