Skip to content

Commit

Permalink
Revert "feat: implement keep_prompt handling in BashSession and add t…
Browse files Browse the repository at this point in the history
…ests"

This reverts commit d491e47.
  • Loading branch information
xingyaoww committed Nov 18, 2024
1 parent d491e47 commit d76bbfa
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 49 deletions.
20 changes: 7 additions & 13 deletions openhands/runtime/utils/bash.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,12 @@ def _get_command_output(
raw_command_output: str,
continue_prefix: str = '',
suffix: str = '',
keep_prompt: bool = False,
) -> str:
"""Get the command output with the previous command output removed.
Args:
continue_prefix: The prefix to add to the command output if it's a continuation of the previous command.
suffix: The suffix to add to the command output.
keep_prompt: If True, keep the prompt in the output.
"""
# remove the previous command output from the new output if any
custom_prefix = ''
Expand All @@ -174,12 +172,11 @@ def _get_command_output(
else:
command_output = raw_command_output
self.prev_output = raw_command_output # update current command output anyway
if not keep_prompt:
command_output = _remove_command_prefix(command_output, command)
command_output = _remove_command_prefix(command_output, command)
command_output = f'{custom_prefix}{command_output}{suffix}'
return command_output

def _handle_completed_command(self, command: str, keep_prompt: bool = False) -> CmdOutputObservation:
def _handle_completed_command(self, command: str) -> CmdOutputObservation:
full_output = self._get_pane_content(full=True)

ps1_matches = CmdOutputMetadata.matches_ps1_metadata(full_output)
Expand All @@ -198,7 +195,6 @@ def _handle_completed_command(self, command: str, keep_prompt: bool = False) ->
if not is_special_key
else f'\n\n[The command completed with exit code {metadata.exit_code}. CTRL+{command[-1].upper()} was sent.]'
),
keep_prompt=keep_prompt,
)
self.prev_status = BashCommandStatus.COMPLETED
self.prev_output = '' # Reset previous command output
Expand All @@ -209,7 +205,7 @@ def _handle_completed_command(self, command: str, keep_prompt: bool = False) ->
metadata=metadata,
)

def _handle_nochange_timeout_command(self, command: str, keep_prompt: bool = False) -> CmdOutputObservation:
def _handle_nochange_timeout_command(self, command: str) -> CmdOutputObservation:
self.prev_status = BashCommandStatus.NO_CHANGE_TIMEOUT
full_output = self._get_pane_content(full=True)

Expand All @@ -227,7 +223,6 @@ def _handle_nochange_timeout_command(self, command: str, keep_prompt: bool = Fal
'send other commands to interact with the current process, '
'or send keys to interrupt/kill the command.]'
),
keep_prompt=keep_prompt,
)
return CmdOutputObservation(
content=command_output,
Expand All @@ -236,7 +231,7 @@ def _handle_nochange_timeout_command(self, command: str, keep_prompt: bool = Fal
)

def _handle_hard_timeout_command(
self, command: str, timeout: float, keep_prompt: bool = False
self, command: str, timeout: float
) -> CmdOutputObservation:
self.prev_status = BashCommandStatus.HARD_TIMEOUT
full_output = self._get_pane_content(full=True)
Expand All @@ -254,7 +249,6 @@ def _handle_hard_timeout_command(
'send other commands to interact with the current process, '
'or send keys to interrupt/kill the command.]'
),
keep_prompt=keep_prompt,
)

return CmdOutputObservation(
Expand Down Expand Up @@ -314,7 +308,7 @@ def execute(self, action: CmdRunAction) -> CmdOutputObservation | ErrorObservati
# 1) Execution completed
# if the last command output contains the end marker
if cur_pane_output.rstrip().endswith(CMD_OUTPUT_PS1_END.rstrip()):
return self._handle_completed_command(action.command, action.keep_prompt)
return self._handle_completed_command(action.command)

# 2) Execution timed out since there's no change in output
# for a while (self.NO_CHANGE_TIMEOUT_SECONDS)
Expand All @@ -324,10 +318,10 @@ def execute(self, action: CmdRunAction) -> CmdOutputObservation | ErrorObservati
not action.blocking
and time_since_last_change >= self.NO_CHANGE_TIMEOUT_SECONDS
):
return self._handle_nochange_timeout_command(action.command, action.keep_prompt)
return self._handle_nochange_timeout_command(action.command)

# 3) Execution timed out due to hard timeout
if action.timeout and time.time() - start_time >= action.timeout:
return self._handle_hard_timeout_command(action.command, action.timeout, action.keep_prompt)
return self._handle_hard_timeout_command(action.command, action.timeout)

time.sleep(self.POLL_INTERVAL)
36 changes: 0 additions & 36 deletions tests/unit/test_bash_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,42 +259,6 @@ def test_ansi_escape_codes():
session.close()


def test_keep_prompt():
session = BashSession(work_dir=os.getcwd())

# Test command with keep_prompt=False (default)
obs = session.execute(CmdRunAction('echo "test"'))
logger.info(obs, extra={'msg_type': 'OBSERVATION'})
assert 'test' in obs.content
assert 'echo "test"' not in obs.content
assert obs.metadata.exit_code == 0

# Test command with keep_prompt=True
obs = session.execute(CmdRunAction('echo "test"', keep_prompt=True))
logger.info(obs, extra={'msg_type': 'OBSERVATION'})
assert 'test' in obs.content
assert 'echo "test"' in obs.content
assert obs.metadata.exit_code == 0

# Test long-running command with keep_prompt=True
obs = session.execute(
CmdRunAction('for i in {1..2}; do echo $i; sleep 3; done', keep_prompt=True, blocking=False)
)
logger.info(obs, extra={'msg_type': 'OBSERVATION'})
assert 'for i in {1..2}; do echo $i; sleep 3; done' in obs.content
assert '1' in obs.content
assert obs.metadata.exit_code == -1

# Continue watching output with keep_prompt=True
obs = session.execute(CmdRunAction('', keep_prompt=True))
logger.info(obs, extra={'msg_type': 'OBSERVATION'})
assert '[Command output continued from previous command]' in obs.content
assert '2' in obs.content
assert obs.metadata.exit_code == -1

session.close()


def test_long_output():
session = BashSession(work_dir=os.getcwd())

Expand Down

0 comments on commit d76bbfa

Please sign in to comment.