Skip to content

SWE-agent takes a GitHub issue and tries to automatically fix it, using your LM of choice. It can also be employed for offensive cybersecurity or competitive coding challenges. [NeurIPS 2024]

CI: Properly test interactive commands

Kilian Lieret3a3d1cf4661b
diff --git a/sweagent/agent/agents.py b/sweagent/agent/agents.py
index 43e134fd..1f67c38d 100644
--- a/sweagent/agent/agents.py
+++ b/sweagent/agent/agents.py
@@ -827,6 +827,9 @@ class Agent:
self.set_environment_vars(env, self.config.env_variables)
def set_environment_vars(self, env: SWEEnv, env_variables: dict[str, Any]) -> None:
+ """Sets environment variables in the container and for example makes sure
+ that all the commands are available in the PATH on the container.
+ """
assert self.config is not None # mypy
commands_to_execute = (
[self.config.state_command.code]
diff --git a/sweagent/agent/interactive_commands.py b/sweagent/agent/interactive_commands.py
index c3fc0b37..c8199ff3 100644
--- a/sweagent/agent/interactive_commands.py
+++ b/sweagent/agent/interactive_commands.py
@@ -229,10 +229,14 @@ class InteractiveSession:
def get_interactive_session(
ctr_name: str, ctr_obj, cwd: str, session_name: str, config: InteractiveSessionConfig, logger: logging.Logger
-) -> InteractiveSession:
+) -> tuple[str, InteractiveSession]:
"""
Starts a new interactive session on the given container name.
Returns a subprocess.Popen object that is available for further read/writes for submitting commands and reading output.
+
+ Returns:
+ observation: observation from starting the interactive session
+ session: InteractiveSession object
"""
startup_cmd = [
"docker",
@@ -248,8 +252,8 @@ def get_interactive_session(
startup_cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, bufsize=1
)
time.sleep(DOCKER_START_UP_DELAY)
- _ = read_with_timeout(session, lambda: list(), timeout_duration=1)
- return InteractiveSession(
+ observation = read_with_timeout(session, lambda: list(), timeout_duration=1)
+ return observation, InteractiveSession(
name=session_name,
session_process=session,
config=config,
diff --git a/sweagent/environment/swe_env.py b/sweagent/environment/swe_env.py
index 754759db..59e7d4a9 100644
--- a/sweagent/environment/swe_env.py
+++ b/sweagent/environment/swe_env.py
@@ -527,7 +527,7 @@ class SWEEnv(gym.Env):
if self.interactive_session is not None:
return self.interactive_session._get_only_one_interactive_error_message_observation()
assert self.container_name is not None
- self.interactive_session = get_interactive_session(
+ _observation, self.interactive_session = get_interactive_session(
ctr_name=self.container_name,
ctr_obj=self.container_obj,
cwd="/" + self._repo_name,
@@ -535,6 +535,7 @@ class SWEEnv(gym.Env):
config=self.args.interactive_sessions_config[session_name],
logger=self.logger,
)
+ observation += _observation
elif command == "STOP":
if self.interactive_session is None:
observation = f"Interactive session {session_name!r} is not running, so it cannot be stopped!"
diff --git a/tests/test_commands/_interactive_dummy.py b/tests/test_commands/_interactive_dummy.py
index 62f8e710..6eff5956 100644
--- a/tests/test_commands/_interactive_dummy.py
+++ b/tests/test_commands/_interactive_dummy.py
@@ -12,7 +12,7 @@ class InteractiveDummyCommand:
def send(self, input: str):
print(f"Received input: {input}")
- time.sleep(2)
+ time.sleep(0.5)
def stop(self):
print("Stopped interactive dummy command")
diff --git a/tests/test_run.py b/tests/test_run.py
index 72fd5f03..40f218ec 100644
--- a/tests/test_run.py
+++ b/tests/test_run.py
@@ -201,3 +201,35 @@ def test_agent_persistent_container(test_script_args: ScriptArguments, capsys):
text = captured.out + captured.err
assert "Trying to clone from non-mirror..." in text
assert "Falling back to full cloning method" in text
+
+
+def test_dummy_interactive_session(test_script_args: ScriptArguments):
+ test_script_args = dataclasses.replace(
+ test_script_args,
+ agent=AgentArguments(
+ model=ModelArguments(
+ model_name="instant_empty_submit",
+ ),
+ config_file=Path("tests", "test_data", "config_files", "dummy_interactive.yaml"),
+ ),
+ )
+ print(test_script_args.agent.config.command_docs) # type: ignore
+ main = Main(test_script_args)
+ env = main.env
+ env.reset()
+ main.agent.set_environment_vars(env, {})
+ action_obs = [
+ ("doesntexit", "command not found"),
+ ("dummy_stop", "is not running"),
+ ("dummy_send", "is not running"),
+ ("dummy_start", "Started interactive dummy command"),
+ ("dummy_start", "Interactive session already open"),
+ ("dummy_send asdf", "asdf"),
+ ("dummy_stop", "stopped successfully"),
+ ("dummy_stop", "is not running"),
+ ]
+ for action, expected_observation in action_obs:
+ observation, *_ = env.step(action)
+ assert observation is not None
+ assert expected_observation in observation, observation
+ env.close()