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): Read patch from file instead of stdout

Kilian Lieret7d7e38dc3c3e
diff --git a/sweagent/agent/agents.py b/sweagent/agent/agents.py
index 4f1d703d..02f646e1 100644
--- a/sweagent/agent/agents.py
+++ b/sweagent/agent/agents.py
@@ -641,8 +641,10 @@ class Agent:
"""
step = step.model_copy()
assert self.tools is not None
- submission = self.tools.parse_submission_cmd_output(observation or step.observation)
- if submission is not None:
+ is_submission = self.tools.check_for_submission_cmd(observation or step.observation)
+ if is_submission:
+ assert self._env is not None
+ submission = self._env.read_file("/root/model.patch")
if submission.strip() != "":
step.submission = submission
else:
diff --git a/sweagent/environment/swe_env.py b/sweagent/environment/swe_env.py
index bd3340c0..eabeedaa 100644
--- a/sweagent/environment/swe_env.py
+++ b/sweagent/environment/swe_env.py
@@ -7,7 +7,7 @@ from typing import Literal, Self
from pydantic import BaseModel, ConfigDict, Field
from swerex.deployment.abstract import AbstractDeployment
from swerex.deployment.config import DeploymentConfig, DockerDeploymentConfig, get_deployment
-from swerex.runtime.abstract import BashAction, BashInterruptAction, CreateBashSessionRequest
+from swerex.runtime.abstract import BashAction, BashInterruptAction, CreateBashSessionRequest, ReadFileRequest
from sweagent.environment.hooks.abstract import CombinedEnvHooks, EnvHook
from sweagent.environment.repo import Repo, RepoConfig
@@ -219,22 +219,17 @@ class SWEEnv:
)
return output
- # todo: Use the runtime for this instead
def read_file(self, path: str | PurePath) -> str:
"""Read file contents from container
Args:
- path: Path to file relative to repository root
+ path: Absolute path to file
Returns:
file_contents: Contents of file as string
"""
- if self.repo is None:
- msg = "Repository not set, cannot read file"
- raise ValueError(msg)
-
- path_in_container = f"/{self.repo.repo_name}/{path}"
- return self.communicate(f"cat {str(path_in_container)}")
+ r = asyncio.run(self.deployment.runtime.read_file(ReadFileRequest(path=str(path))))
+ return r.content
def set_env_variables(self, env_variables: dict[str, str]) -> None:
"""Set environment variables in the environment."""
diff --git a/sweagent/tools/tools.py b/sweagent/tools/tools.py
index 81de77d3..818eaff4 100644
--- a/sweagent/tools/tools.py
+++ b/sweagent/tools/tools.py
@@ -319,20 +319,11 @@ class ToolHandler:
# Parsing & multiline commands
# -----------------------------
- def parse_submission_cmd_output(self, output: str) -> str | None:
- """Function for extracting diff patch submission at the end of an episode.
-
- Args:
- output: `submit` observation
-
- Returns:
- submission: diff patch submission or None if no submission was found
- """
- pattern = r"\<\<SUBMISSION\|\|(.*)\|\|SUBMISSION\>\>"
- match = re.search(pattern, output, re.DOTALL)
- if match is None:
- return None
- return match.group(1)
+ def check_for_submission_cmd(self, output: str) -> bool:
+ """Function for checking submission request."""
+ if r"<<SWE_AGENT_SUBMISSION>>" in output:
+ return True
+ return False
def parse_actions(self, output: dict) -> tuple[str, str]:
"""Parse the model output into a thought and action."""
diff --git a/tools/review_on_submit/bin/submit b/tools/review_on_submit/bin/submit
index 0d6b2be3..d44b8542 100644
--- a/tools/review_on_submit/bin/submit
+++ b/tools/review_on_submit/bin/submit
@@ -37,9 +37,9 @@ def main() -> None:
print(message)
sys.exit(0)
- print("<<SUBMISSION||")
+ print("<<SWE_AGENT_SUBMISSION>>")
print(patch)
- print("||SUBMISSION>>")
+ print("<<SWE_AGENT_SUBMISSION>>")
patch_path.unlink()
diff --git a/tools/submit/bin/submit b/tools/submit/bin/submit
index 667ef74d..57a8928a 100644
--- a/tools/submit/bin/submit
+++ b/tools/submit/bin/submit
@@ -8,10 +8,10 @@ main() {
fi
git add -A
- git diff --cached > model.patch
- echo "<<SUBMISSION||"
- cat model.patch
- echo "||SUBMISSION>>"
+ git diff --cached > /root/model.patch
+ echo "<<SWE_AGENT_SUBMISSION>>"
+ cat /root/model.patch
+ echo "<<SWE_AGENT_SUBMISSION>>"
}
main "$@"