Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix(codeact): make sure agent sees the prefix/suffix as part of observation (#6400)

Xingyao Wangb468150f2abf
diff --git a/openhands/agenthub/codeact_agent/codeact_agent.py b/openhands/agenthub/codeact_agent/codeact_agent.py
index 37c5285514..ecb756781a 100644
--- a/openhands/agenthub/codeact_agent/codeact_agent.py
+++ b/openhands/agenthub/codeact_agent/codeact_agent.py
@@ -277,16 +277,11 @@ class CodeActAgent(Agent):
# if it doesn't have tool call metadata, it was triggered by a user action
if obs.tool_call_metadata is None:
text = truncate_content(
- f'\nObserved result of command executed by user:\n{obs.content}',
+ f'\nObserved result of command executed by user:\n{obs.to_agent_observation()}',
max_message_chars,
)
else:
- text = truncate_content(
- obs.content
- + f'\n[Python Interpreter: {obs.metadata.py_interpreter_path}]',
- max_message_chars,
- )
- text += f'\n[Command finished with exit code {obs.exit_code}]'
+ text = truncate_content(obs.to_agent_observation(), max_message_chars)
message = Message(role='user', content=[TextContent(text=text)])
elif isinstance(obs, IPythonRunCellObservation):
text = obs.content
diff --git a/openhands/events/observation/commands.py b/openhands/events/observation/commands.py
index 31b4472f7e..edddb250cd 100644
--- a/openhands/events/observation/commands.py
+++ b/openhands/events/observation/commands.py
@@ -149,16 +149,18 @@ class CmdOutputObservation(Observation):
f'**CmdOutputObservation (source={self.source}, exit code={self.exit_code}, '
f'metadata={json.dumps(self.metadata.model_dump(), indent=2)})**\n'
'--BEGIN AGENT OBSERVATION--\n'
- f'{self._to_agent_observation()}\n'
+ f'{self.to_agent_observation()}\n'
'--END AGENT OBSERVATION--'
)
- def _to_agent_observation(self) -> str:
+ def to_agent_observation(self) -> str:
ret = f'{self.metadata.prefix}{self.content}{self.metadata.suffix}'
if self.metadata.working_dir:
ret += f'\n[Current working directory: {self.metadata.working_dir}]'
if self.metadata.py_interpreter_path:
ret += f'\n[Python interpreter: {self.metadata.py_interpreter_path}]'
+ if self.metadata.exit_code != -1:
+ ret += f'\n[Command finished with exit code {self.metadata.exit_code}]'
return ret
diff --git a/tests/unit/test_codeact_agent.py b/tests/unit/test_codeact_agent.py
index 26fa442882..39badebff0 100644
--- a/tests/unit/test_codeact_agent.py
+++ b/tests/unit/test_codeact_agent.py
@@ -46,7 +46,7 @@ def agent() -> CodeActAgent:
agent = CodeActAgent(llm=LLM(LLMConfig()), config=config)
agent.llm = Mock()
agent.llm.config = Mock()
- agent.llm.config.max_message_chars = 100
+ agent.llm.config.max_message_chars = 1000
return agent
@@ -65,10 +65,15 @@ def test_cmd_output_observation_message(agent: CodeActAgent):
content='Command output',
metadata=CmdOutputMetadata(
exit_code=0,
+ prefix='[THIS IS PREFIX]',
+ suffix='[THIS IS SUFFIX]',
),
)
- results = agent.get_observation_message(obs, tool_call_id_to_message={})
+ tool_call_id_to_message = {}
+ results = agent.get_observation_message(
+ obs, tool_call_id_to_message=tool_call_id_to_message
+ )
assert len(results) == 1
result = results[0]
@@ -76,8 +81,10 @@ def test_cmd_output_observation_message(agent: CodeActAgent):
assert result.role == 'user'
assert len(result.content) == 1
assert isinstance(result.content[0], TextContent)
- assert 'Command output' in result.content[0].text
+ assert 'Observed result of command executed by user:' in result.content[0].text
assert '[Command finished with exit code 0]' in result.content[0].text
+ assert '[THIS IS PREFIX]' in result.content[0].text
+ assert '[THIS IS SUFFIX]' in result.content[0].text
def test_ipython_run_cell_observation_message(agent: CodeActAgent):