Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve command output on timeout #151

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/swerex/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,18 @@ def __init__(self, message: str, *, extra_info: dict[str, Any] = None):
self.extra_info = extra_info


class CommandTimeoutError(SwerexException, RuntimeError, TimeoutError): ...
class CommandTimeoutError(SwerexException, RuntimeError, TimeoutError):
"""Raised when a command times out, but includes any output collected before the timeout.

Attributes:
partial_output (str): The output that was collected before the timeout occurred
"""
def __init__(self, message: str, partial_output: str = "", *, extra_info: dict[str, Any] = None):
super().__init__(message)
self.partial_output = partial_output
if extra_info is None:
extra_info = {}
self.extra_info = extra_info


class NoExitCodeError(SwerexException, RuntimeError): ...
Expand Down
13 changes: 9 additions & 4 deletions src/swerex/runtime/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,12 @@ async def run(self, action: BashAction | BashInterruptAction) -> BashObservation

Raises:
SessionNotInitializedError: If the shell is not initialized.
CommandTimeoutError: If the command times out.
CommandTimeoutError: If the command times out. The error will contain any output collected before the timeout.
NonZeroExitCodeError: If the command has a non-zero exit code and `action.check` is True.
NoExitCodeError: If we cannot get the exit code of the command.

Returns:
BashObservation: The observation of the command.
BashObservation: The observation of the command, including any partial output if a timeout occurred.
"""
if self.shell is None:
msg = "shell not initialized"
Expand All @@ -231,7 +231,11 @@ async def run(self, action: BashAction | BashInterruptAction) -> BashObservation
return await self.interrupt(action)
if action.is_interactive_command or action.is_interactive_quit:
return await self._run_interactive(action)
r = await self._run_normal(action)
try:
r = await self._run_normal(action)
except CommandTimeoutError as e:
# Return partial output on timeout
return BashObservation(output=e.partial_output, exit_code=None, failure_reason="timeout")
if action.check == "raise" and r.exit_code != 0:
msg = (
f"Command {action.command!r} failed with exit code {r.exit_code}. " f"Here is the output:\n{r.output!r}"
Expand Down Expand Up @@ -309,8 +313,9 @@ async def _run_normal(self, action: BashAction) -> BashObservation:
expect_index = self.shell.expect(expect_strings, timeout=action.timeout) # type: ignore
matched_expect_string = expect_strings[expect_index]
except pexpect.TIMEOUT as e:
partial_output = _strip_control_chars(self.shell.before).strip() # type: ignore
msg = f"timeout after {action.timeout} seconds while running command {action.command!r}"
raise CommandTimeoutError(msg) from e
raise CommandTimeoutError(msg, partial_output=partial_output) from e
output: str = _strip_control_chars(self.shell.before).strip() # type: ignore

# Part 3: Get the exit code
Expand Down