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]

Dummy interact. cmd for testing; fixes after refactor

Kilian Lieret777da10071f6
diff --git a/sweagent/agent/interactive_commands.py b/sweagent/agent/interactive_commands.py
index 7ab0e7fd..c3fc0b37 100644
--- a/sweagent/agent/interactive_commands.py
+++ b/sweagent/agent/interactive_commands.py
@@ -46,6 +46,13 @@ INTERACTIVE_SESSIONS_CONFIG = {
exit_command="connect_stop",
quit_commands_in_session=["quit"],
),
+ "dummy": InteractiveSessionConfig(
+ cmdline="/root/commands/_interactive_dummy",
+ terminal_prompt_pattern="(dummy) ",
+ start_command="dummy_start",
+ exit_command="dummy_stop",
+ quit_commands_in_session=["stop"],
+ ),
}
@@ -125,9 +132,9 @@ class InteractiveSession:
try:
cmd = input if input.endswith("\n") else input + "\n"
- os.write(self.interactive_session.session_process.stdin.fileno(), cmd.encode()) # type: ignore
+ os.write(self.session_process.stdin.fileno(), cmd.encode()) # type: ignore
time.sleep(0.03)
- self.interactive_session.session_process.stdin.flush() # type: ignore
+ self.session_process.stdin.flush() # type: ignore
except BrokenPipeError:
traceback.print_exc()
self.logger.error("Failed to communicate with session. Check docker logs for more information.")
diff --git a/sweagent/environment/swe_env.py b/sweagent/environment/swe_env.py
index 5c76b31a..754759db 100644
--- a/sweagent/environment/swe_env.py
+++ b/sweagent/environment/swe_env.py
@@ -537,21 +537,21 @@ class SWEEnv(gym.Env):
)
elif command == "STOP":
if self.interactive_session is None:
- observation = f"Interactive session {session_name} is not running, so it cannot be stopped!"
+ observation = f"Interactive session {session_name!r} is not running, so it cannot be stopped!"
else:
if self.interactive_session.session_process.poll() is None:
self.logger.warning("Session did not quit successfully, terminating.")
self.interactive_session.session_process.terminate()
- observation = f"Interactive session {session_name} stopped successfully"
+ observation = f"Interactive session {session_name!r} stopped successfully"
self.interactive_session = None
else:
if self.interactive_session is None:
self.logger.warning("Tried to run interactive commands without starting session")
start_command = self.args.interactive_sessions_config[session_name].start_command
- observation = f"Interactive session {session_name} is not running! please start it first using `{start_command}`"
+ observation = f"Interactive session {session_name!r} is not running! please start it first using `{start_command}`"
elif self.interactive_session and self.interactive_session.session_process.poll() is not None:
start_command = self.args.interactive_sessions_config[session_name].start_command
- observation = f"Interactive session {session_name} was unexpectedly closed! Please start it again using `{start_command}`"
+ observation = f"Interactive session {session_name!r} was unexpectedly closed! Please start it again using `{start_command}`"
self._terminate_interactive_session(session_name=session_name)
else:
_observation, terminate = self.interactive_session.communicate_with_handling(
diff --git a/tests/test_commands/_interactive_dummy.py b/tests/test_commands/_interactive_dummy.py
new file mode 100644
index 00000000..62f8e710
--- /dev/null
+++ b/tests/test_commands/_interactive_dummy.py
@@ -0,0 +1,35 @@
+#!/usr/bin/env python3
+from __future__ import annotations
+
+import time
+
+
+class InteractiveDummyCommand:
+ PROMPT = "(dummy) "
+
+ def start(self):
+ print("Started interactive dummy command")
+
+ def send(self, input: str):
+ print(f"Received input: {input}")
+ time.sleep(2)
+
+ def stop(self):
+ print("Stopped interactive dummy command")
+
+ def __call__(self):
+ self.start()
+ while True:
+ inpt = input(self.PROMPT)
+ cmd, _, args = inpt.partition(" ")
+ if cmd == "stop":
+ self.stop()
+ break
+ if cmd == "send":
+ self.send(args)
+ else:
+ print(f"Unknown command: {cmd}")
+
+
+if __name__ == "__main__":
+ InteractiveDummyCommand()()
diff --git a/tests/test_commands/interactive_dummy_wrapper.sh b/tests/test_commands/interactive_dummy_wrapper.sh
new file mode 100644
index 00000000..003b1f56
--- /dev/null
+++ b/tests/test_commands/interactive_dummy_wrapper.sh
@@ -0,0 +1,29 @@
+_debug_command() {
+ echo "<<INTERACTIVE||$@||INTERACTIVE>>"
+}
+
+
+# @yaml
+# signature: dummy_start
+# docstring:
+dummy_start() {
+ _debug_command "SESSION=dummy"
+ _debug_command "START"
+}
+
+# @yaml
+# signature: dummy_stop
+# docstring:
+dummy_stop() {
+ _debug_command "SESSION=dummy"
+ _debug_command "stop"
+ _debug_command "STOP"
+}
+
+# @yaml
+# signature: dummy_send <input>
+# docstring:
+dummy_send() {
+ _debug_command "SESSION=dummy"
+ _debug_command "send $@"
+}
\ No newline at end of file
diff --git a/tests/test_data/config_files/dummy_interactive.yaml b/tests/test_data/config_files/dummy_interactive.yaml
new file mode 100644
index 00000000..1e22ec1a
--- /dev/null
+++ b/tests/test_data/config_files/dummy_interactive.yaml
@@ -0,0 +1,60 @@
+system_template: |-
+ SETTING
+
+ COMMANDS:
+ {command_docs}
+instance_template: |-
+ (Open file: {open_file})
+ (Current directory: {working_dir})
+ (Interactive session: {interactive_session})
+ bash-$
+next_step_template: |-
+ {observation}
+ (Open file: {open_file})
+ (Current directory: {working_dir})
+ (Interactive session: {interactive_session})
+ bash-$
+next_step_no_output_template: |-
+ Your command ran successfully and did not produce any output.
+ (Open file: {open_file})
+ (Current directory: {working_dir})
+ (Interactive session: {interactive_session})
+ bash-$
+state_command:
+ name: state
+ code: |
+ state() {
+ local working_dir="$PWD";
+ local open_file="n/a";
+ local interactive_session="${INTERACTIVE_SESSION:-n/a}";
+ if [ ! -z $CURRENT_FILE ]; then
+ open_file=$(realpath $CURRENT_FILE);
+ fi
+
+ echo '{"open_file": "'$open_file'", "working_dir": "'$working_dir'", "interactive_session": "'$interactive_session'"}'
+ };
+parse_function: ThoughtActionParser
+env_variables:
+ WINDOW: 100
+ OVERLAP: 2
+ CURRENT_LINE: 0
+ CURRENT_FILE: ''
+ SEARCH_RESULTS: ()
+ SEARCH_FILES: ()
+ SEARCH_INDEX: 0
+command_files:
+- config/commands/defaults.sh
+- tests/test_commands/interactive_dummy_wrapper.sh
+- tests/test_commands/_interactive_dummy.py
+parse_command: ParseCommandDetailed
+history_processor: Last5Observations
+blocklist:
+- vim
+- vi
+- emacs
+- nano
+- nohup
+- gdb
+blocklist_error_template: |
+ Interactive operation '{name}' is not supported by this environment.
+ Please consider using one of the interactive commands available to you in this environment.