Skip to content

Commit

Permalink
Only create new output file for new commands, preserve file for conti…
Browse files Browse the repository at this point in the history
…nued/timeout commands
  • Loading branch information
openhands-agent committed Nov 21, 2024
1 parent 743e1a4 commit 33abd47
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions openhands/runtime/utils/bash.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,13 +294,15 @@ def _handle_hard_timeout_command(

def execute(self, action: CmdRunAction) -> CmdOutputObservation | ErrorObservation:
"""Execute a command in the bash session."""
# Clean up previous output file if it exists
if self.output_file and self.output_file.exists():
self.output_file.unlink()

# Create new output file for this command
command_id = str(uuid.uuid4())
self.output_file = self.output_dir / f'{command_id}.txt'
# Only create new output file if previous state is None or COMPLETED
if self.prev_status in {None, BashCommandStatus.COMPLETED}:
# Clean up previous output file if it exists
if self.output_file and self.output_file.exists():
self.output_file.unlink()

# Create new output file for this command
command_id = str(uuid.uuid4())
self.output_file = self.output_dir / f'{command_id}.txt'
if action.command.strip() == '' and self.prev_status not in {
BashCommandStatus.CONTINUE,
BashCommandStatus.NO_CHANGE_TIMEOUT,
Expand Down Expand Up @@ -334,9 +336,10 @@ def execute(self, action: CmdRunAction) -> CmdOutputObservation | ErrorObservati
)

if action.command.strip() != '':
# Set up output redirection for this command
self.pane.send_keys(f'exec > >(tee -a "{self.output_file}") 2>&1')
time.sleep(0.1) # Wait for redirection to take effect
# Set up output redirection only when creating new file
if self.prev_status in {None, BashCommandStatus.COMPLETED}:
self.pane.send_keys(f'exec > >(tee -a "{self.output_file}") 2>&1')
time.sleep(0.1) # Wait for redirection to take effect

self.pane.send_keys(
action.command,
Expand Down

0 comments on commit 33abd47

Please sign in to comment.