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]

Enh(tools/agent): State is written to file instead of extracted from stdout

Kilian Lieret4c99935ed179
diff --git a/sweagent/tools/tools.py b/sweagent/tools/tools.py
index 818eaff4..49937ca4 100644
--- a/sweagent/tools/tools.py
+++ b/sweagent/tools/tools.py
@@ -249,23 +249,16 @@ class ToolHandler:
def _get_state(self, state_command: str, env: SWEEnv) -> dict[str, str]:
"""Execute state command in the environment and parse the output as a json object."""
- # Enough to warn, because we're gonna load the output anyway, so that probably catches all real errors
- output = env.communicate(state_command, check="warn").strip()
- if not output:
- self.logger.warning(f"State command {state_command!r} returned empty output")
+ env.communicate(state_command, check="warn")
+ try:
+ state_str = env.read_file(Path("/root/state.json"))
+ except FileNotFoundError:
+ self.logger.warning("State file not found, returning empty state")
return {}
- if not output.startswith("{") or not output.endswith("}"):
- msg = f"State command {state_command!r} returned invalid output: {output!r}. Trying to recover...."
- self.logger.warning(msg)
- try:
- output = output[output.index("{") : output.rindex("}") + 1]
- except ValueError:
- msg = f"Could not find matching braces in {output!r}. Giving up."
- raise ValueError(msg) from None
try:
- state = json.loads(output)
+ state = json.loads(state_str)
except json.JSONDecodeError as e:
- msg = f"State {output!r} is not valid json. This is an internal error, please report it."
+ msg = f"State {state_str!r} is not valid json. This is an internal error, please report it."
raise ValueError(msg) from e
if not isinstance(state, dict):
msg = f"State commands must return a dictionary. Got {state!r} instead."
diff --git a/tools/defaults/bin/_state b/tools/defaults/bin/_state
index a6b1385a..11df9ab2 100644
--- a/tools/defaults/bin/_state
+++ b/tools/defaults/bin/_state
@@ -8,11 +8,18 @@ from registry import registry # type: ignore
def main():
+ state_path = Path("/root/state.json")
+
+ if state_path.exists():
+ state = json.loads(state_path.read_text())
+ else:
+ state = {}
+
current_file = registry.get("CURRENT_FILE")
open_file = "n/a" if not current_file else str(Path(current_file).resolve())
- state = {"open_file": open_file, "working_dir": os.getcwd()}
-
- print(json.dumps(state))
+ state["open_file"] = open_file
+ state["working_dir"] = os.getcwd()
+ state_path.write_text(json.dumps(state))
if __name__ == "__main__":
main()
\ No newline at end of file
diff --git a/tools/defaults/install.sh b/tools/defaults/install.sh
index e9c5facd..3b5adbce 100644
--- a/tools/defaults/install.sh
+++ b/tools/defaults/install.sh
@@ -13,3 +13,5 @@ _write_env "CURRENT_FILE" "${CURRENT_FILE:-}"
# install jq
# apt-get update && apt-get install -y jq
+
+echo "{}" > /root/state.json
\ No newline at end of file
diff --git a/tools/edit_anthropic/bin/_state_anthropic b/tools/edit_anthropic/bin/_state_anthropic
index ac0f2eca..712b7081 100644
--- a/tools/edit_anthropic/bin/_state_anthropic
+++ b/tools/edit_anthropic/bin/_state_anthropic
@@ -1,3 +1,21 @@
-#!/usr/bin/env bash
+#!/usr/bin/env python3
-echo "{\"working_dir\": \"$(pwd)\"}"
+import json
+import os
+from pathlib import Path
+
+
+def main():
+ state_path = Path("/root/state.json")
+ if state_path.exists():
+ state = json.loads(state_path.read_text())
+ else:
+ state = {}
+
+ state["working_dir"] = os.getcwd()
+
+ state_path.write_text(json.dumps(state))
+
+
+if __name__ == "__main__":
+ main()
diff --git a/tools/edit_anthropic/install.sh b/tools/edit_anthropic/install.sh
index ba402655..34e11990 100644
--- a/tools/edit_anthropic/install.sh
+++ b/tools/edit_anthropic/install.sh
@@ -1,2 +1,3 @@
pip install 'tree-sitter==0.21.3'
-pip install 'tree-sitter-languages'
\ No newline at end of file
+pip install 'tree-sitter-languages'
+echo "{}" > /root/state.json