π OpenHands: AI-Driven Development
fix: better handle bashlex error (#6090)
Xingyao Wangcebd391b7a64
diff --git a/openhands/runtime/utils/bash.py b/openhands/runtime/utils/bash.pyindex 84c5d02659..70a24e2189 100644--- a/openhands/runtime/utils/bash.py+++ b/openhands/runtime/utils/bash.py@@ -1,6 +1,7 @@import osimport reimport time+import tracebackimport uuidfrom enum import Enum@@ -23,11 +24,11 @@ def split_bash_commands(commands):return ['']try:parsed = bashlex.parse(commands)- except bashlex.errors.ParsingError as e:+ except (bashlex.errors.ParsingError, NotImplementedError):logger.debug(f'Failed to parse bash commands\n'f'[input]: {commands}\n'- f'[warning]: {e}\n'+ f'[warning]: {traceback.format_exc()}\n'f'The original command will be returned as is.')# If parsing fails, return the original commands@@ -143,9 +144,13 @@ def escape_bash_special_chars(command: str) -> str:remaining = command[last_pos:]parts.append(remaining)return ''.join(parts)- except bashlex.errors.ParsingError:- # Fallback if parsing fails- logger.warning(f'Failed to parse command: {command}')+ except (bashlex.errors.ParsingError, NotImplementedError):+ logger.debug(+ f'Failed to parse bash commands for special characters escape\n'+ f'[input]: {command}\n'+ f'[warning]: {traceback.format_exc()}\n'+ f'The original command will be returned as is.'+ )return commanddiff --git a/tests/runtime/test_bash.py b/tests/runtime/test_bash.pyindex b97cdbc926..75f9085815 100644--- a/tests/runtime/test_bash.py+++ b/tests/runtime/test_bash.py@@ -153,6 +153,20 @@ def test_multiple_multiline_commands(temp_dir, runtime_cls, run_as_openhands):_close_test_runtime(runtime)+def test_complex_commands(temp_dir, runtime_cls):+ cmd = """count=0; tries=0; while [ $count -lt 3 ]; do result=$(echo "Heads"); tries=$((tries+1)); echo "Flip $tries: $result"; if [ "$result" = "Heads" ]; then count=$((count+1)); else count=0; fi; done; echo "Got 3 heads in a row after $tries flips!";"""++ runtime = _load_runtime(temp_dir, runtime_cls)+ try:+ obs = _run_cmd_action(runtime, cmd)+ logger.info(obs, extra={'msg_type': 'OBSERVATION'})+ assert obs.exit_code == 0, 'The exit code should be 0.'+ assert 'Got 3 heads in a row after 3 flips!' in obs.content++ finally:+ _close_test_runtime(runtime)++def test_no_ps2_in_output(temp_dir, runtime_cls, run_as_openhands):"""Test that the PS2 sign is not added to the output of a multiline command."""runtime = _load_runtime(temp_dir, runtime_cls, run_as_openhands)